| 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 WaitForIOThread(); |
| 246 EXPECT_TRUE(test_helper_.interceptor()->successful_reports().empty()); |
| 247 EXPECT_TRUE(test_helper_.interceptor()->failed_reports().empty()); |
| 248 |
| 249 if (expect_delayed_report) |
| 250 EXPECT_EQ(1u, test_helper_.interceptor()->delayed_reports().size()); |
| 251 else |
| 252 EXPECT_TRUE(test_helper_.interceptor()->delayed_reports().empty()); |
| 253 |
| 254 if (service()->GetReporterForTesting()) { |
| 255 // Reporter can be null if reporting is disabled. |
| 256 size_t num_inflight_reports = expect_delayed_report ? 1u : 0u; |
| 257 EXPECT_EQ(num_inflight_reports, |
| 258 service() |
| 259 ->GetReporterForTesting() |
| 260 ->inflight_report_count_for_testing()); |
| 261 } |
| 262 } |
| 263 |
| 264 net::EmbeddedTestServer https_server_; |
| 265 // If true, the test will expect to see a delayed report during test teardown. |
| 266 bool expect_delayed_report_on_teardown_ = false; |
| 267 |
| 268 unsigned int num_expected_failed_report_ = 0; |
| 269 |
| 270 CertificateReportingServiceTestHelper test_helper_; |
| 271 |
| 272 base::HistogramTester histogram_tester_; |
| 273 |
| 274 DISALLOW_COPY_AND_ASSIGN(CertificateReportingServiceBrowserTest); |
| 275 }; |
| 276 |
| 277 // Tests that report send attempt should be cancelled when extended |
| 278 // reporting is not opted in. |
| 279 IN_PROC_BROWSER_TEST_F(CertificateReportingServiceBrowserTest, |
| 280 NotOptedIn_ShouldNotSendReports) { |
| 281 SetExpectedHistogramCountOnTeardown(0); |
| 282 |
| 283 certificate_reporting_test_utils::SetCertReportingOptIn( |
| 284 browser(), |
| 285 certificate_reporting_test_utils::EXTENDED_REPORTING_DO_NOT_OPT_IN); |
| 286 // Send a report. Test teardown checks for created and in-flight requests. If |
| 287 // a report was incorrectly sent, the test will fail. |
| 288 SendReport("no-report"); |
| 289 } |
| 290 |
| 291 // Tests that report send attempts are not cancelled when extended reporting is |
| 292 // opted in. Goes to an interstitial page and navigates away to force a report |
| 293 // send event. |
| 294 IN_PROC_BROWSER_TEST_F(CertificateReportingServiceBrowserTest, |
| 295 OptedIn_ShouldSendSuccessfulReport) { |
| 296 SetExpectedHistogramCountOnTeardown(0); |
| 297 |
| 298 certificate_reporting_test_utils::SetCertReportingOptIn( |
| 299 browser(), certificate_reporting_test_utils::EXTENDED_REPORTING_OPT_IN); |
| 300 |
| 301 // Let reports uploads successfully complete. |
| 302 test_helper()->SetFailureMode(certificate_reporting_test_utils:: |
| 303 ReportSendingResult::REPORTS_SUCCESSFUL); |
| 304 |
| 305 // Reporting is opted in, so the report should succeed. |
| 306 SendReport("report0"); |
| 307 WaitForReports(ReportExpectation::Successful({"report0"})); |
| 308 } |
| 309 |
| 310 // Tests that report send attempts are not cancelled when extended reporting is |
| 311 // opted in. Goes to an interstitial page and navigate away to force a report |
| 312 // send event. Repeats this three times and checks expected number of reports. |
| 313 IN_PROC_BROWSER_TEST_F(CertificateReportingServiceBrowserTest, |
| 314 OptedIn_ShouldQueueFailedReport) { |
| 315 SetExpectedHistogramCountOnTeardown(2); |
| 316 |
| 317 certificate_reporting_test_utils::SetCertReportingOptIn( |
| 318 browser(), certificate_reporting_test_utils::EXTENDED_REPORTING_OPT_IN); |
| 319 // Let all reports fail. |
| 320 test_helper()->SetFailureMode( |
| 321 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); |
| 322 |
| 323 // Send a failed report. |
| 324 SendReport("report0"); |
| 325 WaitForReports(ReportExpectation::Failed({"report0"})); |
| 326 |
| 327 // Send another failed report. |
| 328 SendReport("report1"); |
| 329 WaitForReports(ReportExpectation::Failed({"report1"})); |
| 330 |
| 331 // Let all report uploads complete successfully now. |
| 332 test_helper()->SetFailureMode(certificate_reporting_test_utils:: |
| 333 ReportSendingResult::REPORTS_SUCCESSFUL); |
| 334 |
| 335 // Send another report. This time the report should be successfully sent. |
| 336 SendReport("report2"); |
| 337 WaitForReports(ReportExpectation::Successful({"report2"})); |
| 338 |
| 339 // Send all pending reports. The two previously failed reports should have |
| 340 // been queued, and now be sent successfully. |
| 341 SendPendingReports(); |
| 342 WaitForReports(ReportExpectation::Successful({"report0", "report1"})); |
| 343 |
| 344 // Try sending pending reports again. Since there is no pending report, |
| 345 // nothing should be sent this time. If any report is sent, test teardown |
| 346 // will catch it. |
| 347 SendPendingReports(); |
| 348 } |
| 349 |
| 350 // Opting in then opting out of extended reporting should clear the pending |
| 351 // report queue. |
| 352 IN_PROC_BROWSER_TEST_F(CertificateReportingServiceBrowserTest, |
| 353 OptedIn_ThenOptedOut) { |
| 354 SetExpectedHistogramCountOnTeardown(1); |
| 355 |
| 356 certificate_reporting_test_utils::SetCertReportingOptIn( |
| 357 browser(), certificate_reporting_test_utils::EXTENDED_REPORTING_OPT_IN); |
| 358 // Let all reports fail. |
| 359 test_helper()->SetFailureMode( |
| 360 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); |
| 361 |
| 362 // Send a failed report. |
| 363 SendReport("report0"); |
| 364 WaitForReports(ReportExpectation::Failed({"report0"})); |
| 365 |
| 366 // Disable reporting. This should clear all pending reports. |
| 367 ChangeOptInAndWait( |
| 368 certificate_reporting_test_utils::EXTENDED_REPORTING_DO_NOT_OPT_IN); |
| 369 |
| 370 // Send pending reports. No reports should be observed during test teardown. |
| 371 SendPendingReports(); |
| 372 } |
| 373 |
| 374 // Opting out, then in, then out of extended reporting should work as expected. |
| 375 IN_PROC_BROWSER_TEST_F(CertificateReportingServiceBrowserTest, |
| 376 OptedOut_ThenOptedIn_ThenOptedOut) { |
| 377 SetExpectedHistogramCountOnTeardown(1); |
| 378 |
| 379 certificate_reporting_test_utils::SetCertReportingOptIn( |
| 380 browser(), |
| 381 certificate_reporting_test_utils::EXTENDED_REPORTING_DO_NOT_OPT_IN); |
| 382 // Let all reports fail. |
| 383 test_helper()->SetFailureMode( |
| 384 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); |
| 385 |
| 386 // Send attempt should be cancelled since reporting is opted out. |
| 387 SendReport("no-report"); |
| 388 CheckNoReports(); |
| 389 |
| 390 // Enable reporting. |
| 391 ChangeOptInAndWait( |
| 392 certificate_reporting_test_utils::EXTENDED_REPORTING_OPT_IN); |
| 393 |
| 394 // A failed report should be observed. |
| 395 SendReport("report0"); |
| 396 WaitForReports(ReportExpectation::Failed({"report0"})); |
| 397 |
| 398 // Disable reporting. This should reset the reporting service and |
| 399 // clear all pending reports. |
| 400 ChangeOptInAndWait( |
| 401 certificate_reporting_test_utils::EXTENDED_REPORTING_DO_NOT_OPT_IN); |
| 402 |
| 403 // Report should be cancelled since reporting is opted out. |
| 404 SendReport("report1"); |
| 405 CheckNoReports(); |
| 406 |
| 407 // Send pending reports. Nothing should be sent since there aren't any |
| 408 // pending reports. If any report is sent, test teardown will catch it. |
| 409 SendPendingReports(); |
| 410 } |
| 411 |
| 412 // Disabling SafeBrowsing should clear pending reports queue in |
| 413 // CertificateReportingService. |
| 414 IN_PROC_BROWSER_TEST_F(CertificateReportingServiceBrowserTest, |
| 415 DisableSafebrowsing) { |
| 416 SetExpectedHistogramCountOnTeardown(2); |
| 417 |
| 418 certificate_reporting_test_utils::SetCertReportingOptIn( |
| 419 browser(), certificate_reporting_test_utils::EXTENDED_REPORTING_OPT_IN); |
| 420 // Let all reports fail. |
| 421 test_helper()->SetFailureMode( |
| 422 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); |
| 423 |
| 424 // Send a failed report. |
| 425 SendReport("report0"); |
| 426 WaitForReports(ReportExpectation::Failed({"report0"})); |
| 427 |
| 428 // Disable SafeBrowsing. This should clear all pending reports. |
| 429 ToggleSafeBrowsingAndWaitForServiceReset(false); |
| 430 |
| 431 // Send pending reports. No reports should be observed. |
| 432 SendPendingReports(); |
| 433 CheckNoReports(); |
| 434 |
| 435 // Re-enable SafeBrowsing and trigger another report which will be queued. |
| 436 ToggleSafeBrowsingAndWaitForServiceReset(true); |
| 437 SendReport("report1"); |
| 438 WaitForReports(ReportExpectation::Failed({"report1"})); |
| 439 |
| 440 // Queued report should now be successfully sent. |
| 441 test_helper()->SetFailureMode(certificate_reporting_test_utils:: |
| 442 ReportSendingResult::REPORTS_SUCCESSFUL); |
| 443 SendPendingReports(); |
| 444 WaitForReports(ReportExpectation::Successful({"report1"})); |
| 445 } |
| 446 |
| 447 // CertificateReportingService should ignore reports older than the report TTL. |
| 448 IN_PROC_BROWSER_TEST_F(CertificateReportingServiceBrowserTest, |
| 449 DontSendOldReports) { |
| 450 SetExpectedHistogramCountOnTeardown(5); |
| 451 |
| 452 base::SimpleTestClock* clock = new base::SimpleTestClock(); |
| 453 base::Time reference_time = base::Time::Now(); |
| 454 clock->SetNow(reference_time); |
| 455 factory()->SetClockForTesting(std::unique_ptr<base::Clock>(clock)); |
| 456 |
| 457 // The service should ignore reports older than 24 hours. |
| 458 factory()->SetQueuedReportTTLForTesting(base::TimeDelta::FromHours(24)); |
| 459 |
| 460 certificate_reporting_test_utils::SetCertReportingOptIn( |
| 461 browser(), certificate_reporting_test_utils::EXTENDED_REPORTING_OPT_IN); |
| 462 |
| 463 // Let all reports fail. |
| 464 test_helper()->SetFailureMode( |
| 465 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); |
| 466 |
| 467 // Send a failed report. |
| 468 SendReport("report0"); |
| 469 WaitForReports(ReportExpectation::Failed({"report0"})); |
| 470 |
| 471 // Advance the clock a bit and trigger another failed report. |
| 472 clock->Advance(base::TimeDelta::FromHours(5)); |
| 473 SendReport("report1"); |
| 474 WaitForReports(ReportExpectation::Failed({"report1"})); |
| 475 |
| 476 // Advance the clock to 20 hours, putting it 25 hours ahead of the reference |
| 477 // time. This makes report0 older than 24 hours. report1 is now 20 hours. |
| 478 clock->Advance(base::TimeDelta::FromHours(20)); |
| 479 |
| 480 // Send pending reports. report0 should be discarded since it's too old. |
| 481 // report1 should be queued again. |
| 482 SendPendingReports(); |
| 483 WaitForReports(ReportExpectation::Failed({"report1"})); |
| 484 |
| 485 // Trigger another failed report. |
| 486 SendReport("report2"); |
| 487 WaitForReports(ReportExpectation::Failed({"report2"})); |
| 488 |
| 489 // Advance the clock 5 hours. report1 will now be 25 hours old. |
| 490 clock->Advance(base::TimeDelta::FromHours(5)); |
| 491 |
| 492 // Send pending reports. report1 should be discarded since it's too old. |
| 493 // report2 should be queued again. |
| 494 SendPendingReports(); |
| 495 WaitForReports(ReportExpectation::Failed({"report2"})); |
| 496 |
| 497 // Advance the clock 20 hours again so that report2 is 25 hours old and is |
| 498 // older than max age (24 hours). |
| 499 clock->Advance(base::TimeDelta::FromHours(20)); |
| 500 |
| 501 // Send pending reports. report2 should be discarded since it's too old. No |
| 502 // other reports remain. If any report is sent, test teardown will catch it. |
| 503 SendPendingReports(); |
| 504 } |
| 505 |
| 506 // CertificateReportingService should drop old reports from its pending report |
| 507 // queue, if the queue is full. |
| 508 IN_PROC_BROWSER_TEST_F(CertificateReportingServiceBrowserTest, |
| 509 DropOldReportsFromQueue) { |
| 510 SetExpectedHistogramCountOnTeardown(7); |
| 511 |
| 512 base::SimpleTestClock* clock = new base::SimpleTestClock(); |
| 513 base::Time reference_time = base::Time::Now(); |
| 514 clock->SetNow(reference_time); |
| 515 factory()->SetClockForTesting(std::unique_ptr<base::Clock>(clock)); |
| 516 |
| 517 // The service should queue a maximum of 3 reports and ignore reports older |
| 518 // than 24 hours. |
| 519 factory()->SetQueuedReportTTLForTesting(base::TimeDelta::FromHours(24)); |
| 520 factory()->SetMaxQueuedReportCountForTesting(3); |
| 521 |
| 522 certificate_reporting_test_utils::SetCertReportingOptIn( |
| 523 browser(), certificate_reporting_test_utils::EXTENDED_REPORTING_OPT_IN); |
| 524 |
| 525 // Let all reports fail. |
| 526 test_helper()->SetFailureMode( |
| 527 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); |
| 528 |
| 529 // Trigger a failed report. |
| 530 SendReport("report0"); |
| 531 WaitForReports(ReportExpectation::Failed({"report0"})); |
| 532 |
| 533 // Trigger three more reports within five hours of each other. After this: |
| 534 // report0 is 0 hours after reference time (15 hours old). |
| 535 // report1 is 5 hours after reference time (10 hours old). |
| 536 // report2 is 10 hours after reference time (5 hours old). |
| 537 // report3 is 15 hours after reference time (0 hours old). |
| 538 clock->Advance(base::TimeDelta::FromHours(5)); |
| 539 SendReport("report1"); |
| 540 |
| 541 clock->Advance(base::TimeDelta::FromHours(5)); |
| 542 SendReport("report2"); |
| 543 |
| 544 clock->Advance(base::TimeDelta::FromHours(5)); |
| 545 SendReport("report3"); |
| 546 |
| 547 WaitForReports(ReportExpectation::Failed({"report1", "report2", "report3"})); |
| 548 |
| 549 // Send pending reports. Four reports were generated above, but the service |
| 550 // only queues three reports, so report0 should be dropped since it's the |
| 551 // oldest. |
| 552 SendPendingReports(); |
| 553 WaitForReports(ReportExpectation::Failed({"report1", "report2", "report3"})); |
| 554 |
| 555 // Let all reports succeed. |
| 556 test_helper()->SetFailureMode(certificate_reporting_test_utils:: |
| 557 ReportSendingResult::REPORTS_SUCCESSFUL); |
| 558 |
| 559 // Advance the clock 15 hours. Current time is now 30 hours after reference |
| 560 // time, and the ages of reports are now as follows: |
| 561 // report1 is 25 hours old. |
| 562 // report2 is 20 hours old. |
| 563 // report3 is 15 hours old. |
| 564 clock->Advance(base::TimeDelta::FromHours(15)); |
| 565 |
| 566 // Send pending reports. Only reports 2 and 3 should be sent, report 1 |
| 567 // should be ignored because it's too old. |
| 568 SendPendingReports(); |
| 569 WaitForReports(ReportExpectation::Successful({"report2", "report3"})); |
| 570 } |
| 571 |
| 572 // Resume a delayed report after CertificateReportingService shuts down. Should |
| 573 // not crash. |
| 574 IN_PROC_BROWSER_TEST_F(CertificateReportingServiceBrowserTest, |
| 575 Delayed_NotResumed_ShouldNotCrash) { |
| 576 SetExpectedHistogramCountOnTeardown(0); |
| 577 |
| 578 certificate_reporting_test_utils::SetCertReportingOptIn( |
| 579 browser(), certificate_reporting_test_utils::EXTENDED_REPORTING_OPT_IN); |
| 580 // Let reports hang. |
| 581 test_helper()->SetFailureMode( |
| 582 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY); |
| 583 |
| 584 // Navigate to and away from an interstitial to trigger a report. The report |
| 585 // is triggered but hangs, so no error or success callbacks should be called. |
| 586 SendReport("no-report"); |
| 587 |
| 588 SetExpectDelayedReportOnTeardown(); |
| 589 } |
| 590 |
| 591 IN_PROC_BROWSER_TEST_F(CertificateReportingServiceBrowserTest, |
| 592 Delayed_Resumed) { |
| 593 SetExpectedHistogramCountOnTeardown(0); |
| 594 |
| 595 certificate_reporting_test_utils::SetCertReportingOptIn( |
| 596 browser(), certificate_reporting_test_utils::EXTENDED_REPORTING_OPT_IN); |
| 597 // Let all reports fail. |
| 598 test_helper()->SetFailureMode( |
| 599 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY); |
| 600 |
| 601 // Trigger a report that hangs. |
| 602 SendReport("report0"); |
| 603 WaitForReports(ReportExpectation::Delayed({"report0"})); |
| 604 |
| 605 // Resume the report upload. The report upload should successfully complete. |
| 606 // The interceptor only observes request creations and not response |
| 607 // completions, so there is nothing to observe. |
| 608 ResumeDelayedRequestAndWait(); |
| 609 } |
| 610 |
| 611 // Same as above, but the service is shut down before resuming the delayed |
| 612 // request. Should not crash. |
| 613 IN_PROC_BROWSER_TEST_F(CertificateReportingServiceBrowserTest, |
| 614 Delayed_Resumed_ServiceShutdown) { |
| 615 SetExpectedHistogramCountOnTeardown(0); |
| 616 |
| 617 certificate_reporting_test_utils::SetCertReportingOptIn( |
| 618 browser(), certificate_reporting_test_utils::EXTENDED_REPORTING_OPT_IN); |
| 619 // Let all reports fail. |
| 620 test_helper()->SetFailureMode( |
| 621 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY); |
| 622 |
| 623 // Trigger a report that hangs. |
| 624 SendReport("report0"); |
| 625 WaitForReports(ReportExpectation::Delayed({"report0"})); |
| 626 |
| 627 // Shutdown the service. Resuming the delayed request shouldn't crash. |
| 628 ShutdownServiceAndWait(); |
| 629 |
| 630 // Resume the report upload. The report upload should successfully complete. |
| 631 // The interceptor only observes request creations and not response |
| 632 // completions, so there is nothing to observe. |
| 633 ResumeDelayedRequestAndWait(); |
| 634 } |
| 635 |
| 636 // Trigger a delayed report, then disable Safebrowsing. Certificate reporting |
| 637 // service should clear its in-flight reports list. Resuming |
| 638 IN_PROC_BROWSER_TEST_F(CertificateReportingServiceBrowserTest, Delayed_Reset) { |
| 639 SetExpectedHistogramCountOnTeardown(0); |
| 640 |
| 641 certificate_reporting_test_utils::SetCertReportingOptIn( |
| 642 browser(), certificate_reporting_test_utils::EXTENDED_REPORTING_OPT_IN); |
| 643 // Let all reports fail. |
| 644 test_helper()->SetFailureMode( |
| 645 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY); |
| 646 |
| 647 // Trigger a report that hangs. |
| 648 SendReport("report0"); |
| 649 WaitForReports(ReportExpectation::Delayed({"report0"})); |
| 650 |
| 651 // Disable SafeBrowsing. This should clear all pending reports. |
| 652 ToggleSafeBrowsingAndWaitForServiceReset(false); |
| 653 |
| 654 // Resume delayed report. No response should be observed since all pending |
| 655 // reports should be cleared. |
| 656 ResumeDelayedRequestAndWait(); |
| 657 CheckNoReports(); |
| 658 |
| 659 // Re-enable SafeBrowsing. |
| 660 ToggleSafeBrowsingAndWaitForServiceReset(true); |
| 661 |
| 662 // Trigger a report that hangs. |
| 663 SendReport("report1"); |
| 664 WaitForReports(ReportExpectation::Delayed({"report1"})); |
| 665 |
| 666 // Resume delayed report. By the time the runloop is finished, the response |
| 667 // will be complete and CertificateReportingService will process the |
| 668 // error/success callback for the report. There will be no inflight reports |
| 669 // remaining. |
| 670 ResumeDelayedRequestAndWait(); |
| 671 } |
| 672 |
| 673 } // namespace safe_browsing |
| OLD | NEW |