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
// Copyright (C) 2012-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/dhcp4.h>
#include <dhcp/dhcp6.h>
#include <dhcp/option4_addrlst.h>
#include <dhcp/option6_addrlst.h>
#include <dhcp/option6_ia.h>
#include <dhcp/option6_iaaddr.h>
#include <dhcp/option4_dnr.h>
#include <dhcp/option6_dnr.h>
#include <dhcp/option_classless_static_route.h>
#include <dhcp/option_custom.h>
#include <dhcp/option_definition.h>
#include <dhcp/option_int.h>
#include <dhcp/option_int_array.h>
#include <dhcp/option_string.h>
#include <dhcp/option_opaque_data_tuples.h>
#include <exceptions/exceptions.h>

#include <boost/pointer_cast.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/shared_ptr.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 std;
using namespace isc;
using namespace isc::dhcp;
using namespace isc::util;

namespace {

/// @brief OptionDefinition test class.
///
/// This class does not do anything useful but we keep
/// it around for the future.
class OptionDefinitionTest : public ::testing::Test {
public:
    /// @brief Constructor
    OptionDefinitionTest() { }

};

// The purpose of this test is to verify that OptionDefinition
// constructor initializes its members correctly.
TEST_F(OptionDefinitionTest, constructor) {
    // Specify the option data type as string. This should get converted
    // to enum value returned by getType().
    OptionDefinition opt_def1("OPTION_CLIENTID", D6O_CLIENTID,
                              DHCP6_OPTION_SPACE, "string");
    EXPECT_EQ("OPTION_CLIENTID", opt_def1.getName());
    EXPECT_EQ(1, opt_def1.getCode());
    EXPECT_EQ(DHCP6_OPTION_SPACE, opt_def1.getOptionSpaceName());
    EXPECT_EQ(OPT_STRING_TYPE, opt_def1.getType());
    EXPECT_FALSE(opt_def1.getArrayType());
    EXPECT_TRUE(opt_def1.getEncapsulatedSpace().empty());
    EXPECT_NO_THROW(opt_def1.validate());

    // Specify the option data type as an enum value.
    OptionDefinition opt_def2("OPTION_RAPID_COMMIT", D6O_RAPID_COMMIT,
                              DHCP6_OPTION_SPACE, OPT_EMPTY_TYPE);
    EXPECT_EQ("OPTION_RAPID_COMMIT", opt_def2.getName());
    EXPECT_EQ(14, opt_def2.getCode());
    EXPECT_EQ(DHCP6_OPTION_SPACE, opt_def2.getOptionSpaceName());
    EXPECT_EQ(OPT_EMPTY_TYPE, opt_def2.getType());
    EXPECT_FALSE(opt_def2.getArrayType());
    EXPECT_TRUE(opt_def2.getEncapsulatedSpace().empty());
    EXPECT_NO_THROW(opt_def2.validate());

    // Specify encapsulated option space name and option data type
    // as enum value.
    OptionDefinition opt_def3("OPTION_VENDOR_OPTS", D6O_VENDOR_OPTS,
                              DHCP6_OPTION_SPACE, OPT_UINT32_TYPE, "isc");
    EXPECT_EQ("OPTION_VENDOR_OPTS", opt_def3.getName());
    EXPECT_EQ(D6O_VENDOR_OPTS, opt_def3.getCode());
    EXPECT_EQ(DHCP6_OPTION_SPACE, opt_def3.getOptionSpaceName());
    EXPECT_EQ(OPT_UINT32_TYPE, opt_def3.getType());
    EXPECT_FALSE(opt_def3.getArrayType());
    EXPECT_EQ("isc", opt_def3.getEncapsulatedSpace());
    EXPECT_NO_THROW(opt_def3.validate());

    // Specify encapsulated option space name and option data type
    // as string value.
    OptionDefinition opt_def4("OPTION_VENDOR_OPTS", D6O_VENDOR_OPTS,
                              DHCP6_OPTION_SPACE, "uint32", "isc");
    EXPECT_EQ("OPTION_VENDOR_OPTS", opt_def4.getName());
    EXPECT_EQ(D6O_VENDOR_OPTS, opt_def4.getCode());
    EXPECT_EQ(DHCP6_OPTION_SPACE, opt_def4.getOptionSpaceName());
    EXPECT_EQ(OPT_UINT32_TYPE, opt_def4.getType());
    EXPECT_FALSE(opt_def4.getArrayType());
    EXPECT_EQ("isc", opt_def4.getEncapsulatedSpace());
    EXPECT_NO_THROW(opt_def4.validate());

    // Check if it is possible to set that option is an array.
    OptionDefinition opt_def5("OPTION_NIS_SERVERS", 27,
                              DHCP6_OPTION_SPACE,
                              OPT_IPV6_ADDRESS_TYPE,
                              true);
    EXPECT_EQ("OPTION_NIS_SERVERS", opt_def5.getName());
    EXPECT_EQ(27, opt_def5.getCode());
    EXPECT_EQ(DHCP6_OPTION_SPACE, opt_def5.getOptionSpaceName());
    EXPECT_EQ(OPT_IPV6_ADDRESS_TYPE, opt_def5.getType());
    EXPECT_TRUE(opt_def5.getArrayType());
    EXPECT_NO_THROW(opt_def5.validate());

    // The created object is invalid if invalid data type is specified but
    // constructor shouldn't throw exception. The object is validated after
    // it has been created.
    EXPECT_NO_THROW(<--- There is an unknown macro here somewhere. Configuration is required. If EXPECT_NO_THROW is a macro then please configure it.
        OptionDefinition opt_def6("OPTION_SERVERID",
                                  OPT_UNKNOWN_TYPE + 10,
                                  DHCP6_OPTION_SPACE,
                                  OPT_STRING_TYPE);
    );
}

// This test checks that the copy constructor works properly.
TEST_F(OptionDefinitionTest, copyConstructor) {
    OptionDefinition opt_def("option-foo", 27, "my-space", "record", true);
    ASSERT_NO_THROW(opt_def.addRecordField("uint16"));
    ASSERT_NO_THROW(opt_def.addRecordField("string"));

    OptionDefinition opt_def_copy(opt_def);
    EXPECT_EQ("option-foo", opt_def_copy.getName());
    EXPECT_EQ(27, opt_def_copy.getCode());
    EXPECT_EQ("my-space", opt_def_copy.getOptionSpaceName());
    EXPECT_TRUE(opt_def_copy.getArrayType());
    EXPECT_TRUE(opt_def_copy.getEncapsulatedSpace().empty());
    ASSERT_EQ(OPT_RECORD_TYPE, opt_def_copy.getType());
    const OptionDefinition::RecordFieldsCollection fields =
        opt_def_copy.getRecordFields();
    ASSERT_EQ(2, fields.size());
    EXPECT_EQ(OPT_UINT16_TYPE, fields[0]);
    EXPECT_EQ(OPT_STRING_TYPE, fields[1]);

    // Let's make another test to check if encapsulated option space is
    // copied properly.
    OptionDefinition opt_def2("option-bar", 30, "my-space", "uint32", "isc");
    OptionDefinition opt_def_copy2(opt_def2);
    EXPECT_EQ("option-bar", opt_def_copy2.getName());
    EXPECT_EQ(30, opt_def_copy2.getCode());
    EXPECT_EQ("my-space", opt_def_copy2.getOptionSpaceName());
    EXPECT_FALSE(opt_def_copy2.getArrayType());
    EXPECT_EQ(OPT_UINT32_TYPE, opt_def_copy2.getType());
    EXPECT_EQ("isc", opt_def_copy2.getEncapsulatedSpace());
}

// This test checks that the factory function taking string option
// data type as argument creates a valid instance.
TEST_F(OptionDefinitionTest, createStringType) {
    auto def = OptionDefinition::create("option-foo", 123, "my-space",
                                        "uint16", "isc");
    ASSERT_TRUE(def);

    EXPECT_EQ("option-foo", def->getName());
    EXPECT_EQ(123, def->getCode());
    EXPECT_EQ("my-space", def->getOptionSpaceName());
    EXPECT_EQ(OPT_UINT16_TYPE, def->getType());
    EXPECT_FALSE(def->getArrayType());
    EXPECT_EQ("isc", def->getEncapsulatedSpace());
}

// This test checks that the factory function taking enum option
// data type as argument creates a valid instance.
TEST_F(OptionDefinitionTest, createEnumType) {
    auto def = OptionDefinition::create("option-foo", 123, "my-space",
                                        OPT_UINT16_TYPE, "isc");
    ASSERT_TRUE(def);

    EXPECT_EQ("option-foo", def->getName());
    EXPECT_EQ(123, def->getCode());
    EXPECT_EQ("my-space", def->getOptionSpaceName());
    EXPECT_EQ(OPT_UINT16_TYPE, def->getType());
    EXPECT_FALSE(def->getArrayType());
    EXPECT_EQ("isc", def->getEncapsulatedSpace());
}

// This test checks that the factory function creating an array and
// taking string option data type as argument creates a valid instance.
TEST_F(OptionDefinitionTest, createStringTypeArray) {
    auto def = OptionDefinition::create("option-foo", 123, "my-space",
                                        "uint16", true);
    ASSERT_TRUE(def);

    EXPECT_EQ("option-foo", def->getName());
    EXPECT_EQ(123, def->getCode());
    EXPECT_EQ("my-space", def->getOptionSpaceName());
    EXPECT_EQ(OPT_UINT16_TYPE, def->getType());
    EXPECT_TRUE(def->getArrayType());
    EXPECT_TRUE(def->getEncapsulatedSpace().empty());
}

// This test checks that the factory function creating an array and
// taking enum option data type as argument creates a valid instance.
TEST_F(OptionDefinitionTest, createEnumTypeArray) {
    auto def = OptionDefinition::create("option-foo", 123, "my-space",
                                        OPT_UINT16_TYPE, true);
    ASSERT_TRUE(def);

    EXPECT_EQ("option-foo", def->getName());
    EXPECT_EQ(123, def->getCode());
    EXPECT_EQ("my-space", def->getOptionSpaceName());
    EXPECT_EQ(OPT_UINT16_TYPE, def->getType());
    EXPECT_TRUE(def->getArrayType());
    EXPECT_TRUE(def->getEncapsulatedSpace().empty());
}

// This test checks that two option definitions may be compared for equality.
TEST_F(OptionDefinitionTest, equality) {
    // Equal definitions.
    EXPECT_TRUE(OptionDefinition("option-foo", 5, "my-space", "uint16", false)
                == OptionDefinition("option-foo", 5, "my-space", "uint16", false));
    EXPECT_FALSE(OptionDefinition("option-foo", 5, "my-space", "uint16", false)
                 != OptionDefinition("option-foo", 5, "my-space", "uint16", false));

    // Differ by name.
    EXPECT_FALSE(OptionDefinition("option-foo", 5, "my-space", "uint16", false)
                 == OptionDefinition("option-foobar", 5, "my-space", "uint16", false));
    EXPECT_FALSE(OptionDefinition("option-bar", 5, "my-space", "uint16", false)
                == OptionDefinition("option-foo", 5, "my-space", "uint16", false));
    EXPECT_TRUE(OptionDefinition("option-bar", 5, "my-space", "uint16", false)
                 != OptionDefinition("option-foo", 5, "my-space", "uint16", false));

    // Differ by option code.
    EXPECT_FALSE(OptionDefinition("option-foo", 5, "my-space", "uint16", false)
                == OptionDefinition("option-foo", 6, "my-space", "uint16", false));
    EXPECT_TRUE(OptionDefinition("option-foo", 5, "my-space", "uint16", false)
                 != OptionDefinition("option-foo", 6, "my-space", "uint16", false));

    // Differ by option space name.
    EXPECT_FALSE(OptionDefinition("option-foo", 5, "my-space", "uint16", false)
                == OptionDefinition("option-foo", 5, "mi-space", "uint16", false));
    EXPECT_TRUE(OptionDefinition("option-foo", 5, "my-space", "uint16", false)
                 != OptionDefinition("option-foo", 5, "mi-space", "uint16", false));

    // Differ by type of the data.
    EXPECT_FALSE(OptionDefinition("option-foo", 5, "my-space", "uint16", false)
                == OptionDefinition("option-foo", 5, "my-space", "uint32", false));
    EXPECT_TRUE(OptionDefinition("option-foo", 5, "my-space", "uint16", false)
                 != OptionDefinition("option-foo", 5, "my-space", "uint32", false));

    // Differ by array-type property.
    EXPECT_FALSE(OptionDefinition("option-foo", 5, "my-space", "uint16", false)
                == OptionDefinition("option-foo", 5, "my-space", "uint16", true));
    EXPECT_TRUE(OptionDefinition("option-foo", 5, "my-space", "uint16", false)
                 != OptionDefinition("option-foo", 5, "my-space", "uint16", true));

    // Differ by record fields.
    OptionDefinition def1("option-foo", 5, "my-space", "record");
    OptionDefinition def2("option-foo", 5, "my-space", "record");

    // There are no record fields specified yet, so initially they have
    // to be equal.
    ASSERT_TRUE(def1 == def2);
    ASSERT_FALSE(def1 != def2);

    // Add some record fields.
    ASSERT_NO_THROW(def1.addRecordField("uint16"));
    ASSERT_NO_THROW(def2.addRecordField("uint16"));

    // Definitions should still remain equal.
    ASSERT_TRUE(def1 == def2);
    ASSERT_FALSE(def1 != def2);

    // Add additional record field to one of the definitions but not the
    // other. They should now be unequal.
    ASSERT_NO_THROW(def1.addRecordField("string"));
    ASSERT_FALSE(def1 == def2);
    ASSERT_TRUE(def1 != def2);

    // Add the same record field to the other definition. They should now
    // be equal again.
    ASSERT_NO_THROW(def2.addRecordField("string"));
    EXPECT_TRUE(def1 == def2);
    EXPECT_FALSE(def1 != def2);
}

// The purpose of this test is to verify that various data fields
// can be specified for an option definition when this definition
// is marked as 'record' and that fields can't be added if option
// definition is not marked as 'record'.
TEST_F(OptionDefinitionTest, addRecordField) {
    // We can only add fields to record if the option type has been
    // specified as 'record'. We try all other types but 'record'
    // here and expect exception to be thrown.
    for (int i = 0; i < OPT_UNKNOWN_TYPE; ++i) {
        // Do not try for 'record' type because this is the only
        // type for which adding record will succeed.
        if (i == OPT_RECORD_TYPE) {
            continue;
        }
        OptionDefinition opt_def("OPTION_IAADDR", 5, DHCP6_OPTION_SPACE,
                                 static_cast<OptionDataType>(i));
        EXPECT_THROW(opt_def.addRecordField("uint8"), isc::InvalidOperation);
    }

    // Positive scenario starts here.
    OptionDefinition opt_def("OPTION_IAADDR", 5, DHCP6_OPTION_SPACE, "record");
    EXPECT_NO_THROW(opt_def.addRecordField("ipv6-address"));
    EXPECT_NO_THROW(opt_def.addRecordField("uint32"));
    // It should not matter if we specify field type by its name or using enum.
    EXPECT_NO_THROW(opt_def.addRecordField(OPT_UINT32_TYPE));

    // Check what we have actually added.
    OptionDefinition::RecordFieldsCollection fields = opt_def.getRecordFields();
    ASSERT_EQ(3, fields.size());
    EXPECT_EQ(OPT_IPV6_ADDRESS_TYPE, fields[0]);
    EXPECT_EQ(OPT_UINT32_TYPE, fields[1]);
    EXPECT_EQ(OPT_UINT32_TYPE, fields[2]);

    // Let's try some more negative scenarios: use invalid data types.
    EXPECT_THROW(opt_def.addRecordField("unknown_type_xyz"), isc::BadValue);
    OptionDataType invalid_type =
        static_cast<OptionDataType>(OPT_UNKNOWN_TYPE + 10);
    EXPECT_THROW(opt_def.addRecordField(invalid_type), isc::BadValue);

    // It is bad if we use 'record' option type but don't specify
    // at least two fields.
    OptionDefinition opt_def2("OPTION_EMPTY_RECORD", 100, "my-space", "record");
    EXPECT_THROW(opt_def2.validate(), MalformedOptionDefinition);
    opt_def2.addRecordField("uint8");
    EXPECT_THROW(opt_def2.validate(), MalformedOptionDefinition);
    opt_def2.addRecordField("uint32");
    EXPECT_NO_THROW(opt_def2.validate());
}

// The purpose of this test is to check that validate() function
// reports errors for invalid option definitions.
TEST_F(OptionDefinitionTest, validate) {
    // Not supported option type string is not allowed.
    OptionDefinition opt_def1("OPTION_CLIENTID", D6O_CLIENTID,
                              DHCP6_OPTION_SPACE, "non-existent-type");
    EXPECT_THROW(opt_def1.validate(), MalformedOptionDefinition);

    // Not supported option type enum value is not allowed.
    OptionDefinition opt_def2("OPTION_CLIENTID", D6O_CLIENTID,
                              DHCP6_OPTION_SPACE, OPT_UNKNOWN_TYPE);
    EXPECT_THROW(opt_def2.validate(), MalformedOptionDefinition);

    OptionDefinition opt_def3("OPTION_CLIENTID", D6O_CLIENTID,
                              DHCP6_OPTION_SPACE,
                              static_cast<OptionDataType>(OPT_UNKNOWN_TYPE
                                                                      + 2));
    EXPECT_THROW(opt_def3.validate(), MalformedOptionDefinition);

    // Empty option name is not allowed.
    OptionDefinition opt_def4("", D6O_CLIENTID, DHCP6_OPTION_SPACE, "string");
    EXPECT_THROW(opt_def4.validate(), MalformedOptionDefinition);

    // Option name must not contain spaces.
    OptionDefinition opt_def5(" OPTION_CLIENTID", D6O_CLIENTID,
                              DHCP6_OPTION_SPACE, "string");
    EXPECT_THROW(opt_def5.validate(), MalformedOptionDefinition);

    // Option name must not contain spaces.
    OptionDefinition opt_def6("OPTION CLIENTID", D6O_CLIENTID,
                              DHCP6_OPTION_SPACE, "string");
    EXPECT_THROW(opt_def6.validate(), MalformedOptionDefinition);

    // Option name may contain lower case letters.
    OptionDefinition opt_def7("option_clientid", D6O_CLIENTID,
                              DHCP6_OPTION_SPACE, "string");
    EXPECT_NO_THROW(opt_def7.validate());

    // Using digits in option name is legal.
    OptionDefinition opt_def8("option_123", D6O_CLIENTID, DHCP6_OPTION_SPACE,
                              "string");
    EXPECT_NO_THROW(opt_def8.validate());

    // Using hyphen is legal.
    OptionDefinition opt_def9("option-clientid", D6O_CLIENTID,
                              DHCP6_OPTION_SPACE, "string");
    EXPECT_NO_THROW(opt_def9.validate());

    // Using hyphen or underscore at the beginning or at the end
    // of the option name is not allowed.
    OptionDefinition opt_def10("-option-clientid", D6O_CLIENTID,
                               DHCP6_OPTION_SPACE, "string");
    EXPECT_THROW(opt_def10.validate(), MalformedOptionDefinition);

    OptionDefinition opt_def11("_option-clientid", D6O_CLIENTID,
                               DHCP6_OPTION_SPACE, "string");
    EXPECT_THROW(opt_def11.validate(), MalformedOptionDefinition);

    OptionDefinition opt_def12("option-clientid_", D6O_CLIENTID,
                               DHCP6_OPTION_SPACE, "string");
    EXPECT_THROW(opt_def12.validate(), MalformedOptionDefinition);

    OptionDefinition opt_def13("option-clientid-", D6O_CLIENTID,
                               DHCP6_OPTION_SPACE, "string");
    EXPECT_THROW(opt_def13.validate(), MalformedOptionDefinition);

    // Empty option space name is not allowed.
    OptionDefinition opt_def14("OPTION_CLIENTID", D6O_CLIENTID, "", "string");
    EXPECT_THROW(opt_def14.validate(), MalformedOptionDefinition);

    // Option name must not contain spaces.
    OptionDefinition opt_def15("OPTION_CLIENTID", D6O_CLIENTID, " space",
                               "string");
    EXPECT_THROW(opt_def15.validate(), MalformedOptionDefinition);

    // Option name must not contain spaces.
    OptionDefinition opt_def16("OPTION_CLIENTID", D6O_CLIENTID, "my space",
                               "string");
    EXPECT_THROW(opt_def16.validate(), MalformedOptionDefinition);

    // Option name may contain upper case letters.
    OptionDefinition opt_def17("OPTION_CLIENTID", D6O_CLIENTID, "SPACE",
                               "string");
    EXPECT_NO_THROW(opt_def17.validate());

    // Using digits in option name is legal.
    OptionDefinition opt_def18("OPTION_CLIENTID", D6O_CLIENTID, "space_123",
                               "string");
    EXPECT_NO_THROW(opt_def18.validate());

    // Using hyphen is legal.
    OptionDefinition opt_def19("OPTION_CLIENTID", D6O_CLIENTID, "my-space",
                               "string");
    EXPECT_NO_THROW(opt_def19.validate());

    // Using hyphen or underscore at the beginning or at the end
    // of the option name is not allowed.
    OptionDefinition opt_def20("OPTION_CLIENTID", D6O_CLIENTID, "-space",
                               "string");
    EXPECT_THROW(opt_def20.validate(), MalformedOptionDefinition);

    OptionDefinition opt_def21("OPTION_CLIENTID", D6O_CLIENTID, "_space",
                               "string");
    EXPECT_THROW(opt_def21.validate(), MalformedOptionDefinition);

    OptionDefinition opt_def22("OPTION_CLIENTID", D6O_CLIENTID, "space_",
                               "string");
    EXPECT_THROW(opt_def22.validate(), MalformedOptionDefinition);

    OptionDefinition opt_def23("OPTION_CLIENTID", D6O_CLIENTID, "space-",
                               "string");
    EXPECT_THROW(opt_def23.validate(), MalformedOptionDefinition);

    // Having array of strings does not make sense because there is no way
    // to determine string's length.
    OptionDefinition opt_def24("OPTION_CLIENTID", D6O_CLIENTID,
                               DHCP6_OPTION_SPACE, "string", true);
    EXPECT_THROW(opt_def24.validate(), MalformedOptionDefinition);

    // It does not make sense to have string field within the record before
    // other fields because there is no way to determine the length of this
    // string and thus there is no way to determine where the other field
    // begins.
    OptionDefinition opt_def25("OPTION_STATUS_CODE", D6O_STATUS_CODE,
                               DHCP6_OPTION_SPACE, "record");
    opt_def25.addRecordField("string");
    opt_def25.addRecordField("uint16");
    EXPECT_THROW(opt_def25.validate(), MalformedOptionDefinition);

    // ... but it is ok if the string value is the last one.
    OptionDefinition opt_def26("OPTION_STATUS_CODE", D6O_STATUS_CODE,
                               DHCP6_OPTION_SPACE, "record");
    opt_def26.addRecordField("uint8");
    opt_def26.addRecordField("string");
    EXPECT_NO_THROW(opt_def26.validate());

    // ... at least if it is not an array.
    OptionDefinition opt_def27("OPTION_STATUS_CODE", D6O_STATUS_CODE,
                               DHCP6_OPTION_SPACE, "record", true);
    opt_def27.addRecordField("uint8");
    opt_def27.addRecordField("string");
    EXPECT_THROW(opt_def27.validate(), MalformedOptionDefinition);

    // Check invalid encapsulated option space name.
    OptionDefinition opt_def28("OPTION_VENDOR_OPTS", D6O_VENDOR_OPTS,
                               DHCP6_OPTION_SPACE, "uint32",
                               "invalid%space%name");
    EXPECT_THROW(opt_def28.validate(), MalformedOptionDefinition);
}


// The purpose of this test is to verify that option definition
// that comprises array of IPv6 addresses will return an instance
// of option with a list of IPv6 addresses.
TEST_F(OptionDefinitionTest, ipv6AddressArray) {
    OptionDefinition opt_def("OPTION_NIS_SERVERS", D6O_NIS_SERVERS,
                             DHCP6_OPTION_SPACE, "ipv6-address", true);

    // Create a list of some V6 addresses.
    std::vector<asiolink::IOAddress> addrs;
    addrs.push_back(asiolink::IOAddress("2001:0db8::ff00:0042:8329"));
    addrs.push_back(asiolink::IOAddress("2001:0db8::ff00:0042:2319"));
    addrs.push_back(asiolink::IOAddress("::1"));
    addrs.push_back(asiolink::IOAddress("::2"));

    // Write addresses to the buffer.
    OptionBuffer buf(addrs.size() * asiolink::V6ADDRESS_LEN);
    for (size_t i = 0; i < addrs.size(); ++i) {
        const std::vector<uint8_t>& vec = addrs[i].toBytes();
        ASSERT_EQ(asiolink::V6ADDRESS_LEN, vec.size());
        std::copy(vec.begin(), vec.end(),
                  buf.begin() + i * asiolink::V6ADDRESS_LEN);
    }
    // Create DHCPv6 option from this buffer. Once option is created it is
    // supposed to have internal list of addresses that it parses out from
    // the provided buffer.
    OptionPtr option_v6;
    ASSERT_NO_THROW(
        option_v6 = opt_def.optionFactory(Option::V6, D6O_NIS_SERVERS, buf);
    );
    const Option* optptr = option_v6.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(Option6AddrLst));
    boost::shared_ptr<Option6AddrLst> option_cast_v6 =
        boost::static_pointer_cast<Option6AddrLst>(option_v6);
    ASSERT_TRUE(option_cast_v6);
    // Get the list of parsed addresses from the option object.
    std::vector<asiolink::IOAddress> addrs_returned =
        option_cast_v6->getAddresses();
    // The list of addresses must exactly match addresses that we
    // stored in the buffer to create the option from it.
    EXPECT_TRUE(std::equal(addrs.begin(), addrs.end(), addrs_returned.begin()));

