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