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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089 | // Copyright (C) 2023-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Kea Hooks Basic
// Commercial End User License Agreement v2.0. See COPYING file in the premium/
// directory.
/// @file This file contains tests DHCPv6 Bulk Lease Queries.
#include <config.h>
#include <asiolink/asio_wrapper.h>
#include <asiolink/interval_timer.h>
#include <asiolink/io_service.h>
#include <dhcp/dhcp4.h>
#include <dhcp/option_int.h>
#include <dhcp/option_custom.h>
#include <dhcp/pkt4.h>
#include <dhcp/testutils/iface_mgr_test_config.h>
#include <dhcpsrv/cfgmgr.h>
#include <dhcpsrv/lease_mgr_factory.h>
#include <bulk_lease_query4.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <blq_service.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <lease_query_impl4.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <testutils/gtest_utils.h>
#include <test_lease_mgr.h><--- 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 <list><--- 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 std;
using namespace isc;
using namespace isc::asiolink;
using namespace isc::data;
using namespace isc::dhcp;
using namespace isc::dhcp::test;
using namespace isc::lease_query;
using namespace isc::util;
namespace ph = std::placeholders;
namespace {
/// @brief Text fixture for testing LeaseQueryImpl4
template <typename TestLeaseMgrType>
class BaseBulkLeaseQuery4ProcessTest : public ::testing::Test {
public:
/// @brief Constructor
///
/// Clears the configuration and unloads hook libraries.
BaseBulkLeaseQuery4ProcessTest()
: hwaddr_empty_(new HWAddr(std::vector<uint8_t>{}, 0)),
hwaddr1_(new HWAddr(std::vector<uint8_t>{ 1, 2, 3, 4, 5, 6 }, 1)),
hwaddr2_(new HWAddr(std::vector<uint8_t>{ 2, 3, 4, 5, 6, 7 }, 1)),
cid_empty_(),
cid1_(new ClientId(vector<uint8_t>{ 1, 2, 3, 4, 5, 6 })),
cid2_(new ClientId(vector<uint8_t>{ 2, 3, 4, 5, 6, 7 })),
id1_(vector<uint8_t>{ 0xaa, 0xbb, 0xcc }),
id2_(vector<uint8_t>{ 1, 2, 3, 4}) {
CfgMgr::instance().clear();
}
/// @brief Destructor
///
/// Clears the configuration and unloads hook libraries.
virtual ~BaseBulkLeaseQuery4ProcessTest() {
BulkLeaseQueryService::reset();
LeaseMgrFactory::destroy();
CfgMgr::instance().clear();
TestLeaseMgrType test_lease_mgr;
test_lease_mgr.destroySchema();
if (test_timer_) {
test_timer_->cancel();
}
if (io_service_) {
io_service_->stopAndPoll();
}
}
/// @brief Sets the DHCP multi-threading configuration in staging SrvConfig.
///
/// @param enable_multi_threading value that maps to enable-multi-threading.
/// @param thread_pool_size value that maps to thread-pool-size.
/// @param queue_size value that maps to queue-size.
void setDHCPMultiThreadingConfig(bool enabled, uint32_t thread_pool_size,
uint32_t packet_queue_size) {
MultiThreadingMgr::instance().setMode(enabled);
ElementPtr mt_config = Element::createMap();
mt_config->set("enable-multi-threading", Element::create(enabled));
mt_config->set("thread-pool-size",
Element::create(static_cast<int>(thread_pool_size)));
mt_config->set("queue-size",
Element::create(static_cast<int>(packet_queue_size)));
CfgMgr::instance().getStagingCfg()->setDHCPMultiThreading(mt_config);
}
/// @brief Prepares system managers for testing
/// - Creates a new v4 lease manager
/// - Creates the configuration.
void SetUp() {
LeaseMgrFactory::destroy();
TestLeaseMgrType test_lease_mgr;
test_lease_mgr.createSchema();
LeaseMgrFactory::create(test_lease_mgr.dbAccess());
CfgMgr& cfg_mgr = CfgMgr::instance();
CfgSubnets4Ptr subnets = cfg_mgr.getStagingCfg()->getCfgSubnets4();
// Build and add subnet2_.
subnet2_.reset(new Subnet4(IOAddress("192.0.2.0"), 24,
1200, 2400, 3600, 2));
Pool4Ptr pool(new Pool4(IOAddress("192.0.2.100"),
IOAddress("192.0.2.200")));
subnet2_->addPool(pool);
// Add a second pool.
pool.reset(new Pool4(IOAddress("192.0.2.201"),
IOAddress("192.0.2.220")));
subnet2_->addPool(pool);
subnets->add(subnet2_);
// Build and add subnet3_.
Triplet<uint32_t> unspec;
subnet3_.reset(new Subnet4(IOAddress("192.0.3.0"), 24,
unspec, unspec, 3600, 3));
pool.reset(new Pool4(IOAddress("192.0.3.100"),
IOAddress("192.0.3.200")));
subnet3_->addPool(pool);
subnet3_->setT1Percent(0.5);
subnet3_->setT2Percent(0.75);
subnet3_->setCalculateTeeTimes(true);
subnets->add(subnet3_);
// Build and add subnet4_.
subnet4_.reset(new Subnet4(IOAddress("192.0.4.0"), 24,
unspec, unspec, 3600, 4));
pool.reset(new Pool4(IOAddress("192.0.4.100"),
IOAddress("192.0.4.200")));
subnet4_->addPool(pool);
subnet4_->setCalculateTeeTimes(false);
subnets->add(subnet4_);
// Build and add a shared-network.
CfgSharedNetworks4Ptr networks = cfg_mgr.getStagingCfg()->getCfgSharedNetworks4();
network1_.reset(new SharedNetwork4("one"));
network1_->add(subnet4_);
networks->add(network1_);
// Commit the config.
cfg_mgr.commit();
// Create I/O service.
io_service_.reset(new IOService());
// Enable Kea core MT. We set it here as code checks staging not current.
setDHCPMultiThreadingConfig(true, 4, 16);
}
/// @brief Create the Multi-Threaded Lease Query Listener Manager.
///
/// @param page_size Maximum size of the page returned.
void createBulkLeaseQueryService(size_t page_size = 10) {
ElementPtr cfg = Element::createMap();
ElementPtr advanced = Element::createMap();
cfg->set("requesters", Element::fromJSON("[ \"127.0.0.1\" ]"));
cfg->set("advanced", advanced);
advanced->set("max-leases-per-fetch",
Element::create(static_cast<long long>(page_size)));
ASSERT_NO_THROW(impl_.reset(new LeaseQueryImpl4(cfg)));
EXPECT_TRUE(BulkLeaseQueryService::instance());
}
/// @brief Adds a v4 lease to the lease store.
///
/// @param addr ip address of the lease
/// @param subnet subnet to which the lease belongs
/// @param hwaddr hardware addresss
/// @param client_id duid of the lease owner
/// @param cltt CLTT of the lease
/// @param json_context user context of the lease as JSON
/// @param state lease state of the lease
///
/// @return pointer to the new lease
Lease4Ptr addLease(const IOAddress& addr, const Subnet4Ptr& subnet,
const HWAddrPtr& hwaddr, const ClientIdPtr& client_id,
time_t cltt, const string& json_context = "",
const uint32_t state = Lease::STATE_DEFAULT) {
if (!subnet) {
isc_throw(Unexpected, "subnet cannot be empty");
}
Lease4Ptr lease(new Lease4(addr, hwaddr, client_id,
subnet->getValid(), cltt, subnet->getID()));
lease->state_ = state;
if (!json_context.empty()) {
ElementPtr user_context;
user_context = Element::fromJSON(json_context);
lease->setContext(user_context);
LeaseMgr::extractLease4ExtendedInfo(lease, false);
}
LeaseMgrFactory::instance().addLease(lease);
return (lease);
}
/// @brief Creates a DHCPBULKLEASEQUERY packet.
///
/// @return DHCPBULKLEASEQUERY packet.
Pkt4Ptr makeLeaseQuery() const {
Pkt4Ptr query(new Pkt4(DHCPBULKLEASEQUERY, 123));
query->setHWAddr(hwaddr_empty_);
return (query);
}
/// @brief Convenience method for making a client identifier option instance.
///
/// @param cid client id.
///
/// @return Pointer to the newly constructed option.
OptionPtr makeClientIdOption(const ClientIdPtr& cid) const {
OptionPtr opt(new Option(Option::V4, DHO_DHCP_CLIENT_IDENTIFIER,
cid->getClientId()));
return (opt);
}
/// @brief Convenience method for making an identifier option instance.
///
/// @param opt_type option type (DHO_...).
/// @param data binary data a vector of bytes.
///
/// @return Pointer to the newly constructed option.
OptionPtr makeBinaryOption(const uint8_t& opt_type,
const vector<uint8_t>& data) const {
OptionPtr opt(new Option(Option::V4, opt_type, data));
return (opt);
}
/// @brief Convenience method for making a relay identifier option instance.
///
/// @param data relay id as a vector of bytes.
///
/// @return Pointer to the newly constructed option.
OptionPtr makeRelayIdOption(const vector<uint8_t>& data) const {
return (makeBinaryOption(RAI_OPTION_RELAY_ID, data));
}
/// @brief Convenience method for making a remote identifier option instance.
///
/// @param data remote id as a vector of bytes.
///
/// @return Pointer to the newly constructed option.
OptionPtr makeRemoteIdOption(const vector<uint8_t>& data) const {
return (makeBinaryOption(RAI_OPTION_REMOTE_ID, data));
}
/// @brief Creates a DHCPBULKLEASEQUERY packet for query by client id.
///
/// @param cid client id for which to query.
///
/// @return DHCPBULKLEASEQUERY packet.
Pkt4Ptr makeQueryByClientId(const ClientIdPtr& cid) const {
Pkt4Ptr query = makeLeaseQuery();
OptionPtr opt = makeClientIdOption(cid);
query->addOption(opt);
return (query);
}
/// @brief Creates a DHCPBULKLEASEQUERY packet for query by hardware address.
///
/// @param hwaddr hardware address for which to query.
///
/// @return DHCPBULKLEASEQUERY packet.
Pkt4Ptr makeQueryByHWAddr(const HWAddrPtr& hwaddr) const {
Pkt4Ptr query = makeLeaseQuery();
query->setHWAddr(hwaddr);
return (query);
}
/// @brief Creates a DHCPBULKLEASEQUERY packet for query by relay id.
///
/// @param data relay id as a vector of bytes.
///
/// @return DHCPBULKLEASEQUERY packet.
Pkt4Ptr makeQueryByRelayId(const vector<uint8_t>& data) const {
Pkt4Ptr query = makeLeaseQuery();
OptionPtr rai(new Option(Option::V4, DHO_DHCP_AGENT_OPTIONS));
OptionPtr opt = makeRelayIdOption(data);
rai->addOption(opt);
query->addOption(rai);
return (query);
}
/// @brief Creates a DHCPBULKLEASEQUERY packet for query by remote id.
///
/// @param data remote id as a vector of bytes.
///
/// @return DHCPBULKLEASEQUERY packet.
Pkt4Ptr makeQueryByRemoteId(const vector<uint8_t>& data) const {
Pkt4Ptr query = makeLeaseQuery();
OptionPtr rai(new Option(Option::V4, DHO_DHCP_AGENT_OPTIONS));
OptionPtr opt = makeRemoteIdOption(data);
rai->addOption(opt);
query->addOption(rai);
return (query);
}
/// @brief Verifies that a response contains the expected value for
/// an option containing a time sensitive value. The value is
/// is correct if it lies within a range.
///
/// @param response response to test
/// @param code option code of the expected option
/// @param exp_value value the option should have
/// @param tol tolerance in seconds. Defaults to 2. Actual values
/// are correct if they fall within +/- tolerance of expected value.
void verifyTimeOption(const Pkt4Ptr& response, uint16_t code,
uint32_t exp_value, uint32_t tol = 2) const {
OptionUint32Ptr uint32_opt;
uint32_opt = boost::dynamic_pointer_cast<OptionUint32>(response->getOption(code));
ASSERT_TRUE(uint32_opt)
<< "option: " << code << " missing or it has an unexpected type";
uint32_t lower = ((exp_value > tol) ? (exp_value - tol) : 0);
uint32_t upper = ((exp_value < (UINT32_MAX - 2)) ? (exp_value + tol) : UINT32_MAX);
uint32_t actual = uint32_opt->getValue();
EXPECT_TRUE((lower <= actual ) && (actual <= upper))
<< "option: " << code << " value is wrong:"
<< " lower: " << lower << " <= "
<< " actual: " << actual << " <= "
<< " upper: " << upper;
}
/// @brief Verify relay-agent-info is correct.
///
/// @param response response to test
/// @param exp_hex_string expected option data as a hex string "0x...".
/// If empty the option should not be present.
void verifyRelayAgentInfo(const Pkt4Ptr& response,
const std::string& exp_hex_string) const {
// Look for relay agent information option (option 82)
OptionPtr rai = response->getOption(DHO_DHCP_AGENT_OPTIONS);
if (exp_hex_string.empty()) {
// Option should not be present.
ASSERT_FALSE(rai) << "relay-agent-info should not be present";
return;
}
// Verify it is present and content is as expected.
ASSERT_TRUE(rai) << "relay-agent-info should be present";
EXPECT_EQ(exp_hex_string, rai->toHexString());
}
/// @brief Validate that a Pkt4 contains the expected DHO_STATUS_CODE.
///
/// @param response response packet.
/// @param exp_status expected status code.
/// @param exp_msg expected status text message.
void checkStatus(const Pkt4Ptr& response,
const BLQStatusCode& exp_status,
const string& exp_msg = "") {
OptionCustomPtr status_opt =
boost::dynamic_pointer_cast<OptionCustom>
(response->getOption(DHO_STATUS_CODE));
ASSERT_TRUE(status_opt) << "DHO_STATUS_CODE missing";
EXPECT_NO_THROW(checkStatus(status_opt, exp_status, exp_msg));
}
/// @brief Validate a DHO_STATUS_CODE option.
///
/// @param status_opt status-code option to check.
/// @param exp_status expected status code.
/// @param exp_msg expected status text message.
void checkStatus(const OptionCustomPtr& status_opt,
const BLQStatusCode& exp_status,
const string& exp_msg = "") {
uint8_t status = status_opt->readInteger<uint8_t>(0);
EXPECT_EQ(exp_status, status);
string msg = status_opt->readString(1);
EXPECT_EQ(exp_msg, msg);
}
/// @brief Checks the value of the dhcp-server-identifier option.
///
/// @param pkt packet to check.
/// @param expected_address IP address the packet's option should contain.
void checkServerIdOption(const Pkt4Ptr& pkt,
const IOAddress& expected_address) {
OptionPtr opt = pkt->getOption(DHO_DHCP_SERVER_IDENTIFIER);
ASSERT_TRUE(opt) << "no server-id option";
OptionCustomPtr server_id =
boost::dynamic_pointer_cast<OptionCustom>(opt);
ASSERT_TRUE(server_id) << "bad server-id option";
EXPECT_EQ(expected_address, server_id->readAddress(0));
}
/// @brief Fetches a packet from the sent packet list.
///
/// @return The packet de-queued.
Pkt4Ptr getSentPacket() {
Pkt4Ptr sent_packet;
{
lock_guard<mutex> lck(sent_packets_mutex_);
if (!sent_packets_.empty()) {
sent_packet = sent_packets_.front();
sent_packets_.pop_front();
}
}
return (sent_packet);
}
/// @brief Flushes any packets in the sent packet queue.
void flushSentPackets() {
lock_guard<mutex> lck(sent_packets_mutex_);
sent_packets_.clear();
}
/// @brief Queue to hold any packets sent by BulkLeaseQuery4 instances.
list<Pkt4Ptr> sent_packets_;
/// @brief Mutex to protect the sent packet queue.
std::mutex sent_packets_mutex_;
/// @brief Convenience values.
HWAddrPtr hwaddr_empty_;
HWAddrPtr hwaddr1_;
HWAddrPtr hwaddr2_;
ClientIdPtr cid_empty_;
ClientIdPtr cid1_;
ClientIdPtr cid2_;
vector<uint8_t> id1_;
vector<uint8_t> id2_;
Subnet4Ptr subnet2_;
Subnet4Ptr subnet3_;
Subnet4Ptr subnet4_;
SharedNetwork4Ptr network1_;
static string user_context_;
static string user_context1_;
static string user_context2_;
IOServicePtr io_service_;
IntervalTimerPtr test_timer_;
LeaseQueryImplPtr impl_;
BulkLeaseQuery4Ptr bulk_lease_query_;
/// @note Factored test parts: to share.
/// @brief Timeout handler.
static void TimeoutHandler() {
isc_throw(Unexpected, "test timeouts");
}
/// @brief The post method.
///
/// @param cont The continuation to post.
void post(const BlqPostCbArg& cont) {
ASSERT_TRUE(io_service_);
io_service_->post(cont);
}
/// @brief The post callback.
BlqPostCb post_cb_ =
std::bind(&BaseBulkLeaseQuery4ProcessTest::post, this, ph::_1);
/// @brief The pushToSend method.
///
/// @param response The response.
/// @return Always true.
bool pushToSend(BlqResponsePtr response) {
Pkt4Ptr resp =
boost::dynamic_pointer_cast<Pkt4>(response->getResponse());
if (!resp) {
isc_throw(Unexpected, "response is null or not v4");
}
lock_guard<mutex> lck(sent_packets_mutex_);
sent_packets_.push_back(resp);
return (true);
}
/// @brief The pushToSend callback.
BlqPushToSendCb push_to_send_cb_ =
std::bind(&BaseBulkLeaseQuery4ProcessTest::pushToSend, this, ph::_1);
/// @brief The queryComplete method.
///
/// @param xid The transaction id (ignored).
void queryComplete(Xid) {
ASSERT_TRUE(io_service_);
io_service_->stop();
}
/// @brief The queryComplete callback.
BlqQueryCompleteCb query_complete_cb_ =
std::bind(&BaseBulkLeaseQuery4ProcessTest::queryComplete, this, ph::_1);
/// @brief Initialize BulkLeaseQuery4.
///
/// @param query The query.
void initBulkQuery(PktPtr query) {
ASSERT_TRUE(query);
BlqQueryPtr blq_query(new BlqQuery(query));
bulk_lease_query_.reset(new BulkLeaseQuery4(blq_query,
post_cb_,
push_to_send_cb_,
query_complete_cb_));
}
/// @brief Process a single bulk query.
///
/// @param query The query.
void processBulkQuery(Pkt4Ptr query) {
// Init BulkLeaseQuery4.
initBulkQuery(query);
ASSERT_TRUE(bulk_lease_query_);
// Check for early errors.
if (bulk_lease_query_->isProcessed()) {
return;
}
// Setup test processing environment.
test_timer_.reset(new IntervalTimer(io_service_));
test_timer_->setup(TimeoutHandler, 2000, IntervalTimer::ONE_SHOT);
// Start processing.
bulk_lease_query_->start();
// Wait for completion.
try {
if (!io_service_->stopped()) {
io_service_->run();
}
} catch (const std::exception& ex) {
ADD_FAILURE() << "run failed with " << ex.what();
}
// Clear test processing environment.
test_timer_->cancel();
io_service_->stopAndPoll();
test_timer_.reset();
io_service_.reset();
}
/// @brief Verify client id.
///
/// @param cid Expected client id.
/// @param pkt Pakcet where to find the client id option.
void verifyClientId(ClientIdPtr cid, const PktPtr& pkt) {
ASSERT_TRUE(cid);
OptionPtr opt = pkt->getOption(DHO_DHCP_CLIENT_IDENTIFIER);
ASSERT_TRUE(opt) << "can't find DHO_DHCP_CLIENT_IDENTIFIER";
EXPECT_EQ(cid->getClientId(), opt->getData());
}
/// @brief Verify hardware address.
///
/// @param hwaddr Expected hardware address.
/// @param pkt Packet where to find the hardware address.
void verifyHWaddr(HWAddrPtr hwaddr, Pkt4Ptr pkt) {
ASSERT_TRUE(hwaddr);
ASSERT_TRUE(pkt);
HWAddrPtr hw = pkt->getHWAddr();
ASSERT_TRUE(hw);
EXPECT_EQ(hwaddr->toText(false), hw->toText(false));
}
/// @brief Verify by client id.
void verifyByClientId();
/// @brief Veify by hardware address.
void verifyByHWAddr();
/// @brief Verify by relay id.
///
/// @param page_size Maximum size of returned pages.
void verifyByRelayId(size_t page_size);
/// @brief Verify by remote id.
///
/// @param page_size Maximum size of returned pages.
void verifyByRemoteId(size_t page_size);
};
template <typename TestLeaseMgrType> string
BaseBulkLeaseQuery4ProcessTest<TestLeaseMgrType>::user_context_ =
"{ \"ISC\": {"
" \"relay-agent-info\": {"
" \"sub-options\": \"0x0104A1B1C1D1\""
" } } }";
template <typename TestLeaseMgrType> string
BaseBulkLeaseQuery4ProcessTest<TestLeaseMgrType>::user_context1_ =
"{ \"ISC\": {"
" \"relay-agent-info\": {"
" \"relay-id\": \"AABBCC\","
" \"remote-id\": \"01020304\","
" \"sub-options\": \"0204010203040C03AABBCC\""
" } } }";
template <typename TestLeaseMgrType> string
BaseBulkLeaseQuery4ProcessTest<TestLeaseMgrType>::user_context2_ =
"{ \"ISC\": {"
" \"relay-agent-info\": {"
" \"relay-id\": \"01020304\","
" \"remote-id\": \"AABBCC\","
" \"sub-options\": \"0203AABBCC0C0401020304\""
" } } }";
// Verifies that configuration errors are detected.
TEST(BulkLeaseQuery4Test, invalidConfig4) {<--- syntax error
struct Scenario {
const string description_;
const string config_;
const string exp_msg_;
};
vector<Scenario> scenarios = {
{
"unknown keyword",
"{ \"foo\": 1 }",
"spurious 'foo' parameter"
},
{
"bulk-query-enabled not a boolean",
"{ \"bulk-query-enabled\": 1 }",
"'bulk-query-enabled' parameter is not a boolean"
},
{
"active-query-enabled not a boolean",
"{ \"active-query-enabled\": \"on\" }",
"'active-query-enabled' parameter is not a boolean"
},
{
"active-query-enabled not supported",
"{ \"active-query-enabled\": true }",
"Active query is not supported"
},
{
"extended-info-tables-enabled not a boolean",
"{ \"extended-info-tables-enabled\": 0}",
"'extended-info-tables-enabled' parameter is not a boolean"
},
{
"lease-query-ip not a string",
"{ \"lease-query-ip\": 1 }",
"'lease-query-ip' parameter is not a string"
},
{
"lease-query-ip bad address",
"{ \"lease-query-ip\": \"1:2\" }",
"invalid lease-query-ip '1:2': Failed to convert string to"
" address '1:2': Invalid argument"
},
{
"lease-query-ip IPv6",
"{ \"lease-query-ip\": \"::1\" }",
"lease-query-ip not a IPv4 address"
},
{
"lease-query-tcp-port not an integer",
"{ \"lease-query-tcp-port\": true }",
"'lease-query-tcp-port' parameter is not an integer"
},
{
"lease-query-tcp-port is 0",
"{ \"lease-query-tcp-port\": 0 }",
"invalid lease-query-tcp-port '0'"
},
{
"lease-query-tcp-port is 65536",
"{ \"lease-query-tcp-port\": 65536 }",
"invalid lease-query-tcp-port '65536'"
},
{
"max-bulk-query-threads not an integer",
"{ \"max-bulk-query-threads\": false }",
"'max-bulk-query-threads' parameter is not an integer"
},
{
"max-bulk-query-threads -1",
"{ \"max-bulk-query-threads\": -1 }",
"invalid max-bulk-query-threads '-1'"
},
{
"max-requester-connections not an integer",
"{ \"max-requester-connections\": 1.000 }",
"'max-requester-connections' parameter is not an integer"
},
{
"max-requester-connections 0",
"{ \"max-requester-connections\": 0 }",
"invalid max-requester-connections '0'"
},
{
"max-concurrent-queries not an integer",
"{ \"max-concurrent-queries\": \"10\" }",
"'max-concurrent-queries' parameter is not an integer"
},
{
"max-concurrent-queries -1",
"{ \"max-concurrent-queries\": -1 }",
"invalid max-concurrent-queries '-1'"
},
{
"max-requester-idle-time not an integer",
"{ \"max-requester-idle-time\": 300. }",
"'max-requester-idle-time' parameter is not an integer"
},
{
"max-requester-idle-time 0",
"{ \"max-requester-idle-time\": 0 }",
"invalid max-requester-idle-time '0'"
},
{
"max-requester-idle-time 5000000",
"{ \"max-requester-idle-time\": 5000000 }",
"invalid max-requester-idle-time '5000000'"
},
{
"max-leases-per-fetch not an integer",
"{ \"max-leases-per-fetch\": \"5\" }",
"'max-leases-per-fetch' parameter is not an integer"
},
{
"max-leases-per-fetch 0",
"{ \"max-leases-per-fetch\": 0 }",
"invalid max-leases-per-fetch '0'"
}
};
for (auto const& scenario : scenarios) {
SCOPED_TRACE(scenario.description_);
LeaseQueryImplPtr impl;
ElementPtr cfg = Element::createMap();
ASSERT_NO_THROW(cfg->set("advanced",
Element::fromJSON(scenario.config_)))
<< "bad JSON: " << scenario.config_;
cfg->set("requesters", Element::fromJSON("[ \"127.0.0.1\" ]"));
EXPECT_THROW_MSG(impl.reset(new LeaseQueryImpl4(cfg)),
Exception, scenario.exp_msg_);
}
}
/// @brief Memfile test fixture.
class MemfileBulkLeaseQuery4ProcessTest : public
BaseBulkLeaseQuery4ProcessTest<MemfileTestLeaseMgr> {
};
// Verifies that valid v4 configuration parses and that requesters
// can be validated.
TEST_F(MemfileBulkLeaseQuery4ProcessTest, validConfig4) {
// Create an implementation with two requesters.
const string json = "{\n"
" \"requesters\" : [ \"127.0.0.1\", \"192.0.2.2\" ],\n"
" \"advanced\": {\n"
" \"bulk-query-enabled\": true,\n"
" \"active-query-enabled\": false,\n"
" \"extended-info-tables-enabled\": false,\n"
" \"lease-query-ip\": \"127.0.0.1\",\n"
" \"lease-query-tcp-port\": 12345,\n"
" \"max-bulk-query-threads\": 10,\n"
" \"max-requester-connections\": 10,\n"
" \"max-concurrent-queries\": 5,\n"
" \"max-requester-idle-time\": 300,\n"
" \"max-leases-per-fetch\": 100\n"
" }"
"}";
ConstElementPtr config;
ASSERT_NO_THROW_LOG(config = Element::fromJSON(json));
LeaseQueryImplPtr impl;
ASSERT_NO_THROW_LOG(impl.reset(new LeaseQueryImpl4(config)));
// Verify known and unknown requesters check correctly.
EXPECT_EQ(2, impl->getNumRequesters());
EXPECT_FALSE(impl->isRequester(IOAddress("192.0.2.1")));
EXPECT_TRUE(impl->isRequester(IOAddress("127.0.0.1")));
EXPECT_TRUE(impl->isRequester(IOAddress("192.0.2.2")));
// Make sure a test with a v6 address complains.
ASSERT_THROW_MSG(impl->isRequester(IOAddress("2001:db8:1::1")), BadValue,
"not a IPv4 address");
// Verify the advanced config.
auto mgr = BulkLeaseQueryService::instance();
ASSERT_TRUE(mgr);
EXPECT_EQ(AF_INET, mgr->getFamily());
EXPECT_TRUE(mgr->getBulkQueryEnabled());
EXPECT_FALSE(mgr->getActiveQueryEnabled());
EXPECT_FALSE(mgr->getExtendedInfoTablesEnabled());
EXPECT_EQ(IOAddress("127.0.0.1"), mgr->getLeaseQueryIp());
EXPECT_EQ(12345, mgr->getLeaseQueryTcpPort());
EXPECT_EQ(10, mgr->getMaxBulkQueryThreads());
EXPECT_EQ(10, mgr->getMaxRequesterConnections());
EXPECT_EQ(5, mgr->getMaxConcurrentQueries());
EXPECT_EQ(300, mgr->getMaxRequesterIdleTime());
EXPECT_EQ(100, mgr->getMaxLeasePerFetch());
// Verify setters.
EXPECT_NO_THROW(mgr->setMaxBulkQueryThreads(20));
EXPECT_EQ(20, mgr->getMaxBulkQueryThreads());
EXPECT_THROW_MSG(mgr->setMaxRequesterConnections(0), BadValue,
"new max requester connections is 0");
EXPECT_NO_THROW(mgr->setMaxRequesterConnections(20));
EXPECT_EQ(20, mgr->getMaxRequesterConnections());
EXPECT_NO_THROW(mgr->setMaxConcurrentQueries(0));
EXPECT_EQ(0, mgr->getMaxConcurrentQueries());
EXPECT_THROW_MSG(mgr->setMaxLeasePerFetch(0), BadValue,
"new max leases per fetch is 0");
EXPECT_NO_THROW(mgr->setMaxLeasePerFetch(200));
EXPECT_EQ(200, mgr->getMaxLeasePerFetch());
// Make sure listener involved functions do not misbehave.
ASSERT_NO_THROW(mgr->startListener());
ASSERT_NO_THROW(mgr->checkListenerPausePermission());
ASSERT_NO_THROW(mgr->pauseListener());
ASSERT_NO_THROW(mgr->resumeListener());
ASSERT_NO_THROW(mgr->stopListener());
}
// Verifies that service detects when Kea core is not configured
// for multi-threading when BLQ is enabled.
TEST_F(MemfileBulkLeaseQuery4ProcessTest, coreNotMultiThreadedCheck) {
// Create an implementation with two requesters.
const string json = "{\n"
" \"requesters\" : [ \"127.0.0.1\", \"192.0.2.2\" ],\n"
" \"advanced\": {\n"
" \"bulk-query-enabled\": true,\n"
" \"active-query-enabled\": false,\n"
" \"extended-info-tables-enabled\": false,\n"
" \"lease-query-ip\": \"127.0.0.1\",\n"
" \"lease-query-tcp-port\": 12345,\n"
" \"max-bulk-query-threads\": 10,\n"
" \"max-requester-connections\": 10,\n"
" \"max-concurrent-queries\": 5,\n"
" \"max-requester-idle-time\": 300,\n"
" \"max-leases-per-fetch\": 100\n"
" }"
"}";
ConstElementPtr config;
ASSERT_NO_THROW_LOG(config = Element::fromJSON(json));
// Turn off core multi-threading.
setDHCPMultiThreadingConfig(false, 0, 0);
LeaseQueryImplPtr impl;
ASSERT_THROW_MSG(impl.reset(new LeaseQueryImpl4(config)), BadValue,
"BulkLeaseQuery requires Kea multi-threading to be enabled");
}
// Verifies that implementation loads when core MT is off but
// bulk lease query is disabled.
TEST_F(MemfileBulkLeaseQuery4ProcessTest, bulkLeaseQueryDisabled) {
// Create an implementation with two requesters.
const string json = "{\n"
" \"requesters\" : [ \"127.0.0.1\", \"192.0.2.2\" ],\n"
" \"advanced\": {\n"
" \"bulk-query-enabled\": false,\n"
" \"active-query-enabled\": false,\n"
" \"extended-info-tables-enabled\": false,\n"
" \"lease-query-ip\": \"127.0.0.1\",\n"
" \"lease-query-tcp-port\": 12345,\n"
" \"max-bulk-query-threads\": 10,\n"
" \"max-requester-connections\": 10,\n"
" \"max-concurrent-queries\": 5,\n"
" \"max-requester-idle-time\": 300,\n"
" \"max-leases-per-fetch\": 100\n"
" }"
"}";
ConstElementPtr config;
ASSERT_NO_THROW_LOG(config = Element::fromJSON(json));
// Turn off core multi-threading.
setDHCPMultiThreadingConfig(false, 0, 0);
LeaseQueryImplPtr impl;
ASSERT_NO_THROW(impl.reset(new LeaseQueryImpl4(config)));
// Verify the advanced config.
auto mgr = BulkLeaseQueryService::instance();
ASSERT_TRUE(mgr);
EXPECT_FALSE(mgr->getBulkQueryEnabled());
// Make sure listener involved functions do not misbehave.
ASSERT_NO_THROW(mgr->startListener());
ASSERT_NO_THROW(mgr->checkListenerPausePermission());
ASSERT_NO_THROW(mgr->pauseListener());
ASSERT_NO_THROW(mgr->resumeListener());
ASSERT_NO_THROW(mgr->stopListener());
}
// These tests verify scenarios where the inbound packet invalid and
// to for which the init function throws.
TEST_F(MemfileBulkLeaseQuery4ProcessTest, initQueryInvalidQuery) {
// An v6 packet should get tossed.
Pkt6Ptr pkt6(new Pkt6(DHCPV6_LEASEQUERY, 123));
ASSERT_THROW_MSG(initBulkQuery(pkt6), BadValue,
"BulkLeaseQuery4 has no DHCPv4 query");
}
// This test verifies scenarios were the query is logically invalid and
// to which the server responds with a DHCPLEASEQUERYDONE packet
// but with an error status code option.
TEST_F(MemfileBulkLeaseQuery4ProcessTest, initQueryInvalidWithStatus) {
// For the server id.
createBulkLeaseQueryService();
// Structure that contains a test scenario.
struct Scenario {
const string description_;
std::function<void(Pkt4Ptr)> modifier_;
BLQStatusCode exp_status_code_;
const string exp_message_;
};
// List of test scenarios to execute.
vector<Scenario> scenarios {
{
"no zero ciaddr",
[](Pkt4Ptr lq) {
lq->setCiaddr(IOAddress("129.0.2.1"));
},
BLQ_STATUS_MalformedQuery,
"ciaddr must be zero"
},
{
"no zero yiaddr",
[](Pkt4Ptr lq) {
lq->setYiaddr(IOAddress("129.0.2.1"));
},
BLQ_STATUS_MalformedQuery,
"yiaddr must be zero"
},
{
"no zero siaddr",
[](Pkt4Ptr lq) {
lq->setSiaddr(IOAddress("129.0.2.1"));
},
BLQ_STATUS_MalformedQuery,
"siaddr must be zero"
},
{
"malformed query start time",
[](Pkt4Ptr lq) {
OptionPtr opt(new Option(Option::V4, DHO_QUERY_START_TIME));
lq->addOption(opt);
},
BLQ_STATUS_MalformedQuery,
"illegal 'query-start-time'"
},
{
"negative query start time",
[](Pkt4Ptr lq) {
OptionPtr opt(new OptionUint32(Option::V4,
DHO_QUERY_START_TIME,
static_cast<uint32_t>(-1)));
lq->addOption(opt);
},
BLQ_STATUS_MalformedQuery,
"illegal 'query-start-time'"
},
{
"malformed query end time",
[](Pkt4Ptr lq) {
OptionPtr opt(new Option(Option::V4, DHO_QUERY_END_TIME));
lq->addOption(opt);
},
BLQ_STATUS_MalformedQuery,
"illegal 'query-end-time'"
},
{
"negative query end time",
[](Pkt4Ptr lq) {
OptionPtr opt(new OptionUint32(Option::V4,
DHO_QUERY_END_TIME,
static_cast<uint32_t>(-1)));
lq->addOption(opt);
},
BLQ_STATUS_MalformedQuery,
"illegal 'query-end-time'"
},
{
"query start time > end time",
[](Pkt4Ptr lq) {
OptionPtr opt(new OptionUint32(Option::V4,
DHO_QUERY_START_TIME,
time(0) + 10000));
lq->addOption(opt);
opt.reset(new OptionUint32(Option::V4,
DHO_QUERY_END_TIME,
time(0)));
lq->addOption(opt);
},
BLQ_STATUS_MalformedQuery,
"query-start-time > query-end-time"
},
{
"vpn id",
[](Pkt4Ptr lq) {
OptionPtr opt(new Option(Option::V4, DHO_VSS));
lq->addOption(opt);
},
BLQ_STATUS_NotAllowed,
"VPNs are not supported"
},
{
"for all configured addresses",
[](Pkt4Ptr) { },
BLQ_STATUS_NotAllowed,
"query for all configured addresses is not supported"
},
{
"multiple query types",
[this](Pkt4Ptr lq) {
OptionPtr opt = makeClientIdOption(cid1_);
lq->addOption(opt);
lq->setHWAddr(hwaddr1_);
},
BLQ_STATUS_MalformedQuery,
"multiple queries"
},
{
"multiple query types 2",
[this](Pkt4Ptr lq) {
OptionPtr rai(new Option(Option::V4, DHO_DHCP_AGENT_OPTIONS));
const vector<uint8_t> relay = { 0xaa, 0xbb, 0xcc };
OptionPtr opt = makeRelayIdOption(relay);
rai->addOption(opt);
const vector<uint8_t> remote = { 1, 2, 3, 4 };
opt = makeRemoteIdOption(remote);
rai->addOption(opt);
lq->addOption(rai);
},
BLQ_STATUS_MalformedQuery,
"multiple queries"
},
{
"empty relay id option",
[this](Pkt4Ptr lq) {
OptionPtr rai(new Option(Option::V4, DHO_DHCP_AGENT_OPTIONS));
const vector<uint8_t> empty;
OptionPtr opt = makeRelayIdOption(empty);
rai->addOption(opt);
lq->addOption(rai);
},
BLQ_STATUS_MalformedQuery,
"empty relay id option"
},
{
"empty remote id option",
[this](Pkt4Ptr lq) {
OptionPtr rai(new Option(Option::V4, DHO_DHCP_AGENT_OPTIONS));
const vector<uint8_t> empty;
OptionPtr opt = makeRemoteIdOption(empty);
rai->addOption(opt);
lq->addOption(rai);
},
BLQ_STATUS_MalformedQuery,
"empty remote id option"
}
};
// Iterate over the scenarios.
for (auto const& scenario : scenarios) {
SCOPED_TRACE(scenario.description_);
// Make the lease query.
Pkt4Ptr lq = makeLeaseQuery();
// Modify the lease query.
ASSERT_NO_THROW_LOG(scenario.modifier_(lq));
// Initialize the query.
ASSERT_NO_THROW_LOG(initBulkQuery(lq));
// We should have generated a DHCPLEASEQUERYDONE with a
// status option containing the expected status.
Pkt4Ptr done = getSentPacket();
ASSERT_TRUE(done);
ASSERT_EQ(DHCPLEASEQUERYDONE, done->getType());
checkStatus(done, scenario.exp_status_code_, scenario.exp_message_);
// It is the first packet so it should carry a server id.
checkServerIdOption(done, IOAddress("127.0.0.1"));
// Make sure there are no left over packets.
ASSERT_FALSE(getSentPacket());
}
}
/// This test exercises queries by client id.
template <typename TestLeaseMgrType> void
BaseBulkLeaseQuery4ProcessTest<TestLeaseMgrType>::verifyByClientId() {
// For the server id.
createBulkLeaseQueryService();
time_t now = time(0);
// Create some leases.
Lease4Ptr active_lease = addLease(IOAddress("192.0.2.2"), subnet2_,
HWAddrPtr(), cid2_, now);
Lease4Ptr expired_lease = addLease(IOAddress("192.0.2.3"), subnet2_,
HWAddrPtr(), cid1_, now - 4000);
Lease4Ptr declined_lease = addLease(IOAddress("192.0.2.4"), subnet2_,
HWAddrPtr(), cid1_, now, "",
Lease::STATE_DECLINED);
Lease4Ptr reclaimed_lease = addLease(IOAddress("192.0.2.5"), subnet2_,
HWAddrPtr(), cid1_, now, "",
Lease::STATE_EXPIRED_RECLAIMED);
Lease4Ptr second_lease = addLease(IOAddress("192.0.3.6"), subnet3_,
hwaddr1_, cid2_, now - 1000,
user_context_);
// Make the lease query.
Pkt4Ptr lq = makeQueryByClientId(cid1_);
// Process the query.
ASSERT_NO_THROW_LOG(processBulkQuery(lq));
// We should have generated a DHCPLEASEQUERYDONE without a status option.
Pkt4Ptr done = getSentPacket();
ASSERT_TRUE(done);
ASSERT_EQ(DHCPLEASEQUERYDONE, done->getType());
EXPECT_FALSE(done->getOption(DHO_STATUS_CODE));
// It is the first packet so it should carry a server id.
checkServerIdOption(done, IOAddress("127.0.0.1"));
// Make sure there are no left over packets.
ASSERT_FALSE(getSentPacket());
// Reset test environment.
io_service_.reset(new IOService());
flushSentPackets();
// Make another lease query.
lq = makeQueryByClientId(cid2_);
// Process the query.
ASSERT_NO_THROW_LOG(processBulkQuery(lq));
// We should have generated two DHCPLEASEACTIVEs.
Pkt4Ptr active = getSentPacket();
ASSERT_TRUE(active);
ASSERT_EQ(DHCPLEASEACTIVE, active->getType());
EXPECT_FALSE(active->getOption(DHO_STATUS_CODE));
// It is the first packet so it should carry a server id.
checkServerIdOption(active, IOAddress("127.0.0.1"));
// Gather the response for further checks.
Pkt4Ptr active1;
Pkt4Ptr active2;
if (active->getCiaddr() == IOAddress("192.0.2.2")) {
active1 = active;
} else if (active->getCiaddr() == IOAddress("192.0.3.6")) {
active2 = active;
} else {
FAIL() << "unexpected address " << active->getCiaddr().toText();
}
// Get the second DHCPLEASEACTIVE.
active = getSentPacket();
ASSERT_TRUE(active);
ASSERT_EQ(DHCPLEASEACTIVE, active->getType());
EXPECT_FALSE(active->getOption(DHO_STATUS_CODE));
EXPECT_FALSE(active->getOption(DHO_DHCP_SERVER_IDENTIFIER));
// Gather the response.
if (active->getCiaddr() == IOAddress("192.0.2.2")) {
active1 = active;
} else if (active->getCiaddr() == IOAddress("192.0.3.6")) {
active2 = active;
} else {
FAIL() << "unexpected address " << active->getCiaddr().toText();
}
// Last packet is the DHCPLEASEQUERYDONE.
done = getSentPacket();
ASSERT_TRUE(done);
ASSERT_EQ(DHCPLEASEQUERYDONE, done->getType());
EXPECT_FALSE(done->getOption(DHO_STATUS_CODE));
EXPECT_FALSE(done->getOption(DHO_DHCP_SERVER_IDENTIFIER));
// Make sure there are no left over packets.
ASSERT_FALSE(getSentPacket());
// Fully check active_lease response.
ASSERT_TRUE(active1);
EXPECT_FALSE(active1->getOption(DHO_DHCP_AGENT_OPTIONS));
verifyClientId(cid2_, active1);
verifyHWaddr(hwaddr_empty_, active1);
verifyTimeOption(active1, DHO_BASE_TIME, now);
verifyTimeOption(active1, DHO_CLIENT_LAST_TRANSACTION_TIME, 0);
int32_t exp_lft = active_lease->valid_lft_;
verifyTimeOption(active1, DHO_DHCP_LEASE_TIME, exp_lft);
uint32_t exp_t1 = subnet2_->getT1().get();
verifyTimeOption(active1, DHO_DHCP_RENEWAL_TIME, exp_t1);
uint32_t exp_t2 = subnet2_->getT2().get();
verifyTimeOption(active1, DHO_DHCP_REBINDING_TIME, exp_t2);
// Fully check second_lease response.
ASSERT_TRUE(active2);
verifyClientId(cid2_, active2);
verifyHWaddr(hwaddr1_, active2);
verifyTimeOption(active2, DHO_BASE_TIME, now);
verifyTimeOption(active2, DHO_CLIENT_LAST_TRANSACTION_TIME, 1000);
exp_lft = second_lease->valid_lft_;
verifyTimeOption(active2, DHO_DHCP_LEASE_TIME,
exp_lft - (now - second_lease->cltt_));
exp_t1 = static_cast<uint32_t>(round(subnet3_->getT1Percent() * exp_lft));
verifyTimeOption(active2, DHO_DHCP_RENEWAL_TIME,
exp_t1 - (now - second_lease->cltt_));
exp_t2 = static_cast<uint32_t>(round(subnet3_->getT2Percent() * exp_lft));
verifyTimeOption(active2, DHO_DHCP_REBINDING_TIME,
exp_t2 - (now - second_lease->cltt_));
verifyRelayAgentInfo(active2, "0x0104A1B1C1D1");
// Reset test environment.
io_service_.reset(new IOService());
flushSentPackets();
// Try to get too old leases.
lq = makeQueryByClientId(cid2_);
OptionPtr opt(new OptionUint32(Option::V4, DHO_QUERY_END_TIME, now - 2000));
lq->addOption(opt);
ASSERT_NO_THROW_LOG(processBulkQuery(lq));
done = getSentPacket();
ASSERT_TRUE(done);
ASSERT_EQ(DHCPLEASEQUERYDONE, done->getType());
EXPECT_FALSE(done->getOption(DHO_STATUS_CODE));
checkServerIdOption(done, IOAddress("127.0.0.1"));
ASSERT_FALSE(getSentPacket());
// Reset test environment.
io_service_.reset(new IOService());
flushSentPackets();
// Try to get too recent leases.
lq = makeQueryByClientId(cid2_);
opt.reset(new OptionUint32(Option::V4, DHO_QUERY_START_TIME, now + 100));
lq->addOption(opt);
ASSERT_NO_THROW_LOG(processBulkQuery(lq));
done = getSentPacket();
ASSERT_TRUE(done);
ASSERT_EQ(DHCPLEASEQUERYDONE, done->getType());
EXPECT_FALSE(done->getOption(DHO_STATUS_CODE));
checkServerIdOption(done, IOAddress("127.0.0.1"));
ASSERT_FALSE(getSentPacket());
// Reset test environment.
io_service_.reset(new IOService());
flushSentPackets();
now = time(0);
// Final test: try to get only the second_lease.
lq = makeQueryByClientId(cid2_);
opt.reset(new OptionUint32(Option::V4, DHO_QUERY_START_TIME, now - 2000));
lq->addOption(opt);
OptionPtr opt2(new OptionUint32(Option::V4, DHO_QUERY_END_TIME, now - 100));
lq->addOption(opt2);
// Process the query.
ASSERT_NO_THROW_LOG(processBulkQuery(lq));
// We should have generated one DHCPLEASEACTIVE.
active = getSentPacket();
ASSERT_TRUE(active);
ASSERT_EQ(DHCPLEASEACTIVE, active->getType());
EXPECT_FALSE(active->getOption(DHO_STATUS_CODE));
// It is the first packet so it should carry a server id.
checkServerIdOption(active, IOAddress("127.0.0.1"));
// Check it.
EXPECT_EQ(IOAddress("192.0.3.6"), active->getCiaddr());
verifyClientId(cid2_, active);
verifyHWaddr(hwaddr1_, active);
verifyTimeOption(active, DHO_BASE_TIME, now);
verifyTimeOption(active, DHO_CLIENT_LAST_TRANSACTION_TIME,
now - second_lease->cltt_);
exp_lft = second_lease->valid_lft_;
verifyTimeOption(active, DHO_DHCP_LEASE_TIME,
exp_lft - (now - second_lease->cltt_));
exp_t1 = static_cast<uint32_t>(round(subnet3_->getT1Percent() * exp_lft));
verifyTimeOption(active, DHO_DHCP_RENEWAL_TIME,
exp_t1 - (now - second_lease->cltt_));
exp_t2 = static_cast<uint32_t>(round(subnet3_->getT2Percent() * exp_lft));
verifyTimeOption(active, DHO_DHCP_REBINDING_TIME,
exp_t2 - (now - second_lease->cltt_));
verifyRelayAgentInfo(active, "0x0104A1B1C1D1");
// Last packet is the DHCPLEASEQUERYDONE.
done = getSentPacket();
ASSERT_TRUE(done);
ASSERT_EQ(DHCPLEASEQUERYDONE, done->getType());
EXPECT_FALSE(done->getOption(DHO_STATUS_CODE));
EXPECT_FALSE(done->getOption(DHO_DHCP_SERVER_IDENTIFIER));
// Make sure there are no left over packets.
ASSERT_FALSE(getSentPacket());
}
TEST_F(MemfileBulkLeaseQuery4ProcessTest, byClientId) {
verifyByClientId();
}
/// This test exercises queries by hardware address.
template <typename TestLeaseMgrType> void
BaseBulkLeaseQuery4ProcessTest<TestLeaseMgrType>::verifyByHWAddr() {
// For the server id.
createBulkLeaseQueryService();
time_t now = time(0);
// Create some leases.
Lease4Ptr active_lease = addLease(IOAddress("192.0.2.2"), subnet2_,
hwaddr2_, cid2_, now);
Lease4Ptr expired_lease = addLease(IOAddress("192.0.2.3"), subnet2_,
hwaddr1_, cid1_, now - 4000);
Lease4Ptr declined_lease = addLease(IOAddress("192.0.2.4"), subnet2_,
hwaddr1_, cid1_, now, "",
Lease::STATE_DECLINED);
Lease4Ptr reclaimed_lease = addLease(IOAddress("192.0.2.5"), subnet2_,
hwaddr1_, cid1_, now, "",
Lease::STATE_EXPIRED_RECLAIMED);
Lease4Ptr second_lease = addLease(IOAddress("192.0.3.6"), subnet3_,
hwaddr2_, cid2_, now - 1000,
user_context_);
// Make the lease query.
Pkt4Ptr lq = makeQueryByHWAddr(hwaddr1_);
// Process the query.
ASSERT_NO_THROW_LOG(processBulkQuery(lq));
// We should have generated a DHCPLEASEQUERYDONE without a status option.
Pkt4Ptr done = getSentPacket();
ASSERT_TRUE(done);
ASSERT_EQ(DHCPLEASEQUERYDONE, done->getType());
EXPECT_FALSE(done->getOption(DHO_STATUS_CODE));
// It is the first packet so it should carry a server id.
checkServerIdOption(done, IOAddress("127.0.0.1"));
// Make sure there are no left over packets.
ASSERT_FALSE(getSentPacket());
// Reset test environment.
io_service_.reset(new IOService());
flushSentPackets();
// Make another lease query.
lq = makeQueryByHWAddr(hwaddr2_);
// Process the query.
ASSERT_NO_THROW_LOG(processBulkQuery(lq));
// We should have generated two DHCPLEASEACTIVEs.
Pkt4Ptr active = getSentPacket();
ASSERT_TRUE(active);
ASSERT_EQ(DHCPLEASEACTIVE, active->getType());
EXPECT_FALSE(active->getOption(DHO_STATUS_CODE));
// It is the first packet so it should carry a server id.
checkServerIdOption(active, IOAddress("127.0.0.1"));
// Gather the response for further checks.
Pkt4Ptr active1;
Pkt4Ptr active2;
if (active->getCiaddr() == IOAddress("192.0.2.2")) {
active1 = active;
} else if (active->getCiaddr() == IOAddress("192.0.3.6")) {
active2 = active;
} else {
FAIL() << "unexpected address " << active->getCiaddr().toText();
}
// Get the second DHCPLEASEACTIVE.
active = getSentPacket();
ASSERT_TRUE(active);
ASSERT_EQ(DHCPLEASEACTIVE, active->getType());
EXPECT_FALSE(active->getOption(DHO_STATUS_CODE));
EXPECT_FALSE(active->getOption(DHO_DHCP_SERVER_IDENTIFIER));
// Gather the response.
if (active->getCiaddr() == IOAddress("192.0.2.2")) {
active1 = active;
} else if (active->getCiaddr() == IOAddress("192.0.3.6")) {
active2 = active;
} else {
FAIL() << "unexpected address " << active->getCiaddr().toText();
}
// Last packet is the DHCPLEASEQUERYDONE.
done = getSentPacket();
ASSERT_TRUE(done);
ASSERT_EQ(DHCPLEASEQUERYDONE, done->getType());
EXPECT_FALSE(done->getOption(DHO_STATUS_CODE));
EXPECT_FALSE(done->getOption(DHO_DHCP_SERVER_IDENTIFIER));
// Make sure there are no left over packets.
ASSERT_FALSE(getSentPacket());
// Fully check active_lease response.
ASSERT_TRUE(active1);
EXPECT_FALSE(active1->getOption(DHO_DHCP_AGENT_OPTIONS));
verifyClientId(cid2_, active1);
verifyHWaddr(hwaddr2_, active1);
verifyTimeOption(active1, DHO_BASE_TIME, now);
verifyTimeOption(active1, DHO_CLIENT_LAST_TRANSACTION_TIME, 0);
int32_t exp_lft = active_lease->valid_lft_;
verifyTimeOption(active1, DHO_DHCP_LEASE_TIME, exp_lft);
uint32_t exp_t1 = subnet2_->getT1().get();
verifyTimeOption(active1, DHO_DHCP_RENEWAL_TIME, exp_t1);
uint32_t exp_t2 = subnet2_->getT2().get();
verifyTimeOption(active1, DHO_DHCP_REBINDING_TIME, exp_t2);
// Fully check second_lease response.
ASSERT_TRUE(active2);
verifyClientId(cid2_, active2);
verifyHWaddr(hwaddr2_, active2);
verifyTimeOption(active2, DHO_BASE_TIME, now);
verifyTimeOption(active2, DHO_CLIENT_LAST_TRANSACTION_TIME, 1000);
exp_lft = second_lease->valid_lft_;
verifyTimeOption(active2, DHO_DHCP_LEASE_TIME,
exp_lft - (now - second_lease->cltt_));
exp_t1 = static_cast<uint32_t>(round(subnet3_->getT1Percent() * exp_lft));
verifyTimeOption(active2, DHO_DHCP_RENEWAL_TIME,
exp_t1 - (now - second_lease->cltt_));
exp_t2 = static_cast<uint32_t>(round(subnet3_->getT2Percent() * exp_lft));
verifyTimeOption(active2, DHO_DHCP_REBINDING_TIME,
exp_t2 - (now - second_lease->cltt_));
verifyRelayAgentInfo(active2, "0x0104A1B1C1D1");
// Reset test environment.
io_service_.reset(new IOService());
flushSentPackets();
// Try to get too old leases.
lq = makeQueryByHWAddr(hwaddr2_);
OptionPtr opt(new OptionUint32(Option::V4, DHO_QUERY_END_TIME, now - 2000));
lq->addOption(opt);
ASSERT_NO_THROW_LOG(processBulkQuery(lq));
done = getSentPacket();
ASSERT_TRUE(done);
ASSERT_EQ(DHCPLEASEQUERYDONE, done->getType());
EXPECT_FALSE(done->getOption(DHO_STATUS_CODE));
checkServerIdOption(done, IOAddress("127.0.0.1"));
ASSERT_FALSE(getSentPacket());
// Reset test environment.
io_service_.reset(new IOService());
flushSentPackets();
// Try to get too recent leases.
lq = makeQueryByHWAddr(hwaddr2_);
opt.reset(new OptionUint32(Option::V4, DHO_QUERY_START_TIME, now + 100));
lq->addOption(opt);
ASSERT_NO_THROW_LOG(processBulkQuery(lq));
done = getSentPacket();
ASSERT_TRUE(done);
ASSERT_EQ(DHCPLEASEQUERYDONE, done->getType());
EXPECT_FALSE(done->getOption(DHO_STATUS_CODE));
checkServerIdOption(done, IOAddress("127.0.0.1"));
ASSERT_FALSE(getSentPacket());
// Reset test environment.
io_service_.reset(new IOService());
flushSentPackets();
now = time(0);
// Final test: try to get only the second_lease.
lq = makeQueryByHWAddr(hwaddr2_);
opt.reset(new OptionUint32(Option::V4, DHO_QUERY_START_TIME, now - 2000));
lq->addOption(opt);
OptionPtr opt2(new OptionUint32(Option::V4, DHO_QUERY_END_TIME, now - 100));
lq->addOption(opt2);
// Process the query.
ASSERT_NO_THROW_LOG(processBulkQuery(lq));
// We should have generated one DHCPLEASEACTIVE.
active = getSentPacket();
ASSERT_TRUE(active);
ASSERT_EQ(DHCPLEASEACTIVE, active->getType());
EXPECT_FALSE(active->getOption(DHO_STATUS_CODE));
// It is the first packet so it should carry a server id.
checkServerIdOption(active, IOAddress("127.0.0.1"));
// Check it.
EXPECT_EQ(IOAddress("192.0.3.6"), active->getCiaddr());
verifyClientId(cid2_, active);
verifyHWaddr(hwaddr2_, active);
verifyTimeOption(active, DHO_BASE_TIME, now);
verifyTimeOption(active, DHO_CLIENT_LAST_TRANSACTION_TIME,
now - second_lease->cltt_);
exp_lft = second_lease->valid_lft_;
verifyTimeOption(active, DHO_DHCP_LEASE_TIME,
exp_lft - (now - second_lease->cltt_));
exp_t1 = static_cast<uint32_t>(round(subnet3_->getT1Percent() * exp_lft));
verifyTimeOption(active, DHO_DHCP_RENEWAL_TIME,
exp_t1 - (now - second_lease->cltt_));
exp_t2 = static_cast<uint32_t>(round(subnet3_->getT2Percent() * exp_lft));
verifyTimeOption(active, DHO_DHCP_REBINDING_TIME,
exp_t2 - (now - second_lease->cltt_));
verifyRelayAgentInfo(active, "0x0104A1B1C1D1");
// Last packet is the DHCPLEASEQUERYDONE.
done = getSentPacket();
ASSERT_TRUE(done);
ASSERT_EQ(DHCPLEASEQUERYDONE, done->getType());
EXPECT_FALSE(done->getOption(DHO_STATUS_CODE));
EXPECT_FALSE(done->getOption(DHO_DHCP_SERVER_IDENTIFIER));
// Make sure there are no left over packets.
ASSERT_FALSE(getSentPacket());
}
TEST_F(MemfileBulkLeaseQuery4ProcessTest, byHWAddr) {
verifyByHWAddr();
}
/// This test exercises queries by relay id.
template <typename TestLeaseMgrType> void
BaseBulkLeaseQuery4ProcessTest<TestLeaseMgrType>::verifyByRelayId(size_t page_size) {
// For the server id.
createBulkLeaseQueryService(page_size);
time_t now = time(0);
// Create some leases.
Lease4Ptr active_lease = addLease(IOAddress("192.0.2.2"), subnet2_,
HWAddrPtr(), cid2_, now,
user_context2_);
Lease4Ptr expired_lease = addLease(IOAddress("192.0.2.3"), subnet2_,
HWAddrPtr(), cid1_, now - 4000,
user_context1_);
Lease4Ptr declined_lease = addLease(IOAddress("192.0.2.4"), subnet2_,
HWAddrPtr(), cid1_, now,
user_context1_,
Lease::STATE_DECLINED);
Lease4Ptr reclaimed_lease = addLease(IOAddress("192.0.2.5"), subnet2_,
HWAddrPtr(), cid1_, now,
user_context1_,
Lease::STATE_EXPIRED_RECLAIMED);
Lease4Ptr second_lease = addLease(IOAddress("192.0.3.6"), subnet3_,
hwaddr1_, cid2_, now - 1000,
user_context2_);
// Make the lease query.
Pkt4Ptr lq = makeQueryByRelayId(id1_);
// Process the query.
ASSERT_NO_THROW_LOG(processBulkQuery(lq));
// We should have generated a DHCPLEASEQUERYDONE without a status option.
Pkt4Ptr done = getSentPacket();
ASSERT_TRUE(done);
ASSERT_EQ(DHCPLEASEQUERYDONE, done->getType());
EXPECT_FALSE(done->getOption(DHO_STATUS_CODE));
// It is the first packet so it should carry a server id.
checkServerIdOption(done, IOAddress("127.0.0.1"));
// Make sure there are no left over packets.
ASSERT_FALSE(getSentPacket());
// Reset test environment.
io_service_.reset(new IOService());
flushSentPackets();
// Make another lease query.
lq = makeQueryByRelayId(id2_);
// Process the query.
ASSERT_NO_THROW_LOG(processBulkQuery(lq));
// We should have generated two DHCPLEASEACTIVEs.
Pkt4Ptr active = getSentPacket();
ASSERT_TRUE(active);
ASSERT_EQ(DHCPLEASEACTIVE, active->getType());
EXPECT_FALSE(active->getOption(DHO_STATUS_CODE));
// It is the first packet so it should carry a server id.
checkServerIdOption(active, IOAddress("127.0.0.1"));
// Gather the response for further checks.
Pkt4Ptr active1;
Pkt4Ptr active2;
if (active->getCiaddr() == IOAddress("192.0.2.2")) {
active1 = active;
} else if (active->getCiaddr() == IOAddress("192.0.3.6")) {
active2 = active;
} else {
FAIL() << "unexpected address " << active->getCiaddr().toText();
}
// Get the second DHCPLEASEACTIVE.
active = getSentPacket();
ASSERT_TRUE(active);
ASSERT_EQ(DHCPLEASEACTIVE, active->getType());
EXPECT_FALSE(active->getOption(DHO_STATUS_CODE));
EXPECT_FALSE(active->getOption(DHO_DHCP_SERVER_IDENTIFIER));
// Gather the response.
if (active->getCiaddr() == IOAddress("192.0.2.2")) {
active1 = active;
} else if (active->getCiaddr() == IOAddress("192.0.3.6")) {
active2 = active;
} else {
FAIL() << "unexpected address " << active->getCiaddr().toText();
}
// Last packet is the DHCPLEASEQUERYDONE.
done = getSentPacket();
ASSERT_TRUE(done);
ASSERT_EQ(DHCPLEASEQUERYDONE, done->getType());
EXPECT_FALSE(done->getOption(DHO_STATUS_CODE));
EXPECT_FALSE(done->getOption(DHO_DHCP_SERVER_IDENTIFIER));
// Make sure there are no left over packets.
ASSERT_FALSE(getSentPacket());
// Fully check active_lease response.
ASSERT_TRUE(active1);
verifyClientId(cid2_, active1);
verifyHWaddr(hwaddr_empty_, active1);
verifyTimeOption(active1, DHO_BASE_TIME, now);
verifyTimeOption(active1, DHO_CLIENT_LAST_TRANSACTION_TIME, 0);
int32_t exp_lft = active_lease->valid_lft_;
verifyTimeOption(active1, DHO_DHCP_LEASE_TIME, exp_lft);
uint32_t exp_t1 = subnet2_->getT1().get();
verifyTimeOption(active1, DHO_DHCP_RENEWAL_TIME, exp_t1);
uint32_t exp_t2 = subnet2_->getT2().get();
verifyTimeOption(active1, DHO_DHCP_REBINDING_TIME, exp_t2);
verifyRelayAgentInfo(active1, "0x0203AABBCC0C0401020304");
// Fully check second_lease response.
ASSERT_TRUE(active2);
verifyClientId(cid2_, active2);
verifyHWaddr(hwaddr1_, active2);
verifyTimeOption(active2, DHO_BASE_TIME, now);
verifyTimeOption(active2, DHO_CLIENT_LAST_TRANSACTION_TIME, 1000);
exp_lft = second_lease->valid_lft_;
verifyTimeOption(active2, DHO_DHCP_LEASE_TIME,
exp_lft - (now - second_lease->cltt_));
exp_t1 = static_cast<uint32_t>(round(subnet3_->getT1Percent() * exp_lft));
verifyTimeOption(active2, DHO_DHCP_RENEWAL_TIME,
exp_t1 - (now - second_lease->cltt_));
exp_t2 = static_cast<uint32_t>(round(subnet3_->getT2Percent() * exp_lft));
verifyTimeOption(active2, DHO_DHCP_REBINDING_TIME,
exp_t2 - (now - second_lease->cltt_));
verifyRelayAgentInfo(active2, "0x0203AABBCC0C0401020304");
// Reset test environment.
io_service_.reset(new IOService());
flushSentPackets();
// Try to get too old leases.
lq = makeQueryByRelayId(id2_);
OptionPtr opt(new OptionUint32(Option::V4, DHO_QUERY_END_TIME, now - 2000));
lq->addOption(opt);
ASSERT_NO_THROW_LOG(processBulkQuery(lq));
done = getSentPacket();
ASSERT_TRUE(done);
ASSERT_EQ(DHCPLEASEQUERYDONE, done->getType());
EXPECT_FALSE(done->getOption(DHO_STATUS_CODE));
checkServerIdOption(done, IOAddress("127.0.0.1"));
ASSERT_FALSE(getSentPacket());
// Reset test environment.
io_service_.reset(new IOService());
flushSentPackets();
// Try to get too recent leases.
lq = makeQueryByRelayId(id2_);
opt.reset(new OptionUint32(Option::V4, DHO_QUERY_START_TIME, now + 100));
lq->addOption(opt);
ASSERT_NO_THROW_LOG(processBulkQuery(lq));
done = getSentPacket();
ASSERT_TRUE(done);
ASSERT_EQ(DHCPLEASEQUERYDONE, done->getType());
EXPECT_FALSE(done->getOption(DHO_STATUS_CODE));
checkServerIdOption(done, IOAddress("127.0.0.1"));
ASSERT_FALSE(getSentPacket());
// Reset test environment.
io_service_.reset(new IOService());
flushSentPackets();
now = time(0);
// Final test: try to get only the second_lease.
lq = makeQueryByRelayId(id2_);
opt.reset(new OptionUint32(Option::V4, DHO_QUERY_START_TIME, now - 2000));
lq->addOption(opt);
OptionPtr opt2(new OptionUint32(Option::V4, DHO_QUERY_END_TIME, now - 100));
lq->addOption(opt2);
// Process the query.
ASSERT_NO_THROW_LOG(processBulkQuery(lq));
// We should have generated one DHCPLEASEACTIVE.
active = getSentPacket();
ASSERT_TRUE(active);
ASSERT_EQ(DHCPLEASEACTIVE, active->getType());
EXPECT_FALSE(active->getOption(DHO_STATUS_CODE));
// It is the first packet so it should carry a server id.
checkServerIdOption(active, IOAddress("127.0.0.1"));
// Check it.
EXPECT_EQ(IOAddress("192.0.3.6"), active->getCiaddr());
verifyClientId(cid2_, active);
verifyHWaddr(hwaddr1_, active);
verifyTimeOption(active, DHO_BASE_TIME, now);
verifyTimeOption(active, DHO_CLIENT_LAST_TRANSACTION_TIME,
now - second_lease->cltt_);
exp_lft = second_lease->valid_lft_;
verifyTimeOption(active, DHO_DHCP_LEASE_TIME,
exp_lft - (now - second_lease->cltt_));
exp_t1 = static_cast<uint32_t>(round(subnet3_->getT1Percent() * exp_lft));
verifyTimeOption(active, DHO_DHCP_RENEWAL_TIME,
exp_t1 - (now - second_lease->cltt_));
exp_t2 = static_cast<uint32_t>(round(subnet3_->getT2Percent() * exp_lft));
verifyTimeOption(active, DHO_DHCP_REBINDING_TIME,
exp_t2 - (now - second_lease->cltt_));
verifyRelayAgentInfo(active, "0x0203AABBCC0C0401020304");
// Last packet is the DHCPLEASEQUERYDONE.
done = getSentPacket();
ASSERT_TRUE(done);
ASSERT_EQ(DHCPLEASEQUERYDONE, done->getType());
EXPECT_FALSE(done->getOption(DHO_STATUS_CODE));
EXPECT_FALSE(done->getOption(DHO_DHCP_SERVER_IDENTIFIER));
// Make sure there are no left over packets.
ASSERT_FALSE(getSentPacket());
}
TEST_F(MemfileBulkLeaseQuery4ProcessTest, byRelayId100) {
verifyByRelayId(100);
}
TEST_F(MemfileBulkLeaseQuery4ProcessTest, byRelayId2) {
verifyByRelayId(2);
}
TEST_F(MemfileBulkLeaseQuery4ProcessTest, byRelayId1) {
verifyByRelayId(1);
}
/// This test exercises queries by remote id.
template <typename TestLeaseMgrType> void
BaseBulkLeaseQuery4ProcessTest<TestLeaseMgrType>::verifyByRemoteId(size_t page_size) {
// For the server id.
createBulkLeaseQueryService(page_size);
time_t now = time(0);
// Create some leases.
Lease4Ptr active_lease = addLease(IOAddress("192.0.2.2"), subnet2_,
HWAddrPtr(), cid2_, now,
user_context1_);
Lease4Ptr expired_lease = addLease(IOAddress("192.0.2.3"), subnet2_,
HWAddrPtr(), cid1_, now - 4000,
user_context2_);
Lease4Ptr declined_lease = addLease(IOAddress("192.0.2.4"), subnet2_,
HWAddrPtr(), cid1_, now,
user_context2_,
Lease::STATE_DECLINED);
Lease4Ptr reclaimed_lease = addLease(IOAddress("192.0.2.5"), subnet2_,
HWAddrPtr(), cid1_, now,
user_context2_,
Lease::STATE_EXPIRED_RECLAIMED);
Lease4Ptr second_lease = addLease(IOAddress("192.0.3.6"), subnet3_,
hwaddr1_, cid2_, now - 1000,
user_context1_);
// Make the lease query.
Pkt4Ptr lq = makeQueryByRemoteId(id1_);
// Process the query.
ASSERT_NO_THROW_LOG(processBulkQuery(lq));
// We should have generated a DHCPLEASEQUERYDONE without a status option.
Pkt4Ptr done = getSentPacket();
ASSERT_TRUE(done);
ASSERT_EQ(DHCPLEASEQUERYDONE, done->getType());
EXPECT_FALSE(done->getOption(DHO_STATUS_CODE));
// It is the first packet so it should carry a server id.
checkServerIdOption(done, IOAddress("127.0.0.1"));
// Make sure there are no left over packets.
ASSERT_FALSE(getSentPacket());
// Reset test environment.
io_service_.reset(new IOService());
flushSentPackets();
// Make another lease query.
lq = makeQueryByRemoteId(id2_);
// Process the query.
ASSERT_NO_THROW_LOG(processBulkQuery(lq));
// We should have generated two DHCPLEASEACTIVEs.
Pkt4Ptr active = getSentPacket();
ASSERT_TRUE(active);
ASSERT_EQ(DHCPLEASEACTIVE, active->getType());
EXPECT_FALSE(active->getOption(DHO_STATUS_CODE));
// It is the first packet so it should carry a server id.
checkServerIdOption(active, IOAddress("127.0.0.1"));
// Gather the response for further checks.
Pkt4Ptr active1;
Pkt4Ptr active2;
if (active->getCiaddr() == IOAddress("192.0.2.2")) {
active1 = active;
} else if (active->getCiaddr() == IOAddress("192.0.3.6")) {
active2 = active;
} else {
FAIL() << "unexpected address " << active->getCiaddr().toText();
}
// Get the second DHCPLEASEACTIVE.
active = getSentPacket();
ASSERT_TRUE(active);
ASSERT_EQ(DHCPLEASEACTIVE, active->getType());
EXPECT_FALSE(active->getOption(DHO_STATUS_CODE));
EXPECT_FALSE(active->getOption(DHO_DHCP_SERVER_IDENTIFIER));
// Gather the response.
if (active->getCiaddr() == IOAddress("192.0.2.2")) {
active1 = active;
} else if (active->getCiaddr() == IOAddress("192.0.3.6")) {
active2 = active;
} else {
FAIL() << "unexpected address " << active->getCiaddr().toText();
}
// Last packet is the DHCPLEASEQUERYDONE.
done = getSentPacket();
ASSERT_TRUE(done);
ASSERT_EQ(DHCPLEASEQUERYDONE, done->getType());
EXPECT_FALSE(done->getOption(DHO_STATUS_CODE));
EXPECT_FALSE(done->getOption(DHO_DHCP_SERVER_IDENTIFIER));
// Make sure there are no left over packets.
ASSERT_FALSE(getSentPacket());
// Fully check active_lease response.
ASSERT_TRUE(active1);
verifyClientId(cid2_, active1);
verifyHWaddr(hwaddr_empty_, active1);
verifyTimeOption(active1, DHO_BASE_TIME, now);
verifyTimeOption(active1, DHO_CLIENT_LAST_TRANSACTION_TIME, 0);
int32_t exp_lft = active_lease->valid_lft_;
verifyTimeOption(active1, DHO_DHCP_LEASE_TIME, exp_lft);
uint32_t exp_t1 = subnet2_->getT1().get();
verifyTimeOption(active1, DHO_DHCP_RENEWAL_TIME, exp_t1);
uint32_t exp_t2 = subnet2_->getT2().get();
verifyTimeOption(active1, DHO_DHCP_REBINDING_TIME, exp_t2);
verifyRelayAgentInfo(active1, "0x0204010203040C03AABBCC");
// Fully check second_lease response.
ASSERT_TRUE(active2);
verifyClientId(cid2_, active2);
verifyHWaddr(hwaddr1_, active2);
verifyTimeOption(active2, DHO_BASE_TIME, now);
verifyTimeOption(active2, DHO_CLIENT_LAST_TRANSACTION_TIME, 1000);
exp_lft = second_lease->valid_lft_;
verifyTimeOption(active2, DHO_DHCP_LEASE_TIME,
exp_lft - (now - second_lease->cltt_));
exp_t1 = static_cast<uint32_t>(round(subnet3_->getT1Percent() * exp_lft));
verifyTimeOption(active2, DHO_DHCP_RENEWAL_TIME,
exp_t1 - (now - second_lease->cltt_));
exp_t2 = static_cast<uint32_t>(round(subnet3_->getT2Percent() * exp_lft));
verifyTimeOption(active2, DHO_DHCP_REBINDING_TIME,
exp_t2 - (now - second_lease->cltt_));
verifyRelayAgentInfo(active2, "0x0204010203040C03AABBCC");
// Reset test environment.
io_service_.reset(new IOService());
flushSentPackets();
// Try to get too old leases.
lq = makeQueryByRemoteId(id2_);
OptionPtr opt(new OptionUint32(Option::V4, DHO_QUERY_END_TIME, now - 2000));
lq->addOption(opt);
ASSERT_NO_THROW_LOG(processBulkQuery(lq));
done = getSentPacket();
ASSERT_TRUE(done);
ASSERT_EQ(DHCPLEASEQUERYDONE, done->getType());
EXPECT_FALSE(done->getOption(DHO_STATUS_CODE));
checkServerIdOption(done, IOAddress("127.0.0.1"));
ASSERT_FALSE(getSentPacket());
// Reset test environment.
io_service_.reset(new IOService());
flushSentPackets();
// Try to get too recent leases.
lq = makeQueryByRemoteId(id2_);
opt.reset(new OptionUint32(Option::V4, DHO_QUERY_START_TIME, now + 100));
lq->addOption(opt);
ASSERT_NO_THROW_LOG(processBulkQuery(lq));
done = getSentPacket();
ASSERT_TRUE(done);
ASSERT_EQ(DHCPLEASEQUERYDONE, done->getType());
EXPECT_FALSE(done->getOption(DHO_STATUS_CODE));
checkServerIdOption(done, IOAddress("127.0.0.1"));
ASSERT_FALSE(getSentPacket());
// Reset test environment.
io_service_.reset(new IOService());
flushSentPackets();
now = time(0);
// Final test: try to get only the second_lease.
lq = makeQueryByRemoteId(id2_);
opt.reset(new OptionUint32(Option::V4, DHO_QUERY_START_TIME, now - 2000));
lq->addOption(opt);
OptionPtr opt2(new OptionUint32(Option::V4, DHO_QUERY_END_TIME, now - 100));
lq->addOption(opt2);
// Process the query.
ASSERT_NO_THROW_LOG(processBulkQuery(lq));
// We should have generated one DHCPLEASEACTIVE.
active = getSentPacket();
ASSERT_TRUE(active);
ASSERT_EQ(DHCPLEASEACTIVE, active->getType());
EXPECT_FALSE(active->getOption(DHO_STATUS_CODE));
// It is the first packet so it should carry a server id.
checkServerIdOption(active, IOAddress("127.0.0.1"));
// Check it.
EXPECT_EQ(IOAddress("192.0.3.6"), active->getCiaddr());
verifyClientId(cid2_, active);
verifyHWaddr(hwaddr1_, active);
verifyTimeOption(active, DHO_BASE_TIME, now);
verifyTimeOption(active, DHO_CLIENT_LAST_TRANSACTION_TIME,
now - second_lease->cltt_);
exp_lft = second_lease->valid_lft_;
verifyTimeOption(active, DHO_DHCP_LEASE_TIME,
exp_lft - (now - second_lease->cltt_));
exp_t1 = static_cast<uint32_t>(round(subnet3_->getT1Percent() * exp_lft));
verifyTimeOption(active, DHO_DHCP_RENEWAL_TIME,
exp_t1 - (now - second_lease->cltt_));
exp_t2 = static_cast<uint32_t>(round(subnet3_->getT2Percent() * exp_lft));
verifyTimeOption(active, DHO_DHCP_REBINDING_TIME,
exp_t2 - (now - second_lease->cltt_));
verifyRelayAgentInfo(active, "0x0204010203040C03AABBCC");
// Last packet is the DHCPLEASEQUERYDONE.
done = getSentPacket();
ASSERT_TRUE(done);
ASSERT_EQ(DHCPLEASEQUERYDONE, done->getType());
EXPECT_FALSE(done->getOption(DHO_STATUS_CODE));
EXPECT_FALSE(done->getOption(DHO_DHCP_SERVER_IDENTIFIER));
// Make sure there are no left over packets.
ASSERT_FALSE(getSentPacket());
}
TEST_F(MemfileBulkLeaseQuery4ProcessTest, byRemoteId100) {
verifyByRemoteId(100);
}
TEST_F(MemfileBulkLeaseQuery4ProcessTest, byRemoteId2) {
verifyByRemoteId(2);
}
TEST_F(MemfileBulkLeaseQuery4ProcessTest, byRemoteId1) {
verifyByRemoteId(1);
}
#ifdef HAVE_MYSQL
/// @brief MySQL test fixture.
class MySQLBulkLeaseQuery4ProcessTest : public
BaseBulkLeaseQuery4ProcessTest<MySQLTestLeaseMgr> {
};
TEST_F(MySQLBulkLeaseQuery4ProcessTest, byClientId) {
verifyByClientId();
}
TEST_F(MySQLBulkLeaseQuery4ProcessTest, byHWAddr) {
verifyByHWAddr();
}
TEST_F(MySQLBulkLeaseQuery4ProcessTest, byRelayId100) {
verifyByRelayId(100);
}
TEST_F(MySQLBulkLeaseQuery4ProcessTest, byRelayId2) {
verifyByRelayId(2);
}
TEST_F(MySQLBulkLeaseQuery4ProcessTest, byRelayId1) {
verifyByRelayId(1);
}
TEST_F(MySQLBulkLeaseQuery4ProcessTest, byRemoteId100) {
verifyByRemoteId(100);
}
TEST_F(MySQLBulkLeaseQuery4ProcessTest, byRemoteId2) {
verifyByRemoteId(2);
}
TEST_F(MySQLBulkLeaseQuery4ProcessTest, byRemoteId1) {
verifyByRemoteId(1);
}
#endif
#ifdef HAVE_PGSQL
/// @brief PostgreSQL test fixture.
class PgSQLBulkLeaseQuery4ProcessTest : public
BaseBulkLeaseQuery4ProcessTest<PgSQLTestLeaseMgr> {
};
TEST_F(PgSQLBulkLeaseQuery4ProcessTest, byClientId) {
verifyByClientId();
}
TEST_F(PgSQLBulkLeaseQuery4ProcessTest, byHWAddr) {
verifyByHWAddr();
}
TEST_F(PgSQLBulkLeaseQuery4ProcessTest, byRelayId100) {
verifyByRelayId(100);
}
TEST_F(PgSQLBulkLeaseQuery4ProcessTest, byRelayId2) {
verifyByRelayId(2);
}
TEST_F(PgSQLBulkLeaseQuery4ProcessTest, byRelayId1) {
verifyByRelayId(1);
}
TEST_F(PgSQLBulkLeaseQuery4ProcessTest, byRemoteId100) {
verifyByRemoteId(100);
}
TEST_F(PgSQLBulkLeaseQuery4ProcessTest, byRemoteId2) {
verifyByRemoteId(2);
}
TEST_F(PgSQLBulkLeaseQuery4ProcessTest, byRemoteId1) {
verifyByRemoteId(1);
}
#endif
}
|