forked from kiwibrowser/src.next
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_cache_lookup_manager_unittest.cc
346 lines (280 loc) · 12.8 KB
/
http_cache_lookup_manager_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
339
340
341
342
343
344
345
346
// Copyright (c) 2016 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 "base/feature_list.h"
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "net/base/features.h"
#include "net/base/net_errors.h"
#include "net/base/schemeful_site.h"
#include "net/base/test_completion_callback.h"
#include "net/http/http_cache_lookup_manager.h"
#include "net/http/http_transaction_test_util.h"
#include "net/http/mock_http_cache.h"
#include "net/test/gtest_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest-param-test.h"
#include "testing/gtest/include/gtest/gtest.h"
using net::test::IsOk;
namespace net {
namespace {
class MockServerPushHelper : public ServerPushDelegate::ServerPushHelper {
public:
explicit MockServerPushHelper(const GURL& url)
: request_url_(url),
network_isolation_key_(SchemefulSite(url), SchemefulSite(url)) {}
const GURL& GetURL() const override { return request_url_; }
NetworkIsolationKey GetNetworkIsolationKey() const override {
return network_isolation_key_;
}
void set_network_isolation_key(
const net::NetworkIsolationKey& network_isolation_key) {
network_isolation_key_ = network_isolation_key;
}
MOCK_METHOD0(Cancel, void());
private:
const GURL request_url_;
NetworkIsolationKey network_isolation_key_;
};
std::unique_ptr<MockTransaction> CreateMockTransaction(const GURL& url) {
MockTransaction mock_trans = {
url.spec().c_str(),
"GET",
base::Time(),
"",
LOAD_NORMAL,
DefaultTransportInfo(),
"HTTP/1.1 200 OK",
"Date: Wed, 28 Nov 2007 09:40:09 GMT\n"
"Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n",
base::Time(),
"<html><body>Google Blah Blah</body></html>",
{},
TEST_MODE_NORMAL,
nullptr,
nullptr,
nullptr,
0,
0,
OK,
};
return std::make_unique<MockTransaction>(mock_trans);
}
void PopulateCacheEntry(HttpCache* cache, const GURL& request_url) {
TestCompletionCallback callback;
std::unique_ptr<MockTransaction> mock_trans =
CreateMockTransaction(request_url);
AddMockTransaction(mock_trans.get());
MockHttpRequest request(*(mock_trans.get()));
std::unique_ptr<HttpTransaction> trans;
int rv = cache->CreateTransaction(DEFAULT_PRIORITY, &trans);
EXPECT_THAT(rv, IsOk());
ASSERT_TRUE(trans.get());
rv = trans->Start(&request, callback.callback(), NetLogWithSource());
base::RunLoop().RunUntilIdle();
if (rv == ERR_IO_PENDING)
rv = callback.WaitForResult();
ASSERT_EQ(mock_trans->start_return_code, rv);
if (OK != rv)
return;
const HttpResponseInfo* response = trans->GetResponseInfo();
ASSERT_TRUE(response);
std::string content;
rv = ReadTransaction(trans.get(), &content);
EXPECT_THAT(rv, IsOk());
std::string expected(mock_trans->data);
EXPECT_EQ(expected, content);
RemoveMockTransaction(mock_trans.get());
}
} // namespace
TEST(HttpCacheLookupManagerTest, ServerPushMissCache) {
base::test::TaskEnvironment task_environment;
MockHttpCache mock_cache;
HttpCacheLookupManager push_delegate(mock_cache.http_cache());
GURL request_url("http://www.example.com/pushed.jpg");
std::unique_ptr<MockServerPushHelper> push_helper =
std::make_unique<MockServerPushHelper>(request_url);
MockServerPushHelper* push_helper_ptr = push_helper.get();
// Receive a server push and should not cancel the push.
EXPECT_CALL(*push_helper_ptr, Cancel()).Times(0);
push_delegate.OnPush(std::move(push_helper), NetLogWithSource());
base::RunLoop().RunUntilIdle();
// Make sure no network transaction is created.
EXPECT_EQ(0, mock_cache.network_layer()->transaction_count());
EXPECT_EQ(0, mock_cache.disk_cache()->open_count());
EXPECT_EQ(0, mock_cache.disk_cache()->create_count());
}
TEST(HttpCacheLookupManagerTest, ServerPushDoNotCreateCacheEntry) {
base::test::TaskEnvironment task_environment;
MockHttpCache mock_cache;
HttpCacheLookupManager push_delegate(mock_cache.http_cache());
GURL request_url("http://www.example.com/pushed.jpg");
std::unique_ptr<MockServerPushHelper> push_helper =
std::make_unique<MockServerPushHelper>(request_url);
MockServerPushHelper* push_helper_ptr = push_helper.get();
// Receive a server push and should not cancel the push.
EXPECT_CALL(*push_helper_ptr, Cancel()).Times(0);
push_delegate.OnPush(std::move(push_helper), NetLogWithSource());
base::RunLoop().RunUntilIdle();
// Receive another server push for the same url.
std::unique_ptr<MockServerPushHelper> push_helper2 =
std::make_unique<MockServerPushHelper>(request_url);
MockServerPushHelper* push_helper_ptr2 = push_helper2.get();
EXPECT_CALL(*push_helper_ptr2, Cancel()).Times(0);
push_delegate.OnPush(std::move(push_helper2), NetLogWithSource());
base::RunLoop().RunUntilIdle();
// Verify no network transaction is created.
EXPECT_EQ(0, mock_cache.network_layer()->transaction_count());
EXPECT_EQ(0, mock_cache.disk_cache()->open_count());
// Verify no cache entry is created for server push lookup.
EXPECT_EQ(0, mock_cache.disk_cache()->create_count());
}
// Parameterized by whether the network isolation key are the same for the
// server push and corresponding cache entry.
class HttpCacheLookupManagerTest_NetworkIsolationKey
: public ::testing::Test,
public ::testing::WithParamInterface<
bool /* use_same_network_isolation_key */> {};
TEST_P(HttpCacheLookupManagerTest_NetworkIsolationKey, ServerPushCacheStatus) {
bool use_same_network_isolation_key = GetParam();
bool split_cache_enabled = base::FeatureList::IsEnabled(
net::features::kSplitCacheByNetworkIsolationKey);
base::test::TaskEnvironment task_environment;
MockHttpCache mock_cache;
HttpCacheLookupManager push_delegate(mock_cache.http_cache());
GURL request_url("http://www.example.com/pushed.jpg");
// Populate the cache entry so that the cache lookup for server push hits.
PopulateCacheEntry(mock_cache.http_cache(), request_url);
EXPECT_EQ(1, mock_cache.network_layer()->transaction_count());
EXPECT_EQ(0, mock_cache.disk_cache()->open_count());
EXPECT_EQ(1, mock_cache.disk_cache()->create_count());
// Add another mock transaction since the OnPush will create a new cache
// transaction.
std::unique_ptr<MockTransaction> mock_trans =
CreateMockTransaction(request_url);
AddMockTransaction(mock_trans.get());
std::unique_ptr<MockServerPushHelper> push_helper =
std::make_unique<MockServerPushHelper>(request_url);
if (!use_same_network_isolation_key) {
SchemefulSite site(GURL("http://www.abc.com"));
push_helper->set_network_isolation_key(
net::NetworkIsolationKey(site, site));
}
MockServerPushHelper* push_helper_ptr = push_helper.get();
int expected_cancel_times =
use_same_network_isolation_key || !split_cache_enabled ? 1 : 0;
EXPECT_CALL(*push_helper_ptr, Cancel()).Times(expected_cancel_times);
push_delegate.OnPush(std::move(push_helper), NetLogWithSource());
base::RunLoop().RunUntilIdle();
// Make sure no new net layer transaction is created.
EXPECT_EQ(1, mock_cache.network_layer()->transaction_count());
int expected_open_count =
use_same_network_isolation_key || !split_cache_enabled ? 1 : 0;
EXPECT_EQ(expected_open_count, mock_cache.disk_cache()->open_count());
EXPECT_EQ(1, mock_cache.disk_cache()->create_count());
RemoveMockTransaction(mock_trans.get());
}
INSTANTIATE_TEST_SUITE_P(
All,
HttpCacheLookupManagerTest_NetworkIsolationKey,
::testing::Bool());
// Test when a server push is received while the HttpCacheLookupManager has a
// pending lookup transaction for the same URL, the new server push will not
// send a new lookup transaction and should not be canceled.
TEST(HttpCacheLookupManagerTest, ServerPushPendingLookup) {
base::test::TaskEnvironment task_environment;
MockHttpCache mock_cache;
HttpCacheLookupManager push_delegate(mock_cache.http_cache());
GURL request_url("http://www.example.com/pushed.jpg");
// Populate the cache entry so that the cache lookup for server push hits.
PopulateCacheEntry(mock_cache.http_cache(), request_url);
EXPECT_EQ(1, mock_cache.network_layer()->transaction_count());
EXPECT_EQ(0, mock_cache.disk_cache()->open_count());
EXPECT_EQ(1, mock_cache.disk_cache()->create_count());
// Add another mock transaction since the OnPush will create a new cache
// transaction.
std::unique_ptr<MockTransaction> mock_trans =
CreateMockTransaction(request_url);
AddMockTransaction(mock_trans.get());
std::unique_ptr<MockServerPushHelper> push_helper =
std::make_unique<MockServerPushHelper>(request_url);
MockServerPushHelper* push_helper_ptr = push_helper.get();
// Receive a server push and should cancel the push eventually.
EXPECT_CALL(*push_helper_ptr, Cancel()).Times(1);
push_delegate.OnPush(std::move(push_helper), NetLogWithSource());
std::unique_ptr<MockServerPushHelper> push_helper2 =
std::make_unique<MockServerPushHelper>(request_url);
MockServerPushHelper* push_helper_ptr2 = push_helper2.get();
// Receive another server push and should not cancel the push.
EXPECT_CALL(*push_helper_ptr2, Cancel()).Times(0);
push_delegate.OnPush(std::move(push_helper2), NetLogWithSource());
base::RunLoop().RunUntilIdle();
// Make sure no new net layer transaction is created.
EXPECT_EQ(1, mock_cache.network_layer()->transaction_count());
EXPECT_EQ(1, mock_cache.disk_cache()->open_count());
EXPECT_EQ(1, mock_cache.disk_cache()->create_count());
RemoveMockTransaction(mock_trans.get());
}
// Test the server push lookup is based on the full url.
TEST(HttpCacheLookupManagerTest, ServerPushLookupOnUrl) {
base::test::TaskEnvironment task_environment;
MockHttpCache mock_cache;
HttpCacheLookupManager push_delegate(mock_cache.http_cache());
GURL request_url("http://www.example.com/pushed.jpg?u=0");
GURL request_url2("http://www.example.com/pushed.jpg?u=1");
// Populate the cache entry so that the cache lookup for server push hits.
PopulateCacheEntry(mock_cache.http_cache(), request_url);
EXPECT_EQ(1, mock_cache.network_layer()->transaction_count());
EXPECT_EQ(0, mock_cache.disk_cache()->open_count());
EXPECT_EQ(1, mock_cache.disk_cache()->create_count());
// Add another mock transaction since the OnPush will create a new cache
// transaction.
std::unique_ptr<MockTransaction> mock_trans =
CreateMockTransaction(request_url);
AddMockTransaction(mock_trans.get());
std::unique_ptr<MockServerPushHelper> push_helper =
std::make_unique<MockServerPushHelper>(request_url);
MockServerPushHelper* push_helper_ptr = push_helper.get();
// Receive a server push and should cancel the push eventually.
EXPECT_CALL(*push_helper_ptr, Cancel()).Times(1);
push_delegate.OnPush(std::move(push_helper), NetLogWithSource());
// Run until the lookup transaction finishes for the first server push.
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, mock_cache.network_layer()->transaction_count());
EXPECT_EQ(1, mock_cache.disk_cache()->open_count());
EXPECT_EQ(1, mock_cache.disk_cache()->create_count());
RemoveMockTransaction(mock_trans.get());
AddMockTransaction(mock_trans.get());
// Receive the second server push with same url after the first lookup
// finishes, and should cancel the push.
std::unique_ptr<MockServerPushHelper> push_helper2 =
std::make_unique<MockServerPushHelper>(request_url);
MockServerPushHelper* push_helper_ptr2 = push_helper2.get();
EXPECT_CALL(*push_helper_ptr2, Cancel()).Times(1);
push_delegate.OnPush(std::move(push_helper2), NetLogWithSource());
// Run until the lookup transaction finishes for the second server push.
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, mock_cache.network_layer()->transaction_count());
EXPECT_EQ(2, mock_cache.disk_cache()->open_count());
EXPECT_EQ(1, mock_cache.disk_cache()->create_count());
RemoveMockTransaction(mock_trans.get());
std::unique_ptr<MockTransaction> mock_trans3 =
CreateMockTransaction(request_url2);
AddMockTransaction(mock_trans3.get());
// Receive the third server push with a different url after lookup for a
// similar server push has been completed, should not cancel the push.
std::unique_ptr<MockServerPushHelper> push_helper3 =
std::make_unique<MockServerPushHelper>(request_url2);
MockServerPushHelper* push_helper_ptr3 = push_helper3.get();
EXPECT_CALL(*push_helper_ptr3, Cancel()).Times(0);
push_delegate.OnPush(std::move(push_helper3), NetLogWithSource());
base::RunLoop().RunUntilIdle();
// Make sure no new net layer transaction is created.
EXPECT_EQ(1, mock_cache.network_layer()->transaction_count());
EXPECT_EQ(2, mock_cache.disk_cache()->open_count());
EXPECT_EQ(1, mock_cache.disk_cache()->create_count());
RemoveMockTransaction(mock_trans3.get());
}
} // namespace net