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