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