1 | /* Test the RES_NOAAAA resolver option with a large response. |
2 | Copyright (C) 2022-2024 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 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <errno.h> |
20 | #include <netdb.h> |
21 | #include <resolv.h> |
22 | #include <stdbool.h> |
23 | #include <stdlib.h> |
24 | #include <support/check.h> |
25 | #include <support/check_nss.h> |
26 | #include <support/resolv_test.h> |
27 | #include <support/support.h> |
28 | #include <support/xmemstream.h> |
29 | |
30 | /* Used to keep track of the number of queries. */ |
31 | static volatile unsigned int queries; |
32 | |
33 | /* If true, add a large TXT record at the start of the answer section. */ |
34 | static volatile bool stuff_txt; |
35 | |
36 | static void |
37 | response (const struct resolv_response_context *ctx, |
38 | struct resolv_response_builder *b, |
39 | const char *qname, uint16_t qclass, uint16_t qtype) |
40 | { |
41 | /* If not using TCP, just force its use. */ |
42 | if (!ctx->tcp) |
43 | { |
44 | struct resolv_response_flags flags = {.tc = true}; |
45 | resolv_response_init (b, flags); |
46 | resolv_response_add_question (b, name: qname, class: qclass, type: qtype); |
47 | return; |
48 | } |
49 | |
50 | /* The test needs to send four queries, the first three are used to |
51 | grow the NSS buffer via the ERANGE handshake. */ |
52 | ++queries; |
53 | TEST_VERIFY (queries <= 4); |
54 | |
55 | /* AAAA queries are supposed to be disabled. */ |
56 | TEST_COMPARE (qtype, T_A); |
57 | TEST_COMPARE (qclass, C_IN); |
58 | TEST_COMPARE_STRING (qname, "example.com" ); |
59 | |
60 | struct resolv_response_flags flags = {}; |
61 | resolv_response_init (b, flags); |
62 | resolv_response_add_question (b, name: qname, class: qclass, type: qtype); |
63 | |
64 | resolv_response_section (b, ns_s_an); |
65 | |
66 | if (stuff_txt) |
67 | { |
68 | resolv_response_open_record (b, name: qname, class: qclass, T_TXT, ttl: 60); |
69 | int zero = 0; |
70 | for (int i = 0; i <= 15000; ++i) |
71 | resolv_response_add_data (b, &zero, sizeof (zero)); |
72 | resolv_response_close_record (b); |
73 | } |
74 | |
75 | for (int i = 0; i < 200; ++i) |
76 | { |
77 | resolv_response_open_record (b, name: qname, class: qclass, type: qtype, ttl: 60); |
78 | char ipv4[4] = {192, 0, 2, i + 1}; |
79 | resolv_response_add_data (b, &ipv4, sizeof (ipv4)); |
80 | resolv_response_close_record (b); |
81 | } |
82 | } |
83 | |
84 | static int |
85 | do_test (void) |
86 | { |
87 | struct resolv_test *obj = resolv_test_start |
88 | ((struct resolv_redirect_config) |
89 | { |
90 | .response_callback = response |
91 | }); |
92 | |
93 | _res.options |= RES_NOAAAA; |
94 | |
95 | for (int do_stuff_txt = 0; do_stuff_txt < 2; ++do_stuff_txt) |
96 | { |
97 | queries = 0; |
98 | stuff_txt = do_stuff_txt; |
99 | |
100 | struct addrinfo *ai = NULL; |
101 | int ret; |
102 | ret = getaddrinfo (name: "example.com" , service: "80" , |
103 | req: &(struct addrinfo) |
104 | { |
105 | .ai_family = AF_UNSPEC, |
106 | .ai_socktype = SOCK_STREAM, |
107 | }, pai: &ai); |
108 | |
109 | char *expected_result; |
110 | { |
111 | struct xmemstream mem; |
112 | xopen_memstream (stream: &mem); |
113 | for (int i = 0; i < 200; ++i) |
114 | fprintf (stream: mem.out, format: "address: STREAM/TCP 192.0.2.%d 80\n" , i + 1); |
115 | xfclose_memstream (stream: &mem); |
116 | expected_result = mem.buffer; |
117 | } |
118 | |
119 | check_addrinfo (query_description: "example.com" , ai, ret, expected: expected_result); |
120 | |
121 | free (ptr: expected_result); |
122 | freeaddrinfo (ai: ai); |
123 | } |
124 | |
125 | resolv_test_end (obj); |
126 | return 0; |
127 | } |
128 | |
129 | #include <support/test-driver.c> |
130 | |