OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/safe_browsing/certificate_reporting_service.h" |
| 6 |
| 7 #include "base/base_switches.h" |
| 8 #include "base/command_line.h" |
| 9 #include "base/macros.h" |
| 10 #include "base/test/histogram_tester.h" |
| 11 #include "base/test/simple_test_clock.h" |
| 12 #include "base/test/thread_test_helper.h" |
| 13 #include "base/time/clock.h" |
| 14 #include "base/time/time.h" |
| 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/safe_browsing/certificate_reporting_service_factory.h" |
| 17 #include "chrome/browser/safe_browsing/certificate_reporting_service_test_utils.
h" |
| 18 #include "chrome/browser/ssl/certificate_reporting_test_utils.h" |
| 19 #include "chrome/browser/ui/browser.h" |
| 20 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 21 #include "chrome/common/pref_names.h" |
| 22 #include "chrome/test/base/in_process_browser_test.h" |
| 23 #include "chrome/test/base/ui_test_utils.h" |
| 24 #include "components/certificate_reporting/error_report.h" |
| 25 #include "components/prefs/pref_service.h" |
| 26 #include "components/variations/variations_switches.h" |
| 27 #include "content/public/browser/web_contents.h" |
| 28 #include "content/public/test/browser_test_utils.h" |
| 29 #include "content/public/test/test_utils.h" |
| 30 #include "net/dns/mock_host_resolver.h" |
| 31 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 32 #include "net/url_request/report_sender.h" |
| 33 #include "net/url_request/url_request_context_getter.h" |
| 34 #include "net/url_request/url_request_filter.h" |
| 35 #include "net/url_request/url_request_test_util.h" |
| 36 #include "url/scheme_host_port.h" |
| 37 |
| 38 using certificate_reporting_test_utils::CertificateReportingServiceTestHelper; |
| 39 using certificate_reporting_test_utils::ReportExpectation; |
| 40 |
| 41 namespace { |
| 42 |
| 43 const char* kFailedReportHistogram = "SSL.CertificateErrorReportFailure"; |
| 44 |
| 45 void CleanUpOnIOThread() { |
| 46 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 47 net::URLRequestFilter::GetInstance()->ClearHandlers(); |
| 48 } |
| 49 |
| 50 } // namespace |
| 51 |
| 52 namespace safe_browsing { |
| 53 |
| 54 // These tests check the whole mechanism to send and queue invalid certificate |
| 55 // reports. Each test triggers reports by visiting broken SSL pages. The reports |
| 56 // succeed, fail or hang indefinitely. The test waits for the URL requests |
| 57 // corresponding to the reports to to be created via the URL request |
| 58 // interceptor. When reports are expected to succeed or fail, test teardown |
| 59 // checks that there are no in-flight or pending reports in the |
| 60 // CertificateReportingService queue. When a report is to be delayed, a single |
| 61 // in-flight report is expected in CertificateReportingService. Since the actual |
| 62 // URL requests for reports are sent from the IO thread, the tests wait for the |
| 63 // IO thread to finish before checking the expected report counts. |
| 64 // |
| 65 // Note that these browser tests differ from the unit tests in how they check |
| 66 // expected reports: Unit tests create a network delegate and observe the |
| 67 // destruction of the URL requests, whereas browser tests wait for the URL |
| 68 // requests to be created instead. |
| 69 class CertificateReportingServiceBrowserTest : public InProcessBrowserTest { |
| 70 public: |
| 71 CertificateReportingServiceBrowserTest() |
| 72 : https_server_(net::EmbeddedTestServer::TYPE_HTTPS), |
| 73 expect_delayed_report_on_teardown_(false) {} |
| 74 |
| 75 void SetUpOnMainThread() override { |
| 76 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 77 host_resolver()->AddRule("*", "127.0.0.1"); |
| 78 |
| 79 https_server_.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 80 https_server_.ServeFilesFromSourceDirectory("chrome/test/data"); |
| 81 ASSERT_TRUE(https_server_.Start()); |
| 82 |
| 83 test_helper_.SetUpInterceptor(); |
| 84 |
| 85 CertificateReportingServiceFactory::GetInstance() |
| 86 ->SetReportEncryptionParamsForTesting( |
| 87 test_helper_.server_public_key(), |
| 88 test_helper_.server_public_key_version()); |
| 89 InProcessBrowserTest::SetUpOnMainThread(); |
| 90 } |
| 91 |
| 92 void TearDownOnMainThread() override { |
| 93 CheckExpectedReportCounts(expect_delayed_report_on_teardown_); |
| 94 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, |
| 95 base::Bind(&CleanUpOnIOThread)); |
| 96 // Check the histogram as the last thing. This makes sure no in-flight |
| 97 // report is missed. |
| 98 if (num_expected_failed_report_ != 0) { |
| 99 histogram_tester_.ExpectUniqueSample(kFailedReportHistogram, |
| 100 -net::ERR_SSL_PROTOCOL_ERROR, |
| 101 num_expected_failed_report_); |
| 102 } else { |
| 103 histogram_tester_.ExpectTotalCount(kFailedReportHistogram, 0); |
| 104 } |
| 105 } |
| 106 |
| 107 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 108 command_line->AppendSwitchASCII( |
| 109 switches::kForceFieldTrials, |
| 110 "ReportCertificateErrors/ShowAndPossiblySend/"); |
| 111 // Setting the sending threshold to 1.0 ensures reporting is enabled. |
| 112 command_line->AppendSwitchASCII( |
| 113 variations::switches::kForceFieldTrialParams, |
| 114 "ReportCertificateErrors.ShowAndPossiblySend:sendingThreshold/1.0"); |
| 115 } |
| 116 |
| 117 CertificateReportingServiceTestHelper* test_helper() { return &test_helper_; } |
| 118 |
| 119 protected: |
| 120 CertificateReportingServiceFactory* factory() { |
| 121 return CertificateReportingServiceFactory::GetInstance(); |
| 122 } |
| 123 |
| 124 // Sends a report using the provided hostname. Navigates to an interstitial |
| 125 // page on this hostname and away from it to trigger a report. |
| 126 void SendReport(const std::string& hostname) { |
| 127 // Create an HTTPS URL from the hostname. This will resolve to the HTTPS |
| 128 // server and cause an SSL error. |
| 129 const GURL kCertErrorURL( |
| 130 url::SchemeHostPort("https", hostname, https_server_.port()).GetURL()); |
| 131 |
| 132 // Navigate to the page with SSL error. |
| 133 TabStripModel* tab_strip_model = browser()->tab_strip_model(); |
| 134 content::WebContents* contents = tab_strip_model->GetActiveWebContents(); |
| 135 ui_test_utils::NavigateToURL(browser(), kCertErrorURL); |
| 136 content::WaitForInterstitialAttach(contents); |
| 137 |
| 138 // Navigate away from the interstitial to trigger report upload. |
| 139 ui_test_utils::NavigateToURL(browser(), GURL("about:blank")); |
| 140 content::WaitForInterstitialDetach(contents); |
| 141 WaitForIOThread(); |
| 142 } |
| 143 |
| 144 void SendPendingReports() { service()->SendPending(); } |
| 145 |
| 146 // Checks that there are no outstanding reports. |
| 147 // If |expect_delayed_report_on_teardown| is true, expects a single delayed |
| 148 // report. |
| 149 void CheckNoReports() { CheckExpectedReportCounts(false); } |
| 150 |
| 151 // Changes opt-in status and waits for the cert reporting service to reset. |
| 152 // Can only be used after the service is initialized. When changing the |
| 153 // value at the beginning of a test, |
| 154 // certificate_reporting_test_utils::SetCertReportingOptIn should be used |
| 155 // instead since the service is only created upon first SSL error. |
| 156 // Changing the opt-in status synchronously fires |
| 157 // CertificateReportingService::PreferenceObserver::OnPreferenceChanged which |
| 158 // will call CertificateReportingService::SetEnabled() which in turn posts |
| 159 // a task to the IO thread to reset the service. Waiting for the IO thread |
| 160 // ensures that the service is reset before returning from this method. |
| 161 void ChangeOptInAndWait(certificate_reporting_test_utils::OptIn opt_in) { |
| 162 certificate_reporting_test_utils::SetCertReportingOptIn(browser(), opt_in); |
| 163 WaitForIOThread(); |
| 164 } |
| 165 |
| 166 // Same as ChangeOptInAndWait, but enables/disables SafeBrowsing instead. |
| 167 void ToggleSafeBrowsingAndWaitForServiceReset(bool safebrowsing_enabled) { |
| 168 browser()->profile()->GetPrefs()->SetBoolean(prefs::kSafeBrowsingEnabled, |
| 169 safebrowsing_enabled); |
| 170 WaitForIOThread(); |
| 171 } |
| 172 |
| 173 void ShutdownServiceAndWait() { |
| 174 service()->Shutdown(); |
| 175 WaitForIOThread(); |
| 176 } |
| 177 |
| 178 // Waits for a number of URL requests to be created for the reports in |
| 179 // |expectation| and checks that the reports in |expectation| matches the |
| 180 // reports observed by URL request interceptor. |
| 181 void WaitForReports(const ReportExpectation& expectation) { |
| 182 test_helper_.interceptor()->WaitForReports(expectation.num_reports()); |
| 183 std::set<std::string> expected_hostnames; |
| 184 CheckReports(expectation.successful_reports, |
| 185 test_helper_.interceptor()->successful_reports()); |
| 186 CheckReports(expectation.failed_reports, |
| 187 test_helper_.interceptor()->failed_reports()); |
| 188 CheckReports(expectation.delayed_reports, |
| 189 test_helper_.interceptor()->delayed_reports()); |
| 190 test_helper_.interceptor()->ClearObservedReports(); |
| 191 } |
| 192 |
| 193 // Resumes the delayed request and waits for the resume task to complete which |
| 194 // in turn means the response starts. |
| 195 void ResumeDelayedRequestAndWait() { |
| 196 base::RunLoop run_loop; |
| 197 test_helper_.ResumeDelayedRequest(run_loop.QuitClosure()); |
| 198 run_loop.Run(); |
| 199 } |
| 200 |
| 201 // Tells the test to expect a delayed report during test teardown. If not set, |
| 202 // the tests expect no in-flight reports during teardown. |
| 203 void SetExpectDelayedReportOnTeardown() { |
| 204 expect_delayed_report_on_teardown_ = true; |
| 205 } |
| 206 |
| 207 void SetExpectedHistogramCountOnTeardown( |
| 208 unsigned int num_expected_failed_report) { |
| 209 num_expected_failed_report_ = num_expected_failed_report; |
| 210 } |
| 211 |
| 212 private: |
| 213 CertificateReportingService* service() const { |
| 214 return CertificateReportingServiceFactory::GetForBrowserContext( |
| 215 browser()->profile()); |
| 216 } |
| 217 |
| 218 // Waits for pending tasks on the IO thread to complete. |
| 219 void WaitForIOThread() { |
| 220 scoped_refptr<base::ThreadTestHelper> io_helper(new base::ThreadTestHelper( |
| 221 content::BrowserThread::GetTaskRunnerForThread( |
| 222 content::BrowserThread::IO) |
| 223 .get())); |
| 224 ASSERT_TRUE(io_helper->Run()); |
| 225 } |
| 226 |
| 227 // Checks that the serialized reports in |received_reports| have the same |
| 228 // hostnames as |expected_hostnames|. |
| 229 void CheckReports(const std::set<std::string>& expected_hostnames, |
| 230 const std::set<std::string>& received_reports) { |
| 231 std::set<std::string> received_hostnames; |
| 232 for (const std::string& serialized_report : received_reports) { |
| 233 certificate_reporting::ErrorReport report; |
| 234 ASSERT_TRUE(report.InitializeFromString(serialized_report)); |
| 235 received_hostnames.insert(report.hostname()); |
| 236 } |
| 237 EXPECT_EQ(expected_hostnames, received_hostnames); |
| 238 } |
| 239 |
| 240 // Checks that there are no remaining successful and failed reports observed |
| 241 // by the interceptor. If |expect_delayed_report| is true, expects a single |
| 242 // delayed report. Otherwise, expects no delayed reports. |
| 243 void CheckExpectedReportCounts(bool expect_delayed_report) { |
| 244 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 245 // Wait for the IO thread to ensure that any report-sending tasks previously |
| 246 // posted to the IO thread hav run (and thus been observed by the |
| 247 // interceptor). |
| 248 WaitForIOThread(); |
| 249 EXPECT_TRUE(test_helper_.interceptor()->successful_reports().empty()); |
| 250 EXPECT_TRUE(test_helper_.interceptor()->failed_reports().empty()); |
| 251 |
| 252 if (expect_delayed_report) |
| 253 EXPECT_EQ(1u, test_helper_.interceptor()->delayed_reports().size()); |
| 254 else |
| 255 EXPECT_TRUE(test_helper_.interceptor()->delayed_reports().empty()); |
| 256 |
| 257 if (service()->GetReporterForTesting()) { |
| 258 // Reporter can be null if reporting is disabled. |
| 259 size_t num_inflight_reports = expect_delayed_report ? 1u : 0u; |
| 260 EXPECT_EQ(num_inflight_reports, |
| 261 service() |
| 262 ->GetReporterForTesting() |
| 263 ->inflight_report_count_for_testing()); |
| 264 } |
| 265 } |
| 266 |
| 267 net::EmbeddedTestServer https_server_; |
| 268 // If true, the test will expect to see a delayed report during test teardown. |
| 269 bool expect_delayed_report_on_teardown_ = false; |
| 270 |
| 271 unsigned int num_expected_failed_report_ = 0; |
| 272 |
| 273 CertificateReportingServiceTestHelper test_helper_; |
| 274 |
| 275 base::HistogramTester histogram_tester_; |
| 276 |
| 277 DISALLOW_COPY_AND_ASSIGN(CertificateReportingServiceBrowserTest); |
| 278 }; |
| 279 |
| 280 // Tests that report send attempt should be cancelled when extended |
| 281 // reporting is not opted in. |
| 282 IN_PROC_BROWSER_TEST_F(CertificateReportingServiceBrowserTest, |
| 283 NotOptedIn_ShouldNotSendReports) { |
| 284 SetExpectedHistogramCountOnTeardown(0); |
| 285 |
| 286 certificate_reporting_test_utils::SetCertReportingOptIn( |
| 287 browser(), |
| 288 certificate_reporting_test_utils::EXTENDED_REPORTING_DO_NOT_OPT_IN); |
| 289 // Send a report. Test teardown checks for created and in-flight requests. If |
| 290 // a report was incorrectly sent, the test will fail. |
| 291 SendReport("no-report"); |
| 292 } |
| 293 |
| 294 // Tests that report send attempts are not cancelled when extended reporting is |
| 295 // opted in. Goes to an interstitial page and navigates away to force a report |
| 296 // send event. |
| 297 IN_PROC_BROWSER_TEST_F(CertificateReportingServiceBrowserTest, |
| 298 OptedIn_ShouldSendSuccessfulReport) { |
| 299 SetExpectedHistogramCountOnTeardown(0); |
| 300 |
| 301 certificate_reporting_test_utils::SetCertReportingOptIn( |
| 302 browser(), certificate_reporting_test_utils::EXTENDED_REPORTING_OPT_IN); |
| 303 |
| 304 // Let reports uploads successfully complete. |
| 305 test_helper()->SetFailureMode(certificate_reporting_test_utils:: |
| 306 ReportSendingResult::REPORTS_SUCCESSFUL); |
| 307 |
| 308 // Reporting is opted in, so the report should succeed. |
| 309 SendReport("report0"); |
| 310 WaitForReports(ReportExpectation::Successful({"report0"})); |
| 311 } |
| 312 |
| 313 // Tests that report send attempts are not cancelled when extended reporting is |
| 314 // opted in. Goes to an interstitial page and navigate away to force a report |
| 315 // send event. Repeats this three times and checks expected number of reports. |
| 316 IN_PROC_BROWSER_TEST_F(CertificateReportingServiceBrowserTest, |
| 317 OptedIn_ShouldQueueFailedReport) { |
| 318 SetExpectedHistogramCountOnTeardown(2); |
| 319 |
| 320 certificate_reporting_test_utils::SetCertReportingOptIn( |
| 321 browser(), certificate_reporting_test_utils::EXTENDED_REPORTING_OPT_IN); |
| 322 // Let all reports fail. |
| 323 test_helper()->SetFailureMode( |
| 324 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); |
| 325 |
| 326 // Send a failed report. |
| 327 SendReport("report0"); |
| 328 WaitForReports(ReportExpectation::Failed({"report0"})); |
| 329 |
| 330 // Send another failed report. |
| 331 SendReport("report1"); |
| 332 WaitForReports(ReportExpectation::Failed({"report1"})); |
| 333 |
| 334 // Let all report uploads complete successfully now. |
| 335 test_helper()->SetFailureMode(certificate_reporting_test_utils:: |
| 336 ReportSendingResult::REPORTS_SUCCESSFUL); |
| 337 |
| 338 // Send another report. This time the report should be successfully sent. |
| 339 SendReport("report2"); |
| 340 WaitForReports(ReportExpectation::Successful({"report2"})); |
| 341 |
| 342 // Send all pending reports. The two previously failed reports should have |
| 343 // been queued, and now be sent successfully. |
| 344 SendPendingReports(); |
| 345 WaitForReports(ReportExpectation::Successful({"report0", "report1"})); |
| 346 |
| 347 // Try sending pending reports again. Since there is no pending report, |
| 348 // nothing should be sent this time. If any report is sent, test teardown |
| 349 // will catch it. |
| 350 SendPendingReports(); |
| 351 } |
| 352 |
| 353 // Opting in then opting out of extended reporting should clear the pending |
| 354 // report queue. |
| 355 IN_PROC_BROWSER_TEST_F(CertificateReportingServiceBrowserTest, |
| 356 OptedIn_ThenOptedOut) { |
| 357 SetExpectedHistogramCountOnTeardown(1); |
| 358 |
| 359 certificate_reporting_test_utils::SetCertReportingOptIn( |
| 360 browser(), certificate_reporting_test_utils::EXTENDED_REPORTING_OPT_IN); |
| 361 // Let all reports fail. |
| 362 test_helper()->SetFailureMode( |
| 363 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); |
| 364 |
| 365 // Send a failed report. |
| 366 SendReport("report0"); |
| 367 WaitForReports(ReportExpectation::Failed({"report0"})); |
| 368 |
| 369 // Disable reporting. This should clear all pending reports. |
| 370 ChangeOptInAndWait( |
| 371 certificate_reporting_test_utils::EXTENDED_REPORTING_DO_NOT_OPT_IN); |
| 372 |
| 373 // Send pending reports. No reports should be observed during test teardown. |
| 374 SendPendingReports(); |
| 375 } |
| 376 |
| 377 // Opting out, then in, then out of extended reporting should work as expected. |
| 378 IN_PROC_BROWSER_TEST_F(CertificateReportingServiceBrowserTest, |
| 379 OptedOut_ThenOptedIn_ThenOptedOut) { |
| 380 SetExpectedHistogramCountOnTeardown(1); |
| 381 |
| 382 certificate_reporting_test_utils::SetCertReportingOptIn( |
| 383 browser(), |
| 384 certificate_reporting_test_utils::EXTENDED_REPORTING_DO_NOT_OPT_IN); |
| 385 // Let all reports fail. |
| 386 test_helper()->SetFailureMode( |
| 387 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); |
| 388 |
| 389 // Send attempt should be cancelled since reporting is opted out. |
| 390 SendReport("no-report"); |
| 391 CheckNoReports(); |
| 392 |
| 393 // Enable reporting. |
| 394 ChangeOptInAndWait( |
| 395 certificate_reporting_test_utils::EXTENDED_REPORTING_OPT_IN); |
| 396 |
| 397 // A failed report should be observed. |
| 398 SendReport("report0"); |
| 399 WaitForReports(ReportExpectation::Failed({"report0"})); |
| 400 |
| 401 // Disable reporting. This should reset the reporting service and |
| 402 // clear all pending reports. |
| 403 ChangeOptInAndWait( |
| 404 certificate_reporting_test_utils::EXTENDED_REPORTING_DO_NOT_OPT_IN); |
| 405 |
| 406 // Report should be cancelled since reporting is opted out. |
| 407 SendReport("report1"); |
| 408 CheckNoReports(); |
| 409 |
| 410 // Send pending reports. Nothing should be sent since there aren't any |
| 411 // pending reports. If any report is sent, test teardown will catch it. |
| 412 SendPendingReports(); |
| 413 } |
| 414 |
| 415 // Disabling SafeBrowsing should clear pending reports queue in |
| 416 // CertificateReportingService. |
| 417 IN_PROC_BROWSER_TEST_F(CertificateReportingServiceBrowserTest, |
| 418 DisableSafebrowsing) { |
| 419 SetExpectedHistogramCountOnTeardown(2); |
| 420 |
| 421 certificate_reporting_test_utils::SetCertReportingOptIn( |
| 422 browser(), certificate_reporting_test_utils::EXTENDED_REPORTING_OPT_IN); |
| 423 // Let all reports fail. |
| 424 test_helper()->SetFailureMode( |
| 425 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); |
| 426 |
| 427 // Send a failed report. |
| 428 SendReport("report0"); |
| 429 WaitForReports(ReportExpectation::Failed({"report0"})); |
| 430 |
| 431 // Disable SafeBrowsing. This should clear all pending reports. |
| 432 ToggleSafeBrowsingAndWaitForServiceReset(false); |
| 433 |
| 434 // Send pending reports. No reports should be observed. |
| 435 SendPendingReports(); |
| 436 CheckNoReports(); |
| 437 |
| 438 // Re-enable SafeBrowsing and trigger another report which will be queued. |
| 439 ToggleSafeBrowsingAndWaitForServiceReset(true); |
| 440 SendReport("report1"); |
| 441 WaitForReports(ReportExpectation::Failed({"report1"})); |
| 442 |
| 443 // Queued report should now be successfully sent. |
| 444 test_helper()->SetFailureMode(certificate_reporting_test_utils:: |
| 445 ReportSendingResult::REPORTS_SUCCESSFUL); |
| 446 SendPendingReports(); |
| 447 WaitForReports(ReportExpectation::Successful({"report1"})); |
| 448 } |
| 449 |
| 450 // CertificateReportingService should ignore reports older than the report TTL. |
| 451 IN_PROC_BROWSER_TEST_F(CertificateReportingServiceBrowserTest, |
| 452 DontSendOldReports) { |
| 453 SetExpectedHistogramCountOnTeardown(5); |
| 454 |
| 455 base::SimpleTestClock* clock = new base::SimpleTestClock(); |
| 456 base::Time reference_time = base::Time::Now(); |
| 457 clock->SetNow(reference_time); |
| 458 factory()->SetClockForTesting(std::unique_ptr<base::Clock>(clock)); |
| 459 |
| 460 // The service should ignore reports older than 24 hours. |
| 461 factory()->SetQueuedReportTTLForTesting(base::TimeDelta::FromHours(24)); |
| 462 |
| 463 certificate_reporting_test_utils::SetCertReportingOptIn( |
| 464 browser(), certificate_reporting_test_utils::EXTENDED_REPORTING_OPT_IN); |
| 465 |
| 466 // Let all reports fail. |
| 467 test_helper()->SetFailureMode( |
| 468 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); |
| 469 |
| 470 // Send a failed report. |
| 471 SendReport("report0"); |
| 472 WaitForReports(ReportExpectation::Failed({"report0"})); |
| 473 |
| 474 // Advance the clock a bit and trigger another failed report. |
| 475 clock->Advance(base::TimeDelta::FromHours(5)); |
| 476 SendReport("report1"); |
| 477 WaitForReports(ReportExpectation::Failed({"report1"})); |
| 478 |
| 479 // Advance the clock to 20 hours, putting it 25 hours ahead of the reference |
| 480 // time. This makes report0 older than 24 hours. report1 is now 20 hours. |
| 481 clock->Advance(base::TimeDelta::FromHours(20)); |
| 482 |
| 483 // Send pending reports. report0 should be discarded since it's too old. |
| 484 // report1 should be queued again. |
| 485 SendPendingReports(); |
| 486 WaitForReports(ReportExpectation::Failed({"report1"})); |
| 487 |
| 488 // Trigger another failed report. |
| 489 SendReport("report2"); |
| 490 WaitForReports(ReportExpectation::Failed({"report2"})); |
| 491 |
| 492 // Advance the clock 5 hours. report1 will now be 25 hours old. |
| 493 clock->Advance(base::TimeDelta::FromHours(5)); |
| 494 |
| 495 // Send pending reports. report1 should be discarded since it's too old. |
| 496 // report2 should be queued again. |
| 497 SendPendingReports(); |
| 498 WaitForReports(ReportExpectation::Failed({"report2"})); |
| 499 |
| 500 // Advance the clock 20 hours again so that report2 is 25 hours old and is |
| 501 // older than max age (24 hours). |
| 502 clock->Advance(base::TimeDelta::FromHours(20)); |
| 503 |
| 504 // Send pending reports. report2 should be discarded since it's too old. No |
| 505 // other reports remain. If any report is sent, test teardown will catch it. |
| 506 SendPendingReports(); |
| 507 } |
| 508 |
| 509 // CertificateReportingService should drop old reports from its pending report |
| 510 // queue, if the queue is full. |
| 511 IN_PROC_BROWSER_TEST_F(CertificateReportingServiceBrowserTest, |
| 512 DropOldReportsFromQueue) { |
| 513 SetExpectedHistogramCountOnTeardown(7); |
| 514 |
| 515 base::SimpleTestClock* clock = new base::SimpleTestClock(); |
| 516 base::Time reference_time = base::Time::Now(); |
| 517 clock->SetNow(reference_time); |
| 518 factory()->SetClockForTesting(std::unique_ptr<base::Clock>(clock)); |
| 519 |
| 520 // The service should queue a maximum of 3 reports and ignore reports older |
| 521 // than 24 hours. |
| 522 factory()->SetQueuedReportTTLForTesting(base::TimeDelta::FromHours(24)); |
| 523 factory()->SetMaxQueuedReportCountForTesting(3); |
| 524 |
| 525 certificate_reporting_test_utils::SetCertReportingOptIn( |
| 526 browser(), certificate_reporting_test_utils::EXTENDED_REPORTING_OPT_IN); |
| 527 |
| 528 // Let all reports fail. |
| 529 test_helper()->SetFailureMode( |
| 530 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); |
| 531 |
| 532 // Trigger a failed report. |
| 533 SendReport("report0"); |
| 534 WaitForReports(ReportExpectation::Failed({"report0"})); |
| 535 |
| 536 // Trigger three more reports within five hours of each other. After this: |
| 537 // report0 is 0 hours after reference time (15 hours old). |
| 538 // report1 is 5 hours after reference time (10 hours old). |
| 539 // report2 is 10 hours after reference time (5 hours old). |
| 540 // report3 is 15 hours after reference time (0 hours old). |
| 541 clock->Advance(base::TimeDelta::FromHours(5)); |
| 542 SendReport("report1"); |
| 543 |
| 544 clock->Advance(base::TimeDelta::FromHours(5)); |
| 545 SendReport("report2"); |
| 546 |
| 547 clock->Advance(base::TimeDelta::FromHours(5)); |
| 548 SendReport("report3"); |
| 549 |
| 550 WaitForReports(ReportExpectation::Failed({"report1", "report2", "report3"})); |
| 551 |
| 552 // Send pending reports. Four reports were generated above, but the service |
| 553 // only queues three reports, so report0 should be dropped since it's the |
| 554 // oldest. |
| 555 SendPendingReports(); |
| 556 WaitForReports(ReportExpectation::Failed({"report1", "report2", "report3"})); |
| 557 |
| 558 // Let all reports succeed. |
| 559 test_helper()->SetFailureMode(certificate_reporting_test_utils:: |
| 560 ReportSendingResult::REPORTS_SUCCESSFUL); |
| 561 |
| 562 // Advance the clock 15 hours. Current time is now 30 hours after reference |
| 563 // time, and the ages of reports are now as follows: |
| 564 // report1 is 25 hours old. |
| 565 // report2 is 20 hours old. |
| 566 // report3 is 15 hours old. |
| 567 clock->Advance(base::TimeDelta::FromHours(15)); |
| 568 |
| 569 // Send pending reports. Only reports 2 and 3 should be sent, report 1 |
| 570 // should be ignored because it's too old. |
| 571 SendPendingReports(); |
| 572 WaitForReports(ReportExpectation::Successful({"report2", "report3"})); |
| 573 } |
| 574 |
| 575 // Resume a delayed report after CertificateReportingService shuts down. Should |
| 576 // not crash. |
| 577 IN_PROC_BROWSER_TEST_F(CertificateReportingServiceBrowserTest, |
| 578 Delayed_NotResumed_ShouldNotCrash) { |
| 579 SetExpectedHistogramCountOnTeardown(0); |
| 580 |
| 581 certificate_reporting_test_utils::SetCertReportingOptIn( |
| 582 browser(), certificate_reporting_test_utils::EXTENDED_REPORTING_OPT_IN); |
| 583 // Let reports hang. |
| 584 test_helper()->SetFailureMode( |
| 585 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY); |
| 586 |
| 587 // Navigate to and away from an interstitial to trigger a report. The report |
| 588 // is triggered but hangs, so no error or success callbacks should be called. |
| 589 SendReport("no-report"); |
| 590 |
| 591 SetExpectDelayedReportOnTeardown(); |
| 592 } |
| 593 |
| 594 IN_PROC_BROWSER_TEST_F(CertificateReportingServiceBrowserTest, |
| 595 Delayed_Resumed) { |
| 596 SetExpectedHistogramCountOnTeardown(0); |
| 597 |
| 598 certificate_reporting_test_utils::SetCertReportingOptIn( |
| 599 browser(), certificate_reporting_test_utils::EXTENDED_REPORTING_OPT_IN); |
| 600 // Let all reports fail. |
| 601 test_helper()->SetFailureMode( |
| 602 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY); |
| 603 |
| 604 // Trigger a report that hangs. |
| 605 SendReport("report0"); |
| 606 WaitForReports(ReportExpectation::Delayed({"report0"})); |
| 607 |
| 608 // Resume the report upload. The report upload should successfully complete. |
| 609 // The interceptor only observes request creations and not response |
| 610 // completions, so there is nothing to observe. |
| 611 ResumeDelayedRequestAndWait(); |
| 612 } |
| 613 |
| 614 // Same as above, but the service is shut down before resuming the delayed |
| 615 // request. Should not crash. |
| 616 IN_PROC_BROWSER_TEST_F(CertificateReportingServiceBrowserTest, |
| 617 Delayed_Resumed_ServiceShutdown) { |
| 618 SetExpectedHistogramCountOnTeardown(0); |
| 619 |
| 620 certificate_reporting_test_utils::SetCertReportingOptIn( |
| 621 browser(), certificate_reporting_test_utils::EXTENDED_REPORTING_OPT_IN); |
| 622 // Let all reports fail. |
| 623 test_helper()->SetFailureMode( |
| 624 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY); |
| 625 |
| 626 // Trigger a report that hangs. |
| 627 SendReport("report0"); |
| 628 WaitForReports(ReportExpectation::Delayed({"report0"})); |
| 629 |
| 630 // Shutdown the service. Resuming the delayed request shouldn't crash. |
| 631 ShutdownServiceAndWait(); |
| 632 |
| 633 // Resume the report upload. The report upload should successfully complete. |
| 634 // The interceptor only observes request creations and not response |
| 635 // completions, so there is nothing to observe. |
| 636 ResumeDelayedRequestAndWait(); |
| 637 } |
| 638 |
| 639 // Trigger a delayed report, then disable Safebrowsing. Certificate reporting |
| 640 // service should clear its in-flight reports list. |
| 641 IN_PROC_BROWSER_TEST_F(CertificateReportingServiceBrowserTest, Delayed_Reset) { |
| 642 SetExpectedHistogramCountOnTeardown(0); |
| 643 |
| 644 certificate_reporting_test_utils::SetCertReportingOptIn( |
| 645 browser(), certificate_reporting_test_utils::EXTENDED_REPORTING_OPT_IN); |
| 646 // Let all reports fail. |
| 647 test_helper()->SetFailureMode( |
| 648 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY); |
| 649 |
| 650 // Trigger a report that hangs. |
| 651 SendReport("report0"); |
| 652 WaitForReports(ReportExpectation::Delayed({"report0"})); |
| 653 |
| 654 // Disable SafeBrowsing. This should clear all pending reports. |
| 655 ToggleSafeBrowsingAndWaitForServiceReset(false); |
| 656 |
| 657 // Resume delayed report. No response should be observed since all pending |
| 658 // reports should be cleared. |
| 659 ResumeDelayedRequestAndWait(); |
| 660 CheckNoReports(); |
| 661 |
| 662 // Re-enable SafeBrowsing. |
| 663 ToggleSafeBrowsingAndWaitForServiceReset(true); |
| 664 |
| 665 // Trigger a report that hangs. |
| 666 SendReport("report1"); |
| 667 WaitForReports(ReportExpectation::Delayed({"report1"})); |
| 668 |
| 669 // Resume delayed report. By the time the runloop is finished, the response |
| 670 // will be complete and CertificateReportingService will process the |
| 671 // error/success callback for the report. There will be no inflight reports |
| 672 // remaining. |
| 673 ResumeDelayedRequestAndWait(); |
| 674 } |
| 675 |
| 676 } // namespace safe_browsing |
OLD | NEW |