memory_client_unittest.cc 25.3 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
39
40
41
#include <gtest/gtest.h>

#include <new>                  // for bad_alloc

using namespace isc::dns;
42
using namespace isc::dns::rdata;
43
44
using namespace isc::datasrc;
using namespace isc::datasrc::memory;
45
using namespace isc::testutils;
46
47

namespace {
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Memory segment specified for tests.  It normally behaves like a "local"
// memory segment.  If "throw count" is set to non 0 via setThrowCount(),
// it continues the normal behavior up to the specified number of calls to
// allocate(), and throws an exception at the next call.
class TestMemorySegment : public isc::util::MemorySegmentLocal {
public:
    TestMemorySegment() : throw_count_(0) {}
    virtual void* allocate(size_t size) {
        if (throw_count_ > 0) {
            if (--throw_count_ == 0) {
                throw std::bad_alloc();
            }
        }
        return (isc::util::MemorySegmentLocal::allocate(size));
    }
    void setThrowCount(size_t count) { throw_count_ = count; }

private:
    size_t throw_count_;
};

69
const char* rrset_data[] = {
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
    "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",
    "a.example.org.		   	 3600 IN MX	10 mail.example.org.",
    NULL
};

class MockIterator : public ZoneIterator {
private:
    MockIterator() :
        rrset_data_ptr_(rrset_data)
    {
    }

    const char** rrset_data_ptr_;

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

        RRsetPtr result(textToRRset(*rrset_data_ptr_,
                                    RRClass::IN(), Name("example.org")));
        rrset_data_ptr_++;

        return (result);
    }

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

    static ZoneIteratorPtr makeIterator(void) {
        return (ZoneIteratorPtr(new MockIterator()));
    }
};

107
108
109
class MemoryClientTest : public ::testing::Test {
protected:
    MemoryClientTest() : zclass_(RRClass::IN()),
110
                         client_(new InMemoryClient(mem_sgmt_, zclass_))
111
112
113
114
115
116
117
118
119
120
121
122
    {}
    ~MemoryClientTest() {
        if (client_ != NULL) {
            delete client_;
        }
    }
    void TearDown() {
        delete client_;
        client_ = NULL;
        EXPECT_TRUE(mem_sgmt_.allMemoryDeallocated()); // catch any leak here.
    }
    const RRClass zclass_;
123
    TestMemorySegment mem_sgmt_;
124
125
126
    InMemoryClient* client_;
};

127
TEST_F(MemoryClientTest, loadRRsetDoesntMatchOrigin) {
128
    // Attempting to load example.org to example.com zone should result
129
    // in an exception.
130
131
132
133
    EXPECT_THROW(client_->load(Name("example.com"),
                               TEST_DATA_DIR "/example.org-empty.zone"),
                 MasterLoadError);
}
134

135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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
}

155
156
157
158
159
160
161
TEST_F(MemoryClientTest, loadNonExistentZoneFile) {
    EXPECT_THROW(client_->load(Name("example.org"),
                               TEST_DATA_DIR "/somerandomfilename"),
                 MasterLoadError);
    // Teardown checks for memory segment leaks
}

162
163
164
165
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.
166
167
168

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

169
170
171
    EXPECT_THROW(client_->load(Name("."),
                               TEST_DATA_DIR "/empty.zone"),
                 InMemoryClient::EmptyZone);
172
173
174

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

175
176
177
    // Teardown checks for memory segment leaks
}

178
179
180
TEST_F(MemoryClientTest, load) {
    // This is a simple load check for a "full" and correct zone that
    // should not result in any exceptions.
181
182
183
184
    client_->load(Name("example.org"),
                  TEST_DATA_DIR "/example.org.zone");
}

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
TEST_F(MemoryClientTest, loadFromIterator) {
    client_->load(Name("example.org"),
                  *MockIterator::makeIterator());

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

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

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

213
214
215
216
217
218
219
220
221
222
223
224
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
}

225
226
227
228
229
230
231
232
TEST_F(MemoryClientTest, loadNSEC3Signed) {
    client_->load(Name("example.org"),
                  TEST_DATA_DIR "/example.org-nsec3-signed.zone");
}

TEST_F(MemoryClientTest, loadNSEC3SignedNoParam) {
    client_->load(Name("example.org"),
                  TEST_DATA_DIR "/example.org-nsec3-signed-no-param.zone");
233
234
}

