1 | /* $Id: tstCidr.cpp 8295 2008-04-22 13:57:10Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - IPv4.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #include <iprt/cidr.h>
|
---|
36 | #include <iprt/err.h>
|
---|
37 | #include <iprt/stream.h>
|
---|
38 | #include <iprt/initterm.h>
|
---|
39 |
|
---|
40 |
|
---|
41 | /*******************************************************************************
|
---|
42 | * Defined Constants And Macros *
|
---|
43 | *******************************************************************************/
|
---|
44 | #define CHECKNETWORK(string, expected_result, expected_network, expected_netmask) \
|
---|
45 | do { \
|
---|
46 | RTIPV4ADDR network, netmask; \
|
---|
47 | int result = RTCidrStrToIPv4(string, &network, &netmask); \
|
---|
48 | if (expected_result && !result) \
|
---|
49 | { \
|
---|
50 | g_cErrors++; \
|
---|
51 | RTPrintf("%s, %d: %s: expected %Vrc got %Vrc\n", \
|
---|
52 | __FUNCTION__, __LINE__, string, expected_result, result); \
|
---|
53 | } \
|
---|
54 | else if ( expected_result != result \
|
---|
55 | || ( result == VINF_SUCCESS \
|
---|
56 | && ( expected_network != network \
|
---|
57 | || expected_netmask != netmask))) \
|
---|
58 | { \
|
---|
59 | g_cErrors++; \
|
---|
60 | RTPrintf("%s, %d: '%s': expected %Vrc got %Vrc, expected network %08x got %08x, expected netmask %08x got %08x\n", \
|
---|
61 | __FUNCTION__, __LINE__, string, expected_result, result, expected_network, network, expected_netmask, netmask); \
|
---|
62 | } \
|
---|
63 | } while (0)
|
---|
64 |
|
---|
65 |
|
---|
66 | /*******************************************************************************
|
---|
67 | * Global Variables *
|
---|
68 | *******************************************************************************/
|
---|
69 | static unsigned g_cErrors = 0;
|
---|
70 |
|
---|
71 |
|
---|
72 | int main()
|
---|
73 | {
|
---|
74 | RTR3Init();
|
---|
75 | CHECKNETWORK("10.0.0/24", VINF_SUCCESS, 0x0A000000, 0xFFFFFF00);
|
---|
76 | CHECKNETWORK("10.0.0/8", VINF_SUCCESS, 0x0A000000, 0xFF000000);
|
---|
77 | CHECKNETWORK("10.0.0./24", VERR_INVALID_PARAMETER, 0, 0);
|
---|
78 | CHECKNETWORK("0.1.0/24", VERR_INVALID_PARAMETER, 0, 0);
|
---|
79 | CHECKNETWORK("10.255.0.0/24", VERR_INVALID_PARAMETER, 0, 0);
|
---|
80 | CHECKNETWORK("10.1234.0.0/24", VERR_INVALID_PARAMETER, 0, 0);
|
---|
81 | CHECKNETWORK("10.256.0.0/24", VERR_INVALID_PARAMETER, 0, 0);
|
---|
82 | CHECKNETWORK("10.0.0/3", VERR_INVALID_PARAMETER, 0, 0);
|
---|
83 | CHECKNETWORK("10.1.2.3/8", VINF_SUCCESS, 0x0A010203, 0xFF000000);
|
---|
84 | CHECKNETWORK("10.0.0/29", VERR_INVALID_PARAMETER, 0, 0);
|
---|
85 | CHECKNETWORK("10.0.0/240", VERR_INVALID_PARAMETER, 0, 0);
|
---|
86 | CHECKNETWORK("10.0.0/24.", VERR_INVALID_PARAMETER, 0, 0);
|
---|
87 | CHECKNETWORK("10.1.2/16", VINF_SUCCESS, 0x0A010200, 0xFFFF0000);
|
---|
88 | CHECKNETWORK("1.2.3.4", VINF_SUCCESS, 0x01020304, 0xFFFFFFFF);
|
---|
89 | if (!g_cErrors)
|
---|
90 | RTPrintf("tstIp: SUCCESS\n", g_cErrors);
|
---|
91 | else
|
---|
92 | RTPrintf("tstIp: FAILURE - %d errors\n", g_cErrors);
|
---|
93 | return !!g_cErrors;
|
---|
94 | }
|
---|
95 |
|
---|