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

Side by Side Diff: chrome/browser/password_manager/password_store_proxy_mac_unittest.cc

Issue 1213043003: Start the migration of passwords from the Keychain. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: move the enum Created 5 years, 5 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
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/password_manager/password_store_proxy_mac.h" 5 #include "chrome/browser/password_manager/password_store_proxy_mac.h"
6 6
7 #include "base/files/scoped_temp_dir.h" 7 #include "base/files/scoped_temp_dir.h"
8 #include "base/scoped_observer.h" 8 #include "base/scoped_observer.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/password_manager/password_store_mac_internal.h"
11 #include "chrome/browser/prefs/browser_prefs.h"
12 #include "chrome/test/base/testing_pref_service_syncable.h"
10 #include "components/os_crypt/os_crypt.h" 13 #include "components/os_crypt/os_crypt.h"
11 #include "components/password_manager/core/browser/login_database.h" 14 #include "components/password_manager/core/browser/login_database.h"
12 #include "components/password_manager/core/browser/password_manager_test_utils.h " 15 #include "components/password_manager/core/browser/password_manager_test_utils.h "
13 #include "components/password_manager/core/browser/password_store_consumer.h" 16 #include "components/password_manager/core/browser/password_store_consumer.h"
17 #include "components/password_manager/core/common/password_manager_pref_names.h"
14 #include "content/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread.h"
15 #include "content/public/test/test_browser_thread_bundle.h" 19 #include "content/public/test/test_browser_thread_bundle.h"
16 #include "crypto/mock_apple_keychain.h" 20 #include "crypto/mock_apple_keychain.h"
17 #include "testing/gmock/include/gmock/gmock.h" 21 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
19 23
20 namespace { 24 namespace {
21 25
22 using autofill::PasswordForm; 26 using autofill::PasswordForm;
23 using content::BrowserThread; 27 using content::BrowserThread;
28 using password_manager::MigrationStatus;
29 using password_manager::PasswordStoreChange;
30 using password_manager::PasswordStoreChangeList;
24 using testing::_; 31 using testing::_;
25 using testing::ElementsAre; 32 using testing::ElementsAre;
26 using testing::IsEmpty; 33 using testing::IsEmpty;
27 using testing::Pointee; 34 using testing::Pointee;
28 35
29 ACTION(QuitUIMessageLoop) { 36 ACTION(QuitUIMessageLoop) {
30 DCHECK_CURRENTLY_ON(BrowserThread::UI); 37 DCHECK_CURRENTLY_ON(BrowserThread::UI);
31 base::MessageLoop::current()->Quit(); 38 base::MessageLoop::current()->Quit();
32 } 39 }
33 40
41 PasswordStoreChangeList AddChangeForForm(const PasswordForm& form) {
stuartmorgan 2015/07/07 17:40:25 Needs a comment.
vasilii 2015/07/08 09:56:58 Done.
42 return PasswordStoreChangeList(
43 1, PasswordStoreChange(PasswordStoreChange::ADD, form));
44 }
45
34 class MockPasswordStoreConsumer 46 class MockPasswordStoreConsumer
35 : public password_manager::PasswordStoreConsumer { 47 : public password_manager::PasswordStoreConsumer {
36 public: 48 public:
37 MOCK_METHOD1(OnGetPasswordStoreResultsConstRef, 49 MOCK_METHOD1(OnGetPasswordStoreResultsConstRef,
38 void(const std::vector<PasswordForm*>&)); 50 void(const std::vector<PasswordForm*>&));
39 51
40 // GMock cannot mock methods with move-only args. 52 // GMock cannot mock methods with move-only args.
41 void OnGetPasswordStoreResults(ScopedVector<PasswordForm> results) override { 53 void OnGetPasswordStoreResults(ScopedVector<PasswordForm> results) override {
42 OnGetPasswordStoreResultsConstRef(results.get()); 54 OnGetPasswordStoreResultsConstRef(results.get());
43 } 55 }
(...skipping 19 matching lines...) Expand all
63 BadLoginDatabase() : password_manager::LoginDatabase(base::FilePath()) {} 75 BadLoginDatabase() : password_manager::LoginDatabase(base::FilePath()) {}
64 ~BadLoginDatabase() override {} 76 ~BadLoginDatabase() override {}
65 77
66 // LoginDatabase: 78 // LoginDatabase:
67 bool Init() override { return false; } 79 bool Init() override { return false; }
68 80
69 private: 81 private:
70 DISALLOW_COPY_AND_ASSIGN(BadLoginDatabase); 82 DISALLOW_COPY_AND_ASSIGN(BadLoginDatabase);
71 }; 83 };
72 84
73 class PasswordStoreProxyMacTest : public testing::Test { 85 class PasswordStoreProxyMacTest
86 : public testing::TestWithParam<MigrationStatus> {
74 public: 87 public:
88 PasswordStoreProxyMacTest();
89 ~PasswordStoreProxyMacTest() override;
90
75 void SetUp() override; 91 void SetUp() override;
76 void TearDown() override; 92 void TearDown() override;
77 93
78 void CreateAndInitPasswordStore( 94 void CreateAndInitPasswordStore(
79 scoped_ptr<password_manager::LoginDatabase> login_db); 95 scoped_ptr<password_manager::LoginDatabase> login_db);
80 96
81 void ClosePasswordStore(); 97 void ClosePasswordStore();
82 98
83 // Do a store-level query to wait for all the previously enqueued operations 99 // Do a store-level query to wait for all the previously enqueued operations
84 // to finish. 100 // to finish.
(...skipping 14 matching lines...) Expand all
99 return store_->login_metadata_db(); 115 return store_->login_metadata_db();
100 } 116 }
101 117
102 PasswordStoreProxyMac* store() { return store_.get(); } 118 PasswordStoreProxyMac* store() { return store_.get(); }
103 119
104 protected: 120 protected:
105 content::TestBrowserThreadBundle ui_thread_; 121 content::TestBrowserThreadBundle ui_thread_;
106 122
107 base::ScopedTempDir db_dir_; 123 base::ScopedTempDir db_dir_;
108 scoped_refptr<PasswordStoreProxyMac> store_; 124 scoped_refptr<PasswordStoreProxyMac> store_;
125 TestingPrefServiceSyncable testing_prefs_;
109 }; 126 };
110 127
111 void PasswordStoreProxyMacTest::SetUp() { 128 PasswordStoreProxyMacTest::PasswordStoreProxyMacTest() {
112 ASSERT_TRUE(db_dir_.CreateUniqueTempDir()); 129 EXPECT_TRUE(db_dir_.CreateUniqueTempDir());
113 130 chrome::RegisterUserProfilePrefs(testing_prefs_.registry());
131 testing_prefs_.SetInteger(password_manager::prefs::kKeychainMigrationStatus,
132 static_cast<int>(GetParam()));
114 // Ensure that LoginDatabase will use the mock keychain if it needs to 133 // Ensure that LoginDatabase will use the mock keychain if it needs to
115 // encrypt/decrypt a password. 134 // encrypt/decrypt a password.
116 OSCrypt::UseMockKeychain(true); 135 OSCrypt::UseMockKeychain(true);
136 }
137
138 PasswordStoreProxyMacTest::~PasswordStoreProxyMacTest() {
139 }
140
141 void PasswordStoreProxyMacTest::SetUp() {
117 scoped_ptr<password_manager::LoginDatabase> login_db( 142 scoped_ptr<password_manager::LoginDatabase> login_db(
118 new password_manager::LoginDatabase(test_login_db_file_path())); 143 new password_manager::LoginDatabase(test_login_db_file_path()));
119 CreateAndInitPasswordStore(login_db.Pass()); 144 CreateAndInitPasswordStore(login_db.Pass());
120 // Make sure deferred initialization is performed before some tests start
121 // accessing the |login_db| directly.
122 FinishAsyncProcessing();
123 } 145 }
124 146
125 void PasswordStoreProxyMacTest::TearDown() { 147 void PasswordStoreProxyMacTest::TearDown() {
126 ClosePasswordStore(); 148 ClosePasswordStore();
127 } 149 }
128 150
129 void PasswordStoreProxyMacTest::CreateAndInitPasswordStore( 151 void PasswordStoreProxyMacTest::CreateAndInitPasswordStore(
130 scoped_ptr<password_manager::LoginDatabase> login_db) { 152 scoped_ptr<password_manager::LoginDatabase> login_db) {
131 store_ = new PasswordStoreProxyMac( 153 store_ = new PasswordStoreProxyMac(
132 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI), 154 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
133 make_scoped_ptr(new crypto::MockAppleKeychain), login_db.Pass()); 155 make_scoped_ptr(new crypto::MockAppleKeychain), login_db.Pass(),
156 &testing_prefs_);
134 ASSERT_TRUE(store_->Init(syncer::SyncableService::StartSyncFlare())); 157 ASSERT_TRUE(store_->Init(syncer::SyncableService::StartSyncFlare()));
135 } 158 }
136 159
137 void PasswordStoreProxyMacTest::ClosePasswordStore() { 160 void PasswordStoreProxyMacTest::ClosePasswordStore() {
138 if (!store_) 161 if (!store_)
139 return; 162 return;
140 store_->Shutdown(); 163 store_->Shutdown();
141 EXPECT_FALSE(store_->GetBackgroundTaskRunner()); 164 EXPECT_FALSE(store_->GetBackgroundTaskRunner());
142 base::MessageLoop::current()->RunUntilIdle();
143 store_ = nullptr; 165 store_ = nullptr;
144 } 166 }
145 167
146 void PasswordStoreProxyMacTest::FinishAsyncProcessing() { 168 void PasswordStoreProxyMacTest::FinishAsyncProcessing() {
147 // Do a store-level query to wait for all the previously enqueued operations 169 // Do a store-level query to wait for all the previously enqueued operations
148 // to finish. 170 // to finish.
149 MockPasswordStoreConsumer consumer; 171 MockPasswordStoreConsumer consumer;
150 store_->GetLogins(PasswordForm(), 172 store_->GetLogins(PasswordForm(),
151 password_manager::PasswordStore::ALLOW_PROMPT, &consumer); 173 password_manager::PasswordStore::ALLOW_PROMPT, &consumer);
152 EXPECT_CALL(consumer, OnGetPasswordStoreResultsConstRef(_)) 174 EXPECT_CALL(consumer, OnGetPasswordStoreResultsConstRef(_))
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 EXPECT_CALL(mock_observer, OnLoginsChanged(list)); 244 EXPECT_CALL(mock_observer, OnLoginsChanged(list));
223 if (check_created) { 245 if (check_created) {
224 store()->RemoveLoginsCreatedBetween(base::Time(), next_day, 246 store()->RemoveLoginsCreatedBetween(base::Time(), next_day,
225 base::Closure()); 247 base::Closure());
226 } else { 248 } else {
227 store()->RemoveLoginsSyncedBetween(base::Time(), next_day); 249 store()->RemoveLoginsSyncedBetween(base::Time(), next_day);
228 } 250 }
229 FinishAsyncProcessing(); 251 FinishAsyncProcessing();
230 } 252 }
231 253
232 TEST_F(PasswordStoreProxyMacTest, FormLifeCycle) { 254 // ----------- Tests -------------
255
256 TEST_P(PasswordStoreProxyMacTest, StartAndStop) {
257 // PasswordStore::Shutdown() immediately follows PasswordStore::Init(). The
258 // message loop isn't running in between. Anyway, PasswordStore should end up
259 // in the right state.
260 ClosePasswordStore();
261
262 int status = testing_prefs_.GetInteger(
263 password_manager::prefs::kKeychainMigrationStatus);
264 if (GetParam() == MigrationStatus::NOT_STARTED ||
265 GetParam() == MigrationStatus::FAILED_ONCE) {
266 EXPECT_EQ(static_cast<int>(MigrationStatus::MIGRATED), status);
267 } else {
268 EXPECT_EQ(static_cast<int>(GetParam()), status);
269 }
270 }
271
272 TEST_P(PasswordStoreProxyMacTest, FormLifeCycle) {
233 PasswordForm password_form; 273 PasswordForm password_form;
234 password_form.origin = GURL("http://example.com"); 274 password_form.origin = GURL("http://example.com");
235 password_form.username_value = base::ASCIIToUTF16("test1@gmail.com"); 275 password_form.username_value = base::ASCIIToUTF16("test1@gmail.com");
236 password_form.password_value = base::ASCIIToUTF16("12345"); 276 password_form.password_value = base::ASCIIToUTF16("12345");
237 password_form.signon_realm = "http://example.com/"; 277 password_form.signon_realm = "http://example.com/";
238 278
239 AddForm(password_form); 279 AddForm(password_form);
240 password_form.password_value = base::ASCIIToUTF16("password"); 280 password_form.password_value = base::ASCIIToUTF16("password");
241 UpdateForm(password_form); 281 UpdateForm(password_form);
242 RemoveForm(password_form); 282 RemoveForm(password_form);
243 } 283 }
244 284
245 TEST_F(PasswordStoreProxyMacTest, TestRemoveLoginsCreatedBetween) { 285 TEST_P(PasswordStoreProxyMacTest, TestRemoveLoginsCreatedBetween) {
246 CheckRemoveLoginsBetween(true); 286 CheckRemoveLoginsBetween(true);
247 } 287 }
248 288
249 TEST_F(PasswordStoreProxyMacTest, TestRemoveLoginsSyncedBetween) { 289 TEST_P(PasswordStoreProxyMacTest, TestRemoveLoginsSyncedBetween) {
250 CheckRemoveLoginsBetween(false); 290 CheckRemoveLoginsBetween(false);
251 } 291 }
252 292
253 TEST_F(PasswordStoreProxyMacTest, FillLogins) { 293 TEST_P(PasswordStoreProxyMacTest, FillLogins) {
254 PasswordForm password_form; 294 PasswordForm password_form;
255 password_form.origin = GURL("http://example.com"); 295 password_form.origin = GURL("http://example.com");
256 password_form.signon_realm = "http://example.com/"; 296 password_form.signon_realm = "http://example.com/";
257 password_form.username_value = base::ASCIIToUTF16("test1@gmail.com"); 297 password_form.username_value = base::ASCIIToUTF16("test1@gmail.com");
258 password_form.password_value = base::ASCIIToUTF16("12345"); 298 password_form.password_value = base::ASCIIToUTF16("12345");
259 AddForm(password_form); 299 AddForm(password_form);
260 300
261 PasswordForm blacklisted_form; 301 PasswordForm blacklisted_form;
262 blacklisted_form.origin = GURL("http://example2.com"); 302 blacklisted_form.origin = GURL("http://example2.com");
263 blacklisted_form.signon_realm = "http://example2.com/"; 303 blacklisted_form.signon_realm = "http://example2.com/";
(...skipping 14 matching lines...) Expand all
278 .WillOnce(QuitUIMessageLoop()); 318 .WillOnce(QuitUIMessageLoop());
279 base::MessageLoop::current()->Run(); 319 base::MessageLoop::current()->Run();
280 320
281 store()->GetAutofillableLogins(&mock_consumer); 321 store()->GetAutofillableLogins(&mock_consumer);
282 EXPECT_CALL(mock_consumer, OnGetPasswordStoreResultsConstRef( 322 EXPECT_CALL(mock_consumer, OnGetPasswordStoreResultsConstRef(
283 ElementsAre(Pointee(password_form)))) 323 ElementsAre(Pointee(password_form))))
284 .WillOnce(QuitUIMessageLoop()); 324 .WillOnce(QuitUIMessageLoop());
285 base::MessageLoop::current()->Run(); 325 base::MessageLoop::current()->Run();
286 } 326 }
287 327
288 TEST_F(PasswordStoreProxyMacTest, OperationsOnABadDatabaseSilentlyFail) { 328 TEST_P(PasswordStoreProxyMacTest, OperationsOnABadDatabaseSilentlyFail) {
289 // Verify that operations on a PasswordStore with a bad database cause no 329 // Verify that operations on a PasswordStore with a bad database cause no
290 // explosions, but fail without side effect, return no data and trigger no 330 // explosions, but fail without side effect, return no data and trigger no
291 // notifications. 331 // notifications.
292 ClosePasswordStore(); 332 ClosePasswordStore();
293 CreateAndInitPasswordStore(make_scoped_ptr(new BadLoginDatabase)); 333 CreateAndInitPasswordStore(make_scoped_ptr(new BadLoginDatabase));
294 FinishAsyncProcessing(); 334 FinishAsyncProcessing();
295 EXPECT_FALSE(login_db()); 335 EXPECT_FALSE(login_db());
296 336
297 // The store should outlive the observer. 337 // The store should outlive the observer.
298 scoped_refptr<PasswordStoreProxyMac> store_refptr = store(); 338 scoped_refptr<PasswordStoreProxyMac> store_refptr = store();
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
344 // Delete one login; a range of logins. 384 // Delete one login; a range of logins.
345 store()->RemoveLogin(*form); 385 store()->RemoveLogin(*form);
346 store()->RemoveLoginsCreatedBetween(base::Time(), base::Time::Max(), 386 store()->RemoveLoginsCreatedBetween(base::Time(), base::Time::Max(),
347 base::Closure()); 387 base::Closure());
348 store()->RemoveLoginsSyncedBetween(base::Time(), base::Time::Max()); 388 store()->RemoveLoginsSyncedBetween(base::Time(), base::Time::Max());
349 FinishAsyncProcessing(); 389 FinishAsyncProcessing();
350 390
351 // Verify no notifications are fired during shutdown either. 391 // Verify no notifications are fired during shutdown either.
352 ClosePasswordStore(); 392 ClosePasswordStore();
353 } 393 }
394
395 INSTANTIATE_TEST_CASE_P(,
396 PasswordStoreProxyMacTest,
397 testing::Values(MigrationStatus::NOT_STARTED,
398 MigrationStatus::MIGRATED,
399 MigrationStatus::FAILED_ONCE,
400 MigrationStatus::FAILED_TWICE));
401
402 // Test the migration process.
403 class PasswordStoreProxyMacMigrationTest : public PasswordStoreProxyMacTest {
404 public:
405 void SetUp() override;
406
407 void TestMigration(bool lock_keychain);
408
409 protected:
410 scoped_ptr<password_manager::LoginDatabase> login_db_;
411 scoped_ptr<crypto::MockAppleKeychain> keychain_;
412 };
413
414 void PasswordStoreProxyMacMigrationTest::SetUp() {
415 login_db_.reset(
416 new password_manager::LoginDatabase(test_login_db_file_path()));
417 keychain_.reset(new crypto::MockAppleKeychain);
418 }
419
420 void PasswordStoreProxyMacMigrationTest::TestMigration(bool lock_keychain) {
421 PasswordForm form;
422 form.origin = GURL("http://accounts.google.com/LoginAuth");
423 form.signon_realm = "http://accounts.google.com/";
424 form.username_value = base::ASCIIToUTF16("my_username");
425 form.password_value = base::ASCIIToUTF16("12345");
426
427 if (GetParam() != MigrationStatus::MIGRATED)
428 login_db_->set_clear_password_values(true);
429 EXPECT_TRUE(login_db_->Init());
430 EXPECT_EQ(AddChangeForForm(form), login_db_->AddLogin(form));
431 // Prepare another database instance with the same content which is to be
432 // initialized by PasswordStoreProxyMac.
433 login_db_.reset(
434 new password_manager::LoginDatabase(test_login_db_file_path()));
435 MacKeychainPasswordFormAdapter adapter(keychain_.get());
436 EXPECT_TRUE(adapter.AddPassword(form));
437
438 // Init the store. It may trigger the migration.
439 if (lock_keychain)
440 keychain_->set_locked(true);
441 crypto::MockAppleKeychain* weak_keychain = keychain_.get();
442 store_ = new PasswordStoreProxyMac(
443 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
444 keychain_.Pass(), login_db_.Pass(), &testing_prefs_);
445 ASSERT_TRUE(store_->Init(syncer::SyncableService::StartSyncFlare()));
446 FinishAsyncProcessing();
447
448 // Check the password is still there.
449 if (lock_keychain)
450 weak_keychain->set_locked(false);
451 MockPasswordStoreConsumer mock_consumer;
452 store()->GetLogins(form, PasswordStoreProxyMac::ALLOW_PROMPT, &mock_consumer);
453 EXPECT_CALL(mock_consumer,
454 OnGetPasswordStoreResultsConstRef(ElementsAre(Pointee(form))))
455 .WillOnce(QuitUIMessageLoop());
456 base::MessageLoop::current()->Run();
457
458 int status = testing_prefs_.GetInteger(
459 password_manager::prefs::kKeychainMigrationStatus);
460 if (GetParam() == MigrationStatus::MIGRATED ||
461 GetParam() == MigrationStatus::FAILED_TWICE) {
462 EXPECT_EQ(static_cast<int>(GetParam()), status);
463 } else if (lock_keychain) {
464 EXPECT_EQ(static_cast<int>(GetParam() == MigrationStatus::NOT_STARTED
465 ? MigrationStatus::FAILED_ONCE
466 : MigrationStatus::FAILED_TWICE),
467 status);
468 } else {
469 EXPECT_EQ(static_cast<int>(MigrationStatus::MIGRATED), status);
470 }
471 }
472
473 TEST_P(PasswordStoreProxyMacMigrationTest, TestSuccessfullMigration) {
474 TestMigration(false);
475 }
476
477 TEST_P(PasswordStoreProxyMacMigrationTest, TestFailedMigration) {
478 TestMigration(true);
479 }
480
481 INSTANTIATE_TEST_CASE_P(,
482 PasswordStoreProxyMacMigrationTest,
483 testing::Values(MigrationStatus::NOT_STARTED,
484 MigrationStatus::MIGRATED,
485 MigrationStatus::FAILED_ONCE,
486 MigrationStatus::FAILED_TWICE));
487
354 } // namespace 488 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698