summaryrefslogtreecommitdiffstats
path: root/src/hooks/dhcp/lease_cmds/tests/lease_cmds4_unittest.cc
blob: 30e0a810efad0a29f6d67dcd51c1e24ee07a8021 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
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
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
// Copyright (C) 2017-2022 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 <exceptions/exceptions.h>
#include <hooks/hooks_manager.h>
#include <config/command_mgr.h>
#include <dhcpsrv/lease_mgr.h>
#include <dhcpsrv/lease_mgr_factory.h>
#include <dhcpsrv/ncr_generator.h>
#include <dhcpsrv/cfgmgr.h>
#include <dhcpsrv/resource_handler.h>
#include <cc/command_interpreter.h>
#include <cc/data.h>
#include <lease_cmds_unittest.h>
#include <stats/stats_mgr.h>
#include <testutils/user_context_utils.h>
#include <testutils/multi_threading_utils.h>

#include <gtest/gtest.h>

#include <errno.h>
#include <set>

using namespace std;
using namespace isc;
using namespace isc::hooks;
using namespace isc::config;
using namespace isc::data;
using namespace isc::dhcp;
using namespace isc::dhcp_ddns;
using namespace isc::asiolink;
using namespace isc::stats;
using namespace isc::test;

namespace {

/// @brief Class dedicated to testing v4 part of lease_cmds library.
///
/// Provides convenience methods for loading, testing all commands and
/// unloading the lease_cmds library.
class Lease4CmdsTest : public LeaseCmdsTest {
public:
    /// @brief Checks if specified response contains IPv4 lease
    ///
    /// @param lease Element tree that represents a lease
    /// @param ip expected IP address
    /// @param subnet_id expected subnet-id
    /// @param hwaddr expected value of hardware address
    /// @param client_id_required true if client-id is expected
    void checkLease4(isc::data::ConstElementPtr l, std::string ip,
                     uint32_t subnet_id, std::string hwaddr,
                     bool client_id_required) {
        ASSERT_TRUE(l);

        // If the element is a list we need to retrieve the lease that
        // we're interested in.
        if (l->getType() == isc::data::Element::list) {
            std::vector<isc::data::ElementPtr> e = l->listValue();
            for (auto it = e.begin(); it != e.end(); ++it) {
                isc::data::ConstElementPtr ip_address = (*it)->get("ip-address");
                if (ip_address && ip_address->stringValue() == ip) {
                    l = (*it);
                    break;
                }
            }

            ASSERT_TRUE(l);
        }

        ASSERT_TRUE(l->get("ip-address"));
        EXPECT_EQ(ip, l->get("ip-address")->stringValue());

        ASSERT_TRUE(l->get("subnet-id"));
        EXPECT_EQ(subnet_id, l->get("subnet-id")->intValue());

        ASSERT_TRUE(l->get("hw-address"));
        EXPECT_EQ(hwaddr, l->get("hw-address")->stringValue());

        // client-id may or may not appear
        if (client_id_required) {
            EXPECT_TRUE(l->get("client-id"));
        }

        // Check that other parameters are there.
        ASSERT_TRUE(l->contains("valid-lft"));
        ASSERT_TRUE(l->contains("cltt"));
        ASSERT_TRUE(l->contains("subnet-id"));
        ASSERT_TRUE(l->contains("state"));
        ASSERT_TRUE(l->contains("fqdn-fwd"));
        ASSERT_TRUE(l->contains("fqdn-rev"));
        ASSERT_TRUE(l->contains("hostname"));
        ASSERT_TRUE(l->contains("state"));

        // Check that there are no v6 specific fields
        ASSERT_FALSE(l->contains("prefix"));
        ASSERT_FALSE(l->contains("duid"));
        ASSERT_FALSE(l->contains("preferred-lft"));

        // Assuming that these values were used to create the lease.
        // If we ever want to test different values they will need to
        // be added as parameters to this function.
        EXPECT_EQ(HIGH_VALID_LIFETIME,
                  static_cast<uint32_t>(l->get("valid-lft")->intValue()));
        EXPECT_EQ(DEC_2030_TIME, l->get("cltt")->intValue());
    }

    /// @brief Check lease4 statistics.
    ///
    /// @param id Subnet id of the stats to check.
    /// @assigned The expected value of assigned addresses in subnet.
    /// @declined The expected value of declined addresses in subnet.
    void checkLease4Stats(isc::dhcp::SubnetID id, int32_t assigned, int32_t declined) {
        ASSERT_EQ(isc::stats::StatsMgr::instance().getObservation(
                isc::stats::StatsMgr::generateName("subnet", id,
                          "assigned-addresses"))->getInteger().first, assigned);

        ASSERT_EQ(isc::stats::StatsMgr::instance().getObservation(
                isc::stats::StatsMgr::generateName("subnet", id,
                          "declined-addresses"))->getInteger().first, declined);
    }

    /// @brief Check that lease4-add with missing parameters will fail.
    void testLease4AddMissingParams();

    /// @brief Verify that lease4-add can be rejected if parameters are
    /// specified, but have incorrect values.
    void testLease4AddBadParams();

    /// @brief Check that a simple, well formed lease4 can be added.
    void testLease4Add();

    /// @brief Check that a simple, well formed lease4 can be added.
    void testLease4AddDeclinedLeases();

    /// @brief Check that a lease4 is not added when it already exists.
    void testLease4AddExisting();

    /// @brief Check that subnet-id is optional. If not specified, Kea should
    /// select it on its own.
    void testLease4AddSubnetIdMissing();

    /// @brief Check that subnet-id is optional. If not specified, Kea should
    /// select it on its own.
    void testLease4AddSubnetIdMissingDeclinedLeases();

    /// @brief Check that subnet-id is optional. If not specified, Kea should
    /// select it on its own, but if there's no subnet for address being added,
    /// it should fail.
    void testLease4AddSubnetIdMissingBadAddr();

    /// @brief Check that the lease with negative expiration time is rejected.
    void testLease4AddNegativeExpireTime();

    /// @brief Check that the lease with negative cltt is rejected.
    void testLease4AddNegativeCltt();

    /// @brief Check that a well formed lease4 with tons of parameters can be
    /// added.
    void testLease4AddFullAddr();

    /// @brief Check that a well formed lease4 with a comment can be added.
    void testLease4AddComment();

    /// @brief Check that lease4-get can handle a situation when the query is
    /// broken (some required parameters are missing).
    void testLease4GetMissingParams();

    /// @brief Check that lease4-get sanitizes its input.
    void testLease4GetByAddrBadParam();

    /// @brief Check that lease4-get can handle a situation when the query is
    /// correctly formed, but the lease is not there.
    void testLease4GetByAddrNotFound();

    /// @brief Check that lease4-get can return a lease by address.
    void testLease4GetByAddr();

    /// @brief Check that lease4-get can handle a situation when the query is
    /// correctly formed, but the lease is not there.
    void testLease4GetByHWAddrNotFound();

    /// @brief Check that lease4-get can find a lease by hardware address.
    void testLease4GetByHWAddr();

    /// @brief Check that lease4-get can handle a situation when the query is
    /// correctly formed, but the lease is not there.
    void testLease4GetByClientIdNotFound();

    /// @brief Check that lease4-get can find a lease by client identifier.
    void testLease4GetByClientId();

    /// @brief Check that lease4-get-all returns all leases.
    void testLease4GetAll();

    /// @brief Check that lease4-get-all returns empty set if no leases are
    /// found.
    void testLease4GetAllNoLeases();

    /// @brief Check that lease4-get-all returns all leases for a subnet.
    void testLease4GetAllBySubnetId();

    /// @brief Check that lease4-get-all returns empty set when no leases are
    /// found.
    void testLease4GetAllBySubnetIdNoLeases();

    /// @brief Check that lease4-get-all returns leases from multiple subnets.
    void testLease4GetAllByMultipleSubnetIds();

    /// @brief Check that lease4-get-all checks its input arguments.
    void testLease4GetBySubnetIdInvalidArguments();

    /// @brief Check that multiple calls to lease4-get-page return all leases.
    void testLease4GetPaged();

    /// @brief Verifies that first page of IPv4 leases can be retrieved by
    /// specifying zero IPv4 address.
    void testLease4GetPagedZeroAddress();

    /// @brief Verifies that IPv6 address as a start address is rejected.
    void testLease4GetPagedIPv6Address();

    /// @brief Verifies that value of 'from' parameter other than 'start' or an
    /// IPv4 address is rejected.
    void testLease4GetPagedInvalidFrom();

    /// @brief Verifies that limit is mandatory.
    void testLease4GetPagedNoLimit();

    /// @brief Verifies that the limit must be a number.
    void testLease4GetPagedLimitNotNumber();

    /// @brief Check that lease4-get-by-hw-address can handle a situation when
    /// the query is broken (required parameter is missing).
    void testLease4GetByHwAddressParams();

    /// @brief Check that lease4-get-by-hw-address works as expected (find no
    /// lease).
    void testLease4GetByHwAddressFind0();

    /// @brief Check that lease4-get-by-hw-address works as expected (find two
    /// leases).
    void testLease4GetByHwAddressFind2();

    /// @brief Check that lease4-get-by-client-id can handle a situation when
    /// the query is broken (required parameter is missing).
    void testLease4GetByClientIdParams();

    /// @brief Check that lease4-get-by-client-id works as expected (find no
    /// lease).
    void testLease4GetByClientIdFind0();

    /// @brief Check that lease4-get-by-client-id works as expected (find two
    /// leases).
    void testLease4GetByClientIdFind2();

    /// @brief Verifies that the limit of 0 is rejected.
    void testLease4GetPagedLimitIsZero();

    /// @brief Check that lease4-get-by-hostname can handle a situation when
    /// the query is broken (required parameter is missing).
    void testLease4GetByHostnameParams();

    /// @brief Check that lease4-get-by-hostname works as expected (find no
    /// lease).
    void testLease4GetByHostnameFind0();

    /// @brief Check that lease4-get-by-hostname works as expected (find two
    /// leases).
    void testLease4GetByHostnameFind2();

    /// @brief Test checks if lease4-update handler refuses calls with missing
    /// parameters.
    void testLease4UpdateMissingParams();

    /// @brief Verify that lease4-update can be rejected if parameters are
    /// specified, but have incorrect values.
    void testLease4UpdateBadParams();

    /// @brief Check that lease4-update correctly handles case when there is no
    /// lease to be updated.
    void testLease4UpdateNoLease();

    /// @brief Check that a lease4 can be updated. We're changing hw-address and
    /// a hostname.
    void testLease4Update();

    /// @brief Check that a lease4 can be updated. We're changing hw-address and
    /// a hostname.
    void testLease4UpdateDeclinedLeases();

    /// @brief Check that a lease4 can be updated. We're changing hw-address and
    /// a hostname. The subnet-id is not specified.
    void testLease4UpdateNoSubnetId();

    /// @brief Check that a lease4 can be updated. We're changing hw-address and
    /// a hostname. The subnet-id is not specified.
    void testLease4UpdateNoSubnetIdDeclinedLeases();

    /// @brief Check that a lease4 is created if it doesn't exist during the
    /// update. To trigger this behavior 'force-create' boolean parameter must
    /// be included in the command.
    void testLease4UpdateForceCreate();

    /// @brief Check that a lease4 is created if it doesn't exist during the
    /// update. To trigger this behavior 'force-create' boolean parameter must
    /// be included in the command. The subnet-id is not specified, Kea will
    /// figure it out.
    void testLease4UpdateForceCreateNoSubnetId();

    /// @brief Check that lease4-update correctly handles case when the
    /// 'force-create' parameter is explicitly set to false.
    void testLease4UpdateDoNotForceCreate();

    /// @brief Check that a lease4 can be updated. We're adding a comment and an
    /// user context.
    void testLease4UpdateComment();

    /// @brief Check that lease4-del can handle a situation when the query is
    /// broken (some required parameters are missing).
    void testLease4DelMissingParams();

    /// @brief Check that lease4-del can handle a situation when the query is
    /// valid, but the lease is not there.
    void testLease4DelByAddrNotFound();

    /// @brief Check that lease4-del sanitizes its input.
    void testLease4DelByAddrBadParam();

    /// @brief Check that lease4-del can handle a situation when the query is
    /// correctly formed and the lease is deleted.
    void testLease4DelByAddr();

    /// @brief Check that lease4-del can handle a situation when the query is
    /// correctly formed and the lease is deleted.
    void testLease4DelByAddrDeclinedLeases();

    /// @brief Check that lease4-del can handle a situation when the query is
    /// correctly formed, but the lease is not there.
    void testLease4DelByHWAddrNotFound();

    /// @brief Check that lease4-del can find and delete a lease by hardware
    /// address.
    void testLease4DelByHWAddr();

    /// @brief Check that lease4-del can handle a situation when the query is
    /// correctly formed, but the lease is not there.
    void testLease4DelByClientIdNotFound();

    /// @brief Check that lease4-del can find and delete a lease by client
    /// identifier.
    void testLease4DelByClientId();

    /// @brief Check that lease4-wipe can remove leases.
    void testLease4Wipe();

    /// @brief Check that lease4-wipe can remove leases from all subnets at
    /// once.
    void testLease4WipeAll();

    /// @brief Check that lease4-wipe can remove leases from all subnets at
    /// once (when no parameters are specified).
    void testLease4WipeAllNoArgs();

    /// @brief Check that lease4-wipe properly reports when no leases were
    /// deleted.
    void testLease4WipeNoLeases();

    /// @brief Check that lease4-wipe properly reports when no leases were
    /// deleted.
    void testLease4WipeNoLeasesAll();

    /// @brief Check that an attempt to update a lease (set incorrect
    /// subnet-id) will fail.
    void testLease4BrokenUpdate();

    /// @brief Check that lease4-resend-ddns sanitizes its input.
    void testLease4ResendDdnsBadParam();

    /// @brief Check that lease4-resend-ddns does not generate an NCR for given
    /// lease when DDNS updating is disabled.
    void testLease4ResendDdnsDisabled();

    /// @brief Check that lease4-resend-ddns does not generate an NCR for when
    /// there is no matching lease.
    void testLease4ResendDdnsNoLease();

    /// @brief Check that lease4-resend-ddns does not generate an NCR for given
    /// lease when updates are enabled but Lease::hostname_ is blank.
    void testLease4ResendNoHostname();

    /// @brief Check that lease4-resend-ddns does not generate an NCR for given
    /// lease when updates are enabled, Lease::hostname_ is not blank, but both
    /// Lease::fqdn_fwd_ and fdqn_rev_ are false.
    void testLease4ResendNoDirectionsEnabled();

    /// @brief Check that lease4-resend-ddns can generate an NCR for given
    /// lease when updates are enabled, Lease::hostname_ is not blank, and at
    /// least one of Lease::fqdn_fwd_ or fdqn_rev_ are true.
    void testLease4ResendDdnsEnabled();

    /// @brief Check that lease4-del does (or does not) generate an NCR to
    /// remove DNS for a given lease based on lease content when DDNS updates
    /// are enabled.
    void testLease4DnsRemoveD2Enabled();

