| 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/memory/ref_counted.h" |
| 11 #include "base/run_loop.h" | |
| 12 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 #include "chrome/browser/password_manager/chrome_password_manager_client.h" |
| 13 #include "chrome/browser/password_manager/password_store_factory.h" | 13 #include "chrome/browser/password_manager/password_store_factory.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/profiles/profile_io_data.h" | 15 #include "chrome/browser/profiles/profile_io_data.h" |
| 16 #include "chrome/browser/ui/autofill/chrome_autofill_client.h" |
| 16 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
| 17 #include "chrome/browser/ui/passwords/manage_passwords_ui_controller.h" | 18 #include "chrome/browser/ui/passwords/manage_passwords_ui_controller.h" |
| 18 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 19 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 19 #include "chrome/test/base/ui_test_utils.h" | 20 #include "chrome/test/base/ui_test_utils.h" |
| 20 #include "components/autofill/core/browser/autofill_test_utils.h" | 21 #include "components/autofill/core/browser/autofill_test_utils.h" |
| 21 #include "components/password_manager/core/browser/password_manager_test_utils.h
" | 22 #include "components/password_manager/core/browser/password_manager_test_utils.h
" |
| 22 #include "components/password_manager/core/browser/test_password_store.h" | 23 #include "components/password_manager/core/browser/test_password_store.h" |
| 23 #include "content/public/browser/navigation_details.h" | 24 #include "content/public/browser/navigation_details.h" |
| 24 #include "content/public/browser/navigation_handle.h" | 25 #include "content/public/browser/navigation_handle.h" |
| 25 #include "content/public/browser/render_frame_host.h" | 26 #include "content/public/browser/render_frame_host.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 46 } | 47 } |
| 47 | 48 |
| 48 void Wait() { run_loop_.Run(); } | 49 void Wait() { run_loop_.Run(); } |
| 49 | 50 |
| 50 private: | 51 private: |
| 51 base::RunLoop run_loop_; | 52 base::RunLoop run_loop_; |
| 52 | 53 |
| 53 DISALLOW_COPY_AND_ASSIGN(PasswordStoreResultsObserver); | 54 DISALLOW_COPY_AND_ASSIGN(PasswordStoreResultsObserver); |
| 54 }; | 55 }; |
| 55 | 56 |
| 57 // ManagePasswordsUIController subclass to capture the UI events. |
| 58 class CustomManagePasswordsUIController : public ManagePasswordsUIController { |
| 59 public: |
| 60 explicit CustomManagePasswordsUIController( |
| 61 content::WebContents* web_contents); |
| 62 |
| 63 void WaitForAccountChooser(); |
| 64 |
| 65 private: |
| 66 // PasswordsClientUIDelegate: |
| 67 bool OnChooseCredentials( |
| 68 std::vector<std::unique_ptr<autofill::PasswordForm>> local_credentials, |
| 69 const GURL& origin, |
| 70 const ManagePasswordsState::CredentialsCallback& callback) override; |
| 71 |
| 72 // The loop to be stopped when the account chooser pops up. |
| 73 base::RunLoop* account_chooser_loop_ = nullptr; |
| 74 |
| 75 DISALLOW_COPY_AND_ASSIGN(CustomManagePasswordsUIController); |
| 76 }; |
| 77 |
| 78 CustomManagePasswordsUIController::CustomManagePasswordsUIController( |
| 79 content::WebContents* web_contents) |
| 80 : ManagePasswordsUIController(web_contents) { |
| 81 // Attach CustomManagePasswordsUIController to |web_contents| so the default |
| 82 // ManagePasswordsUIController isn't created. |
| 83 // Do not silently replace an existing ManagePasswordsUIController because it |
| 84 // unregisters itself in WebContentsDestroyed(). |
| 85 EXPECT_FALSE(web_contents->GetUserData(UserDataKey())); |
| 86 web_contents->SetUserData(UserDataKey(), this); |
| 87 } |
| 88 |
| 89 void CustomManagePasswordsUIController::WaitForAccountChooser() { |
| 90 base::RunLoop account_chooser_loop; |
| 91 account_chooser_loop_ = &account_chooser_loop; |
| 92 account_chooser_loop_->Run(); |
| 93 } |
| 94 |
| 95 bool CustomManagePasswordsUIController::OnChooseCredentials( |
| 96 std::vector<std::unique_ptr<autofill::PasswordForm>> local_credentials, |
| 97 const GURL& origin, |
| 98 const ManagePasswordsState::CredentialsCallback& callback) { |
| 99 if (account_chooser_loop_) { |
| 100 account_chooser_loop_->Quit(); |
| 101 account_chooser_loop_ = nullptr; |
| 102 } |
| 103 return ManagePasswordsUIController::OnChooseCredentials( |
| 104 std::move(local_credentials), origin, callback); |
| 105 } |
| 106 |
| 56 void AddHSTSHostImpl( | 107 void AddHSTSHostImpl( |
| 57 const scoped_refptr<net::URLRequestContextGetter>& request_context, | 108 const scoped_refptr<net::URLRequestContextGetter>& request_context, |
| 58 const std::string& host) { | 109 const std::string& host) { |
| 59 ASSERT_TRUE(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 110 ASSERT_TRUE(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 60 net::TransportSecurityState* transport_security_state = | 111 net::TransportSecurityState* transport_security_state = |
| 61 request_context->GetURLRequestContext()->transport_security_state(); | 112 request_context->GetURLRequestContext()->transport_security_state(); |
| 62 if (!transport_security_state) { | 113 if (!transport_security_state) { |
| 63 ADD_FAILURE(); | 114 ADD_FAILURE(); |
| 64 return; | 115 return; |
| 65 } | 116 } |
| 66 | 117 |
| 67 base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000); | 118 base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000); |
| 68 bool include_subdomains = false; | 119 bool include_subdomains = false; |
| 69 transport_security_state->AddHSTS(host, expiry, include_subdomains); | 120 transport_security_state->AddHSTS(host, expiry, include_subdomains); |
| 70 EXPECT_TRUE(transport_security_state->ShouldUpgradeToSSL(host)); | 121 EXPECT_TRUE(transport_security_state->ShouldUpgradeToSSL(host)); |
| 71 } | 122 } |
| 72 | 123 |
| 73 } // namespace | 124 } // namespace |
| 74 | 125 |
| 75 NavigationObserver::NavigationObserver(content::WebContents* web_contents) | 126 NavigationObserver::NavigationObserver(content::WebContents* web_contents) |
| 76 : content::WebContentsObserver(web_contents), | 127 : content::WebContentsObserver(web_contents), |
| 77 quit_on_entry_committed_(false), | 128 quit_on_entry_committed_(false) {} |
| 78 message_loop_runner_(new content::MessageLoopRunner) { | |
| 79 } | |
| 80 NavigationObserver::~NavigationObserver() { | 129 NavigationObserver::~NavigationObserver() { |
| 81 } | 130 } |
| 82 | 131 |
| 83 void NavigationObserver::DidFinishNavigation( | 132 void NavigationObserver::DidFinishNavigation( |
| 84 content::NavigationHandle* navigation_handle) { | 133 content::NavigationHandle* navigation_handle) { |
| 85 if (!navigation_handle->HasCommitted()) | 134 if (!navigation_handle->HasCommitted()) |
| 86 return; | 135 return; |
| 87 | 136 |
| 88 if (quit_on_entry_committed_) | 137 if (quit_on_entry_committed_) |
| 89 message_loop_runner_->Quit(); | 138 run_loop_.Quit(); |
| 90 } | 139 } |
| 91 | 140 |
| 92 void NavigationObserver::DidFinishLoad( | 141 void NavigationObserver::DidFinishLoad( |
| 93 content::RenderFrameHost* render_frame_host, | 142 content::RenderFrameHost* render_frame_host, |
| 94 const GURL& validated_url) { | 143 const GURL& validated_url) { |
| 95 render_frame_host_ = render_frame_host; | 144 render_frame_host_ = render_frame_host; |
| 96 if (!wait_for_path_.empty()) { | 145 if (!wait_for_path_.empty()) { |
| 97 if (validated_url.path() == wait_for_path_) | 146 if (validated_url.path() == wait_for_path_) |
| 98 message_loop_runner_->Quit(); | 147 run_loop_.Quit(); |
| 99 } else if (!render_frame_host->GetParent()) { | 148 } else if (!render_frame_host->GetParent()) { |
| 100 message_loop_runner_->Quit(); | 149 run_loop_.Quit(); |
| 101 } | 150 } |
| 102 } | 151 } |
| 103 | 152 |
| 104 void NavigationObserver::Wait() { | 153 void NavigationObserver::Wait() { |
| 105 message_loop_runner_->Run(); | 154 run_loop_.Run(); |
| 106 } | 155 } |
| 107 | 156 |
| 108 BubbleObserver::BubbleObserver(content::WebContents* web_contents) | 157 BubbleObserver::BubbleObserver(content::WebContents* web_contents) |
| 109 : passwords_ui_controller_( | 158 : passwords_ui_controller_( |
| 110 ManagePasswordsUIController::FromWebContents(web_contents)) {} | 159 ManagePasswordsUIController::FromWebContents(web_contents)) {} |
| 111 | 160 |
| 112 bool BubbleObserver::IsShowingSavePrompt() const { | 161 bool BubbleObserver::IsShowingSavePrompt() const { |
| 113 return passwords_ui_controller_->GetState() == | 162 return passwords_ui_controller_->GetState() == |
| 114 password_manager::ui::PENDING_PASSWORD_STATE; | 163 password_manager::ui::PENDING_PASSWORD_STATE; |
| 115 } | 164 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 131 EXPECT_FALSE(IsShowingSavePrompt()); | 180 EXPECT_FALSE(IsShowingSavePrompt()); |
| 132 } | 181 } |
| 133 | 182 |
| 134 void BubbleObserver::AcceptUpdatePrompt( | 183 void BubbleObserver::AcceptUpdatePrompt( |
| 135 const autofill::PasswordForm& form) const { | 184 const autofill::PasswordForm& form) const { |
| 136 ASSERT_TRUE(IsShowingUpdatePrompt()); | 185 ASSERT_TRUE(IsShowingUpdatePrompt()); |
| 137 passwords_ui_controller_->UpdatePassword(form); | 186 passwords_ui_controller_->UpdatePassword(form); |
| 138 EXPECT_FALSE(IsShowingUpdatePrompt()); | 187 EXPECT_FALSE(IsShowingUpdatePrompt()); |
| 139 } | 188 } |
| 140 | 189 |
| 190 void BubbleObserver::WaitForAccountChooser() const { |
| 191 if (passwords_ui_controller_->GetState() == |
| 192 password_manager::ui::CREDENTIAL_REQUEST_STATE) |
| 193 return; |
| 194 CustomManagePasswordsUIController* controller = |
| 195 static_cast<CustomManagePasswordsUIController*>(passwords_ui_controller_); |
| 196 controller->WaitForAccountChooser(); |
| 197 } |
| 198 |
| 141 PasswordManagerBrowserTestBase::PasswordManagerBrowserTestBase() | 199 PasswordManagerBrowserTestBase::PasswordManagerBrowserTestBase() |
| 142 : https_test_server_(net::EmbeddedTestServer::TYPE_HTTPS) {} | 200 : https_test_server_(net::EmbeddedTestServer::TYPE_HTTPS), |
| 143 PasswordManagerBrowserTestBase::~PasswordManagerBrowserTestBase() { | 201 web_contents_(nullptr) {} |
| 144 } | 202 |
| 203 PasswordManagerBrowserTestBase::~PasswordManagerBrowserTestBase() = default; |
| 145 | 204 |
| 146 void PasswordManagerBrowserTestBase::SetUpOnMainThread() { | 205 void PasswordManagerBrowserTestBase::SetUpOnMainThread() { |
| 147 // Use TestPasswordStore to remove a possible race. Normally the | 206 // Use TestPasswordStore to remove a possible race. Normally the |
| 148 // PasswordStore does its database manipulation on the DB thread, which | 207 // PasswordStore does its database manipulation on the DB thread, which |
| 149 // creates a possible race during navigation. Specifically the | 208 // creates a possible race during navigation. Specifically the |
| 150 // PasswordManager will ignore any forms in a page if the load from the | 209 // PasswordManager will ignore any forms in a page if the load from the |
| 151 // PasswordStore has not completed. | 210 // PasswordStore has not completed. |
| 152 PasswordStoreFactory::GetInstance()->SetTestingFactory( | 211 PasswordStoreFactory::GetInstance()->SetTestingFactory( |
| 153 browser()->profile(), | 212 browser()->profile(), |
| 154 password_manager::BuildPasswordStore< | 213 password_manager::BuildPasswordStore< |
| 155 content::BrowserContext, password_manager::TestPasswordStore>); | 214 content::BrowserContext, password_manager::TestPasswordStore>); |
| 156 ASSERT_TRUE(embedded_test_server()->Start()); | 215 ASSERT_TRUE(embedded_test_server()->Start()); |
| 157 | 216 |
| 158 // Setup HTTPS server serving files from standard test directory. | 217 // Setup HTTPS server serving files from standard test directory. |
| 159 static constexpr base::FilePath::CharType kDocRoot[] = | 218 static constexpr base::FilePath::CharType kDocRoot[] = |
| 160 FILE_PATH_LITERAL("chrome/test/data"); | 219 FILE_PATH_LITERAL("chrome/test/data"); |
| 161 https_test_server().ServeFilesFromSourceDirectory(base::FilePath(kDocRoot)); | 220 https_test_server().ServeFilesFromSourceDirectory(base::FilePath(kDocRoot)); |
| 162 ASSERT_TRUE(https_test_server().Start()); | 221 ASSERT_TRUE(https_test_server().Start()); |
| 163 | 222 |
| 164 // Whitelist all certs for the HTTPS server. | 223 // Whitelist all certs for the HTTPS server. |
| 165 auto cert = https_test_server().GetCertificate(); | 224 auto cert = https_test_server().GetCertificate(); |
| 166 net::CertVerifyResult verify_result; | 225 net::CertVerifyResult verify_result; |
| 167 verify_result.cert_status = 0; | 226 verify_result.cert_status = 0; |
| 168 verify_result.is_issued_by_known_root = true; | 227 verify_result.is_issued_by_known_root = true; |
| 169 verify_result.verified_cert = cert; | 228 verify_result.verified_cert = cert; |
| 170 mock_cert_verifier().AddResultForCert(cert.get(), verify_result, net::OK); | 229 mock_cert_verifier().AddResultForCert(cert.get(), verify_result, net::OK); |
| 230 |
| 231 // Add a tab with a customized ManagePasswordsUIController. Thus, we can |
| 232 // intercept useful UI events. |
| 233 content::WebContents* tab = |
| 234 browser()->tab_strip_model()->GetActiveWebContents(); |
| 235 web_contents_ = content::WebContents::Create( |
| 236 content::WebContents::CreateParams(tab->GetBrowserContext())); |
| 237 ASSERT_TRUE(web_contents_); |
| 238 |
| 239 // ManagePasswordsUIController needs ChromePasswordManagerClient for logging. |
| 240 autofill::ChromeAutofillClient::CreateForWebContents(web_contents_); |
| 241 ChromePasswordManagerClient::CreateForWebContentsWithAutofillClient( |
| 242 web_contents_, |
| 243 autofill::ChromeAutofillClient::FromWebContents(web_contents_)); |
| 244 ASSERT_TRUE(ChromePasswordManagerClient::FromWebContents(web_contents_)); |
| 245 CustomManagePasswordsUIController* controller = |
| 246 new CustomManagePasswordsUIController(web_contents_); |
| 247 browser()->tab_strip_model()->AppendWebContents(web_contents_, true); |
| 248 browser()->tab_strip_model()->CloseWebContentsAt(0, |
| 249 TabStripModel::CLOSE_NONE); |
| 250 ASSERT_EQ(controller, |
| 251 ManagePasswordsUIController::FromWebContents(web_contents_)); |
| 252 ASSERT_EQ(web_contents_, |
| 253 browser()->tab_strip_model()->GetActiveWebContents()); |
| 254 ASSERT_FALSE(web_contents_->IsLoading()); |
| 171 } | 255 } |
| 172 | 256 |
| 173 void PasswordManagerBrowserTestBase::TearDownOnMainThread() { | 257 void PasswordManagerBrowserTestBase::TearDownOnMainThread() { |
| 174 ASSERT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete()); | 258 ASSERT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete()); |
| 175 } | 259 } |
| 176 | 260 |
| 177 void PasswordManagerBrowserTestBase::SetUpInProcessBrowserTestFixture() { | 261 void PasswordManagerBrowserTestBase::SetUpInProcessBrowserTestFixture() { |
| 178 ProfileIOData::SetCertVerifierForTesting(&mock_cert_verifier_); | 262 ProfileIOData::SetCertVerifierForTesting(&mock_cert_verifier_); |
| 179 } | 263 } |
| 180 | 264 |
| 181 void PasswordManagerBrowserTestBase::TearDownInProcessBrowserTestFixture() { | 265 void PasswordManagerBrowserTestBase::TearDownInProcessBrowserTestFixture() { |
| 182 ProfileIOData::SetCertVerifierForTesting(nullptr); | 266 ProfileIOData::SetCertVerifierForTesting(nullptr); |
| 183 } | 267 } |
| 184 | 268 |
| 185 content::WebContents* PasswordManagerBrowserTestBase::WebContents() { | 269 content::WebContents* PasswordManagerBrowserTestBase::WebContents() { |
| 186 return browser()->tab_strip_model()->GetActiveWebContents(); | 270 return web_contents_; |
| 187 } | 271 } |
| 188 | 272 |
| 189 content::RenderViewHost* PasswordManagerBrowserTestBase::RenderViewHost() { | 273 content::RenderViewHost* PasswordManagerBrowserTestBase::RenderViewHost() { |
| 190 return WebContents()->GetRenderViewHost(); | 274 return WebContents()->GetRenderViewHost(); |
| 191 } | 275 } |
| 192 | 276 |
| 193 void PasswordManagerBrowserTestBase::NavigateToFile(const std::string& path) { | 277 void PasswordManagerBrowserTestBase::NavigateToFile(const std::string& path) { |
| 278 ASSERT_EQ(web_contents_, |
| 279 browser()->tab_strip_model()->GetActiveWebContents()); |
| 194 NavigationObserver observer(WebContents()); | 280 NavigationObserver observer(WebContents()); |
| 195 GURL url = embedded_test_server()->GetURL(path); | 281 GURL url = embedded_test_server()->GetURL(path); |
| 196 ui_test_utils::NavigateToURL(browser(), url); | 282 ui_test_utils::NavigateToURL(browser(), url); |
| 197 observer.Wait(); | 283 observer.Wait(); |
| 198 } | 284 } |
| 199 | 285 |
| 200 void PasswordManagerBrowserTestBase::VerifyPasswordIsSavedAndFilled( | 286 void PasswordManagerBrowserTestBase::VerifyPasswordIsSavedAndFilled( |
| 201 const std::string& filename, | 287 const std::string& filename, |
| 202 const std::string& submission_script, | 288 const std::string& submission_script, |
| 203 const std::string& expected_element, | 289 const std::string& expected_element, |
| 204 const std::string& expected_value) { | 290 const std::string& expected_value) { |
| 205 password_manager::TestPasswordStore* password_store = | 291 password_manager::TestPasswordStore* password_store = |
| 206 static_cast<password_manager::TestPasswordStore*>( | 292 static_cast<password_manager::TestPasswordStore*>( |
| 207 PasswordStoreFactory::GetForProfile( | 293 PasswordStoreFactory::GetForProfile( |
| 208 browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS).get()); | 294 browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS).get()); |
| 209 EXPECT_TRUE(password_store->IsEmpty()); | 295 EXPECT_TRUE(password_store->IsEmpty()); |
| 210 | 296 |
| 211 NavigateToFile(filename); | 297 NavigateToFile(filename); |
| 212 | 298 |
| 213 NavigationObserver observer(WebContents()); | 299 NavigationObserver observer(WebContents()); |
| 214 std::unique_ptr<BubbleObserver> prompt_observer( | 300 std::unique_ptr<BubbleObserver> prompt_observer( |
| 215 new BubbleObserver(WebContents())); | 301 new BubbleObserver(WebContents())); |
| 216 ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), submission_script)); | 302 ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), submission_script)); |
| 217 observer.Wait(); | 303 observer.Wait(); |
| 218 | 304 |
| 219 prompt_observer->AcceptSavePrompt(); | 305 prompt_observer->AcceptSavePrompt(); |
| 220 | 306 |
| 221 // Spin the message loop to make sure the password store had a chance to save | 307 // Spin the message loop to make sure the password store had a chance to save |
| 222 // the password. | 308 // the password. |
| 223 base::RunLoop run_loop; | 309 WaitForPasswordStore(); |
| 224 run_loop.RunUntilIdle(); | |
| 225 ASSERT_FALSE(password_store->IsEmpty()); | 310 ASSERT_FALSE(password_store->IsEmpty()); |
| 226 | 311 |
| 227 NavigateToFile(filename); | 312 NavigateToFile(filename); |
| 228 | 313 |
| 229 // Let the user interact with the page, so that DOM gets modification events, | 314 // Let the user interact with the page, so that DOM gets modification events, |
| 230 // needed for autofilling fields. | 315 // needed for autofilling fields. |
| 231 content::SimulateMouseClickAt( | 316 content::SimulateMouseClickAt( |
| 232 WebContents(), 0, blink::WebMouseEvent::Button::Left, gfx::Point(1, 1)); | 317 WebContents(), 0, blink::WebMouseEvent::Button::Left, gfx::Point(1, 1)); |
| 233 | 318 |
| 234 // Wait until that interaction causes the password value to be revealed. | 319 // Wait until that interaction causes the password value to be revealed. |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 | 421 |
| 337 content::BrowserThread::PostTaskAndReply( | 422 content::BrowserThread::PostTaskAndReply( |
| 338 content::BrowserThread::IO, FROM_HERE, | 423 content::BrowserThread::IO, FROM_HERE, |
| 339 base::Bind(&AddHSTSHostImpl, | 424 base::Bind(&AddHSTSHostImpl, |
| 340 make_scoped_refptr(browser()->profile()->GetRequestContext()), | 425 make_scoped_refptr(browser()->profile()->GetRequestContext()), |
| 341 host), | 426 host), |
| 342 run_loop.QuitClosure()); | 427 run_loop.QuitClosure()); |
| 343 | 428 |
| 344 run_loop.Run(); | 429 run_loop.Run(); |
| 345 } | 430 } |
| OLD | NEW |