Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(95)

Side by Side Diff: chrome/browser/chromeos/login/quick_unlock/pin_storage_unittest.cc

Issue 2387253002: cros: Added policies for screen unlock. (Closed)
Patch Set: Fixed patch set 12 errors. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
17 void SetConfirmationFrequency(
18 PrefService* pref_service,
19 chromeos::PasswordConfirmationFrequency frequency) {
20 pref_service->SetInteger(prefs::kQuickUnlockTimeout,
21 static_cast<int>(frequency));
22 }
23
16 class PinStorageUnitTest : public testing::Test { 24 class PinStorageUnitTest : public testing::Test {
17 protected: 25 protected:
18 PinStorageUnitTest() : profile_(new TestingProfile()) {} 26 PinStorageUnitTest() : profile_(new TestingProfile()) {}
19 ~PinStorageUnitTest() override {} 27 ~PinStorageUnitTest() override {}
20 28
21 // testing::Test: 29 // testing::Test:
22 void SetUp() override { chromeos::EnableQuickUnlockForTesting(); } 30 void SetUp() override { chromeos::EnableQuickUnlockForTesting(); }
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 HasStrongAuthInfo() {
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
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.HasStrongAuthInfo());
110 122
111 pin_storage->MarkStrongAuth(); 123 pin_storage->MarkStrongAuth();
112 124
113 EXPECT_TRUE(pin_storage->HasStrongAuth()); 125 EXPECT_TRUE(pin_storage_test.HasStrongAuthInfo());
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 that by altering the password confirmation preference, the pin
134 // storage will request password reconfirmation as expected.
135 TEST_F(PinStorageUnitTest, PasswordConfirmationFrequencyPreference) {
136 chromeos::PinStorage* pin_storage =
137 chromeos::PinStorageFactory::GetForProfile(profile_.get());
138 PrefService* pref_service = profile_->GetPrefs();
139 PinStorageTestApi test_api(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 test_api.ReduceRemainingStrongAuthTimeBy(base::TimeDelta::FromHours(13));
145 EXPECT_TRUE(pin_storage->HasStrongAuth());
146
147 // Verify moving time back another 13 hours should request strong auth.
148 test_api.ReduceRemainingStrongAuthTimeBy(base::TimeDelta::FromHours(13));
149 EXPECT_FALSE(pin_storage->HasStrongAuth());
150
151 // Verify that by changing the frequency of required password confirmation to
152 // six hours, moving interval by 4 hours will not trigger a request for strong
153 // auth, but moving interval by an additional 4 hours will.
154 pin_storage->MarkStrongAuth();
155 SetConfirmationFrequency(pref_service,
156 chromeos::PasswordConfirmationFrequency::SIX_HOURS);
157 test_api.ReduceRemainingStrongAuthTimeBy(base::TimeDelta::FromHours(4));
158 EXPECT_TRUE(pin_storage->HasStrongAuth());
159 test_api.ReduceRemainingStrongAuthTimeBy(base::TimeDelta::FromHours(4));
160 EXPECT_FALSE(pin_storage->HasStrongAuth());
161
162 // Verify that by changing the frequency in the middle of a auth timeout, the
163 // value of the new frequency persists. In this case, the frequency shortened
jdufault 2016/11/02 22:52:59 How about // A valid strong auth becomes invalid
sammiequon 2016/11/03 00:45:52 Done.
164 // when the delay already passed the new frequency (but still did not reach
165 // the old frequency) so password confirmation is required.
166 pin_storage->MarkStrongAuth();
167 SetConfirmationFrequency(
168 pref_service, chromeos::PasswordConfirmationFrequency::TWELVE_HOURS);
169 EXPECT_TRUE(pin_storage->HasStrongAuth());
170 test_api.ReduceRemainingStrongAuthTimeBy(base::TimeDelta::FromHours(8));
171 EXPECT_TRUE(pin_storage->HasStrongAuth());
172 SetConfirmationFrequency(pref_service,
173 chromeos::PasswordConfirmationFrequency::SIX_HOURS);
174 EXPECT_FALSE(pin_storage->HasStrongAuth());
175
176 // Verify that by changing the frequency in the middle of a auth timeout, the
177 // value of the new frequency persists. In this case, the frequency increased
178 // when the delay already passed the old frequency (but did not reach the new
179 // frequency) so password confirmation is not required.
jdufault 2016/11/02 22:52:59 How about // An expired strong auth becomes usabl
sammiequon 2016/11/03 00:45:52 Done.
180 pin_storage->MarkStrongAuth();
181 SetConfirmationFrequency(pref_service,
182 chromeos::PasswordConfirmationFrequency::SIX_HOURS);
183 EXPECT_TRUE(pin_storage->HasStrongAuth());
184 test_api.ReduceRemainingStrongAuthTimeBy(base::TimeDelta::FromHours(8));
185 EXPECT_FALSE(pin_storage->HasStrongAuth());
186 SetConfirmationFrequency(
187 pref_service, chromeos::PasswordConfirmationFrequency::TWELVE_HOURS);
188 EXPECT_TRUE(pin_storage->HasStrongAuth());
189 }
190
121 // Verifies that the correct pin can be used to authenticate. 191 // Verifies that the correct pin can be used to authenticate.
122 TEST_F(PinStorageUnitTest, AuthenticationSucceedsWithRightPin) { 192 TEST_F(PinStorageUnitTest, AuthenticationSucceedsWithRightPin) {
123 chromeos::PinStorage* pin_storage = 193 chromeos::PinStorage* pin_storage =
124 chromeos::PinStorageFactory::GetForProfile(profile_.get()); 194 chromeos::PinStorageFactory::GetForProfile(profile_.get());
125 195
126 pin_storage->SetPin("1111"); 196 pin_storage->SetPin("1111");
127 197
128 pin_storage->MarkStrongAuth(); 198 pin_storage->MarkStrongAuth();
129 EXPECT_TRUE(pin_storage->TryAuthenticatePin("1111")); 199 EXPECT_TRUE(pin_storage->TryAuthenticatePin("1111"));
130 } 200 }
(...skipping 23 matching lines...) Expand all
154 chromeos::PinStorage* pin_storage = 224 chromeos::PinStorage* pin_storage =
155 chromeos::PinStorageFactory::GetForProfile(profile_.get()); 225 chromeos::PinStorageFactory::GetForProfile(profile_.get());
156 PinStorageTestApi pin_storage_test(pin_storage); 226 PinStorageTestApi pin_storage_test(pin_storage);
157 227
158 pin_storage->SetPin("1111"); 228 pin_storage->SetPin("1111");
159 pin_storage->MarkStrongAuth(); 229 pin_storage->MarkStrongAuth();
160 EXPECT_TRUE(pin_storage->IsPinAuthenticationAvailable()); 230 EXPECT_TRUE(pin_storage->IsPinAuthenticationAvailable());
161 231
162 // Remove all of the strong auth time so that we have a strong auth timeout. 232 // Remove all of the strong auth time so that we have a strong auth timeout.
163 pin_storage_test.ReduceRemainingStrongAuthTimeBy( 233 pin_storage_test.ReduceRemainingStrongAuthTimeBy(
164 chromeos::PinStorage::kStrongAuthTimeout + base::TimeDelta::FromHours(1)); 234 base::TimeDelta::FromDays(10));
jdufault 2016/11/02 22:52:58 This is a little ugly but it is probably not worth
165 235
166 EXPECT_FALSE(pin_storage->IsPinAuthenticationAvailable()); 236 EXPECT_FALSE(pin_storage->IsPinAuthenticationAvailable());
167 } 237 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698