summaryrefslogtreecommitdiffstats
path: root/src/VBox/Runtime/testcase/tstRTNetIPv6.cpp
blob: b466cf16e5ae38d764ef4e17737da2f6464f74b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* $Id: tstRTNetIPv6.cpp $ */
/** @file
 * IPRT Testcase - IPv6.
 */

/*
 * Copyright (C) 2008-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * The contents of this file may alternatively be used under the terms
 * of the Common Development and Distribution License Version 1.0
 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
 * in the VirtualBox distribution, in which case the provisions of the
 * CDDL are applicable instead of those of the GPL.
 *
 * You may elect to license modified versions of this file under the
 * terms and conditions of either the GPL or the CDDL or both.
 *
 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#include <iprt/net.h>

#include <iprt/errcore.h>
#include <iprt/initterm.h>
#include <iprt/test.h>

#include <iprt/string.h>


/*********************************************************************************************************************************
*   Defined Constants And Macros                                                                                                 *
*********************************************************************************************************************************/
#define CHECKADDR(String, rcExpected, u32_0, u32_1, u32_2, u32_3)       \
    do {                                                                \
        RTNETADDRIPV6 Addr;                                             \
        char *pszZone;                                                  \
        uint32_t ExpectedAddr[4] = {                                    \
            RT_H2N_U32_C(u32_0), RT_H2N_U32_C(u32_1),                   \
            RT_H2N_U32_C(u32_2), RT_H2N_U32_C(u32_3)                    \
        };                                                              \
        int rc2 = RTNetStrToIPv6Addr(String, &Addr, &pszZone);          \
        if ((rcExpected) && !rc2)                                       \
        {                                                               \
            RTTestIFailed("at line %d: '%s': expected %Rrc got %Rrc\n", \
                          __LINE__, String, (rcExpected), rc2);         \
        }                                                               \
        else if (   (rcExpected) != rc2                                 \
                 || (   rc2 == VINF_SUCCESS                             \
                     && memcmp(ExpectedAddr, &Addr, sizeof(Addr)) != 0)) \
        {                                                               \
            RTTestIFailed("at line %d: '%s': expected %Rrc got %Rrc,"   \
                          " expected address %RTnaipv6 got %RTnaipv6\n", \
                          __LINE__, String, rcExpected, rc2,            \
                          ExpectedAddr, &Addr);                         \
        }                                                               \
    } while (0)

#define GOODADDR(String, u32_0, u32_1, u32_2, u32_3) \
    CHECKADDR(String, VINF_SUCCESS, u32_0, u32_1, u32_2, u32_3)

#define BADADDR(String) \
    CHECKADDR(String, VERR_INVALID_PARAMETER, 0, 0, 0, 0)


#define CHECKCIDR(String, rcExpected, u32_0, u32_1, u32_2, u32_3, iExpectedPrefix)    \
    do {                                                                \
        RTNETADDRIPV6 Addr;                                             \
        uint32_t ExpectedAddr[4] = {                                    \
            RT_H2N_U32_C(u32_0), RT_H2N_U32_C(u32_1),                   \
            RT_H2N_U32_C(u32_2), RT_H2N_U32_C(u32_3)                    \
        };                                                              \
        int iPrefix;                                                    \
                                                                        \
        int rc2 = RTNetStrToIPv6Cidr(String, &Addr, &iPrefix);          \
        if ((rcExpected) && !rc2)                                       \
        {                                                               \
            RTTestIFailed("at line %d: '%s': expected %Rrc got %Rrc\n", \
                          __LINE__, String, (rcExpected), rc2);         \
        }                                                               \
        else if (   (rcExpected) != rc2                                 \
                 || (   rc2 == VINF_SUCCESS                             \
                     && (   memcmp(ExpectedAddr, &Addr, sizeof(Addr)) != 0 \
                         || iExpectedPrefix != iPrefix)))               \
        {                                                               \
            RTTestIFailed("at line %d: '%s': expected %Rrc got %Rrc,"   \
                          " expected address %RTnaipv6/%d got %RTnaipv6/%d\n",\
                          __LINE__, String, rcExpected, rc2,            \
                          ExpectedAddr, iExpectedPrefix,                \
                          &Addr, iPrefix);                              \
        }                                                               \
    } while (0)

#define GOODCIDR(String, u32_0, u32_1, u32_2, u32_3, iExpectedPrefix) \
    CHECKCIDR(String, VINF_SUCCESS, u32_0, u32_1, u32_2, u32_3, iExpectedPrefix)

#define BADCIDR(String) \
    CHECKCIDR(String, VERR_INVALID_PARAMETER, 0, 0, 0, 0, 0)


