"Fossies" - the Fresh Open Source Software Archive

Member "c-ares-1.17.2/test/ares-test-parse-ns.cc" (8 Aug 2021, 4167 Bytes) of package /linux/misc/dns/c-ares-1.17.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "ares-test-parse-ns.cc": 1.17.1_vs_1.17.2.

    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, ParseNsReplyOK) {
   11   DNSPacket pkt;
   12   pkt.set_qid(0x1234).set_response().set_aa()
   13     .add_question(new DNSQuestion("example.com", 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 
   26 TEST_F(LibraryTest, ParseNsReplyMultiple) {
   27   DNSPacket pkt;
   28   pkt.set_qid(10501).set_response().set_rd().set_ra()
   29     .add_question(new DNSQuestion("google.com", 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 
   49 TEST_F(LibraryTest, ParseNsReplyErrors) {
   50   DNSPacket pkt;
   51   pkt.set_qid(0x1234).set_response().set_aa()
   52     .add_question(new DNSQuestion("example.com", 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", T_NS));
   62 
   63 #ifdef DISABLED
   64   // Question != answer
   65   pkt.questions_.clear();
   66   pkt.add_question(new DNSQuestion("Axample.com", 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", T_NS));
   71 #endif
   72 
   73   // Two questions.
   74   pkt.add_question(new DNSQuestion("example.com", 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", 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 
  101 TEST_F(LibraryTest, ParseNsReplyAllocFail) {
  102   DNSPacket pkt;
  103   pkt.set_qid(0x1234).set_response().set_aa()
  104     .add_question(new DNSQuestion("example.com", 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