summaryrefslogtreecommitdiffstats
path: root/src/lib/dhcpsrv/tests/d2_client_unittest.cc
blob: c15afc59fda26f7d0fb844069afbccaacb58b838 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
// Copyright (C) 2012-2021 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 <dhcp/option6_client_fqdn.h>
#include <dhcpsrv/d2_client_mgr.h>
#include <testutils/test_to_element.h>
#include <exceptions/exceptions.h>
#include <util/strutil.h>

#include <boost/algorithm/string.hpp>
#include <gtest/gtest.h>

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

namespace {

/// @brief Tests conversion of NameChangeFormat between enum and strings.
TEST(ReplaceClientNameModeTest, formatEnumConversion){
    ASSERT_EQ(D2ClientConfig::stringToReplaceClientNameMode("never"),
              D2ClientConfig::RCM_NEVER);
    ASSERT_EQ(D2ClientConfig::stringToReplaceClientNameMode("always"),
              D2ClientConfig::RCM_ALWAYS);
    ASSERT_EQ(D2ClientConfig::stringToReplaceClientNameMode("when-present"),
              D2ClientConfig::RCM_WHEN_PRESENT);
    ASSERT_EQ(D2ClientConfig::stringToReplaceClientNameMode("when-not-present"),
              D2ClientConfig::RCM_WHEN_NOT_PRESENT);
    ASSERT_THROW(D2ClientConfig::stringToReplaceClientNameMode("BOGUS"),
                 isc::BadValue);

    ASSERT_EQ(D2ClientConfig::
              replaceClientNameModeToString(D2ClientConfig::RCM_NEVER),
              "never");
    ASSERT_EQ(D2ClientConfig::
              replaceClientNameModeToString(D2ClientConfig::RCM_ALWAYS),
              "always");
    ASSERT_EQ(D2ClientConfig::
              replaceClientNameModeToString(D2ClientConfig::RCM_WHEN_PRESENT),
              "when-present");
    ASSERT_EQ(D2ClientConfig::
              replaceClientNameModeToString(D2ClientConfig::
                                            RCM_WHEN_NOT_PRESENT),
              "when-not-present");
}

/// @brief Checks constructors and accessors of D2ClientConfig.
TEST(D2ClientConfigTest, constructorsAndAccessors) {
    D2ClientConfigPtr d2_client_config;

    // Verify default constructor creates a disabled instance.
    ASSERT_NO_THROW(d2_client_config.reset(new D2ClientConfig()));
    EXPECT_FALSE(d2_client_config->getEnableUpdates());

    // Verify the enable-updates can be toggled.
    d2_client_config->enableUpdates(true);
    EXPECT_TRUE(d2_client_config->getEnableUpdates());
    d2_client_config->enableUpdates(false);
    EXPECT_FALSE(d2_client_config->getEnableUpdates());

    d2_client_config.reset();

    bool enable_updates = true;
    isc::asiolink::IOAddress server_ip("127.0.0.1");
    size_t server_port = 477;
    isc::asiolink::IOAddress sender_ip("127.0.0.1");
    size_t sender_port = 478;
    size_t max_queue_size = 2048;
    dhcp_ddns::NameChangeProtocol ncr_protocol = dhcp_ddns::NCR_UDP;
    dhcp_ddns::NameChangeFormat ncr_format = dhcp_ddns::FMT_JSON;
    std::string generated_prefix = "the_prefix";
    std::string qualifying_suffix = "the.suffix.";
    std::string hostname_char_set = "[^A-Z]";
    std::string hostname_char_replacement = "*";

    // Verify that we can construct a valid, enabled instance.
    ASSERT_NO_THROW(d2_client_config.reset(new
                                           D2ClientConfig(enable_updates,
                                                          server_ip,
                                                          server_port,
                                                          sender_ip,
                                                          sender_port,
                                                          max_queue_size,
                                                          ncr_protocol,
                                                          ncr_format)));
    ASSERT_TRUE(d2_client_config);

    // Add user context
    std::string user_context = "{ \"comment\": \"bar\", \"foo\": 1 }";
    EXPECT_FALSE(d2_client_config->getContext());
    d2_client_config->setContext(Element::fromJSON(user_context));

    // Verify that the accessors return the expected values.
    EXPECT_EQ(d2_client_config->getEnableUpdates(), enable_updates);

    EXPECT_EQ(d2_client_config->getServerIp(), server_ip);
    EXPECT_EQ(d2_client_config->getServerPort(), server_port);
    EXPECT_EQ(d2_client_config->getSenderIp(), sender_ip);
    EXPECT_EQ(d2_client_config->getSenderPort(), sender_port);
    EXPECT_EQ(d2_client_config->getMaxQueueSize(), max_queue_size);
    EXPECT_EQ(d2_client_config->getNcrProtocol(), ncr_protocol);
    EXPECT_EQ(d2_client_config->getNcrFormat(), ncr_format);
    ASSERT_TRUE(d2_client_config->getContext());
    EXPECT_EQ(d2_client_config->getContext()->str(), user_context);

    // Verify that toText called by << operator doesn't bomb.
    ASSERT_NO_THROW(std::cout << "toText test:" << std::endl <<
                    *d2_client_config << std::endl);

    // Verify what toElement returns.
    std::string expected = "{\n"
        "\"enable-updates\": true,\n"
        "\"server-ip\": \"127.0.0.1\",\n"
        "\"server-port\": 477,\n"
        "\"sender-ip\": \"127.0.0.1\",\n"
        "\"sender-port\": 478,\n"
        "\"max-queue-size\": 2048,\n"
        "\"ncr-protocol\": \"UDP\",\n"
        "\"ncr-format\": \"JSON\",\n"
        "\"user-context\": { \"foo\": 1, \"comment\": \"bar\" }\n"
        "}\n";
    runToElementTest<D2ClientConfig>(expected, *d2_client_config);

    // Verify that constructor does not allow use of NCR_TCP.
    /// @todo obviously this becomes invalid once TCP is supported.
    ASSERT_THROW(d2_client_config.reset(new
                                        D2ClientConfig(enable_updates,
                                                       server_ip,
                                                       server_port,
                                                       sender_ip,
                                                       sender_port,
                                                       max_queue_size,
                                                       dhcp_ddns::NCR_TCP,
                                                       ncr_format)),
                 D2ClientError);

    Optional<std::string> opt_hostname_char_set("", true);
    Optional<std::string> opt_hostname_char_replacement("", true);

    // Verify that constructor handles optional hostname char stuff.
    ASSERT_NO_THROW(d2_client_config.reset(new
                                           D2ClientConfig(enable_updates,
                                                          server_ip,
                                                          server_port,
                                                          sender_ip,
                                                          sender_port,
                                                          max_queue_size,
                                                          ncr_protocol,
                                                          ncr_format)));
    ASSERT_TRUE(d2_client_config);

    // Verify what toElement returns.
    expected = "{\n"
        "\"enable-updates\": true,\n"
        "\"server-ip\": \"127.0.0.1\",\n"
        "\"server-port\": 477,\n"
        "\"sender-ip\": \"127.0.0.1\",\n"
        "\"sender-port\": 478,\n"
        "\"max-queue-size\": 2048,\n"
        "\"ncr-protocol\": \"UDP\",\n"
        "\"ncr-format\": \"JSON\"\n"
        "}\n";
    runToElementTest<D2ClientConfig>(expected, *d2_client_config);

    /// @todo if additional validation is added to ctor, this test needs to
    /// expand accordingly.
}

/// @brief Tests the equality and inequality operators of D2ClientConfig.
TEST(D2ClientConfigTest, equalityOperator) {
    D2ClientConfigPtr ref_config;
    D2ClientConfigPtr test_config;

    isc::asiolink::IOAddress ref_address("127.0.0.1");
    isc::asiolink::IOAddress test_address("127.0.0.2");

    // Create an instance to use as a reference.
    ASSERT_NO_THROW(ref_config.reset(new D2ClientConfig(true,
                    ref_address, 477, ref_address, 478, 1024,
                    dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON)));
    ASSERT_TRUE(ref_config);

    // Check a configuration that is identical to reference configuration.
    ASSERT_NO_THROW(test_config.reset(new D2ClientConfig(true,
                    ref_address, 477, ref_address, 478, 1024,
                    dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON)));
    ASSERT_TRUE(test_config);
    EXPECT_TRUE(*ref_config == *test_config);
    EXPECT_FALSE(*ref_config != *test_config);

    // Check a configuration that differs only by enable flag.
    ASSERT_NO_THROW(test_config.reset(new D2ClientConfig(false,
                    ref_address, 477, ref_address, 478, 1024,
                    dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON)));
    ASSERT_TRUE(test_config);
    EXPECT_FALSE(*ref_config == *test_config);
    EXPECT_TRUE(*ref_config != *test_config);

    // Check a configuration that differs only by server ip.
    ASSERT_NO_THROW(test_config.reset(new D2ClientConfig(true,
                    test_address, 477, ref_address, 478, 1024,
                    dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON)));
    ASSERT_TRUE(test_config);
    EXPECT_FALSE(*ref_config == *test_config);
    EXPECT_TRUE(*ref_config != *test_config);

    // Check a configuration that differs only by server port.
    ASSERT_NO_THROW(test_config.reset(new D2ClientConfig(true,
                    ref_address, 333, ref_address, 478, 1024,
                    dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON)));
    ASSERT_TRUE(test_config);
    EXPECT_FALSE(*ref_config == *test_config);
    EXPECT_TRUE(*ref_config != *test_config);

    // Check a configuration that differs only by sender ip.
    ASSERT_NO_THROW(test_config.reset(new D2ClientConfig(true,
                    ref_address, 477, test_address, 478, 1024,
                    dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON)));
    ASSERT_TRUE(test_config);
    EXPECT_FALSE(*ref_config == *test_config);
    EXPECT_TRUE(*ref_config != *test_config);

    // Check a configuration that differs only by sender port.
    ASSERT_NO_THROW(test_config.reset(new D2ClientConfig(true,
                    ref_address, 477, ref_address, 333, 1024,
                    dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON)));
    ASSERT_TRUE(test_config);
    EXPECT_FALSE(*ref_config == *test_config);
    EXPECT_TRUE(*ref_config != *test_config);

    // Check a configuration that differs only by max queue size.
    ASSERT_NO_THROW(test_config.reset(new D2ClientConfig(true,
                    ref_address, 477, ref_address, 478, 2048,
                    dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON)));
    ASSERT_TRUE(test_config);
    EXPECT_FALSE(*ref_config == *test_config);
    EXPECT_TRUE(*ref_config != *test_config);
}

/// @brief Checks the D2ClientMgr constructor.
TEST(D2ClientMgr, constructor) {
    D2ClientMgrPtr d2_client_mgr;

    // Verify we can construct with the default constructor.
    ASSERT_NO_THROW(d2_client_mgr.reset(new D2ClientMgr()));

    // After construction, D2 configuration should be disabled.
    // Fetch it and verify this is the case.
    D2ClientConfigPtr original_config = d2_client_mgr->getD2ClientConfig();
    ASSERT_TRUE(original_config);
    EXPECT_FALSE(original_config->getEnableUpdates());

    // Make sure convenience method agrees.
    EXPECT_FALSE(d2_client_mgr->ddnsEnabled());
}

/// @brief Checks passing the D2ClientMgr a valid D2 client configuration.
/// @todo Once NameChangeSender is integrated, this test needs to expand, and
/// additional scenario tests will need to be written.
TEST(D2ClientMgr, validConfig) {
    D2ClientMgrPtr d2_client_mgr;

    // Construct the manager and fetch its initial configuration.
    ASSERT_NO_THROW(d2_client_mgr.reset(new D2ClientMgr()));
    D2ClientConfigPtr original_config = d2_client_mgr->getD2ClientConfig();
    ASSERT_TRUE(original_config);

    // Verify that we cannot set the config to an empty pointer.
    D2ClientConfigPtr new_cfg;
    ASSERT_THROW(d2_client_mgr->setD2ClientConfig(new_cfg), D2ClientError);

    // Create a new, enabled config.
    ASSERT_NO_THROW(new_cfg.reset(new D2ClientConfig(true,
                                  isc::asiolink::IOAddress("127.0.0.1"), 477,
                                  isc::asiolink::IOAddress("127.0.0.1"), 478,
                                  1024,
                                  dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON)));

    // Verify that we can assign a new, non-empty configuration.
    ASSERT_NO_THROW(d2_client_mgr->setD2ClientConfig(new_cfg));

    // Verify that we can fetch the newly assigned configuration.
    D2ClientConfigPtr updated_config = d2_client_mgr->getD2ClientConfig();
    ASSERT_TRUE(updated_config);
    EXPECT_TRUE(updated_config->getEnableUpdates());

    // Make sure convenience method agrees with the updated configuration.
    EXPECT_TRUE(d2_client_mgr->ddnsEnabled());

    // Make sure the configuration we fetched is the one  we assigned,
    // and not the original configuration.
    EXPECT_EQ(*new_cfg, *updated_config);
    EXPECT_NE(*original_config, *updated_config);
}

/// @brief Checks passing the D2ClientMgr a valid D2 client configuration
/// using IPv6 service.
TEST(D2ClientMgr, ipv6Config) {
    D2ClientMgrPtr d2_client_mgr;

    // Construct the manager and fetch its initial configuration.
    ASSERT_NO_THROW(d2_client_mgr.reset(new D2ClientMgr()));
    D2ClientConfigPtr original_config = d2_client_mgr->getD2ClientConfig();
    ASSERT_TRUE(original_config);

    // Create a new, enabled config.
    D2ClientConfigPtr new_cfg;
    ASSERT_NO_THROW(new_cfg.reset(new D2ClientConfig(true,
                                  isc::asiolink::IOAddress("::1"), 477,
                                  isc::asiolink::IOAddress("::1"), 478,
                                  1024,
                                  dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON)));

    // Verify that we can assign a new, non-empty configuration.
    ASSERT_NO_THROW(d2_client_mgr->setD2ClientConfig(new_cfg));

    // Verify that we can fetch the newly assigned configuration.
    D2ClientConfigPtr updated_config = d2_client_mgr->getD2ClientConfig();
    ASSERT_TRUE(updated_config);
    EXPECT_TRUE(updated_config->getEnableUpdates());

    // Make sure convenience method agrees with the updated configuration.
    EXPECT_TRUE(d2_client_mgr->ddnsEnabled());

    // Make sure the configuration we fetched is the one we assigned,
    // and not the original configuration.
    EXPECT_EQ(*new_cfg, *updated_config);
    EXPECT_NE(*original_config, *updated_config);
}

/// @brief Test class for execerising manager functions that are
/// influenced by DDNS parameters.
class D2ClientMgrParamsTest : public ::testing::Test {
public:
    /// @brief Constructor
    D2ClientMgrParamsTest() = default;

