diff options
| author | Elliott Hughes <enh@google.com> | 2016-09-16 09:02:47 -0700 |
|---|---|---|
| committer | Elliott Hughes <enh@google.com> | 2016-09-16 09:02:47 -0700 |
| commit | 40104501faba8b46a5ed465863c426258d28671d (patch) | |
| tree | 84ac880a777845acc1204a260d8f5d2b4e9b051e /libbinderwrapper | |
| parent | 67855d8454644ba84931b17013c040154a86fbc5 (diff) | |
| download | system_core-40104501faba8b46a5ed465863c426258d28671d.tar.gz system_core-40104501faba8b46a5ed465863c426258d28671d.tar.bz2 system_core-40104501faba8b46a5ed465863c426258d28671d.zip | |
Move libbinderwrapper's headers into libbinderwrapper.
It was probably the LOCAL_EXPORT_C_INCLUDE_DIRS that made me miss this on the
original code review...
Change-Id: I55bc0b0698604338cd206bf5f336798929b6d8f9
Diffstat (limited to 'libbinderwrapper')
| -rw-r--r-- | libbinderwrapper/Android.mk | 4 | ||||
| -rw-r--r-- | libbinderwrapper/include/binderwrapper/binder_test_base.h | 45 | ||||
| -rw-r--r-- | libbinderwrapper/include/binderwrapper/binder_wrapper.h | 88 | ||||
| -rw-r--r-- | libbinderwrapper/include/binderwrapper/stub_binder_wrapper.h | 133 |
4 files changed, 268 insertions, 2 deletions
diff --git a/libbinderwrapper/Android.mk b/libbinderwrapper/Android.mk index 23c2246da..b38d262ae 100644 --- a/libbinderwrapper/Android.mk +++ b/libbinderwrapper/Android.mk @@ -18,8 +18,8 @@ LOCAL_PATH := $(call my-dir) binderwrapperCommonCFlags := -Wall -Werror -Wno-unused-parameter binderwrapperCommonCFlags += -Wno-sign-promo # for libchrome -binderwrapperCommonExportCIncludeDirs := $(LOCAL_PATH)/../include -binderwrapperCommonCIncludes := $(LOCAL_PATH)/../include +binderwrapperCommonExportCIncludeDirs := $(LOCAL_PATH)/include +binderwrapperCommonCIncludes := $(LOCAL_PATH)/include binderwrapperCommonSharedLibraries := \ libbinder \ libchrome \ diff --git a/libbinderwrapper/include/binderwrapper/binder_test_base.h b/libbinderwrapper/include/binderwrapper/binder_test_base.h new file mode 100644 index 000000000..06543de6f --- /dev/null +++ b/libbinderwrapper/include/binderwrapper/binder_test_base.h @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SYSTEM_CORE_INCLUDE_BINDERWRAPPER_BINDER_TEST_BASE_H_ +#define SYSTEM_CORE_INCLUDE_BINDERWRAPPER_BINDER_TEST_BASE_H_ + +#include <base/macros.h> +#include <gtest/gtest.h> + +namespace android { + +class StubBinderWrapper; + +// Class that can be inherited from (or aliased via typedef/using) when writing +// tests that use StubBinderManager. +class BinderTestBase : public ::testing::Test { + public: + BinderTestBase(); + ~BinderTestBase() override; + + StubBinderWrapper* binder_wrapper() { return binder_wrapper_; } + + protected: + StubBinderWrapper* binder_wrapper_; // Not owned. + + private: + DISALLOW_COPY_AND_ASSIGN(BinderTestBase); +}; + +} // namespace android + +#endif // SYSTEM_CORE_INCLUDE_BINDERWRAPPER_BINDER_TEST_BASE_H_ diff --git a/libbinderwrapper/include/binderwrapper/binder_wrapper.h b/libbinderwrapper/include/binderwrapper/binder_wrapper.h new file mode 100644 index 000000000..a104bff32 --- /dev/null +++ b/libbinderwrapper/include/binderwrapper/binder_wrapper.h @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SYSTEM_CORE_INCLUDE_BINDERWRAPPER_BINDER_WRAPPER_H_ +#define SYSTEM_CORE_INCLUDE_BINDERWRAPPER_BINDER_WRAPPER_H_ + +#include <sys/types.h> + +#include <string> + +#include <base/callback.h> +#include <utils/StrongPointer.h> + +namespace android { + +class BBinder; +class IBinder; + +// Wraps libbinder to make it testable. +// NOTE: Static methods of this class are not thread-safe. +class BinderWrapper { + public: + virtual ~BinderWrapper() {} + + // Creates and initializes the singleton (using a wrapper that communicates + // with the real binder system). + static void Create(); + + // Initializes |wrapper| as the singleton, taking ownership of it. Tests that + // want to inject their own wrappers should call this instead of Create(). + static void InitForTesting(BinderWrapper* wrapper); + + // Destroys the singleton. Must be called before calling Create() or + // InitForTesting() a second time. + static void Destroy(); + + // Returns the singleton instance previously created by Create() or set by + // InitForTesting(). + static BinderWrapper* Get(); + + // Returns the singleton instance if it was previously created by Create() or + // set by InitForTesting(), or creates a new one by calling Create(). + static BinderWrapper* GetOrCreateInstance(); + + // Gets the binder for communicating with the service identified by + // |service_name|, returning null immediately if it doesn't exist. + virtual sp<IBinder> GetService(const std::string& service_name) = 0; + + // Registers |binder| as |service_name| with the service manager. + virtual bool RegisterService(const std::string& service_name, + const sp<IBinder>& binder) = 0; + + // Creates a local binder object. + virtual sp<BBinder> CreateLocalBinder() = 0; + + // Registers |callback| to be invoked when |binder| dies. If another callback + // is currently registered for |binder|, it will be replaced. + virtual bool RegisterForDeathNotifications( + const sp<IBinder>& binder, + const ::base::Closure& callback) = 0; + + // Unregisters the callback, if any, for |binder|. + virtual bool UnregisterForDeathNotifications(const sp<IBinder>& binder) = 0; + + // When called while in a transaction, returns the caller's UID or PID. + virtual uid_t GetCallingUid() = 0; + virtual pid_t GetCallingPid() = 0; + + private: + static BinderWrapper* instance_; +}; + +} // namespace android + +#endif // SYSTEM_CORE_INCLUDE_BINDERWRAPPER_BINDER_WRAPPER_H_ diff --git a/libbinderwrapper/include/binderwrapper/stub_binder_wrapper.h b/libbinderwrapper/include/binderwrapper/stub_binder_wrapper.h new file mode 100644 index 000000000..9d4578eb3 --- /dev/null +++ b/libbinderwrapper/include/binderwrapper/stub_binder_wrapper.h @@ -0,0 +1,133 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SYSTEM_CORE_INCLUDE_BINDERWRAPPER_STUB_BINDER_WRAPPER_H_ +#define SYSTEM_CORE_INCLUDE_BINDERWRAPPER_STUB_BINDER_WRAPPER_H_ + +#include <map> +#include <string> +#include <vector> + +#include <base/macros.h> +#include <binder/Binder.h> +#include <binder/IBinder.h> +#include <binderwrapper/binder_wrapper.h> + +namespace android { + +// Stub implementation of BinderWrapper for testing. +// +// Example usage: +// +// First, assuming a base IFoo binder interface, create a stub class that +// derives from BnFoo to implement the receiver side of the communication: +// +// class StubFoo : public BnFoo { +// public: +// ... +// status_t doSomething(int arg) override { +// // e.g. save passed-in value for later inspection by tests. +// return OK; +// } +// }; +// +// Next, from your test code, inject a StubBinderManager either directly or by +// inheriting from the BinderTestBase class: +// +// StubBinderWrapper* wrapper = new StubBinderWrapper(); +// BinderWrapper::InitForTesting(wrapper); // Takes ownership. +// +// Also from your test, create a StubFoo and register it with the wrapper: +// +// StubFoo* foo = new StubFoo(); +// sp<IBinder> binder(foo); +// wrapper->SetBinderForService("foo", binder); +// +// The code being tested can now use the wrapper to get the stub and call it: +// +// sp<IBinder> binder = BinderWrapper::Get()->GetService("foo"); +// CHECK(binder.get()); +// sp<IFoo> foo = interface_cast<IFoo>(binder); +// CHECK_EQ(foo->doSomething(3), OK); +// +// To create a local BBinder object, production code can call +// CreateLocalBinder(). Then, a test can get the BBinder's address via +// local_binders() to check that they're passed as expected in binder calls. +// +class StubBinderWrapper : public BinderWrapper { + public: + StubBinderWrapper(); + ~StubBinderWrapper() override; + + const std::vector<sp<BBinder>>& local_binders() const { + return local_binders_; + } + void clear_local_binders() { local_binders_.clear(); } + + void set_calling_uid(uid_t uid) { calling_uid_ = uid; } + void set_calling_pid(pid_t pid) { calling_pid_ = pid; } + + // Sets the binder to return when |service_name| is passed to GetService() or + // WaitForService(). + void SetBinderForService(const std::string& service_name, + const sp<IBinder>& binder); + + // Returns the binder previously registered for |service_name| via + // RegisterService(), or null if the service hasn't been registered. + sp<IBinder> GetRegisteredService(const std::string& service_name) const; + + // Run the calback in |death_callbacks_| corresponding to |binder|. + void NotifyAboutBinderDeath(const sp<IBinder>& binder); + + // BinderWrapper: + sp<IBinder> GetService(const std::string& service_name) override; + bool RegisterService(const std::string& service_name, + const sp<IBinder>& binder) override; + sp<BBinder> CreateLocalBinder() override; + bool RegisterForDeathNotifications(const sp<IBinder>& binder, + const ::base::Closure& callback) override; + bool UnregisterForDeathNotifications(const sp<IBinder>& binder) override; + uid_t GetCallingUid() override; + pid_t GetCallingPid() override; + + private: + using ServiceMap = std::map<std::string, sp<IBinder>>; + + // Map from service name to associated binder handle. Used by GetService() and + // WaitForService(). + ServiceMap services_to_return_; + + // Map from service name to associated binder handle. Updated by + // RegisterService(). + ServiceMap registered_services_; + + // Local binders returned by CreateLocalBinder(). + std::vector<sp<BBinder>> local_binders_; + + // Map from binder handle to the callback that should be invoked on binder + // death. + std::map<sp<IBinder>, ::base::Closure> death_callbacks_; + + // Values to return from GetCallingUid() and GetCallingPid(); + uid_t calling_uid_; + pid_t calling_pid_; + + DISALLOW_COPY_AND_ASSIGN(StubBinderWrapper); +}; + +} // namespace android + +#endif // SYSTEM_CORE_INCLUDE_BINDERWRAPPER_STUB_BINDER_WRAPPER_H_ |
