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