Warning: That file was not part of the compilation database. It may have many parsing errors.

1/* Test basic nss_dns functionality with multiple threads.
2 Copyright (C) 2016-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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/>. */
18
19/* Unlike tst-resolv-basic, this test does not overwrite the _res
20 structure and relies on namespaces to achieve the redirection to
21 the test servers with a custom /etc/resolv.conf file. */
22
23#include <dlfcn.h>
24#include <errno.h>
25#include <gnu/lib-names.h>
26#include <netdb.h>
27#include <resolv/resolv-internal.h>
28#include <resolv/resolv_context.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <support/check.h>
33#include <support/namespace.h>
34#include <support/resolv_test.h>
35#include <support/support.h>
36#include <support/temp_file.h>
37#include <support/test-driver.h>
38#include <support/xthread.h>
39#include <support/xunistd.h>
40
41/* Each client thread sends this many queries. */
42enum { queries_per_thread = 500 };
43
44/* Return a small positive number identifying this thread. */
45static int
46get_thread_number (void)
47{
48 static int __thread local;
49 if (local != 0)
50 return local;
51 static int global = 1;
52 local = __atomic_fetch_add (&global, 1, __ATOMIC_RELAXED);
53 return local;
54}
55
56static void
57response (const struct resolv_response_context *ctx,
58 struct resolv_response_builder *b,
59 const char *qname, uint16_t qclass, uint16_t qtype)
60{
61 TEST_VERIFY_EXIT (qname != NULL);
62
63 int counter = 0;
64 int thread = 0;
65 int dummy = 0;
66 TEST_VERIFY (sscanf (qname, "counter%d.thread%d.example.com%n",
67 &counter, &thread, &dummy) == 2);
68 TEST_VERIFY (dummy > 0);
69
70 struct resolv_response_flags flags = { 0 };
71 resolv_response_init (b, flags);
72 resolv_response_add_question (b, qname, qclass, qtype);
73
74 resolv_response_section (b, ns_s_an);
75 resolv_response_open_record (b, qname, qclass, qtype, 0);
76 switch (qtype)
77 {
78 case T_A:
79 {
80 char ipv4[4] = {10, 0, counter, thread};
81 resolv_response_add_data (b, &ipv4, sizeof (ipv4));
82 }
83 break;
84 case T_AAAA:
85 {
86 char ipv6[16]
87 = {0x20, 0x01, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0,
88 counter, 0, thread, 0, 0};
89 resolv_response_add_data (b, &ipv6, sizeof (ipv6));
90 }
91 break;
92 default:
93 support_record_failure ();
94 printf ("error: unexpected QTYPE: %s/%u/%u\n",
95 qname, qclass, qtype);
96 }
97 resolv_response_close_record (b);
98}
99
100/* Check that the resolver configuration for this thread has an
101 extended resolver configuration. */
102static void
103check_have_conf (void)
104{
105 struct resolv_context *ctx = __resolv_context_get ();
106 TEST_VERIFY_EXIT (ctx != NULL);
107 TEST_VERIFY (ctx->conf != NULL);
108 __resolv_context_put (ctx);
109}
110
111/* Verify that E matches the expected response for FAMILY and
112 COUNTER. */
113static void
114check_hostent (const char *caller, const char *function, const char *qname,
115 int ret, struct hostent *e, int family, int counter)
116{
117 if (ret != 0)
118 {
119 errno = ret;
120 support_record_failure ();
121 printf ("error: %s: %s for %s failed: %m\n", caller, function, qname);
122 return;
123 }
124
125 TEST_VERIFY_EXIT (e != NULL);
126 TEST_VERIFY (strcmp (qname, e->h_name) == 0);
127 TEST_VERIFY (e->h_addrtype == family);
128 TEST_VERIFY_EXIT (e->h_addr_list[0] != NULL);
129 TEST_VERIFY (e->h_addr_list[1] == NULL);
130 switch (family)
131 {
132 case AF_INET:
133 {
134 char addr[4] = {10, 0, counter, get_thread_number ()};
135 TEST_VERIFY (e->h_length == sizeof (addr));
136 TEST_VERIFY (memcmp (e->h_addr_list[0], addr, sizeof (addr)) == 0);
137 }
138 break;
139 case AF_INET6:
140 {
141 char addr[16]
142 = {0x20, 0x01, 0xd, 0xb8, 0, 0, 0, 0, 0, 0,
143 0, counter, 0, get_thread_number (), 0, 0};
144 TEST_VERIFY (e->h_length == sizeof (addr));
145 TEST_VERIFY (memcmp (e->h_addr_list[0], addr, sizeof (addr)) == 0);
146 }
147 break;
148 default:
149 FAIL_EXIT1 ("%s: invalid address family %d", caller, family);
150 }
151 check_have_conf ();
152}
153
154/* Check a getaddrinfo result. */
155static void
156check_addrinfo (const char *caller, const char *qname,
157 int ret, struct addrinfo *ai, int family, int counter)
158{
159 if (ret != 0)
160 {
161 support_record_failure ();
162 printf ("error: %s: getaddrinfo for %s failed: %s\n",
163 caller, qname, gai_strerror (ret));
164 return;
165 }
166
167 TEST_VERIFY_EXIT (ai != NULL);
168
169 /* Check that available data matches the requirements. */
170 bool have_ipv4 = false;
171 bool have_ipv6 = false;
172 for (struct addrinfo *p = ai; p != NULL; p = p->ai_next)
173 {
174 TEST_VERIFY (p->ai_socktype == SOCK_STREAM);
175 TEST_VERIFY (p->ai_protocol == IPPROTO_TCP);
176 TEST_VERIFY_EXIT (p->ai_addr != NULL);
177 TEST_VERIFY (p->ai_addr->sa_family == p->ai_family);
178
179 switch (p->ai_family)
180 {
181 case AF_INET:
182 {
183 TEST_VERIFY (!have_ipv4);
184 have_ipv4 = true;
185 struct sockaddr_in *sa = (struct sockaddr_in *) p->ai_addr;
186 TEST_VERIFY (p->ai_addrlen == sizeof (*sa));
187 char addr[4] = {10, 0, counter, get_thread_number ()};
188 TEST_VERIFY (memcmp (&sa->sin_addr, addr, sizeof (addr)) == 0);
189 TEST_VERIFY (ntohs (sa->sin_port) == 80);
190 }
191 break;
192 case AF_INET6:
193 {
194 TEST_VERIFY (!have_ipv6);
195 have_ipv6 = true;
196 struct sockaddr_in6 *sa = (struct sockaddr_in6 *) p->ai_addr;
197 TEST_VERIFY (p->ai_addrlen == sizeof (*sa));
198 char addr[16]
199 = {0x20, 0x01, 0xd, 0xb8, 0, 0, 0, 0, 0, 0,
200 0, counter, 0, get_thread_number (), 0, 0};
201 TEST_VERIFY (memcmp (&sa->sin6_addr, addr, sizeof (addr)) == 0);
202 TEST_VERIFY (ntohs (sa->sin6_port) == 80);
203 }
204 break;
205 default:
206 FAIL_EXIT1 ("%s: invalid address family %d", caller, family);
207 }
208 }
209
210 switch (family)
211 {
212 case AF_INET:
213 TEST_VERIFY (have_ipv4);
214 TEST_VERIFY (!have_ipv6);
215 break;
216 case AF_INET6:
217 TEST_VERIFY (!have_ipv4);
218 TEST_VERIFY (have_ipv6);
219 break;
220 case AF_UNSPEC:
221 TEST_VERIFY (have_ipv4);
222 TEST_VERIFY (have_ipv6);
223 break;
224 default:
225 FAIL_EXIT1 ("%s: invalid address family %d", caller, family);
226 }
227
228 check_have_conf ();
229}
230
231/* This barrier ensures that all test threads begin their work
232 simultaneously. */
233static pthread_barrier_t barrier;
234
235/* Test gethostbyname2_r (if do_2 is false) or gethostbyname2_r with
236 AF_INET (if do_2 is true). */
237static void *
238byname (bool do_2)
239{
240 int this_thread = get_thread_number ();
241 xpthread_barrier_wait (&barrier);
242 for (int i = 0; i < queries_per_thread; ++i)
243 {
244 char qname[100];
245 snprintf (qname, sizeof (qname), "counter%d.thread%d.example.com",
246 i, this_thread);
247 struct hostent storage;
248 char buf[1000];
249 struct hostent *e = NULL;
250 int herrno;
251 int ret;
252 if (do_2)
253 ret = gethostbyname_r (qname, &storage, buf, sizeof (buf),
254 &e, &herrno);
255 else
256 ret = gethostbyname2_r (qname, AF_INET, &storage, buf, sizeof (buf),
257 &e, &herrno);
258 check_hostent (__func__, do_2 ? "gethostbyname2_r" : "gethostbyname_r",
259 qname, ret, e, AF_INET, i);
260 }
261 check_have_conf ();
262 return NULL;
263}
264
265/* Test gethostbyname_r. */
266static void *
267thread_byname (void *closure)
268{
269 return byname (false);
270}
271
272/* Test gethostbyname2_r with AF_INET. */
273static void *
274thread_byname2 (void *closure)
275{
276 return byname (true);
277}
278
279/* Call gethostbyname_r with RES_USE_INET6 (if do_2 is false), or
280 gethostbyname_r with AF_INET6 (if do_2 is true). */
281static void *
282byname_inet6 (bool do_2)
283{
284 int this_thread = get_thread_number ();
285 xpthread_barrier_wait (&barrier);
286 if (!do_2)
287 {
288 res_init ();
289 _res.options |= DEPRECATED_RES_USE_INET6;
290 TEST_VERIFY (strcmp (_res.defdname, "example.com") == 0);
291 }
292 for (int i = 0; i < queries_per_thread; ++i)
293 {
294 char qname[100];
295 snprintf (qname, sizeof (qname), "counter%d.thread%d.example.com",
296 i, this_thread);
297 struct hostent storage;
298 char buf[1000];
299 struct hostent *e = NULL;
300 int herrno;
301 int ret;
302 if (do_2)
303 ret = gethostbyname2_r (qname, AF_INET6, &storage, buf, sizeof (buf),
304 &e, &herrno);
305 else
306 ret = gethostbyname_r (qname, &storage, buf, sizeof (buf),
307 &e, &herrno);
308 check_hostent (__func__,
309 do_2 ? "gethostbyname2_r" : "gethostbyname_r",
310 qname, ret, e, AF_INET6, i);
311 }
312 return NULL;
313}
314
315/* Test gethostbyname_r with AF_INET6. */
316static void *
317thread_byname_inet6 (void *closure)
318{
319 return byname_inet6 (false);
320}
321
322/* Test gethostbyname2_r with AF_INET6. */
323static void *
324thread_byname2_af_inet6 (void *closure)
325{
326 return byname_inet6 (true);
327}
328
329/* Run getaddrinfo tests for FAMILY. */
330static void *
331gai (int family, bool do_inet6)
332{
333 int this_thread = get_thread_number ();
334 xpthread_barrier_wait (&barrier);
335 if (do_inet6)
336 {
337 res_init ();
338 _res.options |= DEPRECATED_RES_USE_INET6;
339 check_have_conf ();
340 }
341 for (int i = 0; i < queries_per_thread; ++i)
342 {
343 char qname[100];
344 snprintf (qname, sizeof (qname), "counter%d.thread%d.example.com",
345 i, this_thread);
346 struct addrinfo hints =
347 {
348 .ai_family = family,
349 .ai_socktype = SOCK_STREAM,
350 .ai_protocol = IPPROTO_TCP,
351 };
352 struct addrinfo *ai;
353 int ret = getaddrinfo (qname, "80", &hints, &ai);
354 check_addrinfo (__func__, qname, ret, ai, family, i);
355 if (ret == 0)
356 freeaddrinfo (ai);
357 }
358 return NULL;
359}
360
361/* Test getaddrinfo with AF_INET. */
362static void *
363thread_gai_inet (void *closure)
364{
365 return gai (AF_INET, false);
366}
367
368/* Test getaddrinfo with AF_INET6. */
369static void *
370thread_gai_inet6 (void *closure)
371{
372 return gai (AF_INET6, false);
373}
374
375/* Test getaddrinfo with AF_UNSPEC. */
376static void *
377thread_gai_unspec (void *closure)
378{
379 return gai (AF_UNSPEC, false);
380}
381
382/* Test getaddrinfo with AF_INET. */
383static void *
384thread_gai_inet_inet6 (void *closure)
385{
386 return gai (AF_INET, true);
387}
388
389/* Test getaddrinfo with AF_INET6. */
390static void *
391thread_gai_inet6_inet6 (void *closure)
392{
393 return gai (AF_INET6, true);
394}
395
396/* Test getaddrinfo with AF_UNSPEC. */
397static void *
398thread_gai_unspec_inet6 (void *closure)
399{
400 return gai (AF_UNSPEC, true);
401}
402
403/* Description of the chroot environment used to run the tests. */
404static struct support_chroot *chroot_env;
405
406/* Set up the chroot environment. */
407static void
408prepare (int argc, char **argv)
409{
410 chroot_env = support_chroot_create
411 ((struct support_chroot_configuration)
412 {
413 .resolv_conf =
414 "search example.com\n"
415 "nameserver 127.0.0.1\n"
416 "nameserver 127.0.0.2\n"
417 "nameserver 127.0.0.3\n",
418 });
419}
420
421static int
422do_test (void)
423{
424 support_become_root ();
425 if (!support_enter_network_namespace ())
426 return EXIT_UNSUPPORTED;
427 if (!support_can_chroot ())
428 return EXIT_UNSUPPORTED;
429
430 /* Load the shared object outside of the chroot. */
431 TEST_VERIFY (dlopen (LIBNSS_DNS_SO, RTLD_LAZY) != NULL);
432
433 xchroot (chroot_env->path_chroot);
434 TEST_VERIFY_EXIT (chdir ("/") == 0);
435
436 struct sockaddr_in server_address =
437 {
438 .sin_family = AF_INET,
439 .sin_addr = { .s_addr = htonl (INADDR_LOOPBACK) },
440 .sin_port = htons (53)
441 };
442 const struct sockaddr *server_addresses[1] =
443 { (const struct sockaddr *) &server_address };
444
445 struct resolv_test *aux = resolv_test_start
446 ((struct resolv_redirect_config)
447 {
448 .response_callback = response,
449 .nscount = 1,
450 .disable_redirect = true,
451 .server_address_overrides = server_addresses,
452 });
453
454 enum { thread_count = 10 };
455 xpthread_barrier_init (&barrier, NULL, thread_count + 1);
456 pthread_t threads[thread_count];
457 typedef void *(*thread_func) (void *);
458 thread_func thread_funcs[thread_count] =
459 {
460 thread_byname,
461 thread_byname2,
462 thread_byname_inet6,
463 thread_byname2_af_inet6,
464 thread_gai_inet,
465 thread_gai_inet6,
466 thread_gai_unspec,
467 thread_gai_inet_inet6,
468 thread_gai_inet6_inet6,
469 thread_gai_unspec_inet6,
470 };
471 for (int i = 0; i < thread_count; ++i)
472 threads[i] = xpthread_create (NULL, thread_funcs[i], NULL);
473 xpthread_barrier_wait (&barrier); /* Start the test threads. */
474 for (int i = 0; i < thread_count; ++i)
475 xpthread_join (threads[i]);
476
477 resolv_test_end (aux);
478 support_chroot_free (chroot_env);
479
480 return 0;
481}
482
483#define PREPARE prepare
484#include <support/test-driver.c>
485

Warning: That file was not part of the compilation database. It may have many parsing errors.