OLD | NEW |
(Empty) | |
| 1 // Copyright 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 "base/run_loop.h" |
| 10 #include "base/time/time.h" |
| 11 #include "chrome/browser/captive_portal/captive_portal_service.h" |
| 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 14 #include "chrome/test/base/testing_profile.h" |
| 15 #include "components/captive_portal/captive_portal_testing_utils.h" |
| 16 #include "content/public/browser/notification_service.h" |
| 17 #include "net/base/net_errors.h" |
| 18 #include "net/ssl/ssl_info.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 |
| 21 class TestSSLErrorHandler : public SSLErrorHandler { |
| 22 public: |
| 23 TestSSLErrorHandler(Profile* profile, |
| 24 content::WebContents* web_contents, |
| 25 const net::SSLInfo& ssl_info) |
| 26 : SSLErrorHandler(web_contents, |
| 27 net::ERR_CERT_COMMON_NAME_INVALID, |
| 28 ssl_info, |
| 29 GURL(), |
| 30 0, |
| 31 base::Callback<void(bool)>()), |
| 32 profile_(profile), |
| 33 captive_portal_checked_(false), |
| 34 ssl_interstitial_shown_(false), |
| 35 captive_portal_interstitial_shown_(false) { |
| 36 } |
| 37 |
| 38 ~TestSSLErrorHandler() override { |
| 39 } |
| 40 |
| 41 using SSLErrorHandler::StartHandlingError; |
| 42 |
| 43 void SendCaptivePortalNotification( |
| 44 captive_portal::CaptivePortalResult result) { |
| 45 CaptivePortalService::Results results; |
| 46 results.previous_result = captive_portal::RESULT_INTERNET_CONNECTED; |
| 47 results.result = result; |
| 48 content::NotificationService::current()->Notify( |
| 49 chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT, |
| 50 content::Source<Profile>(profile_), |
| 51 content::Details<CaptivePortalService::Results>(&results)); |
| 52 } |
| 53 |
| 54 bool IsTimerRunning() const { |
| 55 return get_timer().IsRunning(); |
| 56 } |
| 57 |
| 58 int captive_portal_checked() const { |
| 59 return captive_portal_checked_; |
| 60 } |
| 61 |
| 62 int ssl_interstitial_shown() const { |
| 63 return ssl_interstitial_shown_; |
| 64 } |
| 65 |
| 66 int captive_portal_interstitial_shown() const { |
| 67 return captive_portal_interstitial_shown_; |
| 68 } |
| 69 |
| 70 void Reset() { |
| 71 captive_portal_checked_ = false; |
| 72 ssl_interstitial_shown_ = false; |
| 73 captive_portal_interstitial_shown_ = false; |
| 74 } |
| 75 |
| 76 private: |
| 77 void CheckForCaptivePortal() override { |
| 78 captive_portal_checked_ = true; |
| 79 } |
| 80 |
| 81 void ShowSSLInterstitial() override { |
| 82 ssl_interstitial_shown_ = true; |
| 83 } |
| 84 |
| 85 void ShowCaptivePortalInterstitial() override { |
| 86 captive_portal_interstitial_shown_ = true; |
| 87 } |
| 88 |
| 89 Profile* profile_; |
| 90 bool captive_portal_checked_; |
| 91 bool ssl_interstitial_shown_; |
| 92 bool captive_portal_interstitial_shown_; |
| 93 |
| 94 DISALLOW_COPY_AND_ASSIGN(TestSSLErrorHandler); |
| 95 }; |
| 96 |
| 97 class SSLErrorHandlerTest : public ChromeRenderViewHostTestHarness { |
| 98 public: |
| 99 void SetUp() override { |
| 100 ChromeRenderViewHostTestHarness::SetUp(); |
| 101 SSLErrorHandler::SetInterstitialDelayTypeForTest(SSLErrorHandler::NONE); |
| 102 error_handler_.reset(new TestSSLErrorHandler(profile(), |
| 103 web_contents(), |
| 104 ssl_info_)); |
| 105 } |
| 106 |
| 107 void TearDown() override { |
| 108 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 109 error_handler_.reset(NULL); |
| 110 ChromeRenderViewHostTestHarness::TearDown(); |
| 111 } |
| 112 |
| 113 TestSSLErrorHandler* error_handler() { return error_handler_.get(); } |
| 114 |
| 115 private: |
| 116 net::SSLInfo ssl_info_; |
| 117 scoped_ptr<TestSSLErrorHandler> error_handler_; |
| 118 }; |
| 119 |
| 120 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION) |
| 121 |
| 122 TEST_F(SSLErrorHandlerTest, |
| 123 ShouldShowSSLInterstitialOnTimerExpired) { |
| 124 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 125 error_handler()->StartHandlingError(); |
| 126 |
| 127 EXPECT_TRUE(error_handler()->IsTimerRunning()); |
| 128 EXPECT_TRUE(error_handler()->captive_portal_checked()); |
| 129 EXPECT_FALSE(error_handler()->ssl_interstitial_shown()); |
| 130 EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown()); |
| 131 |
| 132 error_handler()->Reset(); |
| 133 base::MessageLoop::current()->RunUntilIdle(); |
| 134 |
| 135 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 136 EXPECT_FALSE(error_handler()->captive_portal_checked()); |
| 137 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); |
| 138 EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown()); |
| 139 } |
| 140 |
| 141 TEST_F(SSLErrorHandlerTest, |
| 142 ShouldShowCustomInterstitialOnCaptivePortalResult) { |
| 143 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 144 error_handler()->StartHandlingError(); |
| 145 |
| 146 EXPECT_TRUE(error_handler()->IsTimerRunning()); |
| 147 EXPECT_TRUE(error_handler()->captive_portal_checked()); |
| 148 EXPECT_FALSE(error_handler()->ssl_interstitial_shown()); |
| 149 EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown()); |
| 150 // Fake a captive portal result. |
| 151 error_handler()->Reset(); |
| 152 error_handler()->SendCaptivePortalNotification( |
| 153 captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL); |
| 154 base::MessageLoop::current()->RunUntilIdle(); |
| 155 |
| 156 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 157 EXPECT_FALSE(error_handler()->captive_portal_checked()); |
| 158 EXPECT_FALSE(error_handler()->ssl_interstitial_shown()); |
| 159 EXPECT_TRUE(error_handler()->captive_portal_interstitial_shown()); |
| 160 } |
| 161 |
| 162 TEST_F(SSLErrorHandlerTest, |
| 163 ShouldShowSSLInterstitialOnNoCaptivePortalResult) { |
| 164 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 165 error_handler()->StartHandlingError(); |
| 166 |
| 167 EXPECT_TRUE(error_handler()->IsTimerRunning()); |
| 168 EXPECT_TRUE(error_handler()->captive_portal_checked()); |
| 169 EXPECT_FALSE(error_handler()->ssl_interstitial_shown()); |
| 170 EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown()); |
| 171 // Fake a "connected to internet" result for the captive portal check. |
| 172 // This should immediately trigger an SSL interstitial without waiting for |
| 173 // the timer to expire. |
| 174 error_handler()->Reset(); |
| 175 error_handler()->SendCaptivePortalNotification( |
| 176 captive_portal::RESULT_INTERNET_CONNECTED); |
| 177 base::MessageLoop::current()->RunUntilIdle(); |
| 178 |
| 179 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 180 EXPECT_FALSE(error_handler()->captive_portal_checked()); |
| 181 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); |
| 182 EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown()); |
| 183 } |
| 184 |
| 185 #else // #if !defined(ENABLE_CAPTIVE_PORTAL_DETECTION) |
| 186 |
| 187 TEST_F(SSLErrorHandlerTest, |
| 188 ShouldShowSSLInterstitialOnCaptivePortalDetectionDisabled) { |
| 189 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 190 error_handler()->StartHandlingError(); |
| 191 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 192 EXPECT_FALSE(error_handler()->captive_portal_checked()); |
| 193 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); |
| 194 EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown()); |
| 195 } |
| 196 |
| 197 #endif // defined(ENABLE_CAPTIVE_PORTAL_DETECTION) |
OLD | NEW |