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
// Copyright (C) 2020-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Kea Hooks Basic
// Commercial End User License Agreement v2.0. See COPYING file in the premium/
// directory.

/// @file This file contains tests LeaseQueryImpl6 class.

#include <config.h>
#include <dhcp/dhcp6.h>
#include <dhcp/option_int.h>
#include <dhcp/option_custom.h>
#include <dhcp/option6_addrlst.h>
#include <dhcp/option6_iaaddr.h>
#include <dhcp/option6_iaprefix.h>
#include <dhcp/option6_status_code.h>
#include <dhcp/pkt6.h>
#include <dhcp/testutils/iface_mgr_test_config.h>
#include <dhcpsrv/cfgmgr.h>
#include <dhcpsrv/lease_mgr_factory.h>
#include <lease_query_impl6.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <testutils/gtest_utils.h>
#include <test_lease_mgr.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <gtest/gtest.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <sstream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <queue><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace std;
using namespace isc;
using namespace isc::asiolink;
using namespace isc::data;
using namespace isc::dhcp;
using namespace isc::dhcp::test;
using namespace isc::lease_query;
using namespace isc::test;
using namespace isc::util;

namespace {

/// @brief LeaseQueryImpl6 derivation for testing
///
/// This derivation overrides the send() method with a simple
/// queue which captures "outbound" responses so they may
/// be examined.
class LeaseQueryTestImpl6 : public LeaseQueryImpl6 {
public:
    /// @brief Constructor
    ///
    /// @param config input configuration
    /// @param queue reference to queue on which to place sent packets
    LeaseQueryTestImpl6(const isc::data::ConstElementPtr config, std::queue<Pkt6Ptr>& queue)
    : LeaseQueryImpl6(config), packet_queue_(queue) {
    }

    /// @brief Sends a DHCPv6 packet
    ///
    /// Overrides the base class implementation. Rather than passing the
    /// packet to IfaceMgr it pushes it to the end of the packet queue.
    void send(const Pkt6Ptr& packet) const override {
        packet_queue_.push(packet);
    }

    /// @brief Reference to the queue of packets
    std::queue<Pkt6Ptr>& packet_queue_;
};

/// @brief Text fixture for testing LeaseQueryImpl6
template <typename TestLeaseMgrType>
class BaseLeaseQueryImpl6ProcessTest : public ::testing::Test {
public:
    /// @brief Path name of server's duid file
    static const std::string duid_file_;

    /// @brief Constructor
    ///
    /// Clears the configuration and unloads hook libraries.
    BaseLeaseQueryImpl6ProcessTest()
        : cid_empty_(),
          cid1_(new DUID(std::vector<uint8_t>{ 0x01,0x02,0x03,0x04,0x05,0x06 })),
          cid2_(new DUID(std::vector<uint8_t>{ 0x01,0x02,0x03,0x04,0x05,0x07 })) {
        CfgMgr::instance().clear();
    }

    /// @brief Destructor
    ///
    /// Clears the configuration and unloads hook libraries.
    virtual ~BaseLeaseQueryImpl6ProcessTest() {
        LeaseMgrFactory::destroy();
        CfgMgr::instance().clear();
        static_cast<void>(remove(duid_file_.c_str()));
        TestLeaseMgrType test_lease_mgr;
        test_lease_mgr.destroySchema();
    }

    /// @brief  Prepares system managers for testing
    /// - Creates a new v4 lease manager
    /// - Creates the configuration.
    /// - Inits IfaceMgr with test config.
    void SetUp() override {
        LeaseMgrFactory::destroy();
        TestLeaseMgrType test_lease_mgr;
        test_lease_mgr.createSchema();
        LeaseMgrFactory::create(test_lease_mgr.dbAccess());

        CfgMgr& cfg_mgr = CfgMgr::instance();
        CfgSubnets6Ptr subnets = cfg_mgr.getStagingCfg()->getCfgSubnets6();

        // Build and add subnet2_.
        subnet2_.reset(new Subnet6(IOAddress("2001:db8:2::"), 64,
                                   1200, 2400, 3600, 3600, 2));

        Pool6Ptr pool(new Pool6(Lease::TYPE_NA, IOAddress("2001:db8:2::100"),
                                IOAddress("2001:db8:2::200")));

        CfgOptionPtr cfg_option = pool->getCfgOption();
        subnet2_->addPool(pool);

        // Add a second pool.
        pool.reset(new Pool6(Lease::TYPE_NA, IOAddress("2001:db8:2::201"),
                             IOAddress("2001:db8:2::220")));
        subnet2_->addPool(pool);
        subnets->add(subnet2_);

        // Build and add subnet3_.
        Triplet<uint32_t> unspec;
        subnet3_.reset(new Subnet6(IOAddress("2001:db8:3::"), 64,
                                   unspec, unspec, 3600, 3600, 3));

        pool.reset(new Pool6(Lease::TYPE_NA, IOAddress("2001:db8:3::100"),
                             IOAddress("2001:db8:3::200")));

        subnet3_->addPool(pool);
        subnet3_->setT1Percent(0.5);
        subnet3_->setT2Percent(0.75);
        subnet3_->setCalculateTeeTimes(true);
        subnets->add(subnet3_);

        // Build and add subnet4_.
        subnet4_.reset(new Subnet6(IOAddress("2001:db8:4::"), 64,
                                   unspec, unspec, 3600, 3600, 4));

        pool.reset(new Pool6(Lease::TYPE_NA, IOAddress("2001:db8:4::100"),
                             IOAddress("2001:db8:4::200")));

        subnet4_->addPool(pool);

        // Add PD pool
        pool.reset(new Pool6(Lease::TYPE_PD, IOAddress("3001::"), 64, 64));
        subnet4_->addPool(pool);

        // Add second PD pool
        pool.reset(new Pool6(Lease::TYPE_PD, IOAddress("3002::"), 64, 72));
        subnet4_->addPool(pool);

        subnet4_->setCalculateTeeTimes(false);
        subnets->add(subnet4_);

        // Build and add a shared-network.
        CfgSharedNetworks6Ptr networks = cfg_mgr.getStagingCfg()->getCfgSharedNetworks6();
        network1_.reset(new SharedNetwork6("one"));
        network1_->add(subnet4_);
        networks->add(network1_);

        // Configure a server id
        server_id_ = CfgMgr::instance().getStagingCfg()->getCfgDUID()->create(duid_file_);

        // Commit the config.
        cfg_mgr.commit();

        // Initialize interface mgr with test config.
        iface_config_.reset(new IfaceMgrTestConfig(true));
        IfaceMgr::instance().openSockets6();

        // Create the query option.
        lq_query_def_ = LibDHCP::getOptionDef(DHCP6_OPTION_SPACE, D6O_LQ_QUERY);
        ASSERT_TRUE(lq_query_def_) << "D6O_LQ_QUERY is not undefined";
    }

    /// @brief Instantiates a LeaseQueryImpl6 with a valid configuration
    ///
    /// @return Pointer to the new instance.
    LeaseQueryImpl6Ptr createLeaseQueryImpl6(const std::string& prefixes = "") {
        std::ostringstream oss;
        oss << "{ \"requesters\" : [ \"2001:db8:2::1\", \"2001:db8:3::1\" ]";
        if (!prefixes.empty()) {
            oss << ", \"prefix-lengths\" : " << prefixes;
        }

        oss << " }";

        ConstElementPtr config;
        try {
            config = Element::fromJSON(oss.str());
        } catch (const exception& ex) {
            isc_throw(Unexpected, "invalid query config, test is broken: "
                      << ex.what());
        }

        LeaseQueryImpl6Ptr impl;
        try {
            impl.reset(new LeaseQueryTestImpl6(config, sent_packets_));
        } catch (const exception& ex) {
            isc_throw(Unexpected, "impl creation failed: " << ex.what());
        }

        return (impl);
    }

    /// @brief Adds a v6 lease to the lease store.
    ///
    /// @param ltype lease type of the lease
    /// @param addr ip address of the lease
    /// @param subnet  subnet to which the lease belongs
    /// @param client_id duid of the lease owner
    /// @param cltt CLTT of the lease
    /// @param json_context user context of the lease as JSON
    /// @param state lease state of the lease
    ///
    /// @return pointer to the new lease
    Lease6Ptr addLease(const Lease::Type& ltype, const IOAddress& addr,
                       const Subnet6Ptr& subnet, const DuidPtr& client_id,
                       time_t cltt, const std::string& json_context = "",
                       const uint32_t state = Lease::STATE_DEFAULT) {
        static uint32_t iaid=1;

        if (!subnet) {
            isc_throw(Unexpected, "subnet cannot be empty");
        }

        Lease6Ptr lease(new Lease6(ltype, addr, client_id, ++iaid, subnet->getPreferred(),
                                   subnet->getValid(), subnet->getID()));
        lease->cltt_ = cltt;
        lease->state_ = state;
        if (!json_context.empty()) {
            ElementPtr user_context;
            user_context = Element::fromJSON(json_context);
            lease->setContext(user_context);
        }

        LeaseMgrFactory::instance().addLease(lease);
        return (lease);
    }

    /// @brief Converts a packed RELAY_FORW message into a list of RelayInfos
    ///
    /// This function was largely borrowed from Pkt6. The primary difference
    /// is not expected the innermost message to contain a D6O_RELAY_MSG.
    ///
    /// @param data buffer containing the packed RELAY_FORW message
    /// @param[out] relay_infos list into which the RelayInfo instances
    /// should be stored.
    void unpackRelayForwardMsg(const std::vector<uint8_t>data,
                               std::vector<RelayInfoPtr>& relay_infos) {
        size_t bufsize = data.size();
        size_t offset = 0;

        while (bufsize >= Pkt6::DHCPV6_RELAY_HDR_LEN) {
            RelayInfoPtr relay(new Pkt6::RelayInfo);

            size_t relay_msg_offset = 0;
            size_t relay_msg_len = 0;

            // parse fixed header first (first 34 bytes)
            relay->msg_type_ = data[offset++];
            if (relay->msg_type_ != DHCPV6_RELAY_FORW) {
                isc_throw(Unexpected, "invalid message type: " << relay->msg_type_);
            }

            relay->hop_count_ = data[offset++];
            relay->linkaddr_ = IOAddress::fromBytes(AF_INET6, &data[offset]);
            offset += isc::asiolink::V6ADDRESS_LEN;
            relay->peeraddr_ = IOAddress::fromBytes(AF_INET6, &data[offset]);
            offset += isc::asiolink::V6ADDRESS_LEN;
            bufsize -= Pkt6::DHCPV6_RELAY_HDR_LEN; // 34 bytes (1+1+16+16)

            // parse the rest as options
            OptionBuffer opt_buffer;
            if (bufsize != 0) {
                opt_buffer = OptionBuffer(&data[offset], &data[offset] + bufsize);
            }

            // If custom option parsing function has been set, use this function
            // to parse options. Otherwise, use standard function from libdhcp.
            int last_offset =
            LibDHCP::unpackOptions6(opt_buffer, DHCP6_OPTION_SPACE, relay->options_,
                                    &relay_msg_offset, &relay_msg_len);

            // Store the parsed relay information.
            relay_infos.push_back(relay);

            // We're done if this relay has no relay message.
            if (relay_msg_offset == 0 || relay_msg_len == 0) {
                // Account for the options in this relay (if any).
                offset += last_offset;
                bufsize -= last_offset;
                break;
            }

            if (relay_msg_len >= bufsize) {
                // length of the relay_msg option extends beyond end of the message
                isc_throw(Unexpected, "Relay-msg option is truncated.");
            }

            uint8_t inner_type = data[offset + relay_msg_offset];
            offset += relay_msg_offset; // offset is relative
            bufsize = relay_msg_len;    // length is absolute

            if (inner_type != DHCPV6_RELAY_FORW) {
                isc_throw(Unexpected, "inner message is not a RELAY_FORW");
            }
        }

        if ( (offset != data.size()) || (bufsize != 0) ) {
            isc_throw(Unexpected, "message not parsed completely");
        }
    }

