blob: cd6531896927f6a2315f015b1d078bee7ae1f8ec [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001#include "ares-test.h"
2#include "dns-proto.h"
3
4#include <sstream>
5#include <vector>
6
7namespace ares {
8namespace test {
9
10TEST_F(LibraryTest, ParseNsReplyOK) {
11 DNSPacket pkt;
12 pkt.set_qid(0x1234).set_response().set_aa()
13 .add_question(new DNSQuestion("example.com", ns_t_ns))
14 .add_answer(new DNSNsRR("example.com", 100, "ns.example.com"));
15 std::vector<byte> data = pkt.data();
16
17 struct hostent *host = nullptr;
18 EXPECT_EQ(ARES_SUCCESS, ares_parse_ns_reply(data.data(), data.size(), &host));
19 ASSERT_NE(nullptr, host);
20 std::stringstream ss;
21 ss << HostEnt(host);
22 EXPECT_EQ("{'example.com' aliases=[ns.example.com] addrs=[]}", ss.str());
23 ares_free_hostent(host);
24}
25
26TEST_F(LibraryTest, ParseNsReplyMultiple) {
27 DNSPacket pkt;
28 pkt.set_qid(10501).set_response().set_rd().set_ra()
29 .add_question(new DNSQuestion("google.com", ns_t_ns))
30 .add_answer(new DNSNsRR("google.com", 59, "ns1.google.com"))
31 .add_answer(new DNSNsRR("google.com", 59, "ns2.google.com"))
32 .add_answer(new DNSNsRR("google.com", 59, "ns3.google.com"))
33 .add_answer(new DNSNsRR("google.com", 59, "ns4.google.com"))
34 .add_additional(new DNSARR("ns4.google.com", 247, {216,239,38,10}))
35 .add_additional(new DNSARR("ns2.google.com", 247, {216,239,34,10}))
36 .add_additional(new DNSARR("ns1.google.com", 247, {216,239,32,10}))
37 .add_additional(new DNSARR("ns3.google.com", 247, {216,239,36,10}));
38 std::vector<byte> data = pkt.data();
39
40 struct hostent *host = nullptr;
41 EXPECT_EQ(ARES_SUCCESS, ares_parse_ns_reply(data.data(), data.size(), &host));
42 ASSERT_NE(nullptr, host);
43 std::stringstream ss;
44 ss << HostEnt(host);
45 EXPECT_EQ("{'google.com' aliases=[ns1.google.com, ns2.google.com, ns3.google.com, ns4.google.com] addrs=[]}", ss.str());
46 ares_free_hostent(host);
47}
48
49TEST_F(LibraryTest, ParseNsReplyErrors) {
50 DNSPacket pkt;
51 pkt.set_qid(0x1234).set_response().set_aa()
52 .add_question(new DNSQuestion("example.com", ns_t_ns))
53 .add_answer(new DNSNsRR("example.com", 100, "ns.example.com"));
54 std::vector<byte> data;
55 struct hostent *host = nullptr;
56
57 // No question.
58 pkt.questions_.clear();
59 data = pkt.data();
60 EXPECT_EQ(ARES_EBADRESP, ares_parse_ns_reply(data.data(), data.size(), &host));
61 pkt.add_question(new DNSQuestion("example.com", ns_t_ns));
62
63#ifdef DISABLED
64 // Question != answer
65 pkt.questions_.clear();
66 pkt.add_question(new DNSQuestion("Axample.com", ns_t_ns));
67 data = pkt.data();
68 EXPECT_EQ(ARES_ENODATA, ares_parse_ns_reply(data.data(), data.size(), &host));
69 pkt.questions_.clear();
70 pkt.add_question(new DNSQuestion("example.com", ns_t_ns));
71#endif
72
73 // Two questions.
74 pkt.add_question(new DNSQuestion("example.com", ns_t_ns));
75 data = pkt.data();
76 EXPECT_EQ(ARES_EBADRESP, ares_parse_ns_reply(data.data(), data.size(), &host));
77 pkt.questions_.clear();
78 pkt.add_question(new DNSQuestion("example.com", ns_t_ns));
79
80 // Wrong sort of answer.
81 pkt.answers_.clear();
82 pkt.add_answer(new DNSMxRR("example.com", 100, 100, "mx1.example.com"));
83 data = pkt.data();
84 EXPECT_EQ(ARES_ENODATA, ares_parse_ns_reply(data.data(), data.size(), &host));
85 pkt.answers_.clear();
86 pkt.add_answer(new DNSNsRR("example.com", 100, "ns.example.com"));
87
88 // No answer.
89 pkt.answers_.clear();
90 data = pkt.data();
91 EXPECT_EQ(ARES_ENODATA, ares_parse_ns_reply(data.data(), data.size(), &host));
92 pkt.add_answer(new DNSNsRR("example.com", 100, "ns.example.com"));
93
94 // Truncated packets.
95 data = pkt.data();
96 for (size_t len = 1; len < data.size(); len++) {
97 EXPECT_EQ(ARES_EBADRESP, ares_parse_ns_reply(data.data(), len, &host));
98 }
99}
100
101TEST_F(LibraryTest, ParseNsReplyAllocFail) {
102 DNSPacket pkt;
103 pkt.set_qid(0x1234).set_response().set_aa()
104 .add_question(new DNSQuestion("example.com", ns_t_ns))
105 .add_answer(new DNSCnameRR("example.com", 300, "c.example.com"))
106 .add_answer(new DNSNsRR("c.example.com", 100, "ns.example.com"));
107 std::vector<byte> data = pkt.data();
108 struct hostent *host = nullptr;
109
110 for (int ii = 1; ii <= 8; ii++) {
111 ClearFails();
112 SetAllocFail(ii);
113 EXPECT_EQ(ARES_ENOMEM, ares_parse_ns_reply(data.data(), data.size(), &host)) << ii;
114 }
115}
116
117
118} // namespace test
119} // namespace ares