1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
// Copyright (C) 2013-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_ddns/ncr_msg.h>
#include <dhcp/dhcp6.h>
#include <dhcp/option.h>
#include <dhcp/option_custom.h>
#include <dhcp/option6_client_fqdn.h>
#include <dhcp/option6_ia.h>
#include <dhcp/option6_iaaddr.h>
#include <dhcp/option6_status_code.h>
#include <dhcp/option_int_array.h>
#include <dhcp/testutils/iface_mgr_test_config.h>
#include <dhcpsrv/lease.h>
#include <dhcpsrv/lease_mgr_factory.h>
#include <dhcpsrv/ncr_generator.h>
#include <dhcp6/tests/dhcp6_client.h>
#include <dhcp6/tests/dhcp6_test_utils.h>

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

using namespace isc;
using namespace isc::dhcp::test;
using namespace isc::asiolink;
using namespace isc::dhcp;
using namespace isc::dhcp_ddns;
using namespace isc::util;
using namespace isc::hooks;
using namespace std;

namespace {

/// @brief A test fixture class for testing DHCPv6 Client FQDN Option handling.
class FqdnDhcpv6SrvTest : public Dhcpv6SrvTest {
public:
    // Bit Constants for turning on and off DDNS configuration options.
    // (Defined here as these are only meaningful to this class.)
    static const uint16_t OVERRIDE_NO_UPDATE = 1;
    static const uint16_t OVERRIDE_CLIENT_UPDATE = 2;
    static const uint16_t REPLACE_CLIENT_NAME = 4;

    // Enum used to specify whether a client (packet) should include
    // the hostname option
    enum ClientNameFlag {
        CLIENT_NAME_PRESENT,
        CLIENT_NAME_NOT_PRESENT
    };

    // Enum used to specify whether the server should replace/supply
    // the hostname or not
    enum ReplacementFlag {
        NAME_REPLACED,
        NAME_NOT_REPLACED
    };

    // Type used to indicate whether or not forward updates are expected
    struct ExpFwd {
        ExpFwd(bool exp_fwd) : value_(exp_fwd){};
        bool value_;
    };

    // Type used to indicate whether or not reverse updates are expected
    struct ExpRev {
        ExpRev(bool exp_rev) : value_(exp_rev){};
        bool value_;
    };

    /// @brief Constructor
    FqdnDhcpv6SrvTest()
        : Dhcpv6SrvTest(), srv_(new NakedDhcpv6Srv(0)),
          d2_mgr_(CfgMgr::instance().getD2ClientMgr()),
          iface_mgr_test_config_(true) {
        // generateClientId assigns DUID to duid_.
        generateClientId();
        lease_.reset(new Lease6(Lease::TYPE_NA, IOAddress("2001:db8:1::1"),
                                duid_, 1234, 501, 502, 1, HWAddrPtr()));
        // Config DDNS to be enabled, all controls off
        enableD2();
    }

    /// @brief Destructor
    virtual ~FqdnDhcpv6SrvTest() {
        // Default constructor creates a config with DHCP-DDNS updates
        // disabled.
        D2ClientConfigPtr cfg(new D2ClientConfig());
        CfgMgr::instance().setD2ClientConfig(cfg);
    }

    /// @brief Sets the server's DDNS configuration to ddns updates disabled.
    void disableD2() {
        // Default constructor creates a config with DHCP-DDNS updates
        // disabled.
        D2ClientConfigPtr cfg(new D2ClientConfig());
        CfgMgr::instance().setD2ClientConfig(cfg);
    }

    /// @brief Enables DHCP-DDNS updates with the given options enabled.
    ///
    /// Replaces the current D2ClientConfiguration with a configuration
    /// which as updates enabled and the control options set based upon
    /// the bit mask of options.
    ///
    /// @param mask Bit mask of configuration options that should be enabled.
    void enableD2(const uint16_t mask = 0) {
        D2ClientConfigPtr cfg;

        ASSERT_NO_THROW(cfg.reset(new D2ClientConfig(true,
                                  isc::asiolink::IOAddress("::1"), 53001,
                                  isc::asiolink::IOAddress("::"), 0,
                                  1024, dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON)));

        ASSERT_NO_THROW(CfgMgr::instance().setD2ClientConfig(cfg));

        // Now we'll set the DDNS parameters at the subnet level.
        // These should get fetched when getDdnsParams() is invoked.
        ASSERT_TRUE(subnet_) << "enableD2 called without subnet_ set";
        subnet_->setDdnsSendUpdates(true);
        subnet_->setDdnsOverrideNoUpdate(mask & OVERRIDE_NO_UPDATE);
        subnet_->setDdnsOverrideClientUpdate(mask & OVERRIDE_CLIENT_UPDATE);
        subnet_->setDdnsReplaceClientNameMode((mask & REPLACE_CLIENT_NAME) ?
                                              D2ClientConfig::RCM_WHEN_PRESENT :
                                              D2ClientConfig::RCM_NEVER);
        subnet_->setDdnsGeneratedPrefix("myhost");
        subnet_->setDdnsQualifyingSuffix("example.com");
        // In V6 the dot (".") can, and equally can not be specified in the
        // "hostname-char-set" because the FQDN is split before any character
        // is replaced and then is put back together.
        subnet_->setHostnameCharSet("[^A-Za-z0-9.-]");
        subnet_->setHostnameCharReplacement("x");
        subnet_->setDdnsConflictResolutionMode("check-with-dhcid");

        ASSERT_NO_THROW(srv_->startD2());
    }

    // Fetch DDNS parameter set scoped to the current subnet_.
    DdnsParamsPtr getDdnsParams() {
        ConstElementPtr cfg = CfgMgr::instance().getCurrentCfg()->toElement();
        if (!subnet_) {
            ADD_FAILURE() << "getDdnsParams() - subnet_ is empty!";
            return (DdnsParamsPtr(new DdnsParams()));
        }

        return(CfgMgr::instance().getCurrentCfg()->getDdnsParams(subnet_));
    }

    /// @brief Construct the DHCPv6 Client FQDN option using flags and
    /// domain-name.
    ///
    /// @param flags Flags to be set for the created option.
    /// @param fqdn_name A name which should be stored in the option.
    /// @param fqdn_type A type of the name carried by the option: partial
    /// or fully qualified.
    ///
    /// @return A pointer to the created option.
    Option6ClientFqdnPtr
    createClientFqdn(const uint8_t flags,
                     const std::string& fqdn_name,
                     const Option6ClientFqdn::DomainNameType fqdn_type) {
        return (Option6ClientFqdnPtr(new Option6ClientFqdn(flags,
                                                           fqdn_name,
                                                           fqdn_type)));
    }

    /// @brief Create a message with or without DHCPv6 Client FQDN Option.
    ///
    /// @param msg_type A type of the DHCPv6 message to be created.
    /// @param fqdn_flags Flags to be carried in the FQDN option.
    /// @param fqdn_domain_name A name to be carried in the FQDN option.
    /// @param fqdn_type A type of the name carried by the option: partial
    /// or fully qualified.
    /// @param include_oro A boolean value which indicates whether the ORO
    /// option should be added to the message (if true).
    /// @param srvid server id to be stored in the message.
    ///
    /// @return An object representing the created message.
    Pkt6Ptr generateMessage(uint8_t msg_type,
                            const uint8_t fqdn_flags,
                            const std::string& fqdn_domain_name,
                            const Option6ClientFqdn::DomainNameType
                            fqdn_type,
                            const bool include_oro,
                            OptionPtr srvid = OptionPtr()) {
        Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(msg_type, 1234));
        pkt->setIface("eth0");
        pkt->setIndex(ETH0_INDEX);
        Option6IAPtr ia = generateIA(D6O_IA_NA, 234, 1500, 3000);
        if (msg_type != DHCPV6_REPLY) {
            IOAddress hint("2001:db8:1:1::dead:beef");
            OptionPtr hint_opt(new Option6IAAddr(D6O_IAADDR, hint, 300, 500));
            ia->addOption(hint_opt);
            pkt->addOption(ia);
        }

        OptionPtr clientid = generateClientId();
        pkt->addOption(clientid);
        if (srvid && (msg_type != DHCPV6_SOLICIT)) {
            pkt->addOption(srvid);
        }

        pkt->addOption(createClientFqdn(fqdn_flags, fqdn_domain_name,
                                            fqdn_type));

        if (include_oro) {
            OptionUint16ArrayPtr oro(new OptionUint16Array(Option::V6,
                                                           D6O_ORO));
            oro->addValue(D6O_CLIENT_FQDN);
            pkt->addOption(oro);
        }

        return (pkt);
    }

    /// @brief Creates instance of the DHCPv6 message with client id and
    /// server id.
    ///
    /// @param msg_type A type of the message to be created.
    ///
    /// @return An object representing the created message.
    Pkt6Ptr generateMessageWithIds(const uint8_t msg_type) {
        Pkt6Ptr pkt = Pkt6Ptr(new Pkt6(msg_type, 1234));
        pkt->setIface("eth0");
        pkt->setIndex(ETH0_INDEX);
        // Generate client-id.
        OptionPtr opt_clientid = generateClientId();
        pkt->addOption(opt_clientid);

        if (msg_type != DHCPV6_SOLICIT) {
            // Generate server-id.
            pkt->addOption(srv_->getServerID());
        }

        return (pkt);
    }

    /// @brief Returns an instance of the option carrying FQDN.
    ///
    /// @param pkt A message holding FQDN option to be returned.
    ///
    /// @return An object representing DHCPv6 Client FQDN option.
    Option6ClientFqdnPtr getClientFqdnOption(const Pkt6Ptr& pkt) {
        return (boost::dynamic_pointer_cast<Option6ClientFqdn>
                (pkt->getOption(D6O_CLIENT_FQDN)));
    }

    /// @brief Adds IA option to the message.
    ///
    /// Added option holds an address.
    ///
    /// @param iaid IAID
    /// @param pkt A DHCPv6 message to which the IA option should be added.
    /// @param cxt allocation engine context to which IA option should be 
    /// added. 
    void addIA(const uint32_t iaid, const IOAddress& addr, Pkt6Ptr& pkt,
               AllocEngine::ClientContext6& ctx) {
        Option6IAPtr opt_ia = generateIA(D6O_IA_NA, iaid, 1500, 3000);
        Option6IAAddrPtr opt_iaaddr(new Option6IAAddr(D6O_IAADDR, addr,
                                                      300, 500));
        opt_ia->addOption(opt_iaaddr);
        pkt->addOption(opt_ia);
        ctx.createIAContext();
        ctx.currentIA().ia_rsp_ = opt_ia;
    }

    /// @brief Adds IA option to the message.
    ///
    /// Added option holds status code.
    ///
    /// @param iaid IAID
    /// @param status_code Status code
    /// @param pkt A DHCPv6 message to which the option should be added.
    /// @param cxt allocation engine context to which IA option should be 
    /// added. 
    void addIA(const uint32_t iaid, const uint16_t status_code, Pkt6Ptr& pkt,
               AllocEngine::ClientContext6& ctx) {
        Option6IAPtr opt_ia = generateIA(D6O_IA_NA, iaid, 1500, 3000);
        addStatusCode(status_code, "", opt_ia);
        pkt->addOption(opt_ia);
        ctx.createIAContext();
        ctx.currentIA().ia_rsp_ = opt_ia;
    }

    /// @brief Creates status code with the specified code and message.
    ///
    /// @param code A status code.
    /// @param msg A string representation of the message to be added to the
    /// Status Code option.
    ///
    /// @return An object representing the Status Code option.
    Option6StatusCodePtr createStatusCode(const uint16_t code,
                                     const std::string& msg) {
        Option6StatusCodePtr opt_status(new Option6StatusCode(code, msg));
        return (opt_status);
    }

    /// @brief Adds Status Code option to the IA.
    ///
    /// @param code A status code value.
    /// @param msg A string representation of the message to be added to the
    /// Status Code option.
    void addStatusCode(const uint16_t code, const std::string& msg,
                       Option6IAPtr& opt_ia) {
        opt_ia->addOption(createStatusCode(code, msg));
    }

