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 "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 Loading... |
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 Loading... |
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 Loading... |
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 341 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1653 ASSERT_EQ(1u, matching_items.size()); | 1658 ASSERT_EQ(1u, matching_items.size()); |
1654 EXPECT_EQ(ASCIIToUTF16("joe_user"), matching_items[0]->username_value); | 1659 EXPECT_EQ(ASCIIToUTF16("joe_user"), matching_items[0]->username_value); |
1655 | 1660 |
1656 // Check the third-party password is still there. | 1661 // Check the third-party password is still there. |
1657 owned_keychain_adapter.SetFindsOnlyOwnedItems(false); | 1662 owned_keychain_adapter.SetFindsOnlyOwnedItems(false); |
1658 matching_items = owned_keychain_adapter.PasswordsFillingForm( | 1663 matching_items = owned_keychain_adapter.PasswordsFillingForm( |
1659 "http://some.domain.com/insecure.html", PasswordForm::SCHEME_HTML); | 1664 "http://some.domain.com/insecure.html", PasswordForm::SCHEME_HTML); |
1660 ASSERT_EQ(1u, matching_items.size()); | 1665 ASSERT_EQ(1u, matching_items.size()); |
1661 } | 1666 } |
1662 | 1667 |
1663 // Open the store and immediately write to it and try to read it back, without | |
1664 // first waiting for the initialization to finish. If tasks are processed in | |
1665 // order, read/write operations will correctly be performed only after the | |
1666 // initialization has finished. | |
1667 TEST_F(PasswordStoreMacTest, StoreIsUsableImmediatelyAfterConstruction) { | |
1668 ClosePasswordStore(); | |
1669 | |
1670 base::WaitableEvent event(false, false); | |
1671 login_db_.reset( | |
1672 new SlowToInitLoginDatabase(test_login_db_file_path(), &event)); | |
1673 ASSERT_TRUE(thread_->task_runner()->PostTask( | |
1674 FROM_HERE, base::Bind(&PasswordStoreMacTest::InitLoginDatabase, | |
1675 base::Unretained(login_db_.get())))); | |
1676 CreateAndInitPasswordStore(login_db_.get()); | |
1677 | |
1678 PasswordFormData www_form_data = { | |
1679 PasswordForm::SCHEME_HTML, "http://www.facebook.com/", | |
1680 "http://www.facebook.com/index.html", "login", L"username", L"password", | |
1681 L"submit", L"not_joe_user", L"12345", true, false, 1}; | |
1682 scoped_ptr<PasswordForm> form = | |
1683 CreatePasswordFormFromDataForTesting(www_form_data); | |
1684 store()->AddLogin(*form); | |
1685 | |
1686 MockPasswordStoreConsumer mock_consumer; | |
1687 store()->GetLogins(*form, PasswordStore::ALLOW_PROMPT, &mock_consumer); | |
1688 | |
1689 // Now the read/write tasks are scheduled, let the DB initialization proceed. | |
1690 event.Signal(); | |
1691 | |
1692 EXPECT_CALL(mock_consumer, OnGetPasswordStoreResultsConstRef(SizeIs(1u))) | |
1693 .WillOnce(QuitUIMessageLoop()); | |
1694 base::MessageLoop::current()->Run(); | |
1695 EXPECT_TRUE(login_db()); | |
1696 } | |
1697 | |
1698 // Add a facebook form to the store but not to the keychain. The form is to be | 1668 // Add a facebook form to the store but not to the keychain. The form is to be |
1699 // implicitly deleted. However, the observers shouldn't get notified about | 1669 // implicitly deleted. However, the observers shouldn't get notified about |
1700 // deletion of non-existent forms like m.facebook.com. | 1670 // deletion of non-existent forms like m.facebook.com. |
1701 TEST_F(PasswordStoreMacTest, SilentlyRemoveOrphanedForm) { | 1671 TEST_F(PasswordStoreMacTest, SilentlyRemoveOrphanedForm) { |
1702 testing::StrictMock<MockPasswordStoreObserver> mock_observer; | 1672 testing::StrictMock<MockPasswordStoreObserver> mock_observer; |
1703 store()->AddObserver(&mock_observer); | 1673 store()->AddObserver(&mock_observer); |
1704 | 1674 |
1705 // 1. Add a password for www.facebook.com to the LoginDatabase. | 1675 // 1. Add a password for www.facebook.com to the LoginDatabase. |
1706 PasswordFormData www_form_data = { | 1676 PasswordFormData www_form_data = { |
1707 PasswordForm::SCHEME_HTML, "http://www.facebook.com/", | 1677 PasswordForm::SCHEME_HTML, "http://www.facebook.com/", |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1879 histogram_tester_->ExpectUniqueSample( | 1849 histogram_tester_->ExpectUniqueSample( |
1880 "PasswordManager.KeychainMigration.NumPasswordsOnFailure", 1, 1); | 1850 "PasswordManager.KeychainMigration.NumPasswordsOnFailure", 1, 1); |
1881 histogram_tester_->ExpectUniqueSample( | 1851 histogram_tester_->ExpectUniqueSample( |
1882 "PasswordManager.KeychainMigration.NumFailedPasswords", 1, 1); | 1852 "PasswordManager.KeychainMigration.NumFailedPasswords", 1, 1); |
1883 histogram_tester_->ExpectUniqueSample( | 1853 histogram_tester_->ExpectUniqueSample( |
1884 "PasswordManager.KeychainMigration.NumChromeOwnedInaccessiblePasswords", | 1854 "PasswordManager.KeychainMigration.NumChromeOwnedInaccessiblePasswords", |
1885 2, 1); | 1855 2, 1); |
1886 // Don't test the encryption key access. | 1856 // Don't test the encryption key access. |
1887 histogram_tester_.reset(); | 1857 histogram_tester_.reset(); |
1888 } | 1858 } |
OLD | NEW |