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

Commit b8b69001 authored by Mike Yu's avatar Mike Yu Committed by Gerrit Code Review
Browse files

Merge "Test: Use nettestutils to dump binder service" into main

parents a5331b80 29dc0977
Loading
Loading
Loading
Loading
+6 −4
Original line number Diff line number Diff line
@@ -192,22 +192,24 @@ cc_test {
    ],
    static_libs: [
        "dnsresolver_aidl_interface-lateststable-ndk",
        "libc++fs",
        "libconnectivity_native_test_utils",
        "libcrypto_static",
        "libcutils",
        "libdoh_frontend_ffi",
        "libgmock",
        "libip_checksum",
        "libmodules-utils-build",
        "libnetd_test_dnsresponder_ndk",
        "libnetd_test_metrics_listener",
        "libnetd_test_resolv_utils",
        "libnetdutils",
        "libnettestutils",
        "libssl",
        "libc++fs",
        "libcutils",
        "libutils",
        "netd_aidl_interface-lateststable-ndk",
        "netd_event_listener_interface-lateststable-ndk",
        "libip_checksum",
        "resolv_unsolicited_listener",
        "libdoh_frontend_ffi",
    ],
    // This test talks to the DnsResolver module over a binary protocol on a socket, so keep it as
    // multilib setting is worth because we might be able to get some coverage for the case where
+5 −32
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@
#include <gtest/gtest.h>
#include <netdutils/NetNativeTestBase.h>
#include <netdutils/Stopwatch.h>
#include <nettestutils/DumpService.h>

#include <util.h>
#include "dns_metrics_listener/base_metrics_listener.h"
@@ -64,37 +65,6 @@ using android::netdutils::Stopwatch;
// Sync from TEST_NETID in dns_responder_client.cpp as resolv_integration_test.cpp does.
constexpr int TEST_NETID = 30;

namespace {

std::vector<std::string> dumpService(ndk::SpAIBinder binder) {
    unique_fd localFd, remoteFd;
    bool success = Pipe(&localFd, &remoteFd);
    EXPECT_TRUE(success) << "Failed to open pipe for dumping: " << strerror(errno);
    if (!success) return {};

    // dump() blocks until another thread has consumed all its output.
    std::thread dumpThread = std::thread([binder, remoteFd{std::move(remoteFd)}]() {
        EXPECT_EQ(STATUS_OK, AIBinder_dump(binder.get(), remoteFd, nullptr, 0));
    });

    std::string dumpContent;

    EXPECT_TRUE(ReadFdToString(localFd.get(), &dumpContent))
            << "Error during dump: " << strerror(errno);
    dumpThread.join();

    std::stringstream dumpStream(std::move(dumpContent));
    std::vector<std::string> lines;
    std::string line;
    while (std::getline(dumpStream, line)) {
        lines.push_back(std::move(line));
    }

    return lines;
}

}  // namespace

class DnsResolverBinderTest : public NetNativeTestBase {
  public:
    DnsResolverBinderTest() {
@@ -119,7 +89,10 @@ class DnsResolverBinderTest : public NetNativeTestBase {
        // This could happen when the test isn't running as root, or if netd isn't running.
        assert(nullptr != netdBinder.get());
        // Send the service dump request to netd.
        std::vector<std::string> lines = dumpService(netdBinder);
        std::vector<std::string> lines;
        const android::status_t ret =
                dumpService(netdBinder, /*args=*/nullptr, /*num_args=*/0, lines);
        ASSERT_EQ(android::OK, ret) << "Error dumping service: " << android::statusToString(ret);

        // Basic regexp to match dump output lines. Matches the beginning and end of the line, and
        // puts the output of the command itself into the first match group.
+8 −30
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@
#include <netdutils/InternetAddresses.h>
#include <netdutils/NetNativeTestBase.h>
#include <netdutils/Stopwatch.h>
#include <nettestutils/DumpService.h>

#include "doh_frontend.h"
#include "tests/dns_responder/dns_responder.h"
@@ -61,33 +62,6 @@ constexpr int kDohIdleDefaultTimeoutMs = 55000;

namespace {

std::vector<std::string> dumpService(ndk::SpAIBinder binder) {
    unique_fd localFd, remoteFd;
    bool success = Pipe(&localFd, &remoteFd);
    EXPECT_TRUE(success) << "Failed to open pipe for dumping: " << strerror(errno);
    if (!success) return {};

    // dump() blocks until another thread has consumed all its output.
    std::thread dumpThread = std::thread([binder, remoteFd{std::move(remoteFd)}]() {
        EXPECT_EQ(STATUS_OK, AIBinder_dump(binder.get(), remoteFd, nullptr, 0));
    });

    std::string dumpContent;

    EXPECT_TRUE(ReadFdToString(localFd.get(), &dumpContent))
            << "Error during dump: " << strerror(errno);
    dumpThread.join();

    std::stringstream dumpStream(std::move(dumpContent));
    std::vector<std::string> lines;
    std::string line;
    while (std::getline(dumpStream, line)) {
        lines.push_back(std::move(line));
    }

    return lines;
}

int getAsyncResponse(int fd, int* rcode, uint8_t* buf, int bufLen) {
    struct pollfd wait_fd[1];
    wait_fd[0].fd = fd;
@@ -241,9 +215,13 @@ class BaseTest : public NetNativeTestBase {
    }

    bool expectLog(const std::string& ipAddrOrNoData, const std::string& port) {
        ndk::SpAIBinder resolvBinder = ndk::SpAIBinder(AServiceManager_getService("dnsresolver"));
        assert(nullptr != resolvBinder.get());
        std::vector<std::string> lines = dumpService(resolvBinder);
        std::vector<std::string> lines;
        const android::status_t ret =
                dumpService(sResolvBinder, /*args=*/nullptr, /*num_args=*/0, lines);
        if (ret != android::OK) {
            ADD_FAILURE() << "Error dumping service: " << android::statusToString(ret);
            return false;
        }

        const std::string expectedLog =
                port.empty() ? ipAddrOrNoData