    /// @brief Verifies if the DHCPv6 server processes DHCPv6 Client FQDN option
    /// as expected and subsequent interpretation of this processing when
    /// creating NCRs, if any should be created.
    ///
    /// This function simulates generation of the client's message holding FQDN.
    /// It then calls the server's @c Dhcpv6Srv::processClientFqdn option to
    /// generate server's response to the FQDN. This function returns the FQDN
    /// which should be appended to the server's response to the client.
    /// This function verifies that the FQDN option returned is correct
    /// Optionally, this function then proceeds to call createNameChangeRequests
    /// to verify if that method interprets the FQDN information properly.
    ///
    /// @param msg_type A type of the client's message.
    /// @param in_flags A value of flags field to be set for the FQDN carried
    /// in the client's message.
    /// @param in_domain_name A domain name to be carried in the client's FQDN
    /// option.
    /// @param in_domain_type A type of the domain name to be carried in the
    /// client's FQDM option (partial or fully qualified).
    /// @param exp_flags A value of flags expected in the FQDN sent by a server.
    /// @param exp_domain_name A domain name expected in the FQDN sent by a
    /// server.
    /// @param create_ncr_check if true, calls createNameChangeRequests method
    /// and tests the outcome.
    /// @param exp_fwd indicates whether or not a forward change is expected
    /// @param exp_fwd indicates whether or not a reverse change is expected
    void testFqdn(const uint16_t msg_type,
                  const uint8_t in_flags,
                  const std::string& in_domain_name,
                  const Option6ClientFqdn::DomainNameType in_domain_type,
                  const uint8_t exp_flags,
                  const std::string& exp_domain_name,
                  const bool create_ncr_check,
                  const ExpFwd& exp_fwd = ExpFwd(true),
                  const ExpRev& exp_rev = ExpRev(true)) {

        Pkt6Ptr question = generateMessage(msg_type,
                                           in_flags,
                                           in_domain_name,
                                           in_domain_type,
                                           true);

        ASSERT_TRUE(getClientFqdnOption(question));

        Pkt6Ptr answer = generateMessageWithIds(msg_type == DHCPV6_SOLICIT
                                                ? DHCPV6_ADVERTISE :
                                                DHCPV6_REPLY);

        AllocEngine::ClientContext6 ctx;
        // Set the selected subnet so ddns params get returned correctly.
        ctx.subnet_ = subnet_;

        // Create three IAs, each having different address.
        addIA(1234, IOAddress("2001:db8:1::1"), answer, ctx);

        ASSERT_NO_THROW(srv_->processClientFqdn(question, answer, ctx));
        Option6ClientFqdnPtr answ_fqdn = boost::dynamic_pointer_cast<
            Option6ClientFqdn>(answer->getOption(D6O_CLIENT_FQDN));
        ASSERT_TRUE(answ_fqdn);

        const bool flag_n = (exp_flags & Option6ClientFqdn::FLAG_N) != 0;
        const bool flag_s = (exp_flags & Option6ClientFqdn::FLAG_S) != 0;
        const bool flag_o = (exp_flags & Option6ClientFqdn::FLAG_O) != 0;

        EXPECT_EQ(flag_n, answ_fqdn->getFlag(Option6ClientFqdn::FLAG_N));
        EXPECT_EQ(flag_s, answ_fqdn->getFlag(Option6ClientFqdn::FLAG_S));
        EXPECT_EQ(flag_o, answ_fqdn->getFlag(Option6ClientFqdn::FLAG_O));

        EXPECT_EQ(exp_domain_name, answ_fqdn->getDomainName());
        // If server is configured to generate full FQDN for a client, and/or
        // client sent empty FQDN the expected result of the processing by
        // processClientFqdn is an empty, partial FQDN. This is an indication
        // for the code which performs lease allocation that the FQDN has to
        // be generated from the lease address.
        if (exp_domain_name.empty()) {
            EXPECT_EQ(Option6ClientFqdn::PARTIAL,
                      answ_fqdn->getDomainNameType());

        } else {
            EXPECT_EQ(Option6ClientFqdn::FULL, answ_fqdn->getDomainNameType());

        }

        if (create_ncr_check) {
            // Context flags are normally set during lease allocation. Since that
            // hasn't occurred we'll set them here to match the expected values.
            // Call createNameChangeRequests
            ctx.fwd_dns_update_ = exp_fwd.value_;
            ctx.rev_dns_update_ = exp_rev.value_;
            ASSERT_NO_THROW(srv_->createNameChangeRequests(answer, ctx));
            if (exp_fwd.value_ || exp_rev.value_) {
                // Should have created 1 NCR.
                NameChangeRequestPtr ncr;
                ASSERT_EQ(1, d2_mgr_.getQueueSize());
                ASSERT_NO_THROW(ncr = d2_mgr_.peekAt(0));
                ASSERT_TRUE(ncr);
                EXPECT_EQ(dhcp_ddns::CHG_ADD, ncr->getChangeType());
                EXPECT_EQ(exp_fwd.value_, ncr->isForwardChange());
                EXPECT_EQ(exp_rev.value_, ncr->isReverseChange());
                ASSERT_NO_THROW(d2_mgr_.runReadyIO());
            } else {
                // Should not have created any NCRs.
                EXPECT_EQ(0, d2_mgr_.getQueueSize());
            }
        }
    }

    // Test that the server processes the FQDN option (or lack thereof)
    // in a client request correctly, according to the ddns-replace-client-name
    // mode configuration parameter.
    //
    // @param mode - value to use for ddns-replace-client-name mode
    //
    // @param client_name_flag - specifies whether or not the client request
    // should contain a hostname option
    // @param exp_replacement_flag - specifies whether or not the server is
    // expected to replace (or supply) the FQDN/name in its response
    void testReplaceClientNameMode(const char* mode,
                                   enum ClientNameFlag client_name_flag,
                                   enum ReplacementFlag exp_replacement_flag) {
        // Configuration "template" with a replaceable mode parameter
        const char* config_template =
            "{ \"interfaces-config\": { \n"
            "  \"interfaces\": [ \"eth0\" ] \n"
            "}, \n"
            "\"valid-lifetime\": 4000, \n"
            "\"preferred-lifetime\": 3000, \n"
            "\"rebind-timer\": 2000, \n"
            "\"renew-timer\": 1000, \n"
            "\"ddns-replace-client-name\": \"%s\", \n"
            "\"ddns-qualifying-suffix\": \"fake-suffix.isc.org.\", \n"
            "\"subnet6\": [ { \n"
            "    \"id\": 1, \n"
            "    \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ], \n"
            "    \"subnet\": \"2001:db8:1::/48\", \n"
            "    \"interface\": \"eth0\" \n"
            " } ], \n"
            "\"dhcp-ddns\": { \n"
            "\"enable-updates\": true \n"
            "}} \n";

        // Create the configuration and configure the server
        char config_buf[1024];
        snprintf(config_buf, 1024, config_template, mode);
        configure(config_buf, *srv_);

        // Build our client packet
        Pkt6Ptr query;
        if (client_name_flag == CLIENT_NAME_PRESENT) {
            query = generateMessage(DHCPV6_SOLICIT, Option6ClientFqdn::FLAG_S,
                                    "my.example.com.", Option6ClientFqdn::FULL,
                                    true);
        } else {
            query = generateMessageWithIds(DHCPV6_SOLICIT);
        }

        AllocEngine::ClientContext6 ctx;
        bool drop = !srv_->earlyGHRLookup(query, ctx);
        ASSERT_FALSE(drop);
        ctx.subnet_ = srv_->selectSubnet(query, drop);
        ASSERT_FALSE(drop);
        srv_->initContext(ctx, drop);

        ASSERT_FALSE(drop);
        Pkt6Ptr answer = generateMessageWithIds(DHCPV6_ADVERTISE);

        ASSERT_NO_THROW(srv_->processClientFqdn(query, answer, ctx));

        Option6ClientFqdnPtr answ_fqdn = boost::dynamic_pointer_cast<
            Option6ClientFqdn>(answer->getOption(D6O_CLIENT_FQDN));

        // Verify the contents (or lack thereof) of the FQDN
        if (exp_replacement_flag == NAME_REPLACED) {
            ASSERT_TRUE(answ_fqdn);
            EXPECT_TRUE(answ_fqdn->getDomainName().empty());
            EXPECT_EQ(Option6ClientFqdn::PARTIAL,
                      answ_fqdn->getDomainNameType());
        } else {
            if (client_name_flag == CLIENT_NAME_PRESENT) {
                ASSERT_TRUE(answ_fqdn);
                EXPECT_EQ("my.example.com.", answ_fqdn->getDomainName());
                EXPECT_EQ(Option6ClientFqdn::FULL,
                          answ_fqdn->getDomainNameType());
            } else {
                ASSERT_FALSE(answ_fqdn);
            }
        }
    }

    /// @brief Tests that the client's message holding an FQDN is processed
    /// and that lease is acquired.
    ///
    /// @param msg_type A type of the client's message.
    /// @param hostname A domain name in the client's FQDN.
    /// @param client_flags A bitmask of the client FQDN flags
    /// @param include_oro A boolean value which indicates whether the ORO
    /// option should be included in the client's message (if true) or not
    /// (if false). In the former case, the function will expect that server
    /// responds with the FQDN option. In the latter case, the function expects
    /// that the server doesn't respond with the FQDN.
    void testProcessMessage(const uint8_t msg_type,
                            const std::string& hostname,
                            const std::string& exp_hostname,
                            const uint8_t client_flags =
                                Option6ClientFqdn::FLAG_S,
                            const IOAddress& expected_address = IOAddress("2001:db8:1:1::dead:beef"),
                            const bool include_oro = true) {
        // Create a message of a specified type, add server id and
        // FQDN option.
        OptionPtr srvid = srv_->getServerID();
        // Set the appropriate FQDN type. It must be partial if hostname is
        // empty.
        Option6ClientFqdn::DomainNameType fqdn_type = (hostname.empty() ?
            Option6ClientFqdn::PARTIAL : Option6ClientFqdn::FULL);
        Pkt6Ptr req = generateMessage(msg_type, client_flags,
                                      hostname, fqdn_type, include_oro, srvid);

        // For different client's message types we have to invoke different
        // functions to generate response.
        Pkt6Ptr reply;
        AllocEngine::ClientContext6 ctx;
        bool drop = !srv_->earlyGHRLookup(req, ctx);
        ASSERT_FALSE(drop);
        ctx.subnet_ = srv_->selectSubnet(req, drop);
        ASSERT_FALSE(drop);
        srv_->initContext(ctx, drop);

        ASSERT_FALSE(drop);
        if (msg_type == DHCPV6_SOLICIT) {
            ASSERT_NO_THROW(reply = srv_->processSolicit(ctx));

        } else if (msg_type == DHCPV6_REQUEST) {
            ASSERT_NO_THROW(reply = srv_->processRequest(ctx));

        } else if (msg_type == DHCPV6_RENEW) {
            ASSERT_NO_THROW(reply = srv_->processRenew(ctx));

        } else if (msg_type == DHCPV6_RELEASE) {
            // For Release no lease will be acquired so we have to leave
            // function here.
            ASSERT_NO_THROW(reply = srv_->processRelease(ctx));
            return;
        } else {
            // We are not interested in testing other message types.
            return;
        }

        // For Solicit, we will get different message type obviously.
        if (msg_type == DHCPV6_SOLICIT) {
            checkResponse(reply, DHCPV6_ADVERTISE, 1234);

        } else {
            checkResponse(reply, DHCPV6_REPLY, 1234);
        }

        // Check verify that IA_NA is correct.
        Option6IAAddrPtr addr =
            checkIA_NA(reply, 234, subnet_->getT1(), subnet_->getT2());
        ASSERT_TRUE(addr);

        // Check that we have got the address we requested.
        checkIAAddr(addr, expected_address,
                    Lease::TYPE_NA);

        if (msg_type != DHCPV6_SOLICIT) {
            // Check that the lease exists.
            Lease6Ptr lease =
                checkLease(duid_, reply->getOption(D6O_IA_NA), addr);
            ASSERT_TRUE(lease);
            EXPECT_EQ(exp_hostname, lease->hostname_);
        }

        // The Client FQDN option should be always present in the server's
        // response, regardless if requested using ORO or not.
        Option6ClientFqdnPtr fqdn;
        ASSERT_TRUE(fqdn = boost::dynamic_pointer_cast<
                        Option6ClientFqdn>(reply->getOption(D6O_CLIENT_FQDN)));
        EXPECT_EQ(exp_hostname, fqdn->getDomainName());
    }

