forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_cache_data_remover_unittest.cc
338 lines (297 loc) · 13 KB
/
http_cache_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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// Copyright 2018 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 <memory>
#include <string>
#include <vector>
#include "base/bind.h"
#include "base/files/file_util.h"
#include "base/location.h"
#include "base/run_loop.h"
#include "base/stl_util.h"
#include "base/test/scoped_task_environment.h"
#include "base/threading/thread_task_runner_handle.h"
#include "mojo/public/cpp/bindings/interface_request.h"
#include "net/base/cache_type.h"
#include "net/base/net_errors.h"
#include "net/base/test_completion_callback.h"
#include "net/disk_cache/disk_cache.h"
#include "net/http/http_cache.h"
#include "net/http/http_network_session.h"
#include "net/http/http_server_properties_manager.h"
#include "net/http/http_transaction_factory.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_builder.h"
#include "services/network/network_context.h"
#include "services/network/network_service.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace network {
namespace {
struct CacheTestEntry {
const char* url;
const char* date;
};
HttpCacheDataRemover::HttpCacheDataRemoverCallback
MakeHttpCacheDataRemoverCallback(base::OnceClosure callback) {
return base::BindOnce(
[](base::OnceClosure callback, HttpCacheDataRemover* remover) {
std::move(callback).Run();
},
std::move(callback));
}
constexpr CacheTestEntry kCacheEntries[] = {
{"http://www.google.com", "15 Jun 1975"},
{"https://www.google.com", "15 Jun 1985"},
{"http://www.wikipedia.com", "15 Jun 1995"},
{"https://www.wikipedia.com", "15 Jun 2005"},
{"http://localhost:1234/mysite", "15 Jun 2015"},
{"https://localhost:1234/mysite", "15 Jun 2016"},
{"http://localhost:3456/yoursite", "15 Jun 2017"},
{"https://localhost:3456/yoursite", "15 Jun 2018"}};
mojom::NetworkContextParamsPtr CreateContextParams() {
mojom::NetworkContextParamsPtr params = mojom::NetworkContextParams::New();
// Use a fixed proxy config, to avoid dependencies on local network
// configuration.
params->initial_proxy_config = net::ProxyConfigWithAnnotation::CreateDirect();
return params;
}
class HttpCacheDataRemoverTest : public testing::Test {
public:
HttpCacheDataRemoverTest()
: scoped_task_environment_(
base::test::ScopedTaskEnvironment::MainThreadType::IO),
network_service_(NetworkService::CreateForTesting()) {}
~HttpCacheDataRemoverTest() override = default;
void SetUp() override {
InitNetworkContext();
net::HttpCache* cache = network_context_->url_request_context()
->http_transaction_factory()
->GetCache();
ASSERT_TRUE(cache);
{
net::TestCompletionCallback callback;
int rv = cache->GetBackend(&backend_, callback.callback());
ASSERT_EQ(net::OK, callback.GetResult(rv));
ASSERT_TRUE(backend_);
}
// Create some entries in the cache.
for (const CacheTestEntry& test_entry : kCacheEntries) {
disk_cache::Entry* entry = nullptr;
net::TestCompletionCallback callback;
int rv = backend_->CreateEntry(test_entry.url, net::HIGHEST, &entry,
callback.callback());
ASSERT_EQ(net::OK, callback.GetResult(rv));
ASSERT_TRUE(entry);
base::Time time;
ASSERT_TRUE(base::Time::FromString(test_entry.date, &time));
entry->SetLastUsedTimeForTest(time);
entry->Close();
scoped_task_environment_.RunUntilIdle();
}
ASSERT_EQ(base::size(kCacheEntries),
static_cast<size_t>(backend_->GetEntryCount()));
}
void RemoveData(mojom::ClearDataFilterPtr filter,
base::Time start_time,
base::Time end_time) {
base::RunLoop run_loop;
std::unique_ptr<HttpCacheDataRemover> data_remover =
HttpCacheDataRemover::CreateAndStart(
network_context_->url_request_context(), std::move(filter),
start_time, end_time,
MakeHttpCacheDataRemoverCallback(run_loop.QuitClosure()));
run_loop.Run();
}
bool HasEntry(const std::string& key) {
disk_cache::Entry* entry = nullptr;
base::RunLoop run_loop;
net::TestCompletionCallback callback;
if (backend_->OpenEntry(key, net::HIGHEST, &entry, callback.callback()) ==
net::ERR_IO_PENDING) {
callback.WaitForResult();
}
if (entry)
entry->Close();
return entry != nullptr;
}
protected:
void InitNetworkContext() {
mojom::NetworkContextParamsPtr context_params = CreateContextParams();
context_params->http_cache_enabled = true;
network_context_ = std::make_unique<NetworkContext>(
network_service_.get(), mojo::MakeRequest(&network_context_ptr_),
std::move(context_params));
}
base::test::ScopedTaskEnvironment scoped_task_environment_;
std::unique_ptr<NetworkService> network_service_;
std::unique_ptr<NetworkContext> network_context_;
// Stores the NetworkContextPtr of the most recently created NetworkContext.
mojom::NetworkContextPtr network_context_ptr_;
disk_cache::Backend* backend_ = nullptr;
};
TEST_F(HttpCacheDataRemoverTest, ClearAll) {
EXPECT_NE(0, backend_->GetEntryCount());
RemoveData(/*url_filter=*/nullptr, base::Time(), base::Time());
EXPECT_EQ(0, backend_->GetEntryCount());
}
TEST_F(HttpCacheDataRemoverTest, FilterDeleteByDomain) {
mojom::ClearDataFilterPtr filter = mojom::ClearDataFilter::New();
filter->type = mojom::ClearDataFilter_Type::DELETE_MATCHES;
filter->domains.push_back("wikipedia.com");
filter->domains.push_back("google.com");
RemoveData(std::move(filter), base::Time(), base::Time());
EXPECT_FALSE(HasEntry(kCacheEntries[0].url));
EXPECT_FALSE(HasEntry(kCacheEntries[1].url));
EXPECT_FALSE(HasEntry(kCacheEntries[2].url));
EXPECT_FALSE(HasEntry(kCacheEntries[3].url));
EXPECT_EQ(4, backend_->GetEntryCount());
}
TEST_F(HttpCacheDataRemoverTest, FilterKeepByDomain) {
mojom::ClearDataFilterPtr filter = mojom::ClearDataFilter::New();
filter->type = mojom::ClearDataFilter_Type::KEEP_MATCHES;
filter->domains.push_back("wikipedia.com");
filter->domains.push_back("google.com");
RemoveData(std::move(filter), base::Time(), base::Time());
EXPECT_TRUE(HasEntry(kCacheEntries[0].url));
EXPECT_TRUE(HasEntry(kCacheEntries[1].url));
EXPECT_TRUE(HasEntry(kCacheEntries[2].url));
EXPECT_TRUE(HasEntry(kCacheEntries[3].url));
EXPECT_EQ(4, backend_->GetEntryCount());
}
TEST_F(HttpCacheDataRemoverTest, FilterDeleteByOrigin) {
mojom::ClearDataFilterPtr filter = mojom::ClearDataFilter::New();
filter->type = mojom::ClearDataFilter_Type::DELETE_MATCHES;
filter->origins.push_back(url::Origin::Create(GURL("http://www.google.com")));
filter->origins.push_back(url::Origin::Create(GURL("http://localhost:1234")));
RemoveData(std::move(filter), base::Time(), base::Time());
EXPECT_FALSE(HasEntry(kCacheEntries[0].url));
EXPECT_FALSE(HasEntry(kCacheEntries[4].url));
EXPECT_EQ(6, backend_->GetEntryCount());
}
TEST_F(HttpCacheDataRemoverTest, FilterKeepByOrigin) {
mojom::ClearDataFilterPtr filter = mojom::ClearDataFilter::New();
filter->type = mojom::ClearDataFilter_Type::KEEP_MATCHES;
filter->origins.push_back(url::Origin::Create(GURL("http://www.google.com")));
filter->origins.push_back(url::Origin::Create(GURL("http://localhost:1234")));
RemoveData(std::move(filter), base::Time(), base::Time());
EXPECT_TRUE(HasEntry(kCacheEntries[0].url));
EXPECT_TRUE(HasEntry(kCacheEntries[4].url));
EXPECT_EQ(2, backend_->GetEntryCount());
}
TEST_F(HttpCacheDataRemoverTest, FilterDeleteByDomainAndOrigin) {
mojom::ClearDataFilterPtr filter = mojom::ClearDataFilter::New();
filter->type = mojom::ClearDataFilter_Type::DELETE_MATCHES;
filter->domains.push_back("wikipedia.com");
filter->origins.push_back(url::Origin::Create(GURL("http://localhost:1234")));
RemoveData(std::move(filter), base::Time(), base::Time());
EXPECT_FALSE(HasEntry(kCacheEntries[2].url));
EXPECT_FALSE(HasEntry(kCacheEntries[3].url));
EXPECT_FALSE(HasEntry(kCacheEntries[4].url));
EXPECT_EQ(5, backend_->GetEntryCount());
}
TEST_F(HttpCacheDataRemoverTest, FilterKeepByDomainAndOrigin) {
mojom::ClearDataFilterPtr filter = mojom::ClearDataFilter::New();
filter->type = mojom::ClearDataFilter_Type::KEEP_MATCHES;
filter->domains.push_back("wikipedia.com");
filter->origins.push_back(url::Origin::Create(GURL("http://localhost:1234")));
RemoveData(std::move(filter), base::Time(), base::Time());
EXPECT_TRUE(HasEntry(kCacheEntries[2].url));
EXPECT_TRUE(HasEntry(kCacheEntries[3].url));
EXPECT_TRUE(HasEntry(kCacheEntries[4].url));
EXPECT_EQ(3, backend_->GetEntryCount());
}
TEST_F(HttpCacheDataRemoverTest, FilterByDateFromUnbounded) {
base::Time end_time;
ASSERT_TRUE(base::Time::FromString(kCacheEntries[5].date, &end_time));
RemoveData(/*filter=*/nullptr, base::Time(), end_time);
EXPECT_TRUE(HasEntry(kCacheEntries[5].url));
EXPECT_TRUE(HasEntry(kCacheEntries[6].url));
EXPECT_TRUE(HasEntry(kCacheEntries[7].url));
EXPECT_EQ(3, backend_->GetEntryCount());
}
TEST_F(HttpCacheDataRemoverTest, FilterByDateToUnbounded) {
base::Time start_time;
ASSERT_TRUE(base::Time::FromString(kCacheEntries[5].date, &start_time));
RemoveData(/*filter=*/nullptr, start_time, base::Time());
EXPECT_FALSE(HasEntry(kCacheEntries[5].url));
EXPECT_FALSE(HasEntry(kCacheEntries[6].url));
EXPECT_FALSE(HasEntry(kCacheEntries[7].url));
EXPECT_EQ(5, backend_->GetEntryCount());
}
TEST_F(HttpCacheDataRemoverTest, FilterByDateRange) {
base::Time start_time;
ASSERT_TRUE(base::Time::FromString(kCacheEntries[1].date, &start_time));
base::Time end_time;
ASSERT_TRUE(base::Time::FromString(kCacheEntries[6].date, &end_time));
RemoveData(/*filter=*/nullptr, start_time, end_time);
EXPECT_FALSE(HasEntry(kCacheEntries[1].url));
EXPECT_FALSE(HasEntry(kCacheEntries[2].url));
EXPECT_FALSE(HasEntry(kCacheEntries[3].url));
EXPECT_FALSE(HasEntry(kCacheEntries[4].url));
EXPECT_FALSE(HasEntry(kCacheEntries[5].url));
EXPECT_EQ(3, backend_->GetEntryCount());
}
TEST_F(HttpCacheDataRemoverTest, FilterDeleteByDomainAndDate) {
mojom::ClearDataFilterPtr filter = mojom::ClearDataFilter::New();
filter->type = mojom::ClearDataFilter_Type::DELETE_MATCHES;
filter->domains.push_back("google.com");
filter->domains.push_back("wikipedia.com");
base::Time start_time;
ASSERT_TRUE(base::Time::FromString(kCacheEntries[1].date, &start_time));
base::Time end_time;
ASSERT_TRUE(base::Time::FromString(kCacheEntries[6].date, &end_time));
RemoveData(std::move(filter), start_time, end_time);
EXPECT_FALSE(HasEntry(kCacheEntries[1].url));
EXPECT_FALSE(HasEntry(kCacheEntries[2].url));
EXPECT_FALSE(HasEntry(kCacheEntries[3].url));
EXPECT_EQ(5, backend_->GetEntryCount());
}
TEST_F(HttpCacheDataRemoverTest, FilterKeepByDomainAndDate) {
mojom::ClearDataFilterPtr filter = mojom::ClearDataFilter::New();
filter->type = mojom::ClearDataFilter_Type::KEEP_MATCHES;
filter->domains.push_back("google.com");
filter->domains.push_back("wikipedia.com");
base::Time start_time;
ASSERT_TRUE(base::Time::FromString(kCacheEntries[1].date, &start_time));
base::Time end_time;
ASSERT_TRUE(base::Time::FromString(kCacheEntries[6].date, &end_time));
RemoveData(std::move(filter), start_time, end_time);
EXPECT_FALSE(HasEntry(kCacheEntries[4].url));
EXPECT_FALSE(HasEntry(kCacheEntries[5].url));
EXPECT_EQ(6, backend_->GetEntryCount());
}
TEST_F(HttpCacheDataRemoverTest, DeleteHttpRemover) {
bool callback_invoked = false;
std::unique_ptr<HttpCacheDataRemover> data_remover =
HttpCacheDataRemover::CreateAndStart(
network_context_->url_request_context(),
/*url_filter=*/nullptr, base::Time(), base::Time(),
MakeHttpCacheDataRemoverCallback(base::BindOnce(
[](bool* callback_invoked) { *callback_invoked = true; },
&callback_invoked)));
// Delete the data remover and make sure after all task have been processed
// that the callback wasn't invoked.
data_remover.reset();
scoped_task_environment_.RunUntilIdle();
EXPECT_FALSE(callback_invoked);
}
// This test exercises the different code paths wrt whether the cache's backend
// is available when clearing the cache.
TEST_F(HttpCacheDataRemoverTest, TestDelayedBackend) {
// Reinit the network context without retrieving the cache's backend so the
// call to clear the cache does it.
InitNetworkContext();
net::HttpCache* http_cache = network_context_->url_request_context()
->http_transaction_factory()
->GetCache();
ASSERT_TRUE(http_cache);
ASSERT_FALSE(http_cache->GetCurrentBackend());
RemoveData(/*filter=*/nullptr, base::Time(), base::Time());
// This should have retrieved the backend of the cache.
EXPECT_TRUE(http_cache->GetCurrentBackend());
// Clear again the cache to test that it works when the backend is readily
// available.
RemoveData(/*filter=*/nullptr, base::Time(), base::Time());
}
} // namespace
} // namespace network