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
// Copyright (C) 2023-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 <dhcp/option4_dnr.h>

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

using namespace isc;
using namespace isc::dhcp;
using namespace isc::asiolink;

namespace {

// This test verifies constructor from convenient string notation
// with one ADN-only-mode DNR instance.
TEST(Option4DnrTest, fromConfigCtorOneAdnOnlyModeInstance) {
    // Prepare example config.
    const std::string config = "1, myhost1.example.com.";

    OptionBuffer in_buf;
    in_buf.assign(config.begin(), config.end());

    // Create option instance. Check that constructor doesn't throw.
    Option4DnrPtr option;
    EXPECT_NO_THROW(option.reset(new Option4Dnr(in_buf.begin(), in_buf.end(), true)));
    ASSERT_TRUE(option);

    // Check if member variables were correctly set inside DNR instances.
    EXPECT_EQ(1, option->getDnrInstances().size());
    EXPECT_EQ(1, option->getDnrInstances()[0].getServicePriority());
    EXPECT_EQ(21, option->getDnrInstances()[0].getAdnLength());
    EXPECT_EQ("myhost1.example.com.", option->getDnrInstances()[0].getAdnAsText());

    // This is ADN only mode, so Addr Length and SvcParams Length
    // are both expected to be zero.
    EXPECT_EQ(0, option->getDnrInstances()[0].getAddrLength());
    EXPECT_EQ(0, option->getDnrInstances()[0].getSvcParamsLength());

    // BTW let's check if len() works ok. In ADN only mode, DNR Instance Data Len
    // is set to ADN Len (21) + 3 = 24.
    // expected len: 1x(24 (ADN+ADN Len+Service priority) + 2 (DNR Instance Data Len)) + 2 (headers)
    // = 28
    EXPECT_EQ(28, option->len());

    // BTW let's check if toText() works ok.
    // toText() len does not count in headers len.
    EXPECT_EQ("type=162(V4_DNR), len=26, "
              "DNR Instance 1(Instance len=24, service_priority=1, "
              "adn_length=21, adn='myhost1.example.com.')",
              option->toText());
}

// This test verifies constructor from convenient string notation
// with multiple ADN-only-mode DNR instances.
TEST(Option4DnrTest, fromConfigCtorMultipleAdnOnlyModeInstances) {<--- syntax error
    // Prepare example config.
    const std::string config = "1, myhost1.example.com. | "
                               "2, myhost2.example.com. | "
                               "3, myhost3.example.com.";

    OptionBuffer in_buf;
    in_buf.assign(config.begin(), config.end());

    // Create option instance. Check that constructor doesn't throw.
    Option4DnrPtr option;
    EXPECT_NO_THROW(option.reset(new Option4Dnr(in_buf.begin(), in_buf.end(), true)));
    ASSERT_TRUE(option);

    // Check if member variables were correctly set by ctor.
    EXPECT_EQ(Option::V4, option->getUniverse());
    EXPECT_EQ(DHO_V4_DNR, option->getType());

    // Check if member variables were correctly set inside DNR instances.
    EXPECT_EQ(3, option->getDnrInstances().size());
    EXPECT_EQ(1, option->getDnrInstances()[0].getServicePriority());
    EXPECT_EQ(2, option->getDnrInstances()[1].getServicePriority());
    EXPECT_EQ(3, option->getDnrInstances()[2].getServicePriority());
    EXPECT_EQ(21, option->getDnrInstances()[0].getAdnLength());
    EXPECT_EQ(21, option->getDnrInstances()[1].getAdnLength());
    EXPECT_EQ(21, option->getDnrInstances()[2].getAdnLength());
    EXPECT_EQ("myhost1.example.com.", option->getDnrInstances()[0].getAdnAsText());
    EXPECT_EQ("myhost2.example.com.", option->getDnrInstances()[1].getAdnAsText());
    EXPECT_EQ("myhost3.example.com.", option->getDnrInstances()[2].getAdnAsText());

    // This is ADN only mode, so Addr Length and SvcParams Length
    // are both expected to be zero.
    EXPECT_EQ(0, option->getDnrInstances()[0].getAddrLength());
    EXPECT_EQ(0, option->getDnrInstances()[0].getSvcParamsLength());
    EXPECT_EQ(0, option->getDnrInstances()[1].getAddrLength());
    EXPECT_EQ(0, option->getDnrInstances()[1].getSvcParamsLength());
    EXPECT_EQ(0, option->getDnrInstances()[2].getAddrLength());
    EXPECT_EQ(0, option->getDnrInstances()[2].getSvcParamsLength());

    // BTW let's check if len() works ok. In ADN only mode, DNR Instance Data Len
    // is set to ADN Len (21) + 3 = 24.
    // expected len: 3x(24 (ADN+ADN Len+Service priority) + 2 (DNR Instance Data Len)) + 2 (headers)
    // = 78 + 2 = 80
    EXPECT_EQ(80, option->len());

    // BTW let's check if toText() works ok.
    // toText() len does not count in headers len.
    EXPECT_EQ("type=162(V4_DNR), len=78, "
              "DNR Instance 1(Instance len=24, service_priority=1, "
              "adn_length=21, adn='myhost1.example.com.'), "
              "DNR Instance 2(Instance len=24, service_priority=2, "
              "adn_length=21, adn='myhost2.example.com.'), "
              "DNR Instance 3(Instance len=24, service_priority=3, "
              "adn_length=21, adn='myhost3.example.com.')",
              option->toText());
}

// This test verifies constructor from convenient string notation
// with two DNR instances:
// 1. ADN-only-mode DNR instance
// 2. All fields included (IP addresses and service params also) DNR instance.
TEST(Option4DnrTest, fromConfigCtorMixedDnrInstances) {
    // Prepare example config.
    const std::string config = "1, myhost1.example.com. | "
                               "2, myhost2.example.com., 192.168.0.1 192.168.0.2, "
                               "alpn=h3 port=1234 dohpath=/q{?dns}";

    OptionBuffer in_buf;
    in_buf.assign(config.begin(), config.end());

    // Create option instance. Check that constructor doesn't throw.
    Option4DnrPtr option;
    EXPECT_NO_THROW(option.reset(new Option4Dnr(in_buf.begin(), in_buf.end(), true)));
    ASSERT_TRUE(option);

    // Check if member variables were correctly set inside DNR instances.
    EXPECT_EQ(2, option->getDnrInstances().size());
    EXPECT_EQ(1, option->getDnrInstances()[0].getServicePriority());
    EXPECT_EQ(21, option->getDnrInstances()[0].getAdnLength());
    EXPECT_EQ("myhost1.example.com.", option->getDnrInstances()[0].getAdnAsText());
    EXPECT_EQ(2, option->getDnrInstances()[1].getServicePriority());
    EXPECT_EQ(21, option->getDnrInstances()[1].getAdnLength());
    EXPECT_EQ("myhost2.example.com.", option->getDnrInstances()[1].getAdnAsText());

    EXPECT_EQ(0, option->getDnrInstances()[0].getAddrLength());
    EXPECT_EQ(0, option->getDnrInstances()[0].getSvcParamsLength());
    EXPECT_EQ(2, option->getDnrInstances()[1].getAddresses().size());
    EXPECT_EQ(8, option->getDnrInstances()[1].getAddrLength());
    EXPECT_EQ("192.168.0.1", option->getDnrInstances()[1].getAddresses()[0].toText());
    EXPECT_EQ("192.168.0.2", option->getDnrInstances()[1].getAddresses()[1].toText());

    // Reference SvcParams on-wire data buffer.
    const OptionBuffer svc_params = {
        0,   1,                                  // SvcParamKey alpn
        0,   3,                                  // SvcParamVal Len
        2,   'h',  '3',                          // 2 octets long alpn-id http/3
        0,   3,                                  // SvcParamKey port
        0,   2,                                  // SvcParamVal Len
        0x4, 0xD2,                               // 1234 in hex
        0,   7,                                  // SvcParamKey dohpath
        0,   8,                                  // SvcParamVal Len
        '/', 'q',  '{', '?', 'd', 'n', 's', '}'  // dohpath value
    };

    EXPECT_EQ(svc_params, option->getDnrInstances()[1].getSvcParams());
    EXPECT_EQ(svc_params.size(), option->getDnrInstances()[1].getSvcParamsLength());

    // BTW let's check if len() works ok. In ADN only mode, DNR Instance Data Len
    // is set to ADN Len (21) + 3 = 24.
    // expected len: 1x(24 (ADN+ADN Len+Service priority) + 2 (DNR Instance Data Len)) + 2 (headers)
    // + 24 (ADN+ADN Len+Service priority) + 2 (DNR Instance Data Len) + 1 (Addr Len)
    // + 8 (IP addresses) + 25 (svc params)
    // = 88
    EXPECT_EQ(88, option->len());

    // BTW let's check if toText() works ok.
    // toText() len does not count in headers len.
    EXPECT_EQ("type=162(V4_DNR), len=86, "
              "DNR Instance 1(Instance len=24, service_priority=1, "
              "adn_length=21, adn='myhost1.example.com.'), "
              "DNR Instance 2(Instance len=58, service_priority=2, "
              "adn_length=21, adn='myhost2.example.com.', "
              "addr_length=8, address(es): 192.168.0.1 192.168.0.2, "
              "svc_params='alpn=h3 port=1234 dohpath=/q{?dns}')",
              option->toText());
}

// This test verifies option packing into wire data.
// Provided data to pack contains 1 DNR instance:
// 1. ADN only mode
TEST(Option4DnrTest, fromConfigCtorPackOneAdnOnlyModeInstance) {
    // Prepare example config.
    const std::string config = "1, myhost1.example.com.";

    OptionBuffer in_buf;
    in_buf.assign(config.begin(), config.end());

    // Create option instance. Check that constructor doesn't throw.
    Option4DnrPtr option;
    EXPECT_NO_THROW(option.reset(new Option4Dnr(in_buf.begin(), in_buf.end(), true)));
    ASSERT_TRUE(option);

    // Prepare on-wire format of the option.
    isc::util::OutputBuffer buf(10);
    ASSERT_NO_THROW(option->pack(buf));

    // Prepare reference data.
    const uint8_t ref_data[] = {
        DHO_V4_DNR,                                            // Option code
        26,                                                    // Option len=26 dec
        0x00,       24,                                        // DNR Instance Data Len
        0x00,       0x01,                                      // Service priority is 1 dec
        21,                                                    // ADN Length is 21 dec
        0x07,       0x6D, 0x79, 0x68, 0x6F, 0x73, 0x74, '1',   // FQDN: myhost1.
        0x07,       0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65,  // example.
        0x03,       0x63, 0x6F, 0x6D, 0x00                     // com.
    };

    size_t ref_data_size = sizeof(ref_data) / sizeof(ref_data[0]);

    // Check if the buffer has the same length as the reference data,
    // so as they can be compared directly.
    ASSERT_EQ(ref_data_size, buf.getLength());
    EXPECT_EQ(0, memcmp(ref_data, buf.getData(), buf.getLength()));
}

// This test verifies option packing into wire data.
// Provided data to pack contains 3 DNR instances:
// 1. ADN only mode
// 2. ADN only mode
// 3. ADN only mode
TEST(Option4DnrTest, fromConfigCtorPackMultipleAdnOnlyModeInstances) {
    // Prepare example config.
    const std::string config = "1, myhost1.example.com. | "
                               "2, myhost2.example.com. | "
                               "3, myhost3.example.com.";

    OptionBuffer in_buf;
    in_buf.assign(config.begin(), config.end());

    // Create option instance. Check that constructor doesn't throw.
    Option4DnrPtr option;
    EXPECT_NO_THROW(option.reset(new Option4Dnr(in_buf.begin(), in_buf.end(), true)));
    ASSERT_TRUE(option);

    // Check if member variables were correctly set by ctor.
    EXPECT_EQ(Option::V4, option->getUniverse());
    EXPECT_EQ(DHO_V4_DNR, option->getType());

    // Prepare on-wire format of the option.
    isc::util::OutputBuffer buf(10);
    ASSERT_NO_THROW(option->pack(buf));

    // Prepare reference data.
    const uint8_t ref_data[] = {
        DHO_V4_DNR,                                            // Option code
        78,                                                    // Option len=78 dec
        0x00,       24,                                        // DNR Instance Data Len
        0x00,       0x01,                                      // Service priority is 1 dec
        21,                                                    // ADN Length is 21 dec
        0x07,       0x6D, 0x79, 0x68, 0x6F, 0x73, 0x74, '1',   // FQDN: myhost1.
        0x07,       0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65,  // example.
        0x03,       0x63, 0x6F, 0x6D, 0x00,                    // com.
        0x00,       24,                                        // DNR Instance Data Len
        0x00,       0x02,                                      // Service priority is 2 dec
        21,                                                    // ADN Length is 21 dec
        0x07,       0x6D, 0x79, 0x68, 0x6F, 0x73, 0x74, '2',   // FQDN: myhost2.
        0x07,       0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65,  // example.
        0x03,       0x63, 0x6F, 0x6D, 0x00,                    // com.
        0x00,       24,                                        // DNR Instance Data Len
        0x00,       0x03,                                      // Service priority is 3 dec
        21,                                                    // ADN Length is 21 dec
        0x07,       0x6D, 0x79, 0x68, 0x6F, 0x73, 0x74, '3',   // FQDN: myhost3.
        0x07,       0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65,  // example.
        0x03,       0x63, 0x6F, 0x6D, 0x00                     // com.
    };

    size_t ref_data_size = sizeof(ref_data) / sizeof(ref_data[0]);

    // Check if the buffer has the same length as the reference data,
    // so as they can be compared directly.
    ASSERT_EQ(ref_data_size, buf.getLength());
    EXPECT_EQ(0, memcmp(ref_data, buf.getData(), buf.getLength()));
}

// This test verifies option packing into wire data.
// Provided data to pack contains 2 DNR instances:
// 1. ADN only mode
// 2. All fields included (IP addresses and service params also).
TEST(Option4DnrTest, fromConfigCtorPackMixedDnrInstances) {
    // Create option instance. Check that constructor doesn't throw.
    const std::string config = "1, myhost1.example.com. | "
                               "2, myhost2.example.com., 192.168.0.1 192.168.0.2, alpn=dot\\,doq";
    OptionBuffer opt_buf;
    opt_buf.assign(config.begin(), config.end());
    Option4DnrPtr option;
    EXPECT_NO_THROW(option.reset(new Option4Dnr(opt_buf.begin(), opt_buf.end(), true)));
    ASSERT_TRUE(option);

    // Prepare on-wire format of the option.
    isc::util::OutputBuffer out_buf(10);
    ASSERT_NO_THROW(option->pack(out_buf));

    // Prepare reference data.
    const uint8_t ref_data[] = {
        DHO_V4_DNR,                                      // Option code
        73,                                              // Option len
        0x00, 24,                                        // DNR Instance Data Len
        0x00, 0x01,                                      // Service priority is 1 dec
        21,                                              // ADN Length is 21 dec
        0x07, 0x6D, 0x79, 0x68, 0x6F, 0x73, 0x74, '1',   // FQDN: myhost1.
        0x07, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65,  // example.
        0x03, 0x63, 0x6F, 0x6D, 0x00,                    // com.
        0x00, 45,                                        // DNR Instance Data Len
        0x00, 0x02,                                      // Service priority is 2 dec
        21,                                              // ADN Length is 21 dec
        0x07, 0x6D, 0x79, 0x68, 0x6F, 0x73, 0x74, '2',   // FQDN: myhost2.
        0x07, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65,  // example.
        0x03, 0x63, 0x6F, 0x6D, 0x00,                    // com.
        8,                                               // Addr Len
        192,  168,  0,    1,                             // IP address 1
        192,  168,  0,    2,                             // IP address 2
        0,    1,                                         // SvcParamKey alpn
        0,    8,                                         // SvcParamVal Len
        3,    'd',  'o',  't',                           // 3 octets long alpn-id dot
        3,    'd',  'o',  'q'                            // 3 octets long alpn-id doq

    };

    size_t ref_data_size = sizeof(ref_data) / sizeof(ref_data[0]);

    // Check if the buffer has the same length as the reference data,
    // so as they can be compared directly.
    ASSERT_EQ(ref_data_size, out_buf.getLength());
    EXPECT_EQ(0, memcmp(ref_data, out_buf.getData(), out_buf.getLength()));
}

// This test verifies option packing into wire data.
// Provided data to pack contains 2 DNR instances:
// 1. ADN only mode
// 2. Almost all fields included (IP addresses but no service params).
TEST(Option4DnrTest, fromConfigCtorPackMixedDnrInstancesTwo) {
    // Create option instance. Check that constructor doesn't throw.
    const std::string config = "1, myhost1.example.com. | "
                               "2, myhost2.example.com., 192.168.0.1 192.168.0.2";
    OptionBuffer opt_buf;
    opt_buf.assign(config.begin(), config.end());
    Option4DnrPtr option;
    EXPECT_NO_THROW(option.reset(new Option4Dnr(opt_buf.begin(), opt_buf.end(), true)));
    ASSERT_TRUE(option);

    // Prepare on-wire format of the option.
    isc::util::OutputBuffer out_buf(10);
    ASSERT_NO_THROW(option->pack(out_buf));

    // Prepare reference data.
    const uint8_t ref_data[] = {
        DHO_V4_DNR,                                      // Option code
        61,                                              // Option len
        0x00, 24,                                        // DNR Instance Data Len
        0x00, 0x01,                                      // Service priority is 1 dec
        21,                                              // ADN Length is 21 dec
        0x07, 0x6D, 0x79, 0x68, 0x6F, 0x73, 0x74, '1',   // FQDN: myhost1.
        0x07, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65,  // example.
        0x03, 0x63, 0x6F, 0x6D, 0x00,                    // com.
        0x00, 33,                                        // DNR Instance Data Len
        0x00, 0x02,                                      // Service priority is 2 dec
        21,                                              // ADN Length is 21 dec
        0x07, 0x6D, 0x79, 0x68, 0x6F, 0x73, 0x74, '2',   // FQDN: myhost2.
        0x07, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65,  // example.
        0x03, 0x63, 0x6F, 0x6D, 0x00,                    // com.
        8,                                               // Addr Len
        192,  168,  0,    1,                             // IP address 1
        192,  168,  0,    2                              // IP address 2
    };

    size_t ref_data_size = sizeof(ref_data) / sizeof(ref_data[0]);

    // Check if the buffer has the same length as the reference data,
    // so as they can be compared directly.
    ASSERT_EQ(ref_data_size, out_buf.getLength());
    EXPECT_EQ(0, memcmp(ref_data, out_buf.getData(), out_buf.getLength()));
}

// This test verifies that option constructor throws
// an exception when config provided via ctor is malformed
// - DnrInstance config has only Svc Priority.
TEST(Option4DnrTest, fromConfigCtorNotEnoughFields) {
    // Prepare example config.
    const std::string config = "1, myhost1.example.com. | "
                               "2, myhost2.example.com., 192.168.0.1 192.168.0.2, alpn=dot\\,h3 "
                               "dohpath=/some\\|path{?dns} | " // "pipe char" escape is tested here
                               "3"; // 3rd dnr instance config has only Svc Priority

    OptionBuffer buf;
    buf.assign(config.begin(), config.end());

    // Create option instance. Check that constructor throws.
    Option4DnrPtr option;
    EXPECT_THROW(option.reset(new Option4Dnr(buf.begin(), buf.end(), true)),
                 BadValue);
    ASSERT_FALSE(option);
}

// This test verifies that option constructor throws
// an exception when config provided via ctor is malformed
// - DnrInstance config has too many comma separated fields.
TEST(Option4DnrTest, fromConfigCtorTooManyFields) {
    // Prepare example config.
    const std::string config = "1, myhost1.example.com. | "
                               "2, myhost2.example.com., 192.168.0.1 192.168.0.2, alpn=dot\\,h3 "
                               "dohpath=/some-path{?dns} | "
                               "3, test.com., 10.2.3.4, port=1234, extra"; // too many fields

    OptionBuffer buf;
    buf.assign(config.begin(), config.end());

    // Create option instance. Check that constructor throws.
    Option4DnrPtr option;
    EXPECT_THROW(option.reset(new Option4Dnr(buf.begin(), buf.end(), true)),
                 BadValue);
    ASSERT_FALSE(option);
}

// This test verifies that option constructor throws
// an exception when config provided via ctor is malformed
// - DnrInstance config has malformed Svc priority.
TEST(Option4DnrTest, fromConfigCtorWrongSvcPriority) {
    // Prepare example config.
    const std::string config = "1, myhost1.example.com. | "
                               "2, myhost2.example.com., 192.168.0.1 192.168.0.2, alpn=dot\\,h3 "
                               "dohpath=/some-path{?dns} | "
                               "what?, test.com., 10.2.3.4, port=1234"; // non-parsable Svc Priority

    OptionBuffer buf;
    buf.assign(config.begin(), config.end());

    // Create option instance. Check that constructor throws.
    Option4DnrPtr option;
    EXPECT_THROW(option.reset(new Option4Dnr(buf.begin(), buf.end(), true)),
                 BadValue);
    ASSERT_FALSE(option);
}

// This test verifies that option constructor throws
// an exception when config provided via ctor is malformed
// - DnrInstance config has malformed ADN.
TEST(Option4DnrTest, fromConfigCtorWrongAdn) {
    // Prepare example config.
    const std::string config = "1, myhost1.example.com. | "
                               "2, myhost2.example.com., 192.168.0.1 192.168.0.2, alpn=dot\\,h3 "
                               "dohpath=/some-path{?dns} | "
                               "100, , 10.2.3.4, port=1234"; // wrong ADN - empty

    OptionBuffer buf;
    buf.assign(config.begin(), config.end());

    // Create option instance. Check that constructor throws.
    Option4DnrPtr option;
    EXPECT_THROW(option.reset(new Option4Dnr(buf.begin(), buf.end(), true)),
                 InvalidOptionDnrDomainName);
    ASSERT_FALSE(option);
}

// This test verifies that option constructor throws
// an exception when config provided via ctor is malformed
// - DnrInstance config has malformed IP address.
TEST(Option4DnrTest, fromConfigCtorWrongIpAddr) {
    // Prepare example config.
    const std::string config = "1, myhost1.example.com. | "
                               "2, myhost2.example.com., 192.168.0.1 192.168.0.2, alpn=dot\\,h3 "
                               "dohpath=/some-path{?dns} | "
                               "100, test.net. , what?, port=1234"; // wrong IP address

    OptionBuffer buf;
    buf.assign(config.begin(), config.end());

    // Create option instance. Check that constructor throws.
    Option4DnrPtr option;
    EXPECT_THROW(option.reset(new Option4Dnr(buf.begin(), buf.end(), true)),
                 BadValue);
    ASSERT_FALSE(option);
}

// This test verifies that option constructor throws
// an exception when config provided via ctor is malformed
// - DnrInstance config has no IP address.
TEST(Option4DnrTest, fromConfigCtorNoIpAddr) {
    // Prepare example config.
    const std::string config = "1, myhost1.example.com. | "
                               "2, myhost2.example.com., 192.168.0.1 192.168.0.2, alpn=dot\\,h3 "
                               "dohpath=/some-path{?dns} | "
                               "100, test.net. , , port=1234"; // no IP address

    OptionBuffer buf;
    buf.assign(config.begin(), config.end());

    // Create option instance. Check that constructor throws.
    Option4DnrPtr option;
    EXPECT_THROW(option.reset(new Option4Dnr(buf.begin(), buf.end(), true)),
                 BadValue);
    ASSERT_FALSE(option);
}

// This test verifies that option constructor throws
// an exception when config provided via ctor is malformed
// - DnrInstance config has malformed SvcParams.
TEST(Option4DnrTest, fromConfigCtorWrongSvcParams) {
    // Prepare example config.
    const std::string config = "1, myhost1.example.com. | "
                               "2, myhost2.example.com., 192.168.0.1 192.168.0.2, alpn=dot\\,h3 "
                               "dohpath=/some-path{?dns} | "
                               "100, test.net. , 10.2.3.4, ="; // wrong SvcParams

    OptionBuffer buf;
    buf.assign(config.begin(), config.end());

    // Create option instance. Check that constructor throws.
    Option4DnrPtr option;
    EXPECT_THROW(option.reset(new Option4Dnr(buf.begin(), buf.end(), true)),
                 InvalidOptionDnrSvcParams);
    ASSERT_FALSE(option);
}

// This test verifies that option constructor throws
// an exception when config provided via ctor is malformed
// - DnrInstance config has malformed SvcParams - forbidden SvcParam.
TEST(Option4DnrTest, fromConfigCtorWrongSvcParamsForbiddenSvcParam) {
    // Prepare example config.
    const std::string config = "1, myhost1.example.com. | "
                               "2, myhost2.example.com., 192.168.0.1 192.168.0.2, alpn=dot\\,h3 "
                               "dohpath=/some-path{?dns} | "
                               "100, test.net. , 10.2.3.4, ipv4hint=1"; // forbidden SvcParam

    OptionBuffer buf;
    buf.assign(config.begin(), config.end());

    // Create option instance. Check that constructor throws.
    Option4DnrPtr option;
    EXPECT_THROW(option.reset(new Option4Dnr(buf.begin(), buf.end(), true)),
                 InvalidOptionDnrSvcParams);
    ASSERT_FALSE(option);
}

// This test verifies that option constructor throws
// an exception when config provided via ctor is malformed
// - DnrInstance config has malformed SvcParams - unknown SvcParam.
TEST(Option4DnrTest, fromConfigCtorWrongSvcParamsUnknownSvcParam) {
    // Prepare example config.
    const std::string config = "1, myhost1.example.com. | "
                               "2, myhost2.example.com., 192.168.0.1 192.168.0.2, alpn=dot\\,h3 "
                               "dohpath=/some-path{?dns} | "
                               "100, test.net. , 10.2.3.4, unknown=1"; // unknown SvcParam

    OptionBuffer buf;
    buf.assign(config.begin(), config.end());

    // Create option instance. Check that constructor throws.
    Option4DnrPtr option;
    EXPECT_THROW(option.reset(new Option4Dnr(buf.begin(), buf.end(), true)),
                 InvalidOptionDnrSvcParams);
    ASSERT_FALSE(option);
}

// This test verifies that option constructor throws
// an exception when config provided via ctor is malformed
// - DnrInstance config has malformed SvcParams - not supported SvcParam.
TEST(Option4DnrTest, fromConfigCtorWrongSvcParamsNotSupportedSvcParam) {
    // Prepare example config.
    const std::string config = "1, myhost1.example.com. | "
                               "2, myhost2.example.com., 192.168.0.1 192.168.0.2, alpn=dot\\,h3 "
                               "dohpath=/some-path{?dns} | "
                               "100, test.net. , 10.2.3.4, ech=1"; // ech is not supported in DNR

    OptionBuffer buf;
    buf.assign(config.begin(), config.end());

    // Create option instance. Check that constructor throws.
    Option4DnrPtr option;
    EXPECT_THROW(option.reset(new Option4Dnr(buf.begin(), buf.end(), true)),
                 InvalidOptionDnrSvcParams);
    ASSERT_FALSE(option);
}

// This test verifies that option constructor throws
// an exception when config provided via ctor is malformed
// - DnrInstance config has malformed SvcParams - duplicated SvcParam.
TEST(Option4DnrTest, fromConfigCtorWrongSvcParamsDuplicatedSvcParam) {
    // Prepare example config.
    const std::string config = "1, myhost1.example.com. | "
                               "2, myhost2.example.com., 192.168.0.1 192.168.0.2, alpn=dot\\,h3 "
                               "dohpath=/some-path{?dns} | "
                               "100, test.net. , 10.2.3.4,"
                               " port=1234 port=2345"; // duplicated SvcParam

    OptionBuffer buf;
    buf.assign(config.begin(), config.end());

    // Create option instance. Check that constructor throws.
    Option4DnrPtr option;
    EXPECT_THROW(option.reset(new Option4Dnr(buf.begin(), buf.end(), true)),
                 InvalidOptionDnrSvcParams);
    ASSERT_FALSE(option);
}

// This test verifies that option constructor throws
// an exception when config provided via ctor is malformed
// - DnrInstance config has malformed SvcParams - unknown AlpnId.
TEST(Option4DnrTest, fromConfigCtorWrongSvcParamsUnknownAlpnId) {
    // Prepare example config.
    const std::string config = "1, myhost1.example.com. | "
                               "2, myhost2.example.com., 192.168.0.1 192.168.0.2, alpn=dot\\,h3 "
                               "dohpath=/some-path{?dns} | "
                               "100, test.net. , 10.2.3.4, alpn=dot\\,unknown"; // unknown alpn-id

    OptionBuffer buf;
    buf.assign(config.begin(), config.end());

    // Create option instance. Check that constructor throws.
    Option4DnrPtr option;
    EXPECT_THROW(option.reset(new Option4Dnr(buf.begin(), buf.end(), true)),
                 InvalidOptionDnrSvcParams);
    ASSERT_FALSE(option);
}

// This test verifies that option constructor throws
// an exception when config provided via ctor is malformed
// - DnrInstance config has malformed SvcParams - dohpath is missing.
TEST(Option4DnrTest, fromConfigCtorWrongSvcParamsSkippedDohpath) {
    // Prepare example config.
    const std::string config = "1, myhost1.example.com. | "
                               "2, myhost2.example.com., 192.168.0.1 192.168.0.2, alpn=dot\\,h3 "
                               "dohpath=/some-path{?dns} | "
                               "100, test.net. , 10.2.3.4, alpn=h3"; // dohpath must be there
                                                                     // when any of http alpn-ids
                                                                     // are included

    OptionBuffer buf;
    buf.assign(config.begin(), config.end());

    // Create option instance. Check that constructor throws.
    Option4DnrPtr option;
    EXPECT_THROW(option.reset(new Option4Dnr(buf.begin(), buf.end(), true)),
                 InvalidOptionDnrSvcParams);
    ASSERT_FALSE(option);
}

// This test verifies that option constructor throws
// an exception when config provided via ctor is malformed
// - DnrInstance config has malformed SvcParams - port can't be parsed.
TEST(Option4DnrTest, fromConfigCtorWrongSvcParamsWrongPort) {
    // Prepare example config.
    const std::string config = "1, myhost1.example.com. | "
                               "2, myhost2.example.com., 192.168.0.1 192.168.0.2, alpn=dot\\,h3 "
                               "dohpath=/some-path{?dns} | "
                               "100, test.net. , 10.2.3.4, port=what?"; // port must be uint_16

    OptionBuffer buf;
    buf.assign(config.begin(), config.end());

    // Create option instance. Check that constructor throws.
    Option4DnrPtr option;
    EXPECT_THROW(option.reset(new Option4Dnr(buf.begin(), buf.end(), true)),
                 InvalidOptionDnrSvcParams);
    ASSERT_FALSE(option);
}

// This test verifies that option constructor throws
// an exception when config provided via ctor is malformed
// - DnrInstance config has malformed SvcParams - dns var missing in dohpath.
TEST(Option4DnrTest, fromConfigCtorWrongSvcParamsMissingVarInDohpath) {
    // Prepare example config.
    const std::string config = "1, myhost1.example.com. | "
                               "2, myhost2.example.com., 192.168.0.1 192.168.0.2, alpn=dot\\,h3 "
                               "dohpath=/some-path{?dns} | "
                               "100, test.net. , 10.2.3.4, alpn=dot\\,h3 "
                               "dohpath=/some-path"; // dns var is missing in the uri of dohpath

    OptionBuffer buf;
    buf.assign(config.begin(), config.end());

    // Create option instance. Check that constructor throws.
    Option4DnrPtr option;
    EXPECT_THROW(option.reset(new Option4Dnr(buf.begin(), buf.end(), true)),
                 InvalidOptionDnrSvcParams);
    ASSERT_FALSE(option);
}

// This test verifies that option constructor throws
// an exception when config provided via ctor is malformed
// - IPv6 address given.
TEST(Option4DnrTest, fromConfigCtorIPv6Address) {
    // Prepare example config.
    const std::string config = "100, dot1.example.org., 2001:db8::1";

    OptionBuffer buf;
    buf.assign(config.begin(), config.end());

    // Create option instance. Check that constructor throws.
    Option4DnrPtr option;
    EXPECT_THROW(option.reset(new Option4Dnr(buf.begin(), buf.end(), true)),
                 BadValue);
    ASSERT_FALSE(option);
}

// This test verifies option packing into wire data.
// Provided data to pack contains 2 DNR instances:
// 1. ADN only mode
// 2. All fields included and SvcParams are given in wrong order.
TEST(Option4DnrTest, fromConfigCtorPackUnorderedSvcParams) {
    // Create option instance. Check that constructor doesn't throw.
    const std::string config = "1, myhost1.example.com. | "
                               "2, myhost2.example.com., 192.168.0.1 192.168.0.2, port=85"
                               " alpn=dot\\,doq"; // SvcParams are NOT in increasing order
    OptionBuffer opt_buf;
    opt_buf.assign(config.begin(), config.end());
    Option4DnrPtr option;
    EXPECT_NO_THROW(option.reset(new Option4Dnr(opt_buf.begin(), opt_buf.end(), true)));
    ASSERT_TRUE(option);

    // Prepare on-wire format of the option.
    isc::util::OutputBuffer out_buf(10);
    ASSERT_NO_THROW(option->pack(out_buf));

    // Prepare reference data.
    const uint8_t ref_data[] = {
        DHO_V4_DNR,                                      // Option code
        79,                                              // Option len
        0x00, 24,                                        // DNR Instance Data Len
        0x00, 0x01,                                      // Service priority is 1 dec
        21,                                              // ADN Length is 21 dec
        0x07, 0x6D, 0x79, 0x68, 0x6F, 0x73, 0x74, '1',   // FQDN: myhost1.
        0x07, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65,  // example.
        0x03, 0x63, 0x6F, 0x6D, 0x00,                    // com.
        0x00, 51,                                        // DNR Instance Data Len
        0x00, 0x02,                                      // Service priority is 2 dec
        21,                                              // ADN Length is 21 dec
        0x07, 0x6D, 0x79, 0x68, 0x6F, 0x73, 0x74, '2',   // FQDN: myhost2.
        0x07, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65,  // example.
        0x03, 0x63, 0x6F, 0x6D, 0x00,                    // com.
        8,                                               // Addr Len
        192,  168,  0,    1,                             // IP address 1
        192,  168,  0,    2,                             // IP address 2
        0,    1,                                         // SvcParamKey alpn
        0,    8,                                         // SvcParamVal Len
        3,    'd',  'o',  't',                           // 3 octets long alpn-id dot
        3,    'd',  'o',  'q',                           // 3 octets long alpn-id doq
        0,    3,                                         // SvcParamKey port is in correct order
                                                         // after alpn
        0,    2,                                         // SvcParamVal Len
        0,    85                                         // port nr
    };

    size_t ref_data_size = sizeof(ref_data) / sizeof(ref_data[0]);

    // Check if the buffer has the same length as the reference data,
    // so as they can be compared directly.
    ASSERT_EQ(ref_data_size, out_buf.getLength());
    EXPECT_EQ(0, memcmp(ref_data, out_buf.getData(), out_buf.getLength()));
}

// This test verifies option constructor from wire data in terms
// of proper data unpacking.
// Provided wire data contains 1 DNR instance:
// 1. ADN only mode
TEST(Option4DnrTest, unpackOneAdnOnly) {
    // Prepare data to decode - ADN only mode 1 DNR instance.
    const uint8_t buf_data[] = {
        0x00, 24,                                        // DNR Instance Data Len
        0x00, 0x01,                                      // Service priority is 1 dec
        21,                                              // ADN Length is 21 dec
        0x07, 0x6D, 0x79, 0x68, 0x6F, 0x73, 0x74, '1',   // FQDN: myhost1.
        0x07, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65,  // example.
        0x03, 0x63, 0x6F, 0x6D, 0x00                     // com.
    };

    OptionBuffer buf(buf_data, buf_data + sizeof(buf_data));
    // Create option instance. Check that constructor doesn't throw.
    Option4DnrPtr option;
    EXPECT_NO_THROW(option.reset(new Option4Dnr(buf.begin(), buf.end())));
    ASSERT_TRUE(option);

    // Check if member variables were correctly set by ctor.
    EXPECT_EQ(Option::V4, option->getUniverse());
    EXPECT_EQ(DHO_V4_DNR, option->getType());

    // Check if data was unpacked correctly from wire data.
    EXPECT_EQ(24, option->getDnrInstances()[0].getDnrInstanceDataLength());
    EXPECT_EQ(1, option->getDnrInstances()[0].getServicePriority());
    EXPECT_EQ(21, option->getDnrInstances()[0].getAdnLength());
    EXPECT_EQ("myhost1.example.com.", option->getDnrInstances()[0].getAdnAsText());

    // This is ADN only mode, so Addr Length and SvcParams Length
    // are both expected to be zero.
    EXPECT_EQ(0, option->getDnrInstances()[0].getAddrLength());
    EXPECT_EQ(0, option->getDnrInstances()[0].getSvcParamsLength());

    // BTW let's check if len() works ok. In ADN only mode, DNR Instance Data Len
    // is set to ADN Len (21) + 3 = 24.
    // expected len: 1x(24 (ADN+ADN Len+Service priority) + 2 (DNR Instance Data Len)) + 2 (headers)
    // = 28
    EXPECT_EQ(28, option->len());

    // BTW let's check if toText() works ok.
    // toText() len does not count in headers len.
    EXPECT_EQ("type=162(V4_DNR), len=26, "
              "DNR Instance 1(Instance len=24, service_priority=1, "
              "adn_length=21, adn='myhost1.example.com.')",
              option->toText());
}

// This test verifies option constructor from wire data in terms
// of proper data unpacking.
// Provided wire data contains 1 DNR instance:
// 1. All fields included (IP addresses and service params also).
TEST(Option4DnrTest, unpackOneDnrInstance) {
    // Prepare data to decode - 1 DNR instance.
    const uint8_t buf_data[] = {
        0x00, 45,                                        // DNR Instance Data Len
        0x00, 0x01,                                      // Service priority is 1 dec
        21,                                              // ADN Length is 21 dec
        0x07, 0x6D, 0x79, 0x68, 0x6F, 0x73, 0x74, '1',   // FQDN: myhost1.
        0x07, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65,  // example.
        0x03, 0x63, 0x6F, 0x6D, 0x00,                    // com.
        8,                                               // Addr Len
        192,  168,  0,    1,                             // IP address 1
        192,  168,  0,    2,                             // IP address 2
        0,    1,                                         // SvcParamKey alpn
        0,    8,                                         // SvcParamVal Len
        3,    'd',  'o',  't',                           // 3 octets long alpn-id dot
        3,    'd',  'o',  'q'                            // 3 octets long alpn-id doq
    };

    OptionBuffer buf(buf_data, buf_data + sizeof(buf_data));
    // Create option instance. Check that constructor doesn't throw.
    Option4DnrPtr option;
    EXPECT_NO_THROW(option.reset(new Option4Dnr(buf.begin(), buf.end())));
    ASSERT_TRUE(option);

    // Check if member variables were correctly set by ctor.
    EXPECT_EQ(Option::V4, option->getUniverse());
    EXPECT_EQ(DHO_V4_DNR, option->getType());

    // Check if data was unpacked correctly from wire data.
    const DnrInstance& dnr_i = option->getDnrInstances()[0];
    EXPECT_EQ(45, dnr_i.getDnrInstanceDataLength());
    EXPECT_EQ(1, dnr_i.getServicePriority());
    EXPECT_EQ(21, dnr_i.getAdnLength());
    EXPECT_EQ("myhost1.example.com.", dnr_i.getAdnAsText());
    EXPECT_EQ(8, dnr_i.getAddrLength());
    EXPECT_EQ(2, dnr_i.getAddresses().size());
    EXPECT_EQ("192.168.0.1", dnr_i.getAddresses()[0].toText());
    EXPECT_EQ("192.168.0.2", dnr_i.getAddresses()[1].toText());

    // Reference SvcParams on-wire data buffer.
    const OptionBuffer svc_params = {
        0, 1,              // SvcParamKey alpn
        0, 8,              // SvcParamVal Len
        3, 'd', 'o', 't',  // 3 octets long alpn-id dot
        3, 'd', 'o', 'q'   // 3 octets long alpn-id doq
    };

    EXPECT_EQ(svc_params, dnr_i.getSvcParams());
    EXPECT_EQ(svc_params.size(), dnr_i.getSvcParamsLength());
    EXPECT_EQ(49, option->len());
}

// This test verifies option constructor from wire data in terms
// of proper data unpacking.
// Provided wire data contains 2 DNR instances:
// 1. ADN only mode
// 2. All fields included (IP addresses and service params also).
TEST(Option4DnrTest, unpackMixedDnrInstances) {
    // Prepare data to decode - 2 DNR instances.
    const uint8_t buf_data[] = {
        0x00, 24,                                        // DNR Instance Data Len
        0x00, 0x01,                                      // Service priority is 1 dec
        21,                                              // ADN Length is 21 dec
        0x07, 0x6D, 0x79, 0x68, 0x6F, 0x73, 0x74, '1',   // FQDN: myhost1.
        0x07, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65,  // example.
        0x03, 0x63, 0x6F, 0x6D, 0x00,                    // com.
        0x00, 58,                                        // DNR Instance Data Len
        0x00, 0x02,                                      // Service priority is 2 dec
        21,                                              // ADN Length is 21 dec
        0x07, 0x6D, 0x79, 0x68, 0x6F, 0x73, 0x74, '2',   // FQDN: myhost2.
        0x07, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65,  // example.
        0x03, 0x63, 0x6F, 0x6D, 0x00,                    // com.
        8,                                               // Addr Len
        192,  168,  0,    1,                             // IP address 1
        192,  168,  0,    2,                             // IP address 2
        0,   1,                                  // SvcParamKey alpn
        0,   3,                                  // SvcParamVal Len
        2,   'h',  '3',                          // 2 octets long alpn-id http/3
        0,   3,                                  // SvcParamKey port
        0,   2,                                  // SvcParamVal Len
        0x4, 0xD2,                               // 1234 in hex
        0,   7,                                  // SvcParamKey dohpath
        0,   8,                                  // SvcParamVal Len
        '/', 'q',  '{', '?', 'd', 'n', 's', '}'  // dohpath value
    };

    OptionBuffer buf(buf_data, buf_data + sizeof(buf_data));
    // Create option instance. Check that constructor doesn't throw.
    Option4DnrPtr option;
    EXPECT_NO_THROW(option.reset(new Option4Dnr(buf.begin(), buf.end())));
    ASSERT_TRUE(option);

    // Check if member variables were correctly set by ctor.
    EXPECT_EQ(Option::V4, option->getUniverse());
    EXPECT_EQ(DHO_V4_DNR, option->getType());

    // Check if data was unpacked correctly from wire data.
    const DnrInstance& dnr_1 = option->getDnrInstances()[0];
    EXPECT_EQ(24, dnr_1.getDnrInstanceDataLength());
    EXPECT_EQ(1, dnr_1.getServicePriority());
    EXPECT_EQ(21, dnr_1.getAdnLength());
    EXPECT_EQ("myhost1.example.com.", dnr_1.getAdnAsText());
    EXPECT_EQ(0, dnr_1.getAddrLength());
    EXPECT_EQ(0, dnr_1.getSvcParamsLength());

    const DnrInstance& dnr_2 = option->getDnrInstances()[1];
    EXPECT_EQ(58, dnr_2.getDnrInstanceDataLength());
    EXPECT_EQ(2, dnr_2.getServicePriority());
    EXPECT_EQ(21, dnr_2.getAdnLength());
    EXPECT_EQ("myhost2.example.com.", dnr_2.getAdnAsText());
    EXPECT_EQ(8, dnr_2.getAddrLength());
    EXPECT_EQ(2, dnr_2.getAddresses().size());
    EXPECT_EQ("192.168.0.1", dnr_2.getAddresses()[0].toText());
    EXPECT_EQ("192.168.0.2", dnr_2.getAddresses()[1].toText());

    // Reference SvcParams on-wire data buffer.
    const OptionBuffer svc_params = {
        0,   1,                                  // SvcParamKey alpn
        0,   3,                                  // SvcParamVal Len
        2,   'h',  '3',                          // 2 octets long alpn-id http/3
        0,   3,                                  // SvcParamKey port
        0,   2,                                  // SvcParamVal Len
        0x4, 0xD2,                               // 1234 in hex
        0,   7,                                  // SvcParamKey dohpath
        0,   8,                                  // SvcParamVal Len
        '/', 'q',  '{', '?', 'd', 'n', 's', '}'  // dohpath value
    };
    EXPECT_EQ(svc_params, dnr_2.getSvcParams());
    EXPECT_EQ(svc_params.size(), dnr_2.getSvcParamsLength());

    EXPECT_EQ(88, option->len());
}

// Test checks that exception is thrown when trying to unpack malformed wire data
// - mandatory fields are truncated - Service Priority and ADN Len truncated.
TEST(Option4DnrTest, unpackTruncatedDnrInstanceDataLen) {
    // Prepare malformed data to decode.
    const uint8_t buf_data[] = {
        0x00, 24,                                        // DNR Instance Data Len
        0x00, 0x01,                                      // Service priority is 1 dec
        21,                                              // ADN Length is 21 dec
        0x07, 0x6D, 0x79, 0x68, 0x6F, 0x73, 0x74, '1',   // FQDN: myhost1.
        0x07, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65,  // example.
        0x03, 0x63, 0x6F, 0x6D, 0x00,                    // com.
        0x00, 62                                         // DNR Instance Data Len truncated
    };

    OptionBuffer buf(buf_data, buf_data + sizeof(buf_data));

    // Create option instance. Check that constructor throws an exception while doing unpack.
    Option4DnrPtr option;
    EXPECT_THROW(option.reset(new Option4Dnr(buf.begin(), buf.end())), OutOfRange);
    ASSERT_FALSE(option);
}

// Test checks that exception is thrown when trying to unpack malformed wire data
// - DNR instance data truncated when compared to DNR Instance Data Len field.
TEST(Option4DnrTest, unpackTruncatedDnrInstanceData) {
    // Prepare malformed data to decode.
    const uint8_t buf_data[] = {
        0x00, 24,                                        // DNR Instance Data Len
        0x00, 0x01,                                      // Service priority is 1 dec
        21,                                              // ADN Length is 21 dec
        0x07, 0x6D, 0x79, 0x68, 0x6F, 0x73, 0x74, '1',   // FQDN: myhost1.
        0x07, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65,  // example.
        0x03, 0x63, 0x6F, 0x6D, 0x00,                    // com.
        0x00, 62,                                        // DNR Instance Data Len
        0x00, 0x02,                                      // Service priority is 2 dec
        21                                               // ADN Length is 21 dec
        // the rest of DNR instance data is truncated
    };

    OptionBuffer buf(buf_data, buf_data + sizeof(buf_data));

    // Create option instance. Check that constructor throws an exception while doing unpack.
    Option4DnrPtr option;
    EXPECT_THROW(option.reset(new Option4Dnr(buf.begin(), buf.end())), OutOfRange);
    ASSERT_FALSE(option);
}

// Test checks that exception is thrown when trying to unpack malformed wire data
// - ADN field data truncated.
TEST(Option4DnrTest, unpackTruncatedAdn) {
    // Prepare malformed data to decode.
    const uint8_t buf_data[] = {
        0x00, 24,                                        // DNR Instance Data Len
        0x00, 0x01,                                      // Service priority is 1 dec
        21,                                              // ADN Length is 21 dec
        0x07, 0x6D, 0x79, 0x68, 0x6F, 0x73, 0x74, '1',   // FQDN: myhost1.
        0x07, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65,  // example.
        0x03, 0x63, 0x6F, 0x6D, 0x00,                    // com.
        0x00, 3,                                         // DNR Instance Data Len
        0x00, 0x02,                                      // Service priority is 2 dec
        21                                               // ADN Length is 21 dec
        // ADN data is missing.
    };

    OptionBuffer buf(buf_data, buf_data + sizeof(buf_data));

    // Create option instance. Check that constructor throws an exception while doing unpack.
    Option4DnrPtr option;
    EXPECT_THROW(option.reset(new Option4Dnr(buf.begin(), buf.end())), BadValue);
    ASSERT_FALSE(option);
}

// Test checks that exception is thrown when trying to unpack malformed wire data
// - ADN FQDN contains only whitespace - non-valid FQDN.
TEST(Option4DnrTest, unpackInvalidFqdnAdn) {
    // Prepare malformed data to decode.
    const uint8_t buf_data[] = {
        0x00, 24,                                        // DNR Instance Data Len
        0x00, 0x01,                                      // Service priority is 1 dec
        21,                                              // ADN Length is 21 dec
        0x07, 0x6D, 0x79, 0x68, 0x6F, 0x73, 0x74, '1',   // FQDN: myhost1.
        0x07, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65,  // example.
        0x03, 0x63, 0x6F, 0x6D, 0x00,                    // com.
        0x00, 4,                                         // DNR Instance Data Len
        0x00, 0x02,                                      // Service priority is 2 dec
        1,                                               // ADN Length is 1 dec
        ' '                                              // ADN contains only whitespace
    };

    OptionBuffer buf(buf_data, buf_data + sizeof(buf_data));

    // Create option instance. Check that constructor throws an exception while doing unpack.
    Option4DnrPtr option;
    EXPECT_THROW(option.reset(new Option4Dnr(buf.begin(), buf.end())), InvalidOptionDnrDomainName);
    ASSERT_FALSE(option);
}

// Test checks that exception is thrown when trying to unpack malformed wire data
// - ADN Length is 0 and no ADN FQDN at all.
TEST(Option4DnrTest, unpackNoFqdnAdn) {
    // Prepare malformed data to decode.
    const uint8_t buf_data[] = {
        0x00, 24,                                        // DNR Instance Data Len
        0x00, 0x01,                                      // Service priority is 1 dec
        21,                                              // ADN Length is 21 dec
        0x07, 0x6D, 0x79, 0x68, 0x6F, 0x73, 0x74, '1',   // FQDN: myhost1.
        0x07, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65,  // example.
        0x03, 0x63, 0x6F, 0x6D, 0x00,                    // com.
        0x00, 3,                                         // DNR Instance Data Len
        0x00, 0x02,                                      // Service priority is 2 dec
        0                                                // ADN Length is 0 dec
    };

    OptionBuffer buf(buf_data, buf_data + sizeof(buf_data));

    // Create option instance. Check that constructor throws an exception while doing unpack.
    Option4DnrPtr option;
    EXPECT_THROW(option.reset(new Option4Dnr(buf.begin(), buf.end())), InvalidOptionDnrDomainName);
    ASSERT_FALSE(option);
}

// Test checks that exception is thrown when trying to unpack malformed wire data
// - IPv4 address(es) field data truncated.
TEST(Option4DnrTest, unpackTruncatedIpAddress) {
    // Prepare malformed data to decode.
    const uint8_t buf_data[] = {
        0x00, 24,                                        // DNR Instance Data Len
        0x00, 0x01,                                      // Service priority is 1 dec
        21,                                              // ADN Length is 21 dec
        0x07, 0x6D, 0x79, 0x68, 0x6F, 0x73, 0x74, '1',   // FQDN: myhost1.
        0x07, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65,  // example.
        0x03, 0x63, 0x6F, 0x6D, 0x00,                    // com.
        0x00, 25,                                        // DNR Instance Data Len
        0x00, 0x02,                                      // Service priority is 2 dec
        21,                                              // ADN Length is 21 dec
        0x07, 0x6D, 0x79, 0x68, 0x6F, 0x73, 0x74, '2',   // FQDN: myhost2.
        0x07, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65,  // example.
        0x03, 0x63, 0x6F, 0x6D, 0x00,                    // com.
        8                                                // Addr Len
        // the rest of DNR instance data is truncated.
    };

    OptionBuffer buf(buf_data, buf_data + sizeof(buf_data));

    // Create option instance. Check that constructor throws an exception while doing unpack.
    Option4DnrPtr option;
    EXPECT_THROW(option.reset(new Option4Dnr(buf.begin(), buf.end())), BadValue);
    ASSERT_FALSE(option);
}

// Test checks that exception is thrown when trying to unpack malformed wire data
// - Addr length is 0 and no IPv4 addresses at all.
TEST(Option4DnrTest, unpackNoIpAddress) {
    // Prepare malformed data to decode.
    const uint8_t buf_data[] = {
        0x00, 24,                                        // DNR Instance Data Len
        0x00, 0x01,                                      // Service priority is 1 dec
        21,                                              // ADN Length is 21 dec
        0x07, 0x6D, 0x79, 0x68, 0x6F, 0x73, 0x74, '1',   // FQDN: myhost1.
        0x07, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65,  // example.
        0x03, 0x63, 0x6F, 0x6D, 0x00,                    // com.
        0x00, 25,                                        // DNR Instance Data Len
        0x00, 0x02,                                      // Service priority is 2 dec
        21,                                              // ADN Length is 21 dec
        0x07, 0x6D, 0x79, 0x68, 0x6F, 0x73, 0x74, '2',   // FQDN: myhost2.
        0x07, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65,  // example.
        0x03, 0x63, 0x6F, 0x6D, 0x00,                    // com.
        0                                                // Addr Len = 0
    };

    OptionBuffer buf(buf_data, buf_data + sizeof(buf_data));

    // Create option instance. Check that constructor throws an exception while doing unpack.
    Option4DnrPtr option;
    EXPECT_THROW(option.reset(new Option4Dnr(buf.begin(), buf.end())), OutOfRange);
    ASSERT_FALSE(option);
}

// Test checks that exception is thrown when trying to unpack malformed wire data
// - Addr length is not a multiple of 4.
TEST(Option4DnrTest, unpackIpAddressNon4Modulo) {
    // Prepare malformed data to decode.
    const uint8_t buf_data[] = {
        0x00, 24,                                        // DNR Instance Data Len
        0x00, 0x01,                                      // Service priority is 1 dec
        21,                                              // ADN Length is 21 dec
        0x07, 0x6D, 0x79, 0x68, 0x6F, 0x73, 0x74, '1',   // FQDN: myhost1.
        0x07, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65,  // example.
        0x03, 0x63, 0x6F, 0x6D, 0x00,                    // com.
        0x00, 32,                                        // DNR Instance Data Len
        0x00, 0x02,                                      // Service priority is 2 dec
        21,                                              // ADN Length is 21 dec
        0x07, 0x6D, 0x79, 0x68, 0x6F, 0x73, 0x74, '2',   // FQDN: myhost2.
        0x07, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65,  // example.
        0x03, 0x63, 0x6F, 0x6D, 0x00,                    // com.
        7,                                               // Addr Len
        192,  168,  0,    1,                             // IP address 1
        192,  168,  0                                    // IP address 2 truncated
    };

    OptionBuffer buf(buf_data, buf_data + sizeof(buf_data));

    // Create option instance. Check that constructor throws an exception while doing unpack.
    Option4DnrPtr option;
    EXPECT_THROW(option.reset(new Option4Dnr(buf.begin(), buf.end())), OutOfRange);
    ASSERT_FALSE(option);
}

// Test checks that exception is thrown when trying to unpack malformed wire data
// - SvcParams contain unknown SvcParamKey.
TEST(Option4DnrTest, unpackvcParamsInvalidCharKey) {
    // Prepare malformed data to decode.
    const uint8_t buf_data[] = {
        0x00, 24,                                        // DNR Instance Data Len
        0x00, 0x01,                                      // Service priority is 1 dec
        21,                                              // ADN Length is 21 dec
        0x07, 0x6D, 0x79, 0x68, 0x6F, 0x73, 0x74, '1',   // FQDN: myhost1.
        0x07, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65,  // example.
        0x03, 0x63, 0x6F, 0x6D, 0x00,                    // com.
        0x00, 39,                                        // DNR Instance Data Len
        0x00, 0x02,                                      // Service priority is 2 dec
        21,                                              // ADN Length is 21 dec
        0x07, 0x6D, 0x79, 0x68, 0x6F, 0x73, 0x74, '2',   // FQDN: myhost2.
        0x07, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65,  // example.
        0x03, 0x63, 0x6F, 0x6D, 0x00,                    // com.
        8,                                               // Addr Len
        192,  168,  0,    1,                             // IP address 1
        192,  168,  0,    2,                             // IP address 2 truncated
        0,    1,                                         // SvcParamKey alpn
        0,    8,                                         // SvcParamVal Len
        3,    'd',  'o',  't',                           // 3 octets long alpn-id dot
        3,    'd',  'o',  'q',                           // 3 octets long alpn-id doq
        0,    99,                                        // unknown SvcParamKey
        0,    2,                                         // SvcParamVal Len
        1,    2                                          // random data
    };

    OptionBuffer buf(buf_data, buf_data + sizeof(buf_data));

    // Create option instance. Check that constructor throws an exception while doing unpack.
    Option4DnrPtr option;
    EXPECT_THROW(option.reset(new Option4Dnr(buf.begin(), buf.end())), InvalidOptionDnrSvcParams);
    ASSERT_FALSE(option);
}

// This test verifies that string representation of the option returned by
// toText method is correctly formatted.
TEST(Option4DnrTest, toText) {
    // Prepare example config.
    const std::string config = "1, myhost1.example.com.";

    OptionBuffer in_buf;
    in_buf.assign(config.begin(), config.end());

    // Create option instance. Check that constructor doesn't throw.
    Option4DnrPtr option;
    EXPECT_NO_THROW(option.reset(new Option4Dnr(in_buf.begin(), in_buf.end(), true)));
    ASSERT_TRUE(option);

    // Let's check if toText() works ok.
    // toText() len does not count in headers len.
    const int indent = 4;
    std::string expected = "    type=162(V4_DNR), len=26, "  // the indentation of 4 spaces
                           "DNR Instance 1(Instance len=24, service_priority=1, "
                           "adn_length=21, adn='myhost1.example.com.')";
    EXPECT_EQ(expected, option->toText(indent));
}

}  // namespace