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
// Copyright (C) 2014-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>

#include <asiolink/io_address.h>
#include <dhcp/duid.h>
#include <dhcp/hwaddr.h>
#include <dhcpsrv/cfg_hosts.h>
#include <dhcpsrv/cfg_hosts_util.h>
#include <dhcpsrv/host.h>
#include <dhcpsrv/cfgmgr.h>
#include <testutils/gtest_utils.h>
#include <testutils/multi_threading_utils.h>

#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 <set><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace isc;
using namespace isc::asiolink;
using namespace isc::dhcp;
using namespace isc::test;
using namespace isc::util;

namespace {

/// @brief Test fixture class for testing @c CfgHost object holding
/// host reservations specified in the configuration file.
class CfgHostsTest : public ::testing::Test {
public:

    /// @brief Constructor.
    ///
    /// This constructor allocates a collection of @c HWAddr and @c DuidPtr
    /// objects used by the unit tests.
    ///
    /// The allocated HW addresses use the following pattern: 01:02:0A:BB:03:XX
    /// where XX is a number between 0 and 0x32. All of them are of the
    /// HTYPE_ETHER type.
    ///
    /// The allocated DUID LLTs use the following pattern:
    /// 01:02:03:04:05:06:07:08:09:0A:XX where the XX is a number between
    /// 0 and 0x32.
    CfgHostsTest();

    /// @brief Destructor.
    ///
    /// This destructor resets global state after tests are run.
    ~CfgHostsTest();

    /// @brief Increases last byte of an address.
    ///
    /// @param address Address to be increased.
    IOAddress increase(const IOAddress& address, const uint8_t num) const;

    /// @brief test methods.
    void testGetAllNonRepeatingHosts();
    void testGetAllRepeatingHosts();
    void testGetAll4BySubnet();
    void testGetAll6BySubnet();
    void testGetAll6ByAddress();
    void testGetPage4();
    void testGetPage6();
    void testGetPage4All();
    void testGetPage6All();
    void testGetAll4ByAddress();
    void testDeleteForIPv4();
    void testDeleteForIPv6();
    void testDelete2ForIPv6();
    void testDeleteBothForIPv6();
    void testDel4();
    void testDel6();
    void testDeleteAll4();
    void testGet4();
    void testUnparsed4();
    void testGet6();
    void testDeleteAll6();
    void testUnparse6();
    void testGet6ByAddr();
    void testGet6MultipleAddrs();
    void testAdd4AlreadyReserved();
    void testAllow4AlreadyReserved();
    void testAdd6Invalid2Hosts();
    void testAllowAddress6AlreadyReserved();
    void testAllowPrefix6AlreadyReserved();
    void testPrefixExclude();
    void testDuplicatesSubnet4HWAddr();
    void testDuplicatesSubnet4DUID();
    void testDuplicatesSubnet6HWAddr();
    void testDuplicatesSubnet6DUID();
    void testUpdate();

    /// @brief Collection of HW address objects allocated for unit tests.
    std::vector<HWAddrPtr> hwaddrs_;
    /// @brief Collection of DUIDs allocated for unit tests.
    std::vector<DuidPtr> duids_;
    /// @brief Collection of IPv4 address objects allocated for unit tests.
    std::vector<IOAddress> addressesa_;
    std::vector<IOAddress> addressesb_;
};

CfgHostsTest::CfgHostsTest() {
    const uint8_t mac_template[] = {
        0x01, 0x02, 0x0A, 0xBB, 0x03, 0x00
    };
    for (unsigned i = 0; i < 50; ++i) {
        std::vector<uint8_t> vec(mac_template,
                                 mac_template + sizeof(mac_template));
        vec[vec.size() - 1] = i;
        HWAddrPtr hwaddr(new HWAddr(vec, HTYPE_ETHER));
        hwaddrs_.push_back(hwaddr);
    }

    const uint8_t duid_template[] = {
        0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x00
    };
    for (unsigned i = 0; i < 50; ++i) {
        std::vector<uint8_t> vec(duid_template,
                                 duid_template + sizeof(mac_template));
        vec[vec.size() - 1] = i;
        DuidPtr duid(new DUID(vec));
        duids_.push_back(duid);
    }

    const uint32_t addra_template = 0xc0000205; // 192.0.2.5
    const uint32_t addrb_template = 0xc00a020a; // 192.10.2.10
    for (unsigned i = 0; i < 50; ++i) {
        IOAddress addra(addra_template + i);
        addressesa_.push_back(addra);
        IOAddress addrb(addrb_template + i);
        addressesb_.push_back(addrb);
    }
    MultiThreadingMgr::instance().setMode(false);
}

CfgHostsTest::~CfgHostsTest() {
    CfgMgr::instance().setFamily(AF_INET);
    MultiThreadingMgr::instance().setMode(false);
}

IOAddress
CfgHostsTest::increase(const IOAddress& address, const uint8_t num) const {
    std::vector<uint8_t> vec = address.toBytes();
    if (!vec.empty()) {
        vec[vec.size() - 1] += num;
        return (IOAddress::fromBytes(address.getFamily(), &vec[0]));
    }
    return (address);
}

// This test checks that hosts with unique HW addresses and DUIDs can be
// retrieved from the host configuration.
void
CfgHostsTest::testGetAllNonRepeatingHosts() {
    CfgHosts cfg;
    // Add 25 hosts identified by HW address and 25 hosts identified by
    // DUID. They are added to different subnets.
    for (unsigned i = 0; i < 25; ++i) {
        cfg.add(HostPtr(new Host(hwaddrs_[i]->toText(false),
                                 "hw-address",
                                 SubnetID(i % 10 + 1), SubnetID(i % 5 + 1),
                                 addressesa_[i])));

        cfg.add(HostPtr(new Host(duids_[i]->toText(), "duid",
                                 SubnetID(i % 5 + 1), SubnetID(i % 10 + 1),
                                 addressesb_[i])));

    }

    // Try to retrieve each added reservation using HW address and DUID. Do it
    // in the reverse order to make sure that the order doesn't matter.
    for (int i = 24; i >= 0; --i) {
        // Get host identified by HW address.
        HostCollection hosts = cfg.getAll(Host::IDENT_HWADDR,
                                          &hwaddrs_[i]->hwaddr_[0],
                                          hwaddrs_[i]->hwaddr_.size());
        ASSERT_EQ(1, hosts.size());
        EXPECT_EQ(i % 10 + 1, hosts[0]->getIPv4SubnetID());
        EXPECT_EQ(addressesa_[i].toText(),
                  hosts[0]->getIPv4Reservation().toText());

        // Get host identified by DUID.
        hosts = cfg.getAll(Host::IDENT_DUID,
                           &duids_[i]->getDuid()[0],
                           duids_[i]->getDuid().size());
        ASSERT_EQ(1, hosts.size());
        EXPECT_EQ(i % 5 + 1, hosts[0]->getIPv4SubnetID());
        EXPECT_EQ(addressesb_[i].toText(),
                  hosts[0]->getIPv4Reservation().toText());
    }

    // Make sure that the reservations do not exist for the hardware addresses
    // and DUIDs from the range of 25 to 49.
    for (int i = 49; i >= 25; --i) {
        EXPECT_TRUE(cfg.getAll(Host::IDENT_HWADDR, &hwaddrs_[i]->hwaddr_[0],
                               hwaddrs_[i]->hwaddr_.size()).empty());
        EXPECT_TRUE(cfg.getAll(Host::IDENT_DUID, &duids_[i]->getDuid()[0],
                               duids_[i]->getDuid().size()).empty());
    }
}

TEST_F(CfgHostsTest, getAllNonRepeatingHosts) {<--- syntax error
    testGetAllNonRepeatingHosts();
}

TEST_F(CfgHostsTest, getAllNonRepeatingHostsMultiThreading) {
    MultiThreadingTest mt(true);
    testGetAllNonRepeatingHosts();
}

// This test verifies that the host can be added to multiple subnets and
// that the getAll message retrieves all instances of the host.
void
CfgHostsTest::testGetAllRepeatingHosts() {
    CfgHosts cfg;
    // Add hosts.
    for (unsigned i = 0; i < 25; ++i) {
        // Add two hosts, using the same HW address to two distinct subnets.
        cfg.add(HostPtr(new Host(hwaddrs_[i]->toText(false),
                                 "hw-address",
                                 SubnetID(1), SubnetID(2),
                                 addressesa_[i])));
        cfg.add(HostPtr(new Host(hwaddrs_[i]->toText(false),
                                 "hw-address",
                                 SubnetID(2), SubnetID(3),
                                 addressesb_[i])));

        // Add two hosts, using the same DUID to two distinct subnets.
        cfg.add(HostPtr(new Host(duids_[i]->toText(), "duid",
                                 SubnetID(1), SubnetID(2),
                                 addressesb_[i])));
        cfg.add(HostPtr(new Host(duids_[i]->toText(), "duid",
                                 SubnetID(2), SubnetID(3),
                                 addressesa_[i])));
    }

    // Verify that hosts can be retrieved.
    for (unsigned i = 0; i < 25; ++i) {
        // Get host by HW address.
        HostCollection hosts = cfg.getAll(Host::IDENT_HWADDR,
                                          &hwaddrs_[i]->hwaddr_[0],
                                          hwaddrs_[i]->hwaddr_.size());
        ASSERT_EQ(2, hosts.size());
        EXPECT_EQ(1, hosts[0]->getIPv4SubnetID());
        EXPECT_EQ(addressesa_[i], hosts[0]->getIPv4Reservation().toText());
        EXPECT_EQ(2, hosts[1]->getIPv4SubnetID());
        EXPECT_EQ(addressesb_[i], hosts[1]->getIPv4Reservation().toText());

        // The HW address is non-null but there are no reservations
        // for the HW addresses from the range of 25 to 49.
        hosts = cfg.getAll(Host::IDENT_HWADDR,
                           &hwaddrs_[i + 25]->hwaddr_[0],
                           hwaddrs_[i + 25]->hwaddr_.size());
        EXPECT_TRUE(hosts.empty());

        // Get host by DUID.
        hosts = cfg.getAll(Host::IDENT_DUID,
                           &duids_[i]->getDuid()[0],
                           duids_[i]->getDuid().size());

        // The DUID is non-null but there are no reservations
        // for the DUIDs from the range of 25 to 49.
        hosts = cfg.getAll(Host::IDENT_DUID,
                           &duids_[i + 25]->getDuid()[0],
                           duids_[i + 25]->getDuid().size());
        EXPECT_TRUE(hosts.empty());
    }
}

TEST_F(CfgHostsTest, getAllRepeatingHosts) {
    testGetAllRepeatingHosts();
}

TEST_F(CfgHostsTest, getAllRepeatingHostsMultiThreading) {
    MultiThreadingTest mt(true);
    testGetAllRepeatingHosts();
}

// This test checks that hosts in the same subnet can be retrieved from
// the host configuration.
void
CfgHostsTest::testGetAll4BySubnet() {
    CfgHosts cfg;
    // Add 25 hosts identified by HW address in the same subnet.
    for (unsigned i = 0; i < 25; ++i) {
        cfg.add(HostPtr(new Host(hwaddrs_[i]->toText(false),
                                 "hw-address",
                                 SubnetID(1), SubnetID(1),
                                 addressesa_[i])));
    }

    // Check that other subnets are empty.
    HostCollection hosts = cfg.getAll4(SubnetID(100));
    EXPECT_EQ(0, hosts.size());

    // Try to retrieve all added reservations.
    hosts = cfg.getAll4(SubnetID(1));
    ASSERT_EQ(25, hosts.size());
    for (unsigned i = 0; i < 25; ++i) {
        EXPECT_EQ(1, hosts[i]->getIPv4SubnetID());
        EXPECT_EQ(addressesa_[i].toText(),
                  hosts[i]->getIPv4Reservation().toText());
    }
}

TEST_F(CfgHostsTest, getAll4BySubnet) {
    testGetAll4BySubnet();
}

TEST_F(CfgHostsTest, getAll4BySubnetMultiThreading) {
    MultiThreadingTest mt(true);
    testGetAll4BySubnet();
}

// This test checks that hosts in the same subnet can be retrieved from
// the host configuration.
void
CfgHostsTest::testGetAll6BySubnet() {
    CfgHosts cfg;
    // Add 25 hosts identified by DUID in the same subnet.
    for (unsigned i = 0; i < 25; ++i) {
        HostPtr host = HostPtr(new Host(duids_[i]->toText(), "duid",
                                        SubnetID(1), SubnetID(1),
                                        IOAddress("0.0.0.0")));
        host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA,
                                       increase(IOAddress("2001:db8:1::1"),
                                                i)));
        cfg.add(host);
    }

    // Check that other subnets are empty.
    HostCollection hosts = cfg.getAll6(SubnetID(100));
    EXPECT_EQ(0, hosts.size());

    // Try to retrieve all added reservations.
    hosts = cfg.getAll6(SubnetID(1));
    ASSERT_EQ(25, hosts.size());
    for (unsigned i = 0; i < 25; ++i) {
        EXPECT_EQ(1, hosts[i]->getIPv6SubnetID());
        IPv6ResrvRange reservations =
            hosts[i]->getIPv6Reservations(IPv6Resrv::TYPE_NA);
        ASSERT_EQ(1, std::distance(reservations.first, reservations.second));
        EXPECT_EQ(increase(IOAddress("2001:db8:1::1"), i),
                  reservations.first->second.getPrefix());
    }
}

