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