1 // Copyright (C) 2010-2015 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7 #include <config.h>
8
9 #include <util/buffer.h>
10 #include <dns/exceptions.h>
11 #include <dns/messagerenderer.h>
12 #include <dns/rdata.h>
13 #include <dns/rdataclass.h>
14 #include <dns/rrclass.h>
15 #include <dns/rrtype.h>
16
17 #include <gtest/gtest.h>
18
19 #include <dns/tests/unittest_util.h>
20 #include <dns/tests/rdata_unittest.h>
21 #include <util/unittests/wiredata.h>
22
23 using namespace std;
24 using namespace isc::dns;
25 using namespace isc::util;
26 using namespace isc::dns::rdata;
27 using isc::UnitTestUtil;
28 using isc::util::unittests::matchWireData;
29
30 namespace {
31 class Rdata_NS_Test : public RdataTest {
32 public:
Rdata_NS_Test()33 Rdata_NS_Test() :
34 rdata_ns("ns.example.com."),
35 rdata_ns2("ns2.example.com.")
36 {}
37
38 const generic::NS rdata_ns;
39 const generic::NS rdata_ns2;
40 };
41
42 const uint8_t wiredata_ns[] = {
43 0x02, 0x6e, 0x73, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03,
44 0x63, 0x6f, 0x6d, 0x00 };
45 const uint8_t wiredata_ns2[] = {
46 // first name: ns.example.com.
47 0x02, 0x6e, 0x73, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03,
48 0x63, 0x6f, 0x6d, 0x00,
49 // second name: ns2.example.com. all labels except the first should be
50 // compressed.
51 0x03, 0x6e, 0x73, 0x32, 0xc0, 0x03 };
52
TEST_F(Rdata_NS_Test,createFromText)53 TEST_F(Rdata_NS_Test, createFromText) {
54 EXPECT_EQ(0, rdata_ns.compare(generic::NS("ns.example.com.")));
55 // explicitly add a trailing dot. should be the same RDATA.
56 EXPECT_EQ(0, rdata_ns.compare(generic::NS("ns.example.com.")));
57 // should be case sensitive.
58 EXPECT_EQ(0, rdata_ns.compare(generic::NS("NS.EXAMPLE.COM.")));
59 // RDATA of a class-independent type should be recognized for any
60 // "unknown" class.
61 EXPECT_EQ(0, rdata_ns.compare(*createRdata(RRType("NS"), RRClass(65000),
62 "ns.example.com.")));
63 }
64
TEST_F(Rdata_NS_Test,badText)65 TEST_F(Rdata_NS_Test, badText) {
66 // Extra input at end of line
67 EXPECT_THROW(generic::NS("ns.example.com. extra."), InvalidRdataText);
68 }
69
TEST_F(Rdata_NS_Test,createFromWire)70 TEST_F(Rdata_NS_Test, createFromWire) {
71 EXPECT_EQ(0, rdata_ns.compare(
72 *rdataFactoryFromFile(RRType("NS"), RRClass("IN"),
73 "rdata_ns_fromWire")));
74 // RDLENGTH is too short
75 EXPECT_THROW(rdataFactoryFromFile(RRType("NS"), RRClass("IN"),
76 "rdata_ns_fromWire", 18),
77 InvalidRdataLength);
78 // RDLENGTH is too long
79 EXPECT_THROW(rdataFactoryFromFile(RRType("NS"), RRClass("IN"),
80 "rdata_ns_fromWire", 36),
81 InvalidRdataLength);
82 // incomplete name. the error should be detected in the name constructor
83 EXPECT_THROW(rdataFactoryFromFile(RRType("NS"), RRClass("IN"),
84 "rdata_ns_fromWire", 71),
85 DNSMessageFORMERR);
86
87 EXPECT_EQ(0, generic::NS("ns2.example.com.").compare(
88 *rdataFactoryFromFile(RRType("NS"), RRClass("IN"),
89 "rdata_ns_fromWire", 55)));
90 EXPECT_THROW(*rdataFactoryFromFile(RRType("NS"), RRClass("IN"),
91 "rdata_ns_fromWire", 63),
92 InvalidRdataLength);
93 }
94
TEST_F(Rdata_NS_Test,createFromLexer)95 TEST_F(Rdata_NS_Test, createFromLexer) {
96 EXPECT_EQ(0, rdata_ns.compare(
97 *test::createRdataUsingLexer(RRType::NS(), RRClass::IN(),
98 "ns.example.com.")));
99
100 // test::createRdataUsingLexer() constructs relative to
101 // "example.org." origin.
102 EXPECT_EQ(0, generic::NS("ns8.example.org.").compare(
103 *test::createRdataUsingLexer(RRType::NS(), RRClass::IN(),
104 "ns8")));
105
106 // Exceptions cause NULL to be returned.
107 EXPECT_FALSE(test::createRdataUsingLexer(RRType::NS(), RRClass::IN(),
108 ""));
109
110 // Extra input at end of line
111 EXPECT_FALSE(test::createRdataUsingLexer(RRType::NS(), RRClass::IN(),
112 "ns.example.com. extra."));
113 }
114
TEST_F(Rdata_NS_Test,toWireBuffer)115 TEST_F(Rdata_NS_Test, toWireBuffer) {
116 rdata_ns.toWire(obuffer);
117 matchWireData(wiredata_ns, sizeof(wiredata_ns),
118 obuffer.getData(), obuffer.getLength());
119 }
120
TEST_F(Rdata_NS_Test,toWireRenderer)121 TEST_F(Rdata_NS_Test, toWireRenderer) {
122 rdata_ns.toWire(renderer);
123 matchWireData(wiredata_ns, sizeof(wiredata_ns),
124 renderer.getData(), renderer.getLength());
125
126 rdata_ns2.toWire(renderer);
127 matchWireData(wiredata_ns2, sizeof(wiredata_ns2),
128 renderer.getData(), renderer.getLength());
129 }
130
TEST_F(Rdata_NS_Test,toText)131 TEST_F(Rdata_NS_Test, toText) {
132 EXPECT_EQ("ns.example.com.", rdata_ns.toText());
133 }
134
TEST_F(Rdata_NS_Test,compare)135 TEST_F(Rdata_NS_Test, compare) {
136 generic::NS small("a.example.");
137 generic::NS large("example.");
138 EXPECT_TRUE(Name("a.example") > Name("example"));
139 EXPECT_GT(0, small.compare(large));
140 }
141
TEST_F(Rdata_NS_Test,getNSName)142 TEST_F(Rdata_NS_Test, getNSName) {
143 EXPECT_EQ(Name("ns.example.com."), rdata_ns.getNSName());
144 }
145 }
146