    // The provided buffer's length must be a multiple of V6 address length.
    // Let's extend the buffer by one byte so as this condition is not
    // fulfilled anymore.
    buf.insert(buf.end(), 1, 1);
    // It should throw exception then.
    EXPECT_THROW(
        opt_def.optionFactory(Option::V6, D6O_NIS_SERVERS, buf),
        InvalidOptionValue
    );
}

// The purpose of this test is to verify that option definition
// that comprises array of IPv6 addresses will return an instance
// of option with a list of IPv6 addresses. Array of IPv6 addresses
// is specified as a vector of strings (each string represents single
// IPv6 address).
TEST_F(OptionDefinitionTest, ipv6AddressArrayTokenized) {
    OptionDefinition opt_def("OPTION_NIS_SERVERS", D6O_NIS_SERVERS,
                             DHCP6_OPTION_SPACE, "ipv6-address", true);

    // Create a vector of some V6 addresses.
    std::vector<asiolink::IOAddress> addrs;
    addrs.push_back(asiolink::IOAddress("2001:0db8::ff00:0042:8329"));
    addrs.push_back(asiolink::IOAddress("2001:0db8::ff00:0042:2319"));
    addrs.push_back(asiolink::IOAddress("::1"));
    addrs.push_back(asiolink::IOAddress("::2"));

    // Create a vector of strings representing addresses given above.
    std::vector<std::string> addrs_str;
    for (auto const& it : addrs) {
        addrs_str.push_back(it.toText());
    }

    // Create DHCPv6 option using the list of IPv6 addresses given in the
    // string form.
    OptionPtr option_v6;
    ASSERT_NO_THROW(
        option_v6 = opt_def.optionFactory(Option::V6, D6O_NIS_SERVERS,
                                          addrs_str);
    );
    // Non-null pointer option is supposed to be returned and it
    // should have Option6AddrLst type.
    ASSERT_TRUE(option_v6);
    const Option* optptr = option_v6.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(Option6AddrLst));
    // Cast to the actual option type to get IPv6 addresses from it.
    boost::shared_ptr<Option6AddrLst> option_cast_v6 =
        boost::static_pointer_cast<Option6AddrLst>(option_v6);
    // Check that cast was successful.
    ASSERT_TRUE(option_cast_v6);
    // Get the list of parsed addresses from the option object.
    std::vector<asiolink::IOAddress> addrs_returned =
        option_cast_v6->getAddresses();
    // Returned addresses must match the addresses that have been used to create
    // the option instance.
    EXPECT_TRUE(std::equal(addrs.begin(), addrs.end(), addrs_returned.begin()));
}

