OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/bind.h" | 6 #include "base/bind.h" |
7 #include "base/bind_helpers.h" | 7 #include "base/bind_helpers.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
15 #include "components/password_manager/core/browser/login_database.h" | 15 #include "components/password_manager/core/browser/login_database.h" |
16 #include "components/password_manager/core/browser/password_form_data.h" | 16 #include "components/password_manager/core/browser/password_form_data.h" |
17 #include "components/password_manager/core/browser/password_store_change.h" | 17 #include "components/password_manager/core/browser/password_store_change.h" |
18 #include "components/password_manager/core/browser/password_store_consumer.h" | 18 #include "components/password_manager/core/browser/password_store_consumer.h" |
19 #include "components/password_manager/core/browser/password_store_default.h" | 19 #include "components/password_manager/core/browser/password_store_default.h" |
20 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
22 | 22 |
23 using autofill::PasswordForm; | 23 using autofill::PasswordForm; |
24 using testing::_; | |
25 using testing::ElementsAreArray; | 24 using testing::ElementsAreArray; |
26 using testing::WithArg; | 25 using testing::IsEmpty; |
27 | 26 |
28 namespace password_manager { | 27 namespace password_manager { |
29 | 28 |
30 namespace { | 29 namespace { |
31 | 30 |
32 class MockPasswordStoreConsumer : public PasswordStoreConsumer { | 31 class MockPasswordStoreConsumer : public PasswordStoreConsumer { |
33 public: | 32 public: |
34 MOCK_METHOD1(OnGetPasswordStoreResults, | 33 MOCK_METHOD1(OnGetPasswordStoreResultsConstRef, |
35 void(const std::vector<PasswordForm*>&)); | 34 void(const std::vector<PasswordForm*>&)); |
| 35 |
| 36 // GMock cannot mock methods with move-only args. |
| 37 void OnGetPasswordStoreResults(ScopedVector<PasswordForm> results) override { |
| 38 OnGetPasswordStoreResultsConstRef(results.get()); |
| 39 } |
36 }; | 40 }; |
37 | 41 |
38 class MockPasswordStoreObserver : public PasswordStore::Observer { | 42 class MockPasswordStoreObserver : public PasswordStore::Observer { |
39 public: | 43 public: |
40 MOCK_METHOD1(OnLoginsChanged, void(const PasswordStoreChangeList& changes)); | 44 MOCK_METHOD1(OnLoginsChanged, void(const PasswordStoreChangeList& changes)); |
41 }; | 45 }; |
42 | 46 |
43 // A mock LoginDatabase that simulates a failing Init() method. | 47 // A mock LoginDatabase that simulates a failing Init() method. |
44 class BadLoginDatabase : public LoginDatabase { | 48 class BadLoginDatabase : public LoginDatabase { |
45 public: | 49 public: |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 "http://foo.example.com/action", | 111 "http://foo.example.com/action", |
108 L"มีสีสัน", | 112 L"มีสีสัน", |
109 L"お元気ですか?", | 113 L"お元気ですか?", |
110 L"盆栽", | 114 L"盆栽", |
111 L"أحب كرة", | 115 L"أحب كرة", |
112 L"£éä국수çà", | 116 L"£éä국수çà", |
113 true, false, 1 }, | 117 true, false, 1 }, |
114 }; | 118 }; |
115 | 119 |
116 // Build the expected forms vector and add the forms to the store. | 120 // Build the expected forms vector and add the forms to the store. |
117 std::vector<PasswordForm*> expected_forms; | 121 ScopedVector<PasswordForm> expected_forms; |
118 for (unsigned int i = 0; i < arraysize(form_data); ++i) { | 122 for (unsigned int i = 0; i < arraysize(form_data); ++i) { |
119 PasswordForm* form = CreatePasswordFormFromData(form_data[i]); | 123 expected_forms.push_back( |
120 expected_forms.push_back(form); | 124 CreatePasswordFormFromData(form_data[i]).release()); |
121 store->AddLogin(*form); | 125 store->AddLogin(*expected_forms.back()); |
122 } | 126 } |
123 | 127 |
124 base::MessageLoop::current()->RunUntilIdle(); | 128 base::MessageLoop::current()->RunUntilIdle(); |
125 | 129 |
126 MockPasswordStoreConsumer consumer; | 130 MockPasswordStoreConsumer consumer; |
127 | 131 |
128 // We expect to get the same data back, even though it's not all ASCII. | 132 // We expect to get the same data back, even though it's not all ASCII. |
129 EXPECT_CALL(consumer, | 133 EXPECT_CALL(consumer, OnGetPasswordStoreResultsConstRef( |
130 OnGetPasswordStoreResults(ContainsAllPasswordForms(expected_forms))) | 134 ContainsSamePasswordForms(expected_forms.get()))); |
131 .WillOnce(WithArg<0>(STLDeleteElements0())); | |
132 store->GetAutofillableLogins(&consumer); | 135 store->GetAutofillableLogins(&consumer); |
133 | 136 |
134 base::MessageLoop::current()->RunUntilIdle(); | 137 base::MessageLoop::current()->RunUntilIdle(); |
135 | 138 |
136 STLDeleteElements(&expected_forms); | |
137 store->Shutdown(); | 139 store->Shutdown(); |
138 base::MessageLoop::current()->RunUntilIdle(); | 140 base::MessageLoop::current()->RunUntilIdle(); |
139 } | 141 } |
140 | 142 |
141 TEST_F(PasswordStoreDefaultTest, Notifications) { | 143 TEST_F(PasswordStoreDefaultTest, Notifications) { |
142 scoped_refptr<PasswordStoreDefault> store(new PasswordStoreDefault( | 144 scoped_refptr<PasswordStoreDefault> store(new PasswordStoreDefault( |
143 base::MessageLoopProxy::current(), base::MessageLoopProxy::current(), | 145 base::MessageLoopProxy::current(), base::MessageLoopProxy::current(), |
144 make_scoped_ptr(new LoginDatabase(test_login_db_file_path())))); | 146 make_scoped_ptr(new LoginDatabase(test_login_db_file_path())))); |
145 store->Init(syncer::SyncableService::StartSyncFlare()); | 147 store->Init(syncer::SyncableService::StartSyncFlare()); |
146 | 148 |
147 scoped_ptr<PasswordForm> form( | 149 scoped_ptr<PasswordForm> form = |
148 CreatePasswordFormFromData(CreateTestPasswordFormData())); | 150 CreatePasswordFormFromData(CreateTestPasswordFormData()); |
149 | 151 |
150 MockPasswordStoreObserver observer; | 152 MockPasswordStoreObserver observer; |
151 store->AddObserver(&observer); | 153 store->AddObserver(&observer); |
152 | 154 |
153 const PasswordStoreChange expected_add_changes[] = { | 155 const PasswordStoreChange expected_add_changes[] = { |
154 PasswordStoreChange(PasswordStoreChange::ADD, *form), | 156 PasswordStoreChange(PasswordStoreChange::ADD, *form), |
155 }; | 157 }; |
156 | 158 |
157 EXPECT_CALL(observer, | 159 EXPECT_CALL(observer, |
158 OnLoginsChanged(ElementsAreArray(expected_add_changes))); | 160 OnLoginsChanged(ElementsAreArray(expected_add_changes))); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 make_scoped_ptr<LoginDatabase>(new BadLoginDatabase))); | 202 make_scoped_ptr<LoginDatabase>(new BadLoginDatabase))); |
201 | 203 |
202 bad_store->Init(syncer::SyncableService::StartSyncFlare()); | 204 bad_store->Init(syncer::SyncableService::StartSyncFlare()); |
203 base::MessageLoop::current()->RunUntilIdle(); | 205 base::MessageLoop::current()->RunUntilIdle(); |
204 ASSERT_EQ(nullptr, bad_store->login_db()); | 206 ASSERT_EQ(nullptr, bad_store->login_db()); |
205 | 207 |
206 testing::StrictMock<MockPasswordStoreObserver> mock_observer; | 208 testing::StrictMock<MockPasswordStoreObserver> mock_observer; |
207 bad_store->AddObserver(&mock_observer); | 209 bad_store->AddObserver(&mock_observer); |
208 | 210 |
209 // Add a new autofillable login + a blacklisted login. | 211 // Add a new autofillable login + a blacklisted login. |
210 scoped_ptr<PasswordForm> form( | 212 scoped_ptr<PasswordForm> form = |
211 CreatePasswordFormFromData(CreateTestPasswordFormData())); | 213 CreatePasswordFormFromData(CreateTestPasswordFormData()); |
212 scoped_ptr<PasswordForm> blacklisted_form(new PasswordForm(*form)); | 214 scoped_ptr<PasswordForm> blacklisted_form(new PasswordForm(*form)); |
213 blacklisted_form->signon_realm = "http://foo.example.com"; | 215 blacklisted_form->signon_realm = "http://foo.example.com"; |
214 blacklisted_form->origin = GURL("http://foo.example.com/origin"); | 216 blacklisted_form->origin = GURL("http://foo.example.com/origin"); |
215 blacklisted_form->action = GURL("http://foo.example.com/action"); | 217 blacklisted_form->action = GURL("http://foo.example.com/action"); |
216 blacklisted_form->blacklisted_by_user = true; | 218 blacklisted_form->blacklisted_by_user = true; |
217 bad_store->AddLogin(*form); | 219 bad_store->AddLogin(*form); |
218 bad_store->AddLogin(*blacklisted_form); | 220 bad_store->AddLogin(*blacklisted_form); |
219 base::MessageLoop::current()->RunUntilIdle(); | 221 base::MessageLoop::current()->RunUntilIdle(); |
220 | 222 |
221 // Get all logins; autofillable logins; blacklisted logins. | 223 // Get all logins; autofillable logins; blacklisted logins. |
222 testing::StrictMock<MockPasswordStoreConsumer> mock_consumer; | 224 testing::StrictMock<MockPasswordStoreConsumer> mock_consumer; |
223 EXPECT_CALL(mock_consumer, OnGetPasswordStoreResults(testing::ElementsAre())); | 225 EXPECT_CALL(mock_consumer, OnGetPasswordStoreResultsConstRef(IsEmpty())); |
224 bad_store->GetLogins(*form, PasswordStore::DISALLOW_PROMPT, &mock_consumer); | 226 bad_store->GetLogins(*form, PasswordStore::DISALLOW_PROMPT, &mock_consumer); |
225 base::MessageLoop::current()->RunUntilIdle(); | 227 base::MessageLoop::current()->RunUntilIdle(); |
226 testing::Mock::VerifyAndClearExpectations(&mock_consumer); | 228 testing::Mock::VerifyAndClearExpectations(&mock_consumer); |
227 EXPECT_CALL(mock_consumer, OnGetPasswordStoreResults(testing::ElementsAre())); | 229 EXPECT_CALL(mock_consumer, OnGetPasswordStoreResultsConstRef(IsEmpty())); |
228 bad_store->GetAutofillableLogins(&mock_consumer); | 230 bad_store->GetAutofillableLogins(&mock_consumer); |
229 base::MessageLoop::current()->RunUntilIdle(); | 231 base::MessageLoop::current()->RunUntilIdle(); |
230 testing::Mock::VerifyAndClearExpectations(&mock_consumer); | 232 testing::Mock::VerifyAndClearExpectations(&mock_consumer); |
231 EXPECT_CALL(mock_consumer, OnGetPasswordStoreResults(testing::ElementsAre())); | 233 EXPECT_CALL(mock_consumer, OnGetPasswordStoreResultsConstRef(IsEmpty())); |
232 bad_store->GetBlacklistLogins(&mock_consumer); | 234 bad_store->GetBlacklistLogins(&mock_consumer); |
233 base::MessageLoop::current()->RunUntilIdle(); | 235 base::MessageLoop::current()->RunUntilIdle(); |
234 | 236 |
235 // Report metrics. | 237 // Report metrics. |
236 bad_store->ReportMetrics("Test Username", true); | 238 bad_store->ReportMetrics("Test Username", true); |
237 base::MessageLoop::current()->RunUntilIdle(); | 239 base::MessageLoop::current()->RunUntilIdle(); |
238 | 240 |
239 // Change the login. | 241 // Change the login. |
240 form->password_value = base::ASCIIToUTF16("a different password"); | 242 form->password_value = base::ASCIIToUTF16("a different password"); |
241 bad_store->UpdateLogin(*form); | 243 bad_store->UpdateLogin(*form); |
242 base::MessageLoop::current()->RunUntilIdle(); | 244 base::MessageLoop::current()->RunUntilIdle(); |
243 | 245 |
244 // Delete one login; a range of logins. | 246 // Delete one login; a range of logins. |
245 bad_store->RemoveLogin(*form); | 247 bad_store->RemoveLogin(*form); |
246 base::MessageLoop::current()->RunUntilIdle(); | 248 base::MessageLoop::current()->RunUntilIdle(); |
247 bad_store->RemoveLoginsCreatedBetween(base::Time(), base::Time::Max()); | 249 bad_store->RemoveLoginsCreatedBetween(base::Time(), base::Time::Max()); |
248 base::MessageLoop::current()->RunUntilIdle(); | 250 base::MessageLoop::current()->RunUntilIdle(); |
249 bad_store->RemoveLoginsSyncedBetween(base::Time(), base::Time::Max()); | 251 bad_store->RemoveLoginsSyncedBetween(base::Time(), base::Time::Max()); |
250 base::MessageLoop::current()->RunUntilIdle(); | 252 base::MessageLoop::current()->RunUntilIdle(); |
251 | 253 |
252 // Ensure no notifications and no explosions during shutdown either. | 254 // Ensure no notifications and no explosions during shutdown either. |
253 bad_store->RemoveObserver(&mock_observer); | 255 bad_store->RemoveObserver(&mock_observer); |
254 bad_store->Shutdown(); | 256 bad_store->Shutdown(); |
255 base::MessageLoop::current()->RunUntilIdle(); | 257 base::MessageLoop::current()->RunUntilIdle(); |
256 } | 258 } |
257 | 259 |
258 } // namespace password_manager | 260 } // namespace password_manager |
OLD | NEW |