memory_client_unittest.cc 24.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Copyright (C) 2012  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 <exceptions/exceptions.h>

#include <util/memory_segment_local.h>

#include <dns/name.h>
#include <dns/rrclass.h>
21
22
23
24
25
26
27
#include <dns/masterload.h>
#include <dns/nsec3hash.h>
#include <dns/rdata.h>
#include <dns/rdataclass.h>
#include <dns/rrsetlist.h>
#include <dns/rrttl.h>
#include <dns/masterload.h>
28
29

#include <datasrc/result.h>
30
#include <datasrc/data_source.h>
31
32
33
34
#include <datasrc/memory/zone_data.h>
#include <datasrc/memory/zone_table.h>
#include <datasrc/memory/memory_client.h>

35
36
#include <testutils/dnsmessage_test.h>

37
38
#include "memory_segment_test.h"

39
40
41
42
43
#include <gtest/gtest.h>

#include <new>                  // for bad_alloc

using namespace isc::dns;
44
using namespace isc::dns::rdata;
45
46
using namespace isc::datasrc;
using namespace isc::datasrc::memory;
47
using namespace isc::testutils;
48
49

namespace {
50

51
const char* rrset_data[] = {
52
53
54
55
    "example.org. 3600 IN SOA ns1.example.org. bugs.x.w.example.org. "
    "68 3600 300 3600000 3600",
    "a.example.org. 3600 IN A 192.168.0.1\n" // RRset containing 2 RRs
    "a.example.org. 3600 IN A 192.168.0.2",
56
57
    "a.example.org. 3600 IN RRSIG A 5 3 3600 20150420235959 20051021000000 "
    "40430 example.org. FAKEFAKE",
58
59
60
61
62
63
    "a.example.org. 3600 IN MX 10 mail.example.org.",
    "a.example.org. 3600 IN RRSIG MX 5 3 3600 20150420235959 20051021000000 "
    "40430 example.org. FAKEFAKEFAKE",
    NULL
};

64
// RRsets that emulate the "separate RRs" mode.
65
const char* rrset_data_separated[] = {
66
67
68
69
70
71
72
73
74
    "example.org. 3600 IN SOA ns1.example.org. bugs.x.w.example.org. "
    "68 3600 300 3600000 3600",
    "a.example.org. 3600 IN A 192.168.0.1", // these two belong to the same
    "a.example.org. 3600 IN A 192.168.0.2", // RRset, but are separated.
    NULL
};

// Similar to the previous one, but with separated RRSIGs
const char* rrset_data_sigseparated[] = {
75
76
77
    "example.org. 3600 IN SOA ns1.example.org. bugs.x.w.example.org. "
    "68 3600 300 3600000 3600",
    "a.example.org. 3600 IN A 192.168.0.1",
78
79
80
81
    "a.example.org. 3600 IN RRSIG A 5 3 3600 20150420235959 20051021000000 "
    "40430 example.org. FAKEFAKE",
    "a.example.org. 3600 IN RRSIG A 5 3 3600 20150420235959 20051021000000 "
    "53535 example.org. FAKEFAKE",
82
83
84
85
86
    NULL
};

class MockIterator : public ZoneIterator {
private:
87
88
89
    MockIterator(const char** rrset_data_ptr, bool pass_empty_rrsig) :
        rrset_data_ptr_(rrset_data_ptr),
        pass_empty_rrsig_(pass_empty_rrsig)
90
91
92
93
    {
    }

    const char** rrset_data_ptr_;
94
95
96
    // If true, emulate an unexpected bogus case where an RRSIG RRset is
    // returned without the RDATA.  For brevity allow tests tweak it directly.
    bool pass_empty_rrsig_;
97
98
99
100
101
102
103

public:
    virtual ConstRRsetPtr getNextRRset() {
        if (*rrset_data_ptr_ == NULL) {
             return (ConstRRsetPtr());
        }

104
105
        ConstRRsetPtr result(textToRRset(*rrset_data_ptr_,
                                         RRClass::IN(), Name("example.org")));
106
107
108
109
        if (pass_empty_rrsig_ && result->getType() == RRType::RRSIG()) {
            result.reset(new RRset(result->getName(), result->getClass(),
                                   result->getType(), result->getTTL()));
        }
110
        ++rrset_data_ptr_;
111
112
113
114
115
116
117
118

        return (result);
    }

