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

Side by Side Diff: content/browser/fileapi/file_system_operation_impl_unittest.cc

Issue 2368913003: Populate storage_unittests target. (Closed)
Patch Set: Removed unnecessary include from storage/browser/blob/blob_storage_context_unittest.cc. Created 4 years, 2 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
OLDNEW
(Empty)
1 // Copyright 2013 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 "storage/browser/fileapi/file_system_operation_impl.h"
6
7 #include <stddef.h>
8 #include <stdint.h>
9
10 #include <memory>
11
12 #include "base/bind.h"
13 #include "base/files/file_util.h"
14 #include "base/files/scoped_temp_dir.h"
15 #include "base/logging.h"
16 #include "base/macros.h"
17 #include "base/memory/ptr_util.h"
18 #include "base/memory/weak_ptr.h"
19 #include "base/run_loop.h"
20 #include "base/strings/stringprintf.h"
21 #include "base/threading/thread_task_runner_handle.h"
22 #include "content/browser/fileapi/mock_file_change_observer.h"
23 #include "content/browser/fileapi/mock_file_update_observer.h"
24 #include "content/browser/quota/mock_quota_manager.h"
25 #include "content/browser/quota/mock_quota_manager_proxy.h"
26 #include "content/public/test/async_file_test_helper.h"
27 #include "content/public/test/sandbox_file_system_test_helper.h"
28 #include "storage/browser/blob/shareable_file_reference.h"
29 #include "storage/browser/fileapi/file_system_context.h"
30 #include "storage/browser/fileapi/file_system_file_util.h"
31 #include "storage/browser/fileapi/file_system_operation_context.h"
32 #include "storage/browser/fileapi/file_system_operation_runner.h"
33 #include "storage/browser/fileapi/sandbox_file_system_backend.h"
34 #include "storage/browser/quota/quota_manager.h"
35 #include "storage/browser/quota/quota_manager_proxy.h"
36 #include "storage/common/fileapi/file_system_util.h"
37 #include "testing/gtest/include/gtest/gtest.h"
38 #include "url/gurl.h"
39
40 using content::AsyncFileTestHelper;
41 using storage::FileSystemOperation;
42 using storage::FileSystemOperationContext;
43 using storage::FileSystemOperationRunner;
44 using storage::FileSystemURL;
45 using storage::QuotaManager;
46 using storage::QuotaManagerProxy;
47 using storage::ShareableFileReference;
48
49 namespace content {
50
51 // Test class for FileSystemOperationImpl.
52 class FileSystemOperationImplTest
53 : public testing::Test {
54 public:
55 FileSystemOperationImplTest() : weak_factory_(this) {}
56
57 protected:
58 void SetUp() override {
59 EXPECT_TRUE(base_.CreateUniqueTempDir());
60 change_observers_ =
61 storage::MockFileChangeObserver::CreateList(&change_observer_);
62 update_observers_ =
63 storage::MockFileUpdateObserver::CreateList(&update_observer_);
64
65 base::FilePath base_dir = base_.GetPath().AppendASCII("filesystem");
66 quota_manager_ =
67 new MockQuotaManager(false /* is_incognito */, base_dir,
68 base::ThreadTaskRunnerHandle::Get().get(),
69 base::ThreadTaskRunnerHandle::Get().get(),
70 NULL /* special storage policy */);
71 quota_manager_proxy_ = new MockQuotaManagerProxy(
72 quota_manager(), base::ThreadTaskRunnerHandle::Get().get());
73 sandbox_file_system_.SetUp(base_dir, quota_manager_proxy_.get());
74 sandbox_file_system_.AddFileChangeObserver(&change_observer_);
75 sandbox_file_system_.AddFileUpdateObserver(&update_observer_);
76 update_observer_.Disable();
77 }
78
79 void TearDown() override {
80 // Let the client go away before dropping a ref of the quota manager proxy.
81 quota_manager_proxy()->SimulateQuotaManagerDestroyed();
82 quota_manager_ = NULL;
83 quota_manager_proxy_ = NULL;
84 sandbox_file_system_.TearDown();
85 }
86
87 FileSystemOperationRunner* operation_runner() {
88 return sandbox_file_system_.operation_runner();
89 }
90
91 const base::File::Info& info() const { return info_; }
92 const base::FilePath& path() const { return path_; }
93 const std::vector<storage::DirectoryEntry>& entries() const {
94 return entries_;
95 }
96
97 const ShareableFileReference* shareable_file_ref() const {
98 return shareable_file_ref_.get();
99 }
100
101 MockQuotaManager* quota_manager() {
102 return static_cast<MockQuotaManager*>(quota_manager_.get());
103 }
104
105 MockQuotaManagerProxy* quota_manager_proxy() {
106 return static_cast<MockQuotaManagerProxy*>(
107 quota_manager_proxy_.get());
108 }
109
110 storage::FileSystemFileUtil* file_util() {
111 return sandbox_file_system_.file_util();
112 }
113
114 storage::MockFileChangeObserver* change_observer() {
115 return &change_observer_;
116 }
117
118 std::unique_ptr<FileSystemOperationContext> NewContext() {
119 FileSystemOperationContext* context =
120 sandbox_file_system_.NewOperationContext();
121 // Grant enough quota for all test cases.
122 context->set_allowed_bytes_growth(1000000);
123 return base::WrapUnique(context);
124 }
125
126 FileSystemURL URLForPath(const std::string& path) const {
127 return sandbox_file_system_.CreateURLFromUTF8(path);
128 }
129
130 base::FilePath PlatformPath(const std::string& path) {
131 return sandbox_file_system_.GetLocalPath(
132 base::FilePath::FromUTF8Unsafe(path));
133 }
134
135 bool FileExists(const std::string& path) {
136 return AsyncFileTestHelper::FileExists(
137 sandbox_file_system_.file_system_context(), URLForPath(path),
138 AsyncFileTestHelper::kDontCheckSize);
139 }
140
141 bool DirectoryExists(const std::string& path) {
142 return AsyncFileTestHelper::DirectoryExists(
143 sandbox_file_system_.file_system_context(), URLForPath(path));
144 }
145
146 FileSystemURL CreateFile(const std::string& path) {
147 FileSystemURL url = URLForPath(path);
148 bool created = false;
149 EXPECT_EQ(base::File::FILE_OK,
150 file_util()->EnsureFileExists(NewContext().get(),
151 url, &created));
152 EXPECT_TRUE(created);
153 return url;
154 }
155
156 FileSystemURL CreateDirectory(const std::string& path) {
157 FileSystemURL url = URLForPath(path);
158 EXPECT_EQ(base::File::FILE_OK,
159 file_util()->CreateDirectory(NewContext().get(), url,
160 false /* exclusive */, true));
161 return url;
162 }
163
164 int64_t GetFileSize(const std::string& path) {
165 base::File::Info info;
166 EXPECT_TRUE(base::GetFileInfo(PlatformPath(path), &info));
167 return info.size;
168 }
169
170 // Callbacks for recording test results.
171 FileSystemOperation::StatusCallback RecordStatusCallback(
172 const base::Closure& closure,
173 base::File::Error* status) {
174 return base::Bind(&FileSystemOperationImplTest::DidFinish,
175 weak_factory_.GetWeakPtr(),
176 closure,
177 status);
178 }
179
180 FileSystemOperation::ReadDirectoryCallback RecordReadDirectoryCallback(
181 const base::Closure& closure,
182 base::File::Error* status) {
183 return base::Bind(&FileSystemOperationImplTest::DidReadDirectory,
184 weak_factory_.GetWeakPtr(),
185 closure,
186 status);
187 }
188
189 FileSystemOperation::GetMetadataCallback RecordMetadataCallback(
190 const base::Closure& closure,
191 base::File::Error* status) {
192 return base::Bind(&FileSystemOperationImplTest::DidGetMetadata,
193 weak_factory_.GetWeakPtr(),
194 closure,
195 status);
196 }
197
198 FileSystemOperation::SnapshotFileCallback RecordSnapshotFileCallback(
199 const base::Closure& closure,
200 base::File::Error* status) {
201 return base::Bind(&FileSystemOperationImplTest::DidCreateSnapshotFile,
202 weak_factory_.GetWeakPtr(),
203 closure,
204 status);
205 }
206
207 void DidFinish(const base::Closure& closure,
208 base::File::Error* status,
209 base::File::Error actual) {
210 *status = actual;
211 closure.Run();
212 }
213
214 void DidReadDirectory(const base::Closure& closure,
215 base::File::Error* status,
216 base::File::Error actual,
217 const std::vector<storage::DirectoryEntry>& entries,
218 bool /* has_more */) {
219 entries_ = entries;
220 *status = actual;
221 closure.Run();
222 }
223
224 void DidGetMetadata(const base::Closure& closure,
225 base::File::Error* status,
226 base::File::Error actual,
227 const base::File::Info& info) {
228 info_ = info;
229 *status = actual;
230 closure.Run();
231 }
232
233 void DidCreateSnapshotFile(
234 const base::Closure& closure,
235 base::File::Error* status,
236 base::File::Error actual,
237 const base::File::Info& info,
238 const base::FilePath& platform_path,
239 const scoped_refptr<ShareableFileReference>& shareable_file_ref) {
240 info_ = info;
241 path_ = platform_path;
242 *status = actual;
243 shareable_file_ref_ = shareable_file_ref;
244 closure.Run();
245 }
246
247 int64_t GetDataSizeOnDisk() {
248 return sandbox_file_system_.ComputeCurrentOriginUsage() -
249 sandbox_file_system_.ComputeCurrentDirectoryDatabaseUsage();
250 }
251
252 void GetUsageAndQuota(int64_t* usage, int64_t* quota) {
253 storage::QuotaStatusCode status =
254 AsyncFileTestHelper::GetUsageAndQuota(quota_manager_.get(),
255 sandbox_file_system_.origin(),
256 sandbox_file_system_.type(),
257 usage,
258 quota);
259 base::RunLoop().RunUntilIdle();
260 ASSERT_EQ(storage::kQuotaStatusOk, status);
261 }
262
263 int64_t ComputePathCost(const FileSystemURL& url) {
264 int64_t base_usage;
265 GetUsageAndQuota(&base_usage, NULL);
266
267 AsyncFileTestHelper::CreateFile(
268 sandbox_file_system_.file_system_context(), url);
269 EXPECT_EQ(base::File::FILE_OK, Remove(url, false /* recursive */));
270
271 change_observer()->ResetCount();
272
273 int64_t total_usage;
274 GetUsageAndQuota(&total_usage, NULL);
275 return total_usage - base_usage;
276 }
277
278 void GrantQuotaForCurrentUsage() {
279 int64_t usage;
280 GetUsageAndQuota(&usage, NULL);
281 quota_manager()->SetQuota(sandbox_file_system_.origin(),
282 sandbox_file_system_.storage_type(),
283 usage);
284 }
285
286 int64_t GetUsage() {
287 int64_t usage = 0;
288 GetUsageAndQuota(&usage, NULL);
289 return usage;
290 }
291
292 void AddQuota(int64_t quota_delta) {
293 int64_t quota;
294 GetUsageAndQuota(NULL, &quota);
295 quota_manager()->SetQuota(sandbox_file_system_.origin(),
296 sandbox_file_system_.storage_type(),
297 quota + quota_delta);
298 }
299
300 base::File::Error Move(
301 const FileSystemURL& src,
302 const FileSystemURL& dest,
303 storage::FileSystemOperation::CopyOrMoveOption option) {
304 base::File::Error status;
305 base::RunLoop run_loop;
306 update_observer_.Enable();
307 operation_runner()->Move(
308 src,
309 dest,
310 option,
311 RecordStatusCallback(run_loop.QuitClosure(), &status));
312 run_loop.Run();
313 update_observer_.Disable();
314 return status;
315 }
316
317 base::File::Error Copy(
318 const FileSystemURL& src,
319 const FileSystemURL& dest,
320 storage::FileSystemOperation::CopyOrMoveOption option) {
321 base::File::Error status;
322 base::RunLoop run_loop;
323 update_observer_.Enable();
324 operation_runner()->Copy(
325 src, dest, option, storage::FileSystemOperation::ERROR_BEHAVIOR_ABORT,
326 FileSystemOperationRunner::CopyProgressCallback(),
327 RecordStatusCallback(run_loop.QuitClosure(), &status));
328 run_loop.Run();
329 update_observer_.Disable();
330 return status;
331 }
332
333 base::File::Error CopyInForeignFile(const base::FilePath& src,
334 const FileSystemURL& dest) {
335 base::File::Error status;
336 base::RunLoop run_loop;
337 update_observer_.Enable();
338 operation_runner()->CopyInForeignFile(
339 src, dest, RecordStatusCallback(run_loop.QuitClosure(), &status));
340 run_loop.Run();
341 update_observer_.Disable();
342 return status;
343 }
344
345 base::File::Error Truncate(const FileSystemURL& url, int size) {
346 base::File::Error status;
347 base::RunLoop run_loop;
348 update_observer_.Enable();
349 operation_runner()->Truncate(
350 url, size, RecordStatusCallback(run_loop.QuitClosure(), &status));
351 run_loop.Run();
352 update_observer_.Disable();
353 return status;
354 }
355
356 base::File::Error CreateFile(const FileSystemURL& url, bool exclusive) {
357 base::File::Error status;
358 base::RunLoop run_loop;
359 update_observer_.Enable();
360 operation_runner()->CreateFile(
361 url, exclusive, RecordStatusCallback(run_loop.QuitClosure(), &status));
362 run_loop.Run();
363 update_observer_.Disable();
364 return status;
365 }
366
367 base::File::Error Remove(const FileSystemURL& url, bool recursive) {
368 base::File::Error status;
369 base::RunLoop run_loop;
370 update_observer_.Enable();
371 operation_runner()->Remove(
372 url, recursive, RecordStatusCallback(run_loop.QuitClosure(), &status));
373 run_loop.Run();
374 update_observer_.Disable();
375 return status;
376 }
377
378 base::File::Error CreateDirectory(const FileSystemURL& url,
379 bool exclusive,
380 bool recursive) {
381 base::File::Error status;
382 base::RunLoop run_loop;
383 update_observer_.Enable();
384 operation_runner()->CreateDirectory(
385 url,
386 exclusive,
387 recursive,
388 RecordStatusCallback(run_loop.QuitClosure(), &status));
389 run_loop.Run();
390 update_observer_.Disable();
391 return status;
392 }
393
394 base::File::Error GetMetadata(const FileSystemURL& url, int fields) {
395 base::File::Error status;
396 base::RunLoop run_loop;
397 update_observer_.Enable();
398 operation_runner()->GetMetadata(
399 url, fields, RecordMetadataCallback(run_loop.QuitClosure(), &status));
400 run_loop.Run();
401 update_observer_.Disable();
402 return status;
403 }
404
405 base::File::Error ReadDirectory(const FileSystemURL& url) {
406 base::File::Error status;
407 base::RunLoop run_loop;
408 update_observer_.Enable();
409 operation_runner()->ReadDirectory(
410 url, RecordReadDirectoryCallback(run_loop.QuitClosure(), &status));
411 run_loop.Run();
412 update_observer_.Disable();
413 return status;
414 }
415
416 base::File::Error CreateSnapshotFile(const FileSystemURL& url) {
417 base::File::Error status;
418 base::RunLoop run_loop;
419 update_observer_.Enable();
420 operation_runner()->CreateSnapshotFile(
421 url, RecordSnapshotFileCallback(run_loop.QuitClosure(), &status));
422 run_loop.Run();
423 update_observer_.Disable();
424 return status;
425 }
426
427 base::File::Error FileExists(const FileSystemURL& url) {
428 base::File::Error status;
429 base::RunLoop run_loop;
430 update_observer_.Enable();
431 operation_runner()->FileExists(
432 url, RecordStatusCallback(run_loop.QuitClosure(), &status));
433 run_loop.Run();
434 update_observer_.Disable();
435 return status;
436 }
437
438 base::File::Error DirectoryExists(const FileSystemURL& url) {
439 base::File::Error status;
440 base::RunLoop run_loop;
441 update_observer_.Enable();
442 operation_runner()->DirectoryExists(
443 url, RecordStatusCallback(run_loop.QuitClosure(), &status));
444 run_loop.Run();
445 update_observer_.Disable();
446 return status;
447 }
448
449 base::File::Error TouchFile(const FileSystemURL& url,
450 const base::Time& last_access_time,
451 const base::Time& last_modified_time) {
452 base::File::Error status;
453 base::RunLoop run_loop;
454 update_observer_.Enable();
455 operation_runner()->TouchFile(
456 url,
457 last_access_time,
458 last_modified_time,
459 RecordStatusCallback(run_loop.QuitClosure(), &status));
460 run_loop.Run();
461 update_observer_.Disable();
462 return status;
463 }
464
465 private:
466 base::MessageLoopForIO message_loop_;
467 scoped_refptr<QuotaManager> quota_manager_;
468 scoped_refptr<QuotaManagerProxy> quota_manager_proxy_;
469
470 // Common temp base for nondestructive uses.
471 base::ScopedTempDir base_;
472
473 SandboxFileSystemTestHelper sandbox_file_system_;
474
475 // For post-operation status.
476 base::File::Info info_;
477 base::FilePath path_;
478 std::vector<storage::DirectoryEntry> entries_;
479 scoped_refptr<ShareableFileReference> shareable_file_ref_;
480
481 storage::MockFileChangeObserver change_observer_;
482 storage::ChangeObserverList change_observers_;
483 storage::MockFileUpdateObserver update_observer_;
484 storage::UpdateObserverList update_observers_;
485
486 base::WeakPtrFactory<FileSystemOperationImplTest> weak_factory_;
487
488 DISALLOW_COPY_AND_ASSIGN(FileSystemOperationImplTest);
489 };
490
491 TEST_F(FileSystemOperationImplTest, TestMoveFailureSrcDoesntExist) {
492 change_observer()->ResetCount();
493 EXPECT_EQ(
494 base::File::FILE_ERROR_NOT_FOUND,
495 Move(URLForPath("a"), URLForPath("b"), FileSystemOperation::OPTION_NONE));
496 EXPECT_TRUE(change_observer()->HasNoChange());
497 }
498
499 TEST_F(FileSystemOperationImplTest, TestMoveFailureContainsPath) {
500 FileSystemURL src_dir(CreateDirectory("src"));
501 FileSystemURL dest_dir(CreateDirectory("src/dest"));
502
503 EXPECT_EQ(base::File::FILE_ERROR_INVALID_OPERATION,
504 Move(src_dir, dest_dir, FileSystemOperation::OPTION_NONE));
505 EXPECT_TRUE(change_observer()->HasNoChange());
506 }
507
508 TEST_F(FileSystemOperationImplTest, TestMoveFailureSrcDirExistsDestFile) {
509 // Src exists and is dir. Dest is a file.
510 FileSystemURL src_dir(CreateDirectory("src"));
511 FileSystemURL dest_dir(CreateDirectory("dest"));
512 FileSystemURL dest_file(CreateFile("dest/file"));
513
514 EXPECT_EQ(base::File::FILE_ERROR_INVALID_OPERATION,
515 Move(src_dir, dest_file, FileSystemOperation::OPTION_NONE));
516 EXPECT_TRUE(change_observer()->HasNoChange());
517 }
518
519 TEST_F(FileSystemOperationImplTest,
520 TestMoveFailureSrcFileExistsDestNonEmptyDir) {
521 // Src exists and is a directory. Dest is a non-empty directory.
522 FileSystemURL src_dir(CreateDirectory("src"));
523 FileSystemURL dest_dir(CreateDirectory("dest"));
524 FileSystemURL dest_file(CreateFile("dest/file"));
525
526 EXPECT_EQ(base::File::FILE_ERROR_NOT_EMPTY,
527 Move(src_dir, dest_dir, FileSystemOperation::OPTION_NONE));
528 EXPECT_TRUE(change_observer()->HasNoChange());
529 }
530
531 TEST_F(FileSystemOperationImplTest, TestMoveFailureSrcFileExistsDestDir) {
532 // Src exists and is a file. Dest is a directory.
533 FileSystemURL src_dir(CreateDirectory("src"));
534 FileSystemURL src_file(CreateFile("src/file"));
535 FileSystemURL dest_dir(CreateDirectory("dest"));
536
537 EXPECT_EQ(base::File::FILE_ERROR_INVALID_OPERATION,
538 Move(src_file, dest_dir, FileSystemOperation::OPTION_NONE));
539 EXPECT_TRUE(change_observer()->HasNoChange());
540 }
541
542 TEST_F(FileSystemOperationImplTest, TestMoveFailureDestParentDoesntExist) {
543 // Dest. parent path does not exist.
544 FileSystemURL src_dir(CreateDirectory("src"));
545 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
546 Move(src_dir,
547 URLForPath("nonexistent/deset"),
548 FileSystemOperation::OPTION_NONE));
549 EXPECT_TRUE(change_observer()->HasNoChange());
550 }
551
552 TEST_F(FileSystemOperationImplTest, TestMoveSuccessSrcFileAndOverwrite) {
553 FileSystemURL src_file(CreateFile("src"));
554 FileSystemURL dest_file(CreateFile("dest"));
555
556 EXPECT_EQ(base::File::FILE_OK,
557 Move(src_file, dest_file, FileSystemOperation::OPTION_NONE));
558 EXPECT_TRUE(FileExists("dest"));
559
560 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
561 EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count());
562 EXPECT_TRUE(change_observer()->HasNoChange());
563
564 EXPECT_EQ(1, quota_manager_proxy()->notify_storage_accessed_count());
565 }
566
567 TEST_F(FileSystemOperationImplTest, TestMoveSuccessSrcFileAndNew) {
568 FileSystemURL src_file(CreateFile("src"));
569
570 EXPECT_EQ(
571 base::File::FILE_OK,
572 Move(src_file, URLForPath("new"), FileSystemOperation::OPTION_NONE));
573 EXPECT_TRUE(FileExists("new"));
574
575 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_from_count());
576 EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count());
577 EXPECT_TRUE(change_observer()->HasNoChange());
578 }
579
580 TEST_F(FileSystemOperationImplTest, TestMoveSuccessSrcDirAndOverwrite) {
581 FileSystemURL src_dir(CreateDirectory("src"));
582 FileSystemURL dest_dir(CreateDirectory("dest"));
583
584 EXPECT_EQ(base::File::FILE_OK,
585 Move(src_dir, dest_dir, FileSystemOperation::OPTION_NONE));
586 EXPECT_FALSE(DirectoryExists("src"));
587
588 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
589 EXPECT_EQ(2, change_observer()->get_and_reset_remove_directory_count());
590 EXPECT_TRUE(change_observer()->HasNoChange());
591
592 // Make sure we've overwritten but not moved the source under the |dest_dir|.
593 EXPECT_TRUE(DirectoryExists("dest"));
594 EXPECT_FALSE(DirectoryExists("dest/src"));
595 }
596
597 TEST_F(FileSystemOperationImplTest, TestMoveSuccessSrcDirAndNew) {
598 FileSystemURL src_dir(CreateDirectory("src"));
599 FileSystemURL dest_dir(CreateDirectory("dest"));
600
601 EXPECT_EQ(
602 base::File::FILE_OK,
603 Move(src_dir, URLForPath("dest/new"), FileSystemOperation::OPTION_NONE));
604 EXPECT_FALSE(DirectoryExists("src"));
605 EXPECT_TRUE(DirectoryExists("dest/new"));
606
607 EXPECT_EQ(1, change_observer()->get_and_reset_remove_directory_count());
608 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
609 EXPECT_TRUE(change_observer()->HasNoChange());
610 }
611
612 TEST_F(FileSystemOperationImplTest, TestMoveSuccessSrcDirRecursive) {
613 FileSystemURL src_dir(CreateDirectory("src"));
614 CreateDirectory("src/dir");
615 CreateFile("src/dir/sub");
616
617 FileSystemURL dest_dir(CreateDirectory("dest"));
618
619 EXPECT_EQ(base::File::FILE_OK,
620 Move(src_dir, dest_dir, FileSystemOperation::OPTION_NONE));
621 EXPECT_TRUE(DirectoryExists("dest/dir"));
622 EXPECT_TRUE(FileExists("dest/dir/sub"));
623
624 EXPECT_EQ(3, change_observer()->get_and_reset_remove_directory_count());
625 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
626 EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count());
627 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_from_count());
628 EXPECT_TRUE(change_observer()->HasNoChange());
629 }
630
631 TEST_F(FileSystemOperationImplTest, TestMoveSuccessSamePath) {
632 FileSystemURL src_dir(CreateDirectory("src"));
633 CreateDirectory("src/dir");
634 CreateFile("src/dir/sub");
635
636 EXPECT_EQ(base::File::FILE_OK,
637 Move(src_dir, src_dir, FileSystemOperation::OPTION_NONE));
638 EXPECT_TRUE(DirectoryExists("src/dir"));
639 EXPECT_TRUE(FileExists("src/dir/sub"));
640
641 EXPECT_EQ(0, change_observer()->get_and_reset_remove_directory_count());
642 EXPECT_EQ(0, change_observer()->get_and_reset_create_directory_count());
643 EXPECT_EQ(0, change_observer()->get_and_reset_remove_file_count());
644 EXPECT_EQ(0, change_observer()->get_and_reset_create_file_from_count());
645 EXPECT_TRUE(change_observer()->HasNoChange());
646 }
647
648 TEST_F(FileSystemOperationImplTest, TestCopyFailureSrcDoesntExist) {
649 EXPECT_EQ(
650 base::File::FILE_ERROR_NOT_FOUND,
651 Copy(URLForPath("a"), URLForPath("b"), FileSystemOperation::OPTION_NONE));
652 EXPECT_TRUE(change_observer()->HasNoChange());
653 }
654
655 TEST_F(FileSystemOperationImplTest, TestCopyFailureContainsPath) {
656 FileSystemURL src_dir(CreateDirectory("src"));
657 FileSystemURL dest_dir(CreateDirectory("src/dir"));
658
659 EXPECT_EQ(base::File::FILE_ERROR_INVALID_OPERATION,
660 Copy(src_dir, dest_dir, FileSystemOperation::OPTION_NONE));
661 EXPECT_TRUE(change_observer()->HasNoChange());
662 }
663
664 TEST_F(FileSystemOperationImplTest, TestCopyFailureSrcDirExistsDestFile) {
665 // Src exists and is dir. Dest is a file.
666 FileSystemURL src_dir(CreateDirectory("src"));
667 FileSystemURL dest_dir(CreateDirectory("dest"));
668 FileSystemURL dest_file(CreateFile("dest/file"));
669
670 EXPECT_EQ(base::File::FILE_ERROR_INVALID_OPERATION,
671 Copy(src_dir, dest_file, FileSystemOperation::OPTION_NONE));
672 EXPECT_TRUE(change_observer()->HasNoChange());
673 }
674
675 TEST_F(FileSystemOperationImplTest,
676 TestCopyFailureSrcFileExistsDestNonEmptyDir) {
677 // Src exists and is a directory. Dest is a non-empty directory.
678 FileSystemURL src_dir(CreateDirectory("src"));
679 FileSystemURL dest_dir(CreateDirectory("dest"));
680 FileSystemURL dest_file(CreateFile("dest/file"));
681
682 EXPECT_EQ(base::File::FILE_ERROR_NOT_EMPTY,
683 Copy(src_dir, dest_dir, FileSystemOperation::OPTION_NONE));
684 EXPECT_TRUE(change_observer()->HasNoChange());
685 }
686
687 TEST_F(FileSystemOperationImplTest, TestCopyFailureSrcFileExistsDestDir) {
688 // Src exists and is a file. Dest is a directory.
689 FileSystemURL src_file(CreateFile("src"));
690 FileSystemURL dest_dir(CreateDirectory("dest"));
691
692 EXPECT_EQ(base::File::FILE_ERROR_INVALID_OPERATION,
693 Copy(src_file, dest_dir, FileSystemOperation::OPTION_NONE));
694 EXPECT_TRUE(change_observer()->HasNoChange());
695 }
696
697 TEST_F(FileSystemOperationImplTest, TestCopyFailureDestParentDoesntExist) {
698 // Dest. parent path does not exist.
699 FileSystemURL src_dir(CreateDirectory("src"));
700
701 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
702 Copy(src_dir,
703 URLForPath("nonexistent/dest"),
704 FileSystemOperation::OPTION_NONE));
705 EXPECT_TRUE(change_observer()->HasNoChange());
706 }
707
708 TEST_F(FileSystemOperationImplTest, TestCopyFailureByQuota) {
709 FileSystemURL src_dir(CreateDirectory("src"));
710 FileSystemURL src_file(CreateFile("src/file"));
711 FileSystemURL dest_dir(CreateDirectory("dest"));
712 EXPECT_EQ(base::File::FILE_OK, Truncate(src_file, 6));
713 EXPECT_EQ(6, GetFileSize("src/file"));
714
715 FileSystemURL dest_file(URLForPath("dest/file"));
716 int64_t dest_path_cost = ComputePathCost(dest_file);
717 GrantQuotaForCurrentUsage();
718 AddQuota(6 + dest_path_cost - 1);
719
720 EXPECT_EQ(base::File::FILE_ERROR_NO_SPACE,
721 Copy(src_file, dest_file, FileSystemOperation::OPTION_NONE));
722 EXPECT_FALSE(FileExists("dest/file"));
723 }
724
725 TEST_F(FileSystemOperationImplTest, TestCopySuccessSrcFileAndOverwrite) {
726 FileSystemURL src_file(CreateFile("src"));
727 FileSystemURL dest_file(CreateFile("dest"));
728
729 EXPECT_EQ(base::File::FILE_OK,
730 Copy(src_file, dest_file, FileSystemOperation::OPTION_NONE));
731
732 EXPECT_TRUE(FileExists("dest"));
733 EXPECT_EQ(4, quota_manager_proxy()->notify_storage_accessed_count());
734 EXPECT_EQ(2, change_observer()->get_and_reset_modify_file_count());
735
736 EXPECT_TRUE(change_observer()->HasNoChange());
737 }
738
739 TEST_F(FileSystemOperationImplTest, TestCopySuccessSrcFileAndNew) {
740 FileSystemURL src_file(CreateFile("src"));
741
742 EXPECT_EQ(
743 base::File::FILE_OK,
744 Copy(src_file, URLForPath("new"), FileSystemOperation::OPTION_NONE));
745 EXPECT_TRUE(FileExists("new"));
746 EXPECT_EQ(4, quota_manager_proxy()->notify_storage_accessed_count());
747
748 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
749 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
750 EXPECT_TRUE(change_observer()->HasNoChange());
751 }
752
753 TEST_F(FileSystemOperationImplTest, TestCopySuccessSrcDirAndOverwrite) {
754 FileSystemURL src_dir(CreateDirectory("src"));
755 FileSystemURL dest_dir(CreateDirectory("dest"));
756
757 EXPECT_EQ(base::File::FILE_OK,
758 Copy(src_dir, dest_dir, FileSystemOperation::OPTION_NONE));
759
760 // Make sure we've overwritten but not copied the source under the |dest_dir|.
761 EXPECT_TRUE(DirectoryExists("dest"));
762 EXPECT_FALSE(DirectoryExists("dest/src"));
763 EXPECT_GE(quota_manager_proxy()->notify_storage_accessed_count(), 3);
764
765 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
766 EXPECT_EQ(1, change_observer()->get_and_reset_remove_directory_count());
767 EXPECT_TRUE(change_observer()->HasNoChange());
768 }
769
770 TEST_F(FileSystemOperationImplTest, TestCopySuccessSrcDirAndNew) {
771 FileSystemURL src_dir(CreateDirectory("src"));
772 FileSystemURL dest_dir_new(URLForPath("dest"));
773
774 EXPECT_EQ(base::File::FILE_OK,
775 Copy(src_dir, dest_dir_new, FileSystemOperation::OPTION_NONE));
776 EXPECT_TRUE(DirectoryExists("dest"));
777 EXPECT_GE(quota_manager_proxy()->notify_storage_accessed_count(), 2);
778
779 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
780 EXPECT_TRUE(change_observer()->HasNoChange());
781 }
782
783 TEST_F(FileSystemOperationImplTest, TestCopySuccessSrcDirRecursive) {
784 FileSystemURL src_dir(CreateDirectory("src"));
785 CreateDirectory("src/dir");
786 CreateFile("src/dir/sub");
787
788 FileSystemURL dest_dir(CreateDirectory("dest"));
789
790 EXPECT_EQ(base::File::FILE_OK,
791 Copy(src_dir, dest_dir, FileSystemOperation::OPTION_NONE));
792
793 EXPECT_TRUE(DirectoryExists("dest/dir"));
794 EXPECT_TRUE(FileExists("dest/dir/sub"));
795
796 // For recursive copy we may record multiple read access.
797 EXPECT_GE(quota_manager_proxy()->notify_storage_accessed_count(), 1);
798
799 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
800 EXPECT_EQ(1, change_observer()->get_and_reset_remove_directory_count());
801 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
802 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
803 EXPECT_TRUE(change_observer()->HasNoChange());
804 }
805
806 TEST_F(FileSystemOperationImplTest, TestCopySuccessSamePath) {
807 FileSystemURL src_dir(CreateDirectory("src"));
808 CreateDirectory("src/dir");
809 CreateFile("src/dir/sub");
810
811 EXPECT_EQ(base::File::FILE_OK,
812 Copy(src_dir, src_dir, FileSystemOperation::OPTION_NONE));
813
814 EXPECT_TRUE(DirectoryExists("src/dir"));
815 EXPECT_TRUE(FileExists("src/dir/sub"));
816
817 EXPECT_EQ(0, change_observer()->get_and_reset_create_directory_count());
818 EXPECT_EQ(0, change_observer()->get_and_reset_remove_file_count());
819 EXPECT_EQ(0, change_observer()->get_and_reset_create_file_from_count());
820 EXPECT_TRUE(change_observer()->HasNoChange());
821 }
822
823 TEST_F(FileSystemOperationImplTest, TestCopyInForeignFileSuccess) {
824 base::FilePath src_local_disk_file_path;
825 base::CreateTemporaryFile(&src_local_disk_file_path);
826 const char test_data[] = "foo";
827 int data_size = arraysize(test_data);
828 base::WriteFile(src_local_disk_file_path, test_data, data_size);
829
830 FileSystemURL dest_dir(CreateDirectory("dest"));
831
832 int64_t before_usage;
833 GetUsageAndQuota(&before_usage, NULL);
834
835 // Check that the file copied and corresponding usage increased.
836 EXPECT_EQ(
837 base::File::FILE_OK,
838 CopyInForeignFile(src_local_disk_file_path, URLForPath("dest/file")));
839
840 EXPECT_EQ(1, change_observer()->create_file_count());
841 EXPECT_TRUE(FileExists("dest/file"));
842 int64_t after_usage;
843 GetUsageAndQuota(&after_usage, NULL);
844 EXPECT_GT(after_usage, before_usage);
845
846 // Compare contents of src and copied file.
847 char buffer[100];
848 EXPECT_EQ(data_size, base::ReadFile(PlatformPath("dest/file"),
849 buffer, data_size));
850 for (int i = 0; i < data_size; ++i)
851 EXPECT_EQ(test_data[i], buffer[i]);
852 }
853
854 TEST_F(FileSystemOperationImplTest, TestCopyInForeignFileFailureByQuota) {
855 base::FilePath src_local_disk_file_path;
856 base::CreateTemporaryFile(&src_local_disk_file_path);
857 const char test_data[] = "foo";
858 base::WriteFile(src_local_disk_file_path, test_data, arraysize(test_data));
859
860 FileSystemURL dest_dir(CreateDirectory("dest"));
861
862 GrantQuotaForCurrentUsage();
863 EXPECT_EQ(
864 base::File::FILE_ERROR_NO_SPACE,
865 CopyInForeignFile(src_local_disk_file_path, URLForPath("dest/file")));
866
867 EXPECT_FALSE(FileExists("dest/file"));
868 EXPECT_EQ(0, change_observer()->create_file_count());
869 }
870
871 TEST_F(FileSystemOperationImplTest, TestCreateFileFailure) {
872 // Already existing file and exclusive true.
873 FileSystemURL file(CreateFile("file"));
874 EXPECT_EQ(base::File::FILE_ERROR_EXISTS, CreateFile(file, true));
875 EXPECT_TRUE(change_observer()->HasNoChange());
876 }
877
878 TEST_F(FileSystemOperationImplTest, TestCreateFileSuccessFileExists) {
879 // Already existing file and exclusive false.
880 FileSystemURL file(CreateFile("file"));
881 EXPECT_EQ(base::File::FILE_OK, CreateFile(file, false));
882 EXPECT_TRUE(FileExists("file"));
883
884 // The file was already there; did nothing.
885 EXPECT_TRUE(change_observer()->HasNoChange());
886 }
887
888 TEST_F(FileSystemOperationImplTest, TestCreateFileSuccessExclusive) {
889 // File doesn't exist but exclusive is true.
890 EXPECT_EQ(base::File::FILE_OK, CreateFile(URLForPath("new"), true));
891 EXPECT_TRUE(FileExists("new"));
892 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
893 }
894
895 TEST_F(FileSystemOperationImplTest, TestCreateFileSuccessFileDoesntExist) {
896 // Non existing file.
897 EXPECT_EQ(base::File::FILE_OK, CreateFile(URLForPath("nonexistent"), false));
898 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
899 }
900
901 TEST_F(FileSystemOperationImplTest,
902 TestCreateDirFailureDestParentDoesntExist) {
903 // Dest. parent path does not exist.
904 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
905 CreateDirectory(URLForPath("nonexistent/dir"), false, false));
906 EXPECT_TRUE(change_observer()->HasNoChange());
907 }
908
909 TEST_F(FileSystemOperationImplTest, TestCreateDirFailureDirExists) {
910 // Exclusive and dir existing at path.
911 FileSystemURL dir(CreateDirectory("dir"));
912 EXPECT_EQ(base::File::FILE_ERROR_EXISTS, CreateDirectory(dir, true, false));
913 EXPECT_TRUE(change_observer()->HasNoChange());
914 }
915
916 TEST_F(FileSystemOperationImplTest, TestCreateDirFailureFileExists) {
917 // Exclusive true and file existing at path.
918 FileSystemURL file(CreateFile("file"));
919 EXPECT_EQ(base::File::FILE_ERROR_EXISTS, CreateDirectory(file, true, false));
920 EXPECT_TRUE(change_observer()->HasNoChange());
921 }
922
923 TEST_F(FileSystemOperationImplTest, TestCreateDirSuccess) {
924 // Dir exists and exclusive is false.
925 FileSystemURL dir(CreateDirectory("dir"));
926 EXPECT_EQ(base::File::FILE_OK, CreateDirectory(dir, false, false));
927 EXPECT_TRUE(change_observer()->HasNoChange());
928
929 // Dir doesn't exist.
930 EXPECT_EQ(base::File::FILE_OK,
931 CreateDirectory(URLForPath("new"), false, false));
932 EXPECT_TRUE(DirectoryExists("new"));
933 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
934 }
935
936 TEST_F(FileSystemOperationImplTest, TestCreateDirSuccessExclusive) {
937 // Dir doesn't exist.
938 EXPECT_EQ(base::File::FILE_OK,
939 CreateDirectory(URLForPath("new"), true, false));
940 EXPECT_TRUE(DirectoryExists("new"));
941 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
942 EXPECT_TRUE(change_observer()->HasNoChange());
943 }
944
945 TEST_F(FileSystemOperationImplTest, TestExistsAndMetadataFailure) {
946 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
947 GetMetadata(URLForPath("nonexistent"),
948 storage::FileSystemOperation::GET_METADATA_FIELD_NONE));
949
950 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
951 FileExists(URLForPath("nonexistent")));
952
953 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
954 DirectoryExists(URLForPath("nonexistent")));
955 EXPECT_TRUE(change_observer()->HasNoChange());
956 }
957
958 TEST_F(FileSystemOperationImplTest, TestExistsAndMetadataSuccess) {
959 FileSystemURL dir(CreateDirectory("dir"));
960 FileSystemURL file(CreateFile("dir/file"));
961 int read_access = 0;
962
963 EXPECT_EQ(base::File::FILE_OK, DirectoryExists(dir));
964 ++read_access;
965
966 EXPECT_EQ(
967 base::File::FILE_OK,
968 GetMetadata(
969 dir, storage::FileSystemOperation::GET_METADATA_FIELD_IS_DIRECTORY));
970 EXPECT_TRUE(info().is_directory);
971 ++read_access;
972
973 EXPECT_EQ(base::File::FILE_OK, FileExists(file));
974 ++read_access;
975
976 EXPECT_EQ(
977 base::File::FILE_OK,
978 GetMetadata(
979 file, storage::FileSystemOperation::GET_METADATA_FIELD_IS_DIRECTORY));
980 EXPECT_FALSE(info().is_directory);
981 ++read_access;
982
983 EXPECT_EQ(read_access,
984 quota_manager_proxy()->notify_storage_accessed_count());
985 EXPECT_TRUE(change_observer()->HasNoChange());
986 }
987
988 TEST_F(FileSystemOperationImplTest, TestTypeMismatchErrors) {
989 FileSystemURL dir(CreateDirectory("dir"));
990 EXPECT_EQ(base::File::FILE_ERROR_NOT_A_FILE, FileExists(dir));
991
992 FileSystemURL file(CreateFile("file"));
993 EXPECT_EQ(base::File::FILE_ERROR_NOT_A_DIRECTORY, DirectoryExists(file));
994 }
995
996 TEST_F(FileSystemOperationImplTest, TestReadDirFailure) {
997 // Path doesn't exist
998 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
999 ReadDirectory(URLForPath("nonexistent")));
1000
1001 // File exists.
1002 FileSystemURL file(CreateFile("file"));
1003 EXPECT_EQ(base::File::FILE_ERROR_NOT_A_DIRECTORY, ReadDirectory(file));
1004 EXPECT_TRUE(change_observer()->HasNoChange());
1005 }
1006
1007 TEST_F(FileSystemOperationImplTest, TestReadDirSuccess) {
1008 // parent_dir
1009 // | |
1010 // child_dir child_file
1011 // Verify reading parent_dir.
1012 FileSystemURL parent_dir(CreateDirectory("dir"));
1013 FileSystemURL child_dir(CreateDirectory("dir/child_dir"));
1014 FileSystemURL child_file(CreateFile("dir/child_file"));
1015
1016 EXPECT_EQ(base::File::FILE_OK, ReadDirectory(parent_dir));
1017 EXPECT_EQ(2u, entries().size());
1018
1019 for (size_t i = 0; i < entries().size(); ++i) {
1020 if (entries()[i].is_directory)
1021 EXPECT_EQ(FILE_PATH_LITERAL("child_dir"), entries()[i].name);
1022 else
1023 EXPECT_EQ(FILE_PATH_LITERAL("child_file"), entries()[i].name);
1024 }
1025 EXPECT_EQ(1, quota_manager_proxy()->notify_storage_accessed_count());
1026 EXPECT_TRUE(change_observer()->HasNoChange());
1027 }
1028
1029 TEST_F(FileSystemOperationImplTest, TestRemoveFailure) {
1030 // Path doesn't exist.
1031 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
1032 Remove(URLForPath("nonexistent"), false /* recursive */));
1033
1034 // It's an error to try to remove a non-empty directory if recursive flag
1035 // is false.
1036 // parent_dir
1037 // | |
1038 // child_dir child_file
1039 // Verify deleting parent_dir.
1040 FileSystemURL parent_dir(CreateDirectory("dir"));
1041 FileSystemURL child_dir(CreateDirectory("dir/child_dir"));
1042 FileSystemURL child_file(CreateFile("dir/child_file"));
1043
1044 EXPECT_EQ(base::File::FILE_ERROR_NOT_EMPTY,
1045 Remove(parent_dir, false /* recursive */));
1046 EXPECT_TRUE(change_observer()->HasNoChange());
1047 }
1048
1049 TEST_F(FileSystemOperationImplTest, TestRemoveSuccess) {
1050 FileSystemURL empty_dir(CreateDirectory("empty_dir"));
1051 EXPECT_TRUE(DirectoryExists("empty_dir"));
1052 EXPECT_EQ(base::File::FILE_OK, Remove(empty_dir, false /* recursive */));
1053 EXPECT_FALSE(DirectoryExists("empty_dir"));
1054
1055 EXPECT_EQ(1, change_observer()->get_and_reset_remove_directory_count());
1056 EXPECT_TRUE(change_observer()->HasNoChange());
1057 }
1058
1059 TEST_F(FileSystemOperationImplTest, TestRemoveSuccessRecursive) {
1060 // Removing a non-empty directory with recursive flag == true should be ok.
1061 // parent_dir
1062 // | |
1063 // child_dir child_files
1064 // |
1065 // child_files
1066 //
1067 // Verify deleting parent_dir.
1068 FileSystemURL parent_dir(CreateDirectory("dir"));
1069 for (int i = 0; i < 8; ++i)
1070 CreateFile(base::StringPrintf("dir/file-%d", i));
1071 FileSystemURL child_dir(CreateDirectory("dir/child_dir"));
1072 for (int i = 0; i < 8; ++i)
1073 CreateFile(base::StringPrintf("dir/child_dir/file-%d", i));
1074
1075 EXPECT_EQ(base::File::FILE_OK, Remove(parent_dir, true /* recursive */));
1076 EXPECT_FALSE(DirectoryExists("parent_dir"));
1077
1078 EXPECT_EQ(2, change_observer()->get_and_reset_remove_directory_count());
1079 EXPECT_EQ(16, change_observer()->get_and_reset_remove_file_count());
1080 EXPECT_TRUE(change_observer()->HasNoChange());
1081 }
1082
1083 TEST_F(FileSystemOperationImplTest, TestTruncate) {
1084 FileSystemURL file(CreateFile("file"));
1085 base::FilePath platform_path = PlatformPath("file");
1086
1087 char test_data[] = "test data";
1088 int data_size = static_cast<int>(sizeof(test_data));
1089 EXPECT_EQ(data_size,
1090 base::WriteFile(platform_path, test_data, data_size));
1091
1092 // Check that its length is the size of the data written.
1093 EXPECT_EQ(
1094 base::File::FILE_OK,
1095 GetMetadata(
1096 file, storage::FileSystemOperation::GET_METADATA_FIELD_IS_DIRECTORY |
1097 storage::FileSystemOperation::GET_METADATA_FIELD_SIZE));
1098 EXPECT_FALSE(info().is_directory);
1099 EXPECT_EQ(data_size, info().size);
1100
1101 // Extend the file by truncating it.
1102 int length = 17;
1103 EXPECT_EQ(base::File::FILE_OK, Truncate(file, length));
1104
1105 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
1106 EXPECT_TRUE(change_observer()->HasNoChange());
1107
1108 // Check that its length is now 17 and that it's all zeroes after the test
1109 // data.
1110 EXPECT_EQ(length, GetFileSize("file"));
1111 char data[100];
1112 EXPECT_EQ(length, base::ReadFile(platform_path, data, length));
1113 for (int i = 0; i < length; ++i) {
1114 if (i < static_cast<int>(sizeof(test_data)))
1115 EXPECT_EQ(test_data[i], data[i]);
1116 else
1117 EXPECT_EQ(0, data[i]);
1118 }
1119
1120 // Shorten the file by truncating it.
1121 length = 3;
1122 EXPECT_EQ(base::File::FILE_OK, Truncate(file, length));
1123
1124 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
1125 EXPECT_TRUE(change_observer()->HasNoChange());
1126
1127 // Check that its length is now 3 and that it contains only bits of test data.
1128 EXPECT_EQ(length, GetFileSize("file"));
1129 EXPECT_EQ(length, base::ReadFile(platform_path, data, length));
1130 for (int i = 0; i < length; ++i)
1131 EXPECT_EQ(test_data[i], data[i]);
1132
1133 // Truncate is not a 'read' access. (Here expected access count is 1
1134 // since we made 1 read access for GetMetadata.)
1135 EXPECT_EQ(1, quota_manager_proxy()->notify_storage_accessed_count());
1136 }
1137
1138 TEST_F(FileSystemOperationImplTest, TestTruncateFailureByQuota) {
1139 FileSystemURL dir(CreateDirectory("dir"));
1140 FileSystemURL file(CreateFile("dir/file"));
1141
1142 GrantQuotaForCurrentUsage();
1143 AddQuota(10);
1144
1145 EXPECT_EQ(base::File::FILE_OK, Truncate(file, 10));
1146 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
1147 EXPECT_TRUE(change_observer()->HasNoChange());
1148
1149 EXPECT_EQ(10, GetFileSize("dir/file"));
1150
1151 EXPECT_EQ(base::File::FILE_ERROR_NO_SPACE, Truncate(file, 11));
1152 EXPECT_TRUE(change_observer()->HasNoChange());
1153
1154 EXPECT_EQ(10, GetFileSize("dir/file"));
1155 }
1156
1157 TEST_F(FileSystemOperationImplTest, TestTouchFile) {
1158 FileSystemURL file(CreateFile("file"));
1159 base::FilePath platform_path = PlatformPath("file");
1160
1161 base::File::Info info;
1162 EXPECT_TRUE(base::GetFileInfo(platform_path, &info));
1163 EXPECT_FALSE(info.is_directory);
1164 EXPECT_EQ(0, info.size);
1165 const base::Time last_modified = info.last_modified;
1166 const base::Time last_accessed = info.last_accessed;
1167
1168 const base::Time new_modified_time = base::Time::UnixEpoch();
1169 const base::Time new_accessed_time = new_modified_time +
1170 base::TimeDelta::FromHours(77);
1171 ASSERT_NE(last_modified, new_modified_time);
1172 ASSERT_NE(last_accessed, new_accessed_time);
1173
1174 EXPECT_EQ(base::File::FILE_OK,
1175 TouchFile(file, new_accessed_time, new_modified_time));
1176 EXPECT_TRUE(change_observer()->HasNoChange());
1177
1178 EXPECT_TRUE(base::GetFileInfo(platform_path, &info));
1179 // We compare as time_t here to lower our resolution, to avoid false
1180 // negatives caused by conversion to the local filesystem's native
1181 // representation and back.
1182 EXPECT_EQ(new_modified_time.ToTimeT(), info.last_modified.ToTimeT());
1183 EXPECT_EQ(new_accessed_time.ToTimeT(), info.last_accessed.ToTimeT());
1184 }
1185
1186 TEST_F(FileSystemOperationImplTest, TestCreateSnapshotFile) {
1187 FileSystemURL dir(CreateDirectory("dir"));
1188
1189 // Create a file for the testing.
1190 EXPECT_EQ(base::File::FILE_OK, DirectoryExists(dir));
1191 FileSystemURL file(CreateFile("dir/file"));
1192 EXPECT_EQ(base::File::FILE_OK, FileExists(file));
1193
1194 // See if we can get a 'snapshot' file info for the file.
1195 // Since FileSystemOperationImpl assumes the file exists in the local
1196 // directory it should just returns the same metadata and platform_path
1197 // as the file itself.
1198 EXPECT_EQ(base::File::FILE_OK, CreateSnapshotFile(file));
1199 EXPECT_FALSE(info().is_directory);
1200 EXPECT_EQ(PlatformPath("dir/file"), path());
1201 EXPECT_TRUE(change_observer()->HasNoChange());
1202
1203 // The FileSystemOpration implementation does not create a
1204 // shareable file reference.
1205 EXPECT_EQ(NULL, shareable_file_ref());
1206 }
1207
1208 TEST_F(FileSystemOperationImplTest,
1209 TestMoveSuccessSrcDirRecursiveWithQuota) {
1210 FileSystemURL src(CreateDirectory("src"));
1211 int src_path_cost = GetUsage();
1212
1213 FileSystemURL dest(CreateDirectory("dest"));
1214 FileSystemURL child_file1(CreateFile("src/file1"));
1215 FileSystemURL child_file2(CreateFile("src/file2"));
1216 FileSystemURL child_dir(CreateDirectory("src/dir"));
1217 FileSystemURL grandchild_file1(CreateFile("src/dir/file1"));
1218 FileSystemURL grandchild_file2(CreateFile("src/dir/file2"));
1219
1220 int total_path_cost = GetUsage();
1221 EXPECT_EQ(0, GetDataSizeOnDisk());
1222
1223 EXPECT_EQ(base::File::FILE_OK, Truncate(child_file1, 5000));
1224 EXPECT_EQ(base::File::FILE_OK, Truncate(child_file2, 400));
1225 EXPECT_EQ(base::File::FILE_OK, Truncate(grandchild_file1, 30));
1226 EXPECT_EQ(base::File::FILE_OK, Truncate(grandchild_file2, 2));
1227
1228 const int64_t all_file_size = 5000 + 400 + 30 + 2;
1229 EXPECT_EQ(all_file_size, GetDataSizeOnDisk());
1230 EXPECT_EQ(all_file_size + total_path_cost, GetUsage());
1231
1232 EXPECT_EQ(base::File::FILE_OK,
1233 Move(src, dest, FileSystemOperation::OPTION_NONE));
1234
1235 EXPECT_FALSE(DirectoryExists("src/dir"));
1236 EXPECT_FALSE(FileExists("src/dir/file2"));
1237 EXPECT_TRUE(DirectoryExists("dest/dir"));
1238 EXPECT_TRUE(FileExists("dest/dir/file2"));
1239
1240 EXPECT_EQ(all_file_size, GetDataSizeOnDisk());
1241 EXPECT_EQ(all_file_size + total_path_cost - src_path_cost,
1242 GetUsage());
1243 }
1244
1245 TEST_F(FileSystemOperationImplTest,
1246 TestCopySuccessSrcDirRecursiveWithQuota) {
1247 FileSystemURL src(CreateDirectory("src"));
1248 FileSystemURL dest1(CreateDirectory("dest1"));
1249 FileSystemURL dest2(CreateDirectory("dest2"));
1250
1251 int64_t usage = GetUsage();
1252 FileSystemURL child_file1(CreateFile("src/file1"));
1253 FileSystemURL child_file2(CreateFile("src/file2"));
1254 FileSystemURL child_dir(CreateDirectory("src/dir"));
1255 int64_t child_path_cost = GetUsage() - usage;
1256 usage += child_path_cost;
1257
1258 FileSystemURL grandchild_file1(CreateFile("src/dir/file1"));
1259 FileSystemURL grandchild_file2(CreateFile("src/dir/file2"));
1260 int64_t total_path_cost = GetUsage();
1261 int64_t grandchild_path_cost = total_path_cost - usage;
1262
1263 EXPECT_EQ(0, GetDataSizeOnDisk());
1264
1265 EXPECT_EQ(base::File::FILE_OK, Truncate(child_file1, 8000));
1266 EXPECT_EQ(base::File::FILE_OK, Truncate(child_file2, 700));
1267 EXPECT_EQ(base::File::FILE_OK, Truncate(grandchild_file1, 60));
1268 EXPECT_EQ(base::File::FILE_OK, Truncate(grandchild_file2, 5));
1269
1270 const int64_t child_file_size = 8000 + 700;
1271 const int64_t grandchild_file_size = 60 + 5;
1272 const int64_t all_file_size = child_file_size + grandchild_file_size;
1273 int64_t expected_usage = all_file_size + total_path_cost;
1274
1275 usage = GetUsage();
1276 EXPECT_EQ(all_file_size, GetDataSizeOnDisk());
1277 EXPECT_EQ(expected_usage, usage);
1278
1279 EXPECT_EQ(base::File::FILE_OK,
1280 Copy(src, dest1, FileSystemOperation::OPTION_NONE));
1281
1282 expected_usage += all_file_size + child_path_cost + grandchild_path_cost;
1283 EXPECT_TRUE(DirectoryExists("src/dir"));
1284 EXPECT_TRUE(FileExists("src/dir/file2"));
1285 EXPECT_TRUE(DirectoryExists("dest1/dir"));
1286 EXPECT_TRUE(FileExists("dest1/dir/file2"));
1287
1288 EXPECT_EQ(2 * all_file_size, GetDataSizeOnDisk());
1289 EXPECT_EQ(expected_usage, GetUsage());
1290
1291 EXPECT_EQ(base::File::FILE_OK,
1292 Copy(child_dir, dest2, FileSystemOperation::OPTION_NONE));
1293
1294 expected_usage += grandchild_file_size + grandchild_path_cost;
1295 usage = GetUsage();
1296 EXPECT_EQ(2 * child_file_size + 3 * grandchild_file_size,
1297 GetDataSizeOnDisk());
1298 EXPECT_EQ(expected_usage, usage);
1299 }
1300
1301 TEST_F(FileSystemOperationImplTest,
1302 TestCopySuccessSrcFileWithDifferentFileSize) {
1303 FileSystemURL src_file(CreateFile("src"));
1304 FileSystemURL dest_file(CreateFile("dest"));
1305
1306 EXPECT_EQ(base::File::FILE_OK, Truncate(dest_file, 6));
1307 EXPECT_EQ(base::File::FILE_OK,
1308 Copy(src_file, dest_file, FileSystemOperation::OPTION_NONE));
1309 EXPECT_EQ(0, GetFileSize("dest"));
1310 }
1311
1312 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698