Commit 799fcff0 authored by Evan Stade's avatar Evan Stade Committed by Commit Bot

[WebLayer] Add more SSL browser tests.

Bug: none
Change-Id: Iba8ede87362c80c739613cc748559ee1f1dc539e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1894315Reviewed-by: default avatarJohn Abd-El-Malek <jam@chromium.org>
Commit-Queue: Evan Stade <estade@chromium.org>
Cr-Commit-Position: refs/heads/master@{#713596}
parent 1b42c22a
......@@ -5,43 +5,61 @@
#include "weblayer/test/weblayer_browser_test.h"
#include "base/files/file_path.h"
#include "base/macros.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "weblayer/shell/browser/shell.h"
#include "weblayer/test/interstitial_utils.h"
#include "weblayer/test/test_navigation_observer.h"
#include "weblayer/test/weblayer_browser_test_utils.h"
namespace weblayer {
IN_PROC_BROWSER_TEST_F(WebLayerBrowserTest, SSLErrorMismatchedCertificate) {
net::EmbeddedTestServer https_server(net::EmbeddedTestServer::TYPE_HTTPS);
https_server.AddDefaultHandlers(
class SSLBrowserTest : public WebLayerBrowserTest {
public:
SSLBrowserTest() = default;
~SSLBrowserTest() override = default;
// WebLayerBrowserTest:
void PreRunTestOnMainThread() override {
WebLayerBrowserTest::PreRunTestOnMainThread();
https_server_ = std::make_unique<net::EmbeddedTestServer>(
net::EmbeddedTestServer::TYPE_HTTPS);
https_server_->AddDefaultHandlers(
base::FilePath(FILE_PATH_LITERAL("weblayer/test/data")));
net::EmbeddedTestServer https_server_mismatched(
https_server_mismatched_ = std::make_unique<net::EmbeddedTestServer>(
net::EmbeddedTestServer::TYPE_HTTPS);
https_server_mismatched.SetSSLConfig(
https_server_mismatched_->SetSSLConfig(
net::EmbeddedTestServer::CERT_MISMATCHED_NAME);
https_server_mismatched.AddDefaultHandlers(
https_server_mismatched_->AddDefaultHandlers(
base::FilePath(FILE_PATH_LITERAL("weblayer/test/data")));
ASSERT_TRUE(https_server.Start());
ASSERT_TRUE(https_server_mismatched.Start());
ASSERT_TRUE(https_server_->Start());
ASSERT_TRUE(https_server_mismatched_->Start());
}
// First navigate to an OK page.
GURL initial_url = https_server.GetURL("/simple_page.html");
ASSERT_EQ("127.0.0.1", initial_url.host());
void PostRunTestOnMainThread() override {
https_server_.reset();
https_server_mismatched_.reset();
WebLayerBrowserTest::PostRunTestOnMainThread();
}
NavigateAndWaitForCompletion(initial_url, shell());
void NavigateToOkPage() {
ASSERT_EQ("127.0.0.1", ok_url().host());
NavigateAndWaitForCompletion(ok_url(), shell());
EXPECT_FALSE(IsShowingSecurityInterstitial(shell()->tab()));
}
void NavigateToPageWithSslError() {
// Now do a navigation that should result in an SSL error.
GURL url_with_mismatched_cert =
https_server_mismatched.GetURL("/simple_page.html");
https_server_mismatched_->GetURL("/simple_page.html");
NavigateAndWaitForFailure(url_with_mismatched_cert, shell());
// First check that there *is* an interstitial.
EXPECT_TRUE(IsShowingSecurityInterstitial(shell()->tab()));
ASSERT_TRUE(IsShowingSecurityInterstitial(shell()->tab()));
// Now verify that the interstitial is in fact an SSL interstitial.
EXPECT_TRUE(IsShowingSSLInterstitial(shell()->tab()));
......@@ -49,13 +67,44 @@ IN_PROC_BROWSER_TEST_F(WebLayerBrowserTest, SSLErrorMismatchedCertificate) {
// TODO(blundell): Check the security state once security state is available
// via the public WebLayer API, following the example of //chrome's
// ssl_browsertest.cc's CheckAuthenticationBrokenState() function.
}
GURL ok_url() { return https_server_->GetURL("/simple_page.html"); }
protected:
std::unique_ptr<net::EmbeddedTestServer> https_server_;
std::unique_ptr<net::EmbeddedTestServer> https_server_mismatched_;
private:
DISALLOW_COPY_AND_ASSIGN(SSLBrowserTest);
};
// TODO(blundell): Bring up test of the "Take me back" functionality
// following the example of //chrome's ssl_browsertest.cc:1467, once that
// functionality is implemented in weblayer.
// Tests clicking "take me back" on the interstitial page.
IN_PROC_BROWSER_TEST_F(SSLBrowserTest, TakeMeBack) {
NavigateToOkPage();
NavigateToPageWithSslError();
// Click "Take me back".
TestNavigationObserver navigation_observer(
ok_url(), TestNavigationObserver::NavigationEvent::Completion, shell());
ExecuteScript(shell(), "window.certificateErrorPageController.dontProceed();",
false /*use_separate_isolate*/);
navigation_observer.Wait();
EXPECT_FALSE(IsShowingSSLInterstitial(shell()->tab()));
// Check that it's possible to navigate to a new page.
NavigateAndWaitForCompletion(initial_url, shell());
NavigateAndWaitForCompletion(https_server_->GetURL("/simple_page2.html"),
shell());
EXPECT_FALSE(IsShowingSecurityInterstitial(shell()->tab()));
}
// Tests navigating away from the interstitial page.
IN_PROC_BROWSER_TEST_F(SSLBrowserTest, NavigateAway) {
NavigateToOkPage();
NavigateToPageWithSslError();
NavigateAndWaitForCompletion(https_server_->GetURL("/simple_page2.html"),
shell());
EXPECT_FALSE(IsShowingSecurityInterstitial(shell()->tab()));
}
......
......@@ -94,6 +94,8 @@ test("weblayer_browsertests") {
"interstitial_utils.h",
"test_launcher_delegate_impl.cc",
"test_launcher_delegate_impl.h",
"test_navigation_observer.cc",
"test_navigation_observer.h",
"weblayer_browser_test.cc",
"weblayer_browser_test.h",
"weblayer_browser_test_test.cc",
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "weblayer/test/test_navigation_observer.h"
#include "base/test/bind_test_util.h"
#include "url/gurl.h"
#include "weblayer/public/navigation.h"
#include "weblayer/public/navigation_controller.h"
#include "weblayer/public/tab.h"
#include "weblayer/shell/browser/shell.h"
namespace weblayer {
TestNavigationObserver::TestNavigationObserver(const GURL& url,
NavigationEvent target_event,
Shell* shell)
: url_(url), target_event_(target_event), tab_(shell->tab()) {
tab_->GetNavigationController()->AddObserver(this);
}
TestNavigationObserver::~TestNavigationObserver() {
tab_->GetNavigationController()->RemoveObserver(this);
}
void TestNavigationObserver::NavigationCompleted(Navigation* navigation) {
if (navigation->GetURL() == url_)
observed_event_ = NavigationEvent::Completion;
CheckNavigationCompleted();
}
void TestNavigationObserver::NavigationFailed(Navigation* navigation) {
if (navigation->GetURL() == url_)
observed_event_ = NavigationEvent::Failure;
CheckNavigationCompleted();
}
void TestNavigationObserver::LoadStateChanged(bool is_loading,
bool to_different_document) {
done_loading_ = !is_loading;
CheckNavigationCompleted();
}
void TestNavigationObserver::CheckNavigationCompleted() {
if (done_loading_ && observed_event_ == target_event_)
run_loop_.Quit();
}
void TestNavigationObserver::Wait() {
run_loop_.Run();
}
} // namespace weblayer
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef WEBLAYER_TEST_TEST_NAVIGATION_OBSERVER_H_
#define WEBLAYER_TEST_TEST_NAVIGATION_OBSERVER_H_
#include "base/macros.h"
#include "base/optional.h"
#include "base/run_loop.h"
#include "url/gurl.h"
#include "weblayer/public/navigation_observer.h"
namespace weblayer {
class Shell;
class Tab;
// A helper that waits for a navigation to finish.
class TestNavigationObserver : public NavigationObserver {
public:
enum class NavigationEvent { Completion, Failure };
// Creates an instance that begins waiting for a Navigation within |shell| and
// to |url| to either complete or fail as per |target_event|.
TestNavigationObserver(const GURL& url,
NavigationEvent target_event,
Shell* shell);
~TestNavigationObserver() override;
// Spins a RunLoop until the requested type of navigation event is observed.
void Wait();
private:
// NavigationObserver implementation:
void NavigationCompleted(Navigation* navigation) override;
void NavigationFailed(Navigation* navigation) override;
void LoadStateChanged(bool is_loading, bool to_different_document) override;
void CheckNavigationCompleted();
const GURL url_;
base::Optional<NavigationEvent> observed_event_;
NavigationEvent target_event_;
Tab* tab_;
bool done_loading_ = false;
base::RunLoop run_loop_;
DISALLOW_COPY_AND_ASSIGN(TestNavigationObserver);
};
} // namespace weblayer
#endif // WEBLAYER_TEST_TEST_NAVIGATION_OBSERVER_H_
......@@ -4,98 +4,37 @@
#include "weblayer/test/weblayer_browser_test_utils.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind_test_util.h"
#include "url/gurl.h"
#include "weblayer/public/navigation.h"
#include "weblayer/public/navigation_controller.h"
#include "weblayer/public/navigation_observer.h"
#include "weblayer/public/tab.h"
#include "weblayer/shell/browser/shell.h"
#include "weblayer/test/test_navigation_observer.h"
namespace weblayer {
namespace {
// Runs |closure| once |url| is successfully navigated to.
class TestNavigationObserver : public NavigationObserver {
public:
enum class NavigationEventToObserve { Completion, Failure };
TestNavigationObserver(base::OnceClosure closure,
const GURL& url,
NavigationEventToObserve event,
Shell* shell)
: closure_(std::move(closure)),
url_(url),
event_(event),
tab_(shell->tab()) {
tab_->GetNavigationController()->AddObserver(this);
}
~TestNavigationObserver() override {
tab_->GetNavigationController()->RemoveObserver(this);
}
private:
// NavigationObserver implementation:
void NavigationCompleted(Navigation* navigation) override {
if (navigation->GetURL() == url_ &&
event_ == NavigationEventToObserve::Completion) {
navigation_complete_ = true;
CheckComplete();
}
}
void NavigationFailed(Navigation* navigation) override {
if (navigation->GetURL() == url_ &&
event_ == NavigationEventToObserve::Failure) {
std::move(closure_).Run();
}
}
void LoadStateChanged(bool is_loading, bool to_different_document) override {
done_loading_ = !is_loading;
CheckComplete();
}
void CheckComplete() {
if (done_loading_ && navigation_complete_)
std::move(closure_).Run();
}
base::OnceClosure closure_;
const GURL url_;
NavigationEventToObserve event_;
Tab* tab_;
bool done_loading_ = false;
bool navigation_complete_ = false;
};
// Navigates to |url| in |shell| and waits for |event| to occur.
void NavigateAndWaitForEvent(
const GURL& url,
void NavigateAndWaitForEvent(const GURL& url,
Shell* shell,
TestNavigationObserver::NavigationEventToObserve event) {
base::RunLoop run_loop;
TestNavigationObserver test_observer(run_loop.QuitClosure(), url, event,
shell);
TestNavigationObserver::NavigationEvent event) {
TestNavigationObserver test_observer(url, event, shell);
shell->tab()->GetNavigationController()->Navigate(url);
run_loop.Run();
test_observer.Wait();
}
} // namespace
void NavigateAndWaitForCompletion(const GURL& url, Shell* shell) {
NavigateAndWaitForEvent(
url, shell, TestNavigationObserver::NavigationEventToObserve::Completion);
NavigateAndWaitForEvent(url, shell,
TestNavigationObserver::NavigationEvent::Completion);
}
void NavigateAndWaitForFailure(const GURL& url, Shell* shell) {
NavigateAndWaitForEvent(
url, shell, TestNavigationObserver::NavigationEventToObserve::Failure);
NavigateAndWaitForEvent(url, shell,
TestNavigationObserver::NavigationEvent::Failure);
}
base::Value ExecuteScript(Shell* shell,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment