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

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: fix the tests 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) {
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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 list.push_back(password_manager::PasswordStoreChange( 242 list.push_back(password_manager::PasswordStoreChange(
221 password_manager::PasswordStoreChange::REMOVE, old_form)); 243 password_manager::PasswordStoreChange::REMOVE, old_form));
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 else 247 else
226 store()->RemoveLoginsSyncedBetween(base::Time(), next_day); 248 store()->RemoveLoginsSyncedBetween(base::Time(), next_day);
227 FinishAsyncProcessing(); 249 FinishAsyncProcessing();
228 } 250 }
229 251
230 TEST_F(PasswordStoreProxyMacTest, FormLifeCycle) { 252 // ----------- Tests -------------
253
254 TEST_P(PasswordStoreProxyMacTest, StartAndStop) {
255 // PasswordStore::Shutdown() immediately follows PasswordStore::Init(). The
256 // message loop isn't running in between. Anyway, PasswordStore should end up
257 // in the right state.
258 ClosePasswordStore();
259
260 int status = testing_prefs_.GetInteger(
261 password_manager::prefs::kKeychainMigrationStatus);
262 if (GetParam() == MigrationStatus::NOT_STARTED ||
263 GetParam() == MigrationStatus::FAILED_ONCE) {
264 EXPECT_EQ(static_cast<int>(MigrationStatus::MIGRATED), status);
vabr (Chromium) 2015/07/06 09:50:19 How do we ensure that store's ShutDown() did not s
vasilii 2015/07/06 13:57:58 Shutdown() calls Thread::Stop which runs the messa
265 } else {
266 EXPECT_EQ(static_cast<int>(GetParam()), status);
267 }
268 }
269
270 TEST_P(PasswordStoreProxyMacTest, FormLifeCycle) {
231 PasswordForm password_form; 271 PasswordForm password_form;
232 password_form.origin = GURL("http://example.com"); 272 password_form.origin = GURL("http://example.com");
233 password_form.username_value = base::ASCIIToUTF16("test1@gmail.com"); 273 password_form.username_value = base::ASCIIToUTF16("test1@gmail.com");
234 password_form.password_value = base::ASCIIToUTF16("12345"); 274 password_form.password_value = base::ASCIIToUTF16("12345");
235 password_form.signon_realm = "http://example.com/"; 275 password_form.signon_realm = "http://example.com/";
236 276
237 AddForm(password_form); 277 AddForm(password_form);
238 password_form.password_value = base::ASCIIToUTF16("password"); 278 password_form.password_value = base::ASCIIToUTF16("password");
239 UpdateForm(password_form); 279 UpdateForm(password_form);
240 RemoveForm(password_form); 280 RemoveForm(password_form);
241 } 281 }
242 282
243 TEST_F(PasswordStoreProxyMacTest, TestRemoveLoginsCreatedBetween) { 283 TEST_P(PasswordStoreProxyMacTest, TestRemoveLoginsCreatedBetween) {
244 CheckRemoveLoginsBetween(true); 284 CheckRemoveLoginsBetween(true);
245 } 285 }
246 286
247 TEST_F(PasswordStoreProxyMacTest, TestRemoveLoginsSyncedBetween) { 287 TEST_P(PasswordStoreProxyMacTest, TestRemoveLoginsSyncedBetween) {
248 CheckRemoveLoginsBetween(false); 288 CheckRemoveLoginsBetween(false);
249 } 289 }
250 290
251 TEST_F(PasswordStoreProxyMacTest, FillLogins) { 291 TEST_P(PasswordStoreProxyMacTest, FillLogins) {
252 PasswordForm password_form; 292 PasswordForm password_form;
253 password_form.origin = GURL("http://example.com"); 293 password_form.origin = GURL("http://example.com");
254 password_form.signon_realm = "http://example.com/"; 294 password_form.signon_realm = "http://example.com/";
255 password_form.username_value = base::ASCIIToUTF16("test1@gmail.com"); 295 password_form.username_value = base::ASCIIToUTF16("test1@gmail.com");
256 password_form.password_value = base::ASCIIToUTF16("12345"); 296 password_form.password_value = base::ASCIIToUTF16("12345");
257 AddForm(password_form); 297 AddForm(password_form);
258 298
259 PasswordForm blacklisted_form; 299 PasswordForm blacklisted_form;
260 blacklisted_form.origin = GURL("http://example2.com"); 300 blacklisted_form.origin = GURL("http://example2.com");
261 blacklisted_form.signon_realm = "http://example2.com/"; 301 blacklisted_form.signon_realm = "http://example2.com/";
(...skipping 14 matching lines...) Expand all
276 .WillOnce(QuitUIMessageLoop()); 316 .WillOnce(QuitUIMessageLoop());
277 base::MessageLoop::current()->Run(); 317 base::MessageLoop::current()->Run();
278 318
279 store()->GetAutofillableLogins(&mock_consumer); 319 store()->GetAutofillableLogins(&mock_consumer);
280 EXPECT_CALL(mock_consumer, OnGetPasswordStoreResultsConstRef( 320 EXPECT_CALL(mock_consumer, OnGetPasswordStoreResultsConstRef(
281 ElementsAre(Pointee(password_form)))) 321 ElementsAre(Pointee(password_form))))
282 .WillOnce(QuitUIMessageLoop()); 322 .WillOnce(QuitUIMessageLoop());
283 base::MessageLoop::current()->Run(); 323 base::MessageLoop::current()->Run();
284 } 324 }
285 325
286 TEST_F(PasswordStoreProxyMacTest, OperationsOnABadDatabaseSilentlyFail) { 326 TEST_P(PasswordStoreProxyMacTest, OperationsOnABadDatabaseSilentlyFail) {
287 // Verify that operations on a PasswordStore with a bad database cause no 327 // Verify that operations on a PasswordStore with a bad database cause no
288 // explosions, but fail without side effect, return no data and trigger no 328 // explosions, but fail without side effect, return no data and trigger no
289 // notifications. 329 // notifications.
290 ClosePasswordStore(); 330 ClosePasswordStore();
291 CreateAndInitPasswordStore(make_scoped_ptr(new BadLoginDatabase)); 331 CreateAndInitPasswordStore(make_scoped_ptr(new BadLoginDatabase));
292 FinishAsyncProcessing(); 332 FinishAsyncProcessing();
293 EXPECT_FALSE(login_db()); 333 EXPECT_FALSE(login_db());
294 334
295 // The store should outlive the observer. 335 // The store should outlive the observer.
296 scoped_refptr<PasswordStoreProxyMac> store_refptr = store(); 336 scoped_refptr<PasswordStoreProxyMac> store_refptr = store();
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 381
342 // Delete one login; a range of logins. 382 // Delete one login; a range of logins.
343 store()->RemoveLogin(*form); 383 store()->RemoveLogin(*form);
344 store()->RemoveLoginsCreatedBetween(base::Time(), base::Time::Max()); 384 store()->RemoveLoginsCreatedBetween(base::Time(), base::Time::Max());
345 store()->RemoveLoginsSyncedBetween(base::Time(), base::Time::Max()); 385 store()->RemoveLoginsSyncedBetween(base::Time(), base::Time::Max());
346 FinishAsyncProcessing(); 386 FinishAsyncProcessing();
347 387
348 // Verify no notifications are fired during shutdown either. 388 // Verify no notifications are fired during shutdown either.
349 ClosePasswordStore(); 389 ClosePasswordStore();
350 } 390 }
391
392 INSTANTIATE_TEST_CASE_P(,
393 PasswordStoreProxyMacTest,
394 testing::Values(MigrationStatus::NOT_STARTED,
395 MigrationStatus::MIGRATED,
396 MigrationStatus::FAILED_ONCE,
397 MigrationStatus::FAILED_TWICE));
398
399 // Test the migration process.
400 class PasswordStoreProxyMacMigrationTest : public PasswordStoreProxyMacTest {
401 public:
402 void SetUp() override;
403
404 void TestMigration(bool lock_keychain);
405
406 protected:
407 scoped_ptr<password_manager::LoginDatabase> login_db_;
408 scoped_ptr<crypto::MockAppleKeychain> keychain_;
409 };
410
411 void PasswordStoreProxyMacMigrationTest::SetUp() {
412 login_db_.reset(
413 new password_manager::LoginDatabase(test_login_db_file_path()));
414 keychain_.reset(new crypto::MockAppleKeychain);
415 }
416
417 void PasswordStoreProxyMacMigrationTest::TestMigration(bool lock_keychain) {
418 PasswordForm form;
419 form.origin = GURL("http://accounts.google.com/LoginAuth");
420 form.signon_realm = "http://accounts.google.com/";
421 form.username_value = base::ASCIIToUTF16("my_username");
422 form.password_value = base::ASCIIToUTF16("12345");
423
424 if (GetParam() == MigrationStatus::NOT_STARTED ||
425 GetParam() == MigrationStatus::FAILED_ONCE)
vabr (Chromium) 2015/07/06 09:50:19 Shouldn't the login DB also be clearing passwords
vasilii 2015/07/06 13:57:58 Done.
426 login_db_->set_clear_password_values(true);
427 EXPECT_TRUE(login_db_->Init());
428 EXPECT_EQ(AddChangeForForm(form), login_db_->AddLogin(form));
429 // Prepare another database instance with the same content which is to be
430 // initialized by PasswordStoreProxyMac.
431 login_db_.reset(
432 new password_manager::LoginDatabase(test_login_db_file_path()));
433 MacKeychainPasswordFormAdapter adapter(keychain_.get());
434 EXPECT_TRUE(adapter.AddPassword(form));
435
436 // Init the store. It may trigger the migration.
437 if (lock_keychain)
438 keychain_->set_locked(true);
439 crypto::MockAppleKeychain* weak_keychain = keychain_.get();
440 store_ = new PasswordStoreProxyMac(
441 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
442 keychain_.Pass(), login_db_.Pass(), &testing_prefs_);
443 ASSERT_TRUE(store_->Init(syncer::SyncableService::StartSyncFlare()));
444 FinishAsyncProcessing();
445
446 // Check the password is still there.
447 if (lock_keychain)
448 weak_keychain->set_locked(false);
449 MockPasswordStoreConsumer mock_consumer;
450 store()->GetLogins(form, PasswordStoreProxyMac::ALLOW_PROMPT, &mock_consumer);
451 EXPECT_CALL(mock_consumer,
452 OnGetPasswordStoreResultsConstRef(ElementsAre(Pointee(form))))
453 .WillOnce(QuitUIMessageLoop());
454 base::MessageLoop::current()->Run();
455
456 int status = testing_prefs_.GetInteger(
457 password_manager::prefs::kKeychainMigrationStatus);
458 if (GetParam() == MigrationStatus::MIGRATED ||
459 GetParam() == MigrationStatus::FAILED_TWICE) {
460 EXPECT_EQ(static_cast<int>(GetParam()), status);
461 } else if (lock_keychain) {
462 EXPECT_EQ(static_cast<int>(GetParam() == MigrationStatus::NOT_STARTED
463 ? MigrationStatus::FAILED_ONCE
464 : MigrationStatus::FAILED_TWICE),
465 status);
466 } else {
467 EXPECT_EQ(static_cast<int>(MigrationStatus::MIGRATED), status);
468 }
469 }
470
471 TEST_P(PasswordStoreProxyMacMigrationTest, TestSuccessfullMigration) {
472 TestMigration(false);
473 }
474
475 TEST_P(PasswordStoreProxyMacMigrationTest, TestFailedMigration) {
476 TestMigration(true);
477 }
478
479 INSTANTIATE_TEST_CASE_P(,
480 PasswordStoreProxyMacMigrationTest,
481 testing::Values(MigrationStatus::NOT_STARTED,
482 MigrationStatus::MIGRATED,
483 MigrationStatus::FAILED_ONCE,
484 MigrationStatus::FAILED_TWICE));
485
351 } // namespace 486 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698