VirtualBox

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

Last change on this file since 60257 was 58438, checked in by vboxsync, 9 years ago

IPRT/net: add RTNetStrIsIPv4AddrAny, RTNetStrIsIPv6AddrAny.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 7.4 KB
Line 
1/* $Id: tstRTNetIPv4.cpp 58438 2015-10-27 16:30:34Z vboxsync $ */
2/** @file
3 * IPRT Testcase - IPv4.
4 */
5
6/*
7 * Copyright (C) 2008-2015 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 CHECKANY(String, fExpected) \
100 do { \
101 bool fRc = RTNetStrIsIPv4AddrAny(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_ANY(String) CHECKANY((String), true)
111#define NOT_ANY(String) CHECKANY((String), false)
112
113
114int main()
115{
116 RTTEST hTest;
117 int rc = RTTestInitAndCreate("tstRTNetIPv4", &hTest);
118 if (rc)
119 return rc;
120 RTTestBanner(hTest);
121
122 GOODADDR("1.2.3.4", 0x01020304);
123 GOODADDR("0.0.0.0", 0x00000000);
124 GOODADDR("255.255.255.255", 0xFFFFFFFF);
125
126 /* leading and trailing whitespace is allowed */
127 GOODADDR(" 1.2.3.4 ", 0x01020304);
128 GOODADDR("\t1.2.3.4\t", 0x01020304);
129
130 BADADDR("1.2.3.4x");
131 BADADDR("1.2.3.4.");
132 BADADDR("1.2.3");
133 BADADDR("0x1.2.3.4");
134 BADADDR("666.2.3.4");
135 BADADDR("1.666.3.4");
136 BADADDR("1.2.666.4");
137 BADADDR("1.2.3.666");
138
139 /*
140 * Parsing itself is covered by the tests above, here we only
141 * check trailers
142 */
143 CHECKADDREX("1.2.3.4", "", VINF_SUCCESS, 0x01020304);
144 CHECKADDREX("1.2.3.4", " ", VINF_SUCCESS, 0x01020304);
145 CHECKADDREX("1.2.3.4", "x", VINF_SUCCESS, 0x01020304);
146 CHECKADDREX("1.2.3.444", "", VERR_INVALID_PARAMETER, 0);
147
148
149 IS_ANY("0.0.0.0");
150 IS_ANY("\t 0.0.0.0 \t");
151
152 NOT_ANY("1.1.1.1"); /* good address, but not INADDR_ANY */
153 NOT_ANY("0.0.0.0x"); /* bad address */
154
155 return RTTestSummaryAndDestroy(hTest);
156}
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