OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "net/reporting/reporting_cache.h" | 5 #include "net/reporting/reporting_cache.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/test/simple_test_tick_clock.h" |
10 #include "base/time/time.h" | 11 #include "base/time/time.h" |
11 #include "base/values.h" | 12 #include "base/values.h" |
12 #include "net/reporting/reporting_client.h" | 13 #include "net/reporting/reporting_client.h" |
13 #include "net/reporting/reporting_observer.h" | 14 #include "net/reporting/reporting_observer.h" |
14 #include "net/reporting/reporting_report.h" | 15 #include "net/reporting/reporting_report.h" |
15 #include "net/reporting/reporting_test_util.h" | 16 #include "net/reporting/reporting_test_util.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
17 #include "url/gurl.h" | 18 #include "url/gurl.h" |
18 #include "url/origin.h" | 19 #include "url/origin.h" |
19 | 20 |
(...skipping 15 matching lines...) Expand all Loading... |
35 class ReportingCacheTest : public ReportingTestBase { | 36 class ReportingCacheTest : public ReportingTestBase { |
36 protected: | 37 protected: |
37 ReportingCacheTest() : ReportingTestBase() { | 38 ReportingCacheTest() : ReportingTestBase() { |
38 context()->AddObserver(&observer_); | 39 context()->AddObserver(&observer_); |
39 } | 40 } |
40 | 41 |
41 ~ReportingCacheTest() override { context()->RemoveObserver(&observer_); } | 42 ~ReportingCacheTest() override { context()->RemoveObserver(&observer_); } |
42 | 43 |
43 TestReportingObserver* observer() { return &observer_; } | 44 TestReportingObserver* observer() { return &observer_; } |
44 | 45 |
| 46 size_t report_count() { |
| 47 std::vector<const ReportingReport*> reports; |
| 48 cache()->GetReports(&reports); |
| 49 return reports.size(); |
| 50 } |
| 51 |
45 const GURL kUrl1_ = GURL("https://origin1/path"); | 52 const GURL kUrl1_ = GURL("https://origin1/path"); |
46 const url::Origin kOrigin1_ = url::Origin(GURL("https://origin1/")); | 53 const url::Origin kOrigin1_ = url::Origin(GURL("https://origin1/")); |
47 const url::Origin kOrigin2_ = url::Origin(GURL("https://origin2/")); | 54 const url::Origin kOrigin2_ = url::Origin(GURL("https://origin2/")); |
48 const GURL kEndpoint1_ = GURL("https://endpoint1/"); | 55 const GURL kEndpoint1_ = GURL("https://endpoint1/"); |
49 const GURL kEndpoint2_ = GURL("https://endpoint2/"); | 56 const GURL kEndpoint2_ = GURL("https://endpoint2/"); |
50 const std::string kGroup1_ = "group1"; | 57 const std::string kGroup1_ = "group1"; |
51 const std::string kGroup2 = "group2"; | 58 const std::string kGroup2 = "group2"; |
52 const std::string kType_ = "default"; | 59 const std::string kType_ = "default"; |
53 const base::TimeTicks kNow_ = base::TimeTicks::Now(); | 60 const base::TimeTicks kNow_ = base::TimeTicks::Now(); |
54 const base::TimeTicks kExpires1_ = kNow_ + base::TimeDelta::FromDays(7); | 61 const base::TimeTicks kExpires1_ = kNow_ + base::TimeDelta::FromDays(7); |
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 cache()->SetClient(kSuperSuperOrigin, kEndpoint1_, | 400 cache()->SetClient(kSuperSuperOrigin, kEndpoint1_, |
394 ReportingClient::Subdomains::INCLUDE, kGroup1_, | 401 ReportingClient::Subdomains::INCLUDE, kGroup1_, |
395 kExpires1_); | 402 kExpires1_); |
396 | 403 |
397 std::vector<const ReportingClient*> clients; | 404 std::vector<const ReportingClient*> clients; |
398 cache()->GetClientsForOriginAndGroup(kOrigin, kGroup1_, &clients); | 405 cache()->GetClientsForOriginAndGroup(kOrigin, kGroup1_, &clients); |
399 ASSERT_EQ(1u, clients.size()); | 406 ASSERT_EQ(1u, clients.size()); |
400 EXPECT_EQ(kSuperOrigin, clients[0]->origin); | 407 EXPECT_EQ(kSuperOrigin, clients[0]->origin); |
401 } | 408 } |
402 | 409 |
| 410 TEST_F(ReportingCacheTest, EvictOldest) { |
| 411 ASSERT_LT(0u, policy().max_report_count); |
| 412 ASSERT_GT(std::numeric_limits<size_t>::max(), policy().max_report_count); |
| 413 |
| 414 base::TimeTicks earliest_queued = tick_clock()->NowTicks(); |
| 415 |
| 416 // Enqueue the maximum number of reports, spaced apart in time. |
| 417 for (size_t i = 0; i < policy().max_report_count; ++i) { |
| 418 cache()->AddReport(kUrl1_, kGroup1_, kType_, |
| 419 base::MakeUnique<base::DictionaryValue>(), |
| 420 tick_clock()->NowTicks(), 0); |
| 421 tick_clock()->Advance(base::TimeDelta::FromMinutes(1)); |
| 422 } |
| 423 EXPECT_EQ(policy().max_report_count, report_count()); |
| 424 |
| 425 // Add one more report to force the cache to evict one. |
| 426 cache()->AddReport(kUrl1_, kGroup1_, kType_, |
| 427 base::MakeUnique<base::DictionaryValue>(), kNow_, 0); |
| 428 |
| 429 // Make sure the cache evicted a report to make room for the new one, and make |
| 430 // sure the report evicted was the earliest-queued one. |
| 431 std::vector<const ReportingReport*> reports; |
| 432 cache()->GetReports(&reports); |
| 433 EXPECT_EQ(policy().max_report_count, reports.size()); |
| 434 for (const ReportingReport* report : reports) |
| 435 EXPECT_NE(earliest_queued, report->queued); |
| 436 } |
| 437 |
| 438 TEST_F(ReportingCacheTest, DontEvictPendingReports) { |
| 439 ASSERT_LT(0u, policy().max_report_count); |
| 440 ASSERT_GT(std::numeric_limits<size_t>::max(), policy().max_report_count); |
| 441 |
| 442 // Enqueue the maximum number of reports, spaced apart in time. |
| 443 for (size_t i = 0; i < policy().max_report_count; ++i) { |
| 444 cache()->AddReport(kUrl1_, kGroup1_, kType_, |
| 445 base::MakeUnique<base::DictionaryValue>(), |
| 446 tick_clock()->NowTicks(), 0); |
| 447 tick_clock()->Advance(base::TimeDelta::FromMinutes(1)); |
| 448 } |
| 449 EXPECT_EQ(policy().max_report_count, report_count()); |
| 450 |
| 451 // Mark all of the queued reports pending. |
| 452 std::vector<const ReportingReport*> queued_reports; |
| 453 cache()->GetReports(&queued_reports); |
| 454 cache()->SetReportsPending(queued_reports); |
| 455 |
| 456 // Add one more report to force the cache to evict one. Since the cache has |
| 457 // only pending reports, it will be forced to evict the *new* report! |
| 458 cache()->AddReport(kUrl1_, kGroup1_, kType_, |
| 459 base::MakeUnique<base::DictionaryValue>(), kNow_, 0); |
| 460 |
| 461 // Make sure the cache evicted a report, and make sure the report evicted was |
| 462 // the new, non-pending one. |
| 463 std::vector<const ReportingReport*> reports; |
| 464 cache()->GetReports(&reports); |
| 465 EXPECT_EQ(policy().max_report_count, reports.size()); |
| 466 for (const ReportingReport* report : reports) |
| 467 EXPECT_TRUE(cache()->IsReportPendingForTesting(report)); |
| 468 } |
| 469 |
403 } // namespace | 470 } // namespace |
404 } // namespace net | 471 } // namespace net |
OLD | NEW |