blob: 6fc21c65d13c55f89dfbc052e544648f620fa13d [file] [log] [blame]
Brett Chabotc9ee3302020-01-31 09:20:28 -08001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18#include <gmock/gmock.h>
19
20#include <binder/Binder.h>
21#include <binder/ProcessState.h>
22#include <binder/IServiceManager.h>
23
Devin Moore1621b4b2023-02-03 22:01:09 +000024#include "fakeservicemanager/FakeServiceManager.h"
Brett Chabotc9ee3302020-01-31 09:20:28 -080025
26using android::sp;
27using android::BBinder;
28using android::IBinder;
29using android::OK;
30using android::status_t;
Devin Moore1621b4b2023-02-03 22:01:09 +000031using android::FakeServiceManager;
Brett Chabotc9ee3302020-01-31 09:20:28 -080032using android::String16;
33using android::IServiceManager;
34using testing::ElementsAre;
35
36static sp<IBinder> getBinder() {
37 class LinkableBinder : public BBinder {
38 status_t linkToDeath(const sp<DeathRecipient>&, void*, uint32_t) override {
39 // let SM linkToDeath
40 return OK;
41 }
42 };
43
44 return new LinkableBinder;
45}
46
47TEST(AddService, HappyHappy) {
Devin Moore1621b4b2023-02-03 22:01:09 +000048 auto sm = new FakeServiceManager();
Brett Chabotc9ee3302020-01-31 09:20:28 -080049 EXPECT_EQ(sm->addService(String16("foo"), getBinder(), false /*allowIsolated*/,
50 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT), OK);
51}
52
Devin Moore5f6ded82022-11-18 18:36:30 +000053TEST(AddService, SadNullBinder) {
Devin Moore1621b4b2023-02-03 22:01:09 +000054 auto sm = new FakeServiceManager();
Devin Moore5f6ded82022-11-18 18:36:30 +000055 EXPECT_EQ(sm->addService(String16("foo"), nullptr, false /*allowIsolated*/,
56 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT), android::UNEXPECTED_NULL);
57}
58
Brett Chabotc9ee3302020-01-31 09:20:28 -080059TEST(AddService, HappyOverExistingService) {
Devin Moore1621b4b2023-02-03 22:01:09 +000060 auto sm = new FakeServiceManager();
Brett Chabotc9ee3302020-01-31 09:20:28 -080061 EXPECT_EQ(sm->addService(String16("foo"), getBinder(), false /*allowIsolated*/,
62 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT), OK);
63 EXPECT_EQ(sm->addService(String16("foo"), getBinder(), false /*allowIsolated*/,
64 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT), OK);
65}
66
Devin Moore8cc776a2022-11-18 18:43:53 +000067TEST(AddService, HappyClearAddedService) {
Devin Moore1621b4b2023-02-03 22:01:09 +000068 auto sm = new FakeServiceManager();
Devin Moore8cc776a2022-11-18 18:43:53 +000069 EXPECT_EQ(sm->addService(String16("foo"), getBinder(), false /*allowIsolated*/,
70 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT), OK);
71 EXPECT_NE(sm->getService(String16("foo")), nullptr);
72 sm->clear();
73 EXPECT_EQ(sm->getService(String16("foo")), nullptr);
74}
75
Brett Chabotc9ee3302020-01-31 09:20:28 -080076TEST(GetService, HappyHappy) {
Devin Moore1621b4b2023-02-03 22:01:09 +000077 auto sm = new FakeServiceManager();
Brett Chabotc9ee3302020-01-31 09:20:28 -080078 sp<IBinder> service = getBinder();
79
80 EXPECT_EQ(sm->addService(String16("foo"), service, false /*allowIsolated*/,
81 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT), OK);
82
83 EXPECT_EQ(sm->getService(String16("foo")), service);
84}
85
86TEST(GetService, NonExistant) {
Devin Moore1621b4b2023-02-03 22:01:09 +000087 auto sm = new FakeServiceManager();
Brett Chabotc9ee3302020-01-31 09:20:28 -080088
89 EXPECT_EQ(sm->getService(String16("foo")), nullptr);
90}
91
92TEST(ListServices, AllServices) {
Devin Moore1621b4b2023-02-03 22:01:09 +000093 auto sm = new FakeServiceManager();
Brett Chabotc9ee3302020-01-31 09:20:28 -080094
95 EXPECT_EQ(sm->addService(String16("sd"), getBinder(), false /*allowIsolated*/,
96 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT), OK);
97 EXPECT_EQ(sm->addService(String16("sc"), getBinder(), false /*allowIsolated*/,
98 IServiceManager::DUMP_FLAG_PRIORITY_NORMAL), OK);
99 EXPECT_EQ(sm->addService(String16("sb"), getBinder(), false /*allowIsolated*/,
100 IServiceManager::DUMP_FLAG_PRIORITY_HIGH), OK);
101 EXPECT_EQ(sm->addService(String16("sa"), getBinder(), false /*allowIsolated*/,
102 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL), OK);
103
104 android::Vector<String16> out = sm->listServices(IServiceManager::DUMP_FLAG_PRIORITY_ALL);
105
106 // all there and in the right order
107 EXPECT_THAT(out, ElementsAre(String16("sa"), String16("sb"), String16("sc"),
108 String16("sd")));
109}
110
111TEST(WaitForService, NonExistant) {
Devin Moore1621b4b2023-02-03 22:01:09 +0000112 auto sm = new FakeServiceManager();
Brett Chabotc9ee3302020-01-31 09:20:28 -0800113
114 EXPECT_EQ(sm->waitForService(String16("foo")), nullptr);
115}
116
117TEST(WaitForService, HappyHappy) {
Devin Moore1621b4b2023-02-03 22:01:09 +0000118 auto sm = new FakeServiceManager();
Brett Chabotc9ee3302020-01-31 09:20:28 -0800119 sp<IBinder> service = getBinder();
120
121 EXPECT_EQ(sm->addService(String16("foo"), service, false /*allowIsolated*/,
122 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT), OK);
123
124 EXPECT_EQ(sm->waitForService(String16("foo")), service);
125}
126
127TEST(IsDeclared, NonExistant) {
Devin Moore1621b4b2023-02-03 22:01:09 +0000128 auto sm = new FakeServiceManager();
Brett Chabotc9ee3302020-01-31 09:20:28 -0800129
130 EXPECT_FALSE(sm->isDeclared(String16("foo")));
131}
132
133TEST(IsDeclared, HappyHappy) {
Devin Moore1621b4b2023-02-03 22:01:09 +0000134 auto sm = new FakeServiceManager();
Brett Chabotc9ee3302020-01-31 09:20:28 -0800135 sp<IBinder> service = getBinder();
136
137 EXPECT_EQ(sm->addService(String16("foo"), service, false /*allowIsolated*/,
138 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT), OK);
139
140 EXPECT_TRUE(sm->isDeclared(String16("foo")));
141}