    /// @brief Check that lease4-del does not generate an NCR to remove DNS for
    /// a given lease based on lease content when DDNS updates are disabled.
    void testLease4DnsRemoveD2Disabled();

    /// @brief Verify that v4 lease add handles conflict as expected.
    void testLease4ConflictingAdd();

    /// @brief Verify that v4 lease update handles conflict as expected.
    void testLease4ConflictingUpdate();
};

void Lease4CmdsTest::testLease4AddMissingParams() {
    // Initialize lease manager (false = v4, false = don't add leases)
    initLeaseMgr(false, false);

    // Everything missing. What sort of crap is that?
    string txt =
        "{\n"
        "    \"command\": \"lease4-add\",\n"
        "    \"arguments\": {"
        "    }\n"
        "}";
    string exp_rsp = "missing parameter 'ip-address' (<string>:3:19)";
    testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp);

    // Just ip is not enough (subnet-id and hwaddr missing, although
    // subnet-id can now be figured out by Kea code)
    txt =
        "{\n"
        "    \"command\": \"lease4-add\",\n"
        "    \"arguments\": {"
        "            \"ip-address\": \"192.0.2.123\"\n"
        "    }\n"
        "}";
    exp_rsp = "missing parameter 'hw-address' (<string>:3:19)";
    testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp);

    // Better, but still no luck. (hwaddr missing).
    txt =
        "{\n"
        "    \"command\": \"lease4-add\",\n"
        "    \"arguments\": {"
        "            \"subnet-id\": 44,\n"
        "            \"ip-address\": \"192.0.2.202\"\n"
        "    }\n"
        "}";
    exp_rsp = "missing parameter 'hw-address' (<string>:3:19)";
    testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp);

    // Close, but no cigars. (ip-address missing).
    txt =
        "{\n"
        "    \"command\": \"lease4-add\",\n"
        "    \"arguments\": {"
        "            \"subnet-id\": 44,\n"
        "            \"hw-address\": \"1a:1b:1c:1d:1e:1f\"\n"
        "    }\n"
        "}";
    exp_rsp = "missing parameter 'ip-address' (<string>:3:19)";
    testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp);
}

void Lease4CmdsTest::testLease4AddBadParams() {
    // Initialize lease manager (false = v4, false = don't add leases)
    initLeaseMgr(false, false);

    // All params are there, but there's no subnet-id 123 configured.
    // (initLeaseMgr initialized subnet-id 44 for v4 and subnet-id 66 for v6).
    string txt =
        "{\n"
        "    \"command\": \"lease4-add\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 123,\n"
        "        \"ip-address\": \"192.0.2.202\",\n"
        "        \"hw-address\": \"1a:1b:1c:1d:1e:1f\"\n"
        "    }\n"
        "}";
    string exp_rsp = "Invalid subnet-id: No IPv4 subnet with subnet-id=123 currently configured.";
    testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp);

    // This time the IP address does not belong to the subnet.
    txt =
        "{\n"
        "    \"command\": \"lease4-add\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 44,\n"
        "        \"ip-address\": \"10.0.0.1\",\n"
        "        \"hw-address\": \"1a:1b:1c:1d:1e:1f\"\n"
        "    }\n"
        "}";
    exp_rsp = "The address 10.0.0.1 does not belong to subnet 192.0.2.0/24, subnet-id=44";
    testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp);

    // We don't use any of that bleeding edge nonsense in this museum. v4 only.
    txt =
        "{\n"
        "    \"command\": \"lease4-add\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 44,\n"
        "        \"ip-address\": \"2001:db8:1::1\",\n"
        "        \"hw-address\": \"1a:1b:1c:1d:1e:1f\"\n"
        "    }\n"
        "}";
    exp_rsp = "Non-IPv4 address specified: 2001:db8:1::1";
    testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp);

    // currently defined states are 0,1 and 2. 123 is junk.
    txt =
        "{\n"
        "    \"command\": \"lease4-add\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 44,\n"
        "        \"ip-address\": \"192.0.2.1\",\n"
        "        \"hw-address\": \"1a:1b:1c:1d:1e:1f\",\n"
        "        \"state\": 123\n"
        "    }\n"
        "}";
    exp_rsp = "Invalid state value: 123, supported values are: 0 (default), 1 "
        "(declined) and 2 (expired-reclaimed)";
    testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp);

    // Bad user context: not a map.
    txt =
        "{\n"
        "    \"command\": \"lease4-add\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 44,\n"
        "        \"ip-address\": \"192.0.2.1\",\n"
        "        \"hw-address\": \"1a:1b:1c:1d:1e:1f\",\n"
        "        \"user-context\": \"bad value\"\n"
        "    }\n"
        "}";
    exp_rsp = "Invalid user context '\"bad value\"' is not a JSON map.";
    testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp);

    // Duplicated comment.
    txt =
        "{\n"
        "    \"command\": \"lease4-add\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 44,\n"
        "        \"ip-address\": \"192.0.2.1\",\n"
        "        \"hw-address\": \"1a:1b:1c:1d:1e:1f\",\n"
        "        \"user-context\": { \"comment\": \"in user context\" },\n"
        "        \"comment\": \"direct\"\n"
        "    }\n"
        "}";
    exp_rsp = "Duplicated comment entry '\"direct\"' in user context "
        "'{ \"comment\": \"in user context\" }'";
    testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp);
}

void Lease4CmdsTest::testLease4Add() {
    // Initialize lease manager (false = v4, false = don't add leases)
    initLeaseMgr(false, false);

    checkLease4Stats(44, 0, 0);

    checkLease4Stats(88, 0, 0);

    // Now send the command.
    string txt =
        "{\n"
        "    \"command\": \"lease4-add\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 44,\n"
        "        \"ip-address\": \"192.0.2.202\",\n"
        "        \"hw-address\": \"1a:1b:1c:1d:1e:1f\"\n"
        "    }\n"
        "}";
    string exp_rsp = "Lease for address 192.0.2.202, subnet-id 44 added.";
    testCommand(txt, CONTROL_RESULT_SUCCESS, exp_rsp);

    checkLease4Stats(44, 1, 0);

    checkLease4Stats(88, 0, 0);

    // Now check that the lease is really there.
    Lease4Ptr l = lmptr_->getLease4(IOAddress("192.0.2.202"));
    ASSERT_TRUE(l);

    // Make sure the lease has proper value set.
    ASSERT_TRUE(l->hwaddr_);
    EXPECT_EQ("1a:1b:1c:1d:1e:1f", l->hwaddr_->toText(false));
    EXPECT_EQ(3, l->valid_lft_); // taken from subnet configuration
    EXPECT_FALSE(l->fqdn_fwd_);
    EXPECT_FALSE(l->fqdn_rev_);
    EXPECT_EQ("", l->hostname_);
    EXPECT_FALSE(l->getContext());

    // Test execution is fast. The cltt should be set to now. In some rare
    // cases we could have the seconds counter to tick, so having a value off
    // by one is ok.
    EXPECT_LE(abs(l->cltt_ - time(NULL)), 1);
    EXPECT_EQ(0, l->state_);
}

void Lease4CmdsTest::testLease4AddDeclinedLeases() {
    // Initialize lease manager (false = v4, false = don't add leases)
    initLeaseMgr(false, false);

    checkLease4Stats(44, 0, 0);

    checkLease4Stats(88, 0, 0);

    // Now send the command.
    string txt =
        "{\n"
        "    \"command\": \"lease4-add\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 44,\n"
        "        \"ip-address\": \"192.0.2.202\",\n"
        "        \"state\": 1,\n"
        "        \"hw-address\": \"1a:1b:1c:1d:1e:1f\"\n"
        "    }\n"
        "}";
    string exp_rsp = "Lease for address 192.0.2.202, subnet-id 44 added.";
    testCommand(txt, CONTROL_RESULT_SUCCESS, exp_rsp);

    checkLease4Stats(44, 1, 1);

    checkLease4Stats(88, 0, 0);

    // Now check that the lease is really there.
    Lease4Ptr l = lmptr_->getLease4(IOAddress("192.0.2.202"));
    ASSERT_TRUE(l);

    // Make sure the lease have proper value set.
    ASSERT_TRUE(l->hwaddr_);
    EXPECT_EQ("1a:1b:1c:1d:1e:1f", l->hwaddr_->toText(false));
    EXPECT_EQ(3, l->valid_lft_); // taken from subnet configuration
    EXPECT_FALSE(l->fqdn_fwd_);
    EXPECT_FALSE(l->fqdn_rev_);
    EXPECT_EQ("", l->hostname_);
    EXPECT_FALSE(l->getContext());

    // Test execution is fast. The cltt should be set to now. In some rare
    // cases we could have the seconds counter to tick, so having a value off
    // by one is ok.
    EXPECT_LE(abs(l->cltt_ - time(NULL)), 1);
    EXPECT_EQ(1, l->state_);
}

void Lease4CmdsTest::testLease4AddExisting() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    checkLease4Stats(44, 2, 0);

    checkLease4Stats(88, 2, 0);

    // Now send the command.
    string txt =
        "{\n"
        "    \"command\": \"lease4-add\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 44,\n"
        "        \"ip-address\": \"192.0.2.1\",\n"
        "        \"hw-address\": \"1a:1b:1c:1d:1e:1f\"\n"
        "    }\n"
        "}";
    string exp_rsp = "IPv4 lease already exists.";
    testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp);

    checkLease4Stats(44, 2, 0);

    checkLease4Stats(88, 2, 0);
}

void Lease4CmdsTest::testLease4AddSubnetIdMissing() {
    // Initialize lease manager (false = v4, false = don't add leases)
    initLeaseMgr(false, false);

    checkLease4Stats(44, 0, 0);

    checkLease4Stats(88, 0, 0);

    // Now send the command without subnet-id. Kea should select
    // the subnet id on its own.
    string txt =
        "{\n"
        "    \"command\": \"lease4-add\",\n"
        "    \"arguments\": {"
        "        \"ip-address\": \"192.0.2.202\",\n"
        "        \"hw-address\": \"1a:1b:1c:1d:1e:1f\"\n"
        "    }\n"
        "}";
    string exp_rsp = "Lease for address 192.0.2.202, subnet-id 44 added.";
    testCommand(txt, CONTROL_RESULT_SUCCESS, exp_rsp);

    checkLease4Stats(44, 1, 0);

    checkLease4Stats(88, 0, 0);

    // Now check that the lease is really there.
    Lease4Ptr l = lmptr_->getLease4(IOAddress("192.0.2.202"));
    ASSERT_TRUE(l);
    EXPECT_EQ(44, l->subnet_id_);
}

void Lease4CmdsTest::testLease4AddSubnetIdMissingDeclinedLeases() {
    // Initialize lease manager (false = v4, false = don't add leases)
    initLeaseMgr(false, false);

    checkLease4Stats(44, 0, 0);

    checkLease4Stats(88, 0, 0);

    // Now send the command without subnet-id. Kea should select
    // the subnet id on its own.
    string txt =
        "{\n"
        "    \"command\": \"lease4-add\",\n"
        "    \"arguments\": {"
        "        \"ip-address\": \"192.0.2.202\",\n"
        "        \"state\": 1,\n"
        "        \"hw-address\": \"1a:1b:1c:1d:1e:1f\"\n"
        "    }\n"
        "}";
    string exp_rsp = "Lease for address 192.0.2.202, subnet-id 44 added.";
    testCommand(txt, CONTROL_RESULT_SUCCESS, exp_rsp);

    checkLease4Stats(44, 1, 1);

    checkLease4Stats(88, 0, 0);

    // Now check that the lease is really there.
    Lease4Ptr l = lmptr_->getLease4(IOAddress("192.0.2.202"));
    ASSERT_TRUE(l);
    EXPECT_EQ(44, l->subnet_id_);
}

void Lease4CmdsTest::testLease4AddSubnetIdMissingBadAddr() {
    // Initialize lease manager (false = v4, false = don't add leases)
    initLeaseMgr(false, false);

    checkLease4Stats(44, 0, 0);

    checkLease4Stats(88, 0, 0);

    // Now send the command without subnet-id. Kea should select
    // the subnet id on its own.
    string txt =
        "{\n"
        "    \"command\": \"lease4-add\",\n"
        "    \"arguments\": {"
        "        \"ip-address\": \"192.0.55.1\",\n"
        "        \"hw-address\": \"1a:1b:1c:1d:1e:1f\"\n"
        "    }\n"
        "}";
    string exp_rsp = "subnet-id not specified and failed to find a subnet for "
                     "address 192.0.55.1";
    testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp);

    checkLease4Stats(44, 0, 0);

    checkLease4Stats(88, 0, 0);

    // Now check that the lease was not added.
    Lease4Ptr l = lmptr_->getLease4(IOAddress("192.0.55.1"));
    ASSERT_FALSE(l);
}

void Lease4CmdsTest::testLease4AddNegativeExpireTime() {
    // Initialize lease manager (false = v4, false = don't add leases)
    initLeaseMgr(false, false);

    checkLease4Stats(44, 0, 0);

    checkLease4Stats(88, 0, 0);

    // Add a lease with negative expiration time.
    string txt =
        "{\n"
        "    \"command\": \"lease4-add\",\n"
        "    \"arguments\": {"
        "        \"ip-address\": \"192.0.2.202\",\n"
        "        \"hw-address\": \"1a:1b:1c:1d:1e:1f\",\n"
        "        \"expire\": -6218189367\n"
        "    }\n"
        "}";
    string exp_rsp = "expiration time must be positive for address 192.0.2.202";
    testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp);

    checkLease4Stats(44, 0, 0);

    checkLease4Stats(88, 0, 0);

    // Now check that the lease was not added.
    Lease4Ptr l = lmptr_->getLease4(IOAddress("192.0.2.202"));
    ASSERT_FALSE(l);
}

void Lease4CmdsTest::testLease4AddNegativeCltt() {
    // Initialize lease manager (false = v4, false = don't add leases)
    initLeaseMgr(false, false);

    checkLease4Stats(44, 0, 0);

    checkLease4Stats(88, 0, 0);

    // Add a lease with negative cltt (expiration time - valid lifetime)
    string txt =
        "{\n"
        "    \"command\": \"lease4-add\",\n"
        "    \"arguments\": {"
        "        \"ip-address\": \"192.0.2.202\",\n"
        "        \"hw-address\": \"1a:1b:1c:1d:1e:1f\",\n"
        "        \"expire\": 123456,\n"
        "        \"valid-lft\": 123457"
        "    }\n"
        "}";
    string exp_rsp = "expiration time must be greater than valid lifetime for "
        "address 192.0.2.202";
    testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp);

    checkLease4Stats(44, 0, 0);

    checkLease4Stats(88, 0, 0);

    // Now check that the lease was not added.
    Lease4Ptr l = lmptr_->getLease4(IOAddress("192.0.2.202"));
    ASSERT_FALSE(l);
}

