1 /*
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 <android-base/properties.h>
18 #include <android-base/strings.h>
19 #include <android/os/BnServiceCallback.h>
20 #include <binder/Binder.h>
21 #include <binder/IServiceManager.h>
22 #include <binder/ProcessState.h>
23 #include <cutils/android_filesystem_config.h>
24 #include <gmock/gmock.h>
25 #include <gtest/gtest.h>
26
27 #include "Access.h"
28 #include "ServiceManager.h"
29
30 using android::Access;
31 using android::BBinder;
32 using android::IBinder;
33 using android::ServiceManager;
34 using android::sp;
35 using android::base::EndsWith;
36 using android::base::GetProperty;
37 using android::base::StartsWith;
38 using android::binder::Status;
39 using android::os::BnServiceCallback;
40 using android::os::IServiceManager;
41 using testing::_;
42 using testing::ElementsAre;
43 using testing::NiceMock;
44 using testing::Return;
45
getBinder()46 static sp<IBinder> getBinder() {
47 class LinkableBinder : public BBinder {
48 android::status_t linkToDeath(const sp<DeathRecipient>&, void*, uint32_t) override {
49 // let SM linkToDeath
50 return android::OK;
51 }
52 };
53
54 return sp<LinkableBinder>::make();
55 }
56
57 class MockAccess : public Access {
58 public:
59 MOCK_METHOD0(getCallingContext, CallingContext());
60 MOCK_METHOD2(canAdd, bool(const CallingContext&, const std::string& name));
61 MOCK_METHOD2(canFind, bool(const CallingContext&, const std::string& name));
62 MOCK_METHOD1(canList, bool(const CallingContext&));
63 };
64
65 class MockServiceManager : public ServiceManager {
66 public:
MockServiceManager(std::unique_ptr<Access> && access)67 MockServiceManager(std::unique_ptr<Access>&& access) : ServiceManager(std::move(access)) {}
68 MOCK_METHOD2(tryStartService, void(const Access::CallingContext&, const std::string& name));
69 };
70
getPermissiveServiceManager()71 static sp<ServiceManager> getPermissiveServiceManager() {
72 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
73
74 ON_CALL(*access, getCallingContext()).WillByDefault(Return(Access::CallingContext{}));
75 ON_CALL(*access, canAdd(_, _)).WillByDefault(Return(true));
76 ON_CALL(*access, canFind(_, _)).WillByDefault(Return(true));
77 ON_CALL(*access, canList(_)).WillByDefault(Return(true));
78
79 sp<ServiceManager> sm = sp<NiceMock<MockServiceManager>>::make(std::move(access));
80 return sm;
81 }
82
83 // Determines if test device is a cuttlefish phone device
isCuttlefishPhone()84 static bool isCuttlefishPhone() {
85 auto device = GetProperty("ro.product.vendor.device", "");
86 auto product = GetProperty("ro.product.vendor.name", "");
87 return StartsWith(device, "vsoc_") && EndsWith(product, "_phone");
88 }
89
TEST(AddService,HappyHappy)90 TEST(AddService, HappyHappy) {
91 auto sm = getPermissiveServiceManager();
92 EXPECT_TRUE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
93 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
94 }
95
TEST(AddService,EmptyNameDisallowed)96 TEST(AddService, EmptyNameDisallowed) {
97 auto sm = getPermissiveServiceManager();
98 EXPECT_FALSE(sm->addService("", getBinder(), false /*allowIsolated*/,
99 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
100 }
101
TEST(AddService,JustShortEnoughServiceNameHappy)102 TEST(AddService, JustShortEnoughServiceNameHappy) {
103 auto sm = getPermissiveServiceManager();
104 EXPECT_TRUE(sm->addService(std::string(127, 'a'), getBinder(), false /*allowIsolated*/,
105 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
106 }
107
TEST(AddService,TooLongNameDisallowed)108 TEST(AddService, TooLongNameDisallowed) {
109 auto sm = getPermissiveServiceManager();
110 EXPECT_FALSE(sm->addService(std::string(128, 'a'), getBinder(), false /*allowIsolated*/,
111 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
112 }
113
TEST(AddService,WeirdCharactersDisallowed)114 TEST(AddService, WeirdCharactersDisallowed) {
115 auto sm = getPermissiveServiceManager();
116 EXPECT_FALSE(sm->addService("happy$foo$foo", getBinder(), false /*allowIsolated*/,
117 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
118 }
119
TEST(AddService,AddNullServiceDisallowed)120 TEST(AddService, AddNullServiceDisallowed) {
121 auto sm = getPermissiveServiceManager();
122 EXPECT_FALSE(sm->addService("foo", nullptr, false /*allowIsolated*/,
123 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
124 }
125
TEST(AddService,AddDisallowedFromApp)126 TEST(AddService, AddDisallowedFromApp) {
127 for (uid_t uid : { AID_APP_START, AID_APP_START + 1, AID_APP_END }) {
128 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
129 EXPECT_CALL(*access, getCallingContext()).WillOnce(Return(Access::CallingContext{
130 .debugPid = 1337,
131 .uid = uid,
132 }));
133 EXPECT_CALL(*access, canAdd(_, _)).Times(0);
134 sp<ServiceManager> sm = sp<NiceMock<MockServiceManager>>::make(std::move(access));
135
136 EXPECT_FALSE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
137 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
138 }
139
140 }
141
TEST(AddService,HappyOverExistingService)142 TEST(AddService, HappyOverExistingService) {
143 auto sm = getPermissiveServiceManager();
144 EXPECT_TRUE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
145 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
146 EXPECT_TRUE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
147 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
148 }
149
TEST(AddService,OverwriteExistingService)150 TEST(AddService, OverwriteExistingService) {
151 auto sm = getPermissiveServiceManager();
152 sp<IBinder> serviceA = getBinder();
153 EXPECT_TRUE(sm->addService("foo", serviceA, false /*allowIsolated*/,
154 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
155
156 sp<IBinder> outA;
157 EXPECT_TRUE(sm->getService("foo", &outA).isOk());
158 EXPECT_EQ(serviceA, outA);
159
160 // serviceA should be overwritten by serviceB
161 sp<IBinder> serviceB = getBinder();
162 EXPECT_TRUE(sm->addService("foo", serviceB, false /*allowIsolated*/,
163 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
164
165 sp<IBinder> outB;
166 EXPECT_TRUE(sm->getService("foo", &outB).isOk());
167 EXPECT_EQ(serviceB, outB);
168 }
169
TEST(AddService,NoPermissions)170 TEST(AddService, NoPermissions) {
171 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
172
173 EXPECT_CALL(*access, getCallingContext()).WillOnce(Return(Access::CallingContext{}));
174 EXPECT_CALL(*access, canAdd(_, _)).WillOnce(Return(false));
175
176 sp<ServiceManager> sm = sp<NiceMock<MockServiceManager>>::make(std::move(access));
177
178 EXPECT_FALSE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
179 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
180 }
181
TEST(GetService,HappyHappy)182 TEST(GetService, HappyHappy) {
183 auto sm = getPermissiveServiceManager();
184 sp<IBinder> service = getBinder();
185
186 EXPECT_TRUE(sm->addService("foo", service, false /*allowIsolated*/,
187 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
188
189 sp<IBinder> out;
190 EXPECT_TRUE(sm->getService("foo", &out).isOk());
191 EXPECT_EQ(service, out);
192 }
193
TEST(GetService,NonExistant)194 TEST(GetService, NonExistant) {
195 auto sm = getPermissiveServiceManager();
196
197 sp<IBinder> out;
198 EXPECT_TRUE(sm->getService("foo", &out).isOk());
199 EXPECT_EQ(nullptr, out.get());
200 }
201
TEST(GetService,NoPermissionsForGettingService)202 TEST(GetService, NoPermissionsForGettingService) {
203 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
204
205 EXPECT_CALL(*access, getCallingContext()).WillRepeatedly(Return(Access::CallingContext{}));
206 EXPECT_CALL(*access, canAdd(_, _)).WillOnce(Return(true));
207 EXPECT_CALL(*access, canFind(_, _)).WillOnce(Return(false));
208
209 sp<ServiceManager> sm = sp<NiceMock<MockServiceManager>>::make(std::move(access));
210
211 EXPECT_TRUE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
212 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
213
214 sp<IBinder> out;
215 // returns nullptr but has OK status for legacy compatibility
216 EXPECT_TRUE(sm->getService("foo", &out).isOk());
217 EXPECT_EQ(nullptr, out.get());
218 }
219
TEST(GetService,AllowedFromIsolated)220 TEST(GetService, AllowedFromIsolated) {
221 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
222
223 EXPECT_CALL(*access, getCallingContext())
224 // something adds it
225 .WillOnce(Return(Access::CallingContext{}))
226 // next call is from isolated app
227 .WillOnce(Return(Access::CallingContext{
228 .uid = AID_ISOLATED_START,
229 }));
230 EXPECT_CALL(*access, canAdd(_, _)).WillOnce(Return(true));
231 EXPECT_CALL(*access, canFind(_, _)).WillOnce(Return(true));
232
233 sp<ServiceManager> sm = sp<NiceMock<MockServiceManager>>::make(std::move(access));
234
235 sp<IBinder> service = getBinder();
236 EXPECT_TRUE(sm->addService("foo", service, true /*allowIsolated*/,
237 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
238
239 sp<IBinder> out;
240 EXPECT_TRUE(sm->getService("foo", &out).isOk());
241 EXPECT_EQ(service, out.get());
242 }
243
TEST(GetService,NotAllowedFromIsolated)244 TEST(GetService, NotAllowedFromIsolated) {
245 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
246
247 EXPECT_CALL(*access, getCallingContext())
248 // something adds it
249 .WillOnce(Return(Access::CallingContext{}))
250 // next call is from isolated app
251 .WillOnce(Return(Access::CallingContext{
252 .uid = AID_ISOLATED_START,
253 }));
254 EXPECT_CALL(*access, canAdd(_, _)).WillOnce(Return(true));
255
256 // TODO(b/136023468): when security check is first, this should be called first
257 // EXPECT_CALL(*access, canFind(_, _)).WillOnce(Return(true));
258
259 sp<ServiceManager> sm = sp<NiceMock<MockServiceManager>>::make(std::move(access));
260
261 EXPECT_TRUE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
262 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
263
264 sp<IBinder> out;
265 // returns nullptr but has OK status for legacy compatibility
266 EXPECT_TRUE(sm->getService("foo", &out).isOk());
267 EXPECT_EQ(nullptr, out.get());
268 }
269
TEST(ListServices,NoPermissions)270 TEST(ListServices, NoPermissions) {
271 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
272
273 EXPECT_CALL(*access, getCallingContext()).WillOnce(Return(Access::CallingContext{}));
274 EXPECT_CALL(*access, canList(_)).WillOnce(Return(false));
275
276 sp<ServiceManager> sm = sp<NiceMock<MockServiceManager>>::make(std::move(access));
277
278 std::vector<std::string> out;
279 EXPECT_FALSE(sm->listServices(IServiceManager::DUMP_FLAG_PRIORITY_ALL, &out).isOk());
280 EXPECT_TRUE(out.empty());
281 }
282
TEST(ListServices,AllServices)283 TEST(ListServices, AllServices) {
284 auto sm = getPermissiveServiceManager();
285
286 EXPECT_TRUE(sm->addService("sd", getBinder(), false /*allowIsolated*/,
287 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
288 EXPECT_TRUE(sm->addService("sc", getBinder(), false /*allowIsolated*/,
289 IServiceManager::DUMP_FLAG_PRIORITY_NORMAL).isOk());
290 EXPECT_TRUE(sm->addService("sb", getBinder(), false /*allowIsolated*/,
291 IServiceManager::DUMP_FLAG_PRIORITY_HIGH).isOk());
292 EXPECT_TRUE(sm->addService("sa", getBinder(), false /*allowIsolated*/,
293 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL).isOk());
294
295 std::vector<std::string> out;
296 EXPECT_TRUE(sm->listServices(IServiceManager::DUMP_FLAG_PRIORITY_ALL, &out).isOk());
297
298 // all there and in the right order
299 EXPECT_THAT(out, ElementsAre("sa", "sb", "sc", "sd"));
300 }
301
TEST(ListServices,CriticalServices)302 TEST(ListServices, CriticalServices) {
303 auto sm = getPermissiveServiceManager();
304
305 EXPECT_TRUE(sm->addService("sd", getBinder(), false /*allowIsolated*/,
306 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
307 EXPECT_TRUE(sm->addService("sc", getBinder(), false /*allowIsolated*/,
308 IServiceManager::DUMP_FLAG_PRIORITY_NORMAL).isOk());
309 EXPECT_TRUE(sm->addService("sb", getBinder(), false /*allowIsolated*/,
310 IServiceManager::DUMP_FLAG_PRIORITY_HIGH).isOk());
311 EXPECT_TRUE(sm->addService("sa", getBinder(), false /*allowIsolated*/,
312 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL).isOk());
313
314 std::vector<std::string> out;
315 EXPECT_TRUE(sm->listServices(IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL, &out).isOk());
316
317 // all there and in the right order
318 EXPECT_THAT(out, ElementsAre("sa"));
319 }
320
TEST(Vintf,UpdatableViaApex)321 TEST(Vintf, UpdatableViaApex) {
322 if (!isCuttlefishPhone()) GTEST_SKIP() << "Skipping non-Cuttlefish-phone devices";
323
324 auto sm = getPermissiveServiceManager();
325 std::optional<std::string> updatableViaApex;
326 EXPECT_TRUE(sm->updatableViaApex("android.hardware.camera.provider.ICameraProvider/internal/0",
327 &updatableViaApex)
328 .isOk());
329 EXPECT_EQ(std::make_optional<std::string>("com.google.emulated.camera.provider.hal"),
330 updatableViaApex);
331 }
332
TEST(Vintf,UpdatableViaApex_InvalidNameReturnsNullOpt)333 TEST(Vintf, UpdatableViaApex_InvalidNameReturnsNullOpt) {
334 if (!isCuttlefishPhone()) GTEST_SKIP() << "Skipping non-Cuttlefish-phone devices";
335
336 auto sm = getPermissiveServiceManager();
337 std::optional<std::string> updatableViaApex;
338 EXPECT_TRUE(sm->updatableViaApex("android.hardware.camera.provider.ICameraProvider",
339 &updatableViaApex)
340 .isOk()); // missing instance name
341 EXPECT_EQ(std::nullopt, updatableViaApex);
342 }
343
TEST(Vintf,GetUpdatableNames)344 TEST(Vintf, GetUpdatableNames) {
345 if (!isCuttlefishPhone()) GTEST_SKIP() << "Skipping non-Cuttlefish-phone devices";
346
347 auto sm = getPermissiveServiceManager();
348 std::vector<std::string> names;
349 EXPECT_TRUE(sm->getUpdatableNames("com.google.emulated.camera.provider.hal", &names).isOk());
350 EXPECT_EQ(std::vector<
351 std::string>{"android.hardware.camera.provider.ICameraProvider/internal/0"},
352 names);
353 }
354
TEST(Vintf,GetUpdatableNames_InvalidApexNameReturnsEmpty)355 TEST(Vintf, GetUpdatableNames_InvalidApexNameReturnsEmpty) {
356 if (!isCuttlefishPhone()) GTEST_SKIP() << "Skipping non-Cuttlefish-phone devices";
357
358 auto sm = getPermissiveServiceManager();
359 std::vector<std::string> names;
360 EXPECT_TRUE(sm->getUpdatableNames("non.existing.apex.name", &names).isOk());
361 EXPECT_EQ(std::vector<std::string>{}, names);
362 }
363
TEST(Vintf,IsDeclared_native)364 TEST(Vintf, IsDeclared_native) {
365 if (!isCuttlefishPhone()) GTEST_SKIP() << "Skipping non-Cuttlefish-phone devices";
366
367 auto sm = getPermissiveServiceManager();
368 bool declared = false;
369 EXPECT_TRUE(sm->isDeclared("mapper/minigbm", &declared).isOk());
370 EXPECT_TRUE(declared);
371 }
372
TEST(Vintf,GetDeclaredInstances_native)373 TEST(Vintf, GetDeclaredInstances_native) {
374 if (!isCuttlefishPhone()) GTEST_SKIP() << "Skipping non-Cuttlefish-phone devices";
375
376 auto sm = getPermissiveServiceManager();
377 std::vector<std::string> instances;
378 EXPECT_TRUE(sm->getDeclaredInstances("mapper", &instances).isOk());
379 EXPECT_EQ(std::vector<std::string>{"minigbm"}, instances);
380 }
381
382 class CallbackHistorian : public BnServiceCallback {
onRegistration(const std::string & name,const sp<IBinder> & binder)383 Status onRegistration(const std::string& name, const sp<IBinder>& binder) override {
384 registrations.push_back(name);
385 binders.push_back(binder);
386 return Status::ok();
387 }
388
linkToDeath(const sp<DeathRecipient> &,void *,uint32_t)389 android::status_t linkToDeath(const sp<DeathRecipient>&, void*, uint32_t) override {
390 // let SM linkToDeath
391 return android::OK;
392 }
393
394 public:
395 std::vector<std::string> registrations;
396 std::vector<sp<IBinder>> binders;
397 };
398
TEST(ServiceNotifications,NoPermissionsRegister)399 TEST(ServiceNotifications, NoPermissionsRegister) {
400 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
401
402 EXPECT_CALL(*access, getCallingContext()).WillOnce(Return(Access::CallingContext{}));
403 EXPECT_CALL(*access, canFind(_,_)).WillOnce(Return(false));
404
405 sp<ServiceManager> sm = sp<ServiceManager>::make(std::move(access));
406
407 sp<CallbackHistorian> cb = sp<CallbackHistorian>::make();
408
409 EXPECT_EQ(sm->registerForNotifications("foofoo", cb).exceptionCode(), Status::EX_SECURITY);
410 }
411
TEST(GetService,IsolatedCantRegister)412 TEST(GetService, IsolatedCantRegister) {
413 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
414
415 EXPECT_CALL(*access, getCallingContext())
416 .WillOnce(Return(Access::CallingContext{
417 .uid = AID_ISOLATED_START,
418 }));
419 EXPECT_CALL(*access, canFind(_, _)).WillOnce(Return(true));
420
421 sp<ServiceManager> sm = sp<ServiceManager>::make(std::move(access));
422
423 sp<CallbackHistorian> cb = sp<CallbackHistorian>::make();
424
425 EXPECT_EQ(sm->registerForNotifications("foofoo", cb).exceptionCode(),
426 Status::EX_SECURITY);
427 }
428
TEST(ServiceNotifications,NoPermissionsUnregister)429 TEST(ServiceNotifications, NoPermissionsUnregister) {
430 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
431
432 EXPECT_CALL(*access, getCallingContext()).WillOnce(Return(Access::CallingContext{}));
433 EXPECT_CALL(*access, canFind(_,_)).WillOnce(Return(false));
434
435 sp<ServiceManager> sm = sp<ServiceManager>::make(std::move(access));
436
437 sp<CallbackHistorian> cb = sp<CallbackHistorian>::make();
438
439 // should always hit security error first
440 EXPECT_EQ(sm->unregisterForNotifications("foofoo", cb).exceptionCode(),
441 Status::EX_SECURITY);
442 }
443
TEST(ServiceNotifications,InvalidName)444 TEST(ServiceNotifications, InvalidName) {
445 auto sm = getPermissiveServiceManager();
446
447 sp<CallbackHistorian> cb = sp<CallbackHistorian>::make();
448
449 EXPECT_EQ(sm->registerForNotifications("foo@foo", cb).exceptionCode(),
450 Status::EX_ILLEGAL_ARGUMENT);
451 }
452
TEST(ServiceNotifications,NullCallback)453 TEST(ServiceNotifications, NullCallback) {
454 auto sm = getPermissiveServiceManager();
455
456 EXPECT_EQ(sm->registerForNotifications("foofoo", nullptr).exceptionCode(),
457 Status::EX_NULL_POINTER);
458 }
459
TEST(ServiceNotifications,Unregister)460 TEST(ServiceNotifications, Unregister) {
461 auto sm = getPermissiveServiceManager();
462
463 sp<CallbackHistorian> cb = sp<CallbackHistorian>::make();
464
465 EXPECT_TRUE(sm->registerForNotifications("foofoo", cb).isOk());
466 EXPECT_EQ(sm->unregisterForNotifications("foofoo", cb).exceptionCode(), 0);
467 }
468
TEST(ServiceNotifications,UnregisterWhenNoRegistrationExists)469 TEST(ServiceNotifications, UnregisterWhenNoRegistrationExists) {
470 auto sm = getPermissiveServiceManager();
471
472 sp<CallbackHistorian> cb = sp<CallbackHistorian>::make();
473
474 EXPECT_EQ(sm->unregisterForNotifications("foofoo", cb).exceptionCode(),
475 Status::EX_ILLEGAL_STATE);
476 }
477
TEST(ServiceNotifications,NoNotification)478 TEST(ServiceNotifications, NoNotification) {
479 auto sm = getPermissiveServiceManager();
480
481 sp<CallbackHistorian> cb = sp<CallbackHistorian>::make();
482
483 EXPECT_TRUE(sm->registerForNotifications("foofoo", cb).isOk());
484 EXPECT_TRUE(sm->addService("otherservice", getBinder(),
485 false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
486
487 EXPECT_THAT(cb->registrations, ElementsAre());
488 EXPECT_THAT(cb->binders, ElementsAre());
489 }
490
TEST(ServiceNotifications,GetNotification)491 TEST(ServiceNotifications, GetNotification) {
492 auto sm = getPermissiveServiceManager();
493
494 sp<CallbackHistorian> cb = sp<CallbackHistorian>::make();
495
496 sp<IBinder> service = getBinder();
497
498 EXPECT_TRUE(sm->registerForNotifications("asdfasdf", cb).isOk());
499 EXPECT_TRUE(sm->addService("asdfasdf", service,
500 false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
501
502 EXPECT_THAT(cb->registrations, ElementsAre("asdfasdf"));
503 EXPECT_THAT(cb->binders, ElementsAre(service));
504 }
505
TEST(ServiceNotifications,GetNotificationForAlreadyRegisteredService)506 TEST(ServiceNotifications, GetNotificationForAlreadyRegisteredService) {
507 auto sm = getPermissiveServiceManager();
508
509 sp<CallbackHistorian> cb = sp<CallbackHistorian>::make();
510
511 sp<IBinder> service = getBinder();
512
513 EXPECT_TRUE(sm->addService("asdfasdf", service,
514 false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
515
516 EXPECT_TRUE(sm->registerForNotifications("asdfasdf", cb).isOk());
517
518 EXPECT_THAT(cb->registrations, ElementsAre("asdfasdf"));
519 EXPECT_THAT(cb->binders, ElementsAre(service));
520 }
521
TEST(ServiceNotifications,GetMultipleNotification)522 TEST(ServiceNotifications, GetMultipleNotification) {
523 auto sm = getPermissiveServiceManager();
524
525 sp<CallbackHistorian> cb = sp<CallbackHistorian>::make();
526
527 sp<IBinder> binder1 = getBinder();
528 sp<IBinder> binder2 = getBinder();
529
530 EXPECT_TRUE(sm->registerForNotifications("asdfasdf", cb).isOk());
531 EXPECT_TRUE(sm->addService("asdfasdf", binder1,
532 false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
533 EXPECT_TRUE(sm->addService("asdfasdf", binder2,
534 false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
535
536 EXPECT_THAT(cb->registrations, ElementsAre("asdfasdf", "asdfasdf"));
537 EXPECT_THAT(cb->registrations, ElementsAre("asdfasdf", "asdfasdf"));
538 }
539