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
|
// 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 <string>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/message_loop.h"
#include "base/pickle.h"
#include "net/base/completion_callback.h"
#include "net/base/io_buffer.h"
#include "net/http/http_response_headers.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "webkit/appcache/appcache_response.h"
#include "webkit/appcache/appcache_service.h"
#include "webkit/appcache/mock_appcache_storage.h"
namespace appcache {
namespace {
const int64 kMockGroupId = 1;
const int64 kMockCacheId = 1;
const int64 kMockResponseId = 1;
const int64 kMissingCacheId = 5;
const int64 kMissingResponseId = 5;
const char kMockHeaders[] =
"HTTP/1.0 200 OK\0Content-Length: 5\0\0";
const char kMockBody[] = "Hello";
const int kMockBodySize = 5;
class MockResponseReader : public AppCacheResponseReader {
public:
MockResponseReader(int64 response_id,
net::HttpResponseInfo* info, int info_size,
const char* data, int data_size)
: AppCacheResponseReader(response_id, 0, NULL),
info_(info), info_size_(info_size),
data_(data), data_size_(data_size) {
}
virtual void ReadInfo(HttpResponseInfoIOBuffer* info_buf,
const net::CompletionCallback& callback) OVERRIDE {
info_buffer_ = info_buf;
callback_ = callback; // Cleared on completion.
int rv = info_.get() ? info_size_ : net::ERR_FAILED;
info_buffer_->http_info.reset(info_.release());
info_buffer_->response_data_size = data_size_;
ScheduleUserCallback(rv);
}
virtual void ReadData(net::IOBuffer* buf, int buf_len,
const net::CompletionCallback& callback) OVERRIDE {
buffer_ = buf;
buffer_len_ = buf_len;
callback_ = callback; // Cleared on completion.
if (!data_) {
ScheduleUserCallback(net::ERR_CACHE_READ_FAILURE);
return;
}
DCHECK(buf_len >= data_size_);
memcpy(buf->data(), data_, data_size_);
ScheduleUserCallback(data_size_);
data_size_ = 0;
}
private:
void ScheduleUserCallback(int result) {
MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&MockResponseReader::InvokeUserCompletionCallback,
weak_factory_.GetWeakPtr(), result));
}
scoped_ptr<net::HttpResponseInfo> info_;
int info_size_;
const char* data_;
int data_size_;
};
} // namespace
class AppCacheServiceTest : public testing::Test {
public:
AppCacheServiceTest()
: kOrigin("http://hello/"),
kManifestUrl(kOrigin.Resolve("manifest")),
service_(new AppCacheService(NULL)),
delete_result_(net::OK), delete_completion_count_(0),
ALLOW_THIS_IN_INITIALIZER_LIST(deletion_callback_(
base::Bind(&AppCacheServiceTest::OnDeleteAppCachesComplete,
base::Unretained(this)))) {
// Setup to use mock storage.
service_->storage_.reset(new MockAppCacheStorage(service_.get()));
}
void OnDeleteAppCachesComplete(int result) {
delete_result_ = result;
++delete_completion_count_;
}
MockAppCacheStorage* mock_storage() {
return static_cast<MockAppCacheStorage*>(service_->storage());
}
void ResetStorage() {
service_->storage_.reset(new MockAppCacheStorage(service_.get()));
}
bool IsGroupStored(const GURL& manifest_url) {
return mock_storage()->IsGroupForManifestStored(manifest_url);
}
int CountPendingHelpers() {
return service_->pending_helpers_.size();
}
void SetupMockGroup() {
scoped_ptr<net::HttpResponseInfo> info(MakeMockResponseInfo());
const int kMockInfoSize = GetResponseInfoSize(info.get());
// Create a mock group, cache, and entry and stuff them into mock storage.
scoped_refptr<AppCacheGroup> group(
new AppCacheGroup(service_.get(), kManifestUrl, kMockGroupId));
scoped_refptr<AppCache> cache(new AppCache(service_.get(), kMockCacheId));
cache->AddEntry(
kManifestUrl,
AppCacheEntry(AppCacheEntry::MANIFEST, kMockResponseId,
kMockInfoSize + kMockBodySize));
cache->set_complete(true);
group->AddCache(cache);
mock_storage()->AddStoredGroup(group);
mock_storage()->AddStoredCache(cache);
}
void SetupMockReader(
bool valid_info, bool valid_data, bool valid_size) {
net::HttpResponseInfo* info = valid_info ? MakeMockResponseInfo() : NULL;
int info_size = info ? GetResponseInfoSize(info) : 0;
const char* data = valid_data ? kMockBody : NULL;
int data_size = valid_size ? kMockBodySize : 3;
mock_storage()->SimulateResponseReader(
new MockResponseReader(kMockResponseId, info, info_size,
data, data_size));
}
net::HttpResponseInfo* MakeMockResponseInfo() {
net::HttpResponseInfo* info = new net::HttpResponseInfo;
info->request_time = base::Time::Now();
info->response_time = base::Time::Now();
info->was_cached = false;
info->headers = new net::HttpResponseHeaders(
std::string(kMockHeaders, arraysize(kMockHeaders)));
return info;
}
int GetResponseInfoSize(const net::HttpResponseInfo* info) {
Pickle pickle;
return PickleResponseInfo(&pickle, info);
}
int PickleResponseInfo(Pickle* pickle, const net::HttpResponseInfo* info) {
const bool kSkipTransientHeaders = true;
const bool kTruncated = false;
info->Persist(pickle, kSkipTransientHeaders, kTruncated);
return pickle->size();
}
const GURL kOrigin;
const GURL kManifestUrl;
scoped_ptr<AppCacheService> service_;
int delete_result_;
int delete_completion_count_;
net::CompletionCallback deletion_callback_;
private:
MessageLoop message_loop_;
};
TEST_F(AppCacheServiceTest, DeleteAppCachesForOrigin) {
// Without giving mock storage simiulated info, should fail.
service_->DeleteAppCachesForOrigin(kOrigin, deletion_callback_);
EXPECT_EQ(0, delete_completion_count_);
MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(1, delete_completion_count_);
EXPECT_EQ(net::ERR_FAILED, delete_result_);
delete_completion_count_ = 0;
// Should succeed given an empty info collection.
mock_storage()->SimulateGetAllInfo(new AppCacheInfoCollection);
service_->DeleteAppCachesForOrigin(kOrigin, deletion_callback_);
EXPECT_EQ(0, delete_completion_count_);
MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(1, delete_completion_count_);
EXPECT_EQ(net::OK, delete_result_);
delete_completion_count_ = 0;
scoped_refptr<AppCacheInfoCollection> info(new AppCacheInfoCollection);
// Should succeed given a non-empty info collection.
AppCacheInfo mock_manifest_1;
AppCacheInfo mock_manifest_2;
AppCacheInfo mock_manifest_3;
mock_manifest_1.manifest_url = kOrigin.Resolve("manifest1");
mock_manifest_2.manifest_url = kOrigin.Resolve("manifest2");
mock_manifest_3.manifest_url = kOrigin.Resolve("manifest3");
AppCacheInfoVector info_vector;
info_vector.push_back(mock_manifest_1);
info_vector.push_back(mock_manifest_2);
info_vector.push_back(mock_manifest_3);
info->infos_by_origin[kOrigin] = info_vector;
mock_storage()->SimulateGetAllInfo(info);
service_->DeleteAppCachesForOrigin(kOrigin, deletion_callback_);
EXPECT_EQ(0, delete_completion_count_);
MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(1, delete_completion_count_);
EXPECT_EQ(net::OK, delete_result_);
delete_completion_count_ = 0;
// Should fail if storage fails to delete.
info->infos_by_origin[kOrigin] = info_vector;
mock_storage()->SimulateGetAllInfo(info);
mock_storage()->SimulateMakeGroupObsoleteFailure();
service_->DeleteAppCachesForOrigin(kOrigin, deletion_callback_);
EXPECT_EQ(0, delete_completion_count_);
MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(1, delete_completion_count_);
EXPECT_EQ(net::ERR_FAILED, delete_result_);
delete_completion_count_ = 0;
// Should complete with abort error if the service is deleted
// prior to a delete completion.
service_->DeleteAppCachesForOrigin(kOrigin, deletion_callback_);
EXPECT_EQ(0, delete_completion_count_);
service_.reset(); // kill it
EXPECT_EQ(1, delete_completion_count_);
EXPECT_EQ(net::ERR_ABORTED, delete_result_);
delete_completion_count_ = 0;
// Let any tasks lingering from the sudden deletion run and verify
// no other completion calls occur.
MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(0, delete_completion_count_);
}
TEST_F(AppCacheServiceTest, CheckAppCacheResponse) {
// Check a non-existing manifest.
EXPECT_FALSE(IsGroupStored(kManifestUrl));
service_->CheckAppCacheResponse(kManifestUrl, 1, 1);
MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(0, CountPendingHelpers());
EXPECT_FALSE(IsGroupStored(kManifestUrl));
ResetStorage();
// Check a response that looks good.
// Nothing should be deleted.
SetupMockGroup();
EXPECT_TRUE(IsGroupStored(kManifestUrl));
SetupMockReader(true, true, true);
service_->CheckAppCacheResponse(kManifestUrl, kMockCacheId, kMockResponseId);
MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(0, CountPendingHelpers());
EXPECT_TRUE(IsGroupStored(kManifestUrl));
ResetStorage();
// Check a response for which there is no cache entry.
// The group should get deleted.
SetupMockGroup();
service_->CheckAppCacheResponse(kManifestUrl, kMockCacheId,
kMissingResponseId);
MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(0, CountPendingHelpers());
EXPECT_FALSE(IsGroupStored(kManifestUrl));
ResetStorage();
// Check a response for which there is no manifest entry in a newer version
// of the cache. Nothing should get deleted in this case.
SetupMockGroup();
service_->CheckAppCacheResponse(kManifestUrl, kMissingCacheId,
kMissingResponseId);
MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(0, CountPendingHelpers());
EXPECT_TRUE(IsGroupStored(kManifestUrl));
ResetStorage();
// Check a response with bad headers.
SetupMockGroup();
service_->CheckAppCacheResponse(kManifestUrl, kMockCacheId, kMockResponseId);
SetupMockReader(false, true, true);
MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(0, CountPendingHelpers());
EXPECT_FALSE(IsGroupStored(kManifestUrl));
ResetStorage();
// Check a response with bad data.
SetupMockGroup();
service_->CheckAppCacheResponse(kManifestUrl, kMockCacheId, kMockResponseId);
SetupMockReader(true, false, true);
MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(0, CountPendingHelpers());
EXPECT_FALSE(IsGroupStored(kManifestUrl));
ResetStorage();
// Check a response with truncated data.
SetupMockGroup();
service_->CheckAppCacheResponse(kManifestUrl, kMockCacheId, kMockResponseId);
SetupMockReader(true, true, false);
MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(0, CountPendingHelpers());
EXPECT_FALSE(IsGroupStored(kManifestUrl));
ResetStorage();
service_.reset(); // Clean up.
MessageLoop::current()->RunUntilIdle();
}
} // namespace appcache
|