summaryrefslogtreecommitdiffstats
path: root/src/lib/dhcpsrv/tests/free_lease_queue_unittest.cc
blob: 21a4b50f0f5c5df41f0476f8a09fb34d989dcc44 (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
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
// Copyright (C) 2020 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/free_lease_queue.h>

#include <gtest/gtest.h>

using namespace isc;
using namespace isc::asiolink;
using namespace isc::dhcp;

namespace {

// This test verifies that it is not allowed to add a range that overlaps with
// any existing range.
TEST(FreeLeaseQueueTest, addRangeOverlapping) {
    FreeLeaseQueue lq;
    // Add the initial range. This should succeed.
    ASSERT_NO_THROW(lq.addRange(IOAddress("192.0.2.10"), IOAddress("192.0.3.100")));

    // Let's assume the following naming convention:
    // - r1s - start of the first range added.
    // - r1e - end of the first range added.
    // - r2s - start of the second range (will be added later in this test).
    // - r2e - end of the second range (will be added later in this test).
    // - ns - start of the new range colliding with existing ones.
    // - ne - end of the new range colliding with existing ones.
    // - #### overlap
    // - ns/ne/r1s - overlap on the edges of the ranges (single address shared).

    // r1s    ns####r2s    ne
    EXPECT_THROW(lq.addRange(IOAddress("192.0.2.50"), IOAddress("192.0.3.199")),
                 BadValue);

    // ns     r1s###ne     r1s
    EXPECT_THROW(lq.addRange(IOAddress("192.0.2.5"), IOAddress("192.0.2.100")),
                 BadValue);

    // r1s    ns####ne     r2s
    EXPECT_THROW(lq.addRange(IOAddress("192.0.2.50"), IOAddress("192.0.3.50")),
                 BadValue);
    EXPECT_THROW(lq.addRange(IOAddress("192.0.2.5"), IOAddress("192.0.3.200")),
                 BadValue);

    // ns    ne/r1s    r1e
    EXPECT_THROW(lq.addRange(IOAddress("192.0.2.5"), IOAddress("192.0.2.10")),
                 BadValue);

    // r1s   r1e/ns    ne
    EXPECT_THROW(lq.addRange(IOAddress("192.0.3.100"), IOAddress("192.0.3.105")),
                 BadValue);

    // ns/ne/r1s    r1e
    EXPECT_THROW(lq.addRange(IOAddress("192.0.2.10"), IOAddress("192.0.2.10")),
                 BadValue);

    // r1s    r1e/ns/ne
    EXPECT_THROW(lq.addRange(IOAddress("192.0.3.100"), IOAddress("192.0.3.100")),
                 BadValue);

    // Add another range, marked as r2s, r2e.
    ASSERT_NO_THROW(lq.addRange(IOAddress("192.0.4.10"), IOAddress("192.0.5.100")));

    // r1s    ns####r1e    ne    r2s    r2e
    EXPECT_THROW(lq.addRange(IOAddress("192.0.2.50"), IOAddress("192.0.3.199")),
                 BadValue);

    // r1s    r1e    r2s    ns####r2e    ne
    EXPECT_THROW(lq.addRange(IOAddress("192.0.4.50"), IOAddress("192.0.5.199")),
                 BadValue);

    // r1s    ns####r2s####r2e    ne
    EXPECT_THROW(lq.addRange(IOAddress("192.0.2.50"), IOAddress("192.0.5.199")),
                 BadValue);

    // ns    r1s####ne    r1e    r2s    r2e
    EXPECT_THROW(lq.addRange(IOAddress("192.0.2.5"), IOAddress("192.0.2.100")),
                 BadValue);

    // r1s    r1e    ns    r2s####ne    r2e
    EXPECT_THROW(lq.addRange(IOAddress("192.0.4.5"), IOAddress("192.0.4.100")),
                 BadValue);

    // ns    r1s####r1e    r2s####ne    r2e
    EXPECT_THROW(lq.addRange(IOAddress("192.0.2.5"), IOAddress("192.0.4.100")),
                 BadValue);

    // r1s    ns####ne    r1e    r2s    r2e
    EXPECT_THROW(lq.addRange(IOAddress("192.0.2.50"), IOAddress("192.0.3.50")),
                 BadValue);

    // r1s    r1e    r2s    ns####ne    r2e
    EXPECT_THROW(lq.addRange(IOAddress("192.0.4.50"), IOAddress("192.0.5.50")),
                 BadValue);

    // r1s    ns####r1e    r2s####ne    r2e
    EXPECT_THROW(lq.addRange(IOAddress("192.0.2.50"), IOAddress("192.0.5.50")),
                 BadValue);

    // ns    r1s    r1e   ne    r2s    r2e
    EXPECT_THROW(lq.addRange(IOAddress("192.0.2.5"), IOAddress("192.0.3.200")),
                 BadValue);

    // r1s    r1e    ns    r2s####r2e   ne
    EXPECT_THROW(lq.addRange(IOAddress("192.0.4.5"), IOAddress("192.0.5.200")),
                 BadValue);

    // ns     r1s####r1e    r2s####r2e    ne
    EXPECT_THROW(lq.addRange(IOAddress("192.0.2.5"), IOAddress("192.0.5.200")),
                 BadValue);

    // ns     ne/r1s     r1e    r2s    r2e
    EXPECT_THROW(lq.addRange(IOAddress("192.0.2.5"), IOAddress("192.0.2.10")),
                 BadValue);
    // r1s    r1e/ns     ne     r2s    r2e
    EXPECT_THROW(lq.addRange(IOAddress("192.0.3.100"), IOAddress("192.0.3.105")),
                 BadValue);
    // r1s    r1e    ns    ne/r2s    r2e
    EXPECT_THROW(lq.addRange(IOAddress("192.0.4.5"), IOAddress("192.0.4.10")),
                 BadValue);
    // r1s    r1e    r2s    r2e/ns    ne
    EXPECT_THROW(lq.addRange(IOAddress("192.0.5.100"), IOAddress("192.0.5.105")),
                 BadValue);
}

// This test verifies that it is not allowed to add a prefix range that overlaps with
// any existing range.
TEST(FreeLeaseQueueTest, addPrefixRangeOverlapping) {
    FreeLeaseQueue lq;
    // Add the initial range. This should succeed.
    ASSERT_NO_THROW(lq.addRange(IOAddress("2001:db8:1::"), 64, 96));

    EXPECT_THROW(lq.addRange(IOAddress("2001:db8:1:0:0:5:ffff:0"), 96, 120),
                 BadValue);
    EXPECT_THROW(lq.addRange(IOAddress("2001:db8:1::0"), 80, 88),
                 BadValue);
}

// This test verifies that a range can be removed from the container.
TEST(FreeLeaseQueueTest, removeRange) {
    FreeLeaseQueue lq;

    // Add two ranges.
    AddressRange range1(IOAddress("192.0.2.99"), IOAddress("192.0.2.100"));
    AddressRange range2(IOAddress("192.0.3.99"), IOAddress("192.0.3.100"));
    ASSERT_NO_THROW(lq.addRange(range1));
    ASSERT_NO_THROW(lq.addRange(range2));

    bool removed = false;

    // Expect true to be returned when the range is removed.
    ASSERT_NO_THROW(removed = lq.removeRange(range1));
    EXPECT_TRUE(removed);

    // An attempt to append an address to the removed range should not succeed.
    EXPECT_FALSE(lq.append(IOAddress("192.0.2.99")));

    // Removing it the second time should return false to indicate that the range
    // was no longer there.
    ASSERT_NO_THROW(removed = lq.removeRange(range1));
    EXPECT_FALSE(removed);

    // Same for the second range.
    ASSERT_NO_THROW(removed = lq.removeRange(range2));
    EXPECT_TRUE(removed);

    ASSERT_NO_THROW(removed = lq.removeRange(range2));
    EXPECT_FALSE(removed);
}

// This test verifies that a prefix range can be removed from the container.
TEST(FreeLeaseQueueTest, removePrefixRange) {
    FreeLeaseQueue lq;

    // Add two ranges.
    PrefixRange range1(IOAddress("3000::"), 64, 96);
    PrefixRange range2(IOAddress("3001::"), 64, 96);
    ASSERT_NO_THROW(lq.addRange(range1));
    ASSERT_NO_THROW(lq.addRange(range2));

    bool removed = false;

    // Expect true to be returned when the range is removed.
    ASSERT_NO_THROW(removed = lq.removeRange(range1));
    EXPECT_TRUE(removed);

    // An attempt to append a prefix to the removed range should not succeed.
    EXPECT_FALSE(lq.append(IOAddress("3000::5:0:0"), 96));

    // Removing it the second time should return false to indicate that the range
    // was no longer there.
    ASSERT_NO_THROW(removed = lq.removeRange(range1));
    EXPECT_FALSE(removed);

    // Same for the second range.
    ASSERT_NO_THROW(removed = lq.removeRange(range2));
    EXPECT_TRUE(removed);

    ASSERT_NO_THROW(removed = lq.removeRange(range2));
    EXPECT_FALSE(removed);
}

// This test verifies that an attempt to use an address from outside the
// given range throws and that an attempt to use non-existing in-range
// address returns false.
TEST(FreeLeaseQueueTest, useInvalidAddress) {
    AddressRange range(IOAddress("192.0.2.99"), IOAddress("192.0.2.100"));

    FreeLeaseQueue lq;
    ASSERT_NO_THROW(lq.addRange(range));

    // Using out of range address.
    EXPECT_THROW(lq.use(range, IOAddress("192.0.2.1")), BadValue);

    // Using in-range address but not existing in the container.
    bool used = true;
    ASSERT_NO_THROW(used = lq.use(range, IOAddress("192.0.2.99")));
    EXPECT_FALSE(used);
}

// This test verifies that an attempt to use a prefix from outside the
// given range throws and that an attempt to use non-existing in-range
// address returns false.
TEST(FreeLeaseQueueTest, useInvalidPrefix) {
    PrefixRange range(IOAddress("2001:db8:1::"), 64, 96);

    FreeLeaseQueue lq;
    ASSERT_NO_THROW(lq.addRange(range));

    // Using out of range prefix.
    EXPECT_THROW(lq.use(range, IOAddress("2001:db8:2::")), BadValue);

    // Using in-range prefix but not existing in the container.
    bool used = false;
    ASSERT_NO_THROW(used = lq.use(range, IOAddress("2001:db8:1::5:0:0")));
    EXPECT_FALSE(used);
}

// Check that duplicates are eliminated when appending free address to
// the range queue.
TEST(FreeLeaseQueueTest, appendDuplicates) {
    FreeLeaseQueue lq;

    AddressRange range(IOAddress("192.0.2.1"), IOAddress("192.0.2.255"));
    ASSERT_NO_THROW(lq.addRange(range));

    ASSERT_NO_THROW(lq.append(range, IOAddress("192.0.2.10")));
    // Append the duplicate of the first address.
    ASSERT_NO_THROW(lq.append(range, IOAddress("192.0.2.10")));
    ASSERT_NO_THROW(lq.append(range, IOAddress("192.0.2.5")));

    IOAddress next(0);
    // The first address should be returned first.
    ASSERT_NO_THROW(next = lq.next(range));
    EXPECT_EQ("192.0.2.10", next.toText());

    // The duplicate should not be returned. Instead, the second address should
    // be returned.
    ASSERT_NO_THROW(next = lq.next(range));
    EXPECT_EQ("192.0.2.5", next.toText());
}

// This test verifies that it is possible to pick next address from the given
// range.
TEST(FreeLeaseQueueTest, next) {
    FreeLeaseQueue lq;

    // Let's create two distinct address ranges.
    AddressRange range1(IOAddress("192.0.2.1"), IOAddress("192.0.2.255"));
    AddressRange range2(IOAddress("192.0.3.1"), IOAddress("192.0.3.255"));
    ASSERT_NO_THROW(lq.addRange(range1));
    ASSERT_NO_THROW(lq.addRange(range2));

    // Append some IP addresses to those address ranges.
    ASSERT_NO_THROW(lq.append(AddressRange(range1), IOAddress("192.0.2.10")));
    ASSERT_NO_THROW(lq.append(AddressRange(range1), IOAddress("192.0.2.5")));
    ASSERT_NO_THROW(lq.append(AddressRange(range2), IOAddress("192.0.3.23")));
    ASSERT_NO_THROW(lq.append(AddressRange(range2), IOAddress("192.0.3.46")));

    // Get the first address from the first range.
    IOAddress next(0);
    ASSERT_NO_THROW(next = lq.next(range1));
    EXPECT_EQ("192.0.2.10", next.toText());

    // Get the next one.
    ASSERT_NO_THROW(next = lq.next(range1));
    EXPECT_EQ("192.0.2.5", next.toText());

    // After iterating over all addresses we should get the first one again.
    ASSERT_NO_THROW(next = lq.next(range1));
    EXPECT_EQ("192.0.2.10", next.toText());

    // Repeat that test for the second address range.
    ASSERT_NO_THROW(next = lq.next(range2));
    EXPECT_EQ("192.0.3.23", next.toText());

    ASSERT_NO_THROW(next = lq.next(range2));
    EXPECT_EQ("192.0.3.46", next.toText());

    ASSERT_NO_THROW(next = lq.next(range2));
    EXPECT_EQ("192.0.3.23", next.toText());

    // Use (remove) the address from the first range.
    bool used = false;
    ASSERT_NO_THROW(used = lq.use(range1, IOAddress("192.0.2.5")));
    EXPECT_TRUE(used);

    // We should now be getting the sole address from that range.
    ASSERT_NO_THROW(next = lq.next(range1));
    EXPECT_EQ("192.0.2.10", next.toText());

    ASSERT_NO_THROW(next = lq.next(range1));
    EXPECT_EQ("192.0.2.10", next.toText());

    // Check the same for the second range.
    ASSERT_NO_THROW(lq.use(range2, IOAddress("192.0.3.46")));

    ASSERT_NO_THROW(next = lq.next(range2));
    EXPECT_EQ("192.0.3.23", next.toText());
}

// This test verifies that it is possible to pick next prefix from the given
// range.
TEST(FreeLeaseQueueTest, nextPrefix) {
    FreeLeaseQueue lq;

    PrefixRange range1(IOAddress("2001:db8:1::"), 64, 96);
    ASSERT_NO_THROW(lq.addRange(range1));

    ASSERT_NO_THROW(lq.append(range1, IOAddress("2001:db8:1::4:0")));
    ASSERT_NO_THROW(lq.append(range1, IOAddress("2001:db8:1::7:0")));
    ASSERT_NO_THROW(lq.append(range1, IOAddress("2001:db8:1::3:0")));
    ASSERT_NO_THROW(lq.append(range1, IOAddress("2001:db8:1::")));

    IOAddress next = IOAddress::IPV6_ZERO_ADDRESS();
    ASSERT_NO_THROW(next = lq.next(range1));
    EXPECT_EQ("2001:db8:1::4:0", next.toText());

    ASSERT_NO_THROW(next = lq.next(range1));
    EXPECT_EQ("2001:db8:1::7:0", next.toText());

    ASSERT_NO_THROW(next = lq.next(range1));
    EXPECT_EQ("2001:db8:1::3:0", next.toText());

    ASSERT_NO_THROW(next = lq.next(range1));
    EXPECT_EQ("2001:db8:1::", next.toText());

    ASSERT_NO_THROW(next = lq.next(range1));
    EXPECT_EQ("2001:db8:1::4:0", next.toText());

    // Use (remove) the prefix from the range.
    bool used = false;
    ASSERT_NO_THROW(used = lq.use(range1, IOAddress("2001:db8:1::7:0")));
    EXPECT_TRUE(used);

    // After we have removed the second prefix, the third prefix should be
    // returned.
    ASSERT_NO_THROW(next = lq.next(range1));
    EXPECT_EQ("2001:db8:1::3:0", next.toText());
}

// This test verifies that it is possible to pop next address from the given
// range.
TEST(FreeLeaseQueueTest, pop) {
    FreeLeaseQueue lq;

    // Let's create two distinct address ranges.
    AddressRange range1(IOAddress("192.0.2.1"), IOAddress("192.0.2.255"));
    AddressRange range2(IOAddress("192.0.3.1"), IOAddress("192.0.3.255"));
    ASSERT_NO_THROW(lq.addRange(range1));
    ASSERT_NO_THROW(lq.addRange(range2));

    // Append some IP addresses to those address ranges.
    ASSERT_NO_THROW(lq.append(AddressRange(range1), IOAddress("192.0.2.10")));
    ASSERT_NO_THROW(lq.append(AddressRange(range1), IOAddress("192.0.2.5")));
    ASSERT_NO_THROW(lq.append(AddressRange(range2), IOAddress("192.0.3.23")));
    ASSERT_NO_THROW(lq.append(AddressRange(range2), IOAddress("192.0.3.46")));

    // Pop first from first range.
    IOAddress next(0);
    ASSERT_NO_THROW(next = lq.pop(range1));
    EXPECT_EQ("192.0.2.10", next.toText());

    // Pop second from the first range.
    ASSERT_NO_THROW(next = lq.pop(range1));
    EXPECT_EQ("192.0.2.5", next.toText());

    // After iterating over all addresses we should get empty queue.
    ASSERT_NO_THROW(next = lq.pop(range1));
    EXPECT_TRUE(next.isV4Zero());

    // Repeat that test for the second address range.
    ASSERT_NO_THROW(next = lq.pop(range2));
    EXPECT_EQ("192.0.3.23", next.toText());

    ASSERT_NO_THROW(next = lq.pop(range2));
    EXPECT_EQ("192.0.3.46", next.toText());

    ASSERT_NO_THROW(next = lq.pop(range2));
    EXPECT_TRUE(next.isV4Zero());
}

// This test verifies that it is possible to pop next prefix from the given
// range.
TEST(FreeLeaseQueueTest, popPrefix) {
    FreeLeaseQueue lq;

    // Add the range.
    PrefixRange range1(IOAddress("2001:db8:1::"), 64, 96);
    ASSERT_NO_THROW(lq.addRange(range1));

    // Append several prefixes to that range.
    ASSERT_NO_THROW(lq.append(range1, IOAddress("2001:db8:1::4:0")));
    ASSERT_NO_THROW(lq.append(range1, IOAddress("2001:db8:1::7:0")));
    ASSERT_NO_THROW(lq.append(range1, IOAddress("2001:db8:1::3:0")));
    ASSERT_NO_THROW(lq.append(range1, IOAddress("2001:db8:1::")));

    // Make sure we get retrieve them in the order in which they have
    // been added.
    IOAddress next = IOAddress::IPV6_ZERO_ADDRESS();
    ASSERT_NO_THROW(next = lq.pop(range1));
    EXPECT_EQ("2001:db8:1::4:0", next.toText());

    ASSERT_NO_THROW(next = lq.pop(range1));
    EXPECT_EQ("2001:db8:1::7:0", next.toText());

    ASSERT_NO_THROW(next = lq.pop(range1));
    EXPECT_EQ("2001:db8:1::3:0", next.toText());

    ASSERT_NO_THROW(next = lq.pop(range1));
    EXPECT_EQ("2001:db8:1::", next.toText());

    // After we went over all of them they should all be gone from the
    // container and the IPv6 zero address should be returned.
    ASSERT_NO_THROW(next = lq.pop(range1));
    EXPECT_TRUE(next.isV6Zero());
}


// Check that out of bounds address can't be appended to the range.
TEST(FreeLeaseQueueTest, nextRangeMismatch) {
    AddressRange range(IOAddress("192.0.2.1"), IOAddress("192.0.2.255"));

    FreeLeaseQueue lq;
    EXPECT_THROW(lq.append(AddressRange(range), IOAddress("192.0.3.1")),
                 isc::BadValue);
}

// Check that it is possible to return an address to the range and that the
// appropriate range is detected.
TEST(FreeLeaseQueueTest, detectRange) {
    FreeLeaseQueue lq;

    // Create three ranges.
    AddressRange range1(IOAddress("192.0.2.1"), IOAddress("192.0.2.255"));
    AddressRange range2(IOAddress("192.0.3.1"), IOAddress("192.0.3.255"));
    AddressRange range3(IOAddress("10.0.0.1"), IOAddress("10.8.1.45"));
    ASSERT_NO_THROW(lq.addRange(range1));
    ASSERT_NO_THROW(lq.addRange(range2));
    ASSERT_NO_THROW(lq.addRange(range3));

    // Append some addresses matching the first range.
    ASSERT_NO_THROW(lq.append(IOAddress("192.0.2.7")));
    ASSERT_NO_THROW(lq.append(IOAddress("192.0.2.1")));
    ASSERT_NO_THROW(lq.append(IOAddress("192.0.2.255")));

    // Make sure that these addresses have been appended to that range.
    IOAddress next(0);
    ASSERT_NO_THROW(next = lq.next(range1));
    EXPECT_EQ("192.0.2.7", next.toText());

    ASSERT_NO_THROW(next = lq.next(range1));
    EXPECT_EQ("192.0.2.1", next.toText());

    ASSERT_NO_THROW(next = lq.next(range1));
    EXPECT_EQ("192.0.2.255", next.toText());

    // Append some addresses matching the remaining two ranges.
    ASSERT_NO_THROW(lq.append(IOAddress("10.5.4.3")));
    ASSERT_NO_THROW(lq.append(IOAddress("192.0.3.98")));

    ASSERT_NO_THROW(next = lq.next(range3));
    EXPECT_EQ("10.5.4.3", next.toText());

    ASSERT_NO_THROW(next = lq.next(range2));
    EXPECT_EQ("192.0.3.98", next.toText());

    // Appending out of bounds address should not succeed.
    EXPECT_FALSE(lq.append(IOAddress("10.0.0.0")));
}

// Check that it is possible to return a delegated prefix to the range and
// that the appropriate range is detected.
TEST(FreeLeaseQueueTest, detectPrefixRange) {
    FreeLeaseQueue lq;

    // Create three ranges.
    PrefixRange range1(IOAddress("2001:db8:1::"), 64, 96);
    PrefixRange range2(IOAddress("2001:db8:2::"), 112, 120);
    PrefixRange range3(IOAddress("2001:db8:3::"), 96, 104);
    ASSERT_NO_THROW(lq.addRange(range1));
    ASSERT_NO_THROW(lq.addRange(range2));
    ASSERT_NO_THROW(lq.addRange(range3));

    // Append some prefixes matching the first range.
    ASSERT_NO_THROW(lq.append(IOAddress("2001:db8:1::7:0"), 96));
    ASSERT_NO_THROW(lq.append(IOAddress("2001:db8:1::100:0"), 96));
    ASSERT_NO_THROW(lq.append(IOAddress("2001:db8:1::4:0"), 96));

    // Make sure that these prefixes have been appended to that range.
    IOAddress next(0);
    ASSERT_NO_THROW(next = lq.next(range1));
    EXPECT_EQ("2001:db8:1::7:0", next.toText());

    ASSERT_NO_THROW(next = lq.next(range1));
    EXPECT_EQ("2001:db8:1::100:0", next.toText());

    ASSERT_NO_THROW(next = lq.next(range1));
    EXPECT_EQ("2001:db8:1::4:0", next.toText());

    // Append some prefixes matching the remaining two ranges.
    ASSERT_NO_THROW(lq.append(IOAddress("2001:db8:2::10"), 120));
    ASSERT_NO_THROW(lq.append(IOAddress("2001:db8:3::50"), 104));

    ASSERT_NO_THROW(next = lq.next(range3));
    EXPECT_EQ("2001:db8:3::50", next.toText());

    ASSERT_NO_THROW(next = lq.next(range2));
    EXPECT_EQ("2001:db8:2::10", next.toText());

    // Appending out of bounds prefix should not succeed.
    EXPECT_FALSE(lq.append(IOAddress("2001:db8:4::10"), 96));
    EXPECT_FALSE(lq.append(IOAddress("2001:db8:2::30"), 97));
}

// This test verifies that false is returned if the specified address to be
// appended does not belong to any of the existing ranges.
TEST(FreeLeaseQueueTest, detectRangeFailed) {
    AddressRange range(IOAddress("192.0.2.1"), IOAddress("192.0.2.255"));

    FreeLeaseQueue lq;
    ASSERT_NO_THROW(lq.addRange(range));

    EXPECT_FALSE(lq.append(IOAddress("192.0.3.7")));
}

// This test verifies that it is possible to append IP addresses to the
// selected range via random access index.
TEST(FreeLeaseQueueTest, appendThroughRangeIndex) {
    FreeLeaseQueue lq;

    AddressRange range1(IOAddress("192.0.2.1"), IOAddress("192.0.2.255"));
    AddressRange range2(IOAddress("192.0.3.1"), IOAddress("192.0.3.255"));
    AddressRange range3(IOAddress("10.0.0.1"), IOAddress("10.8.1.45"));
    ASSERT_NO_THROW(lq.addRange(range1));
    ASSERT_NO_THROW(lq.addRange(range2));
    ASSERT_NO_THROW(lq.addRange(range3));

    uint64_t index1 = 0;
    ASSERT_NO_THROW(index1 = lq.getRangeIndex(range1));
    uint64_t index2 = 0;
    ASSERT_NO_THROW(index2 = lq.getRangeIndex(range2));
    uint64_t index3 = 0;
    ASSERT_NO_THROW(index3 = lq.getRangeIndex(range3));

    EXPECT_NE(index1, index2);
    EXPECT_NE(index2, index3);
    EXPECT_NE(index1, index3);

    ASSERT_NO_THROW(lq.append(index1, IOAddress("192.0.2.50")));
    ASSERT_NO_THROW(lq.append(index2, IOAddress("192.0.3.50")));
    ASSERT_NO_THROW(lq.append(index3, IOAddress("10.1.1.50")));

    ASSERT_THROW(lq.append(index2, IOAddress("10.1.1.51")), BadValue);
    ASSERT_THROW(lq.append(index3, IOAddress("192.0.3.34")), BadValue);
}

// This test verifies that it is possible to append delegated prefixes to the
// selected range via random access index.
TEST(FreeLeaseQueueTest, appendPrefixThroughRangeIndex) {
    FreeLeaseQueue lq;

    PrefixRange range1(IOAddress("2001:db8:1::"), 64, 96);
    PrefixRange range2(IOAddress("2001:db8:2::"), 64, 96);
    PrefixRange range3(IOAddress("2001:db8:3::"), 64, 96);
    ASSERT_NO_THROW(lq.addRange(range1));
    ASSERT_NO_THROW(lq.addRange(range2));
    ASSERT_NO_THROW(lq.addRange(range3));

    uint64_t index1 = 0;
    ASSERT_NO_THROW(index1 = lq.getRangeIndex(range1));
    uint64_t index2 = 0;
    ASSERT_NO_THROW(index2 = lq.getRangeIndex(range2));
    uint64_t index3 = 0;
    ASSERT_NO_THROW(index3 = lq.getRangeIndex(range3));

    EXPECT_NE(index1, index2);
    EXPECT_NE(index2, index3);
    EXPECT_NE(index1, index3);

    ASSERT_NO_THROW(lq.append(index1, IOAddress("2001:db8:1::5:0:0")));
    ASSERT_NO_THROW(lq.append(index2, IOAddress("2001:db8:2::7:0:0")));
    ASSERT_NO_THROW(lq.append(index3, IOAddress("2001:db8:3::2:0:0")));

    ASSERT_THROW(lq.append(index2, IOAddress("2001:db8:3::3:0:0")), BadValue);
    ASSERT_THROW(lq.append(index3, IOAddress("2001:db8:2::8:0:0")), BadValue);
}

} // end of anonymous namespace