void Lease4CmdsTest::testLease4AddFullAddr() {
    // Initialize lease manager (false = v4, false = don't add leases)
    initLeaseMgr(false, false);

    checkLease4Stats(44, 0, 0);

    checkLease4Stats(88, 0, 0);

    // Now send the command.
    string txt =
        "{\n"
        "    \"command\": \"lease4-add\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 44,\n"
        "        \"ip-address\": \"192.0.2.202\",\n"
        "        \"hw-address\": \"1a:1b:1c:1d:1e:1f\",\n"
        "        \"client-id\": \"01:02:03:04:05:06:07:08\",\n"
        "        \"valid-lft\": 1000,\n"
        "        \"expire\": 6218189367,\n"
        "        \"fqdn-fwd\": true,\n"
        "        \"fqdn-rev\": true,\n"
        "        \"hostname\": \"urania.example.org\",\n"
        "        \"user-context\": { \"foobar\": true }\n"
        "    }\n"
        "}";
    string exp_rsp = "Lease for address 192.0.2.202, subnet-id 44 added.";
    testCommand(txt, CONTROL_RESULT_SUCCESS, exp_rsp);

    checkLease4Stats(44, 1, 0);

    checkLease4Stats(88, 0, 0);

    // Now check that the lease is really there.
    Lease4Ptr l = lmptr_->getLease4(IOAddress("192.0.2.202"));
    ASSERT_TRUE(l);
    EXPECT_EQ("192.0.2.202", l->addr_.toText());
    ASSERT_TRUE(l->hwaddr_);
    EXPECT_EQ("1a:1b:1c:1d:1e:1f", l->hwaddr_->toText(false));
    ASSERT_TRUE(l->client_id_);
    EXPECT_EQ("01:02:03:04:05:06:07:08", l->client_id_->toText());
    // expire (6218189367) - valid_lft(1000)
    EXPECT_EQ(static_cast<time_t>(6218189367 - 1000), l->cltt_);
    EXPECT_EQ(true, l->fqdn_fwd_);
    EXPECT_EQ(true, l->fqdn_rev_);
    EXPECT_EQ("urania.example.org", l->hostname_);
    ASSERT_TRUE(l->getContext());
    EXPECT_EQ("{ \"foobar\": true }", l->getContext()->str());
}

void Lease4CmdsTest::testLease4AddComment() {
    // Initialize lease manager (false = v4, false = don't add leases)
    initLeaseMgr(false, false);

    checkLease4Stats(44, 0, 0);

    checkLease4Stats(88, 0, 0);

    // Now send the command.
    string txt =
        "{\n"
        "    \"command\": \"lease4-add\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 44,\n"
        "        \"ip-address\": \"192.0.2.202\",\n"
        "        \"hw-address\": \"1a:1b:1c:1d:1e:1f\",\n"
        "        \"comment\": \"a comment\"\n"
        "    }\n"
            "}";
    string exp_rsp = "Lease for address 192.0.2.202, subnet-id 44 added.";
    testCommand(txt, CONTROL_RESULT_SUCCESS, exp_rsp);

    checkLease4Stats(44, 1, 0);

    checkLease4Stats(88, 0, 0);

    // Now check that the lease is really there.
    Lease4Ptr l = lmptr_->getLease4(IOAddress("192.0.2.202"));
    ASSERT_TRUE(l);

    // Make sure the lease have proper value set.
    ASSERT_TRUE(l->hwaddr_);
    EXPECT_EQ("1a:1b:1c:1d:1e:1f", l->hwaddr_->toText(false));
    ASSERT_TRUE(l->getContext());
    EXPECT_EQ("{ \"comment\": \"a comment\" }", l->getContext()->str());
}

void Lease4CmdsTest::testLease4GetMissingParams() {
    // No parameters whatsoever. You want just a lease, any lease?
    string cmd =
        "{\n"
        "    \"command\": \"lease4-get\",\n"
        "    \"arguments\": {"
        "    }\n"
        "}";
    string exp_rsp = "Mandatory 'subnet-id' parameter missing.";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);

    // Just the subnet-id won't cut it, either.
    cmd =
        "{\n"
        "    \"command\": \"lease4-get\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 123"
        "    }\n"
        "}";
    exp_rsp = "No 'ip-address' provided and 'identifier-type' is either missing or not a string.";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);

    // We can't identify your laptop by color. Sorry, buddy.
    cmd =
        "{\n"
        "    \"command\": \"lease4-get\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 123,\n"
        "        \"identifier-type\": \"color\",\n"
        "        \"identifier\": \"blue\"\n"
        "    }\n"
        "}";
    exp_rsp = "Incorrect identifier type: color, the only supported values are: "
        "address, hw-address, duid";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);

    // Query by DUID is not supported in v4. Sorry.
    cmd =
        "{\n"
        "    \"command\": \"lease4-get\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 123,\n"
        "        \"identifier-type\": \"duid\",\n"
        "        \"identifier\": \"01:01:01:01:01:01\"\n"
        "    }\n"
        "}";
    exp_rsp = "Query by duid is not allowed in v4.";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);

    // Identifier value is missing.
    cmd =
        "{\n"
        "    \"command\": \"lease4-get\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 123,\n"
        "        \"identifier-type\": \"hw-address\"\n"
        "    }\n"
        "}";
    exp_rsp = "No 'ip-address' provided and 'identifier' is either missing or not a string.";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);

    // Identifier-type is missing.
    cmd =
        "{\n"
        "    \"command\": \"lease4-get\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 123,\n"
        "        \"identifier\": \"01:02:03:04:05\"\n"
        "    }\n"
        "}";
    exp_rsp = "No 'ip-address' provided and 'identifier-type' is either missing or not a string.";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
}

void Lease4CmdsTest::testLease4GetByAddrBadParam() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    // Invalid family
    string cmd =
        "{\n"
        "    \"command\": \"lease4-get\",\n"
        "    \"arguments\": {"
        "        \"ip-address\": \"2001:db8:1::1\""
        "    }\n"
        "}";
    string exp_rsp = "Invalid IPv4 address specified: 2001:db8:1::1";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);

    // This is way off
    cmd =
        "{\n"
        "    \"command\": \"lease4-get\",\n"
        "    \"arguments\": {"
        "        \"ip-address\": \"221B Baker St.\""
        "    }\n"
        "}";
    exp_rsp = "Failed to convert string to address '221B Baker St.': Invalid argument";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
}

void Lease4CmdsTest::testLease4GetByAddrNotFound() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    // Invalid
    string cmd =
        "{\n"
        "    \"command\": \"lease4-get\",\n"
        "    \"arguments\": {"
        "        \"ip-address\": \"192.0.2.5\","
        "        \"subnet-id\": 44"
        "    }\n"
        "}";
    string exp_rsp = "Lease not found.";
    ConstElementPtr rsp = testCommand(cmd, CONTROL_RESULT_EMPTY, exp_rsp);
}

void Lease4CmdsTest::testLease4GetByAddr() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    // Query for valid, existing lease.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-get\",\n"
        "    \"arguments\": {"
        "        \"ip-address\": \"192.0.2.1\""
        "    }\n"
        "}";
    string exp_rsp = "IPv4 lease found.";
    ConstElementPtr rsp = testCommand(cmd, CONTROL_RESULT_SUCCESS, exp_rsp);

    // Now check that the lease parameters were indeed returned.
    ASSERT_TRUE(rsp);
    ConstElementPtr lease = rsp->get("arguments");
    ASSERT_TRUE(lease);

    // Let's check if the response makes any sense.
    checkLease4(lease, "192.0.2.1", 44, "08:08:08:08:08:08", true);
}

void Lease4CmdsTest::testLease4GetByHWAddrNotFound() {
    // Initialize lease manager (false = v4, false = don't add leases)
    initLeaseMgr(false, false);

    // No such lease.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-get\",\n"
        "    \"arguments\": {"
        "        \"identifier-type\": \"hw-address\","
        "        \"identifier\": \"01:02:03:04:05:06\","
        "        \"subnet-id\": 44"
        "    }\n"
        "}";
    string exp_rsp = "Lease not found.";
    ConstElementPtr rsp = testCommand(cmd, CONTROL_RESULT_EMPTY, exp_rsp);
}

void Lease4CmdsTest::testLease4GetByHWAddr() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    // Invalid
    string cmd =
        "{\n"
        "    \"command\": \"lease4-get\",\n"
        "    \"arguments\": {"
        "        \"identifier-type\": \"hw-address\","
        "        \"identifier\": \"08:08:08:08:08:08\","
        "        \"subnet-id\": 44"
        "    }\n"
        "}";
    string exp_rsp = "IPv4 lease found.";
    ConstElementPtr rsp = testCommand(cmd, CONTROL_RESULT_SUCCESS, exp_rsp);

    // Now check that the lease parameters were indeed returned.
    ASSERT_TRUE(rsp);
    ConstElementPtr lease = rsp->get("arguments");
    ASSERT_TRUE(lease);

    // Let's check if the response makes any sense.
    checkLease4(lease, "192.0.2.1", 44, "08:08:08:08:08:08", false);
}

void Lease4CmdsTest::testLease4GetByClientIdNotFound() {
    // Initialize lease manager (false = v4, false = don't add leases)
    initLeaseMgr(false, false);

    // No such lease.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-get\",\n"
        "    \"arguments\": {"
        "        \"identifier-type\": \"client-id\","
        "        \"identifier\": \"01:02:03:04\","
        "        \"subnet-id\": 44"
        "    }\n"
        "}";
    string exp_rsp = "Lease not found.";
    ConstElementPtr rsp = testCommand(cmd, CONTROL_RESULT_EMPTY, exp_rsp);
}

void Lease4CmdsTest::testLease4GetByClientId() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    string cmd =
        "{\n"
        "    \"command\": \"lease4-get\",\n"
        "    \"arguments\": {"
        "        \"identifier-type\": \"client-id\","
        "        \"identifier\": \"42:42:42:42:42:42:42:42\","
        "        \"subnet-id\": 44"
        "    }\n"
        "}";
    string exp_rsp = "IPv4 lease found.";
    ConstElementPtr rsp = testCommand(cmd, CONTROL_RESULT_SUCCESS, exp_rsp);

    // Now check that the lease parameters were indeed returned.
    ASSERT_TRUE(rsp);
    ConstElementPtr lease = rsp->get("arguments");
    ASSERT_TRUE(lease);

    // Let's check if the response makes any sense.
    checkLease4(lease, "192.0.2.1", 44, "08:08:08:08:08:08", false);
}

void Lease4CmdsTest::testLease4GetAll() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    // Query for all leases.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-get-all\"\n"
        "}";
    string exp_rsp = "4 IPv4 lease(s) found.";
    ConstElementPtr rsp = testCommand(cmd, CONTROL_RESULT_SUCCESS, exp_rsp);

    // Now check that the lease parameters were indeed returned.
    ASSERT_TRUE(rsp);

    ConstElementPtr args = rsp->get("arguments");
    ASSERT_TRUE(args);
    ASSERT_EQ(Element::map, args->getType());

    ConstElementPtr leases = args->get("leases");
    ASSERT_TRUE(leases);
    ASSERT_EQ(Element::list, leases->getType());

    // Let's check if the response contains desired leases.
    checkLease4(leases, "192.0.2.1", 44, "08:08:08:08:08:08", true);
    checkLease4(leases, "192.0.2.2", 44, "09:09:09:09:09:09", true);
    checkLease4(leases, "192.0.3.1", 88, "08:08:08:08:08:08", true);
    checkLease4(leases, "192.0.3.2", 88, "09:09:09:09:09:09", true);
}

void Lease4CmdsTest::testLease4GetAllNoLeases() {
    // Initialize lease manager (false = v4, false = don't add leases)
    initLeaseMgr(false, false);

    // Query for all leases.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-get-all\"\n"
        "}";
    string exp_rsp = "0 IPv4 lease(s) found.";
    ConstElementPtr rsp = testCommand(cmd, CONTROL_RESULT_EMPTY, exp_rsp);

    // Now check that the lease parameters were indeed returned.
    ASSERT_TRUE(rsp);

    ConstElementPtr args = rsp->get("arguments");
    ASSERT_TRUE(args);
    ASSERT_EQ(Element::map, args->getType());

    ConstElementPtr leases = args->get("leases");
    ASSERT_TRUE(leases);
    ASSERT_EQ(Element::list, leases->getType());

    EXPECT_EQ(0, leases->size());
}

void Lease4CmdsTest::testLease4GetAllBySubnetId() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    // Query for leases from subnet 44. Subnet 127 will be ignored because
    // it doesn't contain any leases.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-get-all\",\n"
        "    \"arguments\": {\n"
        "        \"subnets\": [ 44, 127 ]"
        "    }\n"
        "}";
    string exp_rsp = "2 IPv4 lease(s) found.";
    ConstElementPtr rsp = testCommand(cmd, CONTROL_RESULT_SUCCESS, exp_rsp);

    // Now check that the lease parameters were indeed returned.
    ASSERT_TRUE(rsp);

    ConstElementPtr args = rsp->get("arguments");
    ASSERT_TRUE(args);
    ASSERT_EQ(Element::map, args->getType());

    ConstElementPtr leases = args->get("leases");
    ASSERT_TRUE(leases);
    ASSERT_EQ(Element::list, leases->getType());

    // Let's check if the response contains desired leases.
    checkLease4(leases, "192.0.2.1", 44, "08:08:08:08:08:08", true);
    checkLease4(leases, "192.0.2.2", 44, "09:09:09:09:09:09", true);
}

void Lease4CmdsTest::testLease4GetAllBySubnetIdNoLeases() {
    // Initialize lease manager (false = v4, false = don't add leases)
    initLeaseMgr(false, false);

    // Query for leases from subnet 44. Subnet 127 will be ignored because
    // it doesn't contain any leases.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-get-all\",\n"
        "    \"arguments\": {\n"
        "        \"subnets\": [ 44, 127 ]"
        "    }\n"
        "}";
    string exp_rsp = "0 IPv4 lease(s) found.";
    ConstElementPtr rsp = testCommand(cmd, CONTROL_RESULT_EMPTY, exp_rsp);

    // Now check that the lease parameters were indeed returned.
    ASSERT_TRUE(rsp);

    ConstElementPtr args = rsp->get("arguments");
    ASSERT_TRUE(args);
    ASSERT_EQ(Element::map, args->getType());

    ConstElementPtr leases = args->get("leases");
    ASSERT_TRUE(leases);
    ASSERT_EQ(Element::list, leases->getType());

    EXPECT_EQ(0, leases->size());
}

void Lease4CmdsTest::testLease4GetAllByMultipleSubnetIds() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    // Query for leases from subnet 44 and 88.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-get-all\",\n"
        "    \"arguments\": {\n"
        "        \"subnets\": [ 44, 88 ]"
        "    }\n"
        "}";
    string exp_rsp = "4 IPv4 lease(s) found.";
    ConstElementPtr rsp = testCommand(cmd, CONTROL_RESULT_SUCCESS, exp_rsp);

    // Now check that the lease parameters were indeed returned.
    ASSERT_TRUE(rsp);

    ConstElementPtr args = rsp->get("arguments");
    ASSERT_TRUE(args);
    ASSERT_EQ(Element::map, args->getType());

    ConstElementPtr leases = args->get("leases");
    ASSERT_TRUE(leases);
    ASSERT_EQ(Element::list, leases->getType());

    // Let's check if the response contains desired leases.
    checkLease4(leases, "192.0.2.1", 44, "08:08:08:08:08:08", true);
    checkLease4(leases, "192.0.2.2", 44, "09:09:09:09:09:09", true);
    checkLease4(leases, "192.0.3.1", 88, "08:08:08:08:08:08", true);
    checkLease4(leases, "192.0.3.2", 88, "09:09:09:09:09:09", true);
}

