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 new_url) { | |
meacer
2015/07/15 20:11:46
const GURL&
Bhanu Dev
2015/07/16 23:38:06
Done.
| |
64 CommonNameMismatchHandler::Results results; | |
65 results.result = result; | |
66 results.new_url = new_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 SetSuggestedUrl() { suggested_url_exists_ = true; } | |
meacer
2015/07/15 20:11:47
I suggest changing this to SetSuggestedUrlExists(b
Bhanu Dev
2015/07/16 23:38:06
Done.
| |
87 | |
88 bool GetSuggestedUrl(const GURL& request_url, | |
89 const std::vector<std::string>& dns_names, | |
90 GURL* suggested_url) override { | |
91 if (!suggested_url_exists_) | |
92 return false; | |
93 *suggested_url = GURL("www.example.com"); | |
94 return true; | |
95 } | |
96 | |
97 int suggested_url_checked() const { return suggested_url_checked_; } | |
meacer
2015/07/15 20:11:46
bool return type
Bhanu Dev
2015/07/16 23:38:06
Done.
| |
98 | |
99 int common_name_mismatch_interstitial_shown() const { | |
meacer
2015/07/15 20:11:46
bool return type
Bhanu Dev
2015/07/16 23:38:06
Done.
| |
100 return common_name_mismatch_interstitial_shown_; | |
101 } | |
102 | |
71 void Reset() { | 103 void Reset() { |
72 captive_portal_checked_ = false; | 104 captive_portal_checked_ = false; |
105 suggested_url_exists_ = false; | |
106 suggested_url_checked_ = false; | |
73 ssl_interstitial_shown_ = false; | 107 ssl_interstitial_shown_ = false; |
74 captive_portal_interstitial_shown_ = false; | 108 captive_portal_interstitial_shown_ = false; |
109 common_name_mismatch_interstitial_shown_ = false; | |
75 } | 110 } |
76 | 111 |
77 private: | 112 private: |
78 void CheckForCaptivePortal() override { | 113 void CheckForCaptivePortal() override { |
79 captive_portal_checked_ = true; | 114 captive_portal_checked_ = true; |
80 } | 115 } |
81 | 116 |
82 void ShowSSLInterstitial() override { | 117 void ShowSSLInterstitial(const GURL& suggested_url) override { |
83 ssl_interstitial_shown_ = true; | 118 if (!suggested_url.is_empty()) |
119 common_name_mismatch_interstitial_shown_ = true; | |
120 else | |
121 ssl_interstitial_shown_ = true; | |
84 } | 122 } |
85 | 123 |
86 void ShowCaptivePortalInterstitial(const GURL& landing_url) override { | 124 void ShowCaptivePortalInterstitial(const GURL& landing_url) override { |
87 captive_portal_interstitial_shown_ = true; | 125 captive_portal_interstitial_shown_ = true; |
88 } | 126 } |
89 | 127 |
128 void CheckSuggestedUrl(const GURL& suggested_url) override { | |
129 suggested_url_checked_ = true; | |
130 } | |
131 | |
90 Profile* profile_; | 132 Profile* profile_; |
91 bool captive_portal_checked_; | 133 bool captive_portal_checked_; |
134 bool suggested_url_exists_; | |
135 bool suggested_url_checked_; | |
meacer
2015/07/15 20:11:46
nit: Put these in opposite order here in other pla
Bhanu Dev
2015/07/16 23:38:06
If the suggested URL exists, we check for the vali
| |
92 bool ssl_interstitial_shown_; | 136 bool ssl_interstitial_shown_; |
93 bool captive_portal_interstitial_shown_; | 137 bool captive_portal_interstitial_shown_; |
138 bool common_name_mismatch_interstitial_shown_; | |
94 | 139 |
95 DISALLOW_COPY_AND_ASSIGN(TestSSLErrorHandler); | 140 DISALLOW_COPY_AND_ASSIGN(TestSSLErrorHandler); |
96 }; | 141 }; |
97 | 142 |
98 class SSLErrorHandlerTest : public ChromeRenderViewHostTestHarness { | 143 class SSLErrorHandlerTest : public ChromeRenderViewHostTestHarness { |
99 public: | 144 public: |
100 SSLErrorHandlerTest() | 145 SSLErrorHandlerTest() |
101 : field_trial_list_(NULL) { | 146 : field_trial_list_(NULL) { |
102 } | 147 } |
103 | 148 |
104 void SetUp() override { | 149 void SetUp() override { |
105 ChromeRenderViewHostTestHarness::SetUp(); | 150 ChromeRenderViewHostTestHarness::SetUp(); |
106 SSLErrorHandler::SetInterstitialDelayTypeForTest(SSLErrorHandler::NONE); | 151 SSLErrorHandler::SetInterstitialDelayTypeForTest(SSLErrorHandler::NONE); |
152 ssl_info_.cert = net::X509Certificate::CreateFromBytes( | |
153 reinterpret_cast<const char*>(google_der), sizeof(google_der)); | |
meacer
2015/07/15 20:11:46
Do you need an actual cert here? If not, you can c
Bhanu Dev
2015/07/16 23:38:06
|SSLErrorHandler| calls method like |GetDNSNames|
| |
107 error_handler_.reset(new TestSSLErrorHandler(profile(), | 154 error_handler_.reset(new TestSSLErrorHandler(profile(), |
108 web_contents(), | 155 web_contents(), |
109 ssl_info_)); | 156 ssl_info_)); |
110 // Enable finch experiment for captive portal interstitials. | 157 // Enable finch experiment for captive portal interstitials. |
111 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( | 158 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( |
112 "CaptivePortalInterstitial", "Enabled")); | 159 "CaptivePortalInterstitial", "Enabled")); |
113 } | 160 } |
114 | 161 |
115 void TearDown() override { | 162 void TearDown() override { |
116 EXPECT_FALSE(error_handler()->IsTimerRunning()); | 163 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
117 error_handler_.reset(NULL); | 164 error_handler_.reset(NULL); |
118 ChromeRenderViewHostTestHarness::TearDown(); | 165 ChromeRenderViewHostTestHarness::TearDown(); |
119 } | 166 } |
120 | 167 |
121 TestSSLErrorHandler* error_handler() { return error_handler_.get(); } | 168 TestSSLErrorHandler* error_handler() { return error_handler_.get(); } |
122 | 169 |
123 private: | 170 private: |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
184 error_handler()->SendCaptivePortalNotification( | 231 error_handler()->SendCaptivePortalNotification( |
185 captive_portal::RESULT_INTERNET_CONNECTED); | 232 captive_portal::RESULT_INTERNET_CONNECTED); |
186 base::MessageLoop::current()->RunUntilIdle(); | 233 base::MessageLoop::current()->RunUntilIdle(); |
187 | 234 |
188 EXPECT_FALSE(error_handler()->IsTimerRunning()); | 235 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
189 EXPECT_FALSE(error_handler()->captive_portal_checked()); | 236 EXPECT_FALSE(error_handler()->captive_portal_checked()); |
190 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); | 237 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); |
191 EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown()); | 238 EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown()); |
192 } | 239 } |
193 | 240 |
241 TEST_F(SSLErrorHandlerTest, | |
242 ShouldNotCheckSuggestedUrlIfGetSuggestedUrlIsFalse) { | |
meacer
2015/07/15 20:11:46
nit: name this ShouldNotCheckSuggestedUrlIfNoSugge
Bhanu Dev
2015/07/16 23:38:06
Done.
| |
243 EXPECT_FALSE(error_handler()->IsTimerRunning()); | |
244 error_handler()->StartHandlingError(); | |
meacer
2015/07/15 20:11:46
Looks like you are using |SetSuggestedURL| to enab
Bhanu Dev
2015/07/16 23:38:06
It is good to add a bool param to |SetSuggestedURL
| |
245 | |
246 EXPECT_TRUE(error_handler()->IsTimerRunning()); | |
247 EXPECT_FALSE(error_handler()->suggested_url_checked()); | |
248 base::MessageLoop::current()->RunUntilIdle(); | |
249 | |
250 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); | |
meacer
2015/07/15 20:11:46
EXPECT_FALSE(error_handler()->IsTimerRunning()); b
Bhanu Dev
2015/07/16 23:38:06
Done.
| |
251 } | |
252 | |
253 TEST_F(SSLErrorHandlerTest, | |
254 ShouldNotCheckForCaptivePortalIfSuggestedUrlExists) { | |
meacer
2015/07/15 20:11:46
nit: This name uses CheckFor while the previous on
Bhanu Dev
2015/07/16 23:38:06
Done.
| |
255 EXPECT_FALSE(error_handler()->IsTimerRunning()); | |
256 error_handler()->SetSuggestedUrl(); | |
257 error_handler()->StartHandlingError(); | |
258 | |
259 EXPECT_TRUE(error_handler()->IsTimerRunning()); | |
260 EXPECT_TRUE(error_handler()->suggested_url_checked()); | |
261 EXPECT_FALSE(error_handler()->captive_portal_checked()); | |
262 base::MessageLoop::current()->RunUntilIdle(); | |
263 | |
264 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); | |
meacer
2015/07/15 20:11:47
EXPECT_FALSE(error_handler()->IsTimerRunning()); b
Bhanu Dev
2015/07/16 23:38:06
Done.
| |
265 } | |
266 | |
194 #else // #if !defined(ENABLE_CAPTIVE_PORTAL_DETECTION) | 267 #else // #if !defined(ENABLE_CAPTIVE_PORTAL_DETECTION) |
195 | 268 |
196 TEST_F(SSLErrorHandlerTest, | 269 TEST_F(SSLErrorHandlerTest, |
197 ShouldShowSSLInterstitialOnCaptivePortalDetectionDisabled) { | 270 ShouldShowSSLInterstitialOnCaptivePortalDetectionDisabled) { |
198 EXPECT_FALSE(error_handler()->IsTimerRunning()); | 271 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
199 error_handler()->StartHandlingError(); | 272 error_handler()->StartHandlingError(); |
200 EXPECT_FALSE(error_handler()->IsTimerRunning()); | 273 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
201 EXPECT_FALSE(error_handler()->captive_portal_checked()); | 274 EXPECT_FALSE(error_handler()->captive_portal_checked()); |
202 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); | 275 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); |
203 EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown()); | 276 EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown()); |
204 } | 277 } |
205 | 278 |
206 #endif // defined(ENABLE_CAPTIVE_PORTAL_DETECTION) | 279 #endif // defined(ENABLE_CAPTIVE_PORTAL_DETECTION) |
280 | |
281 TEST_F(SSLErrorHandlerTest, | |
282 ShouldShowSSLInterstitialOnTimerExpiredWhenSuggestedUrlExists) { | |
283 EXPECT_FALSE(error_handler()->IsTimerRunning()); | |
284 error_handler()->SetSuggestedUrl(); | |
285 error_handler()->StartHandlingError(); | |
286 | |
287 EXPECT_TRUE(error_handler()->IsTimerRunning()); | |
288 EXPECT_TRUE(error_handler()->suggested_url_checked()); | |
289 EXPECT_FALSE(error_handler()->ssl_interstitial_shown()); | |
290 EXPECT_FALSE(error_handler()->common_name_mismatch_interstitial_shown()); | |
291 | |
292 base::MessageLoop::current()->RunUntilIdle(); | |
293 | |
294 EXPECT_FALSE(error_handler()->IsTimerRunning()); | |
295 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); | |
296 EXPECT_FALSE(error_handler()->common_name_mismatch_interstitial_shown()); | |
297 } | |
298 | |
299 TEST_F(SSLErrorHandlerTest, | |
300 ShouldShowCommonNameMismatchInterstitialOnSuggestedUrlCheckResult) { | |
301 EXPECT_FALSE(error_handler()->IsTimerRunning()); | |
302 error_handler()->SetSuggestedUrl(); | |
303 error_handler()->StartHandlingError(); | |
304 | |
305 EXPECT_TRUE(error_handler()->IsTimerRunning()); | |
306 EXPECT_TRUE(error_handler()->suggested_url_checked()); | |
307 EXPECT_FALSE(error_handler()->ssl_interstitial_shown()); | |
308 EXPECT_FALSE(error_handler()->common_name_mismatch_interstitial_shown()); | |
309 // Fake a Valid Suggested URL Check result. | |
meacer
2015/07/15 20:11:46
nit: don't capitalize "Valid Suggested URL Check"
Bhanu Dev
2015/07/16 23:38:06
Done.
| |
310 error_handler()->SendSuggestedUrlCheckResult( | |
311 CommonNameMismatchHandler::SuggestedUrlCheckResult:: | |
312 RESULT_SUGGESTED_URL_VALID, | |
313 GURL("https://random.example.com")); | |
meacer
2015/07/15 20:11:46
Why is this URL different than the return value of
meacer
2015/07/15 20:11:46
indent 4 more spaces
Bhanu Dev
2015/07/16 23:38:06
Done.
Bhanu Dev
2015/07/16 23:38:06
This URL is the new landing page and this can be d
Bhanu Dev
2015/07/16 23:38:06
I tried to indent it, but |git cl format| is rever
| |
314 | |
315 EXPECT_FALSE(error_handler()->IsTimerRunning()); | |
316 EXPECT_FALSE(error_handler()->ssl_interstitial_shown()); | |
317 EXPECT_TRUE(error_handler()->common_name_mismatch_interstitial_shown()); | |
318 } | |
319 | |
320 TEST_F(SSLErrorHandlerTest, ShouldShowSSLInterstitialOnInvalidUrlCheckResult) { | |
321 EXPECT_FALSE(error_handler()->IsTimerRunning()); | |
322 error_handler()->SetSuggestedUrl(); | |
323 error_handler()->StartHandlingError(); | |
324 | |
325 EXPECT_TRUE(error_handler()->IsTimerRunning()); | |
326 EXPECT_TRUE(error_handler()->suggested_url_checked()); | |
327 EXPECT_FALSE(error_handler()->ssl_interstitial_shown()); | |
328 EXPECT_FALSE(error_handler()->common_name_mismatch_interstitial_shown()); | |
329 // Fake an Invalid Suggested URL Check result. | |
330 error_handler()->SendSuggestedUrlCheckResult( | |
331 CommonNameMismatchHandler::SuggestedUrlCheckResult:: | |
332 RESULT_SUGGESTED_URL_INVALID, | |
333 GURL()); | |
meacer
2015/07/15 20:11:47
indent 4 more spaces
Bhanu Dev
2015/07/16 23:38:06
Done.
| |
334 | |
335 EXPECT_FALSE(error_handler()->IsTimerRunning()); | |
336 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); | |
337 EXPECT_FALSE(error_handler()->common_name_mismatch_interstitial_shown()); | |
338 } | |
OLD | NEW |