OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ssl/ssl_error_handler.h" |
| 6 |
| 7 #include "base/callback.h" |
| 8 #include "base/message_loop/message_loop.h" |
| 9 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 10 #include "net/ssl/ssl_info.h" |
| 11 #include "testing/gmock/include/gmock/gmock.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 |
| 15 class TestSSLErrorHandler : public SSLErrorHandler { |
| 16 public: |
| 17 TestSSLErrorHandler(content::WebContents* web_contents, |
| 18 const net::SSLInfo& ssl_info, |
| 19 bool overridable) |
| 20 : SSLErrorHandler(web_contents, |
| 21 -1, |
| 22 ssl_info, |
| 23 GURL(), |
| 24 overridable, |
| 25 false, |
| 26 base::Callback<void(bool)>()) { |
| 27 } |
| 28 |
| 29 virtual ~TestSSLErrorHandler() { |
| 30 } |
| 31 |
| 32 void set_ssl_interstitial_display_delay(base::TimeDelta delay) { |
| 33 EXPECT_FALSE(TimerRunning()); |
| 34 SSLErrorHandler::set_ssl_interstitial_display_delay(delay); |
| 35 } |
| 36 |
| 37 bool TimerRunning() { |
| 38 return timer_.IsRunning(); |
| 39 } |
| 40 |
| 41 MOCK_METHOD0(CheckForCaptivePortal, void()); |
| 42 MOCK_METHOD0(ShowCaptivePortalInterstitial, void()); |
| 43 MOCK_METHOD0(ShowSSLInterstitial, void()); |
| 44 private: |
| 45 DISALLOW_COPY_AND_ASSIGN(TestSSLErrorHandler); |
| 46 }; |
| 47 |
| 48 class SSLErrorHandlerTest : public ChromeRenderViewHostTestHarness { |
| 49 public: |
| 50 // testing::Test: |
| 51 virtual void SetUp() OVERRIDE { |
| 52 ChromeRenderViewHostTestHarness::SetUp(); |
| 53 ssl_info_.reset(new net::SSLInfo); |
| 54 error_handler_.reset( |
| 55 new testing::StrictMock<TestSSLErrorHandler>(web_contents(), |
| 56 *ssl_info_.get(), |
| 57 false)); |
| 58 error_handler_->set_ssl_interstitial_display_delay(base::TimeDelta()); |
| 59 } |
| 60 |
| 61 virtual void TearDown() OVERRIDE { |
| 62 EXPECT_FALSE(error_handler().TimerRunning()); |
| 63 error_handler_.reset(NULL); |
| 64 ssl_info_.reset(NULL); |
| 65 ChromeRenderViewHostTestHarness::TearDown(); |
| 66 } |
| 67 |
| 68 TestSSLErrorHandler& error_handler() { return *error_handler_.get(); } |
| 69 |
| 70 private: |
| 71 scoped_ptr<net::SSLInfo> ssl_info_; |
| 72 scoped_ptr<TestSSLErrorHandler> error_handler_; |
| 73 }; |
| 74 |
| 75 #ifdef ENABLE_CAPTIVE_PORTAL_DETECTION |
| 76 |
| 77 TEST_F(SSLErrorHandlerTest, |
| 78 ShouldShowSSLInterstitialOnTimerExpired) { |
| 79 EXPECT_FALSE(error_handler().TimerRunning()); |
| 80 EXPECT_CALL(error_handler(), CheckForCaptivePortal()).Times(1); |
| 81 error_handler().Handle(); |
| 82 |
| 83 EXPECT_TRUE(error_handler().TimerRunning()); |
| 84 EXPECT_FALSE(error_handler().handled()); |
| 85 EXPECT_CALL(error_handler(), ShowSSLInterstitial()).Times(1); |
| 86 EXPECT_CALL(error_handler(), ShowCaptivePortalInterstitial()).Times(0); |
| 87 base::MessageLoop::current()->RunUntilIdle(); |
| 88 |
| 89 EXPECT_FALSE(error_handler().TimerRunning()); |
| 90 EXPECT_TRUE(error_handler().handled()); |
| 91 |
| 92 // Any captive portal result after the timer expired should be ignored. |
| 93 error_handler().OnCaptivePortalResult(); |
| 94 EXPECT_CALL(error_handler(), ShowSSLInterstitial()).Times(0); |
| 95 EXPECT_CALL(error_handler(), ShowCaptivePortalInterstitial()).Times(0); |
| 96 } |
| 97 |
| 98 TEST_F(SSLErrorHandlerTest, |
| 99 ShouldShowCustomInterstitialOnCaptivePortalResult) { |
| 100 EXPECT_FALSE(error_handler().TimerRunning()); |
| 101 EXPECT_CALL(error_handler(), CheckForCaptivePortal()).Times(1); |
| 102 error_handler().Handle(); |
| 103 |
| 104 EXPECT_TRUE(error_handler().TimerRunning()); |
| 105 EXPECT_FALSE(error_handler().handled()); |
| 106 EXPECT_CALL(error_handler(), ShowSSLInterstitial()).Times(0); |
| 107 EXPECT_CALL(error_handler(), ShowCaptivePortalInterstitial()).Times(1); |
| 108 // Fake a captive portal result. |
| 109 error_handler().OnCaptivePortalResult(); |
| 110 base::MessageLoop::current()->RunUntilIdle(); |
| 111 |
| 112 EXPECT_FALSE(error_handler().TimerRunning()); |
| 113 EXPECT_TRUE(error_handler().handled()); |
| 114 |
| 115 // Any captive portal result after the timer expired should be ignored. |
| 116 error_handler().OnCaptivePortalResult(); |
| 117 EXPECT_CALL(error_handler(), ShowSSLInterstitial()).Times(0); |
| 118 EXPECT_CALL(error_handler(), ShowCaptivePortalInterstitial()).Times(0); |
| 119 } |
| 120 |
| 121 TEST_F(SSLErrorHandlerTest, |
| 122 ShouldShowSSLInterstitialOnTooEarlyCaptivePortalResult) { |
| 123 EXPECT_FALSE(error_handler().TimerRunning()); |
| 124 EXPECT_CALL(error_handler(), CheckForCaptivePortal()).Times(1); |
| 125 |
| 126 EXPECT_CALL(error_handler(), ShowSSLInterstitial()).Times(0); |
| 127 EXPECT_CALL(error_handler(), ShowCaptivePortalInterstitial()).Times(0); |
| 128 // A captive portal result arrives before error handler starts. |
| 129 error_handler().OnCaptivePortalResult(); |
| 130 |
| 131 EXPECT_FALSE(error_handler().handled()); |
| 132 EXPECT_FALSE(error_handler().TimerRunning()); |
| 133 |
| 134 error_handler().Handle(); |
| 135 |
| 136 EXPECT_TRUE(error_handler().TimerRunning()); |
| 137 EXPECT_FALSE(error_handler().handled()); |
| 138 EXPECT_CALL(error_handler(), ShowSSLInterstitial()).Times(1); |
| 139 EXPECT_CALL(error_handler(), ShowCaptivePortalInterstitial()).Times(0); |
| 140 base::MessageLoop::current()->RunUntilIdle(); |
| 141 } |
| 142 |
| 143 #else |
| 144 |
| 145 TEST_F(SSLErrorHandlerTest, |
| 146 ShouldShowSSLInterstitialOnCaptivePortalDetectionDisabled) { |
| 147 EXPECT_FALSE(error_handler().TimerRunning()); |
| 148 EXPECT_CALL(error_handler(), ShowSSLInterstitial()).Times(1); |
| 149 EXPECT_CALL(error_handler(), CheckForCaptivePortal()).Times(0); |
| 150 EXPECT_CALL(error_handler(), ShowCaptivePortalInterstitial()).Times(0); |
| 151 error_handler().Handle(); |
| 152 } |
| 153 |
| 154 #endif |
OLD | NEW |