TEST_F(CfgHostsTest, getAll6BySubnet) {
    testGetAll6BySubnet();
}

TEST_F(CfgHostsTest, getAll6BySubnetMultiThreading) {
    MultiThreadingTest mt(true);
    testGetAll6BySubnet();
}

// This test checks that hosts with the same reserved address can be retrieved
// from the host configuration.
void
CfgHostsTest::testGetAll6ByAddress() {
    CfgHosts cfg;
    // Add 25 hosts identified by DUID in the same subnet.
    for (unsigned i = 0; i < 25; ++i) {
        HostPtr host = HostPtr(new Host(duids_[i]->toText(), "duid",
                                        SUBNET_ID_UNUSED, SubnetID(i+1),
                                        IOAddress("0.0.0.0")));
        host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA,
                                       increase(IOAddress("2001:db8:1::1"),
                                                i % 5)));
        cfg.add(host);
    }

    // Try to retrieve all added reservations with IP equals 2001:db8:1::1.
    auto hosts = cfg.getAll6(IOAddress("2001:db8:1::1"));
    EXPECT_EQ(5, hosts.size());
    for (unsigned i = 0; i < 5; ++i) {
        EXPECT_EQ(1 + 5 * i, hosts[i]->getIPv6SubnetID());
        IPv6ResrvRange reservations =
            hosts[i]->getIPv6Reservations(IPv6Resrv::TYPE_NA);
        ASSERT_EQ(1, std::distance(reservations.first, reservations.second));
        EXPECT_EQ(IOAddress("2001:db8:1::1"),
                  reservations.first->second.getPrefix());
    }
}

TEST_F(CfgHostsTest, getAll6ByAddress) {
    testGetAll6ByAddress();
}

TEST_F(CfgHostsTest, getAll6ByAddressMultiThreading) {
    MultiThreadingTest mt(true);
    testGetAll6ByAddress();
}

// This test checks that hosts in the same subnet can be retrieved from
// the host configuration by pages.
void
CfgHostsTest::testGetPage4() {
    CfgHosts cfg;
    // Add 25 hosts identified by DUID in the same subnet.
    for (unsigned i = 0; i < 25; ++i) {
        cfg.add(HostPtr(new Host(duids_[i]->toText(), "duid",
                                 SubnetID(1), SubnetID(1),
                                 addressesa_[i])));
    }
    size_t idx(0);
    uint64_t host_id(0);
    HostPageSize page_size(10);

    // Check that other subnets are empty.
    HostCollection page = cfg.getPage4(SubnetID(100), idx, host_id, page_size);
    EXPECT_EQ(0, page.size());

    // Try to retrieve all added reservations.
    // Get first page.
    page = cfg.getPage4(SubnetID(1), idx, host_id, page_size);
    EXPECT_EQ(10, page.size());
    host_id = page[9]->getHostId();

    // Get second and last pages.
    page = cfg.getPage4(SubnetID(1), idx, host_id, page_size);
    EXPECT_EQ(10, page.size());
    host_id = page[9]->getHostId();
    page = cfg.getPage4(SubnetID(1), idx, host_id, page_size);
    EXPECT_EQ(5, page.size());
    host_id = page[4]->getHostId();

    // Verify we have everything.
    page = cfg.getPage4(SubnetID(1), idx, host_id, page_size);
    EXPECT_EQ(0, page.size());
}

TEST_F(CfgHostsTest, getPage4) {
    testGetPage4();
}

TEST_F(CfgHostsTest, getPage4MultiThreading) {
    MultiThreadingTest mt(true);
    testGetPage4();
}

// This test checks that hosts in the same subnet can be retrieved from
// the host configuration by pages.
void
CfgHostsTest::testGetPage6() {
    CfgHosts cfg;
    // Add 25 hosts identified by HW address in the same subnet.
    for (unsigned i = 0; i < 25; ++i) {
        HostPtr host = HostPtr(new Host(hwaddrs_[i]->toText(false),
                                        "hw-address",
                                        SubnetID(1), SubnetID(1),
                                        IOAddress("0.0.0.0")));
        host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA,
                                       increase(IOAddress("2001:db8:1::1"),
                                                i)));
        cfg.add(host);
    }
    size_t idx(0);
    uint64_t host_id(0);
    HostPageSize page_size(10);

    // Check that other subnets are empty.
    HostCollection page = cfg.getPage6(SubnetID(100), idx, host_id, page_size);
    EXPECT_EQ(0, page.size());

    // Try to retrieve all added reservations.
    // Get first page.
    page = cfg.getPage6(SubnetID(1), idx, host_id, page_size);
    EXPECT_EQ(10, page.size());
    host_id = page[9]->getHostId();

    // Get second and last pages.
    page = cfg.getPage6(SubnetID(1), idx, host_id, page_size);
    EXPECT_EQ(10, page.size());
    host_id = page[9]->getHostId();
    page = cfg.getPage6(SubnetID(1), idx, host_id, page_size);
    EXPECT_EQ(5, page.size());
    host_id = page[4]->getHostId();

    // Verify we have everything.
    page = cfg.getPage6(SubnetID(1), idx, host_id, page_size);
    EXPECT_EQ(0, page.size());
}

TEST_F(CfgHostsTest, getPage6) {
    testGetPage6();
}

TEST_F(CfgHostsTest, getPage6MultiThreading) {
    MultiThreadingTest mt(true);
    testGetPage6();
}

