1 | /* $Id: tstRTCidr.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - IPv4.
|
---|
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/cidr.h>
|
---|
42 |
|
---|
43 | #include <iprt/errcore.h>
|
---|
44 | #include <iprt/initterm.h>
|
---|
45 | #include <iprt/test.h>
|
---|
46 |
|
---|
47 |
|
---|
48 | /*********************************************************************************************************************************
|
---|
49 | * Defined Constants And Macros *
|
---|
50 | *********************************************************************************************************************************/
|
---|
51 | #define CHECKNETWORK(String, rcExpected, ExpectedNetwork, ExpectedNetMask) \
|
---|
52 | do { \
|
---|
53 | RTNETADDRIPV4 Network, NetMask; \
|
---|
54 | int rc2 = RTCidrStrToIPv4(String, &Network, &NetMask); \
|
---|
55 | if ((rcExpected) && !rc2) \
|
---|
56 | { \
|
---|
57 | RTTestIFailed("at line %d: '%s': expected %Rrc got %Rrc\n", \
|
---|
58 | __LINE__, String, (rcExpected), rc2); \
|
---|
59 | } \
|
---|
60 | else if ( (rcExpected) != rc2 \
|
---|
61 | || ( rc2 == VINF_SUCCESS \
|
---|
62 | && ( (ExpectedNetwork) != Network.u \
|
---|
63 | || (ExpectedNetMask) != NetMask.u))) \
|
---|
64 | { \
|
---|
65 | RTTestIFailed("at line %d: '%s': expected %Rrc got %Rrc, expected network %RTnaipv4 got %RTnaipv4, expected netmask %RTnaipv4 got %RTnaipv4\n", \
|
---|
66 | __LINE__, String, rcExpected, rc2, (ExpectedNetwork), Network.u, (ExpectedNetMask), NetMask.u); \
|
---|
67 | } \
|
---|
68 | } while (0)
|
---|
69 |
|
---|
70 |
|
---|
71 | int main()
|
---|
72 | {
|
---|
73 | RTTEST hTest;
|
---|
74 | int rc = RTTestInitAndCreate("tstRTCidr", &hTest);
|
---|
75 | if (rc)
|
---|
76 | return rc;
|
---|
77 | RTTestBanner(hTest);
|
---|
78 |
|
---|
79 | CHECKNETWORK("10.0.0/45", VERR_INVALID_PARAMETER, 0, 0);
|
---|
80 | CHECKNETWORK("10.0.0/-45", VERR_INVALID_PARAMETER, 0, 0);
|
---|
81 | CHECKNETWORK("10.0.0/24", VINF_SUCCESS, 0x0A000000, 0xFFFFFF00);
|
---|
82 | CHECKNETWORK("10..0.0/24", VERR_INVALID_PARAMETER, 0, 0);
|
---|
83 | CHECKNETWORK(".10.0.0/24", VERR_INVALID_PARAMETER, 0, 0);
|
---|
84 | CHECKNETWORK("10.0.0//24", VERR_INVALID_PARAMETER, 0, 0);
|
---|
85 | CHECKNETWORK("10.0.0/8", VINF_SUCCESS, 0x0A000000, 0xFF000000);
|
---|
86 | CHECKNETWORK("10.0.0./24", VERR_INVALID_PARAMETER, 0, 0);
|
---|
87 | CHECKNETWORK("0.1.0/24", VERR_INVALID_PARAMETER, 0, 0);
|
---|
88 | /* RFC 4632 s3.1: n.n.n.0/24, where n is an 8-bit decimal octet value*/
|
---|
89 | CHECKNETWORK("10.255.0.0/24", VINF_SUCCESS, 0x0AFF0000, 0xFFFFFF00);
|
---|
90 | CHECKNETWORK("10.1234.0.0/24", VERR_INVALID_PARAMETER, 0, 0);
|
---|
91 | CHECKNETWORK("10.256.0.0/24", VERR_INVALID_PARAMETER, 0, 0);
|
---|
92 | CHECKNETWORK("10.0.0/3", VERR_INVALID_PARAMETER, 0, 0);
|
---|
93 | /* RFC 4632 s3.1: legacy "Class A" is n.0.0.0/8 */
|
---|
94 | CHECKNETWORK("10.1.2.3/8", VERR_INVALID_PARAMETER, 0, 0);
|
---|
95 | CHECKNETWORK("10.1.2.4/30", VINF_SUCCESS, 0x0A010204, 0xFFFFFFFC);
|
---|
96 | CHECKNETWORK("10.0.0/29", VERR_INVALID_PARAMETER, 0, 0);
|
---|
97 | CHECKNETWORK("10.0.0/240", VERR_INVALID_PARAMETER, 0, 0);
|
---|
98 | CHECKNETWORK("10.0.0/24.", VERR_INVALID_PARAMETER, 0, 0);
|
---|
99 | /* RFC 4632 s3.1: legacy "Class B" is n.n.0.0/16 */
|
---|
100 | CHECKNETWORK("10.1.2/16", VERR_INVALID_PARAMETER, 0, 0);
|
---|
101 | CHECKNETWORK("10.1/16", VINF_SUCCESS, 0x0A010000, 0xFFFF0000);
|
---|
102 | CHECKNETWORK("10.1.0.0/16", VINF_SUCCESS, 0x0A010000, 0xFFFF0000);
|
---|
103 | CHECKNETWORK("10.1.0.0.0/16", VERR_INVALID_PARAMETER, 0, 0);
|
---|
104 | CHECKNETWORK("1.2.3.4", VINF_SUCCESS, 0x01020304, 0xFFFFFFFF);
|
---|
105 | CHECKNETWORK("1.2.3.255", VINF_SUCCESS, 0x010203FF, 0xFFFFFFFF);
|
---|
106 | CHECKNETWORK("1.2.3.256", VERR_INVALID_PARAMETER, 0, 0);
|
---|
107 | CHECKNETWORK("10.1.255/24", VINF_SUCCESS, 0x0A01FF00, 0xFFFFFF00);
|
---|
108 | CHECKNETWORK("10.1.254/24", VINF_SUCCESS, 0x0A01FE00, 0xFFFFFF00);
|
---|
109 | CHECKNETWORK("10.255.1/24", VINF_SUCCESS, 0x0AFF0100, 0xFFFFFF00);
|
---|
110 | CHECKNETWORK("10.255.1.1/24", VERR_INVALID_PARAMETER, 0, 0);
|
---|
111 | CHECKNETWORK("1.2", VERR_INVALID_PARAMETER, 0, 0);
|
---|
112 | CHECKNETWORK("1.2.3.4.5", VERR_INVALID_PARAMETER, 0, 0);
|
---|
113 |
|
---|
114 | return RTTestSummaryAndDestroy(hTest);
|
---|
115 | }
|
---|
116 |
|
---|