summaryrefslogtreecommitdiffstats
path: root/src/lib/dhcpsrv/tests/host_cache_unittest.cc
blob: c6ad92009845c7a3a9e7c12097a24e9ce09fdf7f (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
// Copyright (C) 2018-2020 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 <dhcpsrv/host_data_source_factory.h>
#include <dhcpsrv/cache_host_data_source.h>
#include <dhcpsrv/host_mgr.h>
#include <dhcpsrv/testutils/host_data_source_utils.h>
#include <dhcpsrv/testutils/memory_host_data_source.h>
#include <exceptions/exceptions.h>

#include <gtest/gtest.h>

#include <iostream>
#include <sstream>

using namespace std;
using namespace isc;
using namespace isc::asiolink;
using namespace isc::db;
using namespace isc::dhcp;
using namespace isc::dhcp::test;

namespace {

/// @brief Test dump cache class.
class TestHostCache : public MemHostDataSource, public CacheHostDataSource {
public:

    /// Constructor
    TestHostCache() : adds_(0), inserts_(0) { }

    /// Destructor
    virtual ~TestHostCache() { }

    /// Override add
    void add(const HostPtr& host) {
        isc_throw(NotImplemented,
                  "add is not implemented: " << host->toText());
    }

    /// Insert
    size_t insert(const ConstHostPtr& host, bool overwrite) {
        if (overwrite) {
            ++inserts_;
        } else {
            ++adds_;
        }
        HostPtr host_copy(new Host(*host));
        store_.push_back(host_copy);
        return (0);
    }

    /// Remove
    bool remove(const HostPtr& host) {
        for (auto h = store_.begin(); h != store_.end(); ++h) {
            if (*h == host) {
                store_.erase(h);
                return (true);
            }
        }
        return (false);
    }

    /// Flush
    void flush(size_t) {
        isc_throw(NotImplemented, "flush is not implemented");
    }

    /// Size
    size_t size() const {
        return (store_.size());
    }

    /// Capacity
    size_t capacity() const {
        return (0);
    }

    /// Type
    string getType() const {
        return ("cache");
    }

    /// Add counter
    size_t adds_;

    /// Insert counter
    size_t inserts_;
};

/// @brief TestHostCache pointer type
typedef boost::shared_ptr<TestHostCache> TestHostCachePtr;

/// @brief Test data source class.
class TestHostDataSource : public MemHostDataSource {
public:

    /// Destructor
    virtual ~TestHostDataSource() { }

    /// Type
    string getType() const {
        return ("test");
    }
};

/// @brief TestHostDataSource pointer type
typedef boost::shared_ptr<TestHostDataSource> TestHostDataSourcePtr;

/// @brief Test fixture for testing cache feature.
class HostCacheTest : public ::testing::Test {
public:

    /// @brief Constructor.
    HostCacheTest() {
        HostMgr::create();

        // Host cache.
        hcptr_.reset(new TestHostCache());
        auto cacheFactory = [this](const DatabaseConnection::ParameterMap&) {
            return (hcptr_);
        };
        HostDataSourceFactory::registerFactory("cache", cacheFactory);
        HostMgr::addBackend("type=cache");

        // Host data source.
        memptr_.reset(new TestHostDataSource());
        auto testFactory = [this](const DatabaseConnection::ParameterMap&) {
            return (memptr_);
        };
        HostDataSourceFactory::registerFactory("test", testFactory);
        HostMgr::addBackend("type=test");
    }

    /// @brief Destructor.
    virtual ~HostCacheTest() {
        HostDataSourceFactory::deregisterFactory("test");
        HostDataSourceFactory::deregisterFactory("cache");
    }

    /// @brief Test host cache.
    TestHostCachePtr hcptr_;

    /// @brief Test host data source.
    TestHostDataSourcePtr memptr_;
};

// Check basic cache feature for IPv4.
TEST_F(HostCacheTest, identifier4) {
    // Check we have what we need.
    ASSERT_TRUE(hcptr_);
    EXPECT_TRUE(HostMgr::checkCacheBackend());
    EXPECT_EQ(0, hcptr_->size());
    EXPECT_EQ(0, hcptr_->inserts_);
    ASSERT_TRUE(memptr_);

    // Create a host reservation.
    HostPtr host = HostDataSourceUtils::initializeHost4("192.0.2.1",
                                                        Host::IDENT_HWADDR);
    ASSERT_TRUE(host);  // Make sure the host is generated properly.
    const IOAddress& address = host->getIPv4Reservation();

    // Try to add it to the host data source.
    ASSERT_NO_THROW(memptr_->add(host));

    // Try to get it cached.
    ConstHostPtr got = HostMgr::instance().get4(host->getIPv4SubnetID(),
                                                host->getIdentifierType(),
                                                &host->getIdentifier()[0],
                                                host->getIdentifier().size());
    ASSERT_TRUE(got);
    HostDataSourceUtils::compareHosts(got, host);

    // Verify it was cached.
    EXPECT_EQ(1, hcptr_->size());
    EXPECT_EQ(1, hcptr_->inserts_);

    // Remove it from test host data source.
    EXPECT_TRUE(memptr_->del(host->getIPv4SubnetID(), address));

    // It is cached so we can still get it.
    got = HostMgr::instance().get4(host->getIPv4SubnetID(),
                                   host->getIdentifierType(),
                                   &host->getIdentifier()[0],
                                   host->getIdentifier().size());
    ASSERT_TRUE(got);
    HostDataSourceUtils::compareHosts(got, host);

    // Even by address.
    got = HostMgr::instance().get4(host->getIPv4SubnetID(), address);
    ASSERT_TRUE(got);
    HostDataSourceUtils::compareHosts(got, host);

    // Verify cache status.
    EXPECT_EQ(1, hcptr_->size());
    EXPECT_EQ(1, hcptr_->inserts_);
}

// Check basic cache feature for IPv6.
TEST_F(HostCacheTest, identifier6) {
    // Check we have what we need.
    ASSERT_TRUE(hcptr_);
    EXPECT_TRUE(HostMgr::checkCacheBackend());
    EXPECT_EQ(0, hcptr_->size());
    EXPECT_EQ(0, hcptr_->inserts_);
    ASSERT_TRUE(memptr_);

    // Create a host reservation.
    HostPtr host = HostDataSourceUtils::initializeHost6("2001:db8::1",
                                                        Host::IDENT_DUID,
                                                        false);
    ASSERT_TRUE(host);  // Make sure the host is generated properly.

    // Get the address.
    IPv6ResrvRange resrvs = host->getIPv6Reservations();
    ASSERT_EQ(1, std::distance(resrvs.first, resrvs.second));
    const IOAddress& address = resrvs.first->second.getPrefix();
    ASSERT_EQ("2001:db8::1", address.toText());

    // Try to add it to the host data source.
    ASSERT_NO_THROW(memptr_->add(host));

    // Try to get it cached.
    ConstHostPtr got = HostMgr::instance().get6(host->getIPv6SubnetID(),
                                                host->getIdentifierType(),
                                                &host->getIdentifier()[0],
                                                host->getIdentifier().size());
    ASSERT_TRUE(got);
    HostDataSourceUtils::compareHosts(got, host);

    // Verify it was cached.
    EXPECT_EQ(1, hcptr_->size());
    EXPECT_EQ(1, hcptr_->inserts_);

    // Remove it from test host data source.
    EXPECT_TRUE(memptr_->del(host->getIPv6SubnetID(), address));

    // It is cached so we can still get it.
    got = HostMgr::instance().get6(host->getIPv6SubnetID(),
                                   host->getIdentifierType(),
                                   &host->getIdentifier()[0],
                                   host->getIdentifier().size());
    ASSERT_TRUE(got);
    HostDataSourceUtils::compareHosts(got, host);

    // Even by address.
    got = HostMgr::instance().get6(host->getIPv6SubnetID(), address);
    ASSERT_TRUE(got);
    HostDataSourceUtils::compareHosts(got, host);

    // Verify cache status.
    EXPECT_EQ(1, hcptr_->size());
    EXPECT_EQ(1, hcptr_->inserts_);
}

// Check by address caching for IPv4.
TEST_F(HostCacheTest, address4) {
    // Check we have what we need.
    ASSERT_TRUE(hcptr_);
    EXPECT_TRUE(HostMgr::checkCacheBackend());
    EXPECT_EQ(0, hcptr_->size());
    EXPECT_EQ(0, hcptr_->inserts_);
    ASSERT_TRUE(memptr_);

    // Create a host reservation.
    HostPtr host = HostDataSourceUtils::initializeHost4("192.0.2.1",
                                                        Host::IDENT_HWADDR);
    ASSERT_TRUE(host);  // Make sure the host is generated properly.
    const IOAddress& address = host->getIPv4Reservation();

    // Try to add it to the host data source.
    ASSERT_NO_THROW(memptr_->add(host));

    // Try to get it cached.
    ConstHostPtr got = HostMgr::instance().get4(host->getIPv4SubnetID(),
                                                address);
    ASSERT_TRUE(got);
    HostDataSourceUtils::compareHosts(got, host);

    // Verify it was cached.
    EXPECT_EQ(1, hcptr_->size());
    EXPECT_EQ(1, hcptr_->inserts_);

    // Remove it from test host data source.
    EXPECT_TRUE(memptr_->del(host->getIPv4SubnetID(), address));

    // It is cached so we can still get it.
    got = HostMgr::instance().get4(host->getIPv4SubnetID(), address);
    ASSERT_TRUE(got);
    HostDataSourceUtils::compareHosts(got, host);

    // Even by identifier.
    got = HostMgr::instance().get4(host->getIPv4SubnetID(),
                                   host->getIdentifierType(),
                                   &host->getIdentifier()[0],
                                   host->getIdentifier().size());
    ASSERT_TRUE(got);
    HostDataSourceUtils::compareHosts(got, host);

    // Verify cache status.
    EXPECT_EQ(1, hcptr_->size());
    EXPECT_EQ(1, hcptr_->inserts_);
}

// Check by address caching for IPv6.
TEST_F(HostCacheTest, address6) {
    // Check we have what we need.
    ASSERT_TRUE(hcptr_);
    EXPECT_TRUE(HostMgr::checkCacheBackend());
    EXPECT_EQ(0, hcptr_->size());
    EXPECT_EQ(0, hcptr_->inserts_);
    ASSERT_TRUE(memptr_);

    // Create a host reservation.
    HostPtr host = HostDataSourceUtils::initializeHost6("2001:db8::1",
                                                        Host::IDENT_DUID,
                                                        false);
    ASSERT_TRUE(host);  // Make sure the host is generated properly.

    // Get the address.
    IPv6ResrvRange resrvs = host->getIPv6Reservations();
    ASSERT_EQ(1, std::distance(resrvs.first, resrvs.second));
    const IOAddress& address = resrvs.first->second.getPrefix();
    ASSERT_EQ("2001:db8::1", address.toText());

    // Try to add it to the host data source.
    ASSERT_NO_THROW(memptr_->add(host));

    // Try to get it cached.
    ConstHostPtr got = HostMgr::instance().get6(host->getIPv6SubnetID(),
                                                address);
    ASSERT_TRUE(got);
    HostDataSourceUtils::compareHosts(got, host);

    // Verify it was cached.
    EXPECT_EQ(1, hcptr_->size());
    EXPECT_EQ(1, hcptr_->inserts_);

    // Remove it from test host data source.
    EXPECT_TRUE(memptr_->del(host->getIPv6SubnetID(), address));

    // It is cached so we can still get it.
    got = HostMgr::instance().get6(host->getIPv6SubnetID(), address);
    ASSERT_TRUE(got);
    HostDataSourceUtils::compareHosts(got, host);

    // Even by identifier.
    got = HostMgr::instance().get6(host->getIPv6SubnetID(),
                                   host->getIdentifierType(),
                                   &host->getIdentifier()[0],
                                   host->getIdentifier().size());
    ASSERT_TRUE(got);
    HostDataSourceUtils::compareHosts(got, host);

    // Verify cache status.
    EXPECT_EQ(1, hcptr_->size());
    EXPECT_EQ(1, hcptr_->inserts_);
}

// Check negative cache feature for IPv4.
TEST_F(HostCacheTest, negativeIdentifier4) {
    // Check we have what we need.
    ASSERT_TRUE(hcptr_);
    EXPECT_TRUE(HostMgr::checkCacheBackend());
    EXPECT_EQ(0, hcptr_->size());
    EXPECT_EQ(0, hcptr_->adds_);
    ASSERT_TRUE(memptr_);
    ASSERT_FALSE(HostMgr::instance().getNegativeCaching());

    // Create a host reservation.
    // We will not add it anywhere, just will use its values.
    HostPtr host = HostDataSourceUtils::initializeHost4("192.0.2.1", Host::IDENT_HWADDR);
    ASSERT_TRUE(host);

    // Try to get it cached.
    ConstHostPtr got = HostMgr::instance().get4(host->getIPv4SubnetID(),
                                                host->getIdentifierType(),
                                                &host->getIdentifier()[0],
                                                host->getIdentifier().size());
    ASSERT_FALSE(got);
    // Again to be sure it was not negatively cached.
    got = HostMgr::instance().get4Any(host->getIPv4SubnetID(),
                                      host->getIdentifierType(),
                                      &host->getIdentifier()[0],
                                      host->getIdentifier().size());
    ASSERT_FALSE(got);

    // Enable negative caching.
    HostMgr::instance().setNegativeCaching(true);
    ASSERT_TRUE(HostMgr::instance().getNegativeCaching());

    // Try it again. There is no such host, but this time negative cache is enabled,
    // so this negative response will be added to the cache.
    got = HostMgr::instance().get4(host->getIPv4SubnetID(),
                                   host->getIdentifierType(),
                                   &host->getIdentifier()[0],
                                   host->getIdentifier().size());
    ASSERT_FALSE(got);
    EXPECT_EQ(1, hcptr_->size());
    EXPECT_EQ(1, hcptr_->adds_);
    got = HostMgr::instance().get4Any(host->getIPv4SubnetID(),
                                      host->getIdentifierType(),
                                      &host->getIdentifier()[0],
                                      host->getIdentifier().size());
    ASSERT_TRUE(got);
    EXPECT_TRUE(got->getNegative());

    // Look at inside the negative cached value.
    EXPECT_EQ(host->getIPv4SubnetID(), got->getIPv4SubnetID());
    EXPECT_EQ(host->getIdentifierType(), got->getIdentifierType());
    ASSERT_EQ(host->getIdentifier().size(), got->getIdentifier().size());
    EXPECT_EQ(0, memcmp(&host->getIdentifier()[0],
                        &got->getIdentifier()[0],
                        host->getIdentifier().size()));
    EXPECT_EQ("0.0.0.0", got->getIPv4Reservation().toText());

    // get4() (vs get4Any()) does not return negative cached values.
    got = HostMgr::instance().get4(host->getIPv4SubnetID(),
                                   host->getIdentifierType(),
                                   &host->getIdentifier()[0],
                                   host->getIdentifier().size());
    EXPECT_FALSE(got);

    // Verify cache status.
    EXPECT_EQ(1, hcptr_->size());
    EXPECT_EQ(1, hcptr_->adds_);
    EXPECT_EQ(0, hcptr_->inserts_);

    // We can verify other overloads of get4() but the hwaddr/duid is
    // not implemented by the memory test backend and the negative cache
    // entry has no IP reservation when inserted by the host manager.
}

// Check negative cache feature for IPv6.
TEST_F(HostCacheTest, negativeIdentifier6) {
    // Check we have what we need.
    ASSERT_TRUE(hcptr_);
    EXPECT_TRUE(HostMgr::checkCacheBackend());
    EXPECT_EQ(0, hcptr_->size());
    EXPECT_EQ(0, hcptr_->adds_);
    ASSERT_TRUE(memptr_);

    // Create a host reservation.
    // We will not add it anywhere, just will use its values.
    HostPtr host = HostDataSourceUtils::initializeHost6("2001:db8::1",
                                                        Host::IDENT_DUID,
                                                        false);
    ASSERT_TRUE(host);  // Make sure the host is generated properly.

    // Do not add it to the host data source.

    // Try to get it cached.
    ConstHostPtr got = HostMgr::instance().get6(host->getIPv6SubnetID(),
                                                host->getIdentifierType(),
                                                &host->getIdentifier()[0],
                                                host->getIdentifier().size());
    ASSERT_FALSE(got);
    // Again to be sure it was not negatively cached.
    got = HostMgr::instance().get6Any(host->getIPv6SubnetID(),
                                      host->getIdentifierType(),
                                      &host->getIdentifier()[0],
                                      host->getIdentifier().size());
    ASSERT_FALSE(got);

    // Enable negative caching.
    HostMgr::instance().setNegativeCaching(true);
    ASSERT_TRUE(HostMgr::instance().getNegativeCaching());

    // Try it but it will be cached only when get6 (vs get6Any) is called.
    got = HostMgr::instance().get6Any(host->getIPv6SubnetID(),
                                      host->getIdentifierType(),
                                      &host->getIdentifier()[0],
                                      host->getIdentifier().size());
    ASSERT_FALSE(got);
    EXPECT_EQ(0, hcptr_->size());
    EXPECT_EQ(0, hcptr_->adds_);

    got = HostMgr::instance().get6(host->getIPv6SubnetID(),
                                   host->getIdentifierType(),
                                   &host->getIdentifier()[0],
                                   host->getIdentifier().size());
    ASSERT_FALSE(got);

    // There is a negative cached value now.
    EXPECT_EQ(1, hcptr_->size());
    EXPECT_EQ(1, hcptr_->adds_);
    got = HostMgr::instance().get6Any(host->getIPv6SubnetID(),
                                      host->getIdentifierType(),
                                      &host->getIdentifier()[0],
                                      host->getIdentifier().size());
    ASSERT_TRUE(got);
    EXPECT_TRUE(got->getNegative());

    // Look at inside the negative cached value.
    EXPECT_EQ(host->getIPv6SubnetID(), got->getIPv6SubnetID());
    EXPECT_EQ(host->getIdentifierType(), got->getIdentifierType());
    ASSERT_EQ(host->getIdentifier().size(), got->getIdentifier().size());
    EXPECT_EQ(0, memcmp(&host->getIdentifier()[0],
                        &got->getIdentifier()[0],
                        host->getIdentifier().size()));
    EXPECT_FALSE(got->hasIPv6Reservation());

    // get6() (vs get6Any()) does not return negative cached values.
    got = HostMgr::instance().get6(host->getIPv6SubnetID(),
                                   host->getIdentifierType(),
                                   &host->getIdentifier()[0],
                                   host->getIdentifier().size());
    EXPECT_FALSE(got);

    // Verify cache status.
    EXPECT_EQ(1, hcptr_->size());
    EXPECT_EQ(1, hcptr_->adds_);
    EXPECT_EQ(0, hcptr_->inserts_);

    // No other tests, cf negativeIdentifier4 end comment.
}

// Check that negative caching by address is not done for IPv4.
TEST_F(HostCacheTest, negativeAddress4) {
    // Check we have what we need.
    ASSERT_TRUE(hcptr_);
    EXPECT_TRUE(HostMgr::checkCacheBackend());
    EXPECT_EQ(0, hcptr_->size());
    EXPECT_EQ(0, hcptr_->adds_);
    ASSERT_TRUE(memptr_);

    // Create a host reservation.
    // We will not add it anywhere, just will use its values.
    HostPtr host = HostDataSourceUtils::initializeHost4("192.0.2.1",
                                                        Host::IDENT_HWADDR);
    ASSERT_TRUE(host);  // Make sure the host is generated properly.
    const IOAddress& address = host->getIPv4Reservation();

    // Do not add it to the host data source.

    // Enable negative caching.
    HostMgr::instance().setNegativeCaching(true);
    ASSERT_TRUE(HostMgr::instance().getNegativeCaching());

    // Try to get it in negative cache.
    ConstHostPtr got = HostMgr::instance().get4(host->getIPv4SubnetID(),
                                                address);
    ASSERT_FALSE(got);
    EXPECT_EQ(0, hcptr_->size());
    EXPECT_EQ(0, hcptr_->adds_);
}

// Check that negative caching by address is not done for IPv6.
TEST_F(HostCacheTest, negativeAddress6) {
    // Check we have what we need.
    ASSERT_TRUE(hcptr_);
    EXPECT_TRUE(HostMgr::checkCacheBackend());
    EXPECT_EQ(0, hcptr_->size());
    EXPECT_EQ(0, hcptr_->adds_);
    ASSERT_TRUE(memptr_);

    // Create a host reservation.
    // We will not add it anywhere, just will use its values.
    HostPtr host = HostDataSourceUtils::initializeHost6("2001:db8::1",
                                                        Host::IDENT_DUID,
                                                        false);
    ASSERT_TRUE(host);  // Make sure the host is generated properly.

    // Get the address.
    IPv6ResrvRange resrvs = host->getIPv6Reservations();
    ASSERT_EQ(1, std::distance(resrvs.first, resrvs.second));
    const IOAddress& address = resrvs.first->second.getPrefix();
    ASSERT_EQ("2001:db8::1", address.toText());

    // Do not add it to the host data source.

    // Enable negative caching.
    HostMgr::instance().setNegativeCaching(true);
    ASSERT_TRUE(HostMgr::instance().getNegativeCaching());

    // Try to get it in negative cache.
    ConstHostPtr got = HostMgr::instance().get6(host->getIPv6SubnetID(),
                                                address);
    ASSERT_FALSE(got);
    EXPECT_EQ(0, hcptr_->size());
    EXPECT_EQ(0, hcptr_->adds_);
}

/// @brief Test one backend class.
///
/// This class has one entry which is returned to any lookup.
class TestOneBackend : public BaseHostDataSource {
public:

    /// Constructor
    TestOneBackend() : value_() { }

    /// Destructor
    virtual ~TestOneBackend() { }

    ConstHostCollection getAll(const Host::IdentifierType&, const uint8_t*,
                               const size_t) const {
        return (getCollection());
    }

    ConstHostCollection getAll4(const SubnetID&) const {
        return (getCollection());
    }

    ConstHostCollection getAll6(const SubnetID&) const {
        return (getCollection());
    }

    ConstHostCollection getAllbyHostname(const std::string&) const {
        return (getCollection());
    }

    ConstHostCollection getAllbyHostname4(const std::string&,
                                          const SubnetID&) const {
        return (getCollection());
    }

    ConstHostCollection getAllbyHostname6(const std::string&,
                                          const SubnetID&) const {
        return (getCollection());
    }

    ConstHostCollection getPage4(const SubnetID&, size_t&, uint64_t,
                                 const HostPageSize&) const {
        return (getCollection());
    }

    ConstHostCollection getPage6(const SubnetID&, size_t&, uint64_t,
                                 const HostPageSize&) const {
        return (getCollection());
    }

    ConstHostCollection getPage4(size_t&, uint64_t,
                                 const HostPageSize&) const {
        return (getCollection());
    }

    ConstHostCollection getPage6(size_t&, uint64_t,
                                 const HostPageSize&) const {
        return (getCollection());
    }

    ConstHostCollection getAll4(const IOAddress&) const {
        return (getCollection());
    }

    ConstHostPtr get4(const SubnetID&, const Host::IdentifierType&,
                      const uint8_t*, const size_t) const {
        return (getOne());
    }

    ConstHostPtr get4(const SubnetID&, const IOAddress&) const {
        return (getOne());
    }

    ConstHostCollection
    getAll4(const SubnetID&, const asiolink::IOAddress&) const {
        return (getCollection());
    }

    ConstHostPtr get6(const SubnetID&, const Host::IdentifierType&,
                      const uint8_t*, const size_t) const {
        return (getOne());
    }

    ConstHostPtr get6(const IOAddress&, const uint8_t) const {
        return (getOne());
    }

    ConstHostPtr get6(const SubnetID&, const IOAddress&) const {
        return (getOne());
    }

    ConstHostCollection
    getAll6(const SubnetID&, const IOAddress&) const {
        return (getCollection());
    }

    void add(const HostPtr&) {
    }

    bool del(const SubnetID&, const IOAddress&) {
        return (false);
    }

    bool del4(const SubnetID&, const Host::IdentifierType&,
              const uint8_t*, const size_t) {
        return (false);
    }

    bool del6(const SubnetID&, const Host::IdentifierType&,
              const uint8_t*, const size_t) {
        return (false);
    }

    std::string getType() const {
        return ("one");
    }

    bool setIPReservationsUnique(const bool) {
        return (true);
    }

    /// Specific methods

    /// @brief Set the entry
    void setValue(const HostPtr& value) {
        value_ = value;
    }

protected:
    /// @brief Return collection
    ConstHostCollection getCollection() const {
        ConstHostCollection hosts;
        hosts.push_back(value_);
        return (hosts);
    }

    /// @brief Return one entry
    ConstHostPtr getOne() const {
        return(value_);
    }

    /// #brief The value
    HostPtr value_;
};

/// @brief TestOneBackend pointer type
typedef boost::shared_ptr<TestOneBackend> TestOneBackendPtr;

/// @brief Test no cache class.
///
/// This class looks like a cache but throws when insert() is called.
class TestNoCache : public MemHostDataSource, public CacheHostDataSource {
public:

    /// Destructor
    virtual ~TestNoCache() { }

    /// Override add
    void add(const HostPtr& host) {
        isc_throw(NotImplemented,
                  "add is not implemented: " << host->toText());
    }

    /// Insert throws
    size_t insert(const ConstHostPtr& host, bool overwrite) {
        isc_throw(NotImplemented,
                  "insert is not implemented: " << host->toText()
                  << " with overwrite: " << overwrite);
    }

    /// Remove throws
    bool remove(const HostPtr& host) {
        isc_throw(NotImplemented,
                  "remove is not implemented: " << host->toText());
    }

    /// Flush
    void flush(size_t) {
        isc_throw(NotImplemented, "flush is not implemented");
    }

    /// Size
    size_t size() const {
        return (0);
    }

    /// Capacity
    size_t capacity() const {
        return (0);
    }

    /// Type
    string getType() const {
        return ("nocache");
    }
};

/// @brief TestNoCache pointer type
typedef boost::shared_ptr<TestNoCache> TestNoCachePtr;

/// @brief Test fixture for testing generic negative cache handling.
class NegativeCacheTest : public ::testing::Test {
public:

    /// @brief Constructor.
    NegativeCacheTest() {
        HostMgr::create();

        // No cache.
        ncptr_.reset(new TestNoCache());
        auto nocacheFactory = [this](const DatabaseConnection::ParameterMap&) {
            return (ncptr_);
        };
        HostDataSourceFactory::registerFactory("nocache", nocacheFactory);
        HostMgr::addBackend("type=nocache");

        // One backend.
        obptr_.reset(new TestOneBackend());
        auto oneFactory = [this](const DatabaseConnection::ParameterMap&) {
            return (obptr_);
        };
        HostDataSourceFactory::registerFactory("one", oneFactory);
        HostMgr::addBackend("type=one");
    }

    /// @brief Destructor.
    virtual ~NegativeCacheTest() {
        HostDataSourceFactory::deregisterFactory("one");
        HostDataSourceFactory::deregisterFactory("nocache");
    }

    /// @brief Test one backend.
    TestOneBackendPtr obptr_;

    /// @brief Test no cache.
    TestNoCachePtr ncptr_;

    /// Test methods

    /// @brief Set negative caching.
    void setNegativeCaching() {
        ASSERT_TRUE(HostMgr::instance().checkCacheBackend());
        ASSERT_FALSE(HostMgr::instance().getNegativeCaching());
        HostMgr::instance().setNegativeCaching(true);
        ASSERT_TRUE(HostMgr::instance().getNegativeCaching());
    }

    void testGetAll();
    void testGet4();
    void testGet6();
};

/// Verify that getAll* methods ignore negative caching.
void NegativeCacheTest::testGetAll() {
    // Create a host reservation.
    HostPtr host = HostDataSourceUtils::initializeHost4("192.0.2.1",
                                                        Host::IDENT_HWADDR);
    ASSERT_TRUE(host);
    ASSERT_TRUE(host->getHWAddress());
    ASSERT_EQ("192.0.2.1", host->getIPv4Reservation().toText());

    // Make it a negative cached entry.
    host->setNegative(true);
    ASSERT_TRUE(host->getNegative());

    // Set the backend with it.
    obptr_->setValue(host);

    // Verifies getAll* return a collection with it.
    ConstHostCollection hosts;
    ASSERT_NO_THROW(hosts =
        HostMgr::instance().getAll(host->getIdentifierType(),
                                   &host->getIdentifier()[0],
                                   host->getIdentifier().size()));
    ASSERT_EQ(1, hosts.size());
    EXPECT_EQ(host, hosts[0]);

    ASSERT_NO_THROW(hosts =
                    HostMgr::instance().getAll4(host->getIPv4Reservation()));
    ASSERT_EQ(1, hosts.size());
    EXPECT_EQ(host, hosts[0]);
}

/// Verify that get4* methods handle negative caching.
void NegativeCacheTest::testGet4() {
    // Create a host reservation.
    HostPtr host = HostDataSourceUtils::initializeHost4("192.0.2.1",
                                                        Host::IDENT_HWADDR);
    ASSERT_TRUE(host);
    ASSERT_TRUE(host->getHWAddress());
    ASSERT_EQ("192.0.2.1", host->getIPv4Reservation().toText());

    // Make it a negative cached entry.
    host->setNegative(true);
    ASSERT_TRUE(host->getNegative());

    // Set the backend with it.
    obptr_->setValue(host);

    // Verifies get4 overloads return a null pointer.
    ConstHostPtr got;
    ASSERT_NO_THROW(got =
        HostMgr::instance().get4(host->getIPv4SubnetID(),
                                 host->getIdentifierType(),
                                 &host->getIdentifier()[0],
                                 host->getIdentifier().size()));
    EXPECT_FALSE(got);

    ASSERT_NO_THROW(got =
                    HostMgr::instance().get4(host->getIPv4SubnetID(),
                                             host->getIPv4Reservation()));
    EXPECT_FALSE(got);

    // Only getAny returns the negative cached entry.
    ASSERT_NO_THROW(got =
                    HostMgr::instance().get4Any(host->getIPv4SubnetID(),
                                                host->getIdentifierType(),
                                                &host->getIdentifier()[0],
                                                host->getIdentifier().size()));
    EXPECT_TRUE(got);
    EXPECT_EQ(host, got);
}

/// Verify that get6* methods handle negative caching.
void NegativeCacheTest::testGet6() {
    // Create a host reservation.
    HostPtr host = HostDataSourceUtils::initializeHost6("2001:db8::1",
                                                        Host::IDENT_DUID,
                                                        false);
    ASSERT_TRUE(host);
    ASSERT_TRUE(host->getDuid());

    // Get the address.
    IPv6ResrvRange resrvs = host->getIPv6Reservations();
    ASSERT_EQ(1, std::distance(resrvs.first, resrvs.second));
    const IOAddress& address = resrvs.first->second.getPrefix();
    ASSERT_EQ("2001:db8::1", address.toText());

    // Make it a negative cached entry.
    host->setNegative(true);
    ASSERT_TRUE(host->getNegative());

    // Set the backend with it.
    obptr_->setValue(host);

    // Verifies get6 overloads return a null pointer.
    ConstHostPtr got;
    ASSERT_NO_THROW(got =
                    HostMgr::instance().get6(host->getIPv6SubnetID(),
                                             host->getIdentifierType(),
                                             &host->getIdentifier()[0],
                                             host->getIdentifier().size()));
    EXPECT_FALSE(got);

    ASSERT_NO_THROW(got = HostMgr::instance().get6(address, 128));
    EXPECT_FALSE(got);

    ASSERT_NO_THROW(got =
                    HostMgr::instance().get6(host->getIPv6SubnetID(),
                                             address));
    EXPECT_FALSE(got);

    // Only getAny returns the negative cached entry.
    ASSERT_NO_THROW(got =
                    HostMgr::instance().get6Any(host->getIPv6SubnetID(),
                                                host->getIdentifierType(),
                                                &host->getIdentifier()[0],
                                                host->getIdentifier().size()));
    EXPECT_TRUE(got);
    EXPECT_EQ(host, got);
}

/// Verify that getAll* methods ignore negative caching.
TEST_F(NegativeCacheTest, getAll) {
    testGetAll();
}

/// Verify that get4* methods handle negative caching.
TEST_F(NegativeCacheTest, get4) {
    testGet4();
}

/// Verify that get6* methods handle negative caching.
TEST_F(NegativeCacheTest, get6) {
    testGet6();
}

/// Verify that getAll* methods behavior does not change with
/// host manager negative caching.
TEST_F(NegativeCacheTest, getAllwithNegativeCaching) {
    setNegativeCaching();
    testGetAll();
}

/// Verify that get4* methods behavior does not change with
/// host manager negative caching.
TEST_F(NegativeCacheTest, get4withNegativeCaching) {
    setNegativeCaching();
    testGet4();
}

/// Verify that get6* methods behavior does not change with
/// host manager negative caching.
TEST_F(NegativeCacheTest, get6withNegativeCaching) {
    setNegativeCaching();
    testGet6();
}

}; // end of anonymous namespace