diff --git a/libbinderwrapper/Android.bp b/libbinderwrapper/Android.bp deleted file mode 100644 index 87c55faad..000000000 --- a/libbinderwrapper/Android.bp +++ /dev/null @@ -1,63 +0,0 @@ -// -// 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. -// - -package { - default_applicable_licenses: ["Android-Apache-2.0"], -} - -cc_defaults { - name: "libbinderwrapper_defaults", - - cflags: [ - "-Wall", - "-Werror", - "-Wno-unused-parameter", - - ], - export_include_dirs: ["include"], - shared_libs: [ - "libbinder", - "libutils", - ], -} - -// libbinderwrapper shared library -// ======================================================== -cc_library_shared { - name: "libbinderwrapper", - defaults: ["libbinderwrapper_defaults"], - vendor_available: true, - - srcs: [ - "binder_wrapper.cc", - "real_binder_wrapper.cc", - ], -} - -// libbinderwrapper_test_support static library -// ======================================================== -cc_library_static { - name: "libbinderwrapper_test_support", - defaults: ["libbinderwrapper_defaults"], - - static_libs: ["libgtest"], - shared_libs: ["libbinderwrapper"], - - srcs: [ - "binder_test_base.cc", - "stub_binder_wrapper.cc", - ], -} diff --git a/libbinderwrapper/binder_test_base.cc b/libbinderwrapper/binder_test_base.cc deleted file mode 100644 index af93a04d9..000000000 --- a/libbinderwrapper/binder_test_base.cc +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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. - */ - -#include - -#include -#include - -namespace android { - -BinderTestBase::BinderTestBase() : binder_wrapper_(new StubBinderWrapper()) { - // Pass ownership. - BinderWrapper::InitForTesting(binder_wrapper_); -} - -BinderTestBase::~BinderTestBase() { - BinderWrapper::Destroy(); -} - -} // namespace android diff --git a/libbinderwrapper/binder_wrapper.cc b/libbinderwrapper/binder_wrapper.cc deleted file mode 100644 index f5e7476f1..000000000 --- a/libbinderwrapper/binder_wrapper.cc +++ /dev/null @@ -1,60 +0,0 @@ -/* - * 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. - */ - -#include - -#include - -#include "real_binder_wrapper.h" - -namespace android { - -// Singleton instance. -BinderWrapper* BinderWrapper::instance_ = nullptr; - -// static -void BinderWrapper::Create() { - CHECK(!instance_) << "Already initialized; missing call to Destroy()?"; - instance_ = new RealBinderWrapper(); -} - -// static -void BinderWrapper::InitForTesting(BinderWrapper* wrapper) { - CHECK(!instance_) << "Already initialized; missing call to Destroy()?"; - instance_ = wrapper; -} - -// static -void BinderWrapper::Destroy() { - CHECK(instance_) << "Not initialized; missing call to Create()?"; - delete instance_; - instance_ = nullptr; -} - -// static -BinderWrapper* BinderWrapper::Get() { - CHECK(instance_) << "Not initialized; missing call to Create()?"; - return instance_; -} - -// static -BinderWrapper* BinderWrapper::GetOrCreateInstance() { - if (!instance_) - instance_ = new RealBinderWrapper(); - return instance_; -} - -} // namespace android diff --git a/libbinderwrapper/include/binderwrapper/binder_test_base.h b/libbinderwrapper/include/binderwrapper/binder_test_base.h deleted file mode 100644 index 46eca3f15..000000000 --- a/libbinderwrapper/include/binderwrapper/binder_test_base.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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 - -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: - BinderTestBase(const BinderTestBase&) = delete; -}; - -} // 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 deleted file mode 100644 index fc5708b62..000000000 --- a/libbinderwrapper/include/binderwrapper/binder_wrapper.h +++ /dev/null @@ -1,87 +0,0 @@ -/* - * 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 - -#include -#include - -#include - -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 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& binder) = 0; - - // Creates a local binder object. - virtual sp 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& binder, - const std::function&) = 0; - - // Unregisters the callback, if any, for |binder|. - virtual bool UnregisterForDeathNotifications(const sp& 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 deleted file mode 100644 index 2a7f77e7a..000000000 --- a/libbinderwrapper/include/binderwrapper/stub_binder_wrapper.h +++ /dev/null @@ -1,132 +0,0 @@ -/* - * 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 -#include -#include - -#include -#include -#include - -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 binder(foo); -// wrapper->SetBinderForService("foo", binder); -// -// The code being tested can now use the wrapper to get the stub and call it: -// -// sp binder = BinderWrapper::Get()->GetService("foo"); -// CHECK(binder.get()); -// sp foo = interface_cast(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>& 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& binder); - - // Returns the binder previously registered for |service_name| via - // RegisterService(), or null if the service hasn't been registered. - sp GetRegisteredService(const std::string& service_name) const; - - // Run the calback in |death_callbacks_| corresponding to |binder|. - void NotifyAboutBinderDeath(const sp& binder); - - // BinderWrapper: - sp GetService(const std::string& service_name) override; - bool RegisterService(const std::string& service_name, - const sp& binder) override; - sp CreateLocalBinder() override; - bool RegisterForDeathNotifications(const sp& binder, - const std::function& callback) override; - bool UnregisterForDeathNotifications(const sp& binder) override; - uid_t GetCallingUid() override; - pid_t GetCallingPid() override; - - private: - using ServiceMap = std::map>; - - // 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> local_binders_; - - // Map from binder handle to the callback that should be invoked on binder - // death. - std::map, std::function> death_callbacks_; - - // Values to return from GetCallingUid() and GetCallingPid(); - uid_t calling_uid_; - pid_t calling_pid_; - - StubBinderWrapper(const StubBinderWrapper&) = delete; -}; - -} // namespace android - -#endif // SYSTEM_CORE_INCLUDE_BINDERWRAPPER_STUB_BINDER_WRAPPER_H_ diff --git a/libbinderwrapper/real_binder_wrapper.cc b/libbinderwrapper/real_binder_wrapper.cc deleted file mode 100644 index d214ce31c..000000000 --- a/libbinderwrapper/real_binder_wrapper.cc +++ /dev/null @@ -1,121 +0,0 @@ -/* - * 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. - */ - -#include "real_binder_wrapper.h" - -#include - -#include -#include -#include -#include - -namespace android { - -// Class that handles binder death notifications. libbinder wants the recipient -// to be wrapped in sp<>, so registering RealBinderWrapper as a recipient would -// be awkward. -class RealBinderWrapper::DeathRecipient : public IBinder::DeathRecipient { - public: - explicit DeathRecipient(const std::function& callback) - : callback_(std::move(callback)) {} - ~DeathRecipient() = default; - - // IBinder::DeathRecipient: - void binderDied(const wp& who) override { callback_(); } - - private: - // Callback to run in response to binder death. - std::function callback_; - - DISALLOW_COPY_AND_ASSIGN(DeathRecipient); -}; - -RealBinderWrapper::RealBinderWrapper() = default; - -RealBinderWrapper::~RealBinderWrapper() = default; - -sp RealBinderWrapper::GetService(const std::string& service_name) { - sp service_manager = defaultServiceManager(); - if (!service_manager.get()) { - LOG(ERROR) << "Unable to get service manager"; - return sp(); - } - sp binder = - service_manager->checkService(String16(service_name.c_str())); - if (!binder.get()) - LOG(ERROR) << "Unable to get \"" << service_name << "\" service"; - return binder; -} - -bool RealBinderWrapper::RegisterService(const std::string& service_name, - const sp& binder) { - sp service_manager = defaultServiceManager(); - if (!service_manager.get()) { - LOG(ERROR) << "Unable to get service manager"; - return false; - } - status_t status = defaultServiceManager()->addService( - String16(service_name.c_str()), binder); - if (status != OK) { - LOG(ERROR) << "Failed to register \"" << service_name << "\" with service " - << "manager"; - return false; - } - return true; -} - -sp RealBinderWrapper::CreateLocalBinder() { - return sp(new BBinder()); -} - -bool RealBinderWrapper::RegisterForDeathNotifications(const sp& binder, - const std::function& callback) { - sp recipient(new DeathRecipient(callback)); - if (binder->linkToDeath(recipient) != OK) { - LOG(ERROR) << "Failed to register for death notifications on " - << binder.get(); - return false; - } - death_recipients_[binder] = recipient; - return true; -} - -bool RealBinderWrapper::UnregisterForDeathNotifications( - const sp& binder) { - auto it = death_recipients_.find(binder); - if (it == death_recipients_.end()) { - LOG(ERROR) << "Not registered for death notifications on " << binder.get(); - return false; - } - if (binder->unlinkToDeath(it->second) != OK) { - LOG(ERROR) << "Failed to unregister for death notifications on " - << binder.get(); - return false; - } - death_recipients_.erase(it); - return true; -} - -uid_t RealBinderWrapper::GetCallingUid() { - return IPCThreadState::self()->getCallingUid(); -} - -pid_t RealBinderWrapper::GetCallingPid() { - return IPCThreadState::self()->getCallingPid(); -} - -} // namespace android diff --git a/libbinderwrapper/real_binder_wrapper.h b/libbinderwrapper/real_binder_wrapper.h deleted file mode 100644 index d0468f079..000000000 --- a/libbinderwrapper/real_binder_wrapper.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - * 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_LIBBINDERWRAPPER_REAL_BINDER_WRAPPER_H_ -#define SYSTEM_CORE_LIBBINDERWRAPPER_REAL_BINDER_WRAPPER_H_ - -#include - -#include - -namespace android { - -class IBinder; - -// Real implementation of BinderWrapper. -class RealBinderWrapper : public BinderWrapper { - public: - RealBinderWrapper(); - ~RealBinderWrapper() override; - - // BinderWrapper: - sp GetService(const std::string& service_name) override; - bool RegisterService(const std::string& service_name, - const sp& binder) override; - sp CreateLocalBinder() override; - bool RegisterForDeathNotifications(const sp& binder, - const std::function& callback) override; - bool UnregisterForDeathNotifications(const sp& binder) override; - uid_t GetCallingUid() override; - pid_t GetCallingPid() override; - - private: - class DeathRecipient; - - // Map from binder handle to object that should be notified of the binder's - // death. - std::map, sp> death_recipients_; - - RealBinderWrapper(const RealBinderWrapper&) = delete; -}; - -} // namespace android - -#endif // SYSTEM_CORE_LIBBINDER_WRAPPER_REAL_BINDER_WRAPPER_H_ diff --git a/libbinderwrapper/stub_binder_wrapper.cc b/libbinderwrapper/stub_binder_wrapper.cc deleted file mode 100644 index fabf1228a..000000000 --- a/libbinderwrapper/stub_binder_wrapper.cc +++ /dev/null @@ -1,85 +0,0 @@ -/* - * 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. - */ - -#include - -#include - -#include -#include - -namespace android { - -StubBinderWrapper::StubBinderWrapper() - : calling_uid_(-1), - calling_pid_(-1) {} - -StubBinderWrapper::~StubBinderWrapper() = default; - -void StubBinderWrapper::SetBinderForService(const std::string& service_name, - const sp& binder) { - services_to_return_[service_name] = binder; -} - -sp StubBinderWrapper::GetRegisteredService( - const std::string& service_name) const { - const auto it = registered_services_.find(service_name); - return it != registered_services_.end() ? it->second : sp(); -} - -void StubBinderWrapper::NotifyAboutBinderDeath(const sp& binder) { - const auto it = death_callbacks_.find(binder); - if (it != death_callbacks_.end()) it->second(); -} - -sp StubBinderWrapper::GetService(const std::string& service_name) { - const auto it = services_to_return_.find(service_name); - return it != services_to_return_.end() ? it->second : sp(); -} - -bool StubBinderWrapper::RegisterService(const std::string& service_name, - const sp& binder) { - registered_services_[service_name] = binder; - return true; -} - -sp StubBinderWrapper::CreateLocalBinder() { - sp binder(new BBinder()); - local_binders_.push_back(binder); - return binder; -} - -bool StubBinderWrapper::RegisterForDeathNotifications(const sp& binder, - const std::function& callback) { - death_callbacks_[binder] = callback; - return true; -} - -bool StubBinderWrapper::UnregisterForDeathNotifications( - const sp& binder) { - death_callbacks_.erase(binder); - return true; -} - -uid_t StubBinderWrapper::GetCallingUid() { - return calling_uid_; -} - -pid_t StubBinderWrapper::GetCallingPid() { - return calling_pid_; -} - -} // namespace android