blob: 9d22641ac543b63de8cdf80492662abcbe963815 [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
Jooyung Hance94b752022-11-14 18:55:06 +090017#include <android-base/properties.h>
18#include <android-base/strings.h>
Steven Moreland27cfab02019-08-12 14:34:16 -070019#include <android/os/BnServiceCallback.h>
20#include <binder/Binder.h>
Steven Moreland27cfab02019-08-12 14:34:16 -070021#include <binder/IServiceManager.h>
Jooyung Hance94b752022-11-14 18:55:06 +090022#include <binder/ProcessState.h>
Steven Moreland80e1e6d2019-06-21 12:35:59 -070023#include <cutils/android_filesystem_config.h>
Steven Moreland80e1e6d2019-06-21 12:35:59 -070024#include <gmock/gmock.h>
Jooyung Hance94b752022-11-14 18:55:06 +090025#include <gtest/gtest.h>
Steven Moreland80e1e6d2019-06-21 12:35:59 -070026
27#include "Access.h"
28#include "ServiceManager.h"
29
Steven Moreland80e1e6d2019-06-21 12:35:59 -070030using android::Access;
Steven Moreland27cfab02019-08-12 14:34:16 -070031using android::BBinder;
Steven Moreland80e1e6d2019-06-21 12:35:59 -070032using android::IBinder;
33using android::ServiceManager;
Jooyung Hande196982023-05-08 15:01:12 +090034using android::sp;
35using android::base::EndsWith;
36using android::base::GetProperty;
37using android::base::StartsWith;
Steven Moreland27cfab02019-08-12 14:34:16 -070038using android::binder::Status;
39using android::os::BnServiceCallback;
Steven Moreland80e1e6d2019-06-21 12:35:59 -070040using android::os::IServiceManager;
Alice Wang8578f132024-05-03 09:01:56 +000041using android::os::Service;
Steven Moreland80e1e6d2019-06-21 12:35:59 -070042using testing::_;
43using testing::ElementsAre;
44using testing::NiceMock;
45using testing::Return;
46
47static sp<IBinder> getBinder() {
Steven Moreland27cfab02019-08-12 14:34:16 -070048 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
Steven Morelandb0983182021-04-02 03:14:04 +000055 return sp<LinkableBinder>::make();
Steven Moreland80e1e6d2019-06-21 12:35:59 -070056}
57
58class MockAccess : public Access {
59public:
Steven Morelanda9fe4742019-07-18 14:45:20 -070060 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));
Steven Moreland80e1e6d2019-06-21 12:35:59 -070063 MOCK_METHOD1(canList, bool(const CallingContext&));
64};
65
Jon Spivack0d844302019-07-22 18:40:34 -070066class MockServiceManager : public ServiceManager {
67 public:
68 MockServiceManager(std::unique_ptr<Access>&& access) : ServiceManager(std::move(access)) {}
Steven Morelandaa33e852023-05-10 16:42:15 +000069 MOCK_METHOD2(tryStartService, void(const Access::CallingContext&, const std::string& name));
Jon Spivack0d844302019-07-22 18:40:34 -070070};
71
Steven Moreland80e1e6d2019-06-21 12:35:59 -070072static sp<ServiceManager> getPermissiveServiceManager() {
73 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
74
Steven Morelanda9fe4742019-07-18 14:45:20 -070075 ON_CALL(*access, getCallingContext()).WillByDefault(Return(Access::CallingContext{}));
76 ON_CALL(*access, canAdd(_, _)).WillByDefault(Return(true));
77 ON_CALL(*access, canFind(_, _)).WillByDefault(Return(true));
Steven Moreland80e1e6d2019-06-21 12:35:59 -070078 ON_CALL(*access, canList(_)).WillByDefault(Return(true));
79
Steven Morelandb0983182021-04-02 03:14:04 +000080 sp<ServiceManager> sm = sp<NiceMock<MockServiceManager>>::make(std::move(access));
Steven Moreland80e1e6d2019-06-21 12:35:59 -070081 return sm;
82}
83
Jooyung Hande196982023-05-08 15:01:12 +090084// Determines if test device is a cuttlefish phone device
85static 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");
Jooyung Hance94b752022-11-14 18:55:06 +090089}
90
Steven Moreland80e1e6d2019-06-21 12:35:59 -070091TEST(AddService, HappyHappy) {
92 auto sm = getPermissiveServiceManager();
93 EXPECT_TRUE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
94 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
95}
96
97TEST(AddService, EmptyNameDisallowed) {
98 auto sm = getPermissiveServiceManager();
99 EXPECT_FALSE(sm->addService("", getBinder(), false /*allowIsolated*/,
100 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
101}
102
103TEST(AddService, JustShortEnoughServiceNameHappy) {
104 auto sm = getPermissiveServiceManager();
105 EXPECT_TRUE(sm->addService(std::string(127, 'a'), getBinder(), false /*allowIsolated*/,
106 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
107}
108
109TEST(AddService, TooLongNameDisallowed) {
110 auto sm = getPermissiveServiceManager();
111 EXPECT_FALSE(sm->addService(std::string(128, 'a'), getBinder(), false /*allowIsolated*/,
112 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
113}
114
Steven Moreland905e2e82019-07-17 11:05:45 -0700115TEST(AddService, WeirdCharactersDisallowed) {
116 auto sm = getPermissiveServiceManager();
117 EXPECT_FALSE(sm->addService("happy$foo$foo", getBinder(), false /*allowIsolated*/,
118 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
119}
120
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700121TEST(AddService, AddNullServiceDisallowed) {
122 auto sm = getPermissiveServiceManager();
123 EXPECT_FALSE(sm->addService("foo", nullptr, false /*allowIsolated*/,
124 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
125}
126
127TEST(AddService, AddDisallowedFromApp) {
128 for (uid_t uid : { AID_APP_START, AID_APP_START + 1, AID_APP_END }) {
129 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
Steven Morelanda9fe4742019-07-18 14:45:20 -0700130 EXPECT_CALL(*access, getCallingContext()).WillOnce(Return(Access::CallingContext{
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700131 .debugPid = 1337,
132 .uid = uid,
133 }));
Steven Morelanda9fe4742019-07-18 14:45:20 -0700134 EXPECT_CALL(*access, canAdd(_, _)).Times(0);
Steven Morelandb0983182021-04-02 03:14:04 +0000135 sp<ServiceManager> sm = sp<NiceMock<MockServiceManager>>::make(std::move(access));
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700136
137 EXPECT_FALSE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
138 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
139 }
140
141}
142
143TEST(AddService, HappyOverExistingService) {
144 auto sm = getPermissiveServiceManager();
145 EXPECT_TRUE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
146 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
147 EXPECT_TRUE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
148 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
149}
150
Devin Moore05ffe522020-08-06 13:58:29 -0700151TEST(AddService, OverwriteExistingService) {
152 auto sm = getPermissiveServiceManager();
153 sp<IBinder> serviceA = getBinder();
154 EXPECT_TRUE(sm->addService("foo", serviceA, false /*allowIsolated*/,
155 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
156
Alice Wang8578f132024-05-03 09:01:56 +0000157 Service outA;
Devin Moore05ffe522020-08-06 13:58:29 -0700158 EXPECT_TRUE(sm->getService("foo", &outA).isOk());
Alice Wang8578f132024-05-03 09:01:56 +0000159 EXPECT_EQ(serviceA, outA.get<Service::Tag::binder>());
Devin Moore05ffe522020-08-06 13:58:29 -0700160
161 // serviceA should be overwritten by serviceB
162 sp<IBinder> serviceB = getBinder();
163 EXPECT_TRUE(sm->addService("foo", serviceB, false /*allowIsolated*/,
164 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
165
Alice Wang8578f132024-05-03 09:01:56 +0000166 Service outB;
Devin Moore05ffe522020-08-06 13:58:29 -0700167 EXPECT_TRUE(sm->getService("foo", &outB).isOk());
Alice Wang8578f132024-05-03 09:01:56 +0000168 EXPECT_EQ(serviceB, outB.get<Service::Tag::binder>());
Devin Moore05ffe522020-08-06 13:58:29 -0700169}
170
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700171TEST(AddService, NoPermissions) {
172 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
173
Steven Morelanda9fe4742019-07-18 14:45:20 -0700174 EXPECT_CALL(*access, getCallingContext()).WillOnce(Return(Access::CallingContext{}));
175 EXPECT_CALL(*access, canAdd(_, _)).WillOnce(Return(false));
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700176
Steven Morelandb0983182021-04-02 03:14:04 +0000177 sp<ServiceManager> sm = sp<NiceMock<MockServiceManager>>::make(std::move(access));
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700178
179 EXPECT_FALSE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
180 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
181}
182
183TEST(GetService, HappyHappy) {
184 auto sm = getPermissiveServiceManager();
Steven Moreland27cfab02019-08-12 14:34:16 -0700185 sp<IBinder> service = getBinder();
186
187 EXPECT_TRUE(sm->addService("foo", service, false /*allowIsolated*/,
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700188 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
189
Alice Wang8578f132024-05-03 09:01:56 +0000190 Service out;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700191 EXPECT_TRUE(sm->getService("foo", &out).isOk());
Alice Wang8578f132024-05-03 09:01:56 +0000192 EXPECT_EQ(service, out.get<Service::Tag::binder>());
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700193}
194
195TEST(GetService, NonExistant) {
196 auto sm = getPermissiveServiceManager();
197
Alice Wang8578f132024-05-03 09:01:56 +0000198 Service out;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700199 EXPECT_TRUE(sm->getService("foo", &out).isOk());
Alice Wang8578f132024-05-03 09:01:56 +0000200 EXPECT_EQ(nullptr, out.get<Service::Tag::binder>());
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700201}
202
203TEST(GetService, NoPermissionsForGettingService) {
204 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
205
Steven Morelanda9fe4742019-07-18 14:45:20 -0700206 EXPECT_CALL(*access, getCallingContext()).WillRepeatedly(Return(Access::CallingContext{}));
207 EXPECT_CALL(*access, canAdd(_, _)).WillOnce(Return(true));
208 EXPECT_CALL(*access, canFind(_, _)).WillOnce(Return(false));
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700209
Steven Morelandb0983182021-04-02 03:14:04 +0000210 sp<ServiceManager> sm = sp<NiceMock<MockServiceManager>>::make(std::move(access));
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700211
212 EXPECT_TRUE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
213 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
214
Alice Wang8578f132024-05-03 09:01:56 +0000215 Service out;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700216 // returns nullptr but has OK status for legacy compatibility
217 EXPECT_TRUE(sm->getService("foo", &out).isOk());
Alice Wang8578f132024-05-03 09:01:56 +0000218 EXPECT_EQ(nullptr, out.get<Service::Tag::binder>());
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700219}
220
221TEST(GetService, AllowedFromIsolated) {
222 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
223
Steven Morelanda9fe4742019-07-18 14:45:20 -0700224 EXPECT_CALL(*access, getCallingContext())
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700225 // something adds it
226 .WillOnce(Return(Access::CallingContext{}))
227 // next call is from isolated app
228 .WillOnce(Return(Access::CallingContext{
229 .uid = AID_ISOLATED_START,
230 }));
Steven Morelanda9fe4742019-07-18 14:45:20 -0700231 EXPECT_CALL(*access, canAdd(_, _)).WillOnce(Return(true));
232 EXPECT_CALL(*access, canFind(_, _)).WillOnce(Return(true));
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700233
Steven Morelandb0983182021-04-02 03:14:04 +0000234 sp<ServiceManager> sm = sp<NiceMock<MockServiceManager>>::make(std::move(access));
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700235
Steven Moreland27cfab02019-08-12 14:34:16 -0700236 sp<IBinder> service = getBinder();
237 EXPECT_TRUE(sm->addService("foo", service, true /*allowIsolated*/,
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700238 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
239
Alice Wang8578f132024-05-03 09:01:56 +0000240 Service out;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700241 EXPECT_TRUE(sm->getService("foo", &out).isOk());
Alice Wang8578f132024-05-03 09:01:56 +0000242 EXPECT_EQ(service, out.get<Service::Tag::binder>());
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700243}
244
245TEST(GetService, NotAllowedFromIsolated) {
246 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
247
Steven Morelanda9fe4742019-07-18 14:45:20 -0700248 EXPECT_CALL(*access, getCallingContext())
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700249 // something adds it
250 .WillOnce(Return(Access::CallingContext{}))
251 // next call is from isolated app
252 .WillOnce(Return(Access::CallingContext{
253 .uid = AID_ISOLATED_START,
254 }));
Steven Morelanda9fe4742019-07-18 14:45:20 -0700255 EXPECT_CALL(*access, canAdd(_, _)).WillOnce(Return(true));
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700256
257 // TODO(b/136023468): when security check is first, this should be called first
Steven Morelanda9fe4742019-07-18 14:45:20 -0700258 // EXPECT_CALL(*access, canFind(_, _)).WillOnce(Return(true));
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700259
Steven Morelandb0983182021-04-02 03:14:04 +0000260 sp<ServiceManager> sm = sp<NiceMock<MockServiceManager>>::make(std::move(access));
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700261
262 EXPECT_TRUE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
263 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
264
Alice Wang8578f132024-05-03 09:01:56 +0000265 Service out;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700266 // returns nullptr but has OK status for legacy compatibility
267 EXPECT_TRUE(sm->getService("foo", &out).isOk());
Alice Wang8578f132024-05-03 09:01:56 +0000268 EXPECT_EQ(nullptr, out.get<Service::Tag::binder>());
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700269}
270
271TEST(ListServices, NoPermissions) {
272 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
273
Steven Morelanda9fe4742019-07-18 14:45:20 -0700274 EXPECT_CALL(*access, getCallingContext()).WillOnce(Return(Access::CallingContext{}));
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700275 EXPECT_CALL(*access, canList(_)).WillOnce(Return(false));
276
Steven Morelandb0983182021-04-02 03:14:04 +0000277 sp<ServiceManager> sm = sp<NiceMock<MockServiceManager>>::make(std::move(access));
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700278
279 std::vector<std::string> out;
280 EXPECT_FALSE(sm->listServices(IServiceManager::DUMP_FLAG_PRIORITY_ALL, &out).isOk());
281 EXPECT_TRUE(out.empty());
282}
283
284TEST(ListServices, AllServices) {
285 auto sm = getPermissiveServiceManager();
286
287 EXPECT_TRUE(sm->addService("sd", getBinder(), false /*allowIsolated*/,
288 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
289 EXPECT_TRUE(sm->addService("sc", getBinder(), false /*allowIsolated*/,
290 IServiceManager::DUMP_FLAG_PRIORITY_NORMAL).isOk());
291 EXPECT_TRUE(sm->addService("sb", getBinder(), false /*allowIsolated*/,
292 IServiceManager::DUMP_FLAG_PRIORITY_HIGH).isOk());
293 EXPECT_TRUE(sm->addService("sa", getBinder(), false /*allowIsolated*/,
294 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL).isOk());
295
296 std::vector<std::string> out;
297 EXPECT_TRUE(sm->listServices(IServiceManager::DUMP_FLAG_PRIORITY_ALL, &out).isOk());
298
299 // all there and in the right order
300 EXPECT_THAT(out, ElementsAre("sa", "sb", "sc", "sd"));
301}
302
303TEST(ListServices, CriticalServices) {
304 auto sm = getPermissiveServiceManager();
305
306 EXPECT_TRUE(sm->addService("sd", getBinder(), false /*allowIsolated*/,
307 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
308 EXPECT_TRUE(sm->addService("sc", getBinder(), false /*allowIsolated*/,
309 IServiceManager::DUMP_FLAG_PRIORITY_NORMAL).isOk());
310 EXPECT_TRUE(sm->addService("sb", getBinder(), false /*allowIsolated*/,
311 IServiceManager::DUMP_FLAG_PRIORITY_HIGH).isOk());
312 EXPECT_TRUE(sm->addService("sa", getBinder(), false /*allowIsolated*/,
313 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL).isOk());
314
315 std::vector<std::string> out;
316 EXPECT_TRUE(sm->listServices(IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL, &out).isOk());
317
318 // all there and in the right order
319 EXPECT_THAT(out, ElementsAre("sa"));
320}
Steven Moreland27cfab02019-08-12 14:34:16 -0700321
Jooyung Hance94b752022-11-14 18:55:06 +0900322TEST(Vintf, UpdatableViaApex) {
Jooyung Hande196982023-05-08 15:01:12 +0900323 if (!isCuttlefishPhone()) GTEST_SKIP() << "Skipping non-Cuttlefish-phone devices";
Jooyung Hance94b752022-11-14 18:55:06 +0900324
325 auto sm = getPermissiveServiceManager();
326 std::optional<std::string> updatableViaApex;
327 EXPECT_TRUE(sm->updatableViaApex("android.hardware.camera.provider.ICameraProvider/internal/0",
328 &updatableViaApex)
329 .isOk());
330 EXPECT_EQ(std::make_optional<std::string>("com.google.emulated.camera.provider.hal"),
331 updatableViaApex);
332}
333
334TEST(Vintf, UpdatableViaApex_InvalidNameReturnsNullOpt) {
Jooyung Hande196982023-05-08 15:01:12 +0900335 if (!isCuttlefishPhone()) GTEST_SKIP() << "Skipping non-Cuttlefish-phone devices";
Jooyung Hance94b752022-11-14 18:55:06 +0900336
337 auto sm = getPermissiveServiceManager();
338 std::optional<std::string> updatableViaApex;
339 EXPECT_TRUE(sm->updatableViaApex("android.hardware.camera.provider.ICameraProvider",
340 &updatableViaApex)
341 .isOk()); // missing instance name
342 EXPECT_EQ(std::nullopt, updatableViaApex);
343}
344
345TEST(Vintf, GetUpdatableNames) {
Jooyung Hande196982023-05-08 15:01:12 +0900346 if (!isCuttlefishPhone()) GTEST_SKIP() << "Skipping non-Cuttlefish-phone devices";
Jooyung Hance94b752022-11-14 18:55:06 +0900347
348 auto sm = getPermissiveServiceManager();
349 std::vector<std::string> names;
350 EXPECT_TRUE(sm->getUpdatableNames("com.google.emulated.camera.provider.hal", &names).isOk());
351 EXPECT_EQ(std::vector<
352 std::string>{"android.hardware.camera.provider.ICameraProvider/internal/0"},
353 names);
354}
355
356TEST(Vintf, GetUpdatableNames_InvalidApexNameReturnsEmpty) {
Jooyung Hande196982023-05-08 15:01:12 +0900357 if (!isCuttlefishPhone()) GTEST_SKIP() << "Skipping non-Cuttlefish-phone devices";
Jooyung Hance94b752022-11-14 18:55:06 +0900358
359 auto sm = getPermissiveServiceManager();
360 std::vector<std::string> names;
361 EXPECT_TRUE(sm->getUpdatableNames("non.existing.apex.name", &names).isOk());
362 EXPECT_EQ(std::vector<std::string>{}, names);
363}
364
Jooyung Han205e2822023-12-19 16:59:39 +0900365TEST(Vintf, IsDeclared_native) {
366 if (!isCuttlefishPhone()) GTEST_SKIP() << "Skipping non-Cuttlefish-phone devices";
367
368 auto sm = getPermissiveServiceManager();
369 bool declared = false;
370 EXPECT_TRUE(sm->isDeclared("mapper/minigbm", &declared).isOk());
371 EXPECT_TRUE(declared);
372}
373
374TEST(Vintf, GetDeclaredInstances_native) {
375 if (!isCuttlefishPhone()) GTEST_SKIP() << "Skipping non-Cuttlefish-phone devices";
376
377 auto sm = getPermissiveServiceManager();
378 std::vector<std::string> instances;
379 EXPECT_TRUE(sm->getDeclaredInstances("mapper", &instances).isOk());
380 EXPECT_EQ(std::vector<std::string>{"minigbm"}, instances);
381}
382
Steven Moreland27cfab02019-08-12 14:34:16 -0700383class CallbackHistorian : public BnServiceCallback {
384 Status onRegistration(const std::string& name, const sp<IBinder>& binder) override {
385 registrations.push_back(name);
386 binders.push_back(binder);
387 return Status::ok();
388 }
389
390 android::status_t linkToDeath(const sp<DeathRecipient>&, void*, uint32_t) override {
391 // let SM linkToDeath
392 return android::OK;
393 }
394
395public:
396 std::vector<std::string> registrations;
397 std::vector<sp<IBinder>> binders;
398};
399
400TEST(ServiceNotifications, NoPermissionsRegister) {
401 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
402
403 EXPECT_CALL(*access, getCallingContext()).WillOnce(Return(Access::CallingContext{}));
404 EXPECT_CALL(*access, canFind(_,_)).WillOnce(Return(false));
405
Steven Morelandb0983182021-04-02 03:14:04 +0000406 sp<ServiceManager> sm = sp<ServiceManager>::make(std::move(access));
Steven Moreland27cfab02019-08-12 14:34:16 -0700407
Steven Morelandb0983182021-04-02 03:14:04 +0000408 sp<CallbackHistorian> cb = sp<CallbackHistorian>::make();
Steven Moreland27cfab02019-08-12 14:34:16 -0700409
Steven Morelandb9e1cbe2023-02-01 22:44:45 +0000410 EXPECT_EQ(sm->registerForNotifications("foofoo", cb).exceptionCode(), Status::EX_SECURITY);
411}
412
413TEST(GetService, IsolatedCantRegister) {
414 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
415
416 EXPECT_CALL(*access, getCallingContext())
417 .WillOnce(Return(Access::CallingContext{
418 .uid = AID_ISOLATED_START,
419 }));
420 EXPECT_CALL(*access, canFind(_, _)).WillOnce(Return(true));
421
422 sp<ServiceManager> sm = sp<ServiceManager>::make(std::move(access));
423
424 sp<CallbackHistorian> cb = sp<CallbackHistorian>::make();
425
Steven Moreland27cfab02019-08-12 14:34:16 -0700426 EXPECT_EQ(sm->registerForNotifications("foofoo", cb).exceptionCode(),
427 Status::EX_SECURITY);
428}
429
430TEST(ServiceNotifications, NoPermissionsUnregister) {
431 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
432
433 EXPECT_CALL(*access, getCallingContext()).WillOnce(Return(Access::CallingContext{}));
434 EXPECT_CALL(*access, canFind(_,_)).WillOnce(Return(false));
435
Steven Morelandb0983182021-04-02 03:14:04 +0000436 sp<ServiceManager> sm = sp<ServiceManager>::make(std::move(access));
Steven Moreland27cfab02019-08-12 14:34:16 -0700437
Steven Morelandb0983182021-04-02 03:14:04 +0000438 sp<CallbackHistorian> cb = sp<CallbackHistorian>::make();
Steven Moreland27cfab02019-08-12 14:34:16 -0700439
440 // should always hit security error first
441 EXPECT_EQ(sm->unregisterForNotifications("foofoo", cb).exceptionCode(),
442 Status::EX_SECURITY);
443}
444
445TEST(ServiceNotifications, InvalidName) {
446 auto sm = getPermissiveServiceManager();
447
Steven Morelandb0983182021-04-02 03:14:04 +0000448 sp<CallbackHistorian> cb = sp<CallbackHistorian>::make();
Steven Moreland27cfab02019-08-12 14:34:16 -0700449
450 EXPECT_EQ(sm->registerForNotifications("foo@foo", cb).exceptionCode(),
451 Status::EX_ILLEGAL_ARGUMENT);
452}
453
454TEST(ServiceNotifications, NullCallback) {
455 auto sm = getPermissiveServiceManager();
456
457 EXPECT_EQ(sm->registerForNotifications("foofoo", nullptr).exceptionCode(),
458 Status::EX_NULL_POINTER);
459}
460
461TEST(ServiceNotifications, Unregister) {
462 auto sm = getPermissiveServiceManager();
463
Steven Morelandb0983182021-04-02 03:14:04 +0000464 sp<CallbackHistorian> cb = sp<CallbackHistorian>::make();
Steven Moreland27cfab02019-08-12 14:34:16 -0700465
466 EXPECT_TRUE(sm->registerForNotifications("foofoo", cb).isOk());
467 EXPECT_EQ(sm->unregisterForNotifications("foofoo", cb).exceptionCode(), 0);
468}
469
470TEST(ServiceNotifications, UnregisterWhenNoRegistrationExists) {
471 auto sm = getPermissiveServiceManager();
472
Steven Morelandb0983182021-04-02 03:14:04 +0000473 sp<CallbackHistorian> cb = sp<CallbackHistorian>::make();
Steven Moreland27cfab02019-08-12 14:34:16 -0700474
475 EXPECT_EQ(sm->unregisterForNotifications("foofoo", cb).exceptionCode(),
476 Status::EX_ILLEGAL_STATE);
477}
478
479TEST(ServiceNotifications, NoNotification) {
480 auto sm = getPermissiveServiceManager();
481
Steven Morelandb0983182021-04-02 03:14:04 +0000482 sp<CallbackHistorian> cb = sp<CallbackHistorian>::make();
Steven Moreland27cfab02019-08-12 14:34:16 -0700483
484 EXPECT_TRUE(sm->registerForNotifications("foofoo", cb).isOk());
485 EXPECT_TRUE(sm->addService("otherservice", getBinder(),
486 false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
487
488 EXPECT_THAT(cb->registrations, ElementsAre());
489 EXPECT_THAT(cb->binders, ElementsAre());
490}
491
492TEST(ServiceNotifications, GetNotification) {
493 auto sm = getPermissiveServiceManager();
494
Steven Morelandb0983182021-04-02 03:14:04 +0000495 sp<CallbackHistorian> cb = sp<CallbackHistorian>::make();
Steven Moreland27cfab02019-08-12 14:34:16 -0700496
497 sp<IBinder> service = getBinder();
498
499 EXPECT_TRUE(sm->registerForNotifications("asdfasdf", cb).isOk());
500 EXPECT_TRUE(sm->addService("asdfasdf", service,
501 false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
502
503 EXPECT_THAT(cb->registrations, ElementsAre("asdfasdf"));
504 EXPECT_THAT(cb->binders, ElementsAre(service));
505}
506
507TEST(ServiceNotifications, GetNotificationForAlreadyRegisteredService) {
508 auto sm = getPermissiveServiceManager();
509
Steven Morelandb0983182021-04-02 03:14:04 +0000510 sp<CallbackHistorian> cb = sp<CallbackHistorian>::make();
Steven Moreland27cfab02019-08-12 14:34:16 -0700511
512 sp<IBinder> service = getBinder();
513
514 EXPECT_TRUE(sm->addService("asdfasdf", service,
515 false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
516
517 EXPECT_TRUE(sm->registerForNotifications("asdfasdf", cb).isOk());
518
519 EXPECT_THAT(cb->registrations, ElementsAre("asdfasdf"));
520 EXPECT_THAT(cb->binders, ElementsAre(service));
521}
522
523TEST(ServiceNotifications, GetMultipleNotification) {
524 auto sm = getPermissiveServiceManager();
525
Steven Morelandb0983182021-04-02 03:14:04 +0000526 sp<CallbackHistorian> cb = sp<CallbackHistorian>::make();
Steven Moreland27cfab02019-08-12 14:34:16 -0700527
528 sp<IBinder> binder1 = getBinder();
529 sp<IBinder> binder2 = getBinder();
530
531 EXPECT_TRUE(sm->registerForNotifications("asdfasdf", cb).isOk());
532 EXPECT_TRUE(sm->addService("asdfasdf", binder1,
533 false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
534 EXPECT_TRUE(sm->addService("asdfasdf", binder2,
535 false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
536
537 EXPECT_THAT(cb->registrations, ElementsAre("asdfasdf", "asdfasdf"));
538 EXPECT_THAT(cb->registrations, ElementsAre("asdfasdf", "asdfasdf"));
539}