lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | #include "ares-test.h" |
| 2 | #include "dns-proto.h" |
| 3 | |
| 4 | #include <vector> |
| 5 | |
| 6 | namespace ares { |
| 7 | namespace test { |
| 8 | |
| 9 | TEST(DNSProto, EncodeQuestions) { |
| 10 | DNSPacket pkt; |
| 11 | pkt.set_qid(0x1234).set_response().set_aa() |
| 12 | .add_question(new DNSQuestion("example.com.", ns_t_a)) |
| 13 | .add_question(new DNSQuestion("www.example.com", ns_t_aaaa, ns_c_chaos)); |
| 14 | |
| 15 | std::vector<byte> data = { |
| 16 | 0x12, 0x34, // qid |
| 17 | 0x84, // response + query + AA + not-TC + not-RD |
| 18 | 0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError |
| 19 | 0x00, 0x02, // num questions |
| 20 | 0x00, 0x00, // num answer RRs |
| 21 | 0x00, 0x00, // num authority RRs |
| 22 | 0x00, 0x00, // num additional RRs |
| 23 | // Question 1 |
| 24 | 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e', |
| 25 | 0x03, 'c', 'o', 'm', |
| 26 | 0x00, |
| 27 | 0x00, 0x01, // type A |
| 28 | 0x00, 0x01, // class IN |
| 29 | // Question 2 |
| 30 | 0x03, 'w', 'w', 'w', |
| 31 | 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e', |
| 32 | 0x03, 'c', 'o', 'm', |
| 33 | 0x00, |
| 34 | 0x00, 0x1C, // type AAAA = 28 |
| 35 | 0x00, 0x03, // class CHAOS = 3 |
| 36 | }; |
| 37 | EXPECT_EQ(data, pkt.data()); |
| 38 | } |
| 39 | |
| 40 | TEST(DNSProto, EncodeSingleNameAnswers) { |
| 41 | DNSPacket pkt; |
| 42 | pkt.qid_ = 0x1234; |
| 43 | pkt.response_ = true; |
| 44 | pkt.aa_ = true; |
| 45 | pkt.opcode_ = ns_o_query; |
| 46 | pkt.add_answer(new DNSCnameRR("example.com", 0x01020304, "other.com.")); |
| 47 | pkt.add_auth(new DNSPtrRR("www.example.com", 0x01020304, "www.other.com")); |
| 48 | |
| 49 | std::vector<byte> data = { |
| 50 | 0x12, 0x34, // qid |
| 51 | 0x84, // response + query + AA + not-TC + not-RD |
| 52 | 0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError |
| 53 | 0x00, 0x00, // num questions |
| 54 | 0x00, 0x01, // num answer RRs |
| 55 | 0x00, 0x01, // num authority RRs |
| 56 | 0x00, 0x00, // num additional RRs |
| 57 | // Answer 1 |
| 58 | 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e', |
| 59 | 0x03, 'c', 'o', 'm', |
| 60 | 0x00, |
| 61 | 0x00, 0x05, // RR type |
| 62 | 0x00, 0x01, // class IN |
| 63 | 0x01, 0x02, 0x03, 0x04, // TTL |
| 64 | 0x00, 0x0B, // rdata length |
| 65 | 0x05, 'o', 't', 'h', 'e', 'r', |
| 66 | 0x03, 'c', 'o', 'm', |
| 67 | 0x00, |
| 68 | // Authority 1 |
| 69 | 0x03, 'w', 'w', 'w', |
| 70 | 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e', |
| 71 | 0x03, 'c', 'o', 'm', |
| 72 | 0x00, |
| 73 | 0x00, 0x0c, // RR type |
| 74 | 0x00, 0x01, // class IN |
| 75 | 0x01, 0x02, 0x03, 0x04, // TTL |
| 76 | 0x00, 0x0F, // rdata length |
| 77 | 0x03, 'w', 'w', 'w', |
| 78 | 0x05, 'o', 't', 'h', 'e', 'r', |
| 79 | 0x03, 'c', 'o', 'm', |
| 80 | 0x00, |
| 81 | }; |
| 82 | EXPECT_EQ(data, pkt.data()); |
| 83 | } |
| 84 | |
| 85 | TEST(DNSProto, EncodeAddressAnswers) { |
| 86 | DNSPacket pkt; |
| 87 | pkt.qid_ = 0x1234; |
| 88 | pkt.response_ = true; |
| 89 | pkt.aa_ = true; |
| 90 | pkt.opcode_ = ns_o_query; |
| 91 | std::vector<byte> addrv4 = {0x02, 0x03, 0x04, 0x05}; |
| 92 | pkt.add_answer(new DNSARR("example.com", 0x01020304, addrv4)); |
| 93 | byte addrv6[16] = {0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, |
| 94 | 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04}; |
| 95 | pkt.add_additional(new DNSAaaaRR("www.example.com", 0x01020304, addrv6, 16)); |
| 96 | |
| 97 | std::vector<byte> data = { |
| 98 | 0x12, 0x34, // qid |
| 99 | 0x84, // response + query + AA + not-TC + not-RD |
| 100 | 0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError |
| 101 | 0x00, 0x00, // num questions |
| 102 | 0x00, 0x01, // num answer RRs |
| 103 | 0x00, 0x00, // num authority RRs |
| 104 | 0x00, 0x01, // num additional RRs |
| 105 | // Answer 1 |
| 106 | 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e', |
| 107 | 0x03, 'c', 'o', 'm', |
| 108 | 0x00, |
| 109 | 0x00, 0x01, // RR type |
| 110 | 0x00, 0x01, // class IN |
| 111 | 0x01, 0x02, 0x03, 0x04, // TTL |
| 112 | 0x00, 0x04, // rdata length |
| 113 | 0x02, 0x03, 0x04, 0x05, |
| 114 | // Additional 1 |
| 115 | 0x03, 'w', 'w', 'w', |
| 116 | 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e', |
| 117 | 0x03, 'c', 'o', 'm', |
| 118 | 0x00, |
| 119 | 0x00, 0x1c, // RR type |
| 120 | 0x00, 0x01, // class IN |
| 121 | 0x01, 0x02, 0x03, 0x04, // TTL |
| 122 | 0x00, 0x10, // rdata length |
| 123 | 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, |
| 124 | 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04 |
| 125 | }; |
| 126 | EXPECT_EQ(data, pkt.data()); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | } // namespace test |
| 131 | } // namespace ares |