Index: chrome/browser/ssl/ssl_error_handler_unittest.cc |
diff --git a/chrome/browser/ssl/ssl_error_handler_unittest.cc b/chrome/browser/ssl/ssl_error_handler_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9d5096473032df3e0e583960bb2a2c1c1b15fb11 |
--- /dev/null |
+++ b/chrome/browser/ssl/ssl_error_handler_unittest.cc |
@@ -0,0 +1,176 @@ |
+// Copyright 2014 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 "chrome/browser/ssl/ssl_error_handler.h" |
+ |
+#include "base/callback.h" |
+#include "base/command_line.h" |
+#include "base/message_loop/message_loop.h" |
+#include "base/run_loop.h" |
+#include "chrome/test/base/chrome_render_view_host_test_harness.h" |
+#include "content/public/common/content_switches.h" |
+#include "net/base/net_errors.h" |
+#include "net/ssl/ssl_info.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+class TestSSLErrorHandler : public SSLErrorHandler { |
+ public: |
+ TestSSLErrorHandler(content::WebContents* web_contents, |
+ const net::SSLInfo& ssl_info) |
+ : SSLErrorHandler(web_contents, |
+ net::ERR_CERT_COMMON_NAME_INVALID, |
+ ssl_info, |
+ GURL(), |
+ 0, |
+ base::TimeDelta(), |
+ base::Callback<void(bool)>()) { |
+ SSLErrorHandler::DontCreateInterstitials(); |
mmenke
2014/10/30 19:28:02
Any reason to prefer this over InterstitialPage::D
meacer
2014/11/06 21:21:57
Not needed anymore, removed.
|
+ } |
+ |
+ ~TestSSLErrorHandler() override { |
+ } |
+ |
+ bool TimerRunning() const { |
+ return SSLErrorHandler::TimerRunning(); |
+ } |
+ |
+ bool handled() const { |
+ return SSLErrorHandler::handled(); |
+ } |
+ |
+ int captive_portal_check_count() const { |
+ return SSLErrorHandler::captive_portal_check_count(); |
+ } |
+ |
+ int ssl_interstitial_show_count() const { |
+ return SSLErrorHandler::ssl_interstitial_show_count(); |
+ } |
+ |
+ int captive_portal_interstitial_show_count() const { |
+ return SSLErrorHandler::captive_portal_interstitial_show_count(); |
+ } |
Ryan Sleevi
2014/10/29 23:17:32
These aren't virtual methods you're overriding, so
meacer
2014/11/06 21:21:57
They are not overrides anymore since I moved the c
|
+ |
+ private: |
+ |
+ DISALLOW_COPY_AND_ASSIGN(TestSSLErrorHandler); |
Ryan Sleevi
2014/10/29 23:17:32
no newline
meacer
2014/11/06 21:21:57
Done.
|
+}; |
+ |
+class SSLErrorHandlerTest : public ChromeRenderViewHostTestHarness { |
+ public: |
+ virtual void SetUp() override { |
mmenke
2014/10/30 19:28:02
The new coolness is to use override without virtua
meacer
2014/11/06 21:21:57
Done. That change happened long after I wrote this
|
+ CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
+ command_line.AppendSwitch(::switches::kTestType); |
+ ChromeRenderViewHostTestHarness::SetUp(); |
+ ssl_info_.reset(new net::SSLInfo); |
+ error_handler_.reset(new TestSSLErrorHandler(web_contents(), |
+ *ssl_info_.get())); |
+ } |
+ |
+ virtual void TearDown() override { |
+ EXPECT_FALSE(error_handler().TimerRunning()); |
+ error_handler_.reset(NULL); |
+ ssl_info_.reset(NULL); |
+ ChromeRenderViewHostTestHarness::TearDown(); |
+ } |
+ |
+ TestSSLErrorHandler& error_handler() { return *error_handler_.get(); } |
+ |
+ private: |
+ scoped_ptr<net::SSLInfo> ssl_info_; |
+ scoped_ptr<TestSSLErrorHandler> error_handler_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(SSLErrorHandlerTest); |
+}; |
+ |
+#ifdef ENABLE_CAPTIVE_PORTAL_DETECTION |
Ryan Sleevi
2014/10/29 23:17:32
#if defined(...)
meacer
2014/11/06 21:21:56
Done.
|
+ |
+TEST_F(SSLErrorHandlerTest, |
+ ShouldShowSSLInterstitialOnTimerExpired) { |
+ EXPECT_FALSE(error_handler().TimerRunning()); |
+ error_handler().Handle(); |
+ |
+ EXPECT_TRUE(error_handler().TimerRunning()); |
+ EXPECT_FALSE(error_handler().handled()); |
+ EXPECT_EQ(1, error_handler().captive_portal_check_count()); |
+ EXPECT_EQ(0, error_handler().ssl_interstitial_show_count()); |
+ EXPECT_EQ(0, error_handler().captive_portal_interstitial_show_count()); |
+ |
+ base::MessageLoop::current()->RunUntilIdle(); |
+ |
+ EXPECT_FALSE(error_handler().TimerRunning()); |
+ EXPECT_TRUE(error_handler().handled()); |
+ EXPECT_EQ(1, error_handler().captive_portal_check_count()); |
+ EXPECT_EQ(1, error_handler().ssl_interstitial_show_count()); |
+ EXPECT_EQ(0, error_handler().captive_portal_interstitial_show_count()); |
+ |
+ // Any captive portal result after the timer expired should be ignored and |
+ // counts shouldn't change. |
+ error_handler().OnCaptivePortalResult(); |
+ EXPECT_EQ(1, error_handler().ssl_interstitial_show_count()); |
+ EXPECT_EQ(0, error_handler().captive_portal_interstitial_show_count()); |
+} |
+ |
+TEST_F(SSLErrorHandlerTest, |
+ ShouldShowCustomInterstitialOnCaptivePortalResult) { |
+ EXPECT_FALSE(error_handler().TimerRunning()); |
+ error_handler().Handle(); |
+ |
+ EXPECT_TRUE(error_handler().TimerRunning()); |
+ EXPECT_FALSE(error_handler().handled()); |
+ EXPECT_EQ(1, error_handler().captive_portal_check_count()); |
+ EXPECT_EQ(0, error_handler().ssl_interstitial_show_count()); |
+ EXPECT_EQ(0, error_handler().captive_portal_interstitial_show_count()); |
+ // Fake a captive portal result. |
+ error_handler().OnCaptivePortalResult(); |
+ base::MessageLoop::current()->RunUntilIdle(); |
+ |
+ EXPECT_FALSE(error_handler().TimerRunning()); |
+ EXPECT_TRUE(error_handler().handled()); |
+ EXPECT_EQ(1, error_handler().captive_portal_check_count()); |
+ EXPECT_EQ(0, error_handler().ssl_interstitial_show_count()); |
+ EXPECT_EQ(1, error_handler().captive_portal_interstitial_show_count()); |
+ |
+ // Any captive portal result after the timer expired should be ignored and |
+ // counts shouldn't change. |
+ error_handler().OnCaptivePortalResult(); |
+ EXPECT_EQ(0, error_handler().ssl_interstitial_show_count()); |
+ EXPECT_EQ(1, error_handler().captive_portal_interstitial_show_count()); |
+} |
+ |
+TEST_F(SSLErrorHandlerTest, |
+ ShouldShowCaptivePortalInterstitialOnTooEarlyCaptivePortalResult) { |
+ EXPECT_FALSE(error_handler().TimerRunning()); |
+ // A captive portal result arrives before error handler starts. |
+ error_handler().OnCaptivePortalResult(); |
+ |
+ EXPECT_FALSE(error_handler().TimerRunning()); |
+ EXPECT_TRUE(error_handler().handled()); |
+ EXPECT_EQ(0, error_handler().captive_portal_check_count()); |
+ EXPECT_EQ(0, error_handler().ssl_interstitial_show_count()); |
+ EXPECT_EQ(1, error_handler().captive_portal_interstitial_show_count()); |
+ |
+ error_handler().Handle(); |
+ |
+ // Once the captive portal warning is shown, the state shouldn't change later. |
+ // The timer shouldn't be fired as we already know captive portal status. |
+ EXPECT_FALSE(error_handler().TimerRunning()); |
+ EXPECT_TRUE(error_handler().handled()); |
+ EXPECT_EQ(0, error_handler().captive_portal_check_count()); |
+ EXPECT_EQ(0, error_handler().ssl_interstitial_show_count()); |
+ EXPECT_EQ(1, error_handler().captive_portal_interstitial_show_count()); |
+ base::MessageLoop::current()->RunUntilIdle(); |
+} |
+ |
+#else |
mmenke
2014/10/30 19:28:02
#else // if !defined(...)
meacer
2014/11/06 21:21:56
Done.
|
+ |
+TEST_F(SSLErrorHandlerTest, |
+ ShouldShowSSLInterstitialOnCaptivePortalDetectionDisabled) { |
+ EXPECT_FALSE(error_handler().TimerRunning()); |
+ error_handler().Handle(); |
+ EXPECT_EQ(0, error_handler().captive_portal_check_count()); |
+ EXPECT_EQ(1, error_handler().ssl_interstitial_show_count()); |
+ EXPECT_EQ(0, error_handler().captive_portal_interstitial_show_count()); |
+} |
+ |
+#endif |
mmenke
2014/10/30 19:28:02
#endif // defined(ENABLE_CAPTIVE_PORTAL_DETECTION
meacer
2014/11/06 21:21:57
Done.
|