Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(870)

Side by Side Diff: webkit/plugins/ppapi/quota_file_io_unittest.cc

Issue 20165002: Move webkit/plugins/ppapi to content/renderer/pepper. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: more more clang fun Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « webkit/plugins/ppapi/quota_file_io.cc ('k') | webkit/plugins/ppapi/resource_creation_impl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <deque>
6 #include <limits>
7 #include <string>
8
9 #include "base/basictypes.h"
10 #include "base/bind.h"
11 #include "base/file_util.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/platform_file.h"
16 #include "webkit/plugins/ppapi/mock_plugin_delegate.h"
17 #include "webkit/plugins/ppapi/ppapi_plugin_instance_impl.h"
18 #include "webkit/plugins/ppapi/ppapi_unittest.h"
19 #include "webkit/plugins/ppapi/quota_file_io.h"
20
21 using base::MessageLoopProxy;
22 using base::PlatformFile;
23 using base::PlatformFileError;
24
25 namespace webkit {
26 namespace ppapi {
27
28 namespace {
29 class QuotaMockPluginDelegate : public MockPluginDelegate {
30 public:
31 typedef PluginDelegate::AvailableSpaceCallback Callback;
32
33 QuotaMockPluginDelegate()
34 : available_space_(0),
35 will_update_count_(0),
36 file_thread_(MessageLoopProxy::current()),
37 weak_factory_(this) {
38 }
39 virtual ~QuotaMockPluginDelegate() {}
40
41 virtual scoped_refptr<MessageLoopProxy> GetFileThreadMessageLoopProxy() {
42 return file_thread_;
43 }
44
45 virtual void QueryAvailableSpace(
46 const GURL& origin,
47 quota::StorageType type,
48 const Callback& callback) OVERRIDE {
49 DCHECK_EQ(false, callback.is_null());
50 MessageLoopProxy::current()->PostTask(
51 FROM_HERE, base::Bind(
52 &QuotaMockPluginDelegate::RunAvailableSpaceCallback,
53 weak_factory_.GetWeakPtr(), callback));
54 }
55
56 virtual void WillUpdateFile(const GURL& file_path) OVERRIDE {
57 file_path_ = file_path;
58 ++will_update_count_;
59 }
60
61 virtual void DidUpdateFile(const GURL& file_path, int64_t delta) OVERRIDE {
62 ASSERT_EQ(file_path_, file_path);
63 ASSERT_GT(will_update_count_, 0);
64 --will_update_count_;
65 available_space_ -= delta;
66 }
67
68 void set_available_space(int64 available) { available_space_ = available; }
69 int64_t available_space() const { return available_space_; }
70
71 private:
72 void RunAvailableSpaceCallback(const Callback& callback) {
73 callback.Run(available_space_);
74 }
75
76 int64_t available_space_;
77 int will_update_count_;
78 GURL file_path_;
79 scoped_refptr<MessageLoopProxy> file_thread_;
80 base::WeakPtrFactory<QuotaMockPluginDelegate> weak_factory_;
81 };
82 } // namespace
83
84 class QuotaFileIOTest : public PpapiUnittest {
85 public:
86 QuotaFileIOTest()
87 : weak_factory_(this) {}
88
89 virtual void SetUp() OVERRIDE {
90 PpapiUnittest::SetUp();
91 ASSERT_TRUE(dir_.CreateUniqueTempDir());
92 base::FilePath path;
93 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(dir_.path(), &path));
94 int file_flags = base::PLATFORM_FILE_OPEN |
95 base::PLATFORM_FILE_READ |
96 base::PLATFORM_FILE_WRITE |
97 base::PLATFORM_FILE_WRITE_ATTRIBUTES;
98 bool created = false;
99 file_ = base::kInvalidPlatformFileValue;
100 PlatformFileError error = base::PLATFORM_FILE_OK;
101 file_ = base::CreatePlatformFile(path, file_flags, &created, &error);
102 ASSERT_EQ(base::PLATFORM_FILE_OK, error);
103 ASSERT_NE(base::kInvalidPlatformFileValue, file_);
104 ASSERT_FALSE(created);
105 quota_file_io_.reset(new QuotaFileIO(
106 instance()->pp_instance(), file_, GURL(),
107 PP_FILESYSTEMTYPE_LOCALTEMPORARY));
108 }
109
110 virtual void TearDown() OVERRIDE {
111 quota_file_io_.reset();
112 if (file_ != base::kInvalidPlatformFileValue)
113 base::ClosePlatformFile(file_);
114 PpapiUnittest::TearDown();
115 }
116
117 protected:
118 virtual MockPluginDelegate* NewPluginDelegate() OVERRIDE {
119 return static_cast<MockPluginDelegate*>(new QuotaMockPluginDelegate);
120 }
121
122 void WriteTestBody(bool will_operation) {
123 // Attempt to write zero bytes.
124 EXPECT_FALSE(quota_file_io_->Write(
125 0, "data", 0,
126 base::Bind(&QuotaFileIOTest::DidWrite, weak_factory_.GetWeakPtr())));
127 // Attempt to write negative number of bytes.
128 EXPECT_FALSE(quota_file_io_->Write(
129 0, "data", std::numeric_limits<int32_t>::min(),
130 base::Bind(&QuotaFileIOTest::DidWrite, weak_factory_.GetWeakPtr())));
131
132 quota_plugin_delegate()->set_available_space(100);
133 std::string read_buffer;
134
135 // Write 8 bytes at offset 0 (-> length=8).
136 std::string data("12345678");
137 Write(0, data, will_operation);
138 base::MessageLoop::current()->RunUntilIdle();
139 ASSERT_EQ(1U, num_results());
140 EXPECT_EQ(static_cast<int>(data.size()), bytes_written().front());
141 EXPECT_EQ(base::PLATFORM_FILE_OK, status().front());
142 EXPECT_EQ(100 - 8, quota_plugin_delegate()->available_space());
143 reset_results();
144
145 if (will_operation) {
146 // WillWrite doesn't actually write.
147 EXPECT_EQ(0, GetPlatformFileSize());
148 // Adjust the actual file size to 'fake' write to proceed testing.
149 SetPlatformFileSize(8);
150 } else {
151 EXPECT_EQ(8, GetPlatformFileSize());
152 ReadPlatformFile(&read_buffer);
153 EXPECT_EQ(data, read_buffer);
154 }
155
156 // Write 5 bytes at offset 5 (-> length=10).
157 data = "55555";
158 Write(5, data, will_operation);
159 base::MessageLoop::current()->RunUntilIdle();
160 ASSERT_EQ(1U, num_results());
161 EXPECT_EQ(static_cast<int>(data.size()), bytes_written().front());
162 EXPECT_EQ(base::PLATFORM_FILE_OK, status().front());
163 EXPECT_EQ(100 - 10, quota_plugin_delegate()->available_space());
164 reset_results();
165
166 if (will_operation) {
167 EXPECT_EQ(8, GetPlatformFileSize());
168 SetPlatformFileSize(10);
169 } else {
170 EXPECT_EQ(10, GetPlatformFileSize());
171 ReadPlatformFile(&read_buffer);
172 EXPECT_EQ("1234555555", read_buffer);
173 }
174
175 // Write 7 bytes at offset 8 (-> length=15).
176 data = "9012345";
177 Write(8, data, will_operation);
178 base::MessageLoop::current()->RunUntilIdle();
179 ASSERT_EQ(1U, num_results());
180 EXPECT_EQ(static_cast<int>(data.size()), bytes_written().front());
181 EXPECT_EQ(base::PLATFORM_FILE_OK, status().front());
182 EXPECT_EQ(100 - 15, quota_plugin_delegate()->available_space());
183 reset_results();
184
185 if (will_operation) {
186 EXPECT_EQ(10, GetPlatformFileSize());
187 SetPlatformFileSize(15);
188 } else {
189 EXPECT_EQ(15, GetPlatformFileSize());
190 ReadPlatformFile(&read_buffer);
191 EXPECT_EQ("123455559012345", read_buffer);
192 }
193
194 // Write 2 bytes at offset 2 (-> length=15).
195 data = "33";
196 Write(2, data, will_operation);
197 base::MessageLoop::current()->RunUntilIdle();
198 ASSERT_EQ(1U, num_results());
199 EXPECT_EQ(static_cast<int>(data.size()), bytes_written().front());
200 EXPECT_EQ(base::PLATFORM_FILE_OK, status().front());
201 EXPECT_EQ(100 - 15, quota_plugin_delegate()->available_space());
202 reset_results();
203
204 if (will_operation) {
205 EXPECT_EQ(15, GetPlatformFileSize());
206 } else {
207 EXPECT_EQ(15, GetPlatformFileSize());
208 ReadPlatformFile(&read_buffer);
209 EXPECT_EQ("123355559012345", read_buffer);
210 }
211
212 // Write 4 bytes at offset 20 (-> length=24).
213 data = "XXXX";
214 Write(20, data, will_operation);
215 base::MessageLoop::current()->RunUntilIdle();
216 ASSERT_EQ(1U, num_results());
217 EXPECT_EQ(static_cast<int>(data.size()), bytes_written().front());
218 EXPECT_EQ(base::PLATFORM_FILE_OK, status().front());
219 EXPECT_EQ(100 - 24, quota_plugin_delegate()->available_space());
220 reset_results();
221
222 if (will_operation) {
223 EXPECT_EQ(15, GetPlatformFileSize());
224 SetPlatformFileSize(24);
225 } else {
226 EXPECT_EQ(24, GetPlatformFileSize());
227 ReadPlatformFile(&read_buffer);
228 EXPECT_EQ(std::string("123355559012345\0\0\0\0\0XXXX", 24), read_buffer);
229 }
230
231 quota_plugin_delegate()->set_available_space(5);
232
233 // Quota error case. Write 7 bytes at offset 23 (-> length is unchanged)
234 data = "ABCDEFG";
235 Write(23, data, will_operation);
236 base::MessageLoop::current()->RunUntilIdle();
237 ASSERT_EQ(1U, num_results());
238 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, status().front());
239 EXPECT_EQ(5, quota_plugin_delegate()->available_space());
240 reset_results();
241
242 // Overlapping write. Write 6 bytes at offset 2 (-> length is unchanged)
243 data = "ABCDEF";
244 Write(2, data, will_operation);
245 base::MessageLoop::current()->RunUntilIdle();
246 ASSERT_EQ(1U, num_results());
247 EXPECT_EQ(static_cast<int>(data.size()), bytes_written().front());
248 EXPECT_EQ(base::PLATFORM_FILE_OK, status().front());
249 EXPECT_EQ(5, quota_plugin_delegate()->available_space());
250 reset_results();
251
252 // Overlapping + extending the file size, but within the quota.
253 // Write 6 bytes at offset 23 (-> length=29).
254 Write(23, data, will_operation);
255 base::MessageLoop::current()->RunUntilIdle();
256 ASSERT_EQ(1U, num_results());
257 EXPECT_EQ(static_cast<int>(data.size()), bytes_written().front());
258 EXPECT_EQ(base::PLATFORM_FILE_OK, status().front());
259 EXPECT_EQ(0, quota_plugin_delegate()->available_space());
260 reset_results();
261
262 if (!will_operation) {
263 EXPECT_EQ(29, GetPlatformFileSize());
264 ReadPlatformFile(&read_buffer);
265 EXPECT_EQ(std::string("12ABCDEF9012345\0\0\0\0\0XXXABCDEF", 29),
266 read_buffer);
267 }
268 }
269
270 void SetLengthTestBody(bool will_operation) {
271 quota_plugin_delegate()->set_available_space(100);
272
273 SetLength(0, will_operation);
274 base::MessageLoop::current()->RunUntilIdle();
275 ASSERT_EQ(1U, num_results());
276 EXPECT_EQ(base::PLATFORM_FILE_OK, status().front());
277 EXPECT_EQ(0, GetPlatformFileSize());
278 EXPECT_EQ(100, quota_plugin_delegate()->available_space());
279 reset_results();
280
281 SetLength(8, will_operation);
282 base::MessageLoop::current()->RunUntilIdle();
283 ASSERT_EQ(1U, num_results());
284 EXPECT_EQ(base::PLATFORM_FILE_OK, status().front());
285 EXPECT_EQ(100 - 8, quota_plugin_delegate()->available_space());
286 reset_results();
287
288 if (will_operation) {
289 EXPECT_EQ(0, GetPlatformFileSize());
290 SetPlatformFileSize(8);
291 } else {
292 EXPECT_EQ(8, GetPlatformFileSize());
293 }
294
295 SetLength(16, will_operation);
296 base::MessageLoop::current()->RunUntilIdle();
297 ASSERT_EQ(1U, num_results());
298 EXPECT_EQ(base::PLATFORM_FILE_OK, status().front());
299 EXPECT_EQ(100 - 16, quota_plugin_delegate()->available_space());
300 reset_results();
301
302 if (will_operation) {
303 EXPECT_EQ(8, GetPlatformFileSize());
304 SetPlatformFileSize(16);
305 } else {
306 EXPECT_EQ(16, GetPlatformFileSize());
307 }
308
309 SetLength(4, will_operation);
310 base::MessageLoop::current()->RunUntilIdle();
311 ASSERT_EQ(1U, num_results());
312 EXPECT_EQ(base::PLATFORM_FILE_OK, status().front());
313 EXPECT_EQ(100 - 4, quota_plugin_delegate()->available_space());
314 reset_results();
315
316 if (will_operation) {
317 EXPECT_EQ(16, GetPlatformFileSize());
318 SetPlatformFileSize(4);
319 } else {
320 EXPECT_EQ(4, GetPlatformFileSize());
321 }
322
323 SetLength(0, will_operation);
324 base::MessageLoop::current()->RunUntilIdle();
325 ASSERT_EQ(1U, num_results());
326 EXPECT_EQ(base::PLATFORM_FILE_OK, status().front());
327 EXPECT_EQ(100, quota_plugin_delegate()->available_space());
328 reset_results();
329
330 if (will_operation) {
331 EXPECT_EQ(4, GetPlatformFileSize());
332 SetPlatformFileSize(0);
333 } else {
334 EXPECT_EQ(0, GetPlatformFileSize());
335 }
336
337 quota_plugin_delegate()->set_available_space(5);
338
339 // Quota error case.
340 SetLength(7, will_operation);
341 base::MessageLoop::current()->RunUntilIdle();
342 ASSERT_EQ(1U, num_results());
343 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, status().front());
344 EXPECT_EQ(5, quota_plugin_delegate()->available_space());
345 reset_results();
346 }
347
348 QuotaMockPluginDelegate* quota_plugin_delegate() {
349 return static_cast<QuotaMockPluginDelegate*>(delegate());
350 }
351
352 void Write(int64_t offset, const std::string& data, bool will_operation) {
353 if (will_operation) {
354 ASSERT_TRUE(quota_file_io_->WillWrite(
355 offset, data.size(),
356 base::Bind(&QuotaFileIOTest::DidWrite, weak_factory_.GetWeakPtr())));
357 } else {
358 ASSERT_TRUE(quota_file_io_->Write(
359 offset, data.c_str(), data.size(),
360 base::Bind(&QuotaFileIOTest::DidWrite, weak_factory_.GetWeakPtr())));
361 }
362 }
363
364 void SetLength(int64_t length, bool will_operation) {
365 if (will_operation) {
366 ASSERT_TRUE(quota_file_io_->WillSetLength(
367 length,
368 base::Bind(&QuotaFileIOTest::DidSetLength,
369 weak_factory_.GetWeakPtr())));
370 } else {
371 ASSERT_TRUE(quota_file_io_->SetLength(
372 length,
373 base::Bind(&QuotaFileIOTest::DidSetLength,
374 weak_factory_.GetWeakPtr())));
375 }
376 }
377
378 void DidWrite(PlatformFileError status, int bytes_written) {
379 status_.push_back(status);
380 bytes_written_.push_back(bytes_written);
381 }
382
383 void DidSetLength(PlatformFileError status) {
384 status_.push_back(status);
385 }
386
387 size_t num_results() const { return status_.size(); }
388 const std::deque<int>& bytes_written() const { return bytes_written_; }
389 const std::deque<PlatformFileError>& status() const { return status_; }
390
391 void reset_results() {
392 bytes_written_.clear();
393 status_.clear();
394 }
395
396 void pop_result() {
397 bytes_written_.pop_front();
398 status_.pop_front();
399 }
400
401 void ReadPlatformFile(std::string* data) {
402 data->clear();
403 char buf[256];
404 int32_t read_offset = 0;
405 for (;;) {
406 int rv = base::ReadPlatformFile(file_, read_offset, buf, sizeof(buf));
407 ASSERT_GE(rv, 0);
408 if (rv == 0)
409 break;
410 read_offset += rv;
411 data->append(buf, rv);
412 }
413 }
414
415 int64_t GetPlatformFileSize() {
416 base::PlatformFileInfo info;
417 EXPECT_TRUE(base::GetPlatformFileInfo(file_, &info));
418 return info.size;
419 }
420
421 void SetPlatformFileSize(int64_t length) {
422 EXPECT_TRUE(base::TruncatePlatformFile(file_, length));
423 }
424
425 private:
426 base::ScopedTempDir dir_;
427 PlatformFile file_;
428 scoped_ptr<QuotaFileIO> quota_file_io_;
429 std::deque<int> bytes_written_;
430 std::deque<PlatformFileError> status_;
431 base::WeakPtrFactory<QuotaFileIOTest> weak_factory_;
432 };
433
434 TEST_F(QuotaFileIOTest, Write) {
435 WriteTestBody(false);
436 }
437
438 TEST_F(QuotaFileIOTest, WillWrite) {
439 WriteTestBody(true);
440 }
441
442 TEST_F(QuotaFileIOTest, SetLength) {
443 SetLengthTestBody(false);
444 }
445
446 TEST_F(QuotaFileIOTest, WillSetLength) {
447 SetLengthTestBody(true);
448 }
449
450 TEST_F(QuotaFileIOTest, ParallelWrites) {
451 quota_plugin_delegate()->set_available_space(22);
452 std::string read_buffer;
453
454 const std::string data1[] = {
455 std::string("12345678"),
456 std::string("55555"),
457 std::string("9012345"),
458 };
459 Write(0, data1[0], false);
460 Write(5, data1[1], false);
461 Write(8, data1[2], false);
462 base::MessageLoop::current()->RunUntilIdle();
463
464 ASSERT_EQ(ARRAYSIZE_UNSAFE(data1), num_results());
465 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data1); ++i) {
466 EXPECT_EQ(static_cast<int>(data1[i].size()), bytes_written().front());
467 EXPECT_EQ(base::PLATFORM_FILE_OK, status().front());
468 pop_result();
469 }
470
471 EXPECT_EQ(22 - 15, quota_plugin_delegate()->available_space());
472 EXPECT_EQ(15, GetPlatformFileSize());
473 ReadPlatformFile(&read_buffer);
474 EXPECT_EQ("123455559012345", read_buffer);
475
476 // The second write will fail for quota error.
477 const std::string data2[] = {
478 std::string("33"),
479 std::string("XXXX"),
480 };
481 Write(2, data2[0], false);
482 Write(20, data2[1], false);
483 base::MessageLoop::current()->RunUntilIdle();
484
485 ASSERT_EQ(ARRAYSIZE_UNSAFE(data2), num_results());
486 EXPECT_EQ(static_cast<int>(data2[0].size()), bytes_written().front());
487 EXPECT_EQ(base::PLATFORM_FILE_OK, status().front());
488 pop_result();
489 EXPECT_EQ(0, bytes_written().front());
490 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, status().front());
491 pop_result();
492
493 EXPECT_EQ(22 - 15, quota_plugin_delegate()->available_space());
494 EXPECT_EQ(15, GetPlatformFileSize());
495 ReadPlatformFile(&read_buffer);
496 EXPECT_EQ("123355559012345", read_buffer);
497 }
498
499 } // namespace ppapi
500 } // namespace webkit
OLDNEW
« no previous file with comments | « webkit/plugins/ppapi/quota_file_io.cc ('k') | webkit/plugins/ppapi/resource_creation_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698