Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(789)

Side by Side Diff: chrome/browser/ssl/ssl_error_handler_unittest.cc

Issue 1223233002: Common Name Mismatch Handler For WWW Subdomain Mismatch case (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Documentation Changes Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/base/test_data_directory.h"
21 #include "net/cert/x509_certificate.h"
19 #include "net/ssl/ssl_info.h" 22 #include "net/ssl/ssl_info.h"
20 #include "testing/gtest/include/gtest/gtest.h" 23 #include "net/test/cert_test_util.h"
24 #include "net/test/test_certificate_data.h"
21 25
22 class TestSSLErrorHandler : public SSLErrorHandler { 26 class TestSSLErrorHandler : public SSLErrorHandler {
23 public: 27 public:
24 TestSSLErrorHandler(Profile* profile, 28 TestSSLErrorHandler(Profile* profile,
25 content::WebContents* web_contents, 29 content::WebContents* web_contents,
26 const net::SSLInfo& ssl_info) 30 const net::SSLInfo& ssl_info)
27 : SSLErrorHandler(web_contents, 31 : SSLErrorHandler(web_contents,
28 net::ERR_CERT_COMMON_NAME_INVALID, 32 net::ERR_CERT_COMMON_NAME_INVALID,
29 ssl_info, 33 ssl_info,
30 GURL(), 34 GURL(),
31 0, 35 0,
32 nullptr, 36 nullptr,
33 base::Callback<void(bool)>()), 37 base::Callback<void(bool)>()),
34 profile_(profile), 38 profile_(profile),
35 captive_portal_checked_(false), 39 captive_portal_checked_(false),
40 suggested_url_exists_(false),
41 suggested_url_checked_(false),
36 ssl_interstitial_shown_(false), 42 ssl_interstitial_shown_(false),
37 captive_portal_interstitial_shown_(false) {} 43 captive_portal_interstitial_shown_(false),
44 common_name_mismatch_redirect_(false) {}
38 45
39 ~TestSSLErrorHandler() override { 46 ~TestSSLErrorHandler() override {
40 } 47 }
41 48
42 using SSLErrorHandler::StartHandlingError; 49 using SSLErrorHandler::StartHandlingError;
43 50
44 void SendCaptivePortalNotification( 51 void SendCaptivePortalNotification(
45 captive_portal::CaptivePortalResult result) { 52 captive_portal::CaptivePortalResult result) {
46 CaptivePortalService::Results results; 53 CaptivePortalService::Results results;
47 results.previous_result = captive_portal::RESULT_INTERNET_CONNECTED; 54 results.previous_result = captive_portal::RESULT_INTERNET_CONNECTED;
48 results.result = result; 55 results.result = result;
49 content::NotificationService::current()->Notify( 56 content::NotificationService::current()->Notify(
50 chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT, 57 chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT,
51 content::Source<Profile>(profile_), 58 content::Source<Profile>(profile_),
52 content::Details<CaptivePortalService::Results>(&results)); 59 content::Details<CaptivePortalService::Results>(&results));
53 } 60 }
54 61
62 void SendSuggestedUrlCheckResult(
63 const CommonNameMismatchHandler::SuggestedUrlCheckResult& result,
64 const GURL& suggested_url) {
65 CommonNameMismatchHandler::Results results;
66 results.result = result;
67 results.suggested_url = suggested_url;
68 CommonNameMismatchHandlerCallback(results);
69 }
70
55 bool IsTimerRunning() const { 71 bool IsTimerRunning() const {
56 return get_timer().IsRunning(); 72 return get_timer().IsRunning();
57 } 73 }
58 74
59 int captive_portal_checked() const { 75 int captive_portal_checked() const {
60 return captive_portal_checked_; 76 return captive_portal_checked_;
61 } 77 }
62 78
63 int ssl_interstitial_shown() const { 79 int ssl_interstitial_shown() const {
64 return ssl_interstitial_shown_; 80 return ssl_interstitial_shown_;
65 } 81 }
66 82
67 int captive_portal_interstitial_shown() const { 83 int captive_portal_interstitial_shown() const {
68 return captive_portal_interstitial_shown_; 84 return captive_portal_interstitial_shown_;
69 } 85 }
70 86
87 void SetSuggestedUrlExists(bool suggested_url_exists) {
88 suggested_url_exists_ = suggested_url_exists;
89 }
90
91 bool GetSuggestedUrl(const std::vector<std::string>& dns_names,
92 GURL* suggested_url) const override {
93 if (!suggested_url_exists_)
94 return false;
95 *suggested_url = GURL("www.example.com");
96 return true;
97 }
98
99 bool suggested_url_checked() const { return suggested_url_checked_; }
100
101 bool common_name_mismatch_redirect() const {
102 return common_name_mismatch_redirect_;
103 }
104
71 void Reset() { 105 void Reset() {
72 captive_portal_checked_ = false; 106 captive_portal_checked_ = false;
107 suggested_url_exists_ = false;
108 suggested_url_checked_ = false;
73 ssl_interstitial_shown_ = false; 109 ssl_interstitial_shown_ = false;
74 captive_portal_interstitial_shown_ = false; 110 captive_portal_interstitial_shown_ = false;
111 common_name_mismatch_redirect_ = false;
75 } 112 }
76 113
77 private: 114 private:
78 void CheckForCaptivePortal() override { 115 void CheckForCaptivePortal() override {
79 captive_portal_checked_ = true; 116 captive_portal_checked_ = true;
80 } 117 }
81 118
82 void ShowSSLInterstitial() override { 119 void ShowSSLInterstitial() override { ssl_interstitial_shown_ = true; }
83 ssl_interstitial_shown_ = true;
84 }
85 120
86 void ShowCaptivePortalInterstitial(const GURL& landing_url) override { 121 void ShowCaptivePortalInterstitial(const GURL& landing_url) override {
87 captive_portal_interstitial_shown_ = true; 122 captive_portal_interstitial_shown_ = true;
88 } 123 }
89 124
125 void CheckSuggestedUrl(const GURL& suggested_url) override {
126 suggested_url_checked_ = true;
127 }
128
129 void NavigateToSuggestedURL(const GURL& suggested_url) override {
130 common_name_mismatch_redirect_ = 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_redirect_;
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 =
154 net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem");
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
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();
Ryan Sleevi 2015/08/14 00:40:17 Hrm, I missed that existing code calls this, but b
Bhanu Dev 2015/08/15 00:18:54 Done.
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());
Ryan Sleevi 2015/08/14 00:40:17 Why have this call? This is guaranteed by the test
Bhanu Dev 2015/08/15 00:18:54 Done. Yes, timer would not be running when SSLErro
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_redirect());
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_redirect());
301 }
302
303 TEST_F(SSLErrorHandlerTest, ShouldRedirectOnSuggestedUrlCheckResult) {
304 EXPECT_FALSE(error_handler()->IsTimerRunning());
305 error_handler()->SetSuggestedUrlExists(true);
306 error_handler()->StartHandlingError();
307
308 EXPECT_TRUE(error_handler()->IsTimerRunning());
309 EXPECT_TRUE(error_handler()->suggested_url_checked());
310 EXPECT_FALSE(error_handler()->ssl_interstitial_shown());
311 EXPECT_FALSE(error_handler()->common_name_mismatch_redirect());
312 // Fake a valid suggested URL check result.
313 // The URL returned by |SuggestedUrlCheckResult| can be different from
314 // |suggested_url|, if there is a redirect.
315 error_handler()->SendSuggestedUrlCheckResult(
316 CommonNameMismatchHandler::SuggestedUrlCheckResult::
317 SUGGESTED_URL_AVAILABLE,
318 GURL("https://random.example.com"));
319
320 EXPECT_FALSE(error_handler()->IsTimerRunning());
321 EXPECT_FALSE(error_handler()->ssl_interstitial_shown());
322 EXPECT_TRUE(error_handler()->common_name_mismatch_redirect());
323 }
324
325 TEST_F(SSLErrorHandlerTest, ShouldShowSSLInterstitialOnInvalidUrlCheckResult) {
326 EXPECT_FALSE(error_handler()->IsTimerRunning());
327 error_handler()->SetSuggestedUrlExists(true);
328 error_handler()->StartHandlingError();
329
330 EXPECT_TRUE(error_handler()->IsTimerRunning());
331 EXPECT_TRUE(error_handler()->suggested_url_checked());
332 EXPECT_FALSE(error_handler()->ssl_interstitial_shown());
333 EXPECT_FALSE(error_handler()->common_name_mismatch_redirect());
334 // Fake an Invalid Suggested URL Check result.
335 error_handler()->SendSuggestedUrlCheckResult(
336 CommonNameMismatchHandler::SuggestedUrlCheckResult::
337 SUGGESTED_URL_NOT_AVAILABLE,
338 GURL());
339
340 EXPECT_FALSE(error_handler()->IsTimerRunning());
341 EXPECT_TRUE(error_handler()->ssl_interstitial_shown());
342 EXPECT_FALSE(error_handler()->common_name_mismatch_redirect());
343 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698