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