    /// @brief Convenience method for making a server identifier option instance.
    ///
    /// @param opt_type option type (D6O_...)
    /// @param data binary data a vector of bytes
    ///
    /// @return Pointer to the newly constructed option.
    OptionPtr makeBinaryOption(const uint16_t& opt_type,
                               const std::vector<uint8_t>& data) const {
        OptionPtr opt(new Option(Option::V6, opt_type, data));
        return (opt);
    }

    /// @brief Convenience method for making a server identifier option instance.
    ///
    /// @param data server id as a vector of bytes
    ///
    /// @return Pointer to the newly constructed option.
    OptionPtr makeServerIdOption(const std::vector<uint8_t>& data) const {
        return (makeBinaryOption(D6O_SERVERID, data));
    }

    /// @brief Convenience method for making a client identifier option instance.
    ///
    /// @param data client id as a vector of bytes
    ///
    /// @return Pointer to the newly constructed option.
    OptionPtr makeClientIdOption(const std::vector<uint8_t>& data) const {
        return (makeBinaryOption(D6O_CLIENTID, data));
    }

    /// @brief Creates a DHCPV6_LEASEQUERY packet for query by ip address
    ///
    /// @param qry_iaaddr ip address for which to query
    ///
    /// @return DHCPV6_LEASEQUERY packet
    Pkt6Ptr makeQueryByIpAddress(const IOAddress& qry_iaaddr) const {
        return (makeLeaseQuery(LQ6QT_BY_ADDRESS, qry_iaaddr, DuidPtr()));
    }

    /// @brief Creates a DHCPV6_LEASEQUERY packet for query by client id
    ///
    /// @param qry_cid client id for which to query
    /// @param link_addr link address to filter by
    ///
    /// @return DHCPV6_LEASEQUERY packet
    Pkt6Ptr makeQueryByClientId(const DuidPtr& qry_cid, const IOAddress& link_addr
                                = IOAddress::IPV6_ZERO_ADDRESS()) const {
        return (makeLeaseQuery(LQ6QT_BY_CLIENTID, link_addr, qry_cid));
    }

    /// @brief Constructs a DHCPV6_LEASEQUERY message.
    ///
    /// @param qry_type type of query to perform
    /// @param qry_iaaddr ip address for which to query
    /// @param qry_link_addr ip address of the link for which to query. Defaults
    /// to ::.
    Pkt6Ptr makeLeaseQuery(const uint8_t& qry_type, const IOAddress& qry_iaaddr,
                           const DuidPtr& qry_cid, const IOAddress& qry_link_addr =
                           IOAddress::IPV6_ZERO_ADDRESS()) const {
        Pkt6Ptr lq(new Pkt6(DHCPV6_LEASEQUERY, 123));
        // Give a client id and a known requester.
        lq->addOption(makeClientIdOption(std::vector<uint8_t>{ 01, 02, 03, 04, 05, 06}));
        lq->setRemoteAddr(IOAddress("2001:db8:2::1"));

        // Create the query option.
        OptionCustomPtr lq_option(new OptionCustom(*lq_query_def_, Option::V6));

        // Set the query option's query type and query link fields.
        lq_option->writeInteger<uint8_t>(qry_type, 0);
        lq_option->writeAddress(qry_link_addr, 1);

        // Add a D6O_IAADDR option if we have a non-zero address parameter.
        if (qry_iaaddr != IOAddress::IPV6_ZERO_ADDRESS()) {
            Option6IAAddrPtr opt;
            opt.reset(new Option6IAAddr(D6O_IAADDR, qry_iaaddr, 0, 0));
            lq_option->addOption(opt);
        }

        // Add a D6O_CLIENTID option if we have a non-empty client id parameter.
        if (qry_cid) {
            OptionPtr opt = makeClientIdOption(qry_cid->getDuid());
            lq_option->addOption(opt);
        }

        lq->addOption(lq_option);
        return (lq);
    }

    /// @brief Validate a D6O_IAAddr option against a lease and elapsed time
    ///
    /// @param opt OptionPtr which should point to a D6O_IAAddr instance
    /// @param lease lease from which the option should have been created
    /// @param elapsed number of seconds that have elapsed since the
    /// lease's CLTT
    void checkIAAddrOption(OptionPtr opt, const Lease6Ptr lease, uint32_t elapsed) {
        Option6IAAddrPtr iaaddr_opt;
        iaaddr_opt = boost::dynamic_pointer_cast<Option6IAAddr>(opt);
        ASSERT_TRUE(iaaddr_opt);
        EXPECT_EQ(lease->addr_, iaaddr_opt->getAddress());
        // Since the initial lifetime were set, one second could have ticked,
        // so allow one second of margin error.
        EXPECT_NEAR(lease->valid_lft_ - elapsed, iaaddr_opt->getValid(), 1);
        EXPECT_NEAR(lease->preferred_lft_ - elapsed, iaaddr_opt->getPreferred(), 1);
    }

    /// @brief Validate a D6O_IAPrefix option against a lease and elapsed time
    ///
    /// @param opt OptionPtr which should point to a D6O_IAAddr instance
    /// @param lease lease from which the option should have been created
    /// @param elapsed number of seconds that have elapsed since the
    /// lease's CLTT
    void checkIAPrefixOption(OptionPtr opt, const Lease6Ptr lease, uint32_t elapsed = 0) {
        Option6IAPrefixPtr iaprefix_opt;
        iaprefix_opt = boost::dynamic_pointer_cast<Option6IAPrefix>(opt);
        ASSERT_TRUE(iaprefix_opt);
        EXPECT_EQ(lease->addr_, iaprefix_opt->getAddress());
        EXPECT_EQ(lease->prefixlen_, iaprefix_opt->getLength());
        if (elapsed) {
            // Since the initial lifetime were set, one second could have ticked,
            // so allow one second of margin error.
            EXPECT_NEAR(lease->valid_lft_ - elapsed, iaprefix_opt->getValid(), 1);
            EXPECT_NEAR(lease->preferred_lft_ - elapsed, iaprefix_opt->getPreferred(), 1);
        }
    }

    /// @brief Validate that a Pkt6 contains the expected D6O_STATUS_CODE
    ///
    /// @param reply reply packet
    /// @param exp_status expected status code
    /// @param exp_msg expected status text message
    void checkStatus(const Pkt6Ptr& reply, const DHCPv6StatusCode& exp_status,
                     const std::string& exp_msg = "") {
        Option6StatusCodePtr status_opt = boost::dynamic_pointer_cast<Option6StatusCode>
                                          (reply->getOption(D6O_STATUS_CODE));
        ASSERT_TRUE(status_opt) << "D6O_STATUS_CODE missing";
        checkStatus(status_opt, exp_status, exp_msg);
    }

    /// @brief Validate that a Pkt6 contains the expected D6O_STATUS_CODE
    ///
    /// @param status_opt status-code option to check
    /// @param exp_status expected status code
    /// @param exp_msg expected status text message
    void checkStatus(Option6StatusCodePtr& status_opt, const DHCPv6StatusCode& exp_status,
                     const std::string& exp_msg = "") {
        EXPECT_EQ(status_opt->getStatusCode(), exp_status);
        EXPECT_EQ(status_opt->getStatusMessage(), exp_msg);
    }

    /// @brief Fetches a packet from the sent packet queue
    ///
    /// Used in conjunction with LeaseQueryTestImpl6 instances which
    /// "send" packets to the queue.  If the queue is not empty, it pops a
    /// a packed packet from the queue.  It then creates a clone using the
    /// packed content, and unpacks it.  The clone is then returned.
    /// Cloning ensures that callers do not see any vestigial information.
    ///
    /// @return A clone of the packet de-queued
    Pkt6Ptr getSentPacket() {
        Pkt6Ptr clone;
        if (!sent_packets_.empty()) {
            Pkt6Ptr sent_packet = sent_packets_.front();
            sent_packets_.pop();
            clone.reset(new Pkt6(sent_packet->getBuffer().getData(),
                                 sent_packet->getBuffer().getLength()));
            clone->unpack();
        }

        return (clone);
    }

    /// @brief Flushes any packets in the sent packet queue.
    void flushSentPackets() {
        while (!sent_packets_.empty()) {
            sent_packets_.pop();
        }
    }

    /// @brief Test makeClientOption with multiple links.
    void testMakeClientOptionMultipleLinks();

    /// @brief Test makeClientOption with a single link.
    void testMakeClientOptionSingleLink();

    /// @brief Test makeRelayOption with no relay info.
    void testMakeRelayOptionNoRelayInfo();

    /// @brief Test makeRelayOption with relay info.
    void testMakeRelayOptionRelayInfo();

    /// @brief Test makeRelayOption with old relay info.
    void testOldMakeRelayOptionRelayInfo();

    /// @brief Test queryByIpAddress with no active lease.
    void testQueryByIpAddressNoActiveLease();

    /// @brief Test queryByIpAddress with active lease.
    void testQueryByIpAddressActiveLease();

    /// @brief Test queryByClientId with no active lease.
    void testQueryByClientIdNoActiveLease();

    /// @brief Test queryByClientId with multiple links.
    void testQueryByClientIdMultipleLinks();

    /// @brief Test queryByClientId with active leases.
    void testQueryByClientIdActiveLeases();

    /// @brief Test queryByClientId with link address.
    void testQueryByClientIdLinkAddr();

    /// @brief Test queryByIpaddress with prefix delegation leases.
    void testQueryByIpaddressPDLeases();

    /// @brief Queue to hold any packets sent by LeaseQueryImpl6Test
    /// instances.
    std::queue<Pkt6Ptr> sent_packets_;

    /// @brief Convenience values.
    DuidPtr cid_empty_;
    DuidPtr cid1_;
    DuidPtr cid2_;
    Subnet6Ptr subnet2_;
    Subnet6Ptr subnet3_;
    Subnet6Ptr subnet4_;
    SharedNetwork6Ptr network1_;
    DuidPtr server_id_;
    OptionDefinitionPtr lq_query_def_;

