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
|
#include "ares-test.h"
#include "dns-proto.h"
#include <sstream>
#include <vector>
namespace ares {
namespace test {
TEST_F(LibraryTest, ParseAaaaReplyOK) {
DNSPacket pkt;
pkt.set_qid(0x1234).set_response().set_aa()
.add_question(new DNSQuestion("example.com", T_AAAA))
.add_answer(new DNSAaaaRR("example.com", 100,
{0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04}))
.add_answer(new DNSARR("example.com", 0x01020304, {2,3,4,5}));
std::vector<byte> data = pkt.data();
struct hostent *host = nullptr;
struct ares_addr6ttl info[5];
int count = 5;
EXPECT_EQ(ARES_SUCCESS, ares_parse_aaaa_reply(data.data(), data.size(),
&host, info, &count));
EXPECT_EQ(1, count);
EXPECT_EQ(100, info[0].ttl);
EXPECT_EQ(0x01, info[0].ip6addr._S6_un._S6_u8[0]);
EXPECT_EQ(0x02, info[0].ip6addr._S6_un._S6_u8[4]);
ASSERT_NE(nullptr, host);
std::stringstream ss;
ss << HostEnt(host);
EXPECT_EQ("{'example.com' aliases=[] addrs=[0101:0101:0202:0202:0303:0303:0404:0404]}", ss.str());
ares_free_hostent(host);
// Repeat without providing places to put the results
count = 0;
EXPECT_EQ(ARES_SUCCESS, ares_parse_aaaa_reply(data.data(), data.size(),
nullptr, info, &count));
}
TEST_F(LibraryTest, ParseAaaaReplyCname) {
DNSPacket pkt;
pkt.set_qid(0x1234).set_response().set_aa()
.add_question(new DNSQuestion("example.com", T_AAAA))
.add_answer(new DNSCnameRR("example.com", 50, "c.example.com"))
.add_answer(new DNSAaaaRR("c.example.com", 100,
{0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04}));
std::vector<byte> data = pkt.data();
struct hostent *host = nullptr;
struct ares_addr6ttl info[5];
int count = 5;
EXPECT_EQ(ARES_SUCCESS, ares_parse_aaaa_reply(data.data(), data.size(),
&host, info, &count));
EXPECT_EQ(1, count);
// CNAME TTL overrides AAAA TTL.
EXPECT_EQ(50, info[0].ttl);
EXPECT_EQ(0x01, info[0].ip6addr._S6_un._S6_u8[0]);
EXPECT_EQ(0x02, info[0].ip6addr._S6_un._S6_u8[4]);
ASSERT_NE(nullptr, host);
std::stringstream ss;
ss << HostEnt(host);
EXPECT_EQ("{'c.example.com' aliases=[example.com] addrs=[0101:0101:0202:0202:0303:0303:0404:0404]}", ss.str());
ares_free_hostent(host);
// Repeat without providing a hostent
count = 5;
EXPECT_EQ(ARES_SUCCESS, ares_parse_aaaa_reply(data.data(), data.size(),
nullptr, info, &count));
EXPECT_EQ(1, count);
EXPECT_EQ(50, info[0].ttl);
EXPECT_EQ(0x01, info[0].ip6addr._S6_un._S6_u8[0]);
EXPECT_EQ(0x02, info[0].ip6addr._S6_un._S6_u8[4]);
}
TEST_F(LibraryTest, ParseAaaaReplyNoData) {
DNSPacket pkt;
pkt.set_qid(0x1234).set_response().set_aa()
.add_question(new DNSQuestion("example.com", T_AAAA));
std::vector<byte> data = pkt.data();
struct hostent *host = nullptr;
struct ares_addr6ttl info[2];
int count = 2;
EXPECT_EQ(ARES_ENODATA, ares_parse_aaaa_reply(data.data(), data.size(),
&host, info, &count));
EXPECT_EQ(0, count);
EXPECT_EQ(nullptr, host);
// Again but with a CNAME.
pkt.add_answer(new DNSCnameRR("example.com", 200, "c.example.com"));
EXPECT_EQ(ARES_ENODATA, ares_parse_aaaa_reply(data.data(), data.size(),
&host, info, &count));
EXPECT_EQ(0, count);
EXPECT_EQ(nullptr, host);
}
TEST_F(LibraryTest, ParseAaaaReplyErrors) {
DNSPacket pkt;
pkt.set_qid(0x1234).set_response().set_aa()
.add_question(new DNSQuestion("example.com", T_AAAA))
.add_answer(new DNSAaaaRR("example.com", 100,
{0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04}));
std::vector<byte> data;
struct hostent *host = nullptr;
struct ares_addr6ttl info[2];
int count = 2;
// No question.
pkt.questions_.clear();
data = pkt.data();
EXPECT_EQ(ARES_EBADRESP, ares_parse_aaaa_reply(data.data(), data.size(),
&host, info, &count));
EXPECT_EQ(nullptr, host);
pkt.add_question(new DNSQuestion("example.com", T_AAAA));
// Question != answer
pkt.questions_.clear();
pkt.add_question(new DNSQuestion("Axample.com", T_AAAA));
data = pkt.data();
EXPECT_EQ(ARES_ENODATA, ares_parse_aaaa_reply(data.data(), data.size(),
&host, info, &count));
EXPECT_EQ(nullptr, host);
pkt.questions_.clear();
pkt.add_question(new DNSQuestion("example.com", T_AAAA));
// Two questions.
pkt.add_question(new DNSQuestion("example.com", T_AAAA));
data = pkt.data();
EXPECT_EQ(ARES_EBADRESP, ares_parse_aaaa_reply(data.data(), data.size(),
&host, info, &count));
EXPECT_EQ(nullptr, host);
pkt.questions_.clear();
pkt.add_question(new DNSQuestion("example.com", T_AAAA));
// 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_aaaa_reply(data.data(), data.size(),
&host, info, &count));
EXPECT_EQ(nullptr, host);
pkt.answers_.clear();
pkt.add_answer(new DNSAaaaRR("example.com", 100,
{0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04}));
// No answer.
pkt.answers_.clear();
data = pkt.data();
EXPECT_EQ(ARES_ENODATA, ares_parse_aaaa_reply(data.data(), data.size(),
&host, info, &count));
EXPECT_EQ(nullptr, host);
pkt.add_answer(new DNSAaaaRR("example.com", 100,
{0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04}));
// Truncated packets.
data = pkt.data();
for (size_t len = 1; len < data.size(); len++) {
EXPECT_EQ(ARES_EBADRESP, ares_parse_aaaa_reply(data.data(), len,
&host, info, &count));
EXPECT_EQ(nullptr, host);
EXPECT_EQ(ARES_EBADRESP, ares_parse_aaaa_reply(data.data(), len,
nullptr, info, &count));
}
}
TEST_F(LibraryTest, ParseAaaaReplyAllocFail) {
DNSPacket pkt;
pkt.set_qid(0x1234).set_response().set_aa()
.add_question(new DNSQuestion("example.com", T_AAAA))
.add_answer(new DNSCnameRR("example.com", 300, "c.example.com"))
.add_answer(new DNSAaaaRR("c.example.com", 100,
{0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04}));
std::vector<byte> data = pkt.data();
struct hostent *host = nullptr;
struct ares_addr6ttl info[2];
int count = 2;
for (int ii = 1; ii <= 8; ii++) {
ClearFails();
SetAllocFail(ii);
EXPECT_EQ(ARES_ENOMEM, ares_parse_aaaa_reply(data.data(), data.size(),
&host, info, &count)) << ii;
EXPECT_EQ(nullptr, host);
}
}
} // namespace test
} // namespace ares
|