summaryrefslogtreecommitdiffstats
path: root/src/lib/dns/tests/rdata_afsdb_unittest.cc
blob: 26c31352f84a5dbb1b342d3e2a1a4e9aec65a075 (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
// Copyright (C) 2011-2019 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 <util/buffer.h>
#include <dns/exceptions.h>
#include <dns/messagerenderer.h>
#include <dns/rdata.h>
#include <dns/rdataclass.h>
#include <dns/rrclass.h>
#include <dns/rrtype.h>

#include <gtest/gtest.h>

#include <dns/tests/unittest_util.h>
#include <dns/tests/rdata_unittest.h>
#include <util/unittests/wiredata.h>

using namespace std;
using namespace isc::dns;
using namespace isc::util;
using namespace isc::dns::rdata;
using isc::UnitTestUtil;
using isc::util::unittests::matchWireData;

const char* const afsdb_text = "1 afsdb.example.com.";
const char* const afsdb_text2 = "0 root.example.com.";
const char* const too_long_label("012345678901234567890123456789"
        "0123456789012345678901234567890123.");

namespace {
class Rdata_AFSDB_Test : public RdataTest {
protected:
    Rdata_AFSDB_Test() :
        rdata_afsdb(string(afsdb_text)), rdata_afsdb2(string(afsdb_text2))
    {}

    const generic::AFSDB rdata_afsdb;
    const generic::AFSDB rdata_afsdb2;
    vector<uint8_t> expected_wire;
};


TEST_F(Rdata_AFSDB_Test, createFromText) {
    EXPECT_EQ(1, rdata_afsdb.getSubtype());
    EXPECT_EQ(Name("afsdb.example.com."), rdata_afsdb.getServer());

    EXPECT_EQ(0, rdata_afsdb2.getSubtype());
    EXPECT_EQ(Name("root.example.com."), rdata_afsdb2.getServer());
}

TEST_F(Rdata_AFSDB_Test, badText) {
    // subtype is too large
    EXPECT_THROW(const generic::AFSDB rdata_afsdb("99999999 afsdb.example.com."),
                 InvalidRdataText);
    // incomplete text
    EXPECT_THROW(const generic::AFSDB rdata_afsdb("10"), InvalidRdataText);
    EXPECT_THROW(const generic::AFSDB rdata_afsdb("SPOON"), InvalidRdataText);
    EXPECT_THROW(const generic::AFSDB rdata_afsdb("1root.example.com."), InvalidRdataText);
    // number of fields (must be 2) is incorrect
    EXPECT_THROW(const generic::AFSDB rdata_afsdb("10 afsdb. example.com."),
                 InvalidRdataText);
    // No origin and relative
    EXPECT_THROW(const generic::AFSDB rdata_afsdb("1 afsdb.example.com"),
                 MissingNameOrigin);
    // bad name
    EXPECT_THROW(const generic::AFSDB rdata_afsdb("1 afsdb.example.com." +
                 string(too_long_label)), TooLongLabel);
}

TEST_F(Rdata_AFSDB_Test, copy) {
    const generic::AFSDB rdata_afsdb2(rdata_afsdb);
    EXPECT_EQ(0, rdata_afsdb.compare(rdata_afsdb2));
}

TEST_F(Rdata_AFSDB_Test, assignment) {
    generic::AFSDB copy((string(afsdb_text2)));
    copy = rdata_afsdb;
    EXPECT_EQ(0, copy.compare(rdata_afsdb));

    // Check if the copied data is valid even after the original is deleted
    generic::AFSDB* copy2 = new generic::AFSDB(rdata_afsdb);
    generic::AFSDB copy3((string(afsdb_text2)));
    copy3 = *copy2;
    delete copy2;
    EXPECT_EQ(0, copy3.compare(rdata_afsdb));

    // Self assignment
    copy = *&copy;
    EXPECT_EQ(0, copy.compare(rdata_afsdb));
}

TEST_F(Rdata_AFSDB_Test, createFromWire) {
    // uncompressed names
    EXPECT_EQ(0, rdata_afsdb.compare(
                  *rdataFactoryFromFile(RRType::AFSDB(), RRClass::IN(),
                                     "rdata_afsdb_fromWire1.wire")));
    // compressed name
    EXPECT_EQ(0, rdata_afsdb.compare(
                  *rdataFactoryFromFile(RRType::AFSDB(), RRClass::IN(),
                                     "rdata_afsdb_fromWire2.wire", 13)));
    // RDLENGTH is too short
    EXPECT_THROW(rdataFactoryFromFile(RRType::AFSDB(), RRClass::IN(),
                                     "rdata_afsdb_fromWire3.wire"),
                 InvalidRdataLength);
    // RDLENGTH is too long
    EXPECT_THROW(rdataFactoryFromFile(RRType::AFSDB(), RRClass::IN(),
                                      "rdata_afsdb_fromWire4.wire"),
                 InvalidRdataLength);
    // bogus server name, the error should be detected in the name
    // constructor
    EXPECT_THROW(rdataFactoryFromFile(RRType::AFSDB(), RRClass::IN(),
                                      "rdata_afsdb_fromWire5.wire"),
                 DNSMessageFORMERR);
}

TEST_F(Rdata_AFSDB_Test, createFromLexer) {
    EXPECT_EQ(0, rdata_afsdb.compare(
        *test::createRdataUsingLexer(RRType::AFSDB(), RRClass::IN(),
                                     afsdb_text)));

    // test::createRdataUsingLexer() constructs relative to
    // "example.org." origin.
    generic::AFSDB tmp = generic::AFSDB("1 afsdb2.example.org.");
    EXPECT_EQ(0, tmp.compare(
        *test::createRdataUsingLexer(RRType::AFSDB(), RRClass::IN(),
                                     "1 afsdb2")));

    // Exceptions cause NULL to be returned.
    EXPECT_FALSE(test::createRdataUsingLexer(RRType::AFSDB(), RRClass::IN(),
                                             "1root.example.com."));

    // 65536 is larger than maximum possible subtype
    EXPECT_FALSE(test::createRdataUsingLexer(RRType::AFSDB(), RRClass::IN(),
                                             "65536 afsdb.example.com."));

    // Extra text at end of line
    EXPECT_FALSE(test::createRdataUsingLexer(RRType::AFSDB(), RRClass::IN(),
                                             "1 afsdb.example.com. extra."));
}

TEST_F(Rdata_AFSDB_Test, toWireBuffer) {
    // construct actual data
    rdata_afsdb.toWire(obuffer);

    // construct expected data
    UnitTestUtil::readWireData("rdata_afsdb_toWire1.wire", expected_wire);

    // then compare them
    matchWireData(&expected_wire[0], expected_wire.size(),
                  obuffer.getData(), obuffer.getLength());

    // clear buffer for the next test
    obuffer.clear();

    // construct actual data
    Name("example.com.").toWire(obuffer);
    rdata_afsdb2.toWire(obuffer);

    // construct expected data
    UnitTestUtil::readWireData("rdata_afsdb_toWire2.wire", expected_wire);

    // then compare them
    matchWireData(&expected_wire[0], expected_wire.size(),
                  obuffer.getData(), obuffer.getLength());
}

TEST_F(Rdata_AFSDB_Test, toWireRenderer) {
    // similar to toWireBuffer, but names in RDATA could be compressed due to
    // preceding names.  Actually they must not be compressed according to
    // RFC3597, and this test checks that.

    // construct actual data
    rdata_afsdb.toWire(renderer);

    // construct expected data
    UnitTestUtil::readWireData("rdata_afsdb_toWire1.wire", expected_wire);

    // then compare them
    matchWireData(&expected_wire[0], expected_wire.size(),
                  renderer.getData(), renderer.getLength());

    // clear renderer for the next test
    renderer.clear();

    // construct actual data
    renderer.writeName(Name("example.com."));
    rdata_afsdb2.toWire(renderer);

    // construct expected data
    UnitTestUtil::readWireData("rdata_afsdb_toWire2.wire", expected_wire);

    // then compare them
    matchWireData(&expected_wire[0], expected_wire.size(),
                  renderer.getData(), renderer.getLength());
}

TEST_F(Rdata_AFSDB_Test, toText) {
    EXPECT_EQ(afsdb_text, rdata_afsdb.toText());
    EXPECT_EQ(afsdb_text2, rdata_afsdb2.toText());
}

TEST_F(Rdata_AFSDB_Test, compare) {
    // check reflexivity
    EXPECT_EQ(0, rdata_afsdb.compare(rdata_afsdb));

    // name must be compared in case-insensitive manner
    EXPECT_EQ(0, rdata_afsdb.compare(generic::AFSDB("1 "
                                "AFSDB.example.com.")));

    const generic::AFSDB small1("10 afsdb.example.com.");
    const generic::AFSDB large1("65535 afsdb.example.com.");
    const generic::AFSDB large2("256 afsdb.example.com.");

    // confirm these are compared as unsigned values
    EXPECT_GT(0, rdata_afsdb.compare(large1));
    EXPECT_LT(0, large1.compare(rdata_afsdb));

    // confirm these are compared in network byte order
    EXPECT_GT(0, small1.compare(large2));
    EXPECT_LT(0, large2.compare(small1));

    // another AFSDB whose server name is larger than that of rdata_afsdb.
    const generic::AFSDB large3("256 zzzzz.example.com.");
    EXPECT_GT(0, large2.compare(large3));
    EXPECT_LT(0, large3.compare(large2));

    // comparison attempt between incompatible RR types should be rejected
    EXPECT_THROW(rdata_afsdb.compare(*rdata_nomatch), bad_cast);
}
}