Loading libs/fakeservicemanager/Android.bp +39 −0 Original line number Diff line number Diff line Loading @@ -17,6 +17,7 @@ cc_defaults { shared_libs: [ "libbinder", "libutils", "liblog", ], target: { darwin: { Loading @@ -40,3 +41,41 @@ cc_test_host { static_libs: ["libgmock"], local_include_dirs: ["include"], } rust_bindgen { name: "libfakeservicemanager_bindgen", crate_name: "fakeservicemanager_bindgen", host_supported: true, wrapper_src: "rust/wrappers/FakeServiceManagerWrapper.hpp", source_stem: "bindings", visibility: [":__subpackages__"], bindgen_flags: [ "--allowlist-function", "setupFakeServiceManager", "--allowlist-function", "clearFakeServiceManager", ], shared_libs: [ "libc++", "libbinder", "libfakeservicemanager", ], } rust_library { name: "libfakeservicemanager_rs", crate_name: "fakeservicemanager_rs", host_supported: true, srcs: [ "rust/src/lib.rs", ], shared_libs: [ "libc++", "libfakeservicemanager", ], rustlibs: [ "libfakeservicemanager_bindgen", ], lints: "none", clippy_lints: "none", } libs/fakeservicemanager/FakeServiceManager.cpp +25 −0 Original line number Diff line number Diff line Loading @@ -16,6 +16,10 @@ #include "fakeservicemanager/FakeServiceManager.h" using android::sp; using android::FakeServiceManager; using android::setDefaultServiceManager; namespace android { FakeServiceManager::FakeServiceManager() {} Loading Loading @@ -123,3 +127,24 @@ void FakeServiceManager::clear() { mNameToService.clear(); } } // namespace android [[clang::no_destroy]] static sp<FakeServiceManager> gFakeServiceManager; [[clang::no_destroy]] static std::once_flag gSmOnce; extern "C" { // Setup FakeServiceManager to mock dependencies in test using this API for rust backend void setupFakeServiceManager() { /* Create a FakeServiceManager instance and add required services */ std::call_once(gSmOnce, [&]() { gFakeServiceManager = new FakeServiceManager(); android::setDefaultServiceManager(gFakeServiceManager); }); } // Clear existing services from Fake SM for rust backend void clearFakeServiceManager() { LOG_ALWAYS_FATAL_IF(gFakeServiceManager == nullptr, "Fake Service Manager is not available. Forgot to call setupFakeServiceManager?"); gFakeServiceManager->clear(); } } //extern "C" No newline at end of file libs/fakeservicemanager/rust/src/lib.rs 0 → 100644 +34 −0 Original line number Diff line number Diff line /* * Copyright (C) 2023 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. */ use fakeservicemanager_bindgen::{clearFakeServiceManager, setupFakeServiceManager}; // Setup FakeServiceManager for testing and fuzzing purposes pub fn setup_fake_service_manager() { unsafe { // Safety: This API creates a new FakeSm object which will be always valid and sets up // defaultServiceManager setupFakeServiceManager(); } } // Setup FakeServiceManager for testing and fuzzing purposes pub fn clear_fake_service_manager() { unsafe { // Safety: This API clears all registered services with Fake SM. This should be only used // setupFakeServiceManager is already called. clearFakeServiceManager(); } } libs/fakeservicemanager/rust/wrappers/FakeServiceManagerWrapper.hpp 0 → 100644 +25 −0 Original line number Diff line number Diff line /* * Copyright (C) 2023 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 "fakeservicemanager/FakeServiceManager.h" extern "C" { // Setup FakeServiceManager to mock dependencies in test using this API void setupFakeServiceManager(); // Clear existing services from Fake SM. void clearFakeServiceManager(); } // extern "C" libs/fakeservicemanager/test_sm.cpp +33 −3 Original line number Diff line number Diff line Loading @@ -22,6 +22,7 @@ #include <binder/IServiceManager.h> #include "fakeservicemanager/FakeServiceManager.h" #include "rust/wrappers/FakeServiceManagerWrapper.hpp" using android::sp; using android::BBinder; Loading @@ -31,6 +32,7 @@ using android::status_t; using android::FakeServiceManager; using android::String16; using android::IServiceManager; using android::defaultServiceManager; using testing::ElementsAre; static sp<IBinder> getBinder() { Loading Loading @@ -83,7 +85,7 @@ TEST(GetService, HappyHappy) { EXPECT_EQ(sm->getService(String16("foo")), service); } TEST(GetService, NonExistant) { TEST(GetService, NonExistent) { auto sm = new FakeServiceManager(); EXPECT_EQ(sm->getService(String16("foo")), nullptr); Loading @@ -108,7 +110,7 @@ TEST(ListServices, AllServices) { String16("sd"))); } TEST(WaitForService, NonExistant) { TEST(WaitForService, NonExistent) { auto sm = new FakeServiceManager(); EXPECT_EQ(sm->waitForService(String16("foo")), nullptr); Loading @@ -124,7 +126,7 @@ TEST(WaitForService, HappyHappy) { EXPECT_EQ(sm->waitForService(String16("foo")), service); } TEST(IsDeclared, NonExistant) { TEST(IsDeclared, NonExistent) { auto sm = new FakeServiceManager(); EXPECT_FALSE(sm->isDeclared(String16("foo"))); Loading @@ -139,3 +141,31 @@ TEST(IsDeclared, HappyHappy) { EXPECT_TRUE(sm->isDeclared(String16("foo"))); } TEST(SetupFakeServiceManager, NonExistent) { setupFakeServiceManager(); EXPECT_EQ(defaultServiceManager()->getService(String16("foo")), nullptr); } TEST(SetupFakeServiceManager, GetExistingService) { setupFakeServiceManager(); sp<IBinder> service = getBinder(); EXPECT_EQ(defaultServiceManager()->addService(String16("foo"), service, false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT), OK); EXPECT_EQ(defaultServiceManager()->getService(String16("foo")), service); clearFakeServiceManager(); } TEST(ClearFakeServiceManager, GetServiceAfterClear) { setupFakeServiceManager(); sp<IBinder> service = getBinder(); EXPECT_EQ(defaultServiceManager()->addService(String16("foo"), service, false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT), OK); clearFakeServiceManager(); EXPECT_EQ(defaultServiceManager()->getService(String16("foo")), nullptr); } No newline at end of file Loading
libs/fakeservicemanager/Android.bp +39 −0 Original line number Diff line number Diff line Loading @@ -17,6 +17,7 @@ cc_defaults { shared_libs: [ "libbinder", "libutils", "liblog", ], target: { darwin: { Loading @@ -40,3 +41,41 @@ cc_test_host { static_libs: ["libgmock"], local_include_dirs: ["include"], } rust_bindgen { name: "libfakeservicemanager_bindgen", crate_name: "fakeservicemanager_bindgen", host_supported: true, wrapper_src: "rust/wrappers/FakeServiceManagerWrapper.hpp", source_stem: "bindings", visibility: [":__subpackages__"], bindgen_flags: [ "--allowlist-function", "setupFakeServiceManager", "--allowlist-function", "clearFakeServiceManager", ], shared_libs: [ "libc++", "libbinder", "libfakeservicemanager", ], } rust_library { name: "libfakeservicemanager_rs", crate_name: "fakeservicemanager_rs", host_supported: true, srcs: [ "rust/src/lib.rs", ], shared_libs: [ "libc++", "libfakeservicemanager", ], rustlibs: [ "libfakeservicemanager_bindgen", ], lints: "none", clippy_lints: "none", }
libs/fakeservicemanager/FakeServiceManager.cpp +25 −0 Original line number Diff line number Diff line Loading @@ -16,6 +16,10 @@ #include "fakeservicemanager/FakeServiceManager.h" using android::sp; using android::FakeServiceManager; using android::setDefaultServiceManager; namespace android { FakeServiceManager::FakeServiceManager() {} Loading Loading @@ -123,3 +127,24 @@ void FakeServiceManager::clear() { mNameToService.clear(); } } // namespace android [[clang::no_destroy]] static sp<FakeServiceManager> gFakeServiceManager; [[clang::no_destroy]] static std::once_flag gSmOnce; extern "C" { // Setup FakeServiceManager to mock dependencies in test using this API for rust backend void setupFakeServiceManager() { /* Create a FakeServiceManager instance and add required services */ std::call_once(gSmOnce, [&]() { gFakeServiceManager = new FakeServiceManager(); android::setDefaultServiceManager(gFakeServiceManager); }); } // Clear existing services from Fake SM for rust backend void clearFakeServiceManager() { LOG_ALWAYS_FATAL_IF(gFakeServiceManager == nullptr, "Fake Service Manager is not available. Forgot to call setupFakeServiceManager?"); gFakeServiceManager->clear(); } } //extern "C" No newline at end of file
libs/fakeservicemanager/rust/src/lib.rs 0 → 100644 +34 −0 Original line number Diff line number Diff line /* * Copyright (C) 2023 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. */ use fakeservicemanager_bindgen::{clearFakeServiceManager, setupFakeServiceManager}; // Setup FakeServiceManager for testing and fuzzing purposes pub fn setup_fake_service_manager() { unsafe { // Safety: This API creates a new FakeSm object which will be always valid and sets up // defaultServiceManager setupFakeServiceManager(); } } // Setup FakeServiceManager for testing and fuzzing purposes pub fn clear_fake_service_manager() { unsafe { // Safety: This API clears all registered services with Fake SM. This should be only used // setupFakeServiceManager is already called. clearFakeServiceManager(); } }
libs/fakeservicemanager/rust/wrappers/FakeServiceManagerWrapper.hpp 0 → 100644 +25 −0 Original line number Diff line number Diff line /* * Copyright (C) 2023 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 "fakeservicemanager/FakeServiceManager.h" extern "C" { // Setup FakeServiceManager to mock dependencies in test using this API void setupFakeServiceManager(); // Clear existing services from Fake SM. void clearFakeServiceManager(); } // extern "C"
libs/fakeservicemanager/test_sm.cpp +33 −3 Original line number Diff line number Diff line Loading @@ -22,6 +22,7 @@ #include <binder/IServiceManager.h> #include "fakeservicemanager/FakeServiceManager.h" #include "rust/wrappers/FakeServiceManagerWrapper.hpp" using android::sp; using android::BBinder; Loading @@ -31,6 +32,7 @@ using android::status_t; using android::FakeServiceManager; using android::String16; using android::IServiceManager; using android::defaultServiceManager; using testing::ElementsAre; static sp<IBinder> getBinder() { Loading Loading @@ -83,7 +85,7 @@ TEST(GetService, HappyHappy) { EXPECT_EQ(sm->getService(String16("foo")), service); } TEST(GetService, NonExistant) { TEST(GetService, NonExistent) { auto sm = new FakeServiceManager(); EXPECT_EQ(sm->getService(String16("foo")), nullptr); Loading @@ -108,7 +110,7 @@ TEST(ListServices, AllServices) { String16("sd"))); } TEST(WaitForService, NonExistant) { TEST(WaitForService, NonExistent) { auto sm = new FakeServiceManager(); EXPECT_EQ(sm->waitForService(String16("foo")), nullptr); Loading @@ -124,7 +126,7 @@ TEST(WaitForService, HappyHappy) { EXPECT_EQ(sm->waitForService(String16("foo")), service); } TEST(IsDeclared, NonExistant) { TEST(IsDeclared, NonExistent) { auto sm = new FakeServiceManager(); EXPECT_FALSE(sm->isDeclared(String16("foo"))); Loading @@ -139,3 +141,31 @@ TEST(IsDeclared, HappyHappy) { EXPECT_TRUE(sm->isDeclared(String16("foo"))); } TEST(SetupFakeServiceManager, NonExistent) { setupFakeServiceManager(); EXPECT_EQ(defaultServiceManager()->getService(String16("foo")), nullptr); } TEST(SetupFakeServiceManager, GetExistingService) { setupFakeServiceManager(); sp<IBinder> service = getBinder(); EXPECT_EQ(defaultServiceManager()->addService(String16("foo"), service, false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT), OK); EXPECT_EQ(defaultServiceManager()->getService(String16("foo")), service); clearFakeServiceManager(); } TEST(ClearFakeServiceManager, GetServiceAfterClear) { setupFakeServiceManager(); sp<IBinder> service = getBinder(); EXPECT_EQ(defaultServiceManager()->addService(String16("foo"), service, false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT), OK); clearFakeServiceManager(); EXPECT_EQ(defaultServiceManager()->getService(String16("foo")), nullptr); } No newline at end of file