// The purpose of this test is to verify that option definition
// that comprises array of IPv4 addresses will return an instance
// of option with a list of IPv4 addresses.
TEST_F(OptionDefinitionTest, ipv4AddressArray) {
    OptionDefinition opt_def("OPTION_NAME_SERVERS", D6O_NIS_SERVERS,
                             DHCP6_OPTION_SPACE, "ipv4-address", true);

    // Create a list of some V6 addresses.
    std::vector<asiolink::IOAddress> addrs;
    addrs.push_back(asiolink::IOAddress("192.168.0.1"));
    addrs.push_back(asiolink::IOAddress("172.16.1.1"));
    addrs.push_back(asiolink::IOAddress("127.0.0.1"));
    addrs.push_back(asiolink::IOAddress("213.41.23.12"));

    // Write addresses to the buffer.
    OptionBuffer buf(addrs.size() * asiolink::V4ADDRESS_LEN);
    for (size_t i = 0; i < addrs.size(); ++i) {
        const std::vector<uint8_t> vec = addrs[i].toBytes();
        ASSERT_EQ(asiolink::V4ADDRESS_LEN, vec.size());
        std::copy(vec.begin(), vec.end(),
                  buf.begin() + i * asiolink::V4ADDRESS_LEN);
    }
    // Create DHCPv6 option from this buffer. Once option is created it is
    // supposed to have internal list of addresses that it parses out from
    // the provided buffer.
    OptionPtr option_v4;
    ASSERT_NO_THROW(
        option_v4 = opt_def.optionFactory(Option::V4, DHO_NAME_SERVERS, buf)
    );
    const Option* optptr = option_v4.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(Option4AddrLst));
    // Get the list of parsed addresses from the option object.
    boost::shared_ptr<Option4AddrLst> option_cast_v4 =
        boost::static_pointer_cast<Option4AddrLst>(option_v4);
    std::vector<asiolink::IOAddress> addrs_returned =
        option_cast_v4->getAddresses();
    // The list of addresses must exactly match addresses that we
    // stored in the buffer to create the option from it.
    EXPECT_TRUE(std::equal(addrs.begin(), addrs.end(), addrs_returned.begin()));

    // The provided buffer's length must be a multiple of V4 address length.
    // Let's extend the buffer by one byte so as this condition is not
    // fulfilled anymore.
    buf.insert(buf.end(), 1, 1);
    // It should throw exception then.
    EXPECT_THROW(opt_def.optionFactory(Option::V4, DHO_NIS_SERVERS, buf),
                 InvalidOptionValue);
}

// The purpose of this test is to verify that option definition
// that comprises array of IPv4 addresses will return an instance
// of option with a list of IPv4 addresses. The array of IPv4 addresses
// is specified as a vector of strings (each string represents single
// IPv4 address).
TEST_F(OptionDefinitionTest, ipv4AddressArrayTokenized) {
    OptionDefinition opt_def("OPTION_NIS_SERVERS", DHO_NIS_SERVERS,
                             DHCP4_OPTION_SPACE, "ipv4-address", true);

    // Create a vector of some V6 addresses.
    std::vector<asiolink::IOAddress> addrs;
    addrs.push_back(asiolink::IOAddress("192.168.0.1"));
    addrs.push_back(asiolink::IOAddress("172.16.1.1"));
    addrs.push_back(asiolink::IOAddress("127.0.0.1"));
    addrs.push_back(asiolink::IOAddress("213.41.23.12"));

    // Create a vector of strings representing addresses given above.
    std::vector<std::string> addrs_str;
    for (auto const& it : addrs) {
        addrs_str.push_back(it.toText());
    }

    // Create DHCPv4 option using the list of IPv4 addresses given in the
    // string form.
    OptionPtr option_v4;
    ASSERT_NO_THROW(
        option_v4 = opt_def.optionFactory(Option::V4, DHO_NIS_SERVERS,
                                          addrs_str);
    );
    // Non-null pointer option is supposed to be returned and it
    // should have Option6AddrLst type.
    ASSERT_TRUE(option_v4);
    const Option* optptr = option_v4.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(Option4AddrLst));
    // Cast to the actual option type to get IPv4 addresses from it.
    boost::shared_ptr<Option4AddrLst> option_cast_v4 =
        boost::static_pointer_cast<Option4AddrLst>(option_v4);
    // Check that cast was successful.
    ASSERT_TRUE(option_cast_v4);
    // Get the list of parsed addresses from the option object.
    std::vector<asiolink::IOAddress> addrs_returned =
        option_cast_v4->getAddresses();
    // Returned addresses must match the addresses that have been used to create
    // the option instance.
    EXPECT_TRUE(std::equal(addrs.begin(), addrs.end(), addrs_returned.begin()));
}

// The purpose of this test is to verify that option definition for
// 'empty' option can be created and that it returns 'empty' option.
TEST_F(OptionDefinitionTest, empty) {
    OptionDefinition opt_def("OPTION_RAPID_COMMIT", D6O_RAPID_COMMIT,
                             DHCP6_OPTION_SPACE, "empty");

    // Create option instance and provide empty buffer as expected.
    OptionPtr option_v6;
    ASSERT_NO_THROW(
        option_v6 = opt_def.optionFactory(Option::V6, D6O_RAPID_COMMIT, OptionBuffer())
    );
    const Option* optptr = option_v6.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(Option));
    // Expect 'empty' DHCPv6 option.
    EXPECT_EQ(Option::V6, option_v6->getUniverse());
    EXPECT_EQ(4, option_v6->getHeaderLen());
    EXPECT_EQ(0, option_v6->getData().size());

    // Repeat the same test scenario for DHCPv4 option.
    OptionPtr option_v4;
    ASSERT_NO_THROW(option_v4 = opt_def.optionFactory(Option::V4, 214, OptionBuffer()));
    // Expect 'empty' DHCPv4 option.
    EXPECT_EQ(Option::V4, option_v4->getUniverse());
    EXPECT_EQ(2, option_v4->getHeaderLen());
    EXPECT_EQ(0, option_v4->getData().size());
}

// The purpose of this test is to verify that when the empty option encapsulates
// some option space, an instance of the OptionCustom is returned and its
// suboptions are decoded.
TEST_F(OptionDefinitionTest, emptyWithSuboptions) {
    // Create an instance of the 'empty' option definition. This option
    // encapsulates 'option-foo-space' so when we create a new option
    // with this definition the OptionCustom should be returned. The
    // Option Custom is generic option which support variety of formats
    // and supports decoding suboptions.
    OptionDefinition opt_def("option-foo", 1024, "my-space", "empty",
                             "option-foo-space");
    // Define a suboption.
    const uint8_t subopt_data[] = {
        0x04, 0x01,  // Option code 1025
        0x00, 0x04,  // Option len = 4
        0x01, 0x02, 0x03, 0x04 // Option data
    };

    // Create an option, having option code 1024 from the definition. Pass
    // the option buffer containing suboption.
    OptionPtr option_v6;
    ASSERT_NO_THROW(
        option_v6 = opt_def.optionFactory(Option::V6, 1024,
                                          OptionBuffer(subopt_data,
                                                       subopt_data +
                                                       sizeof(subopt_data)))
    );
    // Returned option should be of the OptionCustom type.
    const Option* optptr = option_v6.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(OptionCustom));
    // Sanity-check length, universe etc.
    EXPECT_EQ(Option::V6, option_v6->getUniverse());
    EXPECT_EQ(4, option_v6->getHeaderLen());
    // This option should have one suboption with the code of 1025.
    OptionPtr subopt_v6 = option_v6->getOption(1025);
    EXPECT_TRUE(subopt_v6);
    // Check that this suboption holds valid data.
    EXPECT_EQ(1025, subopt_v6->getType());
    EXPECT_EQ(Option::V6, subopt_v6->getUniverse());
    EXPECT_EQ(0, memcmp(&subopt_v6->getData()[0], subopt_data + 4, 4));

    // @todo consider having a similar test for V4.
}