    // Test configuration for IfaceMgr.
    boost::shared_ptr<IfaceMgrTestConfig> iface_config_;
};

/// @brief Path name for server-id generation
template <typename TestLeaseMgrType> const std::string
BaseLeaseQueryImpl6ProcessTest<TestLeaseMgrType>::duid_file_
    = std::string(TEST_DATA_BUILDDIR) + std::string("/lq6-duid");

// Verifies that configuration errors are detected.
TEST(LeaseQueryImpl6Test, invalidConfig6) {<--- syntax error

    struct Scenario {
        const std::string description_;
        ConstElementPtr config_;
        const std::string exp_msg_;
    };

    std::vector<Scenario> scenarios = {
        {
            "empty config",
            ConstElementPtr(),
            "Lease Query config is empty or not a map"
        },
        {
            "config not a map",
            Element::create("foo"),
            "Lease Query config is empty or not a map"
        },
        {
            "requesters missing",
            Element::fromJSON("{}"),
            "'requesters' address list is missing or not a list"
        },
        {
            "requesters not a list",
            Element::fromJSON("{ \"requesters\" : 123 }"),
            "'requesters' address list is missing or not a list"
        },
        {
            "requesters list is empty",
            Element::fromJSON("{ \"requesters\" : [] }"),
            "'requesters' address list cannot be empty"
        },
        {
            "requesters entry not an address",
            Element::fromJSON("{ \"requesters\" : [ \"foo\" ] }"),
            "'requesters' entry 'foo' is invalid: Failed to convert"
            " string to address 'foo': Invalid argument"
        },
        {
            "requesters entry not a v6 address",
            Element::fromJSON("{ \"requesters\" : [ \"192.0.2.1\" ] }"),
            "'requesters' entry '192.0.2.1' is invalid: not a IPv6 address"
        },
        {
            "requesters entry is a duplicate",
            Element::fromJSON("{ \"requesters\" : [ \"2001:db8:1::\", \"2001:db8:1::\" ] }"),
            "'requesters' entry '2001:db8:1::' is invalid: address is already in the list"
        },
        {
            "prefix_lengths not a list",
            Element::fromJSON("{ \"requesters\" : [ \"2001:db8:1::\" ], \"prefix-lengths\": 77 }"),
            "'prefix-lengths' is not a list"
        },
        {
            "prefix_lengths entry is not an int",
            Element::fromJSON("{ \"requesters\" : [ \"2001:db8:1::\" ], \"prefix-lengths\": [ \"boo\" ] }"),
            "'prefix-lengths' entry '\"boo\"' is invalid: must be an integer"
        },
        {
            "prefix_lengths entry is 0",
            Element::fromJSON("{ \"requesters\" : [ \"2001:db8:1::\" ], \"prefix-lengths\": [ 0 ] }"),
            "'prefix-lengths' entry '0' is invalid: must be greater than 0 and less than or equal to 128"

        },
        {
            "prefix_lengths entry is 129",
            Element::fromJSON("{ \"requesters\" : [ \"2001:db8:1::\" ], \"prefix-lengths\": [ 4, 129 ] }"),
            "'prefix-lengths' entry '129' is invalid: must be greater than 0 and less than or equal to 128"

        }
    };

    for (auto const& scenario : scenarios) {
        SCOPED_TRACE(scenario.description_);
        LeaseQueryImplPtr impl;
        ASSERT_THROW_MSG(impl.reset(new LeaseQueryImpl6(scenario.config_)),
                         BadValue, scenario.exp_msg_);
    }
}

// Verifies that valid v6 configuration parses and that requesters
// can be validated.
TEST(LeaseQueryImpl6Test, validConfig6) {
    // Create an implementation with two requesters.
    const std::string json = "{ \"requesters\" : [ \"2001:db8:1::1\", \"2001:db8:1::3\" ] }";
    ConstElementPtr config;
    ASSERT_NO_THROW_LOG(config = Element::fromJSON(json));

    LeaseQueryImplPtr impl;
    ASSERT_NO_THROW_LOG(impl.reset(new LeaseQueryImpl6(config)));

    // Verify known and unknown requesters check correctly.
    EXPECT_FALSE(impl->isRequester(IOAddress("2001:db8:1::")));
    EXPECT_TRUE(impl->isRequester(IOAddress("2001:db8:1::1")));
    EXPECT_FALSE(impl->isRequester(IOAddress("2001:db8:1::2")));
    EXPECT_TRUE(impl->isRequester(IOAddress("2001:db8:1::3")));

    // Make sure a test with a v4 address complains.
    ASSERT_THROW_MSG(impl->isRequester(IOAddress("192.0.2.1")), BadValue,
                     "not a IPv6 address");
}

/// @brief Memfile test fixture.
class MemfileLeaseQueryImpl6ProcessTest : public
    BaseLeaseQueryImpl6ProcessTest<MemfileTestLeaseMgr> {
};

// These tests verify scenarios where the inbound packet invalid and
// to for which the hook lib will drop the packet without generating
// a reply.
// - Not a DHCPv6 packet
// - No D6O_CLIENTID option
// - Non-matching D6O_SERVERID
// - Source address of ::
// - Source address that does not match a requester
// - No D6O_LQ_QUERY option
TEST_F(MemfileLeaseQueryImpl6ProcessTest, processQueryInvalidQuery) {
    // Create an implementation with two requesters.
    const std::string json = "{ \"requesters\" : [ \"2001:db8:2::1\", \"2001:db8:3::1\" ] }";
    ConstElementPtr config;
    ASSERT_NO_THROW_LOG(config = Element::fromJSON(json));
    LeaseQueryImpl6Ptr impl;
    ASSERT_NO_THROW_LOG(impl.reset(new LeaseQueryImpl6(config)));

    // A v4 packet should get tossed.
    Pkt4Ptr pkt4(new Pkt4(DHCPLEASEQUERY, 0));
    ASSERT_THROW_MSG(impl->processQuery(pkt4), BadValue,
                     "LeaseQueryImpl6 query is not DHCPv6 packet");

    // No client-id option should fail.
    Pkt6Ptr lq(new Pkt6(DHCPV6_LEASEQUERY_REPLY, 123));
    ASSERT_THROW_MSG(impl->processQuery(lq), BadValue,
                     "DHCPV6_LEASEQUERY must supply a D6O_CLIENTID");

    // Add a client-id option.
    lq->addOption(makeClientIdOption(std::vector<uint8_t>{ 01, 02, 03, 04, 05, 06}));

    // Add an a non-matching server id.
    lq->addOption(makeServerIdOption(std::vector<uint8_t>{ 10, 11, 12, 13, 14, 15, 16 }));
    ASSERT_THROW_MSG(impl->processQuery(lq), BadValue,
                     "rejecting DHCPV6_LEASEQUERY from: ::,"
                     " unknown server-id: type=00002, len=00007: 0a:0b:0c:0d:0e:0f:10");

    // Add a matching server id.
    lq->delOption(D6O_SERVERID);
    lq->addOption(makeServerIdOption(server_id_->getDuid()));

    // Source address cannot be ::.
    ASSERT_THROW_MSG(impl->processQuery(lq), BadValue,
                     "DHCPV6_LEASEQUERY source address cannot be ::");

    // Set source address to an unknown requester address.
    lq->setRemoteAddr(IOAddress("de:ad:be:ef::"));

    // An unknown requester should fail.
    ASSERT_THROW_MSG(impl->processQuery(lq), BadValue,
                     "rejecting DHCPV6_LEASEQUERY from unauthorized requester: de:ad:be:ef::");

    // Set source address to a known requester address.
    lq->setRemoteAddr(IOAddress("2001:db8:2::1"));

    // A query without a D6O_LQ_QUERY option should fail.
    ASSERT_THROW_MSG(impl->processQuery(lq), BadValue,
                     "DHCPV6_LEASEQUERY must supply a D6O_LQ_QUERY option");
}

// Verifies the operation of LeaseQueryImpl6::initReply().
TEST_F(MemfileLeaseQueryImpl6ProcessTest, initReply) {
    LeaseQueryImpl6Ptr impl;
    ASSERT_NO_THROW_LOG(impl = createLeaseQueryImpl6());

    Pkt6Ptr lq = makeQueryByIpAddress(IOAddress("2001:db8::100"));
    lq->setIface("eth1");
    lq->setIndex(ETH1_INDEX);
    lq->setRemoteAddr(IOAddress("2001:db8::77"));
    lq->setRemotePort(777);
    lq->setLocalAddr(IOAddress("2001:db8::88"));
    lq->setLocalPort(888);

    // Given a valid content in the query, the reply should
    // be generated without a throw.
    Pkt6Ptr reply;
    ASSERT_NO_THROW_LOG(reply = impl->initReply(lq));
    ASSERT_TRUE(reply);

    // Make sure it's the right message type.
    EXPECT_EQ(DHCPV6_LEASEQUERY_REPLY, reply->getType());

    // Verify reply content that is copied from the
    // input query.
    EXPECT_EQ(lq->getTransid(), reply->getTransid());
    EXPECT_EQ(lq->getRemoteAddr(), reply->getRemoteAddr());
    EXPECT_EQ(lq->getRemotePort(), reply->getRemotePort());
    EXPECT_EQ(lq->getLocalAddr(), reply->getLocalAddr());;
    EXPECT_EQ(lq->getLocalPort(), reply->getLocalPort());
    EXPECT_EQ(lq->getIface(), reply->getIface());
    EXPECT_EQ(lq->getIndex(), reply->getIndex());

    // Reply should have a client id and it should match
    // the client id in the query.
    ASSERT_TRUE(reply->getClientId());
    EXPECT_EQ(*(lq->getClientId()), *(reply->getClientId()));

    // Reply should have the server-id.
    OptionPtr server_id_opt =  reply->getOption(D6O_SERVERID);
    ASSERT_TRUE(server_id_opt);
    EXPECT_EQ(server_id_->getDuid(), server_id_opt->getData());

    // Verify that a query missing the D6O_CLIENTID option throws.
    // This case really shouldn't happen.
    lq->delOption(D6O_CLIENTID);
    ASSERT_THROW_MSG(reply = impl->initReply(lq), Unexpected,
                     "query has no D6O_CLIENTID");
}

// Verifies that makeClientOption() throws when passed an empty
// lease collection.
TEST_F(MemfileLeaseQueryImpl6ProcessTest, makeClientOptionNoLeases) {
    Lease6Collection leases;

    ASSERT_THROW_MSG(LeaseQueryImpl6::makeClientOption(leases), Unexpected,
                     "makeClientOption: leases list is empty");
}

// Verifies that makeClientOption() creates an D6O_LQ_CLIENT_LINK option
// when passed a lease collection containing active leases from
// multiple subnets.
template <typename TestLeaseMgrType> void
BaseLeaseQueryImpl6ProcessTest<TestLeaseMgrType>::testMakeClientOptionMultipleLinks() {
    Lease6Collection leases;

    // Create some leases.
    Lease6Ptr lease1 = addLease(Lease::TYPE_NA, IOAddress("2001:db8:2::100"),
                                subnet2_, cid1_, time(0));
    Lease6Ptr lease2 = addLease(Lease::TYPE_NA, IOAddress("2001:db8:3::100"),
                                subnet3_, cid1_, time(0));
    Lease6Ptr lease3 = addLease(Lease::TYPE_NA, IOAddress("2001:db8:3::101"),
                                subnet3_, cid1_, time(0));
    Lease6Ptr lease4 = addLease(Lease::TYPE_NA, IOAddress("2001:db8:4::101"),
                                subnet4_, cid1_, time(0));

    // Add them to lease collection.
    leases.push_back(lease2);
    leases.push_back(lease1);
    leases.push_back(lease4);
    leases.push_back(lease3);

    // Invoke makeClientOption.
    OptionPtr opt;
    ASSERT_NO_THROW_LOG(opt = LeaseQueryImpl6::makeClientOption(leases));

    // We should have a non-empty option. That converts to an Option6AddrLst.
    ASSERT_TRUE(opt);
    ASSERT_EQ(D6O_LQ_CLIENT_LINK, opt->getType());
    Option6AddrLstPtr link_opt = boost::dynamic_pointer_cast<Option6AddrLst>(opt);
    ASSERT_TRUE(link_opt);

    // We should have the addresses for subnet2_, subnet3_, and sunbed4_
    // in that order.  Note subnet3_ should only be in the list once
    // despite they're being two of its leases in the collection.
    Option6AddrLst::AddressContainer links = link_opt->getAddresses();
    ASSERT_EQ(3, links.size());
    EXPECT_EQ(subnet2_->get().first, links[0]);
    EXPECT_EQ(subnet3_->get().first, links[1]);
    EXPECT_EQ(subnet4_->get().first, links[2]);
}

TEST_F(MemfileLeaseQueryImpl6ProcessTest, makeClientOptionMultipleLinks) {
    testMakeClientOptionMultipleLinks();
}

// Verifies that makeClientOption() creates an D6O_CLIENT_DATA option
// when passed a lease collection containing multiple active leases on a
// single subnet.
template <typename TestLeaseMgrType> void
BaseLeaseQueryImpl6ProcessTest<TestLeaseMgrType>::testMakeClientOptionSingleLink () {
    Lease6Collection leases;
    time_t now = time(0);

    // Create some leases.
    Lease6Ptr lease1 = addLease(Lease::TYPE_NA, IOAddress("2001:db8:4::100"),
                                subnet4_, cid1_, now - 100);
    Lease6Ptr lease2 = addLease(Lease::TYPE_NA, IOAddress("2001:db8:4::104"),
                                subnet4_, cid1_, now - 200);
    Lease6Ptr lease3 = addLease(Lease::TYPE_NA, IOAddress("2001:db8:4::108"),
                                subnet4_, cid1_, now - 300);
    Lease6Ptr lease4 = addLease(Lease::TYPE_NA, IOAddress("2001:db8:4::10a"),
                                subnet4_, cid1_, now - 400);
    Lease6Ptr lease5 = addLease(Lease::TYPE_PD, IOAddress("3001:0:0:0:1::"),
                                subnet4_, cid1_, now - 500);
    Lease6Ptr lease6 = addLease(Lease::TYPE_PD, IOAddress("3001:0:0:0:2::"),
                                subnet4_, cid1_, now - 600);

    // Add them to lease collection.
    leases.push_back(lease1);
    leases.push_back(lease2);
    leases.push_back(lease3);
    leases.push_back(lease4);
    leases.push_back(lease5);
    leases.push_back(lease6);

    // Invoke makeClientOption.
    OptionPtr opt;
    ASSERT_NO_THROW_LOG(opt = LeaseQueryImpl6::makeClientOption(leases));
    // We should have a non-empty option, that converts to a OptionCustom
    // i.e. D6O_CLIENT_DATA option.
    ASSERT_TRUE(opt);
    ASSERT_EQ(D6O_CLIENT_DATA, opt->getType());
    OptionCustomPtr data_option = boost::dynamic_pointer_cast<OptionCustom>(opt);
    ASSERT_TRUE(data_option);

    auto options = data_option->getOptions();
    EXPECT_EQ(8, options.size());

    // Verify client id.
    auto opt_iter = options.find(D6O_CLIENTID);
    ASSERT_TRUE(opt_iter != options.end());
    opt = opt_iter->second;
    EXPECT_EQ(cid1_->getDuid(), opt->getData());

    // Verify CLTT, is take from the first lease in the list.
    opt_iter = options.find(D6O_CLT_TIME);
    ASSERT_TRUE(opt_iter != options.end());
    OptionUint32Ptr cltt_opt = boost::dynamic_pointer_cast<OptionUint32>
                              (opt_iter->second);
    // CLTT on the initial lease was set to 100 seconds ago, but one second could have ticked since
    // then, so allow one second of margin error.
    EXPECT_NEAR(cltt_opt->getValue(), 100, 1);

    // Now let's check the TYPE_NA leases. We should have a four leases
    // (lease1 through lease4).
    opt_iter = options.find(D6O_IAADDR);
    ASSERT_TRUE(opt_iter != options.end());
    checkIAAddrOption(opt_iter->second, lease1, 100);

    ASSERT_TRUE(++opt_iter != options.end());
    checkIAAddrOption(opt_iter->second, lease2, 200);

    ASSERT_TRUE(++opt_iter != options.end());
    checkIAAddrOption(opt_iter->second, lease3, 300);

    ASSERT_TRUE(++opt_iter != options.end());
    checkIAAddrOption(opt_iter->second, lease4, 400);

    // Now let's check the TYPE_PD leases. We should have two: lease6 then
    // lease5.
    opt_iter = options.find(D6O_IAPREFIX);
    ASSERT_TRUE(opt_iter != options.end());
    checkIAPrefixOption(opt_iter->second, lease5, 500);

    ASSERT_TRUE(++opt_iter != options.end());
    checkIAPrefixOption(opt_iter->second, lease6, 600);
}

TEST_F(MemfileLeaseQueryImpl6ProcessTest, makeClientOptionSingleLink) {
    testMakeClientOptionSingleLink();
}

// Verifies that parseRelayInfo() correctly detects invalid content.
TEST(LeaseQueryImpl6Test, parseRelayInfoInvalid) {
    ElementPtr relay_elem;
    // An empty element will throw.
    ASSERT_THROW_MSG(LeaseQueryImpl6::parseRelayInfo(relay_elem), BadValue,
                     "parseRelayInfo: relay element empty or not a map");

    // An element that is not a map will throw.
    relay_elem = Element::create("foo");
    ASSERT_THROW_MSG(LeaseQueryImpl6::parseRelayInfo(relay_elem), BadValue,
                     "parseRelayInfo: relay element empty or not a map");

    // 'hop' is required.
    relay_elem = Element::createMap();
    ASSERT_THROW_MSG(LeaseQueryImpl6::parseRelayInfo(relay_elem), BadValue,
                     "parseRelayInfo: 'hop' is missing");

    // 'hop' must be an integer.
    relay_elem->set("hop", Element::create("not an int"));
    ASSERT_THROW_MSG(LeaseQueryImpl6::parseRelayInfo(relay_elem), BadValue,
                     "parseRelayInfo: 'hop' is invalid:"
                     " intValue() called on non-integer Element");

    // Add a valid 'hop' entry.
    relay_elem->set("hop", Element::create(1));

    // 'peer' is required.
    ASSERT_THROW_MSG(LeaseQueryImpl6::parseRelayInfo(relay_elem), BadValue,
                     "parseRelayInfo: 'peer' is missing");

    // 'peer' must be an string.
    relay_elem->set("peer", Element::create(777));
    ASSERT_THROW_MSG(LeaseQueryImpl6::parseRelayInfo(relay_elem), BadValue,
                     "parseRelayInfo: 'peer' is invalid:"
                     " stringValue() called on non-string Element");

    // 'peer' must convert to an IP address.
    relay_elem->set("peer", Element::create("boo"));
    ASSERT_THROW_MSG(LeaseQueryImpl6::parseRelayInfo(relay_elem), BadValue,
                     "parseRelayInfo: 'peer' is invalid: Failed to convert string"
                     " to address 'boo': Invalid argument");

    // 'peer' must convert to an IPv6 address.
    relay_elem->set("peer", Element::create("192.1.2.1"));
    ASSERT_THROW_MSG(LeaseQueryImpl6::parseRelayInfo(relay_elem), BadValue,
                     "parseRelayInfo: 'peer' is invalid: not a V6 address: 192.1.2.1");

    // Add a valid 'peer'.
    relay_elem->set("peer", Element::create("2001:db8:1::1"));

    // 'link' is required.
    ASSERT_THROW_MSG(LeaseQueryImpl6::parseRelayInfo(relay_elem), BadValue,
                     "parseRelayInfo: 'link' is missing");

    // 'link' must be an string.
    relay_elem->set("link", Element::create(777));
    ASSERT_THROW_MSG(LeaseQueryImpl6::parseRelayInfo(relay_elem), BadValue,
                     "parseRelayInfo: 'link' is invalid:"
                     " stringValue() called on non-string Element");

    // 'link' must convert to an IP address.
    relay_elem->set("link", Element::create("boo"));
    ASSERT_THROW_MSG(LeaseQueryImpl6::parseRelayInfo(relay_elem), BadValue,
                     "parseRelayInfo: 'link' is invalid: Failed to convert string"
                     " to address 'boo': Invalid argument");

    // 'link' must convert to an IPv6 address.
    relay_elem->set("link", Element::create("192.1.2.1"));
    ASSERT_THROW_MSG(LeaseQueryImpl6::parseRelayInfo(relay_elem), BadValue,
                     "parseRelayInfo: 'link' is invalid: not a V6 address: 192.1.2.1");

    // Add a valid 'link'.
    relay_elem->set("link", Element::create("2001:db8:1::2"));

    // Add an invalid options member.
    relay_elem->set("options", Element::create("not hex digits"));
    ASSERT_THROW_MSG(LeaseQueryImpl6::parseRelayInfo(relay_elem), BadValue,
                     "parseRelayInfo: 'options' is invalid: invalid format"
                     " of the decoded string 'not hex digits'");
}

// Verifies that parseRelayInfo() correctly parses valid content.
TEST(LeaseQueryImpl6Test, parseRelayInfoValid) {
    ElementPtr relay_elem;

    relay_elem = Element::createMap();
    relay_elem->set("hop", Element::create(1));
    relay_elem->set("peer", Element::create("2001:db8:1::1"));
    relay_elem->set("link", Element::create("2001:db8:1::2"));

    // Should parse because "options" is not a required member.
    RelayInfoPtr relay_info;
    ASSERT_NO_THROW_LOG(relay_info = LeaseQueryImpl6::parseRelayInfo(relay_elem));
    ASSERT_TRUE(relay_info);

    // Make sure the field contents are correct.
    ASSERT_EQ(relay_info->hop_count_, 1);
    ASSERT_EQ(relay_info->peeraddr_, IOAddress("2001:db8:1::1"));
    ASSERT_EQ(relay_info->linkaddr_, IOAddress("2001:db8:1::2"));

    // Make sure there are no options.
    ASSERT_EQ(0, relay_info->options_.size());

    // Now Add a valid options member: D6O_INTERFACE_ID = "eth0"
    relay_elem->set("options", Element::create("0x0012000465746830"));
    ASSERT_NO_THROW_LOG(relay_info = LeaseQueryImpl6::parseRelayInfo(relay_elem));
    ASSERT_TRUE(relay_info);

    // Make sure the field contents are correct.
    ASSERT_EQ(relay_info->hop_count_, 1);
    ASSERT_EQ(relay_info->peeraddr_, IOAddress("2001:db8:1::1"));
    ASSERT_EQ(relay_info->linkaddr_, IOAddress("2001:db8:1::2"));

    // Verify that the relay options_ data unpacked correctly.
    // Under the covers this relies on LibDHCP::unpackOptions6()
    // which is tested pretty exhaustively elsewhere, so we're not
    // going to go nuts with testing here.
    ASSERT_EQ(1, relay_info->options_.size());
    auto x = relay_info->options_.find(0x12);
    ASSERT_TRUE(x != relay_info->options_.end());
    ASSERT_EQ(std::string("type=00018, len=00004: 65:74:68:30"), x->second->toString());
}

// Verifies that parseRelayInfoList() detects invalid lists.
TEST(LeaseQueryImpl6Test, parseRelayInfoListInvalid) {
    ElementPtr relay_list;
    std::vector<RelayInfoPtr> relay_infos;

    // Relay list cannot be null.
    ASSERT_THROW_MSG(LeaseQueryImpl6::parseRelayInfoList(relay_list, relay_infos), BadValue,
                     "parseRelayInfoList: relay-info element empty or not a list");

    relay_list = Element::create("not a list");
    ASSERT_THROW_MSG(LeaseQueryImpl6::parseRelayInfoList(relay_list, relay_infos), BadValue,
                     "parseRelayInfoList: relay-info element empty or not a list");
}

// Verifies that parseRelayInfoList() correctly parses a valid list.
TEST(LeaseQueryImpl6Test, parseRelayInfoListValid) {
    ElementPtr relay_list;
    std::vector<RelayInfoPtr> relay_infos;

    // Test input which equates to user-context content.
    std::string relays_json =
    "[ \n"
    "   { \n"
    "      \"hop\": 3, \n"
    "      \"peer\": \"2001:db8:3::1\", \n"
    "      \"link\": \"2001:db8:3::2\" \n"
    "   }, \n"
    "   { \n"
    "      \"hop\": 2, \n"
    "      \"peer\": \"2001:db8:2::1\", \n"
    "      \"link\": \"2001:db8:2::2\", \n"
    "      \"options\": \"0x0012000465746830\" \n"
    "   }, \n"
    "   { \n"
    "      \"hop\": 1, \n"
    "      \"peer\": \"2001:db8:1::1\", \n"
    "      \"link\": \"2001:db8:1::2\", \n"
    "      \"options\": \"0x01ff0003666f6f02ff0003626172\" \n"
    "   } \n"
    "]";

    // Make an Element::list of relays.
    ASSERT_NO_THROW_LOG(relay_list = Element::fromJSON(relays_json));

    // Parse it.
    ASSERT_NO_THROW_LOG(LeaseQueryImpl6::parseRelayInfoList(relay_list, relay_infos));

    // We should have three instances.
    ASSERT_EQ(3, relay_infos.size());

    // Verify the first instance.
    // Make sure the field contents are correct.
    ASSERT_EQ(relay_infos[0]->hop_count_, 3);
    ASSERT_EQ(relay_infos[0]->peeraddr_, IOAddress("2001:db8:3::1"));
    ASSERT_EQ(relay_infos[0]->linkaddr_, IOAddress("2001:db8:3::2"));
    // It should have no options.
    ASSERT_EQ(0, relay_infos[0]->options_.size());

    // Verify the second instance.
    ASSERT_EQ(relay_infos[1]->hop_count_, 2);
    ASSERT_EQ(relay_infos[1]->peeraddr_, IOAddress("2001:db8:2::1"));
    ASSERT_EQ(relay_infos[1]->linkaddr_, IOAddress("2001:db8:2::2"));

    // It should have one option.
    ASSERT_EQ(1, relay_infos[1]->options_.size());
    auto x = relay_infos[1]->options_.find(0x12);
    ASSERT_TRUE(x != relay_infos[1]->options_.end());
    ASSERT_EQ(std::string("type=00018, len=00004: 65:74:68:30"), x->second->toString());

    // Verify the third instance.
    ASSERT_EQ(relay_infos[2]->hop_count_, 1);
    ASSERT_EQ(relay_infos[2]->peeraddr_, IOAddress("2001:db8:1::1"));
    ASSERT_EQ(relay_infos[2]->linkaddr_, IOAddress("2001:db8:1::2"));

    // We should have two options, "foo" and then "bar"
    ASSERT_EQ(2, relay_infos[2]->options_.size());

    // Option "foo"
    x = relay_infos[2]->options_.find(0x1FF);
    ASSERT_TRUE(x != relay_infos[2]->options_.end());
    ASSERT_EQ(std::string("type=00511, len=00003: 66:6f:6f"), x->second->toString());

    // Option "bar"
    x = relay_infos[2]->options_.find(0x2FF);
    ASSERT_TRUE(x != relay_infos[2]->options_.end());
    ASSERT_EQ(std::string("type=00767, len=00003: 62:61:72"), x->second->toString());
}

// Verifies that makeRelayOption() does not return a D6O_RELAY_DATA option
// when given a lease with invalid relay information.
TEST_F(MemfileLeaseQueryImpl6ProcessTest, makeRelayOptionInvalid) {
    time_t now = time(0);

    // Create some leases.
    Lease6Ptr lease = addLease(Lease::TYPE_NA, IOAddress("2001:db8:4::100"),
                                subnet4_, cid1_, now - 400);

    struct Scenario {
        std::string description_;
        std::string context_json_;
    };

    std::vector<Scenario> scenarios {
        {
        "no ISC entry",
        "{ \"foo\": \"bar\" }"
        },
        {
        "no relays element",
        "{ \"ISC\": {  \"magic\": \"stuff\" } }"
        },
        {
        "empty relay list",
        "{ \"ISC\": {  \"relay-info\": []  } }"
        },
        {
        "relay-info not list",
        "{ \"ISC\": {  \"relay-info\": 123  } }"
        },
        {
        "invalid relay",
        "{ \"ISC\": { \"relay-info\": [{ \"hop\": 1 }] } }"
        }
    };

    for (auto const& scenario : scenarios) {
        SCOPED_TRACE(scenario.description_);
        ASSERT_NO_THROW_LOG(lease->setContext(Element::fromJSON(scenario.context_json_)));

        // Invoke makeRelayOption.
        OptionPtr opt;
        ASSERT_NO_THROW_LOG(opt = LeaseQueryImpl6::makeRelayOption(*lease));
        ASSERT_FALSE(opt);
    }
}

// Verifies makeRelayOption() does not return a D6O_RELAY_DATA option
// when given a lease with no relay information.
template <typename TestLeaseMgrType> void
BaseLeaseQueryImpl6ProcessTest<TestLeaseMgrType>::testMakeRelayOptionNoRelayInfo() {
    time_t now = time(0);

    // Create some leases.
    Lease6Ptr lease = addLease(Lease::TYPE_NA, IOAddress("2001:db8:4::100"),
                                subnet4_, cid1_, now - 400);

    // Invoke makeRelayOption.
    OptionPtr opt;
    ASSERT_NO_THROW_LOG(opt = LeaseQueryImpl6::makeRelayOption(*lease));
    ASSERT_FALSE(opt);
}

TEST_F(MemfileLeaseQueryImpl6ProcessTest, makeRelayOptionNoRelayInfo) {
        testMakeRelayOptionNoRelayInfo();
}

// Verifies makeRelayOption() returns a D6O_RELAY_DATA option
// when given a lease with relay information.
template <typename TestLeaseMgrType> void
BaseLeaseQueryImpl6ProcessTest<TestLeaseMgrType>::testMakeRelayOptionRelayInfo() {
    time_t now = time(0);

    // JSON user-context content.
    std::string user_context =
    "{ \"ISC\": {"
    "   \"relay-info\": [ \n"
    "       { \n"
    "           \"hop\": 1, \n"
    "           \"peer\": \"2001:db8:2::1\", \n"
    "           \"link\": \"2001:db8:2::2\", \n"
    "           \"options\": \"0x0012000465746830\" \n"
    "       }, \n"
    "       { \n"
    "           \"hop\": 0, \n"
    "           \"peer\": \"2001:db8:1::1\", \n"
    "           \"link\": \"2001:db8:1::2\" \n"
    "       }] \n"
    "   }"
    "}";

    // Create a lease with user-context.
    Lease6Ptr lease = addLease(Lease::TYPE_NA, IOAddress("2001:db8:4::100"),
                                subnet4_, cid1_, now - 400, user_context);
    // Invoke makeRelayOption.
    OptionPtr opt;
    ASSERT_NO_THROW_LOG(opt = LeaseQueryImpl6::makeRelayOption(*lease));
    ASSERT_TRUE(opt);
    OptionCustomPtr relay_option = boost::dynamic_pointer_cast<OptionCustom>(opt);
    ASSERT_TRUE(relay_option);

    // Verify the peer address field. It should come from the outermost
    // relay.
    IOAddress peer_addr("::");
    ASSERT_NO_THROW_LOG(peer_addr = relay_option->readAddress(0));
    EXPECT_EQ(IOAddress("2001:db8:2::1"), peer_addr);

    // Verify the RELAY_FORW content.
    OptionBuffer relay_forw_data;
    ASSERT_NO_THROW_LOG(relay_forw_data = relay_option->readBinary(1));

    // First we unpack it.
    std::vector<RelayInfoPtr> relay_infos;
    ASSERT_NO_THROW_LOG(unpackRelayForwardMsg(relay_forw_data, relay_infos));

    // We should have two relays.
    EXPECT_EQ(2, relay_infos.size());

    // Verify the first instance.
    // Make sure the field contents are correct.
    ASSERT_EQ(relay_infos[0]->hop_count_, 1);
    ASSERT_EQ(relay_infos[0]->peeraddr_, IOAddress("2001:db8:2::1"));
    ASSERT_EQ(relay_infos[0]->linkaddr_, IOAddress("2001:db8:2::2"));

    // It should have one option.
    ASSERT_EQ(1, relay_infos[0]->options_.size());
    auto x = relay_infos[0]->options_.find(0x12);
    ASSERT_TRUE(x != relay_infos[0]->options_.end());
    ASSERT_EQ(std::string("type=00018, len=00004: 65:74:68:30"), x->second->toString());

    // Verify the second instance.
    ASSERT_EQ(relay_infos[1]->hop_count_, 0);
    ASSERT_EQ(relay_infos[1]->peeraddr_, IOAddress("2001:db8:1::1"));
    ASSERT_EQ(relay_infos[1]->linkaddr_, IOAddress("2001:db8:1::2"));

    // It should have no options.
    ASSERT_EQ(0, relay_infos[1]->options_.size());
}

TEST_F(MemfileLeaseQueryImpl6ProcessTest, makeRelayOptionRelayInfo) {
    testMakeRelayOptionRelayInfo();
}

// Same with the old entry name.
template <typename TestLeaseMgrType> void
BaseLeaseQueryImpl6ProcessTest<TestLeaseMgrType>::testOldMakeRelayOptionRelayInfo() {
    time_t now = time(0);

    // JSON user-context content.
    std::string user_context =
    "{ \"ISC\": {"
    "   \"relays\": [ \n"
    "       { \n"
    "           \"hop\": 1, \n"
    "           \"peer\": \"2001:db8:2::1\", \n"
    "           \"link\": \"2001:db8:2::2\", \n"
    "           \"options\": \"0x0012000465746830\" \n"
    "       }, \n"
    "       { \n"
    "           \"hop\": 0, \n"
    "           \"peer\": \"2001:db8:1::1\", \n"
    "           \"link\": \"2001:db8:1::2\" \n"
    "       }] \n"
    "   }"
    "}";

    // Create a lease with user-context.
    Lease6Ptr lease = addLease(Lease::TYPE_NA, IOAddress("2001:db8:4::100"),
                                subnet4_, cid1_, now - 400, user_context);
    // Invoke makeRelayOption.
    OptionPtr opt;
    ASSERT_NO_THROW_LOG(opt = LeaseQueryImpl6::makeRelayOption(*lease));
    ASSERT_TRUE(opt);
    OptionCustomPtr relay_option = boost::dynamic_pointer_cast<OptionCustom>(opt);
    ASSERT_TRUE(relay_option);

    // Verify the peer address field. It should come from the outermost
    // relay.
    IOAddress peer_addr("::");
    ASSERT_NO_THROW_LOG(peer_addr = relay_option->readAddress(0));
    EXPECT_EQ(IOAddress("2001:db8:2::1"), peer_addr);

    // Verify the RELAY_FORW content.
    OptionBuffer relay_forw_data;
    ASSERT_NO_THROW_LOG(relay_forw_data = relay_option->readBinary(1));

    // First we unpack it.
    std::vector<RelayInfoPtr> relay_infos;
    ASSERT_NO_THROW_LOG(unpackRelayForwardMsg(relay_forw_data, relay_infos));

    // We should have two relays.
    EXPECT_EQ(2, relay_infos.size());

    // Verify the first instance.
    // Make sure the field contents are correct.
    ASSERT_EQ(relay_infos[0]->hop_count_, 1);
    ASSERT_EQ(relay_infos[0]->peeraddr_, IOAddress("2001:db8:2::1"));
    ASSERT_EQ(relay_infos[0]->linkaddr_, IOAddress("2001:db8:2::2"));

    // It should have one option.
    ASSERT_EQ(1, relay_infos[0]->options_.size());
    auto x = relay_infos[0]->options_.find(0x12);
    ASSERT_TRUE(x != relay_infos[0]->options_.end());
    ASSERT_EQ(std::string("type=00018, len=00004: 65:74:68:30"), x->second->toString());

    // Verify the second instance.
    ASSERT_EQ(relay_infos[1]->hop_count_, 0);
    ASSERT_EQ(relay_infos[1]->peeraddr_, IOAddress("2001:db8:1::1"));
    ASSERT_EQ(relay_infos[1]->linkaddr_, IOAddress("2001:db8:1::2"));

    // It should have no options.
    ASSERT_EQ(0, relay_infos[1]->options_.size());
}

TEST_F(MemfileLeaseQueryImpl6ProcessTest, oldMakeRelayOptionRelayInfo) {
    testOldMakeRelayOptionRelayInfo();
}

// This test verifies scenarios were the query is logically
// invalid and to which the server responds with a DHCPV6_LEASEQUERY_REPLY
// packet but with an other than successful status code option:
// - Unknown query type
// - query by ip address that's missing the D6O_IAADDR option
// - query by client id that's missing the D6O_CLIENTID option
TEST_F(MemfileLeaseQueryImpl6ProcessTest, processQueryInvalidWithStatus) {
    LeaseQueryImpl6Ptr impl;
    ASSERT_NO_THROW_LOG(impl = createLeaseQueryImpl6());

    // Structure that contains a test scenario.
    struct Scenario {
        const std::string description_;
        uint8_t qry_type_;
        IOAddress qry_iaaddr_;
        DuidPtr qry_cid_;
        DHCPv6StatusCode exp_status_code_;
        const std::string exp_message_;
    };

    // List of test scenarios to execute.
    std::vector<Scenario> scenarios {
        {
        "unknown query type",
        77,
        IOAddress::IPV6_ZERO_ADDRESS(),
        DuidPtr(),
        STATUS_UnknownQueryType,
        "unknown query-type"
        },
        {
        "missing iaaddr",
        LQ6QT_BY_ADDRESS,
        IOAddress::IPV6_ZERO_ADDRESS(),
        DuidPtr(),
        STATUS_MalformedQuery,
        "missing D6O_IAADDR"
        },
        {
        "missing client id",
        LQ6QT_BY_CLIENTID,
        IOAddress::IPV6_ZERO_ADDRESS(),
        DuidPtr(),
        STATUS_MalformedQuery,
        "missing D6O_CLIENTID"
        }
    };

    // Iterate over the scenarios.
    for (auto const& scenario : scenarios) {
        SCOPED_TRACE(scenario.description_);

        // Make the lease query.
        Pkt6Ptr lq = makeLeaseQuery(scenario.qry_type_, scenario.qry_iaaddr_,
                                    scenario.qry_cid_);
        // Process the query.
        ASSERT_NO_THROW_LOG(impl->processQuery(lq));

        // We should have generated a LEASE_QUERY_REPLY with a
        // status option containing the expected status.
        Pkt6Ptr rsp = getSentPacket();
        ASSERT_TRUE(rsp);
        ASSERT_EQ(DHCPV6_LEASEQUERY_REPLY, rsp->getType());
        checkStatus(rsp, scenario.exp_status_code_, scenario.exp_message_);

        // Should not have any options.
        EXPECT_FALSE(rsp->getOption(D6O_LQ_CLIENT_LINK));
        EXPECT_FALSE(rsp->getOption(D6O_CLIENT_DATA));
        EXPECT_FALSE(rsp->getOption(D6O_LQ_RELAY_DATA));

        // Make sure there are no left over packets.
        ASSERT_FALSE(getSentPacket());
    }
}

// This test verifies scenarios were a query by ip address is logically
// valid but for which no active leases exist and to which the server
// should respond with a DHCPV6_LEASEQUERY_REPLY packet with the
// appropriate status.
// - Lease is expired
// - Lease is declined
// - Lease is reclaimed
// - Lease address is known but no lease exists
// - Lease address is not known
template <typename TestLeaseMgrType> void
BaseLeaseQueryImpl6ProcessTest<TestLeaseMgrType>::testQueryByIpAddressNoActiveLease() {
    // Create our impl instance.
    LeaseQueryImpl6Ptr impl;
    ASSERT_NO_THROW_LOG(impl = createLeaseQueryImpl6());

    // Create some leases.
    Lease6Ptr active_lease = addLease(Lease::TYPE_NA, IOAddress("2001:db8:2::120"),
                                      subnet2_, cid1_, time(0));

    Lease6Ptr expired_lease = addLease(Lease::TYPE_NA, IOAddress("2001:db8:2::115"),
                                       subnet2_, cid1_, time(0) - 4000, "");

    Lease6Ptr declined_lease = addLease(Lease::TYPE_NA, IOAddress("2001:db8:2::110"),
                                        subnet2_, cid1_, time(0), "",
                                        Lease::STATE_DECLINED);

    Lease6Ptr reclaimed_lease = addLease(Lease::TYPE_NA, IOAddress("2001:db8:2::105"),
                                         subnet2_, cid1_, time(0), "",
                                         Lease::STATE_EXPIRED_RECLAIMED);

    // Structure that contains a test scenario.
    struct Scenario {
        const std::string description_;
        IOAddress qry_iaaddr_;
        DHCPv6StatusCode exp_status_code_;
        const std::string exp_message_;
    };

    // List of test scenarios to execute.
    std::vector<Scenario> scenarios {
        {
        "lease is expired",
        expired_lease->addr_,
        STATUS_Success,
        "inactive lease exists"
        },
        {
        "lease is declined",
        declined_lease->addr_,
        STATUS_Success,
        "inactive lease exists"
        },
        {
        "lease is reclaimed",
        reclaimed_lease->addr_,
        STATUS_Success,
        "inactive lease exists"
        },
        {
        "lease does not exist, but is in a pool",
        IOAddress("2001:db8:2::200"),
        STATUS_Success,
        "no active lease"
        },
        {
        "address is not in a configured pool",
        IOAddress("2001:db8:77::1"),
        STATUS_NotConfigured,
        "address not in a configured pool"
        }
    };

    // Iterate over the scenarios.
    for (auto const& scenario : scenarios) {
        SCOPED_TRACE(scenario.description_);

        // Make the lease query.
        Pkt6Ptr lq = makeQueryByIpAddress(scenario.qry_iaaddr_);

        // Process the query.
        ASSERT_NO_THROW_LOG(impl->processQuery(lq));

        // We should have generated a DHCPV6_LEASEQUERY_REPLY with a
        // status option containing the expected status.
        Pkt6Ptr rsp = getSentPacket();
        ASSERT_TRUE(rsp);
        ASSERT_EQ(DHCPV6_LEASEQUERY_REPLY, rsp->getType());
        checkStatus(rsp, scenario.exp_status_code_, scenario.exp_message_);

        // Should not have any options.
        EXPECT_FALSE(rsp->getOption(D6O_LQ_CLIENT_LINK));
        EXPECT_FALSE(rsp->getOption(D6O_CLIENT_DATA));
        EXPECT_FALSE(rsp->getOption(D6O_LQ_RELAY_DATA));

        // Make sure there are no left over packets.
        ASSERT_FALSE(getSentPacket());
    }
}

TEST_F(MemfileLeaseQueryImpl6ProcessTest, queryByIpAddressNoActiveLease) {
    testQueryByIpAddressNoActiveLease();
}

// This test verifies that given a logically valid query by ip address
// for which an active lease exists the server responds with a
// DHCPV6_LEASEQUERY_REPLY packet with the proper content.
template <typename TestLeaseMgrType> void
BaseLeaseQueryImpl6ProcessTest<TestLeaseMgrType>::testQueryByIpAddressActiveLease() {
    // Create our impl instance.
    LeaseQueryImpl6Ptr impl;
    ASSERT_NO_THROW_LOG(impl = createLeaseQueryImpl6());
    time_t now = time(0);

    // Create some leases.
    Lease6Ptr active_lease = addLease(Lease::TYPE_NA, IOAddress("2001:db8:2::120"),
                                      subnet2_, cid1_, now - 100);

    Lease6Ptr expired_lease = addLease(Lease::TYPE_NA, IOAddress("2001:db8:2::115"),
                                       subnet2_, cid1_, now - 4000, "");

    Lease6Ptr declined_lease = addLease(Lease::TYPE_NA, IOAddress("2001:db8:2::110"),
                                        subnet2_, cid1_, now, "",
                                        Lease::STATE_DECLINED);

    Lease6Ptr reclaimed_lease = addLease(Lease::TYPE_NA, IOAddress("2001:db8:2::105"),
                                         subnet2_, cid1_, now, "",
                                         Lease::STATE_EXPIRED_RECLAIMED);

    // Make the lease query.
    Pkt6Ptr lq = makeQueryByIpAddress(active_lease->addr_);

    // Process the query.
    ASSERT_NO_THROW_LOG(impl->processQuery(lq));

    // We should have generated a DHCPV6_LEASEQUERY_REPLY with a
    // status option containing the successful status.
    Pkt6Ptr rsp = getSentPacket();
    ASSERT_TRUE(rsp);
    ASSERT_EQ(DHCPV6_LEASEQUERY_REPLY, rsp->getType());
    checkStatus(rsp, STATUS_Success, "active lease found");

    // Should not have either of these two options.
    EXPECT_FALSE(rsp->getOption(D6O_LQ_CLIENT_LINK));
    EXPECT_FALSE(rsp->getOption(D6O_LQ_RELAY_DATA));

    // We should have D6O_CLIENT_DATA option.
    OptionPtr opt;
    ASSERT_TRUE(opt = rsp->getOption(D6O_CLIENT_DATA));
    OptionCustomPtr data_option = boost::dynamic_pointer_cast<OptionCustom>(opt);
    ASSERT_TRUE(data_option);

    auto options = data_option->getOptions();
    EXPECT_EQ(3, options.size());

    // Verify client id.
    auto opt_iter = options.find(D6O_CLIENTID);
    ASSERT_TRUE(opt_iter != options.end());
    opt = opt_iter->second;
    EXPECT_EQ(cid1_->getDuid(), opt->getData());

    // Verify CLTT, is take from the first lease in the list.
    opt_iter = options.find(D6O_CLT_TIME);
    ASSERT_TRUE(opt_iter != options.end());
    OptionUint32Ptr cltt_opt = boost::dynamic_pointer_cast<OptionUint32>
                              (opt_iter->second);
    EXPECT_NEAR(cltt_opt->getValue(), 100, 1);

    // Now let's check w should have one TYPE_NA lease.
    opt_iter = options.find(D6O_IAADDR);
    ASSERT_TRUE(opt_iter != options.end());

    Option6IAAddrPtr iaaddr_opt;
    iaaddr_opt = boost::dynamic_pointer_cast<Option6IAAddr>(opt_iter->second);
    ASSERT_TRUE(iaaddr_opt);
    EXPECT_EQ(active_lease->addr_, iaaddr_opt->getAddress());

    // Should not be any more options.
    ASSERT_TRUE((++opt_iter) != options.end());

    // Make sure there are no left over packets.
    ASSERT_FALSE(getSentPacket());
}

TEST_F(MemfileLeaseQueryImpl6ProcessTest, queryByIpAddressActiveLease) {
    testQueryByIpAddressActiveLease();
}

// This test verifies that given a logically valid query by client id
// for which no active leases exist the server responds with a
// DHCPV6_LEASEQUERY_REPLY packet with the proper content.
template <typename TestLeaseMgrType> void
BaseLeaseQueryImpl6ProcessTest<TestLeaseMgrType>::testQueryByClientIdNoActiveLease() {
    // Create our impl instance.
    LeaseQueryImpl6Ptr impl;
    ASSERT_NO_THROW_LOG(impl = createLeaseQueryImpl6());

    // Create some leases.
    Lease6Ptr active_lease = addLease(Lease::TYPE_NA, IOAddress("2001:db8:2::100"),
                                      subnet2_, cid2_, time(0));

    Lease6Ptr expired_lease = addLease(Lease::TYPE_NA, IOAddress("2001:db8:3::100"),
                                       subnet3_, cid1_, time(0) - 4000, "");

    Lease6Ptr declined_lease = addLease(Lease::TYPE_NA, IOAddress("2001:db8:4::100"),
                                        subnet4_, cid1_, time(0), "",
                                        Lease::STATE_DECLINED);

    Lease6Ptr reclaimed_lease = addLease(Lease::TYPE_NA, IOAddress("2001:db8:2::101"),
                                         subnet2_, cid1_, time(0), "",
                                         Lease::STATE_EXPIRED_RECLAIMED);

    // Make the lease query.
    Pkt6Ptr lq = makeQueryByClientId(cid1_);

    // Process the query.
    ASSERT_NO_THROW_LOG(impl->processQuery(lq));

    // We should have generated a LEASE_QUERY_REPLY with a
    // status option containing the expected status.
    Pkt6Ptr rsp = getSentPacket();
    ASSERT_TRUE(rsp);
    ASSERT_EQ(DHCPV6_LEASEQUERY_REPLY, rsp->getType());
    checkStatus(rsp, STATUS_Success, "no active leases");

    // Should not have any options.
    EXPECT_FALSE(rsp->getOption(D6O_LQ_CLIENT_LINK));
    EXPECT_FALSE(rsp->getOption(D6O_CLIENT_DATA));
    EXPECT_FALSE(rsp->getOption(D6O_LQ_RELAY_DATA));

    // Make sure there are no left over packets.
    ASSERT_FALSE(getSentPacket());
}

TEST_F(MemfileLeaseQueryImpl6ProcessTest, queryByClientIdNoActiveLease) {
    testQueryByClientIdNoActiveLease();
}

// This test verifies that given a logically valid query by client id
// for which active leases on multiple subnets exist, the server responds
// with a DHCPV6_LEASEQUERY_REPLY packet with the proper content.
template <typename TestLeaseMgrType> void
BaseLeaseQueryImpl6ProcessTest<TestLeaseMgrType>::testQueryByClientIdMultipleLinks() {
    // Create our impl instance.
    LeaseQueryImpl6Ptr impl;
    ASSERT_NO_THROW_LOG(impl = createLeaseQueryImpl6());
    time_t now = time(0);

    // Create some leases.
    Lease6Ptr lease1 = addLease(Lease::TYPE_NA, IOAddress("2001:db8:2::100"),
                                      subnet2_, cid1_, now - 300);

    Lease6Ptr lease2 = addLease(Lease::TYPE_NA, IOAddress("2001:db8:3::100"),
                                       subnet3_, cid1_, now - 100);

    Lease6Ptr lease3 = addLease(Lease::TYPE_NA, IOAddress("2001:db8:4::100"),
                                        subnet4_, cid1_, now - 200);

    // Make the lease query.
    Pkt6Ptr lq = makeQueryByClientId(cid1_);

    // Process the query.
    ASSERT_NO_THROW_LOG(impl->processQuery(lq));

    // We should have generated a LEASE_QUERY_REPLY with a
    // status option containing the expected status.
    Pkt6Ptr rsp = getSentPacket();
    ASSERT_TRUE(rsp);
    ASSERT_EQ(DHCPV6_LEASEQUERY_REPLY, rsp->getType());
    checkStatus(rsp, STATUS_Success, "active lease(s) found");

    // Should not these two any options.
    EXPECT_FALSE(rsp->getOption(D6O_CLIENT_DATA));
    EXPECT_FALSE(rsp->getOption(D6O_LQ_RELAY_DATA));

    // Should have client link option.
    OptionPtr opt;
    ASSERT_TRUE(opt = rsp->getOption(D6O_LQ_CLIENT_LINK));
    Option6AddrLstPtr link_opt = boost::dynamic_pointer_cast<Option6AddrLst>(opt);
    ASSERT_TRUE(link_opt);

    // We should have the addresses for subnet2_, subnet3_, and subnet4_
    // in that order.
    Option6AddrLst::AddressContainer links = link_opt->getAddresses();
    ASSERT_EQ(3, links.size());
    EXPECT_EQ(subnet2_->get().first, links[0]);
    EXPECT_EQ(subnet3_->get().first, links[1]);
    EXPECT_EQ(subnet4_->get().first, links[2]);

    // Make sure there are no left over packets.
    ASSERT_FALSE(getSentPacket());
}

TEST_F(MemfileLeaseQueryImpl6ProcessTest, queryByClientIdMultipleLinks) {
    testQueryByClientIdMultipleLinks();
}

// This test verifies that given a logically valid query by client id
// for which active leases on the same subnet exist, the server responds
// with a DHCPV6_LEASEQUERY_REPLY packet with the proper content, including
// a D6O_LQ_RELAY_DATA option from the newest active lease.
template <typename TestLeaseMgrType> void
BaseLeaseQueryImpl6ProcessTest<TestLeaseMgrType>::testQueryByClientIdActiveLeases() {
    // Create our impl instance.
    LeaseQueryImpl6Ptr impl;
    ASSERT_NO_THROW_LOG(impl = createLeaseQueryImpl6());
    time_t now = time(0);

    // JSON user-context content.
    std::string user_context =
    "{ \"ISC\": {"
    "   \"relay-info\": ["
    "       {"
    "           \"hop\": 1,"
    "           \"peer\": \"2001:db8:2::1\","
    "           \"link\": \"2001:db8:2::2\","
    "           \"options\": \"0x0012000465746830\""
    "       }]"
    "   }"
    "}";

    // Create some leases.
    Lease6Ptr lease1 = addLease(Lease::TYPE_NA, IOAddress("2001:db8:4::100"),
                                subnet4_, cid1_, now - 4000);
    Lease6Ptr lease2 = addLease(Lease::TYPE_NA, IOAddress("2001:db8:4::104"),
                                subnet4_, cid1_, now - 100, user_context);
    Lease6Ptr lease3 = addLease(Lease::TYPE_PD, IOAddress("3001:0:0:0:1::"),
                                subnet4_, cid1_, now - 200);
    Lease6Ptr lease4 = addLease(Lease::TYPE_PD, IOAddress("3001:0:0:0:2::"),
                                subnet4_, cid1_, now - 4000);

    // Make the lease query.
    Pkt6Ptr lq = makeQueryByClientId(cid1_);

    // Process the query.
    ASSERT_NO_THROW_LOG(impl->processQuery(lq));

    // We should have generated a LEASE_QUERY_REPLY with a
    // status option containing the expected status.
    Pkt6Ptr rsp = getSentPacket();
    ASSERT_TRUE(rsp);
    ASSERT_EQ(DHCPV6_LEASEQUERY_REPLY, rsp->getType());
    checkStatus(rsp, STATUS_Success, "active lease(s) found");

    // Should not have a clink link option.
    EXPECT_FALSE(rsp->getOption(D6O_LQ_CLIENT_LINK));

    // Should have client data option.
    OptionPtr opt;
    ASSERT_TRUE(opt = rsp->getOption(D6O_CLIENT_DATA));
    OptionCustomPtr data_option = boost::dynamic_pointer_cast<OptionCustom>(opt);
    ASSERT_TRUE(data_option);

    // Client data option should have four sub options.
    auto options = data_option->getOptions();
    EXPECT_EQ(4, options.size());

    // Verify client id.
    auto opt_iter = options.find(D6O_CLIENTID);
    ASSERT_TRUE(opt_iter != options.end());
    opt = opt_iter->second;
    EXPECT_EQ(cid1_->getDuid(), opt->getData());

    // Verify that CLTT comes from lease2.
    opt_iter = options.find(D6O_CLT_TIME);
    ASSERT_TRUE(opt_iter != options.end());
    OptionUint32Ptr cltt_opt = boost::dynamic_pointer_cast<OptionUint32>
                              (opt_iter->second);
    // CLTT on the initial lease was set to 100 seconds ago, but one second could have ticked since
    // then, so allow one second of margin error.
    EXPECT_NEAR(cltt_opt->getValue(), 100, 1);

    // Now let's check we should have one TYPE_NA lease.
    opt_iter = options.find(D6O_IAADDR);
    ASSERT_TRUE(opt_iter != options.end());
    checkIAAddrOption(opt_iter->second, lease2, 100);

    // Now let's check we should have one TYPE_PD lease.
    opt_iter = options.find(D6O_IAPREFIX);
    ASSERT_TRUE(opt_iter != options.end());
    checkIAPrefixOption(opt_iter->second, lease3, 200);

    // We should have a relay option.
    opt = rsp->getOption(D6O_LQ_RELAY_DATA);
    ASSERT_TRUE(opt);
    OptionCustomPtr relay_option = boost::dynamic_pointer_cast<OptionCustom>(opt);
    ASSERT_TRUE(relay_option);

    // Verify the peer address field. It should come from the outermost
    // relay.
    IOAddress peer_addr("::");
    ASSERT_NO_THROW_LOG(peer_addr = relay_option->readAddress(0));
    EXPECT_EQ(IOAddress("2001:db8:2::1"), peer_addr);

    // Verify the RELAY_FORW content.
    OptionBuffer relay_forw_data;
    ASSERT_NO_THROW_LOG(relay_forw_data = relay_option->readBinary(1));

    // First we unpack it.
    std::vector<RelayInfoPtr> relay_infos;
    ASSERT_NO_THROW_LOG(unpackRelayForwardMsg(relay_forw_data, relay_infos));

    // We should have two relays.
    EXPECT_EQ(1, relay_infos.size());

    // Verify the first instance.
    // Make sure the field contents are correct.
    ASSERT_EQ(relay_infos[0]->hop_count_, 1);
    ASSERT_EQ(relay_infos[0]->peeraddr_, IOAddress("2001:db8:2::1"));
    ASSERT_EQ(relay_infos[0]->linkaddr_, IOAddress("2001:db8:2::2"));

    // It should have one option.
    ASSERT_EQ(1, relay_infos[0]->options_.size());
    auto x = relay_infos[0]->options_.find(0x12);
    ASSERT_TRUE(x != relay_infos[0]->options_.end());
    ASSERT_EQ(std::string("type=00018, len=00004: 65:74:68:30"), x->second->toString());

    // Make sure there are no left over packets.
    ASSERT_FALSE(getSentPacket());
}

TEST_F(MemfileLeaseQueryImpl6ProcessTest, queryByClientIdActiveLeases) {
    testQueryByClientIdActiveLeases();
}

// Verifies that queryByClientId() correctly applies the link address
// value when matching leases.
// - Detect unknown link address
// - No filtering when the link address is ::
// - Include only a client's leases whose subnet matches the link addr (three scenarios)
template <typename TestLeaseMgrType> void
BaseLeaseQueryImpl6ProcessTest<TestLeaseMgrType>::testQueryByClientIdLinkAddr() {
    // Create our impl instance.
    LeaseQueryImpl6Ptr impl;
    ASSERT_NO_THROW_LOG(impl = createLeaseQueryImpl6());
    time_t now = time(0);

    // Create some leases.
    Lease6Ptr lease1 = addLease(Lease::TYPE_NA, IOAddress("2001:db8:2::100"),
                                       subnet2_, cid1_, now - 300);

    Lease6Ptr lease2 = addLease(Lease::TYPE_NA, IOAddress("2001:db8:2::101"),
                                       subnet2_, cid1_, now - 100);

    Lease6Ptr lease3 = addLease(Lease::TYPE_NA, IOAddress("2001:db8:3::100"),
                                       subnet3_, cid2_, now - 100);

    Lease6Ptr lease4 = addLease(Lease::TYPE_NA, IOAddress("2001:db8:4::100"),
                                       subnet4_, cid1_, now - 200);

    Lease6Collection leases;
    Option6StatusCodePtr status;

    // With an unknown link address we should not configured status and no leases.
    leases.clear();
    ASSERT_NO_THROW_LOG(status = impl->queryByClientId(cid1_, IOAddress("3001::"), leases));
    checkStatus(status, STATUS_NotConfigured, "not a configured link");
    ASSERT_EQ(0, leases.size());

    // With no link address filter. We should get all three cid1_ leases
    // ordered by CLTT : lease2, lease4, lease1
    ASSERT_NO_THROW_LOG(status = impl->queryByClientId(cid1_, IOAddress("::"), leases));
    checkStatus(status, STATUS_Success, "active lease(s) found");
    ASSERT_EQ(3, leases.size());
    EXPECT_EQ(lease2->addr_, leases[0]->addr_);
    EXPECT_EQ(lease4->addr_, leases[1]->addr_);
    EXPECT_EQ(lease1->addr_, leases[2]->addr_);

    // With link address = subnet4. We should only get lease4 for cid1_
    leases.clear();
    ASSERT_NO_THROW_LOG(status = impl->queryByClientId(cid1_, IOAddress("2001:db8:4::"), leases));
    checkStatus(status, STATUS_Success, "active lease(s) found");
    ASSERT_EQ(1, leases.size());
    EXPECT_EQ(lease4->addr_, leases[0]->addr_);

    // With link address filter = subnet3, we should no active lease for cid1_.
    leases.clear();
    ASSERT_NO_THROW_LOG(status = impl->queryByClientId(cid1_, IOAddress("2001:db8:3::"), leases));
    ASSERT_EQ(0, leases.size());
    checkStatus(status, STATUS_Success, "no active leases");

    // With link address filter = subnet3, we should get one active lease for cid2_.
    leases.clear();
    ASSERT_NO_THROW_LOG(status = impl->queryByClientId(cid2_, IOAddress("2001:db8:3::"), leases));
    checkStatus(status, STATUS_Success, "active lease(s) found");
    ASSERT_EQ(1, leases.size());
    EXPECT_EQ(lease3->addr_, leases[0]->addr_);
}

// This test exercises LeaseQueryImpl6::getPrefixFromAddress().
TEST_F(MemfileLeaseQueryImpl6ProcessTest, getPrefixFromAddress) {
    IOAddress src_addr("8001:db8:0134:dead:beef::0567");
    IOAddress prefix_addr("::");
    ASSERT_NO_THROW(prefix_addr = LeaseQueryImpl6::getPrefixFromAddress(src_addr, 64));
    EXPECT_EQ(IOAddress("8001:db8:0134:dead::"), prefix_addr);

    ASSERT_NO_THROW(prefix_addr = LeaseQueryImpl6::getPrefixFromAddress(src_addr, 70));
    EXPECT_EQ(IOAddress("8001:db8:0134:dead:bc00::"), prefix_addr);

    ASSERT_NO_THROW(prefix_addr = LeaseQueryImpl6::getPrefixFromAddress(src_addr, 128));
    EXPECT_EQ(IOAddress("8001:db8:0134:dead:beef::0567"), prefix_addr);

    prefix_addr = LeaseQueryImpl6::getPrefixFromAddress(src_addr, 1);
    EXPECT_EQ(IOAddress("8000::"), prefix_addr);

    ASSERT_THROW (LeaseQueryImpl6::getPrefixFromAddress(src_addr, 129), BadValue);
    ASSERT_THROW (LeaseQueryImpl6::getPrefixFromAddress(src_addr, 0), BadValue);
}

TEST_F(MemfileLeaseQueryImpl6ProcessTest, queryByClientIdLinkAddr) {
    testQueryByClientIdLinkAddr();
}

// This test verifies searching for PD leases associated with a
// IP address and permutations of a list of prefix lengths to search.
template <typename TestLeaseMgrType> void
BaseLeaseQueryImpl6ProcessTest<TestLeaseMgrType>::testQueryByIpaddressPDLeases() {
    // Create our impl instance.
    LeaseQueryImpl6Ptr impl;
    ASSERT_NO_THROW_LOG(impl = createLeaseQueryImpl6());
    time_t now = time(0);

    // Create some PD leases.
    Lease6Ptr lease1 = addLease(Lease::TYPE_PD, IOAddress("3001:aabb:ccdd:eeff:1000::"),
                                subnet4_, cid1_, now - 200);
    Lease6Ptr lease2 = addLease(Lease::TYPE_PD, IOAddress("3001:aabb:ccdd:eeff:1f00::"),
                                subnet4_, cid1_, now - 200);
    Lease6Ptr lease3 = addLease(Lease::TYPE_PD, IOAddress("3001:aabb:ccdd:eeff:1d00::"),
                                subnet4_, cid1_, now - 4000);

    // Structure that contains a test scenario.
    struct Scenario {
        std::string desc_;
        IOAddress query_address_;
        std::list<uint8_t> prefix_lengths_;
        DHCPv6StatusCode expected_status_;
        std::string expected_msg_;
        Lease6Ptr expected_lease_;
    };

    // List of scenarios to test.
    std::list<Scenario> scenarios {
        {
            "No prefix lengths, no match",
            IOAddress("3001:aabb:ccdd:eeff:1f00::1"),
            {},
            STATUS_NotConfigured,
            "address not in a configured pool",
            Lease6Ptr()
        },
        {
            "Prefix too short to match",
            IOAddress("3001:aabb:ccdd:eeff:1f00::1"),
            {64},
            STATUS_NotConfigured,
            "address not in a configured pool",
            Lease6Ptr()
        },
        {
            "Should match active lease2",
            IOAddress("3001:aabb:ccdd:eeff:1f00::1"),
            {64, 72},
            STATUS_Success,
            "active lease found",
            lease2
        },
        {
            "Should be within PD pool 4, and return no active leases",
            IOAddress("3001::"),
            {64, 72},
            STATUS_Success,
            "no active lease",
            Lease6Ptr()
        },
        {
            "Should be outside all pools, and return not configured",
            IOAddress("3001:dead:beef::"),
            {64, 72},
            STATUS_NotConfigured,
            "address not in a configured pool",
            Lease6Ptr()
        }
    };

    for (auto const& scenario : scenarios) {
        SCOPED_TRACE(scenario.desc_);
        // Make the lease query.
        Pkt6Ptr lq = makeQueryByIpAddress(scenario.query_address_);

        impl->clearPrefixLengthList();
        for (auto const& plen : scenario.prefix_lengths_) {
            impl->addPrefixLength(plen);
        }

        // Process the query.
        ASSERT_NO_THROW_LOG(impl->processQuery(lq));

        // We should have generated a LEASE_QUERY_REPLY with a
        // status option containing the expected status.
        Pkt6Ptr rsp = getSentPacket();
        ASSERT_TRUE(rsp);
        ASSERT_EQ(DHCPV6_LEASEQUERY_REPLY, rsp->getType());
        checkStatus(rsp, scenario.expected_status_, scenario.expected_msg_);

        if (!scenario.expected_lease_) {
            // Should not have any options.
            EXPECT_FALSE(rsp->getOption(D6O_LQ_CLIENT_LINK));
            EXPECT_FALSE(rsp->getOption(D6O_CLIENT_DATA));
            EXPECT_FALSE(rsp->getOption(D6O_LQ_RELAY_DATA));
        } else {
            // We should have D6O_CLIENT_DATA option.
            OptionPtr opt;
            ASSERT_TRUE(opt = rsp->getOption(D6O_CLIENT_DATA));
            OptionCustomPtr data_option = boost::dynamic_pointer_cast<OptionCustom>(opt);
            ASSERT_TRUE(data_option);

            auto options = data_option->getOptions();
            EXPECT_EQ(3, options.size());

            // Now let's check we should have one TYPE_PD lease.
            auto opt_iter = options.find(D6O_IAPREFIX);
            ASSERT_TRUE(opt_iter != options.end());
            checkIAPrefixOption(opt_iter->second, scenario.expected_lease_);
        }
    }
}

TEST_F(MemfileLeaseQueryImpl6ProcessTest, queryByIpaddressPDLeases) {
    testQueryByIpaddressPDLeases();
}

// Verifies prefix length list population over valid configuration
// permutations.
TEST_F(MemfileLeaseQueryImpl6ProcessTest, populatePrefixLengthList) {

    // Struct descibing each test scenario.
    struct Scenario {
        std::string desc_;            // text description
        std::string cfg_prefixes_;    // RHV for "prefix-length-list" if not empty
        size_t expected_start_size_;  // number of entries after LeaseQueryImpl6 is configured
        size_t expected_end_size_;    // number of entries after call to populatePrefixLenghtList()
        std::string expected_list_;   // expected contents of the prefix length list
    };

    // List of scenarios.
    std::list<Scenario> scenarios {
        {
            "No config entry, populate should build it",
            "",
            0, 2,
            "[ 72, 64 ]"
        },
        {
            "Empty list, should stay empty",
            "[ ]",
            0, 0,
            "[ ]"
        },
        {
            "Non-empty list, should remain intact",
            "[ 96 ]",
            1, 1,
            "[ 96 ]"
        }
    };

    // Iterate over the scenarios.
    for (auto const& scenario : scenarios) {
        // Create the impl.
        LeaseQueryImpl6Ptr impl;
        ASSERT_NO_THROW_LOG(impl = createLeaseQueryImpl6(scenario.cfg_prefixes_));

        // Verify the list after configuration.
        auto prefix_lengths = impl->getPrefixLengthList();
        ASSERT_EQ(scenario.expected_start_size_, prefix_lengths.size());

        // Verify the list after call to populatePrefixLengthList().
        impl->populatePrefixLengthList(CfgMgr::instance().getCurrentCfg());
        prefix_lengths = impl->getPrefixLengthList();
        ASSERT_EQ(scenario.expected_end_size_, prefix_lengths.size());
        EXPECT_EQ(scenario.expected_list_, impl->dumpPrefixLengthList(prefix_lengths));
    }
}

#ifdef HAVE_MYSQL
/// @brief MySQL test fixture.
class MySQLLeaseQueryImpl6ProcessTest : public
    BaseLeaseQueryImpl6ProcessTest<MySQLTestLeaseMgr> {
};

TEST_F(MySQLLeaseQueryImpl6ProcessTest, makeClientOptionMultipleLinks) {
    testMakeClientOptionMultipleLinks();
}

TEST_F(MySQLLeaseQueryImpl6ProcessTest, makeClientOptionSingleLink) {
    testMakeClientOptionSingleLink();
}

TEST_F(MySQLLeaseQueryImpl6ProcessTest, makeRelayOptionNoRelayInfo) {
        testMakeRelayOptionNoRelayInfo();
}

TEST_F(MySQLLeaseQueryImpl6ProcessTest, makeRelayOptionRelayInfo) {
    testMakeRelayOptionRelayInfo();
}

TEST_F(MySQLLeaseQueryImpl6ProcessTest, oldMakeRelayOptionRelayInfo) {
    testOldMakeRelayOptionRelayInfo();
}

TEST_F(MySQLLeaseQueryImpl6ProcessTest, queryByIpAddressNoActiveLease) {
    testQueryByIpAddressNoActiveLease();
}

TEST_F(MySQLLeaseQueryImpl6ProcessTest, queryByIpAddressActiveLease) {
    testQueryByIpAddressActiveLease();
}

TEST_F(MySQLLeaseQueryImpl6ProcessTest, queryByClientIdNoActiveLease) {
    testQueryByClientIdNoActiveLease();
}

TEST_F(MySQLLeaseQueryImpl6ProcessTest, queryByClientIdMultipleLinks) {
    testQueryByClientIdMultipleLinks();
}

TEST_F(MySQLLeaseQueryImpl6ProcessTest, queryByClientIdActiveLeases) {
    testQueryByClientIdActiveLeases();
}
#endif

#ifdef HAVE_PGSQL
/// @brief PostgreSQL test fixture.
class PgSQLLeaseQueryImpl6ProcessTest : public
    BaseLeaseQueryImpl6ProcessTest<PgSQLTestLeaseMgr> {
};

TEST_F(PgSQLLeaseQueryImpl6ProcessTest, makeClientOptionMultipleLinks) {
    testMakeClientOptionMultipleLinks();
}

TEST_F(PgSQLLeaseQueryImpl6ProcessTest, makeClientOptionSingleLink) {
    testMakeClientOptionSingleLink();
}

TEST_F(PgSQLLeaseQueryImpl6ProcessTest, makeRelayOptionNoRelayInfo) {
        testMakeRelayOptionNoRelayInfo();
}

TEST_F(PgSQLLeaseQueryImpl6ProcessTest, makeRelayOptionRelayInfo) {
    testMakeRelayOptionRelayInfo();
}

TEST_F(PgSQLLeaseQueryImpl6ProcessTest, oldMakeRelayOptionRelayInfo) {
    testOldMakeRelayOptionRelayInfo();
}

TEST_F(PgSQLLeaseQueryImpl6ProcessTest, queryByIpAddressNoActiveLease) {
    testQueryByIpAddressNoActiveLease();
}

TEST_F(PgSQLLeaseQueryImpl6ProcessTest, queryByIpAddressActiveLease) {
    testQueryByIpAddressActiveLease();
}

TEST_F(PgSQLLeaseQueryImpl6ProcessTest, queryByClientIdNoActiveLease) {
    testQueryByClientIdNoActiveLease();
}

TEST_F(PgSQLLeaseQueryImpl6ProcessTest, queryByClientIdMultipleLinks) {
    testQueryByClientIdMultipleLinks();
}

TEST_F(PgSQLLeaseQueryImpl6ProcessTest, queryByClientIdActiveLeases) {
    testQueryByClientIdActiveLeases();
}
#endif

}