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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
|
// Copyright (C) 2012-2023 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <config.h>
#include <asiolink/addr_utilities.h>
#include <exceptions/exceptions.h>
#include <util/bigints.h>
#include <gtest/gtest.h>
#include <vector>
#include <stdint.h>
#include <stdlib.h>
using namespace std;
using namespace isc::asiolink;
using namespace isc::util;
namespace {
// This test verifies that lastAddrInPrefix is able to handle IPv4 operations.
TEST(AddrUtilitiesTest, lastAddrInPrefix4) {
IOAddress addr1("192.0.2.1");
// Prefixes rounded to addresses are easy...
EXPECT_EQ("192.255.255.255", lastAddrInPrefix(addr1, 8).toText());
EXPECT_EQ("192.0.255.255", lastAddrInPrefix(addr1, 16).toText());
EXPECT_EQ("192.0.2.255", lastAddrInPrefix(addr1, 24).toText());
// these are trickier
EXPECT_EQ("192.0.2.127", lastAddrInPrefix(addr1, 25).toText());
EXPECT_EQ("192.0.2.63", lastAddrInPrefix(addr1, 26).toText());
EXPECT_EQ("192.0.2.31", lastAddrInPrefix(addr1, 27).toText());
EXPECT_EQ("192.0.2.15", lastAddrInPrefix(addr1, 28).toText());
EXPECT_EQ("192.0.2.7", lastAddrInPrefix(addr1, 29).toText());
EXPECT_EQ("192.0.2.3", lastAddrInPrefix(addr1, 30).toText());
// that doesn't make much sense as /31 subnet consists of network address
// and a broadcast address, with 0 usable addresses.
EXPECT_EQ("192.0.2.1", lastAddrInPrefix(addr1, 31).toText());
EXPECT_EQ("192.0.2.1", lastAddrInPrefix(addr1, 32).toText());
// Let's check extreme cases
IOAddress anyAddr("0.0.0.0");
EXPECT_EQ("127.255.255.255", lastAddrInPrefix(anyAddr, 1).toText());
EXPECT_EQ("255.255.255.255", lastAddrInPrefix(anyAddr, 0).toText());
EXPECT_EQ("0.0.0.0", lastAddrInPrefix(anyAddr, 32).toText());
}
// This test checks if firstAddrInPrefix is able to handle IPv4 operations.
TEST(AddrUtilitiesTest, firstAddrInPrefix4) {
IOAddress addr1("192.223.2.255");
// Prefixes rounded to addresses are easy...
EXPECT_EQ("192.0.0.0", firstAddrInPrefix(addr1, 8).toText());
EXPECT_EQ("192.223.0.0", firstAddrInPrefix(addr1, 16).toText());
EXPECT_EQ("192.223.2.0", firstAddrInPrefix(addr1, 24).toText());
// these are trickier
EXPECT_EQ("192.223.2.128", firstAddrInPrefix(addr1, 25).toText());
EXPECT_EQ("192.223.2.192", firstAddrInPrefix(addr1, 26).toText());
EXPECT_EQ("192.223.2.224", firstAddrInPrefix(addr1, 27).toText());
EXPECT_EQ("192.223.2.240", firstAddrInPrefix(addr1, 28).toText());
EXPECT_EQ("192.223.2.248", firstAddrInPrefix(addr1, 29).toText());
EXPECT_EQ("192.223.2.252", firstAddrInPrefix(addr1, 30).toText());
// that doesn't make much sense as /31 subnet consists of network address
// and a broadcast address, with 0 usable addresses.
EXPECT_EQ("192.223.2.254", firstAddrInPrefix(addr1, 31).toText());
EXPECT_EQ("192.223.2.255", firstAddrInPrefix(addr1, 32).toText());
// Let's check extreme cases.
IOAddress bcast("255.255.255.255");
EXPECT_EQ("128.0.0.0", firstAddrInPrefix(bcast, 1).toText());
EXPECT_EQ("0.0.0.0", firstAddrInPrefix(bcast, 0).toText());
EXPECT_EQ("255.255.255.255", firstAddrInPrefix(bcast, 32).toText());
}
/// This test checks if lastAddrInPrefix properly supports IPv6 operations
TEST(AddrUtilitiesTest, lastAddrInPrefix6) {
IOAddress addr1("2001:db8:1:1234:5678:abcd:1234:beef");
// Prefixes rounded to nibbles are easy...
EXPECT_EQ("2001:db8:1:1234:5678:abcd:1234:ffff",
lastAddrInPrefix(addr1, 112).toText());
EXPECT_EQ("2001:db8:1:1234:5678:abcd:123f:ffff",
lastAddrInPrefix(addr1, 108).toText());
EXPECT_EQ("2001:db8:1:1234:5678:abcd:12ff:ffff",
lastAddrInPrefix(addr1, 104).toText());
EXPECT_EQ("2001:db8:1:1234:ffff:ffff:ffff:ffff",
lastAddrInPrefix(addr1, 64).toText());
IOAddress addr2("2001::");
// These are trickier, though, as they are done in 1 bit increments
// the last address in 2001::/127 pool should be 2001::1
EXPECT_EQ("2001::1", lastAddrInPrefix(addr2, 127).toText());
EXPECT_EQ("2001::3", lastAddrInPrefix(addr2, 126).toText());
EXPECT_EQ("2001::7", lastAddrInPrefix(addr2, 125).toText());
EXPECT_EQ("2001::f", lastAddrInPrefix(addr2, 124).toText());
EXPECT_EQ("2001::1f", lastAddrInPrefix(addr2, 123).toText());
EXPECT_EQ("2001::3f", lastAddrInPrefix(addr2, 122).toText());
EXPECT_EQ("2001::7f", lastAddrInPrefix(addr2, 121).toText());
EXPECT_EQ("2001::ff", lastAddrInPrefix(addr2, 120).toText());
// Let's check extreme cases
IOAddress anyAddr("::");
EXPECT_EQ("7fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
lastAddrInPrefix(anyAddr, 1).toText());
EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
lastAddrInPrefix(anyAddr, 0).toText());
EXPECT_EQ("::", lastAddrInPrefix(anyAddr, 128).toText());
}
/// This test checks if firstAddrInPrefix properly supports IPv6 operations
TEST(AddrUtilitiesTest, firstAddrInPrefix6) {
IOAddress addr1("2001:db8:1:1234:5678:1234:abcd:beef");
// Prefixes rounded to nibbles are easy...
EXPECT_EQ("2001:db8:1:1234:5678:1234::",
firstAddrInPrefix(addr1, 96).toText());
EXPECT_EQ("2001:db8:1:1234:5678:1230::",
firstAddrInPrefix(addr1, 92).toText());
EXPECT_EQ("2001:db8:1:1234:5678:1200::",
firstAddrInPrefix(addr1, 88).toText());
EXPECT_EQ("2001:db8:1:1234::",
firstAddrInPrefix(addr1, 64).toText());
IOAddress addr2("2001::ffff");
// These are trickier, though, as they are done in 1 bit increments
// the first address in 2001::/127 pool should be 2001::1
EXPECT_EQ("2001::fffe", firstAddrInPrefix(addr2, 127).toText());
EXPECT_EQ("2001::fffc", firstAddrInPrefix(addr2, 126).toText());
EXPECT_EQ("2001::fff8", firstAddrInPrefix(addr2, 125).toText());
EXPECT_EQ("2001::fff0", firstAddrInPrefix(addr2, 124).toText());
EXPECT_EQ("2001::ffe0", firstAddrInPrefix(addr2, 123).toText());
EXPECT_EQ("2001::ffc0", firstAddrInPrefix(addr2, 122).toText());
EXPECT_EQ("2001::ff80", firstAddrInPrefix(addr2, 121).toText());
EXPECT_EQ("2001::ff00", firstAddrInPrefix(addr2, 120).toText());
}
// Checks if IPv4 netmask is generated properly
TEST(AddrUtilitiesTest, getNetmask4) {
EXPECT_EQ("0.0.0.0", getNetmask4(0).toText());
EXPECT_EQ("128.0.0.0", getNetmask4(1).toText());
EXPECT_EQ("192.0.0.0", getNetmask4(2).toText());
EXPECT_EQ("224.0.0.0", getNetmask4(3).toText());
EXPECT_EQ("240.0.0.0", getNetmask4(4).toText());
EXPECT_EQ("248.0.0.0", getNetmask4(5).toText());
EXPECT_EQ("252.0.0.0", getNetmask4(6).toText());
EXPECT_EQ("254.0.0.0", getNetmask4(7).toText());
EXPECT_EQ("255.0.0.0", getNetmask4(8).toText());
EXPECT_EQ("255.128.0.0", getNetmask4(9).toText());
EXPECT_EQ("255.192.0.0", getNetmask4(10).toText());
EXPECT_EQ("255.224.0.0", getNetmask4(11).toText());
EXPECT_EQ("255.240.0.0", getNetmask4(12).toText());
EXPECT_EQ("255.248.0.0", getNetmask4(13).toText());
EXPECT_EQ("255.252.0.0", getNetmask4(14).toText());
EXPECT_EQ("255.254.0.0", getNetmask4(15).toText());
EXPECT_EQ("255.255.0.0", getNetmask4(16).toText());
EXPECT_EQ("255.255.128.0", getNetmask4(17).toText());
EXPECT_EQ("255.255.192.0", getNetmask4(18).toText());
EXPECT_EQ("255.255.224.0", getNetmask4(19).toText());
EXPECT_EQ("255.255.240.0", getNetmask4(20).toText());
EXPECT_EQ("255.255.248.0", getNetmask4(21).toText());
EXPECT_EQ("255.255.252.0", getNetmask4(22).toText());
EXPECT_EQ("255.255.254.0", getNetmask4(23).toText());
EXPECT_EQ("255.255.255.0", getNetmask4(24).toText());
EXPECT_EQ("255.255.255.128", getNetmask4(25).toText());
EXPECT_EQ("255.255.255.192", getNetmask4(26).toText());
EXPECT_EQ("255.255.255.224", getNetmask4(27).toText());
EXPECT_EQ("255.255.255.240", getNetmask4(28).toText());
EXPECT_EQ("255.255.255.248", getNetmask4(29).toText());
EXPECT_EQ("255.255.255.252", getNetmask4(30).toText());
EXPECT_EQ("255.255.255.254", getNetmask4(31).toText());
EXPECT_EQ("255.255.255.255", getNetmask4(32).toText());
EXPECT_THROW(getNetmask4(33), isc::BadValue);
}
// Checks if the calculation for IPv4 addresses in range are correct.
TEST(AddrUtilitiesTest, addrsInRange4) {
// Let's start with something simple
EXPECT_EQ(1, addrsInRange(IOAddress("192.0.2.0"), IOAddress("192.0.2.0")));
EXPECT_EQ(10, addrsInRange(IOAddress("192.0.2.10"), IOAddress("192.0.2.19")));
EXPECT_EQ(256, addrsInRange(IOAddress("192.0.2.0"), IOAddress("192.0.2.255")));
EXPECT_EQ(65536, addrsInRange(IOAddress("192.0.0.0"), IOAddress("192.0.255.255")));
EXPECT_EQ(16777216, addrsInRange(IOAddress("10.0.0.0"), IOAddress("10.255.255.255")));
// Let's check if the network boundaries are crossed correctly.
EXPECT_EQ(3, addrsInRange(IOAddress("10.0.0.255"), IOAddress("10.0.1.1")));
// Let's go a bit overboard with this! How many addresses are there in
// IPv4 address space? That's a slightly tricky question, as the answer
// cannot be stored in uint32_t.
EXPECT_EQ(uint64_t(std::numeric_limits<uint32_t>::max()) + 1,
addrsInRange(IOAddress("0.0.0.0"), IOAddress("255.255.255.255")));
// The upper bound cannot be smaller than the lower bound.
EXPECT_THROW(addrsInRange(IOAddress("192.0.2.5"), IOAddress("192.0.2.4")),
isc::BadValue);
}
// Checks if the calculation for IPv6 addresses in range are correct.
TEST(AddrUtilitiesTest, addrsInRange6) {
// Let's start with something simple
EXPECT_EQ(1, addrsInRange(IOAddress("::"), IOAddress("::")));
EXPECT_EQ(16, addrsInRange(IOAddress("fe80::1"), IOAddress("fe80::10")));
EXPECT_EQ(65536, addrsInRange(IOAddress("fe80::"), IOAddress("fe80::ffff")));
EXPECT_EQ(uint64_t(std::numeric_limits<uint32_t>::max()) + 1,
addrsInRange(IOAddress("fe80::"), IOAddress("fe80::ffff:ffff")));
// There's 2^80 addresses between those. Due to uint64_t limits, this method is
// capped at 2^64 -1.
EXPECT_EQ(std::numeric_limits<uint64_t>::max(),
addrsInRange(IOAddress("2001:db8:1::"), IOAddress("2001:db8:2::")));
// Let's check if the network boundaries are crossed correctly.
EXPECT_EQ(3, addrsInRange(IOAddress("2001:db8::ffff"), IOAddress("2001:db8::1:1")));
// Let's go a bit overboard with this! How many addresses are there in
// IPv6 address space? That's a really tricky question, as the answer
// wouldn't fit even in uint128_t (if we had it). This method is capped
// at max value of uint64_t.
EXPECT_EQ(std::numeric_limits<uint64_t>::max(), addrsInRange(IOAddress("::"),
IOAddress("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")));
EXPECT_THROW(addrsInRange(IOAddress("fe80::5"), IOAddress("fe80::4")),
isc::BadValue);
}
// Checks if IPv4 address ranges can be converted to prefix / prefix_len
TEST(AddrUtilitiesTest, prefixLengthFromRange4) {
// Use a shorter name
const auto& plfr = prefixLengthFromRange;
// Let's start with something simple
EXPECT_EQ(32, plfr(IOAddress("192.0.2.0"), IOAddress("192.0.2.0")));
EXPECT_EQ(31, plfr(IOAddress("192.0.2.0"), IOAddress("192.0.2.1")));
EXPECT_EQ(30, plfr(IOAddress("192.0.2.0"), IOAddress("192.0.2.3")));
EXPECT_EQ(29, plfr(IOAddress("192.0.2.0"), IOAddress("192.0.2.7")));
EXPECT_EQ(28, plfr(IOAddress("192.0.2.0"), IOAddress("192.0.2.15")));
EXPECT_EQ(27, plfr(IOAddress("192.0.2.0"), IOAddress("192.0.2.31")));
EXPECT_EQ(26, plfr(IOAddress("192.0.2.0"), IOAddress("192.0.2.63")));
EXPECT_EQ(25, plfr(IOAddress("192.0.2.0"), IOAddress("192.0.2.127")));
EXPECT_EQ(24, plfr(IOAddress("192.0.2.0"), IOAddress("192.0.2.255")));
EXPECT_EQ(23, plfr(IOAddress("192.0.2.0"), IOAddress("192.0.3.255")));
EXPECT_EQ(16, plfr(IOAddress("10.0.0.0"), IOAddress("10.0.255.255")));
EXPECT_EQ(8, plfr(IOAddress("10.0.0.0"), IOAddress("10.255.255.255")));
EXPECT_EQ(0, plfr(IOAddress("0.0.0.0"), IOAddress("255.255.255.255")));
// Fail if a network boundary is crossed
EXPECT_EQ(-1, plfr(IOAddress("10.0.0.255"), IOAddress("10.0.1.1")));
// Fail if first is not at the begin
EXPECT_EQ(-1, plfr(IOAddress("10.0.0.2"), IOAddress("10.0.0.5")));
// The upper bound cannot be smaller than the lower bound
EXPECT_THROW(plfr(IOAddress("192.0.2.5"), IOAddress("192.0.2.4")),
isc::BadValue);
}
// Checks if IPv6 address ranges can be converted to prefix / prefix_len
TEST(AddrUtilitiesTest, prefixLengthFromRange6) {
// Use a shorter name
const auto& plfr = prefixLengthFromRange;
// Let's start with something simple
EXPECT_EQ(128, plfr(IOAddress("::"), IOAddress("::")));
EXPECT_EQ(112, plfr(IOAddress("fe80::"), IOAddress("fe80::ffff")));
EXPECT_EQ(96, plfr(IOAddress("fe80::"), IOAddress("fe80::ffff:ffff")));
EXPECT_EQ(80, plfr(IOAddress("fe80::"),
IOAddress("fe80::ffff:ffff:ffff")));
EXPECT_EQ(64, plfr(IOAddress("fe80::"),
IOAddress("fe80::ffff:ffff:ffff:ffff")));
EXPECT_EQ(63, plfr(IOAddress("fe80::"),
IOAddress("fe80::1:ffff:ffff:ffff:ffff")));
EXPECT_EQ(62, plfr(IOAddress("fe80::"),
IOAddress("fe80::3:ffff:ffff:ffff:ffff")));
EXPECT_EQ(61, plfr(IOAddress("fe80::"),
IOAddress("fe80::7:ffff:ffff:ffff:ffff")));
EXPECT_EQ(60, plfr(IOAddress("fe80::"),
IOAddress("fe80::f:ffff:ffff:ffff:ffff")));
EXPECT_EQ(59, plfr(IOAddress("fe80::"),
IOAddress("fe80::1f:ffff:ffff:ffff:ffff")));
EXPECT_EQ(58, plfr(IOAddress("fe80::"),
IOAddress("fe80::3f:ffff:ffff:ffff:ffff")));
EXPECT_EQ(57, plfr(IOAddress("fe80::"),
IOAddress("fe80::7f:ffff:ffff:ffff:ffff")));
EXPECT_EQ(56, plfr(IOAddress("fe80::"),
IOAddress("fe80::ff:ffff:ffff:ffff:ffff")));
EXPECT_EQ(55, plfr(IOAddress("fe80::"),
IOAddress("fe80::1ff:ffff:ffff:ffff:ffff")));
EXPECT_EQ(54, plfr(IOAddress("fe80::"),
IOAddress("fe80::3ff:ffff:ffff:ffff:ffff")));
EXPECT_EQ(53, plfr(IOAddress("fe80::"),
IOAddress("fe80::7ff:ffff:ffff:ffff:ffff")));
EXPECT_EQ(52, plfr(IOAddress("fe80::"),
IOAddress("fe80::fff:ffff:ffff:ffff:ffff")));
EXPECT_EQ(51, plfr(IOAddress("fe80::"),
IOAddress("fe80::1fff:ffff:ffff:ffff:ffff")));
EXPECT_EQ(50, plfr(IOAddress("fe80::"),
IOAddress("fe80::3fff:ffff:ffff:ffff:ffff")));
EXPECT_EQ(49, plfr(IOAddress("fe80::"),
IOAddress("fe80::7fff:ffff:ffff:ffff:ffff")));
EXPECT_EQ(48, plfr(IOAddress("fe80::"),
IOAddress("fe80::ffff:ffff:ffff:ffff:ffff")));
EXPECT_EQ(0, plfr(IOAddress("::"),
IOAddress("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")));
// Fail if a network boundary is crossed
EXPECT_EQ(-1, plfr(IOAddress("2001:db8::ffff"),
IOAddress("2001:db8::1:1")));
// Fail if first is not at the begin
EXPECT_EQ(-1, plfr(IOAddress("2001:db8::2"), IOAddress("2001:db8::5")));
EXPECT_EQ(-1, plfr(IOAddress("2001:db8::2:0"),
IOAddress("2001:db8::5:ffff")));
EXPECT_EQ(-1, plfr(IOAddress("2001:db8::2:ff00:0"),
IOAddress("2001:db8::3:00ff:ffff")));
// The upper bound cannot be smaller than the lower bound
EXPECT_THROW(plfr(IOAddress("fe80::5"), IOAddress("fe80::4")),
isc::BadValue);
// Address family must match
EXPECT_THROW(plfr(IOAddress("192.0.2.0"), IOAddress("fe80::1")),
isc::BadValue);
}
// Checks if prefixInRange returns valid number of prefixes in specified range.
TEST(AddrUtilitiesTest, prefixesInRange) {
// How many /64 prefixes are in /64 pool?
EXPECT_EQ(1, prefixesInRange(64, 64));
// How many /63 prefixes are in /64 pool?
EXPECT_EQ(2, prefixesInRange(63, 64));
// How many /64 prefixes are in /48 pool?
EXPECT_EQ(65536, prefixesInRange(48, 64));
// How many /127 prefixes are in /64 pool?
EXPECT_EQ(uint64_t(9223372036854775808ull), prefixesInRange(64, 127));
// How many /128 prefixes are in /64 pool?
EXPECT_EQ(uint128_t(1) << 64, prefixesInRange(64, 128));
// Let's go overboard again. How many IPv6 addresses are there?
EXPECT_EQ(uint128_t(1) << 127, prefixesInRange(1, 128));
// Let's go overboard again. How many IPv6 addresses are there?
EXPECT_EQ(uint128_t(-1), prefixesInRange(0, 128));
}
// Checks the function which finds an IPv4 address from input address and offset.
TEST(AddrUtilitiesTest, offsetIPv4Address) {
EXPECT_EQ("10.1.2.46", offsetAddress(IOAddress("10.1.1.45"), 257).toText());
EXPECT_EQ("10.1.7.9", offsetAddress(IOAddress("10.1.1.45"), 1500).toText());
// Using very large offset. The maximum IPv4 address should be returned.
EXPECT_EQ("255.255.255.255", offsetAddress(IOAddress("255.255.254.254"), 0xFFFFFFFFFFFFFFFA).toText());
}
// Checks the function which finds an IPv6 address from input address and offset.
TEST(AddrUtilitiesTest, offsetIPv6Address) {
EXPECT_EQ("2001:db8:1::4", offsetAddress(IOAddress("2001:db8:1::4"), 0).toText());
EXPECT_EQ("2001:db8:1::10:3", offsetAddress(IOAddress("2001:db8:1::4"), 0xFFFFF).toText());
EXPECT_EQ("2001:db8:2::", offsetAddress(IOAddress("2001:db8:1:FFFF::1"), 0xFFFFFFFFFFFFFFFF).toText());
EXPECT_EQ("3000::1c", offsetAddress(IOAddress("3000::15"), 7).toText());
}
} // end of anonymous namespace
|