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

#include <config.h>
#include <dhcp/option4_client_fqdn.h>
#include <dns/name.h>
#include <util/buffer.h>
#include <boost/scoped_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.

namespace {

using namespace isc;
using namespace isc::dhcp;

// RAII device to make sure that lenient parsing flag is reset to false on exit.
class LenientOptionParsing {
public:
    LenientOptionParsing(bool value) {
        Option::lenient_parsing_ = value;
    }

    ~LenientOptionParsing() {
        Option::lenient_parsing_ = false;
    }
};

// This test verifies that constructor accepts empty partial domain-name but
// does not accept empty fully qualified domain name.
TEST(Option4ClientFqdnTest, constructEmptyName) {<--- syntax error
    // Create an instance of the source option.
    boost::scoped_ptr<Option4ClientFqdn> option;
    ASSERT_NO_THROW(
        option.reset(new Option4ClientFqdn(Option4ClientFqdn::FLAG_S |
                                           Option4ClientFqdn::FLAG_E,
                                           Option4ClientFqdn::RCODE_SERVER(),
                                           "",
                                           Option4ClientFqdn::PARTIAL))
    );
    ASSERT_TRUE(option);
    EXPECT_FALSE(option->getFlag(Option4ClientFqdn::FLAG_O));
    EXPECT_FALSE(option->getFlag(Option4ClientFqdn::FLAG_N));
    EXPECT_TRUE(option->getFlag(Option4ClientFqdn::FLAG_S));
    EXPECT_TRUE(option->getFlag(Option4ClientFqdn::FLAG_E));
    EXPECT_TRUE(option->getDomainName().empty());
    EXPECT_EQ(Option4ClientFqdn::PARTIAL, option->getDomainNameType());

    // Constructor should not accept empty fully qualified domain name.
    EXPECT_THROW(Option4ClientFqdn(Option4ClientFqdn::FLAG_S |
                                   Option4ClientFqdn::FLAG_E,
                                   Option4ClientFqdn::RCODE_CLIENT(),
                                   "",
                                   Option4ClientFqdn::FULL),
                 InvalidOption4FqdnDomainName);
    // This check is similar to previous one, but using domain-name comprising
    // a single space character. This should be treated as empty domain-name.
    EXPECT_THROW(Option4ClientFqdn(Option4ClientFqdn::FLAG_S |
                                   Option4ClientFqdn::FLAG_E,
                                   Option4ClientFqdn::RCODE_CLIENT(),
                                   " ",
                                   Option4ClientFqdn::FULL),
                 InvalidOption4FqdnDomainName);

    // Try different constructor.
    ASSERT_NO_THROW(
        option.reset(new Option4ClientFqdn(Option4ClientFqdn::FLAG_O |
                                           Option4ClientFqdn::FLAG_E,
                                           Option4ClientFqdn::RCODE_SERVER()))
    );
    ASSERT_TRUE(option);
    EXPECT_TRUE(option->getFlag(Option4ClientFqdn::FLAG_O));
    EXPECT_TRUE(option->getFlag(Option4ClientFqdn::FLAG_E));
    EXPECT_FALSE(option->getFlag(Option4ClientFqdn::FLAG_N));
    EXPECT_FALSE(option->getFlag(Option4ClientFqdn::FLAG_S));
    EXPECT_TRUE(option->getDomainName().empty());
    EXPECT_EQ(Option4ClientFqdn::PARTIAL, option->getDomainNameType());
}

// This test verifies that copy constructor makes a copy of the option and
// the source option instance can be deleted (both instances don't share
// any resources).
TEST(Option4ClientFqdnTest, copyConstruct) {
    // Create an instance of the source option.
    boost::scoped_ptr<Option4ClientFqdn> option;
    ASSERT_NO_THROW(
        option.reset(new Option4ClientFqdn(Option4ClientFqdn::FLAG_S |
                                           Option4ClientFqdn::FLAG_E,
                                           Option4ClientFqdn::RCODE_SERVER(),
                                           "myhost.example.com",
                                           Option4ClientFqdn::FULL))
    );
    ASSERT_TRUE(option);

    // Use copy constructor to create a second instance of the option.
    boost::scoped_ptr<Option4ClientFqdn> option_copy;
    ASSERT_NO_THROW(
        option_copy.reset(new Option4ClientFqdn(*option))
    );
    ASSERT_TRUE(option_copy);

    // Copy construction should result in no shared resources between
    // two objects. In particular, pointer to implementation should not
    // be shared. Thus, we can release the source object now.
    option.reset();

    // Verify that all parameters have been copied to the target object.
    EXPECT_TRUE(option_copy->getFlag(Option4ClientFqdn::FLAG_S));
    EXPECT_TRUE(option_copy->getFlag(Option4ClientFqdn::FLAG_E));
    EXPECT_FALSE(option_copy->getFlag(Option4ClientFqdn::FLAG_O));
    EXPECT_FALSE(option_copy->getFlag(Option4ClientFqdn::FLAG_N));
    EXPECT_EQ("myhost.example.com.", option_copy->getDomainName());
    EXPECT_EQ(Option4ClientFqdn::FULL, option_copy->getDomainNameType());

    // Do another test with different parameters to verify that parameters
    // change when copied object is changed.

    // Create an option with different parameters.
    ASSERT_NO_THROW(
        option.reset(new Option4ClientFqdn(Option4ClientFqdn::FLAG_O |
                                           Option4ClientFqdn::FLAG_E,
                                           Option4ClientFqdn::RCODE_SERVER(),
                                           "example",
                                           Option4ClientFqdn::PARTIAL))
    );
    ASSERT_TRUE(option);

    // Call copy-constructor to copy the option.
    ASSERT_NO_THROW(
        option_copy.reset(new Option4ClientFqdn(*option))
    );
    ASSERT_TRUE(option_copy);

    option.reset();

    EXPECT_FALSE(option_copy->getFlag(Option4ClientFqdn::FLAG_S));
    EXPECT_TRUE(option_copy->getFlag(Option4ClientFqdn::FLAG_E));
    EXPECT_TRUE(option_copy->getFlag(Option4ClientFqdn::FLAG_O));
    EXPECT_FALSE(option_copy->getFlag(Option4ClientFqdn::FLAG_N));
    EXPECT_EQ("example", option_copy->getDomainName());
    EXPECT_EQ(Option4ClientFqdn::PARTIAL, option_copy->getDomainNameType());
}

// This test verifies that the option in the on-wire format with the domain-name
// encoded in the canonical format is parsed correctly.
TEST(Option4ClientFqdnTest, constructFromWire) {
    // The E flag sets the domain-name format to canonical.
    const uint8_t in_data[] = {
        Option4ClientFqdn::FLAG_S | Option4ClientFqdn::FLAG_E, // flags
        0,                                                     // RCODE1
        0,                                                     // RCODE2
        6, 77, 121, 104, 111, 115, 116,                        // Myhost.
        7, 69, 120, 97, 109, 112, 108, 101,                    // Example.
        3, 67, 111, 109, 0                                     // Com.
    };
    size_t in_data_size = sizeof(in_data) / sizeof(in_data[0]);
    OptionBuffer in_buf(in_data, in_data + in_data_size);

    // Create option instance. Check that constructor doesn't throw.
    boost::scoped_ptr<Option4ClientFqdn> option;
    ASSERT_NO_THROW(
        option.reset(new Option4ClientFqdn(in_buf.begin(), in_buf.end()))
    );
    ASSERT_TRUE(option);

    EXPECT_TRUE(option->getFlag(Option4ClientFqdn::FLAG_S));
    EXPECT_TRUE(option->getFlag(Option4ClientFqdn::FLAG_E));
    EXPECT_FALSE(option->getFlag(Option4ClientFqdn::FLAG_O));
    EXPECT_FALSE(option->getFlag(Option4ClientFqdn::FLAG_N));
    EXPECT_EQ("myhost.example.com.", option->getDomainName());
    EXPECT_EQ(Option4ClientFqdn::FULL, option->getDomainNameType());
}

// This test verifies that the option in the on-wire format with the domain-name
// encoded in the ASCII format is parsed correctly.
TEST(Option4ClientFqdnTest, constructFromWireASCII) {
    // The E flag is set to zero which indicates that the domain name
    // is encoded in the ASCII format. The "dot" character at the end
    // indicates that the domain-name is fully qualified.
    const uint8_t in_data[] = {
        Option4ClientFqdn::FLAG_S,            // flags
        0,                                    // RCODE1
        0,                                    // RCODE2
        77, 121, 104, 111, 115, 116, 46,      // Myhost.
        69, 120, 97, 109, 112, 108, 101, 46,  // Example.
        67, 111, 109, 46                      // Com.
    };
    size_t in_data_size = sizeof(in_data) / sizeof(in_data[0]);
    OptionBuffer in_buf(in_data, in_data + in_data_size);

    // Create option instance. Check that constructor doesn't throw.
    boost::scoped_ptr<Option4ClientFqdn> option;
    ASSERT_NO_THROW(
        option.reset(new Option4ClientFqdn(in_buf.begin(), in_buf.end()))
    );
    ASSERT_TRUE(option);

    EXPECT_TRUE(option->getFlag(Option4ClientFqdn::FLAG_S));
    EXPECT_FALSE(option->getFlag(Option4ClientFqdn::FLAG_E));
    EXPECT_FALSE(option->getFlag(Option4ClientFqdn::FLAG_O));
    EXPECT_FALSE(option->getFlag(Option4ClientFqdn::FLAG_N));
    EXPECT_EQ("myhost.example.com.", option->getDomainName());
    EXPECT_EQ(Option4ClientFqdn::FULL, option->getDomainNameType());
}

// This test verifies that truncated option is rejected.
TEST(Option4ClientFqdnTest, constructFromWireTruncated) {
    // Empty buffer is invalid. It should be at least one octet long.
    OptionBuffer in_buf;
    for (uint8_t i = 0; i < 3; ++i) {
        EXPECT_THROW(Option4ClientFqdn(in_buf.begin(), in_buf.end()),
                     OutOfRange) << "Test of the truncated buffer failed for"
                                 << " buffer length " << static_cast<int>(i);
        in_buf.push_back(0);
    }

    // Buffer is now 3 bytes long, so it should not fail now.
    EXPECT_NO_THROW(Option4ClientFqdn(in_buf.begin(), in_buf.end()));
}

// This test verifies that exception is thrown when invalid domain-name
// in canonical format is carried in the option.
TEST(Option4ClientFqdnTest, constructFromWireInvalidName) {
    const uint8_t in_data[] = {
        Option4ClientFqdn::FLAG_S | Option4ClientFqdn::FLAG_E, // flags
        0,                                                     // RCODE1
        0,                                                     // RCODE2
        6, 109, 121, 104, 111, 115, 116,                       // myhost.
        7, 101, 120, 97, 109, 112, 108, 101,                   // example.
        5, 99, 111, 109, 0                 // com. (invalid label length 5)
    };
    size_t in_data_size = sizeof(in_data) / sizeof(in_data[0]);
    OptionBuffer in_buf(in_data, in_data + in_data_size);

    LenientOptionParsing lop(false);
    EXPECT_THROW(Option4ClientFqdn(in_buf.begin(), in_buf.end()),
                 InvalidOption4FqdnDomainName);
    Option::lenient_parsing_ = true;
    EXPECT_THROW(Option4ClientFqdn(in_buf.begin(), in_buf.end()),
                 SkipThisOptionError);
}

// This test verifies that exception is thrown when invalid domain-name
// in ASCII format is carried in the option.
TEST(Option4ClientFqdnTest, constructFromWireInvalidASCIIName) {
    const uint8_t in_data[] = {
        Option4ClientFqdn::FLAG_S,            // flags
        0,                                    // RCODE1
        0,                                    // RCODE2
        109, 121, 104, 111, 115, 116, 46, 46, // myhost.. (double dot!)
        101, 120, 97, 109, 112, 108, 101, 46, // example.
        99, 111, 109, 46                      // com.
    };
    size_t in_data_size = sizeof(in_data) / sizeof(in_data[0]);
    OptionBuffer in_buf(in_data, in_data + in_data_size);

    LenientOptionParsing lop(false);
    EXPECT_THROW(Option4ClientFqdn(in_buf.begin(), in_buf.end()),
                 InvalidOption4FqdnDomainName);
    Option::lenient_parsing_ = true;
    EXPECT_THROW(Option4ClientFqdn(in_buf.begin(), in_buf.end()),
                 SkipThisOptionError);
}

// This test verifies that the option in the on-wire format with partial
// domain-name encoded in canonical format is parsed correctly.
TEST(Option4ClientFqdnTest, constructFromWirePartial) {
    const uint8_t in_data[] = {
        Option4ClientFqdn::FLAG_N | Option4ClientFqdn:: FLAG_E,  // flags
        255,                                                     // RCODE1
        255,                                                     // RCODE2
        6, 77, 121, 104, 111, 115, 116                           // Myhost
    };
    size_t in_data_size = sizeof(in_data) / sizeof(in_data[0]);
    OptionBuffer in_buf(in_data, in_data + in_data_size);

    // Create option instance. Check that constructor doesn't throw.
    boost::scoped_ptr<Option4ClientFqdn> option;
    ASSERT_NO_THROW(
        option.reset(new Option4ClientFqdn(in_buf.begin(), in_buf.end()))
    );
    ASSERT_TRUE(option);

    EXPECT_FALSE(option->getFlag(Option4ClientFqdn::FLAG_S));
    EXPECT_TRUE(option->getFlag(Option4ClientFqdn::FLAG_E));
    EXPECT_FALSE(option->getFlag(Option4ClientFqdn::FLAG_O));
    EXPECT_TRUE(option->getFlag(Option4ClientFqdn::FLAG_N));
    EXPECT_EQ("myhost", option->getDomainName());
    EXPECT_EQ(Option4ClientFqdn::PARTIAL, option->getDomainNameType());
}

// This test verifies that the option in the on-wire format with partial
// domain-name encoded in ASCII format is parsed correctly.
TEST(Option4ClientFqdnTest, constructFromWirePartialASCII) {
    // There is no "dot" character at the end, so the domain-name is partial.
    const uint8_t in_data[] = {
        Option4ClientFqdn::FLAG_N,            // flags
        255,                                  // RCODE1
        255,                                  // RCODE2
        109, 121, 104, 111, 115, 116, 46,     // myhost.
        101, 120, 97, 109, 112, 108, 101      // example
    };
    size_t in_data_size = sizeof(in_data) / sizeof(in_data[0]);
    OptionBuffer in_buf(in_data, in_data + in_data_size);

    // Create option instance. Check that constructor doesn't throw.
    boost::scoped_ptr<Option4ClientFqdn> option;
    ASSERT_NO_THROW(
        option.reset(new Option4ClientFqdn(in_buf.begin(), in_buf.end()))
    );
    ASSERT_TRUE(option);

    EXPECT_FALSE(option->getFlag(Option4ClientFqdn::FLAG_S));
    EXPECT_FALSE(option->getFlag(Option4ClientFqdn::FLAG_E));
    EXPECT_FALSE(option->getFlag(Option4ClientFqdn::FLAG_O));
    EXPECT_TRUE(option->getFlag(Option4ClientFqdn::FLAG_N));
    EXPECT_EQ("myhost.example", option->getDomainName());
    EXPECT_EQ(Option4ClientFqdn::PARTIAL, option->getDomainNameType());
}

// This test verifies that the option in the on-wire format with empty
// domain-name is parsed correctly.
TEST(Option4ClientFqdnTest, constructFromWireEmpty) {
    // Initialize the 3-byte long buffer. All bytes initialized to 0:
    // Flags, RCODE1 and RCODE2.
    OptionBuffer in_buf(3, 0);
    // Create option instance. Check that constructor doesn't throw.
    boost::scoped_ptr<Option4ClientFqdn> option;
    ASSERT_NO_THROW(
        option.reset(new Option4ClientFqdn(in_buf.begin(), in_buf.end()))
    );
    ASSERT_TRUE(option);

    // domain-name field should be empty because on-wire data comprised
    // flags field only.
    EXPECT_TRUE(option->getDomainName().empty());
}

// This test verifies that assignment operator can be used to assign one
// instance of the option to another.
TEST(Option4ClientFqdnTest, assignment) {
    // Usually the smart pointer is used to declare options and call
    // constructor within assert. Thanks to this approach, the option instance
    // is in the function scope and only initialization is done within assert.
    // In this particular test we can't use smart pointers because we are
    // testing assignment operator like this:
    //
    //          option2 = option;
    //
    // The two asserts below do not create the instances that we will used to
    // test assignment. They just attempt to create instances of the options
    // with the same parameters as those that will be created for the actual
    // assignment test. If these asserts do not fail, we can create options
    // for the assignment test, do not surround them with asserts and be sure
    // they will not throw.
    ASSERT_NO_THROW(Option4ClientFqdn(Option4ClientFqdn::FLAG_S |
                                      Option4ClientFqdn::FLAG_E,
                                      Option4ClientFqdn::RCODE_SERVER(),
                                      "myhost.example.com",
                                      Option4ClientFqdn::FULL));

    ASSERT_NO_THROW(Option4ClientFqdn(Option4ClientFqdn::FLAG_N |
                                      Option4ClientFqdn::FLAG_E,
                                      Option4ClientFqdn::RCODE_SERVER(),
                                      "myhost",
                                      Option4ClientFqdn::PARTIAL));

    // Create options with the same parameters as tested above.

    // Create first option.
    Option4ClientFqdn option(Option4ClientFqdn::FLAG_S |
                             Option4ClientFqdn::FLAG_E,
                             Option4ClientFqdn::RCODE_SERVER(),
                             "myhost.Example.com",
                             Option4ClientFqdn::FULL);

    // Verify that the values have been set correctly.
    ASSERT_TRUE(option.getFlag(Option4ClientFqdn::FLAG_S));
    ASSERT_TRUE(option.getFlag(Option4ClientFqdn::FLAG_E));
    ASSERT_FALSE(option.getFlag(Option4ClientFqdn::FLAG_O));
    ASSERT_FALSE(option.getFlag(Option4ClientFqdn::FLAG_N));
    ASSERT_EQ("myhost.example.com.", option.getDomainName());
    ASSERT_EQ(Option4ClientFqdn::FULL, option.getDomainNameType());

    // Create a second option.
    Option4ClientFqdn option2(Option4ClientFqdn::FLAG_N |
                              Option4ClientFqdn::FLAG_E,
                              Option4ClientFqdn::RCODE_SERVER(),
                              "Myhost",
                              Option4ClientFqdn::PARTIAL);

    // Verify that the values have been set correctly.
    ASSERT_FALSE(option2.getFlag(Option4ClientFqdn::FLAG_S));
    ASSERT_TRUE(option2.getFlag(Option4ClientFqdn::FLAG_E));
    ASSERT_FALSE(option2.getFlag(Option4ClientFqdn::FLAG_O));
    ASSERT_TRUE(option2.getFlag(Option4ClientFqdn::FLAG_N));
    ASSERT_EQ("myhost", option2.getDomainName());
    ASSERT_EQ(Option4ClientFqdn::PARTIAL, option2.getDomainNameType());


    // Make the assignment.
    ASSERT_NO_THROW(option2 = option);

    // Both options should now have the same values.
    EXPECT_TRUE(option2.getFlag(Option4ClientFqdn::FLAG_S));
    EXPECT_TRUE(option2.getFlag(Option4ClientFqdn::FLAG_E));
    EXPECT_FALSE(option2.getFlag(Option4ClientFqdn::FLAG_O));
    EXPECT_FALSE(option2.getFlag(Option4ClientFqdn::FLAG_N));
    EXPECT_EQ(option.getDomainName(), option2.getDomainName());
    EXPECT_EQ(option.getDomainNameType(), option2.getDomainNameType());

    // Make self-assignment.
    ASSERT_NO_THROW(option2 = option2);
    EXPECT_TRUE(option2.getFlag(Option4ClientFqdn::FLAG_S));
    EXPECT_TRUE(option2.getFlag(Option4ClientFqdn::FLAG_E));
    EXPECT_FALSE(option2.getFlag(Option4ClientFqdn::FLAG_O));
    EXPECT_FALSE(option2.getFlag(Option4ClientFqdn::FLAG_N));
    EXPECT_EQ(option.getDomainName(), option2.getDomainName());
    EXPECT_EQ(option.getDomainNameType(), option2.getDomainNameType());
}

// This test verifies that constructor will throw an exception if invalid
// DHCPv6 Client FQDN Option flags are specified.
TEST(Option4ClientFqdnTest, constructInvalidFlags) {
    // First, check that constructor does not throw an exception when
    // valid flags values are provided. That way we eliminate the issue
    // that constructor always throws exception.
    uint8_t flags = 0;
    ASSERT_NO_THROW(Option4ClientFqdn(flags, Option4ClientFqdn::RCODE_CLIENT(),
                                      "myhost.example.com"));

    // Invalid flags: The maximal value is 0xF when all flag bits are set
    // (00001111b). The flag value of 0x18 sets the bit from the Must Be
    // Zero (MBZ) bitset (00011000b).
    flags = 0x18;
    EXPECT_THROW(Option4ClientFqdn(flags, Option4ClientFqdn::RCODE_CLIENT(),
                                   "myhost.example.com"),
                 InvalidOption4FqdnFlags);

    // According to RFC 4702, section 2.1. if the N bit is set the S bit MUST
    // be zero. If both are set, constructor is expected to throw.
    flags = Option4ClientFqdn::FLAG_N | Option4ClientFqdn::FLAG_S;
    EXPECT_THROW(Option4ClientFqdn(flags, Option4ClientFqdn::RCODE_CLIENT(),
                                   "myhost.example.com"),
                 InvalidOption4FqdnFlags);
}

// This test verifies that constructor which parses option from on-wire format
// will throw exception if parsed flags field is invalid.
TEST(Option4ClientFqdnTest, constructFromWireInvalidFlags) {
    // Create a buffer which holds flags field only. Set valid flag field at
    // at first to make sure that constructor doesn't always throw an exception.
    OptionBuffer in_buf(3, 0);
    in_buf[0] = Option4ClientFqdn::FLAG_S;
    ASSERT_NO_THROW(Option4ClientFqdn(in_buf.begin(), in_buf.end()));

    // Replace the flags with invalid value and verify that constructor throws
    // appropriate exception.
    in_buf[0] = Option4ClientFqdn::FLAG_N | Option4ClientFqdn::FLAG_S;
    EXPECT_THROW(Option4ClientFqdn(in_buf.begin(), in_buf.end()),
                 InvalidOption4FqdnFlags);
}

// This test verifies that if invalid domain name is used the constructor
// will throw appropriate exception.
TEST(Option4ClientFqdnTest, constructInvalidName) {
    // First, check that constructor does not throw when valid domain name
    // is specified. That way we eliminate the possibility that constructor
    // always throws exception.
    ASSERT_NO_THROW(Option4ClientFqdn(Option4ClientFqdn::FLAG_E,
                                      Option4ClientFqdn::RCODE_CLIENT(),
                                      "myhost.example.com"));

    // Specify invalid domain name and expect that exception is thrown.
    EXPECT_THROW(Option4ClientFqdn(Option4ClientFqdn::FLAG_E,
                                   Option4ClientFqdn::RCODE_CLIENT(),
                                   "my...host.example.com"),
                 InvalidOption4FqdnDomainName);

    // Do the same test for the domain-name in ASCII format.
    ASSERT_NO_THROW(Option4ClientFqdn(0, Option4ClientFqdn::RCODE_CLIENT(),
                                      "myhost.example.com"));

    EXPECT_THROW(Option4ClientFqdn(0, Option4ClientFqdn::RCODE_CLIENT(),
                                   "my...host.example.com"),
                 InvalidOption4FqdnDomainName);
}

// This test verifies that getFlag throws an exception if flag value other
// than N, E, O, S was specified.
TEST(Option4ClientFqdnTest, getFlag) {
    boost::scoped_ptr<Option4ClientFqdn> option;
    ASSERT_NO_THROW(
        option.reset(new Option4ClientFqdn(Option4ClientFqdn::FLAG_E,
                                           Option4ClientFqdn::RCODE_CLIENT(),
                                           "myhost.example.com"))
    );
    ASSERT_TRUE(option);

    // The value of 0x3 (binary 0011) is invalid because it specifies two bits
    // in the flags field which value is to be checked.
    EXPECT_THROW(option->getFlag(0x3), InvalidOption4FqdnFlags);
}

// This test verifies that flags can be modified and that incorrect flags
// are rejected.
TEST(Option4ClientFqdnTest, setFlag) {
    // Create option instance. Check that constructor doesn't throw.
    boost::scoped_ptr<Option4ClientFqdn> option;
    ASSERT_NO_THROW(
        option.reset(new Option4ClientFqdn(0,
                                           Option4ClientFqdn::RCODE_CLIENT(),
                                           "myhost.example.com"))
    );
    ASSERT_TRUE(option);

    // All flags should be set to 0 initially.
    ASSERT_FALSE(option->getFlag(Option4ClientFqdn::FLAG_N));
    ASSERT_FALSE(option->getFlag(Option4ClientFqdn::FLAG_S));
    ASSERT_FALSE(option->getFlag(Option4ClientFqdn::FLAG_O));
    ASSERT_FALSE(option->getFlag(Option4ClientFqdn::FLAG_E));

    // Set E = 1
    ASSERT_NO_THROW(option->setFlag(Option4ClientFqdn::FLAG_E, true));
    ASSERT_TRUE(option->getFlag(Option4ClientFqdn::FLAG_E));

    // Set N = 1
    ASSERT_NO_THROW(option->setFlag(Option4ClientFqdn::FLAG_N, true));
    ASSERT_TRUE(option->getFlag(Option4ClientFqdn::FLAG_N));

    // Set O = 1
    ASSERT_NO_THROW(option->setFlag(Option4ClientFqdn::FLAG_O, true));
    ASSERT_TRUE(option->getFlag(Option4ClientFqdn::FLAG_O));

    // Set S = 1, this should throw exception because S and N must not
    // be set in the same time.
    ASSERT_THROW(option->setFlag(Option4ClientFqdn::FLAG_S, true),
                 InvalidOption4FqdnFlags);

    // Set E = 0
    ASSERT_NO_THROW(option->setFlag(Option4ClientFqdn::FLAG_N, false));
    ASSERT_FALSE(option->getFlag(Option4ClientFqdn::FLAG_N));

    // Set N = 0
    ASSERT_NO_THROW(option->setFlag(Option4ClientFqdn::FLAG_N, false));
    ASSERT_FALSE(option->getFlag(Option4ClientFqdn::FLAG_N));

    // Set S = 1, this should not result in exception because N has been
    // cleared.
    ASSERT_NO_THROW(option->setFlag(Option4ClientFqdn::FLAG_S, true));
    ASSERT_TRUE(option->getFlag(Option4ClientFqdn::FLAG_S));

    // Set N = 1, this should result in exception because S = 1
    ASSERT_THROW(option->setFlag(Option4ClientFqdn::FLAG_N, true),
                 InvalidOption4FqdnFlags);

    // Set O = 0
    ASSERT_NO_THROW(option->setFlag(Option4ClientFqdn::FLAG_O, false));
    ASSERT_FALSE(option->getFlag(Option4ClientFqdn::FLAG_O));

    // Try out of bounds settings.
    uint8_t flags = 0;
    ASSERT_THROW(option->setFlag(flags, true), InvalidOption4FqdnFlags);

    flags = 0x18;
    ASSERT_THROW(option->setFlag(flags, true), InvalidOption4FqdnFlags);
}

// This test verifies that flags field of the option is set to 0 when resetFlags
// function is called.
TEST(Option4ClientFqdnTest, resetFlags) {
    boost::scoped_ptr<Option4ClientFqdn> option;
    ASSERT_NO_THROW(
        option.reset(new Option4ClientFqdn(Option4ClientFqdn::FLAG_S |
                                           Option4ClientFqdn::FLAG_O |
                                           Option4ClientFqdn::FLAG_E,
                                           Option4ClientFqdn::RCODE_CLIENT(),
                                           "myhost.example.com",
                                           Option4ClientFqdn::FULL))
    );
    ASSERT_TRUE(option);

    // Check that flags we set in the constructor are set.
    ASSERT_TRUE(option->getFlag(Option4ClientFqdn::FLAG_S));
    ASSERT_TRUE(option->getFlag(Option4ClientFqdn::FLAG_O));
    ASSERT_FALSE(option->getFlag(Option4ClientFqdn::FLAG_N));
    ASSERT_TRUE(option->getFlag(Option4ClientFqdn::FLAG_E));

    option->resetFlags();

    // After reset, all flags should be 0.
    EXPECT_FALSE(option->getFlag(Option4ClientFqdn::FLAG_S));
    EXPECT_FALSE(option->getFlag(Option4ClientFqdn::FLAG_O));
    EXPECT_FALSE(option->getFlag(Option4ClientFqdn::FLAG_N));
    EXPECT_FALSE(option->getFlag(Option4ClientFqdn::FLAG_E));
}

// This test verifies that current domain-name can be replaced with a new
// domain-name.
TEST(Option4ClientFqdnTest, setDomainName) {
    boost::scoped_ptr<Option4ClientFqdn> option;
    ASSERT_NO_THROW(
        option.reset(new Option4ClientFqdn(Option4ClientFqdn::FLAG_S |
                                           Option4ClientFqdn::FLAG_E,
                                           Option4ClientFqdn::RCODE_SERVER(),
                                           "myhost.Example.com",
                                           Option4ClientFqdn::FULL))
    );
    ASSERT_TRUE(option);
    ASSERT_EQ("myhost.example.com.", option->getDomainName());
    ASSERT_EQ(Option4ClientFqdn::FULL, option->getDomainNameType());

    // Partial domain-name.
    ASSERT_NO_THROW(option->setDomainName("myHost",
                                          Option4ClientFqdn::PARTIAL));
    EXPECT_EQ("myhost", option->getDomainName());
    EXPECT_EQ(Option4ClientFqdn::PARTIAL, option->getDomainNameType());

    // Fully qualified domain-name.
    ASSERT_NO_THROW(option->setDomainName("example.Com",
                                          Option4ClientFqdn::FULL));
    EXPECT_EQ("example.com.", option->getDomainName());
    EXPECT_EQ(Option4ClientFqdn::FULL, option->getDomainNameType());

    // Empty domain name (partial). This should be successful.
    ASSERT_NO_THROW(option->setDomainName("", Option4ClientFqdn::PARTIAL));
    EXPECT_TRUE(option->getDomainName().empty());
    EXPECT_EQ(Option4ClientFqdn::PARTIAL, option->getDomainNameType());

    // Fully qualified domain-names must not be empty.
    EXPECT_THROW(option->setDomainName("", Option4ClientFqdn::FULL),
                 InvalidOption4FqdnDomainName);
    EXPECT_THROW(option->setDomainName(" ", Option4ClientFqdn::FULL),
                 InvalidOption4FqdnDomainName);
}

// This test verifies that current domain-name can be reset to empty one.
TEST(Option4ClientFqdnTest, resetDomainName) {
    boost::scoped_ptr<Option4ClientFqdn> option;
    ASSERT_NO_THROW(
        option.reset(new Option4ClientFqdn(Option4ClientFqdn::FLAG_S |
                                           Option4ClientFqdn::FLAG_E,
                                           Option4ClientFqdn::RCODE_CLIENT(),
                                           "Myhost.Example.com",
                                           Option4ClientFqdn::FULL))
    );
    ASSERT_TRUE(option);
    ASSERT_EQ("myhost.example.com.", option->getDomainName());
    ASSERT_EQ(Option4ClientFqdn::FULL, option->getDomainNameType());

    // Set the domain-name to empty one.
    ASSERT_NO_THROW(option->resetDomainName());
    EXPECT_TRUE(option->getDomainName().empty());
}

// This test verifies on-wire format of the option is correctly created.
TEST(Option4ClientFqdnTest, pack) {
    // Create option instance. Check that constructor doesn't throw.
    const uint8_t flags = Option4ClientFqdn::FLAG_S | Option4ClientFqdn::FLAG_E;
    boost::scoped_ptr<Option4ClientFqdn> option;
    ASSERT_NO_THROW(
        option.reset(new Option4ClientFqdn(flags,
                                           Option4ClientFqdn::RCODE_CLIENT(),
                                           "Myhost.Example.Com"))
    );
    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[] = {
        81, 23,                                                 // header
        Option4ClientFqdn::FLAG_S | Option4ClientFqdn::FLAG_E,  // flags
        0,                                                      // RCODE1
        0,                                                      // RCODE2
        6, 109, 121, 104, 111, 115, 116,                        // myhost.
        7, 101, 120, 97, 109, 112, 108, 101,                    // example.
        3, 99, 111, 109, 0                                      // 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 on-wire format of the Client FQDN option
// output in deprecated ASCII format.
TEST(Option4ClientFqdnTest, packASCII) {
    // Create option instance. Check that constructor doesn't throw.
    const uint8_t flags = Option4ClientFqdn::FLAG_S;
    boost::scoped_ptr<Option4ClientFqdn> option;
    ASSERT_NO_THROW(
        option.reset(new Option4ClientFqdn(flags,
                                           Option4ClientFqdn::RCODE_CLIENT(),
                                           "Myhost.Example.Com"))
    );
    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[] = {
        81, 22,                               // header
        Option4ClientFqdn::FLAG_S,            // flags
        0,                                    // RCODE1
        0,                                    // RCODE2
        109, 121, 104, 111, 115, 116, 46,     // myhost.
        101, 120, 97, 109, 112, 108, 101, 46, // example.
        99, 111, 109, 46                      // 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 on-wire format of the option with partial domain name
// is correctly created.
TEST(Option4ClientFqdnTest, packPartial) {
    // Create option instance. Check that constructor doesn't throw.
    const uint8_t flags = Option4ClientFqdn::FLAG_S | Option4ClientFqdn::FLAG_E;
    boost::scoped_ptr<Option4ClientFqdn> option;
    ASSERT_NO_THROW(
        option.reset(new Option4ClientFqdn(flags,
                                           Option4ClientFqdn::RCODE_CLIENT(),
                                           "myhost",
                                           Option4ClientFqdn::PARTIAL))
    );
    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[] = {
        81, 10,                                                // header
        Option4ClientFqdn::FLAG_S | Option4ClientFqdn::FLAG_E, // flags
        0,                                                     // RCODE1
        0,                                                     // RCODE2
        6, 109, 121, 104, 111, 115, 116                        // myhost
    };
    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 that it is possible to encode option with empty
// domain-name in the on-wire format.
TEST(Option4ClientFqdnTest, packEmpty) {
    // Create option instance. Check that constructor doesn't throw.
    const uint8_t flags = Option4ClientFqdn::FLAG_S | Option4ClientFqdn::FLAG_E;
    boost::scoped_ptr<Option4ClientFqdn> option;
    ASSERT_NO_THROW(
        option.reset(new Option4ClientFqdn(flags,
                                           Option4ClientFqdn::RCODE_CLIENT()))
    );
    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[] = {
        81, 3,                                                 // header
        Option4ClientFqdn::FLAG_S | Option4ClientFqdn::FLAG_E, // flags
        0,                                                     // RCODE1
        0                                                      // RCODE2
    };
    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 that on-wire option data holding fully qualified domain
// name is parsed correctly.
TEST(Option4ClientFqdnTest, unpack) {
    // Create option instance. Check that constructor doesn't throw.
    boost::scoped_ptr<Option4ClientFqdn> option;
    ASSERT_NO_THROW(
        option.reset(new Option4ClientFqdn(Option4ClientFqdn::FLAG_O |
                                           Option4ClientFqdn::FLAG_E,
                                           Option4ClientFqdn::RCODE_CLIENT(),
                                           "myhost",
                                           Option4ClientFqdn::PARTIAL))
    );
    ASSERT_TRUE(option);
    // Make sure that the parameters have been set correctly. Later in this
    // test we will check that they will be replaced with new values when
    // unpack is called.
    EXPECT_FALSE(option->getFlag(Option4ClientFqdn::FLAG_S));
    EXPECT_FALSE(option->getFlag(Option4ClientFqdn::FLAG_N));
    EXPECT_TRUE(option->getFlag(Option4ClientFqdn::FLAG_O));
    EXPECT_TRUE(option->getFlag(Option4ClientFqdn::FLAG_E));
    EXPECT_EQ("myhost", option->getDomainName());
    EXPECT_EQ(Option4ClientFqdn::PARTIAL, option->getDomainNameType());

    const uint8_t in_data[] = {
        Option4ClientFqdn::FLAG_S | Option4ClientFqdn::FLAG_E, // flags
        0,                                                     // RCODE1
        0,                                                     // RCODE2
        6, 109, 121, 104, 111, 115, 116,                       // myhost.
        7, 101, 120, 97, 109, 112, 108, 101,                   // example.
        3, 99, 111, 109, 0                                     // com.
    };
    size_t in_data_size = sizeof(in_data) / sizeof(in_data[0]);
    OptionBuffer in_buf(in_data, in_data + in_data_size);

    // Initialize new values from the on-wire format.
    ASSERT_NO_THROW(option->unpack(in_buf.begin(), in_buf.end()));

    // Check that new values are correct.
    EXPECT_TRUE(option->getFlag(Option4ClientFqdn::FLAG_S));
    EXPECT_FALSE(option->getFlag(Option4ClientFqdn::FLAG_N));
    EXPECT_FALSE(option->getFlag(Option4ClientFqdn::FLAG_O));
    EXPECT_TRUE(option->getFlag(Option4ClientFqdn::FLAG_E));
    EXPECT_EQ("myhost.example.com.", option->getDomainName());
    EXPECT_EQ(Option4ClientFqdn::FULL, option->getDomainNameType());
}

// This test verifies that on-wire option data holding partial domain name
// is parsed correctly.
TEST(Option4ClientFqdnTest, unpackPartial) {
    // Create option instance. Check that constructor doesn't throw.
    boost::scoped_ptr<Option4ClientFqdn> option;
    ASSERT_NO_THROW(
        option.reset(new Option4ClientFqdn(Option4ClientFqdn::FLAG_O |
                                           Option4ClientFqdn::FLAG_E,
                                           Option4ClientFqdn::RCODE_CLIENT(),
                                           "myhost.example.com"))
    );
    ASSERT_TRUE(option);
    // Make sure that the parameters have been set correctly. Later in this
    // test we will check that they will be replaced with new values when
    // unpack is called.
    EXPECT_FALSE(option->getFlag(Option4ClientFqdn::FLAG_S));
    EXPECT_FALSE(option->getFlag(Option4ClientFqdn::FLAG_N));
    EXPECT_TRUE(option->getFlag(Option4ClientFqdn::FLAG_O));
    EXPECT_TRUE(option->getFlag(Option4ClientFqdn::FLAG_E));
    EXPECT_EQ("myhost.example.com.", option->getDomainName());
    EXPECT_EQ(Option4ClientFqdn::FULL, option->getDomainNameType());

    const uint8_t in_data[] = {
        Option4ClientFqdn::FLAG_S | Option4ClientFqdn::FLAG_E, // flags
        0,                                                     // RCODE1
        0,                                                     // RCODE2
        6, 109, 121, 104, 111, 115, 116                        // myhost
    };
    size_t in_data_size = sizeof(in_data) / sizeof(in_data[0]);
    OptionBuffer in_buf(in_data, in_data + in_data_size);

    // Initialize new values from the on-wire format.
    ASSERT_NO_THROW(option->unpack(in_buf.begin(), in_buf.end()));

    // Check that new values are correct.
    EXPECT_TRUE(option->getFlag(Option4ClientFqdn::FLAG_S));
    EXPECT_TRUE(option->getFlag(Option4ClientFqdn::FLAG_E));
    EXPECT_FALSE(option->getFlag(Option4ClientFqdn::FLAG_N));
    EXPECT_FALSE(option->getFlag(Option4ClientFqdn::FLAG_O));
    EXPECT_EQ("myhost", option->getDomainName());
    EXPECT_EQ(Option4ClientFqdn::PARTIAL, option->getDomainNameType());
}

// This test verifies that the empty buffer is rejected when decoding an option
// from on-wire format.
TEST(Option4ClientFqdnTest, unpackTruncated) {
    boost::scoped_ptr<Option4ClientFqdn> option;
    ASSERT_NO_THROW(
        option.reset(new Option4ClientFqdn(Option4ClientFqdn::FLAG_O |
                                           Option4ClientFqdn::FLAG_E,
                                           Option4ClientFqdn::RCODE_CLIENT()))
    );
    ASSERT_TRUE(option);

    // Empty buffer is invalid. It should be at least 1 octet long.
    OptionBuffer in_buf;
    EXPECT_THROW(option->unpack(in_buf.begin(), in_buf.end()), OutOfRange);
}

// This test verifies that string representation of the option returned by
// toText method is correctly formatted.
TEST(Option4ClientFqdnTest, toText) {
    // Create option instance. Check that constructor doesn't throw.
    uint8_t flags = Option4ClientFqdn::FLAG_N | Option4ClientFqdn::FLAG_O |
        Option4ClientFqdn::FLAG_E;
    boost::scoped_ptr<Option4ClientFqdn> option;
    ASSERT_NO_THROW(
        option.reset(new Option4ClientFqdn(flags,
                                           Option4ClientFqdn::RCODE_CLIENT(),
                                           "myhost.example.com"))
    );
    ASSERT_TRUE(option);

    // The base indentation of the option will be set to 2. It should appear
    // as follows.
    std::string ref_string =
        "  type=81 (CLIENT_FQDN), flags: (N=1, E=1, O=1, S=0), "
        "domain-name='myhost.example.com.' (full)";
    const int indent = 2;
    EXPECT_EQ(ref_string, option->toText(indent));

    // Create another option with different parameters:
    // - flags set to 0
    // - domain-name is now partial, not fully qualified
    // Also, remove base indentation.
    flags = 0;
    ASSERT_NO_THROW(
        option.reset(new Option4ClientFqdn(flags,
                                           Option4ClientFqdn::RCODE_CLIENT(),
                                           "myhost",
                                           Option4ClientFqdn::PARTIAL))
    );
    ref_string =
        "type=81 (CLIENT_FQDN), flags: (N=0, E=0, O=0, S=0), "
        "domain-name='myhost' (partial)";
    EXPECT_EQ(ref_string, option->toText());
}

// This test verifies that the correct length of the option in on-wire
// format is returned.
TEST(Option4ClientFqdnTest, len) {
    // Create option instance. Check that constructor doesn't throw.
    boost::scoped_ptr<Option4ClientFqdn> option;
    ASSERT_NO_THROW(
        option.reset(new Option4ClientFqdn(Option4ClientFqdn::FLAG_E,
                                           Option4ClientFqdn::RCODE_CLIENT(),
                                           "myhost.example.com"))
    );
    ASSERT_TRUE(option);
    // This option comprises a header (2 octets), flag field (1 octet),
    // RCODE1 and RCODE2 (2 octets) and wire representation of the
    // domain name (length equal to the length of the string representation
    // of the domain name + 1).
    EXPECT_EQ(25, option->len());

    // Let's check that the size will change when domain name of a different
    // size is used.
    ASSERT_NO_THROW(
        option.reset(new Option4ClientFqdn(Option4ClientFqdn::FLAG_E,
                                           Option4ClientFqdn::RCODE_CLIENT(),
                                           "example.com"))
    );
    ASSERT_TRUE(option);
    EXPECT_EQ(18, option->len());

    ASSERT_NO_THROW(
        option.reset(new Option4ClientFqdn(Option4ClientFqdn::FLAG_E,
                                           Option4ClientFqdn::RCODE_CLIENT(),
                                           "myhost",
                                           Option4ClientFqdn::PARTIAL))
    );
    ASSERT_TRUE(option);
    EXPECT_EQ(12, option->len());

    // Another test for partial domain name but this time the domain name
    // contains two labels.
    ASSERT_NO_THROW(
        option.reset(new Option4ClientFqdn(Option4ClientFqdn::FLAG_E,
                                           Option4ClientFqdn::RCODE_CLIENT(),
                                           "myhost.example",
                                           Option4ClientFqdn::PARTIAL))
    );
    ASSERT_TRUE(option);
    EXPECT_EQ(20, option->len());

}

// This test verifies that the correct length of the option in on-wire
// format is returned when ASCII encoding for FQDN is in use.
TEST(Option4ClientFqdnTest, lenAscii) {
    // Create option instance. Check that constructor doesn't throw.
    boost::scoped_ptr<Option4ClientFqdn> option;
    ASSERT_NO_THROW(
        option.reset(new Option4ClientFqdn(0, Option4ClientFqdn::RCODE_CLIENT(),
                                           "myhost.example.com"))
    );
    ASSERT_TRUE(option);

    // This option comprises a header (2 octets), flag field (1 octet),
    // RCODE1 and RCODE2 (2 octets) and the domain name in the ASCII format.
    // The length of the domain name in the ASCII format is 19 - length
    // of the string plus terminating dot.
    EXPECT_EQ(24, option->len());

    // Let's change the domain name and see if the length is different.
    ASSERT_NO_THROW(
        option.reset(new Option4ClientFqdn(0, Option4ClientFqdn::RCODE_CLIENT(),
                                           "example.com"))
    );
    ASSERT_TRUE(option);

    EXPECT_EQ(17, option->len());

    // Let's test the length of the option when the partial domain name is
    // specified.
    ASSERT_NO_THROW(
        option.reset(new Option4ClientFqdn(0, Option4ClientFqdn::RCODE_CLIENT(),
                                           "myhost",
                                           Option4ClientFqdn::PARTIAL))
    );
    ASSERT_TRUE(option);

    // For partial names, there is no terminating dot, so the length of the
    // domain name is equal to the length of the "myhost".
    EXPECT_EQ(11, option->len());

    // Another check for partial domain name but this time the domain name
    // contains two labels.
    ASSERT_NO_THROW(
        option.reset(new Option4ClientFqdn(0, Option4ClientFqdn::RCODE_CLIENT(),
                                           "myhost.example",
                                           Option4ClientFqdn::PARTIAL))
    );
    ASSERT_TRUE(option);

    EXPECT_EQ(19, option->len());


    // A special case is an empty domain name for which the returned length
    // should be a sum of the header length, RCODE1, RCODE2 and flag fields
    // length.
    ASSERT_NO_THROW(
        option.reset(new Option4ClientFqdn(0, Option4ClientFqdn::RCODE_CLIENT(),
                                           "", Option4ClientFqdn::PARTIAL))
    );
    ASSERT_TRUE(option);

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

} // anonymous namespace