blob: 91485e456bbfea7ad321203192df22c3a6c1254b [file] [log] [blame]
Steven Moreland80e1e6d2019-06-21 12:35:59 -07001/*
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 <binder/ProcessState.h>
18#include <cutils/android_filesystem_config.h>
19#include <gtest/gtest.h>
20#include <gmock/gmock.h>
21
22#include "Access.h"
23#include "ServiceManager.h"
24
25using android::sp;
26using android::Access;
27using android::IBinder;
28using android::ServiceManager;
29using android::os::IServiceManager;
30using testing::_;
31using testing::ElementsAre;
32using testing::NiceMock;
33using testing::Return;
34
35static sp<IBinder> getBinder() {
36 // It doesn't matter what remote binder it is, we just need one so that linkToDeath will work.
37 // The context manager (servicemanager) is easy to get and is in another process.
38 return android::ProcessState::self()->getContextObject(nullptr);
39}
40
41class MockAccess : public Access {
42public:
Steven Morelanda9fe4742019-07-18 14:45:20 -070043 MOCK_METHOD0(getCallingContext, CallingContext());
44 MOCK_METHOD2(canAdd, bool(const CallingContext&, const std::string& name));
45 MOCK_METHOD2(canFind, bool(const CallingContext&, const std::string& name));
Steven Moreland80e1e6d2019-06-21 12:35:59 -070046 MOCK_METHOD1(canList, bool(const CallingContext&));
47};
48
49static sp<ServiceManager> getPermissiveServiceManager() {
50 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
51
Steven Morelanda9fe4742019-07-18 14:45:20 -070052 ON_CALL(*access, getCallingContext()).WillByDefault(Return(Access::CallingContext{}));
53 ON_CALL(*access, canAdd(_, _)).WillByDefault(Return(true));
54 ON_CALL(*access, canFind(_, _)).WillByDefault(Return(true));
Steven Moreland80e1e6d2019-06-21 12:35:59 -070055 ON_CALL(*access, canList(_)).WillByDefault(Return(true));
56
57 sp<ServiceManager> sm = new ServiceManager(std::move(access));
58 return sm;
59}
60
61TEST(AddService, HappyHappy) {
62 auto sm = getPermissiveServiceManager();
63 EXPECT_TRUE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
64 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
65}
66
67TEST(AddService, EmptyNameDisallowed) {
68 auto sm = getPermissiveServiceManager();
69 EXPECT_FALSE(sm->addService("", getBinder(), false /*allowIsolated*/,
70 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
71}
72
73TEST(AddService, JustShortEnoughServiceNameHappy) {
74 auto sm = getPermissiveServiceManager();
75 EXPECT_TRUE(sm->addService(std::string(127, 'a'), getBinder(), false /*allowIsolated*/,
76 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
77}
78
79TEST(AddService, TooLongNameDisallowed) {
80 auto sm = getPermissiveServiceManager();
81 EXPECT_FALSE(sm->addService(std::string(128, 'a'), getBinder(), false /*allowIsolated*/,
82 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
83}
84
Steven Moreland905e2e82019-07-17 11:05:45 -070085TEST(AddService, WeirdCharactersDisallowed) {
86 auto sm = getPermissiveServiceManager();
87 EXPECT_FALSE(sm->addService("happy$foo$foo", getBinder(), false /*allowIsolated*/,
88 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
89}
90
Steven Moreland80e1e6d2019-06-21 12:35:59 -070091TEST(AddService, AddNullServiceDisallowed) {
92 auto sm = getPermissiveServiceManager();
93 EXPECT_FALSE(sm->addService("foo", nullptr, false /*allowIsolated*/,
94 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
95}
96
97TEST(AddService, AddDisallowedFromApp) {
98 for (uid_t uid : { AID_APP_START, AID_APP_START + 1, AID_APP_END }) {
99 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
Steven Morelanda9fe4742019-07-18 14:45:20 -0700100 EXPECT_CALL(*access, getCallingContext()).WillOnce(Return(Access::CallingContext{
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700101 .debugPid = 1337,
102 .uid = uid,
103 }));
Steven Morelanda9fe4742019-07-18 14:45:20 -0700104 EXPECT_CALL(*access, canAdd(_, _)).Times(0);
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700105 sp<ServiceManager> sm = new ServiceManager(std::move(access));
106
107 EXPECT_FALSE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
108 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
109 }
110
111}
112
113TEST(AddService, HappyOverExistingService) {
114 auto sm = getPermissiveServiceManager();
115 EXPECT_TRUE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
116 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
117 EXPECT_TRUE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
118 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
119}
120
121TEST(AddService, NoPermissions) {
122 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
123
Steven Morelanda9fe4742019-07-18 14:45:20 -0700124 EXPECT_CALL(*access, getCallingContext()).WillOnce(Return(Access::CallingContext{}));
125 EXPECT_CALL(*access, canAdd(_, _)).WillOnce(Return(false));
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700126
127 sp<ServiceManager> sm = new ServiceManager(std::move(access));
128
129 EXPECT_FALSE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
130 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
131}
132
133TEST(GetService, HappyHappy) {
134 auto sm = getPermissiveServiceManager();
135 EXPECT_TRUE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
136 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
137
138 sp<IBinder> out;
139 EXPECT_TRUE(sm->getService("foo", &out).isOk());
140 EXPECT_EQ(getBinder(), out);
141}
142
143TEST(GetService, NonExistant) {
144 auto sm = getPermissiveServiceManager();
145
146 sp<IBinder> out;
147 EXPECT_TRUE(sm->getService("foo", &out).isOk());
148 EXPECT_EQ(nullptr, out.get());
149}
150
151TEST(GetService, NoPermissionsForGettingService) {
152 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
153
Steven Morelanda9fe4742019-07-18 14:45:20 -0700154 EXPECT_CALL(*access, getCallingContext()).WillRepeatedly(Return(Access::CallingContext{}));
155 EXPECT_CALL(*access, canAdd(_, _)).WillOnce(Return(true));
156 EXPECT_CALL(*access, canFind(_, _)).WillOnce(Return(false));
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700157
158 sp<ServiceManager> sm = new ServiceManager(std::move(access));
159
160 EXPECT_TRUE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
161 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
162
163 sp<IBinder> out;
164 // returns nullptr but has OK status for legacy compatibility
165 EXPECT_TRUE(sm->getService("foo", &out).isOk());
166 EXPECT_EQ(nullptr, out.get());
167}
168
169TEST(GetService, AllowedFromIsolated) {
170 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
171
Steven Morelanda9fe4742019-07-18 14:45:20 -0700172 EXPECT_CALL(*access, getCallingContext())
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700173 // something adds it
174 .WillOnce(Return(Access::CallingContext{}))
175 // next call is from isolated app
176 .WillOnce(Return(Access::CallingContext{
177 .uid = AID_ISOLATED_START,
178 }));
Steven Morelanda9fe4742019-07-18 14:45:20 -0700179 EXPECT_CALL(*access, canAdd(_, _)).WillOnce(Return(true));
180 EXPECT_CALL(*access, canFind(_, _)).WillOnce(Return(true));
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700181
182 sp<ServiceManager> sm = new ServiceManager(std::move(access));
183
184 EXPECT_TRUE(sm->addService("foo", getBinder(), true /*allowIsolated*/,
185 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
186
187 sp<IBinder> out;
188 EXPECT_TRUE(sm->getService("foo", &out).isOk());
189 EXPECT_EQ(getBinder(), out.get());
190}
191
192TEST(GetService, NotAllowedFromIsolated) {
193 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
194
Steven Morelanda9fe4742019-07-18 14:45:20 -0700195 EXPECT_CALL(*access, getCallingContext())
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700196 // something adds it
197 .WillOnce(Return(Access::CallingContext{}))
198 // next call is from isolated app
199 .WillOnce(Return(Access::CallingContext{
200 .uid = AID_ISOLATED_START,
201 }));
Steven Morelanda9fe4742019-07-18 14:45:20 -0700202 EXPECT_CALL(*access, canAdd(_, _)).WillOnce(Return(true));
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700203
204 // TODO(b/136023468): when security check is first, this should be called first
Steven Morelanda9fe4742019-07-18 14:45:20 -0700205 // EXPECT_CALL(*access, canFind(_, _)).WillOnce(Return(true));
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700206
207 sp<ServiceManager> sm = new ServiceManager(std::move(access));
208
209 EXPECT_TRUE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
210 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
211
212 sp<IBinder> out;
213 // returns nullptr but has OK status for legacy compatibility
214 EXPECT_TRUE(sm->getService("foo", &out).isOk());
215 EXPECT_EQ(nullptr, out.get());
216}
217
218TEST(ListServices, NoPermissions) {
219 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
220
Steven Morelanda9fe4742019-07-18 14:45:20 -0700221 EXPECT_CALL(*access, getCallingContext()).WillOnce(Return(Access::CallingContext{}));
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700222 EXPECT_CALL(*access, canList(_)).WillOnce(Return(false));
223
224 sp<ServiceManager> sm = new ServiceManager(std::move(access));
225
226 std::vector<std::string> out;
227 EXPECT_FALSE(sm->listServices(IServiceManager::DUMP_FLAG_PRIORITY_ALL, &out).isOk());
228 EXPECT_TRUE(out.empty());
229}
230
231TEST(ListServices, AllServices) {
232 auto sm = getPermissiveServiceManager();
233
234 EXPECT_TRUE(sm->addService("sd", getBinder(), false /*allowIsolated*/,
235 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
236 EXPECT_TRUE(sm->addService("sc", getBinder(), false /*allowIsolated*/,
237 IServiceManager::DUMP_FLAG_PRIORITY_NORMAL).isOk());
238 EXPECT_TRUE(sm->addService("sb", getBinder(), false /*allowIsolated*/,
239 IServiceManager::DUMP_FLAG_PRIORITY_HIGH).isOk());
240 EXPECT_TRUE(sm->addService("sa", getBinder(), false /*allowIsolated*/,
241 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL).isOk());
242
243 std::vector<std::string> out;
244 EXPECT_TRUE(sm->listServices(IServiceManager::DUMP_FLAG_PRIORITY_ALL, &out).isOk());
245
246 // all there and in the right order
247 EXPECT_THAT(out, ElementsAre("sa", "sb", "sc", "sd"));
248}
249
250TEST(ListServices, CriticalServices) {
251 auto sm = getPermissiveServiceManager();
252
253 EXPECT_TRUE(sm->addService("sd", getBinder(), false /*allowIsolated*/,
254 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
255 EXPECT_TRUE(sm->addService("sc", getBinder(), false /*allowIsolated*/,
256 IServiceManager::DUMP_FLAG_PRIORITY_NORMAL).isOk());
257 EXPECT_TRUE(sm->addService("sb", getBinder(), false /*allowIsolated*/,
258 IServiceManager::DUMP_FLAG_PRIORITY_HIGH).isOk());
259 EXPECT_TRUE(sm->addService("sa", getBinder(), false /*allowIsolated*/,
260 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL).isOk());
261
262 std::vector<std::string> out;
263 EXPECT_TRUE(sm->listServices(IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL, &out).isOk());
264
265 // all there and in the right order
266 EXPECT_THAT(out, ElementsAre("sa"));
267}