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