rrset_collection_unittest.cc 6.27 KB
Newer Older
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
// 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 <dns/rrset_collection.h>
#include <dns/rrttl.h>
#include <dns/rdataclass.h>

#include <gtest/gtest.h>

using namespace isc::dns;
using namespace isc::dns::rdata;
using namespace std;

namespace {

class RRsetCollectionTest : public ::testing::Test {
public:
    RRsetCollectionTest() :
        rrclass("IN"),
        origin("example.org"),
        collection(TEST_DATA_SRCDIR "/example.org", origin, rrclass)
    {}

    const RRClass rrclass;
    const Name origin;
    RRsetCollection collection;
};

TEST_F(RRsetCollectionTest, findBase) {
    // Test the find() that returns isc::dns::AbstractRRset*
    const AbstractRRset* rrset = collection.find(Name("www.example.org"),
43
                                                 RRType::A(), rrclass);
44
45
46
47
48
49
50
    EXPECT_NE(static_cast<AbstractRRset*>(NULL), rrset);
    EXPECT_EQ(RRType::A(), rrset->getType());
    EXPECT_EQ(RRTTL(3600), rrset->getTTL());
    EXPECT_EQ(RRClass("IN"), rrset->getClass());
    EXPECT_EQ(Name("www.example.org"), rrset->getName());

    // foo.example.org doesn't exist
51
    rrset = collection.find(Name("foo.example.org"), RRType::A(), rrclass);
52
53
54
    EXPECT_EQ(static_cast<AbstractRRset*>(NULL), rrset);

    // www.example.org exists, but not with MX
55
    rrset = collection.find(Name("www.example.org"), RRType::MX(), rrclass);
56
57
58
    EXPECT_EQ(static_cast<AbstractRRset*>(NULL), rrset);

    // www.example.org exists, with AAAA
59
    rrset = collection.find(Name("www.example.org"), RRType::AAAA(), rrclass);
60
    EXPECT_NE(static_cast<AbstractRRset*>(NULL), rrset);
61
62
63
64
65

    // www.example.org with AAAA does not exist in RRClass::CH()
    rrset = collection.find(Name("www.example.org"), RRType::AAAA(),
                            RRClass::CH());
    EXPECT_EQ(static_cast<AbstractRRset*>(NULL), rrset);
66
67
}

68
69
template <typename T, typename TP>
void doFind(T& collection, const RRClass& rrclass) {
70
    // Test the find() that returns ConstRRsetPtr
71
    TP rrset = collection.find(Name("www.example.org"), rrclass, RRType::A());
72
73
74
75
76
77
78
    EXPECT_TRUE(rrset);
    EXPECT_EQ(RRType::A(), rrset->getType());
    EXPECT_EQ(RRTTL(3600), rrset->getTTL());
    EXPECT_EQ(RRClass("IN"), rrset->getClass());
    EXPECT_EQ(Name("www.example.org"), rrset->getName());

    // foo.example.org doesn't exist
79
    rrset = collection.find(Name("foo.example.org"), rrclass, RRType::A());
80
81
82
    EXPECT_FALSE(rrset);

    // www.example.org exists, but not with MX
83
    rrset = collection.find(Name("www.example.org"), rrclass, RRType::MX());
84
85
86
    EXPECT_FALSE(rrset);

    // www.example.org exists, with AAAA
87
    rrset = collection.find(Name("www.example.org"), rrclass, RRType::AAAA());
88
    EXPECT_TRUE(rrset);
89
90
91
92
93

    // www.example.org with AAAA does not exist in RRClass::CH()
    rrset = collection.find(Name("www.example.org"), RRClass::CH(),
                            RRType::AAAA());
    EXPECT_FALSE(rrset);
94
95
}

96
TEST_F(RRsetCollectionTest, findConst) {
97
    // Test the find() that returns ConstRRsetPtr
98
99
100
    const RRsetCollection& ccln = collection;
    doFind<const RRsetCollection, ConstRRsetPtr>(ccln, rrclass);
}
101

102
103
104
TEST_F(RRsetCollectionTest, find) {
    // Test the find() that returns RRsetPtr
    doFind<RRsetCollection, RRsetPtr>(collection, rrclass);
105
106
}

107
108
void
doAddAndRemove(RRsetCollection& collection, const RRClass& rrclass) {
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
    // foo.example.org/A doesn't exist
    RRsetPtr rrset_found = collection.find(Name("foo.example.org"), rrclass,
                                           RRType::A());
    EXPECT_FALSE(rrset_found);

    // Add foo.example.org/A
    RRsetPtr rrset(new BasicRRset(Name("foo.example.org"), rrclass, RRType::A(),
                                  RRTTL(7200)));
    rrset->addRdata(in::A("192.0.2.1"));
    collection.addRRset(rrset);

    // foo.example.org/A should now exist
    rrset_found = collection.find(Name("foo.example.org"), rrclass,
                                  RRType::A());
    EXPECT_TRUE(rrset_found);
    EXPECT_EQ(RRType::A(), rrset_found->getType());
    EXPECT_EQ(RRTTL(7200), rrset_found->getTTL());
    EXPECT_EQ(RRClass("IN"), rrset_found->getClass());
    EXPECT_EQ(Name("foo.example.org"), rrset_found->getName());

129
130
131
    // The collection must not be empty.
    EXPECT_NE(collection.end(), collection.begin());

132
133
134
135
136
    // Adding a duplicate RRset must throw.
    EXPECT_THROW({
        collection.addRRset(rrset);
    }, isc::InvalidParameter);

137
138
139
140
141
142
143
144
145
    // Remove foo.example.org/A
    collection.removeRRset(Name("foo.example.org"), rrclass, RRType::A());

    // foo.example.org/A should not exist now
    rrset_found = collection.find(Name("foo.example.org"), rrclass,
                                  RRType::A());
    EXPECT_FALSE(rrset_found);
}

146
147
148
149
TEST_F(RRsetCollectionTest, addAndRemove) {
    doAddAndRemove(collection, rrclass);
}

150
151
152
153
154
155
156
157
158
159
160
161
162
TEST_F(RRsetCollectionTest, empty) {
    RRsetCollection cln;

    // Here, cln is empty.
    EXPECT_EQ(cln.end(), cln.begin());

    doAddAndRemove(cln, rrclass);

    // cln should be empty again here, after the add and remove
    // operations.
    EXPECT_EQ(cln.end(), cln.begin());
}

163
TEST_F(RRsetCollectionTest, iteratorTest) {
164
    // The collection must not be empty.
Mukund Sivaraman's avatar
Mukund Sivaraman committed
165
    EXPECT_NE(collection.end(), collection.begin());
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181

    // Here, we just count the records and do some basic tests on them.
    size_t count = 0;
    for (RRsetCollection::iterator it = collection.begin();
         it != collection.end(); ++it) {
         ++count;
         const AbstractRRset& rrset = *it;
         EXPECT_EQ(rrclass, rrset.getClass());
         EXPECT_EQ(RRTTL(3600), rrset.getTTL());
    }

    // example.org master file has SOA, NS, A, AAAA
    EXPECT_EQ(4, count);
}

} // namespace