Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
| 17 | #include <android-base/logging.h> |
Steven Moreland | 02f7565 | 2018-09-18 14:08:30 -0700 | [diff] [blame] | 18 | #include <android/binder_ibinder_jni.h> |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 19 | #include <android/binder_manager.h> |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 20 | #include <android/binder_process.h> |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 21 | #include <gtest/gtest.h> |
| 22 | #include <iface/iface.h> |
| 23 | |
Steven Moreland | 507547f | 2018-11-06 15:58:05 -0800 | [diff] [blame] | 24 | #include <chrono> |
| 25 | #include <condition_variable> |
| 26 | #include <mutex> |
| 27 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 28 | using ::android::sp; |
| 29 | |
| 30 | constexpr char kExistingNonNdkService[] = "SurfaceFlinger"; |
| 31 | |
| 32 | // This is too slow |
| 33 | // TEST(NdkBinder, GetServiceThatDoesntExist) { |
| 34 | // sp<IFoo> foo = IFoo::getService("asdfghkl;"); |
| 35 | // EXPECT_EQ(nullptr, foo.get()); |
| 36 | // } |
| 37 | |
| 38 | TEST(NdkBinder, DoubleNumber) { |
| 39 | sp<IFoo> foo = IFoo::getService(IFoo::kSomeInstanceName); |
| 40 | ASSERT_NE(foo, nullptr); |
Steven Moreland | 507547f | 2018-11-06 15:58:05 -0800 | [diff] [blame] | 41 | |
| 42 | int32_t out; |
| 43 | EXPECT_EQ(STATUS_OK, foo->doubleNumber(1, &out)); |
| 44 | EXPECT_EQ(2, out); |
| 45 | } |
| 46 | |
| 47 | void LambdaOnDeath(void* cookie) { |
| 48 | auto onDeath = static_cast<std::function<void(void)>*>(cookie); |
| 49 | (*onDeath)(); |
| 50 | }; |
| 51 | TEST(NdkBinder, DeathRecipient) { |
| 52 | using namespace std::chrono_literals; |
| 53 | |
| 54 | AIBinder* binder; |
| 55 | sp<IFoo> foo = IFoo::getService(IFoo::kInstanceNameToDieFor, &binder); |
| 56 | ASSERT_NE(nullptr, foo.get()); |
| 57 | ASSERT_NE(nullptr, binder); |
| 58 | |
| 59 | std::mutex deathMutex; |
| 60 | std::condition_variable deathCv; |
| 61 | bool deathRecieved = false; |
| 62 | |
| 63 | std::function<void(void)> onDeath = [&] { |
| 64 | std::cerr << "Binder died (as requested)." << std::endl; |
| 65 | deathRecieved = true; |
| 66 | deathCv.notify_one(); |
| 67 | }; |
| 68 | |
| 69 | AIBinder_DeathRecipient* recipient = AIBinder_DeathRecipient_new(LambdaOnDeath); |
| 70 | |
| 71 | EXPECT_EQ(STATUS_OK, AIBinder_linkToDeath(binder, recipient, static_cast<void*>(&onDeath))); |
| 72 | |
| 73 | // the binder driver should return this if the service dies during the transaction |
| 74 | EXPECT_EQ(STATUS_DEAD_OBJECT, foo->die()); |
| 75 | |
| 76 | std::unique_lock<std::mutex> lock(deathMutex); |
| 77 | EXPECT_TRUE(deathCv.wait_for(lock, 1s, [&] { return deathRecieved; })); |
| 78 | EXPECT_TRUE(deathRecieved); |
| 79 | |
| 80 | AIBinder_DeathRecipient_delete(recipient); |
| 81 | AIBinder_decStrong(binder); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | TEST(NdkBinder, RetrieveNonNdkService) { |
| 85 | AIBinder* binder = AServiceManager_getService(kExistingNonNdkService); |
| 86 | ASSERT_NE(nullptr, binder); |
Steven Moreland | 65867d7 | 2018-09-04 14:22:26 -0700 | [diff] [blame] | 87 | EXPECT_TRUE(AIBinder_isRemote(binder)); |
| 88 | EXPECT_TRUE(AIBinder_isAlive(binder)); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 89 | EXPECT_EQ(STATUS_OK, AIBinder_ping(binder)); |
Steven Moreland | 65867d7 | 2018-09-04 14:22:26 -0700 | [diff] [blame] | 90 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 91 | AIBinder_decStrong(binder); |
| 92 | } |
| 93 | |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 94 | void OnBinderDeath(void* cookie) { |
| 95 | LOG(ERROR) << "BINDER DIED. COOKIE: " << cookie; |
| 96 | } |
| 97 | |
| 98 | TEST(NdkBinder, LinkToDeath) { |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 99 | AIBinder* binder = AServiceManager_getService(kExistingNonNdkService); |
| 100 | ASSERT_NE(nullptr, binder); |
| 101 | |
| 102 | AIBinder_DeathRecipient* recipient = AIBinder_DeathRecipient_new(OnBinderDeath); |
| 103 | ASSERT_NE(nullptr, recipient); |
| 104 | |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 105 | EXPECT_EQ(STATUS_OK, AIBinder_linkToDeath(binder, recipient, nullptr)); |
| 106 | EXPECT_EQ(STATUS_OK, AIBinder_linkToDeath(binder, recipient, nullptr)); |
| 107 | EXPECT_EQ(STATUS_OK, AIBinder_unlinkToDeath(binder, recipient, nullptr)); |
| 108 | EXPECT_EQ(STATUS_OK, AIBinder_unlinkToDeath(binder, recipient, nullptr)); |
| 109 | EXPECT_EQ(STATUS_NAME_NOT_FOUND, AIBinder_unlinkToDeath(binder, recipient, nullptr)); |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 110 | |
Steven Moreland | 9b80e28 | 2018-09-19 13:57:23 -0700 | [diff] [blame] | 111 | AIBinder_DeathRecipient_delete(recipient); |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 112 | AIBinder_decStrong(binder); |
| 113 | } |
| 114 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 115 | class MyTestFoo : public IFoo { |
Steven Moreland | 507547f | 2018-11-06 15:58:05 -0800 | [diff] [blame] | 116 | binder_status_t doubleNumber(int32_t in, int32_t* out) override { |
| 117 | *out = 2 * in; |
| 118 | LOG(INFO) << "doubleNumber (" << in << ") => " << *out; |
| 119 | return STATUS_OK; |
| 120 | } |
| 121 | binder_status_t die() override { |
| 122 | ADD_FAILURE() << "die called on local instance"; |
| 123 | return STATUS_OK; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 124 | } |
| 125 | }; |
| 126 | |
| 127 | TEST(NdkBinder, GetServiceInProcess) { |
| 128 | static const char* kInstanceName = "test-get-service-in-process"; |
| 129 | |
| 130 | sp<IFoo> foo = new MyTestFoo; |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 131 | EXPECT_EQ(STATUS_OK, foo->addService(kInstanceName)); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 132 | |
| 133 | sp<IFoo> getFoo = IFoo::getService(kInstanceName); |
| 134 | EXPECT_EQ(foo.get(), getFoo.get()); |
| 135 | |
Steven Moreland | 507547f | 2018-11-06 15:58:05 -0800 | [diff] [blame] | 136 | int32_t out; |
| 137 | EXPECT_EQ(STATUS_OK, getFoo->doubleNumber(1, &out)); |
| 138 | EXPECT_EQ(2, out); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 141 | TEST(NdkBinder, EqualityOfRemoteBinderPointer) { |
| 142 | AIBinder* binderA = AServiceManager_getService(kExistingNonNdkService); |
| 143 | ASSERT_NE(nullptr, binderA); |
| 144 | |
| 145 | AIBinder* binderB = AServiceManager_getService(kExistingNonNdkService); |
| 146 | ASSERT_NE(nullptr, binderB); |
| 147 | |
| 148 | EXPECT_EQ(binderA, binderB); |
| 149 | |
| 150 | AIBinder_decStrong(binderA); |
| 151 | AIBinder_decStrong(binderB); |
| 152 | } |
| 153 | |
Steven Moreland | 02f7565 | 2018-09-18 14:08:30 -0700 | [diff] [blame] | 154 | TEST(NdkBinder, ToFromJavaNullptr) { |
| 155 | EXPECT_EQ(nullptr, AIBinder_toJavaBinder(nullptr, nullptr)); |
| 156 | EXPECT_EQ(nullptr, AIBinder_fromJavaBinder(nullptr, nullptr)); |
| 157 | } |
| 158 | |
Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 159 | TEST(NdkBinder, ABpBinderRefCount) { |
| 160 | AIBinder* binder = AServiceManager_getService(kExistingNonNdkService); |
| 161 | AIBinder_Weak* wBinder = AIBinder_Weak_new(binder); |
| 162 | |
| 163 | ASSERT_NE(nullptr, binder); |
| 164 | EXPECT_EQ(1, AIBinder_debugGetRefCount(binder)); |
| 165 | |
| 166 | AIBinder_decStrong(binder); |
| 167 | |
| 168 | // assert because would need to decStrong if non-null and we shouldn't need to add a no-op here |
| 169 | ASSERT_NE(nullptr, AIBinder_Weak_promote(wBinder)); |
| 170 | |
Steven Moreland | 9b80e28 | 2018-09-19 13:57:23 -0700 | [diff] [blame] | 171 | AIBinder_Weak_delete(wBinder); |
Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 174 | TEST(NdkBinder, AddServiceMultipleTimes) { |
| 175 | static const char* kInstanceName1 = "test-multi-1"; |
| 176 | static const char* kInstanceName2 = "test-multi-2"; |
| 177 | sp<IFoo> foo = new MyTestFoo; |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 178 | EXPECT_EQ(STATUS_OK, foo->addService(kInstanceName1)); |
| 179 | EXPECT_EQ(STATUS_OK, foo->addService(kInstanceName2)); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 180 | EXPECT_EQ(IFoo::getService(kInstanceName1), IFoo::getService(kInstanceName2)); |
| 181 | } |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 182 | |
Steven Moreland | 507547f | 2018-11-06 15:58:05 -0800 | [diff] [blame] | 183 | int main(int argc, char* argv[]) { |
| 184 | ::testing::InitGoogleTest(&argc, argv); |
| 185 | |
| 186 | ABinderProcess_setThreadPoolMaxThreadCount(1); // to recieve death notifications/callbacks |
| 187 | ABinderProcess_startThreadPool(); |
| 188 | |
| 189 | return RUN_ALL_TESTS(); |
| 190 | } |
| 191 | |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 192 | #include <android/binder_auto_utils.h> |
| 193 | #include <android/binder_interface_utils.h> |
Steven Moreland | a884566 | 2018-10-12 11:53:03 -0700 | [diff] [blame] | 194 | #include <android/binder_parcel_utils.h> |