| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/safe_browsing/certificate_reporting_service.h" | 5 #include "chrome/browser/safe_browsing/certificate_reporting_service.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" |
| 9 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "base/single_thread_task_runner.h" |
| 10 #include "base/test/simple_test_clock.h" | 13 #include "base/test/simple_test_clock.h" |
| 14 #include "base/test/thread_test_helper.h" |
| 11 #include "base/time/clock.h" | 15 #include "base/time/clock.h" |
| 12 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 17 #include "chrome/browser/safe_browsing/certificate_reporting_service_test_utils.
h" |
| 18 #include "content/public/browser/browser_thread.h" |
| 13 #include "content/public/test/test_browser_thread.h" | 19 #include "content/public/test/test_browser_thread.h" |
| 20 #include "content/public/test/test_browser_thread_bundle.h" |
| 14 #include "net/base/network_delegate_impl.h" | 21 #include "net/base/network_delegate_impl.h" |
| 15 #include "net/test/url_request/url_request_failed_job.h" | 22 #include "net/test/url_request/url_request_failed_job.h" |
| 16 #include "net/test/url_request/url_request_mock_data_job.h" | 23 #include "net/test/url_request/url_request_mock_data_job.h" |
| 24 #include "net/url_request/url_request_filter.h" |
| 17 #include "net/url_request/url_request_test_util.h" | 25 #include "net/url_request/url_request_test_util.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 27 |
| 20 TEST(CertificateReportingServiceTest, BoundedReportList) { | 28 namespace { |
| 29 |
| 30 // Maximum number of reports kept in the certificate reporting service's retry |
| 31 // queue. |
| 32 const size_t kMaxReportCountInQueue = 3; |
| 33 |
| 34 void ClearURLHandlers() { |
| 35 net::URLRequestFilter::GetInstance()->ClearHandlers(); |
| 36 } |
| 37 |
| 38 } // namespace |
| 39 |
| 40 TEST(CertificateReportingServiceReportListTest, BoundedReportList) { |
| 21 // Create a report list with maximum of 2 items. | 41 // Create a report list with maximum of 2 items. |
| 22 CertificateReportingService::BoundedReportList list(2 /* max_size */); | 42 CertificateReportingService::BoundedReportList list(2 /* max_size */); |
| 23 EXPECT_EQ(0u, list.items().size()); | 43 EXPECT_EQ(0u, list.items().size()); |
| 24 | 44 |
| 25 // Add a ten minute old report. | 45 // Add a ten minute old report. |
| 26 list.Add(CertificateReportingService::Report( | 46 list.Add(CertificateReportingService::Report( |
| 27 1, base::Time::Now() - base::TimeDelta::FromMinutes(10), | 47 1, base::Time::Now() - base::TimeDelta::FromMinutes(10), |
| 28 std::string("report1_ten_minutes_old"))); | 48 std::string("report1_ten_minutes_old"))); |
| 29 EXPECT_EQ(1u, list.items().size()); | 49 EXPECT_EQ(1u, list.items().size()); |
| 30 EXPECT_EQ("report1_ten_minutes_old", list.items()[0].serialized_report); | 50 EXPECT_EQ("report1_ten_minutes_old", list.items()[0].serialized_report); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 49 // a no-op. | 69 // a no-op. |
| 50 list.Add(CertificateReportingService::Report( | 70 list.Add(CertificateReportingService::Report( |
| 51 0, base::Time::Now() - base::TimeDelta::FromMinutes( | 71 0, base::Time::Now() - base::TimeDelta::FromMinutes( |
| 52 10) /* 5 minutes older than report2 */, | 72 10) /* 5 minutes older than report2 */, |
| 53 std::string("report0_ten_minutes_old"))); | 73 std::string("report0_ten_minutes_old"))); |
| 54 EXPECT_EQ(2u, list.items().size()); | 74 EXPECT_EQ(2u, list.items().size()); |
| 55 EXPECT_EQ("report3_zero_minutes_old", list.items()[0].serialized_report); | 75 EXPECT_EQ("report3_zero_minutes_old", list.items()[0].serialized_report); |
| 56 EXPECT_EQ("report2_five_minutes_old", list.items()[1].serialized_report); | 76 EXPECT_EQ("report2_five_minutes_old", list.items()[1].serialized_report); |
| 57 } | 77 } |
| 58 | 78 |
| 59 class CertificateReportingServiceReporterTest : public ::testing::Test { | 79 class CertificateReportingServiceReporterOnIOThreadTest |
| 80 : public ::testing::Test { |
| 60 public: | 81 public: |
| 61 void SetUp() override { | 82 void SetUp() override { |
| 62 message_loop_.reset(new base::MessageLoopForIO()); | 83 message_loop_.reset(new base::MessageLoopForIO()); |
| 63 io_thread_.reset(new content::TestBrowserThread(content::BrowserThread::IO, | 84 io_thread_.reset(new content::TestBrowserThread(content::BrowserThread::IO, |
| 64 message_loop_.get())); | 85 message_loop_.get())); |
| 65 url_request_context_getter_ = | 86 url_request_context_getter_ = |
| 66 new net::TestURLRequestContextGetter(message_loop_->task_runner()); | 87 new net::TestURLRequestContextGetter(message_loop_->task_runner()); |
| 67 net::URLRequestFailedJob::AddUrlHandler(); | 88 net::URLRequestFailedJob::AddUrlHandler(); |
| 68 net::URLRequestMockDataJob::AddUrlHandler(); | 89 net::URLRequestMockDataJob::AddUrlHandler(); |
| 69 } | 90 } |
| 70 | 91 |
| 92 void TearDown() override { ClearURLHandlers(); } |
| 93 |
| 71 protected: | 94 protected: |
| 72 net::URLRequestContextGetter* url_request_context_getter() { | 95 net::URLRequestContextGetter* url_request_context_getter() { |
| 73 return url_request_context_getter_.get(); | 96 return url_request_context_getter_.get(); |
| 74 } | 97 } |
| 75 | 98 |
| 76 private: | 99 private: |
| 77 std::unique_ptr<base::MessageLoopForIO> message_loop_; | 100 std::unique_ptr<base::MessageLoopForIO> message_loop_; |
| 78 std::unique_ptr<content::TestBrowserThread> io_thread_; | 101 std::unique_ptr<content::TestBrowserThread> io_thread_; |
| 79 | 102 |
| 80 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 103 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| 81 }; | 104 }; |
| 82 | 105 |
| 83 TEST_F(CertificateReportingServiceReporterTest, Reporter) { | 106 TEST_F(CertificateReportingServiceReporterOnIOThreadTest, |
| 107 Reporter_RetriesEnabled) { |
| 84 std::unique_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock()); | 108 std::unique_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock()); |
| 85 base::Time reference_time = base::Time::Now(); | 109 base::Time reference_time = base::Time::Now(); |
| 86 clock->SetNow(reference_time); | 110 clock->SetNow(reference_time); |
| 87 | 111 |
| 88 const GURL kFailureURL = | 112 const GURL kFailureURL = |
| 89 net::URLRequestFailedJob::GetMockHttpsUrl(net::ERR_SSL_PROTOCOL_ERROR); | 113 net::URLRequestFailedJob::GetMockHttpsUrl(net::ERR_SSL_PROTOCOL_ERROR); |
| 90 certificate_reporting::ErrorReporter* certificate_error_reporter = | 114 certificate_reporting::ErrorReporter* certificate_error_reporter = |
| 91 new certificate_reporting::ErrorReporter( | 115 new certificate_reporting::ErrorReporter( |
| 92 url_request_context_getter()->GetURLRequestContext(), kFailureURL, | 116 url_request_context_getter()->GetURLRequestContext(), kFailureURL, |
| 93 net::ReportSender::DO_NOT_SEND_COOKIES); | 117 net::ReportSender::DO_NOT_SEND_COOKIES); |
| 94 | 118 |
| 95 CertificateReportingService::BoundedReportList* list = | 119 CertificateReportingService::BoundedReportList* list = |
| 96 new CertificateReportingService::BoundedReportList(2); | 120 new CertificateReportingService::BoundedReportList(2); |
| 97 | 121 |
| 122 // Create a reporter with retries enabled. |
| 98 CertificateReportingService::Reporter reporter( | 123 CertificateReportingService::Reporter reporter( |
| 99 std::unique_ptr<certificate_reporting::ErrorReporter>( | 124 std::unique_ptr<certificate_reporting::ErrorReporter>( |
| 100 certificate_error_reporter), | 125 certificate_error_reporter), |
| 101 std::unique_ptr<CertificateReportingService::BoundedReportList>(list), | 126 std::unique_ptr<CertificateReportingService::BoundedReportList>(list), |
| 102 clock.get(), base::TimeDelta::FromSeconds(100)); | 127 clock.get(), base::TimeDelta::FromSeconds(100), |
| 128 true /* retries_enabled */); |
| 103 EXPECT_EQ(0u, list->items().size()); | 129 EXPECT_EQ(0u, list->items().size()); |
| 104 EXPECT_EQ(0u, reporter.inflight_report_count_for_testing()); | 130 EXPECT_EQ(0u, reporter.inflight_report_count_for_testing()); |
| 105 | 131 |
| 106 // Sending a failed report will put the report in the retry list. | 132 // Sending a failed report will put the report in the retry list. |
| 107 reporter.Send("report1"); | 133 reporter.Send("report1"); |
| 108 EXPECT_EQ(1u, reporter.inflight_report_count_for_testing()); | 134 EXPECT_EQ(1u, reporter.inflight_report_count_for_testing()); |
| 109 base::RunLoop().RunUntilIdle(); | 135 base::RunLoop().RunUntilIdle(); |
| 110 | 136 |
| 111 EXPECT_EQ(0u, reporter.inflight_report_count_for_testing()); | 137 EXPECT_EQ(0u, reporter.inflight_report_count_for_testing()); |
| 112 ASSERT_EQ(1u, list->items().size()); | 138 ASSERT_EQ(1u, list->items().size()); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 // left. | 191 // left. |
| 166 const GURL kSuccessURL = | 192 const GURL kSuccessURL = |
| 167 net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | 193 net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); |
| 168 certificate_error_reporter->set_upload_url_for_testing(kSuccessURL); | 194 certificate_error_reporter->set_upload_url_for_testing(kSuccessURL); |
| 169 clock->Advance(base::TimeDelta::FromSeconds(1)); | 195 clock->Advance(base::TimeDelta::FromSeconds(1)); |
| 170 reporter.SendPending(); | 196 reporter.SendPending(); |
| 171 EXPECT_EQ(1u, reporter.inflight_report_count_for_testing()); | 197 EXPECT_EQ(1u, reporter.inflight_report_count_for_testing()); |
| 172 base::RunLoop().RunUntilIdle(); | 198 base::RunLoop().RunUntilIdle(); |
| 173 EXPECT_EQ(0u, list->items().size()); | 199 EXPECT_EQ(0u, list->items().size()); |
| 174 } | 200 } |
| 201 |
| 202 // Same as above, but retries are disabled. |
| 203 TEST_F(CertificateReportingServiceReporterOnIOThreadTest, |
| 204 Reporter_RetriesDisabled) { |
| 205 std::unique_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock()); |
| 206 base::Time reference_time = base::Time::Now(); |
| 207 clock->SetNow(reference_time); |
| 208 |
| 209 const GURL kFailureURL = |
| 210 net::URLRequestFailedJob::GetMockHttpsUrl(net::ERR_SSL_PROTOCOL_ERROR); |
| 211 certificate_reporting::ErrorReporter* certificate_error_reporter = |
| 212 new certificate_reporting::ErrorReporter( |
| 213 url_request_context_getter()->GetURLRequestContext(), kFailureURL, |
| 214 net::ReportSender::DO_NOT_SEND_COOKIES); |
| 215 |
| 216 CertificateReportingService::BoundedReportList* list = |
| 217 new CertificateReportingService::BoundedReportList(2); |
| 218 |
| 219 // Create a reporter with retries disabled. |
| 220 CertificateReportingService::Reporter reporter( |
| 221 std::unique_ptr<certificate_reporting::ErrorReporter>( |
| 222 certificate_error_reporter), |
| 223 std::unique_ptr<CertificateReportingService::BoundedReportList>(list), |
| 224 clock.get(), base::TimeDelta::FromSeconds(100), |
| 225 false /* retries_enabled */); |
| 226 EXPECT_EQ(0u, list->items().size()); |
| 227 EXPECT_EQ(0u, reporter.inflight_report_count_for_testing()); |
| 228 |
| 229 // Sending a failed report will not put the report in the retry list. |
| 230 reporter.Send("report1"); |
| 231 EXPECT_EQ(1u, reporter.inflight_report_count_for_testing()); |
| 232 base::RunLoop().RunUntilIdle(); |
| 233 |
| 234 EXPECT_EQ(0u, reporter.inflight_report_count_for_testing()); |
| 235 ASSERT_EQ(0u, list->items().size()); |
| 236 |
| 237 // Sending a second failed report will also not put it in the retry list. |
| 238 clock->Advance(base::TimeDelta::FromSeconds(10)); |
| 239 reporter.Send("report2"); |
| 240 base::RunLoop().RunUntilIdle(); |
| 241 ASSERT_EQ(0u, list->items().size()); |
| 242 |
| 243 // Send pending reports. Nothing should be sent. |
| 244 clock->Advance(base::TimeDelta::FromSeconds(10)); |
| 245 reporter.SendPending(); |
| 246 base::RunLoop().RunUntilIdle(); |
| 247 ASSERT_EQ(0u, list->items().size()); |
| 248 } |
| 249 |
| 250 class CertificateReportingServiceTest |
| 251 : public ::testing::Test, |
| 252 public certificate_reporting_test_utils:: |
| 253 CertificateReportingServiceTestBase { |
| 254 public: |
| 255 CertificateReportingServiceTest() |
| 256 : CertificateReportingServiceTestBase(), |
| 257 thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD), |
| 258 io_task_runner_(content::BrowserThread::GetTaskRunnerForThread( |
| 259 content::BrowserThread::IO)) {} |
| 260 |
| 261 ~CertificateReportingServiceTest() override {} |
| 262 |
| 263 void SetUp() override { |
| 264 SetUpInterceptor(); |
| 265 WaitForIOThread(); |
| 266 |
| 267 content::BrowserThread::PostTask( |
| 268 content::BrowserThread::IO, FROM_HERE, |
| 269 base::Bind( |
| 270 &CertificateReportingServiceTest::SetUpURLRequestContextOnIOThread, |
| 271 base::Unretained(this))); |
| 272 WaitForIOThread(); |
| 273 |
| 274 clock_ = new base::SimpleTestClock(); |
| 275 service_.reset(new CertificateReportingService( |
| 276 url_request_context_getter(), server_public_key(), |
| 277 server_public_key_version(), kMaxReportCountInQueue, |
| 278 base::TimeDelta::FromHours(24), std::unique_ptr<base::Clock>(clock_))); |
| 279 // Wait for service reset. |
| 280 WaitForIOThread(); |
| 281 } |
| 282 |
| 283 void SetUpURLRequestContextOnIOThread() { |
| 284 std::unique_ptr<net::TestURLRequestContext> url_request_context( |
| 285 new net::TestURLRequestContext(true)); |
| 286 url_request_context->set_network_delegate(network_delegate()); |
| 287 url_request_context->Init(); |
| 288 url_request_context_getter_ = new net::TestURLRequestContextGetter( |
| 289 io_task_runner_, std::move(url_request_context)); |
| 290 } |
| 291 |
| 292 void TearDown() override { |
| 293 WaitForIOThread(); |
| 294 EXPECT_TRUE(interceptor()->successful_reports().empty()); |
| 295 EXPECT_TRUE(interceptor()->failed_reports().empty()); |
| 296 EXPECT_TRUE(interceptor()->delayed_reports().empty()); |
| 297 EXPECT_EQ(0u, service() |
| 298 ->GetReporterForTesting() |
| 299 ->inflight_report_count_for_testing()); |
| 300 |
| 301 service_->Shutdown(); |
| 302 WaitForIOThread(); |
| 303 |
| 304 service_.reset(nullptr); |
| 305 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, |
| 306 base::Bind(&ClearURLHandlers)); |
| 307 TearDownInterceptor(); |
| 308 } |
| 309 |
| 310 protected: |
| 311 net::URLRequestContextGetter* url_request_context_getter() { |
| 312 return url_request_context_getter_.get(); |
| 313 } |
| 314 |
| 315 void WaitForIOThread() { |
| 316 scoped_refptr<base::ThreadTestHelper> io_helper( |
| 317 new base::ThreadTestHelper(io_task_runner_)); |
| 318 ASSERT_TRUE(io_helper->Run()); |
| 319 } |
| 320 |
| 321 // Sets service enabled state and waits for a reset event. |
| 322 void SetServiceEnabledAndWait(bool enabled) { |
| 323 service()->SetEnabled(enabled); |
| 324 WaitForIOThread(); |
| 325 } |
| 326 |
| 327 void AdvanceClock(base::TimeDelta delta) { |
| 328 content::BrowserThread::PostTask( |
| 329 content::BrowserThread::IO, FROM_HERE, |
| 330 base::Bind(&base::SimpleTestClock::Advance, base::Unretained(clock_), |
| 331 delta)); |
| 332 } |
| 333 |
| 334 void SetNow(base::Time now) { |
| 335 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, |
| 336 base::Bind(&base::SimpleTestClock::SetNow, |
| 337 base::Unretained(clock_), now)); |
| 338 } |
| 339 |
| 340 CertificateReportingService* service() { return service_.get(); } |
| 341 |
| 342 private: |
| 343 // Must be initialized before url_request_context_getter_ |
| 344 content::TestBrowserThreadBundle thread_bundle_; |
| 345 |
| 346 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; |
| 347 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| 348 |
| 349 std::unique_ptr<CertificateReportingService> service_; |
| 350 base::SimpleTestClock* clock_; |
| 351 }; |
| 352 |
| 353 TEST_F(CertificateReportingServiceTest, Send) { |
| 354 WaitForIOThread(); |
| 355 // Let all reports fail. |
| 356 SetFailureMode( |
| 357 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); |
| 358 |
| 359 // Send two reports. Both should fail and get queued. |
| 360 service()->Send("report0"); |
| 361 WaitForRequestsDestroyed(ReportExpectation::Failed({"report0"})); |
| 362 |
| 363 service()->Send("report1"); |
| 364 WaitForRequestsDestroyed(ReportExpectation::Failed({"report1"})); |
| 365 |
| 366 // Send pending reports. Previously queued reports should be observed. They |
| 367 // will also be queued again. |
| 368 service()->SendPending(); |
| 369 WaitForRequestsDestroyed(ReportExpectation::Failed({"report0", "report1"})); |
| 370 |
| 371 // Let all reports succeed. |
| 372 SetFailureMode(certificate_reporting_test_utils::ReportSendingResult:: |
| 373 REPORTS_SUCCESSFUL); |
| 374 |
| 375 // Send a third report. This should not be queued. |
| 376 service()->Send("report2"); |
| 377 WaitForRequestsDestroyed(ReportExpectation::Successful({"report2"})); |
| 378 |
| 379 // Send pending reports. Previously failed and queued two reports should be |
| 380 // observed. |
| 381 service()->SendPending(); |
| 382 WaitForRequestsDestroyed( |
| 383 ReportExpectation::Successful({"report0", "report1"})); |
| 384 } |
| 385 |
| 386 TEST_F(CertificateReportingServiceTest, Disabled_ShouldNotSend) { |
| 387 // Let all reports succeed. |
| 388 SetFailureMode(certificate_reporting_test_utils::ReportSendingResult:: |
| 389 REPORTS_SUCCESSFUL); |
| 390 |
| 391 // Disable the service. |
| 392 SetServiceEnabledAndWait(false); |
| 393 |
| 394 // Send a report. Report attempt should be cancelled and no sent reports |
| 395 // should be observed. |
| 396 service()->Send("report0"); |
| 397 |
| 398 // Enable the service and send a report again. |
| 399 SetServiceEnabledAndWait(true); |
| 400 |
| 401 service()->Send("report1"); |
| 402 WaitForRequestsDestroyed(ReportExpectation::Successful({"report1"})); |
| 403 } |
| 404 |
| 405 TEST_F(CertificateReportingServiceTest, Disabled_ShouldClearPendingReports) { |
| 406 // Let all reports fail. |
| 407 SetFailureMode( |
| 408 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); |
| 409 |
| 410 service()->Send("report0"); |
| 411 WaitForRequestsDestroyed(ReportExpectation::Failed({"report0"})); |
| 412 |
| 413 // Disable the service. |
| 414 SetServiceEnabledAndWait(false); |
| 415 |
| 416 // Sending has no effect while disabled, wait for a single cancelled event. |
| 417 service()->SendPending(); |
| 418 |
| 419 // Re-enable the service and send pending reports. Pending reports should have |
| 420 // been cleared when the service was disabled, so no report should be seen. |
| 421 SetServiceEnabledAndWait(true); |
| 422 |
| 423 // Sending with empty queue has no effect. |
| 424 service()->SendPending(); |
| 425 } |
| 426 |
| 427 TEST_F(CertificateReportingServiceTest, DontSendOldReports) { |
| 428 SetNow(base::Time::Now()); |
| 429 // Let all reports fail. |
| 430 SetFailureMode( |
| 431 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); |
| 432 |
| 433 // Send a report. |
| 434 service()->Send("report0"); |
| 435 WaitForRequestsDestroyed(ReportExpectation::Failed({"report0"})); |
| 436 |
| 437 // Advance the clock a bit and trigger another report. |
| 438 AdvanceClock(base::TimeDelta::FromHours(5)); |
| 439 |
| 440 service()->Send("report1"); |
| 441 WaitForRequestsDestroyed(ReportExpectation::Failed({"report1"})); |
| 442 |
| 443 // Advance the clock to 20 hours, putting it 25 hours ahead of the reference |
| 444 // time. This makes the report0 older than max age (24 hours). The report1 is |
| 445 // now 20 hours old. |
| 446 AdvanceClock(base::TimeDelta::FromHours(20)); |
| 447 // Send pending reports. report0 should be discarded since it's too old. |
| 448 // report1 should be queued again. |
| 449 service()->SendPending(); |
| 450 WaitForRequestsDestroyed(ReportExpectation::Failed({"report1"})); |
| 451 |
| 452 // Send a third report. |
| 453 service()->Send("report2"); |
| 454 WaitForRequestsDestroyed(ReportExpectation::Failed({"report2"})); |
| 455 |
| 456 // Advance the clock 5 hours. The report1 will now be 25 hours old. |
| 457 AdvanceClock(base::TimeDelta::FromHours(5)); |
| 458 // Send pending reports. report1 should be discarded since it's too old. |
| 459 // report2 should be queued again. |
| 460 service()->SendPending(); |
| 461 WaitForRequestsDestroyed(ReportExpectation::Failed({"report2"})); |
| 462 |
| 463 // Advance the clock 20 hours again so that report2 is 25 hours old and is |
| 464 // older than max age (24 hours) |
| 465 AdvanceClock(base::TimeDelta::FromHours(20)); |
| 466 // Send pending reports. report2 should be discarded since it's too old. No |
| 467 // other reports remain. |
| 468 service()->SendPending(); |
| 469 } |
| 470 |
| 471 TEST_F(CertificateReportingServiceTest, DiscardOldReports) { |
| 472 SetNow(base::Time::Now()); |
| 473 // Let all reports fail. |
| 474 SetFailureMode( |
| 475 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL); |
| 476 |
| 477 // Send a failed report. |
| 478 service()->Send("report0"); |
| 479 WaitForRequestsDestroyed(ReportExpectation::Failed({"report0"})); |
| 480 |
| 481 // Send three more reports within five hours of each other. After this: |
| 482 // report0 is 0 hours after reference time (15 hours old). |
| 483 // report1 is 5 hours after reference time (10 hours old). |
| 484 // report2 is 10 hours after reference time (5 hours old). |
| 485 // report3 is 15 hours after reference time (0 hours old). |
| 486 AdvanceClock(base::TimeDelta::FromHours(5)); |
| 487 service()->Send("report1"); |
| 488 |
| 489 AdvanceClock(base::TimeDelta::FromHours(5)); |
| 490 service()->Send("report2"); |
| 491 |
| 492 AdvanceClock(base::TimeDelta::FromHours(5)); |
| 493 service()->Send("report3"); |
| 494 WaitForRequestsDestroyed( |
| 495 ReportExpectation::Failed({"report1", "report2", "report3"})); |
| 496 |
| 497 // Send pending reports. Four reports were generated above, but the service |
| 498 // only queues three reports, so the very first one should be dropped since |
| 499 // it's the oldest. |
| 500 service()->SendPending(); |
| 501 WaitForRequestsDestroyed( |
| 502 ReportExpectation::Failed({"report1", "report2", "report3"})); |
| 503 |
| 504 // Let all reports succeed. |
| 505 SetFailureMode(certificate_reporting_test_utils::ReportSendingResult:: |
| 506 REPORTS_SUCCESSFUL); |
| 507 |
| 508 // Advance the clock by 15 hours. Current time is now 30 hours after reference |
| 509 // time. The ages of reports are now as follows: |
| 510 // report1 is 25 hours old. |
| 511 // report2 is 20 hours old. |
| 512 // report3 is 15 hours old. |
| 513 AdvanceClock(base::TimeDelta::FromHours(15)); |
| 514 // Send pending reports. Only report2 and report3 should be sent, report1 |
| 515 // should be ignored because it's too old. |
| 516 service()->SendPending(); |
| 517 WaitForRequestsDestroyed( |
| 518 ReportExpectation::Successful({"report2", "report3"})); |
| 519 |
| 520 // Do a final send. No reports should be sent. |
| 521 service()->SendPending(); |
| 522 } |
| 523 |
| 524 // A delayed report should successfully upload when it's resumed. |
| 525 TEST_F(CertificateReportingServiceTest, Delayed_Resumed) { |
| 526 // Let reports hang. |
| 527 SetFailureMode( |
| 528 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY); |
| 529 // Send a report. The report upload hangs, so no error or success callbacks |
| 530 // should be called. |
| 531 service()->Send("report0"); |
| 532 |
| 533 // Resume the report upload and run the callbacks. The report should be |
| 534 // successfully sent. |
| 535 ResumeDelayedRequest(); |
| 536 WaitForRequestsDestroyed(ReportExpectation::Delayed({"report0"})); |
| 537 } |
| 538 |
| 539 // Delayed reports should cleaned when the service is reset. |
| 540 TEST_F(CertificateReportingServiceTest, Delayed_Reset) { |
| 541 // Let reports hang. |
| 542 SetFailureMode( |
| 543 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY); |
| 544 // Send a report. The report is triggered but hangs, so no error or success |
| 545 // callbacks should be called. |
| 546 service()->Send("report0"); |
| 547 |
| 548 // Disable the service. This should reset the reporting service and |
| 549 // clear all pending reports. |
| 550 SetServiceEnabledAndWait(false); |
| 551 |
| 552 // Resume delayed report. No report should be observed since the service |
| 553 // should have reset and all pending reports should be cleared. If any report |
| 554 // is observed, the next WaitForRequestsDestroyed() will fail. |
| 555 ResumeDelayedRequest(); |
| 556 |
| 557 // Enable the service. |
| 558 SetServiceEnabledAndWait(true); |
| 559 |
| 560 // Send a report. The report is triggered but hangs, so no error or success |
| 561 // callbacks should be called. The report id is again 0 since the pending |
| 562 // report queue has been cleared above. |
| 563 service()->Send("report1"); |
| 564 |
| 565 // Resume delayed report. The report should be observed. |
| 566 ResumeDelayedRequest(); |
| 567 WaitForRequestsDestroyed(ReportExpectation::Delayed({"report0", "report1"})); |
| 568 } |
| OLD | NEW |