tst-nss-gai-hv2-canonname.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* Test NSS query path for plugins that only implement gethostbyname2
  2. (#30843).
  3. Copyright The GNU Toolchain Authors.
  4. This file is part of the GNU C Library.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <https://www.gnu.org/licenses/>. */
  16. #include <nss.h>
  17. #include <netdb.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <mcheck.h>
  21. #include <support/check.h>
  22. #include <support/xstdio.h>
  23. #include "nss/tst-nss-gai-hv2-canonname.h"
  24. #define PREPARE do_prepare
  25. static void do_prepare (int a, char **av)
  26. {
  27. FILE *hosts = xfopen ("/etc/hosts", "w");
  28. for (unsigned i = 2; i < 255; i++)
  29. {
  30. fprintf (hosts, "ff01::ff02:ff03:%u:2\ttest.example.com\n", i);
  31. fprintf (hosts, "192.168.0.%u\ttest.example.com\n", i);
  32. }
  33. xfclose (hosts);
  34. }
  35. static int
  36. do_test (void)
  37. {
  38. mtrace ();
  39. __nss_configure_lookup ("hosts", "test_gai_hv2_canonname");
  40. struct addrinfo hints = {};
  41. struct addrinfo *result = NULL;
  42. hints.ai_family = AF_INET6;
  43. hints.ai_flags = AI_ALL | AI_V4MAPPED | AI_CANONNAME;
  44. int ret = getaddrinfo (QUERYNAME, NULL, &hints, &result);
  45. if (ret != 0)
  46. FAIL_EXIT1 ("getaddrinfo failed: %s\n", gai_strerror (ret));
  47. TEST_COMPARE_STRING (result->ai_canonname, QUERYNAME);
  48. freeaddrinfo(result);
  49. return 0;
  50. }
  51. #include <support/test-driver.c>