// The purpose of this test is to verify that definition can be
// creates for the option that holds binary data.
TEST_F(OptionDefinitionTest, binary) {
    // Binary option is the one that is represented by the generic
    // Option class. In fact all options can be represented by this
    // class but for some of them it is just natural. The SERVERID
    // option consists of the option code, length and binary data so
    // this one was picked for this test.
    OptionDefinition opt_def("OPTION_SERVERID", D6O_SERVERID,
                             DHCP6_OPTION_SPACE, "binary");

    // Prepare some dummy data (serverid): 0, 1, 2 etc.
    OptionBuffer buf(14);
    for (unsigned i = 0; i < 14; ++i) {
        buf[i] = i;
    }
    // Create option instance with the factory function.
    // If the OptionDefinition code works properly than
    // object of the type Option should be returned.
    OptionPtr option_v6;
    ASSERT_NO_THROW(
        option_v6 = opt_def.optionFactory(Option::V6, D6O_SERVERID, buf);
    );
    // Expect base option type returned.
    const Option* optptr = option_v6.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(Option));
    // Sanity check on universe, length and size. These are
    // the basic parameters identifying any option.
    EXPECT_EQ(Option::V6, option_v6->getUniverse());
    EXPECT_EQ(4, option_v6->getHeaderLen());
    ASSERT_EQ(buf.size(), option_v6->getData().size());

    // Get the server id data from the option and compare
    // against reference buffer. They are expected to match.
    EXPECT_TRUE(std::equal(option_v6->getData().begin(),
                           option_v6->getData().end(),
                           buf.begin()));

    // Repeat the same test scenario for DHCPv4 option.
    OptionPtr option_v4;
    ASSERT_NO_THROW(option_v4 = opt_def.optionFactory(Option::V4, 214, buf));
    // Expect 'empty' DHCPv4 option.
    EXPECT_EQ(Option::V4, option_v4->getUniverse());
    EXPECT_EQ(2, option_v4->getHeaderLen());
    ASSERT_EQ(buf.size(), option_v4->getData().size());

    EXPECT_TRUE(std::equal(option_v6->getData().begin(),
                           option_v6->getData().end(),
                           buf.begin()));
}

// The purpose of this test is to verify that definition can be created
// for option that comprises record of data. In this particular test
// the IA_NA option is used. This option comprises three uint32 fields.
TEST_F(OptionDefinitionTest, recordIA6) {
    // This option consists of IAID, T1 and T2 fields (each 4 bytes long).
    const int option6_ia_len = 12;

    // Get the factory function pointer.
    OptionDefinition opt_def("OPTION_IA_NA", D6O_IA_NA, DHCP6_OPTION_SPACE,
                             "record", false);
    // Each data field is uint32.
    for (int i = 0; i < 3; ++i) {
        EXPECT_NO_THROW(opt_def.addRecordField("uint32"));
    }

    // Check the positive scenario.
    OptionBuffer buf(12);
    for (size_t i = 0; i < buf.size(); ++i) {
        buf[i] = i;
    }
    OptionPtr option_v6;
    ASSERT_NO_THROW(option_v6 = opt_def.optionFactory(Option::V6, D6O_IA_NA, buf));
    const Option* optptr = option_v6.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(Option6IA));
    boost::shared_ptr<Option6IA> option_cast_v6 =
        boost::static_pointer_cast<Option6IA>(option_v6);
    EXPECT_EQ(0x00010203, option_cast_v6->getIAID());
    EXPECT_EQ(0x04050607, option_cast_v6->getT1());
    EXPECT_EQ(0x08090A0B, option_cast_v6->getT2());

    // The length of the buffer must be at least 12 bytes.
    // Check too short buffer.
    EXPECT_THROW(
        opt_def.optionFactory(Option::V6, D6O_IA_NA, OptionBuffer(option6_ia_len - 1)),
        InvalidOptionValue
     );
}

// The purpose of this test is to verify that definition can be created
// for option that comprises record of data. In this particular test
// the IAADDR option is used.
TEST_F(OptionDefinitionTest, recordIAAddr6) {
    // This option consists of IPV6 Address (16 bytes) and preferred-lifetime and
    // valid-lifetime fields (each 4 bytes long).
    const int option6_iaaddr_len = 24;

    OptionDefinition opt_def("OPTION_IAADDR", D6O_IAADDR, DHCP6_OPTION_SPACE,
                             "record");
    ASSERT_NO_THROW(opt_def.addRecordField("ipv6-address"));
    ASSERT_NO_THROW(opt_def.addRecordField("uint32"));
    ASSERT_NO_THROW(opt_def.addRecordField("uint32"));

    // Check the positive scenario.
    OptionPtr option_v6;
    asiolink::IOAddress addr_v6("2001:0db8::ff00:0042:8329");
    OptionBuffer buf(asiolink::V6ADDRESS_LEN);
    ASSERT_TRUE(addr_v6.isV6());
    const std::vector<uint8_t>& vec = addr_v6.toBytes();
    ASSERT_EQ(asiolink::V6ADDRESS_LEN, vec.size());
    std::copy(vec.begin(), vec.end(), buf.begin());

    for (unsigned i = 0;
         i < option6_iaaddr_len - asiolink::V6ADDRESS_LEN;
         ++i) {
        buf.push_back(i);
    }
    ASSERT_NO_THROW(option_v6 = opt_def.optionFactory(Option::V6, D6O_IAADDR, buf));
    const Option* optptr = option_v6.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(Option6IAAddr));
    boost::shared_ptr<Option6IAAddr> option_cast_v6 =
        boost::static_pointer_cast<Option6IAAddr>(option_v6);
    EXPECT_EQ(addr_v6, option_cast_v6->getAddress());
    EXPECT_EQ(0x00010203, option_cast_v6->getPreferred());
    EXPECT_EQ(0x04050607, option_cast_v6->getValid());

    // The length of the buffer must be at least 12 bytes.
    // Check too short buffer.
    EXPECT_THROW(
        opt_def.optionFactory(Option::V6, D6O_IAADDR, OptionBuffer(option6_iaaddr_len - 1)),
        InvalidOptionValue
     );
}

// The purpose of this test is to verify that definition can be created
// for option that comprises record of data. In this particular test
// the IAADDR option is used. The data for the option is specified as
// a vector of strings. Each string carries the data for the corresponding
// data field.
TEST_F(OptionDefinitionTest, recordIAAddr6Tokenized) {
    // This option consists of IPV6 Address (16 bytes) and preferred-lifetime and
    // valid-lifetime fields (each 4 bytes long).
    OptionDefinition opt_def("OPTION_IAADDR", D6O_IAADDR, DHCP6_OPTION_SPACE,
                             "record");
    ASSERT_NO_THROW(opt_def.addRecordField("ipv6-address"));
    ASSERT_NO_THROW(opt_def.addRecordField("uint32"));
    ASSERT_NO_THROW(opt_def.addRecordField("uint32"));

    // Check the positive scenario.
    std::vector<std::string> data_field_values;
    data_field_values.push_back("2001:0db8::ff00:0042:8329");
    data_field_values.push_back("1234");
    data_field_values.push_back("5678");

    OptionPtr option_v6;
    ASSERT_NO_THROW(option_v6 = opt_def.optionFactory(Option::V6, D6O_IAADDR,
                                                      data_field_values));
    const Option* optptr = option_v6.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(Option6IAAddr));
    boost::shared_ptr<Option6IAAddr> option_cast_v6 =
        boost::static_pointer_cast<Option6IAAddr>(option_v6);
    EXPECT_EQ("2001:db8::ff00:42:8329", option_cast_v6->getAddress().toText());
    EXPECT_EQ(1234, option_cast_v6->getPreferred());
    EXPECT_EQ(5678, option_cast_v6->getValid());
}

// The purpose of this test is to verify that the definition for option
// that comprises a boolean value can be created and that this definition
// can be used to create and option with a single boolean value.
TEST_F(OptionDefinitionTest, boolValue) {
    // The IP Forwarding option comprises one boolean value.
    OptionDefinition opt_def("ip-forwarding", DHO_IP_FORWARDING,
                             DHCP4_OPTION_SPACE, "boolean");

    OptionPtr option_v4;
    // Use an option buffer which holds one value of 1 (true).
    ASSERT_NO_THROW(
        option_v4 = opt_def.optionFactory(Option::V4, DHO_IP_FORWARDING,
                                          OptionBuffer(1, 1));
    );
    const Option* optptr = option_v4.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(OptionCustom));
    // Validate parsed value in the received option.
    boost::shared_ptr<OptionCustom> option_cast_v4 =
        boost::static_pointer_cast<OptionCustom>(option_v4);
    EXPECT_TRUE(option_cast_v4->readBoolean());

    // Repeat the test above, but set the value to 0 (false).
    ASSERT_NO_THROW(
        option_v4 = opt_def.optionFactory(Option::V4, DHO_IP_FORWARDING,
                                          OptionBuffer(1, 0));
    );
    option_cast_v4 = boost::static_pointer_cast<OptionCustom>(option_v4);
    EXPECT_FALSE(option_cast_v4->readBoolean());

    // Try to provide zero-length buffer. Expect exception.
    EXPECT_THROW(
        opt_def.optionFactory(Option::V4, DHO_IP_FORWARDING, OptionBuffer()),
        InvalidOptionValue
    );

}

// The purpose of this test is to verify that definition for option that
// comprises single boolean value can be created and that this definition
// can be used to create an option holding a single boolean value. The
// boolean value is converted from a string which is expected to hold
// the following values: "true", "false", "1" or "0". For all other
// values exception should be thrown.
TEST_F(OptionDefinitionTest, boolTokenized) {
    OptionDefinition opt_def("ip-forwarding", DHO_IP_FORWARDING,
                             DHCP6_OPTION_SPACE, "boolean");

    OptionPtr option_v4;
    std::vector<std::string> values;
    // Specify a value for the option instance being created.
    values.push_back("true");
    ASSERT_NO_THROW(
        option_v4 = opt_def.optionFactory(Option::V4, DHO_IP_FORWARDING,
                                          values);
    );
    const Option* optptr = option_v4.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(OptionCustom));
    // Validate the value.
    OptionCustomPtr option_cast_v4 =
        boost::static_pointer_cast<OptionCustom>(option_v4);
    EXPECT_TRUE(option_cast_v4->readBoolean());

    // Repeat the test but for "false" value this time.
    values[0] = "false";
    ASSERT_NO_THROW(
        option_v4 = opt_def.optionFactory(Option::V4, DHO_IP_FORWARDING,
                                          values);
    );
    optptr = option_v4.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(OptionCustom));
    // Validate the value.
    option_cast_v4 = boost::static_pointer_cast<OptionCustom>(option_v4);
    EXPECT_FALSE(option_cast_v4->readBoolean());

    // Check if that will work for numeric values.
    values[0] = "0";
    ASSERT_NO_THROW(
        option_v4 = opt_def.optionFactory(Option::V4, DHO_IP_FORWARDING,
                                          values);
    );
    optptr = option_v4.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(OptionCustom));
    // Validate the value.
    option_cast_v4 = boost::static_pointer_cast<OptionCustom>(option_v4);
    EXPECT_FALSE(option_cast_v4->readBoolean());

    // Swap numeric values and test if it works for "true" case.
    values[0] = "1";
    ASSERT_NO_THROW(
        option_v4 = opt_def.optionFactory(Option::V4, DHO_IP_FORWARDING,
                                          values);
    );
    optptr = option_v4.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(OptionCustom));
    // Validate the value.
    option_cast_v4 = boost::static_pointer_cast<OptionCustom>(option_v4);
    EXPECT_TRUE(option_cast_v4->readBoolean());

    // A conversion of non-numeric value to boolean should fail if
    // this value is neither "true" nor "false".
    values[0] = "garbage";
    EXPECT_THROW(opt_def.optionFactory(Option::V4, DHO_IP_FORWARDING, values),
      isc::dhcp::BadDataTypeCast);

    // A conversion of numeric value to boolean should fail if this value
    // is neither "0" nor "1".
    values[0] = "2";
    EXPECT_THROW(opt_def.optionFactory(Option::V4, DHO_IP_FORWARDING, values),
      isc::dhcp::BadDataTypeCast);

}