Mukund Sivaraman's avatar
Mukund Sivaraman committed
235
236
237
238
239
240
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
241
                  TEST_DATA_DIR "/example.org-empty.zone");
Mukund Sivaraman's avatar
Mukund Sivaraman committed
242
243
    EXPECT_EQ(1, client_->getZoneCount());

Mukund Sivaraman's avatar
Mukund Sivaraman committed
244
245
    // Reload zone with same data

Mukund Sivaraman's avatar
Mukund Sivaraman committed
246
    client_->load(Name("example.org"),
Mukund Sivaraman's avatar
Mukund Sivaraman committed
247
                  client_->getFileName(Name("example.org")));
Mukund Sivaraman's avatar
Mukund Sivaraman committed
248
249
    EXPECT_EQ(1, client_->getZoneCount());

250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
    isc::datasrc::memory::ZoneTable::FindResult
        result(client_->findZone2(Name("example.org")));
    EXPECT_EQ(result::SUCCESS, result.code);
    EXPECT_NE(static_cast<ZoneData*>(NULL),
              result.zone_data);

    /* Check SOA */
    const ZoneNode* node = result.zone_data->getOriginNode();
    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 */
    const ZoneTree& tree = result.zone_data->getZoneTree();
    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
274
    client_->load(Name("example.org"),
Mukund Sivaraman's avatar
Mukund Sivaraman committed
275
                  TEST_DATA_DIR "/example.org-rrsigs.zone");
Mukund Sivaraman's avatar
Mukund Sivaraman committed
276
277
    EXPECT_EQ(1, client_->getZoneCount());

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
    isc::datasrc::memory::ZoneTable::FindResult
        result2(client_->findZone2(Name("example.org")));
    EXPECT_EQ(result::SUCCESS, result2.code);
    EXPECT_NE(static_cast<ZoneData*>(NULL),
              result2.zone_data);

    /* Check SOA */
    node = result2.zone_data->getOriginNode();
    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 */
    const ZoneTree& tree2 = result2.zone_data->getZoneTree();
    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
312
313
314
    // Teardown checks for memory segment leaks
}

315
316
317
318
319
320
321
322
323
324
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);
325
326
327
    // Teardown checks for memory segment leaks
}

Mukund Sivaraman's avatar
Mukund Sivaraman committed
328
TEST_F(MemoryClientTest, loadMultipleCNAMEThrows) {
329
330
331
332
333
    // Multiple CNAME RRs should throw.
    EXPECT_THROW(client_->load(Name("example.org"),
                               TEST_DATA_DIR
                               "/example.org-multiple-cname.zone"),
                 InMemoryClient::AddError);
334
335
336
    // Teardown checks for memory segment leaks
}

Mukund Sivaraman's avatar
Mukund Sivaraman committed
337
TEST_F(MemoryClientTest, loadMultipleDNAMEThrows) {
338
339
340
341
342
    // Multiple DNAME RRs should throw.
    EXPECT_THROW(client_->load(Name("example.org"),
                               TEST_DATA_DIR
                               "/example.org-multiple-dname.zone"),
                 InMemoryClient::AddError);
343
344
345
    // Teardown checks for memory segment leaks
}

Mukund Sivaraman's avatar
Mukund Sivaraman committed
346
TEST_F(MemoryClientTest, loadMultipleNSEC3Throws) {
347
348
349
350
351
352
353
354
    // 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
355
TEST_F(MemoryClientTest, loadMultipleNSEC3PARAMThrows) {
356
357
358
359
360
    // Multiple NSEC3PARAM RRs should throw.
    EXPECT_THROW(client_->load(Name("example.org"),
                               TEST_DATA_DIR
                               "/example.org-multiple-nsec3param.zone"),
                 InMemoryClient::AddError);
361
362
363
    // Teardown checks for memory segment leaks
}

364
365
366
367
368
369
370
371
372
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
}

373
374
375
376
377
378
379
380
381
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
}

382
TEST_F(MemoryClientTest, loadWildcardDNAMEThrows) {
383
    // Wildcard DNAME names should throw
384
385
386
387
388
389
390
    EXPECT_THROW(client_->load(Name("example.org"),
                               TEST_DATA_DIR
                               "/example.org-wildcard-dname.zone"),
                 InMemoryClient::AddError);
    // Teardown checks for memory segment leaks
}

