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
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541 | // Copyright (C) 2014-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <config.h>
#include <cc/data.h>
#include <dhcp/classify.h>
#include <dhcp/libdhcp++.h>
#include <dhcp/option_custom.h>
#include <dhcp/option_definition.h>
#include <dhcp/option_space.h>
#include <dhcp/option_string.h>
#include <dhcp/testutils/iface_mgr_test_config.h>
#include <dhcpsrv/cfgmgr.h>
#include <dhcpsrv/lease_mgr_factory.h>
#include <dhcpsrv/parsers/dhcp_parsers.h>
#include <dhcpsrv/cfg_shared_networks.h>
#include <dhcpsrv/cfg_subnets4.h>
#include <dhcpsrv/iterative_allocator.h>
#include <dhcpsrv/shared_network.h>
#include <dhcpsrv/subnet.h>
#include <dhcpsrv/subnet_id.h>
#include <dhcpsrv/subnet_selector.h>
#include <dhcpsrv/cfg_hosts.h>
#include <stats/stats_mgr.h>
#include <testutils/gtest_utils.h>
#include <testutils/log_utils.h>
#include <testutils/test_to_element.h>
#include <util/doubles.h>
#include <boost/range/adaptor/reversed.hpp><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <gtest/gtest.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <vector><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
using namespace isc;
using namespace isc::asiolink;
using namespace isc::data;
using namespace isc::dhcp;
using namespace isc::dhcp::test;
using namespace isc::stats;
using namespace isc::test;
using namespace isc::util;
namespace {
/// @brief An allocator recording calls to @c initAfterConfigure.
class InitRecordingAllocator : public IterativeAllocator {
public:
/// @brief Constructor.
///
/// @param type specifies the type of allocated leases.
/// @param subnet weak pointer to the subnet owning the allocator.
InitRecordingAllocator(Lease::Type type, const WeakSubnetPtr& subnet)
: IterativeAllocator(type, subnet), callcount_(0) {
}
/// @brief Increases the call count of this function.
///
/// The call count can be later examined to check whether or not
/// the function was called.
virtual void initAfterConfigureInternal() {
++callcount_;
};
/// @brief Call count of the @c initAllocatorsAfterConfigure.
int callcount_;
};
/// @brief Verifies that a set of subnets contains a given a subnet
///
/// @param cfg_subnets set of subnets in which to look
/// @param prefix prefix of the target subnet
/// @param exp_subnet_id expected id of the target subnet
/// @param exp_valid expected valid lifetime of the subnet
/// @param exp_network pointer to the subnet's shared-network (if one)
void checkMergedSubnet(CfgSubnets4& cfg_subnets,
const std::string& prefix,
const SubnetID exp_subnet_id,
int exp_valid,
SharedNetwork4Ptr exp_network) {
// Look for the network by prefix.
auto subnet = cfg_subnets.getByPrefix(prefix);
ASSERT_TRUE(subnet) << "subnet: " << prefix << " not found";
// Make sure we have the one we expect.
ASSERT_EQ(exp_subnet_id, subnet->getID()) << "subnet ID is wrong";
ASSERT_EQ(exp_valid, subnet->getValid().get()) << "subnet valid time is wrong";
SharedNetwork4Ptr shared_network;
subnet->getSharedNetwork(shared_network);
if (exp_network) {
ASSERT_TRUE(shared_network)
<< " expected network: " << exp_network->getName() << " not found";
ASSERT_TRUE(shared_network == exp_network) << " networks do no match";
} else {
ASSERT_FALSE(shared_network) << " unexpected network assignment: "
<< shared_network->getName();
}
}
// This test verifies that specific subnet can be retrieved by specifying
// subnet identifier or subnet prefix.
TEST(CfgSubnets4Test, getSpecificSubnet) {<--- syntax error
CfgSubnets4 cfg;
// Create 3 subnets.
Subnet4Ptr subnet1(new Subnet4(IOAddress("192.0.2.0"),
26, 1, 2, 3, SubnetID(5)));
Subnet4Ptr subnet2(new Subnet4(IOAddress("192.0.2.64"),
26, 1, 2, 3, SubnetID(8)));
Subnet4Ptr subnet3(new Subnet4(IOAddress("192.0.2.128"),
26, 1, 2, 3, SubnetID(10)));
// Store the subnets in a vector to make it possible to loop over
// all configured subnets.
std::vector<Subnet4Ptr> subnets;
subnets.push_back(subnet1);
subnets.push_back(subnet2);
subnets.push_back(subnet3);
// Add all subnets to the configuration.
for (auto const& subnet : subnets) {
ASSERT_NO_THROW(cfg.add(subnet)) << "failed to add subnet with id: "
<< subnet->getID();
}
// Iterate over all subnets and make sure they can be retrieved by
// subnet identifier.
for (auto const& subnet : boost::adaptors::reverse(subnets)) {
ConstSubnet4Ptr subnet_returned = cfg.getBySubnetId(subnet->getID());
ASSERT_TRUE(subnet_returned) << "failed to return subnet with id: "
<< subnet->getID();
EXPECT_EQ(subnet->getID(), subnet_returned->getID());
EXPECT_EQ(subnet->toText(), subnet_returned->toText());
}
// Repeat the previous test, but this time retrieve subnets by their
// prefixes.
for (auto const& subnet : boost::adaptors::reverse(subnets)) {
ConstSubnet4Ptr subnet_returned = cfg.getByPrefix(subnet->toText());
ASSERT_TRUE(subnet_returned) << "failed to return subnet with id: "
<< subnet->getID();
EXPECT_EQ(subnet->getID(), subnet_returned->getID());
EXPECT_EQ(subnet->toText(), subnet_returned->toText());
}
// Make sure that null pointers are returned for non-existing subnets.
EXPECT_FALSE(cfg.getBySubnetId(SubnetID(123)));
EXPECT_FALSE(cfg.getByPrefix("10.20.30.0/29"));
}
// This test verifies that a single subnet can be removed from the configuration.
TEST(CfgSubnets4Test, deleteSubnet) {
CfgSubnets4 cfg;
// Create 3 subnets.
Subnet4Ptr subnet1(new Subnet4(IOAddress("192.0.2.0"),
26, 1, 2, 3, SubnetID(5)));
Subnet4Ptr subnet2(new Subnet4(IOAddress("192.0.3.0"),
26, 1, 2, 3, SubnetID(8)));
Subnet4Ptr subnet3(new Subnet4(IOAddress("192.0.4.0"),
26, 1, 2, 3, SubnetID(10)));
ASSERT_NO_THROW(cfg.add(subnet1));
ASSERT_NO_THROW(cfg.add(subnet2));
ASSERT_NO_THROW(cfg.add(subnet3));
// There should be three subnets.
ASSERT_EQ(3, cfg.getAll()->size());
// We're going to remove the subnet #2. Let's make sure it exists before
// we remove it.
ASSERT_TRUE(cfg.getByPrefix("192.0.3.0/26"));
// Remove the subnet and make sure it is gone.
ASSERT_NO_THROW(cfg.del(subnet2));
ASSERT_EQ(2, cfg.getAll()->size());
EXPECT_FALSE(cfg.getByPrefix("192.0.3.0/26"));
// Remove another subnet by ID.
ASSERT_NO_THROW(cfg.del(subnet1->getID()));
ASSERT_EQ(1, cfg.getAll()->size());
EXPECT_FALSE(cfg.getByPrefix("192.0.2.0/26"));
}
// This test verifies that replace a subnet works as expected.
TEST(CfgSubnets4Test, replaceSubnet) {
CfgSubnets4 cfg;
// Create 3 subnets.
Subnet4Ptr subnet1(new Subnet4(IOAddress("192.0.1.0"),
26, 1, 2, 100, SubnetID(10)));
Subnet4Ptr subnet2(new Subnet4(IOAddress("192.0.2.0"),
26, 1, 2, 100, SubnetID(2)));
Subnet4Ptr subnet3(new Subnet4(IOAddress("192.0.3.0"),
26, 1, 2, 100, SubnetID(13)));
ASSERT_NO_THROW(cfg.add(subnet1));
ASSERT_NO_THROW(cfg.add(subnet2));
ASSERT_NO_THROW(cfg.add(subnet3));
// There should be three subnets.
ASSERT_EQ(3, cfg.getAll()->size());
// We're going to replace the subnet #2. Let's make sure it exists before
// we replace it.
ASSERT_TRUE(cfg.getByPrefix("192.0.3.0/26"));
// Replace the subnet and make sure it was updated.
Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.2.0"),
26, 10, 20, 1000, SubnetID(2)));
Subnet4Ptr replaced = cfg.replace(subnet);
ASSERT_TRUE(replaced);
EXPECT_TRUE(replaced == subnet2);
ASSERT_EQ(3, cfg.getAll()->size());
Subnet4Ptr returned = cfg.getSubnet(SubnetID(2));
ASSERT_TRUE(returned);
EXPECT_TRUE(returned == subnet);
// Restore.
replaced = cfg.replace(replaced);
ASSERT_TRUE(replaced);
EXPECT_TRUE(replaced == subnet);
ASSERT_EQ(3, cfg.getAll()->size());
returned = cfg.getSubnet(SubnetID(2));
ASSERT_TRUE(returned);
EXPECT_TRUE(returned == subnet2);
// Prefix conflict returns null.
subnet.reset(new Subnet4(IOAddress("192.0.3.0"),
26, 10, 20, 1000, SubnetID(2)));
replaced = cfg.replace(subnet);
EXPECT_FALSE(replaced);
returned = cfg.getSubnet(SubnetID(2));
ASSERT_TRUE(returned);
EXPECT_TRUE(returned == subnet2);
// Changing prefix works even it is highly not recommended.
subnet.reset(new Subnet4(IOAddress("192.0.10.0"),
26, 10, 20, 1000, SubnetID(2)));
replaced = cfg.replace(subnet);
ASSERT_TRUE(replaced);
EXPECT_TRUE(replaced == subnet2);
returned = cfg.getSubnet(SubnetID(2));
ASSERT_TRUE(returned);
EXPECT_TRUE(returned == subnet);
}
// This test verifies that subnets configuration is properly merged.
TEST(CfgSubnets4Test, mergeSubnets) {
// Create custom options dictionary for testing merge. We're keeping it
// simple because they are more rigorous tests elsewhere.
CfgOptionDefPtr cfg_def(new CfgOptionDef());
cfg_def->add((OptionDefinitionPtr(new OptionDefinition("one", 1, "isc", "string"))));
Subnet4Ptr subnet1(new Subnet4(IOAddress("192.0.1.0"),
26, 1, 2, 100, SubnetID(1)));
Subnet4Ptr subnet2(new Subnet4(IOAddress("192.0.2.0"),
26, 1, 2, 100, SubnetID(2)));
Subnet4Ptr subnet3(new Subnet4(IOAddress("192.0.3.0"),
26, 1, 2, 100, SubnetID(3)));
Subnet4Ptr subnet4(new Subnet4(IOAddress("192.0.4.0"),
26, 1, 2, 100, SubnetID(4)));
// Create the "existing" list of shared networks
CfgSharedNetworks4Ptr networks(new CfgSharedNetworks4());
SharedNetwork4Ptr shared_network1(new SharedNetwork4("shared-network1"));
networks->add(shared_network1);
SharedNetwork4Ptr shared_network2(new SharedNetwork4("shared-network2"));
networks->add(shared_network2);
// Empty network pointer.
SharedNetwork4Ptr no_network;
// Add Subnets 1, 2 and 4 to shared networks.
ASSERT_NO_THROW(shared_network1->add(subnet1));
ASSERT_NO_THROW(shared_network2->add(subnet2));
ASSERT_NO_THROW(shared_network2->add(subnet4));
// Create our "existing" configured subnets.
CfgSubnets4 cfg_to;
ASSERT_NO_THROW(cfg_to.add(subnet1));
ASSERT_NO_THROW(cfg_to.add(subnet2));
ASSERT_NO_THROW(cfg_to.add(subnet3));
ASSERT_NO_THROW(cfg_to.add(subnet4));
// Merge in an "empty" config. Should have the original config,
// still intact.
CfgSubnets4 cfg_from;
ASSERT_NO_THROW(cfg_to.merge(cfg_def, networks, cfg_from));
// We should have all four subnets, with no changes.
ASSERT_EQ(4, cfg_to.getAll()->size());
// Should be no changes to the configuration.
ASSERT_NO_FATAL_FAILURE(checkMergedSubnet(cfg_to, "192.0.1.0/26",
SubnetID(1), 100, shared_network1));
ASSERT_NO_FATAL_FAILURE(checkMergedSubnet(cfg_to, "192.0.2.0/26",
SubnetID(2), 100, shared_network2));
ASSERT_NO_FATAL_FAILURE(checkMergedSubnet(cfg_to, "192.0.3.0/26",
SubnetID(3), 100, no_network));
ASSERT_NO_FATAL_FAILURE(checkMergedSubnet(cfg_to, "192.0.4.0/26",
SubnetID(4), 100, shared_network2));
// Fill cfg_from configuration with subnets.
// subnet 1b updates subnet 1 but leaves it in network 1 with the same ID.
Subnet4Ptr subnet1b(new Subnet4(IOAddress("192.0.10.0"),
26, 2, 3, 400, SubnetID(1)));
subnet1b->setSharedNetworkName("shared-network1");
// Add generic option 1 to subnet 1b.
std::string value("Yay!");
OptionPtr option(new Option(Option::V4, 1));
option->setData(value.begin(), value.end());
ASSERT_NO_THROW(subnet1b->getCfgOption()->add(option, false, false, "isc"));
// subnet 3b updates subnet 3 with different ID and removes it
// from network 2
Subnet4Ptr subnet3b(new Subnet4(IOAddress("192.0.3.0"),
26, 3, 4, 500, SubnetID(30)));
// Now Add generic option 1 to subnet 3b.
value = "Team!";
option.reset(new Option(Option::V4, 1));
option->setData(value.begin(), value.end());
ASSERT_NO_THROW(subnet3b->getCfgOption()->add(option, false, false, "isc"));
// subnet 4b updates subnet 4 and moves it from network2 to network 1
Subnet4Ptr subnet4b(new Subnet4(IOAddress("192.0.4.0"),
26, 3, 4, 500, SubnetID(4)));
subnet4b->setSharedNetworkName("shared-network1");
// subnet 5 is new and belongs to network 2
// Has two pools both with an option 1
Subnet4Ptr subnet5(new Subnet4(IOAddress("192.0.5.0"),
26, 1, 2, 300, SubnetID(5)));
subnet5->setSharedNetworkName("shared-network2");
// Add pool 1
Pool4Ptr pool(new Pool4(IOAddress("192.0.5.10"), IOAddress("192.0.5.20")));
value = "POOLS";
option.reset(new Option(Option::V4, 1));
option->setData(value.begin(), value.end());
ASSERT_NO_THROW(pool->getCfgOption()->add(option, false, false, "isc"));
subnet5->addPool(pool);
// Add pool 2
pool.reset(new Pool4(IOAddress("192.0.5.30"), IOAddress("192.0.5.40")));
value ="RULE!";
option.reset(new Option(Option::V4, 1));
option->setData(value.begin(), value.end());
ASSERT_NO_THROW(pool->getCfgOption()->add(option, false, false, "isc"));
subnet5->addPool(pool);
// Add subnets to the merge from config.
ASSERT_NO_THROW(cfg_from.add(subnet1b));
ASSERT_NO_THROW(cfg_from.add(subnet3b));
ASSERT_NO_THROW(cfg_from.add(subnet4b));
ASSERT_NO_THROW(cfg_from.add(subnet5));
// Merge again.
ASSERT_NO_THROW(cfg_to.merge(cfg_def, networks, cfg_from));
ASSERT_EQ(5, cfg_to.getAll()->size());
// The subnet1 should be replaced by subnet1b.
ASSERT_NO_FATAL_FAILURE(checkMergedSubnet(cfg_to, "192.0.10.0/26",
SubnetID(1), 400, shared_network1));
// Let's verify that our option is there and populated correctly.
auto subnet = cfg_to.getByPrefix("192.0.10.0/26");
auto desc = subnet->getCfgOption()->get("isc", 1);
ASSERT_TRUE(desc.option_);
OptionStringPtr opstr = boost::dynamic_pointer_cast<OptionString>(desc.option_);
ASSERT_TRUE(opstr);
EXPECT_EQ("Yay!", opstr->getValue());
// The subnet2 should not be affected because it was not present.
ASSERT_NO_FATAL_FAILURE(checkMergedSubnet(cfg_to, "192.0.2.0/26",
SubnetID(2), 100, shared_network2));
// subnet3 should be replaced by subnet3b and no longer assigned to a network.
ASSERT_NO_FATAL_FAILURE(checkMergedSubnet(cfg_to, "192.0.3.0/26",
SubnetID(30), 500, no_network));
// Let's verify that our option is there and populated correctly.
subnet = cfg_to.getByPrefix("192.0.3.0/26");
desc = subnet->getCfgOption()->get("isc", 1);
ASSERT_TRUE(desc.option_);
opstr = boost::dynamic_pointer_cast<OptionString>(desc.option_);
ASSERT_TRUE(opstr);
EXPECT_EQ("Team!", opstr->getValue());
EXPECT_TRUE(subnet->getCfgOption()->isEncapsulated());
// subnet4 should be replaced by subnet4b and moved to network1.
ASSERT_NO_FATAL_FAILURE(checkMergedSubnet(cfg_to, "192.0.4.0/26",
SubnetID(4), 500, shared_network1));
// subnet5 should have been added to configuration.
ASSERT_NO_FATAL_FAILURE(checkMergedSubnet(cfg_to, "192.0.5.0/26",
SubnetID(5), 300, shared_network2));
// Let's verify that both pools have the proper options.
subnet = cfg_to.getByPrefix("192.0.5.0/26");
const PoolPtr merged_pool = subnet->getPool(Lease::TYPE_V4, IOAddress("192.0.5.10"));
ASSERT_TRUE(merged_pool);
desc = merged_pool->getCfgOption()->get("isc", 1);
opstr = boost::dynamic_pointer_cast<OptionString>(desc.option_);
ASSERT_TRUE(opstr);
EXPECT_EQ("POOLS", opstr->getValue());
EXPECT_TRUE(merged_pool->getCfgOption()->isEncapsulated());
const PoolPtr merged_pool2 = subnet->getPool(Lease::TYPE_V4, IOAddress("192.0.5.30"));
ASSERT_TRUE(merged_pool2);
desc = merged_pool2->getCfgOption()->get("isc", 1);
opstr = boost::dynamic_pointer_cast<OptionString>(desc.option_);
ASSERT_TRUE(opstr);
EXPECT_EQ("RULE!", opstr->getValue());
EXPECT_TRUE(merged_pool2->getCfgOption()->isEncapsulated());
}
// This test verifies that it is possible to retrieve a subnet using an
// IP address.
TEST(CfgSubnets4Test, selectSubnetByCiaddr) {
CfgSubnets4 cfg;
// Create 3 subnets.
Subnet4Ptr subnet1(new Subnet4(IOAddress("192.0.2.0"),
26, 1, 2, 3, SubnetID(1)));
Subnet4Ptr subnet2(new Subnet4(IOAddress("192.0.2.64"),
26, 1, 2, 3, SubnetID(2)));
Subnet4Ptr subnet3(new Subnet4(IOAddress("192.0.2.128"),
26, 1, 2, 3, SubnetID(3)));
// Make sure that initially the subnets don't exist.
SubnetSelector selector;
selector.ciaddr_ = IOAddress("192.0.2.0");
// Set some unicast local address to simulate a Renew.
selector.local_address_ = IOAddress("10.0.0.100");
ASSERT_FALSE(cfg.selectSubnet(selector));
// Add one subnet and make sure it is returned.
cfg.add(subnet1);
selector.ciaddr_ = IOAddress("192.0.2.63");
EXPECT_EQ(subnet1, cfg.selectSubnet(selector));
// Add all other subnets.
cfg.add(subnet2);
cfg.add(subnet3);
// Make sure they are returned for the appropriate addresses.
selector.ciaddr_ = IOAddress("192.0.2.15");
EXPECT_EQ(subnet1, cfg.selectSubnet(selector));
selector.ciaddr_ = IOAddress("192.0.2.85");
EXPECT_EQ(subnet2, cfg.selectSubnet(selector));
selector.ciaddr_ = IOAddress("192.0.2.191");
EXPECT_EQ(subnet3, cfg.selectSubnet(selector));
// Also, make sure that the NULL pointer is returned if the subnet
// cannot be found.
selector.ciaddr_ = IOAddress("192.0.2.192");
EXPECT_FALSE(cfg.selectSubnet(selector));
}
// This test verifies that it is possible to select a subnet by
// matching an interface name.
TEST(CfgSubnets4Test, selectSubnetByIface) {
// The IfaceMgrTestConfig object initializes fake interfaces:
// eth0, eth1 and lo on the configuration manager. The CfgSubnets4
// object uses interface names to select the appropriate subnet.
IfaceMgrTestConfig config(true);
CfgSubnets4 cfg;
// Create 3 subnets.
Subnet4Ptr subnet1(new Subnet4(IOAddress("192.0.2.0"),
26, 1, 2, 3, SubnetID(1)));
Subnet4Ptr subnet2(new Subnet4(IOAddress("192.0.2.64"),
26, 1, 2, 3, SubnetID(2)));
Subnet4Ptr subnet3(new Subnet4(IOAddress("192.0.2.128"),
26, 1, 2, 3, SubnetID(3)));
// No interface defined for subnet1
subnet2->setIface("lo");
subnet3->setIface("eth1");
cfg.add(subnet1);
cfg.add(subnet2);
cfg.add(subnet3);
// Make sure that initially the subnets don't exist.
SubnetSelector selector;
// Set an interface to a name that is not defined in the config.
// Subnet selection should fail.
selector.iface_name_ = "eth0";
ASSERT_FALSE(cfg.selectSubnet(selector));
// Now select an interface name that matches. Selection should succeed
// and return subnet3.
selector.iface_name_ = "eth1";
ConstSubnet4Ptr selected = cfg.selectSubnet(selector);
ASSERT_TRUE(selected);
EXPECT_EQ(subnet3, selected);
}
// This test verifies that it is possible to select subnet by interface
// name specified on the shared network level.
TEST(CfgSubnets4Test, selectSharedNetworkByIface) {
// The IfaceMgrTestConfig object initializes fake interfaces:
// eth0, eth1 and lo on the configuration manager. The CfgSubnets4
// object uses interface names to select the appropriate subnet.
IfaceMgrTestConfig config(true);
CfgSubnets4 cfg;
// Create 3 subnets.
Subnet4Ptr subnet1(new Subnet4(IOAddress("172.16.2.0"), 24, 1, 2, 3,
SubnetID(1)));
Subnet4Ptr subnet2(new Subnet4(IOAddress("10.1.2.0"), 24, 1, 2, 3,
SubnetID(2)));
Subnet4Ptr subnet3(new Subnet4(IOAddress("192.3.4.0"), 24, 1, 2, 3,
SubnetID(3)));
subnet2->setIface("lo");
cfg.add(subnet1);
cfg.add(subnet2);
cfg.add(subnet3);
SharedNetwork4Ptr network(new SharedNetwork4("network_eth1"));
network->setIface("eth1");
ASSERT_NO_THROW(network->add(subnet1));
ASSERT_NO_THROW(network->add(subnet2));
// Make sure that initially the subnets don't exist.
SubnetSelector selector;
// Set an interface to a name that is not defined in the config.
// Subnet selection should fail.
selector.iface_name_ = "eth0";
ASSERT_FALSE(cfg.selectSubnet(selector));
// Now select an interface name that matches. Selection should succeed
// and return subnet3.
selector.iface_name_ = "eth1";
ConstSubnet4Ptr selected = cfg.selectSubnet(selector);
ASSERT_TRUE(selected);
SharedNetwork4Ptr network_returned;
selected->getSharedNetwork(network_returned);
ASSERT_TRUE(network_returned);
EXPECT_EQ(network, network_returned);
const Subnet4SimpleCollection* subnets_eth1 =
network_returned->getAllSubnets();
EXPECT_EQ(2, subnets_eth1->size());
ASSERT_TRUE(network_returned->getSubnet(SubnetID(1)));
ASSERT_TRUE(network_returned->getSubnet(SubnetID(2)));
// Make sure that it is still possible to select subnet2 which is
// outside of a shared network.
selector.iface_name_ = "lo";
selected = cfg.selectSubnet(selector);
ASSERT_TRUE(selected);
EXPECT_EQ(2, selected->getID());
// Try selecting by eth1 again, but this time set subnet specific
// interface name to eth0. Subnet selection should fail.
selector.iface_name_ = "eth1";
subnet1->setIface("eth0");
subnet3->setIface("eth0");
selected = cfg.selectSubnet(selector);
ASSERT_FALSE(selected);
// It should be possible to select by eth0, though.
selector.iface_name_ = "eth0";
selected = cfg.selectSubnet(selector);
ASSERT_TRUE(selected);
EXPECT_EQ(subnet1, selected);
}
// This test verifies that when the classification information is specified for
// subnets, the proper subnets are returned by the subnet configuration.
TEST(CfgSubnets4Test, selectSubnetByClasses) {
CfgSubnets4 cfg;
// Create 3 subnets.
Subnet4Ptr subnet1(new Subnet4(IOAddress("192.0.2.0"),
26, 1, 2, 3, SubnetID(1)));
Subnet4Ptr subnet2(new Subnet4(IOAddress("192.0.2.64"),
26, 1, 2, 3, SubnetID(2)));
Subnet4Ptr subnet3(new Subnet4(IOAddress("192.0.2.128"),
26, 1, 2, 3, SubnetID(3)));
// Add them to the configuration.
cfg.add(subnet1);
cfg.add(subnet2);
cfg.add(subnet3);
SubnetSelector selector;
selector.local_address_ = IOAddress("10.0.0.10");
selector.ciaddr_ = IOAddress("192.0.2.5");
EXPECT_EQ(subnet1, cfg.selectSubnet(selector));
selector.ciaddr_ = IOAddress("192.0.2.70");
EXPECT_EQ(subnet2, cfg.selectSubnet(selector));
selector.ciaddr_ = IOAddress("192.0.2.130");
EXPECT_EQ(subnet3, cfg.selectSubnet(selector));
ClientClasses client_classes;
client_classes.insert("bar");
selector.client_classes_ = client_classes;
// There are no class restrictions defined, so everything should work
// as before
selector.ciaddr_ = IOAddress("192.0.2.5");
EXPECT_EQ(subnet1, cfg.selectSubnet(selector));
selector.ciaddr_ = IOAddress("192.0.2.70");
EXPECT_EQ(subnet2, cfg.selectSubnet(selector));
selector.ciaddr_ = IOAddress("192.0.2.130");
EXPECT_EQ(subnet3, cfg.selectSubnet(selector));
// Now let's add client class restrictions.
subnet1->allowClientClass("foo"); // Serve here only clients from foo class
subnet2->allowClientClass("bar"); // Serve here only clients from bar class
subnet3->allowClientClass("baz"); // Serve here only clients from baz class
// The same check as above should result in client being served only in
// bar class, i.e. subnet2.
selector.ciaddr_ = IOAddress("192.0.2.5");
EXPECT_FALSE(cfg.selectSubnet(selector));
selector.ciaddr_ = IOAddress("192.0.2.70");
EXPECT_EQ(subnet2, cfg.selectSubnet(selector));
selector.ciaddr_ = IOAddress("192.0.2.130");
EXPECT_FALSE(cfg.selectSubnet(selector));
// Now let's check that client with wrong class is not supported.
client_classes.clear();
client_classes.insert("some_other_class");
selector.client_classes_ = client_classes;
selector.ciaddr_ = IOAddress("192.0.2.5");
EXPECT_FALSE(cfg.selectSubnet(selector));
selector.ciaddr_ = IOAddress("192.0.2.70");
EXPECT_FALSE(cfg.selectSubnet(selector));
selector.ciaddr_ = IOAddress("192.0.2.130");
EXPECT_FALSE(cfg.selectSubnet(selector));
// Finally, let's check that client without any classes is not supported.
client_classes.clear();
selector.ciaddr_ = IOAddress("192.0.2.5");
EXPECT_FALSE(cfg.selectSubnet(selector));
selector.ciaddr_ = IOAddress("192.0.2.70");
EXPECT_FALSE(cfg.selectSubnet(selector));
selector.ciaddr_ = IOAddress("192.0.2.130");
EXPECT_FALSE(cfg.selectSubnet(selector));
}
// This test verifies that shared network can be selected based on client
// classification.
TEST(CfgSubnets4Test, selectSharedNetworkByClasses) {
IfaceMgrTestConfig config(true);
CfgSubnets4 cfg;
// Create 3 subnets.
Subnet4Ptr subnet1(new Subnet4(IOAddress("192.0.2.0"),
26, 1, 2, 3, SubnetID(1)));
Subnet4Ptr subnet2(new Subnet4(IOAddress("192.0.2.64"),
26, 1, 2, 3, SubnetID(2)));
Subnet4Ptr subnet3(new Subnet4(IOAddress("192.0.2.128"),
26, 1, 2, 3, SubnetID(3)));
// Add them to the configuration.
cfg.add(subnet1);
cfg.add(subnet2);
cfg.add(subnet3);
// Create first network and add first two subnets to it.
SharedNetwork4Ptr network1(new SharedNetwork4("network1"));
network1->setIface("eth1");
network1->allowClientClass("device-type1");
ASSERT_NO_THROW(network1->add(subnet1));
ASSERT_NO_THROW(network1->add(subnet2));
// Create second network and add last subnet there.
SharedNetwork4Ptr network2(new SharedNetwork4("network2"));
network2->setIface("eth1");
network2->allowClientClass("device-type2");
ASSERT_NO_THROW(network2->add(subnet3));
// Use interface name as a selector. This guarantees that subnet
// selection will be made based on the classification.
SubnetSelector selector;
selector.iface_name_ = "eth1";
// If the client has "device-type2" class, it is expected that the
// second network will be used. This network has only one subnet
// in it, i.e. subnet3.
ClientClasses client_classes;
client_classes.insert("device-type2");
selector.client_classes_ = client_classes;
EXPECT_EQ(subnet3, cfg.selectSubnet(selector));
// Switch to device-type1 and expect that we assigned a subnet from
// another shared network.
client_classes.clear();
client_classes.insert("device-type1");
selector.client_classes_ = client_classes;
ConstSubnet4Ptr subnet = cfg.selectSubnet(selector);
ASSERT_TRUE(subnet);
SharedNetwork4Ptr network;
subnet->getSharedNetwork(network);
ASSERT_TRUE(network);
EXPECT_EQ("network1", network->getName());
}
// This test verifies the option selection can be used and is only
// used when present.
TEST(CfgSubnets4Test, selectSubnetByOptionSelect) {
CfgSubnets4 cfg;
// Create 3 subnets.
Subnet4Ptr subnet1(new Subnet4(IOAddress("192.0.2.0"),
26, 1, 2, 3, SubnetID(1)));
Subnet4Ptr subnet2(new Subnet4(IOAddress("192.0.2.64"),
26, 1, 2, 3, SubnetID(2)));
Subnet4Ptr subnet3(new Subnet4(IOAddress("192.0.2.128"),
26, 1, 2, 3, SubnetID(3)));
// Add them to the configuration.
cfg.add(subnet1);
cfg.add(subnet2);
cfg.add(subnet3);
SubnetSelector selector;
// Check that without option selection something else is used
selector.ciaddr_ = IOAddress("192.0.2.5");
EXPECT_EQ(subnet1, cfg.selectSubnet(selector));
// The option selection has precedence
selector.option_select_ = IOAddress("192.0.2.130");
EXPECT_EQ(subnet3, cfg.selectSubnet(selector));
// Over relay-info too
selector.giaddr_ = IOAddress("10.0.0.1");
subnet2->addRelayAddress(IOAddress("10.0.0.1"));
EXPECT_EQ(subnet3, cfg.selectSubnet(selector));
selector.option_select_ = IOAddress("0.0.0.0");
EXPECT_EQ(subnet2, cfg.selectSubnet(selector));
// Check that a not matching option selection it shall fail
selector.option_select_ = IOAddress("10.0.0.1");
EXPECT_FALSE(cfg.selectSubnet(selector));
}
// This test verifies that the relay information can be used to retrieve the
// subnet.
TEST(CfgSubnets4Test, selectSubnetByRelayAddress) {
CfgSubnets4 cfg;
// Create 3 subnets.
Subnet4Ptr subnet1(new Subnet4(IOAddress("192.0.2.0"),
26, 1, 2, 3, SubnetID(1)));
Subnet4Ptr subnet2(new Subnet4(IOAddress("192.0.2.64"),
26, 1, 2, 3, SubnetID(2)));
Subnet4Ptr subnet3(new Subnet4(IOAddress("192.0.2.128"),
26, 1, 2, 3, SubnetID(3)));
// Add them to the configuration.
cfg.add(subnet1);
cfg.add(subnet2);
cfg.add(subnet3);
SubnetSelector selector;
// Check that without relay-info specified, subnets are not selected
selector.giaddr_ = IOAddress("10.0.0.1");
EXPECT_FALSE(cfg.selectSubnet(selector));
selector.giaddr_ = IOAddress("10.0.0.2");
EXPECT_FALSE(cfg.selectSubnet(selector));
selector.giaddr_ = IOAddress("10.0.0.3");
EXPECT_FALSE(cfg.selectSubnet(selector));
// Now specify relay info
subnet1->addRelayAddress(IOAddress("10.0.0.1"));
subnet2->addRelayAddress(IOAddress("10.0.0.2"));
subnet3->addRelayAddress(IOAddress("10.0.0.3"));
// And try again. This time relay-info is there and should match.
selector.giaddr_ = IOAddress("10.0.0.1");
EXPECT_EQ(subnet1, cfg.selectSubnet(selector));
selector.giaddr_ = IOAddress("10.0.0.2");
EXPECT_EQ(subnet2, cfg.selectSubnet(selector));
selector.giaddr_ = IOAddress("10.0.0.3");
EXPECT_EQ(subnet3, cfg.selectSubnet(selector));
}
// This test verifies that the relay information specified on the shared
// network level can be used to select a subnet.
TEST(CfgSubnets4Test, selectSharedNetworkByRelayAddressNetworkLevel) {
CfgSubnets4 cfg;
// Create 3 subnets.
Subnet4Ptr subnet1(new Subnet4(IOAddress("192.0.2.0"),
26, 1, 2, 3, SubnetID(1)));
Subnet4Ptr subnet2(new Subnet4(IOAddress("192.0.2.64"),
26, 1, 2, 3, SubnetID(2)));
Subnet4Ptr subnet3(new Subnet4(IOAddress("192.0.2.128"),
26, 1, 2, 3, SubnetID(3)));
// Add them to the configuration.
cfg.add(subnet1);
cfg.add(subnet2);
cfg.add(subnet3);
SharedNetwork4Ptr network(new SharedNetwork4("network"));
network->add(subnet2);
SubnetSelector selector;
// Now specify relay info. Note that for the second subnet we specify
// relay info on the network level.
subnet1->addRelayAddress(IOAddress("10.0.0.1"));
network->addRelayAddress(IOAddress("10.0.0.2"));
subnet3->addRelayAddress(IOAddress("10.0.0.3"));
// And try again. This time relay-info is there and should match.
selector.giaddr_ = IOAddress("10.0.0.1");
EXPECT_EQ(subnet1, cfg.selectSubnet(selector));
selector.giaddr_ = IOAddress("10.0.0.2");
EXPECT_EQ(subnet2, cfg.selectSubnet(selector));
selector.giaddr_ = IOAddress("10.0.0.3");
EXPECT_EQ(subnet3, cfg.selectSubnet(selector));
}
// This test verifies that the relay information specified on the subnet
// level can be used to select a subnet and the fact that a subnet belongs
// to a shared network doesn't affect the process.
TEST(CfgSubnets4Test, selectSharedNetworkByRelayAddressSubnetLevel) {
CfgSubnets4 cfg;
// Create 3 subnets.
Subnet4Ptr subnet1(new Subnet4(IOAddress("192.0.2.0"),
26, 1, 2, 3, SubnetID(1)));
Subnet4Ptr subnet2(new Subnet4(IOAddress("192.0.2.64"),
26, 1, 2, 3, SubnetID(2)));
Subnet4Ptr subnet3(new Subnet4(IOAddress("192.0.2.128"),
26, 1, 2, 3, SubnetID(3)));
// Add them to the configuration.
cfg.add(subnet1);
cfg.add(subnet2);
cfg.add(subnet3);
SharedNetwork4Ptr network1(new SharedNetwork4("network1"));
network1->add(subnet1);
SharedNetwork4Ptr network2(new SharedNetwork4("network2"));
network2->add(subnet2);
SubnetSelector selector;
// Now specify relay info. Note that for the second subnet we specify
// relay info on the network level.
subnet1->addRelayAddress(IOAddress("10.0.0.1"));
subnet2->addRelayAddress(IOAddress("10.0.0.2"));
subnet3->addRelayAddress(IOAddress("10.0.0.3"));
// And try again. This time relay-info is there and should match.
selector.giaddr_ = IOAddress("10.0.0.1");
EXPECT_EQ(subnet1, cfg.selectSubnet(selector));
selector.giaddr_ = IOAddress("10.0.0.2");
EXPECT_EQ(subnet2, cfg.selectSubnet(selector));
selector.giaddr_ = IOAddress("10.0.0.3");
EXPECT_EQ(subnet3, cfg.selectSubnet(selector));
}
// This test verifies that the subnet can be selected for the client
// using a source address if the client hasn't set the ciaddr.
TEST(CfgSubnets4Test, selectSubnetNoCiaddr) {
CfgSubnets4 cfg;
// Create 3 subnets.
Subnet4Ptr subnet1(new Subnet4(IOAddress("192.0.2.0"),
26, 1, 2, 3, SubnetID(1)));
Subnet4Ptr subnet2(new Subnet4(IOAddress("192.0.2.64"),
26, 1, 2, 3, SubnetID(2)));
Subnet4Ptr subnet3(new Subnet4(IOAddress("192.0.2.128"),
26, 1, 2, 3, SubnetID(3)));
// Make sure that initially the subnets don't exist.
SubnetSelector selector;
selector.remote_address_ = IOAddress("192.0.2.0");
// Set some unicast local address to simulate a Renew.
selector.local_address_ = IOAddress("10.0.0.100");
ASSERT_FALSE(cfg.selectSubnet(selector));
// Add one subnet and make sure it is returned.
cfg.add(subnet1);
selector.remote_address_ = IOAddress("192.0.2.63");
EXPECT_EQ(subnet1, cfg.selectSubnet(selector));
// Add all other subnets.
cfg.add(subnet2);
cfg.add(subnet3);
// Make sure they are returned for the appropriate addresses.
selector.remote_address_ = IOAddress("192.0.2.15");
EXPECT_EQ(subnet1, cfg.selectSubnet(selector));
selector.remote_address_ = IOAddress("192.0.2.85");
EXPECT_EQ(subnet2, cfg.selectSubnet(selector));
selector.remote_address_ = IOAddress("192.0.2.191");
EXPECT_EQ(subnet3, cfg.selectSubnet(selector));
// Also, make sure that the NULL pointer is returned if the subnet
// cannot be found.
selector.remote_address_ = IOAddress("192.0.2.192");
EXPECT_FALSE(cfg.selectSubnet(selector));
}
// This test verifies that the subnet can be selected using an address
// set on the local interface.
TEST(CfgSubnets4Test, selectSubnetInterface) {
// The IfaceMgrTestConfig object initializes fake interfaces:
// eth0, eth1 and lo on the configuration manager. The CfgSubnets4
// object uses addresses assigned to these fake interfaces to
// select the appropriate subnet.
IfaceMgrTestConfig config(true);
CfgSubnets4 cfg;
SubnetSelector selector;
// Initially, there are no subnets configured, so none of the IPv4
// addresses assigned to eth0 and eth1 can match with any subnet.
selector.iface_name_ = "eth0";
EXPECT_FALSE(cfg.selectSubnet(selector));
selector.iface_name_ = "eth1";
EXPECT_FALSE(cfg.selectSubnet(selector));
// Configure first subnet which address on eth0 corresponds to.
Subnet4Ptr subnet1(new Subnet4(IOAddress("10.0.0.1"),
24, 1, 2, 3, SubnetID(1)));
cfg.add(subnet1);
// The address on eth0 should match the existing subnet.
selector.iface_name_ = "eth0";
ConstSubnet4Ptr subnet1_ret = cfg.selectSubnet(selector);
ASSERT_TRUE(subnet1_ret);
EXPECT_EQ(subnet1->get().first, subnet1_ret->get().first);
// There should still be no match for eth1.
selector.iface_name_ = "eth1";
EXPECT_FALSE(cfg.selectSubnet(selector));
// Configure a second subnet.
Subnet4Ptr subnet2(new Subnet4(IOAddress("192.0.2.1"),
24, 1, 2, 3, SubnetID(2)));
cfg.add(subnet2);
// There should be match between eth0 and subnet1 and between eth1 and
// subnet 2.
selector.iface_name_ = "eth0";
subnet1_ret = cfg.selectSubnet(selector);
ASSERT_TRUE(subnet1_ret);
EXPECT_EQ(subnet1->get().first, subnet1_ret->get().first);
selector.iface_name_ = "eth1";
ConstSubnet4Ptr subnet2_ret = cfg.selectSubnet(selector);
ASSERT_TRUE(subnet2_ret);
EXPECT_EQ(subnet2->get().first, subnet2_ret->get().first);
// This function throws an exception if the name of the interface is wrong.
selector.iface_name_ = "bogus-interface";
EXPECT_THROW(cfg.selectSubnet(selector), isc::BadValue);
}
// Checks that detection of duplicated subnet IDs works as expected. It should
// not be possible to add two IPv4 subnets holding the same ID.
TEST(CfgSubnets4Test, duplication) {
CfgSubnets4 cfg;
Subnet4Ptr subnet1(new Subnet4(IOAddress("192.0.2.0"), 26, 1, 2, 3, 123));
Subnet4Ptr subnet2(new Subnet4(IOAddress("192.0.2.64"), 26, 1, 2, 3, 124));
Subnet4Ptr subnet3(new Subnet4(IOAddress("192.0.2.128"), 26, 1, 2, 3, 123));
Subnet4Ptr subnet4(new Subnet4(IOAddress("192.0.2.1"), 26, 1, 2, 3, 125));
ASSERT_NO_THROW(cfg.add(subnet1));
EXPECT_NO_THROW(cfg.add(subnet2));
// Subnet 3 has the same ID as subnet 1. It shouldn't be able to add it.
EXPECT_THROW(cfg.add(subnet3), isc::dhcp::DuplicateSubnetID);
// Subnet 4 has a similar but different subnet as subnet 1.
EXPECT_NO_THROW(cfg.add(subnet4));
}
// This test checks if the IPv4 subnet can be selected based on the IPv6 address.
TEST(CfgSubnets4Test, 4o6subnetMatchByAddress) {
CfgSubnets4 cfg;
Subnet4Ptr subnet1(new Subnet4(IOAddress("192.0.2.0"), 26, 1, 2, 3, 123));
Subnet4Ptr subnet2(new Subnet4(IOAddress("192.0.2.64"), 26, 1, 2, 3, 124));
Subnet4Ptr subnet3(new Subnet4(IOAddress("192.0.2.128"), 26, 1, 2, 3, 125));
subnet2->get4o6().setSubnet4o6(IOAddress("2001:db8:1::"), 48);
subnet3->get4o6().setSubnet4o6(IOAddress("2001:db8:2::"), 48);
cfg.add(subnet1);
cfg.add(subnet2);
cfg.add(subnet3);
SubnetSelector selector;
selector.dhcp4o6_ = true;
selector.remote_address_ = IOAddress("2001:db8:1::dead:beef");
EXPECT_EQ(subnet2, cfg.selectSubnet4o6(selector));
}
// This test checks if the IPv4 subnet can be selected based on the value of
// interface-id option.
TEST(CfgSubnets4Test, 4o6subnetMatchByInterfaceId) {
CfgSubnets4 cfg;
Subnet4Ptr subnet1(new Subnet4(IOAddress("192.0.2.0"), 26, 1, 2, 3, 123));
Subnet4Ptr subnet2(new Subnet4(IOAddress("192.0.2.64"), 26, 1, 2, 3, 124));
Subnet4Ptr subnet3(new Subnet4(IOAddress("192.0.2.128"), 26, 1, 2, 3, 125));
const uint8_t dummyPayload1[] = { 1, 2, 3, 4};
const uint8_t dummyPayload2[] = { 1, 2, 3, 5};
std::vector<uint8_t> data1(dummyPayload1, dummyPayload1 + sizeof(dummyPayload1));
std::vector<uint8_t> data2(dummyPayload2, dummyPayload2 + sizeof(dummyPayload2));
OptionPtr interfaceId1(new Option(Option::V6, D6O_INTERFACE_ID, data1));
OptionPtr interfaceId2(new Option(Option::V6, D6O_INTERFACE_ID, data2));
subnet2->get4o6().setInterfaceId(interfaceId1);
cfg.add(subnet1);
cfg.add(subnet2);
cfg.add(subnet3);
SubnetSelector selector;
selector.dhcp4o6_ = true;
selector.interface_id_ = interfaceId2;
// We have mismatched interface-id options (data1 vs data2). Should not match.
EXPECT_FALSE(cfg.selectSubnet4o6(selector));
// This time we have correct interface-id. Should match.
selector.interface_id_ = interfaceId1;
EXPECT_EQ(subnet2, cfg.selectSubnet4o6(selector));
}
// This test checks if the IPv4 subnet can be selected based on the value of
// interface name option.
TEST(CfgSubnets4Test, 4o6subnetMatchByInterfaceName) {
CfgSubnets4 cfg;
Subnet4Ptr subnet1(new Subnet4(IOAddress("192.0.2.0"), 26, 1, 2, 3, 123));
Subnet4Ptr subnet2(new Subnet4(IOAddress("192.0.2.64"), 26, 1, 2, 3, 124));
Subnet4Ptr subnet3(new Subnet4(IOAddress("192.0.2.128"), 26, 1, 2, 3, 125));
subnet2->get4o6().setIface4o6("eth7");
cfg.add(subnet1);
cfg.add(subnet2);
cfg.add(subnet3);
SubnetSelector selector;
selector.dhcp4o6_ = true;
selector.iface_name_ = "eth5";
// We have mismatched interface names. Should not match.
EXPECT_FALSE(cfg.selectSubnet4o6(selector));
// This time we have correct names. Should match.
selector.iface_name_ = "eth7";
EXPECT_EQ(subnet2, cfg.selectSubnet4o6(selector));
}
// This test check if IPv4 subnets can be unparsed in a predictable way,
TEST(CfgSubnets4Test, unparseSubnet) {
CfgSubnets4 cfg;
// Add some subnets.
Subnet4Ptr subnet1(new Subnet4(IOAddress("192.0.2.0"), 26, 1, 2, 3, 123));
Subnet4Ptr subnet2(new Subnet4(IOAddress("192.0.2.64"), 26, 1, 2, 3, 124));
Subnet4Ptr subnet3(new Subnet4(IOAddress("192.0.2.128"), 26, 1, 2, 3, 125));
subnet1->allowClientClass("foo");
subnet1->allowClientClass("bar");
subnet1->setT1Percent(0.45);
subnet1->setT2Percent(0.70);
subnet1->setCacheThreshold(0.20);
subnet2->setIface("lo");
subnet2->addRelayAddress(IOAddress("10.0.0.1"));
subnet2->setValid(Triplet<uint32_t>(100));
subnet2->setStoreExtendedInfo(true);
subnet2->setCacheMaxAge(80);
subnet2->setOfferLft(99);
subnet3->setIface("eth1");
subnet3->addAdditionalClass("foo");
subnet3->addAdditionalClass("bar");
subnet3->setCalculateTeeTimes(true);
subnet3->setT1Percent(0.50);
subnet3->setT2Percent(0.65);
subnet3->setReservationsGlobal(false);
subnet3->setReservationsInSubnet(true);
subnet3->setReservationsOutOfPool(false);
subnet3->setAuthoritative(false);
subnet3->setMatchClientId(true);
subnet3->setSiaddr(IOAddress("192.0.2.2"));
subnet3->setSname("frog");
subnet3->setFilename("/dev/null");
subnet3->setValid(Triplet<uint32_t>(100, 200, 300));
subnet3->setDdnsSendUpdates(true);
subnet3->setDdnsOverrideNoUpdate(true);
subnet3->setDdnsOverrideClientUpdate(true);
subnet3->setDdnsReplaceClientNameMode(D2ClientConfig::RCM_ALWAYS);
subnet3->setDdnsGeneratedPrefix("prefix");
subnet3->setDdnsQualifyingSuffix("example.com.");
subnet3->setHostnameCharSet("[^A-Z]");
subnet3->setHostnameCharReplacement("x");
data::ElementPtr ctx1 = data::Element::fromJSON("{ \"comment\": \"foo\" }");
subnet1->setContext(ctx1);
data::ElementPtr ctx2 = data::Element::createMap();
subnet2->setContext(ctx2);
cfg.add(subnet1);
cfg.add(subnet2);
cfg.add(subnet3);
// Unparse
std::string expected = "[\n"
"{\n"
" \"id\": 123,\n"
" \"subnet\": \"192.0.2.0/26\",\n"
" \"t1-percent\": 0.45,"
" \"t2-percent\": 0.7,"
" \"cache-threshold\": .20,\n"
" \"renew-timer\": 1,\n"
" \"rebind-timer\": 2,\n"
" \"relay\": { \"ip-addresses\": [ ] },\n"
" \"valid-lifetime\": 3,\n"
" \"min-valid-lifetime\": 3,\n"
" \"max-valid-lifetime\": 3,\n"
" \"client-classes\": [ \"foo\", \"bar\" ],\n"
" \"4o6-interface\": \"\",\n"
" \"4o6-interface-id\": \"\",\n"
" \"4o6-subnet\": \"\",\n"
" \"option-data\": [ ],\n"
" \"pools\": [ ],\n"
" \"user-context\": { \"comment\": \"foo\" }\n"
"},{\n"
" \"id\": 124,\n"
" \"subnet\": \"192.0.2.64/26\",\n"
" \"interface\": \"lo\",\n"
" \"renew-timer\": 1,\n"
" \"rebind-timer\": 2,\n"
" \"relay\": { \"ip-addresses\": [ \"10.0.0.1\" ] },\n"
" \"valid-lifetime\": 100,\n"
" \"min-valid-lifetime\": 100,\n"
" \"max-valid-lifetime\": 100,\n"
" \"4o6-interface\": \"\",\n"
" \"4o6-interface-id\": \"\",\n"
" \"4o6-subnet\": \"\",\n"
" \"user-context\": {},\n"
" \"option-data\": [ ],\n"
" \"pools\": [ ],\n"
" \"store-extended-info\": true,\n"
" \"cache-max-age\": 80,\n"
" \"offer-lifetime\": 99\n"
"},{\n"
" \"id\": 125,\n"
" \"subnet\": \"192.0.2.128/26\",\n"
" \"interface\": \"eth1\",\n"
" \"match-client-id\": true,\n"
" \"next-server\": \"192.0.2.2\",\n"
" \"server-hostname\": \"frog\",\n"
" \"boot-file-name\": \"/dev/null\",\n"
" \"renew-timer\": 1,\n"
" \"rebind-timer\": 2,\n"
" \"relay\": { \"ip-addresses\": [ ] },\n"
" \"valid-lifetime\": 200,\n"
" \"min-valid-lifetime\": 100,\n"
" \"max-valid-lifetime\": 300,\n"
" \"4o6-interface\": \"\",\n"
" \"4o6-interface-id\": \"\",\n"
" \"4o6-subnet\": \"\",\n"
" \"authoritative\": false,\n"
" \"reservations-global\": false,\n"
" \"reservations-in-subnet\": true,\n"
" \"reservations-out-of-pool\": false,\n"
" \"option-data\": [ ],\n"
" \"pools\": [ ]\n,"
" \"evaluate-additional-classes\": [ \"foo\", \"bar\" ],\n"
" \"calculate-tee-times\": true,\n"
" \"t1-percent\": 0.50,\n"
" \"t2-percent\": 0.65,\n"
" \"ddns-generated-prefix\": \"prefix\",\n"
" \"ddns-override-client-update\": true,\n"
" \"ddns-override-no-update\": true,\n"
" \"ddns-qualifying-suffix\": \"example.com.\",\n"
" \"ddns-replace-client-name\": \"always\",\n"
" \"ddns-send-updates\": true,\n"
" \"hostname-char-replacement\": \"x\",\n"
" \"hostname-char-set\": \"[^A-Z]\"\n"
"} ]\n";
runToElementTest<CfgSubnets4>(expected, cfg);
}
// This test check if IPv4 pools can be unparsed in a predictable way,
TEST(CfgSubnets4Test, unparsePool) {
CfgSubnets4 cfg;
// Add a subnet with pools
Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.2.0"), 24, 1, 2, 3, 123));
Pool4Ptr pool1(new Pool4(IOAddress("192.0.2.1"), IOAddress("192.0.2.10")));
Pool4Ptr pool2(new Pool4(IOAddress("192.0.2.64"), 26));
pool2->allowClientClass("bar");
std::string json1 = "{ \"comment\": \"foo\", \"version\": 1 }";
data::ElementPtr ctx1 = data::Element::fromJSON(json1);
pool1->setContext(ctx1);
data::ElementPtr ctx2 = data::Element::fromJSON("{ \"foo\": \"bar\" }");
pool2->setContext(ctx2);
pool2->addAdditionalClass("foo");
subnet->addPool(pool1);
subnet->addPool(pool2);
cfg.add(subnet);
// Unparse
std::string expected = "[\n"
"{\n"
" \"id\": 123,\n"
" \"subnet\": \"192.0.2.0/24\",\n"
" \"renew-timer\": 1,\n"
" \"rebind-timer\": 2,\n"
" \"relay\": { \"ip-addresses\": [ ] },\n"
" \"valid-lifetime\": 3,\n"
" \"min-valid-lifetime\": 3,\n"
" \"max-valid-lifetime\": 3,\n"
" \"4o6-interface\": \"\",\n"
" \"4o6-interface-id\": \"\",\n"
" \"4o6-subnet\": \"\",\n"
" \"option-data\": [],\n"
" \"pools\": [\n"
" {\n"
" \"option-data\": [ ],\n"
" \"pool\": \"192.0.2.1-192.0.2.10\",\n"
" \"user-context\": { \"comment\": \"foo\",\n"
" \"version\": 1 }\n"
" },{\n"
" \"option-data\": [ ],\n"
" \"pool\": \"192.0.2.64/26\",\n"
" \"user-context\": { \"foo\": \"bar\" },\n"
" \"client-classes\": [ \"bar\" ],\n"
" \"evaluate-additional-classes\": [ \"foo\" ]\n"
" }\n"
" ]\n"
"} ]\n";
runToElementTest<CfgSubnets4>(expected, cfg);
}
// This test verifies that it is possible to retrieve a subnet using subnet-id.
TEST(CfgSubnets4Test, getSubnet) {
CfgSubnets4 cfg;
// Create 3 subnets.
Subnet4Ptr subnet1(new Subnet4(IOAddress("192.0.2.0"), 26, 1, 2, 3, 100));
Subnet4Ptr subnet2(new Subnet4(IOAddress("192.0.2.64"), 26, 1, 2, 3, 200));
Subnet4Ptr subnet3(new Subnet4(IOAddress("192.0.2.128"), 26, 1, 2, 3, 300));
// Add one subnet and make sure it is returned.
cfg.add(subnet1);
cfg.add(subnet2);
cfg.add(subnet3);
EXPECT_EQ(subnet1, cfg.getSubnet(100));
EXPECT_EQ(subnet2, cfg.getSubnet(200));
EXPECT_EQ(subnet3, cfg.getSubnet(300));
EXPECT_EQ(Subnet4Ptr(), cfg.getSubnet(400)); // no such subnet
}
// This test verifies that hasSubnetWithServerId returns correct value.
TEST(CfgSubnets4Test, hasSubnetWithServerId) {
CfgSubnets4 cfg;
// Initially, there is no server identifier option present.
EXPECT_FALSE(cfg.hasSubnetWithServerId(IOAddress("1.2.3.4")));
const OptionDefinition& def = LibDHCP::DHO_DHCP_SERVER_IDENTIFIER_DEF();
OptionCustomPtr opt_server_id(new OptionCustom(def, Option::V4));
opt_server_id->writeAddress(IOAddress("1.2.3.4"));
Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.2.0"), 26, 1, 2, 3, 100));
subnet->getCfgOption()->add(opt_server_id, false, false,
DHCP4_OPTION_SPACE);
cfg.add(subnet);
EXPECT_TRUE(cfg.hasSubnetWithServerId(IOAddress("1.2.3.4")));
EXPECT_FALSE(cfg.hasSubnetWithServerId(IOAddress("2.3.4.5")));
}
// This test verifies the Subnet4 parser's validation logic for id parameter.
TEST(CfgSubnets4Test, idValidation) {
{
// id 0.
SCOPED_TRACE("id 0");
std::string json =
" {\n"
" \"id\": 0,\n"
" \"subnet\": \"10.1.2.0/24\"\n"
" }\n";
data::ElementPtr elems;
ASSERT_NO_THROW(elems = data::Element::fromJSON(json))
<< "invalid JSON:" << json << "\n test is broken";
std::string expected = "subnet configuration failed: ";
expected += "The 'id' value (0) is not within ";
expected += "expected range: (1 - 4294967294)";
try {
// Attempt to parse the configuration.
Subnet4ConfigParser parser;
Subnet4Ptr subnet = parser.parse(elems);
ADD_FAILURE() << "expected exception";
} catch (const std::exception& ex) {
EXPECT_EQ(expected, ex.what());
}
}
{
// id 4294967295.
SCOPED_TRACE("id 4294967295");
std::string json =
" {\n"
" \"id\": 4294967295,\n"
" \"subnet\": \"10.1.2.0/24\"\n"
" }\n";
data::ElementPtr elems;
ASSERT_NO_THROW(elems = data::Element::fromJSON(json))
<< "invalid JSON:" << json << "\n test is broken";
std::string expected = "subnet configuration failed: ";
expected += "The 'id' value (4294967295) is not within ";
expected += "expected range: (1 - 4294967294)";
try {
// Attempt to parse the configuration.
Subnet4ConfigParser parser;
Subnet4Ptr subnet = parser.parse(elems);
ADD_FAILURE() << "expected exception";
} catch (const std::exception& ex) {
EXPECT_EQ(expected, ex.what());
}
}
{
// id 1 (valid).
SCOPED_TRACE("id 1");
std::string json =
" {\n"
" \"id\": 1,\n"
" \"subnet\": \"10.1.2.0/24\"\n"
" }\n";
data::ElementPtr elems;
ASSERT_NO_THROW(elems = data::Element::fromJSON(json))
<< "invalid JSON:" << json << "\n test is broken";
try {
// Attempt to parse the configuration.
Subnet4ConfigParser parser;
Subnet4Ptr subnet = parser.parse(elems);
EXPECT_TRUE(subnet);
} catch (const std::exception& ex) {
ADD_FAILURE() << "unexpected failure " << ex.what();
}
}
}
// This test verifies the Subnet4 parser's validation logic for
// t1-percent and t2-percent parameters.
TEST(CfgSubnets4Test, teeTimePercentValidation) {
// Describes a single test scenario.
struct Scenario {
std::string label; // label used for logging test failures
bool calculate_tee_times; // value of calculate-tee-times parameter
double t1_percent; // value of t1-percent parameter
double t2_percent; // value of t2-percent parameter
std::string error_message; // expected error message is parsing should fail
};
// Test Scenarios.
std::vector<Scenario> tests = {
{"off and valid", false, .5, .95, ""},
{"on and valid", true, .5, .95, ""},
{"t2_negative", true, .5, -.95,
"subnet configuration failed: t2-percent:"
" -0.95 is invalid, it must be greater than 0.0 and less than 1.0"
},
{"t2_too_big", true, .5, 1.95,
"subnet configuration failed: t2-percent:"
" 1.95 is invalid, it must be greater than 0.0 and less than 1.0"
},
{"t1_negative", true, -.5, .95,
"subnet configuration failed: t1-percent:"
" -0.5 is invalid it must be greater than 0.0 and less than 1.0"
},
{"t1_too_big", true, 1.5, .95,
"subnet configuration failed: t1-percent:"
" 1.5 is invalid it must be greater than 0.0 and less than 1.0"
},
{"t1_bigger_than_t2", true, .85, .45,
"subnet configuration failed: t1-percent:"
" 0.85 is invalid, it must be less than t2-percent: 0.45"
}
};
// First we create a set of elements that provides all
// required for a Subnet4.
std::string json =
" {"
" \"id\": 1,\n"
" \"subnet\": \"10.1.2.0/24\", \n"
" \"interface\": \"\", \n"
" \"renew-timer\": 100, \n"
" \"rebind-timer\": 200, \n"
" \"valid-lifetime\": 300, \n"
" \"match-client-id\": false, \n"
" \"authoritative\": false, \n"
" \"next-server\": \"\", \n"
" \"server-hostname\": \"\", \n"
" \"boot-file-name\": \"\", \n"
" \"client-classes\": [] , \n"
" \"evaluate-additional-classes\": [] \n,"
" \"reservations-global\": false, \n"
" \"reservations-in-subnet\": true, \n"
" \"reservations-out-of-pool\": false, \n"
" \"4o6-interface\": \"\", \n"
" \"4o6-interface-id\": \"\", \n"
" \"4o6-subnet\": \"\" \n"
" }";
data::ElementPtr elems;
ASSERT_NO_THROW(elems = data::Element::fromJSON(json))
<< "invalid JSON:" << json << "\n test is broken";
// Iterate over the test scenarios, verifying each prescribed
// outcome.
for (auto const& test : tests) {
{
SCOPED_TRACE("test: " + test.label);
// Set this scenario's configuration parameters
elems->set("calculate-tee-times", data::Element::create(test.calculate_tee_times));
elems->set("t1-percent", data::Element::create(test.t1_percent));
elems->set("t2-percent", data::Element::create(test.t2_percent));
Subnet4Ptr subnet;
try {
// Attempt to parse the configuration.
Subnet4ConfigParser parser;
subnet = parser.parse(elems);
} catch (const std::exception& ex) {
if (!test.error_message.empty()) {
// We expected a failure, did we fail the correct way?
EXPECT_EQ(test.error_message, ex.what());
} else {
// Should not have failed.
ADD_FAILURE() << "Scenario should not have failed: " << ex.what();
}
// Either way we're done with this scenario.
continue;
}
// We parsed correctly, make sure the values are right.
EXPECT_EQ(test.calculate_tee_times, subnet->getCalculateTeeTimes());
EXPECT_TRUE(util::areDoublesEquivalent(test.t1_percent, subnet->getT1Percent()));
EXPECT_TRUE(util::areDoublesEquivalent(test.t2_percent, subnet->getT2Percent()));
}
}
}
// This test verifies the Subnet4 parser's validation logic for
// valid-lifetime and indirectly shared lifetime parsing.
TEST(CfgSubnets4Test, validLifetimeValidation) {
// First we create a set of elements that provides all
// required for a Subnet4.
std::string json =
" {"
" \"id\": 1,\n"
" \"subnet\": \"10.1.2.0/24\", \n"
" \"interface\": \"\", \n"
" \"renew-timer\": 100, \n"
" \"rebind-timer\": 200, \n"
" \"match-client-id\": false, \n"
" \"authoritative\": false, \n"
" \"next-server\": \"\", \n"
" \"server-hostname\": \"\", \n"
" \"boot-file-name\": \"\", \n"
" \"client-classes\": [], \n"
" \"evaluate-additional-classes\": [], \n"
" \"reservations-global\": false, \n"
" \"reservations-in-subnet\": true, \n"
" \"reservations-out-of-pool\": false, \n"
" \"4o6-interface\": \"\", \n"
" \"4o6-interface-id\": \"\", \n"
" \"4o6-subnet\": \"\" \n"
" }";
data::ElementPtr elems;
ASSERT_NO_THROW(elems = data::Element::fromJSON(json))
<< "invalid JSON:" << json << "\n test is broken";
{
SCOPED_TRACE("no valid-lifetime");
data::ElementPtr copied = data::copy(elems);
Subnet4Ptr subnet;
Subnet4ConfigParser parser;
ASSERT_NO_THROW(subnet = parser.parse(copied));
ASSERT_TRUE(subnet);
EXPECT_TRUE(subnet->getValid().unspecified());
data::ConstElementPtr repr = subnet->toElement();
EXPECT_FALSE(repr->get("valid-lifetime"));
EXPECT_FALSE(repr->get("min-valid-lifetime"));
EXPECT_FALSE(repr->get("max-valid-lifetime"));
}
{
SCOPED_TRACE("valid-lifetime only");
data::ElementPtr copied = data::copy(elems);
copied->set("valid-lifetime", data::Element::create(100));
Subnet4Ptr subnet;
Subnet4ConfigParser parser;
ASSERT_NO_THROW(subnet = parser.parse(copied));
ASSERT_TRUE(subnet);
EXPECT_FALSE(subnet->getValid().unspecified());
EXPECT_EQ(100, subnet->getValid().get());
EXPECT_EQ(100, subnet->getValid().getMin());
EXPECT_EQ(100, subnet->getValid().getMax());
data::ConstElementPtr repr = subnet->toElement();
data::ConstElementPtr value = repr->get("valid-lifetime");
ASSERT_TRUE(value);
EXPECT_EQ("100", value->str());
data::ConstElementPtr min_value = repr->get("min-valid-lifetime");
ASSERT_TRUE(min_value);
EXPECT_EQ("100", min_value->str());
data::ConstElementPtr max_value = repr->get("max-valid-lifetime");
ASSERT_TRUE(max_value);
EXPECT_EQ("100", max_value->str());
}
{
SCOPED_TRACE("min-valid-lifetime only");
data::ElementPtr copied = data::copy(elems);
copied->set("min-valid-lifetime", data::Element::create(100));
Subnet4Ptr subnet;
Subnet4ConfigParser parser;
ASSERT_NO_THROW(subnet = parser.parse(copied));
ASSERT_TRUE(subnet);
EXPECT_FALSE(subnet->getValid().unspecified());
EXPECT_EQ(100, subnet->getValid().get());
EXPECT_EQ(100, subnet->getValid().getMin());
EXPECT_EQ(100, subnet->getValid().getMax());
data::ConstElementPtr repr = subnet->toElement();
data::ConstElementPtr value = repr->get("valid-lifetime");
ASSERT_TRUE(value);
EXPECT_EQ("100", value->str());
data::ConstElementPtr min_value = repr->get("min-valid-lifetime");
ASSERT_TRUE(min_value);
EXPECT_EQ("100", min_value->str());
data::ConstElementPtr max_value = repr->get("max-valid-lifetime");
ASSERT_TRUE(max_value);
EXPECT_EQ("100", max_value->str());
}
{
SCOPED_TRACE("max-valid-lifetime only");
data::ElementPtr copied = data::copy(elems);
copied->set("max-valid-lifetime", data::Element::create(100));
Subnet4Ptr subnet;
Subnet4ConfigParser parser;
ASSERT_NO_THROW(subnet = parser.parse(copied));
ASSERT_TRUE(subnet);
EXPECT_FALSE(subnet->getValid().unspecified());
EXPECT_EQ(100, subnet->getValid().get());
EXPECT_EQ(100, subnet->getValid().getMin());
EXPECT_EQ(100, subnet->getValid().getMax());
data::ConstElementPtr repr = subnet->toElement();
data::ConstElementPtr value = repr->get("valid-lifetime");
ASSERT_TRUE(value);
EXPECT_EQ("100", value->str());
data::ConstElementPtr min_value = repr->get("min-valid-lifetime");
ASSERT_TRUE(min_value);
EXPECT_EQ("100", min_value->str());
data::ConstElementPtr max_value = repr->get("max-valid-lifetime");
ASSERT_TRUE(max_value);
EXPECT_EQ("100", max_value->str());
}
{
SCOPED_TRACE("min-valid-lifetime and valid-lifetime");
data::ElementPtr copied = data::copy(elems);
copied->set("min-valid-lifetime", data::Element::create(100));
// Use a different (and greater) value for the default.
copied->set("valid-lifetime", data::Element::create(200));
Subnet4Ptr subnet;
Subnet4ConfigParser parser;
ASSERT_NO_THROW(subnet = parser.parse(copied));
ASSERT_TRUE(subnet);
EXPECT_FALSE(subnet->getValid().unspecified());
EXPECT_EQ(200, subnet->getValid().get());
EXPECT_EQ(100, subnet->getValid().getMin());
EXPECT_EQ(200, subnet->getValid().getMax());
data::ConstElementPtr repr = subnet->toElement();
data::ConstElementPtr value = repr->get("valid-lifetime");
ASSERT_TRUE(value);
EXPECT_EQ("200", value->str());
data::ConstElementPtr min_value = repr->get("min-valid-lifetime");
ASSERT_TRUE(min_value);
EXPECT_EQ("100", min_value->str());
data::ConstElementPtr max_value = repr->get("max-valid-lifetime");
ASSERT_TRUE(max_value);
EXPECT_EQ("200", max_value->str());
}
{
SCOPED_TRACE("max-valid-lifetime and valid-lifetime");
data::ElementPtr copied = data::copy(elems);
copied->set("max-valid-lifetime", data::Element::create(200));
// Use a different (and smaller) value for the default.
copied->set("valid-lifetime", data::Element::create(100));
Subnet4Ptr subnet;
Subnet4ConfigParser parser;
ASSERT_NO_THROW(subnet = parser.parse(copied));
ASSERT_TRUE(subnet);
EXPECT_FALSE(subnet->getValid().unspecified());
EXPECT_EQ(100, subnet->getValid().get());
EXPECT_EQ(100, subnet->getValid().getMin());
EXPECT_EQ(200, subnet->getValid().getMax());
data::ConstElementPtr repr = subnet->toElement();
data::ConstElementPtr value = repr->get("valid-lifetime");
ASSERT_TRUE(value);
EXPECT_EQ("100", value->str());
data::ConstElementPtr min_value = repr->get("min-valid-lifetime");
ASSERT_TRUE(min_value);
EXPECT_EQ("100", min_value->str());
data::ConstElementPtr max_value = repr->get("max-valid-lifetime");
ASSERT_TRUE(max_value);
EXPECT_EQ("200", max_value->str());
}
{
SCOPED_TRACE("min-valid-lifetime and max-valid-lifetime");
data::ElementPtr copied = data::copy(elems);
copied->set("min-valid-lifetime", data::Element::create(100));
copied->set("max-valid-lifetime", data::Element::create(200));
Subnet4ConfigParser parser;
// No idea about the value to use for the default so failing.
ASSERT_THROW(parser.parse(copied), DhcpConfigError);
}
{
SCOPED_TRACE("all 3 (min, max and default) valid-lifetime");
data::ElementPtr copied = data::copy(elems);
copied->set("min-valid-lifetime", data::Element::create(100));
// Use a different (and greater) value for the default.
copied->set("valid-lifetime", data::Element::create(200));
// Use a different (and greater than both) value for max.
copied->set("max-valid-lifetime", data::Element::create(300));
Subnet4Ptr subnet;
Subnet4ConfigParser parser;
ASSERT_NO_THROW(subnet = parser.parse(copied));
ASSERT_TRUE(subnet);
EXPECT_FALSE(subnet->getValid().unspecified());
EXPECT_EQ(200, subnet->getValid().get());
EXPECT_EQ(100, subnet->getValid().getMin());
EXPECT_EQ(300, subnet->getValid().getMax());
data::ConstElementPtr repr = subnet->toElement();
data::ConstElementPtr value = repr->get("valid-lifetime");
ASSERT_TRUE(value);
EXPECT_EQ("200", value->str());
data::ConstElementPtr min_value = repr->get("min-valid-lifetime");
ASSERT_TRUE(min_value);
EXPECT_EQ("100", min_value->str());
data::ConstElementPtr max_value = repr->get("max-valid-lifetime");
ASSERT_TRUE(max_value);
EXPECT_EQ("300", max_value->str());
}
{
SCOPED_TRACE("default value too small");
data::ElementPtr copied = data::copy(elems);
copied->set("min-valid-lifetime", data::Element::create(200));
// 100 < 200 so it will fail.
copied->set("valid-lifetime", data::Element::create(100));
// Use a different (and greater than both) value for max.
copied->set("max-valid-lifetime", data::Element::create(300));
Subnet4ConfigParser parser;
ASSERT_THROW(parser.parse(copied), DhcpConfigError);
}
{
SCOPED_TRACE("default value too large");
data::ElementPtr copied = data::copy(elems);
copied->set("min-valid-lifetime", data::Element::create(100));
// Use a different (and greater) value for the default.
copied->set("valid-lifetime", data::Element::create(300));
// 300 > 200 so it will fail.
copied->set("max-valid-lifetime", data::Element::create(200));
Subnet4ConfigParser parser;
ASSERT_THROW(parser.parse(copied), DhcpConfigError);
}
{
SCOPED_TRACE("equal bounds are no longer ignored");
data::ElementPtr copied = data::copy(elems);
copied->set("min-valid-lifetime", data::Element::create(100));
copied->set("valid-lifetime", data::Element::create(100));
copied->set("max-valid-lifetime", data::Element::create(100));
Subnet4Ptr subnet;
Subnet4ConfigParser parser;
ASSERT_NO_THROW(subnet = parser.parse(copied));
ASSERT_TRUE(subnet);
EXPECT_FALSE(subnet->getValid().unspecified());
EXPECT_EQ(100, subnet->getValid().get());
EXPECT_EQ(100, subnet->getValid().getMin());
EXPECT_EQ(100, subnet->getValid().getMax());
data::ConstElementPtr repr = subnet->toElement();
data::ConstElementPtr value = repr->get("valid-lifetime");
ASSERT_TRUE(value);
EXPECT_EQ("100", value->str());
data::ConstElementPtr min_value = repr->get("min-valid-lifetime");
ASSERT_TRUE(min_value);
EXPECT_EQ("100", min_value->str());
data::ConstElementPtr max_value = repr->get("max-valid-lifetime");
ASSERT_TRUE(max_value);
EXPECT_EQ("100", max_value->str());
}
}
// This test verifies the Subnet4 parser's validation logic for
// hostname sanitizer values.
TEST(CfgSubnets4Test, hostnameSanitizierValidation) {
// First we create a set of elements that provides all
// required for a Subnet4.
std::string json =
" {"
" \"id\": 1,\n"
" \"subnet\": \"10.1.2.0/24\", \n"
" \"interface\": \"\", \n"
" \"renew-timer\": 100, \n"
" \"rebind-timer\": 200, \n"
" \"match-client-id\": false, \n"
" \"authoritative\": false, \n"
" \"next-server\": \"\", \n"
" \"server-hostname\": \"\", \n"
" \"boot-file-name\": \"\", \n"
" \"client-classes\": [], \n"
" \"evaluate-additional-classes\": [], \n"
" \"reservations-global\": false, \n"
" \"reservations-in-subnet\": true, \n"
" \"reservations-out-of-pool\": false, \n"
" \"4o6-interface\": \"\", \n"
" \"4o6-interface-id\": \"\", \n"
" \"4o6-subnet\": \"\" \n"
" }";
data::ElementPtr elems;
ASSERT_NO_THROW(elems = data::Element::fromJSON(json))
<< "invalid JSON:" << json << "\n test is broken";
{
SCOPED_TRACE("invalid regular expression");
data::ElementPtr copied = data::copy(elems);
copied->set("hostname-char-set", data::Element::create("^[A-"));
copied->set("hostname-char-replacement", data::Element::create("x"));
Subnet4Ptr subnet;
Subnet4ConfigParser parser;
EXPECT_THROW_MSG(subnet = parser.parse(copied), DhcpConfigError,
"subnet configuration failed: hostname-char-set "
"'^[A-' is not a valid regular expression");
}
{
SCOPED_TRACE("valid regular expression");
data::ElementPtr copied = data::copy(elems);
copied->set("hostname-char-set", data::Element::create("^[A-Z]"));
copied->set("hostname-char-replacement", data::Element::create("x"));
Subnet4Ptr subnet;
Subnet4ConfigParser parser;
ASSERT_NO_THROW(subnet = parser.parse(copied));
EXPECT_EQ("^[A-Z]", subnet->getHostnameCharSet().get());
EXPECT_EQ("x", subnet->getHostnameCharReplacement().get());
}
}
// This test verifies the Subnet4 parser's validation logic for
// lease cache parameters.
TEST(CfgSubnets4Test, cacheParamValidation) {
// Describes a single test scenario.
struct Scenario {
std::string label; // label used for logging test failures
double threshold; // value of cache-threshold
std::string error_message; // expected error message is parsing should fail
};
// Test Scenarios.
std::vector<Scenario> tests = {
{"valid", .25, ""},
{"negative", -.25,
"subnet configuration failed: cache-threshold:"
" -0.25 is invalid, it must be greater than 0.0 and less than 1.0"
},
{"too big", 1.05,
"subnet configuration failed: cache-threshold:"
" 1.05 is invalid, it must be greater than 0.0 and less than 1.0"
}
};
// First we create a set of elements that provides all
// required for a Subnet4.
std::string json =
" {"
" \"id\": 1,\n"
" \"subnet\": \"10.1.2.0/24\", \n"
" \"interface\": \"\", \n"
" \"renew-timer\": 100, \n"
" \"rebind-timer\": 200, \n"
" \"valid-lifetime\": 300, \n"
" \"match-client-id\": false, \n"
" \"authoritative\": false, \n"
" \"next-server\": \"\", \n"
" \"server-hostname\": \"\", \n"
" \"boot-file-name\": \"\", \n"
" \"client-classes\": [], \n"
" \"evaluate-additional-classes\": [], \n"
" \"reservations-global\": false, \n"
" \"reservations-in-subnet\": true, \n"
" \"reservations-out-of-pool\": false, \n"
" \"4o6-interface\": \"\", \n"
" \"4o6-interface-id\": \"\", \n"
" \"4o6-subnet\": \"\" \n"
" }";
data::ElementPtr elems;
ASSERT_NO_THROW(elems = data::Element::fromJSON(json))
<< "invalid JSON:" << json << "\n test is broken";
// Iterate over the test scenarios, verifying each prescribed
// outcome.
for (auto const& test : tests) {
{
SCOPED_TRACE("test: " + test.label);
// Set this scenario's configuration parameters
elems->set("cache-threshold", data::Element::create(test.threshold));
Subnet4Ptr subnet;
try {
// Attempt to parse the configuration.
Subnet4ConfigParser parser;
subnet = parser.parse(elems);
} catch (const std::exception& ex) {
if (!test.error_message.empty()) {
// We expected a failure, did we fail the correct way?
EXPECT_EQ(test.error_message, ex.what());
} else {
// Should not have failed.
ADD_FAILURE() << "Scenario should not have failed: " << ex.what();
}
// Either way we're done with this scenario.
continue;
}
// We parsed correctly, make sure the values are right.
EXPECT_TRUE(util::areDoublesEquivalent(test.threshold, subnet->getCacheThreshold()));
}
}
}
// This test verifies that the optional interface check works as expected.
TEST(CfgSubnets4Test, iface) {
// Create a configuration.
std::string json =
" {"
" \"id\": 1,\n"
" \"subnet\": \"10.1.2.0/24\", \n"
" \"interface\": \"eth1961\"\n"
" }";
data::ElementPtr elems;
ASSERT_NO_THROW(elems = data::Element::fromJSON(json))
<< "invalid JSON:" << json << "\n test is broken";
// The interface check can be disabled.
Subnet4ConfigParser parser_no_check(false);
Subnet4Ptr subnet;
EXPECT_NO_THROW(subnet = parser_no_check.parse(elems));
ASSERT_TRUE(subnet);
EXPECT_FALSE(subnet->getIface().unspecified());
EXPECT_EQ("eth1961", subnet->getIface().get());
// Retry with the interface check enabled.
Subnet4ConfigParser parser;
EXPECT_THROW(parser.parse(elems), DhcpConfigError);
// Configure default test interfaces.
IfaceMgrTestConfig config(true);
EXPECT_NO_THROW(subnet = parser_no_check.parse(elems));
ASSERT_TRUE(subnet);
EXPECT_FALSE(subnet->getIface().unspecified());
EXPECT_EQ("eth1961", subnet->getIface().get());
EXPECT_NO_THROW(subnet = parser.parse(elems));
ASSERT_TRUE(subnet);
EXPECT_FALSE(subnet->getIface().unspecified());
EXPECT_EQ("eth1961", subnet->getIface().get());
}
// This test verifies that update statistics works as expected.
TEST(CfgSubnets4Test, updateStatistics) {
CfgMgr::instance().clear();
CfgSubnets4Ptr cfg = CfgMgr::instance().getCurrentCfg()->getCfgSubnets4();
ObservationPtr observation;
SubnetID subnet_id = 100;
LeaseMgrFactory::create("type=memfile universe=4 persist=false");
// remove all statistics
StatsMgr::instance().removeAll();
// Create subnet.
Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.2.0"), 26, 1, 2, 3, 100));
Pool4Ptr pool(new Pool4(IOAddress("192.0.2.0"), 26));
subnet->addPool(pool);
// Add subnet.
cfg->add(subnet);
observation = StatsMgr::instance().getObservation(
"cumulative-assigned-addresses");
ASSERT_FALSE(observation);
observation = StatsMgr::instance().getObservation(
"declined-addresses");
ASSERT_FALSE(observation);
observation = StatsMgr::instance().getObservation(
"reclaimed-declined-addresses");
ASSERT_FALSE(observation);
observation = StatsMgr::instance().getObservation(
"reclaimed-leases");
ASSERT_FALSE(observation);
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
"total-addresses"));
ASSERT_FALSE(observation);
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", 0, "total-addresses")));
ASSERT_FALSE(observation);
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
"assigned-addresses"));
ASSERT_FALSE(observation);
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", 0, "assigned-addresses")));
ASSERT_FALSE(observation);
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
"cumulative-assigned-addresses"));
ASSERT_FALSE(observation);
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", 0, "cumulative-assigned-addresses")));
ASSERT_FALSE(observation);
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
"declined-addresses"));
ASSERT_FALSE(observation);
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", 0, "declined-addresses")));
ASSERT_FALSE(observation);
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
"reclaimed-declined-addresses"));
ASSERT_FALSE(observation);
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", 0, "reclaimed-declined-addresses")));
ASSERT_FALSE(observation);
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
"reclaimed-leases"));
ASSERT_FALSE(observation);
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", 0, "reclaimed-leases")));
ASSERT_FALSE(observation);
cfg->updateStatistics();
observation = StatsMgr::instance().getObservation(
"cumulative-assigned-addresses");
ASSERT_TRUE(observation);
ASSERT_EQ(0, observation->getInteger().first);
observation = StatsMgr::instance().getObservation(
"declined-addresses");
ASSERT_TRUE(observation);
ASSERT_EQ(0, observation->getInteger().first);
observation = StatsMgr::instance().getObservation(
"reclaimed-declined-addresses");
ASSERT_TRUE(observation);
ASSERT_EQ(0, observation->getInteger().first);
observation = StatsMgr::instance().getObservation(
"reclaimed-leases");
ASSERT_TRUE(observation);
ASSERT_EQ(0, observation->getInteger().first);
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
"total-addresses"));
ASSERT_TRUE(observation);
ASSERT_EQ(64, observation->getInteger().first);
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", 0, "total-addresses")));
ASSERT_TRUE(observation);
ASSERT_EQ(64, observation->getInteger().first);
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
"assigned-addresses"));
ASSERT_TRUE(observation);
ASSERT_EQ(0, observation->getInteger().first);
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", 0, "assigned-addresses")));
ASSERT_TRUE(observation);
ASSERT_EQ(0, observation->getInteger().first);
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
"cumulative-assigned-addresses"));
ASSERT_TRUE(observation);
ASSERT_EQ(0, observation->getInteger().first);
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", 0, "cumulative-assigned-addresses")));
ASSERT_TRUE(observation);
ASSERT_EQ(0, observation->getInteger().first);
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
"declined-addresses"));
ASSERT_TRUE(observation);
ASSERT_EQ(0, observation->getInteger().first);
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", 0, "declined-addresses")));
ASSERT_TRUE(observation);
ASSERT_EQ(0, observation->getInteger().first);
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
"reclaimed-declined-addresses"));
ASSERT_TRUE(observation);
ASSERT_EQ(0, observation->getInteger().first);
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", 0, "reclaimed-declined-addresses")));
ASSERT_TRUE(observation);
ASSERT_EQ(0, observation->getInteger().first);
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
"reclaimed-leases"));
ASSERT_TRUE(observation);
ASSERT_EQ(0, observation->getInteger().first);
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", 0, "reclaimed-leases")));
ASSERT_TRUE(observation);
ASSERT_EQ(0, observation->getInteger().first);
}
// This test verifies that remove statistics works as expected.
TEST(CfgSubnets4Test, removeStatistics) {
CfgSubnets4 cfg;
ObservationPtr observation;
SubnetID subnet_id = 100;
// remove all statistics and then set them all to 0
StatsMgr::instance().removeAll();
// Create subnet.
Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.2.0"), 26, 1, 2, 3, 100));
Pool4Ptr pool(new Pool4(IOAddress("192.0.2.0"), 26));
subnet->addPool(pool);
// Add subnet.
cfg.add(subnet);
StatsMgr::instance().setValue(
StatsMgr::generateName("subnet", subnet_id,
"total-addresses"),
int64_t(0));
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
"total-addresses"));
ASSERT_TRUE(observation);
ASSERT_EQ(0, observation->getInteger().first);
StatsMgr::instance().setValue(
StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", 0, "total-addresses")),
int64_t(0));
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", 0, "total-addresses")));
ASSERT_TRUE(observation);
ASSERT_EQ(0, observation->getInteger().first);
StatsMgr::instance().setValue(
StatsMgr::generateName("subnet", subnet_id,
"assigned-addresses"),
int64_t(0));
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
"assigned-addresses"));
ASSERT_TRUE(observation);
ASSERT_EQ(0, observation->getInteger().first);
StatsMgr::instance().setValue(
StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", 0, "assigned-addresses")),
int64_t(0));
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", 0, "assigned-addresses")));
ASSERT_TRUE(observation);
ASSERT_EQ(0, observation->getInteger().first);
StatsMgr::instance().setValue(
StatsMgr::generateName("subnet", subnet_id,
"cumulative-assigned-addresses"),
int64_t(0));
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
"cumulative-assigned-addresses"));
ASSERT_TRUE(observation);
ASSERT_EQ(0, observation->getInteger().first);
StatsMgr::instance().setValue(
StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", 0, "cumulative-assigned-addresses")),
int64_t(0));
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", 0, "cumulative-assigned-addresses")));
ASSERT_TRUE(observation);
ASSERT_EQ(0, observation->getInteger().first);
StatsMgr::instance().setValue(
StatsMgr::generateName("subnet", subnet_id,
"declined-addresses"),
int64_t(0));
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
"declined-addresses"));
ASSERT_TRUE(observation);
ASSERT_EQ(0, observation->getInteger().first);
StatsMgr::instance().setValue(
StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", 0, "declined-addresses")),
int64_t(0));
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", 0, "declined-addresses")));
ASSERT_TRUE(observation);
ASSERT_EQ(0, observation->getInteger().first);
StatsMgr::instance().setValue(
StatsMgr::generateName("subnet", subnet_id,
"reclaimed-declined-addresses"),
int64_t(0));
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
"reclaimed-declined-addresses"));
ASSERT_TRUE(observation);
ASSERT_EQ(0, observation->getInteger().first);
StatsMgr::instance().setValue(
StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", 0, "reclaimed-declined-addresses")),
int64_t(0));
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", 0, "reclaimed-declined-addresses")));
ASSERT_TRUE(observation);
ASSERT_EQ(0, observation->getInteger().first);
StatsMgr::instance().setValue(
StatsMgr::generateName("subnet", subnet_id,
"reclaimed-leases"),
int64_t(0));
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
"reclaimed-leases"));
ASSERT_TRUE(observation);
ASSERT_EQ(0, observation->getInteger().first);
StatsMgr::instance().setValue(
StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", 0, "reclaimed-leases")),
int64_t(0));
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", 0, "reclaimed-leases")));
ASSERT_TRUE(observation);
ASSERT_EQ(0, observation->getInteger().first);
// remove all statistics
cfg.removeStatistics();
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
"total-addresses"));
ASSERT_FALSE(observation);
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", 0, "total-addresses")));
ASSERT_FALSE(observation);
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
"assigned-addresses"));
ASSERT_FALSE(observation);
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", 0, "assigned-addresses")));
ASSERT_FALSE(observation);
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
"cumulative-assigned-addresses"));
ASSERT_FALSE(observation);
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", 0, "cumulative-assigned-addresses")));
ASSERT_FALSE(observation);
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
"declined-addresses"));
ASSERT_FALSE(observation);
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", 0, "declined-addresses")));
ASSERT_FALSE(observation);
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
"reclaimed-declined-addresses"));
ASSERT_FALSE(observation);
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", 0, "reclaimed-declined-addresses")));
ASSERT_FALSE(observation);
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
"reclaimed-leases"));
ASSERT_FALSE(observation);
observation = StatsMgr::instance().getObservation(
StatsMgr::generateName("subnet", subnet_id,
StatsMgr::generateName("pool", 0, "reclaimed-leases")));
ASSERT_FALSE(observation);
}
// This test verifies that in range host reservation works as expected.
TEST(CfgSubnets4Test, host) {
// Create a configuration.
std::string json =
" {"
" \"id\": 1,\n"
" \"subnet\": \"10.1.2.0/24\", \n"
" \"reservations\": [ {\n"
" \"hw-address\": \"aa:bb:cc:dd:ee:ff\", \n"
" \"ip-address\": \"10.1.2.1\" } ]\n"
" }";
data::ElementPtr elems;
ASSERT_NO_THROW(elems = data::Element::fromJSON(json))
<< "invalid JSON:" << json << "\n test is broken";
Subnet4ConfigParser parser;
Subnet4Ptr subnet;
EXPECT_NO_THROW(subnet = parser.parse(elems));
ASSERT_TRUE(subnet);
CfgHostsPtr cfg_hosts = CfgMgr::instance().getStagingCfg()->getCfgHosts();
ASSERT_TRUE(cfg_hosts);
HostCollection hosts = cfg_hosts->getAll4(SubnetID(1));
ASSERT_EQ(1, hosts.size());
ConstHostPtr host = hosts[0];
ASSERT_TRUE(host);
EXPECT_EQ(1, host->getIPv4SubnetID());
EXPECT_EQ("hwaddr=AABBCCDDEEFF", host->getIdentifierAsText());
EXPECT_EQ("10.1.2.1", host->getIPv4Reservation().toText());
CfgMgr::instance().clear();
}
// This test verifies that an out of range host reservation is rejected.
TEST(CfgSubnets4Test, outOfRangeHost) {
// Create a configuration.
std::string json =
" {"
" \"id\": 1,\n"
" \"subnet\": \"10.1.2.0/24\", \n"
" \"reservations\": [ {\n"
" \"hw-address\": \"aa:bb:cc:dd:ee:ff\", \n"
" \"ip-address\": \"10.2.2.1\" } ]\n"
" }";
data::ElementPtr elems;
ASSERT_NO_THROW(elems = data::Element::fromJSON(json))
<< "invalid JSON:" << json << "\n test is broken";
Subnet4ConfigParser parser;
std::string msg = "specified reservation '10.2.2.1' is not within ";
msg += "the IPv4 subnet '10.1.2.0/24'";
EXPECT_THROW_MSG(parser.parse(elems), DhcpConfigError, msg);
}
// This test verifies that the getLinks tool works as expected.
TEST(CfgSubnets4Test, getLinks) {
CfgSubnets4 cfg;
// Create 4 subnets.
Subnet4Ptr subnet0(new Subnet4(IOAddress("0.0.0.0"),
24, 1, 2, 3, SubnetID(111)));
Subnet4Ptr subnet1(new Subnet4(IOAddress("192.0.1.0"),
26, 1, 2, 3, SubnetID(1)));
Subnet4Ptr subnet2(new Subnet4(IOAddress("192.0.2.0"),
26, 1, 2, 3, SubnetID(2)));
Subnet4Ptr subnet3(new Subnet4(IOAddress("192.0.3.0"),
26, 1, 2, 3, SubnetID(3)));
// Add all subnets to the configuration.
ASSERT_NO_THROW(cfg.add(subnet0));
ASSERT_NO_THROW(cfg.add(subnet1));
ASSERT_NO_THROW(cfg.add(subnet2));
ASSERT_NO_THROW(cfg.add(subnet3));
// No 192.0.4.0 subnet.
SubnetIDSet links;
EXPECT_NO_THROW(links = cfg.getLinks(IOAddress("192.0.4.0")));
EXPECT_TRUE(links.empty());
// A 192.0.2.0/26 subnet.
links.clear();
EXPECT_NO_THROW(links = cfg.getLinks(IOAddress("192.0.2.0")));
SubnetIDSet expected = { 2 };
EXPECT_EQ(expected, links);
// Check that any address in the subnet works.
links.clear();
EXPECT_NO_THROW(links = cfg.getLinks(IOAddress("192.0.2.23")));
EXPECT_EQ(expected, links);
// Check that an address outside the subnet does not work.
links.clear();
EXPECT_NO_THROW(links = cfg.getLinks(IOAddress("192.0.2.123")));
EXPECT_TRUE(links.empty());
// Add a second 192.0.2.0/26 subnet.
Subnet4Ptr subnet10(new Subnet4(IOAddress("192.0.2.10"),
26, 1, 2, 3, SubnetID(10)));
ASSERT_NO_THROW(cfg.add(subnet10));
// Now we should get 2 subnets.
links.clear();
EXPECT_NO_THROW(links = cfg.getLinks(IOAddress("192.0.2.0")));
expected = { 2, 10 };
EXPECT_EQ(expected, links);
// Add a larger subnet.
Subnet4Ptr subnet20(new Subnet4(IOAddress("192.0.2.20"),
24, 1, 2, 3, SubnetID(20)));
ASSERT_NO_THROW(cfg.add(subnet20));
// Now we should get 3 subnets.
links.clear();
EXPECT_NO_THROW(links = cfg.getLinks(IOAddress("192.0.2.0")));
expected = { 2, 10, 20 };
EXPECT_EQ(expected, links);
// But only the larger subnet if the address is only in it.
links.clear();
EXPECT_NO_THROW(links = cfg.getLinks(IOAddress("192.0.2.123")));
expected = { 20 };
EXPECT_EQ(expected, links);
// Even it is not used for Bulk Leasequery, it works for 0.0.0.0 too.
links.clear();
EXPECT_NO_THROW(links = cfg.getLinks(IOAddress("0.0.0.0")));
expected = { 111 };
EXPECT_EQ(expected, links);
}
// This test verifies that for each subnet in the configuration it calls
// the initAllocatorAfterConfigure function.
TEST(CfgSubnets4Test, initAllocatorsAfterConfigure) {
CfgSubnets4 cfg;
// Create 4 subnets.
Subnet4Ptr subnet0(new Subnet4(IOAddress("0.0.0.0"),
24, 1, 2, 3, SubnetID(111)));
Subnet4Ptr subnet1(new Subnet4(IOAddress("192.0.1.0"),
26, 1, 2, 3, SubnetID(1)));
Subnet4Ptr subnet2(new Subnet4(IOAddress("192.0.2.0"),
26, 1, 2, 3, SubnetID(2)));
Subnet4Ptr subnet3(new Subnet4(IOAddress("192.0.3.0"),
26, 1, 2, 3, SubnetID(3)));
auto allocator0 = boost::make_shared<InitRecordingAllocator>(Lease::TYPE_V4, subnet0);
subnet0->setAllocator(Lease::TYPE_V4, allocator0);
auto allocator2 = boost::make_shared<InitRecordingAllocator>(Lease::TYPE_V4, subnet2);
subnet2->setAllocator(Lease::TYPE_V4, allocator2);
cfg.add(subnet0);
cfg.add(subnet1);
cfg.add(subnet2);
cfg.add(subnet3);
EXPECT_EQ(0, allocator0->callcount_);
EXPECT_EQ(0, allocator2->callcount_);
cfg.initAllocatorsAfterConfigure();
EXPECT_EQ(1, allocator0->callcount_);
EXPECT_EQ(1, allocator2->callcount_);
}
/// @brief Test fixture for parsing v4 Subnets that can verify log output.
class Subnet4ParserTest : public LogContentTest {
public:
/// @brief virtual destructor
virtual ~Subnet4ParserTest() = default;
};
// This test verifies that subnet parser for IPv4 works properly
// when using invalid renew and rebind timers.
TEST_F(Subnet4ParserTest, parseWithInvalidRenewRebind) {
std::string config =
"{\n"
" \"id\": 1,\n"
" \"subnet\": \"10.1.2.0/24\",\n"
" \"valid-lifetime\": 399,\n"
" \"rebind-timer\": 199,\n"
" \"renew-timer\": 200\n"
"}";
// Basic configuration for subnet4 but with a renew-timer value
// larger than rebind-timer.
ElementPtr config_element = Element::fromJSON(config);
// Parse configuration specified above.
Subnet4ConfigParser parser(false);
Subnet4Ptr subnet;
// Parser should not throw.
ASSERT_NO_THROW(subnet = parser.parse(config_element));
ASSERT_TRUE(subnet);
// Veriy we emitted the proper log message.
addString("DHCPSRV_CFGMGR_RENEW_GTR_REBIND in subnet-id 1,"
" the value of renew-timer 200 is greater than the value"
" of rebind-timer 199, ignoring renew-timer");
EXPECT_TRUE(checkFile());
}
} // end of anonymous namespace
|