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

Commit 7b6c23b2 authored by Hungming Chen's avatar Hungming Chen
Browse files

resolv_gold_test: Gold test with DNS-over-TLS

Use gold test data to verify DNS-over-TLS.

Test: cd packages/modules/DnsResolver && atest
Change-Id: I72b5452bd35e020c17e2ad72f88bb6e984770216
parent 2a56a621
Loading
Loading
Loading
Loading
+141 −69
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@
#include <Fwmark.h>
#include <android-base/chrono_utils.h>
#include <android-base/file.h>
#include <android-base/stringprintf.h>
#include <gmock/gmock-matchers.h>
#include <google/protobuf/text_format.h>
#include <gtest/gtest.h>
@@ -34,11 +35,14 @@
#include "tests/dns_responder/dns_tls_certificate.h"
#include "tests/dns_responder/dns_tls_frontend.h"

namespace android {
namespace net {
namespace android::net {

using android::base::StringPrintf;
using android::netdutils::ScopedAddrinfo;
using std::chrono::milliseconds;

enum class DnsProtocol { CLEARTEXT, TLS };

const std::string kTestDataPath = android::base::GetExecutableDirectory() + "/testdata/";
const std::vector<std::string> kGoldFilesGetAddrInfo = {
        "getaddrinfo.topsite.google.pbtxt",    "getaddrinfo.topsite.youtube.pbtxt",
@@ -46,6 +50,7 @@ const std::vector<std::string> kGoldFilesGetAddrInfo = {
        "getaddrinfo.topsite.facebook.pbtxt",  "getaddrinfo.topsite.reddit.pbtxt",
        "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"};

// Fixture test class definition.
class TestBase : public ::testing::Test {
@@ -110,6 +115,91 @@ class TestBase : public ::testing::Test {
        return false;
    }

    GoldTest ToProto(const std::string& file) {
        // Convert the testing configuration from .pbtxt file to proto.
        std::string file_content;
        EXPECT_TRUE(android::base::ReadFileToString(kTestDataPath + file, &file_content))
                << strerror(errno);
        android::net::GoldTest goldtest;
        EXPECT_TRUE(google::protobuf::TextFormat::ParseFromString(file_content, &goldtest));
        return goldtest;
    }

    void SetupMappings(const android::net::GoldTest& goldtest, test::DNSResponder& dns) {
        for (const auto& m : goldtest.packet_mapping()) {
            // Convert string to bytes because .proto type "bytes" is "string" type in C++.
            // See also the section "Scalar Value Types" in "Language Guide (proto3)".
            // TODO: Use C++20 std::span in addMappingBinaryPacket. It helps to take both
            // std::string and std::vector without conversions.
            dns.addMappingBinaryPacket(
                    std::vector<uint8_t>(m.query().begin(), m.query().end()),
                    std::vector<uint8_t>(m.response().begin(), m.response().end()));
        }
    }

    android_net_context GetNetContext(const DnsProtocol protocol) {
        return protocol == DnsProtocol::TLS ? kNetcontextTls : kNetcontext;
    }

    template <class AddressType>
    void VerifyAddress(const android::net::GoldTest& goldtest, const AddressType& result) {
        if (goldtest.result().return_code() != GT_EAI_NO_ERROR) {
            EXPECT_EQ(result, nullptr);
        } else {
            ASSERT_NE(result, nullptr);
            const auto& addresses = goldtest.result().addresses();
            EXPECT_THAT(ToStrings(result), ::testing::UnorderedElementsAreArray(addresses));
        }
    }

    void VerifyGetAddrInfo(const android::net::GoldTest& goldtest, const DnsProtocol protocol) {
        ASSERT_TRUE(goldtest.config().has_addrinfo());
        const auto& args = goldtest.config().addrinfo();
        const addrinfo hints = {
                // Clear the flag AI_ADDRCONFIG to avoid flaky test because AI_ADDRCONFIG looks at
                // whether connectivity is available. It makes that the resolver may send only A
                // or AAAA DNS query per connectivity even AF_UNSPEC has been assigned. See also
                // have_ipv6() and have_ipv4() in packages/modules/DnsResolver/getaddrinfo.cpp.
                // TODO: Consider keeping the configuration flag AI_ADDRCONFIG once the unit
                // test can treat the IPv4 and IPv6 connectivity.
                .ai_flags = args.ai_flags() & ~AI_ADDRCONFIG,
                .ai_family = args.family(),
                .ai_socktype = args.socktype(),
                .ai_protocol = args.protocol(),
        };
        addrinfo* res = nullptr;
        const android_net_context netcontext = GetNetContext(protocol);
        NetworkDnsEventReported event;
        const int rv =
                resolv_getaddrinfo(args.host().c_str(), nullptr, &hints, &netcontext, &res, &event);
        ScopedAddrinfo result(res);
        ASSERT_EQ(rv, goldtest.result().return_code());
        VerifyAddress(goldtest, result);
    }

    void VerifyResolver(const android::net::GoldTest& goldtest, const test::DNSResponder& dns,
                        const test::DnsTlsFrontend& tls, const DnsProtocol protocol) {
        size_t queries;
        std::string name;

        // Verify DNS query calls and results by proto. Then, determine expected query times and
        // queried name for checking server query status later.
        switch (const auto calltype = goldtest.config().call()) {
            case android::net::CallType::CALL_GETADDRINFO:
                ASSERT_TRUE(goldtest.config().has_addrinfo());
                ASSERT_NO_FATAL_FAILURE(VerifyGetAddrInfo(goldtest, protocol));
                queries = goldtest.config().addrinfo().family() == AF_UNSPEC ? 2U : 1U;
                name = goldtest.config().addrinfo().host();
                break;
            default:
                FAIL() << "Unsupported call type: " << calltype;
        }

        // Verify DNS server query status.
        EXPECT_EQ(GetNumQueries(dns, name.c_str()), queries);
        if (protocol == DnsProtocol::TLS) EXPECT_EQ(tls.queries(), static_cast<int>(queries));
    }

    static constexpr res_params kParams = {
            .sample_validity = 300,
            .success_threshold = 25,
@@ -138,19 +228,6 @@ class TestBase : public ::testing::Test {
};
class ResolvGetAddrInfo : public TestBase {};

// Parameterized test class definition.
class ResolvGoldTest : public TestBase, public ::testing::WithParamInterface<std::string> {};

// GetAddrInfo tests.
INSTANTIATE_TEST_SUITE_P(GetAddrInfo, ResolvGoldTest, ::testing::ValuesIn(kGoldFilesGetAddrInfo),
                         [](const ::testing::TestParamInfo<std::string>& info) {
                             std::string name = info.param;
                             std::replace_if(
                                     std::begin(name), std::end(name),
                                     [](char ch) { return !std::isalnum(ch); }, '_');
                             return name;
                         });

// Fixture tests.
TEST_F(ResolvGetAddrInfo, RemovePacketMapping) {
    test::DNSResponder dns(test::DNSResponder::MappingType::BINARY_PACKET);
@@ -166,7 +243,7 @@ TEST_F(ResolvGetAddrInfo, RemovePacketMapping) {
    ScopedAddrinfo result(res);
    ASSERT_NE(result, nullptr);
    ASSERT_EQ(rv, 0);
    EXPECT_EQ(ToString(result), "1.2.3.4");
    EXPECT_EQ(ToString(result), kHelloExampleComAddrV4);

    // Remove existing DNS record.
    dns.removeMappingBinaryPacket(kHelloExampleComQueryV4);
@@ -231,8 +308,8 @@ TEST_F(ResolvGetAddrInfo, ReplacePacketMapping) {

TEST_F(ResolvGetAddrInfo, BasicTlsQuery) {
    test::DNSResponder dns;
    dns.addMapping(kHelloExampleCom, ns_type::ns_t_a, "1.2.3.4");
    dns.addMapping(kHelloExampleCom, ns_type::ns_t_aaaa, "::1.2.3.4");
    dns.addMapping(kHelloExampleCom, ns_type::ns_t_a, kHelloExampleComAddrV4);
    dns.addMapping(kHelloExampleCom, ns_type::ns_t_aaaa, kHelloExampleComAddrV6);
    ASSERT_TRUE(dns.startServer());

    test::DnsTlsFrontend tls;
@@ -253,66 +330,61 @@ TEST_F(ResolvGetAddrInfo, BasicTlsQuery) {
    ASSERT_EQ(rv, 0);
    EXPECT_EQ(GetNumQueries(dns, kHelloExampleCom), 2U);
    const std::vector<std::string> result_strs = ToStrings(result);
    EXPECT_THAT(result_strs, testing::UnorderedElementsAreArray({"1.2.3.4", "::1.2.3.4"}));
    EXPECT_THAT(result_strs, testing::UnorderedElementsAreArray(
                                     {kHelloExampleComAddrV4, kHelloExampleComAddrV6}));
    EXPECT_EQ(tls.queries(), 3);
}

// Parameterized tests.
TEST_P(ResolvGoldTest, GoldData) {
    const auto& testFile = GetParam();
// Parameterized test class definition.
using GoldTestParamType = std::tuple<DnsProtocol, std::string /* filename */>;
class ResolvGoldTest : public TestBase, public ::testing::WithParamInterface<GoldTestParamType> {
  public:
    // Generate readable string for test name from test parameters.
    static std::string Name(::testing::TestParamInfo<GoldTestParamType> info) {
        const auto& [protocol, file] = info.param;
        std::string name = StringPrintf(
                "%s_%s", protocol == DnsProtocol::CLEARTEXT ? "CLEARTEXT" : "TLS", file.c_str());
        std::replace_if(
                std::begin(name), std::end(name), [](char ch) { return !std::isalnum(ch); }, '_');
        return name;
    }
};

    // Convert the testing configuration from .pbtxt file to proto.
    std::string file_content;
    ASSERT_TRUE(android::base::ReadFileToString(kTestDataPath + testFile, &file_content))
            << strerror(errno);
    android::net::GoldTest goldtest;
    ASSERT_TRUE(google::protobuf::TextFormat::ParseFromString(file_content, &goldtest));
    ASSERT_EQ(android::net::CallType::CALL_GETADDRINFO, goldtest.config().call());
    ASSERT_TRUE(goldtest.config().has_addrinfo());
// GetAddrInfo tests.
INSTANTIATE_TEST_SUITE_P(GetAddrInfo, ResolvGoldTest,
                         ::testing::Combine(::testing::Values(DnsProtocol::CLEARTEXT),
                                            ::testing::ValuesIn(kGoldFilesGetAddrInfo)),
                         ResolvGoldTest::Name);
INSTANTIATE_TEST_SUITE_P(GetAddrInfoTls, ResolvGoldTest,
                         ::testing::Combine(::testing::Values(DnsProtocol::TLS),
                                            ::testing::ValuesIn(kGoldFilesGetAddrInfoTls)),
                         ResolvGoldTest::Name);

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

    // Setup DNS server configuration.
    test::DNSResponder dns(test::DNSResponder::MappingType::BINARY_PACKET);
    ASSERT_TRUE(dns.startServer());
    ASSERT_NO_FATAL_FAILURE(SetResolvers());
    test::DnsTlsFrontend tls;

    // Register packet mapping (query, response) from proto.
    for (const auto& m : goldtest.packet_mapping()) {
        // Convert string to bytes because .proto type "bytes" is "string" type in C++.
        // See also the section "Scalar Value Types" in "Language Guide (proto3)".
        dns.addMappingBinaryPacket(std::vector<uint8_t>(m.query().begin(), m.query().end()),
                                   std::vector<uint8_t>(m.response().begin(), m.response().end()));
    if (protocol == DnsProtocol::CLEARTEXT) {
        ASSERT_NO_FATAL_FAILURE(SetResolvers());
    } else if (protocol == DnsProtocol::TLS) {
        ASSERT_TRUE(tls.startServer());
        ASSERT_NO_FATAL_FAILURE(SetResolversWithTls());
        EXPECT_TRUE(WaitForPrivateDnsValidation(tls.listen_address()));
        tls.clearQueries();
    }

    addrinfo* res = nullptr;
    const auto& args = goldtest.config().addrinfo();
    const addrinfo hints = {
            // Clear the flag AI_ADDRCONFIG to avoid flaky test because AI_ADDRCONFIG looks at
            // whether connectivity is available. It makes that the resolver may send only A
            // or AAAA DNS query per connectivity even AF_UNSPEC has been assigned. See also
            // have_ipv6() and have_ipv4() in packages/modules/DnsResolver/getaddrinfo.cpp.
            // TODO: Consider keeping the configuration flag AI_ADDRCONFIG once the unit
            // test can treat the IPv4 and IPv6 connectivity.
            .ai_flags = args.ai_flags() & ~AI_ADDRCONFIG,
            .ai_family = args.family(),
            .ai_socktype = args.socktype(),
            .ai_protocol = args.protocol(),
    };
    NetworkDnsEventReported event;
    const int rv =
            resolv_getaddrinfo(args.host().c_str(), nullptr, &hints, &kNetcontext, &res, &event);
    ScopedAddrinfo result(res);
    ASSERT_EQ(goldtest.result().return_code(), rv);
    // Read test configuration from proto text file to proto.
    const auto goldtest = ToProto(file);

    if (goldtest.result().return_code() != GT_EAI_NO_ERROR) {
        ASSERT_EQ(result, nullptr);
    } else {
        ASSERT_NE(result, nullptr);
        const auto& addresses = goldtest.result().addresses();
        EXPECT_THAT(ToStrings(result),
                    ::testing::UnorderedElementsAreArray(
                            std::vector<std::string>(addresses.begin(), addresses.end())));
    }
    EXPECT_EQ(GetNumQueries(dns, args.host().c_str()), (hints.ai_family == AF_UNSPEC) ? 2U : 1U);
    // Register packet mappings (query, response) from proto.
    SetupMappings(goldtest, dns);

    // Verify the resolver by proto.
    VerifyResolver(goldtest, dns, tls, protocol);
}

}  // end of namespace net
}  // end of namespace android
}  // namespace android::net
 No newline at end of file
+1 −0
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@ static constexpr char kIp6LocalHost[] = "ip6-localhost";
static constexpr char kIp6LocalHostAddr[] = "::1";
static constexpr char kHelloExampleCom[] = "hello.example.com.";
static constexpr char kHelloExampleComAddrV4[] = "1.2.3.4";
static constexpr char kHelloExampleComAddrV6[] = "::1.2.3.4";

static const std::vector<uint8_t> kHelloExampleComQueryV4 = {
        /* Header */
+61 −0
Original line number Diff line number Diff line
# Location: Taipei, Taiwan
# Network: Wifi, GoogleGuest
# Date: 26 NOV 2019

config {
    call: CALL_GETADDRINFO
    addrinfo {
        host: "www.google.com."
        family: GT_AF_UNSPEC
        socktype: GT_SOCK_DGRAM
        protocol: GT_IPPROTO_IP
        ai_flags: 1024
    };
}
result {
    return_code: GT_EAI_NO_ERROR
    addresses: "2404:6800:4008:801::2004"
    addresses: "172.217.27.132"
}
packet_mapping {
    query:    "\x00\x00\x01\x20\x00\x01\x00\x00\x00\x00\x00\x01\x03\x77\x77\x77"
              "\x06\x67\x6f\x6f\x67\x6c\x65\x03\x63\x6f\x6d\x00\x00\x1c\x00\x01"
              "\x00\x00\x29\x20\x00\x00\x00\x80\x00\x00\x55\x00\x0c\x00\x51\x00"
              "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
              "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
              "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
              "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
              "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
    response: "\x00\x00\x81\x80\x00\x01\x00\x01\x00\x00\x00\x01\x03\x77\x77\x77"
              "\x06\x67\x6f\x6f\x67\x6c\x65\x03\x63\x6f\x6d\x00\x00\x1c\x00\x01"
              "\xc0\x0c\x00\x1c\x00\x01\x00\x00\x01\x2b\x00\x10\x24\x04\x68\x00"
              "\x40\x08\x08\x01\x00\x00\x00\x00\x00\x00\x20\x04\x00\x00\x29\x02"
              "\x00\x00\x00\x80\x00\x01\x8d\x00\x0c\x01\x89\x00\x00\x00\x00\x00"
              "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
              "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
              "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
              "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
              "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
              "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
              "\x00\x00\x00\x00"
}
packet_mapping {
    query:    "\x00\x00\x01\x20\x00\x01\x00\x00\x00\x00\x00\x01\x03\x77\x77\x77"
              "\x06\x67\x6f\x6f\x67\x6c\x65\x03\x63\x6f\x6d\x00\x00\x01\x00\x01"
              "\x00\x00\x29\x20\x00\x00\x00\x80\x00\x00\x55\x00\x0c\x00\x51\x00"
              "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
              "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
              "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
              "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
              "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
    response: "\x00\x00\x81\x80\x00\x01\x00\x01\x00\x00\x00\x01\x03\x77\x77\x77"
              "\x06\x67\x6f\x6f\x67\x6c\x65\x03\x63\x6f\x6d\x00\x00\x01\x00\x01"
              "\xc0\x0c\x00\x01\x00\x01\x00\x00\x00\x86\x00\x04\xac\xd9\x1b\x84"
              "\x00\x00\x29\x02\x00\x00\x00\x80\x00\x01\x99\x00\x0c\x01\x95\x00"
              "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
              "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
              "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
              "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
              "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
              "\x00\x00\x00\x00"
}