summaryrefslogtreecommitdiffstats
path: root/src/lib/dns/tests/rdata_srv_unittest.cc
blob: a3296f2f8c102d3e54e4d2d49b3cc04705320f5d (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
// 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;

namespace {
class Rdata_SRV_Test : public RdataTest {
public:
    Rdata_SRV_Test() :
        srv_txt("1 5 1500 a.example.com."),
        srv_txt2("1 5 1400 example.com."),
        too_long_label("012345678901234567890123456789"
                       "0123456789012345678901234567890123."),
        rdata_srv(srv_txt),
        rdata_srv2(srv_txt2)
    {}

    const string srv_txt;
    const string srv_txt2;
    const string too_long_label;
    const in::SRV rdata_srv;
    const in::SRV rdata_srv2;
};

// 1 5 1500 a.example.com.
const uint8_t wiredata_srv[] = {
    0x00, 0x01, 0x00, 0x05, 0x05, 0xdc, 0x01, 0x61, 0x07, 0x65, 0x78,
    0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00};
// 1 5 1400 example.com.
const uint8_t wiredata_srv2[] = {
    0x00, 0x01, 0x00, 0x05, 0x05, 0x78, 0x07, 0x65, 0x78, 0x61, 0x6d,
    0x70, 0x6c, 0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00};

TEST_F(Rdata_SRV_Test, createFromText) {
    EXPECT_EQ(1, rdata_srv.getPriority());
    EXPECT_EQ(5, rdata_srv.getWeight());
    EXPECT_EQ(1500, rdata_srv.getPort());
    EXPECT_EQ(Name("a.example.com."), rdata_srv.getTarget());
}

TEST_F(Rdata_SRV_Test, badText) {
    // priority is too large (2814...6 is 2^48)
    EXPECT_THROW(in::SRV("281474976710656 5 1500 a.example.com."),
                 InvalidRdataText);
    // weight is too large
    EXPECT_THROW(in::SRV("1 281474976710656 1500 a.example.com."),
                 InvalidRdataText);
    // port is too large
    EXPECT_THROW(in::SRV("1 5 281474976710656 a.example.com."),
                 InvalidRdataText);
    // incomplete text
    EXPECT_THROW(in::SRV("1 5 a.example.com."),
                 InvalidRdataText);
    EXPECT_THROW(in::SRV("1 5 1500a.example.com."),
                 InvalidRdataText);
    // bad name
    EXPECT_THROW(in::SRV("1 5 1500 a.example.com." + too_long_label),
                 TooLongLabel);
    // Extra text at end of line
    EXPECT_THROW(in::SRV("1 5 1500 a.example.com. extra."), InvalidRdataText);
}

TEST_F(Rdata_SRV_Test, assignment) {
    in::SRV copy((string(srv_txt2)));
    copy = rdata_srv;
    EXPECT_EQ(0, copy.compare(rdata_srv));

    // Check if the copied data is valid even after the original is deleted
    in::SRV* copy2 = new in::SRV(rdata_srv);
    in::SRV copy3((string(srv_txt2)));
    copy3 = *copy2;
    delete copy2;
    EXPECT_EQ(0, copy3.compare(rdata_srv));

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

TEST_F(Rdata_SRV_Test, createFromWire) {
    EXPECT_EQ(0, rdata_srv.compare(
                  *rdataFactoryFromFile(RRType("SRV"), RRClass("IN"),
                                        "rdata_srv_fromWire")));
    // RDLENGTH is too short
    EXPECT_THROW(rdataFactoryFromFile(RRType("SRV"), RRClass("IN"),
                                      "rdata_srv_fromWire", 23),
                 InvalidRdataLength);
    // RDLENGTH is too long
    EXPECT_THROW(rdataFactoryFromFile(RRType("SRV"), RRClass("IN"),
                                      "rdata_srv_fromWire", 46),
                 InvalidRdataLength);
    // incomplete name.  the error should be detected in the name constructor
    EXPECT_THROW(rdataFactoryFromFile(RRType("SRV"), RRClass("IN"),
                                      "rdata_cname_fromWire", 69),
                 DNSMessageFORMERR);
    // parse compressed target name
    EXPECT_EQ(0, rdata_srv.compare(
                  *rdataFactoryFromFile(RRType("SRV"), RRClass("IN"),
                                      "rdata_srv_fromWire", 89)));
}

TEST_F(Rdata_SRV_Test, createFromLexer) {
    EXPECT_EQ(0, rdata_srv.compare(
        *test::createRdataUsingLexer(RRType::SRV(), RRClass::IN(),
                                     "1 5 1500 a.example.com.")));

    // test::createRdataUsingLexer() constructs relative to
    // "example.org." origin.
    EXPECT_EQ(0, in::SRV("1 5 1500 server16.example.org.").compare(
        *test::createRdataUsingLexer(RRType::SRV(), RRClass::IN(),
                                     "1 5 1500 server16")));

    // Exceptions cause NULL to be returned.

    // Bad priority
    EXPECT_FALSE(test::createRdataUsingLexer(RRType::SRV(), RRClass::IN(),
                                             "65536 5 1500 "
                                             "a.example.com."));
    // Bad weight
    EXPECT_FALSE(test::createRdataUsingLexer(RRType::SRV(), RRClass::IN(),
                                             "1 65536 1500 "
                                             "a.example.com."));
    // Bad port
    EXPECT_FALSE(test::createRdataUsingLexer(RRType::SRV(), RRClass::IN(),
                                             "1 5 281474976710656 "
                                             "a.example.com."));
    // Extra text at end of line
    EXPECT_FALSE(test::createRdataUsingLexer(RRType::SRV(), RRClass::IN(),
                                             "1 5 1500 a.example.com. extra."));
}

TEST_F(Rdata_SRV_Test, toWireBuffer) {
    rdata_srv.toWire(obuffer);
    matchWireData(wiredata_srv, sizeof(wiredata_srv),
                  obuffer.getData(), obuffer.getLength());

    obuffer.clear();
    rdata_srv2.toWire(obuffer);
    matchWireData(wiredata_srv2, sizeof(wiredata_srv2),
                  obuffer.getData(), obuffer.getLength());
}

TEST_F(Rdata_SRV_Test, toWireRenderer) {
    rdata_srv.toWire(renderer);
    matchWireData(wiredata_srv, sizeof(wiredata_srv),
                  renderer.getData(), renderer.getLength());

    renderer.clear();
    rdata_srv2.toWire(renderer);
    matchWireData(wiredata_srv2, sizeof(wiredata_srv2),
                  renderer.getData(), renderer.getLength());
}

TEST_F(Rdata_SRV_Test, toText) {
    EXPECT_EQ(srv_txt, rdata_srv.toText());
    EXPECT_EQ(srv_txt2, rdata_srv2.toText());
}

TEST_F(Rdata_SRV_Test, compare) {
    // test RDATAs, sorted in the ascending order.
    vector<in::SRV> compare_set;
    compare_set.push_back(in::SRV("1 5 1500 a.example.com."));
    compare_set.push_back(in::SRV("2 5 1500 a.example.com."));
    compare_set.push_back(in::SRV("2 6 1500 a.example.com."));
    compare_set.push_back(in::SRV("2 6 1600 a.example.com."));
    compare_set.push_back(in::SRV("2 6 1600 example.com."));

    EXPECT_EQ(0, compare_set[0].compare(
                  in::SRV("1 5 1500 a.example.com.")));

    vector<in::SRV>::const_iterator it;
    vector<in::SRV>::const_iterator it_end = compare_set.end();
    for (it = compare_set.begin(); it != it_end - 1; ++it) {
        EXPECT_GT(0, (*it).compare(*(it + 1)));
        EXPECT_LT(0, (*(it + 1)).compare(*it));
    }

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