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

Commit 77cd3a2b authored by Parth Sane's avatar Parth Sane Committed by Gerrit Code Review
Browse files

Merge "Add negative tests for ServiceManager RegisterForNotifications and...

Merge "Add negative tests for ServiceManager RegisterForNotifications and unregisterForNotifications" into main
parents b0feef54 81b4d5a5
Loading
Loading
Loading
Loading
+32 −1
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@
#include <binder/IServiceManager.h>
#include <binder/RpcServer.h>
#include <binder/RpcSession.h>
#include <binder/Status.h>
#include <binder/unique_fd.h>
#include <utils/Flattenable.h>

@@ -57,6 +58,7 @@ using namespace std::string_literals;
using namespace std::chrono_literals;
using android::base::testing::HasValue;
using android::base::testing::Ok;
using android::binder::Status;
using android::binder::unique_fd;
using testing::ExplainMatchResult;
using testing::Matcher;
@@ -461,6 +463,35 @@ TEST_F(BinderLibTest, AddManagerToManager) {
    EXPECT_EQ(NO_ERROR, sm->addService(String16("binderLibTest-manager"), binder));
}

TEST_F(BinderLibTest, RegisterForNotificationsFailure) {
    auto sm = defaultServiceManager();
    using LocalRegistrationCallback = IServiceManager::LocalRegistrationCallback;
    class LocalRegistrationCallbackImpl : public virtual LocalRegistrationCallback {
        void onServiceRegistration(const String16&, const sp<IBinder>&) override {}
        virtual ~LocalRegistrationCallbackImpl() {}
    };
    sp<LocalRegistrationCallback> cb = sp<LocalRegistrationCallbackImpl>::make();

    EXPECT_EQ(BAD_VALUE, sm->registerForNotifications(String16("ValidName"), nullptr));
    EXPECT_EQ(UNKNOWN_ERROR, sm->registerForNotifications(String16("InvalidName!$"), cb));
}

TEST_F(BinderLibTest, UnregisterForNotificationsFailure) {
    auto sm = defaultServiceManager();
    using LocalRegistrationCallback = IServiceManager::LocalRegistrationCallback;
    class LocalRegistrationCallbackImpl : public virtual LocalRegistrationCallback {
        void onServiceRegistration(const String16&, const sp<IBinder>&) override {}
        virtual ~LocalRegistrationCallbackImpl() {}
    };
    sp<LocalRegistrationCallback> cb = sp<LocalRegistrationCallbackImpl>::make();

    EXPECT_EQ(OK, sm->registerForNotifications(String16("ValidName"), cb));

    EXPECT_EQ(BAD_VALUE, sm->unregisterForNotifications(String16("ValidName"), nullptr));
    EXPECT_EQ(BAD_VALUE, sm->unregisterForNotifications(String16("AnotherValidName"), cb));
    EXPECT_EQ(BAD_VALUE, sm->unregisterForNotifications(String16("InvalidName!!!"), cb));
}

TEST_F(BinderLibTest, WasParceled) {
    auto binder = sp<BBinder>::make();
    EXPECT_FALSE(binder->wasParceled());