void Lease4CmdsTest::testLease4GetBySubnetIdInvalidArguments() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    // Subnets not specified in arguments.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-get-all\",\n"
        "    \"arguments\": {"
        "        \"foo\": 1\n"
        "    }\n"
        "}";
    string exp_rsp = "'subnets' parameter not specified";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);

    // Subnets are not a list.
    cmd =
        "{\n"
        "    \"command\": \"lease4-get-all\",\n"
        "    \"arguments\": {"
        "        \"subnets\": 1\n"
        "    }\n"
        "}";
    exp_rsp = "'subnets' parameter must be a list";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);

    // Subnets list must contain numbers.
    cmd =
        "{\n"
        "    \"command\": \"lease4-get-all\",\n"
        "    \"arguments\": {"
        "        \"subnets\": [ \"x\", \"y\" ]\n"
        "    }\n"
        "}";
    exp_rsp = "listed subnet identifiers must be numbers";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
}

void Lease4CmdsTest::testLease4GetPaged() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    // Gather all returned addresses to verify that all were returned.
    std::set<std::string> lease_addresses;

    // Keyword start indicates that we want to retrieve the first page.
    std::string last_address = "start";

    // There are 4 leases in the database, so the first two pages should
    // include leases and the 3 page should be empty.
    for (auto i = 0; i < 3; ++i) {
        // Query for a page of leases.
        string cmd =
            "{\n"
            "    \"command\": \"lease4-get-page\",\n"
            "    \"arguments\": {"
            "        \"from\": \"" + last_address + "\","
            "        \"limit\": 2"
            "    }"
            "}";

        // For the first two pages we should get success. For the last
        // one an empty status code.
        ConstElementPtr rsp;
        if (i < 2) {
            string exp_rsp = "2 IPv4 lease(s) found.";
            rsp = testCommand(cmd, CONTROL_RESULT_SUCCESS, exp_rsp);

        } else {
            string exp_rsp = "0 IPv4 lease(s) found.";
            rsp = testCommand(cmd, CONTROL_RESULT_EMPTY, exp_rsp);

        }

        // Now check that the lease parameters were indeed returned.
        ASSERT_TRUE(rsp);

        // Arguments must exist.
        ConstElementPtr args = rsp->get("arguments");
        ASSERT_TRUE(args);
        ASSERT_EQ(Element::map, args->getType());

        // For convenience, we return the number of returned leases,
        // so as the client can check whether there was anything returned
        // before parsing the leases structure.
        ConstElementPtr page_count = args->get("count");
        ASSERT_TRUE(page_count);
        ASSERT_EQ(Element::integer, page_count->getType());

        // leases must exist, but may be empty.
        ConstElementPtr leases = args->get("leases");
        ASSERT_TRUE(leases);
        ASSERT_EQ(Element::list, leases->getType());

        if (!leases->empty()) {
            EXPECT_EQ(2, page_count->intValue());

            // Go over each lease and verify its correctness.
            for (ConstElementPtr lease : leases->listValue()) {
                ASSERT_EQ(Element::map, lease->getType());
                ASSERT_TRUE(lease->contains("ip-address"));
                ConstElementPtr ip_address = lease->get("ip-address");
                ASSERT_EQ(Element::string, ip_address->getType());
                last_address = ip_address->stringValue();

                lease_addresses.insert(last_address);

                // The easiest way to retrieve the subnet id and HW address is to
                // ask the Lease Manager.
                Lease4Ptr from_mgr = LeaseMgrFactory::instance().getLease4(IOAddress(last_address));
                ASSERT_TRUE(from_mgr);
                checkLease4(leases, last_address, from_mgr->subnet_id_,
                            from_mgr->hwaddr_->toText(false), true);
            }

        } else {
            // In the third iteration the page should be empty.
            EXPECT_EQ(0, page_count->intValue());
        }
    }

    // Check if all addresses were returned.
    EXPECT_EQ(1, lease_addresses.count("192.0.2.1"));
    EXPECT_EQ(1, lease_addresses.count("192.0.2.2"));
    EXPECT_EQ(1, lease_addresses.count("192.0.3.1"));
    EXPECT_EQ(1, lease_addresses.count("192.0.3.2"));
}

void Lease4CmdsTest::testLease4GetPagedZeroAddress() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    // Query for a page of leases.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-get-page\",\n"
        "    \"arguments\": {"
        "        \"from\": \"0.0.0.0\","
        "        \"limit\": 2"
        "    }"
        "}";

    string exp_rsp = "2 IPv4 lease(s) found.";
    testCommand(cmd, CONTROL_RESULT_SUCCESS, exp_rsp);
}

void Lease4CmdsTest::testLease4GetPagedIPv6Address() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    // Query for a page of leases.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-get-page\",\n"
        "    \"arguments\": {"
        "        \"from\": \"2001:db8::1\","
        "        \"limit\": 2"
        "    }"
        "}";

    string exp_rsp = "'from' parameter value 2001:db8::1 is not an IPv4 address";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
}

void Lease4CmdsTest::testLease4GetPagedInvalidFrom() {
    // Initialize lease manager (false = v6, true = add leases)
    initLeaseMgr(false, true);

    // Query for a page of leases.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-get-page\",\n"
        "    \"arguments\": {"
        "        \"from\": \"foo\","
        "        \"limit\": 2"
        "    }"
        "}";

    string exp_rsp = "'from' parameter value is neither 'start' keyword "
        "nor a valid IPv4 address";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
}

void Lease4CmdsTest::testLease4GetPagedNoLimit() {
    // Initialize lease manager (false = v6, true = add leases)
    initLeaseMgr(false, true);

    // Query for a page of leases.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-get-page\",\n"
        "    \"arguments\": {"
        "        \"from\": \"start\""
        "    }"
        "}";

    string exp_rsp = "'limit' parameter not specified";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
}

void Lease4CmdsTest::testLease4GetPagedLimitNotNumber() {
    // Initialize lease manager (false = v6, true = add leases)
    initLeaseMgr(false, true);

    // Query for a page of leases.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-get-page\",\n"
        "    \"arguments\": {"
        "        \"from\": \"start\","
        "        \"limit\": false"
        "    }"
        "}";

    string exp_rsp = "'limit' parameter must be a number";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
}

void Lease4CmdsTest::testLease4GetPagedLimitIsZero() {
    // Initialize lease manager (false = v6, true = add leases)
    initLeaseMgr(false, true);

    // Query for a page of leases.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-get-page\",\n"
        "    \"arguments\": {"
        "        \"from\": \"start\","
        "        \"limit\": 0"
        "    }"
        "}";

    string exp_rsp = "page size of retrieved leases must not be 0";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
}

void Lease4CmdsTest::testLease4GetByHwAddressParams() {
    // No parameters whatsoever.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-get-by-hw-address\",\n"
        "    \"arguments\": {"
        "    }\n"
        "}";
    string exp_rsp = "'hw-address' parameter not specified";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);

    // hw-address must be a string.
    cmd =
        "{\n"
        "    \"command\": \"lease4-get-by-hw-address\",\n"
        "    \"arguments\": {"
        "        \"hw-address\": 1234\n"
        "    }\n"
        "}";
    exp_rsp = "'hw-address' parameter must be a string";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);

    // Simply bad value.
    cmd =
        "{\n"
        "    \"command\": \"lease4-get-by-hw-address\",\n"
        "    \"arguments\": {"
        "        \"hw-address\": \"00::01:00:bc:0d:67\"\n"
        "    }\n"
        "}";
    exp_rsp = "two consecutive separators (':') specified in a decoded string";
    exp_rsp += " '00::01:00:bc:0d:67'";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
}

void Lease4CmdsTest::testLease4GetByHwAddressFind0() {
    // Initialize lease manager (false = v4, false = don't add leases)
    initLeaseMgr(false, false);

    // No such lease.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-get-by-hw-address\",\n"
        "    \"arguments\": {"
        "        \"hw-address\": \"01:02:03:04:05:06\"\n"
        "    }\n"
        "}";
    string exp_rsp = "0 IPv4 lease(s) found.";
    testCommand(cmd, CONTROL_RESULT_EMPTY, exp_rsp);
}

void Lease4CmdsTest::testLease4GetByHwAddressFind2() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    // Get the lease.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-get-by-hw-address\",\n"
        "    \"arguments\": {"
        "        \"hw-address\": \"08:08:08:08:08:08\"\n"
        "    }\n"
        "}";
    string exp_rsp = "2 IPv4 lease(s) found.";
    ConstElementPtr rsp = testCommand(cmd, CONTROL_RESULT_SUCCESS, exp_rsp);

    // Now check that the lease parameters were indeed returned.
    ASSERT_TRUE(rsp);
    ConstElementPtr map = rsp->get("arguments");
    ASSERT_TRUE(map);
    ASSERT_EQ(Element::map, map->getType());
    ConstElementPtr leases = map->get("leases");
    ASSERT_TRUE(leases);
    ASSERT_EQ(Element::list, leases->getType());
    ASSERT_EQ(2, leases->size());

    // Let's check if the response makes any sense.
    ConstElementPtr lease = leases->get(0);
    ASSERT_TRUE(lease);
    checkLease4(lease, "192.0.2.1", 44, "08:08:08:08:08:08", false);
    lease = leases->get(1);
    ASSERT_TRUE(lease);
    checkLease4(lease, "192.0.3.1", 88, "08:08:08:08:08:08", false);
}

void Lease4CmdsTest::testLease4GetByClientIdParams() {
    // No parameters whatsoever.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-get-by-client-id\",\n"
        "    \"arguments\": {"
        "    }\n"
        "}";
    string exp_rsp = "'client-id' parameter not specified";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);

    // client-id must be a string.
    cmd =
        "{\n"
        "    \"command\": \"lease4-get-by-client-id\",\n"
        "    \"arguments\": {"
        "        \"client-id\": 1234\n"
        "    }\n"
        "}";
    exp_rsp = "'client-id' parameter must be a string";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);

    // Simply bad value.
    cmd =
        "{\n"
        "    \"command\": \"lease4-get-by-client-id\",\n"
        "    \"arguments\": {"
        "        \"client-id\": \"00::01:00:bc:0d:67\"\n"
        "    }\n"
        "}";
    exp_rsp = "two consecutive separators (':') specified in a decoded string";
    exp_rsp += " '00::01:00:bc:0d:67'";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
}

void Lease4CmdsTest::testLease4GetByClientIdFind0() {
    // Initialize lease manager (false = v4, false = don't add leases)
    initLeaseMgr(false, false);

    // No such lease.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-get-by-client-id\",\n"
        "    \"arguments\": {"
        "        \"client-id\": \"01:02:03:04\"\n"
        "    }\n"
        "}";
    string exp_rsp = "0 IPv4 lease(s) found.";
    testCommand(cmd, CONTROL_RESULT_EMPTY, exp_rsp);
}

void Lease4CmdsTest::testLease4GetByClientIdFind2() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    // Get the lease.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-get-by-client-id\",\n"
        "    \"arguments\": {"
        "        \"client-id\": \"42:42:42:42:42:42:42:42\"\n"
        "    }\n"
        "}";
    string exp_rsp = "2 IPv4 lease(s) found.";
    ConstElementPtr rsp = testCommand(cmd, CONTROL_RESULT_SUCCESS, exp_rsp);

    // Now check that the lease parameters were indeed returned.
    ASSERT_TRUE(rsp);
    ConstElementPtr map = rsp->get("arguments");
    ASSERT_TRUE(map);
    ASSERT_EQ(Element::map, map->getType());
    ConstElementPtr leases = map->get("leases");
    ASSERT_TRUE(leases);
    ASSERT_EQ(Element::list, leases->getType());
    ASSERT_EQ(2, leases->size());

    // Let's check if the response makes any sense.
    ConstElementPtr lease = leases->get(0);
    ASSERT_TRUE(lease);
    checkLease4(lease, "192.0.2.1", 44, "08:08:08:08:08:08", false);
    lease = leases->get(1);
    ASSERT_TRUE(lease);
    checkLease4(lease, "192.0.3.1", 88, "08:08:08:08:08:08", false);
}

void Lease4CmdsTest::testLease4GetByHostnameParams() {
    // No parameters whatsoever.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-get-by-hostname\",\n"
        "    \"arguments\": {"
        "    }\n"
        "}";
    string exp_rsp = "'hostname' parameter not specified";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);

    // hostname must be a string.
    cmd =
        "{\n"
        "    \"command\": \"lease4-get-by-hostname\",\n"
        "    \"arguments\": {"
        "        \"hostname\": 1234\n"
        "    }\n"
        "}";
    exp_rsp = "'hostname' parameter must be a string";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);

    // hostname must be not empty.
    cmd =
        "{\n"
        "    \"command\": \"lease4-get-by-hostname\",\n"
        "    \"arguments\": {"
        "        \"hostname\": \"\"\n"
        "    }\n"
        "}";
    exp_rsp = "'hostname' parameter is empty";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
}

void Lease4CmdsTest::testLease4GetByHostnameFind0() {
    // Initialize lease manager (false = v4, false = don't add leases)
    initLeaseMgr(false, false);

    // No such lease.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-get-by-hostname\",\n"
        "    \"arguments\": {"
        "        \"hostname\": \"foo.bar\"\n"
        "    }\n"
        "}";
    string exp_rsp = "0 IPv4 lease(s) found.";
    testCommand(cmd, CONTROL_RESULT_EMPTY, exp_rsp);
}

void Lease4CmdsTest::testLease4GetByHostnameFind2() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    // Get the lease.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-get-by-hostname\",\n"
        "    \"arguments\": {"
        "        \"hostname\": \"Myhost.Example.Com.\"\n"
        "    }\n"
        "}";
    string exp_rsp = "4 IPv4 lease(s) found.";
    ConstElementPtr rsp = testCommand(cmd, CONTROL_RESULT_SUCCESS, exp_rsp);

    // Now check that the lease parameters were indeed returned.
    ASSERT_TRUE(rsp);
    ConstElementPtr map = rsp->get("arguments");
    ASSERT_TRUE(map);
    ASSERT_EQ(Element::map, map->getType());
    ConstElementPtr leases = map->get("leases");
    ASSERT_TRUE(leases);
    ASSERT_EQ(Element::list, leases->getType());
    ASSERT_EQ(4, leases->size());

    // Let's check if the response makes any sense.
    ConstElementPtr lease = leases->get(0);
    ASSERT_TRUE(lease);
    checkLease4(lease, "192.0.2.1", 44, "08:08:08:08:08:08", false);
    lease = leases->get(2);
    ASSERT_TRUE(lease);
    checkLease4(lease, "192.0.3.1", 88, "08:08:08:08:08:08", false);
}

