tst-resolv-nondecimal.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* Test name resolution behavior for octal, hexadecimal IPv4 addresses.
  2. Copyright (C) 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 <netdb.h>
  16. #include <stdlib.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 void
  30. run_query_addrinfo (const char *query, const char *address)
  31. {
  32. char *quoted_query = support_quote_string (query);
  33. struct addrinfo *ai;
  34. struct addrinfo hints =
  35. {
  36. .ai_socktype = SOCK_STREAM,
  37. .ai_protocol = IPPROTO_TCP,
  38. };
  39. char *context = xasprintf ("getaddrinfo \"%s\" AF_INET", quoted_query);
  40. char *expected = xasprintf ("address: STREAM/TCP %s 80\n", address);
  41. hints.ai_family = AF_INET;
  42. int ret = getaddrinfo (query, "80", &hints, &ai);
  43. check_addrinfo (context, ai, ret, expected);
  44. if (ret == 0)
  45. freeaddrinfo (ai);
  46. free (context);
  47. context = xasprintf ("getaddrinfo \"%s\" AF_UNSPEC", quoted_query);
  48. hints.ai_family = AF_UNSPEC;
  49. ret = getaddrinfo (query, "80", &hints, &ai);
  50. check_addrinfo (context, ai, ret, expected);
  51. if (ret == 0)
  52. freeaddrinfo (ai);
  53. free (expected);
  54. free (context);
  55. context = xasprintf ("getaddrinfo \"%s\" AF_INET6", quoted_query);
  56. expected = xasprintf ("flags: AI_V4MAPPED\n"
  57. "address: STREAM/TCP ::ffff:%s 80\n",
  58. address);
  59. hints.ai_family = AF_INET6;
  60. hints.ai_flags = AI_V4MAPPED;
  61. ret = getaddrinfo (query, "80", &hints, &ai);
  62. check_addrinfo (context, ai, ret, expected);
  63. if (ret == 0)
  64. freeaddrinfo (ai);
  65. free (expected);
  66. free (context);
  67. free (quoted_query);
  68. }
  69. static void
  70. run_query (const char *query, const char *address)
  71. {
  72. char *quoted_query = support_quote_string (query);
  73. char *context = xasprintf ("gethostbyname (\"%s\")", quoted_query);
  74. char *expected = xasprintf ("name: %s\n"
  75. "address: %s\n", query, address);
  76. check_hostent (context, gethostbyname (query), expected);
  77. free (context);
  78. context = xasprintf ("gethostbyname_r \"%s\"", quoted_query);
  79. struct hostent storage;
  80. char buf[4096];
  81. struct hostent *e = NULL;
  82. TEST_COMPARE (gethostbyname_r (query, &storage, buf, sizeof (buf),
  83. &e, &h_errno), 0);
  84. check_hostent (context, e, expected);
  85. free (context);
  86. context = xasprintf ("gethostbyname2 (\"%s\", AF_INET)", quoted_query);
  87. check_hostent (context, gethostbyname2 (query, AF_INET), expected);
  88. free (context);
  89. context = xasprintf ("gethostbyname2_r \"%s\" AF_INET", quoted_query);
  90. e = NULL;
  91. TEST_COMPARE (gethostbyname2_r (query, AF_INET, &storage, buf, sizeof (buf),
  92. &e, &h_errno), 0);
  93. check_hostent (context, e, expected);
  94. free (context);
  95. free (expected);
  96. free (quoted_query);
  97. /* The gethostbyname tests are always valid for getaddrinfo, but not
  98. vice versa. */
  99. run_query_addrinfo (query, address);
  100. }
  101. static int
  102. do_test (void)
  103. {
  104. struct resolv_test *aux = resolv_test_start
  105. ((struct resolv_redirect_config)
  106. {
  107. .response_callback = response,
  108. });
  109. run_query ("192.000.002.010", "192.0.2.8");
  110. /* Hexadecimal numbers are not accepted by gethostbyname. */
  111. run_query_addrinfo ("0xc0000210", "192.0.2.16");
  112. run_query_addrinfo ("192.0x234", "192.0.2.52");
  113. resolv_test_end (aux);
  114. return 0;
  115. }
  116. #include <support/test-driver.c>