    /// @brief Verify that NameChangeRequest holds valid values.
    ///
    /// This function picks first NameChangeRequest from the internal server's
    /// queue and checks that it holds valid parameters. The NameChangeRequest
    /// is removed from the queue.
    ///
    /// @param type An expected type of the NameChangeRequest (Add or Remove).
    /// @param reverse An expected setting of the reverse update flag.
    /// @param forward An expected setting of the forward update flag.
    /// @param addr A string representation of the IPv6 address held in the
    /// NameChangeRequest.
    /// @param dhcid An expected DHCID value.  Ignored if blank.
    /// @note This value is the value that is produced by
    /// dhcp_ddns::D2Dhcid::createDigest() with the appropriate arguments. This
    /// method uses encryption tools to produce the value which cannot be
    /// easily duplicated by hand.  It is more or less necessary to generate
    /// these values programmatically and place them here. Should the
    /// underlying implementation of createDigest() change these test values
    /// will likely need to be updated as well.
    /// @param expires The cltt of the lease associated with the
    /// NameChangeRequest, and used to calculate NCR expires value.
    /// @param valid_lft the valid lifetime of the lease associated with the
    /// NameChangeRequest.
    /// @param fqdn The expected string value of the FQDN, if blank the
    /// check is skipped
    /// @param exp_cr_mode expected value of NCR::conflict_resolution_mode_
    /// @param ddns_ttl_percent expected value of ddns_ttl_percent used for
    /// the NCR
    /// @param exp_ddns_ttl expected configured value for ddns-ttl
    /// @param exp_ddns_ttl_min expected configured value for ddns-ttl-min
    /// @param exp_ddns_ttl_max expected configured value for ddns-ttl-max
    void verifyNameChangeRequest(const isc::dhcp_ddns::NameChangeType type,
                                 const bool reverse, const bool forward,
                                 const std::string& addr,
                                 const std::string& dhcid,
                                 const uint64_t expires,
                                 const uint16_t valid_lft,
                                 const std::string& fqdn = "",
                                 const ConflictResolutionMode exp_cr_mode = CHECK_WITH_DHCID,
                                 util::Optional<double> exp_ddns_ttl_percent
                                 = util::Optional<double>(),
                                 Optional<uint32_t> exp_ddns_ttl = Optional<uint32_t>(),
                                 Optional<uint32_t> exp_ddns_ttl_min = Optional<uint32_t>(),
                                 Optional<uint32_t> exp_ddns_ttl_max = Optional<uint32_t>()) {
        NameChangeRequestPtr ncr;
        ASSERT_NO_THROW(ncr = d2_mgr_.peekAt(0));
        ASSERT_TRUE(ncr);

        EXPECT_EQ(type, ncr->getChangeType());
        EXPECT_EQ(forward, ncr->isForwardChange());
        EXPECT_EQ(reverse, ncr->isReverseChange());
        EXPECT_EQ(addr, ncr->getIpAddress());
        if (!dhcid.empty()) {
            EXPECT_EQ(dhcid, ncr->getDhcid().toStr());
        }

        uint32_t ttl = calculateDdnsTtl(valid_lft, exp_ddns_ttl_percent, exp_ddns_ttl,
                                        exp_ddns_ttl_min, exp_ddns_ttl_max);
        if (expires != 0) {
            EXPECT_EQ(expires + ttl, ncr->getLeaseExpiresOn());
        }

        EXPECT_EQ(ttl, ncr->getLeaseLength());

        EXPECT_EQ(isc::dhcp_ddns::ST_NEW, ncr->getStatus());

        if (! fqdn.empty()) {
           EXPECT_EQ(fqdn, ncr->getFqdn());
        }

        EXPECT_EQ(exp_cr_mode, ncr->getConflictResolutionMode());

        // Process the message off the queue
        ASSERT_NO_THROW(d2_mgr_.runReadyIO());
    }

    /// @brief Updates inherited subnet and pool members
    ///
    /// Hack added to set subnet_ and pool_ members that are buried into lower
    /// level tests such as checkLease(), so one can use "configure" functionality
    /// rather than hand-building configured objects
    /// @param subnet_idx Element index of the desired subnet
    /// @param pool_idx Element index of the desired pool within the desired subnet
    /// @param type lease type of desired pool
    ///
    void setSubnetAndPool(int subnet_idx, int pool_idx, Lease::Type type) {
        ConstCfgSubnets6Ptr subnets = CfgMgr::instance().getCurrentCfg()->getCfgSubnets6();
        ASSERT_TRUE(subnets);
        const Subnet6Collection* subnet_col = subnets->getAll();
        ASSERT_EQ(subnet_idx + 1, subnet_col->size());
        auto subnet_it = subnet_col->begin();
        // std::advance is not available for this iterator.
        for (int i = 0; i < subnet_idx; ++i) {
            subnet_it = std::next(subnet_it);
        }
        subnet_ = *subnet_it;
        ASSERT_TRUE(subnet_);

        const PoolCollection& pool_col = subnet_->getPools(type);
        ASSERT_EQ(pool_idx + 1, pool_col.size());
        PoolPtr pool = (subnet_->getPools(type)).at(pool_idx);
        ASSERT_TRUE(pool);
        pool_ = boost::dynamic_pointer_cast<Pool6>(pool);
        ASSERT_TRUE(pool);
    }

    /// @brief Verifies that DDNS TTL parameters are used when specified. 
    ///
    /// @param valid_flt lease life time
    /// @param ddns_ttl_percent expected configured value for ddns-ttl-percent
    /// @param ddns_ttl expected configured value for ddns-ttl
    /// @param ddns_ttl_min expected configured value for ddns-ttl-min
    /// @param ddns_ttl_max expected configured value for ddns-ttl-max
    void testDdnsTtlParameters(uint32_t valid_lft,
                               Optional<double> ddns_ttl_percent = Optional<double>(),
                               Optional<uint32_t> ddns_ttl = Optional<uint32_t>(),
                               Optional<uint32_t> ddns_ttl_min = Optional<uint32_t>(),
                               Optional<uint32_t> ddns_ttl_max = Optional<uint32_t>()) {

        // Create Reply message with Client Id and Server id.
        Pkt6Ptr answer = generateMessageWithIds(DHCPV6_REPLY);

        // Create NameChangeRequest for the first allocated address.
        AllocEngine::ClientContext6 ctx;
        subnet_->setDdnsConflictResolutionMode("no-check-with-dhcid");
        subnet_->setDdnsTtlPercent(ddns_ttl_percent);
        subnet_->setDdnsTtl(ddns_ttl);
        subnet_->setDdnsTtlMin(ddns_ttl_min);
        subnet_->setDdnsTtlMax(ddns_ttl_max);

        ctx.subnet_ = subnet_;
        ctx.fwd_dns_update_ = ctx.rev_dns_update_ = true;

        // Create an IA.
        Option6IAPtr opt_ia = generateIA(D6O_IA_NA, 1234, 1500, 3000);
        Option6IAAddrPtr opt_iaaddr(new Option6IAAddr(D6O_IAADDR, 
                                                      IOAddress("2001:db8:1::1"),
                                                      valid_lft, valid_lft));
        opt_ia->addOption(opt_iaaddr);
        answer->addOption(opt_ia);
        ctx.createIAContext();
        ctx.currentIA().ia_rsp_ = opt_ia;

        // Use domain name in upper case. It should be converted to lower-case
        // before DHCID is calculated. So, we should get the same result as if
        // we typed domain name in lower-case.
        Option6ClientFqdnPtr fqdn = createClientFqdn(Option6ClientFqdn::FLAG_S,
                                                     "MYHOST.EXAMPLE.COM",
                                                     Option6ClientFqdn::FULL);
        answer->addOption(fqdn);

        ASSERT_NO_THROW(srv_->createNameChangeRequests(answer, ctx));
        ASSERT_EQ(1, d2_mgr_.getQueueSize());

        // Verify that NameChangeRequest is correct.
        verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
                                "2001:db8:1::1",
                                "000201415AA33D1187D148275136FA30300478"
                                "FAAAA3EBD29826B5C907B2C9268A6F52",
                                0, valid_lft, "", NO_CHECK_WITH_DHCID,
                                ddns_ttl_percent,
                                ddns_ttl,
                                ddns_ttl_min,
                                ddns_ttl_max);
    }

    /// Pointer to Dhcpv6Srv that is used in tests
    boost::scoped_ptr<NakedDhcpv6Srv> srv_;

    // Reference to D2ClientMgr singleton
    D2ClientMgr& d2_mgr_;

    /// @brief Interface Manager's fake configuration control.
    IfaceMgrTestConfig iface_mgr_test_config_;

    // Holds a lease used by a test.
    Lease6Ptr lease_;
};

// A set of tests verifying server's behavior when it receives the DHCPv6
// Client Fqdn Option.

// Test server's response when client requests that server performs AAAA update.
TEST_F(FqdnDhcpv6SrvTest, serverAAAAUpdate) {<--- syntax error
    testFqdn(DHCPV6_SOLICIT, Option6ClientFqdn::FLAG_S,
             "myhost.example.com",
             Option6ClientFqdn::FULL, Option6ClientFqdn::FLAG_S,
             "myhost.example.com.", true, ExpFwd(true), ExpRev(true));
}

// Test server's response when client provides partial domain-name and requests
// that server performs AAAA update.
TEST_F(FqdnDhcpv6SrvTest, serverAAAAUpdatePartialName) {
    testFqdn(DHCPV6_SOLICIT, Option6ClientFqdn::FLAG_S, "myhost",
             Option6ClientFqdn::PARTIAL, Option6ClientFqdn::FLAG_S,
             "myhost.example.com.", true, ExpFwd(true), ExpRev(true));
}

// Test server's response when client provides empty domain-name and requests
// that server performs AAAA update.
TEST_F(FqdnDhcpv6SrvTest, serverAAAAUpdateNoName) {
    testFqdn(DHCPV6_SOLICIT, Option6ClientFqdn::FLAG_S, "",
             Option6ClientFqdn::PARTIAL, Option6ClientFqdn::FLAG_S, "", false);
}

// Test server's response when client requests no DNS update.
TEST_F(FqdnDhcpv6SrvTest, noUpdate) {
    testFqdn(DHCPV6_SOLICIT, Option6ClientFqdn::FLAG_N,
             "myhost.example.com",
             Option6ClientFqdn::FULL, Option6ClientFqdn::FLAG_N,
             "myhost.example.com.", true, ExpFwd(false), ExpRev(false));
}

// Test server's response when client requests no DNS update and
// ddns-override-no-updates is true.
TEST_F(FqdnDhcpv6SrvTest, overrideNoUpdate) {
    enableD2(OVERRIDE_NO_UPDATE);
    testFqdn(DHCPV6_SOLICIT, Option6ClientFqdn::FLAG_N,
             "myhost.example.com",
             Option6ClientFqdn::FULL,
             (Option6ClientFqdn::FLAG_S | Option6ClientFqdn::FLAG_O),
             "myhost.example.com.", true, ExpFwd(true), ExpRev(true));
}

// Test server's response when client requests that server delegates the AAAA
// update to the client
TEST_F(FqdnDhcpv6SrvTest, clientAAAAUpdate) {
    testFqdn(DHCPV6_SOLICIT, 0, "myhost.example.com.",
             Option6ClientFqdn::FULL,
             0,
             "myhost.example.com.", true, ExpFwd(false), ExpRev(true));
}

// Test server's response when client requests that server delegates the AAAA
// update to the client and this delegation is not allowed.
TEST_F(FqdnDhcpv6SrvTest, clientAAAAUpdateNotAllowed) {
    enableD2(OVERRIDE_CLIENT_UPDATE);
    testFqdn(DHCPV6_SOLICIT, 0, "myhost.example.com.",
             Option6ClientFqdn::FULL,
             Option6ClientFqdn::FLAG_S | Option6ClientFqdn::FLAG_O,
             "myhost.example.com.", true, ExpFwd(true), ExpRev(true));
}

// Test that exception is thrown if supplied NULL answer packet when
// creating NameChangeRequests.
TEST_F(FqdnDhcpv6SrvTest, createNameChangeRequestsNoAnswer) {
    Pkt6Ptr answer;

    AllocEngine::ClientContext6 ctx;
    ctx.subnet_ = subnet_;
    ctx.fwd_dns_update_ = ctx.rev_dns_update_ = true;
    EXPECT_THROW(srv_->createNameChangeRequests(answer, ctx),
                 isc::Unexpected);
}

// Test that exception is thrown if supplied answer from the server
// contains no DUID when creating NameChangeRequests.
TEST_F(FqdnDhcpv6SrvTest, createNameChangeRequestsNoDUID) {
    Pkt6Ptr answer = Pkt6Ptr(new Pkt6(DHCPV6_REPLY, 1234));
    Option6ClientFqdnPtr fqdn = createClientFqdn(Option6ClientFqdn::FLAG_S,
                                                 "myhost.example.com",
                                                 Option6ClientFqdn::FULL);
    answer->addOption(fqdn);

    AllocEngine::ClientContext6 ctx;
    ctx.subnet_ = subnet_;
    ctx.fwd_dns_update_ = ctx.rev_dns_update_ = true;
    EXPECT_THROW(srv_->createNameChangeRequests(answer, ctx), isc::Unexpected);
}