// The purpose of this test is to verify that definition for option that
// comprises single uint8 value can be created and that this definition
// can be used to create an option with single uint8 value.
TEST_F(OptionDefinitionTest, uint8) {
    OptionDefinition opt_def("OPTION_PREFERENCE", D6O_PREFERENCE,
                             DHCP6_OPTION_SPACE, "uint8");

    OptionPtr option_v6;
    // Try to use correct buffer length = 1 byte.
    ASSERT_NO_THROW(
        option_v6 = opt_def.optionFactory(Option::V6, D6O_PREFERENCE,
                                          OptionBuffer(1, 1));
    );
    const Option* optptr = option_v6.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(OptionInt<uint8_t>));
    // Validate the value.
    boost::shared_ptr<OptionInt<uint8_t> > option_cast_v6 =
        boost::static_pointer_cast<OptionInt<uint8_t> >(option_v6);
    EXPECT_EQ(1, option_cast_v6->getValue());

    // Try to provide zero-length buffer. Expect exception.
    EXPECT_THROW(
        option_v6 = opt_def.optionFactory(Option::V6, D6O_PREFERENCE, OptionBuffer()),
        InvalidOptionValue
    );

    // @todo Add more cases for DHCPv4
}

// The purpose of this test is to verify that definition for option that
// comprises single uint8 value can be created and that this definition
// can be used to create an option with single uint8 value.
TEST_F(OptionDefinitionTest, uint8Tokenized) {
    OptionDefinition opt_def("OPTION_PREFERENCE", D6O_PREFERENCE,
                             DHCP6_OPTION_SPACE, "uint8");

    OptionPtr option_v6;
    std::vector<std::string> values;
    values.push_back("123");
    values.push_back("456");
    ASSERT_NO_THROW(
        option_v6 = opt_def.optionFactory(Option::V6, D6O_PREFERENCE, values);
    );
    const Option* optptr = option_v6.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(OptionInt<uint8_t>));
    // Validate the value.
    boost::shared_ptr<OptionInt<uint8_t> > option_cast_v6 =
        boost::static_pointer_cast<OptionInt<uint8_t> >(option_v6);
    EXPECT_EQ(123, option_cast_v6->getValue());

    // @todo Add more cases for DHCPv4
}

// The purpose of this test is to verify that definition for option that
// comprises single uint16 value can be created and that this definition
// can be used to create an option with single uint16 value.
TEST_F(OptionDefinitionTest, uint16) {
    OptionDefinition opt_def("OPTION_ELAPSED_TIME", D6O_ELAPSED_TIME,
                             DHCP6_OPTION_SPACE, "uint16");

    OptionPtr option_v6;
    // Try to use correct buffer length = 2 bytes.
    OptionBuffer buf;
    buf.push_back(1);
    buf.push_back(2);
    ASSERT_NO_THROW(
        option_v6 = opt_def.optionFactory(Option::V6, D6O_ELAPSED_TIME, buf);
    );
    const Option* optptr = option_v6.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(OptionInt<uint16_t>));
    // Validate the value.
    boost::shared_ptr<OptionInt<uint16_t> > option_cast_v6 =
        boost::static_pointer_cast<OptionInt<uint16_t> >(option_v6);
    EXPECT_EQ(0x0102, option_cast_v6->getValue());

    // Try to provide zero-length buffer. Expect exception.
    EXPECT_THROW(
        option_v6 = opt_def.optionFactory(Option::V6, D6O_ELAPSED_TIME, OptionBuffer(1)),
        InvalidOptionValue
    );

    // @todo Add more cases for DHCPv4
}

// The purpose of this test is to verify that definition for option that
// comprises single uint16 value can be created and that this definition
// can be used to create an option with single uint16 value.
TEST_F(OptionDefinitionTest, uint16Tokenized) {
    OptionDefinition opt_def("OPTION_ELAPSED_TIME", D6O_ELAPSED_TIME,
                             DHCP6_OPTION_SPACE, "uint16");

    OptionPtr option_v6;

    std::vector<std::string> values;
    values.push_back("1234");
    values.push_back("5678");
    ASSERT_NO_THROW(
        option_v6 = opt_def.optionFactory(Option::V6, D6O_ELAPSED_TIME, values);
    );
    const Option* optptr = option_v6.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(OptionInt<uint16_t>));
    // Validate the value.
    boost::shared_ptr<OptionInt<uint16_t> > option_cast_v6 =
        boost::static_pointer_cast<OptionInt<uint16_t> >(option_v6);
    EXPECT_EQ(1234, option_cast_v6->getValue());

    // @todo Add more cases for DHCPv4

}

// The purpose of this test is to verify that definition for option that
// comprises single uint32 value can be created and that this definition
// can be used to create an option with single uint32 value.
TEST_F(OptionDefinitionTest, uint32) {
    OptionDefinition opt_def("OPTION_CLT_TIME", D6O_CLT_TIME,
                             DHCP6_OPTION_SPACE, "uint32");

    OptionPtr option_v6;
    OptionBuffer buf;
    buf.push_back(1);
    buf.push_back(2);
    buf.push_back(3);
    buf.push_back(4);
    ASSERT_NO_THROW(
        option_v6 = opt_def.optionFactory(Option::V6, D6O_CLT_TIME, buf);
    );
    const Option* optptr = option_v6.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(OptionInt<uint32_t>));
    // Validate the value.
    boost::shared_ptr<OptionInt<uint32_t> > option_cast_v6 =
        boost::static_pointer_cast<OptionInt<uint32_t> >(option_v6);
    EXPECT_EQ(0x01020304, option_cast_v6->getValue());

    // Try to provide too short buffer. Expect exception.
    EXPECT_THROW(
        option_v6 = opt_def.optionFactory(Option::V6, D6O_CLT_TIME, OptionBuffer(2)),
        InvalidOptionValue
    );

    // @todo Add more cases for DHCPv4
}

// The purpose of this test is to verify that definition for option that
// comprises single uint32 value can be created and that this definition
// can be used to create an option with single uint32 value.
TEST_F(OptionDefinitionTest, uint32Tokenized) {
    OptionDefinition opt_def("OPTION_CLT_TIME", D6O_CLT_TIME,
                             DHCP6_OPTION_SPACE, "uint32");

    OptionPtr option_v6;
    std::vector<std::string> values;
    values.push_back("123456");
    values.push_back("789");
    ASSERT_NO_THROW(
        option_v6 = opt_def.optionFactory(Option::V6, D6O_CLT_TIME, values);
    );
    const Option* optptr = option_v6.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(OptionInt<uint32_t>));
    // Validate the value.
    boost::shared_ptr<OptionInt<uint32_t> > option_cast_v6 =
        boost::static_pointer_cast<OptionInt<uint32_t> >(option_v6);
    EXPECT_EQ(123456, option_cast_v6->getValue());

    // @todo Add more cases for DHCPv4
}

// The purpose of this test is to verify that definition for option that
// comprises array of uint16 values can be created and that this definition
// can be used to create option with an array of uint16 values.
TEST_F(OptionDefinitionTest, uint16Array) {
    // Let's define some dummy option.
    const uint16_t opt_code = 79;
    OptionDefinition opt_def("OPTION_UINT16_ARRAY", opt_code, "my-space",
                             "uint16", true);

    OptionPtr option_v6;
    // Positive scenario, initiate the buffer with length being
    // multiple of uint16_t size.
    // buffer elements will be: 0x112233.
    OptionBuffer buf(6);
    for (unsigned i = 0; i < 6; ++i) {
        buf[i] = i / 2;
    }
    // Constructor should succeed because buffer has correct size.
    EXPECT_NO_THROW(
        option_v6 = opt_def.optionFactory(Option::V6, opt_code, buf);
    );
    const Option* optptr = option_v6.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(OptionIntArray<uint16_t>));
    boost::shared_ptr<OptionIntArray<uint16_t> > option_cast_v6 =
        boost::static_pointer_cast<OptionIntArray<uint16_t> >(option_v6);
    // Get the values from the initiated options and validate.
    std::vector<uint16_t> values = option_cast_v6->getValues();
    for (size_t i = 0; i < values.size(); ++i) {
        // Expected value is calculated using on the same pattern
        // as the one we used to initiate buffer:
        // for i=0, expected = 0x00, for i = 1, expected == 0x11 etc.
        uint16_t expected = (i << 8) | i;
        EXPECT_EQ(expected, values[i]);
    }

    // Provided buffer size must be greater than zero. Check if we
    // get exception if we provide zero-length buffer.
    EXPECT_THROW(
        option_v6 = opt_def.optionFactory(Option::V6, opt_code, OptionBuffer()),
        InvalidOptionValue
    );
    // Buffer length must be multiple of data type size.
    EXPECT_THROW(
        option_v6 = opt_def.optionFactory(Option::V6, opt_code, OptionBuffer(5)),
        InvalidOptionValue
    );
}

// The purpose of this test is to verify that definition for option that
// comprises array of uint16 values can be created and that this definition
// can be used to create option with an array of uint16 values.
TEST_F(OptionDefinitionTest, uint16ArrayTokenized) {
    // Let's define some dummy option.
    const uint16_t opt_code = 79;
    OptionDefinition opt_def("OPTION_UINT16_ARRAY", opt_code, "my-space",
                             "uint16", true);

    OptionPtr option_v6;
    std::vector<std::string> str_values;
    str_values.push_back("12345");
    str_values.push_back("5679");
    str_values.push_back("12");
    EXPECT_NO_THROW(
        option_v6 = opt_def.optionFactory(Option::V6, opt_code, str_values);
    );
    const Option* optptr = option_v6.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(OptionIntArray<uint16_t>));
    boost::shared_ptr<OptionIntArray<uint16_t> > option_cast_v6 =
        boost::static_pointer_cast<OptionIntArray<uint16_t> >(option_v6);
    // Get the values from the initiated options and validate.
    std::vector<uint16_t> values = option_cast_v6->getValues();
    EXPECT_EQ(12345, values[0]);
    EXPECT_EQ(5679, values[1]);
    EXPECT_EQ(12, values[2]);
}

// The purpose of this test is to verify that definition for option that
// comprises array of uint32 values can be created and that this definition
// can be used to create option with an array of uint32 values.
TEST_F(OptionDefinitionTest, uint32Array) {
    // Let's define some dummy option.
    const uint16_t opt_code = 80;

    OptionDefinition opt_def("OPTION_UINT32_ARRAY", opt_code, "my-space",
                             "uint32", true);

    OptionPtr option_v6;
    // Positive scenario, initiate the buffer with length being
    // multiple of uint16_t size.
    // buffer elements will be: 0x111122223333.
    OptionBuffer buf(12);
    for (size_t i = 0; i < buf.size(); ++i) {
        buf[i] = i / 4;
    }
    // Constructor should succeed because buffer has correct size.
    EXPECT_NO_THROW(
        option_v6 = opt_def.optionFactory(Option::V6, opt_code, buf);
    );
    const Option* optptr = option_v6.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(OptionIntArray<uint32_t>));
    boost::shared_ptr<OptionIntArray<uint32_t> > option_cast_v6 =
        boost::static_pointer_cast<OptionIntArray<uint32_t> >(option_v6);
    // Get the values from the initiated options and validate.
    std::vector<uint32_t> values = option_cast_v6->getValues();
    for (size_t i = 0; i < values.size(); ++i) {
        // Expected value is calculated using on the same pattern
        // as the one we used to initiate buffer:
        // for i=0, expected = 0x0000, for i = 1, expected == 0x1111 etc.
        uint32_t expected = 0x01010101 * i;
        EXPECT_EQ(expected, values[i]);
    }

    // Provided buffer size must be greater than zero. Check if we
    // get exception if we provide zero-length buffer.
    EXPECT_THROW(
        option_v6 = opt_def.optionFactory(Option::V6, opt_code, OptionBuffer()),
        InvalidOptionValue
    );
    // Buffer length must be multiple of data type size.
    EXPECT_THROW(
        option_v6 = opt_def.optionFactory(Option::V6, opt_code, OptionBuffer(5)),
        InvalidOptionValue
    );
}

// The purpose of this test is to verify that definition for option that
// comprises array of uint32 values can be created and that this definition
// can be used to create option with an array of uint32 values.
TEST_F(OptionDefinitionTest, uint32ArrayTokenized) {
    // Let's define some dummy option.
    const uint16_t opt_code = 80;

    OptionDefinition opt_def("OPTION_UINT32_ARRAY", opt_code, "my-space",
                             "uint32", true);

    OptionPtr option_v6;
    std::vector<std::string> str_values;
    str_values.push_back("123456");
    // Try with hexadecimal
    str_values.push_back("0x7");
    str_values.push_back("256");
    str_values.push_back("1111");

    EXPECT_NO_THROW(
        option_v6 = opt_def.optionFactory(Option::V6, opt_code, str_values);
    );
    const Option* optptr = option_v6.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(OptionIntArray<uint32_t>));
    boost::shared_ptr<OptionIntArray<uint32_t> > option_cast_v6 =
        boost::static_pointer_cast<OptionIntArray<uint32_t> >(option_v6);
    // Get the values from the initiated options and validate.
    std::vector<uint32_t> values = option_cast_v6->getValues();
    EXPECT_EQ(123456, values[0]);
    EXPECT_EQ(7, values[1]);
    EXPECT_EQ(256, values[2]);
    EXPECT_EQ(1111, values[3]);
}

