forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reporting_browsing_data_remover_unittest.cc
166 lines (135 loc) · 5.15 KB
/
reporting_browsing_data_remover_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/reporting/reporting_browsing_data_remover.h"
#include <memory>
#include <string>
#include "base/bind.h"
#include "base/test/simple_test_tick_clock.h"
#include "net/base/network_isolation_key.h"
#include "net/reporting/reporting_cache.h"
#include "net/reporting/reporting_context.h"
#include "net/reporting/reporting_report.h"
#include "net/reporting/reporting_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace {
class ReportingBrowsingDataRemoverTest : public ReportingTestBase {
protected:
void RemoveBrowsingData(bool remove_reports,
bool remove_clients,
std::string host) {
uint64_t data_type_mask = 0;
if (remove_reports)
data_type_mask |= ReportingBrowsingDataRemover::DATA_TYPE_REPORTS;
if (remove_clients)
data_type_mask |= ReportingBrowsingDataRemover::DATA_TYPE_CLIENTS;
if (!host.empty()) {
base::RepeatingCallback<bool(const GURL&)> origin_filter =
base::BindRepeating(&ReportingBrowsingDataRemoverTest::HostIs, host);
ReportingBrowsingDataRemover::RemoveBrowsingData(cache(), data_type_mask,
origin_filter);
} else {
ReportingBrowsingDataRemover::RemoveAllBrowsingData(cache(),
data_type_mask);
}
}
// TODO(chlily): Take NIK.
void AddReport(const GURL& url) {
cache()->AddReport(NetworkIsolationKey(), url, kUserAgent_, kGroup_, kType_,
std::make_unique<base::DictionaryValue>(), 0,
tick_clock()->NowTicks(), 0);
}
// TODO(chlily): Take NIK.
void SetEndpoint(const url::Origin& origin) {
SetEndpointInCache(
ReportingEndpointGroupKey(NetworkIsolationKey(), origin, kGroup_),
kEndpoint_, base::Time::Now() + base::TimeDelta::FromDays(7));
}
static bool HostIs(std::string host, const GURL& url) {
return url.host() == host;
}
size_t report_count() {
std::vector<const ReportingReport*> reports;
cache()->GetReports(&reports);
return reports.size();
}
const GURL kUrl1_ = GURL("https://origin1/path");
const GURL kUrl2_ = GURL("https://origin2/path");
const url::Origin kOrigin1_ = url::Origin::Create(kUrl1_);
const url::Origin kOrigin2_ = url::Origin::Create(kUrl2_);
const GURL kEndpoint_ = GURL("https://endpoint/");
const std::string kUserAgent_ = "Mozilla/1.0";
const std::string kGroup_ = "group";
const std::string kType_ = "default";
};
TEST_F(ReportingBrowsingDataRemoverTest, RemoveNothing) {
AddReport(kUrl1_);
AddReport(kUrl2_);
SetEndpoint(kOrigin1_);
SetEndpoint(kOrigin2_);
RemoveBrowsingData(/* remove_reports= */ false, /* remove_clients= */ false,
/* host= */ "");
EXPECT_EQ(2u, report_count());
EXPECT_EQ(2u, cache()->GetEndpointCount());
}
TEST_F(ReportingBrowsingDataRemoverTest, RemoveAllReports) {
AddReport(kUrl1_);
AddReport(kUrl2_);
SetEndpoint(kOrigin1_);
SetEndpoint(kOrigin2_);
RemoveBrowsingData(/* remove_reports= */ true, /* remove_clients= */ false,
/* host= */ "");
EXPECT_EQ(0u, report_count());
EXPECT_EQ(2u, cache()->GetEndpointCount());
}
TEST_F(ReportingBrowsingDataRemoverTest, RemoveAllClients) {
AddReport(kUrl1_);
AddReport(kUrl2_);
SetEndpoint(kOrigin1_);
SetEndpoint(kOrigin2_);
RemoveBrowsingData(/* remove_reports= */ false, /* remove_clients= */ true,
/* host= */ "");
EXPECT_EQ(2u, report_count());
EXPECT_EQ(0u, cache()->GetEndpointCount());
}
TEST_F(ReportingBrowsingDataRemoverTest, RemoveAllReportsAndClients) {
AddReport(kUrl1_);
AddReport(kUrl2_);
SetEndpoint(kOrigin1_);
SetEndpoint(kOrigin2_);
RemoveBrowsingData(/* remove_reports= */ true, /* remove_clients= */ true,
/* host= */ "");
EXPECT_EQ(0u, report_count());
EXPECT_EQ(0u, cache()->GetEndpointCount());
}
TEST_F(ReportingBrowsingDataRemoverTest, RemoveSomeReports) {
AddReport(kUrl1_);
AddReport(kUrl2_);
SetEndpoint(kOrigin1_);
SetEndpoint(kOrigin2_);
RemoveBrowsingData(/* remove_reports= */ true, /* remove_clients= */ false,
/* host= */ kUrl1_.host());
EXPECT_EQ(2u, cache()->GetEndpointCount());
std::vector<const ReportingReport*> reports;
cache()->GetReports(&reports);
ASSERT_EQ(1u, reports.size());
EXPECT_EQ(kUrl2_, reports[0]->url);
}
TEST_F(ReportingBrowsingDataRemoverTest, RemoveSomeClients) {
AddReport(kUrl1_);
AddReport(kUrl2_);
SetEndpoint(kOrigin1_);
SetEndpoint(kOrigin2_);
RemoveBrowsingData(/* remove_reports= */ false, /* remove_clients= */ true,
/* host= */ kUrl1_.host());
EXPECT_EQ(2u, report_count());
EXPECT_FALSE(FindEndpointInCache(
ReportingEndpointGroupKey(NetworkIsolationKey(), kOrigin1_, kGroup_),
kEndpoint_));
EXPECT_TRUE(FindEndpointInCache(
ReportingEndpointGroupKey(NetworkIsolationKey(), kOrigin2_, kGroup_),
kEndpoint_));
}
} // namespace
} // namespace net