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

Commit 10727c4c authored by Devin Moore's avatar Devin Moore Committed by Automerger Merge Worker
Browse files

Merge "Change name of the fake service manager" am: 46ecdf82

parents 6693a493 46ecdf82
Loading
Loading
Loading
Loading
+3 −3
Original line number Original line Diff line number Diff line
@@ -11,7 +11,7 @@ cc_defaults {
    name: "fakeservicemanager_defaults",
    name: "fakeservicemanager_defaults",
    host_supported: true,
    host_supported: true,
    srcs: [
    srcs: [
        "ServiceManager.cpp",
        "FakeServiceManager.cpp",
    ],
    ],


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


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


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


namespace android {
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.
    // Servicemanager is single-threaded and cannot block. This method exists for legacy reasons.
    return checkService(name);
    return checkService(name);
}
}


sp<IBinder> ServiceManager::checkService( const String16& name) const {
sp<IBinder> FakeServiceManager::checkService( const String16& name) const {
    auto it = mNameToService.find(name);
    auto it = mNameToService.find(name);
    if (it == mNameToService.end()) {
    if (it == mNameToService.end()) {
        return nullptr;
        return nullptr;
@@ -33,7 +33,7 @@ sp<IBinder> ServiceManager::checkService( const String16& name) const {
    return it->second;
    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*/,
                                bool /*allowIsolated*/,
                                int /*dumpsysFlags*/) {
                                int /*dumpsysFlags*/) {
    if (service == nullptr) {
    if (service == nullptr) {
@@ -43,7 +43,7 @@ status_t ServiceManager::addService(const String16& name, const sp<IBinder>& ser
    return NO_ERROR;
    return NO_ERROR;
}
}


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
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); });
    std::call_once(gSmOnce, [&] { setDefaultServiceManager(fakeServiceManager); });
    fakeServiceManager->clear();
    fakeServiceManager->clear();