quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

ares-test-parse-ns.cc (5530B)


      1 /* MIT License
      2  *
      3  * Copyright (c) The c-ares project and its contributors
      4  *
      5  * Permission is hereby granted, free of charge, to any person obtaining a copy
      6  * of this software and associated documentation files (the "Software"), to deal
      7  * in the Software without restriction, including without limitation the rights
      8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      9  * copies of the Software, and to permit persons to whom the Software is
     10  * furnished to do so, subject to the following conditions:
     11  *
     12  * The above copyright notice and this permission notice (including the next
     13  * paragraph) shall be included in all copies or substantial portions of the
     14  * Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     22  * SOFTWARE.
     23  *
     24  * SPDX-License-Identifier: MIT
     25  */
     26 #include "ares-test.h"
     27 #include "dns-proto.h"
     28 
     29 #include <sstream>
     30 #include <vector>
     31 
     32 namespace ares {
     33 namespace test {
     34 
     35 TEST_F(LibraryTest, ParseNsReplyOK) {
     36   DNSPacket pkt;
     37   pkt.set_qid(0x1234).set_response().set_aa()
     38     .add_question(new DNSQuestion("example.com", T_NS))
     39     .add_answer(new DNSNsRR("example.com", 100, "ns.example.com"));
     40   std::vector<byte> data = pkt.data();
     41 
     42   struct hostent *host = nullptr;
     43   EXPECT_EQ(ARES_SUCCESS, ares_parse_ns_reply(data.data(), (int)data.size(), &host));
     44   ASSERT_NE(nullptr, host);
     45   std::stringstream ss;
     46   ss << HostEnt(host);
     47   EXPECT_EQ("{'example.com' aliases=[ns.example.com] addrs=[]}", ss.str());
     48   ares_free_hostent(host);
     49 }
     50 
     51 TEST_F(LibraryTest, ParseNsReplyMultiple) {
     52   DNSPacket pkt;
     53   pkt.set_qid(10501).set_response().set_rd().set_ra()
     54     .add_question(new DNSQuestion("google.com", T_NS))
     55     .add_answer(new DNSNsRR("google.com", 59, "ns1.google.com"))
     56     .add_answer(new DNSNsRR("google.com", 59, "ns2.google.com"))
     57     .add_answer(new DNSNsRR("google.com", 59, "ns3.google.com"))
     58     .add_answer(new DNSNsRR("google.com", 59, "ns4.google.com"))
     59     .add_additional(new DNSARR("ns4.google.com", 247, {216,239,38,10}))
     60     .add_additional(new DNSARR("ns2.google.com", 247, {216,239,34,10}))
     61     .add_additional(new DNSARR("ns1.google.com", 247, {216,239,32,10}))
     62     .add_additional(new DNSARR("ns3.google.com", 247, {216,239,36,10}));
     63   std::vector<byte> data = pkt.data();
     64 
     65   struct hostent *host = nullptr;
     66   EXPECT_EQ(ARES_SUCCESS, ares_parse_ns_reply(data.data(), (int)data.size(), &host));
     67   ASSERT_NE(nullptr, host);
     68   std::stringstream ss;
     69   ss << HostEnt(host);
     70   EXPECT_EQ("{'google.com' aliases=[ns1.google.com, ns2.google.com, ns3.google.com, ns4.google.com] addrs=[]}", ss.str());
     71   ares_free_hostent(host);
     72 }
     73 
     74 TEST_F(LibraryTest, ParseNsReplyErrors) {
     75   DNSPacket pkt;
     76   pkt.set_qid(0x1234).set_response().set_aa()
     77     .add_question(new DNSQuestion("example.com", T_NS))
     78     .add_answer(new DNSNsRR("example.com", 100, "ns.example.com"));
     79   std::vector<byte> data;
     80   struct hostent *host = nullptr;
     81 
     82   // No question.
     83   pkt.questions_.clear();
     84   data = pkt.data();
     85   EXPECT_EQ(ARES_EBADRESP, ares_parse_ns_reply(data.data(), (int)data.size(), &host));
     86   pkt.add_question(new DNSQuestion("example.com", T_NS));
     87 
     88 #ifdef DISABLED
     89   // Question != answer
     90   pkt.questions_.clear();
     91   pkt.add_question(new DNSQuestion("Axample.com", T_NS));
     92   data = pkt.data();
     93   EXPECT_EQ(ARES_ENODATA, ares_parse_ns_reply(data.data(), (int)data.size(), &host));
     94   pkt.questions_.clear();
     95   pkt.add_question(new DNSQuestion("example.com", T_NS));
     96 #endif
     97 
     98   // Two questions.
     99   pkt.add_question(new DNSQuestion("example.com", T_NS));
    100   data = pkt.data();
    101   EXPECT_EQ(ARES_EBADRESP, ares_parse_ns_reply(data.data(), (int)data.size(), &host));
    102   pkt.questions_.clear();
    103   pkt.add_question(new DNSQuestion("example.com", T_NS));
    104 
    105   // Wrong sort of answer.
    106   pkt.answers_.clear();
    107   pkt.add_answer(new DNSMxRR("example.com", 100, 100, "mx1.example.com"));
    108   data = pkt.data();
    109   EXPECT_EQ(ARES_ENODATA, ares_parse_ns_reply(data.data(), (int)data.size(), &host));
    110   pkt.answers_.clear();
    111   pkt.add_answer(new DNSNsRR("example.com", 100, "ns.example.com"));
    112 
    113   // No answer.
    114   pkt.answers_.clear();
    115   data = pkt.data();
    116   EXPECT_EQ(ARES_ENODATA, ares_parse_ns_reply(data.data(), (int)data.size(), &host));
    117   pkt.add_answer(new DNSNsRR("example.com", 100, "ns.example.com"));
    118 
    119   // Truncated packets.
    120   data = pkt.data();
    121   for (size_t len = 1; len < data.size(); len++) {
    122     EXPECT_EQ(ARES_EBADRESP, ares_parse_ns_reply(data.data(), (int)len, &host));
    123   }
    124 
    125   // Negative Length
    126   EXPECT_EQ(ARES_EBADRESP, ares_parse_ns_reply(data.data(), -1, &host));
    127 }
    128 
    129 TEST_F(LibraryTest, ParseNsReplyAllocFail) {
    130   DNSPacket pkt;
    131   pkt.set_qid(0x1234).set_response().set_aa()
    132     .add_question(new DNSQuestion("example.com", T_NS))
    133     .add_answer(new DNSCnameRR("example.com", 300, "c.example.com"))
    134     .add_answer(new DNSNsRR("c.example.com", 100, "ns.example.com"));
    135   std::vector<byte> data = pkt.data();
    136   struct hostent *host = nullptr;
    137 
    138   for (int ii = 1; ii <= 8; ii++) {
    139     ClearFails();
    140     SetAllocFail(ii);
    141     EXPECT_EQ(ARES_ENOMEM, ares_parse_ns_reply(data.data(), (int)data.size(), &host)) << ii;
    142   }
    143 }
    144 
    145 
    146 }  // namespace test
    147 }  // namespace ares