ares-test-parse-srv.cc (12247B)
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 35 TEST_F(LibraryTest, ParseSrvReplyOK) { 36 DNSPacket pkt; 37 pkt.set_qid(0x1234).set_response().set_aa() 38 .add_question(new DNSQuestion("example.com", T_SRV)) 39 .add_answer(new DNSSrvRR("example.com", 100, 10, 20, 30, "srv.example.com")) 40 .add_answer(new DNSSrvRR("example.com", 100, 11, 21, 31, "srv2.example.com")); 41 std::vector<byte> data = pkt.data(); 42 43 struct ares_srv_reply* srv = nullptr; 44 EXPECT_EQ(ARES_SUCCESS, ares_parse_srv_reply(data.data(), (int)data.size(), &srv)); 45 ASSERT_NE(nullptr, srv); 46 47 EXPECT_EQ("srv.example.com", std::string(srv->host)); 48 EXPECT_EQ(10, srv->priority); 49 EXPECT_EQ(20, srv->weight); 50 EXPECT_EQ(30, srv->port); 51 52 struct ares_srv_reply* srv2 = srv->next; 53 ASSERT_NE(nullptr, srv2); 54 EXPECT_EQ("srv2.example.com", std::string(srv2->host)); 55 EXPECT_EQ(11, srv2->priority); 56 EXPECT_EQ(21, srv2->weight); 57 EXPECT_EQ(31, srv2->port); 58 EXPECT_EQ(nullptr, srv2->next); 59 60 ares_free_data(srv); 61 } 62 63 TEST_F(LibraryTest, ParseSrvReplySingle) { 64 DNSPacket pkt; 65 pkt.set_qid(0x1234).set_response().set_aa() 66 .add_question(new DNSQuestion("example.abc.def.com", T_SRV)) 67 .add_answer(new DNSSrvRR("example.abc.def.com", 180, 0, 10, 8160, "example.abc.def.com")) 68 .add_auth(new DNSNsRR("abc.def.com", 44, "else1.where.com")) 69 .add_auth(new DNSNsRR("abc.def.com", 44, "else2.where.com")) 70 .add_auth(new DNSNsRR("abc.def.com", 44, "else3.where.com")) 71 .add_auth(new DNSNsRR("abc.def.com", 44, "else4.where.com")) 72 .add_auth(new DNSNsRR("abc.def.com", 44, "else5.where.com")) 73 .add_additional(new DNSARR("else2.where.com", 42, {172,19,0,1})) 74 .add_additional(new DNSARR("else5.where.com", 42, {172,19,0,2})); 75 std::vector<byte> data = pkt.data(); 76 77 struct ares_srv_reply* srv = nullptr; 78 EXPECT_EQ(ARES_SUCCESS, ares_parse_srv_reply(data.data(), (int)data.size(), &srv)); 79 ASSERT_NE(nullptr, srv); 80 81 EXPECT_EQ("example.abc.def.com", std::string(srv->host)); 82 EXPECT_EQ(0, srv->priority); 83 EXPECT_EQ(10, srv->weight); 84 EXPECT_EQ(8160, srv->port); 85 EXPECT_EQ(nullptr, srv->next); 86 87 ares_free_data(srv); 88 } 89 90 TEST_F(LibraryTest, ParseSrvReplyMalformed) { 91 std::vector<byte> data = { 92 0x12, 0x34, // qid 93 0x84, // response + query + AA + not-TC + not-RD 94 0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError 95 0x00, 0x01, // num questions 96 0x00, 0x01, // num answer RRs 97 0x00, 0x00, // num authority RRs 98 0x00, 0x00, // num additional RRs 99 // Question 100 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 101 0x03, 'c', 'o', 'm', 102 0x00, 103 0x00, 0x21, // type SRV 104 0x00, 0x01, // class IN 105 // Answer 1 106 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 107 0x03, 'c', 'o', 'm', 108 0x00, 109 0x00, 0x21, // RR type 110 0x00, 0x01, // class IN 111 0x01, 0x02, 0x03, 0x04, // TTL 112 0x00, 0x04, // rdata length -- too short 113 0x02, 0x03, 0x04, 0x05, 114 }; 115 116 struct ares_srv_reply* srv = nullptr; 117 EXPECT_EQ(ARES_EBADRESP, ares_parse_srv_reply(data.data(), (int)data.size(), &srv)); 118 ASSERT_EQ(nullptr, srv); 119 } 120 121 TEST_F(LibraryTest, ParseSrvReplyMultiple) { 122 DNSPacket pkt; 123 pkt.set_qid(0x1234).set_response().set_ra().set_rd() 124 .add_question(new DNSQuestion("srv.example.com", T_SRV)) 125 .add_answer(new DNSSrvRR("srv.example.com", 300, 0, 5, 6789, "a1.srv.example.com")) 126 .add_answer(new DNSSrvRR("srv.example.com", 300, 0, 5, 4567, "a2.srv.example.com")) 127 .add_answer(new DNSSrvRR("srv.example.com", 300, 0, 5, 5678, "a3.srv.example.com")) 128 .add_auth(new DNSNsRR("example.com", 300, "ns1.example.com")) 129 .add_auth(new DNSNsRR("example.com", 300, "ns2.example.com")) 130 .add_auth(new DNSNsRR("example.com", 300, "ns3.example.com")) 131 .add_additional(new DNSARR("a1.srv.example.com", 300, {172,19,1,1})) 132 .add_additional(new DNSARR("a2.srv.example.com", 300, {172,19,1,2})) 133 .add_additional(new DNSARR("a3.srv.example.com", 300, {172,19,1,3})) 134 .add_additional(new DNSARR("n1.example.com", 300, {172,19,0,1})) 135 .add_additional(new DNSARR("n2.example.com", 300, {172,19,0,2})) 136 .add_additional(new DNSARR("n3.example.com", 300, {172,19,0,3})); 137 std::vector<byte> data = pkt.data(); 138 139 struct ares_srv_reply* srv0 = nullptr; 140 EXPECT_EQ(ARES_SUCCESS, ares_parse_srv_reply(data.data(), (int)data.size(), &srv0)); 141 ASSERT_NE(nullptr, srv0); 142 struct ares_srv_reply* srv = srv0; 143 144 EXPECT_EQ("a1.srv.example.com", std::string(srv->host)); 145 EXPECT_EQ(0, srv->priority); 146 EXPECT_EQ(5, srv->weight); 147 EXPECT_EQ(6789, srv->port); 148 EXPECT_NE(nullptr, srv->next); 149 srv = srv->next; 150 151 EXPECT_EQ("a2.srv.example.com", std::string(srv->host)); 152 EXPECT_EQ(0, srv->priority); 153 EXPECT_EQ(5, srv->weight); 154 EXPECT_EQ(4567, srv->port); 155 EXPECT_NE(nullptr, srv->next); 156 srv = srv->next; 157 158 EXPECT_EQ("a3.srv.example.com", std::string(srv->host)); 159 EXPECT_EQ(0, srv->priority); 160 EXPECT_EQ(5, srv->weight); 161 EXPECT_EQ(5678, srv->port); 162 EXPECT_EQ(nullptr, srv->next); 163 164 ares_free_data(srv0); 165 } 166 167 TEST_F(LibraryTest, ParseSrvReplyCname) { 168 DNSPacket pkt; 169 pkt.set_qid(0x1234).set_response().set_aa() 170 .add_question(new DNSQuestion("example.abc.def.com", T_SRV)) 171 .add_answer(new DNSCnameRR("example.abc.def.com", 300, "cname.abc.def.com")) 172 .add_answer(new DNSSrvRR("cname.abc.def.com", 300, 0, 10, 1234, "srv.abc.def.com")) 173 .add_auth(new DNSNsRR("abc.def.com", 44, "else1.where.com")) 174 .add_auth(new DNSNsRR("abc.def.com", 44, "else2.where.com")) 175 .add_auth(new DNSNsRR("abc.def.com", 44, "else3.where.com")) 176 .add_additional(new DNSARR("example.abc.def.com", 300, {172,19,0,1})) 177 .add_additional(new DNSARR("else1.where.com", 42, {172,19,0,1})) 178 .add_additional(new DNSARR("else2.where.com", 42, {172,19,0,2})) 179 .add_additional(new DNSARR("else3.where.com", 42, {172,19,0,3})); 180 std::vector<byte> data = pkt.data(); 181 182 struct ares_srv_reply* srv = nullptr; 183 EXPECT_EQ(ARES_SUCCESS, ares_parse_srv_reply(data.data(), (int)data.size(), &srv)); 184 ASSERT_NE(nullptr, srv); 185 186 EXPECT_EQ("srv.abc.def.com", std::string(srv->host)); 187 EXPECT_EQ(0, srv->priority); 188 EXPECT_EQ(10, srv->weight); 189 EXPECT_EQ(1234, srv->port); 190 EXPECT_EQ(nullptr, srv->next); 191 192 ares_free_data(srv); 193 } 194 195 TEST_F(LibraryTest, ParseSrvReplyCnameMultiple) { 196 DNSPacket pkt; 197 pkt.set_qid(0x1234).set_response().set_ra().set_rd() 198 .add_question(new DNSQuestion("query.example.com", T_SRV)) 199 .add_answer(new DNSCnameRR("query.example.com", 300, "srv.example.com")) 200 .add_answer(new DNSSrvRR("srv.example.com", 300, 0, 5, 6789, "a1.srv.example.com")) 201 .add_answer(new DNSSrvRR("srv.example.com", 300, 0, 5, 4567, "a2.srv.example.com")) 202 .add_answer(new DNSSrvRR("srv.example.com", 300, 0, 5, 5678, "a3.srv.example.com")) 203 .add_auth(new DNSNsRR("example.com", 300, "ns1.example.com")) 204 .add_auth(new DNSNsRR("example.com", 300, "ns2.example.com")) 205 .add_auth(new DNSNsRR("example.com", 300, "ns3.example.com")) 206 .add_additional(new DNSARR("a1.srv.example.com", 300, {172,19,1,1})) 207 .add_additional(new DNSARR("a2.srv.example.com", 300, {172,19,1,2})) 208 .add_additional(new DNSARR("a3.srv.example.com", 300, {172,19,1,3})) 209 .add_additional(new DNSARR("n1.example.com", 300, {172,19,0,1})) 210 .add_additional(new DNSARR("n2.example.com", 300, {172,19,0,2})) 211 .add_additional(new DNSARR("n3.example.com", 300, {172,19,0,3})); 212 std::vector<byte> data = pkt.data(); 213 214 struct ares_srv_reply* srv0 = nullptr; 215 EXPECT_EQ(ARES_SUCCESS, ares_parse_srv_reply(data.data(), (int)data.size(), &srv0)); 216 ASSERT_NE(nullptr, srv0); 217 struct ares_srv_reply* srv = srv0; 218 219 EXPECT_EQ("a1.srv.example.com", std::string(srv->host)); 220 EXPECT_EQ(0, srv->priority); 221 EXPECT_EQ(5, srv->weight); 222 EXPECT_EQ(6789, srv->port); 223 EXPECT_NE(nullptr, srv->next); 224 srv = srv->next; 225 226 EXPECT_EQ("a2.srv.example.com", std::string(srv->host)); 227 EXPECT_EQ(0, srv->priority); 228 EXPECT_EQ(5, srv->weight); 229 EXPECT_EQ(4567, srv->port); 230 EXPECT_NE(nullptr, srv->next); 231 srv = srv->next; 232 233 EXPECT_EQ("a3.srv.example.com", std::string(srv->host)); 234 EXPECT_EQ(0, srv->priority); 235 EXPECT_EQ(5, srv->weight); 236 EXPECT_EQ(5678, srv->port); 237 EXPECT_EQ(nullptr, srv->next); 238 239 ares_free_data(srv0); 240 } 241 242 TEST_F(LibraryTest, ParseSrvReplyErrors) { 243 DNSPacket pkt; 244 pkt.set_qid(0x1234).set_response().set_aa() 245 .add_question(new DNSQuestion("example.abc.def.com", T_SRV)) 246 .add_answer(new DNSSrvRR("example.abc.def.com", 180, 0, 10, 8160, "example.abc.def.com")); 247 std::vector<byte> data; 248 struct ares_srv_reply* srv = nullptr; 249 250 // No question. 251 pkt.questions_.clear(); 252 data = pkt.data(); 253 EXPECT_EQ(ARES_EBADRESP, ares_parse_srv_reply(data.data(), (int)data.size(), &srv)); 254 pkt.add_question(new DNSQuestion("example.abc.def.com", T_SRV)); 255 256 #ifdef DISABLED 257 // Question != answer 258 pkt.questions_.clear(); 259 pkt.add_question(new DNSQuestion("Axample.com", T_SRV)); 260 data = pkt.data(); 261 EXPECT_EQ(ARES_ENODATA, ares_parse_srv_reply(data.data(), (int)data.size(), &srv)); 262 pkt.questions_.clear(); 263 pkt.add_question(new DNSQuestion("example.com", T_SRV)); 264 #endif 265 266 // Two questions. 267 pkt.add_question(new DNSQuestion("example.abc.def.com", T_SRV)); 268 data = pkt.data(); 269 EXPECT_EQ(ARES_EBADRESP, ares_parse_srv_reply(data.data(), (int)data.size(), &srv)); 270 pkt.questions_.clear(); 271 pkt.add_question(new DNSQuestion("64.48.32.16.in-addr.arpa", T_PTR)); 272 273 // Wrong sort of answer. 274 pkt.answers_.clear(); 275 pkt.add_answer(new DNSMxRR("example.com", 100, 100, "mx1.example.com")); 276 data = pkt.data(); 277 EXPECT_EQ(ARES_SUCCESS, ares_parse_srv_reply(data.data(), (int)data.size(), &srv)); 278 EXPECT_EQ(nullptr, srv); 279 pkt.answers_.clear(); 280 pkt.add_answer(new DNSSrvRR("example.abc.def.com", 180, 0, 10, 8160, "example.abc.def.com")); 281 282 // No answer. 283 pkt.answers_.clear(); 284 data = pkt.data(); 285 EXPECT_EQ(ARES_ENODATA, ares_parse_srv_reply(data.data(), (int)data.size(), &srv)); 286 pkt.add_answer(new DNSSrvRR("example.abc.def.com", 180, 0, 10, 8160, "example.abc.def.com")); 287 288 // Truncated packets. 289 data = pkt.data(); 290 for (size_t len = 1; len < data.size(); len++) { 291 int rc = ares_parse_srv_reply(data.data(), (int)len, &srv); 292 EXPECT_TRUE(rc == ARES_EBADRESP || rc == ARES_EBADNAME); 293 } 294 295 // Negative Length 296 EXPECT_EQ(ARES_EBADRESP, ares_parse_srv_reply(data.data(), -1, &srv)); 297 } 298 299 TEST_F(LibraryTest, ParseSrvReplyAllocFail) { 300 DNSPacket pkt; 301 pkt.set_qid(0x1234).set_response().set_aa() 302 .add_question(new DNSQuestion("example.abc.def.com", T_SRV)) 303 .add_answer(new DNSCnameRR("example.com", 300, "c.example.com")) 304 .add_answer(new DNSSrvRR("example.abc.def.com", 180, 0, 10, 8160, "example.abc.def.com")); 305 std::vector<byte> data = pkt.data(); 306 struct ares_srv_reply* srv = nullptr; 307 308 for (int ii = 1; ii <= 5; ii++) { 309 ClearFails(); 310 SetAllocFail(ii); 311 EXPECT_EQ(ARES_ENOMEM, ares_parse_srv_reply(data.data(), (int)data.size(), &srv)) << ii; 312 } 313 } 314 315 } // namespace test 316 } // namespace ares