Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 48c2f3e1 authored by Treehugger Robot's avatar Treehugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Adding rust wrapper lib for libfakeServiceManager" into main am: c753ca67 am: 3b907f9b

parents 2e77cd52 3b907f9b
Loading
Loading
Loading
Loading
+39 −0
Original line number Original line Diff line number Diff line
@@ -17,6 +17,7 @@ cc_defaults {
    shared_libs: [
    shared_libs: [
        "libbinder",
        "libbinder",
        "libutils",
        "libutils",
        "liblog",
    ],
    ],
    target: {
    target: {
        darwin: {
        darwin: {
@@ -40,3 +41,41 @@ cc_test_host {
    static_libs: ["libgmock"],
    static_libs: ["libgmock"],
    local_include_dirs: ["include"],
    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",
}
+25 −0
Original line number Original line Diff line number Diff line
@@ -16,6 +16,10 @@


#include "fakeservicemanager/FakeServiceManager.h"
#include "fakeservicemanager/FakeServiceManager.h"


using android::sp;
using android::FakeServiceManager;
using android::setDefaultServiceManager;

namespace android {
namespace android {


FakeServiceManager::FakeServiceManager() {}
FakeServiceManager::FakeServiceManager() {}
@@ -123,3 +127,24 @@ void FakeServiceManager::clear() {
    mNameToService.clear();
    mNameToService.clear();
}
}
}  // namespace android
}  // 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
+34 −0
Original line number Original line 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();
    }
}
+25 −0
Original line number Original line 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"
+33 −3
Original line number Original line Diff line number Diff line
@@ -22,6 +22,7 @@
#include <binder/IServiceManager.h>
#include <binder/IServiceManager.h>


#include "fakeservicemanager/FakeServiceManager.h"
#include "fakeservicemanager/FakeServiceManager.h"
#include "rust/wrappers/FakeServiceManagerWrapper.hpp"


using android::sp;
using android::sp;
using android::BBinder;
using android::BBinder;
@@ -31,6 +32,7 @@ using android::status_t;
using android::FakeServiceManager;
using android::FakeServiceManager;
using android::String16;
using android::String16;
using android::IServiceManager;
using android::IServiceManager;
using android::defaultServiceManager;
using testing::ElementsAre;
using testing::ElementsAre;


static sp<IBinder> getBinder() {
static sp<IBinder> getBinder() {
@@ -83,7 +85,7 @@ TEST(GetService, HappyHappy) {
    EXPECT_EQ(sm->getService(String16("foo")), service);
    EXPECT_EQ(sm->getService(String16("foo")), service);
}
}


TEST(GetService, NonExistant) {
TEST(GetService, NonExistent) {
    auto sm = new FakeServiceManager();
    auto sm = new FakeServiceManager();


    EXPECT_EQ(sm->getService(String16("foo")), nullptr);
    EXPECT_EQ(sm->getService(String16("foo")), nullptr);
@@ -108,7 +110,7 @@ TEST(ListServices, AllServices) {
        String16("sd")));
        String16("sd")));
}
}


TEST(WaitForService, NonExistant) {
TEST(WaitForService, NonExistent) {
    auto sm = new FakeServiceManager();
    auto sm = new FakeServiceManager();


    EXPECT_EQ(sm->waitForService(String16("foo")), nullptr);
    EXPECT_EQ(sm->waitForService(String16("foo")), nullptr);
@@ -124,7 +126,7 @@ TEST(WaitForService, HappyHappy) {
    EXPECT_EQ(sm->waitForService(String16("foo")), service);
    EXPECT_EQ(sm->waitForService(String16("foo")), service);
}
}


TEST(IsDeclared, NonExistant) {
TEST(IsDeclared, NonExistent) {
    auto sm = new FakeServiceManager();
    auto sm = new FakeServiceManager();


    EXPECT_FALSE(sm->isDeclared(String16("foo")));
    EXPECT_FALSE(sm->isDeclared(String16("foo")));
@@ -139,3 +141,31 @@ TEST(IsDeclared, HappyHappy) {


    EXPECT_TRUE(sm->isDeclared(String16("foo")));
    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