Index: chrome/browser/password_manager/password_store_mac_unittest.cc |
diff --git a/chrome/browser/password_manager/password_store_mac_unittest.cc b/chrome/browser/password_manager/password_store_mac_unittest.cc |
index a3e8810e9e14243f48c1180ef72c323d3d453e76..ed4e03af2ac118383c084fcdf3f4796ed401185b 100644 |
--- a/chrome/browser/password_manager/password_store_mac_unittest.cc |
+++ b/chrome/browser/password_manager/password_store_mac_unittest.cc |
@@ -13,6 +13,7 @@ |
#include "base/strings/utf_string_conversions.h" |
#include "chrome/browser/password_manager/password_store_mac_internal.h" |
#include "chrome/common/chrome_paths.h" |
+#include "components/password_manager/core/browser/login_database.h" |
#include "components/password_manager/core/browser/password_store_consumer.h" |
#include "content/public/test/test_browser_thread.h" |
#include "crypto/mock_apple_keychain.h" |
@@ -60,17 +61,23 @@ ACTION(QuitUIMessageLoop) { |
base::MessageLoop::current()->Quit(); |
} |
+class MockPasswordStoreObserver : public PasswordStore::Observer { |
+ public: |
+ MOCK_METHOD1(OnLoginsChanged, |
+ void(const password_manager::PasswordStoreChangeList& changes)); |
+}; |
+ |
class TestPasswordStoreMac : public PasswordStoreMac { |
public: |
TestPasswordStoreMac( |
scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner, |
scoped_refptr<base::SingleThreadTaskRunner> db_thread_runner, |
crypto::AppleKeychain* keychain, |
- LoginDatabase* login_db) |
+ const base::FilePath& login_db_file_path) |
: PasswordStoreMac(main_thread_runner, |
db_thread_runner, |
keychain, |
- login_db) { |
+ login_db_file_path) { |
} |
using PasswordStoreMac::GetBackgroundTaskRunner; |
@@ -1049,13 +1056,11 @@ TEST_F(PasswordStoreMacInternalsTest, TestPasswordGetAll) { |
class PasswordStoreMacTest : public testing::Test { |
public: |
- PasswordStoreMacTest() : ui_thread_(BrowserThread::UI, &message_loop_) {} |
+ PasswordStoreMacTest() |
+ : ui_thread_(BrowserThread::UI, &message_loop_), keychain_(nullptr) {} |
void SetUp() override { |
- login_db_ = new LoginDatabase(); |
ASSERT_TRUE(db_dir_.CreateUniqueTempDir()); |
- base::FilePath db_file = db_dir_.path().AppendASCII("login.db"); |
- ASSERT_TRUE(login_db_->Init(db_file)); |
keychain_ = new MockAppleKeychain(); |
@@ -1063,7 +1068,7 @@ class PasswordStoreMacTest : public testing::Test { |
base::MessageLoopProxy::current(), |
base::MessageLoopProxy::current(), |
keychain_, |
- login_db_); |
+ test_login_db_file_path()); |
ASSERT_TRUE(store_->Init(syncer::SyncableService::StartSyncFlare())); |
} |
@@ -1072,6 +1077,14 @@ class PasswordStoreMacTest : public testing::Test { |
EXPECT_FALSE(store_->GetBackgroundTaskRunner().get()); |
} |
+ base::FilePath test_login_db_file_path() const { |
+ return db_dir_.path().Append(FILE_PATH_LITERAL("login.db")); |
+ } |
+ |
+ password_manager::LoginDatabase* login_db() const { |
+ return store_->login_metadata_db(); |
+ } |
+ |
void WaitForStoreUpdate() { |
// Do a store-level query to wait for all the operations above to be done. |
MockPasswordStoreConsumer consumer; |
@@ -1090,7 +1103,6 @@ class PasswordStoreMacTest : public testing::Test { |
content::TestBrowserThread ui_thread_; |
MockAppleKeychain* keychain_; // Owned by store_. |
- LoginDatabase* login_db_; // Owned by store_. |
scoped_refptr<TestPasswordStoreMac> store_; |
base::ScopedTempDir db_dir_; |
}; |
@@ -1107,7 +1119,7 @@ TEST_F(PasswordStoreMacTest, TestStoreUpdate) { |
L"username", L"password", L"submit", L"joe_user", L"sekrit", true, false, 1 |
}; |
scoped_ptr<PasswordForm> joint_form(CreatePasswordFormFromData(joint_data)); |
- login_db_->AddLogin(*joint_form); |
+ login_db()->AddLogin(*joint_form); |
MockAppleKeychain::KeychainTestData joint_keychain_data = { |
kSecAuthenticationTypeHTMLForm, "some.domain.com", |
kSecProtocolTypeHTTP, "/insecure.html", 0, NULL, "20020601171500Z", |
@@ -1180,7 +1192,7 @@ TEST_F(PasswordStoreMacTest, TestStoreUpdate) { |
} |
STLDeleteElements(&matching_items); |
- login_db_->GetLogins(*query_form, &matching_items); |
+ login_db()->GetLogins(*query_form, &matching_items); |
EXPECT_EQ(updates[i].password ? 1U : 0U, matching_items.size()) |
<< "iteration " << i; |
STLDeleteElements(&matching_items); |
@@ -1210,7 +1222,7 @@ TEST_F(PasswordStoreMacTest, TestDBKeychainAssociation) { |
L"username", L"password", L"submit", L"joe_user", L"sekrit", true, false, 1 |
}; |
scoped_ptr<PasswordForm> www_form(CreatePasswordFormFromData(www_form_data)); |
- login_db_->AddLogin(*www_form); |
+ login_db()->AddLogin(*www_form); |
MacKeychainPasswordFormAdapter owned_keychain_adapter(keychain_); |
owned_keychain_adapter.SetFindsOnlyOwnedItems(true); |
owned_keychain_adapter.AddPassword(*www_form); |
@@ -1229,7 +1241,7 @@ TEST_F(PasswordStoreMacTest, TestDBKeychainAssociation) { |
EXPECT_EQ(1u, consumer.last_result.size()); |
// 3. Add the returned password for m.facebook.com. |
- login_db_->AddLogin(consumer.last_result[0]); |
+ login_db()->AddLogin(consumer.last_result[0]); |
owned_keychain_adapter.AddPassword(m_form); |
// 4. Remove both passwords. |
@@ -1242,13 +1254,13 @@ TEST_F(PasswordStoreMacTest, TestDBKeychainAssociation) { |
matching_items = owned_keychain_adapter.PasswordsFillingForm( |
www_form->signon_realm, www_form->scheme); |
EXPECT_EQ(0u, matching_items.size()); |
- login_db_->GetLogins(*www_form, &matching_items); |
+ login_db()->GetLogins(*www_form, &matching_items); |
EXPECT_EQ(0u, matching_items.size()); |
// No trace of m.facebook.com. |
matching_items = owned_keychain_adapter.PasswordsFillingForm( |
m_form.signon_realm, m_form.scheme); |
EXPECT_EQ(0u, matching_items.size()); |
- login_db_->GetLogins(m_form, &matching_items); |
+ login_db()->GetLogins(m_form, &matching_items); |
EXPECT_EQ(0u, matching_items.size()); |
} |
@@ -1418,7 +1430,7 @@ TEST_F(PasswordStoreMacTest, TestRemoveLoginsMultiProfile) { |
WaitForStoreUpdate(); |
ScopedVector<PasswordForm> matching_items; |
- login_db_->GetLogins(*www_form, &matching_items.get()); |
+ login_db()->GetLogins(*www_form, &matching_items.get()); |
EXPECT_EQ(1u, matching_items.size()); |
matching_items.clear(); |
@@ -1426,7 +1438,7 @@ TEST_F(PasswordStoreMacTest, TestRemoveLoginsMultiProfile) { |
WaitForStoreUpdate(); |
// Check the second facebook form is gone. |
- login_db_->GetLogins(*www_form, &matching_items.get()); |
+ login_db()->GetLogins(*www_form, &matching_items.get()); |
EXPECT_EQ(0u, matching_items.size()); |
// Check the first facebook form is still there. |
@@ -1442,3 +1454,73 @@ TEST_F(PasswordStoreMacTest, TestRemoveLoginsMultiProfile) { |
"http://some.domain.com/insecure.html", PasswordForm::SCHEME_HTML); |
ASSERT_EQ(1u, matching_items.size()); |
} |
+ |
+// Verify that operations on a PasswordStore with a bad database cause no |
+// explosions, but fail without side effect, return no data and trigger no |
+// notifications. |
+TEST_F(PasswordStoreMacTest, OperationsOnABadDatabaseSilentlyFail) { |
+ store_ = new PasswordStoreMac(base::MessageLoopProxy::current(), |
+ base::MessageLoopProxy::current(), keychain_, |
+ test_login_db_file_path()); |
+ |
+ password_manager::LoginDatabase::set_simulate_init_failure_for_testing(true); |
+ store_->Init(syncer::SyncableService::StartSyncFlare()); |
+ base::MessageLoop::current()->RunUntilIdle(); |
+ password_manager::LoginDatabase::set_simulate_init_failure_for_testing(false); |
+ ASSERT_EQ(nullptr, store_->login_metadata_db()); |
+ |
+ testing::StrictMock<MockPasswordStoreObserver> mock_observer; |
+ store_->AddObserver(&mock_observer); |
+ |
+ // Add a new autofillable login + a blacklisted login. |
+ PasswordFormData www_form_data = { |
+ PasswordForm::SCHEME_HTML, "http://www.facebook.com/", |
+ "http://www.facebook.com/index.html", "login", L"username", L"password", |
+ L"submit", L"not_joe_user", L"12345", true, false, 1 }; |
+ scoped_ptr<PasswordForm> form( |
+ CreatePasswordFormFromData(www_form_data)); |
+ scoped_ptr<PasswordForm> blacklisted_form(new PasswordForm(*form)); |
+ blacklisted_form->signon_realm = "http://foo.example.com"; |
+ blacklisted_form->origin = GURL("http://foo.example.com/origin"); |
+ blacklisted_form->action = GURL("http://foo.example.com/action"); |
+ blacklisted_form->blacklisted_by_user = true; |
+ store_->AddLogin(*form); |
+ store_->AddLogin(*blacklisted_form); |
+ base::MessageLoop::current()->RunUntilIdle(); |
+ |
+ // Get all logins; autofillable logins; blacklisted logins. |
+ testing::StrictMock<MockPasswordStoreConsumer> mock_consumer; |
+ EXPECT_CALL(mock_consumer, OnGetPasswordStoreResults(testing::ElementsAre())); |
+ store_->GetLogins(*form, PasswordStore::DISALLOW_PROMPT, &mock_consumer); |
+ base::MessageLoop::current()->RunUntilIdle(); |
+ testing::Mock::VerifyAndClearExpectations(&mock_consumer); |
+ EXPECT_CALL(mock_consumer, OnGetPasswordStoreResults(testing::ElementsAre())); |
+ store_->GetAutofillableLogins(&mock_consumer); |
+ base::MessageLoop::current()->RunUntilIdle(); |
+ testing::Mock::VerifyAndClearExpectations(&mock_consumer); |
+ EXPECT_CALL(mock_consumer, OnGetPasswordStoreResults(testing::ElementsAre())); |
+ store_->GetBlacklistLogins(&mock_consumer); |
+ base::MessageLoop::current()->RunUntilIdle(); |
+ |
+ // Report metrics. |
+ store_->ReportMetrics("Test Username", true); |
+ base::MessageLoop::current()->RunUntilIdle(); |
+ |
+ // Change the login. |
+ form->password_value = base::ASCIIToUTF16("a different password"); |
+ store_->UpdateLogin(*form); |
+ base::MessageLoop::current()->RunUntilIdle(); |
+ |
+ // Delete one login; a range of logins. |
+ store_->RemoveLogin(*form); |
+ base::MessageLoop::current()->RunUntilIdle(); |
+ store_->RemoveLoginsCreatedBetween(base::Time(), base::Time::Max()); |
+ base::MessageLoop::current()->RunUntilIdle(); |
+ store_->RemoveLoginsSyncedBetween(base::Time(), base::Time::Max()); |
+ base::MessageLoop::current()->RunUntilIdle(); |
+ |
+ // Ensure no notifications and no explosions during shutdown either. |
+ store_->RemoveObserver(&mock_observer); |
+ store_->Shutdown(); |
+ base::MessageLoop::current()->RunUntilIdle(); |
+} |