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