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

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: 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 CertificateReportingServiceTest()
254 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD),
255 io_task_runner_(content::BrowserThread::GetTaskRunnerForThread(
256 content::BrowserThread::IO)),
257 url_request_context_getter_(
258 new net::TestURLRequestContextGetter(io_task_runner_)) {}
259
260 ~CertificateReportingServiceTest() override {}
261
262 void SetUp() override {
263 SetUpInterceptor();
264
265 // Create an event observer to wait for a single "Reset" event that happens
266 // on service initialization.
267 certificate_reporting_test_utils::ReportEventObserver* observer =
268 new certificate_reporting_test_utils::ReportEventObserver(1);
269
270 service_.reset(new CertificateReportingService(
271 url_request_context_getter_,
272 std::unique_ptr<certificate_reporting_test_utils::ReportEventObserver>(
273 observer),
274 kMaxReportCountInQueue, base::TimeDelta::FromHours(24), &clock_));
275 // Wait for reporting service initialization.
276 observer->WaitForEvents();
277 }
278
279 void TearDown() override {
280 // Wait for reporting service shutdown.
281 certificate_reporting_test_utils::ReportEventObserver* event_observer =
282 NewEventObserver(1);
283 service()->Shutdown();
284 event_observer->WaitForEvents();
285
286 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE,
287 base::Bind(&ClearURLHandlers));
288 }
289
290 protected:
291 net::URLRequestContextGetter* url_request_context_getter() {
292 return url_request_context_getter_.get();
293 }
294
295 certificate_reporting_test_utils::ReportEventObserver* NewEventObserver(
296 int num_events_to_wait_for) {
297 certificate_reporting_test_utils::ReportEventObserver* observer =
298 new certificate_reporting_test_utils::ReportEventObserver(
299 num_events_to_wait_for);
300 service_->SetEventObserverForTesting(
301 std::unique_ptr<CertificateReportingService::EventObserver>(observer));
302 return observer;
303 }
304
305 CertificateReportingService* service() { return service_.get(); }
306
307 // Sends a test report and checks expected events.
308 void SendReport(int expected_completed_attempts,
309 int expected_cancelled_attempts,
310 const std::set<int>& expected_successful_report_ids,
311 const std::set<int>& expected_failed_report_ids) {
312 certificate_reporting_test_utils::ReportEventObserver* report_observer =
313 NewEventObserver(expected_completed_attempts +
314 expected_cancelled_attempts +
315 expected_successful_report_ids.size() +
316 expected_failed_report_ids.size());
317 service()->Send("test_report");
318 report_observer->WaitForEvents();
319
320 EXPECT_EQ(0, report_observer->num_resets());
Jialiu Lin 2016/11/30 22:18:06 nit: maybe move the expected value checking part o
meacer 2016/11/30 23:39:40 I actually did this in the follow up CL which I'll
Jialiu Lin 2016/11/30 23:54:58 Sure. I didn't review patchset#2. Sorry about that
321 EXPECT_EQ(expected_cancelled_attempts,
322 report_observer->num_cancelled_attempts());
323 EXPECT_EQ(expected_completed_attempts,
324 report_observer->num_completed_attempts());
325 EXPECT_EQ(expected_successful_report_ids,
326 report_observer->successful_report_ids());
327 EXPECT_EQ(expected_failed_report_ids, report_observer->failed_report_ids());
328 }
329
330 // Sends pending reports and checks expected events.
331 void SendPendingReports(int expected_completed_attempts,
332 int expected_cancelled_attempts,
333 const std::set<int>& expected_successful_report_ids,
334 const std::set<int>& expected_failed_report_ids) {
335 certificate_reporting_test_utils::ReportEventObserver* report_observer =
336 NewEventObserver(expected_completed_attempts +
337 expected_cancelled_attempts +
338 expected_successful_report_ids.size() +
339 expected_failed_report_ids.size());
340 service()->SendPending();
341 report_observer->WaitForEvents();
342
343 EXPECT_EQ(0, report_observer->num_resets());
344 EXPECT_EQ(expected_cancelled_attempts,
345 report_observer->num_cancelled_attempts());
346 EXPECT_EQ(expected_completed_attempts,
347 report_observer->num_completed_attempts());
348 EXPECT_EQ(expected_successful_report_ids,
349 report_observer->successful_report_ids());
350 EXPECT_EQ(expected_failed_report_ids, report_observer->failed_report_ids());
351 }
352
353 // Sets service enabled state and waits for a reset event.
354 void SetServiceEnabledAndWait(bool enabled) {
355 certificate_reporting_test_utils::ReportEventObserver* report_observer =
356 NewEventObserver(1);
357 service()->SetEnabled(enabled);
358 report_observer->WaitForEvents();
359 EXPECT_EQ(1, report_observer->num_resets());
360 EXPECT_EQ(0, report_observer->num_cancelled_attempts());
361 EXPECT_EQ(0, report_observer->num_completed_attempts());
362 EXPECT_EQ(std::set<int>(), report_observer->successful_report_ids());
363 EXPECT_EQ(std::set<int>(), report_observer->failed_report_ids());
364 }
365
366 // Resumes delayed requests and checks expected events.
367 void ResumeDelayedRequests(
368 int num_expected_events,
369 const std::set<int>& expected_successful_report_ids,
370 const std::set<int>& expected_failed_report_ids) {
371 certificate_reporting_test_utils::ReportEventObserver* report_observer =
372 NewEventObserver(num_expected_events);
373 ResumeDelayedRequest();
374 report_observer->WaitForEvents();
375 EXPECT_EQ(0, report_observer->num_completed_attempts());
376 EXPECT_EQ(0, report_observer->num_cancelled_attempts());
377 EXPECT_EQ(0, report_observer->num_resets());
378 EXPECT_EQ(expected_successful_report_ids,
379 report_observer->successful_report_ids());
380 EXPECT_EQ(expected_failed_report_ids, report_observer->failed_report_ids());
381 }
382
383 base::SimpleTestClock* clock() { return &clock_; }
384
385 private:
386 // Must be initialized before url_request_context_getter_
387 content::TestBrowserThreadBundle thread_bundle_;
388
389 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
390 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
391
392 std::unique_ptr<CertificateReportingService> service_;
393 base::SimpleTestClock clock_;
394 };
395
396 TEST_F(CertificateReportingServiceTest, Send) {
397 // Let all reports fail.
398 SetFailureMode(
399 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL);
400
401 // Send a report. A failed report with id = 0 should be observed.
402 SendReport(1 /* expected_completed_attempts */,
403 0 /* expected_cancelled_attempts */,
404 std::set<int>() /* expected_successful_report_ids */,
405 std::set<int>{0} /* expected_failed_report_ids */);
406
407 // Send another report. A failed report with id = 1 should be observed.
408 SendReport(1 /* expected_completed_attempts */,
409 0 /* expected_cancelled_attempts */,
410 std::set<int>() /* expected_successful_report_ids */,
411 std::set<int>{1} /* expected_failed_report_ids */);
412
413 // Send pending reports. Previously failed reports should be observed again.
414 SendPendingReports(1 /* expected_completed_attempts */,
415 0 /* expected_cancelled_attempts */,
416 std::set<int>() /* expected_successful_report_ids */,
417 std::set<int>{0, 1} /* expected_failed_report_ids */);
418
419 // Let all reports succeed.
420 SetFailureMode(certificate_reporting_test_utils::ReportSendingResult::
421 REPORTS_SUCCESSFUL);
422
423 // Send a third report. A successful report with id = 2 should be observed.
424 SendReport(1 /* expected_completed_attempts */,
425 0 /* expected_cancelled_attempts */,
426 std::set<int>{2} /* expected_successful_report_ids */,
427 std::set<int>() /* expected_failed_report_ids */);
428
429 // Send pending reports again. Previously failed reports should be observed.
430 SendPendingReports(1 /* expected_completed_attempts */,
431 0 /* expected_cancelled_attempts */,
432 std::set<int>{0, 1} /* expected_successful_report_ids */,
433 std::set<int>() /* expected_failed_report_ids */);
434 }
435
436 TEST_F(CertificateReportingServiceTest, Disabled_ShouldNotSend) {
437 // Let all reports succeed.
438 SetFailureMode(certificate_reporting_test_utils::ReportSendingResult::
439 REPORTS_SUCCESSFUL);
440
441 // Disable the service.
442 SetServiceEnabledAndWait(false);
443
444 // Send a report. Report attempt should be cancelled and no sent reports
445 // should be observed.
446 SendReport(0 /* expected_completed_attempts */,
447 1 /* expected_cancelled_attempts */,
448 std::set<int>() /* expected_successful_report_ids */,
449 std::set<int>() /* expected_failed_report_ids */);
450
451 // Enable the service and send a report again. A report with id = 0 should be
452 // successfully sent.
453 SetServiceEnabledAndWait(true);
454 SendReport(1 /* expected_completed_attempts */,
455 0 /* expected_cancelled_attempts */,
456 std::set<int>{0} /* expected_successful_report_ids */,
457 std::set<int>() /* expected_failed_report_ids */);
458 }
459
460 TEST_F(CertificateReportingServiceTest, Disabled_ShouldClearPendingReports) {
461 // Let all reports fail.
462 SetFailureMode(
463 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL);
464
465 // Send a report. A failed report with id = 0 should be observed.
466 SendReport(1 /* expected_completed_attempts */,
467 0 /* expected_cancelled_attempts */,
468 std::set<int>() /* expected_successful_report_ids */,
469 std::set<int>{0} /* expected_failed_report_ids */);
470
471 // Disable the service.
472 SetServiceEnabledAndWait(false);
473
474 // Sending has no effect while disabled.
475 SendPendingReports(0 /* expected_completed_attempts */,
476 1 /* expected_cancelled_attempts */,
477 std::set<int>() /* expected_successful_report_ids */,
478 std::set<int>() /* expected_failed_report_ids */);
479
480 // Re-enable the service and send pending reports. Pending reports should have
481 // been cleared when the service was disabled, so no report should be seen.
482 SetServiceEnabledAndWait(true);
483 SendPendingReports(1 /* expected_completed_attempts */,
484 0 /* expected_cancelled_attempts */,
485 std::set<int>() /* expected_successful_report_ids */,
486 std::set<int>() /* expected_failed_report_ids */);
487 }
488
489 TEST_F(CertificateReportingServiceTest, DontSendOldReports) {
490 base::Time reference_time = base::Time::Now();
491 clock()->SetNow(reference_time);
492 // Let all reports fail.
493 SetFailureMode(
494 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL);
495
496 // Send a report. A failed report with id = 0 should be observed.
497 SendReport(1 /* expected_completed_attempts */,
498 0 /* expected_cancelled_attempts */,
499 std::set<int>() /* expected_successful_report_ids */,
500 std::set<int>{0} /* expected_failed_report_ids */);
501
502 // Advance the clock a bit and trigger another report. A failed report with
503 // id = 1 should be observed.
504 clock()->Advance(base::TimeDelta::FromHours(5));
505 SendReport(1 /* expected_completed_attempts */,
506 0 /* expected_cancelled_attempts */,
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(1 /* expected_completed_attempts */,
517 0 /* expected_cancelled_attempts */,
518 std::set<int>() /* expected_successful_report_ids */,
519 std::set<int>{1} /* expected_failed_report_ids */);
520
521 // Send a third report. A failed report with id = 2 should be observed.
522 SendReport(1 /* expected_completed_attempts */,
523 0 /* expected_cancelled_attempts */,
524 std::set<int>() /* expected_successful_report_ids */,
525 std::set<int>{2} /* expected_failed_report_ids */);
526
527 // Advance the clock 5 hours. The report with id = 1 will now be 25 hours old.
528 clock()->Advance(base::TimeDelta::FromHours(5));
529 // Send pending reports. Report with id = 1 should be discarded since
530 // it's too old. Report with id = 2 should be queued again.
531 SendPendingReports(1 /* expected_completed_attempts */,
532 0 /* expected_cancelled_attempts */,
533 std::set<int>() /* expected_successful_report_ids */,
534 std::set<int>{2} /* expected_failed_report_ids */);
535
536 // Advance the clock 20 hours again so that the report with id = 2 is 25 hours
537 // old and is older than max age (24 hours)
538 clock()->Advance(base::TimeDelta::FromHours(20));
539 // Send pending reports. Report with id = 2 should be discarded since
540 // it's too old. No other reports remain.
541 SendPendingReports(1 /* expected_completed_attempts */,
542 0 /* expected_cancelled_attempts */,
543 std::set<int>() /* expected_successful_report_ids */,
544 std::set<int>() /* expected_failed_report_ids */);
545 }
546
547 TEST_F(CertificateReportingServiceTest, DiscardOldReports) {
548 base::Time reference_time = base::Time::Now();
549 clock()->SetNow(reference_time);
550 // Let all reports fail.
551 SetFailureMode(
552 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL);
553
554 // Trigger a failed report.
555 SendReport(1 /* expected_completed_attempts */,
556 0 /* expected_cancelled_attemps */,
557 std::set<int>() /* expected_successful_report_ids */,
558 std::set<int>{0} /* expected_failed_report_ids */);
559
560 // Trigger three more reports within two hours of each other. After this:
561 // Report with id = 0 is 0 hours after reference time.
562 // Report with id = 1 is 5 hours after reference time.
563 // Report with id = 2 is 10 hours after reference time.
564 // Report with id = 3 is 15 hours after reference time.
565 for (size_t report_id = 1; report_id <= 3; report_id++) {
566 clock()->Advance(base::TimeDelta::FromHours(5));
567 SendReport(1 /* expected_completed_attempts */,
568 0 /* expected_cancelled_attemps */,
569 std::set<int>(), /* expected_successful_report_ids */
570 std::set<int>{report_id} /* expected_failed_report_ids */);
571 }
572
573 // Send pending reports. Four reports were generated above, but the service
574 // only queues three reports, so the very first one should be dropped since
575 // it's the oldest.
576 SendPendingReports(1 /* expected_completed_attempts */,
577 0 /* expected_cancelled_attemps */,
578 std::set<int>() /* expected_successful_report_ids */,
579 std::set<int>{1, 2, 3} /* expected_failed_report_ids */);
580
581 // Let all reports succeed.
582 SetFailureMode(
583 certificate_reporting_test_utils::ReportSendingResult::REPORTS_FAIL);
584
585 // Advance clock to 15 hours. The current time is now 30 hours after
586 // reference time. The ages of reports are now as follows:
587 // Report with id = 1 is 25 hours old.
588 // Report with id = 2 is 20 hours old.
589 // Report with id = 3 is 15 hours old.
590 clock()->Advance(base::TimeDelta::FromHours(15));
591 // Send pending reports. Only reports 2 and 3 should be sent, report 1
592 // should be ignored because it's too old.
593 SendPendingReports(1 /* expected_completed_attempts */,
594 0 /* expected_cancelled_attemps */,
595 std::set<int>() /* expected_successful_report_ids */,
596 std::set<int>{2, 3} /* expected_failed_report_ids */);
597 }
598
599 TEST_F(CertificateReportingServiceTest, Delayed_NotResumed_ShouldNotCrash) {
600 // Let all reports fail.
601 SetFailureMode(
602 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY);
603 // Send a report. The report is triggered but hangs, so no error or success
604 // callbacks should be called.
605 SendReport(1 /* expected_completed_attempts */,
606 0 /* expected_cancelled_attempts */,
607 std::set<int>() /* expected_successful_report_ids */,
608 std::set<int>() /* expected_failed_report_ids */);
609 }
610
611 TEST_F(CertificateReportingServiceTest, Delayed_Resumed) {
612 // Let all reports fail.
613 SetFailureMode(
614 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY);
615 // Send a report. The report upload hangs, so no error or success callbacks
616 // should be called.
617 SendReport(1 /* expected_completed_attempts */,
618 0 /* expected_cancelled_attempts */,
619 std::set<int>() /* expected_successful_report_ids */,
620 std::set<int>() /* expected_failed_report_ids */);
621
622 // Resume the report upload and run the callbacks. The report should be
623 // successfully sent.
624 ResumeDelayedRequests(1 /* num_expected_events */,
625 std::set<int>{0} /* expected_successful_report_ids */,
626 std::set<int>() /* expected_failed_report_ids */);
627 }
628
629 TEST_F(CertificateReportingServiceTest, Delayed_Reset) {
630 // Let all reports fail.
631 SetFailureMode(
632 certificate_reporting_test_utils::ReportSendingResult::REPORTS_DELAY);
633 // Send a report. The report is triggered but hangs, so no error or success
634 // callbacks should be called.
635 SendReport(1 /* expected_completed_attempts */,
636 0 /* expected_cancelled_attemps */,
637 std::set<int>() /* expected_successful_report_ids */,
638 std::set<int>() /* expected_failed_report_ids */);
639
640 // Disable the service. This should reset the reporting service and
641 // clear all pending reports.
642 SetServiceEnabledAndWait(false);
643
644 // Resume delayed report. No response should be observed since the service
645 // should have reset and all pending reports should be cleared.
646 ResumeDelayedRequests(0 /* num_expected_events */,
647 std::set<int>() /* expected_successful_report_ids */,
648 std::set<int>() /* expected_failed_report_ids */);
649
650 // Enable the service.
651 SetServiceEnabledAndWait(true);
652
653 // Send a report. The report is triggered but hangs, so no error or success
654 // callbacks should be called. The report id is again 0 since the pending
655 // report queue has been cleared above.
656 SendReport(1 /* expected_completed_attempts */,
657 0 /* expected_cancelled_attemps */,
658 std::set<int>() /* expected_successful_report_ids */,
659 std::set<int>() /* expected_failed_report_ids */);
660
661 // Resume delayed report. The report should be observed.
662 ResumeDelayedRequests(1 /* num_expected_events */,
663 std::set<int>{0} /* expected_successful_report_ids */,
664 std::set<int>() /* expected_failed_report_ids */);
665 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698