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