// The purpose of this test is to verify that the definition can be created
// for the option that comprises string value in the UTF8 format.
TEST_F(OptionDefinitionTest, utf8StringTokenized) {
    // Let's create some dummy option.
    const uint16_t opt_code = 80;
    OptionDefinition opt_def("OPTION_WITH_STRING", opt_code, "my-space",
                             "string");

    std::vector<std::string> values;
    values.push_back("Hello World");
    values.push_back("this string should not be included in the option");
    OptionPtr option_v6;
    EXPECT_NO_THROW(
        option_v6 = opt_def.optionFactory(Option::V6, opt_code, values);
    );
    ASSERT_TRUE(option_v6);
    const Option* optptr = option_v6.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(OptionString));
    OptionStringPtr option_v6_string =
        boost::static_pointer_cast<OptionString>(option_v6);
    EXPECT_TRUE(values[0] == option_v6_string->getValue());
}

// The purpose of this test is to check that non-integer data type can't
// be used for factoryInteger function.
TEST_F(OptionDefinitionTest, integerInvalidType) {
    // The template function factoryInteger<> accepts integer values only
    // as template typename. Here we try passing different type and
    // see if it rejects it.
    OptionBuffer buf(1);
    EXPECT_THROW(
        OptionDefinition::factoryInteger<bool>(Option::V6, D6O_PREFERENCE, DHCP6_OPTION_SPACE,
                                               buf.begin(), buf.end()),
        isc::dhcp::InvalidDataType
    );
}

// This test verifies that a definition of an option with a single IPv6
// prefix can be created and used to create an instance of the option.
TEST_F(OptionDefinitionTest, prefix) {
    OptionDefinition opt_def("option-prefix", 1000, "my-space", "ipv6-prefix");

    // Create a buffer holding a prefix.
    OptionBuffer buf;
    buf.push_back(32);
    buf.push_back(0x30);
    buf.push_back(0x00);
    buf.resize(5);

    OptionPtr option_v6;

    // Create an instance of this option from the definition.
    ASSERT_NO_THROW(
        option_v6 = opt_def.optionFactory(Option::V6, 1000, buf);
    );

    // Make sure that the returned option class is correct.
    const Option* optptr = option_v6.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(OptionCustom));

    // Validate the value.
    OptionCustomPtr option_cast_v6 =
        boost::dynamic_pointer_cast<OptionCustom>(option_v6);
    ASSERT_EQ(1, option_cast_v6->getDataFieldsNum());
    PrefixTuple prefix = option_cast_v6->readPrefix();
    EXPECT_EQ(32, prefix.first.asUnsigned());
    EXPECT_EQ("3000::", prefix.second.toText());
}

// This test verifies that a definition of an option with a single IPv6
// prefix can be created and that the instance of this option can be
// created by specifying the prefix in the textual format.
TEST_F(OptionDefinitionTest, prefixTokenized) {
    OptionDefinition opt_def("option-prefix", 1000, "my-space", "ipv6-prefix");

    OptionPtr option_v6;
    // Specify a single prefix.
    std::vector<std::string> values(1, "2001:db8:1::/64");

    // Create an instance of the option using the definition.
    ASSERT_NO_THROW(
        option_v6 = opt_def.optionFactory(Option::V6, 1000, values);
    );

    // Make sure that the returned option class is correct.
    const Option* optptr = option_v6.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(OptionCustom));

    // Validate the value.
    OptionCustomPtr option_cast_v6 =
        boost::dynamic_pointer_cast<OptionCustom>(option_v6);
    ASSERT_EQ(1, option_cast_v6->getDataFieldsNum());
    PrefixTuple prefix = option_cast_v6->readPrefix();
    EXPECT_EQ(64, prefix.first.asUnsigned());
    EXPECT_EQ("2001:db8:1::", prefix.second.toText());
}

// This test verifies that a definition of an option with an array
// of IPv6 prefixes can be created and that the instance of this
// option can be created by specifying multiple prefixes in the
// textual format.
TEST_F(OptionDefinitionTest, prefixArrayTokenized) {
    OptionDefinition opt_def("option-prefix", 1000, "my-space",
                             "ipv6-prefix", true);

    OptionPtr option_v6;

    // Specify 3 prefixes
    std::vector<std::string> values;
    values.push_back("2001:db8:1:: /64");
    values.push_back("3000::/ 32");
    values.push_back("3001:1:: /  48");

    // Create an instance of an option using the definition.
    ASSERT_NO_THROW(
        option_v6 = opt_def.optionFactory(Option::V6, 1000, values);
    );

    // Make sure that the option class returned is correct.
    const Option* optptr = option_v6.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(OptionCustom));

    OptionCustomPtr option_cast_v6 =
        boost::dynamic_pointer_cast<OptionCustom>(option_v6);

    // There should be 3 prefixes in this option.
    ASSERT_EQ(3, option_cast_v6->getDataFieldsNum());

    ASSERT_NO_THROW({
        PrefixTuple prefix0 = option_cast_v6->readPrefix(0);
        EXPECT_EQ(64, prefix0.first.asUnsigned());
        EXPECT_EQ("2001:db8:1::", prefix0.second.toText());
    });

    ASSERT_NO_THROW({
        PrefixTuple prefix1 = option_cast_v6->readPrefix(1);
        EXPECT_EQ(32, prefix1.first.asUnsigned());
        EXPECT_EQ("3000::", prefix1.second.toText());
    });

    ASSERT_NO_THROW({
        PrefixTuple prefix2 = option_cast_v6->readPrefix(2);
        EXPECT_EQ(48, prefix2.first.asUnsigned());
        EXPECT_EQ("3001:1::", prefix2.second.toText());
    });
}

// This test verifies that a definition of an option with a single PSID
// value can be created and used to create an instance of the option.
TEST_F(OptionDefinitionTest, psid) {
    OptionDefinition opt_def("option-psid", 1000, "my-space", "psid");

    OptionPtr option_v6;

    // Create a buffer holding PSID.
    OptionBuffer buf;
    buf.push_back(6);
    buf.push_back(0x4);
    buf.push_back(0x0);

    // Create an instance of this option from the definition.
    ASSERT_NO_THROW(
        option_v6 = opt_def.optionFactory(Option::V6, 1000, buf);
    );

    // Make sure that the returned option class is correct.
    const Option* optptr = option_v6.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(OptionCustom));

    // Validate the value.
    OptionCustomPtr option_cast_v6 =
        boost::dynamic_pointer_cast<OptionCustom>(option_v6);
    ASSERT_EQ(1, option_cast_v6->getDataFieldsNum());
    PSIDTuple psid = option_cast_v6->readPsid();
    EXPECT_EQ(6, psid.first.asUnsigned());
    EXPECT_EQ(1, psid.second.asUint16());
}

// This test verifies that a definition of an option with a single PSID
// value can be created and that the instance of this option can be
// created by specifying PSID length and value in the textual format.
TEST_F(OptionDefinitionTest, psidTokenized) {
    OptionDefinition opt_def("option-psid", 1000, "my-space", "psid");

    OptionPtr option_v6;
    // Specify a single PSID with a length of 6 and value of 3.
    std::vector<std::string> values(1, "3 / 6");

    // Create an instance of the option using the definition.
    ASSERT_NO_THROW(
        option_v6 = opt_def.optionFactory(Option::V6, 1000, values);
    );

    // Make sure that the returned option class is correct.
    const Option* optptr = option_v6.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(OptionCustom));

    // Validate the value.
    OptionCustomPtr option_cast_v6 =
        boost::dynamic_pointer_cast<OptionCustom>(option_v6);
    ASSERT_EQ(1, option_cast_v6->getDataFieldsNum());
    PSIDTuple psid = option_cast_v6->readPsid();
    EXPECT_EQ(6, psid.first.asUnsigned());
    EXPECT_EQ(3, psid.second.asUint16());
}

// This test verifies that a definition of an option with an array
// of PSIDs can be created and that the instance of this option can be
// created by specifying multiple PSIDs in the textual format.
TEST_F(OptionDefinitionTest, psidArrayTokenized) {
    OptionDefinition opt_def("option-psid", 1000, "my-space", "psid", true);

    OptionPtr option_v6;

    // Specify 3 PSIDs.
    std::vector<std::string> values;
    values.push_back("3 / 6");
    values.push_back("0/1");
    values.push_back("7     /   3");

    // Create an instance of an option using the definition.
    ASSERT_NO_THROW(
        option_v6 = opt_def.optionFactory(Option::V6, 1000, values);
    );

    // Make sure that the option class returned is correct.
    const Option* optptr = option_v6.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(OptionCustom));

    OptionCustomPtr option_cast_v6 =
        boost::dynamic_pointer_cast<OptionCustom>(option_v6);

    // There should be 3 PSIDs in this option.
    ASSERT_EQ(3, option_cast_v6->getDataFieldsNum());

    // Check their values.
    PSIDTuple psid0;
    PSIDTuple psid1;
    PSIDTuple psid2;

    psid0 = option_cast_v6->readPsid(0);
    EXPECT_EQ(6, psid0.first.asUnsigned());
    EXPECT_EQ(3, psid0.second.asUint16());

    psid1 = option_cast_v6->readPsid(1);
    EXPECT_EQ(1, psid1.first.asUnsigned());
    EXPECT_EQ(0, psid1.second.asUint16());

    psid2 = option_cast_v6->readPsid(2);
    EXPECT_EQ(3, psid2.first.asUnsigned());
    EXPECT_EQ(7, psid2.second.asUint16());
}

// This test verifies that a definition of an option with a single DHCPv4
// tuple can be created and used to create an instance of the option.
TEST_F(OptionDefinitionTest, tuple4) {
    OptionDefinition opt_def("option-tuple", 232, "my-space", "tuple");

    OptionPtr option;

    // Create a buffer holding tuple
    const char data[] = {
        6, 102, 111, 111, 98, 97, 114 // "foobar"
    };
    OptionBuffer buf(data, data + sizeof(data));

    // Create an instance of this option from the definition.
    ASSERT_NO_THROW(
        option = opt_def.optionFactory(Option::V4, 232, buf);
    );

    // Make sure that the returned option class is correct.
    const Option* optptr = option.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(OptionCustom));

    // Validate the value.
    OptionCustomPtr option_cast =
        boost::dynamic_pointer_cast<OptionCustom>(option);
    ASSERT_EQ(1, option_cast->getDataFieldsNum());
    OpaqueDataTuple tuple(OpaqueDataTuple::LENGTH_1_BYTE);
    ASSERT_NO_THROW(option_cast->readTuple(tuple));
    EXPECT_EQ("foobar", tuple.getText());
}

// This test verifies that a definition of an option with a single DHCPv6
// tuple can be created and used to create an instance of the option.
TEST_F(OptionDefinitionTest, tuple6) {
    OptionDefinition opt_def("option-tuple", 1000, "my-space", "tuple");

    OptionPtr option;

    // Create a buffer holding tuple
    const char data[] = {
        0, 6, 102, 111, 111, 98, 97, 114 // "foobar"
    };
    OptionBuffer buf(data, data + sizeof(data));

    // Create an instance of this option from the definition.
    ASSERT_NO_THROW(
        option = opt_def.optionFactory(Option::V6, 1000, buf);
    );

    // Make sure that the returned option class is correct.
    const Option* optptr = option.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(OptionCustom));

    // Validate the value.
    OptionCustomPtr option_cast =
        boost::dynamic_pointer_cast<OptionCustom>(option);
    ASSERT_EQ(1, option_cast->getDataFieldsNum());
    OpaqueDataTuple tuple(OpaqueDataTuple::LENGTH_2_BYTES);
    ASSERT_NO_THROW(option_cast->readTuple(tuple));
    EXPECT_EQ("foobar", tuple.getText());
}