// Test no NameChangeRequests if Client FQDN is not added to the server's
// response.
TEST_F(FqdnDhcpv6SrvTest, createNameChangeRequestsNoFQDN) {
    // Create Reply message with Client Id and Server id.
    Pkt6Ptr answer = generateMessageWithIds(DHCPV6_REPLY);

    AllocEngine::ClientContext6 ctx;
    ctx.subnet_ = subnet_;
    ctx.fwd_dns_update_ = ctx.rev_dns_update_ = true;
    ASSERT_NO_THROW(srv_->createNameChangeRequests(answer, ctx));

    // There should be no new NameChangeRequests.
    ASSERT_EQ(0, d2_mgr_.getQueueSize());
}

// Test that NameChangeRequests are not generated if an answer message
// contains no addresses.
TEST_F(FqdnDhcpv6SrvTest, createNameChangeRequestsNoAddr) {
    // Create Reply message with Client Id and Server id.
    Pkt6Ptr answer = generateMessageWithIds(DHCPV6_REPLY);

    // Add Client FQDN option.
    Option6ClientFqdnPtr fqdn = createClientFqdn(Option6ClientFqdn::FLAG_S,
                                                 "myhost.example.com",
                                                 Option6ClientFqdn::FULL);
    answer->addOption(fqdn);
    AllocEngine::ClientContext6 ctx;
    ctx.fwd_dns_update_ = ctx.rev_dns_update_ = true;
    ASSERT_NO_THROW(srv_->createNameChangeRequests(answer, ctx));

    // We didn't add any IAs, so there should be no NameChangeRequests in the
    // queue.
    ASSERT_EQ(0, d2_mgr_.getQueueSize());
}

// Test that exactly one NameChangeRequest is created as a result of processing
// the answer message which holds 3 IAs and when FQDN is specified.
TEST_F(FqdnDhcpv6SrvTest, createNameChangeRequests) {
    // Create Reply message with Client Id and Server id.
    Pkt6Ptr answer = generateMessageWithIds(DHCPV6_REPLY);

    // Create NameChangeRequest for the first allocated address.
    AllocEngine::ClientContext6 ctx;
    ctx.subnet_ = subnet_;
    ctx.fwd_dns_update_ = ctx.rev_dns_update_ = true;

    // Create three IAs, each having different address.
    addIA(1234, IOAddress("2001:db8:1::1"), answer, ctx);
    addIA(2345, IOAddress("2001:db8:1::2"), answer, ctx);
    addIA(3456, IOAddress("2001:db8:1::3"), answer, ctx);

    // Use domain name in upper case. It should be converted to lower-case
    // before DHCID is calculated. So, we should get the same result as if
    // we typed domain name in lower-case.
    Option6ClientFqdnPtr fqdn = createClientFqdn(Option6ClientFqdn::FLAG_S,
                                                 "MYHOST.EXAMPLE.COM",
                                                 Option6ClientFqdn::FULL);
    answer->addOption(fqdn);


    ASSERT_NO_THROW(srv_->createNameChangeRequests(answer, ctx));
    ASSERT_EQ(1, d2_mgr_.getQueueSize());

    // Verify that NameChangeRequest is correct.
    verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
                            "2001:db8:1::1",
                            "000201415AA33D1187D148275136FA30300478"
                            "FAAAA3EBD29826B5C907B2C9268A6F52",
                            0, 500);
}

// Verify that conflict resolution is turned off when the
// subnet has it disabled.
TEST_F(FqdnDhcpv6SrvTest, noConflictResolution) {
    // Create Reply message with Client Id and Server id.
    Pkt6Ptr answer = generateMessageWithIds(DHCPV6_REPLY);

    // Create NameChangeRequest for the first allocated address.
    AllocEngine::ClientContext6 ctx;
    subnet_->setDdnsConflictResolutionMode("no-check-with-dhcid");
    ctx.subnet_ = subnet_;
    ctx.fwd_dns_update_ = ctx.rev_dns_update_ = true;

    // Create three IAs, each having different address.
    addIA(1234, IOAddress("2001:db8:1::1"), answer, ctx);

    // Use domain name in upper case. It should be converted to lower-case
    // before DHCID is calculated. So, we should get the same result as if
    // we typed domain name in lower-case.
    Option6ClientFqdnPtr fqdn = createClientFqdn(Option6ClientFqdn::FLAG_S,
                                                 "MYHOST.EXAMPLE.COM",
                                                 Option6ClientFqdn::FULL);
    answer->addOption(fqdn);

    ASSERT_NO_THROW(srv_->createNameChangeRequests(answer, ctx));
    ASSERT_EQ(1, d2_mgr_.getQueueSize());

    // Verify that NameChangeRequest is correct.
    verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
                            "2001:db8:1::1",
                            "000201415AA33D1187D148275136FA30300478"
                            "FAAAA3EBD29826B5C907B2C9268A6F52",
                            0, 500, "", NO_CHECK_WITH_DHCID);
}

// Checks that NameChangeRequests to add entries are not
// created when ddns updates are disabled.
TEST_F(FqdnDhcpv6SrvTest, noAddRequestsWhenDisabled) {
    // Disable DDNS updates.
    disableD2();

    // Create Reply message with Client Id and Server id.
    Pkt6Ptr answer = generateMessageWithIds(DHCPV6_REPLY);

    // An attempt to send a NCR would throw.
    AllocEngine::ClientContext6 ctx;
    ctx.subnet_ = subnet_;
    ctx.fwd_dns_update_ = ctx.rev_dns_update_ = true;

    // Create three IAs, each having different address.
    addIA(1234, IOAddress("2001:db8:1::1"), answer, ctx);

    // Use domain name in upper case. It should be converted to lower-case
    // before DHCID is calculated. So, we should get the same result as if
    // we typed domain name in lower-case.
    Option6ClientFqdnPtr fqdn = createClientFqdn(Option6ClientFqdn::FLAG_S,
                                                 "MYHOST.EXAMPLE.COM",
                                                 Option6ClientFqdn::FULL);
    answer->addOption(fqdn);

    ASSERT_NO_THROW(srv_->createNameChangeRequests(answer, ctx));
}

// Test creation of the NameChangeRequest to remove both forward and reverse
// mapping for the given lease.
TEST_F(FqdnDhcpv6SrvTest, createRemovalNameChangeRequestFwdRev) {
    lease_->fqdn_fwd_ = true;
    lease_->fqdn_rev_ = true;
    // Part of the domain name is in upper case, to test that it gets converted
    // to lower case before DHCID is computed. So, we should get the same DHCID
    // as if we typed domain-name in lower case.
    lease_->hostname_ = "MYHOST.example.com.";

    ASSERT_NO_THROW(queueNCR(CHG_REMOVE, lease_));

    ASSERT_EQ(1, d2_mgr_.getQueueSize());
    verifyNameChangeRequest(isc::dhcp_ddns::CHG_REMOVE, true, true,
                            "2001:db8:1::1",
                            "000201415AA33D1187D148275136FA30300478"
                            "FAAAA3EBD29826B5C907B2C9268A6F52",
                            lease_->cltt_, lease_->valid_lft_);
}

// Checks that calling queueNCR would not result in error if DDNS updates are
// disabled.
TEST_F(FqdnDhcpv6SrvTest, noRemovalsWhenDisabled) {
    // Disable DDNS updates.
    disableD2();

    lease_->fqdn_fwd_ = true;
    lease_->fqdn_rev_ = true;
    // Part of the domain name is in upper case, to test that it gets converted
    // to lower case before DHCID is computed. So, we should get the same DHCID
    // as if we typed domain-name in lower case.
    lease_->hostname_ = "MYHOST.example.com.";

    // When DDNS is disabled an attempt to send a request should not throw, but
    // nothing is generated. Unfortunately, we can't see if anything get
    // generated because getting anything from the queue when DDNS is disabled
    // would result in exception.
    ASSERT_NO_THROW(queueNCR(CHG_REMOVE, lease_));
}

// Test creation of the NameChangeRequest to remove reverse mapping for the
// given lease.
TEST_F(FqdnDhcpv6SrvTest, createRemovalNameChangeRequestRev) {
    lease_->fqdn_fwd_ = false;
    lease_->fqdn_rev_ = true;
    lease_->hostname_ = "myhost.example.com.";

    ASSERT_NO_THROW(queueNCR(CHG_REMOVE, lease_));

    ASSERT_EQ(1, d2_mgr_.getQueueSize());

    verifyNameChangeRequest(isc::dhcp_ddns::CHG_REMOVE, true, false,
                            "2001:db8:1::1",
                            "000201415AA33D1187D148275136FA30300478"
                            "FAAAA3EBD29826B5C907B2C9268A6F52",
                            lease_->cltt_, lease_->valid_lft_);
}

// Test that NameChangeRequest to remove DNS records is not generated when
// neither forward nor reverse DNS update has been performed for a lease.
TEST_F(FqdnDhcpv6SrvTest, createRemovalNameChangeRequestNoUpdate) {
    lease_->fqdn_fwd_ = false;
    lease_->fqdn_rev_ = false;

    ASSERT_NO_THROW(queueNCR(CHG_REMOVE, lease_));

    ASSERT_EQ(0, d2_mgr_.getQueueSize());
}

// Test that NameChangeRequest is not generated if the hostname hasn't been
// specified for a lease for which forward and reverse mapping has been set.
TEST_F(FqdnDhcpv6SrvTest, createRemovalNameChangeRequestNoHostname) {
    lease_->fqdn_fwd_ = true;
    lease_->fqdn_rev_ = true;
    lease_->hostname_ = "";

    Pkt6Ptr pkt(new Pkt6(DHCPREQUEST, 1234));
    ASSERT_NO_THROW(queueNCR(CHG_REMOVE, lease_));

    ASSERT_EQ(0, d2_mgr_.getQueueSize());
}

// Test that NameChangeRequest is not generated if the invalid hostname has
// been specified for a lease for which forward and reverse mapping has been
// set.
TEST_F(FqdnDhcpv6SrvTest, createRemovalNameChangeRequestWrongHostname) {
    lease_->fqdn_fwd_ = true;
    lease_->fqdn_rev_ = true;
    lease_->hostname_ = "myhost..example.com.";

    ASSERT_NO_THROW(queueNCR(CHG_REMOVE, lease_));

    ASSERT_EQ(0, d2_mgr_.getQueueSize());
}

// Test that Advertise message generated in a response to the Solicit will
// not result in generation if the NameChangeRequests.
TEST_F(FqdnDhcpv6SrvTest, processSolicit) {
    // Create a Solicit message with FQDN option and generate server's
    // response using processSolicit function.
    testProcessMessage(DHCPV6_SOLICIT, "myhost.example.com",
                       "myhost.example.com.");
    ASSERT_EQ(0, d2_mgr_.getQueueSize());
}

// Test that client may send two requests, each carrying FQDN option with
// a different domain-name. Server should use existing lease for the second
// request but modify the DNS entries for the lease according to the contents
// of the FQDN sent in the second request.
TEST_F(FqdnDhcpv6SrvTest, processTwoRequestsDiffFqdn) {
    // Create a Request message with FQDN option and generate server's
    // response using processRequest function. This will result in the
    // creation of a new lease and the appropriate NameChangeRequest
    // to add both reverse and forward mapping to DNS.
    testProcessMessage(DHCPV6_REQUEST, "myhost.example.com",
                       "myhost.example.com.");

    // The lease should have been recorded in the database.
    lease_ = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
                                                   IOAddress("2001:db8:1:1::dead:beef"));
    ASSERT_TRUE(lease_);

    ASSERT_EQ(1, d2_mgr_.getQueueSize());
    verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
                            "2001:db8:1:1::dead:beef",
                            "000201415AA33D1187D148275136FA30300478"
                            "FAAAA3EBD29826B5C907B2C9268A6F52",
                            0, 4000);

    // Client may send another request message with a new domain-name. In this
    // case the same lease will be returned. The existing DNS entry needs to
    // be replaced with a new one. Server should determine that the different
    // FQDN has been already added to the DNS. As a result, the old DNS
    // entries should be removed and the entries for the new domain-name
    // should be added. Therefore, we expect two NameChangeRequests. One to
    // remove the existing entries, one to add new entries.
    testProcessMessage(DHCPV6_REQUEST, "otherhost.example.com",
                       "otherhost.example.com.");
    ASSERT_EQ(2, d2_mgr_.getQueueSize());
    verifyNameChangeRequest(isc::dhcp_ddns::CHG_REMOVE, true, true,
                            "2001:db8:1:1::dead:beef",
                            "000201415AA33D1187D148275136FA30300478"
                            "FAAAA3EBD29826B5C907B2C9268A6F52",
                            lease_->cltt_, lease_->valid_lft_);
    verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
                            "2001:db8:1:1::dead:beef",
                            "000201D422AA463306223D269B6CB7AFE7AAD265FC"
                            "EA97F93623019B2E0D14E5323D5A",
                            0, lease_->valid_lft_);
}

