diff options
-rw-r--r-- | libs/fakeservicemanager/Android.bp | 39 | ||||
-rw-r--r-- | libs/fakeservicemanager/FakeServiceManager.cpp | 25 | ||||
-rw-r--r-- | libs/fakeservicemanager/rust/src/lib.rs | 34 | ||||
-rw-r--r-- | libs/fakeservicemanager/rust/wrappers/FakeServiceManagerWrapper.hpp | 25 | ||||
-rw-r--r-- | libs/fakeservicemanager/test_sm.cpp | 36 |
5 files changed, 156 insertions, 3 deletions
diff --git a/libs/fakeservicemanager/Android.bp b/libs/fakeservicemanager/Android.bp index 96dcce11ea..38233934c8 100644 --- a/libs/fakeservicemanager/Android.bp +++ b/libs/fakeservicemanager/Android.bp @@ -17,6 +17,7 @@ cc_defaults { shared_libs: [ "libbinder", "libutils", + "liblog", ], target: { darwin: { @@ -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", +} diff --git a/libs/fakeservicemanager/FakeServiceManager.cpp b/libs/fakeservicemanager/FakeServiceManager.cpp index 80661c1cbd..241c866558 100644 --- a/libs/fakeservicemanager/FakeServiceManager.cpp +++ b/libs/fakeservicemanager/FakeServiceManager.cpp @@ -16,6 +16,10 @@ #include "fakeservicemanager/FakeServiceManager.h" +using android::sp; +using android::FakeServiceManager; +using android::setDefaultServiceManager; + namespace android { FakeServiceManager::FakeServiceManager() {} @@ -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 diff --git a/libs/fakeservicemanager/rust/src/lib.rs b/libs/fakeservicemanager/rust/src/lib.rs new file mode 100644 index 0000000000..5b7e756914 --- /dev/null +++ b/libs/fakeservicemanager/rust/src/lib.rs @@ -0,0 +1,34 @@ +/* + * 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(); + } +} diff --git a/libs/fakeservicemanager/rust/wrappers/FakeServiceManagerWrapper.hpp b/libs/fakeservicemanager/rust/wrappers/FakeServiceManagerWrapper.hpp new file mode 100644 index 0000000000..1f5923afba --- /dev/null +++ b/libs/fakeservicemanager/rust/wrappers/FakeServiceManagerWrapper.hpp @@ -0,0 +1,25 @@ +/* + * 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" diff --git a/libs/fakeservicemanager/test_sm.cpp b/libs/fakeservicemanager/test_sm.cpp index 6fc21c65d1..cb6784c04a 100644 --- a/libs/fakeservicemanager/test_sm.cpp +++ b/libs/fakeservicemanager/test_sm.cpp @@ -22,6 +22,7 @@ #include <binder/IServiceManager.h> #include "fakeservicemanager/FakeServiceManager.h" +#include "rust/wrappers/FakeServiceManagerWrapper.hpp" using android::sp; using android::BBinder; @@ -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() { @@ -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); @@ -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); @@ -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"))); @@ -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 |