Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 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 | |
Jooyung Han | ce94b75 | 2022-11-14 18:55:06 +0900 | [diff] [blame^] | 17 | #include <android-base/properties.h> |
| 18 | #include <android-base/strings.h> |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 19 | #include <android/os/BnServiceCallback.h> |
| 20 | #include <binder/Binder.h> |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 21 | #include <binder/IServiceManager.h> |
Jooyung Han | ce94b75 | 2022-11-14 18:55:06 +0900 | [diff] [blame^] | 22 | #include <binder/ProcessState.h> |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 23 | #include <cutils/android_filesystem_config.h> |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 24 | #include <gmock/gmock.h> |
Jooyung Han | ce94b75 | 2022-11-14 18:55:06 +0900 | [diff] [blame^] | 25 | #include <gtest/gtest.h> |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 26 | |
| 27 | #include "Access.h" |
| 28 | #include "ServiceManager.h" |
| 29 | |
| 30 | using android::sp; |
| 31 | using android::Access; |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 32 | using android::BBinder; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 33 | using android::IBinder; |
| 34 | using android::ServiceManager; |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 35 | using android::binder::Status; |
| 36 | using android::os::BnServiceCallback; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 37 | using android::os::IServiceManager; |
| 38 | using testing::_; |
| 39 | using testing::ElementsAre; |
| 40 | using testing::NiceMock; |
| 41 | using testing::Return; |
| 42 | |
| 43 | static sp<IBinder> getBinder() { |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 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 | |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 51 | return sp<LinkableBinder>::make(); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | class MockAccess : public Access { |
| 55 | public: |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 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)); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 59 | MOCK_METHOD1(canList, bool(const CallingContext&)); |
| 60 | }; |
| 61 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 62 | class MockServiceManager : public ServiceManager { |
| 63 | public: |
| 64 | MockServiceManager(std::unique_ptr<Access>&& access) : ServiceManager(std::move(access)) {} |
| 65 | MOCK_METHOD1(tryStartService, void(const std::string& name)); |
| 66 | }; |
| 67 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 68 | static sp<ServiceManager> getPermissiveServiceManager() { |
| 69 | std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>(); |
| 70 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 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)); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 74 | ON_CALL(*access, canList(_)).WillByDefault(Return(true)); |
| 75 | |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 76 | sp<ServiceManager> sm = sp<NiceMock<MockServiceManager>>::make(std::move(access)); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 77 | return sm; |
| 78 | } |
| 79 | |
Jooyung Han | ce94b75 | 2022-11-14 18:55:06 +0900 | [diff] [blame^] | 80 | static bool isCuttlefish() { |
| 81 | return android::base::StartsWith(android::base::GetProperty("ro.product.vendor.device", ""), |
| 82 | "vsoc_"); |
| 83 | } |
| 84 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 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 | |
| 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 | |
| 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 | |
| 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 | |
Steven Moreland | 905e2e8 | 2019-07-17 11:05:45 -0700 | [diff] [blame] | 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 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 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 | |
| 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>>(); |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 124 | EXPECT_CALL(*access, getCallingContext()).WillOnce(Return(Access::CallingContext{ |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 125 | .debugPid = 1337, |
| 126 | .uid = uid, |
| 127 | })); |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 128 | EXPECT_CALL(*access, canAdd(_, _)).Times(0); |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 129 | sp<ServiceManager> sm = sp<NiceMock<MockServiceManager>>::make(std::move(access)); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 130 | |
| 131 | EXPECT_FALSE(sm->addService("foo", getBinder(), false /*allowIsolated*/, |
| 132 | IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); |
| 133 | } |
| 134 | |
| 135 | } |
| 136 | |
| 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 | |
Devin Moore | 05ffe52 | 2020-08-06 13:58:29 -0700 | [diff] [blame] | 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 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 165 | TEST(AddService, NoPermissions) { |
| 166 | std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>(); |
| 167 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 168 | EXPECT_CALL(*access, getCallingContext()).WillOnce(Return(Access::CallingContext{})); |
| 169 | EXPECT_CALL(*access, canAdd(_, _)).WillOnce(Return(false)); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 170 | |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 171 | sp<ServiceManager> sm = sp<NiceMock<MockServiceManager>>::make(std::move(access)); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 172 | |
| 173 | EXPECT_FALSE(sm->addService("foo", getBinder(), false /*allowIsolated*/, |
| 174 | IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); |
| 175 | } |
| 176 | |
| 177 | TEST(GetService, HappyHappy) { |
| 178 | auto sm = getPermissiveServiceManager(); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 179 | sp<IBinder> service = getBinder(); |
| 180 | |
| 181 | EXPECT_TRUE(sm->addService("foo", service, false /*allowIsolated*/, |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 182 | IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); |
| 183 | |
| 184 | sp<IBinder> out; |
| 185 | EXPECT_TRUE(sm->getService("foo", &out).isOk()); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 186 | EXPECT_EQ(service, out); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 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 | |
| 197 | TEST(GetService, NoPermissionsForGettingService) { |
| 198 | std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>(); |
| 199 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 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)); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 203 | |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 204 | sp<ServiceManager> sm = sp<NiceMock<MockServiceManager>>::make(std::move(access)); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 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 | |
| 215 | TEST(GetService, AllowedFromIsolated) { |
| 216 | std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>(); |
| 217 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 218 | EXPECT_CALL(*access, getCallingContext()) |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 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 | })); |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 225 | EXPECT_CALL(*access, canAdd(_, _)).WillOnce(Return(true)); |
| 226 | EXPECT_CALL(*access, canFind(_, _)).WillOnce(Return(true)); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 227 | |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 228 | sp<ServiceManager> sm = sp<NiceMock<MockServiceManager>>::make(std::move(access)); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 229 | |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 230 | sp<IBinder> service = getBinder(); |
| 231 | EXPECT_TRUE(sm->addService("foo", service, true /*allowIsolated*/, |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 232 | IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); |
| 233 | |
| 234 | sp<IBinder> out; |
| 235 | EXPECT_TRUE(sm->getService("foo", &out).isOk()); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 236 | EXPECT_EQ(service, out.get()); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | TEST(GetService, NotAllowedFromIsolated) { |
| 240 | std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>(); |
| 241 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 242 | EXPECT_CALL(*access, getCallingContext()) |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 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 | })); |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 249 | EXPECT_CALL(*access, canAdd(_, _)).WillOnce(Return(true)); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 250 | |
| 251 | // TODO(b/136023468): when security check is first, this should be called first |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 252 | // EXPECT_CALL(*access, canFind(_, _)).WillOnce(Return(true)); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 253 | |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 254 | sp<ServiceManager> sm = sp<NiceMock<MockServiceManager>>::make(std::move(access)); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 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 | |
| 265 | TEST(ListServices, NoPermissions) { |
| 266 | std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>(); |
| 267 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 268 | EXPECT_CALL(*access, getCallingContext()).WillOnce(Return(Access::CallingContext{})); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 269 | EXPECT_CALL(*access, canList(_)).WillOnce(Return(false)); |
| 270 | |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 271 | sp<ServiceManager> sm = sp<NiceMock<MockServiceManager>>::make(std::move(access)); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 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 | |
| 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 | |
| 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 | } |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 315 | |
Jooyung Han | ce94b75 | 2022-11-14 18:55:06 +0900 | [diff] [blame^] | 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 | |
| 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 | |
| 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 | |
| 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 | |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 359 | class CallbackHistorian : public BnServiceCallback { |
| 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 | |
| 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 | |
| 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 | |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 382 | sp<ServiceManager> sm = sp<ServiceManager>::make(std::move(access)); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 383 | |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 384 | sp<CallbackHistorian> cb = sp<CallbackHistorian>::make(); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 385 | |
| 386 | EXPECT_EQ(sm->registerForNotifications("foofoo", cb).exceptionCode(), |
| 387 | Status::EX_SECURITY); |
| 388 | } |
| 389 | |
| 390 | TEST(ServiceNotifications, NoPermissionsUnregister) { |
| 391 | std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>(); |
| 392 | |
| 393 | EXPECT_CALL(*access, getCallingContext()).WillOnce(Return(Access::CallingContext{})); |
| 394 | EXPECT_CALL(*access, canFind(_,_)).WillOnce(Return(false)); |
| 395 | |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 396 | sp<ServiceManager> sm = sp<ServiceManager>::make(std::move(access)); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 397 | |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 398 | sp<CallbackHistorian> cb = sp<CallbackHistorian>::make(); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 399 | |
| 400 | // should always hit security error first |
| 401 | EXPECT_EQ(sm->unregisterForNotifications("foofoo", cb).exceptionCode(), |
| 402 | Status::EX_SECURITY); |
| 403 | } |
| 404 | |
| 405 | TEST(ServiceNotifications, InvalidName) { |
| 406 | auto sm = getPermissiveServiceManager(); |
| 407 | |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 408 | sp<CallbackHistorian> cb = sp<CallbackHistorian>::make(); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 409 | |
| 410 | EXPECT_EQ(sm->registerForNotifications("foo@foo", cb).exceptionCode(), |
| 411 | Status::EX_ILLEGAL_ARGUMENT); |
| 412 | } |
| 413 | |
| 414 | TEST(ServiceNotifications, NullCallback) { |
| 415 | auto sm = getPermissiveServiceManager(); |
| 416 | |
| 417 | EXPECT_EQ(sm->registerForNotifications("foofoo", nullptr).exceptionCode(), |
| 418 | Status::EX_NULL_POINTER); |
| 419 | } |
| 420 | |
| 421 | TEST(ServiceNotifications, Unregister) { |
| 422 | auto sm = getPermissiveServiceManager(); |
| 423 | |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 424 | sp<CallbackHistorian> cb = sp<CallbackHistorian>::make(); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 425 | |
| 426 | EXPECT_TRUE(sm->registerForNotifications("foofoo", cb).isOk()); |
| 427 | EXPECT_EQ(sm->unregisterForNotifications("foofoo", cb).exceptionCode(), 0); |
| 428 | } |
| 429 | |
| 430 | TEST(ServiceNotifications, UnregisterWhenNoRegistrationExists) { |
| 431 | auto sm = getPermissiveServiceManager(); |
| 432 | |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 433 | sp<CallbackHistorian> cb = sp<CallbackHistorian>::make(); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 434 | |
| 435 | EXPECT_EQ(sm->unregisterForNotifications("foofoo", cb).exceptionCode(), |
| 436 | Status::EX_ILLEGAL_STATE); |
| 437 | } |
| 438 | |
| 439 | TEST(ServiceNotifications, NoNotification) { |
| 440 | auto sm = getPermissiveServiceManager(); |
| 441 | |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 442 | sp<CallbackHistorian> cb = sp<CallbackHistorian>::make(); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 443 | |
| 444 | EXPECT_TRUE(sm->registerForNotifications("foofoo", cb).isOk()); |
| 445 | EXPECT_TRUE(sm->addService("otherservice", getBinder(), |
| 446 | false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); |
| 447 | |
| 448 | EXPECT_THAT(cb->registrations, ElementsAre()); |
| 449 | EXPECT_THAT(cb->binders, ElementsAre()); |
| 450 | } |
| 451 | |
| 452 | TEST(ServiceNotifications, GetNotification) { |
| 453 | auto sm = getPermissiveServiceManager(); |
| 454 | |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 455 | sp<CallbackHistorian> cb = sp<CallbackHistorian>::make(); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 456 | |
| 457 | sp<IBinder> service = getBinder(); |
| 458 | |
| 459 | EXPECT_TRUE(sm->registerForNotifications("asdfasdf", cb).isOk()); |
| 460 | EXPECT_TRUE(sm->addService("asdfasdf", service, |
| 461 | false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); |
| 462 | |
| 463 | EXPECT_THAT(cb->registrations, ElementsAre("asdfasdf")); |
| 464 | EXPECT_THAT(cb->binders, ElementsAre(service)); |
| 465 | } |
| 466 | |
| 467 | TEST(ServiceNotifications, GetNotificationForAlreadyRegisteredService) { |
| 468 | auto sm = getPermissiveServiceManager(); |
| 469 | |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 470 | sp<CallbackHistorian> cb = sp<CallbackHistorian>::make(); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 471 | |
| 472 | sp<IBinder> service = getBinder(); |
| 473 | |
| 474 | EXPECT_TRUE(sm->addService("asdfasdf", service, |
| 475 | false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); |
| 476 | |
| 477 | EXPECT_TRUE(sm->registerForNotifications("asdfasdf", cb).isOk()); |
| 478 | |
| 479 | EXPECT_THAT(cb->registrations, ElementsAre("asdfasdf")); |
| 480 | EXPECT_THAT(cb->binders, ElementsAre(service)); |
| 481 | } |
| 482 | |
| 483 | TEST(ServiceNotifications, GetMultipleNotification) { |
| 484 | auto sm = getPermissiveServiceManager(); |
| 485 | |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 486 | sp<CallbackHistorian> cb = sp<CallbackHistorian>::make(); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 487 | |
| 488 | sp<IBinder> binder1 = getBinder(); |
| 489 | sp<IBinder> binder2 = getBinder(); |
| 490 | |
| 491 | EXPECT_TRUE(sm->registerForNotifications("asdfasdf", cb).isOk()); |
| 492 | EXPECT_TRUE(sm->addService("asdfasdf", binder1, |
| 493 | false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); |
| 494 | EXPECT_TRUE(sm->addService("asdfasdf", binder2, |
| 495 | false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); |
| 496 | |
| 497 | EXPECT_THAT(cb->registrations, ElementsAre("asdfasdf", "asdfasdf")); |
| 498 | EXPECT_THAT(cb->registrations, ElementsAre("asdfasdf", "asdfasdf")); |
| 499 | } |