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..6f0e6b1fc969e256cf913f926109db7184ddde8f |
--- /dev/null |
+++ b/chrome/browser/ssl/ssl_error_handler_unittest.cc |
@@ -0,0 +1,225 @@ |
+// 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/browser/captive_portal/captive_portal_service.h" |
+#include "chrome/browser/profiles/profile.h" |
+#include "chrome/test/base/chrome_render_view_host_test_harness.h" |
+#include "chrome/test/base/testing_profile.h" |
+#include "components/captive_portal/captive_portal_testing_utils.h" |
+#include "content/public/browser/notification_service.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(Profile* profile, |
+ 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)>()), |
+ profile_(profile), |
+ captive_portal_check_count_(0), |
+ ssl_interstitial_show_count_(0), |
+ captive_portal_interstitial_show_count_(0) { |
+ } |
+ |
+ ~TestSSLErrorHandler() override { |
+ } |
+ |
+ void Handle() { |
+ SSLErrorHandler::Handle(); |
+ } |
+ |
+ void SendCaptivePortalDetectedNotification() { |
+ CaptivePortalService::Results results; |
+ results.previous_result = captive_portal::RESULT_INTERNET_CONNECTED; |
+ results.result = captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL; |
+ content::NotificationService::current()->Notify( |
+ chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT, |
+ content::Source<Profile>(profile_), |
+ content::Details<CaptivePortalService::Results>(&results)); |
+ } |
+ |
+ bool IsTimerRunningForTests() const { |
+ return SSLErrorHandler::IsTimerRunningForTests(); |
+ } |
+ |
+ bool was_handled_for_testing() const { |
+ return SSLErrorHandler::was_handled_for_testing(); |
+ } |
+ |
+ int captive_portal_check_count() const { |
+ return captive_portal_check_count_; |
+ } |
+ |
+ int ssl_interstitial_show_count() const { |
+ return ssl_interstitial_show_count_; |
+ } |
+ |
+ int captive_portal_interstitial_show_count() const { |
+ return captive_portal_interstitial_show_count_; |
+ } |
+ |
+ void ResetCounts() { |
+ captive_portal_check_count_ = 0; |
+ ssl_interstitial_show_count_ = 0; |
+ captive_portal_interstitial_show_count_ = 0; |
+ } |
+ |
+ private: |
+ void CheckForCaptivePortal() override { |
+ captive_portal_check_count_++; |
+ } |
+ |
+ void ShowSSLInterstitial() override { |
+ ssl_interstitial_show_count_++; |
+ } |
+ |
+ void ShowCaptivePortalInterstitial() override { |
+ captive_portal_interstitial_show_count_++; |
+ } |
+ |
+ Profile* profile_; |
+ int captive_portal_check_count_; |
+ int ssl_interstitial_show_count_; |
+ int captive_portal_interstitial_show_count_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(TestSSLErrorHandler); |
+}; |
+ |
+class SSLErrorHandlerTest : public ChromeRenderViewHostTestHarness { |
+ public: |
+ void SetUp() override { |
+ CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
+ command_line.AppendSwitch(::switches::kTestType); |
+ ChromeRenderViewHostTestHarness::SetUp(); |
+ ssl_info_.reset(new net::SSLInfo); |
+ error_handler_.reset(new TestSSLErrorHandler(profile(), |
+ web_contents(), |
+ *ssl_info_.get())); |
+ } |
+ |
+ void TearDown() override { |
+ EXPECT_FALSE(error_handler().IsTimerRunningForTests()); |
+ 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_; |
+}; |
+ |
+#if defined(ENABLE_CAPTIVE_PORTAL_DETECTION) |
+ |
+TEST_F(SSLErrorHandlerTest, |
+ ShouldShowSSLInterstitialOnTimerExpired) { |
+ EXPECT_FALSE(error_handler().IsTimerRunningForTests()); |
+ error_handler().Handle(); |
+ |
+ EXPECT_TRUE(error_handler().IsTimerRunningForTests()); |
+ EXPECT_FALSE(error_handler().was_handled_for_testing()); |
+ 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()); |
+ |
+ error_handler().ResetCounts(); |
+ base::MessageLoop::current()->RunUntilIdle(); |
+ |
+ EXPECT_FALSE(error_handler().IsTimerRunningForTests()); |
+ EXPECT_TRUE(error_handler().was_handled_for_testing()); |
+ 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()); |
+ |
+ // Any captive portal result after the timer expired should be ignored and |
+ // counts shouldn't change. |
+ error_handler().ResetCounts(); |
+ error_handler().SendCaptivePortalDetectedNotification(); |
+ EXPECT_EQ(0, error_handler().ssl_interstitial_show_count()); |
+ EXPECT_EQ(0, error_handler().captive_portal_interstitial_show_count()); |
+} |
+ |
+TEST_F(SSLErrorHandlerTest, |
+ ShouldShowCustomInterstitialOnCaptivePortalResult) { |
+ EXPECT_FALSE(error_handler().IsTimerRunningForTests()); |
+ error_handler().Handle(); |
+ |
+ EXPECT_TRUE(error_handler().IsTimerRunningForTests()); |
+ EXPECT_FALSE(error_handler().was_handled_for_testing()); |
+ 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().ResetCounts(); |
+ error_handler().SendCaptivePortalDetectedNotification(); |
+ base::MessageLoop::current()->RunUntilIdle(); |
+ |
+ EXPECT_FALSE(error_handler().IsTimerRunningForTests()); |
+ EXPECT_TRUE(error_handler().was_handled_for_testing()); |
+ 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()); |
+ |
+ // Any captive portal result after the timer expired should be ignored and |
+ // counts shouldn't change. |
+ error_handler().ResetCounts(); |
+ error_handler().SendCaptivePortalDetectedNotification(); |
+ EXPECT_EQ(0, error_handler().ssl_interstitial_show_count()); |
+ EXPECT_EQ(0, error_handler().captive_portal_interstitial_show_count()); |
+} |
+ |
+TEST_F(SSLErrorHandlerTest, |
+ ShouldShowCaptivePortalInterstitialOnTooEarlyCaptivePortalResult) { |
+ EXPECT_FALSE(error_handler().IsTimerRunningForTests()); |
+ // A captive portal result arrives before error handler starts. |
+ error_handler().SendCaptivePortalDetectedNotification(); |
+ |
+ EXPECT_FALSE(error_handler().IsTimerRunningForTests()); |
+ EXPECT_TRUE(error_handler().was_handled_for_testing()); |
+ 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().ResetCounts(); |
+ 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().IsTimerRunningForTests()); |
+ EXPECT_TRUE(error_handler().was_handled_for_testing()); |
+ EXPECT_EQ(0, 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(); |
+} |
+ |
+#else // #if !defined(ENABLE_CAPTIVE_PORTAL_DETECTION) |
+ |
+TEST_F(SSLErrorHandlerTest, |
+ ShouldShowSSLInterstitialOnCaptivePortalDetectionDisabled) { |
+ EXPECT_FALSE(error_handler().IsTimerRunningForTests()); |
+ 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 // defined(ENABLE_CAPTIVE_PORTAL_DETECTION) |