// This test checks that all hosts can be retrieved from the host
// configuration by pages.
void
CfgHostsTest::testGetPage4All() {
    CfgHosts cfg;
    // Add 25 hosts identified by DUID.
    for (unsigned i = 0; i < 25; ++i) {
        cfg.add(HostPtr(new Host(duids_[i]->toText(), "duid",
                                 SubnetID(i), SubnetID(i),
                                 addressesa_[i])));
    }
    size_t idx(0);
    uint64_t host_id(0);
    HostPageSize page_size(10);

    // Try to retrieve all added reservations.
    // Get first page.
    HostCollection page = cfg.getPage4(idx, host_id, page_size);
    EXPECT_EQ(10, page.size());
    host_id = page[9]->getHostId();

    // Get second and last pages.
    page = cfg.getPage4(idx, host_id, page_size);
    EXPECT_EQ(10, page.size());
    host_id = page[9]->getHostId();
    page = cfg.getPage4(idx, host_id, page_size);
    EXPECT_EQ(5, page.size());
    host_id = page[4]->getHostId();

    // Verify we have everything.
    page = cfg.getPage4(idx, host_id, page_size);
    EXPECT_EQ(0, page.size());
}

TEST_F(CfgHostsTest, getPage4All) {
    testGetPage4All();
}

TEST_F(CfgHostsTest, getPage4AllMultiThreading) {
    MultiThreadingTest mt(true);
    testGetPage4All();
}

// This test checks that all hosts can be retrieved from the host
// configuration by pages.
void
CfgHostsTest::testGetPage6All() {
    CfgHosts cfg;
    // Add 25 hosts identified by HW address.
    for (unsigned i = 0; i < 25; ++i) {
        HostPtr host = HostPtr(new Host(hwaddrs_[i]->toText(false),
                                        "hw-address",
                                        SubnetID(i), SubnetID(i),
                                        IOAddress("0.0.0.0")));
        host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA,
                                       increase(IOAddress("2001:db8:1::1"),
                                                i)));
        cfg.add(host);
    }
    size_t idx(0);
    uint64_t host_id(0);
    HostPageSize page_size(10);

    // Try to retrieve all added reservations.
    // Get first page.
    HostCollection page = cfg.getPage6(idx, host_id, page_size);
    EXPECT_EQ(10, page.size());
    host_id = page[9]->getHostId();

    // Get second and last pages.
    page = cfg.getPage6(idx, host_id, page_size);
    EXPECT_EQ(10, page.size());
    host_id = page[9]->getHostId();
    page = cfg.getPage6(idx, host_id, page_size);
    EXPECT_EQ(5, page.size());
    host_id = page[4]->getHostId();

    // Verify we have everything.
    page = cfg.getPage6(idx, host_id, page_size);
    EXPECT_EQ(0, page.size());
}

TEST_F(CfgHostsTest, getPage6All) {
    testGetPage6All();
}

TEST_F(CfgHostsTest, getPage6AllMultiThreading) {
    MultiThreadingTest mt(true);
    testGetPage6All();
}

// This test checks that all reservations for the specified IPv4 address can
// be retrieved.
void
CfgHostsTest::testGetAll4ByAddress() {
    CfgHosts cfg;
    // Add hosts.
    for (unsigned i = 0; i < 25; ++i) {
        // Add host identified by the HW address.
        cfg.add(HostPtr(new Host(hwaddrs_[i]->toText(false),
                                 "hw-address",
                                 SubnetID(1 + i), SUBNET_ID_UNUSED,
                                 IOAddress("192.0.2.5"))));
        // Add host identified by the DUID.
        cfg.add(HostPtr(new Host(duids_[i]->toText(),
                                 "duid",
                                 SubnetID(1 + i), SUBNET_ID_UNUSED,
                                 IOAddress("192.0.2.10"))));
    }

    HostCollection hosts = cfg.getAll4(IOAddress("192.0.2.10"));
    std::set<uint32_t> subnet_ids;
    for (auto const& host : hosts) {
        subnet_ids.insert(host->getIPv4SubnetID());
    }
    ASSERT_EQ(25, subnet_ids.size());
    EXPECT_EQ(1, *subnet_ids.begin());
    EXPECT_EQ(25, *subnet_ids.rbegin());
}

TEST_F(CfgHostsTest, getAll4ByAddress) {
    testGetAll4ByAddress();
}

TEST_F(CfgHostsTest, getAll4ByAddressMultiThreading) {
    MultiThreadingTest mt(true);
    testGetAll4ByAddress();
}

// This test checks that the IPv4 reservation for the specified IPv4 address can
// be deleted.
void
CfgHostsTest::testDeleteForIPv4() {
    CfgHosts cfg;
    // Add hosts.
    IOAddress address("10.0.0.42");
    SubnetID subnet_id(42);
    size_t host_count = 10;

    for (size_t i = 0; i < host_count; i++)
    {
        cfg.add(HostPtr(new Host(hwaddrs_[i]->toText(false),
                                "hw-address",
                                subnet_id, SUBNET_ID_UNUSED,
                                increase(address, i))));
    }

    // Get all inserted hosts.
    HostCollection hosts_by_subnet = cfg.getAll4(subnet_id);
    HostCollection hosts_by_address = cfg.getAll4(address);
    // Make sure the hosts and IP reservations were added.
    ASSERT_EQ(host_count, hosts_by_subnet.size());
    ASSERT_EQ(1, hosts_by_address.size());

    // Delete one host.
    EXPECT_TRUE(cfg.del(subnet_id, address));

    // Check if the host is actually deleted.
    hosts_by_subnet = cfg.getAll4(subnet_id);
    hosts_by_address = cfg.getAll4(address);
    EXPECT_EQ(host_count-1, hosts_by_subnet.size());
    EXPECT_EQ(0, hosts_by_address.size());
}

TEST_F(CfgHostsTest, deleteForIPv4) {
    testDeleteForIPv4();
}

TEST_F(CfgHostsTest, deleteForIPv4MultiThreading) {
    MultiThreadingTest mt(true);
    testDeleteForIPv4();
}

// This test checks that the IPv6 reservation for the specified subnet ID and
// IPv6 address can be deleted.
void
CfgHostsTest::testDeleteForIPv6() {
    CfgHosts cfg;
    // Add hosts.
    IOAddress address("2001:db8:1::1");
    size_t host_count = 10;
    SubnetID subnet_id(42);

    for (size_t i = 0; i < host_count; i++)
    {
        HostPtr host = HostPtr(new Host(duids_[i]->toText(), "duid",
                                        SUBNET_ID_UNUSED, subnet_id,
                                        IOAddress::IPV4_ZERO_ADDRESS()));
        host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA,
                                       increase(IOAddress(address), i)));
        cfg.add(host);
    }


    // Get all inserted hosts.
    auto hosts_by_subnet_and_address = cfg.getAll6(subnet_id, address);
    auto hosts_by_subnet = cfg.getAll6(subnet_id);
    // Make sure the hosts and IP reservations were added.
    ASSERT_EQ(1, hosts_by_subnet_and_address.size());
    ASSERT_EQ(host_count, hosts_by_subnet.size());

    // Delete one host.
    EXPECT_TRUE(cfg.del(subnet_id, address));

    // Check if the host is actually deleted.
    hosts_by_subnet_and_address = cfg.getAll6(subnet_id, address);
    hosts_by_subnet = cfg.getAll6(subnet_id);
    EXPECT_EQ(0, hosts_by_subnet_and_address.size());
    EXPECT_EQ(host_count-1, hosts_by_subnet.size());
}

TEST_F(CfgHostsTest, deleteForIPv6) {
    testDeleteForIPv6();
}

TEST_F(CfgHostsTest, deleteForIPv6MultiThreading) {
    MultiThreadingTest mt(true);
    testDeleteForIPv6();
}

// This test checks that two IPv6 reservations for the specified subnet ID and
// IPv6 address can be deleted.
void
CfgHostsTest::testDelete2ForIPv6() {
    CfgHosts cfg;
    // Add a host with two addresses.
    IOAddress address1("2001:db8:1::1");
    IOAddress address2("2001:db8:2::2");
    SubnetID subnet_id(42);

    HostPtr host = HostPtr(new Host(duids_[0]->toText(), "duid",
                                    SUBNET_ID_UNUSED, subnet_id,
                                    IOAddress::IPV4_ZERO_ADDRESS()));
    host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA, address1));
    host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA, address2));
    cfg.add(host);

    // Delete the host using its first address.
    EXPECT_TRUE(cfg.del(subnet_id, address1));

    // Check if all addresses were removed.
    EXPECT_FALSE(cfg.get6(subnet_id, address2));
    EXPECT_FALSE(cfg.del(subnet_id, address2));
}

TEST_F(CfgHostsTest, delete2ForIPv6) {
    testDelete2ForIPv6();
}

TEST_F(CfgHostsTest, delete2ForIPv6MultiThreading) {
    MultiThreadingTest mt(true);
    testDelete2ForIPv6();
}

// This test checks that IPv6 address and prefix reservations for the specified
// subnet ID and IPv6 address can be deleted.
void
CfgHostsTest::testDeleteBothForIPv6() {
    CfgHosts cfg;
    // Add a host with two addresses.
    IOAddress address1("2001:db8:1::1");
    IOAddress address2("2001:db8:2::");
    SubnetID subnet_id(42);

    HostPtr host = HostPtr(new Host(duids_[0]->toText(), "duid",
                                    SUBNET_ID_UNUSED, subnet_id,
                                    IOAddress::IPV4_ZERO_ADDRESS()));
    host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA, address1));
    host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_PD, address2, 64));
    cfg.add(host);

    // Delete the host using its address.
    EXPECT_TRUE(cfg.del(subnet_id, address1));

    // Check if all reservations were removed.
    EXPECT_FALSE(cfg.get6(subnet_id, address2));
    EXPECT_FALSE(cfg.del(subnet_id, address2));
}

TEST_F(CfgHostsTest, deleteBothForIPv6) {
    testDeleteBothForIPv6();
}

TEST_F(CfgHostsTest, deleteBothForIPv6MultiThreading) {
    MultiThreadingTest mt(true);
    testDeleteBothForIPv6();
}

