10 TEST_F(LibraryTest, ParseRootName) {
12 pkt.set_qid(0x1234).set_response().set_aa()
13 .add_question(new DNSQuestion(".", ns_t_a))
14 .add_answer(new DNSARR(".", 100, {0x02, 0x03, 0x04, 0x05}));
15 std::vector<byte> data = pkt.data();
17 struct hostent *host = nullptr;
18 struct ares_addrttl info[2];
20 EXPECT_EQ(ARES_SUCCESS, ares_parse_a_reply(data.data(), data.size(),
21 &host, info, &count));
25 EXPECT_EQ("{'' aliases=[] addrs=[2.3.4.5]}", ss.str());
26 ares_free_hostent(host);
29 TEST_F(LibraryTest, ParseIndirectRootName) {
30 std::vector<byte> data = {
32 0x84, // response + query + AA + not-TC + not-RD
33 0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError
34 0x00, 0x01, // num questions
35 0x00, 0x01, // num answer RRs
36 0x00, 0x00, // num authority RRs
37 0x00, 0x00, // num additional RRs
39 0xC0, 0x04, // weird: pointer to a random zero earlier in the message
41 0x00, 0x01, // class IN
44 0x00, 0x01, // RR type
45 0x00, 0x01, // class IN
46 0x01, 0x02, 0x03, 0x04, // TTL
47 0x00, 0x04, // rdata length
48 0x02, 0x03, 0x04, 0x05,
51 struct hostent *host = nullptr;
52 struct ares_addrttl info[2];
54 EXPECT_EQ(ARES_SUCCESS, ares_parse_a_reply(data.data(), data.size(),
55 &host, info, &count));
59 EXPECT_EQ("{'' aliases=[] addrs=[2.3.4.5]}", ss.str());
60 ares_free_hostent(host);
63 TEST_F(LibraryTest, ParseEscapedName) {
64 std::vector<byte> data = {
66 0x84, // response + query + AA + not-TC + not-RD
67 0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError
68 0x00, 0x01, // num questions
69 0x00, 0x01, // num answer RRs
70 0x00, 0x00, // num authority RRs
71 0x00, 0x00, // num additional RRs
73 0x05, 'a', '\\', 'b', '.', 'c',
77 0x00, 0x01, // class IN
79 0x05, 'a', '\\', 'b', '.', 'c',
82 0x00, 0x01, // RR type
83 0x00, 0x01, // class IN
84 0x01, 0x02, 0x03, 0x04, // TTL
85 0x00, 0x04, // rdata length
86 0x02, 0x03, 0x04, 0x05,
88 struct hostent *host = nullptr;
89 struct ares_addrttl info[2];
91 EXPECT_EQ(ARES_SUCCESS, ares_parse_a_reply(data.data(), data.size(),
92 &host, info, &count));
97 // The printable name is expanded with escapes.
98 EXPECT_EQ(11, hent.name_.size());
99 EXPECT_EQ('a', hent.name_[0]);
100 EXPECT_EQ('\\', hent.name_[1]);
101 EXPECT_EQ('\\', hent.name_[2]);
102 EXPECT_EQ('b', hent.name_[3]);
103 EXPECT_EQ('\\', hent.name_[4]);
104 EXPECT_EQ('.', hent.name_[5]);
105 EXPECT_EQ('c', hent.name_[6]);
106 ares_free_hostent(host);
109 TEST_F(LibraryTest, ParsePartialCompressedName) {
110 std::vector<byte> data = {
112 0x84, // response + query + AA + not-TC + not-RD
113 0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError
114 0x00, 0x01, // num questions
115 0x00, 0x01, // num answer RRs
116 0x00, 0x00, // num authority RRs
117 0x00, 0x00, // num additional RRs
120 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
123 0x00, 0x01, // type A
124 0x00, 0x01, // class IN
127 0xc0, 0x10, // offset 16
128 0x00, 0x01, // RR type
129 0x00, 0x01, // class IN
130 0x01, 0x02, 0x03, 0x04, // TTL
131 0x00, 0x04, // rdata length
132 0x02, 0x03, 0x04, 0x05,
134 struct hostent *host = nullptr;
135 struct ares_addrttl info[2];
137 EXPECT_EQ(ARES_SUCCESS, ares_parse_a_reply(data.data(), data.size(),
138 &host, info, &count));
139 ASSERT_NE(nullptr, host);
140 std::stringstream ss;
142 EXPECT_EQ("{'www.example.com' aliases=[] addrs=[2.3.4.5]}", ss.str());
143 ares_free_hostent(host);
146 TEST_F(LibraryTest, ParseFullyCompressedName) {
147 std::vector<byte> data = {
149 0x84, // response + query + AA + not-TC + not-RD
150 0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError
151 0x00, 0x01, // num questions
152 0x00, 0x01, // num answer RRs
153 0x00, 0x00, // num authority RRs
154 0x00, 0x00, // num additional RRs
157 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
160 0x00, 0x01, // type A
161 0x00, 0x01, // class IN
163 0xc0, 0x0c, // offset 12
164 0x00, 0x01, // RR type
165 0x00, 0x01, // class IN
166 0x01, 0x02, 0x03, 0x04, // TTL
167 0x00, 0x04, // rdata length
168 0x02, 0x03, 0x04, 0x05,
170 struct hostent *host = nullptr;
171 struct ares_addrttl info[2];
173 EXPECT_EQ(ARES_SUCCESS, ares_parse_a_reply(data.data(), data.size(),
174 &host, info, &count));
175 ASSERT_NE(nullptr, host);
176 std::stringstream ss;
178 EXPECT_EQ("{'www.example.com' aliases=[] addrs=[2.3.4.5]}", ss.str());
179 ares_free_hostent(host);
182 TEST_F(LibraryTest, ParseFullyCompressedName2) {
183 std::vector<byte> data = {
185 0x84, // response + query + AA + not-TC + not-RD
186 0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError
187 0x00, 0x01, // num questions
188 0x00, 0x01, // num answer RRs
189 0x00, 0x00, // num authority RRs
190 0x00, 0x00, // num additional RRs
192 0xC0, 0x12, // pointer to later in message
193 0x00, 0x01, // type A
194 0x00, 0x01, // class IN
197 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
200 0x00, 0x01, // RR type
201 0x00, 0x01, // class IN
202 0x01, 0x02, 0x03, 0x04, // TTL
203 0x00, 0x04, // rdata length
204 0x02, 0x03, 0x04, 0x05,
206 struct hostent *host = nullptr;
207 struct ares_addrttl info[2];
209 EXPECT_EQ(ARES_SUCCESS, ares_parse_a_reply(data.data(), data.size(),
210 &host, info, &count));
211 ASSERT_NE(nullptr, host);
212 std::stringstream ss;
214 EXPECT_EQ("{'www.example.com' aliases=[] addrs=[2.3.4.5]}", ss.str());
215 ares_free_hostent(host);