// Test that client may send two requests, each carrying FQDN option with
// the same domain-name. Server should use existing lease for the second
// request and not modify the DNS entries.
TEST_F(FqdnDhcpv6SrvTest, processTwoRequestsSameFqdn) {
    // Create a Request message with FQDN option and generate server's
    // response using processRequest function. This will result in the
    // creation of a new lease and the appropriate NameChangeRequest
    // to add both reverse and forward mapping to DNS.
    testProcessMessage(DHCPV6_REQUEST, "myhost.example.com",
                       "myhost.example.com.");

    // The lease should have been recorded in the database.
    lease_ = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
                                                   IOAddress("2001:db8:1:1::dead:beef"));
    ASSERT_TRUE(lease_);

    ASSERT_EQ(1, d2_mgr_.getQueueSize());
    verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
                            "2001:db8:1:1::dead:beef",
                            "000201415AA33D1187D148275136FA30300478"
                            "FAAAA3EBD29826B5C907B2C9268A6F52",
                            0, 4000);

    // Client may send another request message with a same domain-name. In this
    // case the same lease will be returned. The existing DNS entry should be
    // left alone, so we expect no NameChangeRequests queued..
    testProcessMessage(DHCPV6_REQUEST, "myhost.example.com",
                       "myhost.example.com.");
    ASSERT_EQ(0, d2_mgr_.getQueueSize());
}

// Test that NameChangeRequest is not generated when Solicit message is sent.
// The Solicit is here sent after a lease has been allocated for a client.
// The Solicit conveys a different hostname which would trigger updates to
// DNS if the Request was sent instead of Soicit. The code should differentiate
// behavior depending whether Solicit or Request is sent.
TEST_F(FqdnDhcpv6SrvTest, processRequestSolicit) {
    // Create a Request message with FQDN option and generate server's
    // response using processRequest function. This will result in the
    // creation of a new lease and the appropriate NameChangeRequest
    // to add both reverse and forward mapping to DNS.
    testProcessMessage(DHCPV6_REQUEST, "myhost.example.com",
                       "myhost.example.com.");
    ASSERT_EQ(1, d2_mgr_.getQueueSize());
    verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
                            "2001:db8:1:1::dead:beef",
                            "000201415AA33D1187D148275136FA30300478"
                            "FAAAA3EBD29826B5C907B2C9268A6F52",
                            0, 4000);

    // When the returning client sends Solicit the code should never generate
    // NameChangeRequest and preserve existing DNS entries for the client.
    // The NameChangeRequest should only be generated when a client sends
    // Request or Renew.
    testProcessMessage(DHCPV6_SOLICIT, "otherhost.example.com",
                       "otherhost.example.com.");
    ASSERT_EQ(0, d2_mgr_.getQueueSize());
}

// Test that client may send Request followed by the Renew, both holding
// FQDN options, but each option holding different domain-name. The Renew
// should result in generation of the two NameChangeRequests, one to remove
// DNS entry added previously when Request was processed, another one to
// add a new entry for the FQDN held in the Renew.
/// @todo: Fix will be available on trac3677
TEST_F(FqdnDhcpv6SrvTest, processRequestRenewDiffFqdn) {
    // Create a Request message with FQDN option and generate server's
    // response using processRequest function. This will result in the
    // creation of a new lease and the appropriate NameChangeRequest
    // to add both reverse and forward mapping to DNS.
    testProcessMessage(DHCPV6_REQUEST, "myhost.example.com",
                       "myhost.example.com.");
    // The lease should have been recorded in the database.
    lease_ = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
                                                   IOAddress("2001:db8:1:1::dead:beef"));
    ASSERT_TRUE(lease_);

    ASSERT_EQ(1, d2_mgr_.getQueueSize());
    verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
                            "2001:db8:1:1::dead:beef",
                            "000201415AA33D1187D148275136FA30300478"
                            "FAAAA3EBD29826B5C907B2C9268A6F52",
                            0, 4000);

    // Client may send Renew message with a new domain-name. In this
    // case the same lease will be returned. The existing DNS entry needs to
    // be replaced with a new one. Server should determine that the different
    // FQDN has been already added to the DNS. As a result, the old DNS
    // entries should be removed and the entries for the new domain-name
    // should be added. Therefore, we expect two NameChangeRequests. One to
    // remove the existing entries, one to add new entries.
    testProcessMessage(DHCPV6_RENEW, "otherhost.example.com",
                       "otherhost.example.com.");
    ASSERT_EQ(2, d2_mgr_.getQueueSize());
    verifyNameChangeRequest(isc::dhcp_ddns::CHG_REMOVE, true, true,
                            "2001:db8:1:1::dead:beef",
                            "000201415AA33D1187D148275136FA30300478"
                            "FAAAA3EBD29826B5C907B2C9268A6F52",
                            lease_->cltt_, lease_->valid_lft_);
    verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
                            "2001:db8:1:1::dead:beef",
                            "000201D422AA463306223D269B6CB7AFE7AAD265FC"
                            "EA97F93623019B2E0D14E5323D5A",
                            0, lease_->valid_lft_);
}

// Test that client may send Request followed by the Renew, both holding
// FQDN options, but each option holding different domain-name. The Renew
// should result in generation of the two NameChangeRequests, one to remove
// DNS entry added previously when Request was processed, another one to
// add a new entry for the FQDN held in the Renew.
TEST_F(FqdnDhcpv6SrvTest, processRequestRenewSameFqdn) {
    // Create a Request message with FQDN option and generate server's
    // response using processRequest function. This will result in the
    // creation of a new lease and the appropriate NameChangeRequest
    // to add both reverse and forward mapping to DNS.
    testProcessMessage(DHCPV6_REQUEST, "myhost.example.com",
                       "myhost.example.com.");
    ASSERT_EQ(1, d2_mgr_.getQueueSize());
    verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
                            "2001:db8:1:1::dead:beef",
                            "000201415AA33D1187D148275136FA30300478"
                            "FAAAA3EBD29826B5C907B2C9268A6F52",
                            0, 4000);

    ASSERT_EQ(0, d2_mgr_.getQueueSize());

    // Client may send Renew message with a same domain-name. In this
    // case the same lease will be returned. No DNS updates should be
    // required, so the NCR queue should be empty.
    testProcessMessage(DHCPV6_RENEW, "myhost.example.com",
                       "myhost.example.com.");
    ASSERT_EQ(0, d2_mgr_.getQueueSize());
}

// Tests that renewals using the same domain name but differing values for
// the directional update flags result in NCRs or not, accordingly.
// If the new leases's flags are the same as the previous lease's flags,
// then no requests should be generated.  If at lease one of the new lease's
// flags differ from the previous lease, then:
// A: A removal NCR should be created based on the previous leases's flags
// if at least one of those flags are true
// B: An add NCR should be created based on the new lease's flags, if at
// least one of those flags are true
TEST_F(FqdnDhcpv6SrvTest, processRequestRenewFqdnFlags) {
    // Create a Request message with FQDN option but with N flag = 1, which
    // means no updates should be done. This should result in no NCRs.
    testProcessMessage(DHCPV6_REQUEST, "myhost.example.com",
                       "myhost.example.com.", Option6ClientFqdn::FLAG_N);
    // Queue should be empty.
    ASSERT_EQ(0, d2_mgr_.getQueueSize());

    // Now renew with Both N and S = 0.  This means the server should only
    // do reverse updates and should result in a reverse-only NCR.
    testProcessMessage(DHCPV6_RENEW, "myhost.example.com",
                       "myhost.example.com.", 0);
    // We should a only have reverse-only ADD, no remove.
    ASSERT_EQ(1, d2_mgr_.getQueueSize());
    verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, false,
                            "2001:db8:1:1::dead:beef",
                            "000201415AA33D1187D148275136FA30300478"
                            "FAAAA3EBD29826B5C907B2C9268A6F52",
                            0, 4000);

    // Renew again with the same flags, this should not generate any NCRs.
    testProcessMessage(DHCPV6_RENEW, "myhost.example.com",
                       "myhost.example.com.", 0);
    // Queue should be empty.
    ASSERT_EQ(0, d2_mgr_.getQueueSize());

    // Renew with both N and S flags = 0. This tells the server to update
    // both directions, which should change forward flag to true. This should
    // generate a reverse only remove and a dual add.
    testProcessMessage(DHCPV6_RENEW, "myhost.example.com",
                       "myhost.example.com.", Option6ClientFqdn::FLAG_S);

    // We need the lease for the expiration value.
    lease_ = LeaseMgrFactory::
             instance().getLease6(Lease::TYPE_NA,
                                  IOAddress("2001:db8:1:1::dead:beef"));
    ASSERT_TRUE(lease_);

    // We should have two NCRs, one remove and one add.
    ASSERT_EQ(2, d2_mgr_.getQueueSize());
    verifyNameChangeRequest(isc::dhcp_ddns::CHG_REMOVE, true, false,
                            "2001:db8:1:1::dead:beef",
                            "000201415AA33D1187D148275136FA30300478"
                            "FAAAA3EBD29826B5C907B2C9268A6F52",
                            lease_->cltt_, lease_->valid_lft_);

    verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
                            "2001:db8:1:1::dead:beef",
                            "000201415AA33D1187D148275136FA30300478"
                            "FAAAA3EBD29826B5C907B2C9268A6F52",
                            0, lease_->valid_lft_);

    // Lastly, we renew with the N flag = 1 (which means no updates) so we
    // should have a dual direction remove NCR but NO add NCR.
    testProcessMessage(DHCPV6_RENEW, "myhost.example.com",
                       "myhost.example.com.", Option6ClientFqdn::FLAG_N);
    // We should only have the removal NCR.
    ASSERT_EQ(1, d2_mgr_.getQueueSize());
    verifyNameChangeRequest(isc::dhcp_ddns::CHG_REMOVE, true, true,
                            "2001:db8:1:1::dead:beef",
                            "000201415AA33D1187D148275136FA30300478"
                            "FAAAA3EBD29826B5C907B2C9268A6F52",
                            lease_->cltt_, lease_->valid_lft_);
}

TEST_F(FqdnDhcpv6SrvTest, processRequestRelease) {
    CfgMgr::instance().getCurrentCfg()->getCfgExpiration()->setFlushReclaimedTimerWaitTime(0);
    CfgMgr::instance().getCurrentCfg()->getCfgExpiration()->setHoldReclaimedTime(0);

    // Create a Request message with FQDN option and generate server's
    // response using processRequest function. This will result in the
    // creation of a new lease and the appropriate NameChangeRequest
    // to add both reverse and forward mapping to DNS.
    testProcessMessage(DHCPV6_REQUEST, "myhost.example.com",
                       "myhost.example.com.");

    // The lease should have been recorded in the database.
    lease_ = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
                                                   IOAddress("2001:db8:1:1::dead:beef"));
    ASSERT_TRUE(lease_);

    ASSERT_EQ(1, d2_mgr_.getQueueSize());
    verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
                            "2001:db8:1:1::dead:beef",
                            "000201415AA33D1187D148275136FA30300478"
                            "FAAAA3EBD29826B5C907B2C9268A6F52",
                            0, lease_->valid_lft_);

    // Client may send Release message. In this case the lease should be
    // removed and all existing DNS entries for this lease should also
    // be removed. Therefore, we expect that single NameChangeRequest to
    // remove DNS entries is generated.
    testProcessMessage(DHCPV6_RELEASE, "otherhost.example.com",
                       "otherhost.example.com.");
    ASSERT_EQ(1, d2_mgr_.getQueueSize());
    verifyNameChangeRequest(isc::dhcp_ddns::CHG_REMOVE, true, true,
                            "2001:db8:1:1::dead:beef",
                            "000201415AA33D1187D148275136FA30300478"
                            "FAAAA3EBD29826B5C907B2C9268A6F52",
                            lease_->cltt_, lease_->valid_lft_);
}

TEST_F(FqdnDhcpv6SrvTest, processRequestReleaseNoDelete) {
    // Create a Request message with FQDN option and generate server's
    // response using processRequest function. This will result in the
    // creation of a new lease and the appropriate NameChangeRequest
    // to add both reverse and forward mapping to DNS.
    testProcessMessage(DHCPV6_REQUEST, "myhost.example.com",
                       "myhost.example.com.");

    // The lease should have been recorded in the database.
    lease_ = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
                                                   IOAddress("2001:db8:1:1::dead:beef"));
    ASSERT_TRUE(lease_);

    ASSERT_EQ(1, d2_mgr_.getQueueSize());
    verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
                            "2001:db8:1:1::dead:beef",
                            "000201415AA33D1187D148275136FA30300478"
                            "FAAAA3EBD29826B5C907B2C9268A6F52",
                            0, lease_->valid_lft_);

    // Client may send Release message. In this case the lease should be
    // expired and no NameChangeRequest to remove DNS entries is generated.
    testProcessMessage(DHCPV6_RELEASE, "otherhost.example.com",
                       "otherhost.example.com.");
    ASSERT_EQ(0, d2_mgr_.getQueueSize());
}

