Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/proximity_auth/unlock_manager.h" | |
| 6 | |
| 7 #include "base/macros.h" | |
| 8 #include "base/memory/ref_counted.h" | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/test/test_simple_task_runner.h" | |
| 11 #include "base/thread_task_runner_handle.h" | |
| 12 #include "components/proximity_auth/client.h" | |
| 13 #include "components/proximity_auth/controller.h" | |
| 14 #include "components/proximity_auth/logging/logging.h" | |
| 15 #include "components/proximity_auth/proximity_auth_client.h" | |
| 16 #include "components/proximity_auth/proximity_monitor.h" | |
| 17 #include "components/proximity_auth/remote_status_update.h" | |
| 18 #include "components/proximity_auth/screenlock_bridge.h" | |
| 19 #include "device/bluetooth/bluetooth_adapter_factory.h" | |
| 20 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | |
| 21 #include "testing/gmock/include/gmock/gmock.h" | |
| 22 #include "testing/gtest/include/gtest/gtest.h" | |
| 23 | |
| 24 #if defined(OS_CHROMEOS) | |
| 25 #include "chromeos/dbus/dbus_thread_manager.h" | |
| 26 #endif // defined(OS_CHROMEOS) | |
| 27 | |
| 28 using testing::AtLeast; | |
| 29 using testing::NiceMock; | |
| 30 using testing::Return; | |
| 31 using testing::_; | |
| 32 | |
| 33 namespace proximity_auth { | |
| 34 namespace { | |
| 35 | |
| 36 // Note that the trust agent state is currently ignored by the UnlockManager | |
| 37 // implementation. | |
| 38 RemoteStatusUpdate kRemoteScreenUnlocked = { | |
| 39 USER_PRESENT, SECURE_SCREEN_LOCK_ENABLED, TRUST_AGENT_UNSUPPORTED}; | |
| 40 RemoteStatusUpdate kRemoteScreenLocked = { | |
| 41 USER_ABSENT, SECURE_SCREEN_LOCK_ENABLED, TRUST_AGENT_UNSUPPORTED}; | |
| 42 RemoteStatusUpdate kRemoteScreenlockDisabled = { | |
| 43 USER_PRESENT, SECURE_SCREEN_LOCK_DISABLED, TRUST_AGENT_UNSUPPORTED}; | |
| 44 RemoteStatusUpdate kRemoteScreenlockStateUnknown = { | |
| 45 USER_PRESENCE_UNKNOWN, SECURE_SCREEN_LOCK_STATE_UNKNOWN, | |
| 46 TRUST_AGENT_UNSUPPORTED}; | |
| 47 | |
| 48 class MockController : public Controller { | |
| 49 public: | |
| 50 MockController() {} | |
| 51 ~MockController() override {} | |
| 52 | |
| 53 MOCK_CONST_METHOD0(GetState, State()); | |
| 54 MOCK_METHOD0(GetClient, Client*()); | |
| 55 }; | |
| 56 | |
| 57 class MockClient : public Client { | |
| 58 public: | |
| 59 MockClient() {} | |
| 60 ~MockClient() override {} | |
| 61 | |
| 62 MOCK_METHOD1(AddObserver, void(ClientObserver* observer)); | |
| 63 MOCK_METHOD1(RemoveObserver, void(ClientObserver* observer)); | |
| 64 MOCK_CONST_METHOD0(SupportsSignIn, bool()); | |
| 65 MOCK_METHOD0(DispatchUnlockEvent, void()); | |
| 66 MOCK_METHOD1(RequestDecryption, void(const std::string& challenge)); | |
| 67 MOCK_METHOD0(RequestUnlock, void()); | |
| 68 | |
| 69 private: | |
| 70 DISALLOW_COPY_AND_ASSIGN(MockClient); | |
| 71 }; | |
| 72 | |
| 73 class MockProximityMonitor : public ProximityMonitor { | |
| 74 public: | |
| 75 MockProximityMonitor() { | |
| 76 ON_CALL(*this, GetStrategy()) | |
| 77 .WillByDefault(Return(ProximityMonitor::Strategy::NONE)); | |
| 78 ON_CALL(*this, IsUnlockAllowed()).WillByDefault(Return(true)); | |
| 79 ON_CALL(*this, IsInRssiRange()).WillByDefault(Return(false)); | |
| 80 } | |
| 81 ~MockProximityMonitor() override {} | |
| 82 | |
| 83 MOCK_METHOD0(Start, void()); | |
| 84 MOCK_METHOD0(Stop, void()); | |
| 85 MOCK_CONST_METHOD0(GetStrategy, Strategy()); | |
| 86 MOCK_CONST_METHOD0(IsUnlockAllowed, bool()); | |
| 87 MOCK_CONST_METHOD0(IsInRssiRange, bool()); | |
| 88 MOCK_METHOD0(RecordProximityMetricsOnAuthSuccess, void()); | |
| 89 | |
| 90 private: | |
| 91 DISALLOW_COPY_AND_ASSIGN(MockProximityMonitor); | |
| 92 }; | |
| 93 | |
| 94 class MockProximityAuthClient : public ProximityAuthClient { | |
| 95 public: | |
| 96 MockProximityAuthClient() {} | |
| 97 ~MockProximityAuthClient() override {} | |
| 98 | |
| 99 MOCK_CONST_METHOD0(GetAuthenticatedUsername, std::string()); | |
| 100 MOCK_METHOD0(Lock, void()); | |
| 101 MOCK_METHOD1(UpdateScreenlockState, | |
| 102 void(proximity_auth::ScreenlockState state)); | |
| 103 MOCK_METHOD1(FinalizeUnlock, void(bool success)); | |
| 104 MOCK_METHOD1(FinalizeSignIn, void(const std::string& secret)); | |
| 105 | |
| 106 private: | |
| 107 DISALLOW_COPY_AND_ASSIGN(MockProximityAuthClient); | |
| 108 }; | |
| 109 | |
| 110 class FakeLockHandler : public ScreenlockBridge::LockHandler { | |
| 111 public: | |
| 112 FakeLockHandler() {} | |
| 113 ~FakeLockHandler() override {} | |
| 114 | |
| 115 // LockHandler: | |
| 116 void ShowBannerMessage(const base::string16& message) override {} | |
| 117 void ShowUserPodCustomIcon( | |
| 118 const std::string& user_email, | |
| 119 const ScreenlockBridge::UserPodCustomIconOptions& icon) override {} | |
| 120 void HideUserPodCustomIcon(const std::string& user_email) override {} | |
| 121 void EnableInput() override {} | |
| 122 void SetAuthType(const std::string& user_email, | |
| 123 AuthType auth_type, | |
| 124 const base::string16& auth_value) override {} | |
| 125 AuthType GetAuthType(const std::string& user_email) const override { | |
| 126 return USER_CLICK; | |
| 127 } | |
| 128 ScreenType GetScreenType() const override { return LOCK_SCREEN; } | |
| 129 void Unlock(const std::string& user_email) override {} | |
| 130 void AttemptEasySignin(const std::string& user_email, | |
| 131 const std::string& secret, | |
| 132 const std::string& key_label) override {} | |
| 133 | |
| 134 private: | |
| 135 DISALLOW_COPY_AND_ASSIGN(FakeLockHandler); | |
| 136 }; | |
| 137 | |
| 138 class TestUnlockManager : public UnlockManager { | |
| 139 public: | |
| 140 TestUnlockManager(ScreenlockType screenlock_type, | |
| 141 scoped_ptr<ProximityMonitor> proximity_monitor, | |
| 142 ProximityAuthClient* proximity_auth_client) | |
| 143 : UnlockManager(screenlock_type, | |
| 144 proximity_monitor.Pass(), | |
| 145 proximity_auth_client) {} | |
| 146 ~TestUnlockManager() override {} | |
| 147 | |
| 148 using UnlockManager::OnAuthAttempted; | |
| 149 using ClientObserver::OnUnlockEventSent; | |
| 150 using ClientObserver::OnRemoteStatusUpdate; | |
| 151 using ClientObserver::OnDecryptResponse; | |
| 152 using ClientObserver::OnUnlockResponse; | |
| 153 using ClientObserver::OnDisconnected; | |
| 154 using ScreenlockBridge::Observer::OnScreenDidLock; | |
| 155 using ScreenlockBridge::Observer::OnScreenDidUnlock; | |
| 156 using ScreenlockBridge::Observer::OnFocusedUserChanged; | |
| 157 }; | |
| 158 | |
| 159 // Creates a mock Bluetooth adapter and sets it as the global adapter for | |
| 160 // testing. | |
| 161 scoped_refptr<device::MockBluetoothAdapter> | |
| 162 CreateAndRegisterMockBluetoothAdapter() { | |
| 163 scoped_refptr<device::MockBluetoothAdapter> adapter = | |
| 164 new NiceMock<device::MockBluetoothAdapter>(); | |
| 165 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter); | |
| 166 return adapter; | |
| 167 } | |
| 168 | |
| 169 } // namespace | |
| 170 | |
| 171 class ProximityAuthUnlockManagerTest : public testing::Test { | |
| 172 public: | |
| 173 ProximityAuthUnlockManagerTest() | |
| 174 : bluetooth_adapter_(CreateAndRegisterMockBluetoothAdapter()), | |
| 175 proximity_monitor_(nullptr), | |
| 176 task_runner_(new base::TestSimpleTaskRunner()), | |
| 177 thread_task_runner_handle_(task_runner_) { | |
| 178 ON_CALL(*bluetooth_adapter_, IsPowered()).WillByDefault(Return(true)); | |
| 179 ON_CALL(controller_, GetClient()).WillByDefault(Return(&client_)); | |
| 180 ON_CALL(client_, SupportsSignIn()).WillByDefault(Return(true)); | |
| 181 | |
| 182 ScreenlockBridge::Get()->SetLockHandler(&lock_handler_); | |
| 183 | |
| 184 #if defined(OS_CHROMEOS) | |
| 185 chromeos::DBusThreadManager::Initialize(); | |
| 186 #endif | |
| 187 } | |
| 188 | |
| 189 ~ProximityAuthUnlockManagerTest() override { | |
| 190 // Make sure to verify the mock prior to the destruction of the unlock | |
| 191 // manager, as otherwise it's impossible to tell whether calls to Stop() | |
| 192 // occur as a side-effect of the destruction or from the code intended to be | |
| 193 // under test. | |
| 194 if (proximity_monitor_) | |
| 195 testing::Mock::VerifyAndClearExpectations(proximity_monitor_); | |
| 196 | |
| 197 #if defined(OS_CHROMEOS) | |
| 198 chromeos::DBusThreadManager::Shutdown(); | |
| 199 #endif | |
| 200 | |
| 201 ScreenlockBridge::Get()->SetLockHandler(nullptr); | |
| 202 } | |
| 203 | |
| 204 void CreateUnlockManager(UnlockManager::ScreenlockType screenlock_type) { | |
| 205 proximity_monitor_ = new NiceMock<MockProximityMonitor>; | |
| 206 unlock_manager_.reset(new TestUnlockManager( | |
| 207 screenlock_type, make_scoped_ptr(proximity_monitor_), | |
| 208 &proximity_auth_client_)); | |
| 209 } | |
| 210 | |
| 211 void SimulateUserPresentState() { | |
| 212 ON_CALL(controller_, GetState()) | |
| 213 .WillByDefault(Return(Controller::State::STOPPED)); | |
| 214 unlock_manager_->SetController(&controller_); | |
| 215 | |
| 216 ON_CALL(controller_, GetState()) | |
| 217 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED)); | |
| 218 unlock_manager_->OnControllerStateChanged(); | |
| 219 | |
| 220 unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenUnlocked); | |
| 221 } | |
| 222 | |
| 223 void RunPendingTasks() { task_runner_->RunPendingTasks(); } | |
| 224 | |
| 225 protected: | |
| 226 // Mock used for verifying interactions with the Bluetooth subsystem. | |
| 227 scoped_refptr<device::MockBluetoothAdapter> bluetooth_adapter_; | |
| 228 | |
| 229 NiceMock<MockProximityAuthClient> proximity_auth_client_; | |
| 230 NiceMock<MockController> controller_; | |
| 231 NiceMock<MockClient> client_; | |
| 232 scoped_ptr<TestUnlockManager> unlock_manager_; | |
| 233 // Owned by the |unlock_manager_|. | |
| 234 MockProximityMonitor* proximity_monitor_; | |
| 235 | |
| 236 private: | |
| 237 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | |
| 238 base::ThreadTaskRunnerHandle thread_task_runner_handle_; | |
| 239 FakeLockHandler lock_handler_; | |
| 240 ScopedDisableLoggingForTesting disable_logging_; | |
| 241 }; | |
| 242 | |
| 243 TEST_F(ProximityAuthUnlockManagerTest, IsUnlockAllowed_InitialState) { | |
| 244 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 245 EXPECT_FALSE(unlock_manager_->IsUnlockAllowed()); | |
| 246 } | |
| 247 | |
| 248 TEST_F(ProximityAuthUnlockManagerTest, IsUnlockAllowed_SessionLock_AllGood) { | |
| 249 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 250 | |
| 251 ON_CALL(controller_, GetState()) | |
|
Tim Song
2015/07/23 21:32:15
Wouldn't it be easier to make the controller a fak
Ilya Sherman
2015/08/11 23:37:15
How would you structure the fake? Provide a set_s
| |
| 252 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED)); | |
| 253 unlock_manager_->SetController(&controller_); | |
| 254 unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenUnlocked); | |
| 255 | |
| 256 EXPECT_TRUE(unlock_manager_->IsUnlockAllowed()); | |
| 257 } | |
| 258 | |
| 259 TEST_F(ProximityAuthUnlockManagerTest, IsUnlockAllowed_SignIn_AllGood) { | |
| 260 CreateUnlockManager(UnlockManager::ScreenlockType::SIGN_IN); | |
| 261 | |
| 262 ON_CALL(controller_, GetState()) | |
|
Tim Song
2015/07/23 21:32:16
Can't you replace these lines SimulateUserPresentS
Ilya Sherman
2015/08/11 23:37:15
My goal was to have each test case be pretty expli
| |
| 263 .WillByDefault(Return(Controller::State::STOPPED)); | |
| 264 unlock_manager_->SetController(&controller_); | |
| 265 | |
| 266 ON_CALL(controller_, GetState()) | |
| 267 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED)); | |
| 268 unlock_manager_->OnControllerStateChanged(); | |
| 269 | |
| 270 ON_CALL(client_, SupportsSignIn()).WillByDefault(Return(true)); | |
| 271 unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenUnlocked); | |
| 272 | |
| 273 EXPECT_TRUE(unlock_manager_->IsUnlockAllowed()); | |
| 274 } | |
| 275 | |
| 276 TEST_F(ProximityAuthUnlockManagerTest, | |
| 277 IsUnlockAllowed_SignIn_ClientDoesNotSupportSignIn) { | |
| 278 CreateUnlockManager(UnlockManager::ScreenlockType::SIGN_IN); | |
| 279 | |
| 280 ON_CALL(controller_, GetState()) | |
| 281 .WillByDefault(Return(Controller::State::STOPPED)); | |
| 282 unlock_manager_->SetController(&controller_); | |
| 283 | |
| 284 ON_CALL(controller_, GetState()) | |
| 285 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED)); | |
| 286 unlock_manager_->OnControllerStateChanged(); | |
| 287 | |
| 288 ON_CALL(client_, SupportsSignIn()).WillByDefault(Return(false)); | |
| 289 unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenUnlocked); | |
| 290 | |
| 291 EXPECT_FALSE(unlock_manager_->IsUnlockAllowed()); | |
| 292 } | |
| 293 | |
| 294 TEST_F(ProximityAuthUnlockManagerTest, IsUnlockAllowed_SignIn_ClientIsNull) { | |
| 295 CreateUnlockManager(UnlockManager::ScreenlockType::SIGN_IN); | |
| 296 | |
| 297 ON_CALL(controller_, GetState()) | |
| 298 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED)); | |
|
Tim Song
2015/07/23 21:32:15
Don't you need to make controller_->GetClient() re
Ilya Sherman
2015/08/11 23:37:15
Done.
| |
| 299 unlock_manager_->SetController(&controller_); | |
| 300 unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenUnlocked); | |
| 301 | |
| 302 EXPECT_FALSE(unlock_manager_->IsUnlockAllowed()); | |
| 303 } | |
| 304 | |
| 305 TEST_F(ProximityAuthUnlockManagerTest, | |
| 306 IsUnlockAllowed_DisallowedByProximityMonitor) { | |
| 307 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 308 | |
| 309 ON_CALL(controller_, GetState()) | |
| 310 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED)); | |
| 311 unlock_manager_->SetController(&controller_); | |
| 312 unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenUnlocked); | |
| 313 | |
| 314 ON_CALL(*proximity_monitor_, IsUnlockAllowed()).WillByDefault(Return(false)); | |
| 315 EXPECT_FALSE(unlock_manager_->IsUnlockAllowed()); | |
| 316 } | |
| 317 | |
| 318 TEST_F(ProximityAuthUnlockManagerTest, | |
| 319 IsUnlockAllowed_SecureChannelNotEstablished) { | |
| 320 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 321 | |
| 322 ON_CALL(controller_, GetState()) | |
| 323 .WillByDefault(Return(Controller::State::AUTHENTICATING)); | |
| 324 unlock_manager_->SetController(&controller_); | |
| 325 unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenUnlocked); | |
| 326 | |
| 327 EXPECT_FALSE(unlock_manager_->IsUnlockAllowed()); | |
| 328 } | |
| 329 | |
| 330 TEST_F(ProximityAuthUnlockManagerTest, IsUnlockAllowed_ControllerIsNull) { | |
| 331 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 332 | |
| 333 unlock_manager_->SetController(nullptr); | |
| 334 unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenUnlocked); | |
| 335 | |
| 336 EXPECT_FALSE(unlock_manager_->IsUnlockAllowed()); | |
| 337 } | |
| 338 | |
| 339 TEST_F(ProximityAuthUnlockManagerTest, | |
| 340 IsUnlockAllowed_RemoteScreenlockStateLocked) { | |
| 341 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 342 | |
| 343 ON_CALL(controller_, GetState()) | |
| 344 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED)); | |
| 345 unlock_manager_->SetController(&controller_); | |
| 346 unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenLocked); | |
| 347 | |
| 348 EXPECT_FALSE(unlock_manager_->IsUnlockAllowed()); | |
| 349 } | |
| 350 | |
| 351 TEST_F(ProximityAuthUnlockManagerTest, | |
| 352 IsUnlockAllowed_RemoteScreenlockStateUnknown) { | |
| 353 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 354 | |
| 355 ON_CALL(controller_, GetState()) | |
| 356 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED)); | |
| 357 unlock_manager_->SetController(&controller_); | |
| 358 unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenlockStateUnknown); | |
| 359 | |
| 360 EXPECT_FALSE(unlock_manager_->IsUnlockAllowed()); | |
| 361 } | |
| 362 | |
| 363 TEST_F(ProximityAuthUnlockManagerTest, | |
| 364 IsUnlockAllowed_RemoteScreenlockStateDisabled) { | |
| 365 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 366 | |
| 367 ON_CALL(controller_, GetState()) | |
| 368 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED)); | |
| 369 unlock_manager_->SetController(&controller_); | |
| 370 unlock_manager_->OnRemoteStatusUpdate(kRemoteScreenlockDisabled); | |
| 371 | |
| 372 EXPECT_FALSE(unlock_manager_->IsUnlockAllowed()); | |
| 373 } | |
| 374 | |
| 375 TEST_F(ProximityAuthUnlockManagerTest, | |
| 376 IsUnlockAllowed_RemoteScreenlockStateNotYetReceived) { | |
| 377 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 378 | |
| 379 ON_CALL(controller_, GetState()) | |
| 380 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED)); | |
| 381 unlock_manager_->SetController(&controller_); | |
| 382 | |
| 383 EXPECT_FALSE(unlock_manager_->IsUnlockAllowed()); | |
| 384 } | |
| 385 | |
| 386 TEST_F(ProximityAuthUnlockManagerTest, SetController_SetToNull) { | |
| 387 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 388 SimulateUserPresentState(); | |
| 389 | |
| 390 EXPECT_CALL(proximity_auth_client_, | |
| 391 UpdateScreenlockState(ScreenlockState::INACTIVE)); | |
| 392 unlock_manager_->SetController(nullptr); | |
| 393 } | |
| 394 | |
| 395 TEST_F(ProximityAuthUnlockManagerTest, SetController_ExistingController) { | |
| 396 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 397 SimulateUserPresentState(); | |
| 398 | |
| 399 EXPECT_CALL(proximity_auth_client_, UpdateScreenlockState(_)).Times(0); | |
| 400 unlock_manager_->SetController(&controller_); | |
| 401 } | |
| 402 | |
| 403 TEST_F(ProximityAuthUnlockManagerTest, | |
| 404 SetController_NullThenExistingController) { | |
| 405 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 406 SimulateUserPresentState(); | |
| 407 | |
| 408 unlock_manager_->SetController(nullptr); | |
|
Tim Song
2015/07/23 21:32:16
nit: add an expectation for for the INACTIVE state
Ilya Sherman
2015/08/11 23:37:15
Done.
| |
| 409 | |
| 410 EXPECT_CALL(proximity_auth_client_, | |
| 411 UpdateScreenlockState(ScreenlockState::AUTHENTICATED)); | |
| 412 unlock_manager_->SetController(&controller_); | |
| 413 } | |
| 414 | |
| 415 TEST_F(ProximityAuthUnlockManagerTest, SetController_AuthenticationFailed) { | |
| 416 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 417 SimulateUserPresentState(); | |
| 418 | |
| 419 unlock_manager_->SetController(nullptr); | |
|
Tim Song
2015/07/23 21:32:15
Why simulate the user present state at all for thi
Ilya Sherman
2015/08/11 23:37:15
The idea is to test going from a good state to a b
| |
| 420 | |
| 421 ON_CALL(controller_, GetState()) | |
| 422 .WillByDefault(Return(Controller::State::AUTHENTICATION_FAILED)); | |
| 423 EXPECT_CALL(proximity_auth_client_, | |
| 424 UpdateScreenlockState(ScreenlockState::PHONE_NOT_AUTHENTICATED)); | |
| 425 unlock_manager_->SetController(&controller_); | |
| 426 } | |
| 427 | |
| 428 TEST_F(ProximityAuthUnlockManagerTest, SetController_WakingUp) { | |
| 429 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 430 SimulateUserPresentState(); | |
| 431 | |
| 432 unlock_manager_->SetController(nullptr); | |
| 433 | |
| 434 ON_CALL(controller_, GetState()) | |
| 435 .WillByDefault(Return(Controller::State::FINDING_CONNECTION)); | |
| 436 EXPECT_CALL(proximity_auth_client_, | |
| 437 UpdateScreenlockState(ScreenlockState::BLUETOOTH_CONNECTING)); | |
| 438 unlock_manager_->SetController(&controller_); | |
| 439 } | |
| 440 | |
| 441 TEST_F(ProximityAuthUnlockManagerTest, | |
| 442 SetController_NullController_StopsProximityMonitor) { | |
| 443 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 444 SimulateUserPresentState(); | |
| 445 | |
| 446 EXPECT_CALL(*proximity_monitor_, Stop()).Times(AtLeast(1)); | |
| 447 unlock_manager_->SetController(nullptr); | |
| 448 } | |
| 449 | |
| 450 TEST_F(ProximityAuthUnlockManagerTest, | |
| 451 SetController_ConnectingController_StopsProximityMonitor) { | |
| 452 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 453 SimulateUserPresentState(); | |
| 454 | |
| 455 NiceMock<MockController> controller; | |
| 456 ON_CALL(controller, GetState()) | |
| 457 .WillByDefault(Return(Controller::State::FINDING_CONNECTION)); | |
| 458 | |
| 459 EXPECT_CALL(*proximity_monitor_, Stop()).Times(AtLeast(1)); | |
| 460 unlock_manager_->SetController(&controller); | |
| 461 } | |
| 462 | |
| 463 TEST_F(ProximityAuthUnlockManagerTest, | |
| 464 SetController_ConnectedController_StartsProximityMonitor) { | |
| 465 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 466 SimulateUserPresentState(); | |
| 467 | |
| 468 NiceMock<MockController> controller; | |
| 469 ON_CALL(controller, GetState()) | |
| 470 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED)); | |
| 471 | |
| 472 EXPECT_CALL(*proximity_monitor_, Start()).Times(AtLeast(1)); | |
| 473 unlock_manager_->SetController(&controller); | |
| 474 } | |
| 475 | |
| 476 TEST_F(ProximityAuthUnlockManagerTest, | |
| 477 OnControllerStateChanged_SecureChannelEstablished_RegistersAsObserver) { | |
| 478 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 479 SimulateUserPresentState(); | |
| 480 | |
| 481 NiceMock<MockController> controller; | |
| 482 ON_CALL(controller, GetState()) | |
| 483 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED)); | |
| 484 | |
| 485 EXPECT_CALL(client_, AddObserver(unlock_manager_.get())); | |
| 486 unlock_manager_->OnControllerStateChanged(); | |
| 487 } | |
| 488 | |
| 489 TEST_F(ProximityAuthUnlockManagerTest, | |
| 490 OnControllerStateChanged_StartsProximityMonitor) { | |
| 491 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 492 SimulateUserPresentState(); | |
| 493 | |
| 494 NiceMock<MockController> controller; | |
| 495 ON_CALL(controller, GetState()) | |
| 496 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED)); | |
| 497 | |
| 498 EXPECT_CALL(*proximity_monitor_, Start()).Times(AtLeast(1)); | |
| 499 unlock_manager_->OnControllerStateChanged(); | |
| 500 } | |
| 501 | |
| 502 TEST_F(ProximityAuthUnlockManagerTest, | |
| 503 OnControllerStateChanged_StopsProximityMonitor) { | |
| 504 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 505 SimulateUserPresentState(); | |
| 506 | |
| 507 ON_CALL(controller_, GetState()) | |
| 508 .WillByDefault(Return(Controller::State::AUTHENTICATION_FAILED)); | |
| 509 | |
| 510 EXPECT_CALL(*proximity_monitor_, Stop()).Times(AtLeast(1)); | |
| 511 unlock_manager_->OnControllerStateChanged(); | |
| 512 } | |
| 513 | |
| 514 TEST_F(ProximityAuthUnlockManagerTest, | |
| 515 OnControllerStateChanged_Stopped_UpdatesScreenlockState) { | |
| 516 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 517 SimulateUserPresentState(); | |
| 518 | |
| 519 ON_CALL(controller_, GetState()) | |
| 520 .WillByDefault(Return(Controller::State::STOPPED)); | |
| 521 | |
| 522 EXPECT_CALL(proximity_auth_client_, | |
| 523 UpdateScreenlockState(ScreenlockState::INACTIVE)); | |
| 524 unlock_manager_->OnControllerStateChanged(); | |
| 525 } | |
| 526 | |
| 527 TEST_F(ProximityAuthUnlockManagerTest, | |
| 528 OnControllerStateChanged_AuthenticationFailed_UpdatesScreenlockState) { | |
| 529 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 530 SimulateUserPresentState(); | |
| 531 | |
| 532 ON_CALL(controller_, GetState()) | |
| 533 .WillByDefault(Return(Controller::State::AUTHENTICATION_FAILED)); | |
| 534 | |
| 535 EXPECT_CALL(proximity_auth_client_, | |
| 536 UpdateScreenlockState(ScreenlockState::PHONE_NOT_AUTHENTICATED)); | |
| 537 unlock_manager_->OnControllerStateChanged(); | |
| 538 } | |
| 539 | |
| 540 TEST_F(ProximityAuthUnlockManagerTest, | |
| 541 OnControllerStateChanged_FindingConnection_UpdatesScreenlockState) { | |
| 542 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 543 | |
| 544 ON_CALL(controller_, GetState()) | |
| 545 .WillByDefault(Return(Controller::State::STOPPED)); | |
| 546 unlock_manager_->SetController(&controller_); | |
| 547 | |
| 548 ON_CALL(controller_, GetState()) | |
| 549 .WillByDefault(Return(Controller::State::FINDING_CONNECTION)); | |
| 550 | |
| 551 EXPECT_CALL(proximity_auth_client_, | |
| 552 UpdateScreenlockState(ScreenlockState::BLUETOOTH_CONNECTING)); | |
| 553 unlock_manager_->OnControllerStateChanged(); | |
| 554 } | |
| 555 | |
| 556 TEST_F(ProximityAuthUnlockManagerTest, | |
| 557 OnControllerStateChanged_Authenticating_UpdatesScreenlockState) { | |
| 558 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 559 | |
| 560 ON_CALL(controller_, GetState()) | |
| 561 .WillByDefault(Return(Controller::State::STOPPED)); | |
| 562 unlock_manager_->SetController(&controller_); | |
| 563 | |
| 564 ON_CALL(controller_, GetState()) | |
| 565 .WillByDefault(Return(Controller::State::AUTHENTICATING)); | |
| 566 | |
| 567 EXPECT_CALL(proximity_auth_client_, | |
| 568 UpdateScreenlockState(ScreenlockState::BLUETOOTH_CONNECTING)); | |
| 569 unlock_manager_->OnControllerStateChanged(); | |
| 570 } | |
| 571 | |
| 572 TEST_F( | |
| 573 ProximityAuthUnlockManagerTest, | |
| 574 OnControllerStateChanged_SecureChannelEstablished_UpdatesScreenlockState) { | |
| 575 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 576 | |
| 577 ON_CALL(controller_, GetState()) | |
| 578 .WillByDefault(Return(Controller::State::STOPPED)); | |
| 579 unlock_manager_->SetController(&controller_); | |
| 580 | |
| 581 ON_CALL(controller_, GetState()) | |
| 582 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED)); | |
| 583 | |
| 584 EXPECT_CALL(proximity_auth_client_, | |
| 585 UpdateScreenlockState(ScreenlockState::BLUETOOTH_CONNECTING)); | |
| 586 unlock_manager_->OnControllerStateChanged(); | |
| 587 } | |
| 588 | |
| 589 TEST_F(ProximityAuthUnlockManagerTest, OnDisconnected_UnregistersAsObserver) { | |
| 590 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 591 SimulateUserPresentState(); | |
| 592 | |
| 593 ON_CALL(controller_, GetState()) | |
| 594 .WillByDefault(Return(Controller::State::AUTHENTICATION_FAILED)); | |
| 595 | |
| 596 EXPECT_CALL(client_, RemoveObserver(unlock_manager_.get())); | |
| 597 unlock_manager_.get()->OnDisconnected(); | |
| 598 } | |
| 599 | |
| 600 TEST_F(ProximityAuthUnlockManagerTest, | |
| 601 OnScreenDidUnlock_StopsProximityMonitor) { | |
| 602 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 603 SimulateUserPresentState(); | |
| 604 | |
| 605 EXPECT_CALL(*proximity_monitor_, Stop()); | |
| 606 unlock_manager_.get()->OnScreenDidUnlock( | |
| 607 ScreenlockBridge::LockHandler::LOCK_SCREEN); | |
| 608 } | |
| 609 | |
| 610 TEST_F(ProximityAuthUnlockManagerTest, OnScreenDidLock_StartsProximityMonitor) { | |
| 611 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 612 | |
| 613 ON_CALL(controller_, GetState()) | |
| 614 .WillByDefault(Return(Controller::State::STOPPED)); | |
| 615 unlock_manager_->SetController(&controller_); | |
| 616 | |
| 617 ON_CALL(controller_, GetState()) | |
| 618 .WillByDefault(Return(Controller::State::SECURE_CHANNEL_ESTABLISHED)); | |
| 619 unlock_manager_->OnControllerStateChanged(); | |
| 620 | |
| 621 EXPECT_CALL(*proximity_monitor_, Start()); | |
| 622 unlock_manager_.get()->OnScreenDidLock( | |
| 623 ScreenlockBridge::LockHandler::LOCK_SCREEN); | |
| 624 } | |
| 625 | |
| 626 TEST_F(ProximityAuthUnlockManagerTest, OnScreenDidLock_SetsWakingUpState) { | |
| 627 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 628 SimulateUserPresentState(); | |
| 629 | |
| 630 unlock_manager_.get()->OnScreenDidUnlock( | |
| 631 ScreenlockBridge::LockHandler::LOCK_SCREEN); | |
| 632 | |
| 633 ON_CALL(controller_, GetState()) | |
| 634 .WillByDefault(Return(Controller::State::FINDING_CONNECTION)); | |
| 635 unlock_manager_->OnControllerStateChanged(); | |
| 636 | |
| 637 EXPECT_CALL(proximity_auth_client_, | |
| 638 UpdateScreenlockState(ScreenlockState::BLUETOOTH_CONNECTING)); | |
| 639 unlock_manager_.get()->OnScreenDidLock( | |
| 640 ScreenlockBridge::LockHandler::LOCK_SCREEN); | |
| 641 } | |
| 642 | |
| 643 TEST_F(ProximityAuthUnlockManagerTest, | |
| 644 OnDecryptResponse_NoAuthAttemptInProgress) { | |
| 645 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 646 SimulateUserPresentState(); | |
| 647 | |
| 648 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(_)).Times(0); | |
| 649 unlock_manager_.get()->OnDecryptResponse(nullptr); | |
| 650 } | |
| 651 | |
| 652 TEST_F(ProximityAuthUnlockManagerTest, | |
| 653 OnUnlockEventSent_NoAuthAttemptInProgress) { | |
| 654 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 655 SimulateUserPresentState(); | |
| 656 | |
| 657 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(_)).Times(0); | |
| 658 unlock_manager_.get()->OnUnlockEventSent(true); | |
| 659 } | |
| 660 | |
| 661 TEST_F(ProximityAuthUnlockManagerTest, | |
| 662 OnUnlockResponse_NoAuthAttemptInProgress) { | |
| 663 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 664 SimulateUserPresentState(); | |
| 665 | |
| 666 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(_)).Times(0); | |
| 667 unlock_manager_.get()->OnUnlockResponse(true); | |
| 668 } | |
| 669 | |
| 670 TEST_F(ProximityAuthUnlockManagerTest, OnAuthAttempted_NoController) { | |
| 671 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 672 SimulateUserPresentState(); | |
| 673 | |
| 674 unlock_manager_->SetController(nullptr); | |
| 675 | |
| 676 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(false)); | |
| 677 unlock_manager_->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK); | |
| 678 } | |
| 679 | |
| 680 TEST_F(ProximityAuthUnlockManagerTest, OnAuthAttempted_UnlockNotAllowed) { | |
| 681 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 682 SimulateUserPresentState(); | |
| 683 | |
| 684 ON_CALL(*proximity_monitor_, IsUnlockAllowed()).WillByDefault(Return(false)); | |
| 685 | |
| 686 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(false)); | |
| 687 unlock_manager_->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK); | |
| 688 } | |
| 689 | |
| 690 TEST_F(ProximityAuthUnlockManagerTest, OnAuthAttempted_NotUserClick) { | |
| 691 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 692 SimulateUserPresentState(); | |
| 693 | |
| 694 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(_)).Times(0); | |
| 695 unlock_manager_->OnAuthAttempted( | |
| 696 ScreenlockBridge::LockHandler::EXPAND_THEN_USER_CLICK); | |
| 697 } | |
| 698 | |
| 699 TEST_F(ProximityAuthUnlockManagerTest, OnAuthAttempted_DuplicateCall) { | |
| 700 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 701 SimulateUserPresentState(); | |
| 702 | |
| 703 EXPECT_CALL(client_, RequestUnlock()); | |
| 704 unlock_manager_->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK); | |
| 705 | |
| 706 EXPECT_CALL(client_, RequestUnlock()).Times(0); | |
| 707 unlock_manager_->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK); | |
| 708 } | |
| 709 | |
| 710 TEST_F(ProximityAuthUnlockManagerTest, OnAuthAttempted_TimesOut) { | |
| 711 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 712 SimulateUserPresentState(); | |
| 713 | |
| 714 unlock_manager_->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK); | |
| 715 | |
| 716 // Simulate the timeout period elapsing. | |
| 717 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(false)); | |
| 718 RunPendingTasks(); | |
| 719 } | |
| 720 | |
| 721 TEST_F(ProximityAuthUnlockManagerTest, | |
| 722 OnAuthAttempted_DoesntTimeOutFollowingResponse) { | |
| 723 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 724 SimulateUserPresentState(); | |
| 725 | |
| 726 unlock_manager_->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK); | |
| 727 | |
| 728 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(_)); | |
| 729 unlock_manager_->OnUnlockResponse(false); | |
| 730 | |
| 731 // Simulate the timeout period elapsing. | |
| 732 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(_)).Times(0); | |
| 733 RunPendingTasks(); | |
| 734 } | |
| 735 | |
| 736 TEST_F(ProximityAuthUnlockManagerTest, | |
| 737 OnAuthAttempted_Unlock_SupportsSignIn_UnlockRequestFails) { | |
| 738 ON_CALL(client_, SupportsSignIn()).WillByDefault(Return(true)); | |
| 739 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 740 SimulateUserPresentState(); | |
| 741 | |
| 742 EXPECT_CALL(client_, RequestUnlock()); | |
| 743 unlock_manager_->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK); | |
| 744 | |
| 745 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(false)); | |
| 746 unlock_manager_->OnUnlockResponse(false); | |
| 747 } | |
| 748 | |
| 749 TEST_F( | |
| 750 ProximityAuthUnlockManagerTest, | |
| 751 OnAuthAttempted_Unlock_SupportsSignIn_UnlockRequestSucceeds_UnlockEventSendF ails) { | |
|
Tim Song
2015/07/23 21:32:15
This formatting is kind of weird. Perhaps there is
Ilya Sherman
2015/08/11 23:37:15
Done.
| |
| 752 ON_CALL(client_, SupportsSignIn()).WillByDefault(Return(true)); | |
| 753 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 754 SimulateUserPresentState(); | |
| 755 | |
| 756 EXPECT_CALL(client_, RequestUnlock()); | |
| 757 unlock_manager_->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK); | |
| 758 | |
| 759 EXPECT_CALL(client_, DispatchUnlockEvent()); | |
| 760 unlock_manager_->OnUnlockResponse(true); | |
| 761 | |
| 762 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(false)); | |
| 763 unlock_manager_->OnUnlockEventSent(false); | |
| 764 } | |
| 765 | |
| 766 TEST_F( | |
| 767 ProximityAuthUnlockManagerTest, | |
| 768 OnAuthAttempted_Unlock_SupportsSignIn_UnlockRequestSucceeds_UnlockEventSendS ucceeds) { | |
|
Tim Song
2015/07/23 21:32:15
same here.
Ilya Sherman
2015/08/11 23:37:15
Done.
| |
| 769 ON_CALL(client_, SupportsSignIn()).WillByDefault(Return(true)); | |
| 770 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 771 SimulateUserPresentState(); | |
| 772 | |
| 773 EXPECT_CALL(client_, RequestUnlock()); | |
| 774 unlock_manager_->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK); | |
| 775 | |
| 776 EXPECT_CALL(client_, DispatchUnlockEvent()); | |
| 777 unlock_manager_->OnUnlockResponse(true); | |
| 778 | |
| 779 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(true)); | |
| 780 unlock_manager_->OnUnlockEventSent(true); | |
| 781 } | |
| 782 | |
| 783 TEST_F(ProximityAuthUnlockManagerTest, | |
| 784 OnAuthAttempted_Unlock_DoesntSupportSignIn_UnlockEventSendFails) { | |
| 785 ON_CALL(client_, SupportsSignIn()).WillByDefault(Return(false)); | |
| 786 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 787 SimulateUserPresentState(); | |
| 788 | |
| 789 EXPECT_CALL(client_, DispatchUnlockEvent()); | |
| 790 unlock_manager_->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK); | |
| 791 | |
| 792 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(false)); | |
| 793 unlock_manager_->OnUnlockEventSent(false); | |
| 794 } | |
| 795 | |
| 796 TEST_F(ProximityAuthUnlockManagerTest, | |
| 797 OnAuthAttempted_Unlock_SupportsSignIn_UnlockEventSendSucceeds) { | |
| 798 ON_CALL(client_, SupportsSignIn()).WillByDefault(Return(false)); | |
| 799 CreateUnlockManager(UnlockManager::ScreenlockType::SESSION_LOCK); | |
| 800 SimulateUserPresentState(); | |
| 801 | |
| 802 EXPECT_CALL(client_, DispatchUnlockEvent()); | |
| 803 unlock_manager_->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK); | |
| 804 | |
| 805 EXPECT_CALL(proximity_auth_client_, FinalizeUnlock(true)); | |
| 806 unlock_manager_->OnUnlockEventSent(true); | |
| 807 } | |
| 808 | |
| 809 } // namespace proximity_auth | |
| OLD | NEW |