tst-resolv-basic.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /* Test basic nss_dns functionality and the resolver test harness itself.
  2. Copyright (C) 2016-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 <errno.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <support/check.h>
  20. #include <support/check_nss.h>
  21. #include <support/format_nss.h>
  22. #include <support/resolv_test.h>
  23. #include <support/support.h>
  24. #define LONG_NAME \
  25. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaax." \
  26. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaay." \
  27. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaz." \
  28. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaat"
  29. static void
  30. response (const struct resolv_response_context *ctx,
  31. struct resolv_response_builder *b,
  32. const char *qname, uint16_t qclass, uint16_t qtype)
  33. {
  34. TEST_VERIFY_EXIT (qname != NULL);
  35. /* The "t." prefix can be used to request TCP fallback. */
  36. bool force_tcp;
  37. if (strncmp ("t.", qname, 2) == 0)
  38. force_tcp = true;
  39. else
  40. force_tcp = false;
  41. const char *qname_compare;
  42. if (force_tcp)
  43. qname_compare = qname + 2;
  44. else
  45. qname_compare = qname;
  46. enum {www, alias, nxdomain, long_name, nodata} requested_qname;
  47. if (strcmp (qname_compare, "www.example") == 0)
  48. requested_qname = www;
  49. else if (strcmp (qname_compare, "alias.example") == 0)
  50. requested_qname = alias;
  51. else if (strcmp (qname_compare, "nxdomain.example") == 0)
  52. requested_qname = nxdomain;
  53. else if (strcmp (qname_compare, LONG_NAME) == 0)
  54. requested_qname = long_name;
  55. else if (strcmp (qname_compare, "nodata.example") == 0)
  56. requested_qname = nodata;
  57. else
  58. {
  59. support_record_failure ();
  60. printf ("error: unexpected QNAME: %s\n", qname);
  61. return;
  62. }
  63. TEST_VERIFY_EXIT (qclass == C_IN);
  64. struct resolv_response_flags flags = {.tc = force_tcp && !ctx->tcp};
  65. if (requested_qname == nxdomain)
  66. flags.rcode = 3; /* NXDOMAIN */
  67. resolv_response_init (b, flags);
  68. resolv_response_add_question (b, qname, qclass, qtype);
  69. if (requested_qname == nxdomain || flags.tc)
  70. return;
  71. resolv_response_section (b, ns_s_an);
  72. switch (requested_qname)
  73. {
  74. case www:
  75. case long_name:
  76. resolv_response_open_record (b, qname, qclass, qtype, 0);
  77. break;
  78. case alias:
  79. resolv_response_open_record (b, qname, qclass, T_CNAME, 0);
  80. resolv_response_add_name (b, "www.example");
  81. resolv_response_close_record (b);
  82. resolv_response_open_record (b, "www.example", qclass, qtype, 0);
  83. break;
  84. case nodata:
  85. return;
  86. case nxdomain:
  87. FAIL_EXIT1 ("unreachable");
  88. }
  89. switch (qtype)
  90. {
  91. case T_A:
  92. {
  93. char ipv4[4] = {192, 0, 2, 17};
  94. ipv4[3] += requested_qname + 2 * ctx->tcp + 4 * ctx->server_index;
  95. resolv_response_add_data (b, &ipv4, sizeof (ipv4));
  96. }
  97. break;
  98. case T_AAAA:
  99. {
  100. char ipv6[16]
  101. = {0x20, 0x01, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
  102. ipv6[15] += requested_qname + 2 * ctx->tcp + 4 * ctx->server_index;
  103. resolv_response_add_data (b, &ipv6, sizeof (ipv6));
  104. }
  105. break;
  106. default:
  107. support_record_failure ();
  108. printf ("error: unexpected QTYPE: %s/%u/%u\n",
  109. qname, qclass, qtype);
  110. }
  111. resolv_response_close_record (b);
  112. }
  113. static void
  114. check_h (const char *name, int family, const char *expected)
  115. {
  116. if (family == AF_INET)
  117. {
  118. char *query = xasprintf ("gethostbyname (\"%s\")", name);
  119. check_hostent (query, gethostbyname (name), expected);
  120. free (query);
  121. }
  122. {
  123. char *query = xasprintf ("gethostbyname2 (\"%s\", %d)", name, family);
  124. check_hostent (query, gethostbyname2 (name, family), expected);
  125. free (query);
  126. }
  127. bool too_small = true;
  128. for (unsigned int offset = 0; offset < 8; ++offset)
  129. for (unsigned int size = 1; too_small; ++size)
  130. {
  131. char *buf = xmalloc (offset + size);
  132. too_small = false;
  133. struct hostent hostbuf;
  134. struct hostent *result;
  135. int herror;
  136. if (family == AF_INET)
  137. {
  138. char *query = xasprintf ("gethostbyname (\"%s\") %u/%u",
  139. name, offset, size);
  140. int ret = gethostbyname_r
  141. (name, &hostbuf, buf + offset, size, &result, &herror);
  142. if (ret == 0)
  143. {
  144. h_errno = herror;
  145. check_hostent (query, result, expected);
  146. }
  147. else if (ret == ERANGE)
  148. too_small = true;
  149. else
  150. {
  151. errno = ret;
  152. FAIL_EXIT1 ("gethostbyname_r: %m");
  153. }
  154. free (query);
  155. memset (buf, 0, offset + size);
  156. }
  157. char *query = xasprintf ("gethostbyname2 (\"%s\", %d) %u/%u",
  158. name, family, offset, size);
  159. int ret = gethostbyname2_r
  160. (name, family, &hostbuf, buf + offset, size, &result, &herror);
  161. if (ret == 0)
  162. {
  163. h_errno = herror;
  164. check_hostent (query, result, expected);
  165. }
  166. else if (ret == ERANGE)
  167. too_small = true;
  168. else
  169. {
  170. errno = ret;
  171. FAIL_EXIT1 ("gethostbyname_r: %m");
  172. }
  173. free (buf);
  174. free (query);
  175. }
  176. }
  177. static void
  178. check_ai_hints (const char *name, const char *service,
  179. struct addrinfo hints, const char *expected)
  180. {
  181. struct addrinfo *ai;
  182. char *query = xasprintf ("%s:%s [%d]/0x%x", name, service,
  183. hints.ai_family, hints.ai_flags);
  184. int ret = getaddrinfo (name, service, &hints, &ai);
  185. check_addrinfo (query, ai, ret, expected);
  186. if (ret == 0)
  187. freeaddrinfo (ai);
  188. free (query);
  189. }
  190. static void
  191. check_ai (const char *name, const char *service,
  192. int family, const char *expected)
  193. {
  194. return check_ai_hints (name, service,
  195. (struct addrinfo) { .ai_family = family, },
  196. expected);
  197. }
  198. /* Test for bug 21295: getaddrinfo used to discard address information
  199. instead of merging it. */
  200. static void
  201. test_bug_21295 (void)
  202. {
  203. /* The address order is unpredictable. There are two factors which
  204. contribute to that: The stub resolver does not perform proper
  205. response matching for A/AAAA queries (an A response could be
  206. associated with an AAAA query and vice versa), and without
  207. namespaces, system configuration could affect address
  208. ordering. */
  209. for (int do_tcp = 0; do_tcp < 2; ++do_tcp)
  210. {
  211. const struct addrinfo hints =
  212. {
  213. .ai_family = AF_INET6,
  214. .ai_socktype = SOCK_STREAM,
  215. .ai_flags = AI_V4MAPPED | AI_ALL,
  216. };
  217. const char *qname;
  218. if (do_tcp)
  219. qname = "t.www.example";
  220. else
  221. qname = "www.example";
  222. struct addrinfo *ai = NULL;
  223. int ret = getaddrinfo (qname, "80", &hints, &ai);
  224. TEST_VERIFY_EXIT (ret == 0);
  225. const char *expected_a;
  226. const char *expected_b;
  227. if (do_tcp)
  228. {
  229. expected_a = "flags: AI_V4MAPPED AI_ALL\n"
  230. "address: STREAM/TCP 2001:db8::3 80\n"
  231. "address: STREAM/TCP ::ffff:192.0.2.19 80\n";
  232. expected_b = "flags: AI_V4MAPPED AI_ALL\n"
  233. "address: STREAM/TCP ::ffff:192.0.2.19 80\n"
  234. "address: STREAM/TCP 2001:db8::3 80\n";
  235. }
  236. else
  237. {
  238. expected_a = "flags: AI_V4MAPPED AI_ALL\n"
  239. "address: STREAM/TCP 2001:db8::1 80\n"
  240. "address: STREAM/TCP ::ffff:192.0.2.17 80\n";
  241. expected_b = "flags: AI_V4MAPPED AI_ALL\n"
  242. "address: STREAM/TCP ::ffff:192.0.2.17 80\n"
  243. "address: STREAM/TCP 2001:db8::1 80\n";
  244. }
  245. char *actual = support_format_addrinfo (ai, ret);
  246. if (!(strcmp (actual, expected_a) == 0
  247. || strcmp (actual, expected_b) == 0))
  248. {
  249. support_record_failure ();
  250. printf ("error: %s: unexpected response (TCP: %d):\n%s\n",
  251. __func__, do_tcp, actual);
  252. }
  253. free (actual);
  254. freeaddrinfo (ai);
  255. }
  256. }
  257. /* Run tests which do not expect any data. */
  258. static void
  259. test_nodata_nxdomain (void)
  260. {
  261. /* Iterate through different address families. */
  262. int families[] = { AF_UNSPEC, AF_INET, AF_INET6, -1 };
  263. for (int i = 0; families[i] >= 0; ++i)
  264. /* If do_tcp, prepend "t." to the name to trigger TCP
  265. fallback. */
  266. for (int do_tcp = 0; do_tcp < 2; ++do_tcp)
  267. /* If do_nxdomain, trigger an NXDOMAIN error (DNS failure),
  268. otherwise use a NODATA response (empty but successful
  269. answer). */
  270. for (int do_nxdomain = 0; do_nxdomain < 2; ++do_nxdomain)
  271. {
  272. int family = families[i];
  273. char *name = xasprintf ("%s%s.example",
  274. do_tcp ? "t." : "",
  275. do_nxdomain ? "nxdomain" : "nodata");
  276. if (family != AF_UNSPEC)
  277. {
  278. if (do_nxdomain)
  279. check_h (name, family, "error: HOST_NOT_FOUND\n");
  280. else
  281. check_h (name, family, "error: NO_ADDRESS\n");
  282. }
  283. const char *expected;
  284. if (do_nxdomain)
  285. expected = "error: Name or service not known\n";
  286. else
  287. expected = "error: No address associated with hostname\n";
  288. check_ai (name, "80", family, expected);
  289. struct addrinfo hints =
  290. {
  291. .ai_family = family,
  292. .ai_flags = AI_V4MAPPED | AI_ALL,
  293. };
  294. check_ai_hints (name, "80", hints, expected);
  295. hints.ai_flags |= AI_CANONNAME;
  296. check_ai_hints (name, "80", hints, expected);
  297. free (name);
  298. }
  299. }
  300. static int
  301. do_test (void)
  302. {
  303. struct resolv_test *aux = resolv_test_start
  304. ((struct resolv_redirect_config)
  305. {
  306. .response_callback = response,
  307. });
  308. check_h ("www.example", AF_INET,
  309. "name: www.example\n"
  310. "address: 192.0.2.17\n");
  311. check_h ("alias.example", AF_INET,
  312. "name: www.example\n"
  313. "alias: alias.example\n"
  314. "address: 192.0.2.18\n");
  315. check_h ("www.example", AF_INET6,
  316. "name: www.example\n"
  317. "address: 2001:db8::1\n");
  318. check_h ("alias.example", AF_INET6,
  319. "name: www.example\n"
  320. "alias: alias.example\n"
  321. "address: 2001:db8::2\n");
  322. check_h (LONG_NAME, AF_INET,
  323. "name: " LONG_NAME "\n"
  324. "address: 192.0.2.20\n");
  325. check_ai ("www.example", "80", AF_UNSPEC,
  326. "address: STREAM/TCP 192.0.2.17 80\n"
  327. "address: DGRAM/UDP 192.0.2.17 80\n"
  328. "address: RAW/IP 192.0.2.17 80\n"
  329. "address: STREAM/TCP 2001:db8::1 80\n"
  330. "address: DGRAM/UDP 2001:db8::1 80\n"
  331. "address: RAW/IP 2001:db8::1 80\n");
  332. check_ai_hints ("www.example", "80",
  333. (struct addrinfo) { .ai_family = AF_UNSPEC,
  334. .ai_flags = AI_CANONNAME, },
  335. "flags: AI_CANONNAME\n"
  336. "canonname: www.example\n"
  337. "address: STREAM/TCP 192.0.2.17 80\n"
  338. "address: DGRAM/UDP 192.0.2.17 80\n"
  339. "address: RAW/IP 192.0.2.17 80\n"
  340. "address: STREAM/TCP 2001:db8::1 80\n"
  341. "address: DGRAM/UDP 2001:db8::1 80\n"
  342. "address: RAW/IP 2001:db8::1 80\n");
  343. check_ai ("alias.example", "80", AF_UNSPEC,
  344. "address: STREAM/TCP 192.0.2.18 80\n"
  345. "address: DGRAM/UDP 192.0.2.18 80\n"
  346. "address: RAW/IP 192.0.2.18 80\n"
  347. "address: STREAM/TCP 2001:db8::2 80\n"
  348. "address: DGRAM/UDP 2001:db8::2 80\n"
  349. "address: RAW/IP 2001:db8::2 80\n");
  350. check_ai_hints ("alias.example", "80",
  351. (struct addrinfo) { .ai_family = AF_UNSPEC,
  352. .ai_flags = AI_CANONNAME, },
  353. "flags: AI_CANONNAME\n"
  354. "canonname: www.example\n"
  355. "address: STREAM/TCP 192.0.2.18 80\n"
  356. "address: DGRAM/UDP 192.0.2.18 80\n"
  357. "address: RAW/IP 192.0.2.18 80\n"
  358. "address: STREAM/TCP 2001:db8::2 80\n"
  359. "address: DGRAM/UDP 2001:db8::2 80\n"
  360. "address: RAW/IP 2001:db8::2 80\n");
  361. check_ai (LONG_NAME, "80", AF_UNSPEC,
  362. "address: STREAM/TCP 192.0.2.20 80\n"
  363. "address: DGRAM/UDP 192.0.2.20 80\n"
  364. "address: RAW/IP 192.0.2.20 80\n"
  365. "address: STREAM/TCP 2001:db8::4 80\n"
  366. "address: DGRAM/UDP 2001:db8::4 80\n"
  367. "address: RAW/IP 2001:db8::4 80\n");
  368. check_ai ("www.example", "80", AF_INET,
  369. "address: STREAM/TCP 192.0.2.17 80\n"
  370. "address: DGRAM/UDP 192.0.2.17 80\n"
  371. "address: RAW/IP 192.0.2.17 80\n");
  372. check_ai_hints ("www.example", "80",
  373. (struct addrinfo) { .ai_family = AF_INET,
  374. .ai_flags = AI_CANONNAME, },
  375. "flags: AI_CANONNAME\n"
  376. "canonname: www.example\n"
  377. "address: STREAM/TCP 192.0.2.17 80\n"
  378. "address: DGRAM/UDP 192.0.2.17 80\n"
  379. "address: RAW/IP 192.0.2.17 80\n");
  380. check_ai ("alias.example", "80", AF_INET,
  381. "address: STREAM/TCP 192.0.2.18 80\n"
  382. "address: DGRAM/UDP 192.0.2.18 80\n"
  383. "address: RAW/IP 192.0.2.18 80\n");
  384. check_ai_hints ("alias.example", "80",
  385. (struct addrinfo) { .ai_family = AF_INET,
  386. .ai_flags = AI_CANONNAME, },
  387. "flags: AI_CANONNAME\n"
  388. "canonname: www.example\n"
  389. "address: STREAM/TCP 192.0.2.18 80\n"
  390. "address: DGRAM/UDP 192.0.2.18 80\n"
  391. "address: RAW/IP 192.0.2.18 80\n");
  392. check_ai (LONG_NAME, "80", AF_INET,
  393. "address: STREAM/TCP 192.0.2.20 80\n"
  394. "address: DGRAM/UDP 192.0.2.20 80\n"
  395. "address: RAW/IP 192.0.2.20 80\n");
  396. check_ai ("www.example", "80", AF_INET6,
  397. "address: STREAM/TCP 2001:db8::1 80\n"
  398. "address: DGRAM/UDP 2001:db8::1 80\n"
  399. "address: RAW/IP 2001:db8::1 80\n");
  400. check_ai_hints ("www.example", "80",
  401. (struct addrinfo) { .ai_family = AF_INET6,
  402. .ai_flags = AI_CANONNAME, },
  403. "flags: AI_CANONNAME\n"
  404. "canonname: www.example\n"
  405. "address: STREAM/TCP 2001:db8::1 80\n"
  406. "address: DGRAM/UDP 2001:db8::1 80\n"
  407. "address: RAW/IP 2001:db8::1 80\n");
  408. check_ai ("alias.example", "80", AF_INET6,
  409. "address: STREAM/TCP 2001:db8::2 80\n"
  410. "address: DGRAM/UDP 2001:db8::2 80\n"
  411. "address: RAW/IP 2001:db8::2 80\n");
  412. check_ai_hints ("alias.example", "80",
  413. (struct addrinfo) { .ai_family = AF_INET6,
  414. .ai_flags = AI_CANONNAME, },
  415. "flags: AI_CANONNAME\n"
  416. "canonname: www.example\n"
  417. "address: STREAM/TCP 2001:db8::2 80\n"
  418. "address: DGRAM/UDP 2001:db8::2 80\n"
  419. "address: RAW/IP 2001:db8::2 80\n");
  420. check_ai (LONG_NAME, "80", AF_INET6,
  421. "address: STREAM/TCP 2001:db8::4 80\n"
  422. "address: DGRAM/UDP 2001:db8::4 80\n"
  423. "address: RAW/IP 2001:db8::4 80\n");
  424. check_h ("t.www.example", AF_INET,
  425. "name: t.www.example\n"
  426. "address: 192.0.2.19\n");
  427. check_h ("t.alias.example", AF_INET,
  428. "name: www.example\n"
  429. "alias: t.alias.example\n"
  430. "address: 192.0.2.20\n");
  431. check_h ("t.www.example", AF_INET6,
  432. "name: t.www.example\n"
  433. "address: 2001:db8::3\n");
  434. check_h ("t.alias.example", AF_INET6,
  435. "name: www.example\n"
  436. "alias: t.alias.example\n"
  437. "address: 2001:db8::4\n");
  438. check_ai ("t.www.example", "80", AF_UNSPEC,
  439. "address: STREAM/TCP 192.0.2.19 80\n"
  440. "address: DGRAM/UDP 192.0.2.19 80\n"
  441. "address: RAW/IP 192.0.2.19 80\n"
  442. "address: STREAM/TCP 2001:db8::3 80\n"
  443. "address: DGRAM/UDP 2001:db8::3 80\n"
  444. "address: RAW/IP 2001:db8::3 80\n");
  445. check_ai ("t.alias.example", "80", AF_UNSPEC,
  446. "address: STREAM/TCP 192.0.2.20 80\n"
  447. "address: DGRAM/UDP 192.0.2.20 80\n"
  448. "address: RAW/IP 192.0.2.20 80\n"
  449. "address: STREAM/TCP 2001:db8::4 80\n"
  450. "address: DGRAM/UDP 2001:db8::4 80\n"
  451. "address: RAW/IP 2001:db8::4 80\n");
  452. check_ai ("t.www.example", "80", AF_INET,
  453. "address: STREAM/TCP 192.0.2.19 80\n"
  454. "address: DGRAM/UDP 192.0.2.19 80\n"
  455. "address: RAW/IP 192.0.2.19 80\n");
  456. check_ai ("t.alias.example", "80", AF_INET,
  457. "address: STREAM/TCP 192.0.2.20 80\n"
  458. "address: DGRAM/UDP 192.0.2.20 80\n"
  459. "address: RAW/IP 192.0.2.20 80\n");
  460. check_ai ("t.www.example", "80", AF_INET6,
  461. "address: STREAM/TCP 2001:db8::3 80\n"
  462. "address: DGRAM/UDP 2001:db8::3 80\n"
  463. "address: RAW/IP 2001:db8::3 80\n");
  464. check_ai ("t.alias.example", "80", AF_INET6,
  465. "address: STREAM/TCP 2001:db8::4 80\n"
  466. "address: DGRAM/UDP 2001:db8::4 80\n"
  467. "address: RAW/IP 2001:db8::4 80\n");
  468. test_bug_21295 ();
  469. test_nodata_nxdomain ();
  470. resolv_test_end (aux);
  471. return 0;
  472. }
  473. #include <support/test-driver.c>