| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <map> | 5 #include <map> |
| 6 #include <set> | 6 #include <set> |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 #include "content/public/browser/navigation_entry.h" | 47 #include "content/public/browser/navigation_entry.h" |
| 48 #include "content/public/browser/notification_observer.h" | 48 #include "content/public/browser/notification_observer.h" |
| 49 #include "content/public/browser/notification_registrar.h" | 49 #include "content/public/browser/notification_registrar.h" |
| 50 #include "content/public/browser/notification_service.h" | 50 #include "content/public/browser/notification_service.h" |
| 51 #include "content/public/browser/notification_types.h" | 51 #include "content/public/browser/notification_types.h" |
| 52 #include "content/public/browser/render_frame_host.h" | 52 #include "content/public/browser/render_frame_host.h" |
| 53 #include "content/public/browser/web_contents.h" | 53 #include "content/public/browser/web_contents.h" |
| 54 #include "content/public/common/url_constants.h" | 54 #include "content/public/common/url_constants.h" |
| 55 #include "content/public/test/browser_test_utils.h" | 55 #include "content/public/test/browser_test_utils.h" |
| 56 #include "net/base/net_errors.h" | 56 #include "net/base/net_errors.h" |
| 57 #include "net/base/test_data_directory.h" |
| 58 #include "net/cert/x509_certificate.h" |
| 57 #include "net/http/transport_security_state.h" | 59 #include "net/http/transport_security_state.h" |
| 60 #include "net/test/cert_test_util.h" |
| 58 #include "net/test/url_request/url_request_failed_job.h" | 61 #include "net/test/url_request/url_request_failed_job.h" |
| 59 #include "net/test/url_request/url_request_mock_http_job.h" | 62 #include "net/test/url_request/url_request_mock_http_job.h" |
| 60 #include "net/url_request/url_request.h" | 63 #include "net/url_request/url_request.h" |
| 61 #include "net/url_request/url_request_context.h" | 64 #include "net/url_request/url_request_context.h" |
| 62 #include "net/url_request/url_request_context_getter.h" | 65 #include "net/url_request/url_request_context_getter.h" |
| 63 #include "net/url_request/url_request_filter.h" | 66 #include "net/url_request/url_request_filter.h" |
| 64 #include "net/url_request/url_request_interceptor.h" | 67 #include "net/url_request/url_request_interceptor.h" |
| 65 #include "net/url_request/url_request_job.h" | 68 #include "net/url_request/url_request_job.h" |
| 66 #include "net/url_request/url_request_status.h" | 69 #include "net/url_request/url_request_status.h" |
| 67 #include "testing/gtest/include/gtest/gtest.h" | 70 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 static void WaitForJobs(int num_jobs); | 140 static void WaitForJobs(int num_jobs); |
| 138 | 141 |
| 139 // Fails all active URLRequestTimeoutOnDemandJobs with connection timeouts. | 142 // Fails all active URLRequestTimeoutOnDemandJobs with connection timeouts. |
| 140 // There are expected to be exactly |expected_num_jobs| waiting for | 143 // There are expected to be exactly |expected_num_jobs| waiting for |
| 141 // failure. The only way to guarantee this is with an earlier call to | 144 // failure. The only way to guarantee this is with an earlier call to |
| 142 // WaitForJobs, so makes sure there has been a matching WaitForJobs call. | 145 // WaitForJobs, so makes sure there has been a matching WaitForJobs call. |
| 143 static void FailJobs(int expected_num_jobs); | 146 static void FailJobs(int expected_num_jobs); |
| 144 | 147 |
| 145 // Fails all active URLRequestTimeoutOnDemandJobs with SSL cert errors. | 148 // Fails all active URLRequestTimeoutOnDemandJobs with SSL cert errors. |
| 146 // |expected_num_jobs| behaves just as in FailJobs. | 149 // |expected_num_jobs| behaves just as in FailJobs. |
| 147 static void FailJobsWithCertError(int expected_num_jobs); | 150 static void FailJobsWithCertError(int expected_num_jobs, |
| 151 const net::SSLInfo& ssl_info); |
| 148 | 152 |
| 149 // Abandon all active URLRequestTimeoutOnDemandJobs. |expected_num_jobs| | 153 // Abandon all active URLRequestTimeoutOnDemandJobs. |expected_num_jobs| |
| 150 // behaves just as in FailJobs. | 154 // behaves just as in FailJobs. |
| 151 static void AbandonJobs(int expected_num_jobs); | 155 static void AbandonJobs(int expected_num_jobs); |
| 152 | 156 |
| 153 private: | 157 private: |
| 154 friend class URLRequestMockCaptivePortalJobFactory; | 158 friend class URLRequestMockCaptivePortalJobFactory; |
| 155 | 159 |
| 156 // Operation to perform on jobs when removing them from |job_list_|. | 160 // Operation to perform on jobs when removing them from |job_list_|. |
| 157 enum EndJobOperation { | 161 enum EndJobOperation { |
| 158 FAIL_JOBS, | 162 FAIL_JOBS, |
| 159 ABANDON_JOBS, | 163 ABANDON_JOBS, |
| 160 FAIL_JOBS_WITH_CERT_ERROR | 164 FAIL_JOBS_WITH_CERT_ERROR |
| 161 }; | 165 }; |
| 162 | 166 |
| 163 URLRequestTimeoutOnDemandJob(net::URLRequest* request, | 167 URLRequestTimeoutOnDemandJob(net::URLRequest* request, |
| 164 net::NetworkDelegate* network_delegate); | 168 net::NetworkDelegate* network_delegate); |
| 165 ~URLRequestTimeoutOnDemandJob() override; | 169 ~URLRequestTimeoutOnDemandJob() override; |
| 166 | 170 |
| 167 // Attempts to removes |this| from |jobs_|. Returns true if it was removed | 171 // Attempts to removes |this| from |jobs_|. Returns true if it was removed |
| 168 // from the list. | 172 // from the list. |
| 169 bool RemoveFromList(); | 173 bool RemoveFromList(); |
| 170 | 174 |
| 171 static void WaitForJobsOnIOThread(int num_jobs); | 175 static void WaitForJobsOnIOThread(int num_jobs); |
| 172 static void FailOrAbandonJobsOnIOThread( | 176 static void FailOrAbandonJobsOnIOThread(int expected_num_jobs, |
| 173 int expected_num_jobs, | 177 EndJobOperation end_job_operation, |
| 174 EndJobOperation end_job_operation); | 178 const net::SSLInfo& ssl_info); |
| 175 | 179 |
| 176 // Checks if there are at least |num_jobs_to_wait_for_| jobs in | 180 // Checks if there are at least |num_jobs_to_wait_for_| jobs in |
| 177 // |job_list_|. If so, exits the message loop on the UI thread, which | 181 // |job_list_|. If so, exits the message loop on the UI thread, which |
| 178 // should be spinning in a call to WaitForJobs. Does nothing when | 182 // should be spinning in a call to WaitForJobs. Does nothing when |
| 179 // |num_jobs_to_wait_for_| is 0. | 183 // |num_jobs_to_wait_for_| is 0. |
| 180 static void MaybeStopWaitingForJobsOnIOThread(); | 184 static void MaybeStopWaitingForJobsOnIOThread(); |
| 181 | 185 |
| 182 // All class variables are only accessed on the IO thread. | 186 // All class variables are only accessed on the IO thread. |
| 183 | 187 |
| 184 // Number of jobs currently being waited for, or 0 if not currently | 188 // Number of jobs currently being waited for, or 0 if not currently |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 base::Bind(&URLRequestTimeoutOnDemandJob::WaitForJobsOnIOThread, | 236 base::Bind(&URLRequestTimeoutOnDemandJob::WaitForJobsOnIOThread, |
| 233 num_jobs)); | 237 num_jobs)); |
| 234 content::RunMessageLoop(); | 238 content::RunMessageLoop(); |
| 235 } | 239 } |
| 236 | 240 |
| 237 // static | 241 // static |
| 238 void URLRequestTimeoutOnDemandJob::FailJobs(int expected_num_jobs) { | 242 void URLRequestTimeoutOnDemandJob::FailJobs(int expected_num_jobs) { |
| 239 content::BrowserThread::PostTask( | 243 content::BrowserThread::PostTask( |
| 240 content::BrowserThread::IO, FROM_HERE, | 244 content::BrowserThread::IO, FROM_HERE, |
| 241 base::Bind(&URLRequestTimeoutOnDemandJob::FailOrAbandonJobsOnIOThread, | 245 base::Bind(&URLRequestTimeoutOnDemandJob::FailOrAbandonJobsOnIOThread, |
| 242 expected_num_jobs, | 246 expected_num_jobs, FAIL_JOBS, net::SSLInfo())); |
| 243 FAIL_JOBS)); | |
| 244 } | 247 } |
| 245 | 248 |
| 246 // static | 249 // static |
| 247 void URLRequestTimeoutOnDemandJob::FailJobsWithCertError( | 250 void URLRequestTimeoutOnDemandJob::FailJobsWithCertError( |
| 248 int expected_num_jobs) { | 251 int expected_num_jobs, |
| 252 const net::SSLInfo& ssl_info) { |
| 249 content::BrowserThread::PostTask( | 253 content::BrowserThread::PostTask( |
| 250 content::BrowserThread::IO, FROM_HERE, | 254 content::BrowserThread::IO, FROM_HERE, |
| 251 base::Bind(&URLRequestTimeoutOnDemandJob::FailOrAbandonJobsOnIOThread, | 255 base::Bind(&URLRequestTimeoutOnDemandJob::FailOrAbandonJobsOnIOThread, |
| 252 expected_num_jobs, | 256 expected_num_jobs, FAIL_JOBS_WITH_CERT_ERROR, ssl_info)); |
| 253 FAIL_JOBS_WITH_CERT_ERROR)); | |
| 254 } | 257 } |
| 255 | 258 |
| 256 // static | 259 // static |
| 257 void URLRequestTimeoutOnDemandJob::AbandonJobs(int expected_num_jobs) { | 260 void URLRequestTimeoutOnDemandJob::AbandonJobs(int expected_num_jobs) { |
| 258 content::BrowserThread::PostTask( | 261 content::BrowserThread::PostTask( |
| 259 content::BrowserThread::IO, FROM_HERE, | 262 content::BrowserThread::IO, FROM_HERE, |
| 260 base::Bind(&URLRequestTimeoutOnDemandJob::FailOrAbandonJobsOnIOThread, | 263 base::Bind(&URLRequestTimeoutOnDemandJob::FailOrAbandonJobsOnIOThread, |
| 261 expected_num_jobs, | 264 expected_num_jobs, ABANDON_JOBS, net::SSLInfo())); |
| 262 ABANDON_JOBS)); | |
| 263 } | 265 } |
| 264 | 266 |
| 265 URLRequestTimeoutOnDemandJob::URLRequestTimeoutOnDemandJob( | 267 URLRequestTimeoutOnDemandJob::URLRequestTimeoutOnDemandJob( |
| 266 net::URLRequest* request, net::NetworkDelegate* network_delegate) | 268 net::URLRequest* request, net::NetworkDelegate* network_delegate) |
| 267 : net::URLRequestJob(request, network_delegate), | 269 : net::URLRequestJob(request, network_delegate), |
| 268 next_job_(NULL) { | 270 next_job_(NULL) { |
| 269 } | 271 } |
| 270 | 272 |
| 271 URLRequestTimeoutOnDemandJob::~URLRequestTimeoutOnDemandJob() { | 273 URLRequestTimeoutOnDemandJob::~URLRequestTimeoutOnDemandJob() { |
| 272 // All hanging jobs should have failed or been abandoned before being | 274 // All hanging jobs should have failed or been abandoned before being |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 last_num_jobs_to_wait_for_ = num_jobs_to_wait_for_; | 319 last_num_jobs_to_wait_for_ = num_jobs_to_wait_for_; |
| 318 num_jobs_to_wait_for_ = 0; | 320 num_jobs_to_wait_for_ = 0; |
| 319 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 321 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 320 base::MessageLoop::QuitClosure()); | 322 base::MessageLoop::QuitClosure()); |
| 321 } | 323 } |
| 322 } | 324 } |
| 323 | 325 |
| 324 // static | 326 // static |
| 325 void URLRequestTimeoutOnDemandJob::FailOrAbandonJobsOnIOThread( | 327 void URLRequestTimeoutOnDemandJob::FailOrAbandonJobsOnIOThread( |
| 326 int expected_num_jobs, | 328 int expected_num_jobs, |
| 327 EndJobOperation end_job_operation) { | 329 EndJobOperation end_job_operation, |
| 330 const net::SSLInfo& ssl_info) { |
| 328 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 331 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 329 ASSERT_LT(0, expected_num_jobs); | 332 ASSERT_LT(0, expected_num_jobs); |
| 330 EXPECT_EQ(last_num_jobs_to_wait_for_, expected_num_jobs); | 333 EXPECT_EQ(last_num_jobs_to_wait_for_, expected_num_jobs); |
| 331 last_num_jobs_to_wait_for_ = 0; | 334 last_num_jobs_to_wait_for_ = 0; |
| 332 | 335 |
| 333 int num_jobs = 0; | 336 int num_jobs = 0; |
| 334 while (job_list_) { | 337 while (job_list_) { |
| 335 ++num_jobs; | 338 ++num_jobs; |
| 336 URLRequestTimeoutOnDemandJob* job = job_list_; | 339 URLRequestTimeoutOnDemandJob* job = job_list_; |
| 337 // Since the error notification may result in the job's destruction, remove | 340 // Since the error notification may result in the job's destruction, remove |
| 338 // it from the job list before the error. | 341 // it from the job list before the error. |
| 339 EXPECT_TRUE(job->RemoveFromList()); | 342 EXPECT_TRUE(job->RemoveFromList()); |
| 340 if (end_job_operation == FAIL_JOBS) { | 343 if (end_job_operation == FAIL_JOBS) { |
| 341 job->NotifyStartError(net::URLRequestStatus( | 344 job->NotifyStartError(net::URLRequestStatus( |
| 342 net::URLRequestStatus::FAILED, | 345 net::URLRequestStatus::FAILED, |
| 343 net::ERR_CONNECTION_TIMED_OUT)); | 346 net::ERR_CONNECTION_TIMED_OUT)); |
| 344 } else if (end_job_operation == FAIL_JOBS_WITH_CERT_ERROR) { | 347 } else if (end_job_operation == FAIL_JOBS_WITH_CERT_ERROR) { |
| 345 DCHECK(job->request()->url().SchemeIsCryptographic()); | 348 DCHECK(job->request()->url().SchemeIsCryptographic()); |
| 346 net::SSLInfo info; | 349 job->NotifySSLCertificateError(ssl_info, true); |
| 347 info.cert_status = net::CERT_STATUS_COMMON_NAME_INVALID; | |
| 348 info.cert = new net::X509Certificate( | |
| 349 "bad.host", "CA", base::Time::Max(), base::Time::Max()); | |
| 350 job->NotifySSLCertificateError(info, true); | |
| 351 } | 350 } |
| 352 } | 351 } |
| 353 | 352 |
| 354 EXPECT_EQ(expected_num_jobs, num_jobs_started_); | 353 EXPECT_EQ(expected_num_jobs, num_jobs_started_); |
| 355 EXPECT_EQ(expected_num_jobs, num_jobs); | 354 EXPECT_EQ(expected_num_jobs, num_jobs); |
| 356 | 355 |
| 357 num_jobs_started_ -= expected_num_jobs; | 356 num_jobs_started_ -= expected_num_jobs; |
| 358 } | 357 } |
| 359 | 358 |
| 360 // URLRequestCaptivePortalJobFactory emulates captive portal behavior. | 359 // URLRequestCaptivePortalJobFactory emulates captive portal behavior. |
| (...skipping 2495 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2856 WebContents* broken_tab_contents = tab_strip_model->GetActiveWebContents(); | 2855 WebContents* broken_tab_contents = tab_strip_model->GetActiveWebContents(); |
| 2857 SlowLoadBehindCaptivePortal(browser(), true, cert_error_url, 1, 1); | 2856 SlowLoadBehindCaptivePortal(browser(), true, cert_error_url, 1, 1); |
| 2858 | 2857 |
| 2859 // No longer behind a captive portal. Committing the SSL page should trigger | 2858 // No longer behind a captive portal. Committing the SSL page should trigger |
| 2860 // an SSL interstitial which triggers a new captive portal check. Since there | 2859 // an SSL interstitial which triggers a new captive portal check. Since there |
| 2861 // is no captive portal anymore, should end up with an SSL interstitial. | 2860 // is no captive portal anymore, should end up with an SSL interstitial. |
| 2862 factory_.SetBehindCaptivePortal(false); | 2861 factory_.SetBehindCaptivePortal(false); |
| 2863 | 2862 |
| 2864 CaptivePortalObserver portal_observer(browser()->profile()); | 2863 CaptivePortalObserver portal_observer(browser()->profile()); |
| 2865 MultiNavigationObserver navigation_observer; | 2864 MultiNavigationObserver navigation_observer; |
| 2866 URLRequestTimeoutOnDemandJob::FailJobsWithCertError(1); | 2865 net::SSLInfo info; |
| 2866 info.cert_status = net::CERT_STATUS_COMMON_NAME_INVALID; |
| 2867 info.cert = |
| 2868 net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem"); |
| 2869 URLRequestTimeoutOnDemandJob::FailJobsWithCertError(1, info); |
| 2867 navigation_observer.WaitForNavigations(1); | 2870 navigation_observer.WaitForNavigations(1); |
| 2868 | 2871 |
| 2869 EXPECT_EQ(CaptivePortalTabReloader::STATE_NEEDS_RELOAD, | 2872 EXPECT_EQ(CaptivePortalTabReloader::STATE_NEEDS_RELOAD, |
| 2870 GetStateOfTabReloaderAt(browser(), broken_tab_index)); | 2873 GetStateOfTabReloaderAt(browser(), broken_tab_index)); |
| 2871 | 2874 |
| 2872 WaitForInterstitialAttach(broken_tab_contents); | 2875 WaitForInterstitialAttach(broken_tab_contents); |
| 2873 portal_observer.WaitForResults(1); | 2876 portal_observer.WaitForResults(1); |
| 2874 | 2877 |
| 2875 EXPECT_EQ(SSLBlockingPage::kTypeForTesting, | 2878 EXPECT_EQ(SSLBlockingPage::kTypeForTesting, |
| 2876 GetInterstitialType(broken_tab_contents)); | 2879 GetInterstitialType(broken_tab_contents)); |
| 2877 } | 2880 } |
| OLD | NEW |