OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ssl/ssl_error_handler.h" | 5 #include "chrome/browser/ssl/ssl_error_handler.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
12 #include "chrome/browser/captive_portal/captive_portal_service.h" | 12 #include "chrome/browser/captive_portal/captive_portal_service.h" |
13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/ssl/common_name_mismatch_handler.h" |
14 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 15 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
15 #include "chrome/test/base/testing_profile.h" | 16 #include "chrome/test/base/testing_profile.h" |
16 #include "components/captive_portal/captive_portal_testing_utils.h" | 17 #include "components/captive_portal/captive_portal_testing_utils.h" |
17 #include "content/public/browser/notification_service.h" | 18 #include "content/public/browser/notification_service.h" |
18 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
| 20 #include "net/cert/x509_certificate.h" |
19 #include "net/ssl/ssl_info.h" | 21 #include "net/ssl/ssl_info.h" |
| 22 #include "net/test/test_certificate_data.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
21 | 24 |
22 class TestSSLErrorHandler : public SSLErrorHandler { | 25 class TestSSLErrorHandler : public SSLErrorHandler { |
23 public: | 26 public: |
24 TestSSLErrorHandler(Profile* profile, | 27 TestSSLErrorHandler(Profile* profile, |
25 content::WebContents* web_contents, | 28 content::WebContents* web_contents, |
26 const net::SSLInfo& ssl_info) | 29 const net::SSLInfo& ssl_info) |
27 : SSLErrorHandler(web_contents, | 30 : SSLErrorHandler(web_contents, |
28 net::ERR_CERT_COMMON_NAME_INVALID, | 31 net::ERR_CERT_COMMON_NAME_INVALID, |
29 ssl_info, | 32 ssl_info, |
30 GURL(), | 33 GURL(), |
31 0, | 34 0, |
32 nullptr, | 35 nullptr, |
33 base::Callback<void(bool)>()), | 36 base::Callback<void(bool)>()), |
34 profile_(profile), | 37 profile_(profile), |
35 captive_portal_checked_(false), | 38 captive_portal_checked_(false), |
| 39 suggested_url_exists_(false), |
| 40 suggested_url_checked_(false), |
36 ssl_interstitial_shown_(false), | 41 ssl_interstitial_shown_(false), |
37 captive_portal_interstitial_shown_(false) {} | 42 captive_portal_interstitial_shown_(false), |
| 43 common_name_mismatch_interstitial_shown_(false) {} |
38 | 44 |
39 ~TestSSLErrorHandler() override { | 45 ~TestSSLErrorHandler() override { |
40 } | 46 } |
41 | 47 |
42 using SSLErrorHandler::StartHandlingError; | 48 using SSLErrorHandler::StartHandlingError; |
43 | 49 |
44 void SendCaptivePortalNotification( | 50 void SendCaptivePortalNotification( |
45 captive_portal::CaptivePortalResult result) { | 51 captive_portal::CaptivePortalResult result) { |
46 CaptivePortalService::Results results; | 52 CaptivePortalService::Results results; |
47 results.previous_result = captive_portal::RESULT_INTERNET_CONNECTED; | 53 results.previous_result = captive_portal::RESULT_INTERNET_CONNECTED; |
48 results.result = result; | 54 results.result = result; |
49 content::NotificationService::current()->Notify( | 55 content::NotificationService::current()->Notify( |
50 chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT, | 56 chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT, |
51 content::Source<Profile>(profile_), | 57 content::Source<Profile>(profile_), |
52 content::Details<CaptivePortalService::Results>(&results)); | 58 content::Details<CaptivePortalService::Results>(&results)); |
53 } | 59 } |
54 | 60 |
| 61 void SendSuggestedUrlCheckResult( |
| 62 const CommonNameMismatchHandler::SuggestedUrlCheckResult& result, |
| 63 const GURL& suggested_url) { |
| 64 CommonNameMismatchHandler::Results results; |
| 65 results.result = result; |
| 66 results.suggested_url = suggested_url; |
| 67 CommonNameMismatchHandlerCallback(results); |
| 68 } |
| 69 |
55 bool IsTimerRunning() const { | 70 bool IsTimerRunning() const { |
56 return get_timer().IsRunning(); | 71 return get_timer().IsRunning(); |
57 } | 72 } |
58 | 73 |
59 int captive_portal_checked() const { | 74 int captive_portal_checked() const { |
60 return captive_portal_checked_; | 75 return captive_portal_checked_; |
61 } | 76 } |
62 | 77 |
63 int ssl_interstitial_shown() const { | 78 int ssl_interstitial_shown() const { |
64 return ssl_interstitial_shown_; | 79 return ssl_interstitial_shown_; |
65 } | 80 } |
66 | 81 |
67 int captive_portal_interstitial_shown() const { | 82 int captive_portal_interstitial_shown() const { |
68 return captive_portal_interstitial_shown_; | 83 return captive_portal_interstitial_shown_; |
69 } | 84 } |
70 | 85 |
| 86 void SetSuggestedUrlExists(bool suggested_url_exists) { |
| 87 suggested_url_exists_ = suggested_url_exists; |
| 88 } |
| 89 |
| 90 bool GetSuggestedUrl(const std::vector<std::string>& dns_names, |
| 91 GURL* suggested_url) const override { |
| 92 if (!suggested_url_exists_) |
| 93 return false; |
| 94 *suggested_url = GURL("www.example.com"); |
| 95 return true; |
| 96 } |
| 97 |
| 98 bool suggested_url_checked() const { return suggested_url_checked_; } |
| 99 |
| 100 bool common_name_mismatch_interstitial_shown() const { |
| 101 return common_name_mismatch_interstitial_shown_; |
| 102 } |
| 103 |
71 void Reset() { | 104 void Reset() { |
72 captive_portal_checked_ = false; | 105 captive_portal_checked_ = false; |
| 106 suggested_url_exists_ = false; |
| 107 suggested_url_checked_ = false; |
73 ssl_interstitial_shown_ = false; | 108 ssl_interstitial_shown_ = false; |
74 captive_portal_interstitial_shown_ = false; | 109 captive_portal_interstitial_shown_ = false; |
| 110 common_name_mismatch_interstitial_shown_ = false; |
75 } | 111 } |
76 | 112 |
77 private: | 113 private: |
78 void CheckForCaptivePortal() override { | 114 void CheckForCaptivePortal() override { |
79 captive_portal_checked_ = true; | 115 captive_portal_checked_ = true; |
80 } | 116 } |
81 | 117 |
82 void ShowSSLInterstitial() override { | 118 void ShowSSLInterstitial(const GURL& suggested_url) override { |
83 ssl_interstitial_shown_ = true; | 119 if (!suggested_url.is_empty()) |
| 120 common_name_mismatch_interstitial_shown_ = true; |
| 121 else |
| 122 ssl_interstitial_shown_ = true; |
84 } | 123 } |
85 | 124 |
86 void ShowCaptivePortalInterstitial(const GURL& landing_url) override { | 125 void ShowCaptivePortalInterstitial(const GURL& landing_url) override { |
87 captive_portal_interstitial_shown_ = true; | 126 captive_portal_interstitial_shown_ = true; |
88 } | 127 } |
89 | 128 |
| 129 void CheckSuggestedUrl(const GURL& suggested_url) override { |
| 130 suggested_url_checked_ = true; |
| 131 } |
| 132 |
90 Profile* profile_; | 133 Profile* profile_; |
91 bool captive_portal_checked_; | 134 bool captive_portal_checked_; |
| 135 bool suggested_url_exists_; |
| 136 bool suggested_url_checked_; |
92 bool ssl_interstitial_shown_; | 137 bool ssl_interstitial_shown_; |
93 bool captive_portal_interstitial_shown_; | 138 bool captive_portal_interstitial_shown_; |
| 139 bool common_name_mismatch_interstitial_shown_; |
94 | 140 |
95 DISALLOW_COPY_AND_ASSIGN(TestSSLErrorHandler); | 141 DISALLOW_COPY_AND_ASSIGN(TestSSLErrorHandler); |
96 }; | 142 }; |
97 | 143 |
98 class SSLErrorHandlerTest : public ChromeRenderViewHostTestHarness { | 144 class SSLErrorHandlerTest : public ChromeRenderViewHostTestHarness { |
99 public: | 145 public: |
100 SSLErrorHandlerTest() | 146 SSLErrorHandlerTest() |
101 : field_trial_list_(NULL) { | 147 : field_trial_list_(NULL) { |
102 } | 148 } |
103 | 149 |
104 void SetUp() override { | 150 void SetUp() override { |
105 ChromeRenderViewHostTestHarness::SetUp(); | 151 ChromeRenderViewHostTestHarness::SetUp(); |
106 SSLErrorHandler::SetInterstitialDelayTypeForTest(SSLErrorHandler::NONE); | 152 SSLErrorHandler::SetInterstitialDelayTypeForTest(SSLErrorHandler::NONE); |
| 153 ssl_info_.cert = net::X509Certificate::CreateFromBytes( |
| 154 reinterpret_cast<const char*>(google_der), sizeof(google_der)); |
| 155 ssl_info_.cert_status = net::CERT_STATUS_COMMON_NAME_INVALID; |
107 error_handler_.reset(new TestSSLErrorHandler(profile(), | 156 error_handler_.reset(new TestSSLErrorHandler(profile(), |
108 web_contents(), | 157 web_contents(), |
109 ssl_info_)); | 158 ssl_info_)); |
110 // Enable finch experiment for captive portal interstitials. | 159 // Enable finch experiment for captive portal interstitials. |
111 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( | 160 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( |
112 "CaptivePortalInterstitial", "Enabled")); | 161 "CaptivePortalInterstitial", "Enabled")); |
113 } | 162 } |
114 | 163 |
115 void TearDown() override { | 164 void TearDown() override { |
116 EXPECT_FALSE(error_handler()->IsTimerRunning()); | 165 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
117 error_handler_.reset(NULL); | 166 error_handler_.reset(NULL); |
118 ChromeRenderViewHostTestHarness::TearDown(); | 167 ChromeRenderViewHostTestHarness::TearDown(); |
119 } | 168 } |
120 | 169 |
121 TestSSLErrorHandler* error_handler() { return error_handler_.get(); } | 170 TestSSLErrorHandler* error_handler() { return error_handler_.get(); } |
122 | 171 |
123 private: | 172 private: |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 error_handler()->SendCaptivePortalNotification( | 233 error_handler()->SendCaptivePortalNotification( |
185 captive_portal::RESULT_INTERNET_CONNECTED); | 234 captive_portal::RESULT_INTERNET_CONNECTED); |
186 base::MessageLoop::current()->RunUntilIdle(); | 235 base::MessageLoop::current()->RunUntilIdle(); |
187 | 236 |
188 EXPECT_FALSE(error_handler()->IsTimerRunning()); | 237 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
189 EXPECT_FALSE(error_handler()->captive_portal_checked()); | 238 EXPECT_FALSE(error_handler()->captive_portal_checked()); |
190 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); | 239 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); |
191 EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown()); | 240 EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown()); |
192 } | 241 } |
193 | 242 |
| 243 TEST_F(SSLErrorHandlerTest, ShouldNotCheckSuggestedUrlIfNoSuggestedUrl) { |
| 244 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 245 error_handler()->SetSuggestedUrlExists(false); |
| 246 error_handler()->StartHandlingError(); |
| 247 |
| 248 EXPECT_TRUE(error_handler()->IsTimerRunning()); |
| 249 EXPECT_FALSE(error_handler()->suggested_url_checked()); |
| 250 base::MessageLoop::current()->RunUntilIdle(); |
| 251 |
| 252 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 253 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); |
| 254 } |
| 255 |
| 256 TEST_F(SSLErrorHandlerTest, ShouldNotCheckCaptivePortalIfSuggestedUrlExists) { |
| 257 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 258 error_handler()->SetSuggestedUrlExists(true); |
| 259 error_handler()->StartHandlingError(); |
| 260 |
| 261 EXPECT_TRUE(error_handler()->IsTimerRunning()); |
| 262 EXPECT_TRUE(error_handler()->suggested_url_checked()); |
| 263 EXPECT_FALSE(error_handler()->captive_portal_checked()); |
| 264 base::MessageLoop::current()->RunUntilIdle(); |
| 265 |
| 266 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 267 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); |
| 268 } |
| 269 |
194 #else // #if !defined(ENABLE_CAPTIVE_PORTAL_DETECTION) | 270 #else // #if !defined(ENABLE_CAPTIVE_PORTAL_DETECTION) |
195 | 271 |
196 TEST_F(SSLErrorHandlerTest, | 272 TEST_F(SSLErrorHandlerTest, |
197 ShouldShowSSLInterstitialOnCaptivePortalDetectionDisabled) { | 273 ShouldShowSSLInterstitialOnCaptivePortalDetectionDisabled) { |
198 EXPECT_FALSE(error_handler()->IsTimerRunning()); | 274 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 275 error_handler()->SetSuggestedUrlExists(false); |
199 error_handler()->StartHandlingError(); | 276 error_handler()->StartHandlingError(); |
200 EXPECT_FALSE(error_handler()->IsTimerRunning()); | 277 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
201 EXPECT_FALSE(error_handler()->captive_portal_checked()); | 278 EXPECT_FALSE(error_handler()->captive_portal_checked()); |
202 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); | 279 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); |
203 EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown()); | 280 EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown()); |
204 } | 281 } |
205 | 282 |
206 #endif // defined(ENABLE_CAPTIVE_PORTAL_DETECTION) | 283 #endif // defined(ENABLE_CAPTIVE_PORTAL_DETECTION) |
| 284 |
| 285 TEST_F(SSLErrorHandlerTest, |
| 286 ShouldShowSSLInterstitialOnTimerExpiredWhenSuggestedUrlExists) { |
| 287 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 288 error_handler()->SetSuggestedUrlExists(true); |
| 289 error_handler()->StartHandlingError(); |
| 290 |
| 291 EXPECT_TRUE(error_handler()->IsTimerRunning()); |
| 292 EXPECT_TRUE(error_handler()->suggested_url_checked()); |
| 293 EXPECT_FALSE(error_handler()->ssl_interstitial_shown()); |
| 294 EXPECT_FALSE(error_handler()->common_name_mismatch_interstitial_shown()); |
| 295 |
| 296 base::MessageLoop::current()->RunUntilIdle(); |
| 297 |
| 298 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 299 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); |
| 300 EXPECT_FALSE(error_handler()->common_name_mismatch_interstitial_shown()); |
| 301 } |
| 302 |
| 303 TEST_F(SSLErrorHandlerTest, |
| 304 ShouldShowCommonNameMismatchInterstitialOnSuggestedUrlCheckResult) { |
| 305 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 306 error_handler()->SetSuggestedUrlExists(true); |
| 307 error_handler()->StartHandlingError(); |
| 308 |
| 309 EXPECT_TRUE(error_handler()->IsTimerRunning()); |
| 310 EXPECT_TRUE(error_handler()->suggested_url_checked()); |
| 311 EXPECT_FALSE(error_handler()->ssl_interstitial_shown()); |
| 312 EXPECT_FALSE(error_handler()->common_name_mismatch_interstitial_shown()); |
| 313 // Fake a valid suggested URL check result. |
| 314 // The URL returned by |SuggestedUrlCheckResult| can be different from |
| 315 // |suggested_url|, if there is a redirect. |
| 316 error_handler()->SendSuggestedUrlCheckResult( |
| 317 CommonNameMismatchHandler::SuggestedUrlCheckResult:: |
| 318 SUGGESTED_URL_AVAILABLE, |
| 319 GURL("https://random.example.com")); |
| 320 |
| 321 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 322 EXPECT_FALSE(error_handler()->ssl_interstitial_shown()); |
| 323 EXPECT_TRUE(error_handler()->common_name_mismatch_interstitial_shown()); |
| 324 } |
| 325 |
| 326 TEST_F(SSLErrorHandlerTest, ShouldShowSSLInterstitialOnInvalidUrlCheckResult) { |
| 327 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 328 error_handler()->SetSuggestedUrlExists(true); |
| 329 error_handler()->StartHandlingError(); |
| 330 |
| 331 EXPECT_TRUE(error_handler()->IsTimerRunning()); |
| 332 EXPECT_TRUE(error_handler()->suggested_url_checked()); |
| 333 EXPECT_FALSE(error_handler()->ssl_interstitial_shown()); |
| 334 EXPECT_FALSE(error_handler()->common_name_mismatch_interstitial_shown()); |
| 335 // Fake an Invalid Suggested URL Check result. |
| 336 error_handler()->SendSuggestedUrlCheckResult( |
| 337 CommonNameMismatchHandler::SuggestedUrlCheckResult:: |
| 338 SUGGESTED_URL_NOT_AVAILABLE, |
| 339 GURL()); |
| 340 |
| 341 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 342 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); |
| 343 EXPECT_FALSE(error_handler()->common_name_mismatch_interstitial_shown()); |
| 344 } |
OLD | NEW |