391
392
393
394
395
396
397
398
399
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
}

400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
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
}

418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
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
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
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
}

467
468
469
470
471
472
473
474
TEST_F(MemoryClientTest, loadRRSIGFollowsNothing) {
    EXPECT_THROW(client_->load(Name("example.org"),
                               TEST_DATA_DIR
                               "/example.org-rrsig-follows-nothing.zone"),
                 InMemoryClient::AddError);
    // Teardown checks for memory segment leaks
}

475
476
477
478
479
480
481
482
TEST_F(MemoryClientTest, loadRRSIGNameUnmatched) {
    EXPECT_THROW(client_->load(Name("example.org"),
                               TEST_DATA_DIR
                               "/example.org-rrsig-name-unmatched.zone"),
                 InMemoryClient::AddError);
    // Teardown checks for memory segment leaks
}

483
484
485
486
487
488
489
490
TEST_F(MemoryClientTest, loadRRSIGTypeUnmatched) {
    EXPECT_THROW(client_->load(Name("example.org"),
                               TEST_DATA_DIR
                               "/example.org-rrsig-type-unmatched.zone"),
                 InMemoryClient::AddError);
    // Teardown checks for memory segment leaks
}

491
TEST_F(MemoryClientTest, loadRRSIGs) {
Mukund Sivaraman's avatar
Mukund Sivaraman committed
492
    client_->load(Name("example.org"),
Mukund Sivaraman's avatar
Mukund Sivaraman committed
493
                  TEST_DATA_DIR "/example.org-rrsigs.zone");
494
495
496
    EXPECT_EQ(1, client_->getZoneCount());
}

497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
TEST_F(MemoryClientTest, loadRRSIGsRdataMixedCoveredTypes) {
    client_->load(Name("example.org"),
                  TEST_DATA_DIR "/example.org-rrsigs.zone");

    RRsetPtr rrset(new RRset(Name("example.org"),
                             RRClass::IN(), RRType::A(), RRTTL(3600)));
    rrset->addRdata(in::A("192.0.2.1"));
    rrset->addRdata(in::A("192.0.2.2"));

    RRsetPtr rrsig(new RRset(Name("example.org"), zclass_,
                             RRType::RRSIG(), RRTTL(300)));
    rrsig->addRdata(generic::RRSIG("A 5 3 3600 20000101000000 20000201000000 "
                                   "12345 example.org. FAKEFAKEFAKE"));
    rrsig->addRdata(generic::RRSIG("NS 5 3 3600 20000101000000 20000201000000 "
                                   "54321 example.org. FAKEFAKEFAKEFAKE"));
    rrset->addRRsig(rrsig);

    EXPECT_THROW(client_->add(Name("example.org"), rrset),
                 InMemoryClient::AddError);

    // Teardown checks for memory segment leaks
}

520
521
TEST_F(MemoryClientTest, getZoneCount) {
    EXPECT_EQ(0, client_->getZoneCount());
522
523
524
525
    client_->load(Name("example.org"), TEST_DATA_DIR "/example.org-empty.zone");
    EXPECT_EQ(1, client_->getZoneCount());
}

526
527
528
529
530
531
532
533
534
535
536
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
537
TEST_F(MemoryClientTest, getIteratorForNonExistentZone) {
538
539
    // Zone "." doesn't exist
    EXPECT_THROW(client_->getIterator(Name(".")), DataSourceError);
540
}
541

542
543
544
TEST_F(MemoryClientTest, getIterator) {
    client_->load(Name("example.org"), TEST_DATA_DIR "/example.org-empty.zone");
    ZoneIteratorPtr iterator(client_->getIterator(Name("example.org")));
545
546
547
548
549
550

    // 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
551
    // There's nothing else in this iterator
552
    EXPECT_EQ(ConstRRsetPtr(), iterator->getNextRRset());
553
554
555

    // Iterating past the end should result in an exception
    EXPECT_THROW(iterator->getNextRRset(), isc::Unexpected);
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
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());
}

601
602
603
604
605
606
607
608
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);
}

609
610
611
612
613
614
615
616
617
TEST_F(MemoryClientTest, addRRsetToNonExistentZoneThrows) {
    // The zone "example.org" doesn't exist, so we can't add an RRset to
    // it.
    RRsetPtr rrset_a(new RRset(Name("example.org"), RRClass::IN(), RRType::A(),
                               RRTTL(300)));
    rrset_a->addRdata(rdata::in::A("192.0.2.1"));
    EXPECT_THROW(client_->add(Name("example.org"), rrset_a), DataSourceError);
}