void Lease4CmdsTest::testLease4UpdateMissingParams() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    // Everything missing. What sort of crap is that?
    string txt =
        "{\n"
        "    \"command\": \"lease4-update\",\n"
        "    \"arguments\": {"
        "    }\n"
        "}";
    string exp_rsp = "missing parameter 'ip-address' (<string>:3:19)";
    testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp);

    // Just ip is not enough (subnet-id and hwaddr missing, although
    // Kea can now figure out subnet-id on its own).
    txt =
        "{\n"
        "    \"command\": \"lease4-update\",\n"
        "    \"arguments\": {"
        "            \"ip-address\": \"192.0.2.123\"\n"
        "    }\n"
        "}";
    exp_rsp = "missing parameter 'hw-address' (<string>:3:19)";
    testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp);

    // Better, but still no luck. (hwaddr missing).
    txt =
        "{\n"
        "    \"command\": \"lease4-update\",\n"
        "    \"arguments\": {"
        "            \"subnet-id\": 44,\n"
        "            \"ip-address\": \"192.0.2.202\"\n"
        "    }\n"
        "}";
    exp_rsp = "missing parameter 'hw-address' (<string>:3:19)";
    testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp);

    // Close, but no cigars. (ip-address missing).
    txt =
        "{\n"
        "    \"command\": \"lease4-update\",\n"
        "    \"arguments\": {"
        "            \"subnet-id\": 44,\n"
        "            \"hw-address\": \"1a:1b:1c:1d:1e:1f\"\n"
        "    }\n"
        "}";
    exp_rsp = "missing parameter 'ip-address' (<string>:3:19)";
    testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp);
}

void Lease4CmdsTest::testLease4UpdateBadParams() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    // All params are there, but there's no subnet-id 123 configured.
    // (initLeaseMgr initialized subnet-id 44 for v4 and subnet-id 66 for v6).
    string txt =
        "{\n"
        "    \"command\": \"lease4-update\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 123,\n"
        "        \"ip-address\": \"192.0.2.202\",\n"
        "        \"hw-address\": \"1a:1b:1c:1d:1e:1f\"\n"
        "    }\n"
        "}";
    string exp_rsp = "Invalid subnet-id: No IPv4 subnet with subnet-id=123 currently configured.";
    testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp);

    // This time the new IP address does not belong to the subnet.
    txt =
        "{\n"
        "    \"command\": \"lease4-update\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 44,\n"
        "        \"ip-address\": \"10.0.0.1\",\n"
        "        \"hw-address\": \"1a:1b:1c:1d:1e:1f\"\n"
        "    }\n"
        "}";
    exp_rsp = "The address 10.0.0.1 does not belong to subnet 192.0.2.0/24, subnet-id=44";
    testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp);

    // We don't use any of that bleeding edge nonsense in this museum. v4 only.
    txt =
        "{\n"
        "    \"command\": \"lease4-update\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 44,\n"
        "        \"ip-address\": \"2001:db8:1::1\",\n"
        "        \"hw-address\": \"1a:1b:1c:1d:1e:1f\"\n"
        "    }\n"
        "}";
    exp_rsp = "Non-IPv4 address specified: 2001:db8:1::1";
    testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp);

    // Bad user context: not a map.
    txt =
        "{\n"
        "    \"command\": \"lease4-update\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 44,\n"
        "        \"ip-address\": \"192.0.2.1\",\n"
        "        \"hw-address\": \"1a:1b:1c:1d:1e:1f\",\n"
        "        \"user-context\": \"bad value\"\n"
        "    }\n"
        "}";
    exp_rsp = "Invalid user context '\"bad value\"' is not a JSON map.";
    testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp);

    // Duplicated comment.
    txt =
        "{\n"
        "    \"command\": \"lease4-update\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 44,\n"
        "        \"ip-address\": \"192.0.2.1\",\n"
        "        \"hw-address\": \"1a:1b:1c:1d:1e:1f\",\n"
        "        \"user-context\": { \"comment\": \"in user context\" },\n"
        "        \"comment\": \"direct\"\n"
        "    }\n"
        "}";
    exp_rsp = "Duplicated comment entry '\"direct\"' in user context "
        "'{ \"comment\": \"in user context\" }'";
    testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp);
}

void Lease4CmdsTest::testLease4UpdateNoLease() {
    // Initialize lease manager (false = v4, false = don't add leases)
    initLeaseMgr(false, false);

    // Now send the command.
    string txt =
        "{\n"
        "    \"command\": \"lease4-update\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 44,\n"
        "        \"ip-address\": \"192.0.2.1\",\n"
        "        \"hw-address\": \"1a:1b:1c:1d:1e:1f\",\n"
        "        \"hostname\": \"newhostname.example.org\""
        "    }\n"
        "}";
    string exp_rsp = "failed to update the lease with address 192.0.2.1 "
        "either because the lease has been deleted or it has changed in the "
        "database, in both cases a retry might succeed";
    testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp);
}

void Lease4CmdsTest::testLease4Update() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    checkLease4Stats(44, 2, 0);

    checkLease4Stats(88, 2, 0);

    // Now send the command.
    string txt =
        "{\n"
        "    \"command\": \"lease4-update\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 44,\n"
        "        \"ip-address\": \"192.0.2.1\",\n"
        "        \"hw-address\": \"1a:1b:1c:1d:1e:1f\",\n"
        "        \"hostname\": \"newhostname.example.org\""
        "    }\n"
        "}";
    string exp_rsp = "IPv4 lease updated.";
    testCommand(txt, CONTROL_RESULT_SUCCESS, exp_rsp);

    checkLease4Stats(44, 2, 0);

    checkLease4Stats(88, 2, 0);

    // Now check that the lease is still there.
    Lease4Ptr l = lmptr_->getLease4(IOAddress("192.0.2.1"));
    ASSERT_TRUE(l);

    // Make sure it's been updated.
    ASSERT_TRUE(l->hwaddr_);
    EXPECT_EQ("1a:1b:1c:1d:1e:1f", l->hwaddr_->toText(false));
    EXPECT_EQ("newhostname.example.org", l->hostname_);
    EXPECT_FALSE(l->getContext());
}

void Lease4CmdsTest::testLease4UpdateDeclinedLeases() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true, true);

    checkLease4Stats(44, 2, 2);

    checkLease4Stats(88, 2, 2);

    // Now send the command.
    string txt =
        "{\n"
        "    \"command\": \"lease4-update\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 44,\n"
        "        \"ip-address\": \"192.0.2.1\",\n"
        "        \"hw-address\": \"1a:1b:1c:1d:1e:1f\",\n"
        "        \"hostname\": \"newhostname.example.org\""
        "    }\n"
        "}";
    string exp_rsp = "IPv4 lease updated.";
    testCommand(txt, CONTROL_RESULT_SUCCESS, exp_rsp);

    checkLease4Stats(44, 2, 1);

    checkLease4Stats(88, 2, 2);

    // Now check that the lease is still there.
    Lease4Ptr l = lmptr_->getLease4(IOAddress("192.0.2.1"));
    ASSERT_TRUE(l);

    // Make sure it's been updated.
    ASSERT_TRUE(l->hwaddr_);
    EXPECT_EQ("1a:1b:1c:1d:1e:1f", l->hwaddr_->toText(false));
    EXPECT_EQ("newhostname.example.org", l->hostname_);
    EXPECT_FALSE(l->getContext());
}

void Lease4CmdsTest::testLease4UpdateNoSubnetId() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    checkLease4Stats(44, 2, 0);

    checkLease4Stats(88, 2, 0);

    // Now send the command.
    string txt =
        "{\n"
        "    \"command\": \"lease4-update\",\n"
        "    \"arguments\": {"
        "        \"ip-address\": \"192.0.2.1\",\n"
        "        \"hw-address\": \"1a:1b:1c:1d:1e:1f\",\n"
        "        \"hostname\": \"newhostname.example.org\""
        "    }\n"
        "}";
    string exp_rsp = "IPv4 lease updated.";
    testCommand(txt, CONTROL_RESULT_SUCCESS, exp_rsp);

    checkLease4Stats(44, 2, 0);

    checkLease4Stats(88, 2, 0);

    // Now check that the lease is still there.
    Lease4Ptr l = lmptr_->getLease4(IOAddress("192.0.2.1"));
    ASSERT_TRUE(l);

    // Make sure it's been updated.
    ASSERT_TRUE(l->hwaddr_);
    EXPECT_EQ("1a:1b:1c:1d:1e:1f", l->hwaddr_->toText(false));
    EXPECT_EQ("newhostname.example.org", l->hostname_);
    EXPECT_FALSE(l->getContext());
}

void Lease4CmdsTest::testLease4UpdateNoSubnetIdDeclinedLeases() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true, true);

    checkLease4Stats(44, 2, 2);

    checkLease4Stats(88, 2, 2);

    // Now send the command.
    string txt =
        "{\n"
        "    \"command\": \"lease4-update\",\n"
        "    \"arguments\": {"
        "        \"ip-address\": \"192.0.2.1\",\n"
        "        \"hw-address\": \"1a:1b:1c:1d:1e:1f\",\n"
        "        \"hostname\": \"newhostname.example.org\""
        "    }\n"
        "}";
    string exp_rsp = "IPv4 lease updated.";
    testCommand(txt, CONTROL_RESULT_SUCCESS, exp_rsp);

    checkLease4Stats(44, 2, 1);

    checkLease4Stats(88, 2, 2);

    // Now check that the lease is still there.
    Lease4Ptr l = lmptr_->getLease4(IOAddress("192.0.2.1"));
    ASSERT_TRUE(l);

    // Make sure it's been updated.
    ASSERT_TRUE(l->hwaddr_);
    EXPECT_EQ("1a:1b:1c:1d:1e:1f", l->hwaddr_->toText(false));
    EXPECT_EQ("newhostname.example.org", l->hostname_);
    EXPECT_FALSE(l->getContext());
}

void Lease4CmdsTest::testLease4UpdateForceCreate() {
    // Initialize lease manager (false = v4, false = don't add leases)
    initLeaseMgr(false, false);

    checkLease4Stats(44, 0, 0);

    checkLease4Stats(88, 0, 0);

    // Now send the command.
    string txt =
        "{\n"
        "    \"command\": \"lease4-update\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 44,\n"
        "        \"ip-address\": \"192.0.2.1\",\n"
        "        \"hw-address\": \"1a:1b:1c:1d:1e:1f\",\n"
        "        \"hostname\": \"newhostname.example.org\","
        "        \"force-create\": true"
        "    }\n"
        "}";
    string exp_rsp = "IPv4 lease added.";
    testCommand(txt, CONTROL_RESULT_SUCCESS, exp_rsp);

    checkLease4Stats(44, 1, 0);

    checkLease4Stats(88, 0, 0);

    // Now check that the lease is still there.
    Lease4Ptr l = lmptr_->getLease4(IOAddress("192.0.2.1"));
    ASSERT_TRUE(l);

    // Make sure it contains expected values..
    ASSERT_TRUE(l->hwaddr_);
    EXPECT_EQ("1a:1b:1c:1d:1e:1f", l->hwaddr_->toText(false));
    EXPECT_EQ("newhostname.example.org", l->hostname_);
    EXPECT_FALSE(l->getContext());
}

void Lease4CmdsTest::testLease4UpdateForceCreateNoSubnetId() {
    // Initialize lease manager (false = v4, false = don't add leases)
    initLeaseMgr(false, false);

    checkLease4Stats(44, 0, 0);

    checkLease4Stats(88, 0, 0);

    // Now send the command.
    string txt =
        "{\n"
        "    \"command\": \"lease4-update\",\n"
        "    \"arguments\": {"
        "        \"ip-address\": \"192.0.2.1\",\n"
        "        \"hw-address\": \"1a:1b:1c:1d:1e:1f\",\n"
        "        \"hostname\": \"newhostname.example.org\","
        "        \"force-create\": true"
        "    }\n"
        "}";
    string exp_rsp = "IPv4 lease added.";
    testCommand(txt, CONTROL_RESULT_SUCCESS, exp_rsp);

    checkLease4Stats(44, 1, 0);

    checkLease4Stats(88, 0, 0);

    // Now check that the lease is still there.
    Lease4Ptr l = lmptr_->getLease4(IOAddress("192.0.2.1"));
    ASSERT_TRUE(l);

    // Make sure the subnet-id is figured out correctly.
    EXPECT_EQ(44, l->subnet_id_);

    // Make sure it contains expected values..
    ASSERT_TRUE(l->hwaddr_);
    EXPECT_EQ("1a:1b:1c:1d:1e:1f", l->hwaddr_->toText(false));
    EXPECT_EQ("newhostname.example.org", l->hostname_);
    EXPECT_FALSE(l->getContext());
}

void Lease4CmdsTest::testLease4UpdateDoNotForceCreate() {
    // Initialize lease manager (false = v4, false = don't add leases)
    initLeaseMgr(false, false);

    checkLease4Stats(44, 0, 0);

    checkLease4Stats(88, 0, 0);

    // Now send the command.
    string txt =
        "{\n"
        "    \"command\": \"lease4-update\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 44,\n"
        "        \"ip-address\": \"192.0.2.1\",\n"
        "        \"hw-address\": \"1a:1b:1c:1d:1e:1f\",\n"
        "        \"hostname\": \"newhostname.example.org\","
        "        \"force-create\": false"
        "    }\n"
        "}";
    string exp_rsp = "failed to update the lease with address 192.0.2.1 "
        "either because the lease has been deleted or it has changed in the "
        "database, in both cases a retry might succeed";
    testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp);

    checkLease4Stats(44, 0, 0);

    checkLease4Stats(88, 0, 0);
}

void Lease4CmdsTest::testLease4UpdateComment() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    checkLease4Stats(44, 2, 0);

    checkLease4Stats(88, 2, 0);

    // Now send the command.
    string txt =
        "{\n"
        "    \"command\": \"lease4-update\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 44,\n"
        "        \"ip-address\": \"192.0.2.1\",\n"
        "        \"hw-address\": \"42:42:42:42:42:42:42:42\",\n"
        "        \"comment\": \"a comment\",\n"
        "        \"user-context\": { \"foobar\": true }\n"
        "    }\n"
        "}";
    string exp_rsp = "IPv4 lease updated.";
    testCommand(txt, CONTROL_RESULT_SUCCESS, exp_rsp);

    checkLease4Stats(44, 2, 0);

    checkLease4Stats(88, 2, 0);

    // Now check that the lease is still there.
    Lease4Ptr l = lmptr_->getLease4(IOAddress("192.0.2.1"));
    ASSERT_TRUE(l);

    // Make sure it's been updated.
    ASSERT_TRUE(l->hwaddr_);
    EXPECT_EQ("42:42:42:42:42:42:42:42", l->hwaddr_->toText(false));

    // Check user context.
    ConstElementPtr ctx = l->getContext();
    ASSERT_TRUE(ctx);
    EXPECT_EQ(2, ctx->size());
    ASSERT_TRUE(ctx->contains("comment"));
    EXPECT_EQ("\"a comment\"", ctx->get("comment")->str());
    ASSERT_TRUE(ctx->contains("foobar"));
    EXPECT_EQ("true", ctx->get("foobar")->str());
}

