From 95fd317c476d3444661369185342e6fe91be20d1 Mon Sep 17 00:00:00 2001 From: Keeyou Date: Tue, 23 Jul 2024 14:38:26 +0800 Subject: [PATCH] dns: add c-ares based unittests --- src/net/dns_message_test.cpp | 80 ++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/src/net/dns_message_test.cpp b/src/net/dns_message_test.cpp index 8b0285aab..2b5b4e6c6 100644 --- a/src/net/dns_message_test.cpp +++ b/src/net/dns_message_test.cpp @@ -6,15 +6,36 @@ #include +#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 +#include +#endif + using namespace net; using namespace net::dns_message; +#ifdef HAVE_C_ARES +static span 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(poolbuf, static_cast(buflen)); +} +#endif + TEST(DnsMessageTest, Request) { request msg; ASSERT_TRUE(msg.init("www.google.com", DNS_TYPE_AAAA)); @@ -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}; @@ -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 info_addr; + for (int i = 0; i < count; ++i) { + info_addr.emplace(ntohl(info[i].ipaddr.s_addr)); + } + std::set 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 cname; + cname.insert(host->h_name); + for (int i = 0; host->h_aliases[i]; ++i) { + cname.insert(host->h_aliases[i]); + } + std::set expected_cname{ + "www.baidu.com", + "www.a.shifen.com", + }; + ASSERT_EQ(expected_cname, cname); + + std::set 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