1 /* Self tests for parsing connection specs for GDB, the GNU debugger.
3 Copyright (C) 2018-2019 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "common/selftest.h"
22 #include "common/netstuff.h"
23 #include "diagnostics.h"
25 #if _WIN32_WINNT < 0x0501
27 # define _WIN32_WINNT 0x0501
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
34 #include <sys/socket.h>
35 #include <netinet/tcp.h>
39 namespace parse_connection_spec_tests
{
41 /* Auxiliary struct that holds info about a specific test for a
44 struct parse_conn_test
46 /* The connection spec. */
49 /* Expected result from 'parse_connection_spec'. */
50 parsed_connection_spec expected_result
;
52 /* True if this test should fail, false otherwise. If true, only
53 the CONNSPEC field should be considered as valid. */
56 /* The expected AI_FAMILY to be found on the 'struct addrinfo'
60 /* The expected AI_SOCKTYPE to be found on the 'struct addrinfo'
64 /* The expected AI_PROTOCOL to be found on the 'struct addrinfo'
69 /* Some defines to help us fill a 'struct parse_conn_test'. */
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 }
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)
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)
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)
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, \
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, \
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, \
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, \
113 /* Initialize a bogus entry, i.e., a connection spec that should
115 #define INIT_BOGUS_ENTRY(ADDR) \
116 INIT_ENTRY (ADDR, "", "", true, 0, 0, 0)
118 /* The variable which holds all of our tests. */
120 static const parse_conn_test conn_test
[] =
122 /* Unprefixed addresses. */
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", ""),
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", ""),
142 /* Unspecified, only port. */
143 INIT_UNPREFIXED_ENTRY (":1234", "localhost", "1234"),
145 /* Prefixed addresses. */
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", ""),
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", ""),
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", ""),
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", ""),
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", ""),
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", ""),
199 /* Bogus addresses. */
200 INIT_BOGUS_ENTRY ("tcp6:[::1]123:44"),
201 INIT_BOGUS_ENTRY ("[::1"),
202 INIT_BOGUS_ENTRY ("tcp6:::1]:"),
205 /* Test a connection spec C. */
208 test_conn (const parse_conn_test
&c
)
210 struct addrinfo hint
;
211 parsed_connection_spec ret
;
213 memset (&hint
, 0, sizeof (hint
));
217 ret
= parse_connection_spec (c
.connspec
, &hint
);
219 CATCH (ex
, RETURN_MASK_ERROR
)
221 /* If we caught an error, we should check if this connection
222 spec was supposed to fail. */
223 SELF_CHECK (c
.should_fail
);
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
);
236 /* Run the tests associated with parsing connection specs. */
241 for (const parse_conn_test
&c
: conn_test
)
244 } /* namespace parse_connection_spec_tests */
245 } /* namespace selftests */
248 _initialize_parse_connection_spec_selftests ()
250 selftests::register_test ("parse_connection_spec",
251 selftests::parse_connection_spec_tests::run_tests
);