| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <algorithm> | 5 #include <algorithm> |
| 6 #include <list> | 6 #include <list> |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 const AuthInfo& info = i->second; | 92 const AuthInfo& info = i->second; |
| 93 handler->SetAuth(base::UTF8ToUTF16(info.username_), | 93 handler->SetAuth(base::UTF8ToUTF16(info.username_), |
| 94 base::UTF8ToUTF16(info.password_)); | 94 base::UTF8ToUTF16(info.password_)); |
| 95 } | 95 } |
| 96 } | 96 } |
| 97 | 97 |
| 98 const char kPrefetchAuthPage[] = "/login/prefetch.html"; | 98 const char kPrefetchAuthPage[] = "/login/prefetch.html"; |
| 99 | 99 |
| 100 const char kMultiRealmTestPage[] = "/login/multi_realm.html"; | 100 const char kMultiRealmTestPage[] = "/login/multi_realm.html"; |
| 101 const int kMultiRealmTestRealmCount = 2; | 101 const int kMultiRealmTestRealmCount = 2; |
| 102 const int kMultiRealmTestAuthRequestsCount = 4; | |
| 103 | 102 |
| 104 const char kSingleRealmTestPage[] = "/login/single_realm.html"; | 103 const char kSingleRealmTestPage[] = "/login/single_realm.html"; |
| 105 | 104 |
| 106 const char kAuthBasicPage[] = "/auth-basic"; | 105 const char kAuthBasicPage[] = "/auth-basic"; |
| 107 const char kAuthDigestPage[] = "/auth-digest"; | 106 const char kAuthDigestPage[] = "/auth-digest"; |
| 108 | 107 |
| 109 // It does not matter what pages are selected as no-auth, as long as they exist. | 108 // It does not matter what pages are selected as no-auth, as long as they exist. |
| 110 // Navigating to non-existing pages caused flakes in the past | 109 // Navigating to non-existing pages caused flakes in the past |
| 111 // (https://crbug.com/636875). | 110 // (https://crbug.com/636875). |
| 112 const char kNoAuthPage1[] = "/simple.html"; | 111 const char kNoAuthPage1[] = "/simple.html"; |
| (...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 456 ASSERT_TRUE(controller->CanGoForward()); | 455 ASSERT_TRUE(controller->CanGoForward()); |
| 457 controller->GoForward(); | 456 controller->GoForward(); |
| 458 auth_cancelled_waiter.Wait(); | 457 auth_cancelled_waiter.Wait(); |
| 459 load_stop_waiter.Wait(); | 458 load_stop_waiter.Wait(); |
| 460 EXPECT_TRUE(observer.handlers().empty()); | 459 EXPECT_TRUE(observer.handlers().empty()); |
| 461 } | 460 } |
| 462 | 461 |
| 463 // Test handling of resources that require authentication even though | 462 // Test handling of resources that require authentication even though |
| 464 // the page they are included on doesn't. In this case we should only | 463 // the page they are included on doesn't. In this case we should only |
| 465 // present the minimal number of prompts necessary for successfully | 464 // present the minimal number of prompts necessary for successfully |
| 466 // displaying the page. | 465 // displaying the page. First we check whether cancelling works as |
| 467 class MultiRealmLoginPromptBrowserTest : public LoginPromptBrowserTest { | 466 // expected. |
| 468 public: | 467 IN_PROC_BROWSER_TEST_F(LoginPromptBrowserTest, MultipleRealmCancellation) { |
| 469 void TearDownOnMainThread() override { | |
| 470 login_prompt_observer_.UnregisterAll(); | |
| 471 LoginPromptBrowserTest::TearDownOnMainThread(); | |
| 472 } | |
| 473 | |
| 474 // Load the multi-realm test page, waits for LoginHandlers to be created, then | |
| 475 // calls |for_each_realm_func| once for each authentication realm, passing a | |
| 476 // LoginHandler for the realm as an argument. The page should stop loading | |
| 477 // after that. | |
| 478 template <class F> | |
| 479 void RunTest(const F& for_each_realm_func); | |
| 480 | |
| 481 NavigationController* GetNavigationController() { | |
| 482 return &browser() | |
| 483 ->tab_strip_model() | |
| 484 ->GetActiveWebContents() | |
| 485 ->GetController(); | |
| 486 } | |
| 487 | |
| 488 LoginPromptBrowserTestObserver* login_prompt_observer() { | |
| 489 return &login_prompt_observer_; | |
| 490 } | |
| 491 | |
| 492 private: | |
| 493 LoginPromptBrowserTestObserver login_prompt_observer_; | |
| 494 }; | |
| 495 | |
| 496 template <class F> | |
| 497 void MultiRealmLoginPromptBrowserTest::RunTest(const F& for_each_realm_func) { | |
| 498 ASSERT_TRUE(embedded_test_server()->Start()); | 468 ASSERT_TRUE(embedded_test_server()->Start()); |
| 499 GURL test_page = embedded_test_server()->GetURL(kMultiRealmTestPage); | 469 GURL test_page = embedded_test_server()->GetURL(kMultiRealmTestPage); |
| 500 | 470 |
| 501 NavigationController* controller = GetNavigationController(); | 471 content::WebContents* contents = |
| 472 browser()->tab_strip_model()->GetActiveWebContents(); |
| 473 NavigationController* controller = &contents->GetController(); |
| 474 LoginPromptBrowserTestObserver observer; |
| 502 | 475 |
| 503 login_prompt_observer_.Register( | 476 observer.Register(content::Source<NavigationController>(controller)); |
| 504 content::Source<NavigationController>(controller)); | |
| 505 | 477 |
| 506 WindowedLoadStopObserver load_stop_waiter(controller, 1); | 478 WindowedLoadStopObserver load_stop_waiter(controller, 1); |
| 507 | 479 |
| 508 browser()->OpenURL(OpenURLParams(test_page, Referrer(), | 480 { |
| 509 WindowOpenDisposition::CURRENT_TAB, | 481 WindowedAuthNeededObserver auth_needed_waiter(controller); |
| 510 ui::PAGE_TRANSITION_TYPED, false)); | 482 browser()->OpenURL(OpenURLParams(test_page, Referrer(), |
| 483 WindowOpenDisposition::CURRENT_TAB, |
| 484 ui::PAGE_TRANSITION_TYPED, false)); |
| 485 auth_needed_waiter.Wait(); |
| 486 } |
| 511 | 487 |
| 512 // Need to have LoginHandlers created for all requests that need | 488 int n_handlers = 0; |
| 513 // authentication. | |
| 514 while (login_prompt_observer_.handlers().size() < | |
| 515 kMultiRealmTestAuthRequestsCount) | |
| 516 WindowedAuthNeededObserver(controller).Wait(); | |
| 517 | 489 |
| 518 // Now confirm or cancel auth once per realm. | 490 while (n_handlers < kMultiRealmTestRealmCount) { |
| 519 std::set<std::string> seen_realms; | 491 WindowedAuthNeededObserver auth_needed_waiter(controller); |
| 520 for (int i = 0; i < kMultiRealmTestRealmCount; ++i) { | |
| 521 auto it = std::find_if( | |
| 522 login_prompt_observer_.handlers().begin(), | |
| 523 login_prompt_observer_.handlers().end(), | |
| 524 [&seen_realms](LoginHandler* handler) { | |
| 525 return seen_realms.count(handler->auth_info()->realm) == 0; | |
| 526 }); | |
| 527 ASSERT_TRUE(it != login_prompt_observer_.handlers().end()); | |
| 528 seen_realms.insert((*it)->auth_info()->realm); | |
| 529 | 492 |
| 530 for_each_realm_func(*it); | 493 while (!observer.handlers().empty()) { |
| 494 WindowedAuthCancelledObserver auth_cancelled_waiter(controller); |
| 495 LoginHandler* handler = *observer.handlers().begin(); |
| 496 |
| 497 ASSERT_TRUE(handler); |
| 498 n_handlers++; |
| 499 handler->CancelAuth(); |
| 500 auth_cancelled_waiter.Wait(); |
| 501 } |
| 502 |
| 503 if (n_handlers < kMultiRealmTestRealmCount) |
| 504 auth_needed_waiter.Wait(); |
| 531 } | 505 } |
| 532 | 506 |
| 533 load_stop_waiter.Wait(); | 507 load_stop_waiter.Wait(); |
| 508 |
| 509 EXPECT_EQ(kMultiRealmTestRealmCount, n_handlers); |
| 510 EXPECT_EQ(0, observer.auth_supplied_count()); |
| 511 EXPECT_LT(0, observer.auth_needed_count()); |
| 512 EXPECT_LT(0, observer.auth_cancelled_count()); |
| 534 } | 513 } |
| 535 | 514 |
| 536 // Checks that cancelling works as expected. | 515 // Similar to the MultipleRealmCancellation test above, but tests |
| 537 IN_PROC_BROWSER_TEST_F(MultiRealmLoginPromptBrowserTest, | 516 // whether supplying credentials work as exepcted. |
| 538 MultipleRealmCancellation) { | 517 IN_PROC_BROWSER_TEST_F(LoginPromptBrowserTest, MultipleRealmConfirmation) { |
| 539 RunTest([this](LoginHandler* handler) { | 518 ASSERT_TRUE(embedded_test_server()->Start()); |
| 540 WindowedAuthCancelledObserver waiter(GetNavigationController()); | 519 GURL test_page = embedded_test_server()->GetURL(kMultiRealmTestPage); |
| 541 handler->CancelAuth(); | |
| 542 waiter.Wait(); | |
| 543 }); | |
| 544 | 520 |
| 545 EXPECT_EQ(0, login_prompt_observer()->auth_supplied_count()); | 521 content::WebContents* contents = |
| 546 EXPECT_LT(0, login_prompt_observer()->auth_needed_count()); | 522 browser()->tab_strip_model()->GetActiveWebContents(); |
| 547 EXPECT_LT(0, login_prompt_observer()->auth_cancelled_count()); | 523 NavigationController* controller = &contents->GetController(); |
| 548 } | 524 LoginPromptBrowserTestObserver observer; |
| 549 | 525 |
| 550 // Checks that supplying credentials works as expected. | 526 observer.Register(content::Source<NavigationController>(controller)); |
| 551 IN_PROC_BROWSER_TEST_F(MultiRealmLoginPromptBrowserTest, | |
| 552 MultipleRealmConfirmation) { | |
| 553 RunTest([this](LoginHandler* handler) { | |
| 554 WindowedAuthSuppliedObserver waiter(GetNavigationController()); | |
| 555 SetAuthFor(handler); | |
| 556 waiter.Wait(); | |
| 557 }); | |
| 558 | 527 |
| 559 EXPECT_LT(0, login_prompt_observer()->auth_needed_count()); | 528 WindowedLoadStopObserver load_stop_waiter(controller, 1); |
| 560 EXPECT_LT(0, login_prompt_observer()->auth_supplied_count()); | 529 int n_handlers = 0; |
| 561 EXPECT_EQ(0, login_prompt_observer()->auth_cancelled_count()); | 530 |
| 531 { |
| 532 WindowedAuthNeededObserver auth_needed_waiter(controller); |
| 533 |
| 534 browser()->OpenURL(OpenURLParams(test_page, Referrer(), |
| 535 WindowOpenDisposition::CURRENT_TAB, |
| 536 ui::PAGE_TRANSITION_TYPED, false)); |
| 537 auth_needed_waiter.Wait(); |
| 538 } |
| 539 |
| 540 while (n_handlers < kMultiRealmTestRealmCount) { |
| 541 WindowedAuthNeededObserver auth_needed_waiter(controller); |
| 542 |
| 543 while (!observer.handlers().empty()) { |
| 544 WindowedAuthSuppliedObserver auth_supplied_waiter(controller); |
| 545 LoginHandler* handler = *observer.handlers().begin(); |
| 546 |
| 547 ASSERT_TRUE(handler); |
| 548 n_handlers++; |
| 549 SetAuthFor(handler); |
| 550 auth_supplied_waiter.Wait(); |
| 551 } |
| 552 |
| 553 if (n_handlers < kMultiRealmTestRealmCount) |
| 554 auth_needed_waiter.Wait(); |
| 555 } |
| 556 |
| 557 load_stop_waiter.Wait(); |
| 558 |
| 559 EXPECT_EQ(kMultiRealmTestRealmCount, n_handlers); |
| 560 EXPECT_LT(0, observer.auth_needed_count()); |
| 561 EXPECT_LT(0, observer.auth_supplied_count()); |
| 562 EXPECT_EQ(0, observer.auth_cancelled_count()); |
| 562 } | 563 } |
| 563 | 564 |
| 564 // Testing for recovery from an incorrect password for the case where | 565 // Testing for recovery from an incorrect password for the case where |
| 565 // there are multiple authenticated resources. | 566 // there are multiple authenticated resources. |
| 566 IN_PROC_BROWSER_TEST_F(LoginPromptBrowserTest, IncorrectConfirmation) { | 567 IN_PROC_BROWSER_TEST_F(LoginPromptBrowserTest, IncorrectConfirmation) { |
| 567 ASSERT_TRUE(embedded_test_server()->Start()); | 568 ASSERT_TRUE(embedded_test_server()->Start()); |
| 568 GURL test_page = embedded_test_server()->GetURL(kSingleRealmTestPage); | 569 GURL test_page = embedded_test_server()->GetURL(kSingleRealmTestPage); |
| 569 | 570 |
| 570 content::WebContents* contents = | 571 content::WebContents* contents = |
| 571 browser()->tab_strip_model()->GetActiveWebContents(); | 572 browser()->tab_strip_model()->GetActiveWebContents(); |
| (...skipping 926 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1498 // out. | 1499 // out. |
| 1499 EXPECT_TRUE( | 1500 EXPECT_TRUE( |
| 1500 WaitForRenderFrameReady(contents->GetInterstitialPage()->GetMainFrame())); | 1501 WaitForRenderFrameReady(contents->GetInterstitialPage()->GetMainFrame())); |
| 1501 EXPECT_TRUE(contents->ShowingInterstitialPage()); | 1502 EXPECT_TRUE(contents->ShowingInterstitialPage()); |
| 1502 EXPECT_EQ(SSLBlockingPage::kTypeForTesting, contents->GetInterstitialPage() | 1503 EXPECT_EQ(SSLBlockingPage::kTypeForTesting, contents->GetInterstitialPage() |
| 1503 ->GetDelegateForTesting() | 1504 ->GetDelegateForTesting() |
| 1504 ->GetTypeForTesting()); | 1505 ->GetTypeForTesting()); |
| 1505 } | 1506 } |
| 1506 | 1507 |
| 1507 } // namespace | 1508 } // namespace |
| OLD | NEW |