ares-test-parse-aaaa.cc
Go to the documentation of this file.
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 
10 TEST_F(LibraryTest, ParseAaaaReplyOK) {
11  DNSPacket pkt;
12  pkt.set_qid(0x1234).set_response().set_aa()
13  .add_question(new DNSQuestion("example.com", 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;
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;
37  nullptr, info, &count));
38 }
39 
40 TEST_F(LibraryTest, ParseAaaaReplyCname) {
41  DNSPacket pkt;
42  pkt.set_qid(0x1234).set_response().set_aa()
43  .add_question(new DNSQuestion("example.com", 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;
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;
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 
75 TEST_F(LibraryTest, ParseAaaaReplyNoData) {
76  DNSPacket pkt;
77  pkt.set_qid(0x1234).set_response().set_aa()
78  .add_question(new DNSQuestion("example.com", T_AAAA));
79  std::vector<byte> data = pkt.data();
80  struct hostent *host = nullptr;
81  struct ares_addr6ttl info[2];
82  int count = 2;
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"));
91  &host, info, &count));
92  EXPECT_EQ(0, count);
93  EXPECT_EQ(nullptr, host);
94 }
95 
96 TEST_F(LibraryTest, ParseAaaaReplyErrors) {
97  DNSPacket pkt;
98  pkt.set_qid(0x1234).set_response().set_aa()
99  .add_question(new DNSQuestion("example.com", 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();
113  &host, info, &count));
114  EXPECT_EQ(nullptr, host);
115  pkt.add_question(new DNSQuestion("example.com", T_AAAA));
116 
117  // Question != answer
118  pkt.questions_.clear();
119  pkt.add_question(new DNSQuestion("Axample.com", T_AAAA));
120  data = pkt.data();
122  &host, info, &count));
123  EXPECT_EQ(nullptr, host);
124  pkt.questions_.clear();
125  pkt.add_question(new DNSQuestion("example.com", T_AAAA));
126 
127  // Two questions.
128  pkt.add_question(new DNSQuestion("example.com", T_AAAA));
129  data = pkt.data();
131  &host, info, &count));
132  EXPECT_EQ(nullptr, host);
133  pkt.questions_.clear();
134  pkt.add_question(new DNSQuestion("example.com", 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();
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();
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++) {
162  &host, info, &count));
163  EXPECT_EQ(nullptr, host);
165  nullptr, info, &count));
166  }
167 }
168 
169 TEST_F(LibraryTest, ParseAaaaReplyAllocFail) {
170  DNSPacket pkt;
171  pkt.set_qid(0x1234).set_response().set_aa()
172  .add_question(new DNSQuestion("example.com", 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);
186  &host, info, &count)) << ii;
187  EXPECT_EQ(nullptr, host);
188  }
189 }
190 
191 } // namespace test
192 } // namespace ares
ASSERT_NE
#define ASSERT_NE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2060
ARES_ENOMEM
#define ARES_ENOMEM
Definition: ares.h:117
ares::DNSPacket
Definition: dns-proto.h:188
ares::DNSPacket::set_aa
DNSPacket & set_aa(bool v=true)
Definition: dns-proto.h:213
test
Definition: spinlock_test.cc:36
ares::DNSPacket::set_qid
DNSPacket & set_qid(int qid)
Definition: dns-proto.h:211
ares::DNSAaaaRR
Definition: dns-proto.h:85
ares_in6_addr::_S6_u8
unsigned char _S6_u8[16]
Definition: ares.h:516
ares::DNSPacket::answers_
std::vector< std::unique_ptr< DNSRR > > answers_
Definition: dns-proto.h:237
ares_addr6ttl
Definition: ares.h:525
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
ares::DNSPacket::add_question
DNSPacket & add_question(DNSQuestion *q)
Definition: dns-proto.h:194
ARES_EBADRESP
#define ARES_EBADRESP
Definition: ares.h:112
ares_free_hostent
CARES_EXTERN void ares_free_hostent(struct hostent *host)
Definition: ares_free_hostent.c:26
ares::DNSCnameRR
Definition: dns-proto.h:100
ARES_ENODATA
#define ARES_ENODATA
Definition: ares.h:101
ares_addr6ttl::ip6addr
struct ares_in6_addr ip6addr
Definition: ares.h:526
T_AAAA
#define T_AAAA
Definition: ares_nameser.h:391
ares::DNSPacket::data
std::vector< byte > data() const
Definition: dns-proto.cc:592
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
ares-test.h
ARES_SUCCESS
#define ARES_SUCCESS
Definition: ares.h:98
ares::test::HostEnt
Definition: ares-test.h:237
ares::DNSPacket::add_answer
DNSPacket & add_answer(DNSRR *q)
Definition: dns-proto.h:198
ares::DNSPacket::questions_
std::vector< std::unique_ptr< DNSQuestion > > questions_
Definition: dns-proto.h:236
count
int * count
Definition: bloaty/third_party/googletest/googlemock/test/gmock_stress_test.cc:96
ares::DNSPacket::set_response
DNSPacket & set_response(bool v=true)
Definition: dns-proto.h:212
ares_addr6ttl::ttl
int ttl
Definition: ares.h:527
ares::DNSARR
Definition: dns-proto.h:78
ares::DNSMxRR
Definition: dns-proto.h:122
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
ares::test::TEST_F
TEST_F(LibraryTest, OptionsChannelInit)
Definition: ares-test-init.cc:55
ares::DNSQuestion
Definition: dns-proto.h:45
ares
Definition: ares-test-ai.h:9
ares::test::LibraryTest
Definition: ares-test.h:60
ares_parse_aaaa_reply
CARES_EXTERN int ares_parse_aaaa_reply(const unsigned char *abuf, int alen, struct hostent **host, struct ares_addr6ttl *addrttls, int *naddrttls)
Definition: ares_parse_aaaa_reply.c:46
ares_in6_addr::_S6_un
union ares_in6_addr::@381 _S6_un
dns-proto.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:42