Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(114)

Side by Side Diff: chrome/browser/safe_browsing/certificate_reporting_service_unittest.cc

Issue 2543523002: Implement main CertificateReportingService code and add unit tests. (Closed)
Patch Set: jialiul comments Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <set>
7 #include <string> 8 #include <string>
8 9
10 #include "base/bind.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"
11 #include "base/time/clock.h" 14 #include "base/time/clock.h"
12 #include "base/time/time.h" 15 #include "base/time/time.h"
16 #include "chrome/browser/safe_browsing/certificate_reporting_service_test_utils. h"
17 #include "content/public/browser/browser_thread.h"
13 #include "content/public/test/test_browser_thread.h" 18 #include "content/public/test/test_browser_thread.h"
19 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "net/base/network_delegate_impl.h" 20 #include "net/base/network_delegate_impl.h"
15 #include "net/test/url_request/url_request_failed_job.h" 21 #include "net/test/url_request/url_request_failed_job.h"
16 #include "net/test/url_request/url_request_mock_data_job.h" 22 #include "net/test/url_request/url_request_mock_data_job.h"
23 #include "net/url_request/url_request_filter.h"
17 #include "net/url_request/url_request_test_util.h" 24 #include "net/url_request/url_request_test_util.h"
18 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
19 26
20 TEST(CertificateReportingServiceTest, BoundedReportList) { 27 namespace {
28 void ClearURLHandlers() {
29 net::URLRequestFilter::GetInstance()->ClearHandlers();
30 }
31
32 // Maximum number of reports kept in the certificate reporting service's retry
33 // queue.
34 const size_t kMaxReportCountInQueue = 3;
35
36 } // namespace
37
38 TEST(CertificateReportingServiceReportListTest, BoundedReportList) {
21 // Create a report list with maximum of 2 items. 39 // Create a report list with maximum of 2 items.
22 CertificateReportingService::BoundedReportList list(2 /* max_size */); 40 CertificateReportingService::BoundedReportList list(2 /* max_size */);
23 EXPECT_EQ(0u, list.items().size()); 41 EXPECT_EQ(0u, list.items().size());
24 42
25 // Add a ten minute old report. 43 // Add a ten minute old report.
26 list.Add(CertificateReportingService::Report( 44 list.Add(CertificateReportingService::Report(
27 1, base::Time::Now() - base::TimeDelta::FromMinutes(10), 45 1, base::Time::Now() - base::TimeDelta::FromMinutes(10),
28 std::string("report1_ten_minutes_old"))); 46 std::string("report1_ten_minutes_old")));
29 EXPECT_EQ(1u, list.items().size()); 47 EXPECT_EQ(1u, list.items().size());
30 EXPECT_EQ("report1_ten_minutes_old", list.items()[0].serialized_report); 48 EXPECT_EQ("report1_ten_minutes_old", list.items()[0].serialized_report);
(...skipping 18 matching lines...) Expand all
49 // a no-op. 67 // a no-op.
50 list.Add(CertificateReportingService::Report( 68 list.Add(CertificateReportingService::Report(
51 0, base::Time::Now() - base::TimeDelta::FromMinutes( 69 0, base::Time::Now() - base::TimeDelta::FromMinutes(
52 10) /* 5 minutes older than report2 */, 70 10) /* 5 minutes older than report2 */,
53 std::string("report0_ten_minutes_old"))); 71 std::string("report0_ten_minutes_old")));
54 EXPECT_EQ(2u, list.items().size()); 72 EXPECT_EQ(2u, list.items().size());
55 EXPECT_EQ("report3_zero_minutes_old", list.items()[0].serialized_report); 73 EXPECT_EQ("report3_zero_minutes_old", list.items()[0].serialized_report);
56 EXPECT_EQ("report2_five_minutes_old", list.items()[1].serialized_report); 74 EXPECT_EQ("report2_five_minutes_old", list.items()[1].serialized_report);
57 } 75 }
58 76
59 class CertificateReportingServiceReporterTest : public ::testing::Test { 77 class CertificateReportingServiceReporterOnIOThreadTest
78 : public ::testing::Test {
60 public: 79 public:
61 void SetUp() override { 80 void SetUp() override {
62 message_loop_.reset(new base::MessageLoopForIO()); 81 message_loop_.reset(new base::MessageLoopForIO());
63 io_thread_.reset(new content::TestBrowserThread(content::BrowserThread::IO, 82 io_thread_.reset(new content::TestBrowserThread(content::BrowserThread::IO,
64 message_loop_.get())); 83 message_loop_.get()));
65 url_request_context_getter_ = 84 url_request_context_getter_ =
66 new net::TestURLRequestContextGetter(message_loop_->task_runner()); 85 new net::TestURLRequestContextGetter(message_loop_->task_runner());
67 net::URLRequestFailedJob::AddUrlHandler(); 86 net::URLRequestFailedJob::AddUrlHandler();
68 net::URLRequestMockDataJob::AddUrlHandler(); 87 net::URLRequestMockDataJob::AddUrlHandler();
69 } 88 }
70 89
90 void TearDown() override { ClearURLHandlers(); }
91
71 protected: 92 protected:
72 net::URLRequestContextGetter* url_request_context_getter() { 93 net::URLRequestContextGetter* url_request_context_getter() {
73 return url_request_context_getter_.get(); 94 return url_request_context_getter_.get();
74 } 95 }
75 96
76 private: 97 private:
77 std::unique_ptr<base::MessageLoopForIO> message_loop_; 98 std::unique_ptr<base::MessageLoopForIO> message_loop_;
78 std::unique_ptr<content::TestBrowserThread> io_thread_; 99 std::unique_ptr<content::TestBrowserThread> io_thread_;
79 100
80 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; 101 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
81 }; 102 };
82 103
83 TEST_F(CertificateReportingServiceReporterTest, Reporter) { 104 TEST_F(CertificateReportingServiceReporterOnIOThreadTest,
105 Reporter_RetriesEnabled) {
84 std::unique_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock()); 106 std::unique_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock());
85 base::Time reference_time = base::Time::Now(); 107 base::Time reference_time = base::Time::Now();
86 clock->SetNow(reference_time); 108 clock->SetNow(reference_time);
87 109
88 const GURL kFailureURL = 110 const GURL kFailureURL =
89 net::URLRequestFailedJob::GetMockHttpsUrl(net::ERR_SSL_PROTOCOL_ERROR); 111 net::URLRequestFailedJob::GetMockHttpsUrl(net::ERR_SSL_PROTOCOL_ERROR);
90 certificate_reporting::ErrorReporter* certificate_error_reporter = 112 certificate_reporting::ErrorReporter* certificate_error_reporter =
91 new certificate_reporting::ErrorReporter( 113 new certificate_reporting::ErrorReporter(
92 url_request_context_getter()->GetURLRequestContext(), kFailureURL, 114 url_request_context_getter()->GetURLRequestContext(), kFailureURL,
93 net::ReportSender::DO_NOT_SEND_COOKIES); 115 net::ReportSender::DO_NOT_SEND_COOKIES);
94 116
95 CertificateReportingService::BoundedReportList* list = 117 CertificateReportingService::BoundedReportList* list =
96 new CertificateReportingService::BoundedReportList(2); 118 new CertificateReportingService::BoundedReportList(2);
97 119
120 // Create a reporter with retries enabled.
98 CertificateReportingService::Reporter reporter( 121 CertificateReportingService::Reporter reporter(
99 std::unique_ptr<certificate_reporting::ErrorReporter>( 122 std::unique_ptr<certificate_reporting::ErrorReporter>(
100 certificate_error_reporter), 123 certificate_error_reporter),
101 std::unique_ptr<CertificateReportingService::BoundedReportList>(list), 124 std::unique_ptr<CertificateReportingService::BoundedReportList>(list),
102 clock.get(), base::TimeDelta::FromSeconds(100)); 125 clock.get(), base::TimeDelta::FromSeconds(100), nullptr,
126 true /* retries_enabled */);
103 EXPECT_EQ(0u, list->items().size()); 127 EXPECT_EQ(0u, list->items().size());
104 EXPECT_EQ(0u, reporter.inflight_report_count_for_testing()); 128 EXPECT_EQ(0u, reporter.inflight_report_count_for_testing());
105 129
106 // Sending a failed report will put the report in the retry list. 130 // Sending a failed report will put the report in the retry list.
107 reporter.Send("report1"); 131 reporter.Send("report1");
108 EXPECT_EQ(1u, reporter.inflight_report_count_for_testing()); 132 EXPECT_EQ(1u, reporter.inflight_report_count_for_testing());
109 base::RunLoop().RunUntilIdle(); 133 base::RunLoop().RunUntilIdle();
110 134
111 EXPECT_EQ(0u, reporter.inflight_report_count_for_testing()); 135 EXPECT_EQ(0u, reporter.inflight_report_count_for_testing());
112 ASSERT_EQ(1u, list->items().size()); 136 ASSERT_EQ(1u, list->items().size());
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 // left. 189 // left.
166 const GURL kSuccessURL = 190 const GURL kSuccessURL =
167 net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); 191 net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1);
168 certificate_error_reporter->set_upload_url_for_testing(kSuccessURL); 192 certificate_error_reporter->set_upload_url_for_testing(kSuccessURL);
169 clock->Advance(base::TimeDelta::FromSeconds(1)); 193 clock->Advance(base::TimeDelta::FromSeconds(1));
170 reporter.SendPending(); 194 reporter.SendPending();
171 EXPECT_EQ(1u, reporter.inflight_report_count_for_testing()); 195 EXPECT_EQ(1u, reporter.inflight_report_count_for_testing());
172 base::RunLoop().RunUntilIdle(); 196 base::RunLoop().RunUntilIdle();
173 EXPECT_EQ(0u, list->items().size()); 197 EXPECT_EQ(0u, list->items().size());
174 } 198 }
199
200 // Same as above, but retries are disabled.
201 TEST_F(CertificateReportingServiceReporterOnIOThreadTest,
202 Reporter_RetriesDisabled) {
203 std::unique_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock());
204 base::Time reference_time = base::Time::Now();
205 clock->SetNow(reference_time);
206
207 const GURL kFailureURL =
208 net::URLRequestFailedJob::GetMockHttpsUrl(net::ERR_SSL_PROTOCOL_ERROR);
209 certificate_reporting::ErrorReporter* certificate_error_reporter =
210 new certificate_reporting::ErrorReporter(
211 url_request_context_getter()->GetURLRequestContext(), kFailureURL,
212 net::ReportSender::DO_NOT_SEND_COOKIES);
213
214 CertificateReportingService::BoundedReportList* list =
215 new CertificateReportingService::BoundedReportList(2);
216
217 // Create a reporter with retries disabled.
218 CertificateReportingService::Reporter reporter(
219 std::unique_ptr<certificate_reporting::ErrorReporter>(
220 certificate_error_reporter),
221 std::unique_ptr<CertificateReportingService::BoundedReportList>(list),
222 clock.get(), base::TimeDelta::FromSeconds(100), nullptr,
223 false /* retries_enabled */);
224 EXPECT_EQ(0u, list->items().size());
225 EXPECT_EQ(0u, reporter.inflight_report_count_for_testing());
226
227 // Sending a failed report will not put the report in the retry list.
228 reporter.Send("report1");
229 EXPECT_EQ(1u, reporter.inflight_report_count_for_testing());
230 base::RunLoop().RunUntilIdle();
231
232 EXPECT_EQ(0u, reporter.inflight_report_count_for_testing());
233 ASSERT_EQ(0u, list->items().size());
234
235 // Sending a second failed report will also not put it in the retry list.
236 clock->Advance(base::TimeDelta::FromSeconds(10));
237 reporter.Send("report2");
238 base::RunLoop().RunUntilIdle();
239 ASSERT_EQ(0u, list->items().size());
240
241 // Send pending reports. Nothing should be sent.
242 clock->Advance(base::TimeDelta::FromSeconds(10));
243 reporter.SendPending();
244 base::RunLoop().RunUntilIdle();
245 ASSERT_EQ(0u, list->items().size());
246 }
247
248 class CertificateReportingServiceTest
249 : public ::testing::Test,
250 public certificate_reporting_test_utils::
251 CertificateReportingServiceTestBase {
252 public:
253 enum ReportSendStatus {
254 // Indicates that the send attempt is completed.
255 EXPECT_SEND_ATTEMPT_COMPLETED,
256 // Indicates that the send attempt is cancelled (e.g. extended reporting was
257 // not opted in).
258 EXPECT_SEND_ATTEMPT_CANCELLED,
259 };
260
261 CertificateReportingServiceTest()
262 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD),
263 io_task_runner_(content::BrowserThread::GetTaskRunnerForThread(
264 content::BrowserThread::IO)),
265 url_request_context_getter_(
266 new net::TestURLRequestContextGetter(io_task_runner_)) {}
267
268 ~CertificateReportingServiceTest() override {}
269
270 void SetUp() override {
271 SetUpInterceptor();
272
273 // Create an event observer to wait for a single "Reset" event that happens
274 // on service initialization.
275 certificate_reporting_test_utils::ReportEventObserver* observer =
276 new certificate_reporting_test_utils::ReportEventObserver(1);
277
278 service_.reset(new CertificateReportingService(
279 url_request_context_getter_,
280 std::unique_ptr<certificate_reporting_test_utils::ReportEventObserver>(
281 observer),
282 kMaxReportCountInQueue, base::TimeDelta::FromHours(24), &clock_));
283 // Wait for reporting service initialization.
284 observer->WaitForEvents();
285 }
286
287 void TearDown() override {
288 // Wait for reporting service shutdown.
289 certificate_reporting_test_utils::ReportEventObserver* event_observer =
290 NewEventObserver(1);
291 service()->Shutdown();
292 event_observer->WaitForEvents();
293
294 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE,
295 base::Bind(&ClearURLHandlers));
296 }
297
298 protected:
299 net::URLRequestContextGetter* url_request_context_getter() {
300 return url_request_context_getter_.get();
301 }
302
303 certificate_reporting_test_utils::ReportEventObserver* NewEventObserver(
304 int num_events_to_wait_for) {
305 certificate_reporting_test_utils::ReportEventObserver* observer =
306 new certificate_reporting_test_utils::ReportEventObserver(
307 num_events_to_wait_for);
308 service_->SetEventObserverForTesting(
309 std::unique_ptr<CertificateReportingService::EventObserver>(observer));
310 return observer;
311 }
312
313 CertificateReportingService* service() { return service_.get(); }
314
315 // Sends a test report and checks expected events.
316 void SendReport(ReportSendStatus expected_send_attempt_status,
317 const std::set<int>& expected_successful_report_ids,
318 const std::set<int>& expected_failed_report_ids) {
319 // Observe events: 1 event for send completion or cancellation,
320 // the rest for observed reports after send completion.
321 certificate_reporting_test_utils::ReportEventObserver* report_observer =
322 NewEventObserver(1u + expected_successful_report_ids.size() +
323 expected_failed_report_ids.size());
324 service()->Send("test_report");
325 report_observer->WaitForEvents();
326
327 EXPECT_EQ(0u, report_observer->num_resets());
328 if (expected_send_attempt_status == EXPECT_SEND_ATTEMPT_COMPLETED) {
329 EXPECT_EQ(1u, report_observer->num_completed_attempts());
330 EXPECT_EQ(0u, report_observer->num_cancelled_attempts());
331 } else {
332 EXPECT_EQ(0u, report_observer->num_completed_attempts());
333 EXPECT_EQ(1u, report_observer->num_cancelled_attempts());
334 }
335 EXPECT_EQ(expected_successful_report_ids,
336 report_observer->successful_report_ids());
337 EXPECT_EQ(expected_failed_report_ids, report_observer->failed_report_ids());
338 }
339
340 // Sends pending reports and checks expected events.
341 void SendPendingReports(ReportSendStatus expected_send_attempt_status,
342 const std::set<int>& expected_successful_report_ids,
343 const std::set<int>& expected_failed_report_ids) {
344 // Observe events: 1 event for send completion or cancellation,
345 // the rest for observed reports after send completion.
346 certificate_reporting_test_utils::ReportEventObserver* report_observer =
347 NewEventObserver(1u + expected_successful_report_ids.size() +
348 expected_failed_report_ids.size());
349 service()->SendPending();
350 report_observer->WaitForEvents();
351
352 EXPECT_EQ(0u, report_observer->num_resets());
353 if (expected_send_attempt_status == EXPECT_SEND_ATTEMPT_COMPLETED) {
354 EXPECT_EQ(1u, report_observer->num_completed_attempts());
355 EXPECT_EQ(0u, report_observer->num_cancelled_attempts());
356 } else {
357 EXPECT_EQ(0u, report_observer->num_completed_attempts());
358 EXPECT_EQ(1u, report_observer->num_cancelled_attempts());
359 }
360 EXPECT_EQ(expected_successful_report_ids,
361 report_observer->successful_report_ids());
362 EXPECT_EQ(expected_failed_report_ids, report_observer->failed_report_ids());
363 }
364
365 // Sets service enabled state and waits for a reset event.
366 void SetServiceEnabledAndWait(bool enabled) {
367 certificate_reporting_test_utils::ReportEventObserver* report_observer =
368 NewEventObserver(1u);
369 service()->SetEnabled(enabled);
370 report_observer->WaitForEvents();
371 EXPECT_EQ(1u, report_observer->num_resets());
372 EXPECT_EQ(0u, report_observer->num_cancelled_attempts());
373 EXPECT_EQ(0u, report_observer->num_completed_attempts());
374 EXPECT_EQ(std::set<int>(), report_observer->successful_report_ids());
375 EXPECT_EQ(std::set<int>(), report_observer->failed_report_ids());
376 }
377
378 // Resumes delayed requests and checks expected events.
379 void ResumeDelayedRequests(
380 size_t num_expected_events,
381 const std::set<int>& expected_successful_report_ids,
382 const std::set<int>& expected_failed_report_ids) {
383 certificate_reporting_test_utils::ReportEventObserver* report_observer =
384 NewEventObserver(num_expected_events);
385 ResumeDelayedRequest();
386 report_observer->WaitForEvents();
387 EXPECT_EQ(0u, report_observer->num_completed_attempts());
388 EXPECT_EQ(0u, report_observer->num_cancelled_attempts());
389 EXPECT_EQ(0u, report_observer->num_resets());
390 EXPECT_EQ(expected_successful_report_ids,
391 report_observer->successful_report_ids());
392 EXPECT_EQ(expected_failed_report_ids, report_observer->failed_report_ids());
393 }
394
395 base::SimpleTestClock* clock() { return &clock_; }
396
397 private:
398 // Must be initialized before url_request_context_getter_
399 content::TestBrowserThreadBundle thread_bundle_;
400
401 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
402 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
403
404 std::unique_ptr<CertificateReportingService> service_;
405 base::SimpleTestClock clock_;
406 };
407
408 TEST_F(CertificateReportingServiceTest, Send) {
409 // Let all reports fail.
410 SetFailureMode(
411 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL);
412
413 // Send a report. A failed report with id = 0 should be observed.
414 SendReport(EXPECT_SEND_ATTEMPT_COMPLETED,
415 std::set<int>() /* expected_successful_report_ids */,
416 std::set<int>{0} /* expected_failed_report_ids */);
417
418 // Send another report. A failed report with id = 1 should be observed.
419 SendReport(EXPECT_SEND_ATTEMPT_COMPLETED,
420 std::set<int>() /* expected_successful_report_ids */,
421 std::set<int>{1} /* expected_failed_report_ids */);
422
423 // Send pending reports. Previously failed reports should be observed again.
424 SendPendingReports(EXPECT_SEND_ATTEMPT_COMPLETED,
425 std::set<int>() /* expected_successful_report_ids */,
426 std::set<int>{0, 1} /* expected_failed_report_ids */);
427
428 // Let all reports succeed.
429 SetFailureMode(certificate_reporting_test_utils::ReportSendingResult::
430 REPORTS_SUCCESSFUL);
431
432 // Send a third report. A successful report with id = 2 should be observed.
433 SendReport(EXPECT_SEND_ATTEMPT_COMPLETED,
434 std::set<int>{2} /* expected_successful_report_ids */,
435 std::set<int>() /* expected_failed_report_ids */);
436
437 // Send pending reports again. Previously failed reports should be observed.
438 SendPendingReports(EXPECT_SEND_ATTEMPT_COMPLETED,
439 std::set<int>{0, 1} /* expected_successful_report_ids */,
440 std::set<int>() /* expected_failed_report_ids */);
441 }
442
443 TEST_F(CertificateReportingServiceTest, Disabled_ShouldNotSend) {
444 // Let all reports succeed.
445 SetFailureMode(certificate_reporting_test_utils::ReportSendingResult::
446 REPORTS_SUCCESSFUL);
447
448 // Disable the service.
449 SetServiceEnabledAndWait(false);
450
451 // Send a report. Report attempt should be cancelled and no sent reports
452 // should be observed.
453 SendReport(EXPECT_SEND_ATTEMPT_CANCELLED,
454 std::set<int>() /* expected_successful_report_ids */,
455 std::set<int>() /* expected_failed_report_ids */);
456
457 // Enable the service and send a report again. A report with id = 0 should be
458 // successfully sent.
459 SetServiceEnabledAndWait(true);
460 SendReport(EXPECT_SEND_ATTEMPT_COMPLETED,
461 std::set<int>{0} /* expected_successful_report_ids */,
462 std::set<int>() /* expected_failed_report_ids */);
463 }
464
465 TEST_F(CertificateReportingServiceTest, Disabled_ShouldClearPendingReports) {
466 // Let all reports fail.
467 SetFailureMode(
468 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL);
469
470 // Send a report. A failed report with id = 0 should be observed.
471 SendReport(EXPECT_SEND_ATTEMPT_COMPLETED,
472 std::set<int>() /* expected_successful_report_ids */,
473 std::set<int>{0} /* expected_failed_report_ids */);
474
475 // Disable the service.
476 SetServiceEnabledAndWait(false);
477
478 // Sending has no effect while disabled.
479 SendPendingReports(EXPECT_SEND_ATTEMPT_CANCELLED,
480 std::set<int>() /* expected_successful_report_ids */,
481 std::set<int>() /* expected_failed_report_ids */);
482
483 // Re-enable the service and send pending reports. Pending reports should have
484 // been cleared when the service was disabled, so no report should be seen.
485 SetServiceEnabledAndWait(true);
486 SendPendingReports(EXPECT_SEND_ATTEMPT_COMPLETED,
487 std::set<int>() /* expected_successful_report_ids */,
488 std::set<int>() /* expected_failed_report_ids */);
489 }
490
491 TEST_F(CertificateReportingServiceTest, DontSendOldReports) {
492 base::Time reference_time = base::Time::Now();
493 clock()->SetNow(reference_time);
494 // Let all reports fail.
495 SetFailureMode(
496 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL);
497
498 // Send a report. A failed report with id = 0 should be observed.
499 SendReport(EXPECT_SEND_ATTEMPT_COMPLETED,
500 std::set<int>() /* expected_successful_report_ids */,
501 std::set<int>{0} /* expected_failed_report_ids */);
502
503 // Advance the clock a bit and trigger another report. A failed report with
504 // id = 1 should be observed.
505 clock()->Advance(base::TimeDelta::FromHours(5));
506 SendReport(EXPECT_SEND_ATTEMPT_COMPLETED,
507 std::set<int>() /* expected_successful_report_ids */,
508 std::set<int>{1} /* expected_failed_report_ids */);
509
510 // Advance the clock to 20 hours, putting it 25 hours ahead of the reference
511 // time. This makes the first report with id = 0 older than max age (24
512 // hours). The second report is now 20 hours.
513 clock()->Advance(base::TimeDelta::FromHours(20));
514 // Send pending reports. First report (id = 0) should be discarded since
515 // it's too old. Second report (id = 1) should be queued again.
516 SendPendingReports(EXPECT_SEND_ATTEMPT_COMPLETED,
517 std::set<int>() /* expected_successful_report_ids */,
518 std::set<int>{1} /* expected_failed_report_ids */);
519
520 // Send a third report. A failed report with id = 2 should be observed.
521 SendReport(EXPECT_SEND_ATTEMPT_COMPLETED,
522 std::set<int>() /* expected_successful_report_ids */,
523 std::set<int>{2} /* expected_failed_report_ids */);
524
525 // Advance the clock 5 hours. The report with id = 1 will now be 25 hours old.
526 clock()->Advance(base::TimeDelta::FromHours(5));
527 // Send pending reports. Report with id = 1 should be discarded since
528 // it's too old. Report with id = 2 should be queued again.
529 SendPendingReports(EXPECT_SEND_ATTEMPT_COMPLETED,
530 std::set<int>() /* expected_successful_report_ids */,
531 std::set<int>{2} /* expected_failed_report_ids */);
532
533 // Advance the clock 20 hours again so that the report with id = 2 is 25 hours
534 // old and is older than max age (24 hours)
535 clock()->Advance(base::TimeDelta::FromHours(20));
536 // Send pending reports. Report with id = 2 should be discarded since
537 // it's too old. No other reports remain.
538 SendPendingReports(EXPECT_SEND_ATTEMPT_COMPLETED,
539 std::set<int>() /* expected_successful_report_ids */,
540 std::set<int>() /* expected_failed_report_ids */);
541 }
542
543 TEST_F(CertificateReportingServiceTest, DiscardOldReports) {
544 base::Time reference_time = base::Time::Now();
545 clock()->SetNow(reference_time);
546 // Let all reports fail.
547 SetFailureMode(
548 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL);
549
550 // Trigger a failed report.
551 SendReport(EXPECT_SEND_ATTEMPT_COMPLETED,
552 std::set<int>() /* expected_successful_report_ids */,
553 std::set<int>{0} /* expected_failed_report_ids */);
554
555 // Trigger three more reports within two hours of each other. After this:
556 // Report with id = 0 is 0 hours after reference time.
557 // Report with id = 1 is 5 hours after reference time.
558 // Report with id = 2 is 10 hours after reference time.
559 // Report with id = 3 is 15 hours after reference time.
560 for (int report_id = 1; report_id <= 3; report_id++) {
561 clock()->Advance(base::TimeDelta::FromHours(5));
562 SendReport(EXPECT_SEND_ATTEMPT_COMPLETED,
563 std::set<int>(), /* expected_successful_report_ids */
564 std::set<int>{report_id} /* expected_failed_report_ids */);
565 }
566
567 // Send pending reports. Four reports were generated above, but the service
568 // only queues three reports, so the very first one should be dropped since
569 // it's the oldest.
570 SendPendingReports(EXPECT_SEND_ATTEMPT_COMPLETED,
571 std::set<int>() /* expected_successful_report_ids */,
572 std::set<int>{1, 2, 3} /* expected_failed_report_ids */);
573
574 // Let all reports succeed.
575 SetFailureMode(
576 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL);
577
578 // Advance clock to 15 hours. The current time is now 30 hours after
579 // reference time. The ages of reports are now as follows:
580 // Report with id = 1 is 25 hours old.
581 // Report with id = 2 is 20 hours old.
582 // Report with id = 3 is 15 hours old.
583 clock()->Advance(base::TimeDelta::FromHours(15));
584 // Send pending reports. Only reports 2 and 3 should be sent, report 1
585 // should be ignored because it's too old.
586 SendPendingReports(EXPECT_SEND_ATTEMPT_COMPLETED,
587 std::set<int>() /* expected_successful_report_ids */,
588 std::set<int>{2, 3} /* expected_failed_report_ids */);
589 }
590
591 TEST_F(CertificateReportingServiceTest, Delayed_NotResumed_ShouldNotCrash) {
592 // Let all reports fail.
593 SetFailureMode(
594 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY);
595 // Send a report. The report is triggered but hangs, so no error or success
596 // callbacks should be called.
597 SendReport(EXPECT_SEND_ATTEMPT_COMPLETED,
598 std::set<int>() /* expected_successful_report_ids */,
599 std::set<int>() /* expected_failed_report_ids */);
600 }
601
602 TEST_F(CertificateReportingServiceTest, Delayed_Resumed) {
603 // Let all reports fail.
604 SetFailureMode(
605 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY);
606 // Send a report. The report upload hangs, so no error or success callbacks
607 // should be called.
608 SendReport(EXPECT_SEND_ATTEMPT_COMPLETED,
609 std::set<int>() /* expected_successful_report_ids */,
610 std::set<int>() /* expected_failed_report_ids */);
611
612 // Resume the report upload and run the callbacks. The report should be
613 // successfully sent.
614 ResumeDelayedRequests(1u /* num_expected_events */,
615 std::set<int>{0} /* expected_successful_report_ids */,
616 std::set<int>() /* expected_failed_report_ids */);
617 }
618
619 TEST_F(CertificateReportingServiceTest, Delayed_Reset) {
620 // Let all reports fail.
621 SetFailureMode(
622 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY);
623 // Send a report. The report is triggered but hangs, so no error or success
624 // callbacks should be called.
625 SendReport(EXPECT_SEND_ATTEMPT_COMPLETED,
626 std::set<int>() /* expected_successful_report_ids */,
627 std::set<int>() /* expected_failed_report_ids */);
628
629 // Disable the service. This should reset the reporting service and
630 // clear all pending reports.
631 SetServiceEnabledAndWait(false);
632
633 // Resume delayed report. No response should be observed since the service
634 // should have reset and all pending reports should be cleared.
635 ResumeDelayedRequests(0u /* num_expected_events */,
636 std::set<int>() /* expected_successful_report_ids */,
637 std::set<int>() /* expected_failed_report_ids */);
638
639 // Enable the service.
640 SetServiceEnabledAndWait(true);
641
642 // Send a report. The report is triggered but hangs, so no error or success
643 // callbacks should be called. The report id is again 0 since the pending
644 // report queue has been cleared above.
645 SendReport(EXPECT_SEND_ATTEMPT_COMPLETED,
646 std::set<int>() /* expected_successful_report_ids */,
647 std::set<int>() /* expected_failed_report_ids */);
648
649 // Resume delayed report. The report should be observed.
650 ResumeDelayedRequests(1u /* num_expected_events */,
651 std::set<int>{0} /* expected_successful_report_ids */,
652 std::set<int>() /* expected_failed_report_ids */);
653 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698