// Checks that the server include DHCPv6 Client FQDN option in its
// response even when client doesn't request this option using ORO.
TEST_F(FqdnDhcpv6SrvTest, processRequestWithoutFqdn) {
    // The last parameter disables use of the ORO to request FQDN option
    // In this case, we expect that the FQDN option will not be included
    // in the server's response. The testProcessMessage will check that.
    testProcessMessage(DHCPV6_REQUEST, "myhost.example.com",
                       "myhost.example.com.", Option6ClientFqdn::FLAG_S,
                       IOAddress("2001:db8:1:1::dead:beef"), false);
    ASSERT_EQ(1, d2_mgr_.getQueueSize());
    verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
                            "2001:db8:1:1::dead:beef",
                            "000201415AA33D1187D148275136FA30300478"
                            "FAAAA3EBD29826B5C907B2C9268A6F52",
                            0, 4000);
}

// Checks that FQDN is generated from an ip address, when client sends an empty
// FQDN.
TEST_F(FqdnDhcpv6SrvTest, processRequestEmptyFqdn) {
    testProcessMessage(DHCPV6_REQUEST, "",
                       "myhost-2001-db8-1-1--dead-beef.example.com.",
                       Option6ClientFqdn::FLAG_S,
                       IOAddress("2001:db8:1:1::dead:beef"), false);
    ASSERT_EQ(1, d2_mgr_.getQueueSize());
    verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
                            "2001:db8:1:1::dead:beef",
                            "000201C905E54BE12DE6AF92ADE72752B9F362"
                            "13B5A8BC9D217548CD739B4CF31AFB1B",
                            0, 4000);
}

// Checks that when the server reuses expired lease, the NameChangeRequest
// is generated to remove the DNS mapping for the expired lease and second
// NameChangeRequest to add a DNS mapping for a new lease.
TEST_F(FqdnDhcpv6SrvTest, processRequestReuseExpiredLease) {
    // This address will be used throughout the test.
    IOAddress addr("2001:db8:1:1::dead:beef");
    // We are going to configure a subnet with a pool that consists of
    // exactly one address. This address will be handed out to the
    // client, will get expired and then be reused.
    CfgMgr::instance().clear();
    subnet_ = Subnet6::create(IOAddress("2001:db8:1:1::"),
                              56, 1, 2, 3, 4, SubnetID(10));
    subnet_->setIface("eth0");
    subnet_->setDdnsSendUpdates(true);

    pool_ = Pool6Ptr(new Pool6(Lease::TYPE_NA, addr, addr));
    subnet_->addPool(pool_);
    CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->add(subnet_);
    CfgMgr::instance().commit();

    // Enable D2.
    enableD2();

    // Allocate a lease.
    testProcessMessage(DHCPV6_REQUEST, "myhost.example.com",
                       "myhost.example.com.");
    // Test that the appropriate NameChangeRequest has been generated.
    ASSERT_EQ(1, d2_mgr_.getQueueSize());

    // Get the lease acquired.
    Lease6Ptr lease =
        LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA, addr);
    ASSERT_TRUE(lease);

    verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
                            "2001:db8:1:1::dead:beef",
                            "000201415AA33D1187D148275136FA30300478"
                            "FAAAA3EBD29826B5C907B2C9268A6F52",
                            0, lease_->valid_lft_);
    // One of the following: IAID, DUID or subnet identifier has to be changed
    // because otherwise the allocation engine will treat the lease as
    // being renewed by the same client. If we at least change subnet identifier
    // the lease will be treated as expired lease to be reused.
    ++lease->subnet_id_;

    // Move the cllt back in time and make sure that the lease got expired.
    lease->cltt_ = time(NULL) - 10;
    lease->valid_lft_ = 5;
    ASSERT_TRUE(lease->expired());
    // Change the hostname so as the name change request for removing existing
    // DNS mapping is generated.
    lease->hostname_ = "otherhost.example.com.";
    // Update the lease in the lease database.
    LeaseMgrFactory::instance().updateLease6(lease);

    // Simulate another lease acquisition. Since, our pool consists of
    // exactly one address and this address is used by the lease in the
    // lease database, it is guaranteed that the allocation engine will
    // reuse this lease.
    testProcessMessage(DHCPV6_REQUEST, "myhost.example.com.",
                       "myhost.example.com.");
    ASSERT_EQ(2, d2_mgr_.getQueueSize());
    // The first name change request generated, should remove a DNS
    // mapping for an expired lease.
    verifyNameChangeRequest(isc::dhcp_ddns::CHG_REMOVE, true, true,
                            "2001:db8:1:1::dead:beef",
                            "000201D422AA463306223D269B6CB7AFE7AAD2"
                            "65FCEA97F93623019B2E0D14E5323D5A",
                            lease->cltt_, lease->valid_lft_);
    // The second name change request should add a DNS mapping for
    // a new lease.
    verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
                            "2001:db8:1:1::dead:beef",
                            "000201415AA33D1187D148275136FA30300478"
                            "FAAAA3EBD29826B5C907B2C9268A6F52", 0, 4);
}

TEST_F(FqdnDhcpv6SrvTest, processClientDelegation) {
    testProcessMessage(DHCPV6_REQUEST, "myhost.example.com",
                       "myhost.example.com.", 0);
    ASSERT_EQ(1, d2_mgr_.getQueueSize());
    verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, false,
                            "2001:db8:1:1::dead:beef",
                            "000201415AA33D1187D148275136FA30300478"
                            "FAAAA3EBD29826B5C907B2C9268A6F52",
                            0, 4000);
}

// Verify that the host reservation is found and used. Lease host name and
// FQDN should be the reservation hostname suffixed by the qualifying suffix.
TEST_F(FqdnDhcpv6SrvTest, hostnameReservationSuffix) {
    isc::dhcp::test::IfaceMgrTestConfig test_config(true);

    string config_str = "{ "
        "\"interfaces-config\": {"
        "  \"interfaces\": [ \"*\" ]"
        "},"
        "\"valid-lifetime\": 4000, "
        "\"preferred-lifetime\": 3000,"
        "\"rebind-timer\": 2000, "
        "\"renew-timer\": 1000, "
        "\"subnet6\": [ "
        " { "
        "    \"id\": 1, \n"
        "    \"subnet\": \"2001:db8:1::/48\", "
        "    \"pools\": [ { \"pool\": \"2001:db8:1:1::/64\" } ],"
        "    \"interface\": \"eth0\" , "
        "   \"reservations\": ["
        "    {"
        "        \"duid\": \"" + duid_->toText() + "\","
        "        \"ip-addresses\": [ \"2001:db8:1:1::babe\" ],"
        "        \"hostname\": \"alice\""
        "    }"
        "    ]"
        " } ],"
        " \"ddns-qualifying-suffix\": \"example.com\", "
        " \"dhcp-ddns\": {"
        "     \"enable-updates\": true }"
        "}";

    configure(config_str);

    // Update subnet_ and pool_ members after config
    setSubnetAndPool(0, 0, Lease::TYPE_NA);

    ASSERT_NO_THROW(srv_->startD2());

    ASSERT_TRUE(CfgMgr::instance().ddnsEnabled());

    // Verify that the host reservation is found and lease name/FQDN are
    // formed properly from the host name and qualifying suffix.
    testProcessMessage(DHCPV6_REQUEST, "myhost.example.com",
                       "alice.example.com.", 1, IOAddress("2001:db8:1:1::babe"));

    // Verify that NameChangeRequest is correct.
    verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
                            "2001:db8:1:1::babe",
                            "000201E2EB74FB53A5778E74AFD43870ECA5"
                            "4150B1F52B0CFED434802DA1259D6D3CA4",
                            0, 4000, "alice.example.com.");
}

// Verify that the host reservation is found and used, rather than dynamic
// Address.  Lease host name and FQDN should be the reservation hostname
// without a qualifying suffix.
TEST_F(FqdnDhcpv6SrvTest, hostnameReservationNoSuffix) {
    isc::dhcp::test::IfaceMgrTestConfig test_config(true);

    string config_str = "{ "
        "\"interfaces-config\": {"
        "  \"interfaces\": [ \"*\" ]"
        "},"
        "\"valid-lifetime\": 4000, "
        "\"preferred-lifetime\": 3000,"
        "\"rebind-timer\": 2000, "
        "\"renew-timer\": 1000, "
        "\"subnet6\": [ "
        " { "
        "    \"id\": 1, \n"
        "    \"subnet\": \"2001:db8:1::/48\", "
        "    \"pools\": [ { \"pool\": \"2001:db8:1:1::/64\" } ],"
        "    \"interface\": \"eth0\" , "
        "   \"reservations\": ["
        "    {"
        "        \"duid\": \"" + duid_->toText() + "\","
        "        \"ip-addresses\": [ \"2001:db8:1:1::babe\" ],"
        "        \"hostname\": \"alice.example.com\""
        "    }"
        "    ]"
        " } ],"
        " \"ddns-qualifying-suffix\": \"\", "
        " \"dhcp-ddns\": {"
        "     \"enable-updates\": true }"
        "}";

    configure(config_str);
    // Update subnet_ and pool_ members after config
    setSubnetAndPool(0, 0, Lease::TYPE_NA);

    ASSERT_NO_THROW(srv_->startD2());

    ASSERT_TRUE(CfgMgr::instance().ddnsEnabled());

    testProcessMessage(DHCPV6_REQUEST, "myhost.example.com",
                       "alice.example.com.", 1,
                       IOAddress("2001:db8:1:1::babe"));

    // Verify that NameChangeRequest is correct.
    verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
                            "2001:db8:1:1::babe",
                            "000201E2EB74FB53A5778E74AFD43870ECA5"
                            "4150B1F52B0CFED434802DA1259D6D3CA4",
                            0, 4000, "alice.example.com.");
}

TEST_F(FqdnDhcpv6SrvTest, hostnameReservationDdnsDisabled) {
    isc::dhcp::test::IfaceMgrTestConfig test_config(true);

    string config_str = "{ "
        "\"interfaces-config\": {"
        "  \"interfaces\": [ \"*\" ]"
        "},"
        "\"valid-lifetime\": 4000, "
        "\"preferred-lifetime\": 3000,"
        "\"rebind-timer\": 2000, "
        "\"renew-timer\": 1000, "
        "\"subnet6\": [ "
        " { "
        "    \"id\": 1, \n"
        "    \"subnet\": \"2001:db8:1::/48\", "
        "    \"pools\": [ { \"pool\": \"2001:db8:1:1::/64\" } ],"
        "    \"interface\": \"eth0\" , "
        "    \"reservations\": ["
        "    {"
        "        \"duid\": \"" + duid_->toText() + "\","
        "        \"ip-addresses\": [ \"2001:db8:1:1::babe\" ],"
        "        \"hostname\": \"alice\""
        "    }"
        "    ]"
        " } ],"
        " \"ddns-qualifying-suffix\": \"disabled.example.com\", "
        " \"dhcp-ddns\": {"
        "     \"enable-updates\": false }"
        "}";

    configure(config_str);

    // Update subnet_ and pool_ members after config
    setSubnetAndPool(0, 0, Lease::TYPE_NA);

    ASSERT_FALSE(CfgMgr::instance().ddnsEnabled());

    testProcessMessage(DHCPV6_REQUEST, "myhost.example.com",
                       "alice.disabled.example.com.", 0,
                       IOAddress("2001:db8:1:1::babe"));
}

// Verifies that the ddns-replace-client-name behavior is correct for each of
// the supported modes.
TEST_F(FqdnDhcpv6SrvTest, replaceClientNameModeTest) {
    isc::dhcp::test::IfaceMgrTestConfig test_config(true);

    testReplaceClientNameMode("never",
                              CLIENT_NAME_NOT_PRESENT, NAME_NOT_REPLACED);

    testReplaceClientNameMode("never",
                              CLIENT_NAME_PRESENT, NAME_NOT_REPLACED);

    testReplaceClientNameMode("always",
                              CLIENT_NAME_NOT_PRESENT, NAME_REPLACED);
    testReplaceClientNameMode("always",
                              CLIENT_NAME_PRESENT, NAME_REPLACED);

    testReplaceClientNameMode("when-present",
                              CLIENT_NAME_NOT_PRESENT, NAME_NOT_REPLACED);
    testReplaceClientNameMode("when-present",
                              CLIENT_NAME_PRESENT, NAME_REPLACED);

    testReplaceClientNameMode("when-not-present",
                              CLIENT_NAME_NOT_PRESENT, NAME_REPLACED);
    testReplaceClientNameMode("when-not-present",
                              CLIENT_NAME_PRESENT, NAME_NOT_REPLACED);
}

