Chromium Code Reviews| 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/macros.h" | 8 #include "base/macros.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/test/simple_test_clock.h" | |
| 12 #include "base/test/simple_test_tick_clock.h" | |
| 11 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 12 #include "chrome/browser/captive_portal/captive_portal_service.h" | 14 #include "chrome/browser/captive_portal/captive_portal_service.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/ssl/common_name_mismatch_handler.h" | 16 #include "chrome/browser/ssl/common_name_mismatch_handler.h" |
| 15 #include "chrome/common/features.h" | 17 #include "chrome/common/features.h" |
| 16 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 18 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 17 #include "chrome/test/base/testing_profile.h" | 19 #include "chrome/test/base/testing_profile.h" |
| 18 #include "components/captive_portal/captive_portal_testing_utils.h" | 20 #include "components/captive_portal/captive_portal_testing_utils.h" |
| 21 #include "components/network_time/network_time_test_utils.h" | |
| 22 #include "components/network_time/network_time_tracker.h" | |
| 23 #include "components/prefs/testing_pref_service.h" | |
| 24 #include "content/public/browser/browser_thread.h" | |
| 19 #include "content/public/browser/notification_service.h" | 25 #include "content/public/browser/notification_service.h" |
| 20 #include "net/base/net_errors.h" | 26 #include "net/base/net_errors.h" |
| 27 #include "net/cert/cert_status_flags.h" | |
| 21 #include "net/cert/x509_certificate.h" | 28 #include "net/cert/x509_certificate.h" |
| 29 #include "net/http/http_response_headers.h" | |
| 22 #include "net/ssl/ssl_info.h" | 30 #include "net/ssl/ssl_info.h" |
| 23 #include "net/test/cert_test_util.h" | 31 #include "net/test/cert_test_util.h" |
| 32 #include "net/test/embedded_test_server/embedded_test_server.h" | |
| 33 #include "net/test/embedded_test_server/http_response.h" | |
| 24 #include "net/test/test_certificate_data.h" | 34 #include "net/test/test_certificate_data.h" |
| 25 #include "net/test/test_data_directory.h" | 35 #include "net/test/test_data_directory.h" |
| 36 #include "net/url_request/url_request_test_util.h" | |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 37 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 38 |
| 28 class SSLErrorHandlerForTest : public SSLErrorHandler { | 39 class SSLErrorHandlerForTest : public SSLErrorHandler { |
| 29 public: | 40 public: |
| 30 SSLErrorHandlerForTest(Profile* profile, | 41 SSLErrorHandlerForTest(Profile* profile, |
| 31 content::WebContents* web_contents, | 42 content::WebContents* web_contents, |
| 32 const net::SSLInfo& ssl_info) | 43 const net::SSLInfo& ssl_info) |
| 33 : SSLErrorHandler( | 44 : SSLErrorHandler( |
| 34 web_contents, | 45 web_contents, |
| 35 net::ERR_CERT_COMMON_NAME_INVALID, | 46 net::MapCertStatusToNetError(ssl_info.cert_status), |
| 36 ssl_info, | 47 ssl_info, |
| 37 GURL(), | 48 GURL(), |
| 38 0, | 49 0, |
| 39 nullptr, | 50 nullptr, |
| 40 base::Callback<void(content::CertificateRequestResultType)>()), | 51 base::Callback<void(content::CertificateRequestResultType)>()), |
| 41 profile_(profile), | 52 profile_(profile), |
| 42 captive_portal_checked_(false), | 53 captive_portal_checked_(false), |
| 43 suggested_url_exists_(false), | 54 suggested_url_exists_(false), |
| 44 suggested_url_checked_(false), | 55 suggested_url_checked_(false), |
| 45 ssl_interstitial_shown_(false), | 56 ssl_interstitial_shown_(false), |
| 57 bad_clock_interstitial_shown_(false), | |
| 46 captive_portal_interstitial_shown_(false), | 58 captive_portal_interstitial_shown_(false), |
| 47 redirected_to_suggested_url_(false), | 59 redirected_to_suggested_url_(false), |
| 48 is_overridable_error_(true) {} | 60 is_overridable_error_(true) {} |
| 49 | 61 |
| 50 using SSLErrorHandler::StartHandlingError; | 62 using SSLErrorHandler::StartHandlingError; |
| 51 | 63 |
| 52 void SendCaptivePortalNotification( | 64 void SendCaptivePortalNotification( |
| 53 captive_portal::CaptivePortalResult result) { | 65 captive_portal::CaptivePortalResult result) { |
| 54 CaptivePortalService::Results results; | 66 CaptivePortalService::Results results; |
| 55 results.previous_result = captive_portal::RESULT_INTERNET_CONNECTED; | 67 results.previous_result = captive_portal::RESULT_INTERNET_CONNECTED; |
| 56 results.result = result; | 68 results.result = result; |
| 57 content::NotificationService::current()->Notify( | 69 content::NotificationService::current()->Notify( |
| 58 chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT, | 70 chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT, |
| 59 content::Source<Profile>(profile_), | 71 content::Source<Profile>(profile_), |
| 60 content::Details<CaptivePortalService::Results>(&results)); | 72 content::Details<CaptivePortalService::Results>(&results)); |
| 61 } | 73 } |
| 62 | 74 |
| 63 void SendSuggestedUrlCheckResult( | 75 void SendSuggestedUrlCheckResult( |
| 64 const CommonNameMismatchHandler::SuggestedUrlCheckResult& result, | 76 const CommonNameMismatchHandler::SuggestedUrlCheckResult& result, |
| 65 const GURL& suggested_url) { | 77 const GURL& suggested_url) { |
| 66 CommonNameMismatchHandlerCallback(result, suggested_url); | 78 CommonNameMismatchHandlerCallback(result, suggested_url); |
| 67 } | 79 } |
| 68 | 80 |
| 69 bool IsTimerRunning() const { return get_timer().IsRunning(); } | 81 bool IsTimerRunning() const { return get_timer().IsRunning(); } |
| 70 int captive_portal_checked() const { return captive_portal_checked_; } | 82 int captive_portal_checked() const { return captive_portal_checked_; } |
| 71 int ssl_interstitial_shown() const { return ssl_interstitial_shown_; } | 83 int ssl_interstitial_shown() const { return ssl_interstitial_shown_; } |
| 72 int captive_portal_interstitial_shown() const { | 84 int captive_portal_interstitial_shown() const { |
| 73 return captive_portal_interstitial_shown_; | 85 return captive_portal_interstitial_shown_; |
| 74 } | 86 } |
| 87 bool bad_clock_interstitial_shown() const { | |
| 88 return bad_clock_interstitial_shown_; | |
| 89 } | |
| 75 bool suggested_url_checked() const { return suggested_url_checked_; } | 90 bool suggested_url_checked() const { return suggested_url_checked_; } |
| 76 bool redirected_to_suggested_url() const { | 91 bool redirected_to_suggested_url() const { |
| 77 return redirected_to_suggested_url_; | 92 return redirected_to_suggested_url_; |
| 78 } | 93 } |
| 79 | 94 |
| 80 void set_suggested_url_exists() { suggested_url_exists_ = true; } | 95 void set_suggested_url_exists() { suggested_url_exists_ = true; } |
| 81 void set_non_overridable_error() { is_overridable_error_ = false; } | 96 void set_non_overridable_error() { is_overridable_error_ = false; } |
| 82 | 97 |
| 83 void ClearSeenOperations() { | 98 void ClearSeenOperations() { |
| 84 captive_portal_checked_ = false; | 99 captive_portal_checked_ = false; |
| 85 suggested_url_exists_ = false; | 100 suggested_url_exists_ = false; |
| 86 suggested_url_checked_ = false; | 101 suggested_url_checked_ = false; |
| 87 ssl_interstitial_shown_ = false; | 102 ssl_interstitial_shown_ = false; |
| 103 bad_clock_interstitial_shown_ = false; | |
| 88 captive_portal_interstitial_shown_ = false; | 104 captive_portal_interstitial_shown_ = false; |
| 89 redirected_to_suggested_url_ = false; | 105 redirected_to_suggested_url_ = false; |
| 90 } | 106 } |
| 91 | 107 |
| 92 private: | 108 private: |
| 93 void CheckForCaptivePortal() override { | 109 void CheckForCaptivePortal() override { |
| 94 captive_portal_checked_ = true; | 110 captive_portal_checked_ = true; |
| 95 } | 111 } |
| 96 | 112 |
| 97 bool GetSuggestedUrl(const std::vector<std::string>& dns_names, | 113 bool GetSuggestedUrl(const std::vector<std::string>& dns_names, |
| 98 GURL* suggested_url) const override { | 114 GURL* suggested_url) const override { |
| 99 if (!suggested_url_exists_) | 115 if (!suggested_url_exists_) |
| 100 return false; | 116 return false; |
| 101 *suggested_url = GURL("www.example.com"); | 117 *suggested_url = GURL("www.example.com"); |
| 102 return true; | 118 return true; |
| 103 } | 119 } |
| 104 | 120 |
| 105 void ShowSSLInterstitial() override { ssl_interstitial_shown_ = true; } | 121 void ShowSSLInterstitial() override { ssl_interstitial_shown_ = true; } |
| 106 | 122 |
| 123 void ShowBadClockInterstitial(const base::Time& now, | |
| 124 ssl_errors::ClockState clock_state) override { | |
| 125 bad_clock_interstitial_shown_ = true; | |
| 126 } | |
| 127 | |
| 107 void ShowCaptivePortalInterstitial(const GURL& landing_url) override { | 128 void ShowCaptivePortalInterstitial(const GURL& landing_url) override { |
| 108 captive_portal_interstitial_shown_ = true; | 129 captive_portal_interstitial_shown_ = true; |
| 109 } | 130 } |
| 110 | 131 |
| 111 void CheckSuggestedUrl(const GURL& suggested_url) override { | 132 void CheckSuggestedUrl(const GURL& suggested_url) override { |
| 112 suggested_url_checked_ = true; | 133 suggested_url_checked_ = true; |
| 113 } | 134 } |
| 114 | 135 |
| 115 void NavigateToSuggestedURL(const GURL& suggested_url) override { | 136 void NavigateToSuggestedURL(const GURL& suggested_url) override { |
| 116 redirected_to_suggested_url_ = true; | 137 redirected_to_suggested_url_ = true; |
| 117 } | 138 } |
| 118 | 139 |
| 119 bool IsErrorOverridable() const override { return is_overridable_error_; } | 140 bool IsErrorOverridable() const override { return is_overridable_error_; } |
| 120 | 141 |
| 121 Profile* profile_; | 142 Profile* profile_; |
| 122 bool captive_portal_checked_; | 143 bool captive_portal_checked_; |
| 123 bool suggested_url_exists_; | 144 bool suggested_url_exists_; |
| 124 bool suggested_url_checked_; | 145 bool suggested_url_checked_; |
| 125 bool ssl_interstitial_shown_; | 146 bool ssl_interstitial_shown_; |
| 147 bool bad_clock_interstitial_shown_; | |
| 126 bool captive_portal_interstitial_shown_; | 148 bool captive_portal_interstitial_shown_; |
| 127 bool redirected_to_suggested_url_; | 149 bool redirected_to_suggested_url_; |
| 128 bool is_overridable_error_; | 150 bool is_overridable_error_; |
| 129 | 151 |
| 130 DISALLOW_COPY_AND_ASSIGN(SSLErrorHandlerForTest); | 152 DISALLOW_COPY_AND_ASSIGN(SSLErrorHandlerForTest); |
| 131 }; | 153 }; |
| 132 | 154 |
| 133 class SSLErrorHandlerTest : public ChromeRenderViewHostTestHarness { | 155 class CommonNameSSLErrorHandlerTest : public ChromeRenderViewHostTestHarness { |
|
meacer
2016/10/26 20:03:02
nit: maybe rename to SSLErrorHandlerNameMismatchTe
estark
2016/10/31 16:03:25
Done.
| |
| 134 public: | 156 public: |
| 135 SSLErrorHandlerTest() : field_trial_list_(nullptr) {} | 157 CommonNameSSLErrorHandlerTest() : field_trial_list_(nullptr) {} |
| 136 | 158 |
| 137 void SetUp() override { | 159 void SetUp() override { |
| 138 ChromeRenderViewHostTestHarness::SetUp(); | 160 ChromeRenderViewHostTestHarness::SetUp(); |
| 139 SSLErrorHandler::SetInterstitialDelayForTest(base::TimeDelta()); | 161 SSLErrorHandler::SetInterstitialDelayForTest(base::TimeDelta()); |
| 140 ssl_info_.cert = | 162 ssl_info_.cert = |
| 141 net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem"); | 163 net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem"); |
| 142 ssl_info_.cert_status = net::CERT_STATUS_COMMON_NAME_INVALID; | 164 ssl_info_.cert_status = net::CERT_STATUS_COMMON_NAME_INVALID; |
| 143 error_handler_.reset( | 165 error_handler_.reset( |
| 144 new SSLErrorHandlerForTest(profile(), web_contents(), ssl_info_)); | 166 new SSLErrorHandlerForTest(profile(), web_contents(), ssl_info_)); |
| 145 // Enable finch experiment for captive portal interstitials. | 167 // Enable finch experiment for captive portal interstitials. |
| 146 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( | 168 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( |
| 147 "CaptivePortalInterstitial", "Enabled")); | 169 "CaptivePortalInterstitial", "Enabled")); |
| 148 // Enable finch experiment for SSL common name mismatch handling. | 170 // Enable finch experiment for SSL common name mismatch handling. |
| 149 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( | 171 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( |
| 150 "SSLCommonNameMismatchHandling", "Enabled")); | 172 "SSLCommonNameMismatchHandling", "Enabled")); |
| 151 } | 173 } |
| 152 | 174 |
| 153 void TearDown() override { | 175 void TearDown() override { |
| 154 EXPECT_FALSE(error_handler()->IsTimerRunning()); | 176 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 155 error_handler_.reset(nullptr); | 177 error_handler_.reset(nullptr); |
| 156 ChromeRenderViewHostTestHarness::TearDown(); | 178 ChromeRenderViewHostTestHarness::TearDown(); |
| 157 } | 179 } |
| 158 | 180 |
| 159 SSLErrorHandlerForTest* error_handler() { return error_handler_.get(); } | 181 SSLErrorHandlerForTest* error_handler() { return error_handler_.get(); } |
| 160 | 182 |
| 161 private: | 183 private: |
| 162 net::SSLInfo ssl_info_; | 184 net::SSLInfo ssl_info_; |
| 163 std::unique_ptr<SSLErrorHandlerForTest> error_handler_; | 185 std::unique_ptr<SSLErrorHandlerForTest> error_handler_; |
| 164 base::FieldTrialList field_trial_list_; | 186 base::FieldTrialList field_trial_list_; |
| 187 | |
| 188 DISALLOW_COPY_AND_ASSIGN(CommonNameSSLErrorHandlerTest); | |
| 189 }; | |
| 190 | |
| 191 class DateInvalidSSLErrorHandlerTest : public ChromeRenderViewHostTestHarness { | |
|
meacer
2016/10/26 20:03:03
And this one to SSLErrorHandlerDateInvalidTest?
estark
2016/10/31 16:03:25
Done.
| |
| 192 public: | |
| 193 DateInvalidSSLErrorHandlerTest() | |
| 194 : field_trial_test_(new network_time::FieldTrialTest()), | |
| 195 clock_(new base::SimpleTestClock), | |
| 196 tick_clock_(new base::SimpleTestTickClock), | |
| 197 test_server_(new net::EmbeddedTestServer) { | |
| 198 SetThreadBundleOptions(content::TestBrowserThreadBundle::REAL_IO_THREAD); | |
| 199 network_time::NetworkTimeTracker::RegisterPrefs(pref_service_.registry()); | |
| 200 } | |
| 201 | |
| 202 void SetUp() override { | |
| 203 ChromeRenderViewHostTestHarness::SetUp(); | |
| 204 | |
| 205 field_trial_test()->SetNetworkQueriesWithVariationsService( | |
| 206 false, 0.0, network_time::FieldTrialTest::DISABLE_FETCHES_ON_DEMAND); | |
| 207 tracker_.reset(new network_time::NetworkTimeTracker( | |
| 208 std::unique_ptr<base::Clock>(clock_), | |
| 209 std::unique_ptr<base::TickClock>(tick_clock_), &pref_service_, | |
| 210 new net::TestURLRequestContextGetter( | |
| 211 content::BrowserThread::GetTaskRunnerForThread( | |
| 212 content::BrowserThread::IO)))); | |
| 213 | |
| 214 // Do this to be sure that |is_null| returns false. | |
| 215 clock_->Advance(base::TimeDelta::FromDays(111)); | |
| 216 tick_clock_->Advance(base::TimeDelta::FromDays(222)); | |
| 217 | |
| 218 SSLErrorHandler::SetInterstitialDelayForTest(base::TimeDelta()); | |
| 219 ssl_info_.cert = | |
| 220 net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem"); | |
| 221 ssl_info_.cert_status = net::CERT_STATUS_DATE_INVALID; | |
| 222 error_handler_.reset( | |
| 223 new SSLErrorHandlerForTest(profile(), web_contents(), ssl_info_)); | |
| 224 error_handler_->SetNetworkTimeTrackerForTest(tracker_.get()); | |
| 225 } | |
| 226 | |
| 227 void TearDown() override { | |
| 228 if (error_handler()) { | |
| 229 EXPECT_FALSE(error_handler()->IsTimerRunning()); | |
| 230 error_handler_.reset(nullptr); | |
| 231 } | |
| 232 ChromeRenderViewHostTestHarness::TearDown(); | |
| 233 } | |
| 234 | |
| 235 SSLErrorHandlerForTest* error_handler() { return error_handler_.get(); } | |
| 236 | |
| 237 network_time::FieldTrialTest* field_trial_test() { | |
| 238 return field_trial_test_.get(); | |
| 239 } | |
| 240 | |
| 241 network_time::NetworkTimeTracker* tracker() { return tracker_.get(); } | |
| 242 | |
| 243 net::EmbeddedTestServer* test_server() { return test_server_.get(); } | |
| 244 | |
| 245 void ClearErrorHandler() { error_handler_.reset(nullptr); } | |
| 246 | |
| 247 private: | |
| 248 net::SSLInfo ssl_info_; | |
| 249 std::unique_ptr<SSLErrorHandlerForTest> error_handler_; | |
| 250 std::unique_ptr<network_time::FieldTrialTest> field_trial_test_; | |
| 251 base::SimpleTestClock* clock_; | |
| 252 base::SimpleTestTickClock* tick_clock_; | |
| 253 TestingPrefServiceSimple pref_service_; | |
| 254 std::unique_ptr<network_time::NetworkTimeTracker> tracker_; | |
| 255 std::unique_ptr<net::EmbeddedTestServer> test_server_; | |
| 256 | |
| 257 DISALLOW_COPY_AND_ASSIGN(DateInvalidSSLErrorHandlerTest); | |
| 165 }; | 258 }; |
| 166 | 259 |
| 167 #if BUILDFLAG(ENABLE_CAPTIVE_PORTAL_DETECTION) | 260 #if BUILDFLAG(ENABLE_CAPTIVE_PORTAL_DETECTION) |
| 168 | 261 |
| 169 TEST_F(SSLErrorHandlerTest, | 262 TEST_F(CommonNameSSLErrorHandlerTest, ShouldShowSSLInterstitialOnTimerExpired) { |
| 170 ShouldShowSSLInterstitialOnTimerExpired) { | |
| 171 EXPECT_FALSE(error_handler()->IsTimerRunning()); | 263 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 172 error_handler()->StartHandlingError(); | 264 error_handler()->StartHandlingError(); |
| 173 | 265 |
| 174 EXPECT_TRUE(error_handler()->IsTimerRunning()); | 266 EXPECT_TRUE(error_handler()->IsTimerRunning()); |
| 175 EXPECT_TRUE(error_handler()->captive_portal_checked()); | 267 EXPECT_TRUE(error_handler()->captive_portal_checked()); |
| 176 EXPECT_FALSE(error_handler()->ssl_interstitial_shown()); | 268 EXPECT_FALSE(error_handler()->ssl_interstitial_shown()); |
| 177 EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown()); | 269 EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown()); |
| 178 | 270 |
| 179 error_handler()->ClearSeenOperations(); | 271 error_handler()->ClearSeenOperations(); |
| 180 base::RunLoop().RunUntilIdle(); | 272 base::RunLoop().RunUntilIdle(); |
| 181 | 273 |
| 182 EXPECT_FALSE(error_handler()->IsTimerRunning()); | 274 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 183 EXPECT_FALSE(error_handler()->captive_portal_checked()); | 275 EXPECT_FALSE(error_handler()->captive_portal_checked()); |
| 184 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); | 276 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); |
| 185 EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown()); | 277 EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown()); |
| 186 } | 278 } |
| 187 | 279 |
| 188 TEST_F(SSLErrorHandlerTest, | 280 TEST_F(CommonNameSSLErrorHandlerTest, |
| 189 ShouldShowCustomInterstitialOnCaptivePortalResult) { | 281 ShouldShowCustomInterstitialOnCaptivePortalResult) { |
| 190 EXPECT_FALSE(error_handler()->IsTimerRunning()); | 282 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 191 error_handler()->StartHandlingError(); | 283 error_handler()->StartHandlingError(); |
| 192 | 284 |
| 193 EXPECT_TRUE(error_handler()->IsTimerRunning()); | 285 EXPECT_TRUE(error_handler()->IsTimerRunning()); |
| 194 EXPECT_TRUE(error_handler()->captive_portal_checked()); | 286 EXPECT_TRUE(error_handler()->captive_portal_checked()); |
| 195 EXPECT_FALSE(error_handler()->ssl_interstitial_shown()); | 287 EXPECT_FALSE(error_handler()->ssl_interstitial_shown()); |
| 196 EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown()); | 288 EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown()); |
| 197 // Fake a captive portal result. | 289 // Fake a captive portal result. |
| 198 error_handler()->ClearSeenOperations(); | 290 error_handler()->ClearSeenOperations(); |
| 199 error_handler()->SendCaptivePortalNotification( | 291 error_handler()->SendCaptivePortalNotification( |
| 200 captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL); | 292 captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL); |
| 201 base::RunLoop().RunUntilIdle(); | 293 base::RunLoop().RunUntilIdle(); |
| 202 | 294 |
| 203 EXPECT_FALSE(error_handler()->IsTimerRunning()); | 295 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 204 EXPECT_FALSE(error_handler()->captive_portal_checked()); | 296 EXPECT_FALSE(error_handler()->captive_portal_checked()); |
| 205 EXPECT_FALSE(error_handler()->ssl_interstitial_shown()); | 297 EXPECT_FALSE(error_handler()->ssl_interstitial_shown()); |
| 206 EXPECT_TRUE(error_handler()->captive_portal_interstitial_shown()); | 298 EXPECT_TRUE(error_handler()->captive_portal_interstitial_shown()); |
| 207 } | 299 } |
| 208 | 300 |
| 209 TEST_F(SSLErrorHandlerTest, | 301 TEST_F(CommonNameSSLErrorHandlerTest, |
| 210 ShouldShowSSLInterstitialOnNoCaptivePortalResult) { | 302 ShouldShowSSLInterstitialOnNoCaptivePortalResult) { |
| 211 EXPECT_FALSE(error_handler()->IsTimerRunning()); | 303 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 212 error_handler()->StartHandlingError(); | 304 error_handler()->StartHandlingError(); |
| 213 | 305 |
| 214 EXPECT_TRUE(error_handler()->IsTimerRunning()); | 306 EXPECT_TRUE(error_handler()->IsTimerRunning()); |
| 215 EXPECT_TRUE(error_handler()->captive_portal_checked()); | 307 EXPECT_TRUE(error_handler()->captive_portal_checked()); |
| 216 EXPECT_FALSE(error_handler()->ssl_interstitial_shown()); | 308 EXPECT_FALSE(error_handler()->ssl_interstitial_shown()); |
| 217 EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown()); | 309 EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown()); |
| 218 // Fake a "connected to internet" result for the captive portal check. | 310 // Fake a "connected to internet" result for the captive portal check. |
| 219 // This should immediately trigger an SSL interstitial without waiting for | 311 // This should immediately trigger an SSL interstitial without waiting for |
| 220 // the timer to expire. | 312 // the timer to expire. |
| 221 error_handler()->ClearSeenOperations(); | 313 error_handler()->ClearSeenOperations(); |
| 222 error_handler()->SendCaptivePortalNotification( | 314 error_handler()->SendCaptivePortalNotification( |
| 223 captive_portal::RESULT_INTERNET_CONNECTED); | 315 captive_portal::RESULT_INTERNET_CONNECTED); |
| 224 base::RunLoop().RunUntilIdle(); | 316 base::RunLoop().RunUntilIdle(); |
| 225 | 317 |
| 226 EXPECT_FALSE(error_handler()->IsTimerRunning()); | 318 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 227 EXPECT_FALSE(error_handler()->captive_portal_checked()); | 319 EXPECT_FALSE(error_handler()->captive_portal_checked()); |
| 228 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); | 320 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); |
| 229 EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown()); | 321 EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown()); |
| 230 } | 322 } |
| 231 | 323 |
| 232 TEST_F(SSLErrorHandlerTest, ShouldNotCheckSuggestedUrlIfNoSuggestedUrl) { | 324 TEST_F(CommonNameSSLErrorHandlerTest, |
| 325 ShouldNotCheckSuggestedUrlIfNoSuggestedUrl) { | |
| 233 error_handler()->StartHandlingError(); | 326 error_handler()->StartHandlingError(); |
| 234 | 327 |
| 235 EXPECT_TRUE(error_handler()->captive_portal_checked()); | 328 EXPECT_TRUE(error_handler()->captive_portal_checked()); |
| 236 EXPECT_TRUE(error_handler()->IsTimerRunning()); | 329 EXPECT_TRUE(error_handler()->IsTimerRunning()); |
| 237 EXPECT_FALSE(error_handler()->suggested_url_checked()); | 330 EXPECT_FALSE(error_handler()->suggested_url_checked()); |
| 238 base::RunLoop().RunUntilIdle(); | 331 base::RunLoop().RunUntilIdle(); |
| 239 | 332 |
| 240 EXPECT_FALSE(error_handler()->IsTimerRunning()); | 333 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 241 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); | 334 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); |
| 242 } | 335 } |
| 243 | 336 |
| 244 TEST_F(SSLErrorHandlerTest, ShouldNotCheckCaptivePortalIfSuggestedUrlExists) { | 337 TEST_F(CommonNameSSLErrorHandlerTest, |
| 338 ShouldNotCheckCaptivePortalIfSuggestedUrlExists) { | |
| 245 EXPECT_FALSE(error_handler()->IsTimerRunning()); | 339 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 246 error_handler()->set_suggested_url_exists(); | 340 error_handler()->set_suggested_url_exists(); |
| 247 error_handler()->StartHandlingError(); | 341 error_handler()->StartHandlingError(); |
| 248 | 342 |
| 249 EXPECT_TRUE(error_handler()->IsTimerRunning()); | 343 EXPECT_TRUE(error_handler()->IsTimerRunning()); |
| 250 EXPECT_TRUE(error_handler()->suggested_url_checked()); | 344 EXPECT_TRUE(error_handler()->suggested_url_checked()); |
| 251 EXPECT_FALSE(error_handler()->captive_portal_checked()); | 345 EXPECT_FALSE(error_handler()->captive_portal_checked()); |
| 252 base::RunLoop().RunUntilIdle(); | 346 base::RunLoop().RunUntilIdle(); |
| 253 | 347 |
| 254 EXPECT_FALSE(error_handler()->IsTimerRunning()); | 348 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 255 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); | 349 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); |
| 256 } | 350 } |
| 257 | 351 |
| 258 TEST_F(SSLErrorHandlerTest, ShouldNotHandleNameMismatchOnNonOverridableError) { | 352 TEST_F(CommonNameSSLErrorHandlerTest, |
| 353 ShouldNotHandleNameMismatchOnNonOverridableError) { | |
| 259 error_handler()->set_non_overridable_error(); | 354 error_handler()->set_non_overridable_error(); |
| 260 error_handler()->set_suggested_url_exists(); | 355 error_handler()->set_suggested_url_exists(); |
| 261 error_handler()->StartHandlingError(); | 356 error_handler()->StartHandlingError(); |
| 262 | 357 |
| 263 EXPECT_FALSE(error_handler()->suggested_url_checked()); | 358 EXPECT_FALSE(error_handler()->suggested_url_checked()); |
| 264 EXPECT_TRUE(error_handler()->captive_portal_checked()); | 359 EXPECT_TRUE(error_handler()->captive_portal_checked()); |
| 265 EXPECT_TRUE(error_handler()->IsTimerRunning()); | 360 EXPECT_TRUE(error_handler()->IsTimerRunning()); |
| 266 base::RunLoop().RunUntilIdle(); | 361 base::RunLoop().RunUntilIdle(); |
| 267 | 362 |
| 268 EXPECT_FALSE(error_handler()->IsTimerRunning()); | 363 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 269 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); | 364 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); |
| 270 } | 365 } |
| 271 | 366 |
| 272 #else // #if !BUILDFLAG(ENABLE_CAPTIVE_PORTAL_DETECTION) | 367 #else // #if !BUILDFLAG(ENABLE_CAPTIVE_PORTAL_DETECTION) |
| 273 | 368 |
| 274 TEST_F(SSLErrorHandlerTest, | 369 TEST_F(CommonNameSSLErrorHandlerTest, |
| 275 ShouldShowSSLInterstitialOnCaptivePortalDetectionDisabled) { | 370 ShouldShowSSLInterstitialOnCaptivePortalDetectionDisabled) { |
| 276 EXPECT_FALSE(error_handler()->IsTimerRunning()); | 371 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 277 error_handler()->StartHandlingError(); | 372 error_handler()->StartHandlingError(); |
| 278 EXPECT_FALSE(error_handler()->IsTimerRunning()); | 373 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 279 EXPECT_FALSE(error_handler()->captive_portal_checked()); | 374 EXPECT_FALSE(error_handler()->captive_portal_checked()); |
| 280 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); | 375 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); |
| 281 EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown()); | 376 EXPECT_FALSE(error_handler()->captive_portal_interstitial_shown()); |
| 282 } | 377 } |
| 283 | 378 |
| 284 #endif // BUILDFLAG(ENABLE_CAPTIVE_PORTAL_DETECTION) | 379 #endif // BUILDFLAG(ENABLE_CAPTIVE_PORTAL_DETECTION) |
| 285 | 380 |
| 286 TEST_F(SSLErrorHandlerTest, | 381 TEST_F(CommonNameSSLErrorHandlerTest, |
| 287 ShouldShowSSLInterstitialOnTimerExpiredWhenSuggestedUrlExists) { | 382 ShouldShowSSLInterstitialOnTimerExpiredWhenSuggestedUrlExists) { |
| 288 error_handler()->set_suggested_url_exists(); | 383 error_handler()->set_suggested_url_exists(); |
| 289 error_handler()->StartHandlingError(); | 384 error_handler()->StartHandlingError(); |
| 290 | 385 |
| 291 EXPECT_TRUE(error_handler()->IsTimerRunning()); | 386 EXPECT_TRUE(error_handler()->IsTimerRunning()); |
| 292 EXPECT_TRUE(error_handler()->suggested_url_checked()); | 387 EXPECT_TRUE(error_handler()->suggested_url_checked()); |
| 293 EXPECT_FALSE(error_handler()->ssl_interstitial_shown()); | 388 EXPECT_FALSE(error_handler()->ssl_interstitial_shown()); |
| 294 EXPECT_FALSE(error_handler()->redirected_to_suggested_url()); | 389 EXPECT_FALSE(error_handler()->redirected_to_suggested_url()); |
| 295 | 390 |
| 296 base::RunLoop().RunUntilIdle(); | 391 base::RunLoop().RunUntilIdle(); |
| 297 | 392 |
| 298 EXPECT_FALSE(error_handler()->IsTimerRunning()); | 393 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 299 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); | 394 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); |
| 300 EXPECT_FALSE(error_handler()->redirected_to_suggested_url()); | 395 EXPECT_FALSE(error_handler()->redirected_to_suggested_url()); |
| 301 } | 396 } |
| 302 | 397 |
| 303 TEST_F(SSLErrorHandlerTest, ShouldRedirectOnSuggestedUrlCheckResult) { | 398 TEST_F(CommonNameSSLErrorHandlerTest, ShouldRedirectOnSuggestedUrlCheckResult) { |
| 304 error_handler()->set_suggested_url_exists(); | 399 error_handler()->set_suggested_url_exists(); |
| 305 error_handler()->StartHandlingError(); | 400 error_handler()->StartHandlingError(); |
| 306 | 401 |
| 307 EXPECT_TRUE(error_handler()->IsTimerRunning()); | 402 EXPECT_TRUE(error_handler()->IsTimerRunning()); |
| 308 EXPECT_TRUE(error_handler()->suggested_url_checked()); | 403 EXPECT_TRUE(error_handler()->suggested_url_checked()); |
| 309 EXPECT_FALSE(error_handler()->ssl_interstitial_shown()); | 404 EXPECT_FALSE(error_handler()->ssl_interstitial_shown()); |
| 310 EXPECT_FALSE(error_handler()->redirected_to_suggested_url()); | 405 EXPECT_FALSE(error_handler()->redirected_to_suggested_url()); |
| 311 // Fake a valid suggested URL check result. | 406 // Fake a valid suggested URL check result. |
| 312 // The URL returned by |SuggestedUrlCheckResult| can be different from | 407 // The URL returned by |SuggestedUrlCheckResult| can be different from |
| 313 // |suggested_url|, if there is a redirect. | 408 // |suggested_url|, if there is a redirect. |
| 314 error_handler()->SendSuggestedUrlCheckResult( | 409 error_handler()->SendSuggestedUrlCheckResult( |
| 315 CommonNameMismatchHandler::SuggestedUrlCheckResult:: | 410 CommonNameMismatchHandler::SuggestedUrlCheckResult:: |
| 316 SUGGESTED_URL_AVAILABLE, | 411 SUGGESTED_URL_AVAILABLE, |
| 317 GURL("https://random.example.com")); | 412 GURL("https://random.example.com")); |
| 318 | 413 |
| 319 EXPECT_FALSE(error_handler()->IsTimerRunning()); | 414 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 320 EXPECT_FALSE(error_handler()->ssl_interstitial_shown()); | 415 EXPECT_FALSE(error_handler()->ssl_interstitial_shown()); |
| 321 EXPECT_TRUE(error_handler()->redirected_to_suggested_url()); | 416 EXPECT_TRUE(error_handler()->redirected_to_suggested_url()); |
| 322 } | 417 } |
| 323 | 418 |
| 324 TEST_F(SSLErrorHandlerTest, ShouldShowSSLInterstitialOnInvalidUrlCheckResult) { | 419 TEST_F(CommonNameSSLErrorHandlerTest, |
| 420 ShouldShowSSLInterstitialOnInvalidUrlCheckResult) { | |
| 325 error_handler()->set_suggested_url_exists(); | 421 error_handler()->set_suggested_url_exists(); |
| 326 error_handler()->StartHandlingError(); | 422 error_handler()->StartHandlingError(); |
| 327 | 423 |
| 328 EXPECT_TRUE(error_handler()->IsTimerRunning()); | 424 EXPECT_TRUE(error_handler()->IsTimerRunning()); |
| 329 EXPECT_TRUE(error_handler()->suggested_url_checked()); | 425 EXPECT_TRUE(error_handler()->suggested_url_checked()); |
| 330 EXPECT_FALSE(error_handler()->ssl_interstitial_shown()); | 426 EXPECT_FALSE(error_handler()->ssl_interstitial_shown()); |
| 331 EXPECT_FALSE(error_handler()->redirected_to_suggested_url()); | 427 EXPECT_FALSE(error_handler()->redirected_to_suggested_url()); |
| 332 // Fake an Invalid Suggested URL Check result. | 428 // Fake an Invalid Suggested URL Check result. |
| 333 error_handler()->SendSuggestedUrlCheckResult( | 429 error_handler()->SendSuggestedUrlCheckResult( |
| 334 CommonNameMismatchHandler::SuggestedUrlCheckResult:: | 430 CommonNameMismatchHandler::SuggestedUrlCheckResult:: |
| 335 SUGGESTED_URL_NOT_AVAILABLE, | 431 SUGGESTED_URL_NOT_AVAILABLE, |
| 336 GURL()); | 432 GURL()); |
| 337 | 433 |
| 338 EXPECT_FALSE(error_handler()->IsTimerRunning()); | 434 EXPECT_FALSE(error_handler()->IsTimerRunning()); |
| 339 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); | 435 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); |
| 340 EXPECT_FALSE(error_handler()->redirected_to_suggested_url()); | 436 EXPECT_FALSE(error_handler()->redirected_to_suggested_url()); |
| 341 } | 437 } |
| 438 | |
| 439 TEST_F(DateInvalidSSLErrorHandlerTest, TimeQueryStarted) { | |
| 440 base::Time network_time; | |
| 441 base::TimeDelta uncertainty; | |
| 442 EXPECT_EQ(network_time::NetworkTimeTracker::NETWORK_TIME_NO_SYNC_ATTEMPT, | |
| 443 tracker()->GetNetworkTime(&network_time, &uncertainty)); | |
| 444 | |
| 445 // Enable network time queries and handle the error. A bad clock interstitial | |
| 446 // should be shown. | |
| 447 EXPECT_TRUE(test_server()->Start()); | |
| 448 test_server()->RegisterRequestHandler( | |
| 449 base::Bind(&network_time::GoodTimeResponseHandler)); | |
| 450 tracker()->SetTimeServerURLForTesting(test_server()->GetURL("/")); | |
| 451 field_trial_test()->SetNetworkQueriesWithVariationsService( | |
| 452 true, 0.0, network_time::FieldTrialTest::ENABLE_FETCHES_ON_DEMAND); | |
| 453 error_handler()->StartHandlingError(); | |
| 454 | |
| 455 EXPECT_TRUE(error_handler()->IsTimerRunning()); | |
| 456 tracker()->WaitForFetchForTesting(123123123); | |
| 457 base::RunLoop().RunUntilIdle(); | |
| 458 | |
| 459 EXPECT_TRUE(error_handler()->bad_clock_interstitial_shown()); | |
| 460 EXPECT_FALSE(error_handler()->IsTimerRunning()); | |
| 461 } | |
| 462 | |
| 463 // Tests that an SSL interstitial is shown if the accuracy of the system | |
| 464 // clock can't be determined because network time is unavailable. | |
| 465 TEST_F(DateInvalidSSLErrorHandlerTest, NoTimeQueries) { | |
| 466 base::Time network_time; | |
| 467 base::TimeDelta uncertainty; | |
| 468 EXPECT_EQ(network_time::NetworkTimeTracker::NETWORK_TIME_NO_SYNC_ATTEMPT, | |
| 469 tracker()->GetNetworkTime(&network_time, &uncertainty)); | |
| 470 | |
| 471 // Handle the error without enabling time queries. A bad clock interstitial | |
| 472 // should not be shown. | |
|
meacer
2016/10/26 20:03:03
Just to confirm: This is assuming we don't have th
estark
2016/10/31 16:03:25
Sorta. It's assuming that the build time heuristic
| |
| 473 error_handler()->StartHandlingError(); | |
| 474 | |
| 475 EXPECT_FALSE(error_handler()->IsTimerRunning()); | |
| 476 EXPECT_FALSE(error_handler()->bad_clock_interstitial_shown()); | |
| 477 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); | |
| 478 } | |
| 479 | |
| 480 // Runs |quit_closure| on the UI thread once a URL request has been | |
| 481 // seen. Returns a request that hangs. | |
| 482 std::unique_ptr<net::test_server::HttpResponse> WaitForRequest( | |
| 483 const base::Closure& quit_closure, | |
| 484 const net::test_server::HttpRequest& request) { | |
| 485 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | |
| 486 quit_closure); | |
| 487 return base::MakeUnique<net::test_server::HungResponse>(); | |
| 488 } | |
| 489 | |
| 490 // Tests that an SSL interstitial is shown if determing the accuracy of | |
| 491 // the system clock times out (e.g. because a network time query hangs). | |
| 492 TEST_F(DateInvalidSSLErrorHandlerTest, TimeQueryHangs) { | |
| 493 base::Time network_time; | |
| 494 base::TimeDelta uncertainty; | |
| 495 EXPECT_EQ(network_time::NetworkTimeTracker::NETWORK_TIME_NO_SYNC_ATTEMPT, | |
| 496 tracker()->GetNetworkTime(&network_time, &uncertainty)); | |
| 497 | |
| 498 // Enable network time queries and handle the error. Because the | |
| 499 // network time cannot be determined before the timer elapses, an SSL | |
| 500 // interstitial should be shown. | |
| 501 EXPECT_TRUE(test_server()->Start()); | |
| 502 base::RunLoop wait_for_time_query_loop; | |
| 503 test_server()->RegisterRequestHandler( | |
| 504 base::Bind(&WaitForRequest, wait_for_time_query_loop.QuitClosure())); | |
| 505 tracker()->SetTimeServerURLForTesting(test_server()->GetURL("/")); | |
| 506 field_trial_test()->SetNetworkQueriesWithVariationsService( | |
| 507 true, 0.0, network_time::FieldTrialTest::ENABLE_FETCHES_ON_DEMAND); | |
| 508 error_handler()->StartHandlingError(); | |
| 509 EXPECT_TRUE(error_handler()->IsTimerRunning()); | |
| 510 wait_for_time_query_loop.Run(); | |
| 511 base::RunLoop().RunUntilIdle(); | |
| 512 | |
| 513 EXPECT_FALSE(error_handler()->bad_clock_interstitial_shown()); | |
| 514 EXPECT_TRUE(error_handler()->ssl_interstitial_shown()); | |
| 515 EXPECT_FALSE(error_handler()->IsTimerRunning()); | |
| 516 | |
| 517 // Clear the error handler to test that, when the request completes, | |
| 518 // it doesn't try to call a callback on a deleted SSLErrorHandler. | |
| 519 ClearErrorHandler(); | |
|
meacer
2016/10/26 20:03:02
Is the idea here that the test would crash with a
estark
2016/10/31 16:03:25
Yep
| |
| 520 | |
| 521 // Shut down the server to cancel the pending request. | |
| 522 ASSERT_TRUE(test_server()->ShutdownAndWaitUntilComplete()); | |
| 523 tracker()->WaitForFetchForTesting(123123123); | |
| 524 } | |
| OLD | NEW |