Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2024 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #include <gtest/gtest.h> |
| 17 | |
| 18 | #include <android-base/logging.h> |
| 19 | #include <android/os/IServiceManager.h> |
| 20 | #include <binder/IBinder.h> |
| 21 | #include <binder/IPCThreadState.h> |
| 22 | #include <binder/IServiceManager.h> |
| 23 | #include <binder/IServiceManagerUnitTestHelper.h> |
| 24 | #include "fakeservicemanager/FakeServiceManager.h" |
| 25 | |
| 26 | #include <sys/prctl.h> |
| 27 | #include <thread> |
| 28 | |
| 29 | using namespace android; |
| 30 | |
| 31 | #ifdef LIBBINDER_CLIENT_CACHE |
| 32 | constexpr bool kUseLibbinderCache = true; |
| 33 | #else |
| 34 | constexpr bool kUseLibbinderCache = false; |
| 35 | #endif |
| 36 | |
| 37 | // A service name which is in the static list of cachable services |
| 38 | const String16 kCachedServiceName = String16("isub"); |
| 39 | |
| 40 | #define EXPECT_OK(status) \ |
| 41 | do { \ |
| 42 | binder::Status stat = (status); \ |
| 43 | EXPECT_TRUE(stat.isOk()) << stat; \ |
| 44 | } while (false) |
| 45 | |
| 46 | const String16 kServerName = String16("binderCacheUnitTest"); |
| 47 | |
| 48 | class FooBar : public BBinder { |
| 49 | public: |
| 50 | status_t onTransact(uint32_t, const Parcel&, Parcel*, uint32_t) { |
| 51 | // exit the server |
| 52 | std::thread([] { exit(EXIT_FAILURE); }).detach(); |
| 53 | return OK; |
| 54 | } |
| 55 | void killServer(sp<IBinder> binder) { |
| 56 | Parcel data, reply; |
| 57 | binder->transact(0, data, &reply, 0); |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | class MockAidlServiceManager : public os::IServiceManagerDefault { |
| 62 | public: |
| 63 | MockAidlServiceManager() : innerSm() {} |
| 64 | |
| 65 | binder::Status checkService(const ::std::string& name, os::Service* _out) override { |
| 66 | sp<IBinder> binder = innerSm.getService(String16(name.c_str())); |
| 67 | *_out = os::Service::make<os::Service::Tag::binder>(binder); |
| 68 | return binder::Status::ok(); |
| 69 | } |
| 70 | |
| 71 | binder::Status addService(const std::string& name, const sp<IBinder>& service, |
| 72 | bool allowIsolated, int32_t dumpPriority) override { |
| 73 | return binder::Status::fromStatusT( |
| 74 | innerSm.addService(String16(name.c_str()), service, allowIsolated, dumpPriority)); |
| 75 | } |
| 76 | |
| 77 | FakeServiceManager innerSm; |
| 78 | }; |
| 79 | |
| 80 | class LibbinderCacheTest : public ::testing::Test { |
| 81 | protected: |
| 82 | void SetUp() override { |
| 83 | sp<MockAidlServiceManager> sm = sp<MockAidlServiceManager>::make(); |
| 84 | mServiceManager = getServiceManagerShimFromAidlServiceManagerForTests(sm); |
| 85 | } |
| 86 | |
| 87 | void TearDown() override {} |
| 88 | |
| 89 | public: |
| 90 | void cacheAndConfirmCacheHit(const sp<IBinder>& binder1, const sp<IBinder>& binder2) { |
| 91 | // Add a service |
| 92 | EXPECT_EQ(OK, mServiceManager->addService(kCachedServiceName, binder1)); |
| 93 | // Get the service. This caches it. |
| 94 | sp<IBinder> result = mServiceManager->checkService(kCachedServiceName); |
| 95 | ASSERT_EQ(binder1, result); |
| 96 | |
| 97 | // Add the different binder and replace the service. |
| 98 | // The cache should still hold the original binder. |
| 99 | EXPECT_EQ(OK, mServiceManager->addService(kCachedServiceName, binder2)); |
| 100 | |
| 101 | result = mServiceManager->checkService(kCachedServiceName); |
| 102 | if (kUseLibbinderCache) { |
| 103 | // If cache is enabled, we should get the binder to Service Manager. |
| 104 | EXPECT_EQ(binder1, result); |
| 105 | } else { |
| 106 | // If cache is disabled, then we should get the newer binder |
| 107 | EXPECT_EQ(binder2, result); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | sp<android::IServiceManager> mServiceManager; |
| 112 | }; |
| 113 | |
| 114 | TEST_F(LibbinderCacheTest, AddLocalServiceAndConfirmCacheHit) { |
| 115 | sp<IBinder> binder1 = sp<BBinder>::make(); |
| 116 | sp<IBinder> binder2 = sp<BBinder>::make(); |
| 117 | |
| 118 | cacheAndConfirmCacheHit(binder1, binder2); |
| 119 | } |
| 120 | |
| 121 | TEST_F(LibbinderCacheTest, AddRemoteServiceAndConfirmCacheHit) { |
| 122 | sp<IBinder> binder1 = defaultServiceManager()->checkService(kServerName); |
| 123 | ASSERT_NE(binder1, nullptr); |
| 124 | sp<IBinder> binder2 = IInterface::asBinder(mServiceManager); |
| 125 | |
| 126 | cacheAndConfirmCacheHit(binder1, binder2); |
| 127 | } |
| 128 | |
| 129 | TEST_F(LibbinderCacheTest, RemoveFromCacheOnServerDeath) { |
| 130 | sp<IBinder> binder1 = defaultServiceManager()->checkService(kServerName); |
| 131 | FooBar foo = FooBar(); |
| 132 | |
| 133 | EXPECT_EQ(OK, mServiceManager->addService(kCachedServiceName, binder1)); |
| 134 | |
| 135 | // Check Service, this caches the binder |
| 136 | sp<IBinder> result = mServiceManager->checkService(kCachedServiceName); |
| 137 | ASSERT_EQ(binder1, result); |
| 138 | |
| 139 | // Kill the server, this should remove from cache. |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 140 | pid_t pid; |
| 141 | ASSERT_EQ(OK, binder1->getDebugPid(&pid)); |
Parth Sane | 9dc9984 | 2024-09-17 18:07:22 +0000 | [diff] [blame] | 142 | foo.killServer(binder1); |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 143 | system(("kill -9 " + std::to_string(pid)).c_str()); |
| 144 | |
| 145 | sp<IBinder> binder2 = sp<BBinder>::make(); |
| 146 | |
| 147 | // Add new service with the same name. |
| 148 | // This will replace the service in FakeServiceManager. |
| 149 | EXPECT_EQ(OK, mServiceManager->addService(kCachedServiceName, binder2)); |
| 150 | |
| 151 | // Confirm that new service is returned instead of old. |
Parth Sane | d73a510 | 2024-09-30 17:47:28 +0000 | [diff] [blame^] | 152 | int retry_count = 5; |
| 153 | sp<IBinder> result2; |
| 154 | do { |
| 155 | std::this_thread::sleep_for(std::chrono::milliseconds(50)); |
| 156 | if (retry_count-- == 0) { |
| 157 | break; |
| 158 | } |
| 159 | result2 = mServiceManager->checkService(kCachedServiceName); |
| 160 | } while (result2 != binder2); |
| 161 | |
Parth Sane | b6ed0eb | 2024-06-25 14:38:42 +0000 | [diff] [blame] | 162 | ASSERT_EQ(binder2, result2); |
| 163 | } |
| 164 | |
| 165 | TEST_F(LibbinderCacheTest, NullBinderNotCached) { |
| 166 | sp<IBinder> binder1 = nullptr; |
| 167 | sp<IBinder> binder2 = sp<BBinder>::make(); |
| 168 | |
| 169 | // Check for a cacheble service which isn't registered. |
| 170 | // FakeServiceManager should return nullptr. |
| 171 | // This shouldn't be cached. |
| 172 | sp<IBinder> result = mServiceManager->checkService(kCachedServiceName); |
| 173 | ASSERT_EQ(binder1, result); |
| 174 | |
| 175 | // Add the same service |
| 176 | EXPECT_EQ(OK, mServiceManager->addService(kCachedServiceName, binder2)); |
| 177 | |
| 178 | // This should return the newly added service. |
| 179 | result = mServiceManager->checkService(kCachedServiceName); |
| 180 | EXPECT_EQ(binder2, result); |
| 181 | } |
| 182 | |
| 183 | TEST_F(LibbinderCacheTest, DoNotCacheServiceNotInList) { |
| 184 | sp<IBinder> binder1 = sp<BBinder>::make(); |
| 185 | sp<IBinder> binder2 = sp<BBinder>::make(); |
| 186 | String16 serviceName = String16("NewLibbinderCacheTest"); |
| 187 | // Add a service |
| 188 | EXPECT_EQ(OK, mServiceManager->addService(serviceName, binder1)); |
| 189 | // Get the service. This shouldn't caches it. |
| 190 | sp<IBinder> result = mServiceManager->checkService(serviceName); |
| 191 | ASSERT_EQ(binder1, result); |
| 192 | |
| 193 | // Add the different binder and replace the service. |
| 194 | EXPECT_EQ(OK, mServiceManager->addService(serviceName, binder2)); |
| 195 | |
| 196 | // Confirm that we get the new service |
| 197 | result = mServiceManager->checkService(serviceName); |
| 198 | EXPECT_EQ(binder2, result); |
| 199 | } |
| 200 | |
| 201 | int main(int argc, char** argv) { |
| 202 | ::testing::InitGoogleTest(&argc, argv); |
| 203 | |
| 204 | if (fork() == 0) { |
| 205 | prctl(PR_SET_PDEATHSIG, SIGHUP); |
| 206 | |
| 207 | // Start a FooBar service and add it to the servicemanager. |
| 208 | sp<IBinder> server = new FooBar(); |
| 209 | defaultServiceManager()->addService(kServerName, server); |
| 210 | |
| 211 | IPCThreadState::self()->joinThreadPool(true); |
| 212 | exit(1); // should not reach |
| 213 | } |
| 214 | |
| 215 | status_t err = ProcessState::self()->setThreadPoolMaxThreadCount(3); |
| 216 | ProcessState::self()->startThreadPool(); |
| 217 | CHECK_EQ(ProcessState::self()->isThreadPoolStarted(), true); |
| 218 | CHECK_GT(ProcessState::self()->getThreadPoolMaxTotalThreadCount(), 0); |
| 219 | |
| 220 | auto binder = defaultServiceManager()->waitForService(kServerName); |
| 221 | CHECK_NE(nullptr, binder.get()); |
| 222 | return RUN_ALL_TESTS(); |
| 223 | } |