void Lease4CmdsTest::testLease4DelMissingParams() {
    // No parameters whatsoever. You want just a lease, any lease?
    string cmd =
        "{\n"
        "    \"command\": \"lease4-del\",\n"
        "    \"arguments\": {"
        "    }\n"
        "}";
    string exp_rsp = "Mandatory 'subnet-id' parameter missing.";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);

    // Just the subnet-id won't cut it, either.
    cmd =
        "{\n"
        "    \"command\": \"lease4-del\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 123"
        "    }\n"
        "}";
    exp_rsp = "No 'ip-address' provided and 'identifier-type' is either missing or not a string.";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);

    // We can't identify your laptop by color. Sorry, buddy.
    cmd =
        "{\n"
        "    \"command\": \"lease4-del\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 123,\n"
        "        \"identifier-type\": \"color\",\n"
        "        \"identifier\": \"blue\"\n"
        "    }\n"
        "}";
    exp_rsp = "Incorrect identifier type: color, the only supported values are: "
        "address, hw-address, duid";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);

    // Query by DUID is not supported in v4. Sorry.
    cmd =
        "{\n"
        "    \"command\": \"lease4-del\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 123,\n"
        "        \"identifier-type\": \"duid\",\n"
        "        \"identifier\": \"01:01:01:01:01:01\"\n"
        "    }\n"
        "}";
    exp_rsp = "Delete by duid is not allowed in v4.";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);

    // Identifier value is missing.
    cmd =
        "{\n"
        "    \"command\": \"lease4-del\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 123,\n"
        "        \"identifier-type\": \"hw-address\"\n"
        "    }\n"
        "}";
    exp_rsp = "No 'ip-address' provided and 'identifier' is either missing or not a string.";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);

    // Identifier-type is missing.
    cmd =
        "{\n"
        "    \"command\": \"lease4-del\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 123,\n"
        "        \"identifier\": \"01:02:03:04:05\"\n"
        "    }\n"
        "}";
    exp_rsp = "No 'ip-address' provided and 'identifier-type' is either missing or not a string.";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
}

void Lease4CmdsTest::testLease4DelByAddrNotFound() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    checkLease4Stats(44, 2, 0);

    checkLease4Stats(88, 2, 0);

    // Invalid
    string cmd =
        "{\n"
        "    \"command\": \"lease4-del\",\n"
        "    \"arguments\": {"
        "        \"ip-address\": \"192.0.2.5\","
        "        \"subnet-id\": 44"
        "    }\n"
        "}";
    string exp_rsp = "IPv4 lease not found.";
    ConstElementPtr rsp = testCommand(cmd, CONTROL_RESULT_EMPTY, exp_rsp);

    checkLease4Stats(44, 2, 0);

    checkLease4Stats(88, 2, 0);
}

void Lease4CmdsTest::testLease4DelByAddr() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    checkLease4Stats(44, 2, 0);

    checkLease4Stats(88, 2, 0);

    // Query for valid, existing lease.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-del\",\n"
        "    \"arguments\": {"
        "        \"ip-address\": \"192.0.2.1\""
        "    }\n"
        "}";
    string exp_rsp = "IPv4 lease deleted.";
    testCommand(cmd, CONTROL_RESULT_SUCCESS, exp_rsp);

    checkLease4Stats(44, 1, 0);

    checkLease4Stats(88, 2, 0);

    // Make sure the lease is really gone.
    EXPECT_FALSE(lmptr_->getLease4(IOAddress("192.0.2.1")));
}

void Lease4CmdsTest::testLease4DelByAddrDeclinedLeases() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true, true);

    checkLease4Stats(44, 2, 2);

    checkLease4Stats(88, 2, 2);

    // Query for valid, existing lease.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-del\",\n"
        "    \"arguments\": {"
        "        \"ip-address\": \"192.0.2.1\""
        "    }\n"
        "}";
    string exp_rsp = "IPv4 lease deleted.";
    testCommand(cmd, CONTROL_RESULT_SUCCESS, exp_rsp);

    checkLease4Stats(44, 1, 1);

    checkLease4Stats(88, 2, 2);

    // Make sure the lease is really gone.
    EXPECT_FALSE(lmptr_->getLease4(IOAddress("192.0.2.1")));
}

void Lease4CmdsTest::testLease4DelByAddrBadParam() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    checkLease4Stats(44, 2, 0);

    checkLease4Stats(88, 2, 0);

    // Invalid family
    string cmd =
        "{\n"
        "    \"command\": \"lease4-del\",\n"
        "    \"arguments\": {"
        "        \"ip-address\": \"2001:db8:1::1\""
        "    }\n"
        "}";
    string exp_rsp = "Invalid IPv4 address specified: 2001:db8:1::1";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);

    checkLease4Stats(44, 2, 0);

    checkLease4Stats(88, 2, 0);

    // This is way off
    cmd =
        "{\n"
        "    \"command\": \"lease6-del\",\n"
        "    \"arguments\": {"
        "        \"ip-address\": \"221B Baker St.\""
        "    }\n"
        "}";
    exp_rsp = "Failed to convert string to address '221B Baker St.': Invalid argument";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
}

void Lease4CmdsTest::testLease4DelByHWAddrNotFound() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    checkLease4Stats(44, 2, 0);

    checkLease4Stats(88, 2, 0);

    // No such lease.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-del\",\n"
        "    \"arguments\": {"
        "        \"identifier-type\": \"hw-address\","
        "        \"identifier\": \"01:02:03:04:05:06\","
        "        \"subnet-id\": 44"
        "    }\n"
        "}";
    string exp_rsp = "IPv4 lease not found.";
    ConstElementPtr rsp = testCommand(cmd, CONTROL_RESULT_EMPTY, exp_rsp);

    checkLease4Stats(44, 2, 0);

    checkLease4Stats(88, 2, 0);

    // Make sure the lease is still there.
    EXPECT_TRUE(lmptr_->getLease4(IOAddress("192.0.2.1")));
}

void Lease4CmdsTest::testLease4DelByHWAddr() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    checkLease4Stats(44, 2, 0);

    checkLease4Stats(88, 2, 0);

    // Invalid
    string cmd =
        "{\n"
        "    \"command\": \"lease4-del\",\n"
        "    \"arguments\": {"
        "        \"identifier-type\": \"hw-address\","
        "        \"identifier\": \"08:08:08:08:08:08\","
        "        \"subnet-id\": 44"
        "    }\n"
        "}";
    string exp_rsp = "IPv4 lease deleted.";
    ConstElementPtr rsp = testCommand(cmd, CONTROL_RESULT_SUCCESS, exp_rsp);

    checkLease4Stats(44, 1, 0);

    checkLease4Stats(88, 2, 0);

    // Make sure the lease is really gone.
    EXPECT_FALSE(lmptr_->getLease4(IOAddress("192.0.2.1")));
}

void Lease4CmdsTest::testLease4DelByClientIdNotFound() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    checkLease4Stats(44, 2, 0);

    checkLease4Stats(88, 2, 0);

    // No such lease.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-del\",\n"
        "    \"arguments\": {"
        "        \"identifier-type\": \"client-id\","
        "        \"identifier\": \"01:02:03:04\","
        "        \"subnet-id\": 44"
        "    }\n"
        "}";
    string exp_rsp = "IPv4 lease not found.";
    ConstElementPtr rsp = testCommand(cmd, CONTROL_RESULT_EMPTY, exp_rsp);

    checkLease4Stats(44, 2, 0);

    checkLease4Stats(88, 2, 0);

    // Make sure the lease is still there.
    EXPECT_TRUE(lmptr_->getLease4(IOAddress("192.0.2.1")));
}

void Lease4CmdsTest::testLease4DelByClientId() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    checkLease4Stats(44, 2, 0);

    checkLease4Stats(88, 2, 0);

    // Invalid
    string cmd =
        "{\n"
        "    \"command\": \"lease4-del\",\n"
        "    \"arguments\": {"
        "        \"identifier-type\": \"client-id\","
        "        \"identifier\": \"42:42:42:42:42:42:42:42\","
        "        \"subnet-id\": 44"
        "    }\n"
        "}";
    string exp_rsp = "IPv4 lease deleted.";
    ConstElementPtr rsp = testCommand(cmd, CONTROL_RESULT_SUCCESS, exp_rsp);

    checkLease4Stats(44, 1, 0);

    checkLease4Stats(88, 2, 0);

    // Make sure the lease is really gone.
    EXPECT_FALSE(lmptr_->getLease4(IOAddress("192.0.2.1")));
}

void Lease4CmdsTest::testLease4Wipe() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    checkLease4Stats(44, 2, 0);

    checkLease4Stats(88, 2, 0);

    // Query for valid, existing lease.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-wipe\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 44"
        "    }\n"
        "}";
    string exp_rsp = "Deleted 2 IPv4 lease(s) from subnet(s) 44";
    testCommand(cmd, CONTROL_RESULT_SUCCESS, exp_rsp);

    checkLease4Stats(44, 0, 0);

    checkLease4Stats(88, 2, 0);

    // Make sure the leases in subnet 44 are really gone.
    EXPECT_FALSE(lmptr_->getLease4(IOAddress("192.0.2.1")));
    EXPECT_FALSE(lmptr_->getLease4(IOAddress("192.0.2.2")));

    // Make sure the leases from subnet 88 are still there.
    EXPECT_TRUE(lmptr_->getLease4(IOAddress("192.0.3.1")));
    EXPECT_TRUE(lmptr_->getLease4(IOAddress("192.0.3.2")));
}

void Lease4CmdsTest::testLease4WipeAll() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    checkLease4Stats(44, 2, 0);

    checkLease4Stats(88, 2, 0);

    // Query for valid, existing lease.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-wipe\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 0"
        "    }\n"
        "}";
    string exp_rsp = "Deleted 4 IPv4 lease(s) from subnet(s) 44 88";
    testCommand(cmd, CONTROL_RESULT_SUCCESS, exp_rsp);

    checkLease4Stats(44, 0, 0);

    checkLease4Stats(88, 0, 0);

    // Make sure the leases in subnet 44 are really gone.
    EXPECT_FALSE(lmptr_->getLease4(IOAddress("192.0.2.1")));
    EXPECT_FALSE(lmptr_->getLease4(IOAddress("192.0.2.2")));

    // Make sure the leases from subnet 88 are gone, too.
    EXPECT_FALSE(lmptr_->getLease4(IOAddress("192.0.3.1")));
    EXPECT_FALSE(lmptr_->getLease4(IOAddress("192.0.3.2")));
}

void Lease4CmdsTest::testLease4WipeAllNoArgs() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    checkLease4Stats(44, 2, 0);

    checkLease4Stats(88, 2, 0);

    // Query for valid, existing lease.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-wipe\"\n"
        "}";
    string exp_rsp = "Deleted 4 IPv4 lease(s) from subnet(s) 44 88";
    testCommand(cmd, CONTROL_RESULT_SUCCESS, exp_rsp);

    checkLease4Stats(44, 0, 0);

    checkLease4Stats(88, 0, 0);

    // Make sure the leases in subnet 44 are really gone.
    EXPECT_FALSE(lmptr_->getLease4(IOAddress("192.0.2.1")));
    EXPECT_FALSE(lmptr_->getLease4(IOAddress("192.0.2.2")));

    // Make sure the leases from subnet 88 are gone, too.
    EXPECT_FALSE(lmptr_->getLease4(IOAddress("192.0.3.1")));
    EXPECT_FALSE(lmptr_->getLease4(IOAddress("192.0.3.2")));
}

void Lease4CmdsTest::testLease4WipeNoLeases() {
    // Initialize lease manager (false = v4, false = don't add leases)
    initLeaseMgr(false, false);

    checkLease4Stats(44, 0, 0);

    checkLease4Stats(88, 0, 0);

    // Query for valid, existing lease.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-wipe\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 44"
        "    }\n"
        "}";
    string exp_rsp = "Deleted 0 IPv4 lease(s) from subnet(s) 44";
    testCommand(cmd, CONTROL_RESULT_EMPTY, exp_rsp);

    checkLease4Stats(44, 0, 0);

    checkLease4Stats(88, 0, 0);
}

void Lease4CmdsTest::testLease4WipeNoLeasesAll() {
    // Initialize lease manager (false = v4, false = don't add leases)
    initLeaseMgr(false, false);

    checkLease4Stats(44, 0, 0);

    checkLease4Stats(88, 0, 0);

    // Query for valid, existing lease.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-wipe\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 0"
        "    }\n"
        "}";
    string exp_rsp = "Deleted 0 IPv4 lease(s) from subnet(s) 44 88";
    testCommand(cmd, CONTROL_RESULT_EMPTY, exp_rsp);

    checkLease4Stats(44, 0, 0);

    checkLease4Stats(88, 0, 0);
}

void Lease4CmdsTest::testLease4BrokenUpdate() {
    // Initialize lease manager (false = v4, false = don't add leases)
    initLeaseMgr(false, false);

    // Set the sanity checks level.
    CfgMgr::instance().getCurrentCfg()->getConsistency()
        ->setLeaseSanityCheck(CfgConsistency::LEASE_CHECK_FIX);

    // Now send the command.
    string txt =
        "{\n"
        "    \"command\": \"lease4-update\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 444,\n"
        "        \"ip-address\": \"192.0.2.202\",\n"
        "        \"hw-address\": \"1a:1b:1c:1d:1e:1f\",\n"
        "        \"force-create\": true\n"
        "    }\n"
        "}";
    string exp_rsp = "Invalid subnet-id: No IPv4 subnet with "
                     "subnet-id=444 currently configured.";
    testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp);
}

void Lease4CmdsTest::testLease4ResendDdnsBadParam() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    // Missing address parameter.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-resend-ddns\",\n"
        "    \"arguments\": {\n"
        "    }\n"
        "}\n";

    string exp_rsp = "'ip-address' parameter is missing.";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);

    // Invalid address family.
    cmd =
        "{\n"
        "    \"command\": \"lease4-resend-ddns\",\n"
        "    \"arguments\": {\n"
        "        \"ip-address\": \"2001:db8:1::1\"\n"
        "    }\n"
        "}\n";

    exp_rsp = "Invalid IPv4 address specified: 2001:db8:1::1";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);

    // ip-address is not an address at all.
    cmd =
        "{\n"
        "    \"command\": \"lease4-resend-ddns\",\n"
        "    \"arguments\": {\n"
        "        \"ip-address\": \"221B Baker St.\"\n"
        "    }\n"
        "}\n";

    exp_rsp = "'221B Baker St.' is not a valid IP address.";
    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
}

void Lease4CmdsTest::testLease4ResendDdnsDisabled() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    disableD2();

    // Query for valid, existing lease.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-resend-ddns\",\n"
        "    \"arguments\": {"
        "        \"ip-address\": \"192.0.2.5\""
        "    }\n"
        "}";

    string exp_rsp = "DDNS updating is not enabled";
    ConstElementPtr rsp = testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
    // With D2 disabled there is no queue, size should come back as -1.
    EXPECT_EQ(ncrQueueSize(), -1);
}

