OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/password_manager/core/browser/login_database.h" | 5 #include "components/password_manager/core/browser/login_database.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
| 11 #include <utility> |
11 | 12 |
12 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
13 #include "base/files/scoped_temp_dir.h" | 14 #include "base/files/scoped_temp_dir.h" |
14 #include "base/memory/scoped_vector.h" | 15 #include "base/memory/scoped_vector.h" |
15 #include "base/path_service.h" | 16 #include "base/path_service.h" |
16 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
17 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
18 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
19 #include "base/test/histogram_tester.h" | 20 #include "base/test/histogram_tester.h" |
20 #include "base/time/time.h" | 21 #include "base/time/time.h" |
21 #include "build/build_config.h" | 22 #include "build/build_config.h" |
22 #include "components/autofill/core/common/password_form.h" | 23 #include "components/autofill/core/common/password_form.h" |
| 24 #include "components/os_crypt/os_crypt_mocker.h" |
23 #include "components/password_manager/core/browser/psl_matching_helper.h" | 25 #include "components/password_manager/core/browser/psl_matching_helper.h" |
24 #include "sql/connection.h" | 26 #include "sql/connection.h" |
25 #include "sql/statement.h" | 27 #include "sql/statement.h" |
26 #include "sql/test/test_helpers.h" | 28 #include "sql/test/test_helpers.h" |
27 #include "testing/gmock/include/gmock/gmock.h" | 29 #include "testing/gmock/include/gmock/gmock.h" |
28 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
29 #include "url/origin.h" | 31 #include "url/origin.h" |
30 | 32 |
31 #if defined(OS_MACOSX) | |
32 #include "components/os_crypt/os_crypt.h" | |
33 #endif | |
34 | |
35 using autofill::PasswordForm; | 33 using autofill::PasswordForm; |
36 using base::ASCIIToUTF16; | 34 using base::ASCIIToUTF16; |
37 using ::testing::Eq; | 35 using ::testing::Eq; |
38 | 36 |
39 namespace password_manager { | 37 namespace password_manager { |
40 namespace { | 38 namespace { |
41 PasswordStoreChangeList AddChangeForForm(const PasswordForm& form) { | 39 PasswordStoreChangeList AddChangeForForm(const PasswordForm& form) { |
42 return PasswordStoreChangeList( | 40 return PasswordStoreChangeList( |
43 1, PasswordStoreChange(PasswordStoreChange::ADD, form)); | 41 1, PasswordStoreChange(PasswordStoreChange::ADD, form)); |
44 } | 42 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 static const bool is_specialized = false; | 74 static const bool is_specialized = false; |
77 }; | 75 }; |
78 | 76 |
79 template<class T> T GetFirstColumn(const sql::Statement& s) { | 77 template<class T> T GetFirstColumn(const sql::Statement& s) { |
80 static_assert(must_be_specialized<T>::is_specialized, | 78 static_assert(must_be_specialized<T>::is_specialized, |
81 "Implement a specialization."); | 79 "Implement a specialization."); |
82 } | 80 } |
83 | 81 |
84 template<> int64_t GetFirstColumn(const sql::Statement& s) { | 82 template<> int64_t GetFirstColumn(const sql::Statement& s) { |
85 return s.ColumnInt64(0); | 83 return s.ColumnInt64(0); |
86 }; | 84 } |
87 | 85 |
88 template<> std::string GetFirstColumn(const sql::Statement& s) { | 86 template<> std::string GetFirstColumn(const sql::Statement& s) { |
89 return s.ColumnString(0); | 87 return s.ColumnString(0); |
90 }; | 88 } |
91 | 89 |
92 bool AddZeroClickableLogin(LoginDatabase* db, | 90 bool AddZeroClickableLogin(LoginDatabase* db, |
93 const std::string& unique_string) { | 91 const std::string& unique_string) { |
94 // Example password form. | 92 // Example password form. |
95 PasswordForm form; | 93 PasswordForm form; |
96 form.origin = GURL("https://example.com/"); | 94 form.origin = GURL("https://example.com/"); |
97 form.username_element = ASCIIToUTF16(unique_string); | 95 form.username_element = ASCIIToUTF16(unique_string); |
98 form.username_value = ASCIIToUTF16(unique_string); | 96 form.username_value = ASCIIToUTF16(unique_string); |
99 form.password_element = ASCIIToUTF16(unique_string); | 97 form.password_element = ASCIIToUTF16(unique_string); |
100 form.submit_element = ASCIIToUTF16("signIn"); | 98 form.submit_element = ASCIIToUTF16("signIn"); |
(...skipping 12 matching lines...) Expand all Loading... |
113 | 111 |
114 // Serialization routines for vectors implemented in login_database.cc. | 112 // Serialization routines for vectors implemented in login_database.cc. |
115 base::Pickle SerializeVector(const std::vector<base::string16>& vec); | 113 base::Pickle SerializeVector(const std::vector<base::string16>& vec); |
116 std::vector<base::string16> DeserializeVector(const base::Pickle& pickle); | 114 std::vector<base::string16> DeserializeVector(const base::Pickle& pickle); |
117 | 115 |
118 class LoginDatabaseTest : public testing::Test { | 116 class LoginDatabaseTest : public testing::Test { |
119 protected: | 117 protected: |
120 void SetUp() override { | 118 void SetUp() override { |
121 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 119 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
122 file_ = temp_dir_.path().AppendASCII("TestMetadataStoreMacDatabase"); | 120 file_ = temp_dir_.path().AppendASCII("TestMetadataStoreMacDatabase"); |
123 #if defined(OS_MACOSX) | 121 OSCryptMocker::SetUpWithSingleton(); |
124 OSCrypt::UseMockKeychain(true); | |
125 #endif // defined(OS_MACOSX) | |
126 | 122 |
127 db_.reset(new LoginDatabase(file_)); | 123 db_.reset(new LoginDatabase(file_)); |
128 ASSERT_TRUE(db_->Init()); | 124 ASSERT_TRUE(db_->Init()); |
129 } | 125 } |
130 | 126 |
| 127 void TearDown() override { OSCryptMocker::TearDown(); } |
| 128 |
131 LoginDatabase& db() { return *db_; } | 129 LoginDatabase& db() { return *db_; } |
132 | 130 |
133 void TestNonHTMLFormPSLMatching(const PasswordForm::Scheme& scheme) { | 131 void TestNonHTMLFormPSLMatching(const PasswordForm::Scheme& scheme) { |
134 ScopedVector<autofill::PasswordForm> result; | 132 ScopedVector<autofill::PasswordForm> result; |
135 | 133 |
136 base::Time now = base::Time::Now(); | 134 base::Time now = base::Time::Now(); |
137 | 135 |
138 // Simple non-html auth form. | 136 // Simple non-html auth form. |
139 PasswordForm non_html_auth; | 137 PasswordForm non_html_auth; |
140 non_html_auth.origin = GURL("http://example.com"); | 138 non_html_auth.origin = GURL("http://example.com"); |
(...skipping 1355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1496 // Test the migration from GetParam() version to kCurrentVersionNumber. | 1494 // Test the migration from GetParam() version to kCurrentVersionNumber. |
1497 class LoginDatabaseMigrationTest : public testing::TestWithParam<int> { | 1495 class LoginDatabaseMigrationTest : public testing::TestWithParam<int> { |
1498 protected: | 1496 protected: |
1499 void SetUp() override { | 1497 void SetUp() override { |
1500 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 1498 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
1501 database_dump_location_ = database_dump_location_.AppendASCII("components") | 1499 database_dump_location_ = database_dump_location_.AppendASCII("components") |
1502 .AppendASCII("test") | 1500 .AppendASCII("test") |
1503 .AppendASCII("data") | 1501 .AppendASCII("data") |
1504 .AppendASCII("password_manager"); | 1502 .AppendASCII("password_manager"); |
1505 database_path_ = temp_dir_.path().AppendASCII("test.db"); | 1503 database_path_ = temp_dir_.path().AppendASCII("test.db"); |
1506 #if defined(OS_MACOSX) | 1504 OSCryptMocker::SetUpWithSingleton(); |
1507 OSCrypt::UseMockKeychain(true); | |
1508 #endif // defined(OS_MACOSX) | |
1509 } | 1505 } |
1510 | 1506 |
| 1507 void TearDown() override { OSCryptMocker::TearDown(); } |
| 1508 |
1511 // Creates the databse from |sql_file|. | 1509 // Creates the databse from |sql_file|. |
1512 void CreateDatabase(base::StringPiece sql_file) { | 1510 void CreateDatabase(base::StringPiece sql_file) { |
1513 base::FilePath database_dump; | 1511 base::FilePath database_dump; |
1514 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &database_dump)); | 1512 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &database_dump)); |
1515 database_dump = | 1513 database_dump = |
1516 database_dump.Append(database_dump_location_).AppendASCII(sql_file); | 1514 database_dump.Append(database_dump_location_).AppendASCII(sql_file); |
1517 ASSERT_TRUE( | 1515 ASSERT_TRUE( |
1518 sql::test::CreateDatabaseFromSQL(database_path_, database_dump)); | 1516 sql::test::CreateDatabaseFromSQL(database_path_, database_dump)); |
1519 } | 1517 } |
1520 | 1518 |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1678 LoginDatabaseMigrationTest, | 1676 LoginDatabaseMigrationTest, |
1679 testing::Range(1, kCurrentVersionNumber + 1)); | 1677 testing::Range(1, kCurrentVersionNumber + 1)); |
1680 INSTANTIATE_TEST_CASE_P(MigrationToVCurrent, | 1678 INSTANTIATE_TEST_CASE_P(MigrationToVCurrent, |
1681 LoginDatabaseMigrationTestV9, | 1679 LoginDatabaseMigrationTestV9, |
1682 testing::Values(9)); | 1680 testing::Values(9)); |
1683 INSTANTIATE_TEST_CASE_P(MigrationToVCurrent, | 1681 INSTANTIATE_TEST_CASE_P(MigrationToVCurrent, |
1684 LoginDatabaseMigrationTestBroken, | 1682 LoginDatabaseMigrationTestBroken, |
1685 testing::Range(1, 4)); | 1683 testing::Range(1, 4)); |
1686 | 1684 |
1687 } // namespace password_manager | 1685 } // namespace password_manager |
OLD | NEW |