PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/resolv/tst-resolv-ai_idn-nolibidn2.c

https://github.com/hjl-tools/glibc
C | 151 lines | 105 code | 16 blank | 30 comment | 5 complexity | ce30779a7af0b03ab784dc2dd593993d MD5 | raw file
  1. /* Test getaddrinfo and getnameinfo without usable libidn2.
  2. Copyright (C) 2018-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. #define TEST_USE_UTF8 1
  16. #include "tst-resolv-ai_idn-common.c"
  17. #include <locale.h>
  18. #include <support/xdlfcn.h>
  19. /* Tests for getaddrinfo. */
  20. static void
  21. gai_tests (void)
  22. {
  23. /* No CNAME. */
  24. check_ai ("non-idn.example", 0,
  25. "address: STREAM/TCP 192.0.2.110 80\n");
  26. check_ai ("non-idn.example", AI_IDN,
  27. "flags: AI_IDN\n"
  28. "address: STREAM/TCP 192.0.2.110 80\n");
  29. check_ai ("non-idn.example", AI_IDN | AI_CANONNAME | AI_CANONIDN,
  30. "flags: AI_CANONNAME AI_IDN AI_CANONIDN\n"
  31. "canonname: non-idn.example\n"
  32. "address: STREAM/TCP 192.0.2.110 80\n");
  33. /* This gets passed over the network to the server, so it will
  34. result in an NXDOMAIN error. */
  35. check_ai (NAEMCHEN ".example", 0,
  36. "error: Name or service not known\n");
  37. /* Due to missing libidn2, this fails inside getaddrinfo. */
  38. check_ai (NAEMCHEN ".example", AI_IDN,
  39. "error: Parameter string not correctly encoded\n");
  40. check_ai (NAEMCHEN ".example", AI_IDN | AI_CANONNAME | AI_CANONIDN,
  41. "error: Parameter string not correctly encoded\n");
  42. /* Non-IDN CNAME. */
  43. check_ai ("with.cname.example", 0,
  44. "address: STREAM/TCP 192.0.2.119 80\n");
  45. check_ai ("with.cname.example", AI_IDN,
  46. "flags: AI_IDN\n"
  47. "address: STREAM/TCP 192.0.2.119 80\n");
  48. check_ai ("with.cname.example", AI_IDN | AI_CANONNAME | AI_CANONIDN,
  49. "flags: AI_CANONNAME AI_IDN AI_CANONIDN\n"
  50. "canonname: non-idn-cname.example\n"
  51. "address: STREAM/TCP 192.0.2.119 80\n");
  52. /* IDN CNAME. */
  53. check_ai ("With.idn-cname.example", 0,
  54. "address: STREAM/TCP 192.0.2.87 80\n");
  55. check_ai ("With.idn-cname.example", AI_IDN,
  56. "flags: AI_IDN\n"
  57. "address: STREAM/TCP 192.0.2.87 80\n");
  58. check_ai ("With.idn-cname.example", AI_IDN | AI_CANONNAME,
  59. "flags: AI_CANONNAME AI_IDN\n"
  60. "canonname: " ANDERES_NAEMCHEN_IDNA ".example\n"
  61. "address: STREAM/TCP 192.0.2.87 80\n");
  62. check_ai ("With.idn-cname.example",
  63. AI_IDN | AI_CANONNAME | AI_CANONIDN,
  64. "flags: AI_CANONNAME AI_IDN AI_CANONIDN\n"
  65. "canonname: " ANDERES_NAEMCHEN_IDNA ".example\n"
  66. "address: STREAM/TCP 192.0.2.87 80\n");
  67. /* Non-IDN to IDN CNAME chain. */
  68. check_ai ("both.cname.idn-cname.example", 0,
  69. "address: STREAM/TCP 192.0.2.98 80\n");
  70. check_ai ("both.cname.idn-cname.example", AI_IDN,
  71. "flags: AI_IDN\n"
  72. "address: STREAM/TCP 192.0.2.98 80\n");
  73. check_ai ("both.cname.idn-cname.example", AI_IDN | AI_CANONNAME,
  74. "flags: AI_CANONNAME AI_IDN\n"
  75. "canonname: " ANDERES_NAEMCHEN_IDNA ".example\n"
  76. "address: STREAM/TCP 192.0.2.98 80\n");
  77. check_ai ("both.cname.idn-cname.example",
  78. AI_IDN | AI_CANONNAME | AI_CANONIDN,
  79. "flags: AI_CANONNAME AI_IDN AI_CANONIDN\n"
  80. "canonname: " ANDERES_NAEMCHEN_IDNA ".example\n"
  81. "address: STREAM/TCP 192.0.2.98 80\n");
  82. }
  83. /* Tests for getnameinfo. */
  84. static void
  85. gni_tests (void)
  86. {
  87. /* All non-IDN an IDN results are the same due to lack of libidn2
  88. support. */
  89. for (int do_ni_idn = 0; do_ni_idn < 2; ++do_ni_idn)
  90. {
  91. int flags = 0;
  92. if (do_ni_idn)
  93. flags |= NI_IDN;
  94. gni_test (gni_non_idn_name, flags, "non-idn.example");
  95. gni_test (gni_non_idn_name, flags | NI_NUMERICHOST, "192.0.2.0");
  96. gni_test (gni_non_idn_cname_to_non_idn_name, flags,
  97. "non-idn-name.example");
  98. gni_test (gni_non_idn_cname_to_idn_name, flags,
  99. NAEMCHEN_IDNA ".example");
  100. gni_test (gni_idn_name, flags, NAEMCHEN_IDNA ".example");
  101. gni_test (gni_idn_cname_to_non_idn_name, flags, "non-idn-name.example");
  102. gni_test (gni_idn_cname_to_idn_name, flags,
  103. ANDERES_NAEMCHEN_IDNA ".example");
  104. /* Test encoding errors. */
  105. gni_test (gni_invalid_idn_1, flags, "xn---.example");
  106. gni_test (gni_invalid_idn_2, flags, "xn--x.example");
  107. }
  108. }
  109. static int
  110. do_test (void)
  111. {
  112. void *handle = xdlopen ("tst-no-libidn2.so", RTLD_LAZY);
  113. {
  114. /* Verify that this replaced libidn2. */
  115. void *handle2 = xdlopen (LIBIDN2_SONAME, RTLD_LAZY | RTLD_NOLOAD);
  116. TEST_VERIFY (handle2 == handle);
  117. xdlclose (handle2);
  118. }
  119. if (setlocale (LC_CTYPE, "en_US.UTF-8") == NULL)
  120. FAIL_EXIT1 ("setlocale: %m");
  121. struct resolv_test *aux = resolv_test_start
  122. ((struct resolv_redirect_config)
  123. {
  124. .response_callback = response,
  125. });
  126. gai_tests ();
  127. gni_tests ();
  128. resolv_test_end (aux);
  129. xdlclose (handle);
  130. return 0;
  131. }
  132. #include <support/test-driver.c>