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