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