summaryrefslogtreecommitdiffstats
path: root/src/lib/dhcpsrv/tests/d2_client_unittest.cc
blob: 8a8550fbe246a83dace659b87246af2e6746f8d3 (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
// Copyright (C) 2012-2014 Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

#include <config.h>
#include <dhcp/option4_client_fqdn.h>
#include <dhcp/option6_client_fqdn.h>
#include <dhcpsrv/d2_client.h>
#include <exceptions/exceptions.h>

#include <gtest/gtest.h>

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

namespace {

/// @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());

    d2_client_config.reset();

    bool enable_updates = true;
    isc::asiolink::IOAddress server_ip("127.0.0.1");
    size_t server_port = 477;
    dhcp_ddns::NameChangeProtocol ncr_protocol = dhcp_ddns::NCR_UDP;
    dhcp_ddns::NameChangeFormat ncr_format = dhcp_ddns::FMT_JSON;
    bool always_include_fqdn = true;
    bool override_no_update = true;
    bool override_client_update = true;
    bool replace_client_name = true;
    std::string generated_prefix = "the_prefix";
    std::string qualifying_suffix = "the.suffix.";

    // Verify that we can construct a valid, enabled instance.
    ASSERT_NO_THROW(d2_client_config.reset(new
                                           D2ClientConfig(enable_updates,
                                                          server_ip,
                                                          server_port,
                                                          ncr_protocol,
                                                          ncr_format,
                                                          always_include_fqdn,
                                                          override_no_update,
                                                         override_client_update,
                                                          replace_client_name,
                                                          generated_prefix,
                                                          qualifying_suffix)));

    ASSERT_TRUE(d2_client_config);

    // 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->getNcrProtocol(), ncr_protocol);
    EXPECT_EQ(d2_client_config->getNcrFormat(), ncr_format);
    EXPECT_EQ(d2_client_config->getAlwaysIncludeFqdn(), always_include_fqdn);
    EXPECT_EQ(d2_client_config->getOverrideNoUpdate(), override_no_update);
    EXPECT_EQ(d2_client_config->getOverrideClientUpdate(),
              override_client_update);
    EXPECT_EQ(d2_client_config->getReplaceClientName(), replace_client_name);
    EXPECT_EQ(d2_client_config->getGeneratedPrefix(), generated_prefix);
    EXPECT_EQ(d2_client_config->getQualifyingSuffix(), qualifying_suffix);

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

    // 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,
                                                       dhcp_ddns::NCR_TCP,
                                                       ncr_format,
                                                       always_include_fqdn,
                                                       override_no_update,
                                                       override_client_update,
                                                       replace_client_name,
                                                       generated_prefix,
                                                       qualifying_suffix)),
                 D2ClientError);

    /// @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,
                    dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
                    true, true, true, true,
                    "pre-fix", "suf-fix")));
    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,
                    dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
                    true, true, true, true,
                    "pre-fix", "suf-fix")));
    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,
                    dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
                    true, true, true, true,
                    "pre-fix", "suf-fix")));
    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,
                    dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
                    true, true, true, true,
                    "pre-fix", "suf-fix")));
    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,
                    dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
                    true, true, true, true,
                    "pre-fix", "suf-fix")));
    ASSERT_TRUE(test_config);
    EXPECT_FALSE(*ref_config == *test_config);
    EXPECT_TRUE(*ref_config != *test_config);

    // Check a configuration that differs only by always_include_fqdn.
    ASSERT_NO_THROW(test_config.reset(new D2ClientConfig(true,
                    ref_address, 477,
                    dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
                    false, true, true, true,
                    "pre-fix", "suf-fix")));
    ASSERT_TRUE(test_config);
    EXPECT_FALSE(*ref_config == *test_config);
    EXPECT_TRUE(*ref_config != *test_config);

    // Check a configuration that differs only by override_no_update.
    ASSERT_NO_THROW(test_config.reset(new D2ClientConfig(true,
                    ref_address, 477,
                    dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
                    true, false, true, true,
                    "pre-fix", "suf-fix")));
    ASSERT_TRUE(test_config);
    EXPECT_FALSE(*ref_config == *test_config);
    EXPECT_TRUE(*ref_config != *test_config);

    // Check a configuration that differs only by override_client_update.
    ASSERT_NO_THROW(test_config.reset(new D2ClientConfig(true,
                    ref_address, 477,
                    dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
                    true, true, false, true,
                    "pre-fix", "suf-fix")));
    ASSERT_TRUE(test_config);
    EXPECT_FALSE(*ref_config == *test_config);
    EXPECT_TRUE(*ref_config != *test_config);

    // Check a configuration that differs only by replace_client_name.
    ASSERT_NO_THROW(test_config.reset(new D2ClientConfig(true,
                    ref_address, 477,
                    dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
                    true, true, true, false,
                    "pre-fix", "suf-fix")));
    ASSERT_TRUE(test_config);
    EXPECT_FALSE(*ref_config == *test_config);
    EXPECT_TRUE(*ref_config != *test_config);

    // Check a configuration that differs only by generated_prefix.
    ASSERT_NO_THROW(test_config.reset(new D2ClientConfig(true,
                    ref_address, 477,
                    dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
                    true, true, true, true,
                    "bogus", "suf-fix")));
    ASSERT_TRUE(test_config);
    EXPECT_FALSE(*ref_config == *test_config);
    EXPECT_TRUE(*ref_config != *test_config);

    // Check a configuration that differs only by qualifying_suffix.
    ASSERT_NO_THROW(test_config.reset(new D2ClientConfig(true,
                    ref_address, 477,
                    dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
                    true, true, true, true,
                    "pre-fix", "bogus")));
    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,
                                  dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
                                  true, true, true, true,
                                  "pre-fix", "suf-fix")));

    // 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 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;

    // Create disabled configuration.
    D2ClientConfigPtr cfg;
    ASSERT_NO_THROW(cfg.reset(new D2ClientConfig()));
    ASSERT_NO_THROW(mgr.setD2ClientConfig(cfg));
    ASSERT_FALSE(mgr.ddnsEnabled());

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

    // Create enabled configuration with all controls off (no overrides).
    ASSERT_NO_THROW(cfg.reset(new D2ClientConfig(true,
                                  isc::asiolink::IOAddress("127.0.0.1"), 477,
                                  dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
                                  false, false, false, false,
                                  "pre-fix", "suf-fix")));
    ASSERT_NO_THROW(mgr.setD2ClientConfig(cfg));
    ASSERT_TRUE(mgr.ddnsEnabled());

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

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

    // Create enabled configuration with all controls off (no overrides).
    D2ClientConfigPtr cfg;
    ASSERT_NO_THROW(cfg.reset(new D2ClientConfig(true,
                                  isc::asiolink::IOAddress("127.0.0.1"), 477,
                                  dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
                                  false, false, false, false,
                                  "pre-fix", "suf-fix")));
    ASSERT_NO_THROW(mgr.setD2ClientConfig(cfg));
    ASSERT_TRUE(mgr.ddnsEnabled());
    ASSERT_FALSE(cfg->getOverrideClientUpdate());
    ASSERT_FALSE(cfg->getOverrideNoUpdate());

    // 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 1 (server doing no updates)
    mgr.analyzeFqdn(false, false, server_s, server_n);
    EXPECT_FALSE(server_s);
    EXPECT_TRUE(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);
    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);
    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(D2ClientMgr, analyzeFqdnEnabledOverrideNoUpdate) {
    D2ClientMgr mgr;
    bool server_s = false;
    bool server_n = false;

    // Create enabled configuration with OVERRIDE_NO_UPDATE on.
    D2ClientConfigPtr cfg;
    ASSERT_NO_THROW(cfg.reset(new D2ClientConfig(true,
                                  isc::asiolink::IOAddress("127.0.0.1"), 477,
                                  dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
                                  false, true, false, false,
                                  "pre-fix", "suf-fix")));
    ASSERT_NO_THROW(mgr.setD2ClientConfig(cfg));
    ASSERT_TRUE(mgr.ddnsEnabled());
    ASSERT_TRUE(cfg->getOverrideNoUpdate());
    ASSERT_FALSE(cfg->getOverrideClientUpdate());

    // 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 1 (server is not doing any updates)
    mgr.analyzeFqdn(false, false, server_s, server_n);
    EXPECT_FALSE(server_s);
    EXPECT_TRUE(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);
    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);
    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(D2ClientMgr, analyzeFqdnEnabledOverrideClientUpdate) {
    D2ClientMgr mgr;
    bool server_s = false;
    bool server_n = false;

    // Create enabled configuration with OVERRIDE_CLIENT_UPDATE on.
    D2ClientConfigPtr cfg;
    ASSERT_NO_THROW(cfg.reset(new D2ClientConfig(true,
                                  isc::asiolink::IOAddress("127.0.0.1"), 477,
                                  dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
                                  false, false, true, false,
                                  "pre-fix", "suf-fix")));
    ASSERT_NO_THROW(mgr.setD2ClientConfig(cfg));
    ASSERT_TRUE(mgr.ddnsEnabled());
    ASSERT_FALSE(cfg->getOverrideNoUpdate());
    ASSERT_TRUE(cfg->getOverrideClientUpdate());

    // 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);
    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);
    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);
    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(D2ClientMgr, adjustFqdnFlagsV4) {
    D2ClientMgr mgr;
    Option4ClientFqdnPtr request;
    Option4ClientFqdnPtr response;

    // Create enabled configuration and override-no-update on.
    D2ClientConfigPtr cfg;
    ASSERT_NO_THROW(cfg.reset(new D2ClientConfig(true,
                                  isc::asiolink::IOAddress("127.0.0.1"), 477,
                                  dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
                                  false, true, false, false,
                                  "pre-fix", "suf-fix")));
    ASSERT_NO_THROW(mgr.setD2ClientConfig(cfg));
    ASSERT_TRUE(mgr.ddnsEnabled());
    ASSERT_TRUE(cfg->getOverrideNoUpdate());
    ASSERT_FALSE(cfg->getOverrideClientUpdate());

    // 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 1 (server doing no 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);
    EXPECT_FALSE(response->getFlag(Option4ClientFqdn::FLAG_S));
    EXPECT_TRUE(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);
    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);
    EXPECT_TRUE(response->getFlag(Option4ClientFqdn::FLAG_S));
    EXPECT_FALSE(response->getFlag(Option4ClientFqdn::FLAG_N));
    EXPECT_TRUE(response->getFlag(Option4ClientFqdn::FLAG_O));
}

