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 <sstream>
30 #include <vector>
31
32 namespace ares {
33 namespace test {
34
TEST_F(LibraryTest,ParseMxReplyOK)35 TEST_F(LibraryTest, ParseMxReplyOK) {
36 DNSPacket pkt;
37 pkt.set_qid(0x1234).set_response().set_aa()
38 .add_question(new DNSQuestion("example.com", T_MX))
39 .add_answer(new DNSMxRR("example.com", 100, 100, "mx1.example.com"))
40 .add_answer(new DNSMxRR("example.com", 100, 200, "mx2.example.com"));
41 std::vector<byte> data = pkt.data();
42
43 struct ares_mx_reply* mx = nullptr;
44 EXPECT_EQ(ARES_SUCCESS, ares_parse_mx_reply(data.data(), (int)data.size(), &mx));
45 ASSERT_NE(nullptr, mx);
46 EXPECT_EQ("mx1.example.com", std::string(mx->host));
47 EXPECT_EQ(100, mx->priority);
48
49 struct ares_mx_reply* mx2 = mx->next;
50 ASSERT_NE(nullptr, mx2);
51 EXPECT_EQ("mx2.example.com", std::string(mx2->host));
52 EXPECT_EQ(200, mx2->priority);
53 EXPECT_EQ(nullptr, mx2->next);
54
55 ares_free_data(mx);
56 }
57
TEST_F(LibraryTest,ParseMxReplyMalformed)58 TEST_F(LibraryTest, ParseMxReplyMalformed) {
59 std::vector<byte> data = {
60 0x12, 0x34, // qid
61 0x84, // response + query + AA + not-TC + not-RD
62 0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError
63 0x00, 0x01, // num questions
64 0x00, 0x01, // num answer RRs
65 0x00, 0x00, // num authority RRs
66 0x00, 0x00, // num additional RRs
67 // Question
68 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
69 0x03, 'c', 'o', 'm',
70 0x00,
71 0x00, 0x0F, // type MX
72 0x00, 0x01, // class IN
73 // Answer 1
74 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
75 0x03, 'c', 'o', 'm',
76 0x00,
77 0x00, 0x0F, // RR type
78 0x00, 0x01, // class IN
79 0x01, 0x02, 0x03, 0x04, // TTL
80 0x00, 0x01, // rdata length -- too short
81 0x02,
82 };
83
84 struct ares_mx_reply* mx = nullptr;
85 EXPECT_EQ(ARES_EBADRESP, ares_parse_mx_reply(data.data(), (int)data.size(), &mx));
86 ASSERT_EQ(nullptr, mx);
87 }
88
89
TEST_F(LibraryTest,ParseMxReplyErrors)90 TEST_F(LibraryTest, ParseMxReplyErrors) {
91 DNSPacket pkt;
92 pkt.set_qid(0x1234).set_response().set_aa()
93 .add_question(new DNSQuestion("example.com", T_MX))
94 .add_answer(new DNSMxRR("example.com", 100, 100, "mx1.example.com"));
95 std::vector<byte> data;
96 struct ares_mx_reply* mx = nullptr;
97
98 // No question.
99 pkt.questions_.clear();
100 data = pkt.data();
101 EXPECT_EQ(ARES_EBADRESP, ares_parse_mx_reply(data.data(), (int)data.size(), &mx));
102 EXPECT_EQ(nullptr, mx);
103 pkt.add_question(new DNSQuestion("example.com", T_MX));
104
105 #ifdef DISABLED
106 // Question != answer
107 pkt.questions_.clear();
108 pkt.add_question(new DNSQuestion("Axample.com", T_MX));
109 data = pkt.data();
110 EXPECT_EQ(ARES_EBADRESP, ares_parse_mx_reply(data.data(), (int)data.size(), &mx));
111 pkt.questions_.clear();
112 pkt.add_question(new DNSQuestion("example.com", T_MX));
113 #endif
114
115 // Two questions.
116 pkt.add_question(new DNSQuestion("example.com", T_MX));
117 data = pkt.data();
118 EXPECT_EQ(ARES_EBADRESP, ares_parse_mx_reply(data.data(), (int)data.size(), &mx));
119 EXPECT_EQ(nullptr, mx);
120 pkt.questions_.clear();
121 pkt.add_question(new DNSQuestion("example.com", T_MX));
122
123 // Wrong sort of answer.
124 // TODO(drysdale): check if this should be ARES_ENODATA?
125 pkt.answers_.clear();
126 pkt.add_answer(new DNSSrvRR("example.abc.def.com", 180, 0, 10, 8160, "example.abc.def.com"));
127 data = pkt.data();
128 EXPECT_EQ(ARES_SUCCESS, ares_parse_mx_reply(data.data(), (int)data.size(), &mx));
129 EXPECT_EQ(nullptr, mx);
130 pkt.answers_.clear();
131 pkt.add_answer(new DNSMxRR("example.com", 100, 100, "mx1.example.com"));
132
133 // No answer.
134 pkt.answers_.clear();
135 data = pkt.data();
136 EXPECT_EQ(ARES_ENODATA, ares_parse_mx_reply(data.data(), (int)data.size(), &mx));
137 EXPECT_EQ(nullptr, mx);
138 pkt.add_answer(new DNSMxRR("example.com", 100, 100, "mx1.example.com"));
139
140 // Truncated packets.
141 data = pkt.data();
142 for (size_t len = 1; len < data.size(); len++) {
143 int rc = ares_parse_mx_reply(data.data(), (int)len, &mx);
144 EXPECT_EQ(nullptr, mx);
145 EXPECT_TRUE(rc == ARES_EBADRESP || rc == ARES_EBADNAME);
146 }
147
148 // Negative Length
149 EXPECT_EQ(ARES_EBADRESP, ares_parse_mx_reply(data.data(), -1, &mx));
150 }
151
TEST_F(LibraryTest,ParseMxReplyAllocFail)152 TEST_F(LibraryTest, ParseMxReplyAllocFail) {
153 DNSPacket pkt;
154 pkt.set_qid(0x1234).set_response().set_aa()
155 .add_question(new DNSQuestion("example.com", T_MX))
156 .add_answer(new DNSCnameRR("example.com", 300, "c.example.com"))
157 .add_answer(new DNSMxRR("c.example.com", 100, 100, "mx1.example.com"));
158 std::vector<byte> data = pkt.data();
159 struct ares_mx_reply* mx = nullptr;
160
161 for (int ii = 1; ii <= 5; ii++) {
162 ClearFails();
163 SetAllocFail(ii);
164 EXPECT_EQ(ARES_ENOMEM, ares_parse_mx_reply(data.data(), (int)data.size(), &mx)) << ii;
165 }
166 }
167
168 } // namespace test
169 } // namespace ares
170