• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "ares-test.h"
2 #include "dns-proto.h"
3 
4 #include <sstream>
5 #include <vector>
6 
7 namespace ares {
8 namespace test {
9 
TEST_F(LibraryTest,ParseAaaaReplyOK)10 TEST_F(LibraryTest, ParseAaaaReplyOK) {
11   DNSPacket pkt;
12   pkt.set_qid(0x1234).set_response().set_aa()
13     .add_question(new DNSQuestion("example.com", ns_t_aaaa))
14     .add_answer(new DNSAaaaRR("example.com", 100,
15                               {0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
16                                0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04}))
17     .add_answer(new DNSARR("example.com", 0x01020304, {2,3,4,5}));
18   std::vector<byte> data = pkt.data();
19   struct hostent *host = nullptr;
20   struct ares_addr6ttl info[5];
21   int count = 5;
22   EXPECT_EQ(ARES_SUCCESS, ares_parse_aaaa_reply(data.data(), data.size(),
23                                                 &host, info, &count));
24   EXPECT_EQ(1, count);
25   EXPECT_EQ(100, info[0].ttl);
26   EXPECT_EQ(0x01, info[0].ip6addr._S6_un._S6_u8[0]);
27   EXPECT_EQ(0x02, info[0].ip6addr._S6_un._S6_u8[4]);
28   ASSERT_NE(nullptr, host);
29   std::stringstream ss;
30   ss << HostEnt(host);
31   EXPECT_EQ("{'example.com' aliases=[] addrs=[0101:0101:0202:0202:0303:0303:0404:0404]}", ss.str());
32   ares_free_hostent(host);
33 
34   // Repeat without providing places to put the results
35   count = 0;
36   EXPECT_EQ(ARES_SUCCESS, ares_parse_aaaa_reply(data.data(), data.size(),
37                                                 nullptr, info, &count));
38 }
39 
TEST_F(LibraryTest,ParseAaaaReplyCname)40 TEST_F(LibraryTest, ParseAaaaReplyCname) {
41   DNSPacket pkt;
42   pkt.set_qid(0x1234).set_response().set_aa()
43     .add_question(new DNSQuestion("example.com", ns_t_aaaa))
44     .add_answer(new DNSCnameRR("example.com", 50, "c.example.com"))
45     .add_answer(new DNSAaaaRR("c.example.com", 100,
46                               {0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
47                                0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04}));
48   std::vector<byte> data = pkt.data();
49   struct hostent *host = nullptr;
50   struct ares_addr6ttl info[5];
51   int count = 5;
52   EXPECT_EQ(ARES_SUCCESS, ares_parse_aaaa_reply(data.data(), data.size(),
53                                                 &host, info, &count));
54   EXPECT_EQ(1, count);
55   // CNAME TTL overrides AAAA TTL.
56   EXPECT_EQ(50, info[0].ttl);
57   EXPECT_EQ(0x01, info[0].ip6addr._S6_un._S6_u8[0]);
58   EXPECT_EQ(0x02, info[0].ip6addr._S6_un._S6_u8[4]);
59   ASSERT_NE(nullptr, host);
60   std::stringstream ss;
61   ss << HostEnt(host);
62   EXPECT_EQ("{'c.example.com' aliases=[example.com] addrs=[0101:0101:0202:0202:0303:0303:0404:0404]}", ss.str());
63   ares_free_hostent(host);
64 
65   // Repeat without providing a hostent
66   count = 5;
67   EXPECT_EQ(ARES_SUCCESS, ares_parse_aaaa_reply(data.data(), data.size(),
68                                                 nullptr, info, &count));
69   EXPECT_EQ(1, count);
70   EXPECT_EQ(50, info[0].ttl);
71   EXPECT_EQ(0x01, info[0].ip6addr._S6_un._S6_u8[0]);
72   EXPECT_EQ(0x02, info[0].ip6addr._S6_un._S6_u8[4]);
73 }
74 
TEST_F(LibraryTest,ParseAaaaReplyNoData)75 TEST_F(LibraryTest, ParseAaaaReplyNoData) {
76   DNSPacket pkt;
77   pkt.set_qid(0x1234).set_response().set_aa()
78     .add_question(new DNSQuestion("example.com", ns_t_aaaa));
79   std::vector<byte> data = pkt.data();
80   struct hostent *host = nullptr;
81   struct ares_addr6ttl info[2];
82   int count = 2;
83   EXPECT_EQ(ARES_ENODATA, ares_parse_aaaa_reply(data.data(), data.size(),
84                                                 &host, info, &count));
85   EXPECT_EQ(0, count);
86   EXPECT_EQ(nullptr, host);
87 
88   // Again but with a CNAME.
89   pkt.add_answer(new DNSCnameRR("example.com", 200, "c.example.com"));
90   EXPECT_EQ(ARES_ENODATA, ares_parse_aaaa_reply(data.data(), data.size(),
91                                                 &host, info, &count));
92   EXPECT_EQ(0, count);
93   EXPECT_EQ(nullptr, host);
94 }
95 
TEST_F(LibraryTest,ParseAaaaReplyErrors)96 TEST_F(LibraryTest, ParseAaaaReplyErrors) {
97   DNSPacket pkt;
98   pkt.set_qid(0x1234).set_response().set_aa()
99     .add_question(new DNSQuestion("example.com", ns_t_aaaa))
100     .add_answer(new DNSAaaaRR("example.com", 100,
101                               {0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
102                                0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04}));
103   std::vector<byte> data;
104 
105   struct hostent *host = nullptr;
106   struct ares_addr6ttl info[2];
107   int count = 2;
108 
109   // No question.
110   pkt.questions_.clear();
111   data = pkt.data();
112   EXPECT_EQ(ARES_EBADRESP, ares_parse_aaaa_reply(data.data(), data.size(),
113                                                  &host, info, &count));
114   EXPECT_EQ(nullptr, host);
115   pkt.add_question(new DNSQuestion("example.com", ns_t_aaaa));
116 
117   // Question != answer
118   pkt.questions_.clear();
119   pkt.add_question(new DNSQuestion("Axample.com", ns_t_aaaa));
120   data = pkt.data();
121   EXPECT_EQ(ARES_ENODATA, ares_parse_aaaa_reply(data.data(), data.size(),
122                                                 &host, info, &count));
123   EXPECT_EQ(nullptr, host);
124   pkt.questions_.clear();
125   pkt.add_question(new DNSQuestion("example.com", ns_t_aaaa));
126 
127   // Two questions.
128   pkt.add_question(new DNSQuestion("example.com", ns_t_aaaa));
129   data = pkt.data();
130   EXPECT_EQ(ARES_EBADRESP, ares_parse_aaaa_reply(data.data(), data.size(),
131                                                  &host, info, &count));
132   EXPECT_EQ(nullptr, host);
133   pkt.questions_.clear();
134   pkt.add_question(new DNSQuestion("example.com", ns_t_aaaa));
135 
136   // Wrong sort of answer.
137   pkt.answers_.clear();
138   pkt.add_answer(new DNSMxRR("example.com", 100, 100, "mx1.example.com"));
139   data = pkt.data();
140   EXPECT_EQ(ARES_ENODATA, ares_parse_aaaa_reply(data.data(), data.size(),
141                                                 &host, info, &count));
142   EXPECT_EQ(nullptr, host);
143   pkt.answers_.clear();
144   pkt.add_answer(new DNSAaaaRR("example.com", 100,
145                               {0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
146                                0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04}));
147 
148   // No answer.
149   pkt.answers_.clear();
150   data = pkt.data();
151   EXPECT_EQ(ARES_ENODATA, ares_parse_aaaa_reply(data.data(), data.size(),
152                                                 &host, info, &count));
153   EXPECT_EQ(nullptr, host);
154   pkt.add_answer(new DNSAaaaRR("example.com", 100,
155                               {0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
156                                0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04}));
157 
158   // Truncated packets.
159   data = pkt.data();
160   for (size_t len = 1; len < data.size(); len++) {
161     EXPECT_EQ(ARES_EBADRESP, ares_parse_aaaa_reply(data.data(), len,
162                                                    &host, info, &count));
163     EXPECT_EQ(nullptr, host);
164     EXPECT_EQ(ARES_EBADRESP, ares_parse_aaaa_reply(data.data(), len,
165                                                    nullptr, info, &count));
166   }
167 }
168 
TEST_F(LibraryTest,ParseAaaaReplyAllocFail)169 TEST_F(LibraryTest, ParseAaaaReplyAllocFail) {
170   DNSPacket pkt;
171   pkt.set_qid(0x1234).set_response().set_aa()
172     .add_question(new DNSQuestion("example.com", ns_t_aaaa))
173     .add_answer(new DNSCnameRR("example.com", 300, "c.example.com"))
174     .add_answer(new DNSAaaaRR("c.example.com", 100,
175                               {0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
176                                0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04}));
177   std::vector<byte> data = pkt.data();
178   struct hostent *host = nullptr;
179   struct ares_addr6ttl info[2];
180   int count = 2;
181 
182   for (int ii = 1; ii <= 8; ii++) {
183     ClearFails();
184     SetAllocFail(ii);
185     EXPECT_EQ(ARES_ENOMEM, ares_parse_aaaa_reply(data.data(), data.size(),
186                                                  &host, info, &count)) << ii;
187     EXPECT_EQ(nullptr, host);
188   }
189 }
190 
191 }  // namespace test
192 }  // namespace ares
193