1 /* Test parsing of /etc/resolv.conf. Genric version.
2 Copyright (C) 2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 /* Before including this file, TEST_THREAD has to be defined to 0 or
20 1, depending on whether the threading tests should be compiled
23 #include <arpa/inet.h>
25 #include <gnu/lib-names.h>
27 #include <resolv/resolv-internal.h> /* For DEPRECATED_RES_USE_INET6. */
28 #include <resolv/resolv_context.h>
31 #include <support/capture_subprocess.h>
32 #include <support/check.h>
33 #include <support/namespace.h>
34 #include <support/run_diff.h>
35 #include <support/support.h>
36 #include <support/temp_file.h>
37 #include <support/test-driver.h>
38 #include <support/xsocket.h>
39 #include <support/xstdio.h>
40 #include <support/xunistd.h>
43 # include <support/xthread.h>
46 /* This is the host name used to ensure predictable behavior of
48 static const char *const test_hostname
= "www.example.com";
50 struct support_chroot
*chroot_env
;
53 prepare (int argc
, char **argv
)
55 chroot_env
= support_chroot_create
56 ((struct support_chroot_configuration
)
62 /* Verify that the chroot environment has been set up. */
64 check_chroot_working (void *closure
)
66 xchroot (chroot_env
->path_chroot
);
67 FILE *fp
= xfopen (_PATH_RESCONF
, "r");
70 TEST_VERIFY_EXIT (res_init () == 0);
71 TEST_VERIFY (_res
.options
& RES_INIT
);
74 if (gethostname (buf
, sizeof (buf
)) < 0)
75 FAIL_EXIT1 ("gethostname: %m");
76 if (strcmp (buf
, test_hostname
) != 0)
77 FAIL_EXIT1 ("unexpected host name: %s", buf
);
80 /* If FLAG is set in *OPTIONS, write NAME to FP, and clear it in
83 print_option_flag (FILE *fp
, int *options
, int flag
, const char *name
)
87 fprintf (fp
, " %s", name
);
92 /* Write a decoded version of the resolver configuration *RESP to the
95 print_resp (FILE *fp
, res_state resp
)
97 struct resolv_context
*ctx
= __resolv_context_get_override (resp
);
98 TEST_VERIFY_EXIT (ctx
!= NULL
);
99 if (ctx
->conf
== NULL
)
100 fprintf (fp
, "; extended resolver state missing\n");
102 /* The options directive. */
104 /* RES_INIT is used internally for tracking initialization. */
105 TEST_VERIFY (resp
->options
& RES_INIT
);
106 /* Also mask out other default flags which cannot be set through
107 the options directive. */
109 = resp
->options
& ~(RES_INIT
| RES_RECURSE
| RES_DEFNAMES
| RES_DNSRCH
);
112 || resp
->retrans
!= RES_TIMEOUT
113 || resp
->retry
!= RES_DFLRETRY
)
115 fputs ("options", fp
);
116 if (resp
->ndots
!= 1)
117 fprintf (fp
, " ndots:%d", resp
->ndots
);
118 if (resp
->retrans
!= RES_TIMEOUT
)
119 fprintf (fp
, " timeout:%d", resp
->retrans
);
120 if (resp
->retry
!= RES_DFLRETRY
)
121 fprintf (fp
, " attempts:%d", resp
->retry
);
122 print_option_flag (fp
, &options
, RES_USEVC
, "use-vc");
123 print_option_flag (fp
, &options
, DEPRECATED_RES_USE_INET6
, "inet6");
124 print_option_flag (fp
, &options
, RES_ROTATE
, "rotate");
125 print_option_flag (fp
, &options
, RES_USE_EDNS0
, "edns0");
126 print_option_flag (fp
, &options
, RES_SNGLKUP
,
128 print_option_flag (fp
, &options
, RES_SNGLKUPREOP
,
129 "single-request-reopen");
130 print_option_flag (fp
, &options
, RES_NOTLDQUERY
, "no-tld-query");
131 print_option_flag (fp
, &options
, RES_NORELOAD
, "no-reload");
134 fprintf (fp
, "; error: unresolved option bits: 0x%x\n", options
);
138 /* The search and domain directives. */
139 if (resp
->dnsrch
[0] != NULL
)
141 fputs ("search", fp
);
142 for (int i
= 0; i
< MAXDNSRCH
&& resp
->dnsrch
[i
] != NULL
; ++i
)
145 fputs (resp
->dnsrch
[i
], fp
);
149 else if (resp
->defdname
[0] != '\0')
150 fprintf (fp
, "domain %s\n", resp
->defdname
);
152 /* The extended search path. */
157 const char *name
= __resolv_context_search_list (ctx
, i
);
160 fprintf (fp
, "; search[%zu]: %s\n", i
, name
);
165 /* The sortlist directive. */
168 fputs ("sortlist", fp
);
169 for (int i
= 0; i
< resp
->nsort
&& i
< MAXRESOLVSORT
; ++i
)
172 if (inet_ntop (AF_INET
, &resp
->sort_list
[i
].addr
,
173 net
, sizeof (net
)) == NULL
)
174 FAIL_EXIT1 ("inet_ntop: %m\n");
176 if (inet_ntop (AF_INET
, &resp
->sort_list
[i
].mask
,
177 mask
, sizeof (mask
)) == NULL
)
178 FAIL_EXIT1 ("inet_ntop: %m\n");
179 fprintf (fp
, " %s/%s", net
, mask
);
184 /* The nameserver directives. */
185 for (size_t i
= 0; i
< resp
->nscount
; ++i
)
187 char host
[NI_MAXHOST
];
188 char service
[NI_MAXSERV
];
190 /* See get_nsaddr in res_send.c. */
193 if (resp
->nsaddr_list
[i
].sin_family
== 0
194 && resp
->_u
._ext
.nsaddrs
[i
] != NULL
)
196 addr
= resp
->_u
._ext
.nsaddrs
[i
];
197 addrlen
= sizeof (*resp
->_u
._ext
.nsaddrs
[i
]);
201 addr
= &resp
->nsaddr_list
[i
];
202 addrlen
= sizeof (resp
->nsaddr_list
[i
]);
205 int ret
= getnameinfo (addr
, addrlen
,
206 host
, sizeof (host
), service
, sizeof (service
),
207 NI_NUMERICHOST
| NI_NUMERICSERV
);
210 if (ret
== EAI_SYSTEM
)
211 fprintf (fp
, "; error: getnameinfo: %m\n");
213 fprintf (fp
, "; error: getnameinfo: %s\n", gai_strerror (ret
));
217 fprintf (fp
, "nameserver %s\n", host
);
218 if (strcmp (service
, "53") != 0)
219 fprintf (fp
, "; unrepresentable port number %s\n\n", service
);
223 /* The extended name server list. */
228 const struct sockaddr
*addr
= __resolv_context_nameserver (ctx
, i
);
232 switch (addr
->sa_family
)
235 addrlen
= sizeof (struct sockaddr_in
);
238 addrlen
= sizeof (struct sockaddr_in6
);
241 FAIL_EXIT1 ("invalid address family %d", addr
->sa_family
);
244 char host
[NI_MAXHOST
];
245 char service
[NI_MAXSERV
];
246 int ret
= getnameinfo (addr
, addrlen
,
247 host
, sizeof (host
), service
, sizeof (service
),
248 NI_NUMERICHOST
| NI_NUMERICSERV
);
252 if (ret
== EAI_SYSTEM
)
253 fprintf (fp
, "; error: getnameinfo: %m\n");
255 fprintf (fp
, "; error: getnameinfo: %s\n", gai_strerror (ret
));
258 fprintf (fp
, "; nameserver[%zu]: [%s]:%s\n", i
, host
, service
);
263 TEST_VERIFY (!ferror (fp
));
265 __resolv_context_put (ctx
);
268 /* Parameters of one test case. */
271 /* A short, descriptive name of the test. */
274 /* The contents of the /etc/resolv.conf file. */
277 /* The expected output from print_resp. */
278 const char *expected
;
280 /* Setting for the LOCALDOMAIN environment variable. NULL if the
281 variable is not to be set. */
282 const char *localdomain
;
284 /* Setting for the RES_OPTIONS environment variable. NULL if the
285 variable is not to be set. */
286 const char *res_options
;
288 /* Override the system host name. NULL means that no change is made
289 and the default is used (test_hostname). */
290 const char *hostname
;
300 test_init_method_last
= test_getaddrinfo
303 static const char *const test_init_names
[] =
305 [test_init
] = "res_init",
306 [test_ninit
] = "res_ninit",
307 [test_mkquery
] = "res_mkquery",
308 [test_gethostbyname
] = "gethostbyname",
309 [test_getaddrinfo
] = "getaddrinfo",
312 /* Closure argument for run_res_init. */
316 const struct test_case
*t
;
320 setup_nss_dns_and_chroot (void)
322 /* Load nss_dns outside of the chroot. */
323 if (dlopen (LIBNSS_DNS_SO
, RTLD_LAZY
) == NULL
)
324 FAIL_EXIT1 ("could not load " LIBNSS_DNS_SO
": %s", dlerror ());
325 xchroot (chroot_env
->path_chroot
);
326 /* Force the use of nss_dns. */
327 __nss_configure_lookup ("hosts", "dns");
330 /* Run res_ninit or res_init in a subprocess and dump the parsed
331 resolver state to standard output. */
333 run_res_init (void *closure
)
335 struct test_context
*ctx
= closure
;
336 TEST_VERIFY (getenv ("LOCALDOMAIN") == NULL
);
337 TEST_VERIFY (getenv ("RES_OPTIONS") == NULL
);
338 if (ctx
->t
->localdomain
!= NULL
)
339 setenv ("LOCALDOMAIN", ctx
->t
->localdomain
, 1);
340 if (ctx
->t
->res_options
!= NULL
)
341 setenv ("RES_OPTIONS", ctx
->t
->res_options
, 1);
342 if (ctx
->t
->hostname
!= NULL
)
344 /* This test needs its own namespace, to avoid changing the host
345 name for the parent, too. */
346 TEST_VERIFY_EXIT (unshare (CLONE_NEWUTS
) == 0);
347 if (sethostname (ctx
->t
->hostname
, strlen (ctx
->t
->hostname
)) != 0)
348 FAIL_EXIT1 ("sethostname (\"%s\"): %m", ctx
->t
->hostname
);
354 xchroot (chroot_env
->path_chroot
);
355 TEST_VERIFY (res_init () == 0);
356 print_resp (stdout
, &_res
);
360 xchroot (chroot_env
->path_chroot
);
361 res_state resp
= xmalloc (sizeof (*resp
));
362 memset (resp
, 0, sizeof (*resp
));
363 TEST_VERIFY (res_ninit (resp
) == 0);
364 print_resp (stdout
, resp
);
370 xchroot (chroot_env
->path_chroot
);
371 unsigned char buf
[512];
372 TEST_VERIFY (res_mkquery (QUERY
, "www.example",
373 C_IN
, ns_t_a
, NULL
, 0,
374 NULL
, buf
, sizeof (buf
)) > 0);
375 print_resp (stdout
, &_res
);
378 case test_gethostbyname
:
379 setup_nss_dns_and_chroot ();
380 /* Trigger implicit initialization of the _res structure. The
381 actual lookup result is immaterial. */
382 (void )gethostbyname ("www.example");
383 print_resp (stdout
, &_res
);
386 case test_getaddrinfo
:
387 setup_nss_dns_and_chroot ();
388 /* Trigger implicit initialization of the _res structure. The
389 actual lookup result is immaterial. */
391 (void) getaddrinfo ("www.example", NULL
, NULL
, &ai
);
392 print_resp (stdout
, &_res
);
396 FAIL_EXIT1 ("invalid init method %d", ctx
->init
);
400 /* Helper function which calls run_res_init from a thread. */
402 run_res_init_thread_func (void *closure
)
404 run_res_init (closure
);
408 /* Variant of res_run_init which runs the function on a non-main
411 run_res_init_on_thread (void *closure
)
413 xpthread_join (xpthread_create (NULL
, run_res_init_thread_func
, closure
));
415 #endif /* TEST_THREAD */
417 struct test_case test_cases
[] =
419 {.name
= "empty file",
421 .expected
= "search example.com\n"
422 "; search[0]: example.com\n"
423 "nameserver 127.0.0.1\n"
424 "; nameserver[0]: [127.0.0.1]:53\n"
426 {.name
= "empty file, no-dot hostname",
428 .expected
= "nameserver 127.0.0.1\n"
429 "; nameserver[0]: [127.0.0.1]:53\n",
430 .hostname
= "example",
432 {.name
= "empty file with LOCALDOMAIN",
434 .expected
= "search example.net\n"
435 "; search[0]: example.net\n"
436 "nameserver 127.0.0.1\n"
437 "; nameserver[0]: [127.0.0.1]:53\n",
438 .localdomain
= "example.net",
440 {.name
= "empty file with RES_OPTIONS",
442 .expected
= "options attempts:5 edns0\n"
443 "search example.com\n"
444 "; search[0]: example.com\n"
445 "nameserver 127.0.0.1\n"
446 "; nameserver[0]: [127.0.0.1]:53\n",
447 .res_options
= "edns0 attempts:5",
449 {.name
= "empty file with RES_OPTIONS and LOCALDOMAIN",
451 .expected
= "options attempts:5 edns0\n"
452 "search example.org\n"
453 "; search[0]: example.org\n"
454 "nameserver 127.0.0.1\n"
455 "; nameserver[0]: [127.0.0.1]:53\n",
456 .localdomain
= "example.org",
457 .res_options
= "edns0 attempts:5",
460 .conf
= "search corp.example.com example.com\n"
461 "nameserver 192.0.2.1\n",
462 .expected
= "search corp.example.com example.com\n"
463 "; search[0]: corp.example.com\n"
464 "; search[1]: example.com\n"
465 "nameserver 192.0.2.1\n"
466 "; nameserver[0]: [192.0.2.1]:53\n"
468 {.name
= "basic with no-dot hostname",
469 .conf
= "search corp.example.com example.com\n"
470 "nameserver 192.0.2.1\n",
471 .expected
= "search corp.example.com example.com\n"
472 "; search[0]: corp.example.com\n"
473 "; search[1]: example.com\n"
474 "nameserver 192.0.2.1\n"
475 "; nameserver[0]: [192.0.2.1]:53\n",
476 .hostname
= "example",
478 {.name
= "basic no-reload",
479 .conf
= "options no-reload\n"
480 "search corp.example.com example.com\n"
481 "nameserver 192.0.2.1\n",
482 .expected
= "options no-reload\n"
483 "search corp.example.com example.com\n"
484 "; search[0]: corp.example.com\n"
485 "; search[1]: example.com\n"
486 "nameserver 192.0.2.1\n"
487 "; nameserver[0]: [192.0.2.1]:53\n"
489 {.name
= "basic no-reload via RES_OPTIONS",
490 .conf
= "search corp.example.com example.com\n"
491 "nameserver 192.0.2.1\n",
492 .expected
= "options no-reload\n"
493 "search corp.example.com example.com\n"
494 "; search[0]: corp.example.com\n"
495 "; search[1]: example.com\n"
496 "nameserver 192.0.2.1\n"
497 "; nameserver[0]: [192.0.2.1]:53\n",
498 .res_options
= "no-reload"
500 {.name
= "whitespace",
501 .conf
= "# This test covers comment and whitespace processing "
502 " (trailing whitespace,\n"
503 "# missing newline at end of file).\n"
505 ";search commented out\n"
506 "search corp.example.com\texample.com \n"
507 "#nameserver 192.0.2.3\n"
508 "nameserver 192.0.2.1 \n"
509 "nameserver 192.0.2.2", /* No \n at end of file. */
510 .expected
= "search corp.example.com example.com\n"
511 "; search[0]: corp.example.com\n"
512 "; search[1]: example.com\n"
513 "nameserver 192.0.2.1\n"
514 "nameserver 192.0.2.2\n"
515 "; nameserver[0]: [192.0.2.1]:53\n"
516 "; nameserver[1]: [192.0.2.2]:53\n"
519 .conf
= "domain example.net\n"
520 "nameserver 192.0.2.1\n",
521 .expected
= "search example.net\n"
522 "; search[0]: example.net\n"
523 "nameserver 192.0.2.1\n"
524 "; nameserver[0]: [192.0.2.1]:53\n"
526 {.name
= "domain space",
527 .conf
= "domain example.net \n"
528 "nameserver 192.0.2.1\n",
529 .expected
= "search example.net\n"
530 "; search[0]: example.net\n"
531 "nameserver 192.0.2.1\n"
532 "; nameserver[0]: [192.0.2.1]:53\n"
534 {.name
= "domain tab",
535 .conf
= "domain example.net\t\n"
536 "nameserver 192.0.2.1\n",
537 .expected
= "search example.net\n"
538 "; search[0]: example.net\n"
539 "nameserver 192.0.2.1\n"
540 "; nameserver[0]: [192.0.2.1]:53\n"
542 {.name
= "domain override",
543 .conf
= "search example.com example.org\n"
544 "nameserver 192.0.2.1\n"
545 "domain example.net", /* No \n at end of file. */
546 .expected
= "search example.net\n"
547 "; search[0]: example.net\n"
548 "nameserver 192.0.2.1\n"
549 "; nameserver[0]: [192.0.2.1]:53\n"
551 {.name
= "option values, multiple servers",
552 .conf
= "options\tinet6\tndots:3 edns0\tattempts:5\ttimeout:19\n"
553 "domain example.net\n"
555 "search corp.example.com\texample.com\n"
556 "nameserver 192.0.2.1\n"
558 "nameserver 192.0.2.2\n",
559 .expected
= "options ndots:3 timeout:19 attempts:5 inet6 edns0\n"
560 "search corp.example.com example.com\n"
561 "; search[0]: corp.example.com\n"
562 "; search[1]: example.com\n"
563 "nameserver 192.0.2.1\n"
565 "nameserver 192.0.2.2\n"
566 "; nameserver[0]: [192.0.2.1]:53\n"
567 "; nameserver[1]: [::1]:53\n"
568 "; nameserver[2]: [192.0.2.2]:53\n"
570 {.name
= "out-of-range option vales",
571 .conf
= "options use-vc timeout:999 attempts:999 ndots:99\n"
572 "search example.com\n",
573 .expected
= "options ndots:15 timeout:30 attempts:5 use-vc\n"
574 "search example.com\n"
575 "; search[0]: example.com\n"
576 "nameserver 127.0.0.1\n"
577 "; nameserver[0]: [127.0.0.1]:53\n"
579 {.name
= "repeated directives",
580 .conf
= "options ndots:3 use-vc\n"
581 "options edns0 ndots:2\n"
582 "domain corp.example\n"
583 "search example.net corp.example.com example.com\n"
584 "search example.org\n"
586 .expected
= "options ndots:2 use-vc edns0\n"
587 "search example.org\n"
588 "; search[0]: example.org\n"
589 "nameserver 127.0.0.1\n"
590 "; nameserver[0]: [127.0.0.1]:53\n"
592 {.name
= "many name servers, sortlist",
593 .conf
= "options single-request\n"
594 "search example.org example.com example.net corp.example.com\n"
595 "sortlist 192.0.2.0/255.255.255.0\n"
596 "nameserver 192.0.2.1\n"
597 "nameserver 192.0.2.2\n"
598 "nameserver 192.0.2.3\n"
599 "nameserver 192.0.2.4\n"
600 "nameserver 192.0.2.5\n"
601 "nameserver 192.0.2.6\n"
602 "nameserver 192.0.2.7\n"
603 "nameserver 192.0.2.8\n",
604 .expected
= "options single-request\n"
605 "search example.org example.com example.net corp.example.com\n"
606 "; search[0]: example.org\n"
607 "; search[1]: example.com\n"
608 "; search[2]: example.net\n"
609 "; search[3]: corp.example.com\n"
610 "sortlist 192.0.2.0/255.255.255.0\n"
611 "nameserver 192.0.2.1\n"
612 "nameserver 192.0.2.2\n"
613 "nameserver 192.0.2.3\n"
614 "; nameserver[0]: [192.0.2.1]:53\n"
615 "; nameserver[1]: [192.0.2.2]:53\n"
616 "; nameserver[2]: [192.0.2.3]:53\n"
617 "; nameserver[3]: [192.0.2.4]:53\n"
618 "; nameserver[4]: [192.0.2.5]:53\n"
619 "; nameserver[5]: [192.0.2.6]:53\n"
620 "; nameserver[6]: [192.0.2.7]:53\n"
621 "; nameserver[7]: [192.0.2.8]:53\n"
623 {.name
= "IPv4 and IPv6 nameservers",
624 .conf
= "options single-request\n"
625 "search example.org example.com example.net corp.example.com"
626 " legacy.example.com\n"
627 "sortlist 192.0.2.0\n"
628 "nameserver 192.0.2.1\n"
629 "nameserver 2001:db8::2\n"
630 "nameserver 192.0.2.3\n"
631 "nameserver 2001:db8::4\n"
632 "nameserver 192.0.2.5\n"
633 "nameserver 2001:db8::6\n"
634 "nameserver 192.0.2.7\n"
635 "nameserver 2001:db8::8\n",
636 .expected
= "options single-request\n"
637 "search example.org example.com example.net corp.example.com"
638 " legacy.example.com\n"
639 "; search[0]: example.org\n"
640 "; search[1]: example.com\n"
641 "; search[2]: example.net\n"
642 "; search[3]: corp.example.com\n"
643 "; search[4]: legacy.example.com\n"
644 "sortlist 192.0.2.0/255.255.255.0\n"
645 "nameserver 192.0.2.1\n"
646 "nameserver 2001:db8::2\n"
647 "nameserver 192.0.2.3\n"
648 "; nameserver[0]: [192.0.2.1]:53\n"
649 "; nameserver[1]: [2001:db8::2]:53\n"
650 "; nameserver[2]: [192.0.2.3]:53\n"
651 "; nameserver[3]: [2001:db8::4]:53\n"
652 "; nameserver[4]: [192.0.2.5]:53\n"
653 "; nameserver[5]: [2001:db8::6]:53\n"
654 "; nameserver[6]: [192.0.2.7]:53\n"
655 "; nameserver[7]: [2001:db8::8]:53\n",
657 {.name
= "garbage after nameserver",
658 .conf
= "nameserver 192.0.2.1 garbage\n"
659 "nameserver 192.0.2.2:5353\n"
660 "nameserver 192.0.2.3 5353\n",
661 .expected
= "search example.com\n"
662 "; search[0]: example.com\n"
663 "nameserver 192.0.2.1\n"
664 "nameserver 192.0.2.3\n"
665 "; nameserver[0]: [192.0.2.1]:53\n"
666 "; nameserver[1]: [192.0.2.3]:53\n"
668 {.name
= "RES_OPTIONS is cummulative",
669 .conf
= "options timeout:7 ndots:2 use-vc\n"
670 "nameserver 192.0.2.1\n",
671 .expected
= "options ndots:3 timeout:7 attempts:5 use-vc edns0\n"
672 "search example.com\n"
673 "; search[0]: example.com\n"
674 "nameserver 192.0.2.1\n"
675 "; nameserver[0]: [192.0.2.1]:53\n",
676 .res_options
= "attempts:5 ndots:3 edns0 ",
678 {.name
= "many search list entries (bug 19569)",
679 .conf
= "nameserver 192.0.2.1\n"
680 "search corp.example.com support.example.com"
681 " community.example.org wan.example.net vpn.example.net"
682 " example.com example.org example.net\n",
683 .expected
= "search corp.example.com support.example.com"
684 " community.example.org wan.example.net vpn.example.net example.com\n"
685 "; search[0]: corp.example.com\n"
686 "; search[1]: support.example.com\n"
687 "; search[2]: community.example.org\n"
688 "; search[3]: wan.example.net\n"
689 "; search[4]: vpn.example.net\n"
690 "; search[5]: example.com\n"
691 "; search[6]: example.org\n"
692 "; search[7]: example.net\n"
693 "nameserver 192.0.2.1\n"
694 "; nameserver[0]: [192.0.2.1]:53\n"
696 {.name
= "very long search list entries (bug 21475)",
697 .conf
= "nameserver 192.0.2.1\n"
698 "search example.com "
699 #define H63 "this-host-name-is-longer-than-yours-yes-I-really-really-mean-it"
700 #define D63 "this-domain-name-is-as-long-as-the-previous-name--63-characters"
701 " " H63
"." D63
".example.org"
702 " " H63
"." D63
".example.net\n",
703 .expected
= "search example.com " H63
"." D63
".example.org\n"
704 "; search[0]: example.com\n"
705 "; search[1]: " H63
"." D63
".example.org\n"
706 "; search[2]: " H63
"." D63
".example.net\n"
709 "nameserver 192.0.2.1\n"
710 "; nameserver[0]: [192.0.2.1]:53\n"
715 /* Run the indicated test case. This function assumes that the chroot
716 contents has already been set up. */
718 test_file_contents (const struct test_case
*t
)
721 for (int do_thread
= 0; do_thread
< 2; ++do_thread
)
723 for (int init_method
= 0; init_method
<= test_init_method_last
;
726 if (test_verbose
> 0)
727 printf ("info: testing init method %s\n",
728 test_init_names
[init_method
]);
729 struct test_context ctx
= { .init
= init_method
, .t
= t
};
730 void (*func
) (void *) = run_res_init
;
733 func
= run_res_init_on_thread
;
735 struct support_capture_subprocess proc
736 = support_capture_subprocess (func
, &ctx
);
737 if (strcmp (proc
.out
.buffer
, t
->expected
) != 0)
739 support_record_failure ();
740 printf ("error: output mismatch for %s (init method %s)\n",
741 t
->name
, test_init_names
[init_method
]);
742 support_run_diff ("expected", t
->expected
,
743 "actual", proc
.out
.buffer
);
745 support_capture_subprocess_check (&proc
, t
->name
, 0,
747 support_capture_subprocess_free (&proc
);
751 /* Special tests which do not follow the general pattern. */
752 enum { special_tests_count
= 11 };
754 /* Implementation of special tests. */
756 special_test_callback (void *closure
)
758 unsigned int *test_indexp
= closure
;
759 unsigned test_index
= *test_indexp
;
760 TEST_VERIFY (test_index
< special_tests_count
);
761 if (test_verbose
> 0)
762 printf ("info: special test %u\n", test_index
);
763 xchroot (chroot_env
->path_chroot
);
769 /* Second res_init with missing or empty file preserves
772 TEST_VERIFY (unlink (_PATH_RESCONF
) == 0);
773 _res
.options
= RES_USE_EDNS0
;
774 TEST_VERIFY (res_init () == 0);
775 /* First res_init clears flag. */
776 TEST_VERIFY (!(_res
.options
& RES_USE_EDNS0
));
777 _res
.options
|= RES_USE_EDNS0
;
778 TEST_VERIFY (res_init () == 0);
779 /* Second res_init preserves flag. */
780 TEST_VERIFY (_res
.options
& RES_USE_EDNS0
);
782 /* Restore empty file. */
783 support_write_file_string (_PATH_RESCONF
, "");
787 /* Second res_init is cumulative. */
788 support_write_file_string (_PATH_RESCONF
,
790 "nameserver 192.0.2.1\n");
791 _res
.options
= RES_USE_EDNS0
;
792 TEST_VERIFY (res_init () == 0);
793 /* First res_init clears flag. */
794 TEST_VERIFY (!(_res
.options
& RES_USE_EDNS0
));
795 /* And sets RES_ROTATE. */
796 TEST_VERIFY (_res
.options
& RES_ROTATE
);
797 _res
.options
|= RES_USE_EDNS0
;
798 TEST_VERIFY (res_init () == 0);
799 /* Second res_init preserves flag. */
800 TEST_VERIFY (_res
.options
& RES_USE_EDNS0
);
801 TEST_VERIFY (_res
.options
& RES_ROTATE
);
802 /* Reloading the configuration does not clear the explicitly set
804 support_write_file_string (_PATH_RESCONF
,
805 "nameserver 192.0.2.1\n"
806 "nameserver 192.0.2.2\n");
807 TEST_VERIFY (res_init () == 0);
808 TEST_VERIFY (_res
.nscount
== 2);
809 TEST_VERIFY (_res
.options
& RES_USE_EDNS0
);
810 /* Whether RES_ROTATE (originally in resolv.conf, now removed)
811 should be preserved is subject to debate. See bug 21701. */
812 /* TEST_VERIFY (!(_res.options & RES_ROTATE)); */
819 support_write_file_string (_PATH_RESCONF
,
821 "nameserver 192.0.2.1\n");
823 case 7: /* 7 and the following tests are with no-reload. */
827 support_write_file_string (_PATH_RESCONF
,
828 "options edns0 no-reload\n"
829 "nameserver 192.0.2.1\n");
832 for (int iteration
= 0; iteration
< 2; ++iteration
)
838 TEST_VERIFY (res_init () == 0);
843 unsigned char buf
[512];
845 (res_mkquery (QUERY
, test_hostname
, C_IN
, T_A
,
846 NULL
, 0, NULL
, buf
, sizeof (buf
)) > 0);
851 gethostbyname (test_hostname
);
857 (void) getaddrinfo (test_hostname
, NULL
, NULL
, &ai
);
861 /* test_index == 7 is res_init and performs a reload even
863 if (iteration
== 0 || test_index
> 7)
865 TEST_VERIFY (_res
.options
& RES_USE_EDNS0
);
866 TEST_VERIFY (!(_res
.options
& RES_ROTATE
));
868 TEST_VERIFY (!(_res
.options
& RES_NORELOAD
));
870 TEST_VERIFY (_res
.options
& RES_NORELOAD
);
871 TEST_VERIFY (_res
.nscount
== 1);
872 /* File change triggers automatic reloading. */
873 support_write_file_string (_PATH_RESCONF
,
875 "nameserver 192.0.2.1\n"
876 "nameserver 192.0.2.2\n");
880 if (test_index
!= 3 && test_index
!= 7)
881 /* test_index 3, 7 are res_init; this function does
882 not reset flags. See bug 21701. */
883 TEST_VERIFY (!(_res
.options
& RES_USE_EDNS0
));
884 TEST_VERIFY (_res
.options
& RES_ROTATE
);
885 TEST_VERIFY (_res
.nscount
== 2);
893 /* Helper function which calls special_test_callback from a
896 special_test_thread_func (void *closure
)
898 special_test_callback (closure
);
902 /* Variant of special_test_callback which runs the function on a
905 run_special_test_on_thread (void *closure
)
907 xpthread_join (xpthread_create (NULL
, special_test_thread_func
, closure
));
909 #endif /* TEST_THREAD */
911 /* Perform the requested special test in a subprocess using
912 special_test_callback. */
914 special_test (unsigned int test_index
)
917 for (int do_thread
= 0; do_thread
< 2; ++do_thread
)
920 void (*func
) (void *) = special_test_callback
;
923 func
= run_special_test_on_thread
;
925 struct support_capture_subprocess proc
926 = support_capture_subprocess (func
, &test_index
);
927 char *test_name
= xasprintf ("special test %u", test_index
);
928 if (strcmp (proc
.out
.buffer
, "") != 0)
930 support_record_failure ();
931 printf ("error: output mismatch for %s\n", test_name
);
932 support_run_diff ("expected", "",
933 "actual", proc
.out
.buffer
);
935 support_capture_subprocess_check (&proc
, test_name
, 0, sc_allow_stdout
);
937 support_capture_subprocess_free (&proc
);
942 /* Dummy DNS server. It ensures that the probe queries sent by
943 gethostbyname and getaddrinfo receive a reply even if the system
944 applies a very strict rate limit to localhost. */
946 start_dummy_server (void)
948 int server_socket
= xsocket (AF_INET
, SOCK_DGRAM
, 0);
950 struct sockaddr_in sin
=
952 .sin_family
= AF_INET
,
953 .sin_addr
= { .s_addr
= htonl (INADDR_LOOPBACK
) },
954 .sin_port
= htons (53),
956 int ret
= bind (server_socket
, (struct sockaddr
*) &sin
, sizeof (sin
));
960 /* The port is reserved, which means we cannot start the
963 FAIL_EXIT1 ("cannot bind socket to port 53: %m");
967 pid_t pid
= xfork ();
970 /* Child process. Echo back queries as SERVFAIL responses. */
976 unsigned char bytes
[512];
978 struct sockaddr_in sin
;
979 socklen_t sinlen
= sizeof (sin
);
981 ssize_t ret
= recvfrom
982 (server_socket
, &packet
, sizeof (packet
),
983 MSG_NOSIGNAL
, (struct sockaddr
*) &sin
, &sinlen
);
985 FAIL_EXIT1 ("recvfrom on fake server socket: %m");
986 if (ret
> sizeof (HEADER
))
988 /* Turn the query into a SERVFAIL response. */
989 packet
.header
.qr
= 1;
990 packet
.header
.rcode
= ns_r_servfail
;
992 /* Send the response. */
993 ret
= sendto (server_socket
, &packet
, ret
,
994 MSG_NOSIGNAL
, (struct sockaddr
*) &sin
, sinlen
);
996 /* The peer may have closed socket prematurely, so
997 this is not an error. */
998 printf ("warning: sending DNS server reply: %m\n");
1003 /* In the parent, close the socket. */
1004 xclose (server_socket
);
1012 support_become_root ();
1013 support_enter_network_namespace ();
1014 if (!support_in_uts_namespace () || !support_can_chroot ())
1015 return EXIT_UNSUPPORTED
;
1017 /* We are in an UTS namespace, so we can set the host name without
1018 altering the state of the entire system. */
1019 if (sethostname (test_hostname
, strlen (test_hostname
)) != 0)
1020 FAIL_EXIT1 ("sethostname: %m");
1022 /* These environment variables affect resolv.conf parsing. */
1023 unsetenv ("LOCALDOMAIN");
1024 unsetenv ("RES_OPTIONS");
1026 /* Ensure that the chroot setup worked. */
1028 struct support_capture_subprocess proc
1029 = support_capture_subprocess (check_chroot_working
, NULL
);
1030 support_capture_subprocess_check (&proc
, "chroot", 0, sc_allow_none
);
1031 support_capture_subprocess_free (&proc
);
1034 pid_t server
= start_dummy_server ();
1036 for (size_t i
= 0; test_cases
[i
].name
!= NULL
; ++i
)
1038 if (test_verbose
> 0)
1039 printf ("info: running test: %s\n", test_cases
[i
].name
);
1040 TEST_VERIFY (test_cases
[i
].conf
!= NULL
);
1041 TEST_VERIFY (test_cases
[i
].expected
!= NULL
);
1043 support_write_file_string (chroot_env
->path_resolv_conf
,
1044 test_cases
[i
].conf
);
1046 test_file_contents (&test_cases
[i
]);
1048 /* The expected output from the empty file test is used for
1050 if (test_cases
[i
].conf
[0] == '\0')
1052 if (test_verbose
> 0)
1053 printf ("info: special test: missing file\n");
1054 TEST_VERIFY (unlink (chroot_env
->path_resolv_conf
) == 0);
1055 test_file_contents (&test_cases
[i
]);
1057 if (test_verbose
> 0)
1058 printf ("info: special test: dangling symbolic link\n");
1059 TEST_VERIFY (symlink ("does-not-exist", chroot_env
->path_resolv_conf
) == 0);
1060 test_file_contents (&test_cases
[i
]);
1061 TEST_VERIFY (unlink (chroot_env
->path_resolv_conf
) == 0);
1063 if (test_verbose
> 0)
1064 printf ("info: special test: unreadable file\n");
1065 support_write_file_string (chroot_env
->path_resolv_conf
, "");
1066 TEST_VERIFY (chmod (chroot_env
->path_resolv_conf
, 0) == 0);
1067 test_file_contents (&test_cases
[i
]);
1069 /* Restore the empty file. */
1070 TEST_VERIFY (unlink (chroot_env
->path_resolv_conf
) == 0);
1071 support_write_file_string (chroot_env
->path_resolv_conf
, "");
1075 /* The tests which do not follow a regular pattern. */
1076 for (unsigned int test_index
= 0;
1077 test_index
< special_tests_count
; ++test_index
)
1078 special_test (test_index
);
1082 if (kill (server
, SIGTERM
) < 0)
1083 FAIL_EXIT1 ("could not terminate server process: %m");
1084 xwaitpid (server
, NULL
, 0);
1087 support_chroot_free (chroot_env
);
1091 #define PREPARE prepare
1092 #include <support/test-driver.c>