quickjs-tart

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

ares-test-parse.cc (6902B)


      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, ParseRootName) {
     36   DNSPacket pkt;
     37   pkt.set_qid(0x1234).set_response().set_aa()
     38     .add_question(new DNSQuestion(".", T_A))
     39     .add_answer(new DNSARR(".", 100, {0x02, 0x03, 0x04, 0x05}));
     40   std::vector<byte> data = pkt.data();
     41 
     42   struct hostent *host = nullptr;
     43   struct ares_addrttl info[2];
     44   int count = 2;
     45   EXPECT_EQ(ARES_SUCCESS, ares_parse_a_reply(data.data(), (int)data.size(),
     46                                              &host, info, &count));
     47   EXPECT_EQ(1, count);
     48   std::stringstream ss;
     49   ss << HostEnt(host);
     50   EXPECT_EQ("{'' aliases=[] addrs=[2.3.4.5]}", ss.str());
     51   ares_free_hostent(host);
     52 }
     53 
     54 TEST_F(LibraryTest, ParseIndirectRootName) {
     55   std::vector<byte> data = {
     56     0x12, 0x34,  // qid
     57     0x84, // response + query + AA + not-TC + not-RD
     58     0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError
     59     0x00, 0x01,  // num questions
     60     0x00, 0x01,  // num answer RRs
     61     0x00, 0x00,  // num authority RRs
     62     0x00, 0x00,  // num additional RRs
     63     // Question
     64     0xC0, 0x04,  // weird: pointer to a random zero earlier in the message
     65     0x00, 0x01,  // type A
     66     0x00, 0x01,  // class IN
     67     // Answer 1
     68     0xC0, 0x04,
     69     0x00, 0x01,  // RR type
     70     0x00, 0x01,  // class IN
     71     0x01, 0x02, 0x03, 0x04, // TTL
     72     0x00, 0x04,  // rdata length
     73     0x02, 0x03, 0x04, 0x05,
     74   };
     75 
     76   struct hostent *host = nullptr;
     77   struct ares_addrttl info[2];
     78   int count = 2;
     79   EXPECT_EQ(ARES_SUCCESS, ares_parse_a_reply(data.data(), (int)data.size(),
     80                                              &host, info, &count));
     81   EXPECT_EQ(1, count);
     82   std::stringstream ss;
     83   ss << HostEnt(host);
     84   EXPECT_EQ("{'' aliases=[] addrs=[2.3.4.5]}", ss.str());
     85   ares_free_hostent(host);
     86 }
     87 
     88 
     89 #if 0 /* We are validating hostnames now, its not clear how this would ever be valid */
     90 TEST_F(LibraryTest, ParseEscapedName) {
     91   std::vector<byte> data = {
     92     0x12, 0x34,  // qid
     93     0x84, // response + query + AA + not-TC + not-RD
     94     0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError
     95     0x00, 0x01,  // num questions
     96     0x00, 0x01,  // num answer RRs
     97     0x00, 0x00,  // num authority RRs
     98     0x00, 0x00,  // num additional RRs
     99     // Question
    100     0x05, 'a', '\\', 'b', '.', 'c',
    101     0x03, 'c', 'o', 'm',
    102     0x00,
    103     0x00, 0x01,  // type A
    104     0x00, 0x01,  // class IN
    105     // Answer 1
    106     0x05, 'a', '\\', 'b', '.', 'c',
    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   };
    115   struct hostent *host = nullptr;
    116   struct ares_addrttl info[2];
    117   int count = 2;
    118   EXPECT_EQ(ARES_SUCCESS, ares_parse_a_reply(data.data(), data.size(),
    119                                              &host, info, &count));
    120   EXPECT_EQ(1, count);
    121   HostEnt hent(host);
    122   std::stringstream ss;
    123   ss << hent;
    124   // The printable name is expanded with escapes.
    125   EXPECT_EQ(11, hent.name_.size());
    126   EXPECT_EQ('a', hent.name_[0]);
    127   EXPECT_EQ('\\', hent.name_[1]);
    128   EXPECT_EQ('\\', hent.name_[2]);
    129   EXPECT_EQ('b', hent.name_[3]);
    130   EXPECT_EQ('\\', hent.name_[4]);
    131   EXPECT_EQ('.', hent.name_[5]);
    132   EXPECT_EQ('c', hent.name_[6]);
    133   ares_free_hostent(host);
    134 }
    135 #endif
    136 
    137 TEST_F(LibraryTest, ParsePartialCompressedName) {
    138   std::vector<byte> data = {
    139     0x12, 0x34,  // qid
    140     0x84, // response + query + AA + not-TC + not-RD
    141     0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError
    142     0x00, 0x01,  // num questions
    143     0x00, 0x01,  // num answer RRs
    144     0x00, 0x00,  // num authority RRs
    145     0x00, 0x00,  // num additional RRs
    146     // Question
    147     0x03, 'w', 'w', 'w',
    148     0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
    149     0x03, 'c', 'o', 'm',
    150     0x00,
    151     0x00, 0x01,  // type A
    152     0x00, 0x01,  // class IN
    153     // Answer 1
    154     0x03, 'w', 'w', 'w',
    155     0xc0, 0x10,  // offset 16
    156     0x00, 0x01,  // RR type
    157     0x00, 0x01,  // class IN
    158     0x01, 0x02, 0x03, 0x04, // TTL
    159     0x00, 0x04,  // rdata length
    160     0x02, 0x03, 0x04, 0x05,
    161   };
    162   struct hostent *host = nullptr;
    163   struct ares_addrttl info[2];
    164   int count = 2;
    165   EXPECT_EQ(ARES_SUCCESS, ares_parse_a_reply(data.data(), (int)data.size(),
    166                                              &host, info, &count));
    167   ASSERT_NE(nullptr, host);
    168   std::stringstream ss;
    169   ss << HostEnt(host);
    170   EXPECT_EQ("{'www.example.com' aliases=[] addrs=[2.3.4.5]}", ss.str());
    171   ares_free_hostent(host);
    172 }
    173 
    174 TEST_F(LibraryTest, ParseFullyCompressedName) {
    175   std::vector<byte> data = {
    176     0x12, 0x34,  // qid
    177     0x84, // response + query + AA + not-TC + not-RD
    178     0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError
    179     0x00, 0x01,  // num questions
    180     0x00, 0x01,  // num answer RRs
    181     0x00, 0x00,  // num authority RRs
    182     0x00, 0x00,  // num additional RRs
    183     // Question
    184     0x03, 'w', 'w', 'w',
    185     0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
    186     0x03, 'c', 'o', 'm',
    187     0x00,
    188     0x00, 0x01,  // type A
    189     0x00, 0x01,  // class IN
    190     // Answer 1
    191     0xc0, 0x0c,  // offset 12
    192     0x00, 0x01,  // RR type
    193     0x00, 0x01,  // class IN
    194     0x01, 0x02, 0x03, 0x04, // TTL
    195     0x00, 0x04,  // rdata length
    196     0x02, 0x03, 0x04, 0x05,
    197   };
    198   struct hostent *host = nullptr;
    199   struct ares_addrttl info[2];
    200   int count = 2;
    201   EXPECT_EQ(ARES_SUCCESS, ares_parse_a_reply(data.data(), (int)data.size(),
    202                                              &host, info, &count));
    203   ASSERT_NE(nullptr, host);
    204   std::stringstream ss;
    205   ss << HostEnt(host);
    206   EXPECT_EQ("{'www.example.com' aliases=[] addrs=[2.3.4.5]}", ss.str());
    207   ares_free_hostent(host);
    208 }
    209 
    210 
    211 }  // namespace test
    212 }  // namespace ares