618
619
620
621
622
623
624
625
626
627
628
629
630
631
TEST_F(MemoryClientTest, addOutOfZoneThrows) {
    // Out of zone names should throw.
    client_->load(Name("example.org"),
                  TEST_DATA_DIR "/example.org-empty.zone");

    RRsetPtr rrset_a(new RRset(Name("a.example.com"),
                               RRClass::IN(), RRType::A(), RRTTL(300)));
    rrset_a->addRdata(rdata::in::A("192.0.2.1"));

    EXPECT_THROW(client_->add(Name("example.org"), rrset_a),
                 OutOfZone);
    // Teardown checks for memory segment leaks
}

632
633
634
635
636
637
638
639
640
641
TEST_F(MemoryClientTest, addNullRRsetThrows) {
    client_->load(Name("example.org"),
                  TEST_DATA_DIR "/example.org-rrsigs.zone");

    EXPECT_THROW(client_->add(Name("example.org"), ConstRRsetPtr()),
                 InMemoryClient::NullRRset);

    // Teardown checks for memory segment leaks
}

642
643
644
645
646
647
648
649
650
651
652
653
TEST_F(MemoryClientTest, addEmptyRRsetThrows) {
    client_->load(Name("example.org"),
                  TEST_DATA_DIR "/example.org-rrsigs.zone");

    RRsetPtr rrset_a(new RRset(Name("example.org"), RRClass::IN(), RRType::A(),
                               RRTTL(300)));
    EXPECT_THROW(client_->add(Name("example.org"), rrset_a),
                 InMemoryClient::AddError);

    // Teardown checks for memory segment leaks
}

654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
TEST_F(MemoryClientTest, add) {
    client_->load(Name("example.org"), TEST_DATA_DIR "/example.org-empty.zone");

    // Add another RRset
    RRsetPtr rrset_a(new RRset(Name("example.org"), RRClass::IN(), RRType::A(),
                               RRTTL(300)));
    rrset_a->addRdata(rdata::in::A("192.0.2.1"));
    client_->add(Name("example.org"), rrset_a);

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

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

    rrset = iterator->getNextRRset();
    EXPECT_TRUE(rrset);
    EXPECT_EQ(RRType::SOA(), rrset->getType());

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

678
679
680
681
682
683
TEST_F(MemoryClientTest, findZoneThrowsNotImplemented) {
    // This method is not implemented.
    EXPECT_THROW(client_->findZone(Name(".")),
                 isc::NotImplemented);
}

684
685
686
687
688
689
690
TEST_F(MemoryClientTest, findZone2) {
    client_->load(Name("example.org"),
                  TEST_DATA_DIR "/example.org-rrsigs.zone");

    isc::datasrc::memory::ZoneTable::FindResult
        result(client_->findZone2(Name("example.com")));
    EXPECT_EQ(result::NOTFOUND, result.code);
691
692
    EXPECT_EQ(static_cast<ZoneData*>(NULL),
              result.zone_data);
693
694
695
696

    isc::datasrc::memory::ZoneTable::FindResult
        result2(client_->findZone2(Name("example.org")));
    EXPECT_EQ(result::SUCCESS, result2.code);
697
698
    EXPECT_NE(static_cast<ZoneData*>(NULL),
              result2.zone_data);
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

    /* Check SOA */
    const ZoneNode* node = result2.zone_data->getOriginNode();
    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 */
    const ZoneTree& tree = result2.zone_data->getZoneTree();
    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);
727
728
}

729
TEST_F(MemoryClientTest, getUpdaterThrowsNotImplemented) {
730
    // This method is not implemented.
731
    EXPECT_THROW(client_->getUpdater(Name("."), false, false),
Mukund Sivaraman's avatar
Mukund Sivaraman committed
732
                 isc::NotImplemented);
733
}
734
735

TEST_F(MemoryClientTest, getJournalReaderNotImplemented) {
736
    // This method is not implemented.
737
    EXPECT_THROW(client_->getJournalReader(Name("."), 0, 0),
Mukund Sivaraman's avatar
Mukund Sivaraman committed
738
                 isc::NotImplemented);
739
}
740
}