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

Commit a61c6e57 authored by Hungming Chen's avatar Hungming Chen Committed by android-build-merger
Browse files

Merge "resolv_unit_test: Use resolv_test_utils"

am: eb8b2faeed

Change-Id: I270fa1f3f4a9a30a2ceaad8839ce2c76ab092c7b
parents 459e912e 0c7c5fb7
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -199,6 +199,7 @@ cc_test {
        "dns_tls_test.cpp",
        "libnetd_resolv_test.cpp",
        "res_cache_test.cpp",
        "tests/resolv_test_utils.cpp",
    ],
    shared_libs: [
        "libbase",
@@ -207,6 +208,7 @@ cc_test {
        "libssl",
    ],
    static_libs: [
        "dnsresolver_aidl_interface-V2-cpp",
        "libgmock",
        "libnetd_resolv",
        "libnetd_test_dnsresponder",
+1 −45
Original line number Diff line number Diff line
@@ -28,16 +28,10 @@
#include "gethnamaddr.h"
#include "resolv_cache.h"
#include "stats.pb.h"
#include "tests/resolv_test_utils.h"

#define NAME(variable) #variable

// TODO: make this dynamic and stop depending on implementation details.
constexpr unsigned int TEST_NETID = 30;

// Specifying 0 in ai_socktype or ai_protocol of struct addrinfo indicates that any type or
// protocol can be returned by getaddrinfo().
constexpr unsigned int ANY = 0;

namespace android {
namespace net {

@@ -58,38 +52,6 @@ class TestBase : public ::testing::Test {
        resolv_delete_cache_for_net(TEST_NETID);
    }

    static std::string ToString(const hostent* he) {
        if (he == nullptr) return "<null>";
        char buffer[INET6_ADDRSTRLEN];
        if (!inet_ntop(he->h_addrtype, he->h_addr_list[0], buffer, sizeof(buffer))) {
            return "<invalid>";
        }
        return buffer;
    }

    static std::string ToString(const addrinfo* ai) {
        if (!ai) return "<null>";
        for (const auto* aip = ai; aip != nullptr; aip = aip->ai_next) {
            char host[NI_MAXHOST];
            int rv = getnameinfo(aip->ai_addr, aip->ai_addrlen, host, sizeof(host), nullptr, 0,
                                 NI_NUMERICHOST);
            if (rv != 0) return gai_strerror(rv);
            return host;
        }
        return "<invalid>";
    }

    size_t GetNumQueries(const test::DNSResponder& dns, const char* name) const {
        auto queries = dns.queries();
        size_t found = 0;
        for (const auto& p : queries) {
            if (p.first == name) {
                ++found;
            }
        }
        return found;
    }

    int setResolvers() {
        const std::vector<std::string> servers = {test::kDefaultListenAddr};
        const std::vector<std::string> domains = {"example.com"};
@@ -111,12 +73,6 @@ class TestBase : public ::testing::Test {
            .dns_mark = MARK_UNSET,
            .uid = NET_CONTEXT_INVALID_UID,
    };

    // Illegal hostnames
    static constexpr char kBadCharAfterPeriodHost[] = "hello.example.^com.";
    static constexpr char kBadCharBeforePeriodHost[] = "hello.example^.com.";
    static constexpr char kBadCharAtTheEndHost[] = "hello.example.com^.";
    static constexpr char kBadCharInTheMiddleOfLabelHost[] = "hello.ex^ample.com.";
};

class ResolvGetAddrInfoTest : public TestBase {};
+1 −7
Original line number Diff line number Diff line
@@ -168,12 +168,6 @@ class ResolverTest : public ::testing::Test {

    DnsResponderClient mDnsClient;

    static constexpr char kLocalHost[] = "localhost";
    static constexpr char kLocalHostAddr[] = "127.0.0.1";
    static constexpr char kIp6LocalHost[] = "ip6-localhost";
    static constexpr char kIp6LocalHostAddr[] = "::1";
    static constexpr char kHelloExampleCom[] = "hello.example.com.";

    // Use a shared static DNS listener for all tests to avoid registering lots of listeners
    // which may be released late until process terminated. Currently, registered DNS listener
    // is removed by binder death notification which is fired when the process hosting an
@@ -555,7 +549,7 @@ TEST_F(ResolverTest, GetAddrInfo_InvalidSocketType) {
    // TODO: Test other invalid socket types.
    const addrinfo hints = {
            .ai_family = AF_UNSPEC,
            .ai_protocol = 0,  // any protocol
            .ai_protocol = ANY,
            .ai_socktype = SOCK_PACKET,
    };
    addrinfo* result = nullptr;
+16 −0
Original line number Diff line number Diff line
@@ -34,6 +34,22 @@
// TODO: make this dynamic and stop depending on implementation details.
constexpr int TEST_NETID = 30;

// Specifying 0 in ai_socktype or ai_protocol of struct addrinfo indicates that any type or
// protocol can be returned by getaddrinfo().
constexpr unsigned int ANY = 0;

static constexpr char kLocalHost[] = "localhost";
static constexpr char kLocalHostAddr[] = "127.0.0.1";
static constexpr char kIp6LocalHost[] = "ip6-localhost";
static constexpr char kIp6LocalHostAddr[] = "::1";
static constexpr char kHelloExampleCom[] = "hello.example.com.";

// Illegal hostnames
static constexpr char kBadCharAfterPeriodHost[] = "hello.example.^com.";
static constexpr char kBadCharBeforePeriodHost[] = "hello.example^.com.";
static constexpr char kBadCharAtTheEndHost[] = "hello.example.com^.";
static constexpr char kBadCharInTheMiddleOfLabelHost[] = "hello.ex^ample.com.";

size_t GetNumQueries(const test::DNSResponder& dns, const char* name);
size_t GetNumQueriesForType(const test::DNSResponder& dns, ns_type type, const char* name);
std::string ToString(const hostent* he);