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/strings/stringprintf.h" |
10 #include "base/test/simple_test_tick_clock.h" | 11 #include "base/test/simple_test_tick_clock.h" |
11 #include "base/time/time.h" | 12 #include "base/time/time.h" |
12 #include "base/values.h" | 13 #include "base/values.h" |
13 #include "net/reporting/reporting_client.h" | 14 #include "net/reporting/reporting_client.h" |
14 #include "net/reporting/reporting_observer.h" | 15 #include "net/reporting/reporting_observer.h" |
15 #include "net/reporting/reporting_report.h" | 16 #include "net/reporting/reporting_report.h" |
16 #include "net/reporting/reporting_test_util.h" | 17 #include "net/reporting/reporting_test_util.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
18 #include "url/gurl.h" | 19 #include "url/gurl.h" |
19 #include "url/origin.h" | 20 #include "url/origin.h" |
20 | 21 |
21 namespace net { | 22 namespace net { |
22 namespace { | 23 namespace { |
23 | 24 |
24 class TestReportingObserver : public ReportingObserver { | 25 class TestReportingObserver : public ReportingObserver { |
25 public: | 26 public: |
26 TestReportingObserver() : cache_update_count_(0) {} | 27 TestReportingObserver() : cache_update_count_(0) {} |
27 | 28 |
28 void OnCacheUpdated() override { ++cache_update_count_; } | 29 void OnCacheUpdated() override { ++cache_update_count_; } |
29 | 30 |
30 int cache_update_count() const { return cache_update_count_; } | 31 int cache_update_count() const { return cache_update_count_; } |
31 | 32 |
32 private: | 33 private: |
33 int cache_update_count_; | 34 int cache_update_count_; |
34 }; | 35 }; |
35 | 36 |
36 class ReportingCacheTest : public ReportingTestBase { | 37 class ReportingCacheTest : public ReportingTestBase { |
37 protected: | 38 protected: |
38 ReportingCacheTest() : ReportingTestBase() { | 39 ReportingCacheTest() : ReportingTestBase() { |
| 40 ReportingPolicy policy; |
| 41 policy.max_report_count = 5; |
| 42 policy.max_client_count = 5; |
| 43 UsePolicy(policy); |
| 44 |
39 context()->AddObserver(&observer_); | 45 context()->AddObserver(&observer_); |
40 } | 46 } |
41 | 47 |
42 ~ReportingCacheTest() override { context()->RemoveObserver(&observer_); } | 48 ~ReportingCacheTest() override { context()->RemoveObserver(&observer_); } |
43 | 49 |
44 TestReportingObserver* observer() { return &observer_; } | 50 TestReportingObserver* observer() { return &observer_; } |
45 | 51 |
46 size_t report_count() { | 52 size_t report_count() { |
47 std::vector<const ReportingReport*> reports; | 53 std::vector<const ReportingReport*> reports; |
48 cache()->GetReports(&reports); | 54 cache()->GetReports(&reports); |
49 return reports.size(); | 55 return reports.size(); |
50 } | 56 } |
51 | 57 |
| 58 size_t client_count() { |
| 59 std::vector<const ReportingClient*> clients; |
| 60 cache()->GetClients(&clients); |
| 61 return clients.size(); |
| 62 } |
| 63 |
52 const GURL kUrl1_ = GURL("https://origin1/path"); | 64 const GURL kUrl1_ = GURL("https://origin1/path"); |
53 const url::Origin kOrigin1_ = url::Origin(GURL("https://origin1/")); | 65 const url::Origin kOrigin1_ = url::Origin(GURL("https://origin1/")); |
54 const url::Origin kOrigin2_ = url::Origin(GURL("https://origin2/")); | 66 const url::Origin kOrigin2_ = url::Origin(GURL("https://origin2/")); |
55 const GURL kEndpoint1_ = GURL("https://endpoint1/"); | 67 const GURL kEndpoint1_ = GURL("https://endpoint1/"); |
56 const GURL kEndpoint2_ = GURL("https://endpoint2/"); | 68 const GURL kEndpoint2_ = GURL("https://endpoint2/"); |
57 const std::string kGroup1_ = "group1"; | 69 const std::string kGroup1_ = "group1"; |
58 const std::string kGroup2 = "group2"; | 70 const std::string kGroup2 = "group2"; |
59 const std::string kType_ = "default"; | 71 const std::string kType_ = "default"; |
60 const base::TimeTicks kNow_ = base::TimeTicks::Now(); | 72 const base::TimeTicks kNow_ = base::TimeTicks::Now(); |
61 const base::TimeTicks kExpires1_ = kNow_ + base::TimeDelta::FromDays(7); | 73 const base::TimeTicks kExpires1_ = kNow_ + base::TimeDelta::FromDays(7); |
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 cache()->SetClient(kSuperSuperOrigin, kEndpoint1_, | 412 cache()->SetClient(kSuperSuperOrigin, kEndpoint1_, |
401 ReportingClient::Subdomains::INCLUDE, kGroup1_, | 413 ReportingClient::Subdomains::INCLUDE, kGroup1_, |
402 kExpires1_); | 414 kExpires1_); |
403 | 415 |
404 std::vector<const ReportingClient*> clients; | 416 std::vector<const ReportingClient*> clients; |
405 cache()->GetClientsForOriginAndGroup(kOrigin, kGroup1_, &clients); | 417 cache()->GetClientsForOriginAndGroup(kOrigin, kGroup1_, &clients); |
406 ASSERT_EQ(1u, clients.size()); | 418 ASSERT_EQ(1u, clients.size()); |
407 EXPECT_EQ(kSuperOrigin, clients[0]->origin); | 419 EXPECT_EQ(kSuperOrigin, clients[0]->origin); |
408 } | 420 } |
409 | 421 |
410 TEST_F(ReportingCacheTest, EvictOldest) { | 422 TEST_F(ReportingCacheTest, EvictOldestReport) { |
411 ASSERT_LT(0u, policy().max_report_count); | 423 size_t max_report_count = policy().max_report_count; |
412 ASSERT_GT(std::numeric_limits<size_t>::max(), policy().max_report_count); | 424 |
| 425 ASSERT_LT(0u, max_report_count); |
| 426 ASSERT_GT(std::numeric_limits<size_t>::max(), max_report_count); |
413 | 427 |
414 base::TimeTicks earliest_queued = tick_clock()->NowTicks(); | 428 base::TimeTicks earliest_queued = tick_clock()->NowTicks(); |
415 | 429 |
416 // Enqueue the maximum number of reports, spaced apart in time. | 430 // Enqueue the maximum number of reports, spaced apart in time. |
417 for (size_t i = 0; i < policy().max_report_count; ++i) { | 431 for (size_t i = 0; i < max_report_count; ++i) { |
418 cache()->AddReport(kUrl1_, kGroup1_, kType_, | 432 cache()->AddReport(kUrl1_, kGroup1_, kType_, |
419 base::MakeUnique<base::DictionaryValue>(), | 433 base::MakeUnique<base::DictionaryValue>(), |
420 tick_clock()->NowTicks(), 0); | 434 tick_clock()->NowTicks(), 0); |
421 tick_clock()->Advance(base::TimeDelta::FromMinutes(1)); | 435 tick_clock()->Advance(base::TimeDelta::FromMinutes(1)); |
422 } | 436 } |
423 EXPECT_EQ(policy().max_report_count, report_count()); | 437 EXPECT_EQ(max_report_count, report_count()); |
424 | 438 |
425 // Add one more report to force the cache to evict one. | 439 // Add one more report to force the cache to evict one. |
426 cache()->AddReport(kUrl1_, kGroup1_, kType_, | 440 cache()->AddReport(kUrl1_, kGroup1_, kType_, |
427 base::MakeUnique<base::DictionaryValue>(), kNow_, 0); | 441 base::MakeUnique<base::DictionaryValue>(), kNow_, 0); |
428 | 442 |
429 // Make sure the cache evicted a report to make room for the new one, and make | 443 // 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. | 444 // sure the report evicted was the earliest-queued one. |
431 std::vector<const ReportingReport*> reports; | 445 std::vector<const ReportingReport*> reports; |
432 cache()->GetReports(&reports); | 446 cache()->GetReports(&reports); |
433 EXPECT_EQ(policy().max_report_count, reports.size()); | 447 EXPECT_EQ(max_report_count, reports.size()); |
434 for (const ReportingReport* report : reports) | 448 for (const ReportingReport* report : reports) |
435 EXPECT_NE(earliest_queued, report->queued); | 449 EXPECT_NE(earliest_queued, report->queued); |
436 } | 450 } |
437 | 451 |
438 TEST_F(ReportingCacheTest, DontEvictPendingReports) { | 452 TEST_F(ReportingCacheTest, DontEvictPendingReports) { |
439 ASSERT_LT(0u, policy().max_report_count); | 453 size_t max_report_count = policy().max_report_count; |
440 ASSERT_GT(std::numeric_limits<size_t>::max(), policy().max_report_count); | 454 |
| 455 ASSERT_LT(0u, max_report_count); |
| 456 ASSERT_GT(std::numeric_limits<size_t>::max(), max_report_count); |
441 | 457 |
442 // Enqueue the maximum number of reports, spaced apart in time. | 458 // Enqueue the maximum number of reports, spaced apart in time. |
443 for (size_t i = 0; i < policy().max_report_count; ++i) { | 459 for (size_t i = 0; i < max_report_count; ++i) { |
444 cache()->AddReport(kUrl1_, kGroup1_, kType_, | 460 cache()->AddReport(kUrl1_, kGroup1_, kType_, |
445 base::MakeUnique<base::DictionaryValue>(), | 461 base::MakeUnique<base::DictionaryValue>(), |
446 tick_clock()->NowTicks(), 0); | 462 tick_clock()->NowTicks(), 0); |
447 tick_clock()->Advance(base::TimeDelta::FromMinutes(1)); | 463 tick_clock()->Advance(base::TimeDelta::FromMinutes(1)); |
448 } | 464 } |
449 EXPECT_EQ(policy().max_report_count, report_count()); | 465 EXPECT_EQ(max_report_count, report_count()); |
450 | 466 |
451 // Mark all of the queued reports pending. | 467 // Mark all of the queued reports pending. |
452 std::vector<const ReportingReport*> queued_reports; | 468 std::vector<const ReportingReport*> queued_reports; |
453 cache()->GetReports(&queued_reports); | 469 cache()->GetReports(&queued_reports); |
454 cache()->SetReportsPending(queued_reports); | 470 cache()->SetReportsPending(queued_reports); |
455 | 471 |
456 // Add one more report to force the cache to evict one. Since the cache has | 472 // 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! | 473 // only pending reports, it will be forced to evict the *new* report! |
458 cache()->AddReport(kUrl1_, kGroup1_, kType_, | 474 cache()->AddReport(kUrl1_, kGroup1_, kType_, |
459 base::MakeUnique<base::DictionaryValue>(), kNow_, 0); | 475 base::MakeUnique<base::DictionaryValue>(), kNow_, 0); |
460 | 476 |
461 // Make sure the cache evicted a report, and make sure the report evicted was | 477 // Make sure the cache evicted a report, and make sure the report evicted was |
462 // the new, non-pending one. | 478 // the new, non-pending one. |
463 std::vector<const ReportingReport*> reports; | 479 std::vector<const ReportingReport*> reports; |
464 cache()->GetReports(&reports); | 480 cache()->GetReports(&reports); |
465 EXPECT_EQ(policy().max_report_count, reports.size()); | 481 EXPECT_EQ(max_report_count, reports.size()); |
466 for (const ReportingReport* report : reports) | 482 for (const ReportingReport* report : reports) |
467 EXPECT_TRUE(cache()->IsReportPendingForTesting(report)); | 483 EXPECT_TRUE(cache()->IsReportPendingForTesting(report)); |
468 } | 484 } |
469 | 485 |
| 486 GURL MakeEndpoint(size_t index) { |
| 487 return GURL(base::StringPrintf("https://endpoint/%zd", index)); |
| 488 } |
| 489 |
| 490 TEST_F(ReportingCacheTest, EvictLRUClient) { |
| 491 size_t max_client_count = policy().max_client_count; |
| 492 |
| 493 ASSERT_LT(0u, max_client_count); |
| 494 ASSERT_GT(std::numeric_limits<size_t>::max(), max_client_count); |
| 495 |
| 496 for (size_t i = 0; i < max_client_count; ++i) { |
| 497 cache()->SetClient(kOrigin1_, MakeEndpoint(i), |
| 498 ReportingClient::Subdomains::EXCLUDE, kGroup1_, |
| 499 tomorrow()); |
| 500 } |
| 501 EXPECT_EQ(max_client_count, client_count()); |
| 502 |
| 503 // Use clients in reverse order, so client (max_client_count - 1) is LRU. |
| 504 for (size_t i = 1; i <= max_client_count; ++i) { |
| 505 cache()->MarkClientUsed(kOrigin1_, MakeEndpoint(max_client_count - i)); |
| 506 tick_clock()->Advance(base::TimeDelta::FromSeconds(1)); |
| 507 } |
| 508 |
| 509 // Add one more client, forcing the cache to evict the LRU. |
| 510 cache()->SetClient(kOrigin1_, MakeEndpoint(max_client_count), |
| 511 ReportingClient::Subdomains::EXCLUDE, kGroup1_, |
| 512 tomorrow()); |
| 513 EXPECT_EQ(max_client_count, client_count()); |
| 514 EXPECT_FALSE(FindClientInCache(cache(), kOrigin1_, |
| 515 MakeEndpoint(max_client_count - 1))); |
| 516 } |
| 517 |
| 518 TEST_F(ReportingCacheTest, EvictExpiredClient) { |
| 519 size_t max_client_count = policy().max_client_count; |
| 520 |
| 521 ASSERT_LT(0u, max_client_count); |
| 522 ASSERT_GT(std::numeric_limits<size_t>::max(), max_client_count); |
| 523 |
| 524 for (size_t i = 0; i < max_client_count; ++i) { |
| 525 base::TimeTicks expires = |
| 526 (i == max_client_count - 1) ? yesterday() : tomorrow(); |
| 527 cache()->SetClient(kOrigin1_, MakeEndpoint(i), |
| 528 ReportingClient::Subdomains::EXCLUDE, kGroup1_, expires); |
| 529 } |
| 530 EXPECT_EQ(max_client_count, client_count()); |
| 531 |
| 532 // Add one more client, forcing the cache to evict the expired one. |
| 533 cache()->SetClient(kOrigin1_, MakeEndpoint(max_client_count), |
| 534 ReportingClient::Subdomains::EXCLUDE, kGroup1_, |
| 535 tomorrow()); |
| 536 EXPECT_EQ(max_client_count, client_count()); |
| 537 EXPECT_FALSE(FindClientInCache(cache(), kOrigin1_, |
| 538 MakeEndpoint(max_client_count - 1))); |
| 539 } |
| 540 |
470 } // namespace | 541 } // namespace |
471 } // namespace net | 542 } // namespace net |
OLD | NEW |