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/command_line.h" |
| 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/run_loop.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 "content/public/common/content_switches.h" |
| 18 #include "net/base/net_errors.h" |
| 19 #include "net/ssl/ssl_info.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 |
| 22 class TestSSLErrorHandler : public SSLErrorHandler { |
| 23 public: |
| 24 TestSSLErrorHandler(Profile* profile, |
| 25 content::WebContents* web_contents, |
| 26 const net::SSLInfo& ssl_info) |
| 27 : SSLErrorHandler(web_contents, |
| 28 net::ERR_CERT_COMMON_NAME_INVALID, |
| 29 ssl_info, |
| 30 GURL(), |
| 31 0, |
| 32 base::TimeDelta(), |
| 33 base::Callback<void(bool)>()), |
| 34 profile_(profile), |
| 35 captive_portal_check_count_(0), |
| 36 ssl_interstitial_show_count_(0), |
| 37 captive_portal_interstitial_show_count_(0) { |
| 38 } |
| 39 |
| 40 ~TestSSLErrorHandler() override { |
| 41 } |
| 42 |
| 43 void Handle() { |
| 44 SSLErrorHandler::Handle(); |
| 45 } |
| 46 |
| 47 void SendCaptivePortalDetectedNotification() { |
| 48 CaptivePortalService::Results results; |
| 49 results.previous_result = captive_portal::RESULT_INTERNET_CONNECTED; |
| 50 results.result = captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL; |
| 51 content::NotificationService::current()->Notify( |
| 52 chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT, |
| 53 content::Source<Profile>(profile_), |
| 54 content::Details<CaptivePortalService::Results>(&results)); |
| 55 } |
| 56 |
| 57 bool IsTimerRunningForTests() const { |
| 58 return SSLErrorHandler::IsTimerRunningForTests(); |
| 59 } |
| 60 |
| 61 bool was_handled_for_testing() const { |
| 62 return SSLErrorHandler::was_handled_for_testing(); |
| 63 } |
| 64 |
| 65 int captive_portal_check_count() const { |
| 66 return captive_portal_check_count_; |
| 67 } |
| 68 |
| 69 int ssl_interstitial_show_count() const { |
| 70 return ssl_interstitial_show_count_; |
| 71 } |
| 72 |
| 73 int captive_portal_interstitial_show_count() const { |
| 74 return captive_portal_interstitial_show_count_; |
| 75 } |
| 76 |
| 77 void ResetCounts() { |
| 78 captive_portal_check_count_ = 0; |
| 79 ssl_interstitial_show_count_ = 0; |
| 80 captive_portal_interstitial_show_count_ = 0; |
| 81 } |
| 82 |
| 83 private: |
| 84 void CheckForCaptivePortal() override { |
| 85 captive_portal_check_count_++; |
| 86 } |
| 87 |
| 88 void ShowSSLInterstitial() override { |
| 89 ssl_interstitial_show_count_++; |
| 90 } |
| 91 |
| 92 void ShowCaptivePortalInterstitial() override { |
| 93 captive_portal_interstitial_show_count_++; |
| 94 } |
| 95 |
| 96 Profile* profile_; |
| 97 int captive_portal_check_count_; |
| 98 int ssl_interstitial_show_count_; |
| 99 int captive_portal_interstitial_show_count_; |
| 100 |
| 101 DISALLOW_COPY_AND_ASSIGN(TestSSLErrorHandler); |
| 102 }; |
| 103 |
| 104 class SSLErrorHandlerTest : public ChromeRenderViewHostTestHarness { |
| 105 public: |
| 106 void SetUp() override { |
| 107 CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 108 command_line.AppendSwitch(::switches::kTestType); |
| 109 ChromeRenderViewHostTestHarness::SetUp(); |
| 110 ssl_info_.reset(new net::SSLInfo); |
| 111 error_handler_.reset(new TestSSLErrorHandler(profile(), |
| 112 web_contents(), |
| 113 *ssl_info_.get())); |
| 114 } |
| 115 |
| 116 void TearDown() override { |
| 117 EXPECT_FALSE(error_handler().IsTimerRunningForTests()); |
| 118 error_handler_.reset(NULL); |
| 119 ssl_info_.reset(NULL); |
| 120 ChromeRenderViewHostTestHarness::TearDown(); |
| 121 } |
| 122 |
| 123 TestSSLErrorHandler& error_handler() { return *error_handler_.get(); } |
| 124 |
| 125 private: |
| 126 scoped_ptr<net::SSLInfo> ssl_info_; |
| 127 scoped_ptr<TestSSLErrorHandler> error_handler_; |
| 128 }; |
| 129 |
| 130 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION) |
| 131 |
| 132 TEST_F(SSLErrorHandlerTest, |
| 133 ShouldShowSSLInterstitialOnTimerExpired) { |
| 134 EXPECT_FALSE(error_handler().IsTimerRunningForTests()); |
| 135 error_handler().Handle(); |
| 136 |
| 137 EXPECT_TRUE(error_handler().IsTimerRunningForTests()); |
| 138 EXPECT_FALSE(error_handler().was_handled_for_testing()); |
| 139 EXPECT_EQ(1, error_handler().captive_portal_check_count()); |
| 140 EXPECT_EQ(0, error_handler().ssl_interstitial_show_count()); |
| 141 EXPECT_EQ(0, error_handler().captive_portal_interstitial_show_count()); |
| 142 |
| 143 error_handler().ResetCounts(); |
| 144 base::MessageLoop::current()->RunUntilIdle(); |
| 145 |
| 146 EXPECT_FALSE(error_handler().IsTimerRunningForTests()); |
| 147 EXPECT_TRUE(error_handler().was_handled_for_testing()); |
| 148 EXPECT_EQ(0, error_handler().captive_portal_check_count()); |
| 149 EXPECT_EQ(1, error_handler().ssl_interstitial_show_count()); |
| 150 EXPECT_EQ(0, error_handler().captive_portal_interstitial_show_count()); |
| 151 |
| 152 // Any captive portal result after the timer expired should be ignored and |
| 153 // counts shouldn't change. |
| 154 error_handler().ResetCounts(); |
| 155 error_handler().SendCaptivePortalDetectedNotification(); |
| 156 EXPECT_EQ(0, error_handler().ssl_interstitial_show_count()); |
| 157 EXPECT_EQ(0, error_handler().captive_portal_interstitial_show_count()); |
| 158 } |
| 159 |
| 160 TEST_F(SSLErrorHandlerTest, |
| 161 ShouldShowCustomInterstitialOnCaptivePortalResult) { |
| 162 EXPECT_FALSE(error_handler().IsTimerRunningForTests()); |
| 163 error_handler().Handle(); |
| 164 |
| 165 EXPECT_TRUE(error_handler().IsTimerRunningForTests()); |
| 166 EXPECT_FALSE(error_handler().was_handled_for_testing()); |
| 167 EXPECT_EQ(1, error_handler().captive_portal_check_count()); |
| 168 EXPECT_EQ(0, error_handler().ssl_interstitial_show_count()); |
| 169 EXPECT_EQ(0, error_handler().captive_portal_interstitial_show_count()); |
| 170 // Fake a captive portal result. |
| 171 error_handler().ResetCounts(); |
| 172 error_handler().SendCaptivePortalDetectedNotification(); |
| 173 base::MessageLoop::current()->RunUntilIdle(); |
| 174 |
| 175 EXPECT_FALSE(error_handler().IsTimerRunningForTests()); |
| 176 EXPECT_TRUE(error_handler().was_handled_for_testing()); |
| 177 EXPECT_EQ(0, error_handler().captive_portal_check_count()); |
| 178 EXPECT_EQ(0, error_handler().ssl_interstitial_show_count()); |
| 179 EXPECT_EQ(1, error_handler().captive_portal_interstitial_show_count()); |
| 180 |
| 181 // Any captive portal result after the timer expired should be ignored and |
| 182 // counts shouldn't change. |
| 183 error_handler().ResetCounts(); |
| 184 error_handler().SendCaptivePortalDetectedNotification(); |
| 185 EXPECT_EQ(0, error_handler().ssl_interstitial_show_count()); |
| 186 EXPECT_EQ(0, error_handler().captive_portal_interstitial_show_count()); |
| 187 } |
| 188 |
| 189 TEST_F(SSLErrorHandlerTest, |
| 190 ShouldShowCaptivePortalInterstitialOnTooEarlyCaptivePortalResult) { |
| 191 EXPECT_FALSE(error_handler().IsTimerRunningForTests()); |
| 192 // A captive portal result arrives before error handler starts. |
| 193 error_handler().SendCaptivePortalDetectedNotification(); |
| 194 |
| 195 EXPECT_FALSE(error_handler().IsTimerRunningForTests()); |
| 196 EXPECT_TRUE(error_handler().was_handled_for_testing()); |
| 197 EXPECT_EQ(0, error_handler().captive_portal_check_count()); |
| 198 EXPECT_EQ(0, error_handler().ssl_interstitial_show_count()); |
| 199 EXPECT_EQ(1, error_handler().captive_portal_interstitial_show_count()); |
| 200 |
| 201 error_handler().ResetCounts(); |
| 202 error_handler().Handle(); |
| 203 |
| 204 // Once the captive portal warning is shown, the state shouldn't change later. |
| 205 // The timer shouldn't be fired as we already know captive portal status. |
| 206 EXPECT_FALSE(error_handler().IsTimerRunningForTests()); |
| 207 EXPECT_TRUE(error_handler().was_handled_for_testing()); |
| 208 EXPECT_EQ(0, error_handler().captive_portal_check_count()); |
| 209 EXPECT_EQ(0, error_handler().ssl_interstitial_show_count()); |
| 210 EXPECT_EQ(0, error_handler().captive_portal_interstitial_show_count()); |
| 211 base::MessageLoop::current()->RunUntilIdle(); |
| 212 } |
| 213 |
| 214 #else // #if !defined(ENABLE_CAPTIVE_PORTAL_DETECTION) |
| 215 |
| 216 TEST_F(SSLErrorHandlerTest, |
| 217 ShouldShowSSLInterstitialOnCaptivePortalDetectionDisabled) { |
| 218 EXPECT_FALSE(error_handler().IsTimerRunningForTests()); |
| 219 error_handler().Handle(); |
| 220 EXPECT_EQ(0, error_handler().captive_portal_check_count()); |
| 221 EXPECT_EQ(1, error_handler().ssl_interstitial_show_count()); |
| 222 EXPECT_EQ(0, error_handler().captive_portal_interstitial_show_count()); |
| 223 } |
| 224 |
| 225 #endif // defined(ENABLE_CAPTIVE_PORTAL_DETECTION) |
OLD | NEW |