dns-proto-test.cc
Go to the documentation of this file.
1 #include "ares-test.h"
2 #include "dns-proto.h"
3 
4 #include <vector>
5 
6 namespace ares {
7 namespace test {
8 
9 TEST(DNSProto, EncodeQuestions) {
10  DNSPacket pkt;
11  pkt.set_qid(0x1234).set_response().set_aa()
12  .add_question(new DNSQuestion("example.com.", T_A))
13  .add_question(new DNSQuestion("www.example.com", T_AAAA, C_CHAOS));
14 
15  std::vector<byte> data = {
16  0x12, 0x34, // qid
17  0x84, // response + query + AA + not-TC + not-RD
18  0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError
19  0x00, 0x02, // num questions
20  0x00, 0x00, // num answer RRs
21  0x00, 0x00, // num authority RRs
22  0x00, 0x00, // num additional RRs
23  // Question 1
24  0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
25  0x03, 'c', 'o', 'm',
26  0x00,
27  0x00, 0x01, // type A
28  0x00, 0x01, // class IN
29  // Question 2
30  0x03, 'w', 'w', 'w',
31  0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
32  0x03, 'c', 'o', 'm',
33  0x00,
34  0x00, 0x1C, // type AAAA = 28
35  0x00, 0x03, // class CHAOS = 3
36  };
37  EXPECT_EQ(data, pkt.data());
38 }
39 
40 TEST(DNSProto, EncodeSingleNameAnswers) {
41  DNSPacket pkt;
42  pkt.qid_ = 0x1234;
43  pkt.response_ = true;
44  pkt.aa_ = true;
45  pkt.opcode_ = O_QUERY;
46  pkt.add_answer(new DNSCnameRR("example.com", 0x01020304, "other.com."));
47  pkt.add_auth(new DNSPtrRR("www.example.com", 0x01020304, "www.other.com"));
48 
49  std::vector<byte> data = {
50  0x12, 0x34, // qid
51  0x84, // response + query + AA + not-TC + not-RD
52  0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError
53  0x00, 0x00, // num questions
54  0x00, 0x01, // num answer RRs
55  0x00, 0x01, // num authority RRs
56  0x00, 0x00, // num additional RRs
57  // Answer 1
58  0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
59  0x03, 'c', 'o', 'm',
60  0x00,
61  0x00, 0x05, // RR type
62  0x00, 0x01, // class IN
63  0x01, 0x02, 0x03, 0x04, // TTL
64  0x00, 0x0B, // rdata length
65  0x05, 'o', 't', 'h', 'e', 'r',
66  0x03, 'c', 'o', 'm',
67  0x00,
68  // Authority 1
69  0x03, 'w', 'w', 'w',
70  0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
71  0x03, 'c', 'o', 'm',
72  0x00,
73  0x00, 0x0c, // RR type
74  0x00, 0x01, // class IN
75  0x01, 0x02, 0x03, 0x04, // TTL
76  0x00, 0x0F, // rdata length
77  0x03, 'w', 'w', 'w',
78  0x05, 'o', 't', 'h', 'e', 'r',
79  0x03, 'c', 'o', 'm',
80  0x00,
81  };
82  EXPECT_EQ(data, pkt.data());
83 }
84 
85 TEST(DNSProto, EncodeAddressAnswers) {
86  DNSPacket pkt;
87  pkt.qid_ = 0x1234;
88  pkt.response_ = true;
89  pkt.aa_ = true;
90  pkt.opcode_ = O_QUERY;
91  std::vector<byte> addrv4 = {0x02, 0x03, 0x04, 0x05};
92  pkt.add_answer(new DNSARR("example.com", 0x01020304, addrv4));
93  byte addrv6[16] = {0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
94  0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04};
95  pkt.add_additional(new DNSAaaaRR("www.example.com", 0x01020304, addrv6, 16));
96 
97  std::vector<byte> data = {
98  0x12, 0x34, // qid
99  0x84, // response + query + AA + not-TC + not-RD
100  0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError
101  0x00, 0x00, // num questions
102  0x00, 0x01, // num answer RRs
103  0x00, 0x00, // num authority RRs
104  0x00, 0x01, // num additional RRs
105  // Answer 1
106  0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
107  0x03, 'c', 'o', 'm',
108  0x00,
109  0x00, 0x01, // RR type
110  0x00, 0x01, // class IN
111  0x01, 0x02, 0x03, 0x04, // TTL
112  0x00, 0x04, // rdata length
113  0x02, 0x03, 0x04, 0x05,
114  // Additional 1
115  0x03, 'w', 'w', 'w',
116  0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
117  0x03, 'c', 'o', 'm',
118  0x00,
119  0x00, 0x1c, // RR type
120  0x00, 0x01, // class IN
121  0x01, 0x02, 0x03, 0x04, // TTL
122  0x00, 0x10, // rdata length
123  0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
124  0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04
125  };
126  EXPECT_EQ(data, pkt.data());
127 }
128 
129 
130 } // namespace test
131 } // namespace ares
ares::DNSPacket
Definition: dns-proto.h:188
T_A
#define T_A
Definition: ares_nameser.h:310
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::DNSPacket::add_auth
DNSPacket & add_auth(DNSRR *q)
Definition: dns-proto.h:202
ares::DNSAaaaRR
Definition: dns-proto.h:85
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::DNSCnameRR
Definition: dns-proto.h:100
O_QUERY
#define O_QUERY
Definition: ares_nameser.h:225
ares::DNSPacket::add_additional
DNSPacket & add_additional(DNSRR *q)
Definition: dns-proto.h:206
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
C_CHAOS
#define C_CHAOS
Definition: ares_nameser.h:295
ares-test.h
ares::DNSPacket::add_answer
DNSPacket & add_answer(DNSRR *q)
Definition: dns-proto.h:198
ares::DNSPacket::opcode_
int opcode_
Definition: dns-proto.h:227
ares::DNSPtrRR
Definition: dns-proto.h:110
ares::DNSPacket::set_response
DNSPacket & set_response(bool v=true)
Definition: dns-proto.h:212
ares::test::TEST
TEST(LibraryInit, Basic)
Definition: ares-test-init.cc:13
ares::DNSARR
Definition: dns-proto.h:78
ares::DNSPacket::qid_
int qid_
Definition: dns-proto.h:225
ares::DNSPacket::aa_
bool aa_
Definition: dns-proto.h:228
ares::DNSQuestion
Definition: dns-proto.h:45
ares::DNSPacket::response_
bool response_
Definition: dns-proto.h:226
ares
Definition: ares-test-ai.h:9
dns-proto.h


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