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, ParseAaaaReplyOK) { |
| 11 | DNSPacket pkt; |
| 12 | pkt.set_qid(0x1234).set_response().set_aa() |
| 13 | .add_question(new DNSQuestion("example.com", ns_t_aaaa)) |
| 14 | .add_answer(new DNSAaaaRR("example.com", 100, |
| 15 | {0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, |
| 16 | 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04})); |
| 17 | std::vector<byte> data = pkt.data(); |
| 18 | struct hostent *host = nullptr; |
| 19 | struct ares_addr6ttl info[5]; |
| 20 | int count = 5; |
| 21 | EXPECT_EQ(ARES_SUCCESS, ares_parse_aaaa_reply(data.data(), data.size(), |
| 22 | &host, info, &count)); |
| 23 | EXPECT_EQ(1, count); |
| 24 | EXPECT_EQ(100, info[0].ttl); |
| 25 | EXPECT_EQ(0x01, info[0].ip6addr._S6_un._S6_u8[0]); |
| 26 | EXPECT_EQ(0x02, info[0].ip6addr._S6_un._S6_u8[4]); |
| 27 | ASSERT_NE(nullptr, host); |
| 28 | std::stringstream ss; |
| 29 | ss << HostEnt(host); |
| 30 | EXPECT_EQ("{'example.com' aliases=[] addrs=[0101:0101:0202:0202:0303:0303:0404:0404]}", ss.str()); |
| 31 | ares_free_hostent(host); |
| 32 | |
| 33 | // Repeat without providing places to put the results |
| 34 | count = 0; |
| 35 | EXPECT_EQ(ARES_SUCCESS, ares_parse_aaaa_reply(data.data(), data.size(), |
| 36 | nullptr, info, &count)); |
| 37 | } |
| 38 | |
| 39 | TEST_F(LibraryTest, ParseAaaaReplyCname) { |
| 40 | DNSPacket pkt; |
| 41 | pkt.set_qid(0x1234).set_response().set_aa() |
| 42 | .add_question(new DNSQuestion("example.com", ns_t_aaaa)) |
| 43 | .add_answer(new DNSCnameRR("example.com", 50, "c.example.com")) |
| 44 | .add_answer(new DNSAaaaRR("c.example.com", 100, |
| 45 | {0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, |
| 46 | 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04})); |
| 47 | std::vector<byte> data = pkt.data(); |
| 48 | struct hostent *host = nullptr; |
| 49 | struct ares_addr6ttl info[5]; |
| 50 | int count = 5; |
| 51 | EXPECT_EQ(ARES_SUCCESS, ares_parse_aaaa_reply(data.data(), data.size(), |
| 52 | &host, info, &count)); |
| 53 | EXPECT_EQ(1, count); |
| 54 | // CNAME TTL overrides AAAA TTL. |
| 55 | EXPECT_EQ(50, info[0].ttl); |
| 56 | EXPECT_EQ(0x01, info[0].ip6addr._S6_un._S6_u8[0]); |
| 57 | EXPECT_EQ(0x02, info[0].ip6addr._S6_un._S6_u8[4]); |
| 58 | ASSERT_NE(nullptr, host); |
| 59 | std::stringstream ss; |
| 60 | ss << HostEnt(host); |
| 61 | EXPECT_EQ("{'c.example.com' aliases=[example.com] addrs=[0101:0101:0202:0202:0303:0303:0404:0404]}", ss.str()); |
| 62 | ares_free_hostent(host); |
| 63 | |
| 64 | // Repeat without providing a hostent |
| 65 | count = 5; |
| 66 | EXPECT_EQ(ARES_SUCCESS, ares_parse_aaaa_reply(data.data(), data.size(), |
| 67 | nullptr, info, &count)); |
| 68 | EXPECT_EQ(1, count); |
| 69 | EXPECT_EQ(50, info[0].ttl); |
| 70 | EXPECT_EQ(0x01, info[0].ip6addr._S6_un._S6_u8[0]); |
| 71 | EXPECT_EQ(0x02, info[0].ip6addr._S6_un._S6_u8[4]); |
| 72 | } |
| 73 | |
| 74 | TEST_F(LibraryTest, ParseAaaaReplyNoData) { |
| 75 | DNSPacket pkt; |
| 76 | pkt.set_qid(0x1234).set_response().set_aa() |
| 77 | .add_question(new DNSQuestion("example.com", ns_t_aaaa)); |
| 78 | std::vector<byte> data = pkt.data(); |
| 79 | struct hostent *host = nullptr; |
| 80 | struct ares_addr6ttl info[2]; |
| 81 | int count = 2; |
| 82 | EXPECT_EQ(ARES_ENODATA, ares_parse_aaaa_reply(data.data(), data.size(), |
| 83 | &host, info, &count)); |
| 84 | EXPECT_EQ(0, count); |
| 85 | EXPECT_EQ(nullptr, host); |
| 86 | |
| 87 | // Again but with a CNAME. |
| 88 | pkt.add_answer(new DNSCnameRR("example.com", 200, "c.example.com")); |
| 89 | EXPECT_EQ(ARES_ENODATA, ares_parse_aaaa_reply(data.data(), data.size(), |
| 90 | &host, info, &count)); |
| 91 | EXPECT_EQ(0, count); |
| 92 | EXPECT_EQ(nullptr, host); |
| 93 | } |
| 94 | |
| 95 | TEST_F(LibraryTest, ParseAaaaReplyErrors) { |
| 96 | DNSPacket pkt; |
| 97 | pkt.set_qid(0x1234).set_response().set_aa() |
| 98 | .add_question(new DNSQuestion("example.com", ns_t_aaaa)) |
| 99 | .add_answer(new DNSAaaaRR("example.com", 100, |
| 100 | {0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, |
| 101 | 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04})); |
| 102 | std::vector<byte> data; |
| 103 | |
| 104 | struct hostent *host = nullptr; |
| 105 | struct ares_addr6ttl info[2]; |
| 106 | int count = 2; |
| 107 | |
| 108 | // No question. |
| 109 | pkt.questions_.clear(); |
| 110 | data = pkt.data(); |
| 111 | EXPECT_EQ(ARES_EBADRESP, ares_parse_aaaa_reply(data.data(), data.size(), |
| 112 | &host, info, &count)); |
| 113 | EXPECT_EQ(nullptr, host); |
| 114 | pkt.add_question(new DNSQuestion("example.com", ns_t_aaaa)); |
| 115 | |
| 116 | // Question != answer |
| 117 | pkt.questions_.clear(); |
| 118 | pkt.add_question(new DNSQuestion("Axample.com", ns_t_aaaa)); |
| 119 | data = pkt.data(); |
| 120 | EXPECT_EQ(ARES_ENODATA, ares_parse_aaaa_reply(data.data(), data.size(), |
| 121 | &host, info, &count)); |
| 122 | EXPECT_EQ(nullptr, host); |
| 123 | pkt.questions_.clear(); |
| 124 | pkt.add_question(new DNSQuestion("example.com", ns_t_aaaa)); |
| 125 | |
| 126 | // Two questions. |
| 127 | pkt.add_question(new DNSQuestion("example.com", ns_t_aaaa)); |
| 128 | data = pkt.data(); |
| 129 | EXPECT_EQ(ARES_EBADRESP, ares_parse_aaaa_reply(data.data(), data.size(), |
| 130 | &host, info, &count)); |
| 131 | EXPECT_EQ(nullptr, host); |
| 132 | pkt.questions_.clear(); |
| 133 | pkt.add_question(new DNSQuestion("example.com", ns_t_aaaa)); |
| 134 | |
| 135 | // Wrong sort of answer. |
| 136 | pkt.answers_.clear(); |
| 137 | pkt.add_answer(new DNSMxRR("example.com", 100, 100, "mx1.example.com")); |
| 138 | data = pkt.data(); |
| 139 | EXPECT_EQ(ARES_ENODATA, ares_parse_aaaa_reply(data.data(), data.size(), |
| 140 | &host, info, &count)); |
| 141 | EXPECT_EQ(nullptr, host); |
| 142 | pkt.answers_.clear(); |
| 143 | pkt.add_answer(new DNSAaaaRR("example.com", 100, |
| 144 | {0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, |
| 145 | 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04})); |
| 146 | |
| 147 | // No answer. |
| 148 | pkt.answers_.clear(); |
| 149 | data = pkt.data(); |
| 150 | EXPECT_EQ(ARES_ENODATA, ares_parse_aaaa_reply(data.data(), data.size(), |
| 151 | &host, info, &count)); |
| 152 | EXPECT_EQ(nullptr, host); |
| 153 | pkt.add_answer(new DNSAaaaRR("example.com", 100, |
| 154 | {0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, |
| 155 | 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04})); |
| 156 | |
| 157 | // Truncated packets. |
| 158 | data = pkt.data(); |
| 159 | for (size_t len = 1; len < data.size(); len++) { |
| 160 | EXPECT_EQ(ARES_EBADRESP, ares_parse_aaaa_reply(data.data(), len, |
| 161 | &host, info, &count)); |
| 162 | EXPECT_EQ(nullptr, host); |
| 163 | EXPECT_EQ(ARES_EBADRESP, ares_parse_aaaa_reply(data.data(), len, |
| 164 | nullptr, info, &count)); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | TEST_F(LibraryTest, ParseAaaaReplyAllocFail) { |
| 169 | DNSPacket pkt; |
| 170 | pkt.set_qid(0x1234).set_response().set_aa() |
| 171 | .add_question(new DNSQuestion("example.com", ns_t_aaaa)) |
| 172 | .add_answer(new DNSCnameRR("example.com", 300, "c.example.com")) |
| 173 | .add_answer(new DNSAaaaRR("c.example.com", 100, |
| 174 | {0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, |
| 175 | 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04})); |
| 176 | std::vector<byte> data = pkt.data(); |
| 177 | struct hostent *host = nullptr; |
| 178 | struct ares_addr6ttl info[2]; |
| 179 | int count = 2; |
| 180 | |
| 181 | for (int ii = 1; ii <= 8; ii++) { |
| 182 | ClearFails(); |
| 183 | SetAllocFail(ii); |
| 184 | EXPECT_EQ(ARES_ENOMEM, ares_parse_aaaa_reply(data.data(), data.size(), |
| 185 | &host, info, &count)) << ii; |
| 186 | EXPECT_EQ(nullptr, host); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | } // namespace test |
| 191 | } // namespace ares |