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
|
// 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.
#include <vector>
#include <sstream>
#include <exceptions/exceptions.h>
#include <util/io/buffer.h>
#include <dns/exceptions.h>
#include <dns/messagerenderer.h>
#include <dns/name.h>
#include <dns/question.h>
#include <dns/rrclass.h>
#include <dns/rrtype.h>
#include <gtest/gtest.h>
#include <dns/tests/unittest_util.h>
using isc::UnitTestUtil;
using namespace std;
using namespace isc::dns;
using namespace isc::util::io;
namespace {
class QuestionTest : public ::testing::Test {
protected:
QuestionTest() : obuffer(0), renderer(obuffer),
example_name1(Name("foo.example.com")),
example_name2(Name("bar.example.com")),
test_question1(example_name1, RRClass::IN(),
RRType::NS()),
test_question2(example_name2, RRClass::CH(),
RRType::A())
{}
OutputBuffer obuffer;
MessageRenderer renderer;
Name example_name1;
Name example_name2;
Question test_question1;
Question test_question2;
vector<unsigned char> wiredata;
};
Question
questionFromWire(const char* datafile, size_t position = 0) {
vector<unsigned char> data;
UnitTestUtil::readWireData(datafile, data);
InputBuffer buffer(&data[0], data.size());
buffer.setPosition(position);
return (Question(buffer));
}
TEST_F(QuestionTest, fromWire) {
Question q = questionFromWire("question_fromWire");
EXPECT_EQ(example_name1, q.getName());
EXPECT_EQ(RRClass::IN(), q.getClass());
EXPECT_EQ(RRType::NS(), q.getType());
// owner name of the second Question is compressed. It's uncommon
// (to have multiple questions), but isn't prohibited by the protocol.
q = questionFromWire("question_fromWire", 21);
EXPECT_EQ(example_name2, q.getName());
EXPECT_EQ(RRClass::CH(), q.getClass());
EXPECT_EQ(RRType::A(), q.getType());
// Pathological cases: Corresponding exceptions will be thrown from
// the underlying parser.
EXPECT_THROW(questionFromWire("question_fromWire", 31), DNSMessageFORMERR);
EXPECT_THROW(questionFromWire("question_fromWire", 36), IncompleteRRClass);
}
TEST_F(QuestionTest, toText) {
EXPECT_EQ("foo.example.com. IN NS\n", test_question1.toText());
EXPECT_EQ("bar.example.com. CH A\n", test_question2.toText());
}
TEST_F(QuestionTest, toWireBuffer) {
test_question1.toWire(obuffer);
test_question2.toWire(obuffer);
UnitTestUtil::readWireData("question_toWire1", wiredata);
EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData, obuffer.getData(),
obuffer.getLength(), &wiredata[0], wiredata.size());
}
TEST_F(QuestionTest, toWireRenderer) {
test_question1.toWire(renderer);
test_question2.toWire(renderer);
UnitTestUtil::readWireData("question_toWire2", wiredata);
EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData, obuffer.getData(),
obuffer.getLength(), &wiredata[0], wiredata.size());
}
// test operator<<. We simply confirm it appends the result of toText().
TEST_F(QuestionTest, LeftShiftOperator) {
ostringstream oss;
oss << test_question1;
EXPECT_EQ(test_question1.toText(), oss.str());
}
TEST_F(QuestionTest, comparison) {
const Name a("a"), b("b");
const RRClass in(RRClass::IN()), ch(RRClass::CH());
const RRType ns(RRType::NS()), aaaa(RRType::AAAA());
EXPECT_TRUE(Question(a, in, ns) < Question(a, in, aaaa));
EXPECT_FALSE(Question(a, in, aaaa) < Question(a, in, ns));
EXPECT_TRUE(Question(a, in, ns) < Question(a, ch, ns));
EXPECT_FALSE(Question(a, ch, ns) < Question(a, in, ns));
EXPECT_TRUE(Question(a, in, ns) < Question(a, ch, aaaa));
EXPECT_FALSE(Question(a, ch, aaaa) < Question(a, in, ns));
EXPECT_TRUE(Question(a, in, ns) < Question(b, in, ns));
EXPECT_FALSE(Question(a, in, ns) < Question(a, in, ns));
EXPECT_TRUE(Question(a, in, ns) < Question(b, ch, ns));
EXPECT_FALSE(Question(b, ch, ns) < Question(a, in, ns));
EXPECT_TRUE(Question(a, in, ns) < Question(b, ch, aaaa));
EXPECT_FALSE(Question(b, ch, aaaa) < Question(a, in, ns));
EXPECT_FALSE(Question(a, in, ns) < Question(a, in, ns));
EXPECT_FALSE(Question(a, ch, ns) < Question(a, ch, ns));
EXPECT_FALSE(Question(b, in, ns) < Question(b, in, ns));
EXPECT_FALSE(Question(b, in, aaaa) < Question(b, in, aaaa));
// Identical questions are equal
EXPECT_TRUE(Question(a, in, ns) == Question(a, in, ns));
EXPECT_FALSE(Question(a, in, ns) != Question(a, in, ns));
// Components differing by one component are unequal...
EXPECT_FALSE(Question(b, in, ns) == Question(a, in, ns));
EXPECT_TRUE(Question(b, in, ns) != Question(a, in, ns));
EXPECT_FALSE(Question(a, ch, ns) == Question(a, in, ns));
EXPECT_TRUE(Question(a, ch, ns) != Question(a, in, ns));
EXPECT_FALSE(Question(a, in, aaaa) == Question(a, in, ns));
EXPECT_TRUE(Question(a, in, aaaa) != Question(a, in, ns));
// ... as are those differing by two components
EXPECT_FALSE(Question(b, ch, ns) == Question(a, in, ns));
EXPECT_TRUE(Question(b, ch, ns) != Question(a, in, ns));
EXPECT_FALSE(Question(b, in, aaaa) == Question(a, in, ns));
EXPECT_TRUE(Question(b, in, aaaa) != Question(a, in, ns));
EXPECT_FALSE(Question(a, ch, aaaa) == Question(a, in, ns));
EXPECT_TRUE(Question(a, ch, aaaa) != Question(a, in, ns));
// ... and question differing by all three
EXPECT_FALSE(Question(b, ch, aaaa) == Question(a, in, ns));
EXPECT_TRUE(Question(b, ch, aaaa) != Question(a, in, ns));
}
}
|