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

Side by Side Diff: chrome/browser/password_manager/password_store_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: added a comment 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 (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 "chrome/browser/password_manager/password_store_mac.h" 5 #include "chrome/browser/password_manager/password_store_mac.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/files/scoped_temp_dir.h" 8 #include "base/files/scoped_temp_dir.h"
9 #include "base/scoped_observer.h" 9 #include "base/scoped_observer.h"
10 #include "base/stl_util.h" 10 #include "base/stl_util.h"
11 #include "base/strings/string_util.h" 11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
13 #include "base/synchronization/waitable_event.h" 13 #include "base/synchronization/waitable_event.h"
14 #include "base/test/histogram_tester.h" 14 #include "base/test/histogram_tester.h"
15 #include "base/thread_task_runner_handle.h" 15 #include "base/thread_task_runner_handle.h"
16 #include "chrome/browser/password_manager/password_store_mac_internal.h" 16 #include "chrome/browser/password_manager/password_store_mac_internal.h"
17 #include "chrome/common/chrome_paths.h" 17 #include "chrome/common/chrome_paths.h"
18 #include "components/os_crypt/os_crypt.h" 18 #include "components/os_crypt/os_crypt.h"
19 #include "components/password_manager/core/browser/login_database.h" 19 #include "components/password_manager/core/browser/login_database.h"
20 #include "components/password_manager/core/browser/password_manager_test_utils.h " 20 #include "components/password_manager/core/browser/password_manager_test_utils.h "
21 #include "components/password_manager/core/browser/password_store_consumer.h" 21 #include "components/password_manager/core/browser/password_store_consumer.h"
22 #include "content/public/test/test_browser_thread.h" 22 #include "content/public/test/test_browser_thread.h"
23 #include "content/public/test/test_utils.h"
23 #include "crypto/mock_apple_keychain.h" 24 #include "crypto/mock_apple_keychain.h"
24 #include "testing/gmock/include/gmock/gmock.h" 25 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
26 27
27 using autofill::PasswordForm; 28 using autofill::PasswordForm;
28 using base::ASCIIToUTF16; 29 using base::ASCIIToUTF16;
29 using base::WideToUTF16; 30 using base::WideToUTF16;
30 using content::BrowserThread; 31 using content::BrowserThread;
31 using crypto::MockAppleKeychain; 32 using crypto::MockAppleKeychain;
32 using internal_keychain_helpers::FormsMatchForMerge; 33 using internal_keychain_helpers::FormsMatchForMerge;
(...skipping 19 matching lines...) Expand all
52 base::MessageLoop::current()->Quit(); 53 base::MessageLoop::current()->Quit();
53 } 54 }
54 55
55 // From the mock's argument #0 of type const std::vector<PasswordForm*>& takes 56 // From the mock's argument #0 of type const std::vector<PasswordForm*>& takes
56 // the first form and copies it to the form pointed to by |target_form_ptr|. 57 // the first form and copies it to the form pointed to by |target_form_ptr|.
57 ACTION_P(SaveACopyOfFirstForm, target_form_ptr) { 58 ACTION_P(SaveACopyOfFirstForm, target_form_ptr) {
58 ASSERT_FALSE(arg0.empty()); 59 ASSERT_FALSE(arg0.empty());
59 *target_form_ptr = *arg0[0]; 60 *target_form_ptr = *arg0[0];
60 } 61 }
61 62
63 void Noop() {
64 }
65
62 class MockPasswordStoreConsumer : public PasswordStoreConsumer { 66 class MockPasswordStoreConsumer : public PasswordStoreConsumer {
63 public: 67 public:
64 MOCK_METHOD1(OnGetPasswordStoreResultsConstRef, 68 MOCK_METHOD1(OnGetPasswordStoreResultsConstRef,
65 void(const std::vector<PasswordForm*>&)); 69 void(const std::vector<PasswordForm*>&));
66 70
67 // GMock cannot mock methods with move-only args. 71 // GMock cannot mock methods with move-only args.
68 void OnGetPasswordStoreResults(ScopedVector<PasswordForm> results) override { 72 void OnGetPasswordStoreResults(ScopedVector<PasswordForm> results) override {
69 OnGetPasswordStoreResultsConstRef(results.get()); 73 OnGetPasswordStoreResultsConstRef(results.get());
70 } 74 }
71 }; 75 };
(...skipping 1136 matching lines...) Expand 10 before | Expand all | Expand 10 after
1208 } 1212 }
1209 1213
1210 static void InitLoginDatabase(password_manager::LoginDatabase* login_db) { 1214 static void InitLoginDatabase(password_manager::LoginDatabase* login_db) {
1211 ASSERT_TRUE(login_db->Init()); 1215 ASSERT_TRUE(login_db->Init());
1212 } 1216 }
1213 1217
1214 void CreateAndInitPasswordStore(password_manager::LoginDatabase* login_db) { 1218 void CreateAndInitPasswordStore(password_manager::LoginDatabase* login_db) {
1215 store_ = new PasswordStoreMac( 1219 store_ = new PasswordStoreMac(
1216 base::ThreadTaskRunnerHandle::Get(), nullptr, 1220 base::ThreadTaskRunnerHandle::Get(), nullptr,
1217 make_scoped_ptr<AppleKeychain>(new MockAppleKeychain)); 1221 make_scoped_ptr<AppleKeychain>(new MockAppleKeychain));
1218 store_->InitWithTaskRunner(thread_->task_runner()); 1222 ASSERT_TRUE(thread_->task_runner()->PostTask(
1219 ASSERT_TRUE(store_->ScheduleTask( 1223 FROM_HERE, base::Bind(&PasswordStoreMac::InitWithTaskRunner, store_,
1220 base::Bind(&PasswordStoreMac::set_login_metadata_db, store_, 1224 thread_->task_runner())));
1221 base::Unretained(login_db)))); 1225
1226 ASSERT_TRUE(thread_->task_runner()->PostTask(
1227 FROM_HERE, base::Bind(&PasswordStoreMac::set_login_metadata_db, store_,
1228 base::Unretained(login_db))));
1222 } 1229 }
1223 1230
1224 void ClosePasswordStore() { 1231 void ClosePasswordStore() {
1225 if (!store_) 1232 if (!store_)
1226 return; 1233 return;
1227 1234
1228 store_->Shutdown(); 1235 store_->Shutdown();
1229 store_ = nullptr; 1236 store_ = nullptr;
1230 } 1237 }
1231 1238
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
1285 1292
1286 password_manager::LoginDatabase* login_db() const { 1293 password_manager::LoginDatabase* login_db() const {
1287 return store_->login_metadata_db(); 1294 return store_->login_metadata_db();
1288 } 1295 }
1289 1296
1290 MockAppleKeychain* keychain() { 1297 MockAppleKeychain* keychain() {
1291 return static_cast<MockAppleKeychain*>(store_->keychain()); 1298 return static_cast<MockAppleKeychain*>(store_->keychain());
1292 } 1299 }
1293 1300
1294 void FinishAsyncProcessing() { 1301 void FinishAsyncProcessing() {
1295 // Do a store-level query to wait for all the previously enqueued operations 1302 scoped_refptr<content::MessageLoopRunner> runner =
1296 // to finish. 1303 new content::MessageLoopRunner;
1297 MockPasswordStoreConsumer consumer; 1304 ASSERT_TRUE(thread_->task_runner()->PostTaskAndReply(
1298 store_->GetLogins(PasswordForm(), PasswordStore::ALLOW_PROMPT, &consumer); 1305 FROM_HERE, base::Bind(&Noop), runner->QuitClosure()));
1299 EXPECT_CALL(consumer, OnGetPasswordStoreResultsConstRef(_)) 1306 runner->Run();
1300 .WillOnce(QuitUIMessageLoop());
1301 base::MessageLoop::current()->Run();
1302 } 1307 }
1303 1308
1304 PasswordStoreMac* store() { return store_.get(); } 1309 PasswordStoreMac* store() { return store_.get(); }
1305 1310
1306 protected: 1311 protected:
1307 base::MessageLoopForUI message_loop_; 1312 base::MessageLoopForUI message_loop_;
1308 content::TestBrowserThread ui_thread_; 1313 content::TestBrowserThread ui_thread_;
1309 // Thread that the synchronous methods are run on. 1314 // Thread that the synchronous methods are run on.
1310 scoped_ptr<base::Thread> thread_; 1315 scoped_ptr<base::Thread> thread_;
1311 1316
(...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after
1661 ASSERT_EQ(1u, matching_items.size()); 1666 ASSERT_EQ(1u, matching_items.size());
1662 EXPECT_EQ(ASCIIToUTF16("joe_user"), matching_items[0]->username_value); 1667 EXPECT_EQ(ASCIIToUTF16("joe_user"), matching_items[0]->username_value);
1663 1668
1664 // Check the third-party password is still there. 1669 // Check the third-party password is still there.
1665 owned_keychain_adapter.SetFindsOnlyOwnedItems(false); 1670 owned_keychain_adapter.SetFindsOnlyOwnedItems(false);
1666 matching_items = owned_keychain_adapter.PasswordsFillingForm( 1671 matching_items = owned_keychain_adapter.PasswordsFillingForm(
1667 "http://some.domain.com/insecure.html", PasswordForm::SCHEME_HTML); 1672 "http://some.domain.com/insecure.html", PasswordForm::SCHEME_HTML);
1668 ASSERT_EQ(1u, matching_items.size()); 1673 ASSERT_EQ(1u, matching_items.size());
1669 } 1674 }
1670 1675
1671 // Open the store and immediately write to it and try to read it back, without
1672 // first waiting for the initialization to finish. If tasks are processed in
1673 // order, read/write operations will correctly be performed only after the
1674 // initialization has finished.
1675 TEST_F(PasswordStoreMacTest, StoreIsUsableImmediatelyAfterConstruction) {
1676 ClosePasswordStore();
1677
1678 base::WaitableEvent event(false, false);
1679 login_db_.reset(
1680 new SlowToInitLoginDatabase(test_login_db_file_path(), &event));
1681 ASSERT_TRUE(thread_->task_runner()->PostTask(
1682 FROM_HERE, base::Bind(&PasswordStoreMacTest::InitLoginDatabase,
1683 base::Unretained(login_db_.get()))));
1684 CreateAndInitPasswordStore(login_db_.get());
1685
1686 PasswordFormData www_form_data = {
1687 PasswordForm::SCHEME_HTML, "http://www.facebook.com/",
1688 "http://www.facebook.com/index.html", "login", L"username", L"password",
1689 L"submit", L"not_joe_user", L"12345", true, false, 1};
1690 scoped_ptr<PasswordForm> form =
1691 CreatePasswordFormFromDataForTesting(www_form_data);
1692 store()->AddLogin(*form);
1693
1694 MockPasswordStoreConsumer mock_consumer;
1695 store()->GetLogins(*form, PasswordStore::ALLOW_PROMPT, &mock_consumer);
1696
1697 // Now the read/write tasks are scheduled, let the DB initialization proceed.
1698 event.Signal();
1699
1700 EXPECT_CALL(mock_consumer, OnGetPasswordStoreResultsConstRef(SizeIs(1u)))
1701 .WillOnce(QuitUIMessageLoop());
1702 base::MessageLoop::current()->Run();
1703 EXPECT_TRUE(login_db());
1704 }
1705
1706 // Add a facebook form to the store but not to the keychain. The form is to be 1676 // Add a facebook form to the store but not to the keychain. The form is to be
1707 // implicitly deleted. However, the observers shouldn't get notified about 1677 // implicitly deleted. However, the observers shouldn't get notified about
1708 // deletion of non-existent forms like m.facebook.com. 1678 // deletion of non-existent forms like m.facebook.com.
1709 TEST_F(PasswordStoreMacTest, SilentlyRemoveOrphanedForm) { 1679 TEST_F(PasswordStoreMacTest, SilentlyRemoveOrphanedForm) {
1710 testing::StrictMock<MockPasswordStoreObserver> mock_observer; 1680 testing::StrictMock<MockPasswordStoreObserver> mock_observer;
1711 store()->AddObserver(&mock_observer); 1681 store()->AddObserver(&mock_observer);
1712 1682
1713 // 1. Add a password for www.facebook.com to the LoginDatabase. 1683 // 1. Add a password for www.facebook.com to the LoginDatabase.
1714 PasswordFormData www_form_data = { 1684 PasswordFormData www_form_data = {
1715 PasswordForm::SCHEME_HTML, "http://www.facebook.com/", 1685 PasswordForm::SCHEME_HTML, "http://www.facebook.com/",
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
1887 histogram_tester_->ExpectUniqueSample( 1857 histogram_tester_->ExpectUniqueSample(
1888 "PasswordManager.KeychainMigration.NumPasswordsOnFailure", 1, 1); 1858 "PasswordManager.KeychainMigration.NumPasswordsOnFailure", 1, 1);
1889 histogram_tester_->ExpectUniqueSample( 1859 histogram_tester_->ExpectUniqueSample(
1890 "PasswordManager.KeychainMigration.NumFailedPasswords", 1, 1); 1860 "PasswordManager.KeychainMigration.NumFailedPasswords", 1, 1);
1891 histogram_tester_->ExpectUniqueSample( 1861 histogram_tester_->ExpectUniqueSample(
1892 "PasswordManager.KeychainMigration.NumChromeOwnedInaccessiblePasswords", 1862 "PasswordManager.KeychainMigration.NumChromeOwnedInaccessiblePasswords",
1893 2, 1); 1863 2, 1);
1894 // Don't test the encryption key access. 1864 // Don't test the encryption key access.
1895 histogram_tester_.reset(); 1865 histogram_tester_.reset();
1896 } 1866 }
OLDNEW
« no previous file with comments | « chrome/browser/password_manager/password_store_mac.cc ('k') | chrome/browser/password_manager/password_store_proxy_mac.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698