forked from c-ares/c-ares
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ares-test-parse-naptr.cc
287 lines (256 loc) · 10.6 KB
/
ares-test-parse-naptr.cc
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include "ares-test.h"
#include "dns-proto.h"
#include <sstream>
#include <vector>
namespace ares {
namespace test {
TEST_F(LibraryTest, ParseNaptrReplyOK) {
DNSPacket pkt;
pkt.set_qid(0x1234).set_response().set_aa()
.add_question(new DNSQuestion("example.com", T_NAPTR))
.add_answer(new DNSNaptrRR("example.com", 100,
10, 20, "SP", "service", "regexp", "replace"))
.add_answer(new DNSNaptrRR("example.com", 0x0010,
11, 21, "SP", "service2", "regexp2", "replace2"));
std::vector<byte> data = pkt.data();
struct ares_naptr_reply* naptr = nullptr;
EXPECT_EQ(ARES_SUCCESS, ares_parse_naptr_reply(data.data(), data.size(), &naptr));
ASSERT_NE(nullptr, naptr);
EXPECT_EQ("SP", std::string((char*)naptr->flags));
EXPECT_EQ("service", std::string((char*)naptr->service));
EXPECT_EQ("regexp", std::string((char*)naptr->regexp));
EXPECT_EQ("replace", std::string((char*)naptr->replacement));
EXPECT_EQ(10, naptr->order);
EXPECT_EQ(20, naptr->preference);
struct ares_naptr_reply* naptr2 = naptr->next;
ASSERT_NE(nullptr, naptr2);
EXPECT_EQ("SP", std::string((char*)naptr2->flags));
EXPECT_EQ("service2", std::string((char*)naptr2->service));
EXPECT_EQ("regexp2", std::string((char*)naptr2->regexp));
EXPECT_EQ("replace2", std::string((char*)naptr2->replacement));
EXPECT_EQ(11, naptr2->order);
EXPECT_EQ(21, naptr2->preference);
EXPECT_EQ(nullptr, naptr2->next);
ares_free_data(naptr);
}
TEST_F(LibraryTest, ParseCNaptrReplyOK) {
DNSPacket pkt;
pkt.set_qid(0x1234).set_response().set_aa()
.add_question(new DNSQuestion("example.com", T_NAPTR))
.add_answer(new DNSNaptrRR("example.com", 100,
10, 20, "SP", "service", "regexp", "replace"))
.add_answer(new DNSNaptrRR("example.com", 0x0010,
11, 21, "SP", "service2", "regexp2", "replace2"));
std::vector<byte> data = pkt.data();
cares_naptr_reply* naptr = nullptr;
EXPECT_EQ(ARES_SUCCESS, cares_parse_naptr_reply(data.data(), data.size(), &naptr));
ASSERT_NE(nullptr, naptr);
EXPECT_EQ("SP", std::string((char*)cares_naptr_reply_get_flags(naptr)));
EXPECT_EQ("service", std::string((char*)cares_naptr_reply_get_service(naptr)));
EXPECT_EQ("regexp", std::string((char*)cares_naptr_reply_get_regexp(naptr)));
EXPECT_EQ("replace", std::string((char*)cares_naptr_reply_get_replacement(naptr)));
EXPECT_EQ(10, cares_naptr_reply_get_order(naptr));
EXPECT_EQ(20, cares_naptr_reply_get_preference(naptr));
EXPECT_EQ(100, cares_naptr_reply_get_ttl(naptr));
cares_naptr_reply* naptr2 = cares_naptr_reply_get_next(naptr);
ASSERT_NE(nullptr, naptr2);
EXPECT_EQ("SP", std::string((char*)cares_naptr_reply_get_flags(naptr2)));
EXPECT_EQ("service2", std::string((char*)cares_naptr_reply_get_service(naptr2)));
EXPECT_EQ("regexp2", std::string((char*)cares_naptr_reply_get_regexp(naptr2)));
EXPECT_EQ("replace2", std::string((char*)cares_naptr_reply_get_replacement(naptr2)));
EXPECT_EQ(11, cares_naptr_reply_get_order(naptr2));
EXPECT_EQ(21, cares_naptr_reply_get_preference(naptr2));
EXPECT_EQ(nullptr, cares_naptr_reply_get_next(naptr2));
EXPECT_EQ(16, cares_naptr_reply_get_ttl(naptr2));
ares_free_data(naptr);
}
TEST_F(LibraryTest, ParseNaptrReplyErrors) {
DNSPacket pkt;
pkt.set_qid(0x1234).set_response().set_aa()
.add_question(new DNSQuestion("example.com", T_NAPTR))
.add_answer(new DNSNaptrRR("example.com", 100,
10, 20, "SP", "service", "regexp", "replace"));
std::vector<byte> data;
struct ares_naptr_reply* naptr = nullptr;
// No question.
pkt.questions_.clear();
data = pkt.data();
EXPECT_EQ(ARES_EBADRESP, ares_parse_naptr_reply(data.data(), data.size(), &naptr));
pkt.add_question(new DNSQuestion("example.com", T_NAPTR));
#ifdef DISABLED
// Question != answer
pkt.questions_.clear();
pkt.add_question(new DNSQuestion("Axample.com", T_NAPTR));
data = pkt.data();
EXPECT_EQ(ARES_ENODATA, ares_parse_naptr_reply(data.data(), data.size(), &naptr));
pkt.questions_.clear();
pkt.add_question(new DNSQuestion("example.com", T_NAPTR));
#endif
// Two questions
pkt.add_question(new DNSQuestion("example.com", T_NAPTR));
data = pkt.data();
EXPECT_EQ(ARES_EBADRESP, ares_parse_naptr_reply(data.data(), data.size(), &naptr));
pkt.questions_.clear();
pkt.add_question(new DNSQuestion("example.com", T_NAPTR));
// Wrong sort of answer.
pkt.answers_.clear();
pkt.add_answer(new DNSMxRR("example.com", 100, 100, "mx1.example.com"));
data = pkt.data();
EXPECT_EQ(ARES_ENODATA, ares_parse_naptr_reply(data.data(), data.size(), &naptr));
EXPECT_EQ(nullptr, naptr);
pkt.answers_.clear();
pkt.add_answer(new DNSNaptrRR("example.com", 100,
10, 20, "SP", "service", "regexp", "replace"));
// No answer.
pkt.answers_.clear();
data = pkt.data();
EXPECT_EQ(ARES_ENODATA, ares_parse_naptr_reply(data.data(), data.size(), &naptr));
pkt.add_answer(new DNSNaptrRR("example.com", 100,
10, 20, "SP", "service", "regexp", "replace"));
// Truncated packets.
data = pkt.data();
for (size_t len = 1; len < data.size(); len++) {
int rc = ares_parse_naptr_reply(data.data(), len, &naptr);
EXPECT_TRUE(rc == ARES_EBADRESP || rc == ARES_EBADNAME);
}
}
TEST_F(LibraryTest, ParseCNaptrReplyErrors) {
DNSPacket pkt;
pkt.set_qid(0x1234).set_response().set_aa()
.add_question(new DNSQuestion("example.com", T_NAPTR))
.add_answer(new DNSNaptrRR("example.com", 100,
10, 20, "SP", "service", "regexp", "replace"));
std::vector<byte> data;
cares_naptr_reply* naptr = nullptr;
// No question.
pkt.questions_.clear();
data = pkt.data();
EXPECT_EQ(ARES_EBADRESP, cares_parse_naptr_reply(data.data(), data.size(), &naptr));
pkt.add_question(new DNSQuestion("example.com", T_NAPTR));
#ifdef DISABLED
// Question != answer
pkt.questions_.clear();
pkt.add_question(new DNSQuestion("Axample.com", T_NAPTR));
data = pkt.data();
EXPECT_EQ(ARES_ENODATA, cares_parse_naptr_reply(data.data(), data.size(), &naptr));
pkt.questions_.clear();
pkt.add_question(new DNSQuestion("example.com", T_NAPTR));
#endif
// Two questions
pkt.add_question(new DNSQuestion("example.com", T_NAPTR));
data = pkt.data();
EXPECT_EQ(ARES_EBADRESP, cares_parse_naptr_reply(data.data(), data.size(), &naptr));
pkt.questions_.clear();
pkt.add_question(new DNSQuestion("example.com", T_NAPTR));
// Wrong sort of answer.
pkt.answers_.clear();
pkt.add_answer(new DNSMxRR("example.com", 100, 100, "mx1.example.com"));
data = pkt.data();
EXPECT_EQ(ARES_ENODATA, cares_parse_naptr_reply(data.data(), data.size(), &naptr));
EXPECT_EQ(nullptr, naptr);
pkt.answers_.clear();
pkt.add_answer(new DNSNaptrRR("example.com", 100,
10, 20, "SP", "service", "regexp", "replace"));
// No answer.
pkt.answers_.clear();
data = pkt.data();
EXPECT_EQ(ARES_ENODATA, cares_parse_naptr_reply(data.data(), data.size(), &naptr));
pkt.add_answer(new DNSNaptrRR("example.com", 100,
10, 20, "SP", "service", "regexp", "replace"));
// Truncated packets.
data = pkt.data();
for (size_t len = 1; len < data.size(); len++) {
int rc = cares_parse_naptr_reply(data.data(), len, &naptr);
EXPECT_TRUE(rc == ARES_EBADRESP || rc == ARES_EBADNAME);
}
}
TEST_F(LibraryTest, ParseNaptrReplyTooShort) {
std::vector<byte> data = {
0x12, 0x34, // qid
0x84, // response + query + AA + not-TC + not-RD
0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError
0x00, 0x01, // num questions
0x00, 0x01, // num answer RRs
0x00, 0x00, // num authority RRs
0x00, 0x00, // num additional RRs
// Question
0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
0x03, 'c', 'o', 'm',
0x00,
0x00, 0x23, // type NAPTR
0x00, 0x01, // class IN
// Answer 1
0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
0x03, 'c', 'o', 'm',
0x00,
0x00, 0x23, // RR type
0x00, 0x01, // class IN
0x01, 0x02, 0x03, 0x04, // TTL
0x00, 0x01, // rdata length
0x00, // Too short: expect 2 x int16 and 3 x name (min 1 byte each)
};
struct ares_naptr_reply* naptr = nullptr;
EXPECT_EQ(ARES_EBADRESP, ares_parse_naptr_reply(data.data(), data.size(), &naptr));
}
TEST_F(LibraryTest, ParseCNaptrReplyTooShort) {
std::vector<byte> data = {
0x12, 0x34, // qid
0x84, // response + query + AA + not-TC + not-RD
0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError
0x00, 0x01, // num questions
0x00, 0x01, // num answer RRs
0x00, 0x00, // num authority RRs
0x00, 0x00, // num additional RRs
// Question
0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
0x03, 'c', 'o', 'm',
0x00,
0x00, 0x23, // type NAPTR
0x00, 0x01, // class IN
// Answer 1
0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
0x03, 'c', 'o', 'm',
0x00,
0x00, 0x23, // RR type
0x00, 0x01, // class IN
0x01, 0x02, 0x03, 0x04, // TTL
0x00, 0x01, // rdata length
0x00, // Too short: expect 2 x int16 and 3 x name (min 1 byte each)
};
cares_naptr_reply* naptr = nullptr;
EXPECT_EQ(ARES_EBADRESP, cares_parse_naptr_reply(data.data(), data.size(), &naptr));
}
TEST_F(LibraryTest, ParseNaptrReplyAllocFail) {
DNSPacket pkt;
pkt.set_qid(0x1234).set_response().set_aa()
.add_question(new DNSQuestion("example.com", T_NAPTR))
.add_answer(new DNSNaptrRR("example.com", 100,
10, 20, "SP", "service", "regexp", "replace"))
.add_answer(new DNSNaptrRR("example.com", 0x0010,
11, 21, "SP", "service2", "regexp2", "replace2"));
std::vector<byte> data = pkt.data();
struct ares_naptr_reply* naptr = nullptr;
for (int ii = 1; ii <= 13; ii++) {
ClearFails();
SetAllocFail(ii);
EXPECT_EQ(ARES_ENOMEM, ares_parse_naptr_reply(data.data(), data.size(), &naptr));
}
}
TEST_F(LibraryTest, ParseCNaptrReplyAllocFail) {
DNSPacket pkt;
pkt.set_qid(0x1234).set_response().set_aa()
.add_question(new DNSQuestion("example.com", T_NAPTR))
.add_answer(new DNSNaptrRR("example.com", 100,
10, 20, "SP", "service", "regexp", "replace"))
.add_answer(new DNSNaptrRR("example.com", 0x0010,
11, 21, "SP", "service2", "regexp2", "replace2"));
std::vector<byte> data = pkt.data();
cares_naptr_reply* naptr = nullptr;
for (int ii = 1; ii <= 13; ii++) {
ClearFails();
SetAllocFail(ii);
EXPECT_EQ(ARES_ENOMEM, cares_parse_naptr_reply(data.data(), data.size(), &naptr));
}
}
} // namespace test
} // namespace ares