VirtualBox

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

Last change on this file since 52320 was 50418, checked in by vboxsync, 11 years ago

Add RTNetStrToIPv4AddrEx - a variant of IPv4 dotted-decimal parsing
function with ppszNext argument. Express RTNetStrToIPv4Addr and
RTNetIsIPv4AddrStr in its terms.

Note that RTNetIsIPv4AddrStr does not accept leading/trailing
whitespace while RTNetStrToIPv4Addr does - this is existing behaviour
that these changes keep.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 6.2 KB
Line 
1/* $Id: tstRTNetIPv4.cpp 50418 2014-02-11 18:50:58Z vboxsync $ */
2/** @file
3 * IPRT Testcase - IPv4.
4 */
5
6/*
7 * Copyright (C) 2008-2010 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
99int main()
100{
101 RTTEST hTest;
102 int rc = RTTestInitAndCreate("tstRTNetIPv4", &hTest);
103 if (rc)
104 return rc;
105 RTTestBanner(hTest);
106
107 GOODADDR("1.2.3.4", 0x01020304);
108 GOODADDR("0.0.0.0", 0x00000000);
109 GOODADDR("255.255.255.255", 0xFFFFFFFF);
110
111 /* leading and trailing whitespace is allowed */
112 GOODADDR(" 1.2.3.4 ", 0x01020304);
113 GOODADDR("\t1.2.3.4\t", 0x01020304);
114
115 BADADDR("1.2.3.4x");
116 BADADDR("1.2.3.4.");
117 BADADDR("1.2.3");
118 BADADDR("0x1.2.3.4");
119 BADADDR("666.2.3.4");
120 BADADDR("1.666.3.4");
121 BADADDR("1.2.666.4");
122 BADADDR("1.2.3.666");
123
124 /*
125 * Parsing itself is covered by the tests above, here we only
126 * check trailers
127 */
128 CHECKADDREX("1.2.3.4", "", VINF_SUCCESS, 0x01020304);
129 CHECKADDREX("1.2.3.4", " ", VINF_SUCCESS, 0x01020304);
130 CHECKADDREX("1.2.3.4", "x", VINF_SUCCESS, 0x01020304);
131 CHECKADDREX("1.2.3.444", "", VERR_INVALID_PARAMETER, 0);
132
133 return RTTestSummaryAndDestroy(hTest);
134}
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