VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTNetIPv4.cpp@ 72331

Last change on this file since 72331 was 71435, checked in by vboxsync, 7 years ago

IPRT/net: RTNetStrToIPv4AddrEx - return VWRN_TRAILING_SPACES and
VWRN_TRAILING_CHARS. Adapt callers. Add more test cases to make sure
trailers are handled as before.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 17.7 KB
Line 
1/* $Id: tstRTNetIPv4.cpp 71435 2018-03-21 14:30:29Z vboxsync $ */
2/** @file
3 * IPRT Testcase - IPv4.
4 */
5
6/*
7 * Copyright (C) 2008-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/net.h>
32
33#include <iprt/err.h>
34#include <iprt/initterm.h>
35#include <iprt/test.h>
36
37
38/*********************************************************************************************************************************
39* Defined Constants And Macros *
40*********************************************************************************************************************************/
41#define CHECKADDR(String, rcExpected, ExpectedAddr) \
42 do { \
43 RTNETADDRIPV4 Addr; \
44 int rc2 = RTNetStrToIPv4Addr(String, &Addr); \
45 if ((rcExpected) && !rc2) \
46 { \
47 RTTestIFailed("at line %d: '%s': expected %Rrc got %Rrc\n", \
48 __LINE__, String, (rcExpected), rc2); \
49 } \
50 else if ( (rcExpected) != rc2 \
51 || ( rc2 == VINF_SUCCESS \
52 && RT_H2N_U32_C(ExpectedAddr) != Addr.u)) \
53 { \
54 RTTestIFailed("at line %d: '%s': expected %Rrc got %Rrc," \
55 " expected address %RTnaipv4 got %RTnaipv4\n", \
56 __LINE__, String, rcExpected, rc2, \
57 RT_H2N_U32_C(ExpectedAddr), Addr.u); \
58 } \
59 } while (0)
60
61#define GOODADDR(String, ExpectedAddr) \
62 CHECKADDR(String, VINF_SUCCESS, ExpectedAddr)
63
64#define BADADDR(String) \
65 CHECKADDR(String, VERR_INVALID_PARAMETER, 0)
66
67
68#define CHECKADDREX(String, Trailer, rcExpected, ExpectedAddr) \
69 do { \
70 RTNETADDRIPV4 Addr; \
71 const char *strAll = String /* concat */ Trailer; \
72 const char *pTrailer = strAll + sizeof(String) - 1; \
73 char *pNext = NULL; \
74 int rc2 = RTNetStrToIPv4AddrEx(strAll, &Addr, &pNext); \
75 if ((rcExpected) && !rc2) \
76 { \
77 RTTestIFailed("at line %d: '%s': expected %Rrc got %Rrc\n", \
78 __LINE__, String, (rcExpected), rc2); \
79 } \
80 else if ((rcExpected) != rc2 \
81 || (rc2 == VINF_SUCCESS \
82 && (RT_H2N_U32_C(ExpectedAddr) != Addr.u \
83 || pTrailer != pNext))) \
84 { \
85 RTTestIFailed("at line %d: '%s': expected %Rrc got %Rrc," \
86 " expected address %RTnaipv4 got %RTnaipv4" \
87 " expected trailer \"%s\" got %s%s%s" \
88 "\n", \
89 __LINE__, String, rcExpected, rc2, \
90 RT_H2N_U32_C(ExpectedAddr), Addr.u, \
91 pTrailer, \
92 pNext ? "\"" : "", \
93 pNext ? pNext : "(null)", \
94 pNext ? "\"" : ""); \
95 } \
96 } while (0)
97
98
99#define CHECKISADDR(String, fExpected) \
100 do { \
101 bool fRc = RTNetIsIPv4AddrStr(String); \
102 if (fRc != fExpected) \
103 { \
104 RTTestIFailed("at line %d: '%s':" \
105 " expected %RTbool got %RTbool\n", \
106 __LINE__, (String), fExpected, fRc); \
107 } \
108 } while (0)
109
110#define IS_ADDR(String) CHECKISADDR((String), true)
111#define NOT_ADDR(String) CHECKISADDR((String), false)
112
113
114#define CHECKANY(String, fExpected) \
115 do { \
116 bool fRc = RTNetStrIsIPv4AddrAny(String); \
117 if (fRc != fExpected) \
118 { \
119 RTTestIFailed("at line %d: '%s':" \
120 " expected %RTbool got %RTbool\n", \
121 __LINE__, (String), fExpected, fRc); \
122 } \
123 } while (0)
124
125#define IS_ANY(String) CHECKANY((String), true)
126#define NOT_ANY(String) CHECKANY((String), false)
127
128
129#define CHECKMASK(_mask, _rcExpected, _iExpectedPrefix) \
130 do { \
131 /* const */ RTNETADDRIPV4 Mask; \
132 int iExpectedPrefix = (_iExpectedPrefix); \
133 int iPrefix; \
134 const int rcExpected = (_rcExpected); \
135 int rc2; \
136 \
137 Mask.u = RT_H2N_U32_C(UINT32_C(_mask)); \
138 rc2 = RTNetMaskToPrefixIPv4(&Mask, &iPrefix); \
139 \
140 if (rcExpected == VINF_SUCCESS) \
141 { \
142 if (rc2 != rcExpected) \
143 { \
144 /* unexpected failure */ \
145 RTTestIFailed("at line %d: mask %RTnaipv4:" \
146 " expected prefix length %d got %Rrc\n", \
147 __LINE__, Mask.u, \
148 iExpectedPrefix, rc2); \
149 } \
150 else if (iPrefix != iExpectedPrefix) \
151 { \
152 /* unexpected result */ \
153 RTTestIFailed("at line %d: mask %RTnaipv4:" \
154 " expected prefix length %d got %d\n", \
155 __LINE__, Mask.u, \
156 iExpectedPrefix, iPrefix); \
157 } \
158 } \
159 else /* expect failure */ \
160 { \
161 if (rc2 == VINF_SUCCESS) \
162 { \
163 /* unexpected success */ \
164 RTTestIFailed("at line %d: mask %RTnaipv4:" \
165 " expected %Rrc got prefix length %d\n", \
166 __LINE__, Mask.u, \
167 rcExpected, iPrefix); \
168 } \
169 else if (rc2 != rcExpected) \
170 { \
171 /* unexpected error */ \
172 RTTestIFailed("at line %d: mask %RTnaipv4:" \
173 " expected %Rrc got %Rrc\n", \
174 __LINE__, Mask.u, \
175 rcExpected, rc2); \
176 } \
177 } \
178 } while (0)
179
180#define CHECKPREFIX(_prefix, _rcExpected, _mask) \
181 do { \
182 const int iPrefix = (_prefix); \
183 RTNETADDRIPV4 ExpectedMask, Mask; \
184 const int rcExpected = (_rcExpected); \
185 int rc2; \
186 \
187 ExpectedMask.u = RT_H2N_U32_C(UINT32_C(_mask)); \
188 rc2 = RTNetPrefixToMaskIPv4(iPrefix, &Mask); \
189 \
190 if (rcExpected == VINF_SUCCESS) \
191 { \
192 if (rc2 != rcExpected) \
193 { \
194 /* unexpected failure */ \
195 RTTestIFailed("at line %d: prefix %d:" \
196 " expected mask %RTnaipv4 got %Rrc\n", \
197 __LINE__, iPrefix, \
198 ExpectedMask.u, rc2); \
199 } \
200 else if (Mask.u != ExpectedMask.u) \
201 { \
202 /* unexpected result */ \
203 RTTestIFailed("at line %d: prefix %d:" \
204 " expected mask %RTnaipv4 got %RTnaipv4\n", \
205 __LINE__, iPrefix, \
206 ExpectedMask.u, Mask.u); \
207 } \
208 } \
209 else /* expect failure */ \
210 { \
211 if (rc2 == VINF_SUCCESS) \
212 { \
213 /* unexpected success */ \
214 RTTestIFailed("at line %d: prefix %d:" \
215 " expected %Rrc got mask %RTnaipv4\n", \
216 __LINE__, iPrefix, \
217 rcExpected, Mask.u); \
218 } \
219 else if (rc2 != rcExpected) \
220 { \
221 /* unexpected error */ \
222 RTTestIFailed("at line %d: prefix %d:" \
223 " expected %Rrc got %Rrc\n", \
224 __LINE__, iPrefix, \
225 rcExpected, rc2); \
226 } \
227 } \
228 } while (0)
229
230#define GOODMASK(_mask, _prefix) \
231 do { \
232 CHECKMASK(_mask, VINF_SUCCESS, _prefix); \
233 CHECKPREFIX(_prefix, VINF_SUCCESS, _mask); \
234 } while (0)
235
236#define BADMASK(_mask) \
237 CHECKMASK(_mask, VERR_INVALID_PARAMETER, -1)
238
239#define BADPREFIX(_prefix) \
240 CHECKPREFIX(_prefix, VERR_INVALID_PARAMETER, 0)
241
242
243int main()
244{
245 RTTEST hTest;
246 int rc = RTTestInitAndCreate("tstRTNetIPv4", &hTest);
247 if (rc)
248 return rc;
249 RTTestBanner(hTest);
250
251 GOODADDR("1.2.3.4", 0x01020304);
252 GOODADDR("0.0.0.0", 0x00000000);
253 GOODADDR("255.255.255.255", 0xFFFFFFFF);
254
255 /* leading and trailing whitespace is allowed */
256 GOODADDR(" 1.2.3.4 ", 0x01020304);
257 GOODADDR("\t1.2.3.4\t", 0x01020304);
258
259 BADADDR("1.2.3.4x");
260 BADADDR("1.2.3.4.");
261 BADADDR("1.2.3");
262 BADADDR("0x1.2.3.4");
263 BADADDR("666.2.3.4");
264 BADADDR("1.666.3.4");
265 BADADDR("1.2.666.4");
266 BADADDR("1.2.3.666");
267
268 /*
269 * Parsing itself is covered by the tests above, here we only
270 * check trailers
271 */
272 CHECKADDREX("1.2.3.4", "", VINF_SUCCESS, 0x01020304);
273 CHECKADDREX("1.2.3.4", " ", VWRN_TRAILING_SPACES, 0x01020304);
274 CHECKADDREX("1.2.3.4", "x", VWRN_TRAILING_CHARS, 0x01020304);
275 CHECKADDREX("1.2.3.444", "", VERR_INVALID_PARAMETER, 0);
276
277 /* NB: RTNetIsIPv4AddrStr does NOT allow leading/trailing whitespace */
278 IS_ADDR("1.2.3.4");
279 NOT_ADDR(" 1.2.3.4");
280 NOT_ADDR("1.2.3.4 ");
281 NOT_ADDR("1.2.3.4x");
282
283 IS_ANY("0.0.0.0");
284 IS_ANY("\t 0.0.0.0 \t"); /* ... but RTNetStrIsIPv4AddrAny does */
285
286 NOT_ANY("1.1.1.1"); /* good address, but not INADDR_ANY */
287 NOT_ANY("0.0.0.0x"); /* bad address */
288
289
290 /*
291 * The mask <-> prefix table is small so we can test it all.
292 */
293 GOODMASK(0x00000000, 0); /* 0.0.0.0 */
294 GOODMASK(0x80000000, 1); /* 128.0.0.0 */
295 GOODMASK(0xc0000000, 2); /* 192.0.0.0 */
296 GOODMASK(0xe0000000, 3); /* 224.0.0.0 */
297 GOODMASK(0xf0000000, 4); /* 240.0.0.0 */
298 GOODMASK(0xf8000000, 5); /* 248.0.0.0 */
299 GOODMASK(0xfc000000, 6); /* 252.0.0.0 */
300 GOODMASK(0xfe000000, 7); /* 254.0.0.0 */
301 GOODMASK(0xff000000, 8); /* 255.0.0.0 */
302 GOODMASK(0xff800000, 9); /* 255.128.0.0 */
303 GOODMASK(0xffc00000, 10); /* 255.192.0.0 */
304 GOODMASK(0xffe00000, 11); /* 255.224.0.0 */
305 GOODMASK(0xfff00000, 12); /* 255.240.0.0 */
306 GOODMASK(0xfff80000, 13); /* 255.248.0.0 */
307 GOODMASK(0xfffc0000, 14); /* 255.252.0.0 */
308 GOODMASK(0xfffe0000, 15); /* 255.254.0.0 */
309 GOODMASK(0xffff0000, 16); /* 255.255.0.0 */
310 GOODMASK(0xffff8000, 17); /* 255.255.128.0 */
311 GOODMASK(0xffffc000, 18); /* 255.255.192.0 */
312 GOODMASK(0xffffe000, 19); /* 255.255.224.0 */
313 GOODMASK(0xfffff000, 20); /* 255.255.240.0 */
314 GOODMASK(0xfffff800, 21); /* 255.255.248.0 */
315 GOODMASK(0xfffffc00, 22); /* 255.255.252.0 */
316 GOODMASK(0xfffffe00, 23); /* 255.255.254.0 */
317 GOODMASK(0xffffff00, 24); /* 255.255.255.0 */
318 GOODMASK(0xffffff80, 25); /* 255.255.255.128 */
319 GOODMASK(0xffffffc0, 26); /* 255.255.255.192 */
320 GOODMASK(0xffffffe0, 27); /* 255.255.255.224 */
321 GOODMASK(0xfffffff0, 28); /* 255.255.255.240 */
322 GOODMASK(0xfffffff8, 29); /* 255.255.255.248 */
323 GOODMASK(0xfffffffc, 30); /* 255.255.255.252 */
324 GOODMASK(0xfffffffe, 31); /* 255.255.255.254 */
325 GOODMASK(0xffffffff, 32); /* 255.255.255.255 */
326
327 BADMASK(0x01020304);
328
329 BADPREFIX(-1);
330 BADPREFIX(33);
331
332 return RTTestSummaryAndDestroy(hTest);
333}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette