blob: 3c211d285fd1a529bca9a6864cc7419843a2b50c [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
Steven Moreland27cfab02019-08-12 14:34:16 -070017#include <android/os/BnServiceCallback.h>
18#include <binder/Binder.h>
Steven Moreland80e1e6d2019-06-21 12:35:59 -070019#include <binder/ProcessState.h>
Steven Moreland27cfab02019-08-12 14:34:16 -070020#include <binder/IServiceManager.h>
Steven Moreland80e1e6d2019-06-21 12:35:59 -070021#include <cutils/android_filesystem_config.h>
22#include <gtest/gtest.h>
23#include <gmock/gmock.h>
24
25#include "Access.h"
26#include "ServiceManager.h"
27
28using android::sp;
29using android::Access;
Steven Moreland27cfab02019-08-12 14:34:16 -070030using android::BBinder;
Steven Moreland80e1e6d2019-06-21 12:35:59 -070031using android::IBinder;
32using android::ServiceManager;
Steven Moreland27cfab02019-08-12 14:34:16 -070033using android::binder::Status;
34using android::os::BnServiceCallback;
Steven Moreland80e1e6d2019-06-21 12:35:59 -070035using android::os::IServiceManager;
36using testing::_;
37using testing::ElementsAre;
38using testing::NiceMock;
39using testing::Return;
40
41static sp<IBinder> getBinder() {
Steven Moreland27cfab02019-08-12 14:34:16 -070042 class LinkableBinder : public BBinder {
43 android::status_t linkToDeath(const sp<DeathRecipient>&, void*, uint32_t) override {
44 // let SM linkToDeath
45 return android::OK;
46 }
47 };
48
49 return new LinkableBinder;
Steven Moreland80e1e6d2019-06-21 12:35:59 -070050}
51
52class MockAccess : public Access {
53public:
Steven Morelanda9fe4742019-07-18 14:45:20 -070054 MOCK_METHOD0(getCallingContext, CallingContext());
55 MOCK_METHOD2(canAdd, bool(const CallingContext&, const std::string& name));
56 MOCK_METHOD2(canFind, bool(const CallingContext&, const std::string& name));
Steven Moreland80e1e6d2019-06-21 12:35:59 -070057 MOCK_METHOD1(canList, bool(const CallingContext&));
58};
59
60static sp<ServiceManager> getPermissiveServiceManager() {
61 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
62
Steven Morelanda9fe4742019-07-18 14:45:20 -070063 ON_CALL(*access, getCallingContext()).WillByDefault(Return(Access::CallingContext{}));
64 ON_CALL(*access, canAdd(_, _)).WillByDefault(Return(true));
65 ON_CALL(*access, canFind(_, _)).WillByDefault(Return(true));
Steven Moreland80e1e6d2019-06-21 12:35:59 -070066 ON_CALL(*access, canList(_)).WillByDefault(Return(true));
67
68 sp<ServiceManager> sm = new ServiceManager(std::move(access));
69 return sm;
70}
71
72TEST(AddService, HappyHappy) {
73 auto sm = getPermissiveServiceManager();
74 EXPECT_TRUE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
75 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
76}
77
78TEST(AddService, EmptyNameDisallowed) {
79 auto sm = getPermissiveServiceManager();
80 EXPECT_FALSE(sm->addService("", getBinder(), false /*allowIsolated*/,
81 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
82}
83
84TEST(AddService, JustShortEnoughServiceNameHappy) {
85 auto sm = getPermissiveServiceManager();
86 EXPECT_TRUE(sm->addService(std::string(127, 'a'), getBinder(), false /*allowIsolated*/,
87 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
88}
89
90TEST(AddService, TooLongNameDisallowed) {
91 auto sm = getPermissiveServiceManager();
92 EXPECT_FALSE(sm->addService(std::string(128, 'a'), getBinder(), false /*allowIsolated*/,
93 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
94}
95
Steven Moreland905e2e82019-07-17 11:05:45 -070096TEST(AddService, WeirdCharactersDisallowed) {
97 auto sm = getPermissiveServiceManager();
98 EXPECT_FALSE(sm->addService("happy$foo$foo", getBinder(), false /*allowIsolated*/,
99 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
100}
101
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700102TEST(AddService, AddNullServiceDisallowed) {
103 auto sm = getPermissiveServiceManager();
104 EXPECT_FALSE(sm->addService("foo", nullptr, false /*allowIsolated*/,
105 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
106}
107
108TEST(AddService, AddDisallowedFromApp) {
109 for (uid_t uid : { AID_APP_START, AID_APP_START + 1, AID_APP_END }) {
110 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
Steven Morelanda9fe4742019-07-18 14:45:20 -0700111 EXPECT_CALL(*access, getCallingContext()).WillOnce(Return(Access::CallingContext{
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700112 .debugPid = 1337,
113 .uid = uid,
114 }));
Steven Morelanda9fe4742019-07-18 14:45:20 -0700115 EXPECT_CALL(*access, canAdd(_, _)).Times(0);
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700116 sp<ServiceManager> sm = new ServiceManager(std::move(access));
117
118 EXPECT_FALSE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
119 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
120 }
121
122}
123
124TEST(AddService, HappyOverExistingService) {
125 auto sm = getPermissiveServiceManager();
126 EXPECT_TRUE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
127 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
128 EXPECT_TRUE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
129 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
130}
131
132TEST(AddService, NoPermissions) {
133 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
134
Steven Morelanda9fe4742019-07-18 14:45:20 -0700135 EXPECT_CALL(*access, getCallingContext()).WillOnce(Return(Access::CallingContext{}));
136 EXPECT_CALL(*access, canAdd(_, _)).WillOnce(Return(false));
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700137
138 sp<ServiceManager> sm = new ServiceManager(std::move(access));
139
140 EXPECT_FALSE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
141 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
142}
143
144TEST(GetService, HappyHappy) {
145 auto sm = getPermissiveServiceManager();
Steven Moreland27cfab02019-08-12 14:34:16 -0700146 sp<IBinder> service = getBinder();
147
148 EXPECT_TRUE(sm->addService("foo", service, false /*allowIsolated*/,
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700149 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
150
151 sp<IBinder> out;
152 EXPECT_TRUE(sm->getService("foo", &out).isOk());
Steven Moreland27cfab02019-08-12 14:34:16 -0700153 EXPECT_EQ(service, out);
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700154}
155
156TEST(GetService, NonExistant) {
157 auto sm = getPermissiveServiceManager();
158
159 sp<IBinder> out;
160 EXPECT_TRUE(sm->getService("foo", &out).isOk());
161 EXPECT_EQ(nullptr, out.get());
162}
163
164TEST(GetService, NoPermissionsForGettingService) {
165 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
166
Steven Morelanda9fe4742019-07-18 14:45:20 -0700167 EXPECT_CALL(*access, getCallingContext()).WillRepeatedly(Return(Access::CallingContext{}));
168 EXPECT_CALL(*access, canAdd(_, _)).WillOnce(Return(true));
169 EXPECT_CALL(*access, canFind(_, _)).WillOnce(Return(false));
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700170
171 sp<ServiceManager> sm = new ServiceManager(std::move(access));
172
173 EXPECT_TRUE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
174 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
175
176 sp<IBinder> out;
177 // returns nullptr but has OK status for legacy compatibility
178 EXPECT_TRUE(sm->getService("foo", &out).isOk());
179 EXPECT_EQ(nullptr, out.get());
180}
181
182TEST(GetService, AllowedFromIsolated) {
183 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
184
Steven Morelanda9fe4742019-07-18 14:45:20 -0700185 EXPECT_CALL(*access, getCallingContext())
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700186 // something adds it
187 .WillOnce(Return(Access::CallingContext{}))
188 // next call is from isolated app
189 .WillOnce(Return(Access::CallingContext{
190 .uid = AID_ISOLATED_START,
191 }));
Steven Morelanda9fe4742019-07-18 14:45:20 -0700192 EXPECT_CALL(*access, canAdd(_, _)).WillOnce(Return(true));
193 EXPECT_CALL(*access, canFind(_, _)).WillOnce(Return(true));
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700194
195 sp<ServiceManager> sm = new ServiceManager(std::move(access));
196
Steven Moreland27cfab02019-08-12 14:34:16 -0700197 sp<IBinder> service = getBinder();
198 EXPECT_TRUE(sm->addService("foo", service, true /*allowIsolated*/,
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700199 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
200
201 sp<IBinder> out;
202 EXPECT_TRUE(sm->getService("foo", &out).isOk());
Steven Moreland27cfab02019-08-12 14:34:16 -0700203 EXPECT_EQ(service, out.get());
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700204}
205
206TEST(GetService, NotAllowedFromIsolated) {
207 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
208
Steven Morelanda9fe4742019-07-18 14:45:20 -0700209 EXPECT_CALL(*access, getCallingContext())
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700210 // something adds it
211 .WillOnce(Return(Access::CallingContext{}))
212 // next call is from isolated app
213 .WillOnce(Return(Access::CallingContext{
214 .uid = AID_ISOLATED_START,
215 }));
Steven Morelanda9fe4742019-07-18 14:45:20 -0700216 EXPECT_CALL(*access, canAdd(_, _)).WillOnce(Return(true));
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700217
218 // TODO(b/136023468): when security check is first, this should be called first
Steven Morelanda9fe4742019-07-18 14:45:20 -0700219 // EXPECT_CALL(*access, canFind(_, _)).WillOnce(Return(true));
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700220
221 sp<ServiceManager> sm = new ServiceManager(std::move(access));
222
223 EXPECT_TRUE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
224 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
225
226 sp<IBinder> out;
227 // returns nullptr but has OK status for legacy compatibility
228 EXPECT_TRUE(sm->getService("foo", &out).isOk());
229 EXPECT_EQ(nullptr, out.get());
230}
231
232TEST(ListServices, NoPermissions) {
233 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
234
Steven Morelanda9fe4742019-07-18 14:45:20 -0700235 EXPECT_CALL(*access, getCallingContext()).WillOnce(Return(Access::CallingContext{}));
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700236 EXPECT_CALL(*access, canList(_)).WillOnce(Return(false));
237
238 sp<ServiceManager> sm = new ServiceManager(std::move(access));
239
240 std::vector<std::string> out;
241 EXPECT_FALSE(sm->listServices(IServiceManager::DUMP_FLAG_PRIORITY_ALL, &out).isOk());
242 EXPECT_TRUE(out.empty());
243}
244
245TEST(ListServices, AllServices) {
246 auto sm = getPermissiveServiceManager();
247
248 EXPECT_TRUE(sm->addService("sd", getBinder(), false /*allowIsolated*/,
249 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
250 EXPECT_TRUE(sm->addService("sc", getBinder(), false /*allowIsolated*/,
251 IServiceManager::DUMP_FLAG_PRIORITY_NORMAL).isOk());
252 EXPECT_TRUE(sm->addService("sb", getBinder(), false /*allowIsolated*/,
253 IServiceManager::DUMP_FLAG_PRIORITY_HIGH).isOk());
254 EXPECT_TRUE(sm->addService("sa", getBinder(), false /*allowIsolated*/,
255 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL).isOk());
256
257 std::vector<std::string> out;
258 EXPECT_TRUE(sm->listServices(IServiceManager::DUMP_FLAG_PRIORITY_ALL, &out).isOk());
259
260 // all there and in the right order
261 EXPECT_THAT(out, ElementsAre("sa", "sb", "sc", "sd"));
262}
263
264TEST(ListServices, CriticalServices) {
265 auto sm = getPermissiveServiceManager();
266
267 EXPECT_TRUE(sm->addService("sd", getBinder(), false /*allowIsolated*/,
268 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
269 EXPECT_TRUE(sm->addService("sc", getBinder(), false /*allowIsolated*/,
270 IServiceManager::DUMP_FLAG_PRIORITY_NORMAL).isOk());
271 EXPECT_TRUE(sm->addService("sb", getBinder(), false /*allowIsolated*/,
272 IServiceManager::DUMP_FLAG_PRIORITY_HIGH).isOk());
273 EXPECT_TRUE(sm->addService("sa", getBinder(), false /*allowIsolated*/,
274 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL).isOk());
275
276 std::vector<std::string> out;
277 EXPECT_TRUE(sm->listServices(IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL, &out).isOk());
278
279 // all there and in the right order
280 EXPECT_THAT(out, ElementsAre("sa"));
281}
Steven Moreland27cfab02019-08-12 14:34:16 -0700282
283class CallbackHistorian : public BnServiceCallback {
284 Status onRegistration(const std::string& name, const sp<IBinder>& binder) override {
285 registrations.push_back(name);
286 binders.push_back(binder);
287 return Status::ok();
288 }
289
290 android::status_t linkToDeath(const sp<DeathRecipient>&, void*, uint32_t) override {
291 // let SM linkToDeath
292 return android::OK;
293 }
294
295public:
296 std::vector<std::string> registrations;
297 std::vector<sp<IBinder>> binders;
298};
299
300TEST(ServiceNotifications, NoPermissionsRegister) {
301 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
302
303 EXPECT_CALL(*access, getCallingContext()).WillOnce(Return(Access::CallingContext{}));
304 EXPECT_CALL(*access, canFind(_,_)).WillOnce(Return(false));
305
306 sp<ServiceManager> sm = new ServiceManager(std::move(access));
307
308 sp<CallbackHistorian> cb = new CallbackHistorian;
309
310 EXPECT_EQ(sm->registerForNotifications("foofoo", cb).exceptionCode(),
311 Status::EX_SECURITY);
312}
313
314TEST(ServiceNotifications, NoPermissionsUnregister) {
315 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
316
317 EXPECT_CALL(*access, getCallingContext()).WillOnce(Return(Access::CallingContext{}));
318 EXPECT_CALL(*access, canFind(_,_)).WillOnce(Return(false));
319
320 sp<ServiceManager> sm = new ServiceManager(std::move(access));
321
322 sp<CallbackHistorian> cb = new CallbackHistorian;
323
324 // should always hit security error first
325 EXPECT_EQ(sm->unregisterForNotifications("foofoo", cb).exceptionCode(),
326 Status::EX_SECURITY);
327}
328
329TEST(ServiceNotifications, InvalidName) {
330 auto sm = getPermissiveServiceManager();
331
332 sp<CallbackHistorian> cb = new CallbackHistorian;
333
334 EXPECT_EQ(sm->registerForNotifications("foo@foo", cb).exceptionCode(),
335 Status::EX_ILLEGAL_ARGUMENT);
336}
337
338TEST(ServiceNotifications, NullCallback) {
339 auto sm = getPermissiveServiceManager();
340
341 EXPECT_EQ(sm->registerForNotifications("foofoo", nullptr).exceptionCode(),
342 Status::EX_NULL_POINTER);
343}
344
345TEST(ServiceNotifications, Unregister) {
346 auto sm = getPermissiveServiceManager();
347
348 sp<CallbackHistorian> cb = new CallbackHistorian;
349
350 EXPECT_TRUE(sm->registerForNotifications("foofoo", cb).isOk());
351 EXPECT_EQ(sm->unregisterForNotifications("foofoo", cb).exceptionCode(), 0);
352}
353
354TEST(ServiceNotifications, UnregisterWhenNoRegistrationExists) {
355 auto sm = getPermissiveServiceManager();
356
357 sp<CallbackHistorian> cb = new CallbackHistorian;
358
359 EXPECT_EQ(sm->unregisterForNotifications("foofoo", cb).exceptionCode(),
360 Status::EX_ILLEGAL_STATE);
361}
362
363TEST(ServiceNotifications, NoNotification) {
364 auto sm = getPermissiveServiceManager();
365
366 sp<CallbackHistorian> cb = new CallbackHistorian;
367
368 EXPECT_TRUE(sm->registerForNotifications("foofoo", cb).isOk());
369 EXPECT_TRUE(sm->addService("otherservice", getBinder(),
370 false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
371
372 EXPECT_THAT(cb->registrations, ElementsAre());
373 EXPECT_THAT(cb->binders, ElementsAre());
374}
375
376TEST(ServiceNotifications, GetNotification) {
377 auto sm = getPermissiveServiceManager();
378
379 sp<CallbackHistorian> cb = new CallbackHistorian;
380
381 sp<IBinder> service = getBinder();
382
383 EXPECT_TRUE(sm->registerForNotifications("asdfasdf", cb).isOk());
384 EXPECT_TRUE(sm->addService("asdfasdf", service,
385 false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
386
387 EXPECT_THAT(cb->registrations, ElementsAre("asdfasdf"));
388 EXPECT_THAT(cb->binders, ElementsAre(service));
389}
390
391TEST(ServiceNotifications, GetNotificationForAlreadyRegisteredService) {
392 auto sm = getPermissiveServiceManager();
393
394 sp<CallbackHistorian> cb = new CallbackHistorian;
395
396 sp<IBinder> service = getBinder();
397
398 EXPECT_TRUE(sm->addService("asdfasdf", service,
399 false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
400
401 EXPECT_TRUE(sm->registerForNotifications("asdfasdf", cb).isOk());
402
403 EXPECT_THAT(cb->registrations, ElementsAre("asdfasdf"));
404 EXPECT_THAT(cb->binders, ElementsAre(service));
405}
406
407TEST(ServiceNotifications, GetMultipleNotification) {
408 auto sm = getPermissiveServiceManager();
409
410 sp<CallbackHistorian> cb = new CallbackHistorian;
411
412 sp<IBinder> binder1 = getBinder();
413 sp<IBinder> binder2 = getBinder();
414
415 EXPECT_TRUE(sm->registerForNotifications("asdfasdf", cb).isOk());
416 EXPECT_TRUE(sm->addService("asdfasdf", binder1,
417 false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
418 EXPECT_TRUE(sm->addService("asdfasdf", binder2,
419 false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
420
421 EXPECT_THAT(cb->registrations, ElementsAre("asdfasdf", "asdfasdf"));
422 EXPECT_THAT(cb->registrations, ElementsAre("asdfasdf", "asdfasdf"));
423}