1 | /* $Id: tstRTNetIPv6.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - IPv6.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <iprt/net.h>
|
---|
42 |
|
---|
43 | #include <iprt/errcore.h>
|
---|
44 | #include <iprt/initterm.h>
|
---|
45 | #include <iprt/test.h>
|
---|
46 |
|
---|
47 | #include <iprt/string.h>
|
---|
48 |
|
---|
49 |
|
---|
50 | /*********************************************************************************************************************************
|
---|
51 | * Defined Constants And Macros *
|
---|
52 | *********************************************************************************************************************************/
|
---|
53 | #define CHECKADDR(String, rcExpected, u32_0, u32_1, u32_2, u32_3) \
|
---|
54 | do { \
|
---|
55 | RTNETADDRIPV6 Addr; \
|
---|
56 | char *pszZone; \
|
---|
57 | uint32_t ExpectedAddr[4] = { \
|
---|
58 | RT_H2N_U32_C(u32_0), RT_H2N_U32_C(u32_1), \
|
---|
59 | RT_H2N_U32_C(u32_2), RT_H2N_U32_C(u32_3) \
|
---|
60 | }; \
|
---|
61 | int rc2 = RTNetStrToIPv6Addr(String, &Addr, &pszZone); \
|
---|
62 | if ((rcExpected) && !rc2) \
|
---|
63 | { \
|
---|
64 | RTTestIFailed("at line %d: '%s': expected %Rrc got %Rrc\n", \
|
---|
65 | __LINE__, String, (rcExpected), rc2); \
|
---|
66 | } \
|
---|
67 | else if ( (rcExpected) != rc2 \
|
---|
68 | || ( rc2 == VINF_SUCCESS \
|
---|
69 | && memcmp(ExpectedAddr, &Addr, sizeof(Addr)) != 0)) \
|
---|
70 | { \
|
---|
71 | RTTestIFailed("at line %d: '%s': expected %Rrc got %Rrc," \
|
---|
72 | " expected address %RTnaipv6 got %RTnaipv6\n", \
|
---|
73 | __LINE__, String, rcExpected, rc2, \
|
---|
74 | ExpectedAddr, &Addr); \
|
---|
75 | } \
|
---|
76 | } while (0)
|
---|
77 |
|
---|
78 | #define GOODADDR(String, u32_0, u32_1, u32_2, u32_3) \
|
---|
79 | CHECKADDR(String, VINF_SUCCESS, u32_0, u32_1, u32_2, u32_3)
|
---|
80 |
|
---|
81 | #define BADADDR(String) \
|
---|
82 | CHECKADDR(String, VERR_INVALID_PARAMETER, 0, 0, 0, 0)
|
---|
83 |
|
---|
84 |
|
---|
85 | #define CHECKCIDR(String, rcExpected, u32_0, u32_1, u32_2, u32_3, iExpectedPrefix) \
|
---|
86 | do { \
|
---|
87 | RTNETADDRIPV6 Addr; \
|
---|
88 | uint32_t ExpectedAddr[4] = { \
|
---|
89 | RT_H2N_U32_C(u32_0), RT_H2N_U32_C(u32_1), \
|
---|
90 | RT_H2N_U32_C(u32_2), RT_H2N_U32_C(u32_3) \
|
---|
91 | }; \
|
---|
92 | int iPrefix; \
|
---|
93 | \
|
---|
94 | int rc2 = RTNetStrToIPv6Cidr(String, &Addr, &iPrefix); \
|
---|
95 | if ((rcExpected) && !rc2) \
|
---|
96 | { \
|
---|
97 | RTTestIFailed("at line %d: '%s': expected %Rrc got %Rrc\n", \
|
---|
98 | __LINE__, String, (rcExpected), rc2); \
|
---|
99 | } \
|
---|
100 | else if ( (rcExpected) != rc2 \
|
---|
101 | || ( rc2 == VINF_SUCCESS \
|
---|
102 | && ( memcmp(ExpectedAddr, &Addr, sizeof(Addr)) != 0 \
|
---|
103 | || iExpectedPrefix != iPrefix))) \
|
---|
104 | { \
|
---|
105 | RTTestIFailed("at line %d: '%s': expected %Rrc got %Rrc," \
|
---|
106 | " expected address %RTnaipv6/%d got %RTnaipv6/%d\n",\
|
---|
107 | __LINE__, String, rcExpected, rc2, \
|
---|
108 | ExpectedAddr, iExpectedPrefix, \
|
---|
109 | &Addr, iPrefix); \
|
---|
110 | } \
|
---|
111 | } while (0)
|
---|
112 |
|
---|
113 | #define GOODCIDR(String, u32_0, u32_1, u32_2, u32_3, iExpectedPrefix) \
|
---|
114 | CHECKCIDR(String, VINF_SUCCESS, u32_0, u32_1, u32_2, u32_3, iExpectedPrefix)
|
---|
115 |
|
---|
116 | #define BADCIDR(String) \
|
---|
117 | CHECKCIDR(String, VERR_INVALID_PARAMETER, 0, 0, 0, 0, 0)
|
---|
118 |
|
---|
119 |
|
---|
120 | #define CHECKANY(String, fExpected) \
|
---|
121 | do { \
|
---|
122 | bool fRc = RTNetStrIsIPv6AddrAny(String); \
|
---|
123 | if (fRc != fExpected) \
|
---|
124 | { \
|
---|
125 | RTTestIFailed("at line %d: '%s':" \
|
---|
126 | " expected %RTbool got %RTbool\n", \
|
---|
127 | __LINE__, (String), fExpected, fRc); \
|
---|
128 | } \
|
---|
129 | } while (0)
|
---|
130 |
|
---|
131 | #define IS_ANY(String) CHECKANY((String), true)
|
---|
132 | #define NOT_ANY(String) CHECKANY((String), false)
|
---|
133 |
|
---|
134 |
|
---|
135 | int main()
|
---|
136 | {
|
---|
137 | RTTEST hTest;
|
---|
138 | int rc = RTTestInitAndCreate("tstRTNetIPv6", &hTest);
|
---|
139 | if (rc)
|
---|
140 | return rc;
|
---|
141 | RTTestBanner(hTest);
|
---|
142 |
|
---|
143 |
|
---|
144 | /* base case: eight groups fully spelled */
|
---|
145 | GOODADDR("1:2:3:4:5:6:7:8", 0x00010002, 0x00030004, 0x00050006, 0x00070008);
|
---|
146 | GOODADDR("0001:0002:0003:0004:0005:0006:0007:0008", 0x00010002, 0x00030004, 0x00050006, 0x00070008);
|
---|
147 | GOODADDR("D:E:A:D:b:e:e:f", 0x000d000e, 0x000a000d, 0x000b000e, 0x000e000f);
|
---|
148 |
|
---|
149 | /* ... too short or too long */
|
---|
150 | BADADDR("1:2:3:4:5:6:7");
|
---|
151 | BADADDR("1:2:3:4:5:6:7:8:9");
|
---|
152 |
|
---|
153 | /* ... hex group constraints */
|
---|
154 | BADADDR("1:2:3:4:5:6:7:-8");
|
---|
155 | BADADDR("1:2:3:4:5:6:7:0x8");
|
---|
156 | BADADDR("1:2:3:4:5:6:7:88888");
|
---|
157 | BADADDR("1:2:3:4:5:6:7:00008");
|
---|
158 |
|
---|
159 |
|
---|
160 | /* embedded IPv4 at the end */
|
---|
161 | GOODADDR("0:0:0:0:0:0:1.2.3.4", 0, 0, 0, 0x01020304);
|
---|
162 |
|
---|
163 | /* ... not at the end */
|
---|
164 | BADADDR("0:0:0:0:0:1.2.3.4:0");
|
---|
165 |
|
---|
166 | /* ... too short or too long */
|
---|
167 | BADADDR("0:0:0:0:0:0:0:1.2.3.4");
|
---|
168 | BADADDR("0:0:0:0:0:1.2.3.4");
|
---|
169 |
|
---|
170 | /* ... invalid IPv4 address */
|
---|
171 | BADADDR("0:0:0:0:0:0:111.222.333.444");
|
---|
172 |
|
---|
173 |
|
---|
174 | /* "any" in compressed form */
|
---|
175 | GOODADDR("::", 0, 0, 0, 0);
|
---|
176 |
|
---|
177 | /* compressed run at the beginning */
|
---|
178 | GOODADDR("::8", 0, 0, 0, 0x00000008);
|
---|
179 | GOODADDR("::7:8", 0, 0, 0, 0x00070008);
|
---|
180 | GOODADDR("::6:7:8", 0, 0, 0x00000006, 0x00070008);
|
---|
181 | GOODADDR("::5:6:7:8", 0, 0, 0x00050006, 0x00070008);
|
---|
182 | GOODADDR("::4:5:6:7:8", 0, 0x00000004, 0x00050006, 0x00070008);
|
---|
183 | GOODADDR("::3:4:5:6:7:8", 0, 0x00030004, 0x00050006, 0x00070008);
|
---|
184 | GOODADDR("::2:3:4:5:6:7:8", 0x00000002, 0x00030004, 0x00050006, 0x00070008);
|
---|
185 |
|
---|
186 | /* ... too long */
|
---|
187 | BADADDR("::1:2:3:4:5:6:7:8");
|
---|
188 |
|
---|
189 | /* compressed run at the end */
|
---|
190 | GOODADDR("1::", 0x00010000, 0, 0, 0);
|
---|
191 | GOODADDR("1:2::", 0x00010002, 0, 0, 0);
|
---|
192 | GOODADDR("1:2:3::", 0x00010002, 0x00030000, 0, 0);
|
---|
193 | GOODADDR("1:2:3:4::", 0x00010002, 0x00030004, 0, 0);
|
---|
194 | GOODADDR("1:2:3:4:5::", 0x00010002, 0x00030004, 0x00050000, 0);
|
---|
195 | GOODADDR("1:2:3:4:5:6::", 0x00010002, 0x00030004, 0x00050006, 0);
|
---|
196 | GOODADDR("1:2:3:4:5:6:7::", 0x00010002, 0x00030004, 0x00050006, 0x00070000);
|
---|
197 |
|
---|
198 | /* ... too long */
|
---|
199 | BADADDR("1:2:3:4:5:6:7:8::");
|
---|
200 |
|
---|
201 | /* compressed run in the middle */
|
---|
202 | GOODADDR("1::8", 0x00010000, 0, 0, 0x00000008);
|
---|
203 | GOODADDR("1:2::8", 0x00010002, 0, 0, 0x00000008);
|
---|
204 | GOODADDR("1:2:3::8", 0x00010002, 0x00030000, 0, 0x00000008);
|
---|
205 | GOODADDR("1:2:3:4::8", 0x00010002, 0x00030004, 0, 0x00000008);
|
---|
206 | GOODADDR("1:2:3:4:5::8", 0x00010002, 0x00030004, 0x00050000, 0x00000008);
|
---|
207 | GOODADDR("1:2:3:4:5:6::8", 0x00010002, 0x00030004, 0x00050006, 0x00000008);
|
---|
208 |
|
---|
209 | GOODADDR("1::7:8", 0x00010000, 0, 0, 0x00070008);
|
---|
210 | GOODADDR("1::6:7:8", 0x00010000, 0, 0x00000006, 0x00070008);
|
---|
211 | GOODADDR("1::5:6:7:8", 0x00010000, 0, 0x00050006, 0x00070008);
|
---|
212 | GOODADDR("1::4:5:6:7:8", 0x00010000, 0x00000004, 0x00050006, 0x00070008);
|
---|
213 | GOODADDR("1::3:4:5:6:7:8", 0x00010000, 0x00030004, 0x00050006, 0x00070008);
|
---|
214 |
|
---|
215 | /* ... too long */
|
---|
216 | BADADDR("1::2:3:4:5:6:7:8");
|
---|
217 | BADADDR("1:2::3:4:5:6:7:8");
|
---|
218 | BADADDR("1:2:3::4:5:6:7:8");
|
---|
219 | BADADDR("1:2:3:4::5:6:7:8");
|
---|
220 | BADADDR("1:2:3:4:5::6:7:8");
|
---|
221 | BADADDR("1:2:3:4:5:6::7:8");
|
---|
222 | BADADDR("1:2:3:4:5:6:7::8");
|
---|
223 |
|
---|
224 | /* compressed with embedded IPv4 */
|
---|
225 | GOODADDR("::0.0.0.0", 0, 0, 0, 0);
|
---|
226 | GOODADDR("::1.2.3.4", 0, 0, 0, 0x01020304);
|
---|
227 | GOODADDR("::ffff:1.2.3.4", 0, 0, 0x0000ffff, 0x01020304);
|
---|
228 | GOODADDR("::ffff:0:1.2.3.4", 0, 0, 0xffff0000, 0x01020304);
|
---|
229 |
|
---|
230 | GOODADDR("1::1.2.3.4", 0x00010000, 0, 0, 0x01020304);
|
---|
231 | GOODADDR("1:2::1.2.3.4", 0x00010002, 0, 0, 0x01020304);
|
---|
232 | GOODADDR("1:2:3::1.2.3.4", 0x00010002, 0x00030000, 0, 0x01020304);
|
---|
233 | GOODADDR("1:2:3:4::1.2.3.4", 0x00010002, 0x00030004, 0, 0x01020304);
|
---|
234 | GOODADDR("1:2:3:4:5::1.2.3.4", 0x00010002, 0x00030004, 0x00050000, 0x01020304);
|
---|
235 |
|
---|
236 | /* ... too long */
|
---|
237 | BADADDR("1:2:3:4:5:6::1.2.3.4");
|
---|
238 | BADADDR("1:2:3:4:5::6:1.2.3.4");
|
---|
239 | BADADDR("1:2:3:4::5:6:1.2.3.4");
|
---|
240 | BADADDR("1:2:3::4:5:6:1.2.3.4");
|
---|
241 | BADADDR("1:2::3:4:5:6:1.2.3.4");
|
---|
242 | BADADDR("1::2:3:4:5:6:1.2.3.4");
|
---|
243 |
|
---|
244 | /* zone ids (beware, shaky ground) */
|
---|
245 | GOODADDR("ff01::1%0", 0xff010000, 0, 0, 1);
|
---|
246 | GOODADDR("ff01::1%eth0", 0xff010000, 0, 0, 1);
|
---|
247 | GOODADDR("ff01::1%net1.0", 0xff010000, 0, 0, 1);
|
---|
248 |
|
---|
249 | GOODADDR(" ff01::1%net1.1\t", 0xff010000, 0, 0, 1);
|
---|
250 |
|
---|
251 | /* just some light testing */
|
---|
252 | GOODCIDR("1:2:3:4:5:6:7:8", 0x00010002, 0x00030004, 0x00050006, 0x00070008, 128);
|
---|
253 | GOODCIDR("1:2:3:4::/64", 0x00010002, 0x00030004, 0, 0, 64);
|
---|
254 | GOODCIDR(" 1:2:3:4::/64 ", 0x00010002, 0x00030004, 0, 0, 64);
|
---|
255 |
|
---|
256 | /* we currently ignore the zone */
|
---|
257 | GOODCIDR("1:2:3:4::%if/64", 0x00010002, 0x00030004, 0, 0, 64);
|
---|
258 |
|
---|
259 |
|
---|
260 | GOODCIDR("::/0", 0, 0, 0, 0, 0);
|
---|
261 |
|
---|
262 | /*
|
---|
263 | * we allow zero prefix mostly for the sake of the above
|
---|
264 | * "everything"/default case, but allow it on everything - a
|
---|
265 | * conscientious caller should be doing more checks on the result
|
---|
266 | * anyway.
|
---|
267 | */
|
---|
268 | GOODCIDR("1:2:3:4::/0", 0x00010002, 0x00030004, 0, 0, 0);
|
---|
269 |
|
---|
270 |
|
---|
271 | BADCIDR("1:2:3:4:: 64");
|
---|
272 | BADCIDR("1:2:3:4::/64x");
|
---|
273 | BADCIDR("1:2:3:4::/-1");
|
---|
274 | BADCIDR("1:2:3:4::/129");
|
---|
275 | BADCIDR("1:2:3:4::/256");
|
---|
276 |
|
---|
277 | IS_ANY("::");
|
---|
278 | IS_ANY("::0.0.0.0");
|
---|
279 | IS_ANY("0:0:0:0:0:0:0:0");
|
---|
280 | IS_ANY("0000:0000:0000:0000:0000:0000:0000:0000");
|
---|
281 |
|
---|
282 | IS_ANY("\t :: \t");
|
---|
283 |
|
---|
284 | NOT_ANY("::1");
|
---|
285 | NOT_ANY("0:0:0:0:0:0:0:1");
|
---|
286 |
|
---|
287 | NOT_ANY(":: x");
|
---|
288 | NOT_ANY("::%");
|
---|
289 | NOT_ANY("::%eth0"); /* or is it? */
|
---|
290 |
|
---|
291 | return RTTestSummaryAndDestroy(hTest);
|
---|
292 | }
|
---|