PageRenderTime 35ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/resolv/tst-resolv-trailing.c

https://github.com/hjl-tools/glibc
C | 136 lines | 104 code | 14 blank | 18 comment | 8 complexity | e500446538e16dc4c7de66895ff960b5 MD5 | raw file
  1. /* Test name resolution behavior with trailing characters.
  2. Copyright (C) 2019-2020 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. <https://www.gnu.org/licenses/>. */
  15. #include <array_length.h>
  16. #include <netdb.h>
  17. #include <support/check.h>
  18. #include <support/check_nss.h>
  19. #include <support/resolv_test.h>
  20. #include <support/support.h>
  21. static void
  22. response (const struct resolv_response_context *ctx,
  23. struct resolv_response_builder *b,
  24. const char *qname, uint16_t qclass, uint16_t qtype)
  25. {
  26. /* The tests are not supposed send any DNS queries. */
  27. FAIL_EXIT1 ("unexpected DNS query for %s/%d/%d", qname, qclass, qtype);
  28. }
  29. static int
  30. do_test (void)
  31. {
  32. struct resolv_test *aux = resolv_test_start
  33. ((struct resolv_redirect_config)
  34. {
  35. .response_callback = response,
  36. });
  37. static const char *const queries[] =
  38. {
  39. "192.0.2.1 ",
  40. "192.0.2.2\t",
  41. "192.0.2.3\n",
  42. "192.0.2.4 X",
  43. "192.0.2.5\tY",
  44. "192.0.2.6\nZ",
  45. "192.0.2. ",
  46. "192.0.2.\t",
  47. "192.0.2.\n",
  48. "192.0.2. X",
  49. "192.0.2.\tY",
  50. "192.0.2.\nZ",
  51. "2001:db8::1 ",
  52. "2001:db8::2\t",
  53. "2001:db8::3\n",
  54. "2001:db8::4 X",
  55. "2001:db8::5\tY",
  56. "2001:db8::6\nZ",
  57. };
  58. for (size_t query_idx = 0; query_idx < array_length (queries); ++query_idx)
  59. {
  60. const char *query = queries[query_idx];
  61. struct hostent storage;
  62. char buf[4096];
  63. struct hostent *e;
  64. h_errno = 0;
  65. TEST_VERIFY (gethostbyname (query) == NULL);
  66. TEST_COMPARE (h_errno, HOST_NOT_FOUND);
  67. h_errno = 0;
  68. e = NULL;
  69. TEST_COMPARE (gethostbyname_r (query, &storage, buf, sizeof (buf),
  70. &e, &h_errno), 0);
  71. TEST_VERIFY (e == NULL);
  72. TEST_COMPARE (h_errno, HOST_NOT_FOUND);
  73. h_errno = 0;
  74. TEST_VERIFY (gethostbyname2 (query, AF_INET) == NULL);
  75. TEST_COMPARE (h_errno, HOST_NOT_FOUND);
  76. h_errno = 0;
  77. e = NULL;
  78. TEST_COMPARE (gethostbyname2_r (query, AF_INET,
  79. &storage, buf, sizeof (buf),
  80. &e, &h_errno), 0);
  81. TEST_VERIFY (e == NULL);
  82. TEST_COMPARE (h_errno, HOST_NOT_FOUND);
  83. h_errno = 0;
  84. TEST_VERIFY (gethostbyname2 (query, AF_INET6) == NULL);
  85. TEST_COMPARE (h_errno, HOST_NOT_FOUND);
  86. h_errno = 0;
  87. e = NULL;
  88. TEST_COMPARE (gethostbyname2_r (query, AF_INET6,
  89. &storage, buf, sizeof (buf),
  90. &e, &h_errno), 0);
  91. TEST_VERIFY (e == NULL);
  92. TEST_COMPARE (h_errno, HOST_NOT_FOUND);
  93. static const int gai_flags[] =
  94. {
  95. 0,
  96. AI_ADDRCONFIG,
  97. AI_NUMERICHOST,
  98. AI_IDN,
  99. AI_IDN | AI_NUMERICHOST,
  100. AI_V4MAPPED,
  101. AI_V4MAPPED | AI_NUMERICHOST,
  102. };
  103. for (size_t gai_flags_idx; gai_flags_idx < array_length (gai_flags);
  104. ++gai_flags_idx)
  105. {
  106. struct addrinfo hints = { .ai_flags = gai_flags[gai_flags_idx], };
  107. struct addrinfo *ai;
  108. hints.ai_family = AF_INET;
  109. TEST_COMPARE (getaddrinfo (query, "80", &hints, &ai), EAI_NONAME);
  110. hints.ai_family = AF_INET6;
  111. TEST_COMPARE (getaddrinfo (query, "80", &hints, &ai), EAI_NONAME);
  112. hints.ai_family = AF_UNSPEC;
  113. TEST_COMPARE (getaddrinfo (query, "80", &hints, &ai), EAI_NONAME);
  114. }
  115. };
  116. resolv_test_end (aux);
  117. return 0;
  118. }
  119. #include <support/test-driver.c>