// This test checks that false is returned for deleting the IPv4 reservation
// that doesn't exist.
TEST_F(CfgHostsTest, deleteForMissingIPv4) {
    CfgHosts cfg;

    // Delete non-existent host.
    EXPECT_FALSE(cfg.del(SubnetID(42), IOAddress(("10.0.0.42"))));

    MultiThreadingTest mt(true);
    EXPECT_FALSE(cfg.del(SubnetID(42), IOAddress(("10.0.0.42"))));
}

// This test checks that false is returned for deleting the IPv6 reservation
// that doesn't exist.
TEST_F(CfgHostsTest, deleteForMissingIPv6) {
    CfgHosts cfg;

    // Delete non-existent host.
    EXPECT_FALSE(cfg.del(SubnetID(42), IOAddress(("2001:db8:1::1"))));

    MultiThreadingTest mt(true);
    EXPECT_FALSE(cfg.del(SubnetID(42), IOAddress(("2001:db8:1::1"))));
}

// This test checks that the reservation for the specified IPv4 subnet and
// identifier can be deleted.
void
CfgHostsTest::testDel4() {
    CfgHosts cfg;

    // Add hosts.
    size_t host_count = 20;
    size_t host_id = 5;
    SubnetID subnet_id(42);
    IOAddress address("10.0.0.1");

    // Add half of the hosts with the same subnet ID but differ with DUID and
    // address.
    for (size_t i = 0; i < host_count / 2; i++) {
        HostPtr host = HostPtr(new Host(duids_[i]->toText(), "duid",
                                        subnet_id, SUBNET_ID_UNUSED,
                                        increase(address, i)));
        cfg.add(host);
    }
    // Add half of the hosts with the same subnet DUID and address but
    // differ with address.
    for (size_t i = 0; i < host_count / 2; i++) {
        HostPtr host = HostPtr(new Host(duids_[host_id]->toText(), "duid",
                                        SubnetID(subnet_id + i + 1), SUBNET_ID_UNUSED,
                                        increase(address, host_id)));
        cfg.add(host);
    }


    // Get all inserted hosts.
    HostCollection hosts_by_subnet = cfg.getAll4(subnet_id);
    HostCollection hosts_by_address = cfg.getAll4(increase(address, host_id));
    HostPtr host = cfg.get4(subnet_id, Host::IdentifierType::IDENT_DUID,
                                             &duids_[host_id]->getDuid()[0],
                                             duids_[host_id]->getDuid().size());
    // Make sure the hosts and IP reservations were added.
    ASSERT_EQ(host_count / 2, hosts_by_subnet.size());
    ASSERT_EQ(host_count / 2 + 1, hosts_by_address.size());
    ASSERT_TRUE(host);

    // Delete one host.
    EXPECT_TRUE(cfg.del4(subnet_id, Host::IdentifierType::IDENT_DUID,
                         &duids_[host_id]->getDuid()[0], duids_[host_id]->getDuid().size()));

    // Check if the host is actually deleted.
    hosts_by_subnet = cfg.getAll4(subnet_id);
    hosts_by_address = cfg.getAll4(increase(address, host_id));
    host = cfg.get4(subnet_id, Host::IdentifierType::IDENT_DUID,
                                             &duids_[host_id]->getDuid()[0],
                                             duids_[host_id]->getDuid().size());
    EXPECT_EQ((host_count / 2)-1, hosts_by_subnet.size());
    EXPECT_EQ(host_count / 2, hosts_by_address.size());
    EXPECT_FALSE(host);
}

TEST_F(CfgHostsTest, del4) {
    testDel4();
}

TEST_F(CfgHostsTest, del4MultiThreading) {
    MultiThreadingTest mt(true);
    testDel4();
}

// This test checks that the host and its reservations for the specified IPv6
// subnet and identifier can be deleted.
void
CfgHostsTest::testDel6() {
    CfgHosts cfg;

    // Add hosts.
    size_t host_count = 20;
    size_t host_id = 5;
    SubnetID subnet_id(42);
    IOAddress address("2001:db8:1::1");

    // Add half of the hosts with the same subnet ID but differ with DUID and
    // address.
    for (size_t i = 0; i < host_count / 2; i++) {
        HostPtr host = HostPtr(new Host(duids_[i]->toText(), "duid",
                                        SUBNET_ID_UNUSED, subnet_id,
                                        IOAddress::IPV4_ZERO_ADDRESS()));
        host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA,
                                       increase(IOAddress(address), i)));
        cfg.add(host);
    }
    // Add half of the hosts with the same subnet DUID and address but
    // differ with address.
    for (size_t i = 0; i < host_count / 2; i++) {
        HostPtr host = HostPtr(new Host(duids_[host_id]->toText(), "duid",
                                        SUBNET_ID_UNUSED, SubnetID(subnet_id + i + 1),
                                        IOAddress::IPV4_ZERO_ADDRESS()));
        host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA,
                                       increase(address, host_id)));
        cfg.add(host);
    }


    // Get all inserted hosts.
    HostCollection hosts_by_subnet = cfg.getAll6(subnet_id);
    HostCollection hosts_by_address = cfg.getAll6(increase(address, host_id));
    HostPtr host = cfg.get6(subnet_id, Host::IdentifierType::IDENT_DUID,
                                             &duids_[host_id]->getDuid()[0],
                                             duids_[host_id]->getDuid().size());
    // Make sure the hosts and IP reservations were added.
    ASSERT_EQ(host_count / 2, hosts_by_subnet.size());
    ASSERT_EQ(host_count / 2 + 1, hosts_by_address.size());
    ASSERT_TRUE(host);

    // Delete one host.
    EXPECT_TRUE(cfg.del6(subnet_id, Host::IdentifierType::IDENT_DUID,
                         &duids_[host_id]->getDuid()[0], duids_[host_id]->getDuid().size()));

    // Check if the host is actually deleted.
    hosts_by_subnet = cfg.getAll6(subnet_id);
    hosts_by_address = cfg.getAll6(increase(address, host_id));
    host = cfg.get6(subnet_id, Host::IdentifierType::IDENT_DUID,
                                             &duids_[host_id]->getDuid()[0],
                                             duids_[host_id]->getDuid().size());
    EXPECT_EQ((host_count / 2)-1, hosts_by_subnet.size());
    EXPECT_EQ(host_count / 2, hosts_by_address.size());
    EXPECT_FALSE(host);
}

TEST_F(CfgHostsTest, del6) {
    testDel6();
}

TEST_F(CfgHostsTest, del6MultiThreading) {
    MultiThreadingTest mt(true);
    testDel6();
}

// This test checks that false is returned for deleting the IPv4 host that
// doesn't exist.
TEST_F(CfgHostsTest, del4MissingHost) {
    CfgHosts cfg;
    EXPECT_FALSE(cfg.del4(SubnetID(42), Host::IdentifierType::IDENT_DUID,
                         &duids_[0]->getDuid()[0], duids_[0]->getDuid().size()));
    MultiThreadingTest mt(true);
    EXPECT_FALSE(cfg.del4(SubnetID(42), Host::IdentifierType::IDENT_DUID,
                         &duids_[0]->getDuid()[0], duids_[0]->getDuid().size()));
}

// This test checks that false is returned for deleting the IPv6 host that
// doesn't exist.
TEST_F(CfgHostsTest, del6MissingHost) {
    CfgHosts cfg;
    EXPECT_FALSE(cfg.del6(SubnetID(42), Host::IdentifierType::IDENT_DUID,
                         &duids_[0]->getDuid()[0], duids_[0]->getDuid().size()));
    MultiThreadingTest mt(true);
    EXPECT_FALSE(cfg.del6(SubnetID(42), Host::IdentifierType::IDENT_DUID,
                         &duids_[0]->getDuid()[0], duids_[0]->getDuid().size()));
}

// This test checks that all reservations for the specified IPv4 subnet can
// be deleted.
void
CfgHostsTest::testDeleteAll4() {
    CfgHosts cfg;
    // Add hosts.
    for (unsigned i = 0; i < 25; ++i) {
        // Hosts will differ by hostname. It is easier than differentiating by
        // IPv4 address because if they all have zero IPv4 address it is
        // easier to retrieve all of them to check the host counts.
        std::ostringstream s;
        s << "hostname" << i;

        // Add host identified by the HW address.
        cfg.add(HostPtr(new Host(hwaddrs_[i]->toText(false),
                                 "hw-address",
                                 SubnetID(1 + i % 2), SUBNET_ID_UNUSED,
                                 IOAddress::IPV4_ZERO_ADDRESS(),
                                 s.str())));
    }

    // Get all inserted hosts.
    HostCollection hosts = cfg.getAll4(IOAddress::IPV4_ZERO_ADDRESS());
    std::set<uint32_t> subnet_ids;
    for (auto const& host : hosts) {
        subnet_ids.insert(host->getIPv4SubnetID());
    }
    // Make sure there are two unique subnets: 1 and 2.
    ASSERT_EQ(2, subnet_ids.size());
    EXPECT_EQ(1, *subnet_ids.begin());
    EXPECT_EQ(2, *subnet_ids.rbegin());

    // Delete all hosts for subnet id 2. There should be 12 of them.
    EXPECT_EQ(12, cfg.delAll4(SubnetID(2)));

    // Gather the host counts again.
    subnet_ids.clear();
    hosts = cfg.getAll4(IOAddress::IPV4_ZERO_ADDRESS());
    for (auto const& host : hosts) {
        subnet_ids.insert(host->getIPv4SubnetID());
    }
    // We should only have hosts for one subnet and it should be the subnet
    // with ID of 1.
    ASSERT_EQ(1, subnet_ids.size());
    EXPECT_EQ(1, *subnet_ids.begin());
}

TEST_F(CfgHostsTest, deleteAll4) {
    testDeleteAll4();
}

