tst-resolv-res_ninit.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* Test the creation of many struct __res_state objects.
  2. Copyright (C) 2017-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <mcheck.h>
  16. #include <resolv.h>
  17. #include <resolv/resolv_context.h>
  18. #include <stdlib.h>
  19. #include <support/check.h>
  20. /* Order the resolver states by their extended resolver state
  21. index. */
  22. static int
  23. sort_res_state (const void *a, const void *b)
  24. {
  25. res_state left = (res_state) a;
  26. res_state right = (res_state) b;
  27. return memcmp (&left->_u._ext.__glibc_extension_index,
  28. &right->_u._ext.__glibc_extension_index,
  29. sizeof (left->_u._ext.__glibc_extension_index));
  30. }
  31. static int
  32. do_test (void)
  33. {
  34. mtrace ();
  35. enum { count = 100 * 1000 };
  36. res_state array = calloc (count, sizeof (*array));
  37. const struct resolv_conf *conf = NULL;
  38. for (size_t i = 0; i < count; ++i)
  39. {
  40. TEST_VERIFY (res_ninit (array + i) == 0);
  41. TEST_VERIFY (array[i].nscount > 0);
  42. struct resolv_context *ctx = __resolv_context_get_override (array + i);
  43. TEST_VERIFY_EXIT (ctx != NULL);
  44. TEST_VERIFY (ctx->resp == array + i);
  45. if (i == 0)
  46. {
  47. conf = ctx->conf;
  48. TEST_VERIFY (conf != NULL);
  49. }
  50. else
  51. /* The underyling configuration should be identical across all
  52. res_state opjects because resolv.conf did not change. */
  53. TEST_VERIFY (ctx->conf == conf);
  54. }
  55. qsort (array, count, sizeof (*array), sort_res_state);
  56. for (size_t i = 1; i < count; ++i)
  57. /* All extension indices should be different. */
  58. TEST_VERIFY (sort_res_state (array + i - 1, array + i) < 0);
  59. for (size_t i = 0; i < count; ++i)
  60. res_nclose (array + i);
  61. free (array);
  62. TEST_VERIFY (res_init () == 0);
  63. return 0;
  64. }
  65. #define TIMEOUT 50
  66. #include <support/test-driver.c>