tst-resolv-canonname.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /* Test _nss_dns_getcanonname_r corner cases.
  2. Copyright (C) 2017-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 <dlfcn.h>
  16. #include <errno.h>
  17. #include <gnu/lib-names.h>
  18. #include <netdb.h>
  19. #include <nss.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <support/check.h>
  24. #include <support/resolv_test.h>
  25. #include <support/support.h>
  26. /* _nss_dns_getcanonname_r is not called during regular operation
  27. because nss_dns directly provides a canonical name, so we have to
  28. test it directly. The function pointer is initialized by do_test
  29. below. */
  30. static enum nss_status
  31. (*getcanonname) (const char *name, char *buffer, size_t buflen,
  32. char **result, int *errnop, int *h_errnop);
  33. static void
  34. response (const struct resolv_response_context *ctx,
  35. struct resolv_response_builder *b,
  36. const char *qname, uint16_t qclass, uint16_t qtype)
  37. {
  38. int code;
  39. {
  40. char *tail;
  41. if (sscanf (qname, "code%d.%ms", &code, &tail) != 2
  42. || strcmp (tail, "example") != 0)
  43. FAIL_EXIT1 ("error: invalid QNAME: %s\n", qname);
  44. free (tail);
  45. }
  46. switch (code)
  47. {
  48. case 1:
  49. resolv_response_init (b, (struct resolv_response_flags) {});
  50. resolv_response_add_question (b, qname, qclass, qtype);
  51. resolv_response_section (b, ns_s_an);
  52. resolv_response_open_record (b, "www.example", qclass, qtype, 0);
  53. resolv_response_add_data (b, "\xC0\x00\x02\x01", 4);
  54. resolv_response_close_record (b);
  55. break;
  56. case 2:
  57. resolv_response_init (b, (struct resolv_response_flags) {});
  58. resolv_response_add_question (b, qname, qclass, qtype);
  59. resolv_response_section (b, ns_s_an);
  60. if (qtype == T_AAAA)
  61. {
  62. resolv_response_open_record (b, "www.example", qclass, qtype, 0);
  63. resolv_response_add_data (b, "\xC0\x00\x02\x01", 4);
  64. resolv_response_close_record (b);
  65. for (int i = 0; i < 30000; ++i)
  66. resolv_response_add_data (b, "", 1);
  67. }
  68. break;
  69. case 3:
  70. resolv_response_init (b, (struct resolv_response_flags) {});
  71. resolv_response_add_question (b, qname, qclass, qtype);
  72. resolv_response_section (b, ns_s_an);
  73. if (qtype == T_AAAA)
  74. {
  75. resolv_response_open_record (b, "www.example", qclass, qtype, 0);
  76. resolv_response_add_data (b, "\xC0\x00\x02\x01", 4);
  77. resolv_response_close_record (b);
  78. }
  79. else
  80. {
  81. for (int i = 0; i < 30000; ++i)
  82. resolv_response_add_data (b, "", 1);
  83. }
  84. break;
  85. case 4:
  86. resolv_response_init (b, (struct resolv_response_flags) {});
  87. resolv_response_add_question (b, qname, qclass, qtype);
  88. resolv_response_section (b, ns_s_an);
  89. resolv_response_open_record (b, qname, qclass, T_CNAME, 0);
  90. resolv_response_add_name (b, "www.example");
  91. resolv_response_close_record (b);
  92. resolv_response_open_record (b, "www.example", qclass, qtype, 0);
  93. resolv_response_add_data (b, "\xC0\x00\x02\x01", 4);
  94. resolv_response_close_record (b);
  95. break;
  96. case 5:
  97. resolv_response_init (b, (struct resolv_response_flags) {});
  98. resolv_response_add_question (b, qname, qclass, qtype);
  99. resolv_response_section (b, ns_s_an);
  100. resolv_response_open_record (b, qname, qclass, T_CNAME, 0);
  101. resolv_response_add_name (b, "www.example");
  102. resolv_response_close_record (b);
  103. resolv_response_open_record (b, qname, qclass, T_CNAME, 0);
  104. resolv_response_add_name (b, "www1.example");
  105. resolv_response_close_record (b);
  106. resolv_response_open_record (b, "www1.example", qclass, qtype, 0);
  107. resolv_response_add_data (b, "\xC0\x00\x02\x01", 4);
  108. resolv_response_close_record (b);
  109. break;
  110. case 6:
  111. resolv_response_init (b, (struct resolv_response_flags) {});
  112. resolv_response_add_question (b, qname, qclass, qtype);
  113. resolv_response_section (b, ns_s_an);
  114. resolv_response_open_record (b, qname, qclass, T_CNAME, 0);
  115. resolv_response_add_name (b, "www.example");
  116. resolv_response_close_record (b);
  117. resolv_response_open_record (b, qname, qclass, 46 /* RRSIG */, 0);
  118. resolv_response_add_name (b, ".");
  119. resolv_response_close_record (b);
  120. resolv_response_open_record (b, "www.example", qclass, qtype, 0);
  121. resolv_response_add_data (b, "\xC0\x00\x02\x01", 4);
  122. resolv_response_close_record (b);
  123. break;
  124. case 102:
  125. if (!ctx->tcp)
  126. {
  127. resolv_response_init (b, (struct resolv_response_flags) {.tc = true});
  128. resolv_response_add_question (b, qname, qclass, qtype);
  129. }
  130. else
  131. {
  132. resolv_response_init
  133. (b, (struct resolv_response_flags) {.ancount = 1});
  134. resolv_response_add_question (b, qname, qclass, qtype);
  135. resolv_response_section (b, ns_s_an);
  136. resolv_response_open_record (b, qname, qclass, T_CNAME, 0);
  137. size_t to_fill = 65535 - resolv_response_length (b)
  138. - 2 /* length, "n" */ - 2 /* compression reference */
  139. - 2 /* RR type */;
  140. for (size_t i = 0; i < to_fill; ++i)
  141. resolv_response_add_data (b, "", 1);
  142. resolv_response_close_record (b);
  143. resolv_response_add_name (b, "n.example");
  144. uint16_t rrtype = htons (T_CNAME);
  145. resolv_response_add_data (b, &rrtype, sizeof (rrtype));
  146. }
  147. break;
  148. case 103:
  149. /* NODATA repsonse. */
  150. resolv_response_init (b, (struct resolv_response_flags) {});
  151. resolv_response_add_question (b, qname, qclass, qtype);
  152. break;
  153. case 104:
  154. resolv_response_init (b, (struct resolv_response_flags) {.ancount = 1});
  155. resolv_response_add_question (b, qname, qclass, qtype);
  156. /* No RR metadata. */
  157. resolv_response_add_name (b, "www.example");
  158. break;
  159. case 105:
  160. if (qtype == T_A)
  161. {
  162. resolv_response_init (b, (struct resolv_response_flags) {});
  163. resolv_response_add_question (b, qname, qclass, qtype);
  164. /* No data, trigger AAAA query. */
  165. }
  166. else
  167. {
  168. resolv_response_init
  169. (b, (struct resolv_response_flags) {.ancount = 1});
  170. resolv_response_add_question (b, qname, qclass, qtype);
  171. /* No RR metadata. */
  172. resolv_response_add_name
  173. (b, "long-name-exceed-previously-initialized-buffer.example");
  174. }
  175. break;
  176. case 106:
  177. resolv_response_init (b, (struct resolv_response_flags) {.ancount = 1});
  178. resolv_response_add_question (b, qname, qclass, qtype);
  179. /* No RR metadata. */
  180. resolv_response_add_name (b, "www.example");
  181. resolv_response_add_data (b, "\xff\xff", 2);
  182. break;
  183. case 107:
  184. if (qtype == T_A)
  185. {
  186. resolv_response_init (b, (struct resolv_response_flags) {});
  187. resolv_response_add_question (b, qname, qclass, qtype);
  188. /* No data, trigger AAAA query. */
  189. }
  190. else
  191. {
  192. resolv_response_init
  193. (b, (struct resolv_response_flags) {.ancount = 1});
  194. resolv_response_add_question (b, qname, qclass, qtype);
  195. /* No RR metadata. */
  196. resolv_response_add_name (b, "www.example");
  197. resolv_response_add_data (b, "\xff\xff", 2);
  198. }
  199. break;
  200. default:
  201. FAIL_EXIT1 ("error: invalid QNAME: %s (code %d)\n", qname, code);
  202. }
  203. }
  204. static void
  205. check (int code, const char *expected)
  206. {
  207. char qname[200];
  208. snprintf (qname, sizeof (qname), "code%d.example", code);
  209. char *result;
  210. enum nss_status status;
  211. {
  212. enum { buffer_size = 4096 };
  213. char *buffer = xmalloc (buffer_size);
  214. char *temp_result;
  215. int temp_errno;
  216. int temp_herrno;
  217. status = getcanonname
  218. (qname, buffer, buffer_size, &temp_result, &temp_errno, &temp_herrno);
  219. if (status == NSS_STATUS_SUCCESS)
  220. result = xstrdup (temp_result);
  221. else
  222. {
  223. errno = temp_errno;
  224. h_errno = temp_herrno;
  225. }
  226. free (buffer);
  227. }
  228. if (status == NSS_STATUS_SUCCESS)
  229. {
  230. if (expected != NULL)
  231. {
  232. if (strcmp (result, expected) != 0)
  233. {
  234. support_record_failure ();
  235. printf ("error: getcanonname (%s) failed\n", qname);
  236. printf ("error: expected: %s\n", expected);
  237. printf ("error: actual: %s\n", result);
  238. free (result);
  239. return;
  240. }
  241. }
  242. else
  243. {
  244. support_record_failure ();
  245. printf ("error: getcanonname (%s) unexpected success\n", qname);
  246. printf ("error: actual: %s\n", result);
  247. free (result);
  248. return;
  249. }
  250. free (result);
  251. }
  252. else
  253. {
  254. if (expected != NULL)
  255. {
  256. support_record_failure ();
  257. printf ("error: getcanonname (%s) failed\n", qname);
  258. printf ("error: expected: %s\n", expected);
  259. return;
  260. }
  261. }
  262. }
  263. static int
  264. do_test (void)
  265. {
  266. void *nss_dns_handle = dlopen (LIBNSS_DNS_SO, RTLD_LAZY);
  267. if (nss_dns_handle == NULL)
  268. FAIL_EXIT1 ("could not dlopen %s: %s", LIBNSS_DNS_SO, dlerror ());
  269. {
  270. const char *func = "_nss_dns_getcanonname_r";
  271. void *ptr = dlsym (nss_dns_handle, func);
  272. if (ptr == NULL)
  273. FAIL_EXIT1 ("could not look up %s: %s", func, dlerror ());
  274. getcanonname = ptr;
  275. }
  276. struct resolv_test *aux = resolv_test_start
  277. ((struct resolv_redirect_config)
  278. {
  279. .response_callback = response,
  280. });
  281. check (1, "www.example");
  282. check (2, "www.example");
  283. check (3, "www.example");
  284. check (4, "www.example");
  285. check (5, "www1.example");
  286. /* This should really result in "www.example", but the fake RRSIG
  287. record causes the current implementation to stop parsing. */
  288. check (6, NULL);
  289. for (int i = 102; i <= 107; ++i)
  290. check (i, NULL);
  291. resolv_test_end (aux);
  292. TEST_VERIFY (dlclose (nss_dns_handle) == 0);
  293. return 0;
  294. }
  295. #include <support/test-driver.c>