aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Yu <yumike@google.com>2023-11-13 01:58:44 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2023-11-13 01:58:44 +0000
commitb8b6900127895ce891545212019391f1cf2fd6cc (patch)
treef55be6196651093b08c4e6abc4661fb6142ae79e
parenta5331b802416bfa426efeaed4ac01395ca529521 (diff)
parent29dc0977fcae02403724c924fc3347afca7737f9 (diff)
downloadDnsResolver-b8b6900127895ce891545212019391f1cf2fd6cc.tar.gz
Merge "Test: Use nettestutils to dump binder service" into main
-rw-r--r--tests/Android.bp10
-rw-r--r--tests/dnsresolver_binder_test.cpp37
-rw-r--r--tests/resolv_private_dns_test.cpp38
3 files changed, 19 insertions, 66 deletions
diff --git a/tests/Android.bp b/tests/Android.bp
index aa60b1b0..908cc09b 100644
--- a/tests/Android.bp
+++ b/tests/Android.bp
@@ -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
diff --git a/tests/dnsresolver_binder_test.cpp b/tests/dnsresolver_binder_test.cpp
index 2d9801e0..85c10720 100644
--- a/tests/dnsresolver_binder_test.cpp
+++ b/tests/dnsresolver_binder_test.cpp
@@ -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.
diff --git a/tests/resolv_private_dns_test.cpp b/tests/resolv_private_dns_test.cpp
index 1af5570b..cef39b40 100644
--- a/tests/resolv_private_dns_test.cpp
+++ b/tests/resolv_private_dns_test.cpp
@@ -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