TEST_F(CfgHostsTest, deleteAll4MultiThreading) {
    MultiThreadingTest mt(true);
    testDeleteAll4();
}

// This test checks that the reservations can be retrieved for the particular
// host connected to the specific IPv4 subnet (by subnet id).
void
CfgHostsTest::testGet4() {
    CfgHosts cfg;
    // Add hosts.
    for (unsigned i = 0; i < 25; ++i) {
        // Add host identified by HW address.
        cfg.add(HostPtr(new Host(hwaddrs_[i]->toText(false),
                                 "hw-address",
                                 SubnetID(1 + i % 2), SubnetID(13),
                                 increase(IOAddress("192.0.2.5"), i))));

        // Add host identified by DUID.
        cfg.add(HostPtr(new Host(duids_[i]->toText(), "duid",
                                 SubnetID(1 + i % 2), SubnetID(13),
                                 increase(IOAddress("192.0.2.100"), i))));
    }

    for (unsigned i = 0; i < 25; ++i) {
        // Retrieve host by HW address.
        HostPtr host = cfg.get4(SubnetID(1 + i % 2), Host::IDENT_HWADDR,
                                &hwaddrs_[i]->hwaddr_[0],
                                hwaddrs_[i]->hwaddr_.size());
        ASSERT_TRUE(host);
        EXPECT_EQ(1 + i % 2, host->getIPv4SubnetID());
        EXPECT_EQ(increase(IOAddress("192.0.2.5"), i),
                  host->getIPv4Reservation());

        // Retrieve host by DUID.
        host = cfg.get4(SubnetID(1 + i % 2), Host::IDENT_DUID,
                        &duids_[i]->getDuid()[0], duids_[i]->getDuid().size());
        ASSERT_TRUE(host);
        EXPECT_EQ(1 + i % 2, host->getIPv4SubnetID());
        EXPECT_EQ(increase(IOAddress("192.0.2.100"), i),
                  host->getIPv4Reservation());

    }
}

TEST_F(CfgHostsTest, get4) {
    testGet4();
}

TEST_F(CfgHostsTest, get4MultiThreading) {
    MultiThreadingTest mt(true);
    testGet4();
}

// This test checks that the DHCPv4 reservations can be unparsed
void
CfgHostsTest::testUnparsed4() {
    CfgMgr::instance().setFamily(AF_INET);
    CfgHosts cfg;
    CfgHostsList list;
    // Add hosts.
    for (unsigned i = 0; i < 25; ++i) {
        // Add host identified by HW address.
        cfg.add(HostPtr(new Host(hwaddrs_[i]->toText(false),
                                 "hw-address",
                                 SubnetID(1 + i), SubnetID(13),
                                 increase(IOAddress("192.0.2.5"), i))));

        // Add host identified by DUID.
        cfg.add(HostPtr(new Host(duids_[i]->toText(), "duid",
                                 SubnetID(1 + i), SubnetID(13),
                                 increase(IOAddress("192.0.2.100"), i))));
    }

    using namespace isc::data;
    ConstElementPtr cfg_unparsed;
    ASSERT_NO_THROW(cfg_unparsed = cfg.toElement());
    ASSERT_NO_THROW(list.internalize(cfg_unparsed));
    for (unsigned i = 0; i < 25; ++i) {
        ConstElementPtr unparsed = list.get(SubnetID(1 + i));
        ASSERT_TRUE(unparsed);
        ASSERT_EQ(Element::list, unparsed->getType());
        EXPECT_EQ(2, unparsed->size());
        ASSERT_NE(0, unparsed->size());

        // Check by HW address entries
        bool checked_hw = false;
        for (unsigned j = 0; j < unparsed->size(); ++j) {
            ConstElementPtr host = unparsed->get(j);
            ASSERT_TRUE(host);
            ASSERT_EQ(Element::map, host->getType());
            if (!host->contains("hw-address")) {
                continue;
            }
            checked_hw = true;
            // Not both hw-address and duid
            EXPECT_FALSE(host->contains("duid"));
            // Check the HW address
            ConstElementPtr hw = host->get("hw-address");
            ASSERT_TRUE(hw);
            ASSERT_EQ(Element::string, hw->getType());
            EXPECT_EQ(hwaddrs_[i]->toText(false), hw->stringValue());
            // Check the reservation
            ConstElementPtr resv = host->get("ip-address");
            ASSERT_TRUE(resv);
            ASSERT_EQ(Element::string, resv->getType());
            EXPECT_EQ(increase(IOAddress("192.0.2.5"), i),
                          IOAddress(resv->stringValue()));
        }
        ASSERT_TRUE(checked_hw);

        // Check by DUID entries
        bool checked_duid = false;
        for (unsigned j = 0; j < unparsed->size(); ++j) {
            ConstElementPtr host = unparsed->get(j);
            ASSERT_TRUE(host);
            ASSERT_EQ(Element::map, host->getType());
            if (!host->contains("duid")) {
                continue;
            }
            checked_duid = true;
            // Not both hw-address and duid
            EXPECT_FALSE(host->contains("hw-address"));
            // Check the DUID
            ConstElementPtr duid = host->get("duid");
            ASSERT_TRUE(duid);
            ASSERT_EQ(Element::string, duid->getType());
            EXPECT_EQ(duids_[i]->toText(), duid->stringValue());
            // Check the reservation
            ConstElementPtr resv = host->get("ip-address");
            ASSERT_TRUE(resv);
            ASSERT_EQ(Element::string, resv->getType());
            EXPECT_EQ(increase(IOAddress("192.0.2.100"), i),
                      IOAddress(resv->stringValue()));
        }
        ASSERT_TRUE(checked_duid);
    }
}

TEST_F(CfgHostsTest, unparsed4) {
    testUnparsed4();
}

TEST_F(CfgHostsTest, unparsed4MultiThreading) {
    MultiThreadingTest mt(true);
    testUnparsed4();
}

// This test checks that the reservations can be retrieved for the particular
// host connected to the specific IPv6 subnet (by subnet id).
void
CfgHostsTest::testGet6() {
    CfgHosts cfg;
    // Add hosts.
    for (unsigned i = 0; i < 25; ++i) {
        // Add host identified by HW address.
        HostPtr host = HostPtr(new Host(hwaddrs_[i]->toText(false),
                                        "hw-address",
                                        SubnetID(10), SubnetID(1 + i % 2),
                                        IOAddress("0.0.0.0")));
        host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA,
                                       increase(IOAddress("2001:db8:1::1"),
                                                i)));
        cfg.add(host);

        // Add host identified by DUID.
        host = HostPtr(new Host(duids_[i]->toText(), "duid",
                                SubnetID(10), SubnetID(1 + i % 2),
                                IOAddress("0.0.0.0")));
        host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA,
                                       increase(IOAddress("2001:db8:2::1"),
                                                i)));
        cfg.add(host);
    }

    for (unsigned i = 0; i < 25; ++i) {
        // Retrieve host by HW address.
        HostPtr host = cfg.get6(SubnetID(1 + i % 2), Host::IDENT_HWADDR,
                                &hwaddrs_[i]->hwaddr_[0],
                                hwaddrs_[i]->hwaddr_.size());
        ASSERT_TRUE(host);
        EXPECT_EQ(1 + i % 2, host->getIPv6SubnetID());
        IPv6ResrvRange reservations =
            host->getIPv6Reservations(IPv6Resrv::TYPE_NA);
        ASSERT_EQ(1, std::distance(reservations.first, reservations.second));
        EXPECT_EQ(increase(IOAddress("2001:db8:1::1"), i),
                  reservations.first->second.getPrefix());

        // Retrieve host by DUID.
        host = cfg.get6(SubnetID(1 + i % 2), Host::IDENT_DUID,
                        &duids_[i]->getDuid()[0], duids_[i]->getDuid().size());
        ASSERT_TRUE(host);
        EXPECT_EQ(1 + i % 2, host->getIPv6SubnetID());
        reservations = host->getIPv6Reservations(IPv6Resrv::TYPE_NA);
        ASSERT_EQ(1, std::distance(reservations.first, reservations.second));
        EXPECT_EQ(increase(IOAddress("2001:db8:2::1"), i),
                  reservations.first->second.getPrefix());
    }
}

TEST_F(CfgHostsTest, get6) {
    testGet6();
}

TEST_F(CfgHostsTest, get6MultiThreading) {
    MultiThreadingTest mt(true);
    testGet6();
}

// This test checks that all reservations for the specified IPv6 subnet can
// be deleted.
void
CfgHostsTest::testDeleteAll6() {
    CfgHosts cfg;
    // Add hosts.
    for (unsigned i = 0; i < 25; ++i) {
        // Add host identified by HW address. The subnet for which we're
        // adding the host has id of 1 for even values of i and 2 for
        // odd values of i.
        HostPtr host = HostPtr(new Host(hwaddrs_[i]->toText(false),
                                        "hw-address",
                                        SubnetID(10), SubnetID(1 + i % 2),
                                        IOAddress("0.0.0.0")));
        host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA,
                                       increase(IOAddress("2001:db8:1::1"),
                                                i)));
        cfg.add(host);
    }

    // Delete all hosts for subnet id. There should be 13 of them.
    EXPECT_EQ(13, cfg.delAll6(SubnetID(1)));

    for (unsigned i = 0; i < 25; ++i) {
        // Calculate subnet id for the given i.
        SubnetID subnet_id = 1 + i % 2;

        // Try to retrieve host by HW address.
        HostPtr host = cfg.get6(subnet_id, Host::IDENT_HWADDR,
                                &hwaddrs_[i]->hwaddr_[0],
                                hwaddrs_[i]->hwaddr_.size());
        // The host should exist for subnet id of 2.
        if (subnet_id == 2) {
            ASSERT_TRUE(host);
            EXPECT_EQ(subnet_id, host->getIPv6SubnetID());
            IPv6ResrvRange reservations =
                host->getIPv6Reservations(IPv6Resrv::TYPE_NA);
            ASSERT_EQ(1, std::distance(reservations.first, reservations.second));
            EXPECT_EQ(increase(IOAddress("2001:db8:1::1"), i),
                      reservations.first->second.getPrefix());

        } else {
            // All hosts for subnet id 1 should be gone.
            EXPECT_FALSE(host);
        }
    }
}

