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. */
30 #include <support/capture_subprocess.h>
31 #include <support/check.h>
32 #include <support/namespace.h>
33 #include <support/run_diff.h>
34 #include <support/support.h>
35 #include <support/temp_file.h>
36 #include <support/test-driver.h>
37 #include <support/xsocket.h>
38 #include <support/xstdio.h>
39 #include <support/xunistd.h>
42 # include <support/xthread.h>
45 /* This is the host name used to ensure predictable behavior of
47 static const char *const test_hostname
= "www.example.com";
49 /* Path to the test root directory. */
50 static char *path_chroot
;
52 /* Path to resolv.conf under path_chroot (outside the chroot). */
53 static char *path_resolv_conf
;
56 prepare (int argc
, char **argv
)
58 path_chroot
= xasprintf ("%s/tst-resolv-res_init-XXXXXX", test_dir
);
59 if (mkdtemp (path_chroot
) == NULL
)
60 FAIL_EXIT1 ("mkdtemp (\"%s\"): %m", path_chroot
);
61 add_temp_file (path_chroot
);
63 /* Create the /etc directory in the chroot environment. */
64 char *path_etc
= xasprintf ("%s/etc", path_chroot
);
65 xmkdir (path_etc
, 0777);
66 add_temp_file (path_etc
);
68 /* Create an empty resolv.conf file. */
69 path_resolv_conf
= xasprintf ("%s/resolv.conf", path_etc
);
70 add_temp_file (path_resolv_conf
);
71 support_write_file_string (path_resolv_conf
, "");
75 /* valgrind needs a temporary directory in the chroot. */
77 char *path_tmp
= xasprintf ("%s/tmp", path_chroot
);
78 xmkdir (path_tmp
, 0777);
79 add_temp_file (path_tmp
);
84 /* Verify that the chroot environment has been set up. */
86 check_chroot_working (void *closure
)
88 xchroot (path_chroot
);
89 FILE *fp
= xfopen (_PATH_RESCONF
, "r");
92 TEST_VERIFY_EXIT (res_init () == 0);
93 TEST_VERIFY (_res
.options
& RES_INIT
);
96 if (gethostname (buf
, sizeof (buf
)) < 0)
97 FAIL_EXIT1 ("gethostname: %m");
98 if (strcmp (buf
, test_hostname
) != 0)
99 FAIL_EXIT1 ("unexpected host name: %s", buf
);
102 /* If FLAG is set in *OPTIONS, write NAME to FP, and clear it in
105 print_option_flag (FILE *fp
, int *options
, int flag
, const char *name
)
109 fprintf (fp
, " %s", name
);
114 /* Write a decoded version of the resolver configuration *RESP to the
117 print_resp (FILE *fp
, res_state resp
)
119 /* The options directive. */
121 /* RES_INIT is used internally for tracking initialization. */
122 TEST_VERIFY (resp
->options
& RES_INIT
);
123 /* Also mask out other default flags which cannot be set through
124 the options directive. */
126 = resp
->options
& ~(RES_INIT
| RES_RECURSE
| RES_DEFNAMES
| RES_DNSRCH
);
129 || resp
->retrans
!= RES_TIMEOUT
130 || resp
->retry
!= RES_DFLRETRY
)
132 fputs ("options", fp
);
133 if (resp
->ndots
!= 1)
134 fprintf (fp
, " ndots:%d", resp
->ndots
);
135 if (resp
->retrans
!= RES_TIMEOUT
)
136 fprintf (fp
, " timeout:%d", resp
->retrans
);
137 if (resp
->retry
!= RES_DFLRETRY
)
138 fprintf (fp
, " attempts:%d", resp
->retry
);
139 print_option_flag (fp
, &options
, RES_USEVC
, "use-vc");
140 print_option_flag (fp
, &options
, DEPRECATED_RES_USE_INET6
, "inet6");
141 print_option_flag (fp
, &options
, RES_ROTATE
, "rotate");
142 print_option_flag (fp
, &options
, RES_USE_EDNS0
, "edns0");
143 print_option_flag (fp
, &options
, RES_SNGLKUP
,
145 print_option_flag (fp
, &options
, RES_SNGLKUPREOP
,
146 "single-request-reopen");
147 print_option_flag (fp
, &options
, RES_NOTLDQUERY
, "no-tld-query");
150 fprintf (fp
, "; error: unresolved option bits: 0x%x\n", options
);
154 /* The search and domain directives. */
155 if (resp
->dnsrch
[0] != NULL
)
157 fputs ("search", fp
);
158 for (int i
= 0; i
< MAXDNSRCH
&& resp
->dnsrch
[i
] != NULL
; ++i
)
161 fputs (resp
->dnsrch
[i
], fp
);
165 else if (resp
->defdname
[0] != '\0')
166 fprintf (fp
, "domain %s\n", resp
->defdname
);
168 /* The sortlist directive. */
171 fputs ("sortlist", fp
);
172 for (int i
= 0; i
< resp
->nsort
&& i
< MAXRESOLVSORT
; ++i
)
175 if (inet_ntop (AF_INET
, &resp
->sort_list
[i
].addr
,
176 net
, sizeof (net
)) == NULL
)
177 FAIL_EXIT1 ("inet_ntop: %m\n");
179 if (inet_ntop (AF_INET
, &resp
->sort_list
[i
].mask
,
180 mask
, sizeof (mask
)) == NULL
)
181 FAIL_EXIT1 ("inet_ntop: %m\n");
182 fprintf (fp
, " %s/%s", net
, mask
);
187 /* The nameserver directives. */
188 for (size_t i
= 0; i
< resp
->nscount
; ++i
)
190 char host
[NI_MAXHOST
];
191 char service
[NI_MAXSERV
];
193 /* See get_nsaddr in res_send.c. */
196 if (resp
->nsaddr_list
[i
].sin_family
== 0
197 && resp
->_u
._ext
.nsaddrs
[i
] != NULL
)
199 addr
= resp
->_u
._ext
.nsaddrs
[i
];
200 addrlen
= sizeof (*resp
->_u
._ext
.nsaddrs
[i
]);
204 addr
= &resp
->nsaddr_list
[i
];
205 addrlen
= sizeof (resp
->nsaddr_list
[i
]);
208 int ret
= getnameinfo (addr
, addrlen
,
209 host
, sizeof (host
), service
, sizeof (service
),
210 NI_NUMERICHOST
| NI_NUMERICSERV
);
213 if (ret
== EAI_SYSTEM
)
214 fprintf (fp
, "; error: getnameinfo: %m\n");
216 fprintf (fp
, "; error: getnameinfo: %s\n", gai_strerror (ret
));
220 fprintf (fp
, "nameserver %s\n", host
);
221 if (strcmp (service
, "53") != 0)
222 fprintf (fp
, "; unrepresentable port number %s\n\n", service
);
226 TEST_VERIFY (!ferror (fp
));
229 /* Parameters of one test case. */
232 /* A short, descriptive name of the test. */
235 /* The contents of the /etc/resolv.conf file. */
238 /* The expected output from print_resp. */
239 const char *expected
;
241 /* Setting for the LOCALDOMAIN environment variable. NULL if the
242 variable is not to be set. */
243 const char *localdomain
;
245 /* Setting for the RES_OPTIONS environment variable. NULL if the
246 variable is not to be set. */
247 const char *res_options
;
257 test_init_method_last
= test_getaddrinfo
260 /* Closure argument for run_res_init. */
264 const struct test_case
*t
;
268 setup_nss_dns_and_chroot (void)
270 /* Load nss_dns outside of the chroot. */
271 if (dlopen (LIBNSS_DNS_SO
, RTLD_LAZY
) == NULL
)
272 FAIL_EXIT1 ("could not load " LIBNSS_DNS_SO
": %s", dlerror ());
273 xchroot (path_chroot
);
274 /* Force the use of nss_dns. */
275 __nss_configure_lookup ("hosts", "dns");
278 /* Run res_ninit or res_init in a subprocess and dump the parsed
279 resolver state to standard output. */
281 run_res_init (void *closure
)
283 struct test_context
*ctx
= closure
;
284 TEST_VERIFY (getenv ("LOCALDOMAIN") == NULL
);
285 TEST_VERIFY (getenv ("RES_OPTIONS") == NULL
);
286 if (ctx
->t
->localdomain
!= NULL
)
287 setenv ("LOCALDOMAIN", ctx
->t
->localdomain
, 1);
288 if (ctx
->t
->res_options
!= NULL
)
289 setenv ("RES_OPTIONS", ctx
->t
->res_options
, 1);
294 xchroot (path_chroot
);
295 TEST_VERIFY (res_init () == 0);
296 print_resp (stdout
, &_res
);
300 xchroot (path_chroot
);
301 res_state resp
= xmalloc (sizeof (*resp
));
302 memset (resp
, 0, sizeof (*resp
));
303 TEST_VERIFY (res_ninit (resp
) == 0);
304 print_resp (stdout
, resp
);
310 xchroot (path_chroot
);
311 unsigned char buf
[512];
312 TEST_VERIFY (res_mkquery (QUERY
, "www.example",
313 C_IN
, ns_t_a
, NULL
, 0,
314 NULL
, buf
, sizeof (buf
)) > 0);
315 print_resp (stdout
, &_res
);
318 case test_gethostbyname
:
319 setup_nss_dns_and_chroot ();
320 /* Trigger implicit initialization of the _res structure. The
321 actual lookup result is immaterial. */
322 (void )gethostbyname ("www.example");
323 print_resp (stdout
, &_res
);
326 case test_getaddrinfo
:
327 setup_nss_dns_and_chroot ();
328 /* Trigger implicit initialization of the _res structure. The
329 actual lookup result is immaterial. */
331 (void) getaddrinfo ("www.example", NULL
, NULL
, &ai
);
332 print_resp (stdout
, &_res
);
336 FAIL_EXIT1 ("invalid init method %d", ctx
->init
);
340 /* Helper function which calls run_res_init from a thread. */
342 run_res_init_thread_func (void *closure
)
344 run_res_init (closure
);
348 /* Variant of res_run_init which runs the function on a non-main
351 run_res_init_on_thread (void *closure
)
353 xpthread_join (xpthread_create (NULL
, run_res_init_thread_func
, closure
));
355 #endif /* TEST_THREAD */
357 struct test_case test_cases
[] =
359 {.name
= "empty file",
361 .expected
= "search example.com\n"
362 "nameserver 127.0.0.1\n"
364 {.name
= "empty file with LOCALDOMAIN",
366 .expected
= "search example.net\n"
367 "nameserver 127.0.0.1\n",
368 .localdomain
= "example.net",
370 {.name
= "empty file with RES_OPTIONS",
372 .expected
= "options attempts:5 edns0\n"
373 "search example.com\n"
374 "nameserver 127.0.0.1\n",
375 .res_options
= "edns0 attempts:5",
377 {.name
= "empty file with RES_OPTIONS and LOCALDOMAIN",
379 .expected
= "options attempts:5 edns0\n"
380 "search example.org\n"
381 "nameserver 127.0.0.1\n",
382 .localdomain
= "example.org",
383 .res_options
= "edns0 attempts:5",
386 .conf
= "domain example.net\n"
387 "search corp.example.com example.com\n"
388 "nameserver 192.0.2.1\n",
389 .expected
= "search corp.example.com example.com\n"
390 "nameserver 192.0.2.1\n"
392 {.name
= "whitespace",
393 .conf
= "# This test covers comment and whitespace processing "
394 " (trailing whitespace,\n"
395 "# missing newline at end of file).\n"
397 "domain example.net\n"
398 ";search commented out\n"
399 "search corp.example.com\texample.com\n"
400 "#nameserver 192.0.2.3\n"
401 "nameserver 192.0.2.1 \n"
402 "nameserver 192.0.2.2", /* No \n at end of file. */
403 .expected
= "search corp.example.com example.com\n"
404 "nameserver 192.0.2.1\n"
405 "nameserver 192.0.2.2\n"
407 {.name
= "option values, multiple servers",
408 .conf
= "options\tinet6\tndots:3 edns0\tattempts:5\ttimeout:19\n"
409 "domain example.net\n"
411 "search corp.example.com\texample.com\n"
412 "nameserver 192.0.2.1\n"
414 "nameserver 192.0.2.2\n",
415 .expected
= "options ndots:3 timeout:19 attempts:5 inet6 edns0\n"
416 "search corp.example.com example.com\n"
417 "nameserver 192.0.2.1\n"
419 "nameserver 192.0.2.2\n"
421 {.name
= "out-of-range option vales",
422 .conf
= "options use-vc timeout:999 attempts:999 ndots:99\n"
423 "search example.com\n",
424 .expected
= "options ndots:15 timeout:30 attempts:5 use-vc\n"
425 "search example.com\n"
426 "nameserver 127.0.0.1\n"
428 {.name
= "repeated directives",
429 .conf
= "options ndots:3 use-vc\n"
430 "options edns0 ndots:2\n"
431 "domain corp.example\n"
432 "search example.net corp.example.com example.com\n"
433 "search example.org\n"
435 .expected
= "options ndots:2 use-vc edns0\n"
436 "search example.org\n"
437 "nameserver 127.0.0.1\n"
439 {.name
= "many name servers, sortlist",
440 .conf
= "options single-request\n"
441 "search example.org example.com example.net corp.example.com\n"
442 "sortlist 192.0.2.0/255.255.255.0\n"
443 "nameserver 192.0.2.1\n"
444 "nameserver 192.0.2.2\n"
445 "nameserver 192.0.2.3\n"
446 "nameserver 192.0.2.4\n"
447 "nameserver 192.0.2.5\n"
448 "nameserver 192.0.2.6\n"
449 "nameserver 192.0.2.7\n"
450 "nameserver 192.0.2.8\n",
451 .expected
= "options single-request\n"
452 "search example.org example.com example.net corp.example.com\n"
453 "sortlist 192.0.2.0/255.255.255.0\n"
454 "nameserver 192.0.2.1\n"
455 "nameserver 192.0.2.2\n"
456 "nameserver 192.0.2.3\n"
458 {.name
= "IPv4 and IPv6 nameservers",
459 .conf
= "options single-request\n"
460 "search example.org example.com example.net corp.example.com"
461 " legacy.example.com\n"
462 "sortlist 192.0.2.0\n"
463 "nameserver 192.0.2.1\n"
464 "nameserver 2001:db8::2\n"
465 "nameserver 192.0.2.3\n"
466 "nameserver 2001:db8::4\n"
467 "nameserver 192.0.2.5\n"
468 "nameserver 2001:db8::6\n"
469 "nameserver 192.0.2.7\n"
470 "nameserver 2001:db8::8\n",
471 .expected
= "options single-request\n"
472 "search example.org example.com example.net corp.example.com"
473 " legacy.example.com\n"
474 "sortlist 192.0.2.0/255.255.255.0\n"
475 "nameserver 192.0.2.1\n"
476 "nameserver 2001:db8::2\n"
477 "nameserver 192.0.2.3\n"
479 {.name
= "garbage after nameserver",
480 .conf
= "nameserver 192.0.2.1 garbage\n"
481 "nameserver 192.0.2.2:5353\n"
482 "nameserver 192.0.2.3 5353\n",
483 .expected
= "search example.com\n"
484 "nameserver 192.0.2.1\n"
485 "nameserver 192.0.2.3\n"
487 {.name
= "RES_OPTIONS is cummulative",
488 .conf
= "options timeout:7 ndots:2 use-vc\n"
489 "nameserver 192.0.2.1\n",
490 .expected
= "options ndots:3 timeout:7 attempts:5 use-vc edns0\n"
491 "search example.com\n"
492 "nameserver 192.0.2.1\n",
493 .res_options
= "attempts:5 ndots:3 edns0 ",
498 /* Run the indicated test case. This function assumes that the chroot
499 contents has already been set up. */
501 test_file_contents (const struct test_case
*t
)
504 for (int do_thread
= 0; do_thread
< 2; ++do_thread
)
506 for (int init_method
= 0; init_method
<= test_init_method_last
;
509 if (test_verbose
> 0)
510 printf ("info: testing init method %d\n", init_method
);
511 struct test_context ctx
= { .init
= init_method
, .t
= t
};
512 void (*func
) (void *) = run_res_init
;
515 func
= run_res_init_on_thread
;
517 struct support_capture_subprocess proc
518 = support_capture_subprocess (func
, &ctx
);
519 if (strcmp (proc
.out
.buffer
, t
->expected
) != 0)
521 support_record_failure ();
522 printf ("error: output mismatch for %s\n", t
->name
);
523 support_run_diff ("expected", t
->expected
,
524 "actual", proc
.out
.buffer
);
526 support_capture_subprocess_check (&proc
, t
->name
, 0,
528 support_capture_subprocess_free (&proc
);
532 /* Dummy DNS server. It ensures that the probe queries sent by
533 gethostbyname and getaddrinfo receive a reply even if the system
534 applies a very strict rate limit to localhost. */
536 start_dummy_server (void)
538 int server_socket
= xsocket (AF_INET
, SOCK_DGRAM
, 0);
540 struct sockaddr_in sin
=
542 .sin_family
= AF_INET
,
543 .sin_addr
= { .s_addr
= htonl (INADDR_LOOPBACK
) },
544 .sin_port
= htons (53),
546 int ret
= bind (server_socket
, (struct sockaddr
*) &sin
, sizeof (sin
));
550 /* The port is reserved, which means we cannot start the
553 FAIL_EXIT1 ("cannot bind socket to port 53: %m");
557 pid_t pid
= xfork ();
560 /* Child process. Echo back queries as SERVFAIL responses. */
566 unsigned char bytes
[512];
568 struct sockaddr_in sin
;
569 socklen_t sinlen
= sizeof (sin
);
571 ssize_t ret
= recvfrom
572 (server_socket
, &packet
, sizeof (packet
),
573 MSG_NOSIGNAL
, (struct sockaddr
*) &sin
, &sinlen
);
575 FAIL_EXIT1 ("recvfrom on fake server socket: %m");
576 if (ret
> sizeof (HEADER
))
578 /* Turn the query into a SERVFAIL response. */
579 packet
.header
.qr
= 1;
580 packet
.header
.rcode
= ns_r_servfail
;
582 /* Send the response. */
583 ret
= sendto (server_socket
, &packet
, ret
,
584 MSG_NOSIGNAL
, (struct sockaddr
*) &sin
, sinlen
);
586 /* The peer may have closed socket prematurely, so
587 this is not an error. */
588 printf ("warning: sending DNS server reply: %m\n");
593 /* In the parent, close the socket. */
594 xclose (server_socket
);
602 support_become_root ();
603 support_enter_network_namespace ();
604 if (!support_in_uts_namespace () || !support_can_chroot ())
605 return EXIT_UNSUPPORTED
;
607 /* We are in an UTS namespace, so we can set the host name without
608 altering the state of the entire system. */
609 if (sethostname (test_hostname
, strlen (test_hostname
)) != 0)
610 FAIL_EXIT1 ("sethostname: %m");
612 /* These environment variables affect resolv.conf parsing. */
613 unsetenv ("LOCALDOMAIN");
614 unsetenv ("RES_OPTIONS");
616 /* Ensure that the chroot setup worked. */
618 struct support_capture_subprocess proc
619 = support_capture_subprocess (check_chroot_working
, NULL
);
620 support_capture_subprocess_check (&proc
, "chroot", 0, sc_allow_none
);
621 support_capture_subprocess_free (&proc
);
624 pid_t server
= start_dummy_server ();
626 for (size_t i
= 0; test_cases
[i
].name
!= NULL
; ++i
)
628 if (test_verbose
> 0)
629 printf ("info: running test: %s\n", test_cases
[i
].name
);
630 TEST_VERIFY (test_cases
[i
].conf
!= NULL
);
631 TEST_VERIFY (test_cases
[i
].expected
!= NULL
);
633 support_write_file_string (path_resolv_conf
, test_cases
[i
].conf
);
635 test_file_contents (&test_cases
[i
]);
637 /* The expected output from the empty file test is used for
639 if (test_cases
[i
].conf
[0] == '\0')
641 if (test_verbose
> 0)
642 printf ("info: special test: missing file\n");
643 TEST_VERIFY (unlink (path_resolv_conf
) == 0);
644 test_file_contents (&test_cases
[i
]);
646 if (test_verbose
> 0)
647 printf ("info: special test: dangling symbolic link\n");
648 TEST_VERIFY (symlink ("does-not-exist", path_resolv_conf
) == 0);
649 test_file_contents (&test_cases
[i
]);
650 TEST_VERIFY (unlink (path_resolv_conf
) == 0);
652 if (test_verbose
> 0)
653 printf ("info: special test: unreadable file\n");
654 support_write_file_string (path_resolv_conf
, "");
655 TEST_VERIFY (chmod (path_resolv_conf
, 0) == 0);
656 test_file_contents (&test_cases
[i
]);
658 /* Restore the empty file. */
659 TEST_VERIFY (unlink (path_resolv_conf
) == 0);
660 support_write_file_string (path_resolv_conf
, "");
666 if (kill (server
, SIGTERM
) < 0)
667 FAIL_EXIT1 ("could not terminate server process: %m");
668 xwaitpid (server
, NULL
, 0);
673 free (path_resolv_conf
);
674 path_resolv_conf
= NULL
;
678 #define PREPARE prepare
679 #include <support/test-driver.c>