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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
// Copyright (C) 2022-2024 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/io_address.h>
#include <dhcpsrv/random_allocator.h>
#include <dhcpsrv/testutils/alloc_engine_utils.h>
#include <boost/make_shared.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <gtest/gtest.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <set><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <vector><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <sstream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace isc::asiolink;
using namespace std;

namespace isc {
namespace dhcp {
namespace test {

using RandomAllocatorTest4 = AllocEngine4Test;

// Test that the allocator returns the correct type.
TEST_F(RandomAllocatorTest4, getType) {
    RandomAllocator alloc(Lease::TYPE_V4, subnet_);
    EXPECT_EQ("random", alloc.getType());
}

// Test allocating IPv4 addresses when a subnet has a single pool.
TEST_F(RandomAllocatorTest4, singlePool) {<--- syntax error
    RandomAllocator alloc(Lease::TYPE_V4, subnet_);

    // Remember returned addresses, so we can verify that unique addresses
    // are returned.
    std::set<IOAddress> addresses;
    for (auto i = 0; i < 1000; ++i) {
        IOAddress candidate = alloc.pickAddress(cc_, clientid_, IOAddress("0.0.0.0"));
        addresses.insert(candidate);
        EXPECT_TRUE(subnet_->inPool(Lease::TYPE_V4, candidate));
        EXPECT_TRUE(subnet_->inPool(Lease::TYPE_V4, candidate, cc_));
    }
    // The pool comprises 10 addresses. All should be returned.
    EXPECT_EQ(10, addresses.size());
}

// Test allocating IPv4 addresses from multiple pools.
TEST_F(RandomAllocatorTest4, manyPools) {
    RandomAllocator alloc(Lease::TYPE_V4, subnet_);

    // Add several more pools.
    for (int i = 1; i < 10; ++i) {
        stringstream min, max;
        min << "192.0.2." << i * 10;
        max << "192.0.2." << i * 10 + 9;
        auto pool = boost::make_shared<Pool4>(IOAddress(min.str()),
                                              IOAddress(max.str()));
        subnet_->addPool(pool);
    }

    // There are ten pools with 10 addresses each.
    int total = 100;

    // Repeat allocation of all addresses several times. Make sure that
    // the same addresses are returned when all pools are exhausted.
    for (auto j = 0; j < 6; ++j) {
        std::set<IOAddress> addresses_set;
        std::vector<IOAddress> addresses_vector;
        std::vector<PoolPtr> pools_vector;

        // Pick random addresses the number of times equal to the
        // subnet capacity to ensure that all addresses are returned.
        for (auto i = 0; i < total; ++i) {
            IOAddress candidate = alloc.pickAddress(cc_, clientid_, IOAddress("0.0.0.0"));
            addresses_set.insert(candidate);
            addresses_vector.push_back(candidate);
            EXPECT_TRUE(subnet_->inPool(Lease::TYPE_V4, candidate));
            EXPECT_TRUE(subnet_->inPool(Lease::TYPE_V4, candidate, cc_));
            pools_vector.push_back(subnet_->getPool(Lease::TYPE_V4, candidate));
        }
        // Make sure that unique addresses have been returned.
        EXPECT_EQ(total, addresses_set.size());

        // Verify that the addresses are returned in the random order.
        // Count how many times we found consecutive addresses. It should
        // be 0 or close to 0.
        int consecutive_addresses = 0;
        for (auto k = 0; k < addresses_vector.size()-1; ++k) {
            if (addresses_vector[k].toUint32() == addresses_vector[k+1].toUint32()-1) {
                ++consecutive_addresses;
            }
        }
        // Ideally, the number of consecutive occurrences should be 0 but we
        // allow some to make sure the test doesn't fall over sporadically.
        EXPECT_LT(consecutive_addresses, addresses_vector.size()/4);

        // Repeat similar check for pools. The pools should be picked in the
        // random order too.
        int consecutive_pools = 0;
        for (auto k = 0; k < pools_vector.size()-1; ++k) {
            // Check if the pools are adjacent (i.e., last address of the
            // previous pool is a neighbor of the first address of the next
            // pool).
            if (pools_vector[k]->getLastAddress().toUint32()+1 ==
                pools_vector[k+1]->getFirstAddress().toUint32()) {
                ++consecutive_pools;
            }
        }
        EXPECT_LT(consecutive_pools, pools_vector.size()/2);
    }
}

// Test that the allocator returns a zero address when there are no
// pools.
TEST_F(RandomAllocatorTest4, noPools) {
   RandomAllocator alloc(Lease::TYPE_V4, subnet_);

   subnet_->delPools(Lease::TYPE_V4);

   IOAddress candidate = alloc.pickAddress(cc_, clientid_, IOAddress("0.0.0.0"));
   EXPECT_TRUE(candidate.isV4Zero());
}

// Test that the allocator respects client classes while it picks
// pools and addresses.
TEST_F(RandomAllocatorTest4, clientClasses) {
   RandomAllocator alloc(Lease::TYPE_V4, subnet_);

   // First pool only allows the client class foo.
   pool_->allowClientClass("foo");

   // Second pool. It only allows client class bar.
   auto pool1 = boost::make_shared<Pool4>(IOAddress("192.0.2.120"),
                                         IOAddress("192.0.2.129"));
   pool1->allowClientClass("bar");
   subnet_->addPool(pool1);

   // Third pool. It only allows client class foo.
   auto pool2 = boost::make_shared<Pool4>(IOAddress("192.0.2.140"),
                                          IOAddress("192.0.2.149"));
   pool2->allowClientClass("foo");
   subnet_->addPool(pool2);

   // Forth pool. It only allows client class bar.
   auto pool3 = boost::make_shared<Pool4>(IOAddress("192.0.2.160"),
                                          IOAddress("192.0.2.169"));
   pool3->allowClientClass("bar");
   subnet_->addPool(pool3);

   // Remember offered addresses.
   std::set<IOAddress> addresses_set;

   // Simulate client's request belonging to the class bar.
   cc_.insert("bar");
   for (auto i = 0; i < 40; ++i) {
       // Allocate random addresses and make sure they belong to the
       // pools associated with the class bar.
       IOAddress candidate = alloc.pickAddress(cc_, clientid_, IOAddress("0.0.0.0"));
       addresses_set.insert(candidate);
       EXPECT_TRUE(pool1->inRange(candidate) || pool3->inRange(candidate));
   }
   EXPECT_EQ(20, addresses_set.size());

   addresses_set.clear();

   // Simulate the case that the client also belongs to the class foo.
   // All pools should now be available.
   cc_.insert("foo");
   for (auto i = 0; i < 60; ++i) {
       IOAddress candidate = alloc.pickAddress(cc_, clientid_, IOAddress("0.0.0.0"));
       addresses_set.insert(candidate);
       EXPECT_TRUE(subnet_->inRange(candidate));
   }
   EXPECT_EQ(40, addresses_set.size());

   // When the client does not belong to any client class the allocator
   // can't offer any address to the client.
   cc_.clear();
   IOAddress candidate = alloc.pickAddress(cc_, clientid_, IOAddress("0.0.0.0"));
   EXPECT_TRUE(candidate.isV4Zero());
}

using RandomAllocatorTest6 = AllocEngine6Test;

// Test that the allocator returns the correct type.
TEST_F(RandomAllocatorTest6, getType) {
    RandomAllocator allocNA(Lease::TYPE_NA, subnet_);
    EXPECT_EQ("random", allocNA.getType());

    RandomAllocator allocPD(Lease::TYPE_PD, subnet_);
    EXPECT_EQ("random", allocPD.getType());
}

// Test allocating IPv6 addresses when a subnet has a single pool.
TEST_F(RandomAllocatorTest6, singlePool) {
    RandomAllocator alloc(Lease::TYPE_NA, subnet_);

    // Remember returned addresses, so we can verify that unique addresses
    // are returned.
    std::set<IOAddress> addresses;
    for (auto i = 0; i < 1000; ++i) {
        IOAddress candidate = alloc.pickAddress(cc_, duid_, IOAddress("::"));
        addresses.insert(candidate);
        EXPECT_TRUE(subnet_->inPool(Lease::TYPE_NA, candidate));
        EXPECT_TRUE(subnet_->inPool(Lease::TYPE_NA, candidate, cc_));
    }
    // The pool comprises 17 addresses. All should be returned.
    EXPECT_EQ(17, addresses.size());
}

// Test allocating IPv6 addresses from multiple pools.
TEST_F(RandomAllocatorTest6, manyPools) {
    RandomAllocator alloc(Lease::TYPE_NA, subnet_);

    // Add several more pools.
    for (int i = 2; i < 10; ++i) {
        stringstream min, max;
        min << "2001:db8:1::" << hex << i * 16 + 1;
        max << "2001:db8:1::" << hex << i * 16 + 16;
        auto pool = boost::make_shared<Pool6>(Lease::TYPE_NA,
                                              IOAddress(min.str()),
                                              IOAddress(max.str()));
        subnet_->addPool(pool);
    }

    // First pool (::10 - ::20) has 17 addresses.
    // There are 8 extra pools with 16 addresses in each.
    int total = 17 + 8 * 16;

    // Repeat allocation of all addresses several times. Make sure that
    // the same addresses are returned when all pools are exhausted.
    for (auto j = 0; j < 6; ++j) {
        std::set<IOAddress> addresses_set;
        std::vector<IOAddress> addresses_vector;
        std::vector<PoolPtr> pools_vector;

        // Pick random addresses the number of times equal to the
        // subnet capacity to ensure that all addresses are returned.
        for (auto i = 0; i < total; ++i) {
            IOAddress candidate = alloc.pickAddress(cc_, duid_, IOAddress("::"));
            addresses_set.insert(candidate);
            addresses_vector.push_back(candidate);
            EXPECT_TRUE(subnet_->inPool(Lease::TYPE_NA, candidate));
            EXPECT_TRUE(subnet_->inPool(Lease::TYPE_NA, candidate, cc_));
            pools_vector.push_back(subnet_->getPool(Lease::TYPE_NA, candidate));
        }
        // Make sure that unique addresses have been returned.
        EXPECT_EQ(total, addresses_set.size());

        // Verify that the addresses are returned in the random order.
        // Count how many times we found consecutive addresses. It should
        // be 0 or close to 0.
        int consecutive_addresses = 0;
        for (auto k = 0; k < addresses_vector.size()-1; ++k) {
            if (IOAddress::increase(addresses_vector[k]) == addresses_vector[k+1]) {
                ++consecutive_addresses;
            }
        }
        // Ideally, the number of consecutive occurrences should be 0 but we
        // allow some to make sure the test doesn't fall over sporadically.
        EXPECT_LT(consecutive_addresses, addresses_vector.size()/4);

        // Repeat similar check for pools. The pools should be picked in the
        // random order too.
        int consecutive_pools = 0;
        for (auto k = 0; k < pools_vector.size()-1; ++k) {
            if (IOAddress::increase(pools_vector[k]->getLastAddress()) ==
                pools_vector[k]->getFirstAddress()) {
                ++consecutive_pools;
            }
        }
        EXPECT_LT(consecutive_pools, pools_vector.size()/2);
    }
}

// Test that the allocator respects client classes while it picks
// pools and addresses.
TEST_F(RandomAllocatorTest6, clientClasses) {
   RandomAllocator alloc(Lease::TYPE_NA, subnet_);

   // First pool only allows the client class foo.
   pool_->allowClientClass("foo");

   // Second pool. It only allows client class bar.
   auto pool1 = boost::make_shared<Pool6>(Lease::TYPE_NA,
                                          IOAddress("2001:db8:1::120"),
                                          IOAddress("2001:db8:1::129"));
   pool1->allowClientClass("bar");
   subnet_->addPool(pool1);

   // Third pool. It only allows client class foo.
   auto pool2 = boost::make_shared<Pool6>(Lease::TYPE_NA,
                                          IOAddress("2001:db8:1::140"),
                                          IOAddress("2001:db8:1::149"));
   pool2->allowClientClass("foo");
   subnet_->addPool(pool2);

   // Forth pool. It only allows client class bar.
   auto pool3 = boost::make_shared<Pool6>(Lease::TYPE_NA,
                                          IOAddress("2001:db8:1::160"),
                                          IOAddress("2001:db8:1::169"));
   pool3->allowClientClass("bar");
   subnet_->addPool(pool3);

   // Remember offered addresses.
   std::set<IOAddress> addresses_set;

   // Simulate client's request belonging to the class bar.
   cc_.insert("bar");
   for (auto i = 0; i < 60; ++i) {
       // Allocate random addresses and make sure they belong to the
       // pools associated with the class bar.
       IOAddress candidate = alloc.pickAddress(cc_, duid_, IOAddress("::"));
       addresses_set.insert(candidate);
       EXPECT_TRUE(pool1->inRange(candidate) || pool3->inRange(candidate));
   }
   EXPECT_EQ(20, addresses_set.size());

   addresses_set.clear();

   // Simulate the case that the client also belongs to the class foo.
   // All pools should now be available.
   cc_.insert("foo");
   for (auto i = 0; i < 100; ++i) {
       IOAddress candidate = alloc.pickAddress(cc_, duid_, IOAddress("::"));
       addresses_set.insert(candidate);
       EXPECT_TRUE(subnet_->inRange(candidate));
   }
   EXPECT_EQ(47, addresses_set.size());

   // When the client does not belong to any client class the allocator
   // can't offer any address to the client.
   cc_.clear();
   IOAddress candidate = alloc.pickAddress(cc_, duid_, IOAddress("0.0.0.0"));
   EXPECT_TRUE(candidate.isV6Zero());
}

// Test allocating delegated prefixes when a subnet has a single pool.
TEST_F(RandomAllocatorTest6, singlePdPool) {
    RandomAllocator alloc(Lease::TYPE_PD, subnet_);

    Pool6Ptr pool;

    // Remember returned prefixes, so we can verify that unique addresses
    // are returned.
    std::set<IOAddress> prefixes;
    for (auto i = 0; i < 66000; ++i) {
        IOAddress candidate = alloc.pickPrefix(cc_, pool, duid_, Allocator::PREFIX_LEN_HIGHER, IOAddress("::"), 0);
        prefixes.insert(candidate);
        EXPECT_TRUE(subnet_->inPool(Lease::TYPE_PD, candidate));
        EXPECT_TRUE(subnet_->inPool(Lease::TYPE_PD, candidate, cc_));
    }
    // The pool comprises 65536 prefixes. All should be returned.
    EXPECT_EQ(65536, prefixes.size());
}

// Test allocating delegated prefixes from multiple pools.
TEST_F(RandomAllocatorTest6, manyPdPools) {
    RandomAllocator alloc(Lease::TYPE_PD, subnet_);

    for (auto i = 0; i < 10; ++i) {
        ostringstream s;
        s << "300" << hex << i + 1 << "::";
        auto pool = boost::make_shared<Pool6>(Lease::TYPE_PD,
                                              IOAddress(s.str()),
                                              120,
                                              128);
        subnet_->addPool(pool);
    }

    size_t total = 65536 + 10 * 256;

    Pool6Ptr pool;

    for (auto j = 0; j < 2; ++j) {
        std::set<IOAddress> prefixes;
        for (auto i = 0; i < total; ++i) {
            IOAddress candidate = alloc.pickPrefix(cc_, pool, duid_, Allocator::PREFIX_LEN_HIGHER, IOAddress("::"), 0);
            prefixes.insert(candidate);
            EXPECT_TRUE(subnet_->inPool(Lease::TYPE_PD, candidate));
            EXPECT_TRUE(subnet_->inPool(Lease::TYPE_PD, candidate, cc_));
        }
        // Make sure that unique prefixes have been returned.
        EXPECT_EQ(total, prefixes.size());
    }
}

// Test allocating delegated prefixes from multiple pools.
TEST_F(RandomAllocatorTest6, manyPdPoolsPreferLower) {
    RandomAllocator alloc(Lease::TYPE_PD, subnet_);

    for (auto i = 0; i < 10; ++i) {
        ostringstream s;
        s << "300" << hex << i + 1 << "::";
        auto pool = boost::make_shared<Pool6>(Lease::TYPE_PD,
                                              IOAddress(s.str()),
                                              120,
                                              128);
        subnet_->addPool(pool);
    }

    size_t total = 65536;

    Pool6Ptr pool;

    for (auto j = 0; j < 2; ++j) {
        std::set<IOAddress> prefixes;
        for (auto i = 0; i < total; ++i) {
            IOAddress candidate = alloc.pickPrefix(cc_, pool, duid_, Allocator::PREFIX_LEN_LOWER, IOAddress("::"), 120);
            prefixes.insert(candidate);
            EXPECT_TRUE(subnet_->inPool(Lease::TYPE_PD, candidate));
            EXPECT_TRUE(subnet_->inPool(Lease::TYPE_PD, candidate, cc_));
        }
        // Make sure that unique prefixes have been returned.
        EXPECT_EQ(total, prefixes.size());
    }
}

// Test allocating delegated prefixes from multiple pools.
TEST_F(RandomAllocatorTest6, manyPdPoolsPreferEqual) {
    RandomAllocator alloc(Lease::TYPE_PD, subnet_);

    for (auto i = 0; i < 10; ++i) {
        ostringstream s;
        s << "300" << hex << i + 1 << "::";
        auto pool = boost::make_shared<Pool6>(Lease::TYPE_PD,
                                              IOAddress(s.str()),
                                              120,
                                              128);
        subnet_->addPool(pool);
    }

    size_t total = 10 * 256;

    Pool6Ptr pool;

    for (auto j = 0; j < 2; ++j) {
        std::set<IOAddress> prefixes;
        for (auto i = 0; i < total; ++i) {
            IOAddress candidate = alloc.pickPrefix(cc_, pool, duid_, Allocator::PREFIX_LEN_EQUAL, IOAddress("::"), 128);
            prefixes.insert(candidate);
            EXPECT_TRUE(subnet_->inPool(Lease::TYPE_PD, candidate));
            EXPECT_TRUE(subnet_->inPool(Lease::TYPE_PD, candidate, cc_));
        }
        // Make sure that unique prefixes have been returned.
        EXPECT_EQ(total, prefixes.size());
    }
}

// Test allocating delegated prefixes from multiple pools.
TEST_F(RandomAllocatorTest6, manyPdPoolsPreferHigher) {
    RandomAllocator alloc(Lease::TYPE_PD, subnet_);

    for (auto i = 0; i < 10; ++i) {
        ostringstream s;
        s << "300" << hex << i + 1 << "::";
        auto pool = boost::make_shared<Pool6>(Lease::TYPE_PD,
                                              IOAddress(s.str()),
                                              120,
                                              128);
        subnet_->addPool(pool);
    }

    size_t total = 10 * 256;

    Pool6Ptr pool;

    for (auto j = 0; j < 2; ++j) {
        std::set<IOAddress> prefixes;
        for (auto i = 0; i < total; ++i) {
            IOAddress candidate = alloc.pickPrefix(cc_, pool, duid_, Allocator::PREFIX_LEN_HIGHER, IOAddress("::"), 64);
            prefixes.insert(candidate);
            EXPECT_TRUE(subnet_->inPool(Lease::TYPE_PD, candidate));
            EXPECT_TRUE(subnet_->inPool(Lease::TYPE_PD, candidate, cc_));
        }
        // Make sure that unique prefixes have been returned.
        EXPECT_EQ(total, prefixes.size());
    }
}

} // end of isc::dhcp::test namespace
} // end of isc::dhcp namespace
} // end of isc namespace