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
|
// Copyright (C) 2020-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 <dhcpsrv/ip_range_permutation.h>
#include <gtest/gtest.h>
#include <set>
#include <vector>
using namespace isc;
using namespace isc::asiolink;
using namespace isc::dhcp;
namespace {
// This test verifies that the object can be successfully constructed for
// both IPv4 and IPv6 address range.
TEST(IPRangePermutationTest, constructor) {
ASSERT_NO_THROW({
AddressRange range(IOAddress("192.0.2.10"), IOAddress("192.0.2.100"));
IPRangePermutation perm(range);
});
ASSERT_NO_THROW({
AddressRange range(IOAddress("3000::"), IOAddress("3000::10"));
IPRangePermutation perm(range);
});
}
// This test verifies that a permutation of IPv4 address range can
// be generated and each time a different permutation is generated.
TEST(IPRangePermutationTest, ipv4) {
// Create address range with 91 addresses.
AddressRange range(IOAddress("192.0.2.10"), IOAddress("192.0.2.100"));
std::vector<std::vector<IOAddress>> iterations;
for (auto i = 0; i < 2; ++i) {
IPRangePermutation perm(range);
// This set will record unique IP addresses generated.
std::set<IOAddress> addrs;
// This vector will record the addresses assignment order.
std::vector<IOAddress> ordered_addrs;
bool done = false;
// Call the next() function 95 times. The first 91 calls should return non-zero
// IP addresses.
for (auto i = 0; i < 95; ++i) {
auto next = perm.next(done);
if (!next.isV4Zero()) {
// Make sure the returned address is within the range.
EXPECT_LE(range.start_, next);
EXPECT_LE(next, range.end_);
}
// If we went over all addresses in the range, the flags indicating that
// the permutation is exhausted should be set to true.
if (i >= 90) {
EXPECT_TRUE(done);
EXPECT_TRUE(perm.exhausted());
} else {
// We're not done yet, so these flag should still be false.
EXPECT_FALSE(done);
EXPECT_FALSE(perm.exhausted());
}
// Insert the address returned to the set and vector.
addrs.insert(next);
ordered_addrs.push_back(next);
}
// We should have recorded 92 unique addresses, including the zero address.
EXPECT_EQ(92, addrs.size());
EXPECT_TRUE(addrs.begin()->isV4Zero());
iterations.push_back(ordered_addrs);
}
// We want to make sure that each new permutation instance produces a different
// sequence of addresses. It checks whether or not the random device has been
// initialized properly. If the random device uses the same seed for each
// new permutation, the output sequence is always the same. The test below
// checks that the sequences are different by comparing the respective addresses
// for two different permutations. It is ok if some of them are equal because it
// is statistically probable. The threshold of 20% should guard against some
// of them being equal without a risk of sporadic test failures.
int overlaps = 0;
for (auto i = 0; i < iterations[0].size(); ++i) {
if (iterations[0][i] == iterations[1][i]) {
++overlaps;
}
}
EXPECT_LE(overlaps, iterations[0].size()/5)
<< "The number of overlapping random address between the test two iterations"
<< " is greater than 20% of all allocated addresses in each iteration."
<< " It means that the permutation mechanism does not sufficiently randomize"
<< " addresses. Perhaps the randomization device is not properly initialized?";
}
// This test verifies that a permutation of IPv6 address range can
// be generated and each time a different permutation is generated.
TEST(IPRangePermutationTest, ipv6) {
AddressRange range(IOAddress("2001:db8:1::1:fea0"),
IOAddress("2001:db8:1::2:abcd"));
std::vector<std::vector<IOAddress>> iterations;
for (auto i = 0; i < 2; ++i) {
IPRangePermutation perm(range);
std::set<IOAddress> addrs;
std::vector<IOAddress> ordered_addrs;
bool done = false;
for (auto i = 0; i < 44335; ++i) {
auto next = perm.next(done);
if (!next.isV6Zero()) {
// Make sure that the address is within the range.
EXPECT_LE(range.start_, next);
EXPECT_LE(next, range.end_);
}
// If we went over all addresses in the range, the flags indicating that
// the permutation is exhausted should be set to true.
if (i >= 44333) {
EXPECT_TRUE(done);
EXPECT_TRUE(perm.exhausted());
} else {
// We're not done yet, so these flag should still be false.
EXPECT_FALSE(done);
EXPECT_FALSE(perm.exhausted());
}
// Insert the address returned to the set and vector.
addrs.insert(next);
ordered_addrs.push_back(next);
}
// We should have recorded 44335 unique addresses, including the zero address.
EXPECT_EQ(44335, addrs.size());
EXPECT_TRUE(addrs.begin()->isV6Zero());
iterations.push_back(ordered_addrs);
}
// We want to make sure that each new permutation instance produces a different
// sequence of addresses. It checks whether or not the random device has been
// initialized properly. If the random device uses the same seed for each
// new permutation, the output sequence is always the same. The test below
// checks that the sequences are different by comparing the respective addresses
// for two different permutations. It is ok if some of them are equal because it
// is statistically probable. The threshold of 20% should guard against some
// of them being equal without a risk of sporadic test failures.
int overlaps = 0;
for (auto i = 0; i < iterations[0].size(); ++i) {
if (iterations[0][i] == iterations[1][i]) {
++overlaps;
}
}
EXPECT_LE(overlaps, iterations[0].size()/5)
<< "The number of overlapping random address between the test two iterations"
<< " is greater than 20% of all allocated addresses in each iteration."
<< " It means that the permutation mechanism does not sufficiently randomize"
<< " addresses. Perhaps the randomization device is not properly initialized?";
}
// This test verifies that a permutation of delegated prefixes can be
// generated and each time a different permutation is generated.
TEST(IPRangePermutationTest, pd) {
PrefixRange range(IOAddress("3000::"), 112, 120);
std::vector<std::vector<IOAddress>> iterations;
for (auto i = 0; i < 2; ++i) {
IPRangePermutation perm(range);
std::set<IOAddress> addrs;
std::vector<IOAddress> ordered_addrs;
bool done = false;
for (auto i = 0; i < 257; ++i) {
auto next = perm.next(done);
if (!next.isV6Zero()) {
// Make sure the prefix is within the range.
EXPECT_LE(range.start_, next);
EXPECT_LE(next, range.end_);
}
// If we went over all delegated prefixes in the range, the flags indicating
// that the permutation is exhausted should be set to true.
if (i >= 255) {
EXPECT_TRUE(done);
EXPECT_TRUE(perm.exhausted());
} else {
// We're not done yet, so these flag should still be false.
EXPECT_FALSE(done);
EXPECT_FALSE(perm.exhausted());
}
// Insert the prefix returned to the set and vector.
addrs.insert(next);
ordered_addrs.push_back(next);
}
// We should have recorded 257 unique addresses, including the zero address.
EXPECT_EQ(257, addrs.size());
EXPECT_TRUE(addrs.begin()->isV6Zero());
iterations.push_back(ordered_addrs);
}
ASSERT_EQ(2, iterations.size());
// We want to make sure that each new permutation instance produces a different
// sequence of prefixes. It checks whether or not the random device has been
// initialized properly. If the random device uses the same seed for each
// new permutation, the output sequence is always the same. The test below
// checks that the sequences are different by comparing the respective prefixes
// for two different permutations. It is ok if some of them are equal because it
// is statistically probable. The threshold of 20% should guard against some
// of them being equal without a risk of sporadic test failures.
int overlaps = 0;
for (auto i = 0; i < iterations[0].size(); ++i) {
if (iterations[0][i] == iterations[1][i]) {
++overlaps;
}
}
EXPECT_LE(overlaps, iterations[0].size()/5)
<< "The number of overlapping random prefixes between the test two iterations"
<< " is greater than 20% of all allocated addresses in each iteration."
<< " It means that the permutation mechanism does not sufficiently randomize"
<< " prefixes. Perhaps the randomization device is not properly initialized?";
}
// This test verifies that a permutation of delegated prefixes is
// generated from the prefix range specified using first and last
// address.
TEST(IPRangePermutationTest, pdStartEnd) {
PrefixRange range(IOAddress("3000::"), IOAddress("3000::ffff"), 120);
IPRangePermutation perm(range);
std::set<IOAddress> addrs;
bool done = false;
for (auto i = 0; i < 257; ++i) {
auto next = perm.next(done);
if (!next.isV6Zero()) {
// Make sure the prefix is within the range.
EXPECT_LE(range.start_, next);
EXPECT_LE(next, range.end_);
}
// If we went over all delegated prefixes in the range, the flags indicating
// that the permutation is exhausted should be set to true.
if (i >= 255) {
EXPECT_TRUE(done);
EXPECT_TRUE(perm.exhausted());
} else {
// We're not done yet, so these flag should still be false.
EXPECT_FALSE(done);
EXPECT_FALSE(perm.exhausted());
}
// Insert the prefix returned to the set.
addrs.insert(next);
}
// We should have recorded 257 unique addresses, including the zero address.
EXPECT_EQ(257, addrs.size());
EXPECT_TRUE(addrs.begin()->isV6Zero());
}
// This test verifies that it is possible to reset the permutation state.
TEST(IPRangePermutationTest, reset) {
// Create address range with 11 addresses.
AddressRange range(IOAddress("192.0.2.10"), IOAddress("192.0.2.20"));
IPRangePermutation perm(range);
// This set will record unique IP addresses generated.
std::set<IOAddress> addrs;
bool done = false;
// Call the next() function several times to consume several addresses.
for (auto i = 0; i < 5; ++i) {
auto next = perm.next(done);
EXPECT_FALSE(next.isV4Zero());
addrs.insert(next);
}
EXPECT_EQ(5, addrs.size());
// Reset the permutation. We should be able to get all addresses again.
perm.reset();
for (auto i = 0; i < 11; ++i) {
auto next = perm.next(done);
EXPECT_FALSE(next.isV4Zero());
addrs.insert(next);
}
EXPECT_EQ(11, addrs.size());
}
} // end of anonymous namespace
|