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

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 // Verifies that:
27 // 1. Prefs are initially empty
28 // 2. Setting a PIN will update the pref system.
29 // 3. Removing a PIN clears prefs.
30 TEST_F(PinStorageUnitTest, PinStorageWritesToPrefs) {
31 PrefService* prefs = profile_->GetPrefs();
32
33 EXPECT_EQ("", prefs->GetString(prefs::kQuickUnlockPinSalt));
34 EXPECT_EQ("", prefs->GetString(prefs::kQuickUnlockPinSecret));
35
36 chromeos::PinStorage* pin_storage =
37 chromeos::PinStorageFactory::GetForProfile(profile_.get());
38 pin_storage->SetPin("1111");
39 EXPECT_TRUE(pin_storage->IsPinSet());
40 EXPECT_EQ(pin_storage->PinSalt(),
41 prefs->GetString(prefs::kQuickUnlockPinSalt));
42 EXPECT_EQ(pin_storage->PinSecret(),
43 prefs->GetString(prefs::kQuickUnlockPinSecret));
44 EXPECT_NE("", pin_storage->PinSalt());
45 EXPECT_NE("", pin_storage->PinSecret());
46
47 pin_storage->RemovePin();
48 EXPECT_FALSE(pin_storage->IsPinSet());
49 EXPECT_EQ("", prefs->GetString(prefs::kQuickUnlockPinSalt));
50 EXPECT_EQ("", prefs->GetString(prefs::kQuickUnlockPinSecret));
51 }
52
53 // Verifies that:
54 // 1. Initial unlock attempt count is zero.
55 // 2. Attempting unlock attempts correctly increases unlock attempt count.
56 // 3. Resetting unlock attempt count correctly sets attempt count to 0.
57 TEST_F(PinStorageUnitTest, UnlockAttemptCount) {
58 chromeos::PinStorage* pin_storage =
59 chromeos::PinStorageFactory::GetForProfile(profile_.get());
60
61 EXPECT_EQ(0, pin_storage->UnlockAttemptCount());
62
63 pin_storage->AddUnlockAttempt();
64 pin_storage->AddUnlockAttempt();
65 pin_storage->AddUnlockAttempt();
66 EXPECT_EQ(3, pin_storage->UnlockAttemptCount());
67
68 pin_storage->ResetUnlockAttemptCount();
69 EXPECT_EQ(0, pin_storage->UnlockAttemptCount());
70 }
71
72 // Verifies that marking the strong auth makes TimeSinceLastStrongAuth a > zero
73 // value.
74 TEST_F(PinStorageUnitTest, TimeSinceLastStrongAuthReturnsPositiveValue) {
75 chromeos::PinStorage* pin_storage =
76 chromeos::PinStorageFactory::GetForProfile(profile_.get());
77
78 EXPECT_FALSE(pin_storage->HasStrongAuth());
79
80 pin_storage->MarkStrongAuth();
81
82 EXPECT_TRUE(pin_storage->HasStrongAuth());
83 pin_storage->last_strong_auth_ -= base::TimeDelta::FromSeconds(60);
84
85 EXPECT_TRUE(pin_storage->TimeSinceLastStrongAuth() >=
86 base::TimeDelta::FromSeconds(30));
87 }
88
89 // Verifies that the correct pin can be used to authenticate.
90 TEST_F(PinStorageUnitTest, AuthenticationSucceedsWithRightPin) {
91 chromeos::PinStorage* pin_storage =
92 chromeos::PinStorageFactory::GetForProfile(profile_.get());
93
94 pin_storage->SetPin("1111");
95
96 pin_storage->MarkStrongAuth();
97 EXPECT_TRUE(pin_storage->TryAuthenticatePin("1111"));
98 }
99
100 // Verifies that the correct pin will fail to authenticate if too many
101 // authentication attempts have been made.
102 TEST_F(PinStorageUnitTest, AuthenticationFailsFromTooManyAttempts) {
103 chromeos::PinStorage* pin_storage =
104 chromeos::PinStorageFactory::GetForProfile(profile_.get());
105
106 pin_storage->SetPin("1111");
107
108 // Use up all of the authentication attempts so authentication fails.
109 pin_storage->MarkStrongAuth();
110 EXPECT_TRUE(pin_storage->IsPinAuthenticationAvailable());
111 for (int i = 0; i < chromeos::PinStorage::kMaximumUnlockAttempts; ++i)
112 EXPECT_FALSE(pin_storage->TryAuthenticatePin("foobar"));
113
114 // We used up all of the attempts, so entering the right PIN will still fail.
115 EXPECT_FALSE(pin_storage->IsPinAuthenticationAvailable());
116 EXPECT_FALSE(pin_storage->TryAuthenticatePin("1111"));
117 }
118
119 // Verifies that the correct pin will fail to authenticate if it has been too
120 // long since a strong-auth/password authentication.
121 TEST_F(PinStorageUnitTest, AuthenticationFailsFromTimeout) {
122 chromeos::PinStorage* pin_storage =
123 chromeos::PinStorageFactory::GetForProfile(profile_.get());
124
125 pin_storage->SetPin("1111");
126 pin_storage->MarkStrongAuth();
127 EXPECT_TRUE(pin_storage->IsPinAuthenticationAvailable());
128
129 // Move the auth time into the past so that it's larger than the auth timeout.
130 pin_storage->last_strong_auth_ -=
achuithb 2016/05/16 23:05:10 Let's use a setter for testing here
jdufault 2016/05/17 19:58:26 Replaced with a custom test API.
131 chromeos::PinStorage::kStrongAuthTimeout + base::TimeDelta::FromHours(1);
132 EXPECT_FALSE(pin_storage->IsPinAuthenticationAvailable());
133 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698