blob: 4ddbe0cf8149372d42f2c28bc13277826fa52173 [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
24#include "ServiceManager.h"
25
26using android::sp;
27using android::BBinder;
28using android::IBinder;
29using android::OK;
30using android::status_t;
31using android::ServiceManager;
32using 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) {
48 auto sm = new ServiceManager();
49 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) {
54 auto sm = new ServiceManager();
55 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) {
60 auto sm = new ServiceManager();
61 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
67TEST(GetService, HappyHappy) {
68 auto sm = new ServiceManager();
69 sp<IBinder> service = getBinder();
70
71 EXPECT_EQ(sm->addService(String16("foo"), service, false /*allowIsolated*/,
72 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT), OK);
73
74 EXPECT_EQ(sm->getService(String16("foo")), service);
75}
76
77TEST(GetService, NonExistant) {
78 auto sm = new ServiceManager();
79
80 EXPECT_EQ(sm->getService(String16("foo")), nullptr);
81}
82
83TEST(ListServices, AllServices) {
84 auto sm = new ServiceManager();
85
86 EXPECT_EQ(sm->addService(String16("sd"), getBinder(), false /*allowIsolated*/,
87 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT), OK);
88 EXPECT_EQ(sm->addService(String16("sc"), getBinder(), false /*allowIsolated*/,
89 IServiceManager::DUMP_FLAG_PRIORITY_NORMAL), OK);
90 EXPECT_EQ(sm->addService(String16("sb"), getBinder(), false /*allowIsolated*/,
91 IServiceManager::DUMP_FLAG_PRIORITY_HIGH), OK);
92 EXPECT_EQ(sm->addService(String16("sa"), getBinder(), false /*allowIsolated*/,
93 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL), OK);
94
95 android::Vector<String16> out = sm->listServices(IServiceManager::DUMP_FLAG_PRIORITY_ALL);
96
97 // all there and in the right order
98 EXPECT_THAT(out, ElementsAre(String16("sa"), String16("sb"), String16("sc"),
99 String16("sd")));
100}
101
102TEST(WaitForService, NonExistant) {
103 auto sm = new ServiceManager();
104
105 EXPECT_EQ(sm->waitForService(String16("foo")), nullptr);
106}
107
108TEST(WaitForService, HappyHappy) {
109 auto sm = new ServiceManager();
110 sp<IBinder> service = getBinder();
111
112 EXPECT_EQ(sm->addService(String16("foo"), service, false /*allowIsolated*/,
113 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT), OK);
114
115 EXPECT_EQ(sm->waitForService(String16("foo")), service);
116}
117
118TEST(IsDeclared, NonExistant) {
119 auto sm = new ServiceManager();
120
121 EXPECT_FALSE(sm->isDeclared(String16("foo")));
122}
123
124TEST(IsDeclared, HappyHappy) {
125 auto sm = new ServiceManager();
126 sp<IBinder> service = getBinder();
127
128 EXPECT_EQ(sm->addService(String16("foo"), service, false /*allowIsolated*/,
129 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT), OK);
130
131 EXPECT_TRUE(sm->isDeclared(String16("foo")));
132}