    virtual ConstRRsetPtr getSOA() const {
        isc_throw(isc::NotImplemented, "Not implemented");
    }

119
120
121
122
123
    static ZoneIteratorPtr makeIterator(const char** rrset_data_ptr,
                                        bool pass_empty_rrsig = false)
    {
        return (ZoneIteratorPtr(new MockIterator(rrset_data_ptr,
                                                 pass_empty_rrsig)));
124
125
126
    }
};

127
128
129
class MemoryClientTest : public ::testing::Test {
protected:
    MemoryClientTest() : zclass_(RRClass::IN()),
130
                         client_(new InMemoryClient(mem_sgmt_, zclass_))
131
132
133
134
135
136
137
138
139
140
141
142
    {}
    ~MemoryClientTest() {
        if (client_ != NULL) {
            delete client_;
        }
    }
    void TearDown() {
        delete client_;
        client_ = NULL;
        EXPECT_TRUE(mem_sgmt_.allMemoryDeallocated()); // catch any leak here.
    }
    const RRClass zclass_;
143
    test::MemorySegmentTest mem_sgmt_;
144
145
146
    InMemoryClient* client_;
};

147
TEST_F(MemoryClientTest, loadRRsetDoesntMatchOrigin) {
148
    // Attempting to load example.org to example.com zone should result
149
    // in an exception.
150
151
152
153
    EXPECT_THROW(client_->load(Name("example.com"),
                               TEST_DATA_DIR "/example.org-empty.zone"),
                 MasterLoadError);
}
154

155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
TEST_F(MemoryClientTest, loadErrorsInParsingZoneMustNotLeak1) {
    // Attempting to load broken example.org zone should result in an
    // exception. This should not leak ZoneData and other such
    // allocations.
    EXPECT_THROW(client_->load(Name("example.org"),
                               TEST_DATA_DIR "/example.org-broken1.zone"),
                 MasterLoadError);
    // Teardown checks for memory segment leaks
}

TEST_F(MemoryClientTest, loadErrorsInParsingZoneMustNotLeak2) {
    // Attempting to load broken example.org zone should result in an
    // exception. This should not leak ZoneData and other such
    // allocations.
    EXPECT_THROW(client_->load(Name("example.org"),
                               TEST_DATA_DIR "/example.org-broken2.zone"),
                 MasterLoadError);
    // Teardown checks for memory segment leaks
}

175
176
177
178
179
180
181
TEST_F(MemoryClientTest, loadNonExistentZoneFile) {
    EXPECT_THROW(client_->load(Name("example.org"),
                               TEST_DATA_DIR "/somerandomfilename"),
                 MasterLoadError);
    // Teardown checks for memory segment leaks
}

182
183
184
185
TEST_F(MemoryClientTest, loadEmptyZoneFileThrows) {
    // When an empty zone file is loaded, the origin doesn't even have
    // an SOA RR. This condition should be avoided, and hence load()
    // should throw when an empty zone is loaded.
186
187
188

    EXPECT_EQ(0, client_->getZoneCount());

189
190
191
    EXPECT_THROW(client_->load(Name("."),
                               TEST_DATA_DIR "/empty.zone"),
                 InMemoryClient::EmptyZone);
192
193
194

    EXPECT_EQ(0, client_->getZoneCount());

195
196
197
    // Teardown checks for memory segment leaks
}

198
199
200
TEST_F(MemoryClientTest, load) {
    // This is a simple load check for a "full" and correct zone that
    // should not result in any exceptions.
201
202
    client_->load(Name("example.org"),
                  TEST_DATA_DIR "/example.org.zone");
203
204
205
206
207
    const ZoneData* zone_data =
        client_->findZoneData(Name("example.org"));
    ASSERT_NE(static_cast<const ZoneData*>(NULL), zone_data);
    EXPECT_FALSE(zone_data->isSigned());
    EXPECT_FALSE(zone_data->isNSEC3Signed());
208
209
}

210
TEST_F(MemoryClientTest, loadFromIterator) {
211
212
    client_->load(Name("example.org"),
                  *MockIterator::makeIterator(rrset_data));
213
214
215
216
217
218
219
220
221
222
223
224

    ZoneIteratorPtr iterator(client_->getIterator(Name("example.org")));

    // First we have the SOA
    ConstRRsetPtr rrset(iterator->getNextRRset());
    EXPECT_TRUE(rrset);
    EXPECT_EQ(RRType::SOA(), rrset->getType());

    // RRType::MX() RRset
    rrset = iterator->getNextRRset();
    EXPECT_TRUE(rrset);
    EXPECT_EQ(RRType::MX(), rrset->getType());
225
    EXPECT_EQ(1, rrset->getRRsigDataCount()); // this RRset is signed
226
227
228
229
230

    // RRType::A() RRset
    rrset = iterator->getNextRRset();
    EXPECT_TRUE(rrset);
    EXPECT_EQ(RRType::A(), rrset->getType());
231
    EXPECT_EQ(1, rrset->getRRsigDataCount()); // also signed
232
233
234
235
236
237

    // There's nothing else in this iterator
    EXPECT_EQ(ConstRRsetPtr(), iterator->getNextRRset());

    // Iterating past the end should result in an exception
    EXPECT_THROW(iterator->getNextRRset(), isc::Unexpected);
238
239
240
241
242

    // Loading the zone with an iterator separating RRs of the same RRset
    // will fail because the resulting sequence doesn't meet assumptions of
    // the (current) in-memory implementation.
    EXPECT_THROW(client_->load(Name("example.org"),
243
244
245
246
247
248
249
250
                               *MockIterator::makeIterator(
                                   rrset_data_separated)),
                 InMemoryClient::AddError);

    // Similar to the previous case, but with separated RRSIGs.
    EXPECT_THROW(client_->load(Name("example.org"),
                               *MockIterator::makeIterator(
                                   rrset_data_sigseparated)),
251
                 InMemoryClient::AddError);
252
253
254
255
256

    // Emulating bogus iterator implementation that passes empty RRSIGs.
    EXPECT_THROW(client_->load(Name("example.org"),
                               *MockIterator::makeIterator(rrset_data, true)),
                 isc::Unexpected);
257
258
}

259
260
261
262
263
264
265
266
267
268
269
270
TEST_F(MemoryClientTest, loadMemoryAllocationFailures) {
    // Just to check that things get cleaned up

    for (int i = 1; i < 16; i++) {
        mem_sgmt_.setThrowCount(i);
        EXPECT_THROW(client_->load(Name("example.org"),
                                   TEST_DATA_DIR "/example.org.zone"),
                     std::bad_alloc);
    }
    // Teardown checks for memory segment leaks
}

271
272
273
TEST_F(MemoryClientTest, loadNSEC3Signed) {
    client_->load(Name("example.org"),
                  TEST_DATA_DIR "/example.org-nsec3-signed.zone");
274
275
276
277
278
    const ZoneData* zone_data =
        client_->findZoneData(Name("example.org"));
    ASSERT_NE(static_cast<const ZoneData*>(NULL), zone_data);
    EXPECT_TRUE(zone_data->isSigned());
    EXPECT_TRUE(zone_data->isNSEC3Signed());
279
280
}

281
282
283
284
285
TEST_F(MemoryClientTest, loadNSEC3EmptySalt) {
    // Load NSEC3 with empty ("-") salt. This should not throw or crash
    // or anything.
    client_->load(Name("example.org"),
                  TEST_DATA_DIR "/example.org-nsec3-empty-salt.zone");
286
287
288
289
290
    const ZoneData* zone_data =
        client_->findZoneData(Name("example.org"));
    ASSERT_NE(static_cast<const ZoneData*>(NULL), zone_data);
    EXPECT_TRUE(zone_data->isSigned());
    EXPECT_TRUE(zone_data->isNSEC3Signed());
291
292
293
294
295
}

TEST_F(MemoryClientTest, loadNSEC3SignedNoParam) {
    client_->load(Name("example.org"),
                  TEST_DATA_DIR "/example.org-nsec3-signed-no-param.zone");
296
297
298
299
300
    const ZoneData* zone_data =
        client_->findZoneData(Name("example.org"));
    ASSERT_NE(static_cast<const ZoneData*>(NULL), zone_data);
    EXPECT_TRUE(zone_data->isSigned());
    EXPECT_TRUE(zone_data->isNSEC3Signed());
301
302
}

Mukund Sivaraman's avatar
Mukund Sivaraman committed
303
304
305
306
307
308
TEST_F(MemoryClientTest, loadReloadZone) {
    // Because we reload the same zone, also check that the zone count
    // doesn't increase.
    EXPECT_EQ(0, client_->getZoneCount());

    client_->load(Name("example.org"),
Mukund Sivaraman's avatar
Mukund Sivaraman committed
309
                  TEST_DATA_DIR "/example.org-empty.zone");
Mukund Sivaraman's avatar
Mukund Sivaraman committed
310
311
    EXPECT_EQ(1, client_->getZoneCount());

Mukund Sivaraman's avatar
Mukund Sivaraman committed
312
313
    // Reload zone with same data

Mukund Sivaraman's avatar
Mukund Sivaraman committed
314
    client_->load(Name("example.org"),
Mukund Sivaraman's avatar
Mukund Sivaraman committed
315
                  client_->getFileName(Name("example.org")));
Mukund Sivaraman's avatar
Mukund Sivaraman committed
316
317
    EXPECT_EQ(1, client_->getZoneCount());

318
319
320
    const ZoneData* zone_data =
        client_->findZoneData(Name("example.org"));
    EXPECT_NE(static_cast<const ZoneData*>(NULL), zone_data);
321
322

    /* Check SOA */
323
    const ZoneNode* node = zone_data->getOriginNode();
324
325
326
327
328
329
330
331
332
333
    EXPECT_NE(static_cast<const ZoneNode*>(NULL), node);

    const RdataSet* set = node->getData();
    EXPECT_NE(static_cast<const RdataSet*>(NULL), set);
    EXPECT_EQ(RRType::SOA(), set->type);

    set = set->getNext();
    EXPECT_EQ(static_cast<const RdataSet*>(NULL), set);

    /* Check ns1.example.org */
334
    const ZoneTree& tree = zone_data->getZoneTree();
335
336
337
338
339
    ZoneTree::Result zresult(tree.find(Name("ns1.example.org"), &node));
    EXPECT_NE(ZoneTree::EXACTMATCH, zresult);

    // Reload zone with different data

Mukund Sivaraman's avatar
Mukund Sivaraman committed
340
    client_->load(Name("example.org"),
Mukund Sivaraman's avatar
Mukund Sivaraman committed
341
                  TEST_DATA_DIR "/example.org-rrsigs.zone");
Mukund Sivaraman's avatar
Mukund Sivaraman committed
342
343
    EXPECT_EQ(1, client_->getZoneCount());

344
345
    zone_data = client_->findZoneData(Name("example.org"));
    EXPECT_NE(static_cast<const ZoneData*>(NULL), zone_data);
346
347

    /* Check SOA */
348
    node = zone_data->getOriginNode();
349
350
351
352
353
354
355
356
357
358
    EXPECT_NE(static_cast<const ZoneNode*>(NULL), node);

    set = node->getData();
    EXPECT_NE(static_cast<const RdataSet*>(NULL), set);
    EXPECT_EQ(RRType::SOA(), set->type);

    set = set->getNext();
    EXPECT_EQ(static_cast<const RdataSet*>(NULL), set);

    /* Check ns1.example.org */
359
    const ZoneTree& tree2 = zone_data->getZoneTree();
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
    ZoneTree::Result zresult2(tree2.find(Name("ns1.example.org"), &node));
    EXPECT_EQ(ZoneTree::EXACTMATCH, zresult2);
    EXPECT_NE(static_cast<const ZoneNode*>(NULL), node);

    set = node->getData();
    EXPECT_NE(static_cast<const RdataSet*>(NULL), set);
    EXPECT_EQ(RRType::AAAA(), set->type);

    set = set->getNext();
    EXPECT_NE(static_cast<const RdataSet*>(NULL), set);
    EXPECT_EQ(RRType::A(), set->type);

    set = set->getNext();
    EXPECT_EQ(static_cast<const RdataSet*>(NULL), set);

Mukund Sivaraman's avatar
Mukund Sivaraman committed
375
376
377
    // Teardown checks for memory segment leaks
}

378
379
380
381
382
383
384
385
386
387
TEST_F(MemoryClientTest, loadDuplicateType) {
    // This should not result in any exceptions:
    client_->load(Name("example.org"),
                  TEST_DATA_DIR "/example.org-duplicate-type.zone");

    // This should throw:
    EXPECT_THROW(client_->load(Name("example.org"),
                               TEST_DATA_DIR
                               "/example.org-duplicate-type-bad.zone"),
                 InMemoryClient::AddError);
388
389
390
    // Teardown checks for memory segment leaks
}

Mukund Sivaraman's avatar
Mukund Sivaraman committed
391
TEST_F(MemoryClientTest, loadMultipleCNAMEThrows) {
392
393
394
395
396
    // Multiple CNAME RRs should throw.
    EXPECT_THROW(client_->load(Name("example.org"),
                               TEST_DATA_DIR
                               "/example.org-multiple-cname.zone"),
                 InMemoryClient::AddError);
397
398
399
    // Teardown checks for memory segment leaks
}

Mukund Sivaraman's avatar
Mukund Sivaraman committed
400
TEST_F(MemoryClientTest, loadMultipleDNAMEThrows) {
401
402
403
404
405
    // Multiple DNAME RRs should throw.
    EXPECT_THROW(client_->load(Name("example.org"),
                               TEST_DATA_DIR
                               "/example.org-multiple-dname.zone"),
                 InMemoryClient::AddError);
406
407
408
    // Teardown checks for memory segment leaks
}

Mukund Sivaraman's avatar
Mukund Sivaraman committed
409
TEST_F(MemoryClientTest, loadMultipleNSEC3Throws) {
410
411
412
413
414
415
416
417
    // Multiple NSEC3 RRs should throw.
    EXPECT_THROW(client_->load(Name("example.org"),
                               TEST_DATA_DIR
                               "/example.org-multiple-nsec3.zone"),
                 InMemoryClient::AddError);
    // Teardown checks for memory segment leaks
}

Mukund Sivaraman's avatar
Mukund Sivaraman committed
418
TEST_F(MemoryClientTest, loadMultipleNSEC3PARAMThrows) {
419
420
421
422
423
    // Multiple NSEC3PARAM RRs should throw.
    EXPECT_THROW(client_->load(Name("example.org"),
                               TEST_DATA_DIR
                               "/example.org-multiple-nsec3param.zone"),
                 InMemoryClient::AddError);
424
425
426
    // Teardown checks for memory segment leaks
}

427
428
429
430
431
432
433
434
435
TEST_F(MemoryClientTest, loadOutOfZoneThrows) {
    // Out of zone names should throw.
    EXPECT_THROW(client_->load(Name("example.org"),
                               TEST_DATA_DIR
                               "/example.org-out-of-zone.zone"),
                 MasterLoadError);
    // Teardown checks for memory segment leaks
}

436
437
438
439
440
441
442
443
444
TEST_F(MemoryClientTest, loadWildcardNSThrows) {
    // Wildcard NS names should throw
    EXPECT_THROW(client_->load(Name("example.org"),
                               TEST_DATA_DIR
                               "/example.org-wildcard-ns.zone"),
                 InMemoryClient::AddError);
    // Teardown checks for memory segment leaks
}

445
TEST_F(MemoryClientTest, loadWildcardDNAMEThrows) {
446
    // Wildcard DNAME names should throw
447
448
449
450
451
452
453
    EXPECT_THROW(client_->load(Name("example.org"),
                               TEST_DATA_DIR
                               "/example.org-wildcard-dname.zone"),
                 InMemoryClient::AddError);
    // Teardown checks for memory segment leaks
}

454
455
456
457
458
459
460
461
462
TEST_F(MemoryClientTest, loadWildcardNSEC3Throws) {
    // Wildcard NSEC3 names should throw
    EXPECT_THROW(client_->load(Name("example.org"),
                               TEST_DATA_DIR
                               "/example.org-wildcard-nsec3.zone"),
                 InMemoryClient::AddError);
    // Teardown checks for memory segment leaks
}

463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
TEST_F(MemoryClientTest, loadNSEC3WithFewerLabelsThrows) {
    // NSEC3 names with labels != (origin_labels + 1) should throw
    EXPECT_THROW(client_->load(Name("example.org"),
                               TEST_DATA_DIR
                               "/example.org-nsec3-fewer-labels.zone"),
                 InMemoryClient::AddError);
    // Teardown checks for memory segment leaks
}

TEST_F(MemoryClientTest, loadNSEC3WithMoreLabelsThrows) {
    // NSEC3 names with labels != (origin_labels + 1) should throw
    EXPECT_THROW(client_->load(Name("example.org"),
                               TEST_DATA_DIR
                               "/example.org-nsec3-more-labels.zone"),
                 InMemoryClient::AddError);
    // Teardown checks for memory segment leaks
}

481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
TEST_F(MemoryClientTest, loadCNAMEAndNotNSECThrows) {
    // CNAME and not NSEC should throw
    EXPECT_THROW(client_->load(Name("example.org"),
                               TEST_DATA_DIR
                               "/example.org-cname-and-not-nsec-1.zone"),
                 InMemoryClient::AddError);

    EXPECT_THROW(client_->load(Name("example.org"),
                               TEST_DATA_DIR
                               "/example.org-cname-and-not-nsec-2.zone"),
                 InMemoryClient::AddError);

    // Teardown checks for memory segment leaks
}

Mukund Sivaraman's avatar
Mukund Sivaraman committed
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
TEST_F(MemoryClientTest, loadDNAMEAndNSApex1) {
    // DNAME + NS (apex) is OK
    client_->load(Name("example.org"),
                  TEST_DATA_DIR
                  "/example.org-dname-ns-apex-1.zone");
    // Teardown checks for memory segment leaks
}

TEST_F(MemoryClientTest, loadDNAMEAndNSApex2) {
    // DNAME + NS (apex) is OK (reverse order)
    client_->load(Name("example.org"),
                  TEST_DATA_DIR
                  "/example.org-dname-ns-apex-2.zone");
    // Teardown checks for memory segment leaks
}

TEST_F(MemoryClientTest, loadDNAMEAndNSNonApex1) {
    // DNAME + NS (non-apex) must throw
    EXPECT_THROW(client_->load(Name("example.org"),
                               TEST_DATA_DIR
                               "/example.org-dname-ns-nonapex-1.zone"),
                 InMemoryClient::AddError);
    // Teardown checks for memory segment leaks
}

TEST_F(MemoryClientTest, loadDNAMEAndNSNonApex2) {
    // DNAME + NS (non-apex) must throw (reverse order)
    EXPECT_THROW(client_->load(Name("example.org"),
                               TEST_DATA_DIR
                               "/example.org-dname-ns-nonapex-2.zone"),
                 InMemoryClient::AddError);
    // Teardown checks for memory segment leaks
}

530
531
532
TEST_F(MemoryClientTest, loadRRSIGFollowsNothing) {
    // This causes the situation where an RRSIG is added without a covered
    // RRset.  Such cases are currently rejected.
533
534
535
536
537
538
539
    EXPECT_THROW(client_->load(Name("example.org"),
                               TEST_DATA_DIR
                               "/example.org-rrsig-follows-nothing.zone"),
                 InMemoryClient::AddError);
    // Teardown checks for memory segment leaks
}

540
TEST_F(MemoryClientTest, loadRRSIGs) {
Mukund Sivaraman's avatar
Mukund Sivaraman committed
541
    client_->load(Name("example.org"),
Mukund Sivaraman's avatar
Mukund Sivaraman committed
542
                  TEST_DATA_DIR "/example.org-rrsigs.zone");
543
544
545
    EXPECT_EQ(1, client_->getZoneCount());
}

546
547
TEST_F(MemoryClientTest, getZoneCount) {
    EXPECT_EQ(0, client_->getZoneCount());
548
549
550
551
    client_->load(Name("example.org"), TEST_DATA_DIR "/example.org-empty.zone");
    EXPECT_EQ(1, client_->getZoneCount());
}

552
553
554
555
556
557
558
559
560
561
562
TEST_F(MemoryClientTest, getFileNameForNonExistentZone) {
    // Zone "example.org." doesn't exist
    EXPECT_TRUE(client_->getFileName(Name("example.org.")).empty());
}

TEST_F(MemoryClientTest, getFileName) {
    client_->load(Name("example.org"), TEST_DATA_DIR "/example.org-empty.zone");
    EXPECT_EQ(TEST_DATA_DIR "/example.org-empty.zone",
              client_->getFileName(Name("example.org")));
}

Mukund Sivaraman's avatar
Mukund Sivaraman committed
563
TEST_F(MemoryClientTest, getIteratorForNonExistentZone) {
564
565
    // Zone "." doesn't exist
    EXPECT_THROW(client_->getIterator(Name(".")), DataSourceError);
566
}
567

568
569
570
TEST_F(MemoryClientTest, getIterator) {
    client_->load(Name("example.org"), TEST_DATA_DIR "/example.org-empty.zone");
    ZoneIteratorPtr iterator(client_->getIterator(Name("example.org")));
571
572
573
574
575
576

    // First we have the SOA
    ConstRRsetPtr rrset_soa(iterator->getNextRRset());
    EXPECT_TRUE(rrset_soa);
    EXPECT_EQ(RRType::SOA(), rrset_soa->getType());

Mukund Sivaraman's avatar
Mukund Sivaraman committed
577
    // There's nothing else in this iterator
578
    EXPECT_EQ(ConstRRsetPtr(), iterator->getNextRRset());
579
580
581

    // Iterating past the end should result in an exception
    EXPECT_THROW(iterator->getNextRRset(), isc::Unexpected);
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
TEST_F(MemoryClientTest, getIteratorSeparateRRs) {
    client_->load(Name("example.org"),
                  TEST_DATA_DIR "/example.org-multiple.zone");

    // separate_rrs = false
    ZoneIteratorPtr iterator(client_->getIterator(Name("example.org")));

    // First we have the SOA
    ConstRRsetPtr rrset(iterator->getNextRRset());
    EXPECT_TRUE(rrset);
    EXPECT_EQ(RRType::SOA(), rrset->getType());

    // Only one RRType::A() RRset
    rrset = iterator->getNextRRset();
    EXPECT_TRUE(rrset);
    EXPECT_EQ(RRType::A(), rrset->getType());

    // There's nothing else in this zone
    EXPECT_EQ(ConstRRsetPtr(), iterator->getNextRRset());


    // separate_rrs = true
    ZoneIteratorPtr iterator2(client_->getIterator(Name("example.org"), true));

    // First we have the SOA
    rrset = iterator2->getNextRRset();
    EXPECT_TRUE(rrset);
    EXPECT_EQ(RRType::SOA(), rrset->getType());

    // First RRType::A() RRset
    rrset = iterator2->getNextRRset();
    EXPECT_TRUE(rrset);
    EXPECT_EQ(RRType::A(), rrset->getType());

    // Second RRType::A() RRset
    rrset = iterator2->getNextRRset();
    EXPECT_TRUE(rrset);
    EXPECT_EQ(RRType::A(), rrset->getType());

    // There's nothing else in this iterator
    EXPECT_EQ(ConstRRsetPtr(), iterator2->getNextRRset());
}

627
628
629
630
631
632
633
634
TEST_F(MemoryClientTest, getIteratorGetSOAThrowsNotImplemented) {
    client_->load(Name("example.org"), TEST_DATA_DIR "/example.org-empty.zone");
    ZoneIteratorPtr iterator(client_->getIterator(Name("example.org")));

    // This method is not implemented.
    EXPECT_THROW(iterator->getSOA(), isc::NotImplemented);
}

635
TEST_F(MemoryClientTest, findZoneData) {
636
637
638
    client_->load(Name("example.org"),
                  TEST_DATA_DIR "/example.org-rrsigs.zone");

639
640
    const ZoneData* zone_data = client_->findZoneData(Name("example.com"));
    EXPECT_EQ(static_cast<const ZoneData*>(NULL), zone_data);
641

642
643
    zone_data = client_->findZoneData(Name("example.org"));
    EXPECT_NE(static_cast<const ZoneData*>(NULL), zone_data);
644
645

    /* Check SOA */
646
    const ZoneNode* node = zone_data->getOriginNode();
647
648
649
650
651
652
653
654
655
656
    EXPECT_NE(static_cast<const ZoneNode*>(NULL), node);

    const RdataSet* set = node->getData();
    EXPECT_NE(static_cast<const RdataSet*>(NULL), set);
    EXPECT_EQ(RRType::SOA(), set->type);

    set = set->getNext();
    EXPECT_EQ(static_cast<const RdataSet*>(NULL), set);

    /* Check ns1.example.org */
657
    const ZoneTree& tree = zone_data->getZoneTree();
658
659
660
661
662
663
664
665
666
667
668
669
670
671
    ZoneTree::Result result3(tree.find(Name("ns1.example.org"), &node));
    EXPECT_EQ(ZoneTree::EXACTMATCH, result3);
    EXPECT_NE(static_cast<const ZoneNode*>(NULL), node);

    set = node->getData();
    EXPECT_NE(static_cast<const RdataSet*>(NULL), set);
    EXPECT_EQ(RRType::AAAA(), set->type);

    set = set->getNext();
    EXPECT_NE(static_cast<const RdataSet*>(NULL), set);
    EXPECT_EQ(RRType::A(), set->type);

    set = set->getNext();
    EXPECT_EQ(static_cast<const RdataSet*>(NULL), set);
672
673
}

674
TEST_F(MemoryClientTest, getUpdaterThrowsNotImplemented) {
675
    // This method is not implemented.
676
    EXPECT_THROW(client_->getUpdater(Name("."), false, false),
Mukund Sivaraman's avatar
Mukund Sivaraman committed
677
                 isc::NotImplemented);
678
}
679
680

TEST_F(MemoryClientTest, getJournalReaderNotImplemented) {
681
    // This method is not implemented.
682
    EXPECT_THROW(client_->getJournalReader(Name("."), 0, 0),
Mukund Sivaraman's avatar
Mukund Sivaraman committed
683
                 isc::NotImplemented);
684
}
685
686
687
688
689
690
691
692
693
694
695
696

// TODO (upon merge of #2268): Re-add (and modify not to need
// InMemoryClient::add) the tests removed in
// 7a628baa1a158b5837d6f383e10b30542d2ac59b. Maybe some of them
// are really not needed.
//
// * MemoryClientTest::loadRRSIGsRdataMixedCoveredTypes
// * MemoryClientTest::addRRsetToNonExistentZoneThrows
// * MemoryClientTest::addOutOfZoneThrows
// * MemoryClientTest::addNullRRsetThrows
// * MemoryClientTest::addEmptyRRsetThrows
// * MemoryClientTest::add
697
}