// This test verifies that a definition of an option with a single DHCPv4
// tuple can be created and that the instance of this option can be
// created by specifying tuple value in the textual format.
TEST_F(OptionDefinitionTest, tuple4Tokenized) {
    OptionDefinition opt_def("option-tuple", 232, "my-space", "tuple");

    OptionPtr option;
    // Specify a single tuple with "foobar" content.
    std::vector<std::string> values(1, "foobar");

    // Create an instance of this option using the definition.
    ASSERT_NO_THROW(
        option = opt_def.optionFactory(Option::V4, 232, values);
    );

    // Make sure that the returned option class is correct.
    const Option* optptr = option.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(OptionCustom));

    // Validate the value.
    OptionCustomPtr option_cast =
        boost::dynamic_pointer_cast<OptionCustom>(option);
    ASSERT_EQ(1, option_cast->getDataFieldsNum());
    OpaqueDataTuple tuple(OpaqueDataTuple::LENGTH_1_BYTE);
    ASSERT_NO_THROW(option_cast->readTuple(tuple));
    EXPECT_EQ("foobar", tuple.getText());
}

// This test verifies that a definition of an option with a single DHCPv6
// tuple can be created and that the instance of this option can be
// created by specifying tuple value in the textual format.
TEST_F(OptionDefinitionTest, tuple6Tokenized) {
    OptionDefinition opt_def("option-tuple", 1000, "my-space", "tuple");

    OptionPtr option;
    // Specify a single tuple with "foobar" content.
    std::vector<std::string> values(1, "foobar");

    // Create an instance of this option using the definition.
    ASSERT_NO_THROW(
        option = opt_def.optionFactory(Option::V6, 1000, values);
    );

    // Make sure that the returned option class is correct.
    const Option* optptr = option.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(OptionCustom));

    // Validate the value.
    OptionCustomPtr option_cast =
        boost::dynamic_pointer_cast<OptionCustom>(option);
    ASSERT_EQ(1, option_cast->getDataFieldsNum());
    OpaqueDataTuple tuple(OpaqueDataTuple::LENGTH_2_BYTES);
    ASSERT_NO_THROW(option_cast->readTuple(tuple));
    EXPECT_EQ("foobar", tuple.getText());
}

// This test verifies that a definition of an option with an array
// of DHCPv4 tuples can be created and that the instance of this option
// can be created by specifying multiple DHCPv4 tuples in the textual format.
TEST_F(OptionDefinitionTest, tuple4ArrayTokenized) {
    OptionDefinition opt_def("option-tuple", 232, "my-space", "tuple", true);

    OptionPtr option;

    // Specify 3 tuples.
    std::vector<std::string> values;
    values.push_back("hello");
    values.push_back("the");
    values.push_back("world");

    // Create an instance of this option using the definition.
    ASSERT_NO_THROW(
        option = opt_def.optionFactory(Option::V4, 232, values);
    );

    // Make sure that the returned option class is correct.
    const Option* optptr = option.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(OptionOpaqueDataTuples));

    // Validate the value.
    OptionOpaqueDataTuplesPtr option_cast =
        boost::dynamic_pointer_cast<OptionOpaqueDataTuples>(option);

    // There should be 3 tuples in this option.
    ASSERT_EQ(3, option_cast->getTuplesNum());

    // Check their values.
    OpaqueDataTuple tuple0 = option_cast->getTuple(0);
    EXPECT_EQ("hello", tuple0.getText());

    OpaqueDataTuple tuple1 = option_cast->getTuple(1);
    EXPECT_EQ("the", tuple1.getText());

    OpaqueDataTuple tuple2 = option_cast->getTuple(2);
    EXPECT_EQ("world", tuple2.getText());
}

// This test verifies that a definition of an option with an array
// of DHCPv4 tuples can be created and that the instance of this option
// can be created by specifying multiple DHCPv4 tuples in the textual format.
// This test also verifies specific v4 Option #143 where tuple's string length
// is coded on 2 octets instead of 1 as usual.
TEST_F(OptionDefinitionTest, tuple4ArrayOption143) {
    OptionDefinition opt_def("option-tuple", DHO_V4_SZTP_REDIRECT, DHCP4_OPTION_SPACE, "tuple", true);

    OptionPtr option;

    // Specify 3 tuples.
    std::vector<std::string> values;
    values.push_back("hello");
    values.push_back("the");
    values.push_back("world");

    // Create an instance of this option using the definition.
    ASSERT_NO_THROW(
        option = opt_def.optionFactory(Option::V4, DHO_V4_SZTP_REDIRECT, values);
    );

    // Make sure that the returned option class is correct.
    const Option* optptr = option.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(OptionOpaqueDataTuples));

    // Validate the value.
    OptionOpaqueDataTuplesPtr option_cast =
        boost::dynamic_pointer_cast<OptionOpaqueDataTuples>(option);

    // There should be 3 tuples in this option.
    ASSERT_EQ(3, option_cast->getTuplesNum());

    // Check their values.
    OpaqueDataTuple tuple0 = option_cast->getTuple(0);
    EXPECT_EQ("hello", tuple0.getText());

    OpaqueDataTuple tuple1 = option_cast->getTuple(1);
    EXPECT_EQ("the", tuple1.getText());

    OpaqueDataTuple tuple2 = option_cast->getTuple(2);
    EXPECT_EQ("world", tuple2.getText());
}

// The purpose of this test is to verify that definition can be created
// for option of internal type with convenient config notation. In this particular test
// the V4-DNR option is used (code 162) in ADN only mode, only one DNR instance.
// Option's fields are specified as a vector of strings.
TEST_F(OptionDefinitionTest, internalOption4DnrAdnOnly) {
    OptionDefinition opt_def("option-dnr", DHO_V4_DNR, DHCP4_OPTION_SPACE, "internal", false);

    OptionPtr option;

    // Specify option's fields for ADN only mode.
    std::vector<std::string> values;
    values.push_back("1234");                    // service priority
    values.push_back("Example.Some.Host.Org.");  // ADN FQDN

    // Create an instance of this option using the definition.
    ASSERT_NO_THROW(option = opt_def.optionFactory(Option::V4, DHO_V4_DNR, values););

    // Make sure that the returned option class is correct.
    const Option* optptr = option.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(Option4Dnr));

    // Validate that option's fields were correctly parsed from strings.
    Option4DnrPtr option_cast = boost::dynamic_pointer_cast<Option4Dnr>(option);

    auto dnr_instances = option_cast->getDnrInstances();

    // Only one DNR instance is expected.
    ASSERT_EQ(1, dnr_instances.size());

    DnrInstance& dnr = dnr_instances[0];
    ASSERT_EQ(26, dnr.getDnrInstanceDataLength());
    ASSERT_EQ(1234, dnr.getServicePriority());
    ASSERT_EQ(true, dnr.isAdnOnlyMode());
    ASSERT_EQ("example.some.host.org.", dnr.getAdnAsText());
    ASSERT_EQ(23, dnr.getAdnLength());
    ASSERT_EQ(0, dnr.getAddrLength());
    ASSERT_EQ(0, dnr.getSvcParamsLength());
}

// The purpose of this test is to verify that definition can be created
// for option of internal type with convenient config notation. In this particular test
// the V4-DNR option is used (code 162) with ADN, IP addresses and Service
// Parameters included. Option's fields are specified as a vector of strings.
// Multiple DNR instances are configured in this test.
TEST_F(OptionDefinitionTest, internalOption4Dnr) {
    OptionDefinition opt_def("option-dnr", DHO_V4_DNR, DHCP4_OPTION_SPACE, "internal", false);

    OptionPtr option;

    // Specify option's fields - multiple DNR instances.
    std::vector<std::string> values;
    values.push_back("1234");                              // service priority
    values.push_back("Example.Some.Host.Org.");            // ADN FQDN
    values.push_back("192.168.0.1 192.168.0.2");           // Addresses
    values.push_back("alpn=dot\\,doq port=2345 | 4321");   // SvcParams, DnrInstance separator
                                                           // and DnrInstance#2 Service Priority
    values.push_back("myhost1.example.com.");              // ADN FQDN
    values.push_back("192.169.0.1 192.169.0.2");           // Addresses
    values.push_back("alpn=h3 dohpath=/dns-query{?dns}");  // SvcParams

    // Create an instance of this option using the definition.
    ASSERT_NO_THROW(option = opt_def.optionFactory(Option::V4, DHO_V4_DNR, values););

    // Make sure that the returned option class is correct.
    const Option* optptr = option.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(Option4Dnr));

    // Validate that option's fields were correctly parsed from strings.
    Option4DnrPtr option_cast = boost::dynamic_pointer_cast<Option4Dnr>(option);

    auto dnr_instances = option_cast->getDnrInstances();

    // Two DNR instances are expected.
    ASSERT_EQ(2, dnr_instances.size());

    // Let's check 1st DNR instance.
    DnrInstance& dnr_1 = dnr_instances[0];
    ASSERT_EQ(53, dnr_1.getDnrInstanceDataLength());
    ASSERT_EQ(1234, dnr_1.getServicePriority());
    ASSERT_EQ(false, dnr_1.isAdnOnlyMode());
    ASSERT_EQ(23, dnr_1.getAdnLength());
    ASSERT_EQ("example.some.host.org.", dnr_1.getAdnAsText());
    ASSERT_EQ(8, dnr_1.getAddrLength());
    ASSERT_EQ(18, dnr_1.getSvcParamsLength());
    auto addresses_1 = dnr_1.getAddresses();
    ASSERT_EQ(2, addresses_1.size());
    ASSERT_EQ("192.168.0.1", addresses_1[0].toText());
    ASSERT_EQ("192.168.0.2", addresses_1[1].toText());

    // Let's check 2nd DNR instance.
    DnrInstance& dnr_2 = dnr_instances[1];
    ASSERT_EQ(60, dnr_2.getDnrInstanceDataLength());
    ASSERT_EQ(4321, dnr_2.getServicePriority());
    ASSERT_EQ(false, dnr_2.isAdnOnlyMode());
    ASSERT_EQ(21, dnr_2.getAdnLength());
    ASSERT_EQ("myhost1.example.com.", dnr_2.getAdnAsText());
    ASSERT_EQ(8, dnr_2.getAddrLength());
    ASSERT_EQ(27, dnr_2.getSvcParamsLength());
    auto addresses_2 = dnr_2.getAddresses();
    ASSERT_EQ(2, addresses_2.size());
    ASSERT_EQ("192.169.0.1", addresses_2[0].toText());
    ASSERT_EQ("192.169.0.2", addresses_2[1].toText());
}

// The purpose of this test is to verify that definition can be created
// for option of internal type with convenient config notation. In this particular test
// the V6-DNR option is used (code 144) in ADN only mode.
// Option's fields are specified as a vector of strings.
TEST_F(OptionDefinitionTest, internalOption6DnrAdnOnly) {
    OptionDefinition opt_def("option-dnr", D6O_V6_DNR, DHCP6_OPTION_SPACE, "internal", false);

    OptionPtr option;

    // Specify option's fields for ADN only mode.
    std::vector<std::string> values;
    values.push_back("1234");                    // service priority
    values.push_back("Example.Some.Host.Org.");  // ADN FQDN

    // Create an instance of this option using the definition.
    ASSERT_NO_THROW(option = opt_def.optionFactory(Option::V6, D6O_V6_DNR, values););

    // Make sure that the returned option class is correct.
    const Option* optptr = option.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(Option6Dnr));

    // Validate that option's fields were correctly parsed from strings.
    Option6DnrPtr option_cast = boost::dynamic_pointer_cast<Option6Dnr>(option);

    ASSERT_EQ(1234, option_cast->getServicePriority());
    ASSERT_EQ(true, option_cast->isAdnOnlyMode());
    ASSERT_EQ("example.some.host.org.", option_cast->getAdnAsText());
    ASSERT_EQ(23, option_cast->getAdnLength());
    ASSERT_EQ(0, option_cast->getAddrLength());
    ASSERT_EQ(0, option_cast->getSvcParamsLength());
}

