OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/reporting/reporting_browsing_data_remover.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/test/simple_test_tick_clock.h" |
| 12 #include "net/reporting/reporting_cache.h" |
| 13 #include "net/reporting/reporting_client.h" |
| 14 #include "net/reporting/reporting_report.h" |
| 15 #include "net/reporting/reporting_test_util.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 namespace net { |
| 19 namespace { |
| 20 |
| 21 class ReportingBrowsingDataRemoverTest : public ReportingTestBase { |
| 22 protected: |
| 23 void RemoveBrowsingData(bool remove_reports, |
| 24 bool remove_clients, |
| 25 std::string host) { |
| 26 base::Callback<bool(const GURL&)> origin_filter; |
| 27 if (!host.empty()) { |
| 28 origin_filter = |
| 29 base::Bind(&ReportingBrowsingDataRemoverTest::HostIs, host); |
| 30 } |
| 31 ReportingBrowsingDataRemover::RemoveBrowsingData( |
| 32 context(), remove_reports, remove_clients, origin_filter); |
| 33 } |
| 34 |
| 35 static bool HostIs(std::string host, const GURL& url) { |
| 36 return url.host() == host; |
| 37 } |
| 38 |
| 39 size_t report_count() { |
| 40 std::vector<const ReportingReport*> reports; |
| 41 cache()->GetReports(&reports); |
| 42 return reports.size(); |
| 43 } |
| 44 |
| 45 size_t client_count() { |
| 46 std::vector<const ReportingClient*> clients; |
| 47 cache()->GetClients(&clients); |
| 48 return clients.size(); |
| 49 } |
| 50 |
| 51 const GURL kUrl1_ = GURL("https://origin1/path"); |
| 52 const GURL kUrl2_ = GURL("https://origin2/path"); |
| 53 const url::Origin kOrigin1_ = url::Origin(kUrl1_); |
| 54 const url::Origin kOrigin2_ = url::Origin(kUrl2_); |
| 55 const GURL kEndpoint_ = GURL("https://endpoint/"); |
| 56 const std::string kGroup_ = "group"; |
| 57 const std::string kType_ = "default"; |
| 58 }; |
| 59 |
| 60 TEST_F(ReportingBrowsingDataRemoverTest, RemoveNothing) { |
| 61 cache()->AddReport(kUrl1_, kGroup_, kType_, |
| 62 base::MakeUnique<base::DictionaryValue>(), |
| 63 tick_clock()->NowTicks(), 0); |
| 64 cache()->AddReport(kUrl2_, kGroup_, kType_, |
| 65 base::MakeUnique<base::DictionaryValue>(), |
| 66 tick_clock()->NowTicks(), 0); |
| 67 cache()->SetClient(kOrigin1_, kEndpoint_, |
| 68 ReportingClient::Subdomains::EXCLUDE, kGroup_, |
| 69 tick_clock()->NowTicks() + base::TimeDelta::FromDays(7)); |
| 70 cache()->SetClient(kOrigin2_, kEndpoint_, |
| 71 ReportingClient::Subdomains::EXCLUDE, kGroup_, |
| 72 tick_clock()->NowTicks() + base::TimeDelta::FromDays(7)); |
| 73 |
| 74 RemoveBrowsingData(/* remove_reports= */ false, /* remove_clients= */ false, |
| 75 /* host= */ ""); |
| 76 EXPECT_EQ(2u, report_count()); |
| 77 EXPECT_EQ(2u, client_count()); |
| 78 } |
| 79 |
| 80 TEST_F(ReportingBrowsingDataRemoverTest, RemoveAllReports) { |
| 81 cache()->AddReport(kUrl1_, kGroup_, kType_, |
| 82 base::MakeUnique<base::DictionaryValue>(), |
| 83 tick_clock()->NowTicks(), 0); |
| 84 cache()->AddReport(kUrl2_, kGroup_, kType_, |
| 85 base::MakeUnique<base::DictionaryValue>(), |
| 86 tick_clock()->NowTicks(), 0); |
| 87 cache()->SetClient(kOrigin1_, kEndpoint_, |
| 88 ReportingClient::Subdomains::EXCLUDE, kGroup_, |
| 89 tick_clock()->NowTicks() + base::TimeDelta::FromDays(7)); |
| 90 cache()->SetClient(kOrigin2_, kEndpoint_, |
| 91 ReportingClient::Subdomains::EXCLUDE, kGroup_, |
| 92 tick_clock()->NowTicks() + base::TimeDelta::FromDays(7)); |
| 93 |
| 94 RemoveBrowsingData(/* remove_reports= */ true, /* remove_clients= */ false, |
| 95 /* host= */ ""); |
| 96 EXPECT_EQ(0u, report_count()); |
| 97 EXPECT_EQ(2u, client_count()); |
| 98 } |
| 99 |
| 100 TEST_F(ReportingBrowsingDataRemoverTest, RemoveAllClients) { |
| 101 cache()->AddReport(kUrl1_, kGroup_, kType_, |
| 102 base::MakeUnique<base::DictionaryValue>(), |
| 103 tick_clock()->NowTicks(), 0); |
| 104 cache()->AddReport(kUrl2_, kGroup_, kType_, |
| 105 base::MakeUnique<base::DictionaryValue>(), |
| 106 tick_clock()->NowTicks(), 0); |
| 107 cache()->SetClient(kOrigin1_, kEndpoint_, |
| 108 ReportingClient::Subdomains::EXCLUDE, kGroup_, |
| 109 tick_clock()->NowTicks() + base::TimeDelta::FromDays(7)); |
| 110 cache()->SetClient(kOrigin2_, kEndpoint_, |
| 111 ReportingClient::Subdomains::EXCLUDE, kGroup_, |
| 112 tick_clock()->NowTicks() + base::TimeDelta::FromDays(7)); |
| 113 |
| 114 RemoveBrowsingData(/* remove_reports= */ false, /* remove_clients= */ true, |
| 115 /* host= */ ""); |
| 116 EXPECT_EQ(2u, report_count()); |
| 117 EXPECT_EQ(0u, client_count()); |
| 118 } |
| 119 |
| 120 TEST_F(ReportingBrowsingDataRemoverTest, RemoveAllReportsAndClients) { |
| 121 cache()->AddReport(kUrl1_, kGroup_, kType_, |
| 122 base::MakeUnique<base::DictionaryValue>(), |
| 123 tick_clock()->NowTicks(), 0); |
| 124 cache()->AddReport(kUrl2_, kGroup_, kType_, |
| 125 base::MakeUnique<base::DictionaryValue>(), |
| 126 tick_clock()->NowTicks(), 0); |
| 127 cache()->SetClient(kOrigin1_, kEndpoint_, |
| 128 ReportingClient::Subdomains::EXCLUDE, kGroup_, |
| 129 tick_clock()->NowTicks() + base::TimeDelta::FromDays(7)); |
| 130 cache()->SetClient(kOrigin2_, kEndpoint_, |
| 131 ReportingClient::Subdomains::EXCLUDE, kGroup_, |
| 132 tick_clock()->NowTicks() + base::TimeDelta::FromDays(7)); |
| 133 |
| 134 RemoveBrowsingData(/* remove_reports= */ true, /* remove_clients= */ true, |
| 135 /* host= */ ""); |
| 136 EXPECT_EQ(0u, report_count()); |
| 137 EXPECT_EQ(0u, client_count()); |
| 138 } |
| 139 |
| 140 TEST_F(ReportingBrowsingDataRemoverTest, RemoveSomeReports) { |
| 141 cache()->AddReport(kUrl1_, kGroup_, kType_, |
| 142 base::MakeUnique<base::DictionaryValue>(), |
| 143 tick_clock()->NowTicks(), 0); |
| 144 cache()->AddReport(kUrl2_, kGroup_, kType_, |
| 145 base::MakeUnique<base::DictionaryValue>(), |
| 146 tick_clock()->NowTicks(), 0); |
| 147 cache()->SetClient(kOrigin1_, kEndpoint_, |
| 148 ReportingClient::Subdomains::EXCLUDE, kGroup_, |
| 149 tick_clock()->NowTicks() + base::TimeDelta::FromDays(7)); |
| 150 cache()->SetClient(kOrigin2_, kEndpoint_, |
| 151 ReportingClient::Subdomains::EXCLUDE, kGroup_, |
| 152 tick_clock()->NowTicks() + base::TimeDelta::FromDays(7)); |
| 153 |
| 154 RemoveBrowsingData(/* remove_reports= */ true, /* remove_clients= */ false, |
| 155 /* host= */ kUrl1_.host()); |
| 156 EXPECT_EQ(2u, client_count()); |
| 157 |
| 158 std::vector<const ReportingReport*> reports; |
| 159 cache()->GetReports(&reports); |
| 160 ASSERT_EQ(1u, reports.size()); |
| 161 EXPECT_EQ(kUrl2_, reports[0]->url); |
| 162 } |
| 163 |
| 164 TEST_F(ReportingBrowsingDataRemoverTest, RemoveSomeClients) { |
| 165 cache()->AddReport(kUrl1_, kGroup_, kType_, |
| 166 base::MakeUnique<base::DictionaryValue>(), |
| 167 tick_clock()->NowTicks(), 0); |
| 168 cache()->AddReport(kUrl2_, kGroup_, kType_, |
| 169 base::MakeUnique<base::DictionaryValue>(), |
| 170 tick_clock()->NowTicks(), 0); |
| 171 cache()->SetClient(kOrigin1_, kEndpoint_, |
| 172 ReportingClient::Subdomains::EXCLUDE, kGroup_, |
| 173 tick_clock()->NowTicks() + base::TimeDelta::FromDays(7)); |
| 174 cache()->SetClient(kOrigin2_, kEndpoint_, |
| 175 ReportingClient::Subdomains::EXCLUDE, kGroup_, |
| 176 tick_clock()->NowTicks() + base::TimeDelta::FromDays(7)); |
| 177 |
| 178 RemoveBrowsingData(/* remove_reports= */ false, /* remove_clients= */ true, |
| 179 /* host= */ kUrl1_.host()); |
| 180 EXPECT_EQ(2u, report_count()); |
| 181 EXPECT_FALSE(FindClientInCache(cache(), kOrigin1_, kEndpoint_) != nullptr); |
| 182 EXPECT_TRUE(FindClientInCache(cache(), kOrigin2_, kEndpoint_) != nullptr); |
| 183 } |
| 184 |
| 185 } // namespace |
| 186 } // namespace net |
OLD | NEW |