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
|
// Copyright (c) 2012 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/url_request/view_cache_helper.h"
#include "base/pickle.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_transaction_test_util.h"
#include "net/url_request/url_request_context.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace {
class TestURLRequestContext : public URLRequestContext {
public:
TestURLRequestContext();
~TestURLRequestContext() override { AssertNoURLRequests(); }
// Gets a pointer to the cache backend.
disk_cache::Backend* GetBackend();
private:
HttpCache cache_;
};
TestURLRequestContext::TestURLRequestContext()
: cache_(make_scoped_ptr(new MockNetworkLayer()),
HttpCache::DefaultBackend::InMemory(0),
true) {
set_http_transaction_factory(&cache_);
}
void WriteHeaders(disk_cache::Entry* entry, int flags,
const std::string& data) {
if (data.empty())
return;
base::Pickle pickle;
pickle.WriteInt(flags | 1); // Version 1.
pickle.WriteInt64(0);
pickle.WriteInt64(0);
pickle.WriteString(data);
scoped_refptr<WrappedIOBuffer> buf(new WrappedIOBuffer(
reinterpret_cast<const char*>(pickle.data())));
int len = static_cast<int>(pickle.size());
TestCompletionCallback cb;
int rv = entry->WriteData(0, 0, buf.get(), len, cb.callback(), true);
ASSERT_EQ(len, cb.GetResult(rv));
}
void WriteData(disk_cache::Entry* entry, int index, const std::string& data) {
if (data.empty())
return;
int len = data.length();
scoped_refptr<IOBuffer> buf(new IOBuffer(len));
memcpy(buf->data(), data.data(), data.length());
TestCompletionCallback cb;
int rv = entry->WriteData(index, 0, buf.get(), len, cb.callback(), true);
ASSERT_EQ(len, cb.GetResult(rv));
}
void WriteToEntry(disk_cache::Backend* cache, const std::string& key,
const std::string& data0, const std::string& data1,
const std::string& data2) {
TestCompletionCallback cb;
disk_cache::Entry* entry;
int rv = cache->CreateEntry(key, &entry, cb.callback());
rv = cb.GetResult(rv);
if (rv != OK) {
rv = cache->OpenEntry(key, &entry, cb.callback());
ASSERT_EQ(OK, cb.GetResult(rv));
}
WriteHeaders(entry, 0, data0);
WriteData(entry, 1, data1);
WriteData(entry, 2, data2);
entry->Close();
}
void FillCache(URLRequestContext* context) {
TestCompletionCallback cb;
disk_cache::Backend* cache;
int rv =
context->http_transaction_factory()->GetCache()->GetBackend(
&cache, cb.callback());
ASSERT_EQ(OK, cb.GetResult(rv));
std::string empty;
WriteToEntry(cache, "first", "some", empty, empty);
WriteToEntry(cache, "second", "only hex_dumped", "same", "kind");
WriteToEntry(cache, "third", empty, "another", "thing");
}
} // namespace.
TEST(ViewCacheHelper, EmptyCache) {
TestURLRequestContext context;
ViewCacheHelper helper;
TestCompletionCallback cb;
std::string prefix, data;
int rv = helper.GetContentsHTML(&context, prefix, &data, cb.callback());
EXPECT_EQ(OK, cb.GetResult(rv));
EXPECT_FALSE(data.empty());
}
TEST(ViewCacheHelper, ListContents) {
TestURLRequestContext context;
ViewCacheHelper helper;
FillCache(&context);
std::string prefix, data;
TestCompletionCallback cb;
int rv = helper.GetContentsHTML(&context, prefix, &data, cb.callback());
EXPECT_EQ(OK, cb.GetResult(rv));
EXPECT_EQ(0U, data.find("<html>"));
EXPECT_NE(std::string::npos, data.find("</html>"));
EXPECT_NE(std::string::npos, data.find("first"));
EXPECT_NE(std::string::npos, data.find("second"));
EXPECT_NE(std::string::npos, data.find("third"));
EXPECT_EQ(std::string::npos, data.find("some"));
EXPECT_EQ(std::string::npos, data.find("same"));
EXPECT_EQ(std::string::npos, data.find("thing"));
}
TEST(ViewCacheHelper, DumpEntry) {
TestURLRequestContext context;
ViewCacheHelper helper;
FillCache(&context);
std::string data;
TestCompletionCallback cb;
int rv = helper.GetEntryInfoHTML("second", &context, &data, cb.callback());
EXPECT_EQ(OK, cb.GetResult(rv));
EXPECT_EQ(0U, data.find("<html>"));
EXPECT_NE(std::string::npos, data.find("</html>"));
EXPECT_NE(std::string::npos, data.find("hex_dumped"));
EXPECT_NE(std::string::npos, data.find("same"));
EXPECT_NE(std::string::npos, data.find("kind"));
EXPECT_EQ(std::string::npos, data.find("first"));
EXPECT_EQ(std::string::npos, data.find("third"));
EXPECT_EQ(std::string::npos, data.find("some"));
EXPECT_EQ(std::string::npos, data.find("another"));
}
// Makes sure the links are correct.
TEST(ViewCacheHelper, Prefix) {
TestURLRequestContext context;
ViewCacheHelper helper;
FillCache(&context);
std::string key, data;
std::string prefix("prefix:");
TestCompletionCallback cb;
int rv = helper.GetContentsHTML(&context, prefix, &data, cb.callback());
EXPECT_EQ(OK, cb.GetResult(rv));
EXPECT_EQ(0U, data.find("<html>"));
EXPECT_NE(std::string::npos, data.find("</html>"));
EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:first\">"));
EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:second\">"));
EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:third\">"));
}
TEST(ViewCacheHelper, TruncatedFlag) {
TestURLRequestContext context;
ViewCacheHelper helper;
TestCompletionCallback cb;
disk_cache::Backend* cache;
int rv =
context.http_transaction_factory()->GetCache()->GetBackend(
&cache, cb.callback());
ASSERT_EQ(OK, cb.GetResult(rv));
std::string key("the key");
disk_cache::Entry* entry;
rv = cache->CreateEntry(key, &entry, cb.callback());
ASSERT_EQ(OK, cb.GetResult(rv));
// RESPONSE_INFO_TRUNCATED defined on response_info.cc
int flags = 1 << 12;
WriteHeaders(entry, flags, "something");
entry->Close();
std::string data;
TestCompletionCallback cb1;
rv = helper.GetEntryInfoHTML(key, &context, &data, cb1.callback());
EXPECT_EQ(OK, cb1.GetResult(rv));
EXPECT_NE(std::string::npos, data.find("RESPONSE_INFO_TRUNCATED"));
}
} // namespace net
|