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