rrsetlist_unittest.cc 5.15 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Copyright (C) 2010  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.

JINMEI Tatuya's avatar
propset    
JINMEI Tatuya committed
15
// $Id$
16
17
18
19

#include <vector>
#include <boost/foreach.hpp>

20
21
22
23
24
25
26
#include <dns/rdata.h>
#include <dns/rdataclass.h>
#include <dns/rrclass.h>
#include <dns/rrtype.h>
#include <dns/rrsetlist.h>
#include <dns/rrset.h>
#include <dns/rrttl.h>
27
28
29
30
31
32
33
34
35
36
37
38
39
40

#include <gtest/gtest.h>

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

namespace {
class RRsetListTest : public ::testing::Test {
protected:
    RRsetListTest() {}
    void setupList(RRsetList& list);
};

41
42
43
44
45
46
const in::A rdata_in_a("192.0.2.1");
const in::AAAA rdata_in_aaaa("2001:db8::1234");
const generic::NS rdata_ns("ns.example.com");
const generic::SOA rdata_soa(Name("ns.example.com"), Name("root.example.com"),
                             2010012601, 3600, 300, 3600000, 1200);
const generic::CNAME rdata_cname("target.example.com");
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

void
RRsetListTest::setupList(RRsetList& list) {
    RRsetPtr a(new RRset(Name("example.com"), RRClass::IN(),
                         RRType::A(), RRTTL(3600)));
    RRsetPtr aaaa(new RRset(Name("example.com"), RRClass::IN(),
                            RRType::AAAA(), RRTTL(3600)));
    RRsetPtr ns(new RRset(Name("example.com"), RRClass::IN(),
                          RRType::NS(), RRTTL(3600)));
    RRsetPtr soa(new RRset(Name("example.com"), RRClass::IN(),
                           RRType::SOA(), RRTTL(3600)));
    RRsetPtr cname(new RRset(Name("example.com"), RRClass::IN(),
                             RRType::CNAME(), RRTTL(3600)));

    a->addRdata(rdata_in_a);
    aaaa->addRdata(rdata_in_aaaa);
    ns->addRdata(rdata_ns);
    soa->addRdata(rdata_soa);
    cname->addRdata(rdata_cname);

    list.addRRset(a);
    list.addRRset(aaaa);
    list.addRRset(ns);
    list.addRRset(soa);
    list.addRRset(cname);
}

TEST_F(RRsetListTest, emptyOnInitialCreate) {
    RRsetList list;
    EXPECT_EQ(list.size(), 0);
}

TEST_F(RRsetListTest, addRRsets) {
    RRsetList list;
    setupList(list);
    EXPECT_EQ(list.size(), 5);
}

TEST_F(RRsetListTest, extraRRset) {
    RRsetList list;
    setupList(list);
    RRsetPtr cname(new RRset(Name("another.example.com"), RRClass::IN(),
                             RRType::CNAME(), RRTTL(3600)));
    EXPECT_THROW(list.addRRset(cname), DuplicateRRset);
}

TEST_F(RRsetListTest, randomAccess) {
    RRsetList list;
    setupList(list);

    RRsetPtr p;

    p = list[RRType::CNAME()];
100
    EXPECT_EQ(p->getType(), RRType::CNAME());
101
102

    p = list[RRType::AAAA()];
103
    EXPECT_EQ(p->getType(), RRType::AAAA());
104
105

    p = list[RRType::NS()];
106
    EXPECT_EQ(p->getType(), RRType::NS());
107
108

    p = list[RRType::A()];
109
    EXPECT_EQ(p->getType(), RRType::A());
110
111

    p = list[RRType::SOA()];
112
    EXPECT_EQ(p->getType(), RRType::SOA());
113
114
115
116
117
118
119
120
121

    // What should this be?
    p = RRsetPtr(new RRset(Name("example.com"), RRClass::CH(),
                           RRType::DNAME(), RRTTL(3600)));
    list.addRRset(p);
    p = list[RRType::DNAME()];
    //This will crash, but it's counter intuitive, isn't it?  We added a DNAME
    //RRset and fetched a DNAME RRset in the list.  Why can't it succeed?
    //EXPECT_EQ(p->getType(), RRType::DNAME());
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
}

TEST_F(RRsetListTest, findRRset) {
    RRsetList list;
    setupList(list);
    EXPECT_EQ(list[RRType::A()], list.findRRset(RRType::A(), RRClass::IN()));
}

TEST_F(RRsetListTest, checkData) {
    RRsetList list;
    RRsetPtr a(new RRset(Name("example.com"), RRClass::IN(),
                         RRType::A(), RRTTL(3600)));
    a->addRdata(rdata_in_a);
    list.addRRset(a);

    RdataIteratorPtr it = list[RRType::A()]->getRdataIterator();
    it->first();
    EXPECT_FALSE(it->isLast());
    EXPECT_EQ("192.0.2.1", it->getCurrent().toText());
}

TEST_F(RRsetListTest, iterate) {
    RRsetList list;
    setupList(list);

JINMEI Tatuya's avatar
JINMEI Tatuya committed
147
148
    bool has_a = false, has_aaaa = false, has_ns = false, has_soa = false,
        has_cname = false;
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
    int i = 0;
    BOOST_FOREACH(RRsetPtr rrset, list) {
        if (rrset->getType() == RRType::A()) {
            has_a = true;
        }
        if (rrset->getType() == RRType::AAAA()) {
            has_aaaa = true;
        }
        if (rrset->getType() == RRType::NS()) {
            has_ns = true;
        }
        if (rrset->getType() == RRType::SOA()) {
            has_soa = true;
        }
        if (rrset->getType() == RRType::CNAME()) {
            has_cname = true;
        }
        ++i;
    }
    EXPECT_TRUE(has_a);
    EXPECT_TRUE(has_aaaa);
    EXPECT_TRUE(has_ns);
    EXPECT_TRUE(has_soa);
    EXPECT_TRUE(has_cname);
    EXPECT_TRUE(i == 5);
}

}