]>
Commit | Line | Data |
---|---|---|
1 | /* Self tests for parsing connection specs for GDB, the GNU debugger. | |
2 | ||
3 | Copyright (C) 2018-2025 Free Software Foundation, Inc. | |
4 | ||
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #include "gdbsupport/selftest.h" | |
21 | #include "gdbsupport/netstuff.h" | |
22 | #include "diagnostics.h" | |
23 | #ifdef USE_WIN32API | |
24 | #include <ws2tcpip.h> | |
25 | #else | |
26 | #include <netinet/in.h> | |
27 | #include <arpa/inet.h> | |
28 | #include <netdb.h> | |
29 | #include <sys/socket.h> | |
30 | #include <netinet/tcp.h> | |
31 | #endif | |
32 | ||
33 | namespace selftests { | |
34 | namespace parse_connection_spec_tests { | |
35 | ||
36 | /* Auxiliary struct that holds info about a specific test for a | |
37 | connection spec. */ | |
38 | ||
39 | struct parse_conn_test | |
40 | { | |
41 | /* The connection spec. */ | |
42 | const char *connspec; | |
43 | ||
44 | /* Expected result from 'parse_connection_spec'. */ | |
45 | parsed_connection_spec expected_result; | |
46 | ||
47 | /* True if this test should fail, false otherwise. If true, only | |
48 | the CONNSPEC field should be considered as valid. */ | |
49 | bool should_fail; | |
50 | ||
51 | /* The expected AI_FAMILY to be found on the 'struct addrinfo' | |
52 | HINT. */ | |
53 | int exp_ai_family; | |
54 | ||
55 | /* The expected AI_SOCKTYPE to be found on the 'struct addrinfo' | |
56 | HINT. */ | |
57 | int exp_ai_socktype; | |
58 | ||
59 | /* The expected AI_PROTOCOL to be found on the 'struct addrinfo' | |
60 | HINT. */ | |
61 | int exp_ai_protocol; | |
62 | }; | |
63 | ||
64 | /* Some defines to help us fill a 'struct parse_conn_test'. */ | |
65 | ||
66 | /* Initialize a full entry. */ | |
67 | #define INIT_ENTRY(ADDR, EXP_HOST, EXP_PORT, SHOULD_FAIL, EXP_AI_FAMILY, \ | |
68 | EXP_AI_SOCKTYPE, EXP_AI_PROTOCOL) \ | |
69 | { ADDR, { EXP_HOST, EXP_PORT }, SHOULD_FAIL, EXP_AI_FAMILY, \ | |
70 | EXP_AI_SOCKTYPE, EXP_AI_PROTOCOL } | |
71 | ||
72 | /* Initialize an unprefixed entry. In this case, we don't expect | |
73 | anything on the 'struct addrinfo' HINT. */ | |
74 | #define INIT_UNPREFIXED_ENTRY(ADDR, EXP_HOST, EXP_PORT) \ | |
75 | INIT_ENTRY (ADDR, EXP_HOST, EXP_PORT, false, 0, 0, 0) | |
76 | ||
77 | /* Initialized an unprefixed IPv6 entry. In this case, we don't | |
78 | expect anything on the 'struct addrinfo' HINT. */ | |
79 | #define INIT_UNPREFIXED_IPV6_ENTRY(ADDR, EXP_HOST, EXP_PORT) \ | |
80 | INIT_ENTRY (ADDR, EXP_HOST, EXP_PORT, false, AF_INET6, 0, 0) | |
81 | ||
82 | /* Initialize a prefixed entry. */ | |
83 | #define INIT_PREFIXED_ENTRY(ADDR, EXP_HOST, EXP_PORT, EXP_AI_FAMILY, \ | |
84 | EXP_AI_SOCKTYPE, EXP_AI_PROTOCOL) \ | |
85 | INIT_ENTRY (ADDR, EXP_HOST, EXP_PORT, false, EXP_AI_FAMILY, \ | |
86 | EXP_AI_SOCKTYPE, EXP_AI_PROTOCOL) | |
87 | ||
88 | /* Initialize an entry prefixed with "tcp4:". */ | |
89 | #define INIT_PREFIXED_IPV4_TCP(ADDR, EXP_HOST, EXP_PORT) \ | |
90 | INIT_PREFIXED_ENTRY (ADDR, EXP_HOST, EXP_PORT, AF_INET, SOCK_STREAM, \ | |
91 | IPPROTO_TCP) | |
92 | ||
93 | /* Initialize an entry prefixed with "tcp6:". */ | |
94 | #define INIT_PREFIXED_IPV6_TCP(ADDR, EXP_HOST, EXP_PORT) \ | |
95 | INIT_PREFIXED_ENTRY (ADDR, EXP_HOST, EXP_PORT, AF_INET6, SOCK_STREAM, \ | |
96 | IPPROTO_TCP) | |
97 | ||
98 | /* Initialize an entry prefixed with "udp4:". */ | |
99 | #define INIT_PREFIXED_IPV4_UDP(ADDR, EXP_HOST, EXP_PORT) \ | |
100 | INIT_PREFIXED_ENTRY (ADDR, EXP_HOST, EXP_PORT, AF_INET, SOCK_DGRAM, \ | |
101 | IPPROTO_UDP) | |
102 | ||
103 | /* Initialize an entry prefixed with "udp6:". */ | |
104 | #define INIT_PREFIXED_IPV6_UDP(ADDR, EXP_HOST, EXP_PORT) \ | |
105 | INIT_PREFIXED_ENTRY (ADDR, EXP_HOST, EXP_PORT, AF_INET6, SOCK_DGRAM, \ | |
106 | IPPROTO_UDP) | |
107 | ||
108 | /* Initialize a bogus entry, i.e., a connection spec that should | |
109 | fail. */ | |
110 | #define INIT_BOGUS_ENTRY(ADDR) \ | |
111 | INIT_ENTRY (ADDR, "", "", true, 0, 0, 0) | |
112 | ||
113 | /* The variable which holds all of our tests. */ | |
114 | ||
115 | static const parse_conn_test conn_test[] = | |
116 | { | |
117 | /* Unprefixed addresses. */ | |
118 | ||
119 | /* IPv4, host and port present. */ | |
120 | INIT_UNPREFIXED_ENTRY ("127.0.0.1:1234", "127.0.0.1", "1234"), | |
121 | /* IPv4, only host. */ | |
122 | INIT_UNPREFIXED_ENTRY ("127.0.0.1", "127.0.0.1", ""), | |
123 | /* IPv4, missing port. */ | |
124 | INIT_UNPREFIXED_ENTRY ("127.0.0.1:", "127.0.0.1", ""), | |
125 | ||
126 | /* IPv6, host and port present, no brackets. */ | |
127 | INIT_UNPREFIXED_ENTRY ("::1:1234", "::1", "1234"), | |
128 | /* IPv6, missing port, no brackets. */ | |
129 | INIT_UNPREFIXED_ENTRY ("::1:", "::1", ""), | |
130 | /* IPv6, host and port present, with brackets. */ | |
131 | INIT_UNPREFIXED_IPV6_ENTRY ("[::1]:1234", "::1", "1234"), | |
132 | /* IPv6, only host, with brackets. */ | |
133 | INIT_UNPREFIXED_IPV6_ENTRY ("[::1]", "::1", ""), | |
134 | /* IPv6, missing port, with brackets. */ | |
135 | INIT_UNPREFIXED_IPV6_ENTRY ("[::1]:", "::1", ""), | |
136 | ||
137 | /* Unspecified, only port. */ | |
138 | INIT_UNPREFIXED_ENTRY (":1234", "localhost", "1234"), | |
139 | ||
140 | /* Prefixed addresses. */ | |
141 | ||
142 | /* Prefixed "tcp4:" IPv4, host and port presents. */ | |
143 | INIT_PREFIXED_IPV4_TCP ("tcp4:127.0.0.1:1234", "127.0.0.1", "1234"), | |
144 | /* Prefixed "tcp4:" IPv4, only port. */ | |
145 | INIT_PREFIXED_IPV4_TCP ("tcp4::1234", "localhost", "1234"), | |
146 | /* Prefixed "tcp4:" IPv4, only host. */ | |
147 | INIT_PREFIXED_IPV4_TCP ("tcp4:127.0.0.1", "127.0.0.1", ""), | |
148 | /* Prefixed "tcp4:" IPv4, missing port. */ | |
149 | INIT_PREFIXED_IPV4_TCP ("tcp4:127.0.0.1:", "127.0.0.1", ""), | |
150 | ||
151 | /* Prefixed "udp4:" IPv4, host and port present. */ | |
152 | INIT_PREFIXED_IPV4_UDP ("udp4:127.0.0.1:1234", "127.0.0.1", "1234"), | |
153 | /* Prefixed "udp4:" IPv4, only port. */ | |
154 | INIT_PREFIXED_IPV4_UDP ("udp4::1234", "localhost", "1234"), | |
155 | /* Prefixed "udp4:" IPv4, only host. */ | |
156 | INIT_PREFIXED_IPV4_UDP ("udp4:127.0.0.1", "127.0.0.1", ""), | |
157 | /* Prefixed "udp4:" IPv4, missing port. */ | |
158 | INIT_PREFIXED_IPV4_UDP ("udp4:127.0.0.1:", "127.0.0.1", ""), | |
159 | ||
160 | ||
161 | /* Prefixed "tcp6:" IPv6, host and port present. */ | |
162 | INIT_PREFIXED_IPV6_TCP ("tcp6:::1:1234", "::1", "1234"), | |
163 | /* Prefixed "tcp6:" IPv6, only port. */ | |
164 | INIT_PREFIXED_IPV6_TCP ("tcp6::1234", "localhost", "1234"), | |
165 | /* Prefixed "tcp6:" IPv6, only host. */ | |
166 | //INIT_PREFIXED_IPV6_TCP ("tcp6:::1", "::1", ""), | |
167 | /* Prefixed "tcp6:" IPv6, missing port. */ | |
168 | INIT_PREFIXED_IPV6_TCP ("tcp6:::1:", "::1", ""), | |
169 | ||
170 | /* Prefixed "udp6:" IPv6, host and port present. */ | |
171 | INIT_PREFIXED_IPV6_UDP ("udp6:::1:1234", "::1", "1234"), | |
172 | /* Prefixed "udp6:" IPv6, only port. */ | |
173 | INIT_PREFIXED_IPV6_UDP ("udp6::1234", "localhost", "1234"), | |
174 | /* Prefixed "udp6:" IPv6, only host. */ | |
175 | //INIT_PREFIXED_IPV6_UDP ("udp6:::1", "::1", ""), | |
176 | /* Prefixed "udp6:" IPv6, missing port. */ | |
177 | INIT_PREFIXED_IPV6_UDP ("udp6:::1:", "::1", ""), | |
178 | ||
179 | /* Prefixed "tcp6:" IPv6 with brackets, host and port present. */ | |
180 | INIT_PREFIXED_IPV6_TCP ("tcp6:[::1]:1234", "::1", "1234"), | |
181 | /* Prefixed "tcp6:" IPv6 with brackets, only host. */ | |
182 | INIT_PREFIXED_IPV6_TCP ("tcp6:[::1]", "::1", ""), | |
183 | /* Prefixed "tcp6:" IPv6 with brackets, missing port. */ | |
184 | INIT_PREFIXED_IPV6_TCP ("tcp6:[::1]:", "::1", ""), | |
185 | ||
186 | /* Prefixed "udp6:" IPv6 with brackets, host and port present. */ | |
187 | INIT_PREFIXED_IPV6_UDP ("udp6:[::1]:1234", "::1", "1234"), | |
188 | /* Prefixed "udp6:" IPv6 with brackets, only host. */ | |
189 | INIT_PREFIXED_IPV6_UDP ("udp6:[::1]", "::1", ""), | |
190 | /* Prefixed "udp6:" IPv6 with brackets, missing port. */ | |
191 | INIT_PREFIXED_IPV6_UDP ("udp6:[::1]:", "::1", ""), | |
192 | ||
193 | ||
194 | /* Bogus addresses. */ | |
195 | INIT_BOGUS_ENTRY ("tcp6:[::1]123:44"), | |
196 | INIT_BOGUS_ENTRY ("[::1"), | |
197 | INIT_BOGUS_ENTRY ("tcp6:::1]:"), | |
198 | }; | |
199 | ||
200 | /* Test a connection spec C. */ | |
201 | ||
202 | static void | |
203 | test_conn (const parse_conn_test &c) | |
204 | { | |
205 | struct addrinfo hint; | |
206 | parsed_connection_spec ret; | |
207 | ||
208 | memset (&hint, 0, sizeof (hint)); | |
209 | ||
210 | try | |
211 | { | |
212 | ret = parse_connection_spec (c.connspec, &hint); | |
213 | } | |
214 | catch (const gdb_exception_error &ex) | |
215 | { | |
216 | /* If we caught an error, we should check if this connection | |
217 | spec was supposed to fail. */ | |
218 | SELF_CHECK (c.should_fail); | |
219 | return; | |
220 | } | |
221 | ||
222 | SELF_CHECK (!c.should_fail); | |
223 | SELF_CHECK (ret.host_str == c.expected_result.host_str); | |
224 | SELF_CHECK (ret.port_str == c.expected_result.port_str); | |
225 | SELF_CHECK (hint.ai_family == c.exp_ai_family); | |
226 | SELF_CHECK (hint.ai_socktype == c.exp_ai_socktype); | |
227 | SELF_CHECK (hint.ai_protocol == c.exp_ai_protocol); | |
228 | } | |
229 | ||
230 | /* Run the tests associated with parsing connection specs. */ | |
231 | ||
232 | static void | |
233 | run_tests () | |
234 | { | |
235 | for (const parse_conn_test &c : conn_test) | |
236 | test_conn (c); | |
237 | } | |
238 | } /* namespace parse_connection_spec_tests */ | |
239 | } /* namespace selftests */ | |
240 | ||
241 | INIT_GDB_FILE (parse_connection_spec_selftests) | |
242 | { | |
243 | selftests::register_test ("parse_connection_spec", | |
244 | selftests::parse_connection_spec_tests::run_tests); | |
245 | } |