Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9c748a29 authored by Hungming Chen's avatar Hungming Chen
Browse files

resolv_gold_test: Gold test with GetHostByName

Use gold test data to verify GetHostByName.

Test: cd packages/modules/DnsResolver && atest
Change-Id: I2af482e59a480938da131ae5e4ffa3b55f27fce5
parent 7b6c23b2
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -50,16 +50,16 @@ cc_test {
        "libssl",
    ],
    static_libs: [
        "dnsresolver_aidl_interface-unstable-cpp",
        "dnsresolver_aidl_interface-unstable-ndk_platform",
        "golddata_proto",
        "libbase",
        "libgmock",
        "liblog",
        "libnetd_resolv",
        "libnetd_test_dnsresponder",
        "libnetd_test_dnsresponder_ndk",
        "libnetd_test_resolv_utils",
        "libnetdutils",
        "netd_aidl_interface-cpp",
        "netd_aidl_interface-ndk_platform",
        "netd_event_listener_interface-ndk_platform",
        "server_configurable_flags",
        "stats_proto",
+7 −0
Original line number Diff line number Diff line
@@ -160,6 +160,12 @@ message GoldTest {
            int32 ai_flags = 5;
        }

        // The arguments used to send a DNS query by call type CALL_GETHOSTBYNAME.
        message HostByName {
            string host = 1;
            AddressFamily family = 2;
        }

        // The call is used to send DNS lookups.
        CallType call = 1;

@@ -167,6 +173,7 @@ message GoldTest {
        oneof Arg {
            // The arguments of call type CALL_GETADDRINFO.
            AddrInfo addrinfo = 2;
            HostByName hostbyname = 3;
        }
    };

+42 −4
Original line number Diff line number Diff line
@@ -26,12 +26,13 @@
#include <gtest/gtest.h>

#include "PrivateDnsConfiguration.h"
#include "dns_responder/dns_responder.h"
#include "dns_responder_client.h"
#include "getaddrinfo.h"
#include "gethnamaddr.h"
#include "golddata.pb.h"
#include "resolv_cache.h"
#include "resolv_test_utils.h"
#include "tests/dns_responder/dns_responder.h"
#include "tests/dns_responder/dns_responder_client_ndk.h"
#include "tests/dns_responder/dns_tls_certificate.h"
#include "tests/dns_responder/dns_tls_frontend.h"

@@ -43,6 +44,10 @@ using std::chrono::milliseconds;

enum class DnsProtocol { CLEARTEXT, TLS };

// The buffer size of resolv_gethostbyname().
// TODO: Consider moving to packages/modules/DnsResolver/tests/resolv_test_utils.h.
constexpr unsigned int MAXPACKET = 8 * 1024;

const std::string kTestDataPath = android::base::GetExecutableDirectory() + "/testdata/";
const std::vector<std::string> kGoldFilesGetAddrInfo = {
        "getaddrinfo.topsite.google.pbtxt",    "getaddrinfo.topsite.youtube.pbtxt",
@@ -51,6 +56,9 @@ const std::vector<std::string> kGoldFilesGetAddrInfo = {
        "getaddrinfo.topsite.wikipedia.pbtxt", "getaddrinfo.topsite.ebay.pbtxt",
        "getaddrinfo.topsite.netflix.pbtxt",   "getaddrinfo.topsite.bing.pbtxt"};
const std::vector<std::string> kGoldFilesGetAddrInfoTls = {"getaddrinfo.tls.topsite.google.pbtxt"};
const std::vector<std::string> kGoldFilesGetHostByName = {"gethostbyname.topsite.youtube.pbtxt"};
const std::vector<std::string> kGoldFilesGetHostByNameTls = {
        "gethostbyname.tls.topsite.youtube.pbtxt"};

// Fixture test class definition.
class TestBase : public ::testing::Test {
@@ -67,8 +75,8 @@ class TestBase : public ::testing::Test {
        resolv_delete_cache_for_net(TEST_NETID);
    }

    void SetResolverConfiguration(const std::vector<std::string>& servers = {},
                                  const std::vector<std::string>& domains = {},
    void SetResolverConfiguration(const std::vector<std::string>& servers,
                                  const std::vector<std::string>& domains,
                                  const std::vector<std::string>& tlsServers = {},
                                  const std::string& tlsHostname = "",
                                  const std::string& caCert = "") {
@@ -177,6 +185,20 @@ class TestBase : public ::testing::Test {
        VerifyAddress(goldtest, result);
    }

    void VerifyGetHostByName(const android::net::GoldTest& goldtest, const DnsProtocol protocol) {
        ASSERT_TRUE(goldtest.config().has_hostbyname());
        const auto& args = goldtest.config().hostbyname();
        hostent* hp = nullptr;
        hostent hbuf;
        char tmpbuf[MAXPACKET];
        const android_net_context netcontext = GetNetContext(protocol);
        NetworkDnsEventReported event;
        const int rv = resolv_gethostbyname(args.host().c_str(), args.family(), &hbuf, tmpbuf,
                                            sizeof(tmpbuf), &netcontext, &hp, &event);
        ASSERT_EQ(rv, goldtest.result().return_code());
        VerifyAddress(goldtest, hp);
    }

    void VerifyResolver(const android::net::GoldTest& goldtest, const test::DNSResponder& dns,
                        const test::DnsTlsFrontend& tls, const DnsProtocol protocol) {
        size_t queries;
@@ -191,6 +213,12 @@ class TestBase : public ::testing::Test {
                queries = goldtest.config().addrinfo().family() == AF_UNSPEC ? 2U : 1U;
                name = goldtest.config().addrinfo().host();
                break;
            case android::net::CallType::CALL_GETHOSTBYNAME:
                ASSERT_TRUE(goldtest.config().has_hostbyname());
                ASSERT_NO_FATAL_FAILURE(VerifyGetHostByName(goldtest, protocol));
                queries = 1U;
                name = goldtest.config().hostbyname().host();
                break;
            default:
                FAIL() << "Unsupported call type: " << calltype;
        }
@@ -360,6 +388,16 @@ INSTANTIATE_TEST_SUITE_P(GetAddrInfoTls, ResolvGoldTest,
                                            ::testing::ValuesIn(kGoldFilesGetAddrInfoTls)),
                         ResolvGoldTest::Name);

// GetHostByName tests.
INSTANTIATE_TEST_SUITE_P(GetHostByName, ResolvGoldTest,
                         ::testing::Combine(::testing::Values(DnsProtocol::CLEARTEXT),
                                            ::testing::ValuesIn(kGoldFilesGetHostByName)),
                         ResolvGoldTest::Name);
INSTANTIATE_TEST_SUITE_P(GetHostByNameTls, ResolvGoldTest,
                         ::testing::Combine(::testing::Values(DnsProtocol::TLS),
                                            ::testing::ValuesIn(kGoldFilesGetHostByNameTls)),
                         ResolvGoldTest::Name);

TEST_P(ResolvGoldTest, GoldData) {
    const auto& [protocol, file] = GetParam();

+21 −0
Original line number Diff line number Diff line
@@ -57,6 +57,27 @@ std::string ToString(const sockaddr_storage* addr) {
    return host;
}

std::vector<std::string> ToStrings(const hostent* he) {
    std::vector<std::string> hosts;
    if (he == nullptr) {
        hosts.push_back("<null>");
        return hosts;
    }
    uint32_t i = 0;
    while (he->h_addr_list[i] != nullptr) {
        char host[INET6_ADDRSTRLEN];
        if (!inet_ntop(he->h_addrtype, he->h_addr_list[i], host, sizeof(host))) {
            hosts.push_back("<invalid>");
            return hosts;
        } else {
            hosts.push_back(host);
        }
        i++;
    }
    if (hosts.empty()) hosts.push_back("<invalid>");
    return hosts;
}

std::vector<std::string> ToStrings(const addrinfo* ai) {
    std::vector<std::string> hosts;
    if (!ai) {
+1 −0
Original line number Diff line number Diff line
@@ -107,5 +107,6 @@ std::string ToString(const hostent* he);
std::string ToString(const addrinfo* ai);
std::string ToString(const android::netdutils::ScopedAddrinfo& ai);
std::string ToString(const sockaddr_storage* addr);
std::vector<std::string> ToStrings(const hostent* he);
std::vector<std::string> ToStrings(const addrinfo* ai);
std::vector<std::string> ToStrings(const android::netdutils::ScopedAddrinfo& ai);
Loading