VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTNetIPv6.cpp@ 53402

Last change on this file since 53402 was 50456, checked in by vboxsync, 11 years ago

RTNetStrToIPv6Addr: first cut at IPv6 address parsing. I'm still not
sure how to deal with zone ids properly given that RFC 4007 leaves
their syntax as "implementation dependent". Suggestions are welcome.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 8.3 KB
Line 
1/* $Id: tstRTNetIPv6.cpp 50456 2014-02-14 02:10:23Z vboxsync $ */
2/** @file
3 * IPRT Testcase - IPv6.
4 */
5
6/*
7 * Copyright (C) 2008-2014 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#include <iprt/string.h>
38
39/*******************************************************************************
40* Defined Constants And Macros *
41*******************************************************************************/
42#define CHECKADDR(String, rcExpected, u32_0, u32_1, u32_2, u32_3) \
43 do { \
44 RTNETADDRIPV6 Addr; \
45 char *pszZone; \
46 uint32_t ExpectedAddr[4] = { \
47 RT_H2N_U32_C(u32_0), RT_H2N_U32_C(u32_1), \
48 RT_H2N_U32_C(u32_2), RT_H2N_U32_C(u32_3) \
49 }; \
50 int rc2 = RTNetStrToIPv6Addr(String, &Addr, &pszZone); \
51 if ((rcExpected) && !rc2) \
52 { \
53 RTTestIFailed("at line %d: '%s': expected %Rrc got %Rrc\n", \
54 __LINE__, String, (rcExpected), rc2); \
55 } \
56 else if ( (rcExpected) != rc2 \
57 || ( rc2 == VINF_SUCCESS \
58 && memcmp(ExpectedAddr, &Addr, sizeof(Addr)) != 0)) \
59 { \
60 RTTestIFailed("at line %d: '%s': expected %Rrc got %Rrc," \
61 " expected address %RTnaipv6 got %RTnaipv6\n", \
62 __LINE__, String, rcExpected, rc2, \
63 ExpectedAddr, &Addr); \
64 } \
65 } while (0)
66
67#define GOODADDR(String, u32_0, u32_1, u32_2, u32_3) \
68 CHECKADDR(String, VINF_SUCCESS, u32_0, u32_1, u32_2, u32_3)
69
70#define BADADDR(String) \
71 CHECKADDR(String, VERR_INVALID_PARAMETER, 0, 0, 0, 0)
72
73
74int main()
75{
76 RTTEST hTest;
77 int rc = RTTestInitAndCreate("tstRTNetIPv6", &hTest);
78 if (rc)
79 return rc;
80 RTTestBanner(hTest);
81
82
83 /* base case: eight groups fully spelled */
84 GOODADDR("1:2:3:4:5:6:7:8", 0x00010002, 0x00030004, 0x00050006, 0x00070008);
85 GOODADDR("0001:0002:0003:0004:0005:0006:0007:0008", 0x00010002, 0x00030004, 0x00050006, 0x00070008);
86 GOODADDR("D:E:A:D:b:e:e:f", 0x000d000e, 0x000a000d, 0x000b000e, 0x000e000f);
87
88 /* ... too short or too long */
89 BADADDR("1:2:3:4:5:6:7");
90 BADADDR("1:2:3:4:5:6:7:8:9");
91
92 /* ... hex group constraints */
93 BADADDR("1:2:3:4:5:6:7:-8");
94 BADADDR("1:2:3:4:5:6:7:0x8");
95 BADADDR("1:2:3:4:5:6:7:88888");
96 BADADDR("1:2:3:4:5:6:7:00008");
97
98
99 /* embedded IPv4 at the end */
100 GOODADDR("0:0:0:0:0:0:1.2.3.4", 0, 0, 0, 0x01020304);
101
102 /* ... not at the end */
103 BADADDR("0:0:0:0:0:1.2.3.4:0");
104
105 /* ... too short or too long */
106 BADADDR("0:0:0:0:0:0:0:1.2.3.4");
107 BADADDR("0:0:0:0:0:1.2.3.4");
108
109 /* ... invalid IPv4 address */
110 BADADDR("0:0:0:0:0:0:111.222.333.444");
111
112
113 /* "any" in compressed form */
114 GOODADDR("::", 0, 0, 0, 0);
115
116 /* compressed run at the beginning */
117 GOODADDR("::8", 0, 0, 0, 0x00000008);
118 GOODADDR("::7:8", 0, 0, 0, 0x00070008);
119 GOODADDR("::6:7:8", 0, 0, 0x00000006, 0x00070008);
120 GOODADDR("::5:6:7:8", 0, 0, 0x00050006, 0x00070008);
121 GOODADDR("::4:5:6:7:8", 0, 0x00000004, 0x00050006, 0x00070008);
122 GOODADDR("::3:4:5:6:7:8", 0, 0x00030004, 0x00050006, 0x00070008);
123 GOODADDR("::2:3:4:5:6:7:8", 0x00000002, 0x00030004, 0x00050006, 0x00070008);
124
125 /* ... too long */
126 BADADDR("::1:2:3:4:5:6:7:8");
127
128 /* compressed run at the end */
129 GOODADDR("1::", 0x00010000, 0, 0, 0);
130 GOODADDR("1:2::", 0x00010002, 0, 0, 0);
131 GOODADDR("1:2:3::", 0x00010002, 0x00030000, 0, 0);
132 GOODADDR("1:2:3:4::", 0x00010002, 0x00030004, 0, 0);
133 GOODADDR("1:2:3:4:5::", 0x00010002, 0x00030004, 0x00050000, 0);
134 GOODADDR("1:2:3:4:5:6::", 0x00010002, 0x00030004, 0x00050006, 0);
135 GOODADDR("1:2:3:4:5:6:7::", 0x00010002, 0x00030004, 0x00050006, 0x00070000);
136
137 /* ... too long */
138 BADADDR("1:2:3:4:5:6:7:8::");
139
140 /* compressed run in the middle */
141 GOODADDR("1::8", 0x00010000, 0, 0, 0x00000008);
142 GOODADDR("1:2::8", 0x00010002, 0, 0, 0x00000008);
143 GOODADDR("1:2:3::8", 0x00010002, 0x00030000, 0, 0x00000008);
144 GOODADDR("1:2:3:4::8", 0x00010002, 0x00030004, 0, 0x00000008);
145 GOODADDR("1:2:3:4:5::8", 0x00010002, 0x00030004, 0x00050000, 0x00000008);
146 GOODADDR("1:2:3:4:5:6::8", 0x00010002, 0x00030004, 0x00050006, 0x00000008);
147
148 GOODADDR("1::7:8", 0x00010000, 0, 0, 0x00070008);
149 GOODADDR("1::6:7:8", 0x00010000, 0, 0x00000006, 0x00070008);
150 GOODADDR("1::5:6:7:8", 0x00010000, 0, 0x00050006, 0x00070008);
151 GOODADDR("1::4:5:6:7:8", 0x00010000, 0x00000004, 0x00050006, 0x00070008);
152 GOODADDR("1::3:4:5:6:7:8", 0x00010000, 0x00030004, 0x00050006, 0x00070008);
153
154 /* ... too long */
155 BADADDR("1::2:3:4:5:6:7:8");
156 BADADDR("1:2::3:4:5:6:7:8");
157 BADADDR("1:2:3::4:5:6:7:8");
158 BADADDR("1:2:3:4::5:6:7:8");
159 BADADDR("1:2:3:4:5::6:7:8");
160 BADADDR("1:2:3:4:5:6::7:8");
161 BADADDR("1:2:3:4:5:6:7::8");
162
163 /* compressed with embedded IPv4 */
164 GOODADDR("::0.0.0.0", 0, 0, 0, 0);
165 GOODADDR("::1.2.3.4", 0, 0, 0, 0x01020304);
166 GOODADDR("::ffff:1.2.3.4", 0, 0, 0x0000ffff, 0x01020304);
167 GOODADDR("::ffff:0:1.2.3.4", 0, 0, 0xffff0000, 0x01020304);
168
169 GOODADDR("1::1.2.3.4", 0x00010000, 0, 0, 0x01020304);
170 GOODADDR("1:2::1.2.3.4", 0x00010002, 0, 0, 0x01020304);
171 GOODADDR("1:2:3::1.2.3.4", 0x00010002, 0x00030000, 0, 0x01020304);
172 GOODADDR("1:2:3:4::1.2.3.4", 0x00010002, 0x00030004, 0, 0x01020304);
173 GOODADDR("1:2:3:4:5::1.2.3.4", 0x00010002, 0x00030004, 0x00050000, 0x01020304);
174
175 /* ... too long */
176 BADADDR("1:2:3:4:5:6::1.2.3.4");
177 BADADDR("1:2:3:4:5::6:1.2.3.4");
178 BADADDR("1:2:3:4::5:6:1.2.3.4");
179 BADADDR("1:2:3::4:5:6:1.2.3.4");
180 BADADDR("1:2::3:4:5:6:1.2.3.4");
181 BADADDR("1::2:3:4:5:6:1.2.3.4");
182
183 /* zone ids (beware, shaky ground) */
184 GOODADDR("ff01::1%0", 0xff010000, 0, 0, 1);
185 GOODADDR("ff01::1%eth0", 0xff010000, 0, 0, 1);
186 GOODADDR("ff01::1%net1.0", 0xff010000, 0, 0, 1);
187
188 GOODADDR(" ff01::1%net1.1\t", 0xff010000, 0, 0, 1);
189
190 return RTTestSummaryAndDestroy(hTest);
191}
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