// The purpose of this test is to verify that definition can be created
// for option of internal type with convenient config notation. In this particular test
// the V6-DNR option is used (code 144) with ADN, IP addresses and Service
// Parameters included. Option's fields are specified as a vector of strings.
TEST_F(OptionDefinitionTest, internalOption6Dnr) {
    OptionDefinition opt_def("option-dnr", D6O_V6_DNR, DHCP6_OPTION_SPACE, "internal", false);

    OptionPtr option;

    // Specify option's fields: service priority, ADN, IP addresses and SvcParams.
    std::vector<std::string> values;
    values.push_back("1234");                                   // service priority
    values.push_back("Example.Some.Host.Org.");                 // ADN FQDN
    values.push_back("2001:db8:1::dead:beef ff02::face:b00c");  // Addresses
    values.push_back("alpn=h3 dohpath=/dns-query{?dns}");       // SvcParams

    // Create an instance of this option using the definition.
    ASSERT_NO_THROW(option = opt_def.optionFactory(Option::V6, D6O_V6_DNR, values););

    // Make sure that the returned option class is correct.
    const Option* optptr = option.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(Option6Dnr));

    // Validate that option's fields were correctly parsed from strings.
    Option6DnrPtr option_cast = boost::dynamic_pointer_cast<Option6Dnr>(option);

    ASSERT_EQ(1234, option_cast->getServicePriority());
    ASSERT_EQ(false, option_cast->isAdnOnlyMode());
    ASSERT_EQ("example.some.host.org.", option_cast->getAdnAsText());
    ASSERT_EQ(23, option_cast->getAdnLength());
    ASSERT_EQ(32, option_cast->getAddrLength());
    ASSERT_EQ(27, option_cast->getSvcParamsLength());
    auto addresses = option_cast->getAddresses();
    ASSERT_EQ(2, addresses.size());
    ASSERT_EQ("2001:db8:1::dead:beef", addresses[0].toText());
    ASSERT_EQ("ff02::face:b00c", addresses[1].toText());
}

// This test verifies that a definition of an option with an array
// of DHCPv6 tuples can be created and that the instance of this option
// can be created by specifying multiple DHCPv6 tuples in the textual format.
TEST_F(OptionDefinitionTest, tuple6ArrayTokenized) {
    OptionDefinition opt_def("option-tuple", 1000, "my-space", "tuple", true);

    OptionPtr option;

    // Specify 3 tuples.
    std::vector<std::string> values;
    values.push_back("hello");
    values.push_back("the");
    values.push_back("world");

    // Create an instance of this option using the definition.
    ASSERT_NO_THROW(
        option = opt_def.optionFactory(Option::V6, 1000, values);
    );

    // Make sure that the returned option class is correct.
    const Option* optptr = option.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(OptionOpaqueDataTuples));

    // Validate the value.
    OptionOpaqueDataTuplesPtr option_cast =
        boost::dynamic_pointer_cast<OptionOpaqueDataTuples>(option);

    // There should be 3 tuples in this option.
    ASSERT_EQ(3, option_cast->getTuplesNum());

    // Check their values.
    OpaqueDataTuple tuple0 = option_cast->getTuple(0);
    EXPECT_EQ("hello", tuple0.getText());

    OpaqueDataTuple tuple1 = option_cast->getTuple(1);
    EXPECT_EQ("the", tuple1.getText());

    OpaqueDataTuple tuple2 = option_cast->getTuple(2);
    EXPECT_EQ("world", tuple2.getText());
}

// The purpose of this test is to verify creation of OPT_INTERNAL_TYPE option
// definition. OptionFactory is used which takes vector of string values as
// data for the option. This is special case for OPT_INTERNAL_TYPE because it means
// that convenient notation option config is provided as string that needs special parsing.
// Custom parsing is verified in this test.
TEST_F(OptionDefinitionTest, internalOptionTypeString) {
    OptionDefinition opt_def("classless-static-route", DHO_CLASSLESS_STATIC_ROUTE,
                             DHCP4_OPTION_SPACE, "internal", false);

    OptionPtr option;

    // Specify option's static routes.
    std::vector<std::string> values;
    values.push_back("0.0.0.0/0-10.17.0.1");
    values.push_back("10.229.0.128/25-10.229.0.1");
    values.push_back("10.27.129.0/24-10.27.129.1");

    // Create an instance of this option using the definition.
    ASSERT_NO_THROW(option = opt_def.optionFactory(Option::V4, DHO_CLASSLESS_STATIC_ROUTE, values););

    // Make sure that the returned option class is correct.
    const Option* optptr = option.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(OptionClasslessStaticRoute));

    // Validate that option's fields were correctly parsed from strings.
    OptionClasslessStaticRoutePtr option_cast = boost::dynamic_pointer_cast<OptionClasslessStaticRoute>(option);

    // Check that cast was successful.
    ASSERT_TRUE(option_cast);

    // Expected len: 2 (option code + option len headers) + 5 (1 dest descriptor + 4 router addr)
    // + 9 (5 dest descriptor + 4 router addr) + 8 (4 dest descriptor + 4 router addr).
    EXPECT_EQ(24, option_cast->len());

    // Verify toText() is working fine.
    EXPECT_EQ("type=121(CLASSLESS_STATIC_ROUTE), len=22, "
              "Route 1 (subnet 0.0.0.0/0, router IP 10.17.0.1), "
              "Route 2 (subnet 10.229.0.128/25, router IP 10.229.0.1), "
              "Route 3 (subnet 10.27.129.0/24, router IP 10.27.129.1)",
              option_cast->toText());
}

// The purpose of this test is to verify creation of OPT_INTERNAL_TYPE option
// definition. OptionFactory is used which takes OptionBuffer as
// data for the option. Binary data unpack is verified in this test.
TEST_F(OptionDefinitionTest, internalOptionTypeBinary) {
    OptionDefinition opt_def("classless-static-route", DHO_CLASSLESS_STATIC_ROUTE,
                             DHCP4_OPTION_SPACE, "internal", false);

    OptionPtr option;

    // Create a buffer holding static routes on-wire data
    const OptionBuffer buf = {
        0,                  // mask width
        10, 17,  0,   1,    // router IP address
        25,                 // mask width
        10, 229, 0,   128,  // significant subnet octets for 10.229.0.128/25
        10, 229, 0,   1,    // router IP address
        24,                 // mask width
        10, 27,  129,       // significant subnet octets for 10.27.129.0/24
        10, 27,  129, 1     // router IP address
    };

    // Create an instance of this option using the definition.
    ASSERT_NO_THROW(option = opt_def.optionFactory(Option::V4, DHO_CLASSLESS_STATIC_ROUTE, buf););

    // Make sure that the returned option class is correct.
    const Option* optptr = option.get();
    ASSERT_TRUE(optptr);
    ASSERT_TRUE(typeid(*optptr) == typeid(OptionClasslessStaticRoute));

    // Sanity check on universe and header length.
    EXPECT_EQ(Option::V4, option->getUniverse());
    EXPECT_EQ(2, option->getHeaderLen());

    // Validate parsed values.
    OptionClasslessStaticRoutePtr option_cast = boost::dynamic_pointer_cast<OptionClasslessStaticRoute>(option);

    // Check that cast was successful.
    ASSERT_TRUE(option_cast);

    // Expected len: 2 (option code + option len headers) + 5 (1 dest descriptor + 4 router addr)
    // + 9 (5 dest descriptor + 4 router addr) + 8 (4 dest descriptor + 4 router addr).
    EXPECT_EQ(24, option_cast->len());

    // Verify toText() is working fine.
    EXPECT_EQ("type=121(CLASSLESS_STATIC_ROUTE), len=22, "
              "Route 1 (subnet 0.0.0.0/0, router IP 10.17.0.1), "
              "Route 2 (subnet 10.229.0.128/25, router IP 10.229.0.1), "
              "Route 3 (subnet 10.27.129.0/24, router IP 10.27.129.1)",
              option_cast->toText());
}

// Verify converstion of OptionDefintion type strings to type enums.
TEST(OptionDataTypeUtil, typeToString) {
    EXPECT_EQ(OPT_EMPTY_TYPE, OptionDataTypeUtil::getDataType("empty"));
    EXPECT_EQ(OPT_BINARY_TYPE, OptionDataTypeUtil::getDataType("binary"));
    EXPECT_EQ(OPT_BOOLEAN_TYPE, OptionDataTypeUtil::getDataType("boolean"));
    EXPECT_EQ(OPT_INT8_TYPE, OptionDataTypeUtil::getDataType("int8"));
    EXPECT_EQ(OPT_INT16_TYPE, OptionDataTypeUtil::getDataType("int16"));
    EXPECT_EQ(OPT_INT32_TYPE, OptionDataTypeUtil::getDataType("int32"));
    EXPECT_EQ(OPT_UINT8_TYPE, OptionDataTypeUtil::getDataType("uint8"));
    EXPECT_EQ(OPT_UINT16_TYPE, OptionDataTypeUtil::getDataType("uint16"));
    EXPECT_EQ(OPT_UINT32_TYPE, OptionDataTypeUtil::getDataType("uint32"));
    EXPECT_EQ(OPT_IPV4_ADDRESS_TYPE, OptionDataTypeUtil::getDataType("ipv4-address"));
    EXPECT_EQ(OPT_IPV6_ADDRESS_TYPE, OptionDataTypeUtil::getDataType("ipv6-address"));
    EXPECT_EQ(OPT_IPV6_PREFIX_TYPE, OptionDataTypeUtil::getDataType("ipv6-prefix"));
    EXPECT_EQ(OPT_PSID_TYPE, OptionDataTypeUtil::getDataType("psid"));
    EXPECT_EQ(OPT_STRING_TYPE, OptionDataTypeUtil::getDataType("string"));
    EXPECT_EQ(OPT_TUPLE_TYPE, OptionDataTypeUtil::getDataType("tuple"));
    EXPECT_EQ(OPT_FQDN_TYPE, OptionDataTypeUtil::getDataType("fqdn"));
    EXPECT_EQ(OPT_INTERNAL_TYPE, OptionDataTypeUtil::getDataType("internal"));
    EXPECT_EQ(OPT_RECORD_TYPE, OptionDataTypeUtil::getDataType("record"));
    EXPECT_EQ(OPT_UNKNOWN_TYPE, OptionDataTypeUtil::getDataType("bogus"));
}

// Verify converstion of OptionDefintion type enums to type strings.
TEST(OptionDataTypeUtil, stringToType) {
    EXPECT_EQ("empty", OptionDataTypeUtil::getDataTypeName(OPT_EMPTY_TYPE));
    EXPECT_EQ("binary", OptionDataTypeUtil::getDataTypeName( OPT_BINARY_TYPE));
    EXPECT_EQ("boolean", OptionDataTypeUtil::getDataTypeName(OPT_BOOLEAN_TYPE));
    EXPECT_EQ("int8", OptionDataTypeUtil::getDataTypeName(OPT_INT8_TYPE));
    EXPECT_EQ("int16", OptionDataTypeUtil::getDataTypeName(OPT_INT16_TYPE));
    EXPECT_EQ("int32", OptionDataTypeUtil::getDataTypeName(OPT_INT32_TYPE));
    EXPECT_EQ("uint8", OptionDataTypeUtil::getDataTypeName(OPT_UINT8_TYPE));
    EXPECT_EQ("uint16", OptionDataTypeUtil::getDataTypeName(OPT_UINT16_TYPE));
    EXPECT_EQ("uint32", OptionDataTypeUtil::getDataTypeName(OPT_UINT32_TYPE));
    EXPECT_EQ("ipv4-address", OptionDataTypeUtil::getDataTypeName(OPT_IPV4_ADDRESS_TYPE));
    EXPECT_EQ("ipv6-address", OptionDataTypeUtil::getDataTypeName(OPT_IPV6_ADDRESS_TYPE));
    EXPECT_EQ("ipv6-prefix", OptionDataTypeUtil::getDataTypeName(OPT_IPV6_PREFIX_TYPE));
    EXPECT_EQ("psid", OptionDataTypeUtil::getDataTypeName(OPT_PSID_TYPE));
    EXPECT_EQ("string", OptionDataTypeUtil::getDataTypeName(OPT_STRING_TYPE));
    EXPECT_EQ("tuple", OptionDataTypeUtil::getDataTypeName(OPT_TUPLE_TYPE));
    EXPECT_EQ("fqdn", OptionDataTypeUtil::getDataTypeName(OPT_FQDN_TYPE));
    EXPECT_EQ("internal", OptionDataTypeUtil::getDataTypeName(OPT_INTERNAL_TYPE));
    EXPECT_EQ("record", OptionDataTypeUtil::getDataTypeName(OPT_RECORD_TYPE));
    EXPECT_EQ("unknown", OptionDataTypeUtil::getDataTypeName(OPT_UNKNOWN_TYPE));
}

} // anonymous namespace