Skip to content

Commit

Permalink
dns: add c-ares based unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
Chilledheart committed Jul 23, 2024
1 parent 7c53eb9 commit 95fd317
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions src/net/dns_message_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,36 @@

#include <gmock/gmock.h>

#include "core/span.hpp"
#include "net/dns_message_request.hpp"
#include "net/dns_message_response_parser.hpp"
#include "net/iobuf.hpp"

#include "test_util.hpp"

#ifdef HAVE_C_ARES
#include <ares.h>
#include <set>
#endif

using namespace net;
using namespace net::dns_message;

#ifdef HAVE_C_ARES
static span<const uint8_t> CreateRequery(const char* name, int dnsclass, int type) {
uint8_t* buf = nullptr;
int buflen = 0;
EXPECT_EQ(ARES_SUCCESS, ares_create_query(name, dnsclass, type, 0, 0x1, &buf, &buflen, 0));
if (buflen == 0) {
return {};
}
static uint8_t poolbuf[1500];
memcpy(poolbuf, buf, buflen);
ares_free_string(buf);
return span<const uint8_t>(poolbuf, static_cast<size_t>(buflen));
}
#endif

TEST(DnsMessageTest, Request) {
request msg;
ASSERT_TRUE(msg.init("www.google.com", DNS_TYPE_AAAA));
Expand All @@ -23,6 +44,25 @@ TEST(DnsMessageTest, Request) {
ASSERT_FALSE(msg.init("www.google-long-long-long-long-long-long-long-long-long-long-long-long.com", DNS_TYPE_AAAA));
}

#ifdef HAVE_C_ARES
TEST(DnsMessageTest, RequestCAresMatch) {
request msg;
ASSERT_TRUE(msg.init("www.google.com", DNS_TYPE_AAAA));

auto source = IOBuf::create(SOCKET_BUF_SIZE);

for (auto buffer : msg.buffers()) {
source->reserve(0, buffer.size());
memcpy(source->mutable_tail(), buffer.data(), buffer.size());
source->append(buffer.size());
}

auto target = CreateRequery("www.google.com", DNS_CLASS_IN, DNS_TYPE_AAAA);

ASSERT_EQ(::testing::Bytes(*source), ::testing::Bytes(target));
}
#endif

static constexpr const uint8_t kExpectedRequestBytes[] = {
0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x77, 0x77, 0x77,
0x05, 0x62, 0x61, 0x69, 0x64, 0x75, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01};
Expand Down Expand Up @@ -70,3 +110,43 @@ TEST(DnsMessageTest, AAndCnameResponseBytes) {
};
ASSERT_EQ(expected_cname, response.cname());
}

#ifdef HAVE_C_ARES
TEST(DnsMessageTest, AAndCnameResponseBytesCAresMatch) {
struct hostent* host = nullptr;
struct ares_addrttl info[2] = {};
int count = std::size(info);
ASSERT_EQ(ARES_SUCCESS, ares_parse_a_reply(kResponseBytes, std::size(kResponseBytes), &host, info, &count));
ASSERT_EQ(2, count);
ASSERT_EQ(25, info[0].ttl);
ASSERT_EQ(25, info[1].ttl);

std::set<asio::ip::address_v4> info_addr;
for (int i = 0; i < count; ++i) {
info_addr.emplace(ntohl(info[i].ipaddr.s_addr));
}
std::set<asio::ip::address_v4> expected_addr{asio::ip::make_address("103.235.47.103").to_v4(),
asio::ip::make_address("103.235.46.40").to_v4()};
ASSERT_EQ(expected_addr, info_addr);

ASSERT_NE(nullptr, host);
ASSERT_EQ(AF_INET, host->h_addrtype);
std::set<std::string> cname;
cname.insert(host->h_name);
for (int i = 0; host->h_aliases[i]; ++i) {
cname.insert(host->h_aliases[i]);
}
std::set<std::string> expected_cname{
"www.baidu.com",
"www.a.shifen.com",
};
ASSERT_EQ(expected_cname, cname);

std::set<asio::ip::address_v4> host_addr;
for (int i = 0; host->h_addr_list[i]; ++i) {
host_addr.emplace(ntohl(((struct in_addr*)(host->h_addr_list[i]))->s_addr));
}
ASSERT_EQ(expected_addr, host_addr);
ares_free_hostent(host);
}
#endif

0 comments on commit 95fd317

Please sign in to comment.