1 /* Test alias handling (mainly for gethostbyname).
2 Copyright (C) 2022-2024 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 <https://www.gnu.org/licenses/>. */
19 #include <array_length.h>
20 #include <arpa/inet.h>
26 #include <support/check.h>
27 #include <support/check_nss.h>
28 #include <support/resolv_test.h>
29 #include <support/support.h>
31 #include "tst-resolv-maybe_insert_sig.h"
35 aADDRESSES-cCNAMES.example.net
37 CNAMES is the length of the CNAME chain, ADDRESSES is the number of
38 addresses in the response. The special value 255 means that there
39 are no addresses, and the RCODE is NXDOMAIN. */
41 response (const struct resolv_response_context
*ctx
,
42 struct resolv_response_builder
*b
,
43 const char *qname
, uint16_t qclass
, uint16_t qtype
)
45 TEST_COMPARE (qclass
, C_IN
);
47 TEST_COMPARE (qtype
, T_AAAA
);
49 unsigned int addresses
, cnames
;
51 if (sscanf (qname
, "a%u-c%u%ms", &addresses
, &cnames
, &tail
) == 3)
53 if (strcmp (tail
, ".example.com") == 0
54 || strcmp (tail
, ".example.net.example.net") == 0
55 || strcmp (tail
, ".example.net.example.com") == 0)
56 /* These only happen after NXDOMAIN. */
57 TEST_VERIFY (addresses
== 255);
58 else if (strcmp (tail
, ".example.net") != 0)
59 FAIL_EXIT1 ("invalid QNAME: %s", qname
);
66 /* Special case: Use no addresses with NXDOMAIN response. */
67 rcode
= ns_r_nxdomain
;
73 struct resolv_response_flags flags
= { .rcode
= rcode
};
74 resolv_response_init (b
, flags
);
75 resolv_response_add_question (b
, qname
, qclass
, qtype
);
76 resolv_response_section (b
, ns_s_an
);
77 maybe_insert_sig (b
, qname
);
79 /* Provide the requested number of CNAME records. */
80 char *previous_name
= (char *) qname
;
81 for (int unique
= 0; unique
< cnames
; ++unique
)
83 resolv_response_open_record (b
, previous_name
, qclass
, T_CNAME
, 60);
84 char *new_name
= xasprintf ("%d.alias.example", unique
);
85 resolv_response_add_name (b
, new_name
);
86 resolv_response_close_record (b
);
88 maybe_insert_sig (b
, qname
);
90 if (previous_name
!= qname
)
92 previous_name
= new_name
;
95 for (int unique
= 0; unique
< addresses
; ++unique
)
97 resolv_response_open_record (b
, previous_name
, qclass
, qtype
, 60);
101 char ipv4
[4] = {192, 0, 2, 1 + unique
};
102 resolv_response_add_data (b
, &ipv4
, sizeof (ipv4
));
104 else if (qtype
== T_AAAA
)
108 0x20, 0x01, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
111 resolv_response_add_data (b
, &ipv6
, sizeof (ipv6
));
113 resolv_response_close_record (b
);
116 if (previous_name
!= qname
)
117 free (previous_name
);
121 make_qname (bool do_search
, int cnames
, int addresses
)
123 return xasprintf ("a%d-c%d%s",
124 addresses
, cnames
, do_search
? "" : ".example.net");
128 check_cnames_failure (int af
, bool do_search
, int cnames
, int addresses
)
130 char *qname
= make_qname (do_search
, cnames
, addresses
);
134 e
= gethostbyname (qname
);
136 e
= gethostbyname2 (qname
, af
);
139 check_hostent (qname
, e
, "error: NO_RECOVERY\n");
141 check_hostent (qname
, e
, "error: HOST_NOT_FOUND\n");
147 check (int af
, bool do_search
, int cnames
, int addresses
)
149 char *qname
= make_qname (do_search
, cnames
, addresses
);
150 char *fqdn
= make_qname (false, cnames
, addresses
);
154 e
= gethostbyname (qname
);
156 e
= gethostbyname2 (qname
, af
);
158 FAIL_EXIT1 ("unexpected failure for %d, %d, %d", af
, cnames
, addresses
);
160 if (af
== AF_UNSPEC
|| af
== AF_INET
)
162 TEST_COMPARE (e
->h_addrtype
, AF_INET
);
163 TEST_COMPARE (e
->h_length
, 4);
167 TEST_COMPARE (e
->h_addrtype
, AF_INET6
);
168 TEST_COMPARE (e
->h_length
, 16);
171 for (int i
= 0; i
< addresses
; ++i
)
173 char ipv4
[4] = {192, 0, 2, 1 + i
};
175 { 0x20, 0x01, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 + i
};
176 char *expected
= e
->h_addrtype
== AF_INET
? ipv4
: ipv6
;
177 TEST_COMPARE_BLOB (e
->h_addr_list
[i
], e
->h_length
,
178 expected
, e
->h_length
);
180 TEST_VERIFY (e
->h_addr_list
[addresses
] == NULL
);
185 /* QNAME is fully qualified. */
186 TEST_COMPARE_STRING (e
->h_name
, fqdn
);
187 TEST_VERIFY (e
->h_aliases
[0] == NULL
);
191 /* Fully-qualified QNAME is demoted to an aliases. */
192 TEST_COMPARE_STRING (e
->h_aliases
[0], fqdn
);
194 for (int i
= 1; i
<= cnames
; ++i
)
196 char *expected
= xasprintf ("%d.alias.example", i
- 1);
198 TEST_COMPARE_STRING (e
->h_name
, expected
);
200 TEST_COMPARE_STRING (e
->h_aliases
[i
], expected
);
203 TEST_VERIFY (e
->h_aliases
[cnames
] == NULL
);
213 struct resolv_test
*obj
= resolv_test_start
214 ((struct resolv_redirect_config
)
216 .response_callback
= response
,
217 .search
= { "example.net", "example.com" },
220 static const int families
[] = { AF_UNSPEC
, AF_INET
, AF_INET6
};
222 for (int do_insert_sig
= 0; do_insert_sig
< 2; ++do_insert_sig
)
224 insert_sig
= do_insert_sig
;
226 /* If do_search is true, a bare host name (for example, a1-c1)
227 is used. This exercises search path processing and FQDN
229 for (int do_search
= 0; do_search
< 2; ++do_search
)
230 for (const int *paf
= families
; paf
!= array_end (families
); ++paf
)
232 for (int cnames
= 0; cnames
<= 100; ++cnames
)
234 check_cnames_failure (*paf
, do_search
, cnames
, 0);
235 /* Now with NXDOMAIN responses. */
236 check_cnames_failure (*paf
, do_search
, cnames
, 255);
239 for (int cnames
= 0; cnames
<= 10; ++cnames
)
240 for (int addresses
= 1; addresses
<= 10; ++addresses
)
241 check (*paf
, do_search
, cnames
, addresses
);
243 /* The current implementation is limited to 47 aliases.
244 Addresses do not have such a limit. */
245 check (*paf
, do_search
, 47, 60);
249 resolv_test_end (obj
);
254 #include <support/test-driver.c>