• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* MIT License
2  *
3  * Copyright (c) The c-ares project and its contributors
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights
8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  * SPDX-License-Identifier: MIT
25  */
26 #include "ares-test.h"
27 #include "dns-proto.h"
28 
29 #include <vector>
30 
31 namespace ares {
32 namespace test {
33 
TEST(DNSProto,EncodeQuestions)34 TEST(DNSProto, EncodeQuestions) {
35   DNSPacket pkt;
36   pkt.set_qid(0x1234).set_response().set_aa()
37     .add_question(new DNSQuestion("example.com.", T_A))
38     .add_question(new DNSQuestion("www.example.com", T_AAAA, C_CHAOS));
39 
40   std::vector<byte> data = {
41     0x12, 0x34,  // qid
42     0x84, // response + query + AA + not-TC + not-RD
43     0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError
44     0x00, 0x02,  // num questions
45     0x00, 0x00,  // num answer RRs
46     0x00, 0x00,  // num authority RRs
47     0x00, 0x00,  // num additional RRs
48     // Question 1
49     0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
50     0x03, 'c', 'o', 'm',
51     0x00,
52     0x00, 0x01,  // type A
53     0x00, 0x01,  // class IN
54     // Question 2
55     0x03, 'w', 'w', 'w',
56     0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
57     0x03, 'c', 'o', 'm',
58     0x00,
59     0x00, 0x1C,  // type AAAA = 28
60     0x00, 0x03,  // class CHAOS = 3
61   };
62   EXPECT_EQ(data, pkt.data());
63 }
64 
TEST(DNSProto,EncodeSingleNameAnswers)65 TEST(DNSProto, EncodeSingleNameAnswers) {
66   DNSPacket pkt;
67   pkt.qid_ = 0x1234;
68   pkt.response_ = true;
69   pkt.aa_ = true;
70   pkt.opcode_ = O_QUERY;
71   pkt.add_answer(new DNSCnameRR("example.com", 0x01020304, "other.com."));
72   pkt.add_auth(new DNSPtrRR("www.example.com", 0x01020304, "www.other.com"));
73 
74   std::vector<byte> data = {
75     0x12, 0x34,  // qid
76     0x84, // response + query + AA + not-TC + not-RD
77     0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError
78     0x00, 0x00,  // num questions
79     0x00, 0x01,  // num answer RRs
80     0x00, 0x01,  // num authority RRs
81     0x00, 0x00,  // num additional RRs
82     // Answer 1
83     0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
84     0x03, 'c', 'o', 'm',
85     0x00,
86     0x00, 0x05,  // RR type
87     0x00, 0x01,  // class IN
88     0x01, 0x02, 0x03, 0x04, // TTL
89     0x00, 0x0B,  // rdata length
90     0x05, 'o', 't', 'h', 'e', 'r',
91     0x03, 'c', 'o', 'm',
92     0x00,
93     // Authority 1
94     0x03, 'w', 'w', 'w',
95     0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
96     0x03, 'c', 'o', 'm',
97     0x00,
98     0x00, 0x0c,  // RR type
99     0x00, 0x01,  // class IN
100     0x01, 0x02, 0x03, 0x04, // TTL
101     0x00, 0x0F,  // rdata length
102     0x03, 'w', 'w', 'w',
103     0x05, 'o', 't', 'h', 'e', 'r',
104     0x03, 'c', 'o', 'm',
105     0x00,
106   };
107   EXPECT_EQ(data, pkt.data());
108 }
109 
TEST(DNSProto,EncodeAddressAnswers)110 TEST(DNSProto, EncodeAddressAnswers) {
111   DNSPacket pkt;
112   pkt.qid_ = 0x1234;
113   pkt.response_ = true;
114   pkt.aa_ = true;
115   pkt.opcode_ = O_QUERY;
116   std::vector<byte> addrv4 = {0x02, 0x03, 0x04, 0x05};
117   pkt.add_answer(new DNSARR("example.com", 0x01020304, addrv4));
118   byte addrv6[16] = {0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
119                      0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04};
120   pkt.add_additional(new DNSAaaaRR("www.example.com", 0x01020304, addrv6, 16));
121 
122   std::vector<byte> data = {
123     0x12, 0x34,  // qid
124     0x84, // response + query + AA + not-TC + not-RD
125     0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError
126     0x00, 0x00,  // num questions
127     0x00, 0x01,  // num answer RRs
128     0x00, 0x00,  // num authority RRs
129     0x00, 0x01,  // num additional RRs
130     // Answer 1
131     0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
132     0x03, 'c', 'o', 'm',
133     0x00,
134     0x00, 0x01,  // RR type
135     0x00, 0x01,  // class IN
136     0x01, 0x02, 0x03, 0x04, // TTL
137     0x00, 0x04,  // rdata length
138     0x02, 0x03, 0x04, 0x05,
139     // Additional 1
140     0x03, 'w', 'w', 'w',
141     0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
142     0x03, 'c', 'o', 'm',
143     0x00,
144     0x00, 0x1c,  // RR type
145     0x00, 0x01,  // class IN
146     0x01, 0x02, 0x03, 0x04, // TTL
147     0x00, 0x10,  // rdata length
148     0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
149     0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04
150   };
151   EXPECT_EQ(data, pkt.data());
152 }
153 
154 
155 }  // namespace test
156 }  // namespace ares
157