TEST_F(CfgHostsTest, deleteAll6) {
    testDeleteAll6();
}

TEST_F(CfgHostsTest, deleteAll6MultiThreading) {
    MultiThreadingTest mt(true);
    testDeleteAll6();
}

// This test checks that the DHCPv6 reservations can be unparsed
void
CfgHostsTest::testUnparse6() {
    CfgMgr::instance().setFamily(AF_INET6);
    CfgHosts cfg;
    CfgHostsList list;
    // Add hosts.
    for (unsigned i = 0; i < 25; ++i) {
        // Add host identified by HW address.
        HostPtr host = HostPtr(new Host(hwaddrs_[i]->toText(false),
                                        "hw-address",
                                        SubnetID(10), SubnetID(1 + i),
                                        IOAddress("0.0.0.0")));
        host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA,
                                       increase(IOAddress("2001:db8:1::1"),
                                                i)));
        cfg.add(host);

        // Add host identified by DUID.
        host = HostPtr(new Host(duids_[i]->toText(), "duid",
                                SubnetID(10), SubnetID(1 + i),
                                IOAddress("0.0.0.0")));
        host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA,
                                       increase(IOAddress("2001:db8:2::1"),
                                                i)));
        cfg.add(host);
    }

    using namespace isc::data;
    ConstElementPtr cfg_unparsed;
    ASSERT_NO_THROW(cfg_unparsed = cfg.toElement());
    ASSERT_NO_THROW(list.internalize(cfg_unparsed));
    for (unsigned i = 0; i < 25; ++i) {
        ConstElementPtr unparsed = list.get(SubnetID(1 + i));
        ASSERT_TRUE(unparsed);
        ASSERT_EQ(Element::list, unparsed->getType());
        EXPECT_EQ(2, unparsed->size());
        ASSERT_NE(0, unparsed->size());

        // Check by HW address entries
        bool checked_hw = false;
        for (unsigned j = 0; j < unparsed->size(); ++j) {
            ConstElementPtr host = unparsed->get(j);
            ASSERT_TRUE(host);
            ASSERT_EQ(Element::map, host->getType());
            if (!host->contains("hw-address")) {
                continue;
            }
            checked_hw = true;
            // Not both hw-address and duid
            EXPECT_FALSE(host->contains("duid"));
            // Check the HW address
            ConstElementPtr hw = host->get("hw-address");
            ASSERT_TRUE(hw);
            ASSERT_EQ(Element::string, hw->getType());
            EXPECT_EQ(hwaddrs_[i]->toText(false), hw->stringValue());
            // Check the reservation
            ConstElementPtr resvs = host->get("ip-addresses");
            ASSERT_TRUE(resvs);
            ASSERT_EQ(Element::list, resvs->getType());
            EXPECT_EQ(1, resvs->size());
            ASSERT_GE(1, resvs->size());
            ConstElementPtr resv = resvs->get(0);
            ASSERT_TRUE(resv);
            ASSERT_EQ(Element::string, resv->getType());
            EXPECT_EQ(increase(IOAddress("2001:db8:1::1"), i),
                          IOAddress(resv->stringValue()));
        }
        ASSERT_TRUE(checked_hw);

        // Check by DUID entries
        bool checked_duid = false;
        for (unsigned j = 0; j < unparsed->size(); ++j) {
            ConstElementPtr host = unparsed->get(j);
            ASSERT_TRUE(host);
            ASSERT_EQ(Element::map, host->getType());
            if (!host->contains("duid")) {
                continue;
            }
            checked_duid = true;
            // Not both hw-address and duid
            EXPECT_FALSE(host->contains("hw-address"));
            // Check the DUID
            ConstElementPtr duid = host->get("duid");
            ASSERT_TRUE(duid);
            ASSERT_EQ(Element::string, duid->getType());
            EXPECT_EQ(duids_[i]->toText(), duid->stringValue());
            // Check the reservation
            ConstElementPtr resvs = host->get("ip-addresses");
            ASSERT_TRUE(resvs);
            ASSERT_EQ(Element::list, resvs->getType());
            EXPECT_EQ(1, resvs->size());
            ASSERT_GE(1, resvs->size());
            ConstElementPtr resv = resvs->get(0);
            ASSERT_TRUE(resv);
            ASSERT_EQ(Element::string, resv->getType());
            EXPECT_EQ(increase(IOAddress("2001:db8:2::1"), i),
                      IOAddress(resv->stringValue()));
        }
        ASSERT_TRUE(checked_duid);
    }
}

TEST_F(CfgHostsTest, unparse6) {
    testUnparse6();
}

TEST_F(CfgHostsTest, unparse6MultiThreading) {
    MultiThreadingTest mt(true);
    testUnparse6();
}

// This test checks that the IPv6 reservations can be retrieved for a particular
// (subnet-id, address) tuple.
void
CfgHostsTest::testGet6ByAddr() {
    CfgHosts cfg;
    // Add hosts.
    for (unsigned i = 0; i < 25; ++i) {

        // Add host identified by DUID.
        HostPtr host = HostPtr(new Host(duids_[i]->toText(), "duid",
                                        SUBNET_ID_UNUSED, SubnetID(1 + i % 2),
                                        IOAddress("0.0.0.0")));
        host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA,
                                       increase(IOAddress("2001:db8:2::1"),
                                                i)));
        cfg.add(host);
    }

    for (unsigned i = 0; i < 25; ++i) {
        // Retrieve host by (subnet-id,address).
        HostPtr host = cfg.get6(SubnetID(1 + i % 2),
                                increase(IOAddress("2001:db8:2::1"), i));
        ASSERT_TRUE(host);

        EXPECT_EQ(1 + i % 2, host->getIPv6SubnetID());
        IPv6ResrvRange reservations =
            host->getIPv6Reservations(IPv6Resrv::TYPE_NA);
        ASSERT_EQ(1, std::distance(reservations.first, reservations.second));
        EXPECT_EQ(increase(IOAddress("2001:db8:2::1"), i),
                  reservations.first->second.getPrefix());
    }
}

TEST_F(CfgHostsTest, get6ByAddr) {
    testGet6ByAddr();
}

TEST_F(CfgHostsTest, get6ByAddrMultiThreading) {
    MultiThreadingTest mt(true);
    testGet6ByAddr();
}

// This test checks that the IPv6 reservations can be retrieved for a particular
// (subnet-id, address) tuple.
void
CfgHostsTest::testGet6MultipleAddrs() {
    CfgHosts cfg;

    // Add 25 hosts. Each host has reservations for 5 addresses.
    for (unsigned i = 0; i < 25; ++i) {

        // Add host identified by DUID.
        HostPtr host = HostPtr(new Host(duids_[i]->toText(), "duid",
                                        SUBNET_ID_UNUSED, SubnetID(1 + i % 2),
                                        IOAddress("0.0.0.0")));

        // Generate 5 unique addresses for this host.
        for (unsigned j = 0; j < 5; ++j) {
            std::stringstream address_stream;
            address_stream << "2001:db8:" << i << "::" << j;
            host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA,
                                           address_stream.str()));
        }
        cfg.add(host);
    }

    // Now check if we can retrieve each of those 25 hosts by using each
    // of their addresses.
    for (unsigned i = 0; i < 25; ++i) {

        // Check that the host is there.
        HostPtr by_duid = cfg.get6(SubnetID(1 + i % 2), Host::IDENT_DUID,
                                   &duids_[i]->getDuid()[0],
                                   duids_[i]->getDuid().size());
        ASSERT_TRUE(by_duid);

        for (unsigned j = 0; j < 5; ++j) {
            std::stringstream address_stream;
            address_stream << "2001:db8:" << i << "::" << j;

            // Retrieve host by (subnet-id,address).
            HostPtr by_addr = cfg.get6(SubnetID(1 + i % 2),
                                       address_stream.str());
            ASSERT_TRUE(by_addr);

            // The pointers should match. Maybe we should compare contents
            // rather than just pointers? I think there's no reason why
            // the code would make any copies of the Host object, so
            // the pointers should always point to the same object.
            EXPECT_EQ(by_duid, by_addr);
        }
    }
}

TEST_F(CfgHostsTest, get6MultipleAddrs) {
    testGet6MultipleAddrs();
}

TEST_F(CfgHostsTest, get6MultipleAddrsMultiThreading) {
    MultiThreadingTest mt(true);
    testGet6MultipleAddrs();
}

// Checks that it's not possible for a second host to reserve an address
// which is already reserved.
void
CfgHostsTest::testAdd4AlreadyReserved() {
    CfgHosts cfg;

    // First host has a reservation for address 192.0.2.1
    HostPtr host1 = HostPtr(new Host(hwaddrs_[0]->toText(false),
                                     "hw-address",
                                     SubnetID(1), SubnetID(SUBNET_ID_UNUSED),
                                     IOAddress("192.0.2.1")));
    // Adding this should work.
    EXPECT_NO_THROW(cfg.add(host1));

    // The second host has a reservation for the same address.
    HostPtr host2 = HostPtr(new Host(hwaddrs_[1]->toText(false),
                                     "hw-address",
                                     SubnetID(1), SUBNET_ID_UNUSED,
                                     IOAddress("192.0.2.1")));

    // This second host has a reservation for an address that is already
    // reserved for the first host, so it should be rejected.
    EXPECT_THROW(cfg.add(host2), isc::dhcp::ReservedAddress);
}

