Imported Upstream version 1.18.1
[platform/upstream/c-ares.git] / test / ares-test-parse-soa-any.cc
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, ParseSoaAnyReplyOK) {
11   DNSPacket pkt;
12   pkt.set_qid(0x1234).set_response().set_aa()
13     .add_question(new DNSQuestion("example.com", T_ANY))\
14     .add_answer(new DNSARR("example.com", 0x01020304, {2,3,4,5}))
15     .add_answer(new DNSMxRR("example.com", 100, 100, "mx1.example.com"))
16     .add_answer(new DNSMxRR("example.com", 100, 200, "mx2.example.com"))
17     .add_answer(new DNSSoaRR("example.com", 100,
18                              "soa1.example.com", "fred.example.com",
19                              1, 2, 3, 4, 5));
20   std::vector<byte> data = pkt.data();
21
22   struct ares_soa_reply* soa = nullptr;
23   EXPECT_EQ(ARES_SUCCESS, ares_parse_soa_reply(data.data(), data.size(), &soa));
24   ASSERT_NE(nullptr, soa);
25   EXPECT_EQ("soa1.example.com", std::string(soa->nsname));
26   EXPECT_EQ("fred.example.com", std::string(soa->hostmaster));
27   EXPECT_EQ(1, soa->serial);
28   EXPECT_EQ(2, soa->refresh);
29   EXPECT_EQ(3, soa->retry);
30   EXPECT_EQ(4, soa->expire);
31   EXPECT_EQ(5, soa->minttl);
32   ares_free_data(soa);
33 }
34
35 TEST_F(LibraryTest, ParseSoaAnyReplyErrors) {
36   DNSPacket pkt;
37   pkt.set_qid(0x1234).set_response().set_aa()
38     .add_question(new DNSQuestion("example.com", T_ANY))
39     .add_answer(new DNSSoaRR("example.com", 100,
40                              "soa1.example.com", "fred.example.com",
41                              1, 2, 3, 4, 5));
42   std::vector<byte> data;
43   struct ares_soa_reply* soa = nullptr;
44
45   // No question.
46   pkt.questions_.clear();
47   data = pkt.data();
48   EXPECT_EQ(ARES_EBADRESP, ares_parse_soa_reply(data.data(), data.size(), &soa));
49   pkt.add_question(new DNSQuestion("example.com", T_ANY));
50
51 #ifdef DISABLED
52   // Question != answer
53   pkt.questions_.clear();
54   pkt.add_question(new DNSQuestion("Axample.com", T_ANY));
55   data = pkt.data();
56   EXPECT_EQ(ARES_EBADRESP, ares_parse_soa_reply(data.data(), data.size(), &soa));
57   pkt.questions_.clear();
58   pkt.add_question(new DNSQuestion("example.com", T_ANY));
59 #endif
60
61   // Two questions
62   pkt.add_question(new DNSQuestion("example.com", T_ANY));
63   data = pkt.data();
64   EXPECT_EQ(ARES_EBADRESP, ares_parse_soa_reply(data.data(), data.size(), &soa));
65   pkt.questions_.clear();
66   pkt.add_question(new DNSQuestion("example.com", T_ANY));
67
68   // Wrong sort of answer.
69   pkt.answers_.clear();
70   pkt.add_answer(new DNSMxRR("example.com", 100, 100, "mx1.example.com"));
71   data = pkt.data();
72   EXPECT_EQ(ARES_EBADRESP, ares_parse_soa_reply(data.data(), data.size(), &soa));
73   pkt.answers_.clear();
74   pkt.add_answer(new DNSSoaRR("example.com", 100,
75                              "soa1.example.com", "fred.example.com",
76                              1, 2, 3, 4, 5));
77
78   // No answer.
79   pkt.answers_.clear();
80   data = pkt.data();
81   EXPECT_EQ(ARES_EBADRESP, ares_parse_soa_reply(data.data(), data.size(), &soa));
82   pkt.add_answer(new DNSSoaRR("example.com", 100,
83                              "soa1.example.com", "fred.example.com",
84                              1, 2, 3, 4, 5));
85
86   // Truncated packets.
87   data = pkt.data();
88   for (size_t len = 1; len < data.size(); len++) {
89     EXPECT_EQ(ARES_EBADRESP, ares_parse_soa_reply(data.data(), len, &soa));
90   }
91 }
92
93 TEST_F(LibraryTest, ParseSoaAnyReplyAllocFail) {
94   DNSPacket pkt;
95   pkt.set_qid(0x1234).set_response().set_aa()
96     .add_question(new DNSQuestion("example.com", T_ANY))
97     .add_answer(new DNSSoaRR("example.com", 100,
98                              "soa1.example.com", "fred.example.com",
99                              1, 2, 3, 4, 5));
100   std::vector<byte> data = pkt.data();
101   struct ares_soa_reply* soa = nullptr;
102
103   for (int ii = 1; ii <= 5; ii++) {
104     ClearFails();
105     SetAllocFail(ii);
106     EXPECT_EQ(ARES_ENOMEM, ares_parse_soa_reply(data.data(), data.size(), &soa)) << ii;
107   }
108 }
109
110 }  // namespace test
111 }  // namespace ares