Blame resolv/tst-resolv-res_ninit.c

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