| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/chromeos/login/quick_unlock/pin_storage.h" |
| 6 #include "chrome/browser/chromeos/login/quick_unlock/pin_storage_factory.h" |
| 7 |
| 8 #include "chrome/common/pref_names.h" |
| 9 #include "chrome/test/base/testing_profile.h" |
| 10 #include "components/prefs/pref_service.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 class PinStorageUnitTest : public testing::Test { |
| 16 protected: |
| 17 PinStorageUnitTest() : profile_(new TestingProfile()) {} |
| 18 |
| 19 std::unique_ptr<TestingProfile> profile_; |
| 20 |
| 21 DISALLOW_COPY_AND_ASSIGN(PinStorageUnitTest); |
| 22 }; |
| 23 |
| 24 } // namespace |
| 25 |
| 26 // Provides test-only PinStorage APIs. |
| 27 class PinStorageTestApi { |
| 28 public: |
| 29 // Does *not* take ownership over |pin_storage|. |
| 30 explicit PinStorageTestApi(chromeos::PinStorage* pin_storage) |
| 31 : pin_storage_(pin_storage) {} |
| 32 |
| 33 // Reduces the amount of strong auth time available by |time_delta|. |
| 34 void ReduceRemainingStrongAuthTimeBy(const base::TimeDelta& time_delta) { |
| 35 pin_storage_->last_strong_auth_ -= time_delta; |
| 36 } |
| 37 |
| 38 std::string PinSalt() const { return pin_storage_->PinSalt(); } |
| 39 |
| 40 std::string PinSecret() const { return pin_storage_->PinSecret(); } |
| 41 |
| 42 private: |
| 43 chromeos::PinStorage* pin_storage_; |
| 44 |
| 45 DISALLOW_COPY_AND_ASSIGN(PinStorageTestApi); |
| 46 }; |
| 47 |
| 48 // Verifies that: |
| 49 // 1. Prefs are initially empty |
| 50 // 2. Setting a PIN will update the pref system. |
| 51 // 3. Removing a PIN clears prefs. |
| 52 TEST_F(PinStorageUnitTest, PinStorageWritesToPrefs) { |
| 53 PrefService* prefs = profile_->GetPrefs(); |
| 54 |
| 55 EXPECT_EQ("", prefs->GetString(prefs::kQuickUnlockPinSalt)); |
| 56 EXPECT_EQ("", prefs->GetString(prefs::kQuickUnlockPinSecret)); |
| 57 |
| 58 chromeos::PinStorage* pin_storage = |
| 59 chromeos::PinStorageFactory::GetForProfile(profile_.get()); |
| 60 PinStorageTestApi pin_storage_test(pin_storage); |
| 61 |
| 62 pin_storage->SetPin("1111"); |
| 63 EXPECT_TRUE(pin_storage->IsPinSet()); |
| 64 EXPECT_EQ(pin_storage_test.PinSalt(), |
| 65 prefs->GetString(prefs::kQuickUnlockPinSalt)); |
| 66 EXPECT_EQ(pin_storage_test.PinSecret(), |
| 67 prefs->GetString(prefs::kQuickUnlockPinSecret)); |
| 68 EXPECT_NE("", pin_storage_test.PinSalt()); |
| 69 EXPECT_NE("", pin_storage_test.PinSecret()); |
| 70 |
| 71 pin_storage->RemovePin(); |
| 72 EXPECT_FALSE(pin_storage->IsPinSet()); |
| 73 EXPECT_EQ("", prefs->GetString(prefs::kQuickUnlockPinSalt)); |
| 74 EXPECT_EQ("", prefs->GetString(prefs::kQuickUnlockPinSecret)); |
| 75 } |
| 76 |
| 77 // Verifies that: |
| 78 // 1. Initial unlock attempt count is zero. |
| 79 // 2. Attempting unlock attempts correctly increases unlock attempt count. |
| 80 // 3. Resetting unlock attempt count correctly sets attempt count to 0. |
| 81 TEST_F(PinStorageUnitTest, UnlockAttemptCount) { |
| 82 chromeos::PinStorage* pin_storage = |
| 83 chromeos::PinStorageFactory::GetForProfile(profile_.get()); |
| 84 |
| 85 EXPECT_EQ(0, pin_storage->unlock_attempt_count()); |
| 86 |
| 87 pin_storage->AddUnlockAttempt(); |
| 88 pin_storage->AddUnlockAttempt(); |
| 89 pin_storage->AddUnlockAttempt(); |
| 90 EXPECT_EQ(3, pin_storage->unlock_attempt_count()); |
| 91 |
| 92 pin_storage->ResetUnlockAttemptCount(); |
| 93 EXPECT_EQ(0, pin_storage->unlock_attempt_count()); |
| 94 } |
| 95 |
| 96 // Verifies that marking the strong auth makes TimeSinceLastStrongAuth a > zero |
| 97 // value. |
| 98 TEST_F(PinStorageUnitTest, TimeSinceLastStrongAuthReturnsPositiveValue) { |
| 99 chromeos::PinStorage* pin_storage = |
| 100 chromeos::PinStorageFactory::GetForProfile(profile_.get()); |
| 101 PinStorageTestApi pin_storage_test(pin_storage); |
| 102 |
| 103 EXPECT_FALSE(pin_storage->HasStrongAuth()); |
| 104 |
| 105 pin_storage->MarkStrongAuth(); |
| 106 |
| 107 EXPECT_TRUE(pin_storage->HasStrongAuth()); |
| 108 pin_storage_test.ReduceRemainingStrongAuthTimeBy( |
| 109 base::TimeDelta::FromSeconds(60)); |
| 110 |
| 111 EXPECT_TRUE(pin_storage->TimeSinceLastStrongAuth() >= |
| 112 base::TimeDelta::FromSeconds(30)); |
| 113 } |
| 114 |
| 115 // Verifies that the correct pin can be used to authenticate. |
| 116 TEST_F(PinStorageUnitTest, AuthenticationSucceedsWithRightPin) { |
| 117 chromeos::PinStorage* pin_storage = |
| 118 chromeos::PinStorageFactory::GetForProfile(profile_.get()); |
| 119 |
| 120 pin_storage->SetPin("1111"); |
| 121 |
| 122 pin_storage->MarkStrongAuth(); |
| 123 EXPECT_TRUE(pin_storage->TryAuthenticatePin("1111")); |
| 124 } |
| 125 |
| 126 // Verifies that the correct pin will fail to authenticate if too many |
| 127 // authentication attempts have been made. |
| 128 TEST_F(PinStorageUnitTest, AuthenticationFailsFromTooManyAttempts) { |
| 129 chromeos::PinStorage* pin_storage = |
| 130 chromeos::PinStorageFactory::GetForProfile(profile_.get()); |
| 131 |
| 132 pin_storage->SetPin("1111"); |
| 133 |
| 134 // Use up all of the authentication attempts so authentication fails. |
| 135 pin_storage->MarkStrongAuth(); |
| 136 EXPECT_TRUE(pin_storage->IsPinAuthenticationAvailable()); |
| 137 for (int i = 0; i < chromeos::PinStorage::kMaximumUnlockAttempts; ++i) |
| 138 EXPECT_FALSE(pin_storage->TryAuthenticatePin("foobar")); |
| 139 |
| 140 // We used up all of the attempts, so entering the right PIN will still fail. |
| 141 EXPECT_FALSE(pin_storage->IsPinAuthenticationAvailable()); |
| 142 EXPECT_FALSE(pin_storage->TryAuthenticatePin("1111")); |
| 143 } |
| 144 |
| 145 // Verifies that the correct pin will fail to authenticate if it has been too |
| 146 // long since a strong-auth/password authentication. |
| 147 TEST_F(PinStorageUnitTest, AuthenticationFailsFromTimeout) { |
| 148 chromeos::PinStorage* pin_storage = |
| 149 chromeos::PinStorageFactory::GetForProfile(profile_.get()); |
| 150 PinStorageTestApi pin_storage_test(pin_storage); |
| 151 |
| 152 pin_storage->SetPin("1111"); |
| 153 pin_storage->MarkStrongAuth(); |
| 154 EXPECT_TRUE(pin_storage->IsPinAuthenticationAvailable()); |
| 155 |
| 156 // Remove all of the strong auth time so that we have a strong auth timeout. |
| 157 pin_storage_test.ReduceRemainingStrongAuthTimeBy( |
| 158 chromeos::PinStorage::kStrongAuthTimeout + base::TimeDelta::FromHours(1)); |
| 159 |
| 160 EXPECT_FALSE(pin_storage->IsPinAuthenticationAvailable()); |
| 161 } |
| OLD | NEW |