#define CHECKANY(String, fExpected)                                     \
    do {                                                                \
        bool fRc = RTNetStrIsIPv6AddrAny(String);                       \
        if (fRc != fExpected)                                           \
        {                                                               \
            RTTestIFailed("at line %d: '%s':"                           \
                          " expected %RTbool got %RTbool\n",            \
                          __LINE__, (String), fExpected, fRc);          \
        }                                                               \
    } while (0)

#define IS_ANY(String)  CHECKANY((String), true)
#define NOT_ANY(String) CHECKANY((String), false)


int main()
{
    RTTEST hTest;
    int rc = RTTestInitAndCreate("tstRTNetIPv6", &hTest);
    if (rc)
        return rc;
    RTTestBanner(hTest);


    /* base case: eight groups fully spelled */
    GOODADDR("1:2:3:4:5:6:7:8", 0x00010002, 0x00030004, 0x00050006, 0x00070008);
    GOODADDR("0001:0002:0003:0004:0005:0006:0007:0008", 0x00010002, 0x00030004, 0x00050006, 0x00070008);
    GOODADDR("D:E:A:D:b:e:e:f", 0x000d000e, 0x000a000d, 0x000b000e, 0x000e000f);

    /* ... too short or too long */
    BADADDR("1:2:3:4:5:6:7");
    BADADDR("1:2:3:4:5:6:7:8:9");

    /* ... hex group constraints */
    BADADDR("1:2:3:4:5:6:7:-8");
    BADADDR("1:2:3:4:5:6:7:0x8");
    BADADDR("1:2:3:4:5:6:7:88888");
    BADADDR("1:2:3:4:5:6:7:00008");


    /* embedded IPv4 at the end */
    GOODADDR("0:0:0:0:0:0:1.2.3.4", 0, 0, 0, 0x01020304);

    /* ... not at the end */
    BADADDR("0:0:0:0:0:1.2.3.4:0");

    /* ... too short or too long */
    BADADDR("0:0:0:0:0:0:0:1.2.3.4");
    BADADDR("0:0:0:0:0:1.2.3.4");

    /* ... invalid IPv4 address */
    BADADDR("0:0:0:0:0:0:111.222.333.444");


    /* "any" in compressed form */
    GOODADDR("::",  0, 0, 0, 0);

    /* compressed run at the beginning */
    GOODADDR("::8",                      0,          0,          0, 0x00000008);
    GOODADDR("::7:8",                    0,          0,          0, 0x00070008);
    GOODADDR("::6:7:8",                  0,          0, 0x00000006, 0x00070008);
    GOODADDR("::5:6:7:8",                0,          0, 0x00050006, 0x00070008);
    GOODADDR("::4:5:6:7:8",              0, 0x00000004, 0x00050006, 0x00070008);
    GOODADDR("::3:4:5:6:7:8",            0, 0x00030004, 0x00050006, 0x00070008);
    GOODADDR("::2:3:4:5:6:7:8", 0x00000002, 0x00030004, 0x00050006, 0x00070008);

    /* ... too long */
    BADADDR("::1:2:3:4:5:6:7:8");

    /* compressed run at the end */
    GOODADDR("1::",             0x00010000,          0,          0,          0);
    GOODADDR("1:2::",           0x00010002,          0,          0,          0);
    GOODADDR("1:2:3::",         0x00010002, 0x00030000,          0,          0);
    GOODADDR("1:2:3:4::",       0x00010002, 0x00030004,          0,          0);
    GOODADDR("1:2:3:4:5::",     0x00010002, 0x00030004, 0x00050000,          0);
    GOODADDR("1:2:3:4:5:6::",   0x00010002, 0x00030004, 0x00050006,          0);
    GOODADDR("1:2:3:4:5:6:7::", 0x00010002, 0x00030004, 0x00050006, 0x00070000);

    /* ... too long */
    BADADDR("1:2:3:4:5:6:7:8::");

    /* compressed run in the middle  */
    GOODADDR("1::8",            0x00010000,          0,          0, 0x00000008);
    GOODADDR("1:2::8",          0x00010002,          0,          0, 0x00000008);
    GOODADDR("1:2:3::8",        0x00010002, 0x00030000,          0, 0x00000008);
    GOODADDR("1:2:3:4::8",      0x00010002, 0x00030004,          0, 0x00000008);
    GOODADDR("1:2:3:4:5::8",    0x00010002, 0x00030004, 0x00050000, 0x00000008);
    GOODADDR("1:2:3:4:5:6::8",  0x00010002, 0x00030004, 0x00050006, 0x00000008);

    GOODADDR("1::7:8",          0x00010000,          0,          0, 0x00070008);
    GOODADDR("1::6:7:8",        0x00010000,          0, 0x00000006, 0x00070008);
    GOODADDR("1::5:6:7:8",      0x00010000,          0, 0x00050006, 0x00070008);
    GOODADDR("1::4:5:6:7:8",    0x00010000, 0x00000004, 0x00050006, 0x00070008);
    GOODADDR("1::3:4:5:6:7:8",  0x00010000, 0x00030004, 0x00050006, 0x00070008);

    /* ... too long */
    BADADDR("1::2:3:4:5:6:7:8");
    BADADDR("1:2::3:4:5:6:7:8");
    BADADDR("1:2:3::4:5:6:7:8");
    BADADDR("1:2:3:4::5:6:7:8");
    BADADDR("1:2:3:4:5::6:7:8");
    BADADDR("1:2:3:4:5:6::7:8");
    BADADDR("1:2:3:4:5:6:7::8");

    /* compressed with embedded IPv4 */
    GOODADDR("::0.0.0.0",        0, 0,          0,          0);
    GOODADDR("::1.2.3.4",        0, 0,          0, 0x01020304);
    GOODADDR("::ffff:1.2.3.4",   0, 0, 0x0000ffff, 0x01020304);
    GOODADDR("::ffff:0:1.2.3.4", 0, 0, 0xffff0000, 0x01020304);

    GOODADDR("1::1.2.3.4",         0x00010000,          0,          0, 0x01020304);
    GOODADDR("1:2::1.2.3.4",       0x00010002,          0,          0, 0x01020304);
    GOODADDR("1:2:3::1.2.3.4",     0x00010002, 0x00030000,          0, 0x01020304);
    GOODADDR("1:2:3:4::1.2.3.4",   0x00010002, 0x00030004,          0, 0x01020304);
    GOODADDR("1:2:3:4:5::1.2.3.4", 0x00010002, 0x00030004, 0x00050000, 0x01020304);

    /* ... too long */
    BADADDR("1:2:3:4:5:6::1.2.3.4");
    BADADDR("1:2:3:4:5::6:1.2.3.4");
    BADADDR("1:2:3:4::5:6:1.2.3.4");
    BADADDR("1:2:3::4:5:6:1.2.3.4");
    BADADDR("1:2::3:4:5:6:1.2.3.4");
    BADADDR("1::2:3:4:5:6:1.2.3.4");

    /* zone ids (beware, shaky ground) */
    GOODADDR("ff01::1%0",      0xff010000, 0, 0, 1);
    GOODADDR("ff01::1%eth0",   0xff010000, 0, 0, 1);
    GOODADDR("ff01::1%net1.0", 0xff010000, 0, 0, 1);

    GOODADDR(" ff01::1%net1.1\t", 0xff010000, 0, 0, 1);

    /* just some light testing */
    GOODCIDR("1:2:3:4:5:6:7:8", 0x00010002, 0x00030004, 0x00050006, 0x00070008, 128);
    GOODCIDR("1:2:3:4::/64",    0x00010002, 0x00030004,          0,          0,  64);
    GOODCIDR(" 1:2:3:4::/64 ",  0x00010002, 0x00030004,          0,          0,  64);

    /* we currently ignore the zone */
    GOODCIDR("1:2:3:4::%if/64", 0x00010002, 0x00030004,          0,          0,  64);


    GOODCIDR("::/0", 0, 0, 0, 0, 0);

    /*
     * we allow zero prefix mostly for the sake of the above
     * "everything"/default case, but allow it on everything - a
     * conscientious caller should be doing more checks on the result
     * anyway.
     */
    GOODCIDR("1:2:3:4::/0",    0x00010002, 0x00030004,          0,          0,    0);


    BADCIDR("1:2:3:4:: 64");
    BADCIDR("1:2:3:4::/64x");
    BADCIDR("1:2:3:4::/-1");
    BADCIDR("1:2:3:4::/129");
    BADCIDR("1:2:3:4::/256");

    IS_ANY("::");
    IS_ANY("::0.0.0.0");
    IS_ANY("0:0:0:0:0:0:0:0");
    IS_ANY("0000:0000:0000:0000:0000:0000:0000:0000");

    IS_ANY("\t :: \t");

    NOT_ANY("::1");
    NOT_ANY("0:0:0:0:0:0:0:1");

    NOT_ANY(":: x");
    NOT_ANY("::%");
    NOT_ANY("::%eth0");         /* or is it? */

    return RTTestSummaryAndDestroy(hTest);
}