/// @brief Tests the qualifyName method's ability to construct FQDNs
TEST(D2ClientMgr, qualifyName) {
    D2ClientMgr mgr;

    // Create enabled configuration.
    D2ClientConfigPtr cfg;
    ASSERT_NO_THROW(cfg.reset(new D2ClientConfig(true,
                                  isc::asiolink::IOAddress("127.0.0.1"), 477,
                                  dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
                                  false, false, true, false,
                                  "prefix", "suffix.com")));
    ASSERT_NO_THROW(mgr.setD2ClientConfig(cfg));

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

    ASSERT_NO_THROW(cfg.reset(new D2ClientConfig(true,
                                  isc::asiolink::IOAddress("127.0.0.1"), 477,
                                  dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
                                  false, false, true, false,
                                  "prefix", "hasdot.com.")));
    ASSERT_NO_THROW(mgr.setD2ClientConfig(cfg));

    // Verify that the qualifying suffix gets appended without dot added.
    qualified_name = mgr.qualifyName(partial_name);
    EXPECT_EQ("somehost.hasdot.com.", qualified_name);
}


/// @brief Tests the generateFdqn method's ability to construct FQDNs
TEST(D2ClientMgr, generateFqdn) {
    D2ClientMgr mgr;

    // Create enabled configuration.
    D2ClientConfigPtr cfg;
    ASSERT_NO_THROW(cfg.reset(new D2ClientConfig(true,
                                  isc::asiolink::IOAddress("127.0.0.1"), 477,
                                  dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
                                  false, false, true, false,
                                  "prefix", "suffix.com")));
    ASSERT_NO_THROW(mgr.setD2ClientConfig(cfg));

    // 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));

    // 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));

    // Create a disabled config.
    ASSERT_NO_THROW(cfg.reset(new D2ClientConfig()));
    ASSERT_NO_THROW(mgr.setD2ClientConfig(cfg));

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

/// @brief Tests adjustDomainName template method with Option4ClientFqdn
TEST(D2ClientMgr, adjustDomainNameV4) {
    D2ClientMgr mgr;
    Option4ClientFqdnPtr request;
    Option4ClientFqdnPtr response;

    // Create enabled configuration.
    D2ClientConfigPtr cfg;
    ASSERT_NO_THROW(cfg.reset(new D2ClientConfig(true,
                                  isc::asiolink::IOAddress("127.0.0.1"), 477,
                                  dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
                                  false, false, false, false,
                                  "prefix", "suffix.com")));
    ASSERT_NO_THROW(mgr.setD2ClientConfig(cfg));
    ASSERT_FALSE(cfg->getReplaceClientName());

    // replace-client-name is false, client passes in empty fqdn
    // reponse domain should be empty/partial.
    request.reset(new Option4ClientFqdn(0, Option4ClientFqdn::RCODE_CLIENT(),
                                        "", Option4ClientFqdn::PARTIAL));
    response.reset(new Option4ClientFqdn(*request));

    mgr.adjustDomainName<Option4ClientFqdn>(*request, *response);
    EXPECT_EQ("", response->getDomainName());
    EXPECT_EQ(Option4ClientFqdn::PARTIAL, response->getDomainNameType());


    // replace-client-name is false, client passes in a partial fqdn
    // response should contain client's name plus the qualifying suffix.
    request.reset(new Option4ClientFqdn(0, Option4ClientFqdn::RCODE_CLIENT(),
                                        "myhost", Option4ClientFqdn::PARTIAL));
    response.reset(new Option4ClientFqdn(*request));

    mgr.adjustDomainName<Option4ClientFqdn>(*request, *response);
    EXPECT_EQ("myhost.suffix.com.", response->getDomainName());
    EXPECT_EQ(Option4ClientFqdn::FULL, response->getDomainNameType());


    // replace-client-name is false, client passes in a full fqdn
    // response domain should not be altered.
    request.reset(new Option4ClientFqdn(0, Option4ClientFqdn::RCODE_CLIENT(),
                                        "myhost.example.com.",
                                         Option4ClientFqdn::FULL));
    response.reset(new Option4ClientFqdn(*request));
    mgr.adjustDomainName<Option4ClientFqdn>(*request, *response);
    EXPECT_EQ("myhost.example.com.", response->getDomainName());
    EXPECT_EQ(Option4ClientFqdn::FULL, response->getDomainNameType());

    // Create enabled configuration.
    ASSERT_NO_THROW(cfg.reset(new D2ClientConfig(true,
                                  isc::asiolink::IOAddress("127.0.0.1"), 477,
                                  dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
                                  false, false, false, true,
                                  "prefix", "suffix.com")));
    ASSERT_NO_THROW(mgr.setD2ClientConfig(cfg));
    ASSERT_TRUE(cfg->getReplaceClientName());

    // replace-client-name is true, client passes in empty fqdn
    // reponse domain should be empty/partial.
    request.reset(new Option4ClientFqdn(0, Option4ClientFqdn::RCODE_CLIENT(),
                                        "", Option4ClientFqdn::PARTIAL));
    response.reset(new Option4ClientFqdn(*request));

    mgr.adjustDomainName<Option4ClientFqdn>(*request, *response);
    EXPECT_EQ("", response->getDomainName());
    EXPECT_EQ(Option4ClientFqdn::PARTIAL, response->getDomainNameType());

    // replace-client-name is true, client passes in a partial fqdn
    // reponse domain should be empty/partial.
    request.reset(new Option4ClientFqdn(0, Option4ClientFqdn::RCODE_CLIENT(),
                                        "myhost", Option4ClientFqdn::PARTIAL));
    response.reset(new Option4ClientFqdn(*request));

    mgr.adjustDomainName<Option4ClientFqdn>(*request, *response);
    EXPECT_EQ("", response->getDomainName());
    EXPECT_EQ(Option4ClientFqdn::PARTIAL, response->getDomainNameType());


    // replace-client-name is true, client passes in a full fqdn
    // reponse domain should be empty/partial.
    request.reset(new Option4ClientFqdn(0, Option4ClientFqdn::RCODE_CLIENT(),
                                        "myhost.example.com.",
                                         Option4ClientFqdn::FULL));
    response.reset(new Option4ClientFqdn(*request));
    mgr.adjustDomainName<Option4ClientFqdn>(*request, *response);
    EXPECT_EQ("", response->getDomainName());
    EXPECT_EQ(Option4ClientFqdn::PARTIAL, response->getDomainNameType());
}

/// @brief Tests adjustDomainName template method with Option6ClientFqdn
TEST(D2ClientMgr, adjustDomainNameV6) {
    D2ClientMgr mgr;
    Option6ClientFqdnPtr request;
    Option6ClientFqdnPtr response;

    // Create enabled configuration.
    D2ClientConfigPtr cfg;
    ASSERT_NO_THROW(cfg.reset(new D2ClientConfig(true,
                                  isc::asiolink::IOAddress("127.0.0.1"), 477,
                                  dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
                                  false, false, false, false,
                                  "prefix", "suffix.com")));
    ASSERT_NO_THROW(mgr.setD2ClientConfig(cfg));
    ASSERT_FALSE(cfg->getReplaceClientName());

    // replace-client-name is false, client passes in empty fqdn
    // reponse domain should be empty/partial.
    request.reset(new Option6ClientFqdn(0, "", Option6ClientFqdn::PARTIAL));
    response.reset(new Option6ClientFqdn(*request));

    mgr.adjustDomainName<Option6ClientFqdn>(*request, *response);
    EXPECT_EQ("", response->getDomainName());
    EXPECT_EQ(Option6ClientFqdn::PARTIAL, response->getDomainNameType());

    // replace-client-name is false, client passes in a partial fqdn
    // response should contain client's name plus the qualifying suffix.
    request.reset(new Option6ClientFqdn(0, "myhost",
                                        Option6ClientFqdn::PARTIAL));
    response.reset(new Option6ClientFqdn(*request));

    mgr.adjustDomainName<Option6ClientFqdn>(*request, *response);
    EXPECT_EQ("myhost.suffix.com.", response->getDomainName());
    EXPECT_EQ(Option6ClientFqdn::FULL, response->getDomainNameType());


    // replace-client-name is false, client passes in a full fqdn
    // response domain should not be altered.
    request.reset(new Option6ClientFqdn(0, "myhost.example.com.",
                                        Option6ClientFqdn::FULL));
    response.reset(new Option6ClientFqdn(*request));
    mgr.adjustDomainName<Option6ClientFqdn>(*request, *response);
    EXPECT_EQ("myhost.example.com.", response->getDomainName());
    EXPECT_EQ(Option6ClientFqdn::FULL, response->getDomainNameType());

    // Create enabled configuration.
    ASSERT_NO_THROW(cfg.reset(new D2ClientConfig(true,
                                  isc::asiolink::IOAddress("127.0.0.1"), 477,
                                  dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
                                  false, false, false, true,
                                  "prefix", "suffix.com")));
    ASSERT_NO_THROW(mgr.setD2ClientConfig(cfg));
    ASSERT_TRUE(cfg->getReplaceClientName());

    // replace-client-name is true, client passes in empty fqdn
    // reponse domain should be empty/partial.
    request.reset(new Option6ClientFqdn(0, "", Option6ClientFqdn::PARTIAL));
    response.reset(new Option6ClientFqdn(*request));

    mgr.adjustDomainName<Option6ClientFqdn>(*request, *response);
    EXPECT_EQ("", response->getDomainName());
    EXPECT_EQ(Option6ClientFqdn::PARTIAL, response->getDomainNameType());

    // replace-client-name is true, client passes in a partial fqdn
    // reponse domain should be empty/partial.
    request.reset(new Option6ClientFqdn(0, "myhost",
                                        Option6ClientFqdn::PARTIAL));
    response.reset(new Option6ClientFqdn(*request));

    mgr.adjustDomainName<Option6ClientFqdn>(*request, *response);
    EXPECT_EQ("", response->getDomainName());
    EXPECT_EQ(Option6ClientFqdn::PARTIAL, response->getDomainNameType());


    // replace-client-name is true, client passes in a full fqdn
    // reponse domain should be empty/partial.
    request.reset(new Option6ClientFqdn(0, "myhost.example.com.",
                                        Option6ClientFqdn::FULL));
    response.reset(new Option6ClientFqdn(*request));
    mgr.adjustDomainName<Option6ClientFqdn>(*request, *response);
    EXPECT_EQ("", response->getDomainName());
    EXPECT_EQ(Option6ClientFqdn::PARTIAL, 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(D2ClientMgr, adjustFqdnFlagsV6) {
    D2ClientMgr mgr;
    Option6ClientFqdnPtr request;
    Option6ClientFqdnPtr response;

    // Create enabled configuration and override-no-update on.
    D2ClientConfigPtr cfg;
    ASSERT_NO_THROW(cfg.reset(new D2ClientConfig(true,
                                  isc::asiolink::IOAddress("127.0.0.1"), 477,
                                  dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
                                  false, true, false, false,
                                  "pre-fix", "suf-fix")));
    ASSERT_NO_THROW(mgr.setD2ClientConfig(cfg));
    ASSERT_TRUE(mgr.ddnsEnabled());
    ASSERT_TRUE(cfg->getOverrideNoUpdate());
    ASSERT_FALSE(cfg->getOverrideClientUpdate());

    // 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 1 (server doing no 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);
    EXPECT_FALSE(response->getFlag(Option6ClientFqdn::FLAG_S));
    EXPECT_TRUE(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);
    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);
    EXPECT_TRUE(response->getFlag(Option6ClientFqdn::FLAG_S));
    EXPECT_FALSE(response->getFlag(Option6ClientFqdn::FLAG_N));
    EXPECT_TRUE(response->getFlag(Option6ClientFqdn::FLAG_O));
}

} // end of anonymous namespace