1 /* Test search/default domain name behavior.
2 Copyright (C) 2016-2018 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/>. */
22 #include <support/check.h>
23 #include <support/check_nss.h>
24 #include <support/resolv_test.h>
25 #include <support/support.h>
26 #include <support/xmemstream.h>
34 const struct item items
[] =
36 {"hostname.usersys.example.com", 1},
37 {"hostname.corp.example.com", 1},
38 {"hostname.example.com", 1},
40 {"mail.corp.example.com", 1},
41 {"mail.example.com", 1},
43 {"file.corp.example.com", 2},
45 {"file.example.com", 1},
46 {"servfail-usersys.usersys.example.com", -ns_r_servfail
},
47 {"servfail-usersys.corp.example.com", 1},
48 {"servfail-usersys.example.com", 1},
49 {"servfail-corp.usersys.example.com", 1},
50 {"servfail-corp.corp.example.com", -ns_r_servfail
},
51 {"servfail-corp.example.com", 1},
52 {"www.example.com", 1},
53 {"large.example.com", 200},
55 /* Test query amplification with a SERVFAIL response combined with
57 {"large-servfail.usersys.example.com", -ns_r_servfail
},
58 {"large-servfail.example.com", 2000},
69 find_name (const char *name
)
71 for (int i
= 0; items
[i
].name
!= NULL
; ++i
)
73 if (strcmp (name
, items
[i
].name
) == 0)
76 if (strcmp (name
, "example.com") == 0
77 || strcmp (name
, "usersys.example.com") == 0
78 || strcmp (name
, "corp.example.com") == 0)
80 return name_not_found
;
83 static int rcode_override_server_index
= -1;
84 static int rcode_override
;
87 response (const struct resolv_response_context
*ctx
,
88 struct resolv_response_builder
*b
,
89 const char *qname
, uint16_t qclass
, uint16_t qtype
)
91 if (ctx
->server_index
== rcode_override_server_index
)
93 struct resolv_response_flags flags
= {.rcode
= rcode_override
};
94 resolv_response_init (b
, flags
);
95 resolv_response_add_question (b
, qname
, qclass
, qtype
);
99 int index
= find_name (qname
);
100 struct resolv_response_flags flags
= {};
101 if (index
== name_not_found
)
102 flags
.rcode
= ns_r_nxdomain
;
103 else if (index
>= 0 && items
[index
].response
< 0)
104 flags
.rcode
= -items
[index
].response
;
105 else if (index
>= 0 && items
[index
].response
> 5 && !ctx
->tcp
)
106 /* Force TCP if more than 5 addresses where requested. */
108 resolv_response_init (b
, flags
);
109 resolv_response_add_question (b
, qname
, qclass
, qtype
);
111 if (flags
.tc
|| index
< 0 || items
[index
].response
< 0)
114 resolv_response_section (b
, ns_s_an
);
116 for (int i
= 0; i
< items
[index
].response
; ++i
)
118 resolv_response_open_record (b
, qname
, qclass
, qtype
, 0);
124 char addr
[4] = {10, index
, i
>> 8, i
};
125 resolv_response_add_data (b
, addr
, sizeof (addr
));
131 = {0x20, 0x01, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0,
132 0, index
+ 1, (i
+ 1) >> 8, i
+ 1};
133 resolv_response_add_data (b
, addr
, sizeof (addr
));
137 support_record_failure ();
138 printf ("error: unexpected QTYPE: %s/%u/%u\n",
139 qname
, qclass
, qtype
);
141 resolv_response_close_record (b
);
147 format_get
, format_gai
151 format_expected_1 (FILE *out
, int family
, enum output_format format
, int index
)
153 for (int i
= 0; i
< items
[index
].response
; ++i
)
159 snprintf (address
, sizeof (address
), "10.%d.%d.%d",
160 index
, (i
>> 8) & 0xff, i
& 0xff);
163 snprintf (address
, sizeof (address
), "2001:db8::%x:%x",
167 FAIL_EXIT1 ("unreachable");
173 fprintf (out
, "address: %s\n", address
);
176 fprintf (out
, "address: STREAM/TCP %s 80\n", address
);
182 format_expected (const char *fqdn
, int family
, enum output_format format
)
184 int index
= find_name (fqdn
);
185 TEST_VERIFY_EXIT (index
>= 0);
186 struct xmemstream stream
;
187 xopen_memstream (&stream
);
189 TEST_VERIFY_EXIT (items
[index
].response
>= 0);
190 if (format
== format_get
)
191 fprintf (stream
.out
, "name: %s\n", items
[index
].name
);
192 if (family
== AF_INET
|| family
== AF_UNSPEC
)
193 format_expected_1 (stream
.out
, AF_INET
, format
, index
);
194 if (family
== AF_INET6
|| family
== AF_UNSPEC
)
195 format_expected_1 (stream
.out
, AF_INET6
, format
, index
);
197 xfclose_memstream (&stream
);
198 return stream
.buffer
;
202 do_get (const char *name
, const char *fqdn
, int family
)
204 char *expected
= format_expected (fqdn
, family
, format_get
);
205 if (family
== AF_INET
)
207 char *query
= xasprintf ("gethostbyname (\"%s\")", name
);
208 check_hostent (query
, gethostbyname (name
), expected
);
211 char *query
= xasprintf ("gethostbyname2 (\"%s\", %d)", name
, family
);
212 check_hostent (query
, gethostbyname2 (name
, family
), expected
);
214 /* Test res_search. */
230 unsigned char *response
= xmalloc (sz
);
231 int ret
= res_search (name
, C_IN
, qtype
, response
, sz
);
232 TEST_VERIFY_EXIT (ret
>= 0);
235 /* Truncation. Retry with a larger buffer. */
237 unsigned char *newptr
= xrealloc (response
, sz
);
240 ret
= res_search (name
, C_IN
, qtype
, response
, sz
);
241 TEST_VERIFY_EXIT (ret
>= 0);
242 TEST_VERIFY_EXIT (ret
< sz
);
244 check_dns_packet (query
, response
, ret
, expected
);
253 do_gai (const char *name
, const char *fqdn
, int family
)
255 struct addrinfo hints
=
258 .ai_protocol
= IPPROTO_TCP
,
259 .ai_socktype
= SOCK_STREAM
262 char *query
= xasprintf ("%s:80 [%d]", name
, family
);
263 int ret
= getaddrinfo (name
, "80", &hints
, &ai
);
264 char *expected
= format_expected (fqdn
, family
, format_gai
);
265 check_addrinfo (query
, ai
, ret
, expected
);
273 do_both (const char *name
, const char *fqdn
)
275 do_get (name
, fqdn
, AF_INET
);
276 do_get (name
, fqdn
, AF_INET6
);
277 do_gai (name
, fqdn
, AF_INET
);
278 do_gai (name
, fqdn
, AF_INET6
);
279 do_gai (name
, fqdn
, AF_UNSPEC
);
283 do_test_all (bool unconnectable_server
)
285 struct resolv_redirect_config config
=
287 .response_callback
= response
,
288 .search
= {"usersys.example.com", "corp.example.com", "example.com"},
290 struct resolv_test
*obj
= resolv_test_start (config
);
292 if (unconnectable_server
)
294 /* 255.255.255.255 results in an immediate connect failure. The
295 next server will supply the answer instead. This is a
296 triggering condition for bug 19791. */
297 _res
.nsaddr_list
[0].sin_addr
.s_addr
= -1;
298 _res
.nsaddr_list
[0].sin_port
= htons (53);
301 do_both ("file", "file.corp.example.com");
302 do_both ("www", "www.example.com");
303 do_both ("servfail-usersys", "servfail-usersys.corp.example.com");
304 do_both ("servfail-corp", "servfail-corp.usersys.example.com");
305 do_both ("large", "large.example.com");
306 do_both ("large-servfail", "large-servfail.example.com");
307 do_both ("file.corp", "file.corp");
309 /* Check that SERVFAIL and REFUSED responses do not alter the search
311 rcode_override_server_index
= 0;
312 rcode_override
= ns_r_servfail
;
313 do_both ("hostname", "hostname.usersys.example.com");
314 do_both ("large", "large.example.com");
315 do_both ("large-servfail", "large-servfail.example.com");
316 rcode_override
= ns_r_refused
;
317 do_both ("hostname", "hostname.usersys.example.com");
318 do_both ("large", "large.example.com");
319 do_both ("large-servfail", "large-servfail.example.com");
320 /* Likewise, but with an NXDOMAIN for the first search path
322 rcode_override
= ns_r_servfail
;
323 do_both ("mail", "mail.corp.example.com");
324 rcode_override
= ns_r_refused
;
325 do_both ("mail", "mail.corp.example.com");
326 /* Likewise, but with ndots handling. */
327 rcode_override
= ns_r_servfail
;
328 do_both ("file.corp", "file.corp");
329 rcode_override
= ns_r_refused
;
330 do_both ("file.corp", "file.corp");
332 resolv_test_end (obj
);
338 for (int unconnectable_server
= 0; unconnectable_server
< 2;
339 ++unconnectable_server
)
340 do_test_all (unconnectable_server
);
344 #include <support/test-driver.c>