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

Commit 1621b4b6 authored by Devin Moore's avatar Devin Moore
Browse files

Change name of the fake service manager

Change the name of the class and the exported include directory to make
it more obvious when this fake service manager is being used.

Test: m
Bug: 259548383
Change-Id: I98205c49babd2e91a8f3f643de9f3eccca302c9c
Merged-In: I98205c49babd2e91a8f3f643de9f3eccca302c9c
parent 2c540ed3
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -11,7 +11,7 @@ cc_defaults {
    name: "fakeservicemanager_defaults",
    host_supported: true,
    srcs: [
        "ServiceManager.cpp",
        "FakeServiceManager.cpp",
    ],

    shared_libs: [
@@ -28,7 +28,7 @@ cc_defaults {
cc_library {
    name: "libfakeservicemanager",
    defaults: ["fakeservicemanager_defaults"],
    export_include_dirs: ["include/fakeservicemanager"],
    export_include_dirs: ["include"],
}

cc_test_host {
@@ -38,5 +38,5 @@ cc_test_host {
        "test_sm.cpp",
    ],
    static_libs: ["libgmock"],
    local_include_dirs: ["include/fakeservicemanager"],
    local_include_dirs: ["include"],
}
+17 −17
Original line number Diff line number Diff line
@@ -14,18 +14,18 @@
 * limitations under the License.
 */

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

namespace android {

ServiceManager::ServiceManager() {}
FakeServiceManager::FakeServiceManager() {}

sp<IBinder> ServiceManager::getService( const String16& name) const {
sp<IBinder> FakeServiceManager::getService( const String16& name) const {
    // Servicemanager is single-threaded and cannot block. This method exists for legacy reasons.
    return checkService(name);
}

sp<IBinder> ServiceManager::checkService( const String16& name) const {
sp<IBinder> FakeServiceManager::checkService( const String16& name) const {
    auto it = mNameToService.find(name);
    if (it == mNameToService.end()) {
        return nullptr;
@@ -33,7 +33,7 @@ sp<IBinder> ServiceManager::checkService( const String16& name) const {
    return it->second;
}

status_t ServiceManager::addService(const String16& name, const sp<IBinder>& service,
status_t FakeServiceManager::addService(const String16& name, const sp<IBinder>& service,
                                bool /*allowIsolated*/,
                                int /*dumpsysFlags*/) {
    if (service == nullptr) {
@@ -43,7 +43,7 @@ status_t ServiceManager::addService(const String16& name, const sp<IBinder>& ser
    return NO_ERROR;
}

Vector<String16> ServiceManager::listServices(int /*dumpsysFlags*/) {
Vector<String16> FakeServiceManager::listServices(int /*dumpsysFlags*/) {
    Vector<String16> services;
    for (auto const& [name, service] : mNameToService) {
        (void) service;
@@ -52,19 +52,19 @@ Vector<String16> ServiceManager::listServices(int /*dumpsysFlags*/) {
  return services;
}

IBinder* ServiceManager::onAsBinder() {
IBinder* FakeServiceManager::onAsBinder() {
    return nullptr;
}

sp<IBinder> ServiceManager::waitForService(const String16& name) {
sp<IBinder> FakeServiceManager::waitForService(const String16& name) {
    return checkService(name);
}

bool ServiceManager::isDeclared(const String16& name) {
bool FakeServiceManager::isDeclared(const String16& name) {
    return mNameToService.find(name) != mNameToService.end();
}

Vector<String16> ServiceManager::getDeclaredInstances(const String16& name) {
Vector<String16> FakeServiceManager::getDeclaredInstances(const String16& name) {
    Vector<String16> out;
    const String16 prefix = name + String16("/");
    for (const auto& [registeredName, service] : mNameToService) {
@@ -76,38 +76,38 @@ Vector<String16> ServiceManager::getDeclaredInstances(const String16& name) {
    return out;
}

std::optional<String16> ServiceManager::updatableViaApex(const String16& name) {
std::optional<String16> FakeServiceManager::updatableViaApex(const String16& name) {
    (void)name;
    return std::nullopt;
}

Vector<String16> ServiceManager::getUpdatableNames(const String16& apexName) {
Vector<String16> FakeServiceManager::getUpdatableNames(const String16& apexName) {
    (void)apexName;
    return {};
}

std::optional<IServiceManager::ConnectionInfo> ServiceManager::getConnectionInfo(
std::optional<IServiceManager::ConnectionInfo> FakeServiceManager::getConnectionInfo(
        const String16& name) {
    (void)name;
    return std::nullopt;
}

status_t ServiceManager::registerForNotifications(const String16&,
status_t FakeServiceManager::registerForNotifications(const String16&,
                                                  const sp<LocalRegistrationCallback>&) {
    return INVALID_OPERATION;
}

status_t ServiceManager::unregisterForNotifications(const String16&,
status_t FakeServiceManager::unregisterForNotifications(const String16&,
                                                const sp<LocalRegistrationCallback>&) {
    return INVALID_OPERATION;
}

std::vector<IServiceManager::ServiceDebugInfo> ServiceManager::getServiceDebugInfo() {
std::vector<IServiceManager::ServiceDebugInfo> FakeServiceManager::getServiceDebugInfo() {
    std::vector<IServiceManager::ServiceDebugInfo> ret;
    return ret;
}

void ServiceManager::clear() {
void FakeServiceManager::clear() {
    mNameToService.clear();
}
}  // namespace android
+2 −2
Original line number Diff line number Diff line
@@ -28,9 +28,9 @@ namespace android {
 * A local host simple implementation of IServiceManager, that does not
 * communicate over binder.
*/
class ServiceManager : public IServiceManager {
class FakeServiceManager : public IServiceManager {
public:
    ServiceManager();
    FakeServiceManager();

    sp<IBinder> getService( const String16& name) const override;

+13 −13
Original line number Diff line number Diff line
@@ -21,14 +21,14 @@
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>

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

using android::sp;
using android::BBinder;
using android::IBinder;
using android::OK;
using android::status_t;
using android::ServiceManager;
using android::FakeServiceManager;
using android::String16;
using android::IServiceManager;
using testing::ElementsAre;
@@ -45,19 +45,19 @@ static sp<IBinder> getBinder() {
}

TEST(AddService, HappyHappy) {
    auto sm = new ServiceManager();
    auto sm = new FakeServiceManager();
    EXPECT_EQ(sm->addService(String16("foo"), getBinder(), false /*allowIsolated*/,
        IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT), OK);
}

TEST(AddService, SadNullBinder) {
    auto sm = new ServiceManager();
    auto sm = new FakeServiceManager();
    EXPECT_EQ(sm->addService(String16("foo"), nullptr, false /*allowIsolated*/,
        IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT), android::UNEXPECTED_NULL);
}

TEST(AddService, HappyOverExistingService) {
    auto sm = new ServiceManager();
    auto sm = new FakeServiceManager();
    EXPECT_EQ(sm->addService(String16("foo"), getBinder(), false /*allowIsolated*/,
        IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT), OK);
    EXPECT_EQ(sm->addService(String16("foo"), getBinder(), false /*allowIsolated*/,
@@ -65,7 +65,7 @@ TEST(AddService, HappyOverExistingService) {
}

TEST(AddService, HappyClearAddedService) {
    auto sm = new ServiceManager();
    auto sm = new FakeServiceManager();
    EXPECT_EQ(sm->addService(String16("foo"), getBinder(), false /*allowIsolated*/,
        IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT), OK);
    EXPECT_NE(sm->getService(String16("foo")), nullptr);
@@ -74,7 +74,7 @@ TEST(AddService, HappyClearAddedService) {
}

TEST(GetService, HappyHappy) {
    auto sm = new ServiceManager();
    auto sm = new FakeServiceManager();
    sp<IBinder> service = getBinder();

    EXPECT_EQ(sm->addService(String16("foo"), service, false /*allowIsolated*/,
@@ -84,13 +84,13 @@ TEST(GetService, HappyHappy) {
}

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

    EXPECT_EQ(sm->getService(String16("foo")), nullptr);
}

TEST(ListServices, AllServices) {
    auto sm = new ServiceManager();
    auto sm = new FakeServiceManager();

    EXPECT_EQ(sm->addService(String16("sd"), getBinder(), false /*allowIsolated*/,
        IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT), OK);
@@ -109,13 +109,13 @@ TEST(ListServices, AllServices) {
}

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

    EXPECT_EQ(sm->waitForService(String16("foo")), nullptr);
}

TEST(WaitForService, HappyHappy) {
    auto sm = new ServiceManager();
    auto sm = new FakeServiceManager();
    sp<IBinder> service = getBinder();

    EXPECT_EQ(sm->addService(String16("foo"), service, false /*allowIsolated*/,
@@ -125,13 +125,13 @@ TEST(WaitForService, HappyHappy) {
}

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

    EXPECT_FALSE(sm->isDeclared(String16("foo")));
}

TEST(IsDeclared, HappyHappy) {
    auto sm = new ServiceManager();
    auto sm = new FakeServiceManager();
    sp<IBinder> service = getBinder();

    EXPECT_EQ(sm->addService(String16("foo"), service, false /*allowIsolated*/,
+2 −2
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@
#include <fuzzbinder/libbinder_ndk_driver.h>
#include <fuzzer/FuzzedDataProvider.h>

#include <ServiceManager.h>
#include <fakeservicemanager/FakeServiceManager.h>
#include <android-base/logging.h>
#include <android/binder_interface_utils.h>
#include <fuzzbinder/random_binder.h>
@@ -29,7 +29,7 @@ using ndk::SharedRefBase;
[[clang::no_destroy]] static std::once_flag gSmOnce;

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
    static android::sp<android::ServiceManager> fakeServiceManager = new android::ServiceManager();
    static android::sp<android::FakeServiceManager> fakeServiceManager = new android::FakeServiceManager();
    std::call_once(gSmOnce, [&] { setDefaultServiceManager(fakeServiceManager); });
    fakeServiceManager->clear();