void Lease4CmdsTest::testLease4ResendDdnsNoLease() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    // Invalid
    string cmd =
        "{\n"
        "    \"command\": \"lease4-resend-ddns\",\n"
        "    \"arguments\": {\n"
        "        \"ip-address\": \"192.0.2.5\"\n"
        "    }\n"
        "}\n";
    string exp_rsp = "No lease found for: 192.0.2.5";
    ConstElementPtr rsp = testCommand(cmd, CONTROL_RESULT_EMPTY, exp_rsp);
}

void Lease4CmdsTest::testLease4ResendNoHostname() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    // NCR sender queue should be empty.
    ASSERT_EQ(ncrQueueSize(), 0);

    // Fetch the lease so we can replace the hostname with "".
    Lease4Ptr lease = lmptr_->getLease4(IOAddress("192.0.2.1"));
    ASSERT_TRUE(lease);
    lease->hostname_ = "";
    lmptr_->updateLease4(lease);

    // Query for valid, existing lease.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-resend-ddns\",\n"
        "    \"arguments\": {"
        "        \"ip-address\": \"192.0.2.1\""
        "    }\n"
        "}";

    string exp_rsp = "Lease for: 192.0.2.1, has no hostname, nothing to update";
    ConstElementPtr rsp = testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);

    // There should not any NCRs queued.
    EXPECT_EQ(ncrQueueSize(), 0);
}

void Lease4CmdsTest::testLease4ResendNoDirectionsEnabled() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    // NCR sender queue should be empty.
    ASSERT_EQ(ncrQueueSize(), 0);

    // Fetch the lease so we can replace the hostname with "".
    Lease4Ptr lease = lmptr_->getLease4(IOAddress("192.0.2.1"));
    ASSERT_TRUE(lease);
    lease->fqdn_fwd_ = false;
    lease->fqdn_rev_ = false;
    lmptr_->updateLease4(lease);

    // Query for valid, existing lease.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-resend-ddns\",\n"
        "    \"arguments\": {"
        "        \"ip-address\": \"192.0.2.1\""
        "    }\n"
        "}";

    string exp_rsp = "Neither forward nor reverse updates enabled for lease for: 192.0.2.1";
    ConstElementPtr rsp = testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);

    // There should not any NCRs queued.
    EXPECT_EQ(ncrQueueSize(), 0);
}

void Lease4CmdsTest::testLease4ResendDdnsEnabled() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    // Structure detailing a test scenario.
    struct Scenario {
        std::string description_;
        bool fqdn_fwd_;
        bool fqdn_rev_;
    };

    // Three test scenarios to verify each combination of true flags.
    std::vector<Scenario> scenarios = {
       { "fwd_only", true,  false },
       { "rev_only", false, true},
       { "both",     true,  true},
    };

    // Query for valid, existing lease.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-resend-ddns\",\n"
        "    \"arguments\": {"
        "        \"ip-address\": \"192.0.2.1\""
        "    }\n"
        "}";

    // Expected response string.
    string exp_rsp = "NCR generated for: 192.0.2.1, hostname: myhost.example.com.";

    for (auto scenario : scenarios) {
        SCOPED_TRACE(scenario.description_);

        // Fetch the lease so we can update the DDNS direction flags.
        Lease4Ptr lease = lmptr_->getLease4(IOAddress("192.0.2.1"));
        ASSERT_TRUE(lease);
        lease->fqdn_rev_ = scenario.fqdn_rev_;
        lease->fqdn_fwd_ = scenario.fqdn_fwd_;
        lmptr_->updateLease4(lease);

        // Queue should be empty.
        ASSERT_EQ(ncrQueueSize(), 0);
        ConstElementPtr rsp = testCommand(cmd, CONTROL_RESULT_SUCCESS, exp_rsp);

        // We should have one entry in the queue.
        ASSERT_EQ(ncrQueueSize(), 1);
        verifyNameChangeRequest(CHG_ADD, scenario.fqdn_rev_, scenario.fqdn_fwd_,
                                "192.0.2.1", "myhost.example.com.");
    }
}

void Lease4CmdsTest::testLease4DnsRemoveD2Enabled() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    // Structure detailing a test scenario.
    struct Scenario {
        std::string description_;
        std::string hostname_;
        bool fqdn_fwd_;
        bool fqdn_rev_;
        std::string update_ddns_;
        bool exp_ncr_;
    };

    bool fwd = true;
    bool rev = true;
    bool ncr = true;

    // Three test scenarios to verify each combination of true flags.
    std::vector<Scenario> scenarios = {
        {
            "no_host",
            "",
            fwd, rev,
            "\"update-ddns\": true",
            !ncr
        },
        {
            "no directions",
            "myhost.example.com.",
            !fwd, !rev,
            "\"update-ddns\": true",
            !ncr
        },
        {
            "fwd_only",
            "myhost.example.com.",
            fwd, !rev,
            "\"update-ddns\": true",
            ncr
        },
        {
            "rev_only",
            "myhost.example.com.",
            !fwd, rev,
            "\"update-ddns\": true",
            ncr
        },
        {
            "both directions",
            "myhost.example.com.",
            fwd, rev,
            "\"update-ddns\": true",
            ncr
        },
        {
            "default update-ddns",
            "myhost.example.com.",
            fwd, rev,
            "",
            !ncr
        },
        {
            "update-ddns = false",
            "myhost.example.com.",
            fwd, rev,
            "\"update-ddns\": false",
            !ncr
        },
    };

    for (auto scenario : scenarios) {
        SCOPED_TRACE(scenario.description_);

        // Let's create a lease with scenario attributes.
        Lease4Ptr lease = createLease4("192.0.2.8", 44, 0x08, 0x42);
        lease->hostname_ = scenario.hostname_;
        lease->fqdn_rev_ = scenario.fqdn_rev_;
        lease->fqdn_fwd_ = scenario.fqdn_fwd_;
        ASSERT_TRUE(lmptr_->addLease(lease));

        // NCR Queue should be empty.
        ASSERT_EQ(ncrQueueSize(), 0);

        // Build the command
        std::stringstream cmd;
        cmd <<
            "{"
            "    \"command\": \"lease4-del\","
            "    \"arguments\": {"
            "        \"ip-address\": \"192.0.2.8\"";

        if (!scenario.update_ddns_.empty()) {
            cmd << "," << scenario.update_ddns_;
        }

        cmd << "}}";

        // Execute the delete command.
        static_cast<void>(testCommand(cmd.str(), CONTROL_RESULT_SUCCESS, "IPv4 lease deleted."));

        if (!scenario.exp_ncr_) {
            // Should not have an ncr.
            ASSERT_EQ(ncrQueueSize(), 0);
        } else {
            // We should have an ncr, verify it.
            ASSERT_EQ(ncrQueueSize(), 1);
            verifyNameChangeRequest(CHG_REMOVE, scenario.fqdn_rev_, scenario.fqdn_fwd_,
                                    lease->addr_.toText(), lease->hostname_);
        }

        // Lease should have been deleted.
        lease = lmptr_->getLease4(lease->addr_);
        ASSERT_FALSE(lease);
    }
}

void Lease4CmdsTest::testLease4DnsRemoveD2Disabled() {
    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    disableD2();

    // Delete for valid, existing lease.
    string cmd =
        "{\n"
        "    \"command\": \"lease4-del\",\n"
        "    \"arguments\": {\n"
        "        \"ip-address\": \"192.0.2.8\",\n"
        "        \"update-ddns\": true\n"
        "    }\n"
        "}";

    // Let's create a lease with scenario attributes.
    Lease4Ptr lease = createLease4("192.0.2.8", 44, 0x08, 0x42);
    lease->hostname_ = "myhost.example.com.";
    lease->fqdn_rev_ = true;
    lease->fqdn_fwd_ = true;
    ASSERT_TRUE(lmptr_->addLease(lease));

    // NCR Queue is not enabled.
    ASSERT_EQ(ncrQueueSize(), -1);

    // Execute the delete command.
    static_cast<void>(testCommand(cmd, CONTROL_RESULT_SUCCESS, "IPv4 lease deleted."));

    // NCR Queue is not enabled.
    ASSERT_EQ(ncrQueueSize(), -1);

    // Lease should have been deleted.
    lease = lmptr_->getLease4(lease->addr_);
    ASSERT_FALSE(lease);
}

// Verify that v4 lease add handles conflict as expected.
void Lease4CmdsTest::testLease4ConflictingAdd() {
    MultiThreadingTest mt(true);

    // Initialize lease manager (false = v4, false = don't add leases)
    initLeaseMgr(false, false);

    // Make sure the lease doesn't exist.
    IOAddress addr("192.0.2.1");
    Lease4Ptr lease = lmptr_->getLease4(addr);
    ASSERT_FALSE(lease);

    // Verify stats show no leases.
    checkLease4Stats(44, 0, 0);

    // Lock the address.
    ResourceHandler4 resource_handler;
    ASSERT_TRUE(resource_handler.tryLock4(addr));

    // Now let's try to add the lease.
    string txt =
        "{\n"
        "    \"command\": \"lease4-add\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 44,\n"
        "        \"ip-address\": \"192.0.2.1\",\n"
        "        \"hw-address\": \"1a:1b:1c:1d:1e:1f\",\n"
        "        \"comment\": \"a comment\"\n"
        "    }\n"
        "}";

    string exp_rsp = "ResourceBusy: IP address:192.0.2.1 could not be added.";
    testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp);

    // Lease should not have been added.
    lease = lmptr_->getLease4(addr);
    ASSERT_FALSE(lease);

    // Stats should not have changed.
    checkLease4Stats(44, 0, 0);
}

void Lease4CmdsTest::testLease4ConflictingUpdate() {
    MultiThreadingTest mt(true);

    // Initialize lease manager (false = v4, true = add leases)
    initLeaseMgr(false, true);

    // Verify stats show no leases.
    checkLease4Stats(44, 2, 0);

    // Make sure the lease exists.
    IOAddress addr("192.0.2.1");
    Lease4Ptr lease = lmptr_->getLease4(addr);
    ASSERT_TRUE(lease);
    // Save a copy of the original lease.
    Lease4 original_lease = *lease;

    // Lock the address.
    ResourceHandler4 resource_handler;
    ASSERT_TRUE(resource_handler.tryLock4(addr));

    // Now let's try to update the lease.
    string txt =
        "{\n"
        "    \"command\": \"lease4-update\",\n"
        "    \"arguments\": {"
        "        \"subnet-id\": 44,\n"
        "        \"ip-address\": \"192.0.2.1\",\n"
        "        \"hw-address\": \"2a:2b:2c:2d:2e:2f\",\n"
        "        \"comment\": \"a comment\"\n"
        "    }\n"
        "}";

    string exp_rsp = "ResourceBusy: IP address:192.0.2.1 could not be updated.";
    testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp);

    // Fetch the lease again.
    lease = lmptr_->getLease4(addr);
    ASSERT_TRUE(lease);

    // Lease should not have been changed.
    EXPECT_EQ(original_lease, *lease);
}

TEST_F(Lease4CmdsTest, lease4AddMissingParams) {
    testLease4AddMissingParams();
}

TEST_F(Lease4CmdsTest, lease4AddMissingParamsMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4AddMissingParams();
}

TEST_F(Lease4CmdsTest, lease4AddBadParams) {
    testLease4AddBadParams();
}

TEST_F(Lease4CmdsTest, lease4AddBadParamsMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4AddBadParams();
}

TEST_F(Lease4CmdsTest, lease4Add) {
    testLease4Add();
}

TEST_F(Lease4CmdsTest, lease4AddMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4Add();
}

TEST_F(Lease4CmdsTest, lease4AddDeclinedLeases) {
    testLease4AddDeclinedLeases();
}

TEST_F(Lease4CmdsTest, lease4AddDeclinedLeasesMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4AddDeclinedLeases();
}

TEST_F(Lease4CmdsTest, lease4AddExisting) {
    testLease4AddExisting();
}

TEST_F(Lease4CmdsTest, lease4AddExistingMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4AddExisting();
}

TEST_F(Lease4CmdsTest, lease4AddSubnetIdMissing) {
    testLease4AddSubnetIdMissing();
}

TEST_F(Lease4CmdsTest, lease4AddSubnetIdMissingMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4AddSubnetIdMissing();
}

TEST_F(Lease4CmdsTest, lease4AddSubnetIdMissingDeclinedLeases) {
    testLease4AddSubnetIdMissingDeclinedLeases();
}

TEST_F(Lease4CmdsTest, lease4AddSubnetIdMissingDeclinedLeasesMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4AddSubnetIdMissingDeclinedLeases();
}

TEST_F(Lease4CmdsTest, lease4AddSubnetIdMissingBadAddr) {
    testLease4AddSubnetIdMissingBadAddr();
}

TEST_F(Lease4CmdsTest, lease4AddSubnetIdMissingBadAddrMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4AddSubnetIdMissingBadAddr();
}

TEST_F(Lease4CmdsTest, lease4AddNegativeExpireTime) {
    testLease4AddNegativeExpireTime();
}

TEST_F(Lease4CmdsTest, lease4AddNegativeExpireTimeMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4AddNegativeExpireTime();
}

TEST_F(Lease4CmdsTest, lease4AddNegativeCltt) {
    testLease4AddNegativeCltt();
}

TEST_F(Lease4CmdsTest, lease4AddNegativeClttMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4AddNegativeCltt();
}

TEST_F(Lease4CmdsTest, lease4AddFullAddr) {
    testLease4AddFullAddr();
}

TEST_F(Lease4CmdsTest, lease4AddFullAddrMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4AddFullAddr();
}

TEST_F(Lease4CmdsTest, lease4AddComment) {
    testLease4AddComment();
}

TEST_F(Lease4CmdsTest, lease4AddCommentMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4AddComment();
}

TEST_F(Lease4CmdsTest, lease4GetMissingParams) {
    testLease4GetMissingParams();
}

TEST_F(Lease4CmdsTest, lease4GetMissingParamsMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4GetMissingParams();
}

TEST_F(Lease4CmdsTest, lease4GetByAddrBadParam) {
    testLease4GetByAddrBadParam();
}

TEST_F(Lease4CmdsTest, lease4GetByAddrBadParamMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4GetByAddrBadParam();
}

TEST_F(Lease4CmdsTest, lease4GetByAddrNotFound) {
    testLease4GetByAddrNotFound();
}

TEST_F(Lease4CmdsTest, lease4GetByAddrNotFoundMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4GetByAddrNotFound();
}

TEST_F(Lease4CmdsTest, lease4GetByAddr) {
    testLease4GetByAddr();
}

TEST_F(Lease4CmdsTest, lease4GetByAddrMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4GetByAddr();
}

TEST_F(Lease4CmdsTest, lease4GetByHWAddrNotFound) {
    testLease4GetByHWAddrNotFound();
}

TEST_F(Lease4CmdsTest, lease4GetByHWAddrNotFoundMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4GetByHWAddrNotFound();
}

TEST_F(Lease4CmdsTest, lease4GetByHWAddr) {
    testLease4GetByHWAddr();
}