// Verifies that setting hostname-char-set sanitizes FQDN option
// values received from clients.
TEST_F(FqdnDhcpv6SrvTest, sanitizeFqdn) {
    // Verify a full FQDN with no invalid chars is left alone
    testFqdn(DHCPV6_SOLICIT, Option6ClientFqdn::FLAG_S,
             "myhost.example.com",
             Option6ClientFqdn::FULL, Option6ClientFqdn::FLAG_S,
             "myhost.example.com.", false);

    // Verify that a partial FQDN with no invalid chars is left alone
    testFqdn(DHCPV6_SOLICIT, Option6ClientFqdn::FLAG_S,
             "myhost",
             Option6ClientFqdn::PARTIAL, Option6ClientFqdn::FLAG_S,
             "myhost.example.com.", false);

    // Verify that a full FQDN with invalid chars is cleaned.
    testFqdn(DHCPV6_SOLICIT, Option6ClientFqdn::FLAG_S,
             "m%y*host.example.com",
             Option6ClientFqdn::FULL, Option6ClientFqdn::FLAG_S,
             "mxyxhost.example.com.", false);

    // Verify that a partial FQDN with invalid chars is cleaned.
    testFqdn(DHCPV6_SOLICIT, Option6ClientFqdn::FLAG_S,
             "m%y*host",
             Option6ClientFqdn::PARTIAL, Option6ClientFqdn::FLAG_S,
             "mxyxhost.example.com.", false);

    // Verify that a full FQDN with nul chars is cleaned.
    testFqdn(DHCPV6_SOLICIT, Option6ClientFqdn::FLAG_S,
             std::string("m\000yhost.exa\000mple.com", 20),
             Option6ClientFqdn::FULL, Option6ClientFqdn::FLAG_S,
             "mxyhost.exaxmple.com.", false);
}

// Verifies that scoped ddns-parameter handling.
// Specifically that D2 can be enabled with sending updates
// disabled globally, and enabled at the subnet level.
TEST_F(FqdnDhcpv6SrvTest, ddnsScopeTest) {
    std::string config_str =
    "{ \"interfaces-config\": {\n"
        "  \"interfaces\": [ \"*\" ]\n"
        "},\n"
        "\"preferred-lifetime\": 3000,\n"
        "\"rebind-timer\": 2000,\n"
        "\"renew-timer\": 1000,\n"
        "\"ddns-send-updates\": false,\n"
        "\"subnet6\": [ {\n"
        "    \"id\": 1, \n"
        "    \"subnet\": \"2001:db8:1::/48\",\n"
        "    \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::10\" } ],\n"
        "    \"interface\": \"eth0\"\n"
        " },\n"
        " {\n"
        "    \"id\": 2, \n"
        "    \"subnet\": \"2001:db8:2::/48\",\n"
        "    \"pools\": [ { \"pool\": \"2001:db8:2::1 - 2001:db8:2::10\" } ],\n"
        "    \"interface\": \"eth1\",\n"
        "    \"ddns-send-updates\": true\n"
        " } ],\n"
        "\"valid-lifetime\": 4000,\n"
        " \"dhcp-ddns\": {\n"
        "     \"enable-updates\": true\n"
        " }\n"
    "}";

    Dhcp6Client client1;
    client1.setInterface("eth0");

    // Load a configuration with D2 enabled
    ASSERT_NO_FATAL_FAILURE(configure(config_str, *client1.getServer()));
    ASSERT_TRUE(CfgMgr::instance().ddnsEnabled());
    ASSERT_NO_THROW(client1.getServer()->startD2());

    // Include the Client FQDN option.
    ASSERT_NO_THROW(client1.useFQDN(Option6ClientFqdn::FLAG_S, "one.example.org.",
                                    Option6ClientFqdn::FULL));

    // Now send the DHCPREQUEST with including the FQDN option.
    ASSERT_NO_THROW(client1.doSARR());
    Pkt6Ptr resp = client1.getContext().response_;
    ASSERT_TRUE(resp);
    ASSERT_EQ(DHCPV6_REPLY, static_cast<int>(resp->getType()));

    // Check that the response FQDN is as expected.
    Option6ClientFqdnPtr fqdn;
    fqdn = boost::dynamic_pointer_cast<Option6ClientFqdn>(resp->getOption(D6O_CLIENT_FQDN));
    ASSERT_TRUE(fqdn);
    EXPECT_EQ("one.example.org.", fqdn->getDomainName());

    // ddns-send-updates for subnet 1 should be off, so we should NOT have an NRC.
    ASSERT_EQ(0, CfgMgr::instance().getD2ClientMgr().getQueueSize());

    // Now let's try with a client on subnet 2.
    Dhcp6Client client2;
    client2.setInterface("eth1");
    client2.requestAddress();

    // Include the Client FQDN option.
    ASSERT_NO_THROW(client2.useFQDN(Option6ClientFqdn::FLAG_S, "two.example.org.",
                                    Option6ClientFqdn::FULL));

    ASSERT_NO_THROW(client2.doSARR());
    resp = client2.getContext().response_;
    ASSERT_TRUE(resp);
    ASSERT_EQ(DHCPV6_REPLY, static_cast<int>(resp->getType()));

    // Check that the response FQDN is as expected.
    fqdn = boost::dynamic_pointer_cast<Option6ClientFqdn>(resp->getOption(D6O_CLIENT_FQDN));
    ASSERT_TRUE(fqdn);
    EXPECT_EQ("two.example.org.", fqdn->getDomainName());

    Subnet6Ptr subnet = (CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getSubnet(2));
    ASSERT_TRUE(subnet);
    DdnsParamsPtr p = (CfgMgr::instance().getCurrentCfg()->getDdnsParams(subnet));
    ASSERT_TRUE(p->getEnableUpdates());

    // ddns-send-updates for subnet 2 are enabled, verify the NCR is correct.
    ASSERT_EQ(1, CfgMgr::instance().getD2ClientMgr().getQueueSize());
    verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true, "2001:db8:2::1",
                            "", 0, 4000);
}

// Verifies that the DDNS parameters used for a lease in subnet in
// a shared-network belong to lease's subnet.  This checks that we
// get the right results even when the allocation engine changes the
// subnet chosen.  The configuration is two 1-address pool subnets in
// a shared-network. The first will do a SARR, which consumes the first
// pool. This should cause the allocation engine to dynamically select
// the second subnet for the second client.  The subnets define different
// values for qualifying suffixes, thus making it simple to verify
// the appropriate subnet parameters are used.  Both clients then
// renew their leases.
TEST_F(FqdnDhcpv6SrvTest, ddnsSharedNetworkTest) {
    std::string config_str =
    "{ \"interfaces-config\": { \n"
        "  \"interfaces\": [ \"*\" ] \n"
        "}, \n"
        "\"preferred-lifetime\": 3000, \n"
        "\"rebind-timer\": 2000, \n"
        "\"renew-timer\": 1000, \n"
        "\"valid-lifetime\": 4000, \n"
        "\"shared-networks\": [ \n"
        "{ \n"
            "\"name\": \"frog\", \n"
            "\"interface\": \"eth0\", \n"
            "\"subnet6\": [ { \n"
                "\"id\": 1, \n"
                "\"subnet\": \"2001:db8:1::/64\", \n"
                "\"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::1\" } ], \n"
                "\"interface\": \"eth0\", \n"
                "\"ddns-qualifying-suffix\": \"one.example.com.\" \n"
            " }, \n"
            " { \n"
                "\"id\": 2, \n"
                "\"subnet\": \"2001:db8:2::/64\", \n"
                "\"pools\": [ { \"pool\": \"2001:db8:2::1 - 2001:db8:2::1\" } ], \n"
                "\"interface\": \"eth0\", \n"
                "\"ddns-qualifying-suffix\": \"two.example.com.\" \n"
            " } ] \n"
        "} ], \n"
        "\"ddns-send-updates\": true, \n"
        "\"dhcp-ddns\": { \n"
        "     \"enable-updates\": true \n"
        " } \n"
    "}";

    Dhcp6Client client1;
    client1.setInterface("eth0");
    client1.requestAddress();

    // Load a configuration with D2 enabled
    ASSERT_NO_FATAL_FAILURE(configure(config_str, *client1.getServer()));
    ASSERT_TRUE(CfgMgr::instance().ddnsEnabled());
    ASSERT_NO_THROW(client1.getServer()->startD2());

    // Include the Client FQDN option.
    ASSERT_NO_THROW(client1.useFQDN(Option6ClientFqdn::FLAG_S, "client1",
                                    Option6ClientFqdn::PARTIAL));

    // Now do a SARR.
    ASSERT_NO_THROW(client1.doSARR());
    Pkt6Ptr resp = client1.getContext().response_;
    ASSERT_TRUE(resp);
    ASSERT_EQ(DHCPV6_REPLY, static_cast<int>(resp->getType()));

    // Check that the response FQDN is as expected.
    Option6ClientFqdnPtr fqdn;
    fqdn = boost::dynamic_pointer_cast<Option6ClientFqdn>(resp->getOption(D6O_CLIENT_FQDN));
    ASSERT_TRUE(fqdn);
    EXPECT_EQ("client1.one.example.com.", fqdn->getDomainName());

    // ddns-send-updates for subnet 1 are enabled, verify the NCR is correct.
    ASSERT_EQ(1, CfgMgr::instance().getD2ClientMgr().getQueueSize());
    verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true, "2001:db8:1::1",
                            "", 0, 4000, "client1.one.example.com.");

    // Make sure the lease hostname and fqdn flags are correct.
    Lease6Ptr lease = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
                                                            IOAddress("2001:db8:1::1"));
    ASSERT_TRUE(lease);
    EXPECT_EQ("client1.one.example.com.", lease->hostname_);
    EXPECT_TRUE(lease->fqdn_fwd_);
    EXPECT_TRUE(lease->fqdn_rev_);

    // Now let's try with a different client. Subnet 1 is full so we should get an
    // address from subnet 2.
    Dhcp6Client client2(client1.getServer());
    client2.setInterface("eth0");
    client2.requestAddress();

    // Include the Client FQDN option.
    ASSERT_NO_THROW(client2.useFQDN(Option6ClientFqdn::FLAG_S, "client2",
                                    Option6ClientFqdn::PARTIAL));

    // Do a SARR.
    ASSERT_NO_THROW(client2.doSARR());
    resp = client2.getContext().response_;
    ASSERT_TRUE(resp);
    ASSERT_EQ(DHCPV6_REPLY, static_cast<int>(resp->getType()));

    // Check that the response FQDN is as expected.
    fqdn = boost::dynamic_pointer_cast<Option6ClientFqdn>(resp->getOption(D6O_CLIENT_FQDN));
    ASSERT_TRUE(fqdn);
    EXPECT_EQ("client2.two.example.com.", fqdn->getDomainName());

    // ddns-send-updates for subnet 2 are enabled, verify the NCR is correct.
    ASSERT_EQ(1, CfgMgr::instance().getD2ClientMgr().getQueueSize());
    verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true, "2001:db8:2::1",
                            "", 0, 4000, "client2.two.example.com.");

    // Make sure the lease hostname and fqdn flags are correct.
    lease = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA, IOAddress("2001:db8:2::1"));
    ASSERT_TRUE(lease);
    EXPECT_EQ("client2.two.example.com.", lease->hostname_);
    EXPECT_TRUE(lease->fqdn_fwd_);
    EXPECT_TRUE(lease->fqdn_rev_);

    // Now let's check Renewals
    // First we'll renew a client2.
    ASSERT_NO_THROW(client2.doRenew());
    resp = client2.getContext().response_;
    ASSERT_TRUE(resp);
    ASSERT_EQ(DHCPV6_REPLY, static_cast<int>(resp->getType()));

    // Check that the response FQDN is as expected.
    fqdn = boost::dynamic_pointer_cast<Option6ClientFqdn>(resp->getOption(D6O_CLIENT_FQDN));
    ASSERT_TRUE(fqdn);
    EXPECT_EQ("client2.two.example.com.", fqdn->getDomainName());

    // ddns-send-updates for subnet 2 are enabled, but currently a renew/rebind does
    // not update, unless the FQDN or flags change.
    ASSERT_EQ(0, CfgMgr::instance().getD2ClientMgr().getQueueSize());

    // Make sure the lease hostname is still correct.
    lease = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA, IOAddress("2001:db8:2::1"));
    ASSERT_TRUE(lease);
    EXPECT_EQ("client2.two.example.com.", lease->hostname_);

    // Next we'll renew client1
    ASSERT_NO_THROW(client1.doRenew());
    resp = client1.getContext().response_;
    ASSERT_TRUE(resp);
    ASSERT_EQ(DHCPV6_REPLY, static_cast<int>(resp->getType()));

    // Check that the response FQDN is as expected.
    fqdn = boost::dynamic_pointer_cast<Option6ClientFqdn>(resp->getOption(D6O_CLIENT_FQDN));
    ASSERT_TRUE(fqdn);
    EXPECT_EQ("client1.one.example.com.", fqdn->getDomainName());

    // ddns-send-updates for subnet 1 are enabled, but currently a renew/rebind does
    // not update, unless the FQDN or flags change.
    ASSERT_EQ(0, CfgMgr::instance().getD2ClientMgr().getQueueSize());

    // Make sure the lease hostname and fqdn flags are correct.
    lease = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::1"));
    ASSERT_TRUE(lease);
    EXPECT_EQ("client1.one.example.com.", lease->hostname_);
    EXPECT_TRUE(lease->fqdn_fwd_);
    EXPECT_TRUE(lease->fqdn_rev_);
}