TEST_F(CfgHostsTest, add4AlreadyReserved) {
    testAdd4AlreadyReserved();
}

TEST_F(CfgHostsTest, add4AlreadyReservedMultiThreading) {
    MultiThreadingTest mt(true);
    testAdd4AlreadyReserved();
}

// Test that it is possible to allow inserting multiple reservations for
// the same IP address.
void
CfgHostsTest::testAllow4AlreadyReserved() {
    CfgHosts cfg;
    // Allow creating multiple reservations for the same IP address.
    ASSERT_TRUE(cfg.setIPReservationsUnique(false));

    // First host has a reservation for address 192.0.2.1
    HostPtr host1 = HostPtr(new Host(hwaddrs_[0]->toText(false),
                                     "hw-address",
                                     SubnetID(1), SubnetID(SUBNET_ID_UNUSED),
                                     IOAddress("192.0.2.1")));
    ASSERT_NO_THROW(cfg.add(host1));

    // The second host has a reservation for the same address.
    HostPtr host2 = HostPtr(new Host(hwaddrs_[1]->toText(false),
                                     "hw-address",
                                     SubnetID(1), SUBNET_ID_UNUSED,
                                     IOAddress("192.0.2.1")));
    // Adding this should work because the HW address is different.
    ASSERT_NO_THROW(cfg.add(host2));

    // Get both hosts.
    ConstHostCollection returned;
    ASSERT_NO_THROW(returned = cfg.getAll4(host1->getIPv4SubnetID(), IOAddress("192.0.2.1")));
    EXPECT_EQ(2, returned.size());

    // Make sure the address is the same but the identifiers are different.
    EXPECT_NE(returned[0]->getIdentifierAsText(), returned[1]->getIdentifierAsText());
    EXPECT_EQ(returned[0]->getIPv4Reservation().toText(),
              returned[1]->getIPv4Reservation().toText());
}

TEST_F(CfgHostsTest, allow4AlreadyReserved) {
    testAllow4AlreadyReserved();
}

TEST_F(CfgHostsTest, allow4AlreadyReservedMultiThreading) {
    MultiThreadingTest mt(true);
    testAllow4AlreadyReserved();
}

// Checks that it's not possible for two hosts to have the same address
// reserved at the same time.
void
CfgHostsTest::testAdd6Invalid2Hosts() {
    CfgHosts cfg;

    // First host has a reservation for address 2001:db8::1
    HostPtr host1 = HostPtr(new Host(duids_[0]->toText(), "duid",
                                     SUBNET_ID_UNUSED, SubnetID(1),
                                     IOAddress("0.0.0.0")));
    host1->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA,
                                    IOAddress("2001:db8::1")));
    // Adding this should work.
    EXPECT_NO_THROW(cfg.add(host1));

    // The second host has a reservation for the same address.
    HostPtr host2 = HostPtr(new Host(duids_[1]->toText(), "duid",
                                     SUBNET_ID_UNUSED, SubnetID(1),
                                     IOAddress("0.0.0.0")));
    host2->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA,
                                    IOAddress("2001:db8::1")));

    // This second host has a reservation for an address that is already
    // reserved for the first host, so it should be rejected.
    EXPECT_THROW(cfg.add(host2), isc::dhcp::DuplicateHost);
}

TEST_F(CfgHostsTest, add6Invalid2Hosts) {
    testAdd6Invalid2Hosts();
}

TEST_F(CfgHostsTest, add6Invalid2HostsMultiThreading) {
    MultiThreadingTest mt(true);
    testAdd6Invalid2Hosts();
}

// Test that it is possible to allow inserting multiple reservations for
// the same IPv6 address.
void
CfgHostsTest::testAllowAddress6AlreadyReserved() {
    CfgHosts cfg;
    // Allow creating multiple reservations for the same IP address.
    ASSERT_TRUE(cfg.setIPReservationsUnique(false));

    // First host has a reservation for address 2001:db8::1
    HostPtr host1 = HostPtr(new Host(duids_[0]->toText(), "duid",
                                     SUBNET_ID_UNUSED, SubnetID(1),
                                     IOAddress("0.0.0.0")));
    host1->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA,
                                    IOAddress("2001:db8::1")));
    // Adding this should work.
    EXPECT_NO_THROW(cfg.add(host1));

    // The second host has a reservation for the same address.
    HostPtr host2 = HostPtr(new Host(duids_[1]->toText(), "duid",
                                     SUBNET_ID_UNUSED, SubnetID(1),
                                     IOAddress("0.0.0.0")));
    host2->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA,
                                    IOAddress("2001:db8::1")));

    // Adding this should work because the DUID is different.
    ASSERT_NO_THROW(cfg.add(host2));

    ConstHostCollection returned;
    ASSERT_NO_THROW(returned = cfg.getAll6(host1->getIPv6SubnetID(), IOAddress("2001:db8::1")));
    EXPECT_EQ(2, returned.size());

    // Make sure the address is the same but the identifiers are different.
    EXPECT_NE(returned[0]->getIdentifierAsText(), returned[1]->getIdentifierAsText());

    auto range0 = returned[0]->getIPv6Reservations(IPv6Resrv::TYPE_NA);
    EXPECT_EQ(1, std::distance(range0.first, range0.second));
    auto range1 = returned[1]->getIPv6Reservations(IPv6Resrv::TYPE_NA);
    EXPECT_EQ(1, std::distance(range1.first, range1.second));
    EXPECT_EQ(range0.first->second.getPrefix().toText(),
              range1.first->second.getPrefix().toText());
}

TEST_F(CfgHostsTest, allowAddress6AlreadyReserved) {
    testAllowAddress6AlreadyReserved();
}

TEST_F(CfgHostsTest, allowAddress6AlreadyReservedMultiThreading) {
    MultiThreadingTest mt(true);
    testAllowAddress6AlreadyReserved();
}

// Test that it is possible to allow inserting multiple reservations for
// the same IPv6 delegated prefix.
void
CfgHostsTest::testAllowPrefix6AlreadyReserved() {
    CfgHosts cfg;
    // Allow creating multiple reservations for the same delegated prefix.
    ASSERT_TRUE(cfg.setIPReservationsUnique(false));

    // First host has a reservation for prefix 3000::/64.
    HostPtr host1 = HostPtr(new Host(duids_[0]->toText(), "duid",
                                     SUBNET_ID_UNUSED, SubnetID(1),
                                     IOAddress("0.0.0.0")));
    host1->addReservation(IPv6Resrv(IPv6Resrv::TYPE_PD,
                                    IOAddress("3000::"), 64));
    // Adding this should work.
    EXPECT_NO_THROW(cfg.add(host1));

    // The second host has a reservation for the same prefix.
    HostPtr host2 = HostPtr(new Host(duids_[1]->toText(), "duid",
                                     SUBNET_ID_UNUSED, SubnetID(1),
                                     IOAddress("0.0.0.0")));
    host2->addReservation(IPv6Resrv(IPv6Resrv::TYPE_PD,
                                    IOAddress("3000::"), 64));

    // Adding this should work because the DUID is different.
    ASSERT_NO_THROW(cfg.add(host2));

    ConstHostCollection returned;
    ASSERT_NO_THROW(returned = cfg.getAll6(host1->getIPv6SubnetID(), IOAddress("3000::")));
    EXPECT_EQ(2, returned.size());

    // Make sure the prefix is the same but the identifiers are different.
    EXPECT_NE(returned[0]->getIdentifierAsText(), returned[1]->getIdentifierAsText());

    auto range0 = returned[0]->getIPv6Reservations(IPv6Resrv::TYPE_PD);
    EXPECT_EQ(1, std::distance(range0.first, range0.second));
    auto range1 = returned[1]->getIPv6Reservations(IPv6Resrv::TYPE_PD);
    EXPECT_EQ(1, std::distance(range1.first, range1.second));
    EXPECT_EQ(range0.first->second.getPrefix().toText(),
              range1.first->second.getPrefix().toText());
}

TEST_F(CfgHostsTest, allowPrefix6AlreadyReserved) {
    testAllowPrefix6AlreadyReserved();
}

TEST_F(CfgHostsTest, allowPrefix6AlreadyReservedMultiThreading) {
    MultiThreadingTest mt(true);
    testAllowPrefix6AlreadyReserved();
}

// Check that no error is reported when adding a host with subnet
// ids equal to global.
TEST_F(CfgHostsTest, globalSubnetIDs) {
    CfgHosts cfg;
    ASSERT_NO_THROW(cfg.add(HostPtr(new Host(hwaddrs_[0]->toText(false),
                                             "hw-address",
                                             SUBNET_ID_GLOBAL, SUBNET_ID_GLOBAL,
                                             IOAddress("10.0.0.1")))));
}


// Check that error is reported when trying to add a host with subnet
// ids equal to unused.
TEST_F(CfgHostsTest, unusedSubnetIDs) {
    CfgHosts cfg;
    ASSERT_THROW(cfg.add(HostPtr(new Host(hwaddrs_[0]->toText(false),
                                          "hw-address",
                                          SUBNET_ID_UNUSED, SUBNET_ID_UNUSED,
                                          IOAddress("10.0.0.1")))),
                 isc::BadValue);
}
// Test insert and retrieve a v6 host with prefix exclude option.
TEST_F(CfgHostsTest, prefixExclude) {
    CfgHosts cfg;

    // Create a host reservation.
    HostPtr host = HostPtr(new Host(duids_[0]->toText(), "duid",
                                    SUBNET_ID_UNUSED, SubnetID(1),
                                    IOAddress("0.0.0.0")));
    IPv6Resrv resv(IPv6Resrv::TYPE_PD, IOAddress("2001:db8::"), 48);
    resv.setPDExclude(IOAddress("2001:db8:0:1::"), 64);
    host->addReservation(resv);
    SubnetID subnet = host->getIPv6SubnetID();

    // Try to add it to the host config.
    ASSERT_NO_THROW(cfg.add(host));

    // Retrieve it.
    ConstHostPtr from_cfg = cfg.get6(subnet, Host::IDENT_DUID,
                                     &host->getIdentifier()[0],
                                     host->getIdentifier().size());
    ASSERT_TRUE(from_cfg);

    // Compare host and from_cfg.
    EXPECT_EQ(host->toText(), from_cfg->toText());
}

