• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <IBinderNdkUnitTest.h>
18 #include <aidl/BnBinderNdkUnitTest.h>
19 #include <aidl/BnEmpty.h>
20 #include <android-base/logging.h>
21 #include <android/binder_ibinder_jni.h>
22 #include <android/binder_ibinder_platform.h>
23 #include <android/binder_libbinder.h>
24 #include <android/binder_manager.h>
25 #include <android/binder_process.h>
26 #include <gtest/gtest.h>
27 #include <iface/iface.h>
28 #include <utils/Looper.h>
29 
30 // warning: this is assuming that libbinder_ndk is using the same copy
31 // of libbinder that we are.
32 #include <binder/IPCThreadState.h>
33 #include <binder/IResultReceiver.h>
34 #include <binder/IServiceManager.h>
35 #include <binder/IShellCallback.h>
36 
37 #include <sys/prctl.h>
38 #include <chrono>
39 #include <condition_variable>
40 #include <iostream>
41 #include <mutex>
42 #include <thread>
43 #include "android/binder_ibinder.h"
44 
45 using namespace android;
46 
47 constexpr char kExistingNonNdkService[] = "SurfaceFlinger";
48 constexpr char kBinderNdkUnitTestService[] = "BinderNdkUnitTest";
49 constexpr char kLazyBinderNdkUnitTestService[] = "LazyBinderNdkUnitTest";
50 constexpr char kForcePersistNdkUnitTestService[] = "ForcePersistNdkUnitTestService";
51 constexpr char kActiveServicesNdkUnitTestService[] = "ActiveServicesNdkUnitTestService";
52 
53 constexpr unsigned int kShutdownWaitTime = 10;
54 constexpr uint64_t kContextTestValue = 0xb4e42fb4d9a1d715;
55 
56 class MyBinderNdkUnitTest : public aidl::BnBinderNdkUnitTest {
repeatInt(int32_t in,int32_t * out)57     ndk::ScopedAStatus repeatInt(int32_t in, int32_t* out) {
58         *out = in;
59         return ndk::ScopedAStatus::ok();
60     }
takeInterface(const std::shared_ptr<aidl::IEmpty> & empty)61     ndk::ScopedAStatus takeInterface(const std::shared_ptr<aidl::IEmpty>& empty) {
62         (void)empty;
63         return ndk::ScopedAStatus::ok();
64     }
forceFlushCommands()65     ndk::ScopedAStatus forceFlushCommands() {
66         // warning: this is assuming that libbinder_ndk is using the same copy
67         // of libbinder that we are.
68         android::IPCThreadState::self()->flushCommands();
69         return ndk::ScopedAStatus::ok();
70     }
getsRequestedSid(bool * out)71     ndk::ScopedAStatus getsRequestedSid(bool* out) {
72         const char* sid = AIBinder_getCallingSid();
73         std::cout << "Got security context: " << (sid ?: "null") << std::endl;
74         *out = sid != nullptr;
75         return ndk::ScopedAStatus::ok();
76     }
handleShellCommand(int,int out,int,const char ** args,uint32_t numArgs)77     binder_status_t handleShellCommand(int /*in*/, int out, int /*err*/, const char** args,
78                                        uint32_t numArgs) override {
79         for (uint32_t i = 0; i < numArgs; i++) {
80             dprintf(out, "%s", args[i]);
81         }
82         fsync(out);
83         return STATUS_OK;
84     }
forcePersist(bool persist)85     ndk::ScopedAStatus forcePersist(bool persist) {
86         AServiceManager_forceLazyServicesPersist(persist);
87         return ndk::ScopedAStatus::ok();
88     }
setCustomActiveServicesCallback()89     ndk::ScopedAStatus setCustomActiveServicesCallback() {
90         AServiceManager_setActiveServicesCallback(activeServicesCallback, this);
91         return ndk::ScopedAStatus::ok();
92     }
activeServicesCallback(bool hasClients,void * context)93     static bool activeServicesCallback(bool hasClients, void* context) {
94         if (hasClients) {
95             return false;
96         }
97 
98         // Unregister all services
99         if (!AServiceManager_tryUnregister()) {
100             // Prevent shutdown (test will fail)
101             return false;
102         }
103 
104         // Re-register all services
105         AServiceManager_reRegister();
106 
107         // Unregister again before shutdown
108         if (!AServiceManager_tryUnregister()) {
109             // Prevent shutdown (test will fail)
110             return false;
111         }
112 
113         // Check if the context was passed correctly
114         MyBinderNdkUnitTest* service = static_cast<MyBinderNdkUnitTest*>(context);
115         if (service->contextTestValue != kContextTestValue) {
116             // Prevent shutdown (test will fail)
117             return false;
118         }
119 
120         exit(EXIT_SUCCESS);
121         // Unreachable
122     }
123 
124     uint64_t contextTestValue = kContextTestValue;
125 };
126 
generatedService()127 int generatedService() {
128     ABinderProcess_setThreadPoolMaxThreadCount(0);
129 
130     auto service = ndk::SharedRefBase::make<MyBinderNdkUnitTest>();
131     auto binder = service->asBinder();
132 
133     AIBinder_setRequestingSid(binder.get(), true);
134 
135     binder_exception_t exception =
136             AServiceManager_addService(binder.get(), kBinderNdkUnitTestService);
137 
138     if (exception != EX_NONE) {
139         LOG(FATAL) << "Could not register: " << exception << " " << kBinderNdkUnitTestService;
140     }
141 
142     ABinderProcess_joinThreadPool();
143 
144     return 1;  // should not return
145 }
146 
147 // manually-written parceling class considered bad practice
148 class MyFoo : public IFoo {
doubleNumber(int32_t in,int32_t * out)149     binder_status_t doubleNumber(int32_t in, int32_t* out) override {
150         *out = 2 * in;
151         LOG(INFO) << "doubleNumber (" << in << ") => " << *out;
152         return STATUS_OK;
153     }
154 
die()155     binder_status_t die() override {
156         LOG(FATAL) << "IFoo::die called!";
157         return STATUS_UNKNOWN_ERROR;
158     }
159 };
160 
manualService(const char * instance)161 void manualService(const char* instance) {
162     // Strong reference to MyFoo kept by service manager.
163     binder_exception_t exception = (new MyFoo)->addService(instance);
164 
165     if (exception != EX_NONE) {
166         LOG(FATAL) << "Could not register: " << exception << " " << instance;
167     }
168 }
manualPollingService(const char * instance)169 int manualPollingService(const char* instance) {
170     int fd;
171     CHECK(STATUS_OK == ABinderProcess_setupPolling(&fd));
172     manualService(instance);
173 
174     class Handler : public LooperCallback {
175         int handleEvent(int /*fd*/, int /*events*/, void* /*data*/) override {
176             ABinderProcess_handlePolledCommands();
177             return 1;  // Continue receiving callbacks.
178         }
179     };
180 
181     sp<Looper> looper = Looper::prepare(0 /* opts */);
182     looper->addFd(fd, Looper::POLL_CALLBACK, Looper::EVENT_INPUT, new Handler(), nullptr /*data*/);
183     // normally, would add additional fds
184     while (true) {
185         looper->pollAll(-1 /* timeoutMillis */);
186     }
187     return 1;  // should not reach
188 }
manualThreadPoolService(const char * instance)189 int manualThreadPoolService(const char* instance) {
190     ABinderProcess_setThreadPoolMaxThreadCount(0);
191     manualService(instance);
192     ABinderProcess_joinThreadPool();
193     return 1;
194 }
195 
lazyService(const char * instance)196 int lazyService(const char* instance) {
197     ABinderProcess_setThreadPoolMaxThreadCount(0);
198     // Wait to register this service to make sure the main test process will
199     // actually wait for the service to be available. Tested with sleep(60),
200     // and reduced for sake of time.
201     sleep(1);
202     // Strong reference to MyBinderNdkUnitTest kept by service manager.
203     // This is just for testing, it has no corresponding init behavior.
204     auto service = ndk::SharedRefBase::make<MyBinderNdkUnitTest>();
205     auto binder = service->asBinder();
206 
207     binder_status_t status = AServiceManager_registerLazyService(binder.get(), instance);
208     if (status != STATUS_OK) {
209         LOG(FATAL) << "Could not register: " << status << " " << instance;
210     }
211 
212     ABinderProcess_joinThreadPool();
213 
214     return 1;  // should not return
215 }
216 
isServiceRunning(const char * serviceName)217 bool isServiceRunning(const char* serviceName) {
218     AIBinder* binder = AServiceManager_checkService(serviceName);
219     if (binder == nullptr) {
220         return false;
221     }
222     AIBinder_decStrong(binder);
223 
224     return true;
225 }
226 
TEST(NdkBinder,DetectDoubleOwn)227 TEST(NdkBinder, DetectDoubleOwn) {
228     auto badService = ndk::SharedRefBase::make<MyBinderNdkUnitTest>();
229     EXPECT_DEATH(std::shared_ptr<MyBinderNdkUnitTest>(badService.get()),
230                  "Is this object double-owned?");
231 }
232 
TEST(NdkBinder,DetectNoSharedRefBaseCreated)233 TEST(NdkBinder, DetectNoSharedRefBaseCreated) {
234     EXPECT_DEATH(MyBinderNdkUnitTest(), "SharedRefBase: no ref created during lifetime");
235 }
236 
TEST(NdkBinder,GetServiceThatDoesntExist)237 TEST(NdkBinder, GetServiceThatDoesntExist) {
238     sp<IFoo> foo = IFoo::getService("asdfghkl;");
239     EXPECT_EQ(nullptr, foo.get());
240 }
241 
TEST(NdkBinder,CheckServiceThatDoesntExist)242 TEST(NdkBinder, CheckServiceThatDoesntExist) {
243     AIBinder* binder = AServiceManager_checkService("asdfghkl;");
244     ASSERT_EQ(nullptr, binder);
245 }
246 
TEST(NdkBinder,CheckServiceThatDoesExist)247 TEST(NdkBinder, CheckServiceThatDoesExist) {
248     AIBinder* binder = AServiceManager_checkService(kExistingNonNdkService);
249     EXPECT_NE(nullptr, binder);
250     EXPECT_EQ(STATUS_OK, AIBinder_ping(binder));
251 
252     AIBinder_decStrong(binder);
253 }
254 
TEST(NdkBinder,UnimplementedDump)255 TEST(NdkBinder, UnimplementedDump) {
256     sp<IFoo> foo = IFoo::getService(IFoo::kSomeInstanceName);
257     ASSERT_NE(foo, nullptr);
258     AIBinder* binder = foo->getBinder();
259     EXPECT_EQ(OK, AIBinder_dump(binder, STDOUT_FILENO, nullptr, 0));
260     AIBinder_decStrong(binder);
261 }
262 
TEST(NdkBinder,UnimplementedShell)263 TEST(NdkBinder, UnimplementedShell) {
264     // libbinder_ndk doesn't support calling shell, so we are calling from the
265     // libbinder across processes to the NDK service which doesn't implement
266     // shell
267     static const sp<android::IServiceManager> sm(android::defaultServiceManager());
268     sp<IBinder> testService = sm->getService(String16(IFoo::kSomeInstanceName));
269 
270     Vector<String16> argsVec;
271     EXPECT_EQ(OK, IBinder::shellCommand(testService, STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO,
272                                         argsVec, nullptr, nullptr));
273 }
274 
TEST(NdkBinder,DoubleNumber)275 TEST(NdkBinder, DoubleNumber) {
276     sp<IFoo> foo = IFoo::getService(IFoo::kSomeInstanceName);
277     ASSERT_NE(foo, nullptr);
278 
279     int32_t out;
280     EXPECT_EQ(STATUS_OK, foo->doubleNumber(1, &out));
281     EXPECT_EQ(2, out);
282 }
283 
TEST(NdkBinder,GetTestServiceStressTest)284 TEST(NdkBinder, GetTestServiceStressTest) {
285     // libbinder has some complicated logic to make sure only one instance of
286     // ABpBinder is associated with each binder.
287 
288     constexpr size_t kNumThreads = 10;
289     constexpr size_t kNumCalls = 1000;
290     std::vector<std::thread> threads;
291 
292     for (size_t i = 0; i < kNumThreads; i++) {
293         threads.push_back(std::thread([&]() {
294             for (size_t j = 0; j < kNumCalls; j++) {
295                 auto binder =
296                         ndk::SpAIBinder(AServiceManager_checkService(IFoo::kSomeInstanceName));
297                 EXPECT_EQ(STATUS_OK, AIBinder_ping(binder.get()));
298             }
299         }));
300     }
301 
302     for (auto& thread : threads) thread.join();
303 }
304 
defaultInstanceCounter(const char * instance,void * context)305 void defaultInstanceCounter(const char* instance, void* context) {
306     if (strcmp(instance, "default") == 0) {
307         ++*(size_t*)(context);
308     }
309 }
310 
TEST(NdkBinder,GetDeclaredInstances)311 TEST(NdkBinder, GetDeclaredInstances) {
312     bool hasLight = AServiceManager_isDeclared("android.hardware.light.ILights/default");
313 
314     size_t count;
315     AServiceManager_forEachDeclaredInstance("android.hardware.light.ILights", &count,
316                                             defaultInstanceCounter);
317 
318     // At the time of writing this test, there is no good interface guaranteed
319     // to be on all devices. Cuttlefish has light, so this will generally test
320     // things.
321     EXPECT_EQ(count, hasLight ? 1 : 0);
322 }
323 
TEST(NdkBinder,GetLazyService)324 TEST(NdkBinder, GetLazyService) {
325     // Not declared in the vintf manifest
326     ASSERT_FALSE(AServiceManager_isDeclared(kLazyBinderNdkUnitTestService));
327     ndk::SpAIBinder binder(AServiceManager_waitForService(kLazyBinderNdkUnitTestService));
328     std::shared_ptr<aidl::IBinderNdkUnitTest> service =
329             aidl::IBinderNdkUnitTest::fromBinder(binder);
330     ASSERT_NE(service, nullptr);
331 
332     EXPECT_EQ(STATUS_OK, AIBinder_ping(binder.get()));
333 }
334 
TEST(NdkBinder,IsUpdatable)335 TEST(NdkBinder, IsUpdatable) {
336     bool isUpdatable = AServiceManager_isUpdatableViaApex("android.hardware.light.ILights/default");
337     EXPECT_EQ(isUpdatable, false);
338 }
339 
340 // This is too slow
TEST(NdkBinder,CheckLazyServiceShutDown)341 TEST(NdkBinder, CheckLazyServiceShutDown) {
342     ndk::SpAIBinder binder(AServiceManager_waitForService(kLazyBinderNdkUnitTestService));
343     std::shared_ptr<aidl::IBinderNdkUnitTest> service =
344             aidl::IBinderNdkUnitTest::fromBinder(binder);
345     ASSERT_NE(service, nullptr);
346 
347     EXPECT_EQ(STATUS_OK, AIBinder_ping(binder.get()));
348     binder = nullptr;
349     service = nullptr;
350     IPCThreadState::self()->flushCommands();
351     // Make sure the service is dead after some time of no use
352     sleep(kShutdownWaitTime);
353     ASSERT_EQ(nullptr, AServiceManager_checkService(kLazyBinderNdkUnitTestService));
354 }
355 
TEST(NdkBinder,ForcedPersistenceTest)356 TEST(NdkBinder, ForcedPersistenceTest) {
357     for (int i = 0; i < 2; i++) {
358         ndk::SpAIBinder binder(AServiceManager_waitForService(kForcePersistNdkUnitTestService));
359         std::shared_ptr<aidl::IBinderNdkUnitTest> service =
360                 aidl::IBinderNdkUnitTest::fromBinder(binder);
361         ASSERT_NE(service, nullptr);
362         ASSERT_TRUE(service->forcePersist(i == 0).isOk());
363 
364         binder = nullptr;
365         service = nullptr;
366         IPCThreadState::self()->flushCommands();
367 
368         sleep(kShutdownWaitTime);
369 
370         bool isRunning = isServiceRunning(kForcePersistNdkUnitTestService);
371 
372         if (i == 0) {
373             ASSERT_TRUE(isRunning) << "Service shut down when it shouldn't have.";
374         } else {
375             ASSERT_FALSE(isRunning) << "Service failed to shut down.";
376         }
377     }
378 }
379 
TEST(NdkBinder,ActiveServicesCallbackTest)380 TEST(NdkBinder, ActiveServicesCallbackTest) {
381     ndk::SpAIBinder binder(AServiceManager_waitForService(kActiveServicesNdkUnitTestService));
382     std::shared_ptr<aidl::IBinderNdkUnitTest> service =
383             aidl::IBinderNdkUnitTest::fromBinder(binder);
384     ASSERT_NE(service, nullptr);
385     ASSERT_TRUE(service->setCustomActiveServicesCallback().isOk());
386 
387     binder = nullptr;
388     service = nullptr;
389     IPCThreadState::self()->flushCommands();
390 
391     sleep(kShutdownWaitTime);
392 
393     ASSERT_FALSE(isServiceRunning(kActiveServicesNdkUnitTestService))
394             << "Service failed to shut down.";
395 }
396 
397 struct DeathRecipientCookie {
398     std::function<void(void)>*onDeath, *onUnlink;
399 };
LambdaOnDeath(void * cookie)400 void LambdaOnDeath(void* cookie) {
401     auto funcs = static_cast<DeathRecipientCookie*>(cookie);
402     (*funcs->onDeath)();
403 };
LambdaOnUnlink(void * cookie)404 void LambdaOnUnlink(void* cookie) {
405     auto funcs = static_cast<DeathRecipientCookie*>(cookie);
406     (*funcs->onUnlink)();
407 };
TEST(NdkBinder,DeathRecipient)408 TEST(NdkBinder, DeathRecipient) {
409     using namespace std::chrono_literals;
410 
411     AIBinder* binder;
412     sp<IFoo> foo = IFoo::getService(IFoo::kInstanceNameToDieFor, &binder);
413     ASSERT_NE(nullptr, foo.get());
414     ASSERT_NE(nullptr, binder);
415 
416     std::mutex deathMutex;
417     std::condition_variable deathCv;
418     bool deathReceived = false;
419 
420     std::function<void(void)> onDeath = [&] {
421         std::cerr << "Binder died (as requested)." << std::endl;
422         deathReceived = true;
423         deathCv.notify_one();
424     };
425 
426     std::mutex unlinkMutex;
427     std::condition_variable unlinkCv;
428     bool unlinkReceived = false;
429     bool wasDeathReceivedFirst = false;
430 
431     std::function<void(void)> onUnlink = [&] {
432         std::cerr << "Binder unlinked (as requested)." << std::endl;
433         wasDeathReceivedFirst = deathReceived;
434         unlinkReceived = true;
435         unlinkCv.notify_one();
436     };
437 
438     DeathRecipientCookie cookie = {&onDeath, &onUnlink};
439 
440     AIBinder_DeathRecipient* recipient = AIBinder_DeathRecipient_new(LambdaOnDeath);
441     AIBinder_DeathRecipient_setOnUnlinked(recipient, LambdaOnUnlink);
442 
443     EXPECT_EQ(STATUS_OK, AIBinder_linkToDeath(binder, recipient, static_cast<void*>(&cookie)));
444 
445     // the binder driver should return this if the service dies during the transaction
446     EXPECT_EQ(STATUS_DEAD_OBJECT, foo->die());
447 
448     foo = nullptr;
449 
450     std::unique_lock<std::mutex> lockDeath(deathMutex);
451     EXPECT_TRUE(deathCv.wait_for(lockDeath, 1s, [&] { return deathReceived; }));
452     EXPECT_TRUE(deathReceived);
453 
454     std::unique_lock<std::mutex> lockUnlink(unlinkMutex);
455     EXPECT_TRUE(deathCv.wait_for(lockUnlink, 1s, [&] { return unlinkReceived; }));
456     EXPECT_TRUE(unlinkReceived);
457     EXPECT_TRUE(wasDeathReceivedFirst);
458 
459     AIBinder_DeathRecipient_delete(recipient);
460     AIBinder_decStrong(binder);
461     binder = nullptr;
462 }
463 
TEST(NdkBinder,RetrieveNonNdkService)464 TEST(NdkBinder, RetrieveNonNdkService) {
465     AIBinder* binder = AServiceManager_getService(kExistingNonNdkService);
466     ASSERT_NE(nullptr, binder);
467     EXPECT_TRUE(AIBinder_isRemote(binder));
468     EXPECT_TRUE(AIBinder_isAlive(binder));
469     EXPECT_EQ(STATUS_OK, AIBinder_ping(binder));
470 
471     AIBinder_decStrong(binder);
472 }
473 
OnBinderDeath(void * cookie)474 void OnBinderDeath(void* cookie) {
475     LOG(ERROR) << "BINDER DIED. COOKIE: " << cookie;
476 }
477 
TEST(NdkBinder,LinkToDeath)478 TEST(NdkBinder, LinkToDeath) {
479     AIBinder* binder = AServiceManager_getService(kExistingNonNdkService);
480     ASSERT_NE(nullptr, binder);
481 
482     AIBinder_DeathRecipient* recipient = AIBinder_DeathRecipient_new(OnBinderDeath);
483     ASSERT_NE(nullptr, recipient);
484 
485     EXPECT_EQ(STATUS_OK, AIBinder_linkToDeath(binder, recipient, nullptr));
486     EXPECT_EQ(STATUS_OK, AIBinder_linkToDeath(binder, recipient, nullptr));
487     EXPECT_EQ(STATUS_OK, AIBinder_unlinkToDeath(binder, recipient, nullptr));
488     EXPECT_EQ(STATUS_OK, AIBinder_unlinkToDeath(binder, recipient, nullptr));
489     EXPECT_EQ(STATUS_NAME_NOT_FOUND, AIBinder_unlinkToDeath(binder, recipient, nullptr));
490 
491     AIBinder_DeathRecipient_delete(recipient);
492     AIBinder_decStrong(binder);
493 }
494 
495 class MyTestFoo : public IFoo {
doubleNumber(int32_t in,int32_t * out)496     binder_status_t doubleNumber(int32_t in, int32_t* out) override {
497         *out = 2 * in;
498         LOG(INFO) << "doubleNumber (" << in << ") => " << *out;
499         return STATUS_OK;
500     }
die()501     binder_status_t die() override {
502         ADD_FAILURE() << "die called on local instance";
503         return STATUS_OK;
504     }
505 };
506 
TEST(NdkBinder,SetInheritRt)507 TEST(NdkBinder, SetInheritRt) {
508     // functional test in binderLibTest
509     sp<IFoo> foo = sp<MyTestFoo>::make();
510     AIBinder* binder = foo->getBinder();
511 
512     // does not abort
513     AIBinder_setInheritRt(binder, true);
514     AIBinder_setInheritRt(binder, false);
515     AIBinder_setInheritRt(binder, true);
516 
517     AIBinder_decStrong(binder);
518 }
519 
TEST(NdkBinder,SetInheritRtNonLocal)520 TEST(NdkBinder, SetInheritRtNonLocal) {
521     AIBinder* binder = AServiceManager_getService(kExistingNonNdkService);
522     ASSERT_NE(binder, nullptr);
523 
524     ASSERT_TRUE(AIBinder_isRemote(binder));
525 
526     EXPECT_DEATH(AIBinder_setInheritRt(binder, true), "");
527     EXPECT_DEATH(AIBinder_setInheritRt(binder, false), "");
528 
529     AIBinder_decStrong(binder);
530 }
531 
TEST(NdkBinder,AddNullService)532 TEST(NdkBinder, AddNullService) {
533     EXPECT_EQ(EX_ILLEGAL_ARGUMENT, AServiceManager_addService(nullptr, "any-service-name"));
534 }
535 
TEST(NdkBinder,AddInvalidServiceName)536 TEST(NdkBinder, AddInvalidServiceName) {
537     sp<IFoo> foo = new MyTestFoo;
538     EXPECT_EQ(EX_ILLEGAL_ARGUMENT, foo->addService("!@#$%^&"));
539 }
540 
TEST(NdkBinder,GetServiceInProcess)541 TEST(NdkBinder, GetServiceInProcess) {
542     static const char* kInstanceName = "test-get-service-in-process";
543 
544     sp<IFoo> foo = new MyTestFoo;
545     EXPECT_EQ(EX_NONE, foo->addService(kInstanceName));
546 
547     sp<IFoo> getFoo = IFoo::getService(kInstanceName);
548     EXPECT_EQ(foo.get(), getFoo.get());
549 
550     int32_t out;
551     EXPECT_EQ(STATUS_OK, getFoo->doubleNumber(1, &out));
552     EXPECT_EQ(2, out);
553 }
554 
TEST(NdkBinder,EqualityOfRemoteBinderPointer)555 TEST(NdkBinder, EqualityOfRemoteBinderPointer) {
556     AIBinder* binderA = AServiceManager_getService(kExistingNonNdkService);
557     ASSERT_NE(nullptr, binderA);
558 
559     AIBinder* binderB = AServiceManager_getService(kExistingNonNdkService);
560     ASSERT_NE(nullptr, binderB);
561 
562     EXPECT_EQ(binderA, binderB);
563 
564     AIBinder_decStrong(binderA);
565     AIBinder_decStrong(binderB);
566 }
567 
TEST(NdkBinder,ToFromJavaNullptr)568 TEST(NdkBinder, ToFromJavaNullptr) {
569     EXPECT_EQ(nullptr, AIBinder_toJavaBinder(nullptr, nullptr));
570     EXPECT_EQ(nullptr, AIBinder_fromJavaBinder(nullptr, nullptr));
571 }
572 
TEST(NdkBinder,ABpBinderRefCount)573 TEST(NdkBinder, ABpBinderRefCount) {
574     AIBinder* binder = AServiceManager_getService(kExistingNonNdkService);
575     AIBinder_Weak* wBinder = AIBinder_Weak_new(binder);
576 
577     ASSERT_NE(nullptr, binder);
578     EXPECT_EQ(1, AIBinder_debugGetRefCount(binder));
579 
580     AIBinder_decStrong(binder);
581 
582     ASSERT_EQ(nullptr, AIBinder_Weak_promote(wBinder));
583 
584     AIBinder_Weak_delete(wBinder);
585 }
586 
TEST(NdkBinder,AddServiceMultipleTimes)587 TEST(NdkBinder, AddServiceMultipleTimes) {
588     static const char* kInstanceName1 = "test-multi-1";
589     static const char* kInstanceName2 = "test-multi-2";
590     sp<IFoo> foo = new MyTestFoo;
591     EXPECT_EQ(EX_NONE, foo->addService(kInstanceName1));
592     EXPECT_EQ(EX_NONE, foo->addService(kInstanceName2));
593     EXPECT_EQ(IFoo::getService(kInstanceName1), IFoo::getService(kInstanceName2));
594 }
595 
TEST(NdkBinder,RequestedSidWorks)596 TEST(NdkBinder, RequestedSidWorks) {
597     ndk::SpAIBinder binder(AServiceManager_getService(kBinderNdkUnitTestService));
598     std::shared_ptr<aidl::IBinderNdkUnitTest> service =
599             aidl::IBinderNdkUnitTest::fromBinder(binder);
600 
601     bool gotSid = false;
602     EXPECT_TRUE(service->getsRequestedSid(&gotSid).isOk());
603     EXPECT_TRUE(gotSid);
604 }
605 
TEST(NdkBinder,SentAidlBinderCanBeDestroyed)606 TEST(NdkBinder, SentAidlBinderCanBeDestroyed) {
607     static volatile bool destroyed = false;
608     static std::mutex dMutex;
609     static std::condition_variable cv;
610 
611     class MyEmpty : public aidl::BnEmpty {
612         virtual ~MyEmpty() {
613             destroyed = true;
614             cv.notify_one();
615         }
616     };
617 
618     std::shared_ptr<MyEmpty> empty = ndk::SharedRefBase::make<MyEmpty>();
619 
620     ndk::SpAIBinder binder(AServiceManager_getService(kBinderNdkUnitTestService));
621     std::shared_ptr<aidl::IBinderNdkUnitTest> service =
622             aidl::IBinderNdkUnitTest::fromBinder(binder);
623 
624     EXPECT_FALSE(destroyed);
625 
626     service->takeInterface(empty);
627     service->forceFlushCommands();
628     empty = nullptr;
629 
630     // give other binder thread time to process commands
631     {
632         using namespace std::chrono_literals;
633         std::unique_lock<std::mutex> lk(dMutex);
634         cv.wait_for(lk, 1s, [] { return destroyed; });
635     }
636 
637     EXPECT_TRUE(destroyed);
638 }
639 
TEST(NdkBinder,ConvertToPlatformBinder)640 TEST(NdkBinder, ConvertToPlatformBinder) {
641     for (const ndk::SpAIBinder& binder :
642          {// remote
643           ndk::SpAIBinder(AServiceManager_getService(kBinderNdkUnitTestService)),
644           // local
645           ndk::SharedRefBase::make<MyBinderNdkUnitTest>()->asBinder()}) {
646         // convert to platform binder
647         EXPECT_NE(binder.get(), nullptr);
648         sp<IBinder> platformBinder = AIBinder_toPlatformBinder(binder.get());
649         EXPECT_NE(platformBinder.get(), nullptr);
650         auto proxy = interface_cast<IBinderNdkUnitTest>(platformBinder);
651         EXPECT_NE(proxy, nullptr);
652 
653         // use platform binder
654         int out;
655         EXPECT_TRUE(proxy->repeatInt(4, &out).isOk());
656         EXPECT_EQ(out, 4);
657 
658         // convert back
659         ndk::SpAIBinder backBinder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(platformBinder));
660         EXPECT_EQ(backBinder.get(), binder.get());
661     }
662 }
663 
664 class MyResultReceiver : public BnResultReceiver {
665    public:
666     Mutex mMutex;
667     Condition mCondition;
668     bool mHaveResult = false;
669     int32_t mResult = 0;
670 
send(int32_t resultCode)671     virtual void send(int32_t resultCode) {
672         AutoMutex _l(mMutex);
673         mResult = resultCode;
674         mHaveResult = true;
675         mCondition.signal();
676     }
677 
waitForResult()678     int32_t waitForResult() {
679         AutoMutex _l(mMutex);
680         while (!mHaveResult) {
681             mCondition.wait(mMutex);
682         }
683         return mResult;
684     }
685 };
686 
687 class MyShellCallback : public BnShellCallback {
688    public:
openFile(const String16 &,const String16 &,const String16 &)689     virtual int openFile(const String16& /*path*/, const String16& /*seLinuxContext*/,
690                          const String16& /*mode*/) {
691         // Empty implementation.
692         return 0;
693     }
694 };
695 
ReadFdToString(int fd,std::string * content)696 bool ReadFdToString(int fd, std::string* content) {
697     char buf[64];
698     ssize_t n;
699     while ((n = TEMP_FAILURE_RETRY(read(fd, &buf[0], sizeof(buf)))) > 0) {
700         content->append(buf, n);
701     }
702     return (n == 0) ? true : false;
703 }
704 
shellCmdToString(sp<IBinder> unitTestService,const std::vector<const char * > & args)705 std::string shellCmdToString(sp<IBinder> unitTestService, const std::vector<const char*>& args) {
706     int inFd[2] = {-1, -1};
707     int outFd[2] = {-1, -1};
708     int errFd[2] = {-1, -1};
709 
710     EXPECT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, inFd));
711     EXPECT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, outFd));
712     EXPECT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, errFd));
713 
714     sp<MyShellCallback> cb = new MyShellCallback();
715     sp<MyResultReceiver> resultReceiver = new MyResultReceiver();
716 
717     Vector<String16> argsVec;
718     for (int i = 0; i < args.size(); i++) {
719         argsVec.add(String16(args[i]));
720     }
721     status_t error = IBinder::shellCommand(unitTestService, inFd[0], outFd[0], errFd[0], argsVec,
722                                            cb, resultReceiver);
723     EXPECT_EQ(error, android::OK);
724 
725     status_t res = resultReceiver->waitForResult();
726     EXPECT_EQ(res, android::OK);
727 
728     close(inFd[0]);
729     close(inFd[1]);
730     close(outFd[0]);
731     close(errFd[0]);
732     close(errFd[1]);
733 
734     std::string ret;
735     EXPECT_TRUE(ReadFdToString(outFd[1], &ret));
736     close(outFd[1]);
737     return ret;
738 }
739 
TEST(NdkBinder,UseHandleShellCommand)740 TEST(NdkBinder, UseHandleShellCommand) {
741     static const sp<android::IServiceManager> sm(android::defaultServiceManager());
742     sp<IBinder> testService = sm->getService(String16(kBinderNdkUnitTestService));
743 
744     EXPECT_EQ("", shellCmdToString(testService, {}));
745     EXPECT_EQ("", shellCmdToString(testService, {"", ""}));
746     EXPECT_EQ("Hello world!", shellCmdToString(testService, {"Hello ", "world!"}));
747     EXPECT_EQ("CMD", shellCmdToString(testService, {"C", "M", "D"}));
748 }
749 
TEST(NdkBinder,GetClassInterfaceDescriptor)750 TEST(NdkBinder, GetClassInterfaceDescriptor) {
751     ASSERT_STREQ(IFoo::kIFooDescriptor, AIBinder_Class_getDescriptor(IFoo::kClass));
752 }
753 
addOne(int * to)754 static void addOne(int* to) {
755     if (!to) return;
756     ++(*to);
757 }
758 struct FakeResource : public ndk::impl::ScopedAResource<int*, addOne, nullptr> {
FakeResourceFakeResource759     explicit FakeResource(int* a) : ScopedAResource(a) {}
760 };
761 
TEST(NdkBinder_ScopedAResource,GetDelete)762 TEST(NdkBinder_ScopedAResource, GetDelete) {
763     int deleteCount = 0;
764     { FakeResource resource(&deleteCount); }
765     EXPECT_EQ(deleteCount, 1);
766 }
767 
TEST(NdkBinder_ScopedAResource,Release)768 TEST(NdkBinder_ScopedAResource, Release) {
769     int deleteCount = 0;
770     {
771         FakeResource resource(&deleteCount);
772         (void)resource.release();
773     }
774     EXPECT_EQ(deleteCount, 0);
775 }
776 
main(int argc,char * argv[])777 int main(int argc, char* argv[]) {
778     ::testing::InitGoogleTest(&argc, argv);
779 
780     if (fork() == 0) {
781         prctl(PR_SET_PDEATHSIG, SIGHUP);
782         return manualThreadPoolService(IFoo::kInstanceNameToDieFor);
783     }
784     if (fork() == 0) {
785         prctl(PR_SET_PDEATHSIG, SIGHUP);
786         return manualPollingService(IFoo::kSomeInstanceName);
787     }
788     if (fork() == 0) {
789         prctl(PR_SET_PDEATHSIG, SIGHUP);
790         return lazyService(kLazyBinderNdkUnitTestService);
791     }
792     if (fork() == 0) {
793         prctl(PR_SET_PDEATHSIG, SIGHUP);
794         return lazyService(kForcePersistNdkUnitTestService);
795     }
796     if (fork() == 0) {
797         prctl(PR_SET_PDEATHSIG, SIGHUP);
798         return lazyService(kActiveServicesNdkUnitTestService);
799     }
800     if (fork() == 0) {
801         prctl(PR_SET_PDEATHSIG, SIGHUP);
802         return generatedService();
803     }
804 
805     ABinderProcess_setThreadPoolMaxThreadCount(1);  // to receive death notifications/callbacks
806     ABinderProcess_startThreadPool();
807 
808     return RUN_ALL_TESTS();
809 }
810 
811 #include <android/binder_auto_utils.h>
812 #include <android/binder_interface_utils.h>
813 #include <android/binder_parcel_utils.h>
814