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

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

Issue 2809993004: cros: Implement cryptohome backend for pin.
Patch Set: Address comments Created 3 years, 6 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/quick_unlock_factory.h"
6 #include "chrome/browser/chromeos/login/quick_unlock/quick_unlock_storage.h"
7 #include "chrome/browser/chromeos/login/quick_unlock/quick_unlock_utils.h"
8 #include "chrome/common/pref_names.h"
9 #include "chrome/test/base/testing_profile.h"
10 #include "components/prefs/pref_service.h"
11 #include "components/prefs/scoped_user_pref_update.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 class PinStorageUnitTest : public testing::Test {
19 protected:
20 PinStorageUnitTest() : profile_(base::MakeUnique<TestingProfile>()) {}
21 ~PinStorageUnitTest() override {}
22
23 // testing::Test:
24 void SetUp() override {
25 quick_unlock::EnableForTesting(quick_unlock::PinStorageType::kPrefs);
26 }
27
28 content::TestBrowserThreadBundle thread_bundle_;
29 std::unique_ptr<TestingProfile> profile_;
30
31 DISALLOW_COPY_AND_ASSIGN(PinStorageUnitTest);
32 };
33
34 } // namespace
35
36 // Provides test-only PinStorage APIs.
37 class PinStorageTestApi {
38 public:
39 // Does *not* take ownership over |pin_storage|.
40 explicit PinStorageTestApi(quick_unlock::PinStorage* pin_storage)
41 : pin_storage_(pin_storage) {}
42
43 std::string PinSalt() const { return pin_storage_->PinSalt(); }
44
45 std::string PinSecret() const { return pin_storage_->PinSecret(); }
46
47 bool IsPinAuthenticationAvailable() const {
48 return pin_storage_->IsPinAuthenticationAvailable();
49 }
50 bool TryAuthenticatePin(const std::string& pin) {
51 return pin_storage_->TryAuthenticatePin(pin);
52 }
53
54 private:
55 quick_unlock::PinStorage* pin_storage_;
56
57 DISALLOW_COPY_AND_ASSIGN(PinStorageTestApi);
58 };
59
60 // Verifies that:
61 // 1. Prefs are initially empty
62 // 2. Setting a PIN will update the pref system.
63 // 3. Removing a PIN clears prefs.
64 TEST_F(PinStorageUnitTest, PinStorageWritesToPrefs) {
65 PrefService* prefs = profile_->GetPrefs();
66
67 EXPECT_EQ("", prefs->GetString(prefs::kQuickUnlockPinSalt));
68 EXPECT_EQ("", prefs->GetString(prefs::kQuickUnlockPinSecret));
69
70 quick_unlock::PinStorage* pin_storage =
71 quick_unlock::QuickUnlockFactory::GetForProfile(profile_.get())
72 ->pin_storage();
73 PinStorageTestApi pin_storage_test(pin_storage);
74
75 pin_storage->SetPin("1111");
76 EXPECT_TRUE(pin_storage->IsPinSet());
77 EXPECT_EQ(pin_storage_test.PinSalt(),
78 prefs->GetString(prefs::kQuickUnlockPinSalt));
79 EXPECT_EQ(pin_storage_test.PinSecret(),
80 prefs->GetString(prefs::kQuickUnlockPinSecret));
81 EXPECT_NE("", pin_storage_test.PinSalt());
82 EXPECT_NE("", pin_storage_test.PinSecret());
83
84 pin_storage->RemovePin();
85 EXPECT_FALSE(pin_storage->IsPinSet());
86 EXPECT_EQ("", prefs->GetString(prefs::kQuickUnlockPinSalt));
87 EXPECT_EQ("", prefs->GetString(prefs::kQuickUnlockPinSecret));
88 }
89
90 // Verifies that:
91 // 1. Initial unlock attempt count is zero.
92 // 2. Attempting unlock attempts correctly increases unlock attempt count.
93 // 3. Resetting unlock attempt count correctly sets attempt count to 0.
94 TEST_F(PinStorageUnitTest, UnlockAttemptCount) {
95 quick_unlock::PinStorage* pin_storage =
96 quick_unlock::QuickUnlockFactory::GetForProfile(profile_.get())
97 ->pin_storage();
98
99 EXPECT_EQ(0, pin_storage->unlock_attempt_count());
100
101 pin_storage->AddUnlockAttempt();
102 pin_storage->AddUnlockAttempt();
103 pin_storage->AddUnlockAttempt();
104 EXPECT_EQ(3, pin_storage->unlock_attempt_count());
105
106 pin_storage->ResetUnlockAttemptCount();
107 EXPECT_EQ(0, pin_storage->unlock_attempt_count());
108 }
109
110 // Verifies that the correct pin can be used to authenticate.
111 TEST_F(PinStorageUnitTest, AuthenticationSucceedsWithRightPin) {
112 quick_unlock::PinStorage* pin_storage =
113 quick_unlock::QuickUnlockFactory::GetForProfile(profile_.get())
114 ->pin_storage();
115 PinStorageTestApi pin_storage_test(pin_storage);
116
117 pin_storage->SetPin("1111");
118
119 EXPECT_TRUE(pin_storage_test.TryAuthenticatePin("1111"));
120 }
121
122 // Verifies that the correct pin will fail to authenticate if too many
123 // authentication attempts have been made.
124 TEST_F(PinStorageUnitTest, AuthenticationFailsFromTooManyAttempts) {
125 quick_unlock::PinStorage* pin_storage =
126 quick_unlock::QuickUnlockFactory::GetForProfile(profile_.get())
127 ->pin_storage();
128 PinStorageTestApi pin_storage_test(pin_storage);
129
130 pin_storage->SetPin("1111");
131
132 // Use up all of the authentication attempts so authentication fails.
133 EXPECT_TRUE(pin_storage_test.IsPinAuthenticationAvailable());
134 for (int i = 0; i < quick_unlock::PinStorage::kMaximumUnlockAttempts; ++i)
135 EXPECT_FALSE(pin_storage_test.TryAuthenticatePin("foobar"));
136
137 // We used up all of the attempts, so entering the right PIN will still fail.
138 EXPECT_FALSE(pin_storage_test.IsPinAuthenticationAvailable());
139 EXPECT_FALSE(pin_storage_test.TryAuthenticatePin("1111"));
140 }
141
142 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698