// This test verifies that it is not possible to add the same Host to the
// same IPv4 subnet twice.
void
CfgHostsTest::testDuplicatesSubnet4HWAddr() {
    CfgHosts cfg;
    // Add a host.
    ASSERT_NO_THROW(cfg.add(HostPtr(new Host(hwaddrs_[0]->toText(false),
                                             "hw-address",
                                             SubnetID(10), SUBNET_ID_UNUSED,
                                             IOAddress("10.0.0.1")))));

    // Try to add the host with the same HW address to the same subnet. The fact
    // that the IP address is different here shouldn't really matter.
    EXPECT_THROW(cfg.add(HostPtr(new Host(hwaddrs_[0]->toText(false),
                                          "hw-address",
                                          SubnetID(10), SUBNET_ID_UNUSED,
                                          IOAddress("10.0.0.10")))),
                 isc::dhcp::DuplicateHost);

    // Now try to add it to a different subnet. It should go through.
    EXPECT_NO_THROW(cfg.add(HostPtr(new Host(hwaddrs_[0]->toText(false),
                                             "hw-address",
                                             SubnetID(11), SUBNET_ID_UNUSED,
                                             IOAddress("10.0.0.10")))));
}

TEST_F(CfgHostsTest, duplicatesSubnet4HWAddr) {
    testDuplicatesSubnet4HWAddr();
}

TEST_F(CfgHostsTest, duplicatesSubnet4HWAddrMultiThreading) {
    MultiThreadingTest mt(true);
    testDuplicatesSubnet4HWAddr();
}

// This test verifies that it is not possible to add the same Host to the
// same IPv4 subnet twice.
void
CfgHostsTest::testDuplicatesSubnet4DUID() {
    CfgHosts cfg;
    // Add a host.
    ASSERT_NO_THROW(cfg.add(HostPtr(new Host(duids_[0]->toText(),
                                             "duid",
                                             SubnetID(10), SUBNET_ID_UNUSED,
                                             IOAddress("10.0.0.1")))));

    // Try to add the host with the same DUID to the same subnet. The fact
    // that the IP address is different here shouldn't really matter.
    EXPECT_THROW(cfg.add(HostPtr(new Host(duids_[0]->toText(),
                                          "duid",
                                          SubnetID(10), SUBNET_ID_UNUSED,
                                          IOAddress("10.0.0.10")))),
                 isc::dhcp::DuplicateHost);

    // Now try to add it to a different subnet. It should go through.
    EXPECT_NO_THROW(cfg.add(HostPtr(new Host(duids_[0]->toText(),
                                             "duid",
                                             SubnetID(11), SUBNET_ID_UNUSED,
                                             IOAddress("10.0.0.10")))));
}

TEST_F(CfgHostsTest, duplicatesSubnet4DUID) {
    testDuplicatesSubnet4DUID();
}

TEST_F(CfgHostsTest, duplicatesSubnet4DUIDMultiThreading) {
    MultiThreadingTest mt(true);
    testDuplicatesSubnet4DUID();
}

// This test verifies that it is not possible to add the same Host to the
// same IPv6 subnet twice.
void
CfgHostsTest::testDuplicatesSubnet6HWAddr() {
    CfgHosts cfg;
    // Add a host.
    ASSERT_NO_THROW(cfg.add(HostPtr(new Host(hwaddrs_[0]->toText(false),
                                             "hw-address",
                                             SUBNET_ID_UNUSED, SubnetID(1),
                                             IOAddress("0.0.0.0"),
                                             "foo.example.com"))));

    // Try to add the host with the same HW address to the same subnet. The fact
    // that the IP address is different here shouldn't really matter.
    EXPECT_THROW(cfg.add(HostPtr(new Host(hwaddrs_[0]->toText(false),
                                          "hw-address",
                                          SUBNET_ID_UNUSED, SubnetID(1),
                                          IOAddress("0.0.0.0"),
                                          "foo.example.com"))),
                 isc::dhcp::DuplicateHost);

    // Now try to add it to a different subnet. It should go through.
    EXPECT_NO_THROW(cfg.add(HostPtr(new Host(hwaddrs_[0]->toText(false),
                                             "hw-address",
                                             SUBNET_ID_UNUSED, SubnetID(2),
                                             IOAddress("0.0.0.0"),
                                             "foo.example.com"))));
}

TEST_F(CfgHostsTest, duplicatesSubnet6HWAddr) {
    testDuplicatesSubnet6HWAddr();
}

TEST_F(CfgHostsTest, duplicatesSubnet6HWAddrMultiThreading) {
    MultiThreadingTest mt(true);
    testDuplicatesSubnet6HWAddr();
}

// This test verifies that it is not possible to add the same Host to the
// same IPv6 subnet twice.
void
CfgHostsTest::testDuplicatesSubnet6DUID() {
    CfgHosts cfg;
    // Add a host.
    ASSERT_NO_THROW(cfg.add(HostPtr(new Host(duids_[0]->toText(),
                                             "duid",
                                             SUBNET_ID_UNUSED, SubnetID(1),
                                             IOAddress("0.0.0.0"),
                                             "foo.example.com"))));

    // Try to add the host with the same DUID to the same subnet. The fact
    // that the IP address is different here shouldn't really matter.
    EXPECT_THROW(cfg.add(HostPtr(new Host(duids_[0]->toText(),
                                          "duid",
                                          SUBNET_ID_UNUSED, SubnetID(1),
                                          IOAddress("0.0.0.0"),
                                          "foo.example.com"))),
                 isc::dhcp::DuplicateHost);

    // Now try to add it to a different subnet. It should go through.
    EXPECT_NO_THROW(cfg.add(HostPtr(new Host(duids_[0]->toText(),
                                             "duid",
                                             SUBNET_ID_UNUSED, SubnetID(2),
                                             IOAddress("0.0.0.0"),
                                             "foo.example.com"))));
}

TEST_F(CfgHostsTest, duplicatesSubnet6DUID) {
    testDuplicatesSubnet6DUID();
}

TEST_F(CfgHostsTest, duplicatesSubnet6DUIDMultiThreading) {
    MultiThreadingTest mt(true);
    testDuplicatesSubnet6DUID();
}

// Checks that updates work correctly.
void
CfgHostsTest::testUpdate() {
    CfgHosts cfg;

    HostPtr const host(boost::make_shared<Host>(duids_[0]->toText(), "duid", SUBNET_ID_UNUSED,
                                                SubnetID(1), IOAddress("0.0.0.0"),
                                                "foo.example.com"));

    // Updating a host that doesn't exist should throw.
    EXPECT_THROW_MSG(cfg.update(host), HostNotFound, "Host not updated (not found).");

    // There should be no hosts.
    HostCollection hosts(cfg.getAll6(SubnetID(1)));
    EXPECT_EQ(0, hosts.size());

    // Add a host.
    EXPECT_NO_THROW(cfg.add(host));

    // The host should be in the config.
    hosts = cfg.getAll6(SubnetID(1));
    ASSERT_EQ(1, hosts.size());
    EXPECT_EQ("duid=010203040500 ipv6_subnet_id=1 hostname=foo.example.com "
              "ipv4_reservation=(no) siaddr=(no) sname=(empty) file=(empty) "
              "key=(empty) ipv6_reservations=(none)", hosts[0]->toText());

    // Update the host. Change nothing.
    EXPECT_NO_THROW(cfg.update(host));

    // The same host should be in the config.
    hosts = cfg.getAll6(SubnetID(1));
    ASSERT_EQ(1, hosts.size());
    EXPECT_EQ("duid=010203040500 ipv6_subnet_id=1 hostname=foo.example.com "
              "ipv4_reservation=(no) siaddr=(no) sname=(empty) file=(empty) "
              "key=(empty) ipv6_reservations=(none)", hosts[0]->toText());

    // Update the host with new hostname.
    host->setHostname("bar.example.com");
    EXPECT_NO_THROW(cfg.update(host));

    // The change should be reflected in the config.
    hosts = cfg.getAll6(SubnetID(1));
    ASSERT_EQ(1, hosts.size());
    EXPECT_EQ("duid=010203040500 ipv6_subnet_id=1 hostname=bar.example.com "
              "ipv4_reservation=(no) siaddr=(no) sname=(empty) file=(empty) "
              "key=(empty) ipv6_reservations=(none)", hosts[0]->toText());

    // Remove hostname from host.
    host->setHostname("");
    EXPECT_NO_THROW(cfg.update(host));

    // The change should be reflected in the config.
    hosts = cfg.getAll6(SubnetID(1));
    ASSERT_EQ(1, hosts.size());
    EXPECT_EQ("duid=010203040500 ipv6_subnet_id=1 hostname=(empty) "
              "ipv4_reservation=(no) siaddr=(no) sname=(empty) file=(empty) "
              "key=(empty) ipv6_reservations=(none)", hosts[0]->toText());
}

TEST_F(CfgHostsTest, update) {
    testUpdate();
}

TEST_F(CfgHostsTest, updateMultiThreading) {
    MultiThreadingTest mt(true);
    testUpdate();
}

}  // namespace