    /// @brief Destructor
    virtual ~D2ClientMgrParamsTest() = default;

private:
    /// @brief Prepares the class for a test.
    virtual void SetUp() {
        // Create a subnet and then a DdnsParams instance.
        // We'll use the subnet's setters to alter DDNS parameter values.
        subnet_.reset(new Subnet4(IOAddress("192.0.2.2"), 16, 1, 2, 3, 10));
        ddns_params_.reset(new DdnsParams(subnet_, true));
    }

    /// @brief Cleans up after the test.
    virtual void TearDown() {};

public:
    /// @brief Acts as the "selected" subnet.  It is passed into the
    /// constructor of ddns_params_.  This allows DDNS parameters to
    /// be modified via setters on subnet_.
    Subnet4Ptr subnet_;
    /// @brief Parameter instance based into D2ClientMgr functions
    DdnsParamsPtr ddns_params_;
};

/// @brief Tests that analyzeFqdn detects invalid combination of both the
/// client S and N flags set to true.
TEST(D2ClientMgr, analyzeFqdnInvalidCombination) {
    D2ClientMgr mgr;
    bool server_s = false;
    bool server_n = false;

    DdnsParams ddns_params;

    // client S=1 N=1 is invalid.  analyzeFqdn should throw.
    ASSERT_THROW(mgr.analyzeFqdn(true, true, server_s, server_n, ddns_params),
                 isc::BadValue);
}

/// @brief Tests that analyzeFqdn generates correct server S and N flags when
/// updates are enabled and all overrides are off.
TEST_F(D2ClientMgrParamsTest, analyzeFqdnEnabledNoOverrides) {
    D2ClientMgr mgr;
    bool server_s = false;
    bool server_n = false;

    // Create enabled configuration with all controls off (no overrides).
    subnet_->setDdnsSendUpdates(true);
    subnet_->setDdnsOverrideNoUpdate(false);
    subnet_->setDdnsOverrideClientUpdate(false);
    subnet_->setDdnsReplaceClientNameMode(D2ClientConfig::RCM_NEVER);
    subnet_->setDdnsGeneratedPrefix("");
    subnet_->setDdnsQualifyingSuffix("");
    subnet_->setHostnameCharSet("");
    subnet_->setHostnameCharReplacement("");

    // client S=0 N=0 means client wants to do forward update.
    // server S should be 0 (server is not doing forward updates)
    // and server N should be 0 (server doing reverse updates)
    mgr.analyzeFqdn(false, false, server_s, server_n, *ddns_params_);
    EXPECT_FALSE(server_s);
    EXPECT_FALSE(server_n);

    // client S=1 N=0 means client wants server to do forward update.
    // server S should be 1 (server is doing forward updates)
    // and server N should be 0 (server doing updates)
    mgr.analyzeFqdn(true, false, server_s, server_n, *ddns_params_);
    EXPECT_TRUE(server_s);
    EXPECT_FALSE(server_n);


    // client S=0 N=1 means client wants no one to do forward updates.
    // server S should be 0 (server is  not forward updates)
    // and server N should be 1 (server is not doing any updates)
    mgr.analyzeFqdn(false, true, server_s, server_n, *ddns_params_);
    EXPECT_FALSE(server_s);
    EXPECT_TRUE(server_n);
}

/// @brief Tests that analyzeFqdn generates correct server S and N flags when
/// updates are enabled and override-no-update is on.
TEST_F(D2ClientMgrParamsTest, analyzeFqdnEnabledOverrideNoUpdate) {
    D2ClientMgr mgr;
    bool server_s = false;
    bool server_n = false;

    // Create enabled configuration with override-no-update true.
    subnet_->setDdnsSendUpdates(true);
    subnet_->setDdnsOverrideNoUpdate(true);
    subnet_->setDdnsOverrideClientUpdate(false);
    subnet_->setDdnsReplaceClientNameMode(D2ClientConfig::RCM_NEVER);
    subnet_->setDdnsGeneratedPrefix("");
    subnet_->setDdnsQualifyingSuffix("");
    subnet_->setHostnameCharSet("");
    subnet_->setHostnameCharReplacement("");

    // client S=0 N=0 means client wants to do forward update.
    // server S should be 0 (server is not doing forward updates)
    // and server N should be 0 (server is doing reverse updates)
    mgr.analyzeFqdn(false, false, server_s, server_n, *ddns_params_);
    EXPECT_FALSE(server_s);
    EXPECT_FALSE(server_n);

    // client S=1 N=0 means client wants server to do forward update.
    // server S should be 1 (server is doing forward updates)
    // and server N should be 0 (server doing updates)
    mgr.analyzeFqdn(true, false, server_s, server_n, *ddns_params_);
    EXPECT_TRUE(server_s);
    EXPECT_FALSE(server_n);

    // client S=0 N=1 means client wants no one to do forward updates.
    // server S should be 1 (server is doing forward updates)
    // and server N should be 0 (server is doing updates)
    mgr.analyzeFqdn(false, true, server_s, server_n, *ddns_params_);
    EXPECT_TRUE(server_s);
    EXPECT_FALSE(server_n);
}

/// @brief Tests that analyzeFqdn generates correct server S and N flags when
/// updates are enabled and override-client-update is on.
TEST_F(D2ClientMgrParamsTest, analyzeFqdnEnabledOverrideClientUpdate) {
    D2ClientMgr mgr;
    bool server_s = false;
    bool server_n = false;

    // Create enabled configuration with override-client-update true.
    subnet_->setDdnsSendUpdates(true);
    subnet_->setDdnsOverrideNoUpdate(false);
    subnet_->setDdnsOverrideClientUpdate(true);
    subnet_->setDdnsReplaceClientNameMode(D2ClientConfig::RCM_NEVER);
    subnet_->setDdnsGeneratedPrefix("");
    subnet_->setDdnsQualifyingSuffix("");
    subnet_->setHostnameCharSet("");
    subnet_->setHostnameCharReplacement("");

    // client S=0 N=0 means client wants to do forward update.
    // server S should be 1 (server is doing forward updates)
    // and server N should be 0 (server doing updates)
    mgr.analyzeFqdn(false, false, server_s, server_n, *ddns_params_);
    EXPECT_TRUE(server_s);
    EXPECT_FALSE(server_n);

    // client S=1 N=0 means client wants server to do forward update.
    // server S should be 1 (server is doing forward updates)
    // and server N should be 0 (server doing updates)
    mgr.analyzeFqdn(true, false, server_s, server_n, *ddns_params_);
    EXPECT_TRUE(server_s);
    EXPECT_FALSE(server_n);

    // client S=0 N=1 means client wants no one to do forward updates.
    // server S should be 0 (server is  not forward updates)
    // and server N should be 1 (server is not doing any updates)
    mgr.analyzeFqdn(false, true, server_s, server_n, *ddns_params_);
    EXPECT_FALSE(server_s);
    EXPECT_TRUE(server_n);
}

/// @brief Verifies the adustFqdnFlags template with Option4ClientFqdn objects.
/// Ensures that the method can set the N, S, and O flags properly.
/// Other permutations are covered by analyzeFqdnFlag tests.
TEST_F(D2ClientMgrParamsTest, adjustFqdnFlagsV4) {
    D2ClientMgr mgr;
    Option4ClientFqdnPtr request;
    Option4ClientFqdnPtr response;

    // Create enabled configuration with override-no-update true.
    subnet_->setDdnsSendUpdates(true);
    subnet_->setDdnsOverrideNoUpdate(true);
    subnet_->setDdnsOverrideClientUpdate(false);
    subnet_->setDdnsReplaceClientNameMode(D2ClientConfig::RCM_NEVER);
    subnet_->setDdnsGeneratedPrefix("");
    subnet_->setDdnsQualifyingSuffix("");
    subnet_->setHostnameCharSet("");
    subnet_->setHostnameCharReplacement("");

    // client S=0 N=0 means client wants to do forward update.
    // server S should be 0 (server is not doing forward updates)
    // and server N should be 0 (server is doing reverse updates)
    // and server O should be 0
    request.reset(new Option4ClientFqdn(0, Option4ClientFqdn::RCODE_CLIENT(),
                                        "", Option4ClientFqdn::PARTIAL));
    response.reset(new Option4ClientFqdn(*request));
    response->resetFlags();

    mgr.adjustFqdnFlags<Option4ClientFqdn>(*request, *response, *ddns_params_);
    EXPECT_FALSE(response->getFlag(Option4ClientFqdn::FLAG_S));
    EXPECT_FALSE(response->getFlag(Option4ClientFqdn::FLAG_N));
    EXPECT_FALSE(response->getFlag(Option4ClientFqdn::FLAG_O));

    // client S=1 N=0 means client wants server to do forward update.
    // server S should be 1 (server is doing forward updates)
    // and server N should be 0 (server doing updates)
    // and server O should be 0
    request.reset(new Option4ClientFqdn(Option4ClientFqdn::FLAG_S,
                                        Option4ClientFqdn::RCODE_CLIENT(),
                                        "", Option4ClientFqdn::PARTIAL));
    response.reset(new Option4ClientFqdn(*request));
    response->resetFlags();

    mgr.adjustFqdnFlags<Option4ClientFqdn>(*request, *response, *ddns_params_);
    EXPECT_TRUE(response->getFlag(Option4ClientFqdn::FLAG_S));
    EXPECT_FALSE(response->getFlag(Option4ClientFqdn::FLAG_N));
    EXPECT_FALSE(response->getFlag(Option4ClientFqdn::FLAG_O));

    // client S=0 N=1 means client wants no one to do updates
    // server S should be 1 (server is doing forward updates)
    // and server N should be 0 (server doing updates)
    // and O should be 1 (overriding client S)
    request.reset(new Option4ClientFqdn(Option4ClientFqdn::FLAG_N,
                                        Option4ClientFqdn::RCODE_CLIENT(),
                                        "", Option4ClientFqdn::PARTIAL));
    response.reset(new Option4ClientFqdn(*request));
    response->resetFlags();

    mgr.adjustFqdnFlags<Option4ClientFqdn>(*request, *response, *ddns_params_);
    EXPECT_TRUE(response->getFlag(Option4ClientFqdn::FLAG_S));
    EXPECT_FALSE(response->getFlag(Option4ClientFqdn::FLAG_N));
    EXPECT_TRUE(response->getFlag(Option4ClientFqdn::FLAG_O));
}

/// @brief Verified the getUpdateDirections template method with
/// Option4ClientFqdn objects.
TEST(D2ClientMgr, updateDirectionsV4) {
    D2ClientMgr mgr;
    Option4ClientFqdnPtr response;

    bool do_forward = false;
    bool do_reverse = false;

    // Response S=0, N=0 should mean do reverse only.
    response.reset(new Option4ClientFqdn(0,
                                         Option4ClientFqdn::RCODE_CLIENT(),
                                         "", Option4ClientFqdn::PARTIAL));
    mgr.getUpdateDirections(*response, do_forward, do_reverse);
    EXPECT_FALSE(do_forward);
    EXPECT_TRUE(do_reverse);

    // Response S=0, N=1 should mean don't do either.
    response.reset(new Option4ClientFqdn(Option4ClientFqdn::FLAG_N,
                                         Option4ClientFqdn::RCODE_CLIENT(),
                                         "", Option4ClientFqdn::PARTIAL));
    mgr.getUpdateDirections(*response, do_forward, do_reverse);
    EXPECT_FALSE(do_forward);
    EXPECT_FALSE(do_reverse);

    // Response S=1, N=0 should mean do both.
    response.reset(new Option4ClientFqdn(Option4ClientFqdn::FLAG_S,
                                         Option4ClientFqdn::RCODE_CLIENT(),
                                         "", Option4ClientFqdn::PARTIAL));
    mgr.getUpdateDirections(*response, do_forward, do_reverse);
    EXPECT_TRUE(do_forward);
    EXPECT_TRUE(do_reverse);

    // Response S=1, N=1 isn't possible.
}

/// @brief Tests the qualifyName method's ability to construct FQDNs
TEST_F(D2ClientMgrParamsTest, qualifyName) {
    D2ClientMgr mgr;
    bool do_not_dot = false;
    bool do_dot = true;

    // Create enabled configuration
    subnet_->setDdnsSendUpdates(true);
    subnet_->setDdnsOverrideNoUpdate(false);
    subnet_->setDdnsOverrideClientUpdate(false);
    subnet_->setDdnsReplaceClientNameMode(D2ClientConfig::RCM_NEVER);
    subnet_->setDdnsGeneratedPrefix("prefix");
    subnet_->setDdnsQualifyingSuffix("suffix.com");
    subnet_->setHostnameCharSet("");
    subnet_->setHostnameCharReplacement("");

    // Verify that the qualifying suffix gets appended with a trailing dot added.
    std::string partial_name = "somehost";
    std::string qualified_name = mgr.qualifyName(partial_name, *ddns_params_, do_dot);
    EXPECT_EQ("somehost.suffix.com.", qualified_name);

    // Verify that the qualifying suffix gets appended without a trailing dot.
    partial_name = "somehost";
    qualified_name = mgr.qualifyName(partial_name, *ddns_params_, do_not_dot);
    EXPECT_EQ("somehost.suffix.com", qualified_name);

    // Verify that an empty suffix and false flag, does not change the name
    subnet_->setDdnsQualifyingSuffix("");
    partial_name = "somehost";
    qualified_name = mgr.qualifyName(partial_name, *ddns_params_, do_not_dot);
    EXPECT_EQ("somehost", qualified_name);

    // Verify that a qualifying suffix that already has a trailing
    // dot gets appended without doubling the dot.
    subnet_->setDdnsQualifyingSuffix("hasdot.com.");
    qualified_name = mgr.qualifyName(partial_name, *ddns_params_, do_dot);
    EXPECT_EQ("somehost.hasdot.com.", qualified_name);

    // Verify that the qualifying suffix gets appended without an
    // extraneous dot when partial_name ends with a "."
    qualified_name = mgr.qualifyName("somehost.", *ddns_params_, do_dot);
    EXPECT_EQ("somehost.hasdot.com.", qualified_name);

    // Verify that a name with a trailing dot does not get an extraneous
    // dot when the suffix is blank
    subnet_->setDdnsQualifyingSuffix("");
    qualified_name = mgr.qualifyName("somehost.", *ddns_params_, do_dot);
    EXPECT_EQ("somehost.", qualified_name);

    // Verify that a name with no trailing dot gets just a dot when the
    // suffix is blank
    qualified_name = mgr.qualifyName("somehost", *ddns_params_, do_dot);
    EXPECT_EQ("somehost.", qualified_name);

    // Verify that a name with no trailing dot does not get dotted when the
    // suffix is blank and trailing dot is false
    qualified_name = mgr.qualifyName("somehost", *ddns_params_, do_not_dot);
    EXPECT_EQ("somehost", qualified_name);

    // Verify that a name with trailing dot gets "undotted" when the
    // suffix is blank and trailing dot is false
    qualified_name = mgr.qualifyName("somehost.", *ddns_params_, do_not_dot);
    EXPECT_EQ("somehost", qualified_name);

}

/// @brief Tests the qualifyName method's ability to avoid duplicating
/// qualifying suffix.
TEST_F(D2ClientMgrParamsTest, qualifyNameWithoutDuplicatingSuffix) {
    D2ClientMgr mgr;
    bool do_dot = true;

    // Create enabled configuration
    subnet_->setDdnsSendUpdates(true);
    subnet_->setDdnsOverrideNoUpdate(false);
    subnet_->setDdnsOverrideClientUpdate(false);
    subnet_->setDdnsReplaceClientNameMode(D2ClientConfig::RCM_NEVER);
    subnet_->setDdnsGeneratedPrefix("prefix");
    subnet_->setDdnsQualifyingSuffix("suffix.com");
    subnet_->setHostnameCharSet("");
    subnet_->setHostnameCharReplacement("");

    // Verify that the qualifying suffix does not get appended when the
    // input name has the suffix but no trailing dot.
    std::string partial_name = "somehost.suffix.com";
    std::string qualified_name = mgr.qualifyName(partial_name, *ddns_params_, do_dot);
    EXPECT_EQ("somehost.suffix.com.", qualified_name);

    // Verify that the qualifying suffix does not get appended when the
    // input name has the suffix and a trailing dot.
    partial_name = "somehost.suffix.com.";
    qualified_name = mgr.qualifyName(partial_name, *ddns_params_, do_dot);
    EXPECT_EQ("somehost.suffix.com.", qualified_name);

    // Verify that the qualifying suffix does get appended when the
    // input name has the suffix embedded in it but does not begin
    // at a label boundary.
    partial_name = "somehost.almostsuffix.com";
    qualified_name = mgr.qualifyName(partial_name, *ddns_params_, do_dot);
    EXPECT_EQ("somehost.almostsuffix.com.suffix.com.", qualified_name);

    // Verify that the qualifying suffix does get appended when the
    // input name has the suffix embedded in it.
    partial_name = "somehost.suffix.com.org";
    qualified_name = mgr.qualifyName(partial_name, *ddns_params_, do_dot);
    EXPECT_EQ("somehost.suffix.com.org.suffix.com.", qualified_name);

    // Verify that the qualifying suffix does not get appended when the
    // input name is the suffix itself.
    partial_name = "suffix.com";
    qualified_name = mgr.qualifyName(partial_name, *ddns_params_, do_dot);
    EXPECT_EQ("suffix.com.", qualified_name);

    subnet_->setDdnsQualifyingSuffix("one.two.suffix.com");
    partial_name = "two.suffix.com";
    qualified_name = mgr.qualifyName(partial_name, *ddns_params_, do_dot);
    EXPECT_EQ("two.suffix.com.one.two.suffix.com.", qualified_name);
}

/// @brief Tests the generateFdqn method's ability to construct FQDNs
TEST_F(D2ClientMgrParamsTest, generateFqdn) {
    D2ClientMgr mgr;
    bool do_dot = true;

    // Create enabled configuration
    subnet_->setDdnsSendUpdates(true);
    subnet_->setDdnsOverrideNoUpdate(false);
    subnet_->setDdnsOverrideClientUpdate(false);
    subnet_->setDdnsReplaceClientNameMode(D2ClientConfig::RCM_NEVER);
    subnet_->setDdnsGeneratedPrefix("prefix");
    subnet_->setDdnsQualifyingSuffix("suffix.com");
    subnet_->setHostnameCharSet("");
    subnet_->setHostnameCharReplacement("");

    // Verify that it works with an IPv4 address.
    asiolink::IOAddress v4address("192.0.2.75");
    EXPECT_EQ("prefix-192-0-2-75.suffix.com.",
              mgr.generateFqdn(v4address, *ddns_params_, do_dot));

    // Verify that it works with an IPv6 address.
    asiolink::IOAddress v6address("2001:db8::2");
    EXPECT_EQ("prefix-2001-db8--2.suffix.com.",
              mgr.generateFqdn(v6address, *ddns_params_, do_dot));

    // Create a disabled config.
    subnet_->setDdnsSendUpdates(false);

    // Verify names generate properly with a disabled configuration.
    EXPECT_EQ("prefix-192-0-2-75.suffix.com.",
               mgr.generateFqdn(v4address, *ddns_params_, do_dot));
    EXPECT_EQ("prefix-2001-db8--2.suffix.com.",
               mgr.generateFqdn(v6address, *ddns_params_, do_dot));
}

/// @brief Tests adjustDomainName template method with Option4ClientFqdn
TEST_F(D2ClientMgrParamsTest, adjustDomainNameV4) {
    D2ClientMgr mgr;

    // Create enabled configuration
    subnet_->setDdnsSendUpdates(true);
    subnet_->setDdnsOverrideNoUpdate(false);
    subnet_->setDdnsOverrideClientUpdate(false);
    subnet_->setDdnsReplaceClientNameMode(D2ClientConfig::RCM_NEVER);
    subnet_->setDdnsGeneratedPrefix("prefix");
    subnet_->setDdnsQualifyingSuffix("suffix.com");
    subnet_->setHostnameCharSet("");
    subnet_->setHostnameCharReplacement("");

    struct Scenario {
        std::string description_;
        D2ClientConfig::ReplaceClientNameMode mode_;
        std::string client_name_;
        Option4ClientFqdn::DomainNameType client_name_type_;
        std::string expected_name_;
        Option4ClientFqdn::DomainNameType expected_name_type_;
    };

    std::vector<Scenario> scenarios = {
        {
            "RCM_NEVER #1, empty client name",
            D2ClientConfig::RCM_NEVER,
            "", Option4ClientFqdn::PARTIAL,
            "", Option4ClientFqdn::PARTIAL
        },
        {
            "RCM_NEVER #2, partial client name",
            D2ClientConfig::RCM_NEVER,
            "myhost", Option4ClientFqdn::PARTIAL,
            "myhost.suffix.com.", Option4ClientFqdn::FULL
        },
        {
            "RCM_NEVER #3, full client name",
            D2ClientConfig::RCM_NEVER,
            "myhost.example.com.", Option4ClientFqdn::FULL,
            "myhost.example.com.", Option4ClientFqdn::FULL
        },
        {
            "RCM_ALWAYS #1, empty client name",
            D2ClientConfig::RCM_ALWAYS,
            "", Option4ClientFqdn::PARTIAL,
            "", Option4ClientFqdn::PARTIAL
        },
        {
            "RCM_ALWAYS #2, partial client name",
            D2ClientConfig::RCM_ALWAYS,
            "myhost", Option4ClientFqdn::PARTIAL,
            "", Option4ClientFqdn::PARTIAL
        },
        {
            "RCM_ALWAYS #3, full client name",
            D2ClientConfig::RCM_ALWAYS,
            "myhost.example.com.", Option4ClientFqdn::FULL,
            "", Option4ClientFqdn::PARTIAL
        },
        {
            "RCM_WHEN_PRESENT #1, empty client name",
            D2ClientConfig::RCM_WHEN_PRESENT,
            "", Option4ClientFqdn::PARTIAL,
            "", Option4ClientFqdn::PARTIAL
        },
        {
            "RCM_WHEN_PRESENT #2, partial client name",
            D2ClientConfig::RCM_WHEN_PRESENT,
            "myhost", Option4ClientFqdn::PARTIAL,
            "", Option4ClientFqdn::PARTIAL
        },
        {
            "RCM_WHEN_PRESENT #3, full client name",
            D2ClientConfig::RCM_WHEN_PRESENT,
            "myhost.example.com.", Option4ClientFqdn::FULL,
            "", Option4ClientFqdn::PARTIAL
        },
        {
            "RCM_WHEN_NOT_PRESENT #1, empty client name",
            D2ClientConfig::RCM_WHEN_NOT_PRESENT,
            "", Option4ClientFqdn::PARTIAL,
            "", Option4ClientFqdn::PARTIAL
        },
        {
            "RCM_WHEN_NOT_PRESENT #2, partial client name",
            D2ClientConfig::RCM_WHEN_NOT_PRESENT,
            "myhost", Option4ClientFqdn::PARTIAL,
            "myhost.suffix.com.", Option4ClientFqdn::FULL
        },
        {
            "RCM_WHEN_NOT_PRESENT #3, full client name",
            D2ClientConfig::RCM_WHEN_NOT_PRESENT,
            "myhost.example.com.", Option4ClientFqdn::FULL,
            "myhost.example.com.", Option4ClientFqdn::FULL,
        }
    };

    for (auto scenario : scenarios) {
        SCOPED_TRACE(scenario.description_);
        {
            subnet_->setDdnsReplaceClientNameMode(scenario.mode_);
            Option4ClientFqdn request (0, Option4ClientFqdn::RCODE_CLIENT(),
                                       scenario.client_name_,
                                       scenario.client_name_type_);

            Option4ClientFqdn response(request);
            mgr.adjustDomainName<Option4ClientFqdn>(request, response, *ddns_params_);
            EXPECT_EQ(scenario.expected_name_, response.getDomainName());
            EXPECT_EQ(scenario.expected_name_type_, response.getDomainNameType());
        }
    }
}


/// @brief Tests adjustDomainName template method with Option6ClientFqdn
TEST_F(D2ClientMgrParamsTest, adjustDomainNameV6) {
    D2ClientMgr mgr;

    // Create enabled configuration
    subnet_->setDdnsSendUpdates(true);
    subnet_->setDdnsOverrideNoUpdate(false);
    subnet_->setDdnsOverrideClientUpdate(false);
    subnet_->setDdnsReplaceClientNameMode(D2ClientConfig::RCM_NEVER);
    subnet_->setDdnsGeneratedPrefix("prefix");
    subnet_->setDdnsQualifyingSuffix("suffix.com");
    subnet_->setHostnameCharSet("");
    subnet_->setHostnameCharReplacement("");

    struct Scenario {
        std::string description_;
        D2ClientConfig::ReplaceClientNameMode mode_;
        std::string client_name_;
        Option6ClientFqdn::DomainNameType client_name_type_;
        std::string expected_name_;
        Option6ClientFqdn::DomainNameType expected_name_type_;
    };

    std::vector<Scenario> scenarios = {
        {
            "RCM_NEVER #1, empty client name",
            D2ClientConfig::RCM_NEVER,
            "", Option6ClientFqdn::PARTIAL,
            "", Option6ClientFqdn::PARTIAL
        },
        {
            "RCM_NEVER #2, partial client name",
            D2ClientConfig::RCM_NEVER,
            "myhost", Option6ClientFqdn::PARTIAL,
            "myhost.suffix.com.", Option6ClientFqdn::FULL
        },
        {
            "RCM_NEVER #3, full client name",
            D2ClientConfig::RCM_NEVER,
            "myhost.example.com.", Option6ClientFqdn::FULL,
            "myhost.example.com.", Option6ClientFqdn::FULL
        },
        {
            "RCM_ALWAYS #1, empty client name",
            D2ClientConfig::RCM_ALWAYS,
            "", Option6ClientFqdn::PARTIAL,
            "", Option6ClientFqdn::PARTIAL
        },
        {
            "RCM_ALWAYS #2, partial client name",
            D2ClientConfig::RCM_ALWAYS,
            "myhost", Option6ClientFqdn::PARTIAL,
            "", Option6ClientFqdn::PARTIAL
        },
        {
            "RCM_ALWAYS #3, full client name",
            D2ClientConfig::RCM_ALWAYS,
            "myhost.example.com.", Option6ClientFqdn::FULL,
            "", Option6ClientFqdn::PARTIAL
        },
        {
            "RCM_WHEN_PRESENT #1, empty client name",
            D2ClientConfig::RCM_WHEN_PRESENT,
            "", Option6ClientFqdn::PARTIAL,
            "", Option6ClientFqdn::PARTIAL
        },
        {
            "RCM_WHEN_PRESENT #2, partial client name",
            D2ClientConfig::RCM_WHEN_PRESENT,
            "myhost", Option6ClientFqdn::PARTIAL,
            "", Option6ClientFqdn::PARTIAL
        },
        {
            "RCM_WHEN_PRESENT #3, full client name",
            D2ClientConfig::RCM_WHEN_PRESENT,
            "myhost.example.com.", Option6ClientFqdn::FULL,
            "", Option6ClientFqdn::PARTIAL
        },
        {
            "RCM_WHEN_NOT_PRESENT #1, empty client name",
            D2ClientConfig::RCM_WHEN_NOT_PRESENT,
            "", Option6ClientFqdn::PARTIAL,
            "", Option6ClientFqdn::PARTIAL
        },
        {
            "RCM_WHEN_NOT_PRESENT #2, partial client name",
            D2ClientConfig::RCM_WHEN_NOT_PRESENT,
            "myhost", Option6ClientFqdn::PARTIAL,
            "myhost.suffix.com.", Option6ClientFqdn::FULL
        },
        {
            "RCM_WHEN_NOT_PRESENT #3, full client name",
            D2ClientConfig::RCM_WHEN_NOT_PRESENT,
            "myhost.example.com.", Option6ClientFqdn::FULL,
            "myhost.example.com.", Option6ClientFqdn::FULL,
        }
    };

    for (auto scenario : scenarios) {
        SCOPED_TRACE(scenario.description_);
        {
            subnet_->setDdnsReplaceClientNameMode(scenario.mode_);
            Option6ClientFqdn request(0, scenario.client_name_,
                                      scenario.client_name_type_);

            Option6ClientFqdn response(request);
            mgr.adjustDomainName<Option6ClientFqdn>(request, response, *ddns_params_);
            EXPECT_EQ(scenario.expected_name_, response.getDomainName());
            EXPECT_EQ(scenario.expected_name_type_, response.getDomainNameType());
        }
    }
}

/// @brief Verifies the adustFqdnFlags template with Option6ClientFqdn objects.
/// Ensures that the method can set the N, S, and O flags properly.
/// Other permutations are covered by analyzeFqdnFlags tests.
TEST_F(D2ClientMgrParamsTest, adjustFqdnFlagsV6) {
    D2ClientMgr mgr;
    Option6ClientFqdnPtr request;
    Option6ClientFqdnPtr response;

    // Create enabled configuration with override-no-update true.
    subnet_->setDdnsSendUpdates(true);
    subnet_->setDdnsOverrideNoUpdate(true);
    subnet_->setDdnsOverrideClientUpdate(false);
    subnet_->setDdnsReplaceClientNameMode(D2ClientConfig::RCM_NEVER);
    subnet_->setDdnsGeneratedPrefix("");
    subnet_->setDdnsQualifyingSuffix("");
    subnet_->setHostnameCharSet("");
    subnet_->setHostnameCharReplacement("");

    // client S=0 N=0 means client wants to do forward update.
    // server S should be 0 (server is not doing forward updates)
    // and server N should be 0 (server doing reverse updates)
    // and server O should be 0
    request.reset(new Option6ClientFqdn(0, "", Option6ClientFqdn::PARTIAL));
    response.reset(new Option6ClientFqdn(*request));
    response->resetFlags();

    mgr.adjustFqdnFlags<Option6ClientFqdn>(*request, *response, *ddns_params_);
    EXPECT_FALSE(response->getFlag(Option6ClientFqdn::FLAG_S));
    EXPECT_FALSE(response->getFlag(Option6ClientFqdn::FLAG_N));
    EXPECT_FALSE(response->getFlag(Option6ClientFqdn::FLAG_O));

    // client S=1 N=0 means client wants server to do forward update.
    // server S should be 1 (server is doing forward updates)
    // and server N should be 0 (server doing updates)
    // and server O should be 0
    request.reset(new Option6ClientFqdn(Option6ClientFqdn::FLAG_S,
                                        "", Option6ClientFqdn::PARTIAL));
    response.reset(new Option6ClientFqdn(*request));
    response->resetFlags();

    mgr.adjustFqdnFlags<Option6ClientFqdn>(*request, *response, *ddns_params_);
    EXPECT_TRUE(response->getFlag(Option6ClientFqdn::FLAG_S));
    EXPECT_FALSE(response->getFlag(Option6ClientFqdn::FLAG_N));
    EXPECT_FALSE(response->getFlag(Option6ClientFqdn::FLAG_O));

    // client S=0 N=1 means client wants no one to do updates
    // server S should be 1 (server is doing forward updates)
    // and server N should be 0 (server doing updates)
    // and O should be 1 (overriding client S)
    request.reset(new Option6ClientFqdn(Option6ClientFqdn::FLAG_N,
                                        "", Option6ClientFqdn::PARTIAL));
    response.reset(new Option6ClientFqdn(*request));
    response->resetFlags();

    mgr.adjustFqdnFlags<Option6ClientFqdn>(*request, *response, *ddns_params_);
    EXPECT_TRUE(response->getFlag(Option6ClientFqdn::FLAG_S));
    EXPECT_FALSE(response->getFlag(Option6ClientFqdn::FLAG_N));
    EXPECT_TRUE(response->getFlag(Option6ClientFqdn::FLAG_O));
}


/// @brief Verified the getUpdateDirections template method with
/// Option6ClientFqdn objects.
TEST(D2ClientMgr, updateDirectionsV6) {
    D2ClientMgr mgr;
    Option6ClientFqdnPtr response;

    bool do_forward = false;
    bool do_reverse = false;

    // Response S=0, N=0 should mean do reverse only.
    response.reset(new Option6ClientFqdn(0,
                                         "", Option6ClientFqdn::PARTIAL));
    mgr.getUpdateDirections(*response, do_forward, do_reverse);
    EXPECT_FALSE(do_forward);
    EXPECT_TRUE(do_reverse);

    // Response S=0, N=1 should mean don't do either.
    response.reset(new Option6ClientFqdn(Option6ClientFqdn::FLAG_N,
                                         "", Option6ClientFqdn::PARTIAL));
    mgr.getUpdateDirections(*response, do_forward, do_reverse);
    EXPECT_FALSE(do_forward);
    EXPECT_FALSE(do_reverse);

    // Response S=1, N=0 should mean do both.
    response.reset(new Option6ClientFqdn(Option6ClientFqdn::FLAG_S,
                                         "", Option6ClientFqdn::PARTIAL));
    mgr.getUpdateDirections(*response, do_forward, do_reverse);
    EXPECT_TRUE(do_forward);
    EXPECT_TRUE(do_reverse);

    // Response S=1, N=1 isn't possible.
}

/// @brief Tests v4 FQDN name sanitizing
TEST_F(D2ClientMgrParamsTest, sanitizeFqdnV4) {
    D2ClientMgr mgr;

    // Create enabled configuration with override-no-update true.
    subnet_->setDdnsSendUpdates(true);
    subnet_->setDdnsOverrideNoUpdate(false);
    subnet_->setDdnsOverrideClientUpdate(false);
    subnet_->setDdnsReplaceClientNameMode(D2ClientConfig::RCM_NEVER);
    subnet_->setDdnsGeneratedPrefix("prefix");
    subnet_->setDdnsQualifyingSuffix("suffix.com");
    subnet_->setHostnameCharSet("[^A-Za-z0-9-]");
    subnet_->setHostnameCharReplacement("x");

    // Get the sanitizer.
    str::StringSanitizerPtr hostname_sanitizer;
    ASSERT_NO_THROW(hostname_sanitizer = ddns_params_->getHostnameSanitizer());
    ASSERT_TRUE(hostname_sanitizer);

    struct Scenario {
        std::string description_;
        std::string client_name_;
        Option4ClientFqdn::DomainNameType name_type_;
        std::string expected_name_;
    };

    std::vector<Scenario> scenarios = {
        {
        "full FQDN, name unchanged",
        "One.123.example.com.",
        Option4ClientFqdn::FULL,
        "one.123.example.com."
        },
        {
        "partial FQDN, name unchanged, but qualified",
        "One.123",
        Option4ClientFqdn::PARTIAL,
        "one.123.suffix.com."
        },
        {
        "full FQDN, scrubbed",
        "O#n^e.123.ex&a*mple.com.",
        Option4ClientFqdn::FULL,
        "oxnxe.123.exxaxmple.com."
        },
        {
        "partial FQDN, scrubbed and qualified",
        "One.1+2|3",
        Option4ClientFqdn::PARTIAL,
        "one.1x2x3.suffix.com."
        },
        {
        "full FQDN with characters that get escaped",
        "O n e.123.exa(m)ple.com.",
        Option4ClientFqdn::FULL,
        "oxnxe.123.exaxmxple.com."
        },
        {
        "full FQDN with escape sequences",
        "O\032n\032e.123.example.com.",
        Option4ClientFqdn::FULL,
        "oxnxe.123.example.com."
        }
    };

    for (auto scenario : scenarios) {
        SCOPED_TRACE(scenario.description_);
        {
            Option4ClientFqdn request(0, Option4ClientFqdn::RCODE_CLIENT(),
                                      scenario.client_name_, scenario.name_type_);
            Option4ClientFqdn response(request);

            mgr.adjustDomainName<Option4ClientFqdn>(request, response, *ddns_params_);
            EXPECT_EQ(scenario.expected_name_, response.getDomainName());
            EXPECT_EQ(Option4ClientFqdn::FULL, response.getDomainNameType());
        }
    }
}

/// @brief Tests v6 FQDN name sanitizing
/// @todo This test currently verifies that Option6ClientFqdn::DomainName
/// downcases strings used to construct it.  For some reason, currently
/// unknown, Option4ClientFqdn preserves the case, while Option6ClientFqdn
/// downcases it (see setDomainName() in both classes.  See Trac #5700.
TEST_F(D2ClientMgrParamsTest, sanitizeFqdnV6) {
    D2ClientMgr mgr;

    // Create enabled configuration with override-no-update true.
    subnet_->setDdnsSendUpdates(true);
    subnet_->setDdnsOverrideNoUpdate(false);
    subnet_->setDdnsOverrideClientUpdate(false);
    subnet_->setDdnsReplaceClientNameMode(D2ClientConfig::RCM_NEVER);
    subnet_->setDdnsGeneratedPrefix("prefix");
    subnet_->setDdnsQualifyingSuffix("suffix.com");
    subnet_->setHostnameCharSet("[^A-Za-z0-9-]");
    subnet_->setHostnameCharReplacement("x");

    // Get the sanitizer.
    str::StringSanitizerPtr hostname_sanitizer;
    ASSERT_NO_THROW(hostname_sanitizer = ddns_params_->getHostnameSanitizer());
    ASSERT_TRUE(hostname_sanitizer);

    struct Scenario {
        std::string description_;
        std::string client_name_;
        Option6ClientFqdn::DomainNameType name_type_;
        std::string expected_name_;
    };

    std::vector<Scenario> scenarios = {
        {
            "full FQDN, name unchanged",
            "One.123.example.com.",
            Option6ClientFqdn::FULL,
            "one.123.example.com."
        },
        {
            "partial FQDN, name unchanged, but qualified",
            "One.123",
            Option6ClientFqdn::PARTIAL,
            "one.123.suffix.com."
        },
        {
            "full FQDN, scrubbed",
            "O#n^e.123.ex&a*mple.com.",
            Option6ClientFqdn::FULL,
            "oxnxe.123.exxaxmple.com."
        },
        {
            "partial FQDN, scrubbed and qualified",
            "One.1+2|3",
            Option6ClientFqdn::PARTIAL,
            "one.1x2x3.suffix.com."
        },
        {
            "full FQDN with characters that get escaped",
            "O n e.123.exa(m)ple.com.",
            Option6ClientFqdn::FULL,
            "oxnxe.123.exaxmxple.com."
        },
        {
            "full FQDN with escape sequences",
            "O\032n\032e.123.example.com.",
            Option6ClientFqdn::FULL,
            "oxnxe.123.example.com."
        }
    };

    Option6ClientFqdnPtr response;
    for (auto scenario : scenarios) {
        SCOPED_TRACE(scenario.description_);
        {
            Option6ClientFqdn request(0, scenario.client_name_, scenario.name_type_);
            Option6ClientFqdn response(request);

            mgr.adjustDomainName<Option6ClientFqdn>(request, response, *ddns_params_);
            EXPECT_EQ(scenario.expected_name_, response.getDomainName());
            EXPECT_EQ(Option6ClientFqdn::FULL, response.getDomainNameType());
        }
    }
}

} // end of anonymous namespace