| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "base/macros.h" | 5 #include "base/macros.h" |
| 6 #include "base/stl_util.h" | 6 #include "base/stl_util.h" |
| 7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "chrome/browser/password_manager/password_manager_test_base.h" | 8 #include "chrome/browser/password_manager/password_manager_test_base.h" |
| 9 #include "chrome/browser/password_manager/password_store_factory.h" | 9 #include "chrome/browser/password_manager/password_store_factory.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/profiles/profile_io_data.h" | 11 #include "chrome/browser/profiles/profile_io_data.h" |
| 12 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
| 13 #include "chrome/browser/ui/passwords/passwords_model_delegate.h" | 13 #include "chrome/browser/ui/passwords/passwords_model_delegate.h" |
| 14 #include "chrome/test/base/ui_test_utils.h" | 14 #include "chrome/test/base/ui_test_utils.h" |
| 15 #include "components/password_manager/core/browser/password_bubble_experiment.h" | 15 #include "components/password_manager/core/browser/password_bubble_experiment.h" |
| 16 #include "components/password_manager/core/browser/test_password_store.h" | 16 #include "components/password_manager/core/browser/test_password_store.h" |
| 17 #include "content/public/test/browser_test.h" | 17 #include "content/public/test/browser_test.h" |
| 18 #include "content/public/test/browser_test_utils.h" | 18 #include "content/public/test/browser_test_utils.h" |
| 19 #include "net/cert/cert_verify_result.h" | |
| 20 #include "net/cert/mock_cert_verifier.h" | |
| 21 #include "net/dns/mock_host_resolver.h" | 19 #include "net/dns/mock_host_resolver.h" |
| 22 | 20 |
| 23 namespace { | 21 namespace { |
| 24 | 22 |
| 25 class CredentialManagerBrowserTest : public PasswordManagerBrowserTestBase { | 23 class CredentialManagerBrowserTest : public PasswordManagerBrowserTestBase { |
| 26 public: | 24 public: |
| 27 CredentialManagerBrowserTest() = default; | 25 CredentialManagerBrowserTest() = default; |
| 28 | 26 |
| 29 bool IsShowingAccountChooser() { | 27 bool IsShowingAccountChooser() { |
| 30 return PasswordsModelDelegateFromWebContents(WebContents())-> | 28 return PasswordsModelDelegateFromWebContents(WebContents())-> |
| 31 GetState() == password_manager::ui::CREDENTIAL_REQUEST_STATE; | 29 GetState() == password_manager::ui::CREDENTIAL_REQUEST_STATE; |
| 32 } | 30 } |
| 33 | 31 |
| 34 // Similarly to PasswordManagerBrowserTestBase::NavigateToFile this is a | 32 // Similarly to PasswordManagerBrowserTestBase::NavigateToFile this is a |
| 35 // wrapper around ui_test_utils::NavigateURL that waits until DidFinishLoad() | 33 // wrapper around ui_test_utils::NavigateURL that waits until DidFinishLoad() |
| 36 // fires. Different to NavigateToFile this method allows passing a test_server | 34 // fires. Different to NavigateToFile this method allows passing a test_server |
| 37 // and modifications to the hostname. | 35 // and modifications to the hostname. |
| 38 void NavigateToURL(const net::EmbeddedTestServer& test_server, | 36 void NavigateToURL(const net::EmbeddedTestServer& test_server, |
| 39 const std::string& hostname, | 37 const std::string& hostname, |
| 40 const std::string& relative_url) { | 38 const std::string& relative_url) { |
| 41 NavigationObserver observer(WebContents()); | 39 NavigationObserver observer(WebContents()); |
| 42 GURL url = test_server.GetURL(hostname, relative_url); | 40 GURL url = test_server.GetURL(hostname, relative_url); |
| 43 ui_test_utils::NavigateToURL(browser(), url); | 41 ui_test_utils::NavigateToURL(browser(), url); |
| 44 observer.Wait(); | 42 observer.Wait(); |
| 45 } | 43 } |
| 46 | 44 |
| 47 void SetUpInProcessBrowserTestFixture() override { | |
| 48 ProfileIOData::SetCertVerifierForTesting(&mock_cert_verifier_); | |
| 49 } | |
| 50 | |
| 51 void TearDownInProcessBrowserTestFixture() override { | |
| 52 ProfileIOData::SetCertVerifierForTesting(nullptr); | |
| 53 } | |
| 54 | |
| 55 net::MockCertVerifier& mock_cert_verifier() { | |
| 56 return mock_cert_verifier_; | |
| 57 } | |
| 58 | |
| 59 private: | 45 private: |
| 60 net::MockCertVerifier mock_cert_verifier_; | |
| 61 DISALLOW_COPY_AND_ASSIGN(CredentialManagerBrowserTest); | 46 DISALLOW_COPY_AND_ASSIGN(CredentialManagerBrowserTest); |
| 62 }; | 47 }; |
| 63 | 48 |
| 64 // Tests. | 49 // Tests. |
| 65 | 50 |
| 66 IN_PROC_BROWSER_TEST_F(CredentialManagerBrowserTest, | 51 IN_PROC_BROWSER_TEST_F(CredentialManagerBrowserTest, |
| 67 AccountChooserWithOldCredentialAndNavigation) { | 52 AccountChooserWithOldCredentialAndNavigation) { |
| 68 // Save credentials with 'skip_zero_click'. | 53 // Save credentials with 'skip_zero_click'. |
| 69 scoped_refptr<password_manager::TestPasswordStore> password_store = | 54 scoped_refptr<password_manager::TestPasswordStore> password_store = |
| 70 static_cast<password_manager::TestPasswordStore*>( | 55 static_cast<password_manager::TestPasswordStore*>( |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 passwords_map.begin()->second; | 96 passwords_map.begin()->second; |
| 112 ASSERT_EQ(1u, passwords_vector.size()); | 97 ASSERT_EQ(1u, passwords_vector.size()); |
| 113 const autofill::PasswordForm& form = passwords_vector[0]; | 98 const autofill::PasswordForm& form = passwords_vector[0]; |
| 114 EXPECT_EQ(base::ASCIIToUTF16("user"), form.username_value); | 99 EXPECT_EQ(base::ASCIIToUTF16("user"), form.username_value); |
| 115 EXPECT_EQ(base::ASCIIToUTF16("password"), form.password_value); | 100 EXPECT_EQ(base::ASCIIToUTF16("password"), form.password_value); |
| 116 EXPECT_FALSE(form.skip_zero_click); | 101 EXPECT_FALSE(form.skip_zero_click); |
| 117 } | 102 } |
| 118 | 103 |
| 119 IN_PROC_BROWSER_TEST_F(CredentialManagerBrowserTest, | 104 IN_PROC_BROWSER_TEST_F(CredentialManagerBrowserTest, |
| 120 StoreSavesPSLMatchedCredential) { | 105 StoreSavesPSLMatchedCredential) { |
| 121 // Setup mock certificate for all origins. | |
| 122 auto cert = https_test_server().GetCertificate(); | |
| 123 net::CertVerifyResult verify_result; | |
| 124 verify_result.cert_status = 0; | |
| 125 verify_result.is_issued_by_known_root = true; | |
| 126 verify_result.verified_cert = cert; | |
| 127 mock_cert_verifier().AddResultForCert(cert.get(), verify_result, net::OK); | |
| 128 | |
| 129 // Redirect all requests to localhost. | 106 // Redirect all requests to localhost. |
| 130 host_resolver()->AddRule("*", "127.0.0.1"); | 107 host_resolver()->AddRule("*", "127.0.0.1"); |
| 131 | 108 |
| 132 scoped_refptr<password_manager::TestPasswordStore> password_store = | 109 scoped_refptr<password_manager::TestPasswordStore> password_store = |
| 133 static_cast<password_manager::TestPasswordStore*>( | 110 static_cast<password_manager::TestPasswordStore*>( |
| 134 PasswordStoreFactory::GetForProfile( | 111 PasswordStoreFactory::GetForProfile( |
| 135 browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS) | 112 browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS) |
| 136 .get()); | 113 .get()); |
| 137 | 114 |
| 138 // The call to |GetURL| is needed to get the correct port. | 115 // The call to |GetURL| is needed to get the correct port. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 // There should be an entry for both psl.example.com and www.example.com. | 156 // There should be an entry for both psl.example.com and www.example.com. |
| 180 password_manager::TestPasswordStore::PasswordMap passwords = | 157 password_manager::TestPasswordStore::PasswordMap passwords = |
| 181 password_store->stored_passwords(); | 158 password_store->stored_passwords(); |
| 182 GURL www_url = https_test_server().GetURL("www.example.com", "/"); | 159 GURL www_url = https_test_server().GetURL("www.example.com", "/"); |
| 183 EXPECT_EQ(2U, passwords.size()); | 160 EXPECT_EQ(2U, passwords.size()); |
| 184 EXPECT_TRUE(base::ContainsKey(passwords, psl_url.spec())); | 161 EXPECT_TRUE(base::ContainsKey(passwords, psl_url.spec())); |
| 185 EXPECT_TRUE(base::ContainsKey(passwords, www_url.spec())); | 162 EXPECT_TRUE(base::ContainsKey(passwords, www_url.spec())); |
| 186 } | 163 } |
| 187 | 164 |
| 188 IN_PROC_BROWSER_TEST_F(CredentialManagerBrowserTest, | 165 IN_PROC_BROWSER_TEST_F(CredentialManagerBrowserTest, |
| 166 ObsoleteHttpCredentialMovedOnMigrationToHstsSite) { |
| 167 // Add an http credential to the password store. |
| 168 GURL https_origin = https_test_server().base_url(); |
| 169 ASSERT_TRUE(https_origin.SchemeIs(url::kHttpsScheme)); |
| 170 GURL::Replacements rep; |
| 171 rep.SetSchemeStr(url::kHttpScheme); |
| 172 GURL http_origin = https_origin.ReplaceComponents(rep); |
| 173 autofill::PasswordForm http_form; |
| 174 http_form.signon_realm = http_origin.spec(); |
| 175 http_form.origin = http_origin; |
| 176 http_form.username_value = base::ASCIIToUTF16("user"); |
| 177 http_form.password_value = base::ASCIIToUTF16("12345"); |
| 178 scoped_refptr<password_manager::TestPasswordStore> password_store = |
| 179 static_cast<password_manager::TestPasswordStore*>( |
| 180 PasswordStoreFactory::GetForProfile( |
| 181 browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS) |
| 182 .get()); |
| 183 password_store->AddLogin(http_form); |
| 184 WaitForPasswordStore(); |
| 185 |
| 186 // Treat the host of the HTTPS test server as HSTS. |
| 187 AddHSTSHost(https_test_server().host_port_pair().host()); |
| 188 |
| 189 // Navigate to HTTPS page and trigger the migration. |
| 190 ui_test_utils::NavigateToURL( |
| 191 browser(), https_test_server().GetURL("/password/done.html")); |
| 192 |
| 193 // Call the API to trigger |get| and |store| and redirect. |
| 194 ASSERT_TRUE(content::ExecuteScript( |
| 195 RenderViewHost(), "navigator.credentials.get({password: true})")); |
| 196 |
| 197 // Issue the query for HTTPS credentials. |
| 198 WaitForPasswordStore(); |
| 199 |
| 200 // Realize there are no HTTPS credentials and issue the query for HTTP |
| 201 // credentials instead. |
| 202 WaitForPasswordStore(); |
| 203 |
| 204 // Sync with IO thread before continuing. This is necessary, because the |
| 205 // credential migration triggers a query for the HSTS state which gets |
| 206 // executed on the IO thread. The actual task is empty, because only the reply |
| 207 // is relevant. By the time the reply is executed it is guaranteed that the |
| 208 // migration is completed. |
| 209 const auto empty_lambda = []() {}; |
| 210 base::RunLoop run_loop; |
| 211 content::BrowserThread::PostTaskAndReply(content::BrowserThread::IO, |
| 212 FROM_HERE, base::Bind(empty_lambda), |
| 213 run_loop.QuitClosure()); |
| 214 run_loop.Run(); |
| 215 |
| 216 // Only HTTPS passwords should be present. |
| 217 EXPECT_TRUE( |
| 218 password_store->stored_passwords().at(http_origin.spec()).empty()); |
| 219 EXPECT_FALSE( |
| 220 password_store->stored_passwords().at(https_origin.spec()).empty()); |
| 221 } |
| 222 |
| 223 IN_PROC_BROWSER_TEST_F(CredentialManagerBrowserTest, |
| 189 AutoSigninOldCredentialAndNavigation) { | 224 AutoSigninOldCredentialAndNavigation) { |
| 190 // Save credentials with 'skip_zero_click' false. | 225 // Save credentials with 'skip_zero_click' false. |
| 191 scoped_refptr<password_manager::TestPasswordStore> password_store = | 226 scoped_refptr<password_manager::TestPasswordStore> password_store = |
| 192 static_cast<password_manager::TestPasswordStore*>( | 227 static_cast<password_manager::TestPasswordStore*>( |
| 193 PasswordStoreFactory::GetForProfile( | 228 PasswordStoreFactory::GetForProfile( |
| 194 browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS).get()); | 229 browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS).get()); |
| 195 autofill::PasswordForm signin_form; | 230 autofill::PasswordForm signin_form; |
| 196 signin_form.signon_realm = embedded_test_server()->base_url().spec(); | 231 signin_form.signon_realm = embedded_test_server()->base_url().spec(); |
| 197 signin_form.password_value = base::ASCIIToUTF16("password"); | 232 signin_form.password_value = base::ASCIIToUTF16("password"); |
| 198 signin_form.username_value = base::ASCIIToUTF16("user"); | 233 signin_form.username_value = base::ASCIIToUTF16("user"); |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 signin_form.skip_zero_click = false; | 346 signin_form.skip_zero_click = false; |
| 312 signin_form.times_used = 1; | 347 signin_form.times_used = 1; |
| 313 signin_form.password_value = base::ASCIIToUTF16("API"); | 348 signin_form.password_value = base::ASCIIToUTF16("API"); |
| 314 password_manager::TestPasswordStore::PasswordMap stored = | 349 password_manager::TestPasswordStore::PasswordMap stored = |
| 315 password_store->stored_passwords(); | 350 password_store->stored_passwords(); |
| 316 ASSERT_EQ(1u, stored.size()); | 351 ASSERT_EQ(1u, stored.size()); |
| 317 EXPECT_EQ(signin_form, stored[signin_form.signon_realm][0]); | 352 EXPECT_EQ(signin_form, stored[signin_form.signon_realm][0]); |
| 318 } | 353 } |
| 319 | 354 |
| 320 } // namespace | 355 } // namespace |
| OLD | NEW |