Blame resolv/tst-resolv-res_ninit.c

Packit Service 82fcde
/* Test the creation of many struct __res_state objects.
Packit Service 82fcde
   Copyright (C) 2017-2018 Free Software Foundation, Inc.
Packit Service 82fcde
   This file is part of the GNU C Library.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 82fcde
   modify it under the terms of the GNU Lesser General Public
Packit Service 82fcde
   License as published by the Free Software Foundation; either
Packit Service 82fcde
   version 2.1 of the License, or (at your option) any later version.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 82fcde
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 82fcde
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 82fcde
   Lesser General Public License for more details.
Packit Service 82fcde
Packit Service 82fcde
   You should have received a copy of the GNU Lesser General Public
Packit Service 82fcde
   License along with the GNU C Library; if not, see
Packit Service 82fcde
   <http://www.gnu.org/licenses/>.  */
Packit Service 82fcde
Packit Service 82fcde
#include <mcheck.h>
Packit Service 82fcde
#include <resolv.h>
Packit Service 82fcde
#include <resolv/resolv_context.h>
Packit Service 82fcde
#include <stdlib.h>
Packit Service 82fcde
#include <support/check.h>
Packit Service 82fcde
Packit Service 82fcde
/* Order the resolver states by their extended resolver state
Packit Service 82fcde
   index.  */
Packit Service 82fcde
static int
Packit Service 82fcde
sort_res_state (const void *a, const void *b)
Packit Service 82fcde
{
Packit Service 82fcde
  res_state left = (res_state) a;
Packit Service 82fcde
  res_state right = (res_state) b;
Packit Service 82fcde
  return memcmp (&left->_u._ext.__glibc_extension_index,
Packit Service 82fcde
                 &right->_u._ext.__glibc_extension_index,
Packit Service 82fcde
                 sizeof (left->_u._ext.__glibc_extension_index));
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
static int
Packit Service 82fcde
do_test (void)
Packit Service 82fcde
{
Packit Service 82fcde
  mtrace ();
Packit Service 82fcde
Packit Service 82fcde
  enum { count = 100 * 1000 };
Packit Service 82fcde
  res_state array = calloc (count, sizeof (*array));
Packit Service 82fcde
  const struct resolv_conf *conf = NULL;
Packit Service 82fcde
  for (size_t i = 0; i < count; ++i)
Packit Service 82fcde
    {
Packit Service 82fcde
      TEST_VERIFY (res_ninit (array + i) == 0);
Packit Service 82fcde
      TEST_VERIFY (array[i].nscount > 0);
Packit Service 82fcde
      struct resolv_context *ctx = __resolv_context_get_override (array + i);
Packit Service 82fcde
      TEST_VERIFY_EXIT (ctx != NULL);
Packit Service 82fcde
      TEST_VERIFY (ctx->resp == array + i);
Packit Service 82fcde
      if (i == 0)
Packit Service 82fcde
        {
Packit Service 82fcde
          conf = ctx->conf;
Packit Service 82fcde
          TEST_VERIFY (conf != NULL);
Packit Service 82fcde
        }
Packit Service 82fcde
      else
Packit Service 82fcde
        /* The underyling configuration should be identical across all
Packit Service 82fcde
           res_state opjects because resolv.conf did not change.  */
Packit Service 82fcde
        TEST_VERIFY (ctx->conf == conf);
Packit Service 82fcde
    }
Packit Service 82fcde
  qsort (array, count, sizeof (*array), sort_res_state);
Packit Service 82fcde
  for (size_t i = 1; i < count; ++i)
Packit Service 82fcde
    /* All extension indices should be different.  */
Packit Service 82fcde
    TEST_VERIFY (sort_res_state (array + i - 1, array + i) < 0);
Packit Service 82fcde
  for (size_t i = 0; i < count; ++i)
Packit Service 82fcde
    res_nclose (array + i);
Packit Service 82fcde
  free (array);
Packit Service 82fcde
Packit Service 82fcde
  TEST_VERIFY (res_init () == 0);
Packit Service 82fcde
  return 0;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
#define TIMEOUT 50
Packit Service 82fcde
#include <support/test-driver.c>