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

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

Issue 1977923002: Implement pin storage backend. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkgr
Patch Set: Address comments Created 4 years, 7 months 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
(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(base::TimeDelta time_delta) {
achuithb 2016/05/17 20:13:44 const base::TimeDelta&
jdufault 2016/05/20 19:29:44 Done.
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 DISALLOW_COPY_AND_ASSIGN(PinStorageTestApi);
achuithb 2016/05/17 20:13:44 newline before this line
jdufault 2016/05/20 19:29:44 Done.
45 };
46
47 // Verifies that:
48 // 1. Prefs are initially empty
49 // 2. Setting a PIN will update the pref system.
50 // 3. Removing a PIN clears prefs.
51 TEST_F(PinStorageUnitTest, PinStorageWritesToPrefs) {
52 PrefService* prefs = profile_->GetPrefs();
53
54 EXPECT_EQ("", prefs->GetString(prefs::kQuickUnlockPinSalt));
55 EXPECT_EQ("", prefs->GetString(prefs::kQuickUnlockPinSecret));
56
57 chromeos::PinStorage* pin_storage =
58 chromeos::PinStorageFactory::GetForProfile(profile_.get());
59 PinStorageTestApi pin_storage_test(pin_storage);
60
61 pin_storage->SetPin("1111");
62 EXPECT_TRUE(pin_storage->IsPinSet());
63 EXPECT_EQ(pin_storage_test.PinSalt(),
64 prefs->GetString(prefs::kQuickUnlockPinSalt));
65 EXPECT_EQ(pin_storage_test.PinSecret(),
66 prefs->GetString(prefs::kQuickUnlockPinSecret));
67 EXPECT_NE("", pin_storage_test.PinSalt());
68 EXPECT_NE("", pin_storage_test.PinSecret());
69
70 pin_storage->RemovePin();
71 EXPECT_FALSE(pin_storage->IsPinSet());
72 EXPECT_EQ("", prefs->GetString(prefs::kQuickUnlockPinSalt));
73 EXPECT_EQ("", prefs->GetString(prefs::kQuickUnlockPinSecret));
74 }
75
76 // Verifies that:
77 // 1. Initial unlock attempt count is zero.
78 // 2. Attempting unlock attempts correctly increases unlock attempt count.
79 // 3. Resetting unlock attempt count correctly sets attempt count to 0.
80 TEST_F(PinStorageUnitTest, UnlockAttemptCount) {
81 chromeos::PinStorage* pin_storage =
82 chromeos::PinStorageFactory::GetForProfile(profile_.get());
83
84 EXPECT_EQ(0, pin_storage->unlock_attempt_count());
85
86 pin_storage->AddUnlockAttempt();
87 pin_storage->AddUnlockAttempt();
88 pin_storage->AddUnlockAttempt();
89 EXPECT_EQ(3, pin_storage->unlock_attempt_count());
90
91 pin_storage->ResetUnlockAttemptCount();
92 EXPECT_EQ(0, pin_storage->unlock_attempt_count());
93 }
94
95 // Verifies that marking the strong auth makes TimeSinceLastStrongAuth a > zero
96 // value.
97 TEST_F(PinStorageUnitTest, TimeSinceLastStrongAuthReturnsPositiveValue) {
98 chromeos::PinStorage* pin_storage =
99 chromeos::PinStorageFactory::GetForProfile(profile_.get());
100 PinStorageTestApi pin_storage_test(pin_storage);
101
102 EXPECT_FALSE(pin_storage->HasStrongAuth());
103
104 pin_storage->MarkStrongAuth();
105
106 EXPECT_TRUE(pin_storage->HasStrongAuth());
107 pin_storage_test.ReduceRemainingStrongAuthTimeBy(
108 base::TimeDelta::FromSeconds(60));
109
110 EXPECT_TRUE(pin_storage->TimeSinceLastStrongAuth() >=
111 base::TimeDelta::FromSeconds(30));
112 }
113
114 // Verifies that the correct pin can be used to authenticate.
115 TEST_F(PinStorageUnitTest, AuthenticationSucceedsWithRightPin) {
116 chromeos::PinStorage* pin_storage =
117 chromeos::PinStorageFactory::GetForProfile(profile_.get());
118
119 pin_storage->SetPin("1111");
120
121 pin_storage->MarkStrongAuth();
122 EXPECT_TRUE(pin_storage->TryAuthenticatePin("1111"));
123 }
124
125 // Verifies that the correct pin will fail to authenticate if too many
126 // authentication attempts have been made.
127 TEST_F(PinStorageUnitTest, AuthenticationFailsFromTooManyAttempts) {
128 chromeos::PinStorage* pin_storage =
129 chromeos::PinStorageFactory::GetForProfile(profile_.get());
130
131 pin_storage->SetPin("1111");
132
133 // Use up all of the authentication attempts so authentication fails.
134 pin_storage->MarkStrongAuth();
135 EXPECT_TRUE(pin_storage->IsPinAuthenticationAvailable());
136 for (int i = 0; i < chromeos::PinStorage::kMaximumUnlockAttempts; ++i)
137 EXPECT_FALSE(pin_storage->TryAuthenticatePin("foobar"));
138
139 // We used up all of the attempts, so entering the right PIN will still fail.
140 EXPECT_FALSE(pin_storage->IsPinAuthenticationAvailable());
141 EXPECT_FALSE(pin_storage->TryAuthenticatePin("1111"));
142 }
143
144 // Verifies that the correct pin will fail to authenticate if it has been too
145 // long since a strong-auth/password authentication.
146 TEST_F(PinStorageUnitTest, AuthenticationFailsFromTimeout) {
147 chromeos::PinStorage* pin_storage =
148 chromeos::PinStorageFactory::GetForProfile(profile_.get());
149 PinStorageTestApi pin_storage_test(pin_storage);
150
151 pin_storage->SetPin("1111");
152 pin_storage->MarkStrongAuth();
153 EXPECT_TRUE(pin_storage->IsPinAuthenticationAvailable());
154
155 // Remove all of the strong auth time so that we have a strong auth timeout.
156 pin_storage_test.ReduceRemainingStrongAuthTimeBy(
157 chromeos::PinStorage::kStrongAuthTimeout + base::TimeDelta::FromHours(1));
158
159 EXPECT_FALSE(pin_storage->IsPinAuthenticationAvailable());
160 }
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/login/quick_unlock/pin_storage_factory.cc ('k') | chrome/browser/prefs/browser_prefs.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698