"Fossies" - the Fresh Open Source Software Archive

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