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 | |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 17 | #include <android/os/BnServiceCallback.h> |
| 18 | #include <binder/Binder.h> |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 19 | #include <binder/ProcessState.h> |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 20 | #include <binder/IServiceManager.h> |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 21 | #include <cutils/android_filesystem_config.h> |
| 22 | #include <gtest/gtest.h> |
| 23 | #include <gmock/gmock.h> |
| 24 | |
| 25 | #include "Access.h" |
| 26 | #include "ServiceManager.h" |
| 27 | |
| 28 | using android::sp; |
| 29 | using android::Access; |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 30 | using android::BBinder; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 31 | using android::IBinder; |
| 32 | using android::ServiceManager; |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 33 | using android::binder::Status; |
| 34 | using android::os::BnServiceCallback; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 35 | using android::os::IServiceManager; |
| 36 | using testing::_; |
| 37 | using testing::ElementsAre; |
| 38 | using testing::NiceMock; |
| 39 | using testing::Return; |
| 40 | |
| 41 | static sp<IBinder> getBinder() { |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 42 | class LinkableBinder : public BBinder { |
| 43 | android::status_t linkToDeath(const sp<DeathRecipient>&, void*, uint32_t) override { |
| 44 | // let SM linkToDeath |
| 45 | return android::OK; |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | return new LinkableBinder; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | class MockAccess : public Access { |
| 53 | public: |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 54 | MOCK_METHOD0(getCallingContext, CallingContext()); |
| 55 | MOCK_METHOD2(canAdd, bool(const CallingContext&, const std::string& name)); |
| 56 | MOCK_METHOD2(canFind, bool(const CallingContext&, const std::string& name)); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 57 | MOCK_METHOD1(canList, bool(const CallingContext&)); |
| 58 | }; |
| 59 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 60 | class MockServiceManager : public ServiceManager { |
| 61 | public: |
| 62 | MockServiceManager(std::unique_ptr<Access>&& access) : ServiceManager(std::move(access)) {} |
| 63 | MOCK_METHOD1(tryStartService, void(const std::string& name)); |
| 64 | }; |
| 65 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 66 | static sp<ServiceManager> getPermissiveServiceManager() { |
| 67 | std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>(); |
| 68 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 69 | ON_CALL(*access, getCallingContext()).WillByDefault(Return(Access::CallingContext{})); |
| 70 | ON_CALL(*access, canAdd(_, _)).WillByDefault(Return(true)); |
| 71 | ON_CALL(*access, canFind(_, _)).WillByDefault(Return(true)); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 72 | ON_CALL(*access, canList(_)).WillByDefault(Return(true)); |
| 73 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 74 | sp<ServiceManager> sm = new NiceMock<MockServiceManager>(std::move(access)); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 75 | return sm; |
| 76 | } |
| 77 | |
| 78 | TEST(AddService, HappyHappy) { |
| 79 | auto sm = getPermissiveServiceManager(); |
| 80 | EXPECT_TRUE(sm->addService("foo", getBinder(), false /*allowIsolated*/, |
| 81 | IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); |
| 82 | } |
| 83 | |
| 84 | TEST(AddService, EmptyNameDisallowed) { |
| 85 | auto sm = getPermissiveServiceManager(); |
| 86 | EXPECT_FALSE(sm->addService("", getBinder(), false /*allowIsolated*/, |
| 87 | IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); |
| 88 | } |
| 89 | |
| 90 | TEST(AddService, JustShortEnoughServiceNameHappy) { |
| 91 | auto sm = getPermissiveServiceManager(); |
| 92 | EXPECT_TRUE(sm->addService(std::string(127, 'a'), getBinder(), false /*allowIsolated*/, |
| 93 | IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); |
| 94 | } |
| 95 | |
| 96 | TEST(AddService, TooLongNameDisallowed) { |
| 97 | auto sm = getPermissiveServiceManager(); |
| 98 | EXPECT_FALSE(sm->addService(std::string(128, 'a'), getBinder(), false /*allowIsolated*/, |
| 99 | IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); |
| 100 | } |
| 101 | |
Steven Moreland | 905e2e8 | 2019-07-17 11:05:45 -0700 | [diff] [blame] | 102 | TEST(AddService, WeirdCharactersDisallowed) { |
| 103 | auto sm = getPermissiveServiceManager(); |
| 104 | EXPECT_FALSE(sm->addService("happy$foo$foo", getBinder(), false /*allowIsolated*/, |
| 105 | IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); |
| 106 | } |
| 107 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 108 | TEST(AddService, AddNullServiceDisallowed) { |
| 109 | auto sm = getPermissiveServiceManager(); |
| 110 | EXPECT_FALSE(sm->addService("foo", nullptr, false /*allowIsolated*/, |
| 111 | IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); |
| 112 | } |
| 113 | |
| 114 | TEST(AddService, AddDisallowedFromApp) { |
| 115 | for (uid_t uid : { AID_APP_START, AID_APP_START + 1, AID_APP_END }) { |
| 116 | std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>(); |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 117 | EXPECT_CALL(*access, getCallingContext()).WillOnce(Return(Access::CallingContext{ |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 118 | .debugPid = 1337, |
| 119 | .uid = uid, |
| 120 | })); |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 121 | EXPECT_CALL(*access, canAdd(_, _)).Times(0); |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 122 | sp<ServiceManager> sm = new NiceMock<MockServiceManager>(std::move(access)); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 123 | |
| 124 | EXPECT_FALSE(sm->addService("foo", getBinder(), false /*allowIsolated*/, |
| 125 | IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); |
| 126 | } |
| 127 | |
| 128 | } |
| 129 | |
| 130 | TEST(AddService, HappyOverExistingService) { |
| 131 | auto sm = getPermissiveServiceManager(); |
| 132 | EXPECT_TRUE(sm->addService("foo", getBinder(), false /*allowIsolated*/, |
| 133 | IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); |
| 134 | EXPECT_TRUE(sm->addService("foo", getBinder(), false /*allowIsolated*/, |
| 135 | IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); |
| 136 | } |
| 137 | |
| 138 | TEST(AddService, NoPermissions) { |
| 139 | std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>(); |
| 140 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 141 | EXPECT_CALL(*access, getCallingContext()).WillOnce(Return(Access::CallingContext{})); |
| 142 | EXPECT_CALL(*access, canAdd(_, _)).WillOnce(Return(false)); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 143 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 144 | sp<ServiceManager> sm = new NiceMock<MockServiceManager>(std::move(access)); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 145 | |
| 146 | EXPECT_FALSE(sm->addService("foo", getBinder(), false /*allowIsolated*/, |
| 147 | IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); |
| 148 | } |
| 149 | |
| 150 | TEST(GetService, HappyHappy) { |
| 151 | auto sm = getPermissiveServiceManager(); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 152 | sp<IBinder> service = getBinder(); |
| 153 | |
| 154 | EXPECT_TRUE(sm->addService("foo", service, false /*allowIsolated*/, |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 155 | IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); |
| 156 | |
| 157 | sp<IBinder> out; |
| 158 | EXPECT_TRUE(sm->getService("foo", &out).isOk()); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 159 | EXPECT_EQ(service, out); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | TEST(GetService, NonExistant) { |
| 163 | auto sm = getPermissiveServiceManager(); |
| 164 | |
| 165 | sp<IBinder> out; |
| 166 | EXPECT_TRUE(sm->getService("foo", &out).isOk()); |
| 167 | EXPECT_EQ(nullptr, out.get()); |
| 168 | } |
| 169 | |
| 170 | TEST(GetService, NoPermissionsForGettingService) { |
| 171 | std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>(); |
| 172 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 173 | EXPECT_CALL(*access, getCallingContext()).WillRepeatedly(Return(Access::CallingContext{})); |
| 174 | EXPECT_CALL(*access, canAdd(_, _)).WillOnce(Return(true)); |
| 175 | EXPECT_CALL(*access, canFind(_, _)).WillOnce(Return(false)); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 176 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 177 | sp<ServiceManager> sm = new NiceMock<MockServiceManager>(std::move(access)); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 178 | |
| 179 | EXPECT_TRUE(sm->addService("foo", getBinder(), false /*allowIsolated*/, |
| 180 | IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); |
| 181 | |
| 182 | sp<IBinder> out; |
| 183 | // returns nullptr but has OK status for legacy compatibility |
| 184 | EXPECT_TRUE(sm->getService("foo", &out).isOk()); |
| 185 | EXPECT_EQ(nullptr, out.get()); |
| 186 | } |
| 187 | |
| 188 | TEST(GetService, AllowedFromIsolated) { |
| 189 | std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>(); |
| 190 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 191 | EXPECT_CALL(*access, getCallingContext()) |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 192 | // something adds it |
| 193 | .WillOnce(Return(Access::CallingContext{})) |
| 194 | // next call is from isolated app |
| 195 | .WillOnce(Return(Access::CallingContext{ |
| 196 | .uid = AID_ISOLATED_START, |
| 197 | })); |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 198 | EXPECT_CALL(*access, canAdd(_, _)).WillOnce(Return(true)); |
| 199 | EXPECT_CALL(*access, canFind(_, _)).WillOnce(Return(true)); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 200 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 201 | sp<ServiceManager> sm = new NiceMock<MockServiceManager>(std::move(access)); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 202 | |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 203 | sp<IBinder> service = getBinder(); |
| 204 | EXPECT_TRUE(sm->addService("foo", service, true /*allowIsolated*/, |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 205 | IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); |
| 206 | |
| 207 | sp<IBinder> out; |
| 208 | EXPECT_TRUE(sm->getService("foo", &out).isOk()); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 209 | EXPECT_EQ(service, out.get()); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | TEST(GetService, NotAllowedFromIsolated) { |
| 213 | std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>(); |
| 214 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 215 | EXPECT_CALL(*access, getCallingContext()) |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 216 | // something adds it |
| 217 | .WillOnce(Return(Access::CallingContext{})) |
| 218 | // next call is from isolated app |
| 219 | .WillOnce(Return(Access::CallingContext{ |
| 220 | .uid = AID_ISOLATED_START, |
| 221 | })); |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 222 | EXPECT_CALL(*access, canAdd(_, _)).WillOnce(Return(true)); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 223 | |
| 224 | // 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] | 225 | // EXPECT_CALL(*access, canFind(_, _)).WillOnce(Return(true)); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 226 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 227 | sp<ServiceManager> sm = new NiceMock<MockServiceManager>(std::move(access)); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 228 | |
| 229 | EXPECT_TRUE(sm->addService("foo", getBinder(), false /*allowIsolated*/, |
| 230 | IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); |
| 231 | |
| 232 | sp<IBinder> out; |
| 233 | // returns nullptr but has OK status for legacy compatibility |
| 234 | EXPECT_TRUE(sm->getService("foo", &out).isOk()); |
| 235 | EXPECT_EQ(nullptr, out.get()); |
| 236 | } |
| 237 | |
| 238 | TEST(ListServices, NoPermissions) { |
| 239 | std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>(); |
| 240 | |
Steven Moreland | a9fe474 | 2019-07-18 14:45:20 -0700 | [diff] [blame] | 241 | EXPECT_CALL(*access, getCallingContext()).WillOnce(Return(Access::CallingContext{})); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 242 | EXPECT_CALL(*access, canList(_)).WillOnce(Return(false)); |
| 243 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 244 | sp<ServiceManager> sm = new NiceMock<MockServiceManager>(std::move(access)); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 245 | |
| 246 | std::vector<std::string> out; |
| 247 | EXPECT_FALSE(sm->listServices(IServiceManager::DUMP_FLAG_PRIORITY_ALL, &out).isOk()); |
| 248 | EXPECT_TRUE(out.empty()); |
| 249 | } |
| 250 | |
| 251 | TEST(ListServices, AllServices) { |
| 252 | auto sm = getPermissiveServiceManager(); |
| 253 | |
| 254 | EXPECT_TRUE(sm->addService("sd", getBinder(), false /*allowIsolated*/, |
| 255 | IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); |
| 256 | EXPECT_TRUE(sm->addService("sc", getBinder(), false /*allowIsolated*/, |
| 257 | IServiceManager::DUMP_FLAG_PRIORITY_NORMAL).isOk()); |
| 258 | EXPECT_TRUE(sm->addService("sb", getBinder(), false /*allowIsolated*/, |
| 259 | IServiceManager::DUMP_FLAG_PRIORITY_HIGH).isOk()); |
| 260 | EXPECT_TRUE(sm->addService("sa", getBinder(), false /*allowIsolated*/, |
| 261 | IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL).isOk()); |
| 262 | |
| 263 | std::vector<std::string> out; |
| 264 | EXPECT_TRUE(sm->listServices(IServiceManager::DUMP_FLAG_PRIORITY_ALL, &out).isOk()); |
| 265 | |
| 266 | // all there and in the right order |
| 267 | EXPECT_THAT(out, ElementsAre("sa", "sb", "sc", "sd")); |
| 268 | } |
| 269 | |
| 270 | TEST(ListServices, CriticalServices) { |
| 271 | auto sm = getPermissiveServiceManager(); |
| 272 | |
| 273 | EXPECT_TRUE(sm->addService("sd", getBinder(), false /*allowIsolated*/, |
| 274 | IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); |
| 275 | EXPECT_TRUE(sm->addService("sc", getBinder(), false /*allowIsolated*/, |
| 276 | IServiceManager::DUMP_FLAG_PRIORITY_NORMAL).isOk()); |
| 277 | EXPECT_TRUE(sm->addService("sb", getBinder(), false /*allowIsolated*/, |
| 278 | IServiceManager::DUMP_FLAG_PRIORITY_HIGH).isOk()); |
| 279 | EXPECT_TRUE(sm->addService("sa", getBinder(), false /*allowIsolated*/, |
| 280 | IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL).isOk()); |
| 281 | |
| 282 | std::vector<std::string> out; |
| 283 | EXPECT_TRUE(sm->listServices(IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL, &out).isOk()); |
| 284 | |
| 285 | // all there and in the right order |
| 286 | EXPECT_THAT(out, ElementsAre("sa")); |
| 287 | } |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 288 | |
| 289 | class CallbackHistorian : public BnServiceCallback { |
| 290 | Status onRegistration(const std::string& name, const sp<IBinder>& binder) override { |
| 291 | registrations.push_back(name); |
| 292 | binders.push_back(binder); |
| 293 | return Status::ok(); |
| 294 | } |
| 295 | |
| 296 | android::status_t linkToDeath(const sp<DeathRecipient>&, void*, uint32_t) override { |
| 297 | // let SM linkToDeath |
| 298 | return android::OK; |
| 299 | } |
| 300 | |
| 301 | public: |
| 302 | std::vector<std::string> registrations; |
| 303 | std::vector<sp<IBinder>> binders; |
| 304 | }; |
| 305 | |
| 306 | TEST(ServiceNotifications, NoPermissionsRegister) { |
| 307 | std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>(); |
| 308 | |
| 309 | EXPECT_CALL(*access, getCallingContext()).WillOnce(Return(Access::CallingContext{})); |
| 310 | EXPECT_CALL(*access, canFind(_,_)).WillOnce(Return(false)); |
| 311 | |
| 312 | sp<ServiceManager> sm = new ServiceManager(std::move(access)); |
| 313 | |
| 314 | sp<CallbackHistorian> cb = new CallbackHistorian; |
| 315 | |
| 316 | EXPECT_EQ(sm->registerForNotifications("foofoo", cb).exceptionCode(), |
| 317 | Status::EX_SECURITY); |
| 318 | } |
| 319 | |
| 320 | TEST(ServiceNotifications, NoPermissionsUnregister) { |
| 321 | std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>(); |
| 322 | |
| 323 | EXPECT_CALL(*access, getCallingContext()).WillOnce(Return(Access::CallingContext{})); |
| 324 | EXPECT_CALL(*access, canFind(_,_)).WillOnce(Return(false)); |
| 325 | |
| 326 | sp<ServiceManager> sm = new ServiceManager(std::move(access)); |
| 327 | |
| 328 | sp<CallbackHistorian> cb = new CallbackHistorian; |
| 329 | |
| 330 | // should always hit security error first |
| 331 | EXPECT_EQ(sm->unregisterForNotifications("foofoo", cb).exceptionCode(), |
| 332 | Status::EX_SECURITY); |
| 333 | } |
| 334 | |
| 335 | TEST(ServiceNotifications, InvalidName) { |
| 336 | auto sm = getPermissiveServiceManager(); |
| 337 | |
| 338 | sp<CallbackHistorian> cb = new CallbackHistorian; |
| 339 | |
| 340 | EXPECT_EQ(sm->registerForNotifications("foo@foo", cb).exceptionCode(), |
| 341 | Status::EX_ILLEGAL_ARGUMENT); |
| 342 | } |
| 343 | |
| 344 | TEST(ServiceNotifications, NullCallback) { |
| 345 | auto sm = getPermissiveServiceManager(); |
| 346 | |
| 347 | EXPECT_EQ(sm->registerForNotifications("foofoo", nullptr).exceptionCode(), |
| 348 | Status::EX_NULL_POINTER); |
| 349 | } |
| 350 | |
| 351 | TEST(ServiceNotifications, Unregister) { |
| 352 | auto sm = getPermissiveServiceManager(); |
| 353 | |
| 354 | sp<CallbackHistorian> cb = new CallbackHistorian; |
| 355 | |
| 356 | EXPECT_TRUE(sm->registerForNotifications("foofoo", cb).isOk()); |
| 357 | EXPECT_EQ(sm->unregisterForNotifications("foofoo", cb).exceptionCode(), 0); |
| 358 | } |
| 359 | |
| 360 | TEST(ServiceNotifications, UnregisterWhenNoRegistrationExists) { |
| 361 | auto sm = getPermissiveServiceManager(); |
| 362 | |
| 363 | sp<CallbackHistorian> cb = new CallbackHistorian; |
| 364 | |
| 365 | EXPECT_EQ(sm->unregisterForNotifications("foofoo", cb).exceptionCode(), |
| 366 | Status::EX_ILLEGAL_STATE); |
| 367 | } |
| 368 | |
| 369 | TEST(ServiceNotifications, NoNotification) { |
| 370 | auto sm = getPermissiveServiceManager(); |
| 371 | |
| 372 | sp<CallbackHistorian> cb = new CallbackHistorian; |
| 373 | |
| 374 | EXPECT_TRUE(sm->registerForNotifications("foofoo", cb).isOk()); |
| 375 | EXPECT_TRUE(sm->addService("otherservice", getBinder(), |
| 376 | false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); |
| 377 | |
| 378 | EXPECT_THAT(cb->registrations, ElementsAre()); |
| 379 | EXPECT_THAT(cb->binders, ElementsAre()); |
| 380 | } |
| 381 | |
| 382 | TEST(ServiceNotifications, GetNotification) { |
| 383 | auto sm = getPermissiveServiceManager(); |
| 384 | |
| 385 | sp<CallbackHistorian> cb = new CallbackHistorian; |
| 386 | |
| 387 | sp<IBinder> service = getBinder(); |
| 388 | |
| 389 | EXPECT_TRUE(sm->registerForNotifications("asdfasdf", cb).isOk()); |
| 390 | EXPECT_TRUE(sm->addService("asdfasdf", service, |
| 391 | false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); |
| 392 | |
| 393 | EXPECT_THAT(cb->registrations, ElementsAre("asdfasdf")); |
| 394 | EXPECT_THAT(cb->binders, ElementsAre(service)); |
| 395 | } |
| 396 | |
| 397 | TEST(ServiceNotifications, GetNotificationForAlreadyRegisteredService) { |
| 398 | auto sm = getPermissiveServiceManager(); |
| 399 | |
| 400 | sp<CallbackHistorian> cb = new CallbackHistorian; |
| 401 | |
| 402 | sp<IBinder> service = getBinder(); |
| 403 | |
| 404 | EXPECT_TRUE(sm->addService("asdfasdf", service, |
| 405 | false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); |
| 406 | |
| 407 | EXPECT_TRUE(sm->registerForNotifications("asdfasdf", cb).isOk()); |
| 408 | |
| 409 | EXPECT_THAT(cb->registrations, ElementsAre("asdfasdf")); |
| 410 | EXPECT_THAT(cb->binders, ElementsAre(service)); |
| 411 | } |
| 412 | |
| 413 | TEST(ServiceNotifications, GetMultipleNotification) { |
| 414 | auto sm = getPermissiveServiceManager(); |
| 415 | |
| 416 | sp<CallbackHistorian> cb = new CallbackHistorian; |
| 417 | |
| 418 | sp<IBinder> binder1 = getBinder(); |
| 419 | sp<IBinder> binder2 = getBinder(); |
| 420 | |
| 421 | EXPECT_TRUE(sm->registerForNotifications("asdfasdf", cb).isOk()); |
| 422 | EXPECT_TRUE(sm->addService("asdfasdf", binder1, |
| 423 | false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); |
| 424 | EXPECT_TRUE(sm->addService("asdfasdf", binder2, |
| 425 | false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); |
| 426 | |
| 427 | EXPECT_THAT(cb->registrations, ElementsAre("asdfasdf", "asdfasdf")); |
| 428 | EXPECT_THAT(cb->registrations, ElementsAre("asdfasdf", "asdfasdf")); |
| 429 | } |