1 /* 2 * Copyright (C) 2015 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 #ifndef SYSTEM_CORE_INCLUDE_BINDERWRAPPER_STUB_BINDER_WRAPPER_H_ 18 #define SYSTEM_CORE_INCLUDE_BINDERWRAPPER_STUB_BINDER_WRAPPER_H_ 19 20 #include <map> 21 #include <string> 22 #include <vector> 23 24 #include <binder/Binder.h> 25 #include <binder/IBinder.h> 26 #include <binderwrapper/binder_wrapper.h> 27 28 namespace android { 29 30 // Stub implementation of BinderWrapper for testing. 31 // 32 // Example usage: 33 // 34 // First, assuming a base IFoo binder interface, create a stub class that 35 // derives from BnFoo to implement the receiver side of the communication: 36 // 37 // class StubFoo : public BnFoo { 38 // public: 39 // ... 40 // status_t doSomething(int arg) override { 41 // // e.g. save passed-in value for later inspection by tests. 42 // return OK; 43 // } 44 // }; 45 // 46 // Next, from your test code, inject a StubBinderManager either directly or by 47 // inheriting from the BinderTestBase class: 48 // 49 // StubBinderWrapper* wrapper = new StubBinderWrapper(); 50 // BinderWrapper::InitForTesting(wrapper); // Takes ownership. 51 // 52 // Also from your test, create a StubFoo and register it with the wrapper: 53 // 54 // StubFoo* foo = new StubFoo(); 55 // sp<IBinder> binder(foo); 56 // wrapper->SetBinderForService("foo", binder); 57 // 58 // The code being tested can now use the wrapper to get the stub and call it: 59 // 60 // sp<IBinder> binder = BinderWrapper::Get()->GetService("foo"); 61 // CHECK(binder.get()); 62 // sp<IFoo> foo = interface_cast<IFoo>(binder); 63 // CHECK_EQ(foo->doSomething(3), OK); 64 // 65 // To create a local BBinder object, production code can call 66 // CreateLocalBinder(). Then, a test can get the BBinder's address via 67 // local_binders() to check that they're passed as expected in binder calls. 68 // 69 class StubBinderWrapper : public BinderWrapper { 70 public: 71 StubBinderWrapper(); 72 ~StubBinderWrapper() override; 73 local_binders()74 const std::vector<sp<BBinder>>& local_binders() const { 75 return local_binders_; 76 } clear_local_binders()77 void clear_local_binders() { local_binders_.clear(); } 78 set_calling_uid(uid_t uid)79 void set_calling_uid(uid_t uid) { calling_uid_ = uid; } set_calling_pid(pid_t pid)80 void set_calling_pid(pid_t pid) { calling_pid_ = pid; } 81 82 // Sets the binder to return when |service_name| is passed to GetService() or 83 // WaitForService(). 84 void SetBinderForService(const std::string& service_name, 85 const sp<IBinder>& binder); 86 87 // Returns the binder previously registered for |service_name| via 88 // RegisterService(), or null if the service hasn't been registered. 89 sp<IBinder> GetRegisteredService(const std::string& service_name) const; 90 91 // Run the calback in |death_callbacks_| corresponding to |binder|. 92 void NotifyAboutBinderDeath(const sp<IBinder>& binder); 93 94 // BinderWrapper: 95 sp<IBinder> GetService(const std::string& service_name) override; 96 bool RegisterService(const std::string& service_name, 97 const sp<IBinder>& binder) override; 98 sp<BBinder> CreateLocalBinder() override; 99 bool RegisterForDeathNotifications(const sp<IBinder>& binder, 100 const std::function<void()>& callback) override; 101 bool UnregisterForDeathNotifications(const sp<IBinder>& binder) override; 102 uid_t GetCallingUid() override; 103 pid_t GetCallingPid() override; 104 105 private: 106 using ServiceMap = std::map<std::string, sp<IBinder>>; 107 108 // Map from service name to associated binder handle. Used by GetService() and 109 // WaitForService(). 110 ServiceMap services_to_return_; 111 112 // Map from service name to associated binder handle. Updated by 113 // RegisterService(). 114 ServiceMap registered_services_; 115 116 // Local binders returned by CreateLocalBinder(). 117 std::vector<sp<BBinder>> local_binders_; 118 119 // Map from binder handle to the callback that should be invoked on binder 120 // death. 121 std::map<sp<IBinder>, std::function<void()>> death_callbacks_; 122 123 // Values to return from GetCallingUid() and GetCallingPid(); 124 uid_t calling_uid_; 125 pid_t calling_pid_; 126 127 StubBinderWrapper(const StubBinderWrapper&) = delete; 128 }; 129 130 } // namespace android 131 132 #endif // SYSTEM_CORE_INCLUDE_BINDERWRAPPER_STUB_BINDER_WRAPPER_H_ 133