From 51e6c252c495234fc4acc256d65fcb04888357a2 Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Tue, 27 Feb 2018 16:00:00 -0800 Subject: [PATCH] Make memunreachable_binder_test suitable for VTS Link statically against libmemunreachable to avoid runtime dependency on libmemunreachable.so. Provide a better error when run as non-root. Rename the test to MemunreachableBinderTest. Add comments explaining the test sequence. Bug: 28275695 Test: memunreachable_binder_test Change-Id: Iddfba636205b7ca3ad31a6ba2c4e85abadd32d0d --- libmemunreachable/Android.bp | 6 +++--- libmemunreachable/tests/Binder_test.cpp | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/libmemunreachable/Android.bp b/libmemunreachable/Android.bp index b428dd785..caca3774e 100644 --- a/libmemunreachable/Android.bp +++ b/libmemunreachable/Android.bp @@ -20,7 +20,7 @@ cc_defaults { }, } -cc_library_shared { +cc_library { name: "libmemunreachable", defaults: ["libmemunreachable_defaults"], srcs: [ @@ -88,14 +88,14 @@ cc_test { cc_test { name: "memunreachable_binder_test", defaults: ["libmemunreachable_defaults"], + test_suites: ["vts"], srcs: [ "tests/Binder_test.cpp", - "tests/MemUnreachable_test.cpp", ], + static_libs: ["libmemunreachable"], shared_libs: [ "libbinder", "libhwbinder", - "libmemunreachable", "libutils", ], } diff --git a/libmemunreachable/tests/Binder_test.cpp b/libmemunreachable/tests/Binder_test.cpp index 6e85d5ab3..eaf7652d1 100644 --- a/libmemunreachable/tests/Binder_test.cpp +++ b/libmemunreachable/tests/Binder_test.cpp @@ -33,6 +33,9 @@ namespace android { static const String16 service_name("test.libmemunreachable_binder"); +// Provides a service that will hold a strong reference to any remote binder +// object, so that the test can verify that a remote strong reference is +// visible to libmemunreachable. class BinderService : public BBinder { public: BinderService() = default; @@ -55,6 +58,8 @@ class BinderObject : public BBinder { ~BinderObject() = default; }; +// Forks a subprocess that registers a BinderService with the global binder +// servicemanager. Requires root permissions. class ServiceProcess { public: ServiceProcess() : child_(0) {} @@ -97,6 +102,7 @@ class ServiceProcess { fprintf(stderr, "Failed to get service manager\n"); return 1; } + // This step requires root permissions if (sm->addService(service_name, new BinderService()) != OK) { fprintf(stderr, "Failed to add test service\n"); return 1; @@ -110,12 +116,18 @@ class ServiceProcess { pid_t child_; }; -class BinderTest : public ::testing::Test { +class MemunreachableBinderTest : public ::testing::Test { protected: ServiceProcess service_process_; }; -TEST_F(BinderTest, binder) { +// Tests that a local binder object with a remote strong reference is visible +// through the libmemunreachable BinderReferences interface, which uses the +// getBinderKernelReferences method in libbinder. Starts a BinderService +// through ServiceProcess as a remote service to hold the strong reference. +TEST_F(MemunreachableBinderTest, binder) { + ASSERT_EQ(static_cast(0), getuid()) << "This test must be run as root."; + ServiceProcess service_process; ASSERT_TRUE(service_process.Run());