| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/proximity_auth/proximity_auth_system.h" | 5 #include "components/proximity_auth/proximity_auth_system.h" |
| 6 | 6 |
| 7 #include "base/test/simple_test_clock.h" |
| 7 #include "base/test/test_simple_task_runner.h" | 8 #include "base/test/test_simple_task_runner.h" |
| 8 #include "base/threading/thread_task_runner_handle.h" | 9 #include "base/threading/thread_task_runner_handle.h" |
| 9 #include "components/cryptauth/remote_device.h" | 10 #include "components/cryptauth/remote_device.h" |
| 10 #include "components/proximity_auth/fake_lock_handler.h" | 11 #include "components/proximity_auth/fake_lock_handler.h" |
| 11 #include "components/proximity_auth/fake_remote_device_life_cycle.h" | 12 #include "components/proximity_auth/fake_remote_device_life_cycle.h" |
| 12 #include "components/proximity_auth/logging/logging.h" | 13 #include "components/proximity_auth/logging/logging.h" |
| 13 #include "components/proximity_auth/mock_proximity_auth_client.h" | 14 #include "components/proximity_auth/mock_proximity_auth_client.h" |
| 15 #include "components/proximity_auth/proximity_auth_pref_manager.h" |
| 14 #include "components/proximity_auth/unlock_manager.h" | 16 #include "components/proximity_auth/unlock_manager.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 19 |
| 18 using cryptauth::RemoteDevice; | 20 using cryptauth::RemoteDevice; |
| 19 using cryptauth::RemoteDeviceList; | 21 using cryptauth::RemoteDeviceList; |
| 20 using testing::AnyNumber; | 22 using testing::AnyNumber; |
| 21 using testing::AtLeast; | 23 using testing::AtLeast; |
| 22 using testing::InSequence; | 24 using testing::InSequence; |
| 23 using testing::NiceMock; | 25 using testing::NiceMock; |
| 24 using testing::NotNull; | 26 using testing::NotNull; |
| 25 using testing::Return; | 27 using testing::Return; |
| 26 using testing::SaveArg; | 28 using testing::SaveArg; |
| 27 using testing::_; | 29 using testing::_; |
| 28 | 30 |
| 29 namespace proximity_auth { | 31 namespace proximity_auth { |
| 30 | 32 |
| 31 namespace { | 33 namespace { |
| 32 | 34 |
| 33 const char kUser1[] = "user1"; | 35 const char kUser1[] = "user1"; |
| 34 const char kUser2[] = "user2"; | 36 const char kUser2[] = "user2"; |
| 35 | 37 |
| 38 const int64_t kLastPasswordEntryTimestampMs = 123456L; |
| 39 const int64_t kTimestampBeforeReauthMs = 123457L; |
| 40 const int64_t kTimestampAfterReauthMs = 123457890123L; |
| 41 |
| 36 void CompareRemoteDeviceLists(const RemoteDeviceList& list1, | 42 void CompareRemoteDeviceLists(const RemoteDeviceList& list1, |
| 37 const RemoteDeviceList& list2) { | 43 const RemoteDeviceList& list2) { |
| 38 ASSERT_EQ(list1.size(), list2.size()); | 44 ASSERT_EQ(list1.size(), list2.size()); |
| 39 for (size_t i = 0; i < list1.size(); ++i) { | 45 for (size_t i = 0; i < list1.size(); ++i) { |
| 40 RemoteDevice device1 = list1[i]; | 46 RemoteDevice device1 = list1[i]; |
| 41 RemoteDevice device2 = list2[i]; | 47 RemoteDevice device2 = list2[i]; |
| 42 EXPECT_EQ(device1.public_key, device2.public_key); | 48 EXPECT_EQ(device1.public_key, device2.public_key); |
| 43 } | 49 } |
| 44 } | 50 } |
| 45 | 51 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 57 ~MockUnlockManager() override {} | 63 ~MockUnlockManager() override {} |
| 58 MOCK_METHOD0(IsUnlockAllowed, bool()); | 64 MOCK_METHOD0(IsUnlockAllowed, bool()); |
| 59 MOCK_METHOD1(SetRemoteDeviceLifeCycle, void(RemoteDeviceLifeCycle*)); | 65 MOCK_METHOD1(SetRemoteDeviceLifeCycle, void(RemoteDeviceLifeCycle*)); |
| 60 MOCK_METHOD0(OnLifeCycleStateChanged, void()); | 66 MOCK_METHOD0(OnLifeCycleStateChanged, void()); |
| 61 MOCK_METHOD1(OnAuthAttempted, void(ScreenlockBridge::LockHandler::AuthType)); | 67 MOCK_METHOD1(OnAuthAttempted, void(ScreenlockBridge::LockHandler::AuthType)); |
| 62 | 68 |
| 63 private: | 69 private: |
| 64 DISALLOW_COPY_AND_ASSIGN(MockUnlockManager); | 70 DISALLOW_COPY_AND_ASSIGN(MockUnlockManager); |
| 65 }; | 71 }; |
| 66 | 72 |
| 73 // Mock implementation of ProximityAuthPrefManager. |
| 74 class MockProximityAuthPrefManager : public ProximityAuthPrefManager { |
| 75 public: |
| 76 MockProximityAuthPrefManager() : ProximityAuthPrefManager(nullptr) {} |
| 77 ~MockProximityAuthPrefManager() override {} |
| 78 MOCK_CONST_METHOD0(GetLastPasswordEntryTimestampMs, int64_t()); |
| 79 |
| 80 private: |
| 81 DISALLOW_COPY_AND_ASSIGN(MockProximityAuthPrefManager); |
| 82 }; |
| 83 |
| 67 // Harness for ProximityAuthSystem to make it testable. | 84 // Harness for ProximityAuthSystem to make it testable. |
| 68 class TestableProximityAuthSystem : public ProximityAuthSystem { | 85 class TestableProximityAuthSystem : public ProximityAuthSystem { |
| 69 public: | 86 public: |
| 70 TestableProximityAuthSystem(ScreenlockType screenlock_type, | 87 TestableProximityAuthSystem( |
| 71 ProximityAuthClient* proximity_auth_client, | 88 ScreenlockType screenlock_type, |
| 72 std::unique_ptr<UnlockManager> unlock_manager) | 89 ProximityAuthClient* proximity_auth_client, |
| 90 std::unique_ptr<UnlockManager> unlock_manager, |
| 91 std::unique_ptr<base::Clock> clock, |
| 92 std::unique_ptr<ProximityAuthPrefManager> pref_manager) |
| 73 : ProximityAuthSystem(screenlock_type, | 93 : ProximityAuthSystem(screenlock_type, |
| 74 proximity_auth_client, | 94 proximity_auth_client, |
| 75 std::move(unlock_manager)), | 95 std::move(unlock_manager), |
| 96 std::move(clock), |
| 97 std::move(pref_manager)), |
| 76 life_cycle_(nullptr) {} | 98 life_cycle_(nullptr) {} |
| 77 ~TestableProximityAuthSystem() override {} | 99 ~TestableProximityAuthSystem() override {} |
| 78 | 100 |
| 79 FakeRemoteDeviceLifeCycle* life_cycle() { return life_cycle_; } | 101 FakeRemoteDeviceLifeCycle* life_cycle() { return life_cycle_; } |
| 80 | 102 |
| 81 private: | 103 private: |
| 82 std::unique_ptr<RemoteDeviceLifeCycle> CreateRemoteDeviceLifeCycle( | 104 std::unique_ptr<RemoteDeviceLifeCycle> CreateRemoteDeviceLifeCycle( |
| 83 const RemoteDevice& remote_device) override { | 105 const RemoteDevice& remote_device) override { |
| 84 std::unique_ptr<FakeRemoteDeviceLifeCycle> life_cycle( | 106 std::unique_ptr<FakeRemoteDeviceLifeCycle> life_cycle( |
| 85 new FakeRemoteDeviceLifeCycle(remote_device)); | 107 new FakeRemoteDeviceLifeCycle(remote_device)); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 LockScreen(); | 142 LockScreen(); |
| 121 } | 143 } |
| 122 | 144 |
| 123 void TearDown() override { UnlockScreen(); } | 145 void TearDown() override { UnlockScreen(); } |
| 124 | 146 |
| 125 void InitProximityAuthSystem(ProximityAuthSystem::ScreenlockType type) { | 147 void InitProximityAuthSystem(ProximityAuthSystem::ScreenlockType type) { |
| 126 std::unique_ptr<MockUnlockManager> unlock_manager( | 148 std::unique_ptr<MockUnlockManager> unlock_manager( |
| 127 new NiceMock<MockUnlockManager>()); | 149 new NiceMock<MockUnlockManager>()); |
| 128 unlock_manager_ = unlock_manager.get(); | 150 unlock_manager_ = unlock_manager.get(); |
| 129 | 151 |
| 152 std::unique_ptr<base::SimpleTestClock> clock = |
| 153 base::MakeUnique<base::SimpleTestClock>(); |
| 154 clock_ = clock.get(); |
| 155 |
| 156 std::unique_ptr<MockProximityAuthPrefManager> pref_manager( |
| 157 new NiceMock<MockProximityAuthPrefManager>()); |
| 158 pref_manager_ = pref_manager.get(); |
| 159 |
| 160 clock_->SetNow(base::Time::FromJavaTime(kTimestampBeforeReauthMs)); |
| 161 ON_CALL(*pref_manager_, GetLastPasswordEntryTimestampMs()) |
| 162 .WillByDefault(Return(kLastPasswordEntryTimestampMs)); |
| 163 |
| 130 proximity_auth_system_.reset(new TestableProximityAuthSystem( | 164 proximity_auth_system_.reset(new TestableProximityAuthSystem( |
| 131 type, &proximity_auth_client_, std::move(unlock_manager))); | 165 type, &proximity_auth_client_, std::move(unlock_manager), |
| 166 std::move(clock), std::move(pref_manager))); |
| 132 } | 167 } |
| 133 | 168 |
| 134 void LockScreen() { ScreenlockBridge::Get()->SetLockHandler(&lock_handler_); } | 169 void LockScreen() { ScreenlockBridge::Get()->SetLockHandler(&lock_handler_); } |
| 135 | 170 |
| 136 void FocusUser(const std::string& user_id) { | 171 void FocusUser(const std::string& user_id) { |
| 137 ScreenlockBridge::Get()->SetFocusedUser(AccountId::FromUserEmail(user_id)); | 172 ScreenlockBridge::Get()->SetFocusedUser(AccountId::FromUserEmail(user_id)); |
| 138 } | 173 } |
| 139 | 174 |
| 140 void UnlockScreen() { ScreenlockBridge::Get()->SetLockHandler(nullptr); } | 175 void UnlockScreen() { ScreenlockBridge::Get()->SetLockHandler(nullptr); } |
| 141 | 176 |
| 142 void SimulateSuspend() { | 177 void SimulateSuspend() { |
| 143 proximity_auth_system_->OnSuspend(); | 178 proximity_auth_system_->OnSuspend(); |
| 144 proximity_auth_system_->OnSuspendDone(); | 179 proximity_auth_system_->OnSuspendDone(); |
| 145 task_runner_->RunUntilIdle(); | 180 task_runner_->RunUntilIdle(); |
| 146 } | 181 } |
| 147 | 182 |
| 148 FakeRemoteDeviceLifeCycle* life_cycle() { | 183 FakeRemoteDeviceLifeCycle* life_cycle() { |
| 149 return proximity_auth_system_->life_cycle(); | 184 return proximity_auth_system_->life_cycle(); |
| 150 } | 185 } |
| 151 | 186 |
| 152 FakeLockHandler lock_handler_; | 187 FakeLockHandler lock_handler_; |
| 153 NiceMock<MockProximityAuthClient> proximity_auth_client_; | 188 NiceMock<MockProximityAuthClient> proximity_auth_client_; |
| 154 std::unique_ptr<TestableProximityAuthSystem> proximity_auth_system_; | 189 std::unique_ptr<TestableProximityAuthSystem> proximity_auth_system_; |
| 155 MockUnlockManager* unlock_manager_; | 190 MockUnlockManager* unlock_manager_; |
| 191 base::SimpleTestClock* clock_; |
| 192 MockProximityAuthPrefManager* pref_manager_; |
| 156 | 193 |
| 157 RemoteDeviceList user1_remote_devices_; | 194 RemoteDeviceList user1_remote_devices_; |
| 158 RemoteDeviceList user2_remote_devices_; | 195 RemoteDeviceList user2_remote_devices_; |
| 159 | 196 |
| 160 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 197 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 161 base::ThreadTaskRunnerHandle thread_task_runner_handle_; | 198 base::ThreadTaskRunnerHandle thread_task_runner_handle_; |
| 162 | 199 |
| 163 private: | 200 private: |
| 164 ScopedDisableLoggingForTesting disable_logging_; | 201 ScopedDisableLoggingForTesting disable_logging_; |
| 165 | 202 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 | 243 |
| 207 CompareRemoteDeviceLists( | 244 CompareRemoteDeviceLists( |
| 208 user2_remote_devices_, | 245 user2_remote_devices_, |
| 209 proximity_auth_system_->GetRemoteDevicesForUser(account2)); | 246 proximity_auth_system_->GetRemoteDevicesForUser(account2)); |
| 210 } | 247 } |
| 211 | 248 |
| 212 TEST_F(ProximityAuthSystemTest, FocusRegisteredUser) { | 249 TEST_F(ProximityAuthSystemTest, FocusRegisteredUser) { |
| 213 EXPECT_FALSE(life_cycle()); | 250 EXPECT_FALSE(life_cycle()); |
| 214 EXPECT_EQ(std::string(), | 251 EXPECT_EQ(std::string(), |
| 215 ScreenlockBridge::Get()->focused_account_id().GetUserEmail()); | 252 ScreenlockBridge::Get()->focused_account_id().GetUserEmail()); |
| 216 EXPECT_FALSE(life_cycle()); | |
| 217 | 253 |
| 218 RemoteDeviceLifeCycle* unlock_manager_life_cycle = nullptr; | 254 RemoteDeviceLifeCycle* unlock_manager_life_cycle = nullptr; |
| 219 EXPECT_CALL(*unlock_manager_, SetRemoteDeviceLifeCycle(_)) | 255 EXPECT_CALL(*unlock_manager_, SetRemoteDeviceLifeCycle(_)) |
| 220 .WillOnce(SaveArg<0>(&unlock_manager_life_cycle)); | 256 .WillOnce(SaveArg<0>(&unlock_manager_life_cycle)); |
| 221 FocusUser(kUser1); | 257 FocusUser(kUser1); |
| 222 | 258 |
| 223 EXPECT_EQ(life_cycle(), unlock_manager_life_cycle); | 259 EXPECT_EQ(life_cycle(), unlock_manager_life_cycle); |
| 224 EXPECT_TRUE(life_cycle()); | 260 EXPECT_TRUE(life_cycle()); |
| 225 EXPECT_TRUE(life_cycle()->started()); | 261 EXPECT_TRUE(life_cycle()->started()); |
| 226 EXPECT_EQ(kUser1, life_cycle()->GetRemoteDevice().user_id); | 262 EXPECT_EQ(kUser1, life_cycle()->GetRemoteDevice().user_id); |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 383 EXPECT_CALL(*unlock_manager_, SetRemoteDeviceLifeCycle(NotNull())); | 419 EXPECT_CALL(*unlock_manager_, SetRemoteDeviceLifeCycle(NotNull())); |
| 384 SimulateSuspend(); | 420 SimulateSuspend(); |
| 385 } | 421 } |
| 386 | 422 |
| 387 EXPECT_EQ(kUser1, life_cycle()->GetRemoteDevice().user_id); | 423 EXPECT_EQ(kUser1, life_cycle()->GetRemoteDevice().user_id); |
| 388 | 424 |
| 389 EXPECT_CALL(*unlock_manager_, SetRemoteDeviceLifeCycle(nullptr)) | 425 EXPECT_CALL(*unlock_manager_, SetRemoteDeviceLifeCycle(nullptr)) |
| 390 .Times(AtLeast(1)); | 426 .Times(AtLeast(1)); |
| 391 } | 427 } |
| 392 | 428 |
| 429 TEST_F(ProximityAuthSystemTest, ForcePasswordReauth) { |
| 430 ON_CALL(*pref_manager_, GetLastPasswordEntryTimestampMs()) |
| 431 .WillByDefault(Return(kTimestampAfterReauthMs)); |
| 432 EXPECT_CALL(proximity_auth_client_, |
| 433 UpdateScreenlockState(ScreenlockState::PASSWORD_REAUTH)); |
| 434 FocusUser(kUser1); |
| 435 EXPECT_FALSE(life_cycle()); |
| 436 } |
| 437 |
| 393 } // namespace proximity_auth | 438 } // namespace proximity_auth |
| OLD | NEW |