From 77649f57920a2c552ad406c15a12894c996dc837 Mon Sep 17 00:00:00 2001 From: Devin Moore Date: Thu, 11 Jan 2024 00:45:09 +0000 Subject: hidl_test(_java) handle missing ashmem allocator If android.hidl.allocator is not declared in the vintf manifest, don't expect it to be installed on the device. Test: atest hidl_test_java hidl_test Bug: 319502861 Change-Id: Ib7f591315fbddec59d3cf3714e87041987c28aec --- test/hidl_test/hidl_test_client.cpp | 20 ++++++++++++++------ test/java_test/hidl_test_java_native.cpp | 13 ++++++++++--- .../commands/hidl_test_java/HidlTestJava.java | 8 +++++++- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/test/hidl_test/hidl_test_client.cpp b/test/hidl_test/hidl_test_client.cpp index 55490d28..614ffdbc 100644 --- a/test/hidl_test/hidl_test_client.cpp +++ b/test/hidl_test/hidl_test_client.cpp @@ -367,15 +367,20 @@ public: ASSERT_NE(manager, nullptr); ASSERT_TRUE(manager->isRemote()); // manager is always remote - // Token manager only exists on devices before Android V - tokenManager = ITokenManager::getService(); - if (tokenManager) { + if (IServiceManager::Transport::EMPTY != + manager->getTransport(ITokenManager::descriptor, "default")) { + // Token manager only exists on devices before Android V + tokenManager = ITokenManager::getService(); + ASSERT_TRUE(tokenManager); ASSERT_TRUE(tokenManager->isRemote()); // tokenManager is always remote } - ashmemAllocator = IAllocator::getService("ashmem"); - ASSERT_NE(ashmemAllocator, nullptr); - ASSERT_TRUE(ashmemAllocator->isRemote()); // allocator is always remote + if (IServiceManager::Transport::EMPTY != + manager->getTransport(IAllocator::descriptor, "ashmem")) { + ashmemAllocator = IAllocator::getService("ashmem"); + ASSERT_NE(ashmemAllocator, nullptr); + ASSERT_TRUE(ashmemAllocator->isRemote()); // allocator is always remote + } // getStub is true if we are in passthrough mode to skip checking // binderized server, false for binderized mode. @@ -897,6 +902,7 @@ TEST_F(HidlTest, TestToken) { } TEST_F(HidlTest, TestSharedMemory) { + if (!ashmemAllocator) GTEST_SKIP() << "ashmem allocator is not on the device"; const uint8_t kValue = 0xCA; hidl_memory mem_copy; EXPECT_OK(ashmemAllocator->allocate(1024, [&](bool success, const hidl_memory& mem) { @@ -960,6 +966,7 @@ TEST_F(HidlTest, TestSharedMemory) { } TEST_F(HidlTest, BatchSharedMemory) { + if (!ashmemAllocator) GTEST_SKIP() << "ashmem allocator is not on the device"; const uint8_t kValue = 0xCA; const uint64_t kBatchSize = 2; hidl_vec batchCopy; @@ -1009,6 +1016,7 @@ TEST_F(HidlTest, BatchSharedMemory) { } TEST_F(HidlTest, MemoryBlock) { + if (!ashmemAllocator) GTEST_SKIP() << "ashmem allocator is not on the device"; const uint8_t kValue = 0xCA; using ::android::hardware::IBinder; using ::android::hardware::interfacesEqual; diff --git a/test/java_test/hidl_test_java_native.cpp b/test/java_test/hidl_test_java_native.cpp index 9827f072..149a17bf 100644 --- a/test/java_test/hidl_test_java_native.cpp +++ b/test/java_test/hidl_test_java_native.cpp @@ -1278,9 +1278,16 @@ int main(int argc, char **argv) { status = registerPassthroughServiceImplementation(); CHECK(status == ::android::OK) << "ISafeUnion didn't register"; - sp memoryInterface = new MemoryInterface(); - status = memoryInterface->registerAsService(); - CHECK(status == ::android::OK) << "IMemoryInterface didn't register"; + using android::hardware::defaultServiceManager; + using android::hidl::manager::V1_0::IServiceManager; + // If the android.hidl.allocator service isn't on the device, then don't + // create or register the MemoryInterface + if (IServiceManager::Transport::EMPTY != + defaultServiceManager()->getTransport(IAllocator::descriptor, "ashmem")) { + sp memoryInterface = new MemoryInterface(); + status = memoryInterface->registerAsService(); + CHECK(status == ::android::OK) << "IMemoryInterface didn't register"; + } joinRpcThreadpool(); return 0; diff --git a/test/java_test/src/com/android/commands/hidl_test_java/HidlTestJava.java b/test/java_test/src/com/android/commands/hidl_test_java/HidlTestJava.java index 47adad8f..0999a92a 100644 --- a/test/java_test/src/com/android/commands/hidl_test_java/HidlTestJava.java +++ b/test/java_test/src/com/android/commands/hidl_test_java/HidlTestJava.java @@ -245,7 +245,13 @@ public final class HidlTestJava { } private void runClientMemoryTests() throws RemoteException, IOException, ErrnoException { - IMemoryInterface memoryInterface = IMemoryInterface.getService(); + IMemoryInterface memoryInterface; + try { + memoryInterface = IMemoryInterface.getService(); + } catch (NoSuchElementException e) { + // If the test didn't register the memory interface, don't try to test it. + return; + } { HidlMemory hidlMem = HidlMemoryUtil.byteArrayToHidlMemory( -- cgit v1.2.3