TEST_F(Lease4CmdsTest, lease4GetByHWAddrMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4GetByHWAddr();
}

TEST_F(Lease4CmdsTest, lease4GetByClientIdNotFound) {
    testLease4GetByClientIdNotFound();
}

TEST_F(Lease4CmdsTest, lease4GetByClientIdNotFoundMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4GetByClientIdNotFound();
}

TEST_F(Lease4CmdsTest, lease4GetByClientId) {
    testLease4GetByClientId();
}

TEST_F(Lease4CmdsTest, lease4GetByClientIdMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4GetByClientId();
}

TEST_F(Lease4CmdsTest, lease4GetAll) {
    testLease4GetAll();
}

TEST_F(Lease4CmdsTest, lease4GetAllMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4GetAll();
}

TEST_F(Lease4CmdsTest, lease4GetAllNoLeases) {
    testLease4GetAllNoLeases();
}

TEST_F(Lease4CmdsTest, lease4GetAllNoLeasesMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4GetAllNoLeases();
}

TEST_F(Lease4CmdsTest, lease4GetAllBySubnetId) {
    testLease4GetAllBySubnetId();
}

TEST_F(Lease4CmdsTest, lease4GetAllBySubnetIdMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4GetAllBySubnetId();
}

TEST_F(Lease4CmdsTest, lease4GetAllBySubnetIdNoLeases) {
    testLease4GetAllBySubnetIdNoLeases();
}

TEST_F(Lease4CmdsTest, lease4GetAllBySubnetIdNoLeasesMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4GetAllBySubnetIdNoLeases();
}

TEST_F(Lease4CmdsTest, lease4GetAllByMultipleSubnetIds) {
    testLease4GetAllByMultipleSubnetIds();
}

TEST_F(Lease4CmdsTest, lease4GetAllByMultipleSubnetIdsMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4GetAllByMultipleSubnetIds();
}

TEST_F(Lease4CmdsTest, lease4GetBySubnetIdInvalidArguments) {
    testLease4GetBySubnetIdInvalidArguments();
}

TEST_F(Lease4CmdsTest, lease4GetBySubnetIdInvalidArgumentsMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4GetBySubnetIdInvalidArguments();
}

TEST_F(Lease4CmdsTest, lease4GetPaged) {
    testLease4GetPaged();
}

TEST_F(Lease4CmdsTest, lease4GetPagedMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4GetPaged();
}

TEST_F(Lease4CmdsTest, lease4GetPagedZeroAddress) {
    testLease4GetPagedZeroAddress();
}

TEST_F(Lease4CmdsTest, lease4GetPagedZeroAddressMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4GetPagedZeroAddress();
}

TEST_F(Lease4CmdsTest, lease4GetPagedIPv6Address) {
    testLease4GetPagedIPv6Address();
}

TEST_F(Lease4CmdsTest, lease4GetPagedIPv6AddressMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4GetPagedIPv6Address();
}

TEST_F(Lease4CmdsTest, lease4GetPagedInvalidFrom) {
    testLease4GetPagedInvalidFrom();
}

TEST_F(Lease4CmdsTest, lease4GetPagedInvalidFromMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4GetPagedInvalidFrom();
}

TEST_F(Lease4CmdsTest, lease4GetPagedNoLimit) {
    testLease4GetPagedNoLimit();
}

TEST_F(Lease4CmdsTest, lease4GetPagedNoLimitMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4GetPagedNoLimit();
}

TEST_F(Lease4CmdsTest, lease4GetPagedLimitNotNumber) {
    testLease4GetPagedLimitNotNumber();
}

TEST_F(Lease4CmdsTest, lease4GetPagedLimitNotNumberMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4GetPagedLimitNotNumber();
}

TEST_F(Lease4CmdsTest, lease4GetPagedLimitIsZero) {
    testLease4GetPagedLimitIsZero();
}

TEST_F(Lease4CmdsTest, lease4GetPagedLimitIsZeroMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4GetPagedLimitIsZero();
}

TEST_F(Lease4CmdsTest, lease4GetByHwAddressParams) {
    testLease4GetByHwAddressParams();
}

TEST_F(Lease4CmdsTest, lease4GetByHwAddressParamsMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4GetByHwAddressParams();
}

TEST_F(Lease4CmdsTest, lease4GetByHwAddressFind0) {
    testLease4GetByHwAddressFind0();
}

TEST_F(Lease4CmdsTest, lease4GetByHwAddressFind0MultiThreading) {
    MultiThreadingTest mt(true);
    testLease4GetByHwAddressFind0();
}

TEST_F(Lease4CmdsTest, lease4GetByHwAddressFind2) {
    testLease4GetByHwAddressFind2();
}

TEST_F(Lease4CmdsTest, lease4GetByHwAddressFind2MultiThreading) {
    MultiThreadingTest mt(true);
    testLease4GetByHwAddressFind2();
}

TEST_F(Lease4CmdsTest, lease4GetByClientIdParams) {
    testLease4GetByClientIdParams();
}

TEST_F(Lease4CmdsTest, lease4GetByClientIdParamsMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4GetByClientIdParams();
}

TEST_F(Lease4CmdsTest, lease4GetByClientIdFind0) {
    testLease4GetByClientIdFind0();
}

TEST_F(Lease4CmdsTest, lease4GetByClientIdFind0MultiThreading) {
    MultiThreadingTest mt(true);
    testLease4GetByClientIdFind0();
}

TEST_F(Lease4CmdsTest, lease4GetByClientIdFind2) {
    testLease4GetByClientIdFind2();
}

TEST_F(Lease4CmdsTest, lease4GetByClientIdFind2MultiThreading) {
    MultiThreadingTest mt(true);
    testLease4GetByClientIdFind2();
}

TEST_F(Lease4CmdsTest, lease4GetByHostnameParams) {
    testLease4GetByHostnameParams();
}

TEST_F(Lease4CmdsTest, lease4GetByHostnameParamsMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4GetByHostnameParams();
}

TEST_F(Lease4CmdsTest, lease4GetByHostnameFind0) {
    testLease4GetByHostnameFind0();
}

TEST_F(Lease4CmdsTest, lease4GetByHostnameFind0MultiThreading) {
    MultiThreadingTest mt(true);
    testLease4GetByHostnameFind0();
}

TEST_F(Lease4CmdsTest, lease4GetByHostnameFind2) {
    testLease4GetByHostnameFind2();
}

TEST_F(Lease4CmdsTest, lease4GetByHostnameFind2MultiThreading) {
    MultiThreadingTest mt(true);
    testLease4GetByHostnameFind2();
}

TEST_F(Lease4CmdsTest, lease4UpdateMissingParams) {
    testLease4UpdateMissingParams();
}

TEST_F(Lease4CmdsTest, lease4UpdateMissingParamsMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4UpdateMissingParams();
}

TEST_F(Lease4CmdsTest, lease4UpdateBadParams) {
    testLease4UpdateBadParams();
}

TEST_F(Lease4CmdsTest, lease4UpdateBadParamsMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4UpdateBadParams();
}

TEST_F(Lease4CmdsTest, lease4UpdateNoLease) {
    testLease4UpdateNoLease();
}

TEST_F(Lease4CmdsTest, lease4UpdateNoLeaseMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4UpdateNoLease();
}

TEST_F(Lease4CmdsTest, lease4Update) {
    testLease4Update();
}

TEST_F(Lease4CmdsTest, lease4UpdateMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4Update();
}

TEST_F(Lease4CmdsTest, lease4UpdateDeclinedLeases) {
    testLease4UpdateDeclinedLeases();
}

TEST_F(Lease4CmdsTest, lease4UpdateDeclinedLeasesMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4UpdateDeclinedLeases();
}

TEST_F(Lease4CmdsTest, lease4UpdateNoSubnetId) {
    testLease4UpdateNoSubnetId();
}

TEST_F(Lease4CmdsTest, lease4UpdateNoSubnetIdMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4UpdateNoSubnetId();
}

TEST_F(Lease4CmdsTest, lease4UpdateNoSubnetIdDeclinedLeases) {
    testLease4UpdateNoSubnetIdDeclinedLeases();
}

TEST_F(Lease4CmdsTest, lease4UpdateNoSubnetIdDeclinedLeasesMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4UpdateNoSubnetIdDeclinedLeases();
}

TEST_F(Lease4CmdsTest, lease4UpdateForceCreate) {
    testLease4UpdateForceCreate();
}

TEST_F(Lease4CmdsTest, lease4UpdateForceCreateMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4UpdateForceCreate();
}

TEST_F(Lease4CmdsTest, lease4UpdateForceCreateNoSubnetId) {
    testLease4UpdateForceCreateNoSubnetId();
}

TEST_F(Lease4CmdsTest, lease4UpdateForceCreateNoSubnetIdMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4UpdateForceCreateNoSubnetId();
}

TEST_F(Lease4CmdsTest, lease4UpdateDoNotForceCreate) {
    testLease4UpdateDoNotForceCreate();
}

TEST_F(Lease4CmdsTest, lease4UpdateDoNotForceCreateMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4UpdateDoNotForceCreate();
}

TEST_F(Lease4CmdsTest, lease4UpdateComment) {
    testLease4UpdateComment();
}

TEST_F(Lease4CmdsTest, lease4UpdateCommentMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4UpdateComment();
}

TEST_F(Lease4CmdsTest, lease4DelMissingParams) {
    testLease4DelMissingParams();
}

TEST_F(Lease4CmdsTest, lease4DelMissingParamsMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4DelMissingParams();
}

TEST_F(Lease4CmdsTest, lease4DelByAddrNotFound) {
    testLease4DelByAddrNotFound();
}

TEST_F(Lease4CmdsTest, lease4DelByAddrNotFoundMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4DelByAddrNotFound();
}

TEST_F(Lease4CmdsTest, lease4DelByAddr) {
    testLease4DelByAddr();
}

TEST_F(Lease4CmdsTest, lease4DelByAddrMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4DelByAddr();
}

TEST_F(Lease4CmdsTest, lease4DelByAddrDeclinedLeases) {
    testLease4DelByAddrDeclinedLeases();
}

TEST_F(Lease4CmdsTest, lease4DelByAddrDeclinedLeasesMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4DelByAddrDeclinedLeases();
}

TEST_F(Lease4CmdsTest, lease4DelByAddrBadParam) {
    testLease4DelByAddrBadParam();
}

TEST_F(Lease4CmdsTest, lease4DelByAddrBadParamMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4DelByAddrBadParam();
}

TEST_F(Lease4CmdsTest, lease4DelByHWAddrNotFound) {
    testLease4DelByHWAddrNotFound();
}

TEST_F(Lease4CmdsTest, lease4DelByHWAddrNotFoundMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4DelByHWAddrNotFound();
}

TEST_F(Lease4CmdsTest, lease4DelByHWAddr) {
    testLease4DelByHWAddr();
}

TEST_F(Lease4CmdsTest, lease4DelByHWAddrMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4DelByHWAddr();
}

TEST_F(Lease4CmdsTest, lease4DelByClientIdNotFound) {
    testLease4DelByClientIdNotFound();
}

TEST_F(Lease4CmdsTest, lease4DelByClientIdNotFoundMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4DelByClientIdNotFound();
}

TEST_F(Lease4CmdsTest, lease4DelByClientId) {
    testLease4DelByClientId();
}

TEST_F(Lease4CmdsTest, lease4DelByClientIdMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4DelByClientId();
}

TEST_F(Lease4CmdsTest, lease4Wipe) {
    testLease4Wipe();
}

TEST_F(Lease4CmdsTest, lease4WipeMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4Wipe();
}

TEST_F(Lease4CmdsTest, lease4WipeAll) {
    testLease4WipeAll();
}

TEST_F(Lease4CmdsTest, lease4WipeAllMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4WipeAll();
}

TEST_F(Lease4CmdsTest, lease4WipeAllNoArgs) {
    testLease4WipeAllNoArgs();
}

TEST_F(Lease4CmdsTest, lease4WipeAllNoArgsMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4WipeAllNoArgs();
}

TEST_F(Lease4CmdsTest, lease4WipeNoLeases) {
    testLease4WipeNoLeases();
}

TEST_F(Lease4CmdsTest, lease4WipeNoLeasesMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4WipeNoLeases();
}

TEST_F(Lease4CmdsTest, lease4WipeNoLeasesAll) {
    testLease4WipeNoLeasesAll();
}

TEST_F(Lease4CmdsTest, lease4WipeNoLeasesAllMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4WipeNoLeasesAll();
}


TEST_F(Lease4CmdsTest, lease4BrokenUpdate) {
    testLease4BrokenUpdate();
}

TEST_F(Lease4CmdsTest, lease4BrokenUpdateMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4BrokenUpdate();
}

TEST_F(Lease4CmdsTest, lease4ResendDdnsBadParam) {
    testLease4ResendDdnsBadParam();
}

TEST_F(Lease4CmdsTest, lease4ResendDdnsBadParamMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4ResendDdnsBadParam();
}

TEST_F(Lease4CmdsTest, lease4ResendDdnsDisabled) {
    testLease4ResendDdnsDisabled();
}

TEST_F(Lease4CmdsTest, lease4ResendDdnsDisabledMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4ResendDdnsDisabled();
}

TEST_F(Lease4CmdsTest, lease4ResendDdnsNoLease) {
    testLease4ResendDdnsNoLease();
}

TEST_F(Lease4CmdsTest, lease4ResendDdnsNoLeaseMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4ResendDdnsNoLease();
}

TEST_F(Lease4CmdsTest, lease4ResendNoHostname) {
    testLease4ResendNoHostname();
}

TEST_F(Lease4CmdsTest, lease4ResendNoHostnameMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4ResendNoHostname();
}

TEST_F(Lease4CmdsTest, lease4ResendNoDirectionsEnabled) {
    testLease4ResendNoDirectionsEnabled();
}

TEST_F(Lease4CmdsTest, lease4ResendNoDirectionsEnabledMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4ResendNoDirectionsEnabled();
}

TEST_F(Lease4CmdsTest, lease4ResendDdnsEnabled) {
    testLease4ResendDdnsEnabled();
}

TEST_F(Lease4CmdsTest, lease4ResendDdnsEnabledMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4ResendDdnsEnabled();
}

TEST_F(Lease4CmdsTest, lease4DnsRemoveD2Enabled) {
    testLease4DnsRemoveD2Enabled();
}

TEST_F(Lease4CmdsTest, lease4DnsRemoveD2EnabledMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4DnsRemoveD2Enabled();
}

TEST_F(Lease4CmdsTest, lease4DnsRemoveD2Disabled) {
    testLease4DnsRemoveD2Disabled();
}

TEST_F(Lease4CmdsTest, lease4DnsRemoveD2DisabledMultiThreading) {
    MultiThreadingTest mt(true);
    testLease4DnsRemoveD2Disabled();
}

TEST_F(Lease4CmdsTest, lease4ConflictingAddMultiThreading) {
    testLease4ConflictingAdd();
}

TEST_F(Lease4CmdsTest, lease4ConflictingUpdateMultiThreading) {
    testLease4ConflictingUpdate();
}

} // end of anonymous namespace