| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_manager_test_base.h" | 5 #include "chrome/browser/password_manager/password_manager_test_base.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/ref_counted.h" |
| 10 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 11 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 12 #include "chrome/browser/password_manager/password_store_factory.h" | 13 #include "chrome/browser/password_manager/password_store_factory.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/profiles/profile_io_data.h" |
| 14 #include "chrome/browser/ui/browser.h" | 16 #include "chrome/browser/ui/browser.h" |
| 15 #include "chrome/browser/ui/passwords/manage_passwords_ui_controller.h" | 17 #include "chrome/browser/ui/passwords/manage_passwords_ui_controller.h" |
| 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 18 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 17 #include "chrome/test/base/ui_test_utils.h" | 19 #include "chrome/test/base/ui_test_utils.h" |
| 18 #include "components/autofill/core/browser/autofill_test_utils.h" | 20 #include "components/autofill/core/browser/autofill_test_utils.h" |
| 19 #include "components/password_manager/core/browser/password_manager_test_utils.h
" | 21 #include "components/password_manager/core/browser/password_manager_test_utils.h
" |
| 20 #include "components/password_manager/core/browser/test_password_store.h" | 22 #include "components/password_manager/core/browser/test_password_store.h" |
| 21 #include "content/public/browser/navigation_details.h" | 23 #include "content/public/browser/navigation_details.h" |
| 22 #include "content/public/browser/navigation_handle.h" | 24 #include "content/public/browser/navigation_handle.h" |
| 23 #include "content/public/browser/render_frame_host.h" | 25 #include "content/public/browser/render_frame_host.h" |
| 24 #include "content/public/test/browser_test_utils.h" | 26 #include "content/public/test/browser_test_utils.h" |
| 25 #include "content/public/test/test_utils.h" | 27 #include "content/public/test/test_utils.h" |
| 28 #include "net/cert/cert_verify_result.h" |
| 29 #include "net/http/transport_security_state.h" |
| 26 #include "net/test/embedded_test_server/embedded_test_server.h" | 30 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 31 #include "net/url_request/url_request_context.h" |
| 32 #include "net/url_request/url_request_context_getter.h" |
| 27 | 33 |
| 28 namespace { | 34 namespace { |
| 29 | 35 |
| 30 // A helper class that synchronously waits until the password store handles a | 36 // A helper class that synchronously waits until the password store handles a |
| 31 // GetLogins() request. | 37 // GetLogins() request. |
| 32 class PasswordStoreResultsObserver | 38 class PasswordStoreResultsObserver |
| 33 : public password_manager::PasswordStoreConsumer { | 39 : public password_manager::PasswordStoreConsumer { |
| 34 public: | 40 public: |
| 35 PasswordStoreResultsObserver() = default; | 41 PasswordStoreResultsObserver() = default; |
| 36 | 42 |
| 37 void OnGetPasswordStoreResults( | 43 void OnGetPasswordStoreResults( |
| 38 std::vector<std::unique_ptr<autofill::PasswordForm>> results) override { | 44 std::vector<std::unique_ptr<autofill::PasswordForm>> results) override { |
| 39 run_loop_.Quit(); | 45 run_loop_.Quit(); |
| 40 } | 46 } |
| 41 | 47 |
| 42 void Wait() { run_loop_.Run(); } | 48 void Wait() { run_loop_.Run(); } |
| 43 | 49 |
| 44 private: | 50 private: |
| 45 base::RunLoop run_loop_; | 51 base::RunLoop run_loop_; |
| 46 | 52 |
| 47 DISALLOW_COPY_AND_ASSIGN(PasswordStoreResultsObserver); | 53 DISALLOW_COPY_AND_ASSIGN(PasswordStoreResultsObserver); |
| 48 }; | 54 }; |
| 49 | 55 |
| 56 void AddHSTSHostImpl( |
| 57 const scoped_refptr<net::URLRequestContextGetter>& request_context, |
| 58 const std::string& host) { |
| 59 ASSERT_TRUE(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 60 net::TransportSecurityState* transport_security_state = |
| 61 request_context->GetURLRequestContext()->transport_security_state(); |
| 62 if (!transport_security_state) { |
| 63 ADD_FAILURE(); |
| 64 return; |
| 65 } |
| 66 |
| 67 base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000); |
| 68 bool include_subdomains = false; |
| 69 transport_security_state->AddHSTS(host, expiry, include_subdomains); |
| 70 EXPECT_TRUE(transport_security_state->ShouldUpgradeToSSL(host)); |
| 71 } |
| 72 |
| 50 } // namespace | 73 } // namespace |
| 51 | 74 |
| 52 NavigationObserver::NavigationObserver(content::WebContents* web_contents) | 75 NavigationObserver::NavigationObserver(content::WebContents* web_contents) |
| 53 : content::WebContentsObserver(web_contents), | 76 : content::WebContentsObserver(web_contents), |
| 54 quit_on_entry_committed_(false), | 77 quit_on_entry_committed_(false), |
| 55 message_loop_runner_(new content::MessageLoopRunner) { | 78 message_loop_runner_(new content::MessageLoopRunner) { |
| 56 } | 79 } |
| 57 NavigationObserver::~NavigationObserver() { | 80 NavigationObserver::~NavigationObserver() { |
| 58 } | 81 } |
| 59 | 82 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 browser()->profile(), | 153 browser()->profile(), |
| 131 password_manager::BuildPasswordStore< | 154 password_manager::BuildPasswordStore< |
| 132 content::BrowserContext, password_manager::TestPasswordStore>); | 155 content::BrowserContext, password_manager::TestPasswordStore>); |
| 133 ASSERT_TRUE(embedded_test_server()->Start()); | 156 ASSERT_TRUE(embedded_test_server()->Start()); |
| 134 | 157 |
| 135 // Setup HTTPS server serving files from standard test directory. | 158 // Setup HTTPS server serving files from standard test directory. |
| 136 static constexpr base::FilePath::CharType kDocRoot[] = | 159 static constexpr base::FilePath::CharType kDocRoot[] = |
| 137 FILE_PATH_LITERAL("chrome/test/data"); | 160 FILE_PATH_LITERAL("chrome/test/data"); |
| 138 https_test_server().ServeFilesFromSourceDirectory(base::FilePath(kDocRoot)); | 161 https_test_server().ServeFilesFromSourceDirectory(base::FilePath(kDocRoot)); |
| 139 ASSERT_TRUE(https_test_server().Start()); | 162 ASSERT_TRUE(https_test_server().Start()); |
| 163 |
| 164 // Whitelist all certs for the HTTPS server. |
| 165 auto cert = https_test_server().GetCertificate(); |
| 166 net::CertVerifyResult verify_result; |
| 167 verify_result.cert_status = 0; |
| 168 verify_result.is_issued_by_known_root = true; |
| 169 verify_result.verified_cert = cert; |
| 170 mock_cert_verifier().AddResultForCert(cert.get(), verify_result, net::OK); |
| 140 } | 171 } |
| 141 | 172 |
| 142 void PasswordManagerBrowserTestBase::TearDownOnMainThread() { | 173 void PasswordManagerBrowserTestBase::TearDownOnMainThread() { |
| 143 ASSERT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete()); | 174 ASSERT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete()); |
| 144 } | 175 } |
| 145 | 176 |
| 177 void PasswordManagerBrowserTestBase::SetUpInProcessBrowserTestFixture() { |
| 178 ProfileIOData::SetCertVerifierForTesting(&mock_cert_verifier_); |
| 179 } |
| 180 |
| 181 void PasswordManagerBrowserTestBase::TearDownInProcessBrowserTestFixture() { |
| 182 ProfileIOData::SetCertVerifierForTesting(nullptr); |
| 183 } |
| 184 |
| 146 content::WebContents* PasswordManagerBrowserTestBase::WebContents() { | 185 content::WebContents* PasswordManagerBrowserTestBase::WebContents() { |
| 147 return browser()->tab_strip_model()->GetActiveWebContents(); | 186 return browser()->tab_strip_model()->GetActiveWebContents(); |
| 148 } | 187 } |
| 149 | 188 |
| 150 content::RenderViewHost* PasswordManagerBrowserTestBase::RenderViewHost() { | 189 content::RenderViewHost* PasswordManagerBrowserTestBase::RenderViewHost() { |
| 151 return WebContents()->GetRenderViewHost(); | 190 return WebContents()->GetRenderViewHost(); |
| 152 } | 191 } |
| 153 | 192 |
| 154 void PasswordManagerBrowserTestBase::NavigateToFile(const std::string& path) { | 193 void PasswordManagerBrowserTestBase::NavigateToFile(const std::string& path) { |
| 155 NavigationObserver observer(WebContents()); | 194 NavigationObserver observer(WebContents()); |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 " var element = document.getElementById('%s');" | 323 " var element = document.getElementById('%s');" |
| 285 "window.domAutomationController.send(element && element.value == '%s');", | 324 "window.domAutomationController.send(element && element.value == '%s');", |
| 286 iframe_id.c_str(), iframe_id.c_str(), element_id.c_str(), | 325 iframe_id.c_str(), iframe_id.c_str(), element_id.c_str(), |
| 287 element_id.c_str(), expected_value.c_str()); | 326 element_id.c_str(), expected_value.c_str()); |
| 288 bool return_value = false; | 327 bool return_value = false; |
| 289 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( | 328 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( |
| 290 RenderViewHost(), value_check_script, &return_value)); | 329 RenderViewHost(), value_check_script, &return_value)); |
| 291 EXPECT_TRUE(return_value) << "element_id = " << element_id | 330 EXPECT_TRUE(return_value) << "element_id = " << element_id |
| 292 << ", expected_value = " << expected_value; | 331 << ", expected_value = " << expected_value; |
| 293 } | 332 } |
| 333 |
| 334 void PasswordManagerBrowserTestBase::AddHSTSHost(const std::string& host) { |
| 335 base::RunLoop run_loop; |
| 336 |
| 337 content::BrowserThread::PostTaskAndReply( |
| 338 content::BrowserThread::IO, FROM_HERE, |
| 339 base::Bind(&AddHSTSHostImpl, |
| 340 make_scoped_refptr(browser()->profile()->GetRequestContext()), |
| 341 host), |
| 342 run_loop.QuitClosure()); |
| 343 |
| 344 run_loop.Run(); |
| 345 } |
| OLD | NEW |