1 /* Self tests for parsing connection specs for GDB, the GNU debugger.
3 Copyright (C) 2018 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/>. */
22 #include "common/netstuff.h"
23 #include "diagnostics.h"
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
31 #include <sys/socket.h>
32 #include <netinet/tcp.h>
36 namespace parse_connection_spec_tests {
38 /* Auxiliary struct that holds info about a specific test for a
41 struct parse_conn_test
43 /* The connection spec. */
46 /* Expected result from 'parse_connection_spec'. */
47 parsed_connection_spec expected_result;
49 /* True if this test should fail, false otherwise. If true, only
50 the CONNSPEC field should be considered as valid. */
53 /* The expected AI_FAMILY to be found on the 'struct addrinfo'
57 /* The expected AI_SOCKTYPE to be found on the 'struct addrinfo'
61 /* The expected AI_PROTOCOL to be found on the 'struct addrinfo'
66 /* Some defines to help us fill a 'struct parse_conn_test'. */
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 }
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)
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)
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)
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, \
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, \
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, \
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, \
110 /* Initialize a bogus entry, i.e., a connection spec that should
112 #define INIT_BOGUS_ENTRY(ADDR) \
113 INIT_ENTRY (ADDR, "", "", true, 0, 0, 0)
115 /* The variable which holds all of our tests. */
117 static const parse_conn_test conn_test[] =
119 /* Unprefixed addresses. */
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", ""),
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", ""),
139 /* Unspecified, only port. */
140 INIT_UNPREFIXED_ENTRY (":1234", "localhost", "1234"),
142 /* Prefixed addresses. */
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", ""),
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", ""),
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", ""),
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", ""),
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", ""),
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", ""),
196 /* Bogus addresses. */
197 INIT_BOGUS_ENTRY ("tcp6:[::1]123:44"),
198 INIT_BOGUS_ENTRY ("[::1"),
199 INIT_BOGUS_ENTRY ("tcp6:::1]:"),
202 /* Test a connection spec C. */
205 test_conn (const parse_conn_test &c)
207 struct addrinfo hint;
208 parsed_connection_spec ret;
210 memset (&hint, 0, sizeof (hint));
214 ret = parse_connection_spec (c.connspec, &hint);
216 CATCH (ex, RETURN_MASK_ERROR)
218 /* If we caught an error, we should check if this connection
219 spec was supposed to fail. */
220 SELF_CHECK (c.should_fail);
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);
233 /* Run the tests associated with parsing connection specs. */
238 for (const parse_conn_test &c : conn_test)
241 } /* namespace parse_connection_spec_tests */
242 } /* namespace selftests */
245 _initialize_parse_connection_spec_selftests ()
247 selftests::register_test ("parse_connection_spec",
248 selftests::parse_connection_spec_tests::run_tests);