Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "chrome/browser/chromeos/login/quick_unlock/pin_storage.h" | 5 #include "chrome/browser/chromeos/login/quick_unlock/pin_storage.h" |
| 6 #include "chrome/browser/chromeos/login/quick_unlock/pin_storage_factory.h" | 6 #include "chrome/browser/chromeos/login/quick_unlock/pin_storage_factory.h" |
| 7 #include "chrome/browser/chromeos/login/quick_unlock/quick_unlock_utils.h" | 7 #include "chrome/browser/chromeos/login/quick_unlock/quick_unlock_utils.h" |
| 8 #include "chrome/common/pref_names.h" | 8 #include "chrome/common/pref_names.h" |
| 9 #include "chrome/test/base/testing_profile.h" | 9 #include "chrome/test/base/testing_profile.h" |
| 10 #include "components/prefs/pref_service.h" | 10 #include "components/prefs/pref_service.h" |
| 11 #include "components/prefs/scoped_user_pref_update.h" | |
| 11 #include "content/public/test/test_browser_thread_bundle.h" | 12 #include "content/public/test/test_browser_thread_bundle.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 | 16 |
| 16 class PinStorageUnitTest : public testing::Test { | 17 class PinStorageUnitTest : public testing::Test { |
| 17 protected: | 18 protected: |
| 18 PinStorageUnitTest() : profile_(new TestingProfile()) {} | 19 PinStorageUnitTest() : profile_(new TestingProfile()) {} |
| 19 ~PinStorageUnitTest() override {} | 20 ~PinStorageUnitTest() override {} |
| 20 | 21 |
| 21 // testing::Test: | 22 // testing::Test: |
| 22 void SetUp() override { chromeos::EnableQuickUnlockForTesting(); } | 23 void SetUp() override { |
| 24 chromeos::EnableQuickUnlockForTesting(); | |
| 25 | |
| 26 // Pin is not allowed by default so we need to update it for this test. | |
|
jdufault
2016/10/25 17:39:50
nit: Pin => PIN
What about
// PIN is not enabl
sammiequon
2016/10/25 19:18:41
Done.
| |
| 27 ListPrefUpdate update(profile_->GetPrefs(), | |
| 28 prefs::kQuickUnlockModeWhitelist); | |
| 29 update->Append(base::MakeUnique<base::StringValue>("pin")); | |
| 30 } | |
| 23 | 31 |
| 24 content::TestBrowserThreadBundle thread_bundle_; | 32 content::TestBrowserThreadBundle thread_bundle_; |
| 25 std::unique_ptr<TestingProfile> profile_; | 33 std::unique_ptr<TestingProfile> profile_; |
| 26 | 34 |
| 27 DISALLOW_COPY_AND_ASSIGN(PinStorageUnitTest); | 35 DISALLOW_COPY_AND_ASSIGN(PinStorageUnitTest); |
| 28 }; | 36 }; |
| 29 | 37 |
| 30 } // namespace | 38 } // namespace |
| 31 | 39 |
| 32 // Provides test-only PinStorage APIs. | 40 // Provides test-only PinStorage APIs. |
| 33 class PinStorageTestApi { | 41 class PinStorageTestApi { |
| 34 public: | 42 public: |
| 35 // Does *not* take ownership over |pin_storage|. | 43 // Does *not* take ownership over |pin_storage|. |
| 36 explicit PinStorageTestApi(chromeos::PinStorage* pin_storage) | 44 explicit PinStorageTestApi(chromeos::PinStorage* pin_storage) |
| 37 : pin_storage_(pin_storage) {} | 45 : pin_storage_(pin_storage) {} |
| 38 | 46 |
| 39 // Reduces the amount of strong auth time available by |time_delta|. | 47 // Reduces the amount of strong auth time available by |time_delta|. |
| 40 void ReduceRemainingStrongAuthTimeBy(const base::TimeDelta& time_delta) { | 48 void ReduceRemainingStrongAuthTimeBy(const base::TimeDelta& time_delta) { |
| 41 pin_storage_->last_strong_auth_ -= time_delta; | 49 pin_storage_->last_strong_auth_ -= time_delta; |
| 42 } | 50 } |
| 43 | 51 |
| 52 bool IsLastStrongAuthAvaliable() { | |
| 53 return !pin_storage_->last_strong_auth_.is_null(); | |
| 54 } | |
| 55 | |
| 44 std::string PinSalt() const { return pin_storage_->PinSalt(); } | 56 std::string PinSalt() const { return pin_storage_->PinSalt(); } |
| 45 | 57 |
| 46 std::string PinSecret() const { return pin_storage_->PinSecret(); } | 58 std::string PinSecret() const { return pin_storage_->PinSecret(); } |
| 47 | 59 |
| 48 private: | 60 private: |
| 49 chromeos::PinStorage* pin_storage_; | 61 chromeos::PinStorage* pin_storage_; |
| 50 | 62 |
| 51 DISALLOW_COPY_AND_ASSIGN(PinStorageTestApi); | 63 DISALLOW_COPY_AND_ASSIGN(PinStorageTestApi); |
| 52 }; | 64 }; |
| 53 | 65 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 99 EXPECT_EQ(0, pin_storage->unlock_attempt_count()); | 111 EXPECT_EQ(0, pin_storage->unlock_attempt_count()); |
| 100 } | 112 } |
| 101 | 113 |
| 102 // Verifies that marking the strong auth makes TimeSinceLastStrongAuth a > zero | 114 // Verifies that marking the strong auth makes TimeSinceLastStrongAuth a > zero |
| 103 // value. | 115 // value. |
| 104 TEST_F(PinStorageUnitTest, TimeSinceLastStrongAuthReturnsPositiveValue) { | 116 TEST_F(PinStorageUnitTest, TimeSinceLastStrongAuthReturnsPositiveValue) { |
| 105 chromeos::PinStorage* pin_storage = | 117 chromeos::PinStorage* pin_storage = |
| 106 chromeos::PinStorageFactory::GetForProfile(profile_.get()); | 118 chromeos::PinStorageFactory::GetForProfile(profile_.get()); |
| 107 PinStorageTestApi pin_storage_test(pin_storage); | 119 PinStorageTestApi pin_storage_test(pin_storage); |
| 108 | 120 |
| 109 EXPECT_FALSE(pin_storage->HasStrongAuth()); | 121 EXPECT_FALSE(pin_storage_test.IsLastStrongAuthAvaliable()); |
| 110 | 122 |
| 111 pin_storage->MarkStrongAuth(); | 123 pin_storage->MarkStrongAuth(); |
| 112 | 124 |
| 113 EXPECT_TRUE(pin_storage->HasStrongAuth()); | 125 EXPECT_TRUE(pin_storage_test.IsLastStrongAuthAvaliable()); |
| 114 pin_storage_test.ReduceRemainingStrongAuthTimeBy( | 126 pin_storage_test.ReduceRemainingStrongAuthTimeBy( |
| 115 base::TimeDelta::FromSeconds(60)); | 127 base::TimeDelta::FromSeconds(60)); |
| 116 | 128 |
| 117 EXPECT_TRUE(pin_storage->TimeSinceLastStrongAuth() >= | 129 EXPECT_TRUE(pin_storage->TimeSinceLastStrongAuth() >= |
| 118 base::TimeDelta::FromSeconds(30)); | 130 base::TimeDelta::FromSeconds(30)); |
| 119 } | 131 } |
| 120 | 132 |
| 133 // Verifies altering the password confirmation preference produces the expected | |
| 134 // results. | |
| 135 TEST_F(PinStorageUnitTest, PasswordConfirmationFrequencyPreference) { | |
| 136 chromeos::PinStorage* pin_storage = | |
| 137 chromeos::PinStorageFactory::GetForProfile(profile_.get()); | |
| 138 PrefService* pref_service = profile_->GetPrefs(); | |
| 139 PinStorageTestApi pin_storage_test(pin_storage); | |
| 140 | |
| 141 // The default is one day, so verify moving time back 13 hours should not | |
| 142 // request strong auth. | |
| 143 pin_storage->MarkStrongAuth(); | |
| 144 pin_storage_test.ReduceRemainingStrongAuthTimeBy( | |
| 145 base::TimeDelta::FromHours(13)); | |
| 146 EXPECT_TRUE(pin_storage->HasStrongAuth()); | |
| 147 | |
| 148 // Verify moving time back another 13 hours should request strong auth. | |
| 149 pin_storage_test.ReduceRemainingStrongAuthTimeBy( | |
| 150 base::TimeDelta::FromHours(13)); | |
| 151 EXPECT_FALSE(pin_storage->HasStrongAuth()); | |
| 152 | |
| 153 // Reset the time since last strong auth. | |
| 154 pin_storage->MarkStrongAuth(); | |
| 155 | |
| 156 // Verify that by changing the frequency of required password confirmation to | |
| 157 // six hours, moving interval by 4 hours will not trigger a request for strong | |
| 158 // auth, but moving interval by an additional 4 hours will. | |
| 159 pref_service->SetInteger( | |
| 160 prefs::kQuickUnlockTimeout, | |
| 161 static_cast<int>(chromeos::PasswordConfirmationFrequency::SIX_HOURS)); | |
| 162 pin_storage_test.ReduceRemainingStrongAuthTimeBy( | |
| 163 base::TimeDelta::FromHours(4)); | |
| 164 EXPECT_TRUE(pin_storage->HasStrongAuth()); | |
| 165 pin_storage_test.ReduceRemainingStrongAuthTimeBy( | |
| 166 base::TimeDelta::FromHours(4)); | |
| 167 EXPECT_FALSE(pin_storage->HasStrongAuth()); | |
| 168 | |
| 169 // Verify that by changing the frequency of required password confirmation to | |
| 170 // twelve hours, the current interval of 8 hours will not trigger a request | |
| 171 // for strong auth, but moving interval by an additional 5 hours will. | |
| 172 pref_service->SetInteger( | |
| 173 prefs::kQuickUnlockTimeout, | |
| 174 static_cast<int>(chromeos::PasswordConfirmationFrequency::TWELVE_HOURS)); | |
| 175 EXPECT_TRUE(pin_storage->HasStrongAuth()); | |
| 176 pin_storage_test.ReduceRemainingStrongAuthTimeBy( | |
| 177 base::TimeDelta::FromHours(5)); | |
| 178 EXPECT_FALSE(pin_storage->HasStrongAuth()); | |
| 179 | |
| 180 pin_storage->MarkStrongAuth(); | |
| 181 // Verify that by changing the frequency of required password confirmation to | |
| 182 // one week, moving interval by 3 days will not trigger a request for strong | |
| 183 // auth, but moving interval by an addition 5 days will. | |
| 184 pref_service->SetInteger( | |
| 185 prefs::kQuickUnlockTimeout, | |
| 186 static_cast<int>(chromeos::PasswordConfirmationFrequency::WEEK)); | |
| 187 pin_storage_test.ReduceRemainingStrongAuthTimeBy( | |
| 188 base::TimeDelta::FromDays(3)); | |
| 189 EXPECT_TRUE(pin_storage->HasStrongAuth()); | |
| 190 pin_storage_test.ReduceRemainingStrongAuthTimeBy( | |
| 191 base::TimeDelta::FromDays(5)); | |
| 192 EXPECT_FALSE(pin_storage->HasStrongAuth()); | |
| 193 } | |
| 194 | |
| 121 // Verifies that the correct pin can be used to authenticate. | 195 // Verifies that the correct pin can be used to authenticate. |
| 122 TEST_F(PinStorageUnitTest, AuthenticationSucceedsWithRightPin) { | 196 TEST_F(PinStorageUnitTest, AuthenticationSucceedsWithRightPin) { |
| 123 chromeos::PinStorage* pin_storage = | 197 chromeos::PinStorage* pin_storage = |
| 124 chromeos::PinStorageFactory::GetForProfile(profile_.get()); | 198 chromeos::PinStorageFactory::GetForProfile(profile_.get()); |
| 125 | 199 |
| 126 pin_storage->SetPin("1111"); | 200 pin_storage->SetPin("1111"); |
| 127 | 201 |
| 128 pin_storage->MarkStrongAuth(); | 202 pin_storage->MarkStrongAuth(); |
| 129 EXPECT_TRUE(pin_storage->TryAuthenticatePin("1111")); | 203 EXPECT_TRUE(pin_storage->TryAuthenticatePin("1111")); |
| 130 } | 204 } |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 154 chromeos::PinStorage* pin_storage = | 228 chromeos::PinStorage* pin_storage = |
| 155 chromeos::PinStorageFactory::GetForProfile(profile_.get()); | 229 chromeos::PinStorageFactory::GetForProfile(profile_.get()); |
| 156 PinStorageTestApi pin_storage_test(pin_storage); | 230 PinStorageTestApi pin_storage_test(pin_storage); |
| 157 | 231 |
| 158 pin_storage->SetPin("1111"); | 232 pin_storage->SetPin("1111"); |
| 159 pin_storage->MarkStrongAuth(); | 233 pin_storage->MarkStrongAuth(); |
| 160 EXPECT_TRUE(pin_storage->IsPinAuthenticationAvailable()); | 234 EXPECT_TRUE(pin_storage->IsPinAuthenticationAvailable()); |
| 161 | 235 |
| 162 // Remove all of the strong auth time so that we have a strong auth timeout. | 236 // Remove all of the strong auth time so that we have a strong auth timeout. |
| 163 pin_storage_test.ReduceRemainingStrongAuthTimeBy( | 237 pin_storage_test.ReduceRemainingStrongAuthTimeBy( |
| 164 chromeos::PinStorage::kStrongAuthTimeout + base::TimeDelta::FromHours(1)); | 238 base::TimeDelta::FromDays(10)); |
| 165 | 239 |
| 166 EXPECT_FALSE(pin_storage->IsPinAuthenticationAvailable()); | 240 EXPECT_FALSE(pin_storage->IsPinAuthenticationAvailable()); |
| 167 } | 241 } |
| 242 | |
| 243 TEST_F(PinStorageUnitTest, VerifyPinUnlockAgainstPolicy) { | |
| 244 ListPrefUpdate update(profile_->GetPrefs(), prefs::kQuickUnlockModeWhitelist); | |
| 245 // Verify that clearing the whitelist diables the pin. | |
| 246 update->Clear(); | |
| 247 EXPECT_FALSE(chromeos::IsPinUnlockEnabled(profile_->GetPrefs())); | |
| 248 | |
| 249 // Verify that by adding pin and another unlock mode, pin is enabled. | |
| 250 update->Append(base::MakeUnique<base::StringValue>("pin")); | |
| 251 EXPECT_TRUE(chromeos::IsPinUnlockEnabled(profile_->GetPrefs())); | |
| 252 update->Append(base::MakeUnique<base::StringValue>("password")); | |
| 253 EXPECT_TRUE(chromeos::IsPinUnlockEnabled(profile_->GetPrefs())); | |
| 254 | |
| 255 // Verify that adding all to the whitelist enables the pin. | |
| 256 update->Append(base::MakeUnique<base::StringValue>("all")); | |
| 257 EXPECT_TRUE(chromeos::IsPinUnlockEnabled(profile_->GetPrefs())); | |
| 258 } | |
| OLD | NEW |