// Verifies lease and NCR content (or lack of NCRs) are correct when
// subnets in a shared-network define different values for send-ddns-updates
// This checks that we get the right results even when the allocation engine
// changes the subnet chosen.  The configuration is two 1-address pool subnets in
// a shared-network. The first client will do a SARR, which consumes the first
// pool. This should cause the allocation engine to dynamically select
// the second subnet for the second client.
TEST_F(FqdnDhcpv6SrvTest, ddnsSharedNetworkTest2) {
    std::string config_str =
    "{ \"interfaces-config\": { \n"
        "  \"interfaces\": [ \"*\" ] \n"
        "}, \n"
        "\"preferred-lifetime\": 3000, \n"
        "\"rebind-timer\": 2000, \n"
        "\"renew-timer\": 1000, \n"
        "\"valid-lifetime\": 4000, \n"
        "\"shared-networks\": [ \n"
        "{ \n"
            "\"name\": \"frog\", \n"
            "\"interface\": \"eth0\", \n"
            "\"subnet6\": [ { \n"
                "\"id\": 1, \n"
                "\"subnet\": \"2001:db8:1::/64\", \n"
                "\"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::1\" } ], \n"
                "\"interface\": \"eth0\", \n"
                "\"ddns-qualifying-suffix\": \"one.example.com.\", \n"
                "\"ddns-send-updates\": true \n"
            " }, \n"
            " { \n"
                "\"id\": 2, \n"
                "\"subnet\": \"2001:db8:2::/64\", \n"
                "\"pools\": [ { \"pool\": \"2001:db8:2::1 - 2001:db8:2::1\" } ], \n"
                "\"interface\": \"eth0\", \n"
                "\"ddns-qualifying-suffix\": \"two.example.com.\", \n"
                "\"ddns-send-updates\": false \n"
            " } ] \n"
        "} ], \n"
        "\"dhcp-ddns\": { \n"
        "     \"enable-updates\": true \n"
        " } \n"
    "}";

    Dhcp6Client client1;
    client1.setInterface("eth0");
    client1.requestAddress();

    // Load a configuration with D2 enabled
    ASSERT_NO_FATAL_FAILURE(configure(config_str, *client1.getServer()));
    ASSERT_TRUE(CfgMgr::instance().ddnsEnabled());
    ASSERT_NO_THROW(client1.getServer()->startD2());

    // Include the Client FQDN option.
    ASSERT_NO_THROW(client1.useFQDN(Option6ClientFqdn::FLAG_S, "client1",
                                    Option6ClientFqdn::PARTIAL));

    // Now do a SARR.
    ASSERT_NO_THROW(client1.doSARR());
    Pkt6Ptr resp = client1.getContext().response_;
    ASSERT_TRUE(resp);
    ASSERT_EQ(DHCPV6_REPLY, static_cast<int>(resp->getType()));

    // Check that the response FQDN is as expected.
    Option6ClientFqdnPtr fqdn;
    fqdn = boost::dynamic_pointer_cast<Option6ClientFqdn>(resp->getOption(D6O_CLIENT_FQDN));
    ASSERT_TRUE(fqdn);
    EXPECT_EQ("client1.one.example.com.", fqdn->getDomainName());

    // ddns-send-updates for subnet 1 are enabled, verify the NCR is correct.
    ASSERT_EQ(1, CfgMgr::instance().getD2ClientMgr().getQueueSize());
    verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true, "2001:db8:1::1",
                            "", 0, 4000, "client1.one.example.com.");

    // Make sure the lease hostname and fdqn flags are correct.
    Lease6Ptr lease = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
                                                            IOAddress("2001:db8:1::1"));
    ASSERT_TRUE(lease);
    EXPECT_EQ("client1.one.example.com.", lease->hostname_);
    EXPECT_TRUE(lease->fqdn_fwd_);
    EXPECT_TRUE(lease->fqdn_rev_);

    // Now let's try with a different client. Subnet 1 is full so we should get an
    // address from subnet 2.
    Dhcp6Client client2(client1.getServer());
    client2.setInterface("eth0");
    client2.requestAddress();

    // Include the Client FQDN option.
    ASSERT_NO_THROW(client2.useFQDN(Option6ClientFqdn::FLAG_S, "client2",
                                    Option6ClientFqdn::PARTIAL));

    // Do a SARR.
    ASSERT_NO_THROW(client2.doSARR());
    resp = client2.getContext().response_;
    ASSERT_TRUE(resp);
    ASSERT_EQ(DHCPV6_REPLY, static_cast<int>(resp->getType()));

    // Check that the response FQDN is as expected.
    fqdn = boost::dynamic_pointer_cast<Option6ClientFqdn>(resp->getOption(D6O_CLIENT_FQDN));
    ASSERT_TRUE(fqdn);
    EXPECT_EQ("client2.two.example.com.", fqdn->getDomainName());

    // ddns-send-updates for subnet 2 are disabled, verify there is no NCR.
    ASSERT_EQ(0, CfgMgr::instance().getD2ClientMgr().getQueueSize());

    // Make sure the lease hostname and fdqn flags are correct.
    lease = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA, IOAddress("2001:db8:2::1"));
    ASSERT_TRUE(lease);
    EXPECT_EQ("client2.two.example.com.", lease->hostname_);
    EXPECT_FALSE(lease->fqdn_fwd_);
    EXPECT_FALSE(lease->fqdn_rev_);
}

// Verifies that renews only generate NCRs if the situation dictates
// that it should. It checks:
//
// -# enable-updates true or false
// -# update-on-renew true or false
// -# Whether or not the FQDN has changed between old and new lease
TEST_F(FqdnDhcpv6SrvTest, processRequestRenew) {
    std::string fqdn1 = "one.example.com.";
    std::string fqdn2 = "two.example.com.";
    struct Scenario {
        std::string description_;
        bool send_updates_;
        bool update_on_renew_;
        std::string old_fqdn_;
        std::string new_fqdn_;
        size_t remove_;
        size_t add_;
    };

    // Mnemonic constants.
    const bool send_updates = true;
    const bool update_on_renew = true;
    const size_t remove = 1;
    const size_t add = 1;

    const std::vector<Scenario> scenarios = {
        {
        "#1 update-on-renew false, no change in fqdn",
        send_updates, !update_on_renew, fqdn1, fqdn1, !remove, !add
        },
        {
        "#2 update-on-renew is false, change in fqdn",
        send_updates, !update_on_renew, fqdn1, fqdn2, remove, add
        },
        {
        "#3 update-on-renew is true, no change in fqdn",
        send_updates, update_on_renew, fqdn1, fqdn1, remove, add
        },
        {
        "#4 update-on-renew is true, change in fqdn",
        send_updates, update_on_renew, fqdn1, fqdn2, remove, add
        },
        // All prior scenarios test with send-updates true.  We really
        // only need one with it false.
        {
        "#5 send-updates false, update-on-renew is true, change in fqdn",
        !send_updates, update_on_renew, fqdn1, fqdn2, !remove, !add
        }
    };

    enableD2();
    subnet_->setDdnsReplaceClientNameMode(D2ClientConfig::RCM_NEVER);

    // Iterate over test scenarios.
    for (auto const& scenario : scenarios) {
        SCOPED_TRACE(scenario.description_); {
            // Make sure the lease does not exist.
            ASSERT_FALSE(LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
                                                               IOAddress("2001:db8:1:1::dead:beef")));
            // Set and verify DDNS params flags
            subnet_->setDdnsSendUpdates(scenario.send_updates_);
            subnet_->setDdnsUpdateOnRenew(scenario.update_on_renew_);

            ASSERT_EQ(scenario.send_updates_, getDdnsParams()->getEnableUpdates());
            ASSERT_EQ(scenario.update_on_renew_, getDdnsParams()->getUpdateOnRenew());

            // Create the "old" lease
            testProcessMessage(DHCPV6_REQUEST, scenario.old_fqdn_, scenario.old_fqdn_);

            // The lease should have been recorded in the database.
            Lease6Ptr old_lease = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
                                                                        IOAddress("2001:db8:1:1::dead:beef"));
            ASSERT_TRUE(old_lease);

            if (!scenario.send_updates_ || scenario.old_fqdn_.empty()) {
                // We should not have an NCR.
                ASSERT_EQ(0, d2_mgr_.getQueueSize());
            } else {
                // We should have an NCR add.
                ASSERT_EQ(1, d2_mgr_.getQueueSize());
                verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
                                        old_lease->addr_.toText(), "",
                                        0, old_lease->valid_lft_,
                                        old_lease->hostname_);
            }

            // Now let's renew (or create) the lease.
            testProcessMessage(DHCPV6_RENEW, scenario.new_fqdn_, scenario.new_fqdn_);

            // The lease should have been recorded in the database.
            Lease6Ptr new_lease = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
                                                                        IOAddress("2001:db8:1:1::dead:beef"));
            ASSERT_TRUE(new_lease);

            // Verify queue count is correct.
            ASSERT_EQ((scenario.remove_ + scenario.add_), d2_mgr_.getQueueSize());

            // If we expect a remove, check it.
            if (scenario.remove_ > 0) {
                // Verify NCR content
                verifyNameChangeRequest(isc::dhcp_ddns::CHG_REMOVE, true, true,
                                        old_lease->addr_.toText(), "",
                                        0, old_lease->valid_lft_,
                                        old_lease->hostname_);
            }

            // If we expect an add, check it.
            if (scenario.add_ > 0) {
                // Verify NCR content
                verifyNameChangeRequest(isc::dhcp_ddns::CHG_ADD, true, true,
                                        new_lease->addr_.toText(), "",
                                        0, new_lease->valid_lft_,
                                        new_lease->hostname_);
            }

            // Now delete the lease.
            bool deleted = false;
            ASSERT_NO_THROW(deleted = LeaseMgrFactory::instance().deleteLease(new_lease));
            ASSERT_TRUE(deleted);
        }
    }
}

// Verify the ddns-ttl-percent is used when specified.
TEST_F(FqdnDhcpv6SrvTest, ddnsTtlPercentTest) {
    testDdnsTtlParameters(2100, 0.5);
}

// Verify the ddns-ttl is used when specified.
TEST_F(FqdnDhcpv6SrvTest, ddnsTtlTest) {
    testDdnsTtlParameters(2100,                     // valid lft
                          Optional<double>(),       // percent
                          999,                      // ttl
                          Optional<uint32_t>(),     // min 
                          Optional<uint32_t>());    // max
}

// Verify the ddns-ttl-min is used when specified.
TEST_F(FqdnDhcpv6SrvTest, ddnsTtlMinTest) {
    testDdnsTtlParameters(2100,                     // valid lft
                          Optional<double>(),       // percent
                          Optional<uint32_t>(),     // ttl 
                          800,                      // ttl-min
                          Optional<uint32_t>());    // ttl-max
}

// Verify the ddns-ttl-max is used when specified.
TEST_F(FqdnDhcpv6SrvTest, ddnsTtlMaxTest) {
    testDdnsTtlParameters(2100,                     // valid lft
                          Optional<double>(),       // percent
                          Optional<uint32_t>(),     // ttl 
                          Optional<uint32_t>(),     // ttl-min
                          500);                     // ttl-max 
}

} // end of anonymous namespace