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"
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
30 #include <sys/socket.h>
31 #include <netinet/tcp.h>
35 namespace parse_connection_spec_tests
{
37 /* Auxiliary struct that holds info about a specific test for a
40 struct parse_conn_test
42 /* The connection spec. */
45 /* Expected result from 'parse_connection_spec'. */
46 parsed_connection_spec expected_result
;
48 /* True if this test should fail, false otherwise. If true, only
49 the CONNSPEC field should be considered as valid. */
52 /* The expected AI_FAMILY to be found on the 'struct addrinfo'
56 /* The expected AI_SOCKTYPE to be found on the 'struct addrinfo'
60 /* The expected AI_PROTOCOL to be found on the 'struct addrinfo'
65 /* Some defines to help us fill a 'struct parse_conn_test'. */
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 }
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)
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)
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)
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, \
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, \
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, \
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, \
109 /* Initialize a bogus entry, i.e., a connection spec that should
111 #define INIT_BOGUS_ENTRY(ADDR) \
112 INIT_ENTRY (ADDR, "", "", true, 0, 0, 0)
114 /* The variable which holds all of our tests. */
116 static const parse_conn_test conn_test
[] =
118 /* Unprefixed addresses. */
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", ""),
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", ""),
138 /* Unspecified, only port. */
139 INIT_UNPREFIXED_ENTRY (":1234", "localhost", "1234"),
141 /* Prefixed addresses. */
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", ""),
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", ""),
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", ""),
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", ""),
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", ""),
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", ""),
195 /* Bogus addresses. */
196 INIT_BOGUS_ENTRY ("tcp6:[::1]123:44"),
197 INIT_BOGUS_ENTRY ("[::1"),
198 INIT_BOGUS_ENTRY ("tcp6:::1]:"),
201 /* Test a connection spec C. */
204 test_conn (const parse_conn_test
&c
)
206 struct addrinfo hint
;
207 parsed_connection_spec ret
;
209 memset (&hint
, 0, sizeof (hint
));
213 ret
= parse_connection_spec (c
.connspec
, &hint
);
215 catch (const gdb_exception_error
&ex
)
217 /* If we caught an error, we should check if this connection
218 spec was supposed to fail. */
219 SELF_CHECK (c
.should_fail
);
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
);
231 /* Run the tests associated with parsing connection specs. */
236 for (const parse_conn_test
&c
: conn_test
)
239 } /* namespace parse_connection_spec_tests */
240 } /* namespace selftests */
243 _initialize_parse_connection_spec_selftests ()
245 selftests::register_test ("parse_connection_spec",
246 selftests::parse_connection_spec_tests::run_tests
);