tst-nss-gai-actions.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* Test continue and merge NSS actions for getaddrinfo.
  2. Copyright The GNU Toolchain Authors.
  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. <https://www.gnu.org/licenses/>. */
  15. #include <dlfcn.h>
  16. #include <gnu/lib-names.h>
  17. #include <nss.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <support/check.h>
  22. #include <support/format_nss.h>
  23. #include <support/support.h>
  24. #include <support/xstdio.h>
  25. #include <support/xunistd.h>
  26. enum
  27. {
  28. ACTION_MERGE = 0,
  29. ACTION_CONTINUE,
  30. };
  31. static const char *
  32. family_str (int family)
  33. {
  34. switch (family)
  35. {
  36. case AF_UNSPEC:
  37. return "AF_UNSPEC";
  38. case AF_INET:
  39. return "AF_INET";
  40. default:
  41. __builtin_unreachable ();
  42. }
  43. }
  44. static const char *
  45. action_str (int action)
  46. {
  47. switch (action)
  48. {
  49. case ACTION_MERGE:
  50. return "merge";
  51. case ACTION_CONTINUE:
  52. return "continue";
  53. default:
  54. __builtin_unreachable ();
  55. }
  56. }
  57. static void
  58. do_one_test (int action, int family, bool canon)
  59. {
  60. struct addrinfo hints =
  61. {
  62. .ai_family = family,
  63. };
  64. struct addrinfo *ai;
  65. if (canon)
  66. hints.ai_flags = AI_CANONNAME;
  67. printf ("***** Testing \"files [SUCCESS=%s] files\" for family %s, %s\n",
  68. action_str (action), family_str (family),
  69. canon ? "AI_CANONNAME" : "");
  70. int ret = getaddrinfo ("example.org", "80", &hints, &ai);
  71. switch (action)
  72. {
  73. case ACTION_MERGE:
  74. if (ret == 0)
  75. {
  76. char *formatted = support_format_addrinfo (ai, ret);
  77. printf ("merge unexpectedly succeeded:\n %s\n", formatted);
  78. support_record_failure ();
  79. free (formatted);
  80. break;
  81. }
  82. else
  83. return;
  84. case ACTION_CONTINUE:
  85. {
  86. char *formatted = support_format_addrinfo (ai, ret);
  87. /* Verify that the result appears exactly once. */
  88. const char *expected = "address: STREAM/TCP 192.0.0.1 80\n"
  89. "address: DGRAM/UDP 192.0.0.1 80\n"
  90. "address: RAW/IP 192.0.0.1 80\n";
  91. const char *contains = strstr (formatted, expected);
  92. const char *contains2 = NULL;
  93. if (contains != NULL)
  94. contains2 = strstr (contains + strlen (expected), expected);
  95. if (contains == NULL || contains2 != NULL)
  96. {
  97. printf ("continue failed:\n%s\n", formatted);
  98. support_record_failure ();
  99. }
  100. free (formatted);
  101. break;
  102. }
  103. default:
  104. __builtin_unreachable ();
  105. }
  106. }
  107. static void
  108. do_one_test_set (int action)
  109. {
  110. char buf[32];
  111. snprintf (buf, sizeof (buf), "files [SUCCESS=%s] files",
  112. action_str (action));
  113. __nss_configure_lookup ("hosts", buf);
  114. do_one_test (action, AF_UNSPEC, false);
  115. do_one_test (action, AF_INET, false);
  116. do_one_test (action, AF_INET, true);
  117. }
  118. static int
  119. do_test (void)
  120. {
  121. do_one_test_set (ACTION_CONTINUE);
  122. do_one_test_set (ACTION_MERGE);
  123. return 0;
  124. }
  125. #include <support/test-driver.c>