OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 "chrome/browser/chromeos/login/quick_unlock/quick_unlock_storage.h" |
| 6 |
| 7 #include "chrome/browser/chromeos/login/quick_unlock/quick_unlock_factory.h" |
| 8 #include "chrome/browser/chromeos/login/quick_unlock/quick_unlock_utils.h" |
| 9 #include "chrome/common/pref_names.h" |
| 10 #include "chrome/test/base/testing_profile.h" |
| 11 #include "components/prefs/pref_service.h" |
| 12 #include "content/public/test/test_browser_thread_bundle.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace chromeos { |
| 16 namespace { |
| 17 |
| 18 void SetConfirmationFrequency( |
| 19 PrefService* pref_service, |
| 20 quick_unlock::PasswordConfirmationFrequency frequency) { |
| 21 pref_service->SetInteger(prefs::kQuickUnlockTimeout, |
| 22 static_cast<int>(frequency)); |
| 23 } |
| 24 |
| 25 base::TimeDelta GetExpirationTime(PrefService* pref_service) { |
| 26 int frequency = pref_service->GetInteger(prefs::kQuickUnlockTimeout); |
| 27 return quick_unlock::PasswordConfirmationFrequencyToTimeDelta( |
| 28 static_cast<quick_unlock::PasswordConfirmationFrequency>(frequency)); |
| 29 } |
| 30 |
| 31 class QuickUnlockStorageUnitTest : public testing::Test { |
| 32 protected: |
| 33 QuickUnlockStorageUnitTest() : profile_(new TestingProfile()) {} |
| 34 ~QuickUnlockStorageUnitTest() override {} |
| 35 |
| 36 // testing::Test: |
| 37 void SetUp() override { quick_unlock::EnableForTesting(); } |
| 38 |
| 39 content::TestBrowserThreadBundle thread_bundle_; |
| 40 std::unique_ptr<TestingProfile> profile_; |
| 41 |
| 42 DISALLOW_COPY_AND_ASSIGN(QuickUnlockStorageUnitTest); |
| 43 }; |
| 44 |
| 45 } // namespace |
| 46 |
| 47 // Provides test-only QuickUnlockStorage APIs. |
| 48 class QuickUnlockStorageTestApi { |
| 49 public: |
| 50 // Does *not* take ownership over |quick_unlock_storage|. |
| 51 explicit QuickUnlockStorageTestApi( |
| 52 quick_unlock::QuickUnlockStorage* quick_unlock_storage) |
| 53 : quick_unlock_storage_(quick_unlock_storage) {} |
| 54 |
| 55 // Reduces the amount of strong auth time available by |time_delta|. |
| 56 void ReduceRemainingStrongAuthTimeBy(const base::TimeDelta& time_delta) { |
| 57 quick_unlock_storage_->last_strong_auth_ -= time_delta; |
| 58 } |
| 59 |
| 60 bool HasStrongAuthInfo() { |
| 61 return !quick_unlock_storage_->last_strong_auth_.is_null(); |
| 62 } |
| 63 |
| 64 private: |
| 65 quick_unlock::QuickUnlockStorage* quick_unlock_storage_; |
| 66 |
| 67 DISALLOW_COPY_AND_ASSIGN(QuickUnlockStorageTestApi); |
| 68 }; |
| 69 |
| 70 // Verifies that marking the strong auth makes TimeSinceLastStrongAuth a > zero |
| 71 // value. |
| 72 TEST_F(QuickUnlockStorageUnitTest, |
| 73 TimeSinceLastStrongAuthReturnsPositiveValue) { |
| 74 quick_unlock::QuickUnlockStorage* quick_unlock_storage = |
| 75 quick_unlock::QuickUnlockFactory::GetForProfile(profile_.get()); |
| 76 PrefService* pref_service = profile_->GetPrefs(); |
| 77 QuickUnlockStorageTestApi test_api(quick_unlock_storage); |
| 78 |
| 79 EXPECT_FALSE(test_api.HasStrongAuthInfo()); |
| 80 |
| 81 quick_unlock_storage->MarkStrongAuth(); |
| 82 |
| 83 EXPECT_TRUE(test_api.HasStrongAuthInfo()); |
| 84 base::TimeDelta expiration_time = GetExpirationTime(pref_service); |
| 85 test_api.ReduceRemainingStrongAuthTimeBy(expiration_time); |
| 86 |
| 87 EXPECT_TRUE(quick_unlock_storage->TimeSinceLastStrongAuth() >= |
| 88 (expiration_time / 2)); |
| 89 } |
| 90 |
| 91 // Verifies that by altering the password confirmation preference, the |
| 92 // quick unlock storage will request password reconfirmation as expected. |
| 93 TEST_F(QuickUnlockStorageUnitTest, |
| 94 QuickUnlockPasswordConfirmationFrequencyPreference) { |
| 95 quick_unlock::QuickUnlockStorage* quick_unlock_storage = |
| 96 quick_unlock::QuickUnlockFactory::GetForProfile(profile_.get()); |
| 97 PrefService* pref_service = profile_->GetPrefs(); |
| 98 QuickUnlockStorageTestApi test_api(quick_unlock_storage); |
| 99 |
| 100 // The default is one day, so verify moving the last strong auth time back 12 |
| 101 // hours(half of the expiration time) should not request strong auth. |
| 102 quick_unlock_storage->MarkStrongAuth(); |
| 103 base::TimeDelta expiration_time = GetExpirationTime(pref_service); |
| 104 test_api.ReduceRemainingStrongAuthTimeBy(expiration_time / 2); |
| 105 EXPECT_TRUE(quick_unlock_storage->HasStrongAuth()); |
| 106 |
| 107 // Verify moving the last strong auth time back another half of the expiration |
| 108 // time should request strong auth. |
| 109 test_api.ReduceRemainingStrongAuthTimeBy(expiration_time / 2); |
| 110 EXPECT_FALSE(quick_unlock_storage->HasStrongAuth()); |
| 111 |
| 112 // Verify that by changing the frequency of required password confirmation to |
| 113 // six hours, moving the last strong auth interval back by 3 hours(half) will |
| 114 // not trigger a request for strong auth, but moving it by an additional 3 |
| 115 // hours will. |
| 116 quick_unlock_storage->MarkStrongAuth(); |
| 117 SetConfirmationFrequency( |
| 118 pref_service, quick_unlock::PasswordConfirmationFrequency::SIX_HOURS); |
| 119 expiration_time = GetExpirationTime(pref_service); |
| 120 test_api.ReduceRemainingStrongAuthTimeBy(expiration_time / 2); |
| 121 EXPECT_TRUE(quick_unlock_storage->HasStrongAuth()); |
| 122 test_api.ReduceRemainingStrongAuthTimeBy(expiration_time / 2); |
| 123 EXPECT_FALSE(quick_unlock_storage->HasStrongAuth()); |
| 124 |
| 125 // A valid strong auth becomes invalid if the confirmation frequency is |
| 126 // shortened to less than the expiration time. |
| 127 quick_unlock_storage->MarkStrongAuth(); |
| 128 SetConfirmationFrequency( |
| 129 pref_service, quick_unlock::PasswordConfirmationFrequency::TWELVE_HOURS); |
| 130 expiration_time = GetExpirationTime(pref_service); |
| 131 EXPECT_TRUE(quick_unlock_storage->HasStrongAuth()); |
| 132 test_api.ReduceRemainingStrongAuthTimeBy(expiration_time / 2); |
| 133 EXPECT_TRUE(quick_unlock_storage->HasStrongAuth()); |
| 134 SetConfirmationFrequency( |
| 135 pref_service, quick_unlock::PasswordConfirmationFrequency::SIX_HOURS); |
| 136 EXPECT_FALSE(quick_unlock_storage->HasStrongAuth()); |
| 137 |
| 138 // An expired strong auth becomes usable if the confirmation frequency gets |
| 139 // extended past the expiration time. |
| 140 quick_unlock_storage->MarkStrongAuth(); |
| 141 SetConfirmationFrequency( |
| 142 pref_service, quick_unlock::PasswordConfirmationFrequency::SIX_HOURS); |
| 143 expiration_time = GetExpirationTime(pref_service); |
| 144 EXPECT_TRUE(quick_unlock_storage->HasStrongAuth()); |
| 145 test_api.ReduceRemainingStrongAuthTimeBy(expiration_time); |
| 146 EXPECT_FALSE(quick_unlock_storage->HasStrongAuth()); |
| 147 SetConfirmationFrequency( |
| 148 pref_service, quick_unlock::PasswordConfirmationFrequency::TWELVE_HOURS); |
| 149 EXPECT_TRUE(quick_unlock_storage->HasStrongAuth()); |
| 150 } |
| 151 |
| 152 } // namespace chromeos |
OLD | NEW |