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