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
347
348
349
350
351
352
353
354
355
356
|
// Copyright (c) 2011 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.
//
// NOTE: These tests are run as part of "unit_tests" (in chrome/test/unit)
// rather than as part of test_shell_tests because they rely on being able
// to instantiate a MessageLoop of type TYPE_IO. test_shell_tests uses
// TYPE_UI, which URLRequest doesn't allow.
//
#include <string>
#include <vector>
#include "base/file_util_proxy.h"
#include "base/memory/scoped_temp_dir.h"
#include "base/message_loop.h"
#include "googleurl/src/gurl.h"
#include "net/base/io_buffer.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_job.h"
#include "net/url_request/url_request_status.h"
#include "testing/platform_test.h"
#include "webkit/fileapi/file_system_callback_dispatcher.h"
#include "webkit/fileapi/file_system_context.h"
#include "webkit/fileapi/file_system_operation.h"
#include "webkit/fileapi/file_system_operation_context.h"
#include "webkit/fileapi/file_system_path_manager.h"
#include "webkit/fileapi/file_system_usage_cache.h"
#include "webkit/fileapi/file_writer_delegate.h"
#include "webkit/fileapi/quota_file_util.h"
#include "webkit/fileapi/sandbox_mount_point_provider.h"
#include "webkit/fileapi/file_system_test_helper.h"
namespace fileapi {
namespace {
class Result {
public:
Result()
: status_(base::PLATFORM_FILE_OK),
bytes_written_(0),
complete_(false) {}
void set_failure_status(base::PlatformFileError status) {
EXPECT_FALSE(complete_);
EXPECT_EQ(status_, base::PLATFORM_FILE_OK);
EXPECT_NE(status, base::PLATFORM_FILE_OK);
complete_ = true;
status_ = status;
}
base::PlatformFileError status() const { return status_; }
void add_bytes_written(int64 bytes, bool complete) {
bytes_written_ += bytes;
EXPECT_FALSE(complete_);
complete_ = complete;
}
int64 bytes_written() const { return bytes_written_; }
bool complete() const { return complete_; }
private:
// For post-operation status.
base::PlatformFileError status_;
int64 bytes_written_;
bool complete_;
};
} // namespace (anonymous)
class FileWriterDelegateTest : public PlatformTest {
public:
FileWriterDelegateTest()
: loop_(MessageLoop::TYPE_IO) {}
protected:
virtual void SetUp();
virtual void TearDown();
int64 GetCachedUsage() {
return FileSystemUsageCache::GetUsage(test_helper_.GetUsageCachePath());
}
FileSystemOperation* CreateNewOperation(Result* result, int64 quota);
static net::URLRequest::ProtocolFactory Factory;
scoped_ptr<FileWriterDelegate> file_writer_delegate_;
scoped_ptr<net::URLRequest> request_;
scoped_ptr<Result> result_;
FileSystemTestOriginHelper test_helper_;
MessageLoop loop_;
ScopedTempDir dir_;
FilePath file_path_;
PlatformFile file_;
static const char* content_;
};
const char* FileWriterDelegateTest::content_ = NULL;
namespace {
static std::string g_content;
class FileWriterDelegateTestJob : public net::URLRequestJob {
public:
FileWriterDelegateTestJob(net::URLRequest* request,
const std::string& content)
: net::URLRequestJob(request),
content_(content),
remaining_bytes_(content.length()),
cursor_(0) {
}
void Start() {
MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
this, &FileWriterDelegateTestJob::NotifyHeadersComplete));
}
bool ReadRawData(net::IOBuffer* buf, int buf_size, int *bytes_read) {
if (remaining_bytes_ < buf_size)
buf_size = static_cast<int>(remaining_bytes_);
for (int i = 0; i < buf_size; ++i)
buf->data()[i] = content_[cursor_++];
remaining_bytes_ -= buf_size;
SetStatus(net::URLRequestStatus());
*bytes_read = buf_size;
return true;
}
private:
std::string content_;
int remaining_bytes_;
int cursor_;
};
class MockDispatcher : public FileSystemCallbackDispatcher {
public:
explicit MockDispatcher(Result* result) : result_(result) {}
virtual void DidFail(base::PlatformFileError status) {
result_->set_failure_status(status);
MessageLoop::current()->Quit();
}
virtual void DidSucceed() {
ADD_FAILURE();
}
virtual void DidReadMetadata(
const base::PlatformFileInfo& info,
const FilePath& platform_path) {
ADD_FAILURE();
}
virtual void DidReadDirectory(
const std::vector<base::FileUtilProxy::Entry>& entries,
bool /* has_more */) {
ADD_FAILURE();
}
virtual void DidOpenFileSystem(const std::string&, const GURL&) {
ADD_FAILURE();
}
virtual void DidWrite(int64 bytes, bool complete) {
result_->add_bytes_written(bytes, complete);
if (complete)
MessageLoop::current()->Quit();
}
private:
Result* result_;
};
} // namespace (anonymous)
// static
net::URLRequestJob* FileWriterDelegateTest::Factory(
net::URLRequest* request,
const std::string& scheme) {
return new FileWriterDelegateTestJob(
request, FileWriterDelegateTest::content_);
}
void FileWriterDelegateTest::SetUp() {
ASSERT_TRUE(dir_.CreateUniqueTempDir());
FilePath base_dir = dir_.path().AppendASCII("filesystem");
test_helper_.SetUp(base_dir, QuotaFileUtil::GetInstance());
FilePath filesystem_dir = test_helper_.GetOriginRootPath();
ASSERT_TRUE(file_util::CreateTemporaryFileInDir(
filesystem_dir, &file_path_));
bool created;
base::PlatformFileError error_code;
file_ = base::CreatePlatformFile(
file_path_,
base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE |
base::PLATFORM_FILE_ASYNC,
&created, &error_code);
ASSERT_EQ(base::PLATFORM_FILE_OK, error_code);
result_.reset(new Result());
net::URLRequest::RegisterProtocolFactory("blob", &Factory);
}
void FileWriterDelegateTest::TearDown() {
net::URLRequest::RegisterProtocolFactory("blob", NULL);
result_.reset();
base::ClosePlatformFile(file_);
test_helper_.TearDown();
}
FileSystemOperation* FileWriterDelegateTest::CreateNewOperation(
Result* result, int64 quota) {
FileSystemOperation* operation = test_helper_.NewOperation(
new MockDispatcher(result));
FileSystemOperationContext* context =
operation->file_system_operation_context();
context->set_allowed_bytes_growth(quota);
return operation;
}
TEST_F(FileWriterDelegateTest, WriteSuccessWithoutQuotaLimit) {
GURL blob_url("blob:nolimit");
content_ = "The quick brown fox jumps over the lazy dog.\n";
file_writer_delegate_.reset(new FileWriterDelegate(
CreateNewOperation(result_.get(), QuotaFileUtil::kNoLimit),
0, base::MessageLoopProxy::CreateForCurrentThread()));
request_.reset(new net::URLRequest(blob_url, file_writer_delegate_.get()));
ASSERT_EQ(0, GetCachedUsage());
file_writer_delegate_->Start(file_, request_.get());
MessageLoop::current()->Run();
ASSERT_EQ(45, GetCachedUsage());
EXPECT_EQ(45, result_->bytes_written());
EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status());
EXPECT_TRUE(result_->complete());
file_writer_delegate_.reset();
}
TEST_F(FileWriterDelegateTest, WriteSuccessWithJustQuota) {
GURL blob_url("blob:just");
content_ = "The quick brown fox jumps over the lazy dog.\n";
file_writer_delegate_.reset(new FileWriterDelegate(
CreateNewOperation(result_.get(), 45),
0, base::MessageLoopProxy::CreateForCurrentThread()));
request_.reset(new net::URLRequest(blob_url, file_writer_delegate_.get()));
ASSERT_EQ(0, GetCachedUsage());
file_writer_delegate_->Start(file_, request_.get());
MessageLoop::current()->Run();
ASSERT_EQ(45, GetCachedUsage());
file_writer_delegate_.reset();
EXPECT_EQ(45, result_->bytes_written());
EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status());
EXPECT_TRUE(result_->complete());
}
TEST_F(FileWriterDelegateTest, WriteFailureByQuota) {
GURL blob_url("blob:failure");
content_ = "The quick brown fox jumps over the lazy dog.\n";
file_writer_delegate_.reset(new FileWriterDelegate(
CreateNewOperation(result_.get(), 44),
0, base::MessageLoopProxy::CreateForCurrentThread()));
request_.reset(new net::URLRequest(blob_url, file_writer_delegate_.get()));
ASSERT_EQ(0, GetCachedUsage());
file_writer_delegate_->Start(file_, request_.get());
MessageLoop::current()->Run();
ASSERT_EQ(44, GetCachedUsage());
file_writer_delegate_.reset();
EXPECT_EQ(44, result_->bytes_written());
EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, result_->status());
EXPECT_TRUE(result_->complete());
}
TEST_F(FileWriterDelegateTest, WriteZeroBytesSuccessfullyWithZeroQuota) {
GURL blob_url("blob:zero");
content_ = "";
file_writer_delegate_.reset(new FileWriterDelegate(
CreateNewOperation(result_.get(), 0),
0, base::MessageLoopProxy::CreateForCurrentThread()));
request_.reset(new net::URLRequest(blob_url, file_writer_delegate_.get()));
ASSERT_EQ(0, GetCachedUsage());
file_writer_delegate_->Start(file_, request_.get());
MessageLoop::current()->Run();
ASSERT_EQ(0, GetCachedUsage());
file_writer_delegate_.reset();
EXPECT_EQ(0, result_->bytes_written());
EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status());
EXPECT_TRUE(result_->complete());
}
TEST_F(FileWriterDelegateTest, WriteSuccessWithoutQuotaLimitConcurrent) {
scoped_ptr<FileWriterDelegate> file_writer_delegate2;
scoped_ptr<net::URLRequest> request2;
scoped_ptr<Result> result2;
PlatformFile file2;
bool created;
base::PlatformFileError error_code;
file2 = base::CreatePlatformFile(
file_path_,
base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE |
base::PLATFORM_FILE_ASYNC,
&created, &error_code);
ASSERT_EQ(base::PLATFORM_FILE_OK, error_code);
result2.reset(new Result());
GURL blob_url("blob:nolimitconcurrent");
GURL blob_url2("blob:nolimitconcurrent2");
content_ = "The quick brown fox jumps over the lazy dog.\n";
file_writer_delegate_.reset(new FileWriterDelegate(
CreateNewOperation(result_.get(), QuotaFileUtil::kNoLimit),
0, base::MessageLoopProxy::CreateForCurrentThread()));
file_writer_delegate2.reset(new FileWriterDelegate(
CreateNewOperation(result2.get(), QuotaFileUtil::kNoLimit),
0, base::MessageLoopProxy::CreateForCurrentThread()));
request_.reset(new net::URLRequest(blob_url, file_writer_delegate_.get()));
request2.reset(new net::URLRequest(blob_url2, file_writer_delegate2.get()));
ASSERT_EQ(0, GetCachedUsage());
file_writer_delegate_->Start(file_, request_.get());
file_writer_delegate2->Start(file2, request2.get());
MessageLoop::current()->Run();
if (!result_->complete() || !result2->complete())
MessageLoop::current()->Run();
ASSERT_EQ(90, GetCachedUsage());
file_writer_delegate_.reset();
EXPECT_EQ(45, result_->bytes_written());
EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status());
EXPECT_TRUE(result_->complete());
EXPECT_EQ(45, result2->bytes_written());
EXPECT_EQ(base::PLATFORM_FILE_OK, result2->status());
EXPECT_TRUE(result2->complete());
}
} // namespace fileapi
|