Alien-cares

 view release on metacpan or  search on metacpan

libcares/test/ares-test-parse-aaaa.cc  view on Meta::CPAN

#include "ares-test.h"
#include "dns-proto.h"

#include <sstream>
#include <vector>

namespace ares {
namespace test {

TEST_F(LibraryTest, ParseAaaaReplyOK) {
  DNSPacket pkt;
  pkt.set_qid(0x1234).set_response().set_aa()
    .add_question(new DNSQuestion("example.com", ns_t_aaaa))
    .add_answer(new DNSAaaaRR("example.com", 100,
                              {0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
                               0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04}));
  std::vector<byte> data = pkt.data();
  struct hostent *host = nullptr;
  struct ares_addr6ttl info[5];
  int count = 5;
  EXPECT_EQ(ARES_SUCCESS, ares_parse_aaaa_reply(data.data(), data.size(),
                                                &host, info, &count));
  EXPECT_EQ(1, count);
  EXPECT_EQ(100, info[0].ttl);
  EXPECT_EQ(0x01, info[0].ip6addr._S6_un._S6_u8[0]);
  EXPECT_EQ(0x02, info[0].ip6addr._S6_un._S6_u8[4]);
  ASSERT_NE(nullptr, host);
  std::stringstream ss;
  ss << HostEnt(host);
  EXPECT_EQ("{'example.com' aliases=[] addrs=[0101:0101:0202:0202:0303:0303:0404:0404]}", ss.str());
  ares_free_hostent(host);

  // Repeat without providing places to put the results
  count = 0;
  EXPECT_EQ(ARES_SUCCESS, ares_parse_aaaa_reply(data.data(), data.size(),
                                                nullptr, info, &count));
}

TEST_F(LibraryTest, ParseAaaaReplyCname) {
  DNSPacket pkt;
  pkt.set_qid(0x1234).set_response().set_aa()
    .add_question(new DNSQuestion("example.com", ns_t_aaaa))
    .add_answer(new DNSCnameRR("example.com", 50, "c.example.com"))
    .add_answer(new DNSAaaaRR("c.example.com", 100,
                              {0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
                               0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04}));
  std::vector<byte> data = pkt.data();
  struct hostent *host = nullptr;
  struct ares_addr6ttl info[5];
  int count = 5;
  EXPECT_EQ(ARES_SUCCESS, ares_parse_aaaa_reply(data.data(), data.size(),
                                                &host, info, &count));
  EXPECT_EQ(1, count);
  // CNAME TTL overrides AAAA TTL.
  EXPECT_EQ(50, info[0].ttl);
  EXPECT_EQ(0x01, info[0].ip6addr._S6_un._S6_u8[0]);
  EXPECT_EQ(0x02, info[0].ip6addr._S6_un._S6_u8[4]);
  ASSERT_NE(nullptr, host);
  std::stringstream ss;
  ss << HostEnt(host);
  EXPECT_EQ("{'c.example.com' aliases=[example.com] addrs=[0101:0101:0202:0202:0303:0303:0404:0404]}", ss.str());
  ares_free_hostent(host);

  // Repeat without providing a hostent
  count = 5;
  EXPECT_EQ(ARES_SUCCESS, ares_parse_aaaa_reply(data.data(), data.size(),
                                                nullptr, info, &count));
  EXPECT_EQ(1, count);
  EXPECT_EQ(50, info[0].ttl);
  EXPECT_EQ(0x01, info[0].ip6addr._S6_un._S6_u8[0]);
  EXPECT_EQ(0x02, info[0].ip6addr._S6_un._S6_u8[4]);
}

TEST_F(LibraryTest, ParseAaaaReplyNoData) {
  DNSPacket pkt;
  pkt.set_qid(0x1234).set_response().set_aa()
    .add_question(new DNSQuestion("example.com", ns_t_aaaa));
  std::vector<byte> data = pkt.data();
  struct hostent *host = nullptr;
  struct ares_addr6ttl info[2];
  int count = 2;
  EXPECT_EQ(ARES_ENODATA, ares_parse_aaaa_reply(data.data(), data.size(),
                                                &host, info, &count));
  EXPECT_EQ(0, count);
  EXPECT_EQ(nullptr, host);

  // Again but with a CNAME.
  pkt.add_answer(new DNSCnameRR("example.com", 200, "c.example.com"));
  EXPECT_EQ(ARES_ENODATA, ares_parse_aaaa_reply(data.data(), data.size(),
                                                &host, info, &count));
  EXPECT_EQ(0, count);
  EXPECT_EQ(nullptr, host);
}

TEST_F(LibraryTest, ParseAaaaReplyErrors) {
  DNSPacket pkt;
  pkt.set_qid(0x1234).set_response().set_aa()
    .add_question(new DNSQuestion("example.com", ns_t_aaaa))
    .add_answer(new DNSAaaaRR("example.com", 100,
                              {0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
                               0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04}));
  std::vector<byte> data;

  struct hostent *host = nullptr;
  struct ares_addr6ttl info[2];
  int count = 2;

  // No question.
  pkt.questions_.clear();
  data = pkt.data();
  EXPECT_EQ(ARES_EBADRESP, ares_parse_aaaa_reply(data.data(), data.size(),
                                                 &host, info, &count));
  EXPECT_EQ(nullptr, host);
  pkt.add_question(new DNSQuestion("example.com", ns_t_aaaa));

  // Question != answer
  pkt.questions_.clear();
  pkt.add_question(new DNSQuestion("Axample.com", ns_t_aaaa));
  data = pkt.data();
  EXPECT_EQ(ARES_ENODATA, ares_parse_aaaa_reply(data.data(), data.size(),
                                                &host, info, &count));



( run in 1.208 second using v1.01-cache-2.11-cpan-39bf76dae61 )