[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / content / browser / fileapi / file_system_operation_impl_unittest.cc
blobb000da2d1e7de5380ec2fecc0f79044ec8a88a67
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.
5 #include "storage/browser/fileapi/file_system_operation_impl.h"
7 #include "base/bind.h"
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/run_loop.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/thread_task_runner_handle.h"
16 #include "content/browser/fileapi/mock_file_change_observer.h"
17 #include "content/browser/fileapi/mock_file_update_observer.h"
18 #include "content/browser/quota/mock_quota_manager.h"
19 #include "content/browser/quota/mock_quota_manager_proxy.h"
20 #include "content/public/test/async_file_test_helper.h"
21 #include "content/public/test/sandbox_file_system_test_helper.h"
22 #include "storage/browser/blob/shareable_file_reference.h"
23 #include "storage/browser/fileapi/file_system_context.h"
24 #include "storage/browser/fileapi/file_system_file_util.h"
25 #include "storage/browser/fileapi/file_system_operation_context.h"
26 #include "storage/browser/fileapi/file_system_operation_runner.h"
27 #include "storage/browser/fileapi/sandbox_file_system_backend.h"
28 #include "storage/browser/quota/quota_manager.h"
29 #include "storage/browser/quota/quota_manager_proxy.h"
30 #include "storage/common/fileapi/file_system_util.h"
31 #include "testing/gtest/include/gtest/gtest.h"
32 #include "url/gurl.h"
34 using content::AsyncFileTestHelper;
35 using storage::FileSystemOperation;
36 using storage::FileSystemOperationContext;
37 using storage::FileSystemOperationRunner;
38 using storage::FileSystemURL;
39 using storage::QuotaManager;
40 using storage::QuotaManagerProxy;
41 using storage::ShareableFileReference;
43 namespace content {
45 // Test class for FileSystemOperationImpl.
46 class FileSystemOperationImplTest
47 : public testing::Test {
48 public:
49 FileSystemOperationImplTest() : weak_factory_(this) {}
51 protected:
52 void SetUp() override {
53 EXPECT_TRUE(base_.CreateUniqueTempDir());
54 change_observers_ =
55 storage::MockFileChangeObserver::CreateList(&change_observer_);
56 update_observers_ =
57 storage::MockFileUpdateObserver::CreateList(&update_observer_);
59 base::FilePath base_dir = base_.path().AppendASCII("filesystem");
60 quota_manager_ =
61 new MockQuotaManager(false /* is_incognito */, base_dir,
62 base::ThreadTaskRunnerHandle::Get().get(),
63 base::ThreadTaskRunnerHandle::Get().get(),
64 NULL /* special storage policy */);
65 quota_manager_proxy_ = new MockQuotaManagerProxy(
66 quota_manager(), base::ThreadTaskRunnerHandle::Get().get());
67 sandbox_file_system_.SetUp(base_dir, quota_manager_proxy_.get());
68 sandbox_file_system_.AddFileChangeObserver(&change_observer_);
69 sandbox_file_system_.AddFileUpdateObserver(&update_observer_);
70 update_observer_.Disable();
73 void TearDown() override {
74 // Let the client go away before dropping a ref of the quota manager proxy.
75 quota_manager_proxy()->SimulateQuotaManagerDestroyed();
76 quota_manager_ = NULL;
77 quota_manager_proxy_ = NULL;
78 sandbox_file_system_.TearDown();
81 FileSystemOperationRunner* operation_runner() {
82 return sandbox_file_system_.operation_runner();
85 const base::File::Info& info() const { return info_; }
86 const base::FilePath& path() const { return path_; }
87 const std::vector<storage::DirectoryEntry>& entries() const {
88 return entries_;
91 const ShareableFileReference* shareable_file_ref() const {
92 return shareable_file_ref_.get();
95 MockQuotaManager* quota_manager() {
96 return static_cast<MockQuotaManager*>(quota_manager_.get());
99 MockQuotaManagerProxy* quota_manager_proxy() {
100 return static_cast<MockQuotaManagerProxy*>(
101 quota_manager_proxy_.get());
104 storage::FileSystemFileUtil* file_util() {
105 return sandbox_file_system_.file_util();
108 storage::MockFileChangeObserver* change_observer() {
109 return &change_observer_;
112 scoped_ptr<FileSystemOperationContext> NewContext() {
113 FileSystemOperationContext* context =
114 sandbox_file_system_.NewOperationContext();
115 // Grant enough quota for all test cases.
116 context->set_allowed_bytes_growth(1000000);
117 return make_scoped_ptr(context);
120 FileSystemURL URLForPath(const std::string& path) const {
121 return sandbox_file_system_.CreateURLFromUTF8(path);
124 base::FilePath PlatformPath(const std::string& path) {
125 return sandbox_file_system_.GetLocalPath(
126 base::FilePath::FromUTF8Unsafe(path));
129 bool FileExists(const std::string& path) {
130 return AsyncFileTestHelper::FileExists(
131 sandbox_file_system_.file_system_context(), URLForPath(path),
132 AsyncFileTestHelper::kDontCheckSize);
135 bool DirectoryExists(const std::string& path) {
136 return AsyncFileTestHelper::DirectoryExists(
137 sandbox_file_system_.file_system_context(), URLForPath(path));
140 FileSystemURL CreateFile(const std::string& path) {
141 FileSystemURL url = URLForPath(path);
142 bool created = false;
143 EXPECT_EQ(base::File::FILE_OK,
144 file_util()->EnsureFileExists(NewContext().get(),
145 url, &created));
146 EXPECT_TRUE(created);
147 return url;
150 FileSystemURL CreateDirectory(const std::string& path) {
151 FileSystemURL url = URLForPath(path);
152 EXPECT_EQ(base::File::FILE_OK,
153 file_util()->CreateDirectory(NewContext().get(), url,
154 false /* exclusive */, true));
155 return url;
158 int64 GetFileSize(const std::string& path) {
159 base::File::Info info;
160 EXPECT_TRUE(base::GetFileInfo(PlatformPath(path), &info));
161 return info.size;
164 // Callbacks for recording test results.
165 FileSystemOperation::StatusCallback RecordStatusCallback(
166 const base::Closure& closure,
167 base::File::Error* status) {
168 return base::Bind(&FileSystemOperationImplTest::DidFinish,
169 weak_factory_.GetWeakPtr(),
170 closure,
171 status);
174 FileSystemOperation::ReadDirectoryCallback RecordReadDirectoryCallback(
175 const base::Closure& closure,
176 base::File::Error* status) {
177 return base::Bind(&FileSystemOperationImplTest::DidReadDirectory,
178 weak_factory_.GetWeakPtr(),
179 closure,
180 status);
183 FileSystemOperation::GetMetadataCallback RecordMetadataCallback(
184 const base::Closure& closure,
185 base::File::Error* status) {
186 return base::Bind(&FileSystemOperationImplTest::DidGetMetadata,
187 weak_factory_.GetWeakPtr(),
188 closure,
189 status);
192 FileSystemOperation::SnapshotFileCallback RecordSnapshotFileCallback(
193 const base::Closure& closure,
194 base::File::Error* status) {
195 return base::Bind(&FileSystemOperationImplTest::DidCreateSnapshotFile,
196 weak_factory_.GetWeakPtr(),
197 closure,
198 status);
201 void DidFinish(const base::Closure& closure,
202 base::File::Error* status,
203 base::File::Error actual) {
204 *status = actual;
205 closure.Run();
208 void DidReadDirectory(const base::Closure& closure,
209 base::File::Error* status,
210 base::File::Error actual,
211 const std::vector<storage::DirectoryEntry>& entries,
212 bool /* has_more */) {
213 entries_ = entries;
214 *status = actual;
215 closure.Run();
218 void DidGetMetadata(const base::Closure& closure,
219 base::File::Error* status,
220 base::File::Error actual,
221 const base::File::Info& info) {
222 info_ = info;
223 *status = actual;
224 closure.Run();
227 void DidCreateSnapshotFile(
228 const base::Closure& closure,
229 base::File::Error* status,
230 base::File::Error actual,
231 const base::File::Info& info,
232 const base::FilePath& platform_path,
233 const scoped_refptr<ShareableFileReference>& shareable_file_ref) {
234 info_ = info;
235 path_ = platform_path;
236 *status = actual;
237 shareable_file_ref_ = shareable_file_ref;
238 closure.Run();
241 int64 GetDataSizeOnDisk() {
242 return sandbox_file_system_.ComputeCurrentOriginUsage() -
243 sandbox_file_system_.ComputeCurrentDirectoryDatabaseUsage();
246 void GetUsageAndQuota(int64* usage, int64* quota) {
247 storage::QuotaStatusCode status =
248 AsyncFileTestHelper::GetUsageAndQuota(quota_manager_.get(),
249 sandbox_file_system_.origin(),
250 sandbox_file_system_.type(),
251 usage,
252 quota);
253 base::RunLoop().RunUntilIdle();
254 ASSERT_EQ(storage::kQuotaStatusOk, status);
257 int64 ComputePathCost(const FileSystemURL& url) {
258 int64 base_usage;
259 GetUsageAndQuota(&base_usage, NULL);
261 AsyncFileTestHelper::CreateFile(
262 sandbox_file_system_.file_system_context(), url);
263 EXPECT_EQ(base::File::FILE_OK, Remove(url, false /* recursive */));
265 change_observer()->ResetCount();
267 int64 total_usage;
268 GetUsageAndQuota(&total_usage, NULL);
269 return total_usage - base_usage;
272 void GrantQuotaForCurrentUsage() {
273 int64 usage;
274 GetUsageAndQuota(&usage, NULL);
275 quota_manager()->SetQuota(sandbox_file_system_.origin(),
276 sandbox_file_system_.storage_type(),
277 usage);
280 int64 GetUsage() {
281 int64 usage = 0;
282 GetUsageAndQuota(&usage, NULL);
283 return usage;
286 void AddQuota(int64 quota_delta) {
287 int64 quota;
288 GetUsageAndQuota(NULL, &quota);
289 quota_manager()->SetQuota(sandbox_file_system_.origin(),
290 sandbox_file_system_.storage_type(),
291 quota + quota_delta);
294 base::File::Error Move(
295 const FileSystemURL& src,
296 const FileSystemURL& dest,
297 storage::FileSystemOperation::CopyOrMoveOption option) {
298 base::File::Error status;
299 base::RunLoop run_loop;
300 update_observer_.Enable();
301 operation_runner()->Move(
302 src,
303 dest,
304 option,
305 RecordStatusCallback(run_loop.QuitClosure(), &status));
306 run_loop.Run();
307 update_observer_.Disable();
308 return status;
311 base::File::Error Copy(
312 const FileSystemURL& src,
313 const FileSystemURL& dest,
314 storage::FileSystemOperation::CopyOrMoveOption option) {
315 base::File::Error status;
316 base::RunLoop run_loop;
317 update_observer_.Enable();
318 operation_runner()->Copy(
319 src, dest, option, storage::FileSystemOperation::ERROR_BEHAVIOR_ABORT,
320 FileSystemOperationRunner::CopyProgressCallback(),
321 RecordStatusCallback(run_loop.QuitClosure(), &status));
322 run_loop.Run();
323 update_observer_.Disable();
324 return status;
327 base::File::Error CopyInForeignFile(const base::FilePath& src,
328 const FileSystemURL& dest) {
329 base::File::Error status;
330 base::RunLoop run_loop;
331 update_observer_.Enable();
332 operation_runner()->CopyInForeignFile(
333 src, dest, RecordStatusCallback(run_loop.QuitClosure(), &status));
334 run_loop.Run();
335 update_observer_.Disable();
336 return status;
339 base::File::Error Truncate(const FileSystemURL& url, int size) {
340 base::File::Error status;
341 base::RunLoop run_loop;
342 update_observer_.Enable();
343 operation_runner()->Truncate(
344 url, size, RecordStatusCallback(run_loop.QuitClosure(), &status));
345 run_loop.Run();
346 update_observer_.Disable();
347 return status;
350 base::File::Error CreateFile(const FileSystemURL& url, bool exclusive) {
351 base::File::Error status;
352 base::RunLoop run_loop;
353 update_observer_.Enable();
354 operation_runner()->CreateFile(
355 url, exclusive, RecordStatusCallback(run_loop.QuitClosure(), &status));
356 run_loop.Run();
357 update_observer_.Disable();
358 return status;
361 base::File::Error Remove(const FileSystemURL& url, bool recursive) {
362 base::File::Error status;
363 base::RunLoop run_loop;
364 update_observer_.Enable();
365 operation_runner()->Remove(
366 url, recursive, RecordStatusCallback(run_loop.QuitClosure(), &status));
367 run_loop.Run();
368 update_observer_.Disable();
369 return status;
372 base::File::Error CreateDirectory(const FileSystemURL& url,
373 bool exclusive,
374 bool recursive) {
375 base::File::Error status;
376 base::RunLoop run_loop;
377 update_observer_.Enable();
378 operation_runner()->CreateDirectory(
379 url,
380 exclusive,
381 recursive,
382 RecordStatusCallback(run_loop.QuitClosure(), &status));
383 run_loop.Run();
384 update_observer_.Disable();
385 return status;
388 base::File::Error GetMetadata(const FileSystemURL& url) {
389 base::File::Error status;
390 base::RunLoop run_loop;
391 update_observer_.Enable();
392 operation_runner()->GetMetadata(
393 url, RecordMetadataCallback(run_loop.QuitClosure(), &status));
394 run_loop.Run();
395 update_observer_.Disable();
396 return status;
399 base::File::Error ReadDirectory(const FileSystemURL& url) {
400 base::File::Error status;
401 base::RunLoop run_loop;
402 update_observer_.Enable();
403 operation_runner()->ReadDirectory(
404 url, RecordReadDirectoryCallback(run_loop.QuitClosure(), &status));
405 run_loop.Run();
406 update_observer_.Disable();
407 return status;
410 base::File::Error CreateSnapshotFile(const FileSystemURL& url) {
411 base::File::Error status;
412 base::RunLoop run_loop;
413 update_observer_.Enable();
414 operation_runner()->CreateSnapshotFile(
415 url, RecordSnapshotFileCallback(run_loop.QuitClosure(), &status));
416 run_loop.Run();
417 update_observer_.Disable();
418 return status;
421 base::File::Error FileExists(const FileSystemURL& url) {
422 base::File::Error status;
423 base::RunLoop run_loop;
424 update_observer_.Enable();
425 operation_runner()->FileExists(
426 url, RecordStatusCallback(run_loop.QuitClosure(), &status));
427 run_loop.Run();
428 update_observer_.Disable();
429 return status;
432 base::File::Error DirectoryExists(const FileSystemURL& url) {
433 base::File::Error status;
434 base::RunLoop run_loop;
435 update_observer_.Enable();
436 operation_runner()->DirectoryExists(
437 url, RecordStatusCallback(run_loop.QuitClosure(), &status));
438 run_loop.Run();
439 update_observer_.Disable();
440 return status;
443 base::File::Error TouchFile(const FileSystemURL& url,
444 const base::Time& last_access_time,
445 const base::Time& last_modified_time) {
446 base::File::Error status;
447 base::RunLoop run_loop;
448 update_observer_.Enable();
449 operation_runner()->TouchFile(
450 url,
451 last_access_time,
452 last_modified_time,
453 RecordStatusCallback(run_loop.QuitClosure(), &status));
454 run_loop.Run();
455 update_observer_.Disable();
456 return status;
459 private:
460 base::MessageLoopForIO message_loop_;
461 scoped_refptr<QuotaManager> quota_manager_;
462 scoped_refptr<QuotaManagerProxy> quota_manager_proxy_;
464 // Common temp base for nondestructive uses.
465 base::ScopedTempDir base_;
467 SandboxFileSystemTestHelper sandbox_file_system_;
469 // For post-operation status.
470 base::File::Info info_;
471 base::FilePath path_;
472 std::vector<storage::DirectoryEntry> entries_;
473 scoped_refptr<ShareableFileReference> shareable_file_ref_;
475 storage::MockFileChangeObserver change_observer_;
476 storage::ChangeObserverList change_observers_;
477 storage::MockFileUpdateObserver update_observer_;
478 storage::UpdateObserverList update_observers_;
480 base::WeakPtrFactory<FileSystemOperationImplTest> weak_factory_;
482 DISALLOW_COPY_AND_ASSIGN(FileSystemOperationImplTest);
485 TEST_F(FileSystemOperationImplTest, TestMoveFailureSrcDoesntExist) {
486 change_observer()->ResetCount();
487 EXPECT_EQ(
488 base::File::FILE_ERROR_NOT_FOUND,
489 Move(URLForPath("a"), URLForPath("b"), FileSystemOperation::OPTION_NONE));
490 EXPECT_TRUE(change_observer()->HasNoChange());
493 TEST_F(FileSystemOperationImplTest, TestMoveFailureContainsPath) {
494 FileSystemURL src_dir(CreateDirectory("src"));
495 FileSystemURL dest_dir(CreateDirectory("src/dest"));
497 EXPECT_EQ(base::File::FILE_ERROR_INVALID_OPERATION,
498 Move(src_dir, dest_dir, FileSystemOperation::OPTION_NONE));
499 EXPECT_TRUE(change_observer()->HasNoChange());
502 TEST_F(FileSystemOperationImplTest, TestMoveFailureSrcDirExistsDestFile) {
503 // Src exists and is dir. Dest is a file.
504 FileSystemURL src_dir(CreateDirectory("src"));
505 FileSystemURL dest_dir(CreateDirectory("dest"));
506 FileSystemURL dest_file(CreateFile("dest/file"));
508 EXPECT_EQ(base::File::FILE_ERROR_INVALID_OPERATION,
509 Move(src_dir, dest_file, FileSystemOperation::OPTION_NONE));
510 EXPECT_TRUE(change_observer()->HasNoChange());
513 TEST_F(FileSystemOperationImplTest,
514 TestMoveFailureSrcFileExistsDestNonEmptyDir) {
515 // Src exists and is a directory. Dest is a non-empty directory.
516 FileSystemURL src_dir(CreateDirectory("src"));
517 FileSystemURL dest_dir(CreateDirectory("dest"));
518 FileSystemURL dest_file(CreateFile("dest/file"));
520 EXPECT_EQ(base::File::FILE_ERROR_NOT_EMPTY,
521 Move(src_dir, dest_dir, FileSystemOperation::OPTION_NONE));
522 EXPECT_TRUE(change_observer()->HasNoChange());
525 TEST_F(FileSystemOperationImplTest, TestMoveFailureSrcFileExistsDestDir) {
526 // Src exists and is a file. Dest is a directory.
527 FileSystemURL src_dir(CreateDirectory("src"));
528 FileSystemURL src_file(CreateFile("src/file"));
529 FileSystemURL dest_dir(CreateDirectory("dest"));
531 EXPECT_EQ(base::File::FILE_ERROR_INVALID_OPERATION,
532 Move(src_file, dest_dir, FileSystemOperation::OPTION_NONE));
533 EXPECT_TRUE(change_observer()->HasNoChange());
536 TEST_F(FileSystemOperationImplTest, TestMoveFailureDestParentDoesntExist) {
537 // Dest. parent path does not exist.
538 FileSystemURL src_dir(CreateDirectory("src"));
539 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
540 Move(src_dir,
541 URLForPath("nonexistent/deset"),
542 FileSystemOperation::OPTION_NONE));
543 EXPECT_TRUE(change_observer()->HasNoChange());
546 TEST_F(FileSystemOperationImplTest, TestMoveSuccessSrcFileAndOverwrite) {
547 FileSystemURL src_file(CreateFile("src"));
548 FileSystemURL dest_file(CreateFile("dest"));
550 EXPECT_EQ(base::File::FILE_OK,
551 Move(src_file, dest_file, FileSystemOperation::OPTION_NONE));
552 EXPECT_TRUE(FileExists("dest"));
554 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
555 EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count());
556 EXPECT_TRUE(change_observer()->HasNoChange());
558 EXPECT_EQ(1, quota_manager_proxy()->notify_storage_accessed_count());
561 TEST_F(FileSystemOperationImplTest, TestMoveSuccessSrcFileAndNew) {
562 FileSystemURL src_file(CreateFile("src"));
564 EXPECT_EQ(
565 base::File::FILE_OK,
566 Move(src_file, URLForPath("new"), FileSystemOperation::OPTION_NONE));
567 EXPECT_TRUE(FileExists("new"));
569 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_from_count());
570 EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count());
571 EXPECT_TRUE(change_observer()->HasNoChange());
574 TEST_F(FileSystemOperationImplTest, TestMoveSuccessSrcDirAndOverwrite) {
575 FileSystemURL src_dir(CreateDirectory("src"));
576 FileSystemURL dest_dir(CreateDirectory("dest"));
578 EXPECT_EQ(base::File::FILE_OK,
579 Move(src_dir, dest_dir, FileSystemOperation::OPTION_NONE));
580 EXPECT_FALSE(DirectoryExists("src"));
582 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
583 EXPECT_EQ(2, change_observer()->get_and_reset_remove_directory_count());
584 EXPECT_TRUE(change_observer()->HasNoChange());
586 // Make sure we've overwritten but not moved the source under the |dest_dir|.
587 EXPECT_TRUE(DirectoryExists("dest"));
588 EXPECT_FALSE(DirectoryExists("dest/src"));
591 TEST_F(FileSystemOperationImplTest, TestMoveSuccessSrcDirAndNew) {
592 FileSystemURL src_dir(CreateDirectory("src"));
593 FileSystemURL dest_dir(CreateDirectory("dest"));
595 EXPECT_EQ(
596 base::File::FILE_OK,
597 Move(src_dir, URLForPath("dest/new"), FileSystemOperation::OPTION_NONE));
598 EXPECT_FALSE(DirectoryExists("src"));
599 EXPECT_TRUE(DirectoryExists("dest/new"));
601 EXPECT_EQ(1, change_observer()->get_and_reset_remove_directory_count());
602 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
603 EXPECT_TRUE(change_observer()->HasNoChange());
606 TEST_F(FileSystemOperationImplTest, TestMoveSuccessSrcDirRecursive) {
607 FileSystemURL src_dir(CreateDirectory("src"));
608 CreateDirectory("src/dir");
609 CreateFile("src/dir/sub");
611 FileSystemURL dest_dir(CreateDirectory("dest"));
613 EXPECT_EQ(base::File::FILE_OK,
614 Move(src_dir, dest_dir, FileSystemOperation::OPTION_NONE));
615 EXPECT_TRUE(DirectoryExists("dest/dir"));
616 EXPECT_TRUE(FileExists("dest/dir/sub"));
618 EXPECT_EQ(3, change_observer()->get_and_reset_remove_directory_count());
619 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
620 EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count());
621 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_from_count());
622 EXPECT_TRUE(change_observer()->HasNoChange());
625 TEST_F(FileSystemOperationImplTest, TestMoveSuccessSamePath) {
626 FileSystemURL src_dir(CreateDirectory("src"));
627 CreateDirectory("src/dir");
628 CreateFile("src/dir/sub");
630 EXPECT_EQ(base::File::FILE_OK,
631 Move(src_dir, src_dir, FileSystemOperation::OPTION_NONE));
632 EXPECT_TRUE(DirectoryExists("src/dir"));
633 EXPECT_TRUE(FileExists("src/dir/sub"));
635 EXPECT_EQ(0, change_observer()->get_and_reset_remove_directory_count());
636 EXPECT_EQ(0, change_observer()->get_and_reset_create_directory_count());
637 EXPECT_EQ(0, change_observer()->get_and_reset_remove_file_count());
638 EXPECT_EQ(0, change_observer()->get_and_reset_create_file_from_count());
639 EXPECT_TRUE(change_observer()->HasNoChange());
642 TEST_F(FileSystemOperationImplTest, TestCopyFailureSrcDoesntExist) {
643 EXPECT_EQ(
644 base::File::FILE_ERROR_NOT_FOUND,
645 Copy(URLForPath("a"), URLForPath("b"), FileSystemOperation::OPTION_NONE));
646 EXPECT_TRUE(change_observer()->HasNoChange());
649 TEST_F(FileSystemOperationImplTest, TestCopyFailureContainsPath) {
650 FileSystemURL src_dir(CreateDirectory("src"));
651 FileSystemURL dest_dir(CreateDirectory("src/dir"));
653 EXPECT_EQ(base::File::FILE_ERROR_INVALID_OPERATION,
654 Copy(src_dir, dest_dir, FileSystemOperation::OPTION_NONE));
655 EXPECT_TRUE(change_observer()->HasNoChange());
658 TEST_F(FileSystemOperationImplTest, TestCopyFailureSrcDirExistsDestFile) {
659 // Src exists and is dir. Dest is a file.
660 FileSystemURL src_dir(CreateDirectory("src"));
661 FileSystemURL dest_dir(CreateDirectory("dest"));
662 FileSystemURL dest_file(CreateFile("dest/file"));
664 EXPECT_EQ(base::File::FILE_ERROR_INVALID_OPERATION,
665 Copy(src_dir, dest_file, FileSystemOperation::OPTION_NONE));
666 EXPECT_TRUE(change_observer()->HasNoChange());
669 TEST_F(FileSystemOperationImplTest,
670 TestCopyFailureSrcFileExistsDestNonEmptyDir) {
671 // Src exists and is a directory. Dest is a non-empty directory.
672 FileSystemURL src_dir(CreateDirectory("src"));
673 FileSystemURL dest_dir(CreateDirectory("dest"));
674 FileSystemURL dest_file(CreateFile("dest/file"));
676 EXPECT_EQ(base::File::FILE_ERROR_NOT_EMPTY,
677 Copy(src_dir, dest_dir, FileSystemOperation::OPTION_NONE));
678 EXPECT_TRUE(change_observer()->HasNoChange());
681 TEST_F(FileSystemOperationImplTest, TestCopyFailureSrcFileExistsDestDir) {
682 // Src exists and is a file. Dest is a directory.
683 FileSystemURL src_file(CreateFile("src"));
684 FileSystemURL dest_dir(CreateDirectory("dest"));
686 EXPECT_EQ(base::File::FILE_ERROR_INVALID_OPERATION,
687 Copy(src_file, dest_dir, FileSystemOperation::OPTION_NONE));
688 EXPECT_TRUE(change_observer()->HasNoChange());
691 TEST_F(FileSystemOperationImplTest, TestCopyFailureDestParentDoesntExist) {
692 // Dest. parent path does not exist.
693 FileSystemURL src_dir(CreateDirectory("src"));
695 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
696 Copy(src_dir,
697 URLForPath("nonexistent/dest"),
698 FileSystemOperation::OPTION_NONE));
699 EXPECT_TRUE(change_observer()->HasNoChange());
702 TEST_F(FileSystemOperationImplTest, TestCopyFailureByQuota) {
703 FileSystemURL src_dir(CreateDirectory("src"));
704 FileSystemURL src_file(CreateFile("src/file"));
705 FileSystemURL dest_dir(CreateDirectory("dest"));
706 EXPECT_EQ(base::File::FILE_OK, Truncate(src_file, 6));
707 EXPECT_EQ(6, GetFileSize("src/file"));
709 FileSystemURL dest_file(URLForPath("dest/file"));
710 int64 dest_path_cost = ComputePathCost(dest_file);
711 GrantQuotaForCurrentUsage();
712 AddQuota(6 + dest_path_cost - 1);
714 EXPECT_EQ(base::File::FILE_ERROR_NO_SPACE,
715 Copy(src_file, dest_file, FileSystemOperation::OPTION_NONE));
716 EXPECT_FALSE(FileExists("dest/file"));
719 TEST_F(FileSystemOperationImplTest, TestCopySuccessSrcFileAndOverwrite) {
720 FileSystemURL src_file(CreateFile("src"));
721 FileSystemURL dest_file(CreateFile("dest"));
723 EXPECT_EQ(base::File::FILE_OK,
724 Copy(src_file, dest_file, FileSystemOperation::OPTION_NONE));
726 EXPECT_TRUE(FileExists("dest"));
727 EXPECT_EQ(4, quota_manager_proxy()->notify_storage_accessed_count());
728 EXPECT_EQ(2, change_observer()->get_and_reset_modify_file_count());
730 EXPECT_TRUE(change_observer()->HasNoChange());
733 TEST_F(FileSystemOperationImplTest, TestCopySuccessSrcFileAndNew) {
734 FileSystemURL src_file(CreateFile("src"));
736 EXPECT_EQ(
737 base::File::FILE_OK,
738 Copy(src_file, URLForPath("new"), FileSystemOperation::OPTION_NONE));
739 EXPECT_TRUE(FileExists("new"));
740 EXPECT_EQ(4, quota_manager_proxy()->notify_storage_accessed_count());
742 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
743 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
744 EXPECT_TRUE(change_observer()->HasNoChange());
747 TEST_F(FileSystemOperationImplTest, TestCopySuccessSrcDirAndOverwrite) {
748 FileSystemURL src_dir(CreateDirectory("src"));
749 FileSystemURL dest_dir(CreateDirectory("dest"));
751 EXPECT_EQ(base::File::FILE_OK,
752 Copy(src_dir, dest_dir, FileSystemOperation::OPTION_NONE));
754 // Make sure we've overwritten but not copied the source under the |dest_dir|.
755 EXPECT_TRUE(DirectoryExists("dest"));
756 EXPECT_FALSE(DirectoryExists("dest/src"));
757 EXPECT_GE(quota_manager_proxy()->notify_storage_accessed_count(), 3);
759 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
760 EXPECT_EQ(1, change_observer()->get_and_reset_remove_directory_count());
761 EXPECT_TRUE(change_observer()->HasNoChange());
764 TEST_F(FileSystemOperationImplTest, TestCopySuccessSrcDirAndNew) {
765 FileSystemURL src_dir(CreateDirectory("src"));
766 FileSystemURL dest_dir_new(URLForPath("dest"));
768 EXPECT_EQ(base::File::FILE_OK,
769 Copy(src_dir, dest_dir_new, FileSystemOperation::OPTION_NONE));
770 EXPECT_TRUE(DirectoryExists("dest"));
771 EXPECT_GE(quota_manager_proxy()->notify_storage_accessed_count(), 2);
773 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
774 EXPECT_TRUE(change_observer()->HasNoChange());
777 TEST_F(FileSystemOperationImplTest, TestCopySuccessSrcDirRecursive) {
778 FileSystemURL src_dir(CreateDirectory("src"));
779 CreateDirectory("src/dir");
780 CreateFile("src/dir/sub");
782 FileSystemURL dest_dir(CreateDirectory("dest"));
784 EXPECT_EQ(base::File::FILE_OK,
785 Copy(src_dir, dest_dir, FileSystemOperation::OPTION_NONE));
787 EXPECT_TRUE(DirectoryExists("dest/dir"));
788 EXPECT_TRUE(FileExists("dest/dir/sub"));
790 // For recursive copy we may record multiple read access.
791 EXPECT_GE(quota_manager_proxy()->notify_storage_accessed_count(), 1);
793 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
794 EXPECT_EQ(1, change_observer()->get_and_reset_remove_directory_count());
795 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
796 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
797 EXPECT_TRUE(change_observer()->HasNoChange());
800 TEST_F(FileSystemOperationImplTest, TestCopySuccessSamePath) {
801 FileSystemURL src_dir(CreateDirectory("src"));
802 CreateDirectory("src/dir");
803 CreateFile("src/dir/sub");
805 EXPECT_EQ(base::File::FILE_OK,
806 Copy(src_dir, src_dir, FileSystemOperation::OPTION_NONE));
808 EXPECT_TRUE(DirectoryExists("src/dir"));
809 EXPECT_TRUE(FileExists("src/dir/sub"));
811 EXPECT_EQ(0, change_observer()->get_and_reset_create_directory_count());
812 EXPECT_EQ(0, change_observer()->get_and_reset_remove_file_count());
813 EXPECT_EQ(0, change_observer()->get_and_reset_create_file_from_count());
814 EXPECT_TRUE(change_observer()->HasNoChange());
817 TEST_F(FileSystemOperationImplTest, TestCopyInForeignFileSuccess) {
818 base::FilePath src_local_disk_file_path;
819 base::CreateTemporaryFile(&src_local_disk_file_path);
820 const char test_data[] = "foo";
821 int data_size = arraysize(test_data);
822 base::WriteFile(src_local_disk_file_path, test_data, data_size);
824 FileSystemURL dest_dir(CreateDirectory("dest"));
826 int64 before_usage;
827 GetUsageAndQuota(&before_usage, NULL);
829 // Check that the file copied and corresponding usage increased.
830 EXPECT_EQ(
831 base::File::FILE_OK,
832 CopyInForeignFile(src_local_disk_file_path, URLForPath("dest/file")));
834 EXPECT_EQ(1, change_observer()->create_file_count());
835 EXPECT_TRUE(FileExists("dest/file"));
836 int64 after_usage;
837 GetUsageAndQuota(&after_usage, NULL);
838 EXPECT_GT(after_usage, before_usage);
840 // Compare contents of src and copied file.
841 char buffer[100];
842 EXPECT_EQ(data_size, base::ReadFile(PlatformPath("dest/file"),
843 buffer, data_size));
844 for (int i = 0; i < data_size; ++i)
845 EXPECT_EQ(test_data[i], buffer[i]);
848 TEST_F(FileSystemOperationImplTest, TestCopyInForeignFileFailureByQuota) {
849 base::FilePath src_local_disk_file_path;
850 base::CreateTemporaryFile(&src_local_disk_file_path);
851 const char test_data[] = "foo";
852 base::WriteFile(src_local_disk_file_path, test_data, arraysize(test_data));
854 FileSystemURL dest_dir(CreateDirectory("dest"));
856 GrantQuotaForCurrentUsage();
857 EXPECT_EQ(
858 base::File::FILE_ERROR_NO_SPACE,
859 CopyInForeignFile(src_local_disk_file_path, URLForPath("dest/file")));
861 EXPECT_FALSE(FileExists("dest/file"));
862 EXPECT_EQ(0, change_observer()->create_file_count());
865 TEST_F(FileSystemOperationImplTest, TestCreateFileFailure) {
866 // Already existing file and exclusive true.
867 FileSystemURL file(CreateFile("file"));
868 EXPECT_EQ(base::File::FILE_ERROR_EXISTS, CreateFile(file, true));
869 EXPECT_TRUE(change_observer()->HasNoChange());
872 TEST_F(FileSystemOperationImplTest, TestCreateFileSuccessFileExists) {
873 // Already existing file and exclusive false.
874 FileSystemURL file(CreateFile("file"));
875 EXPECT_EQ(base::File::FILE_OK, CreateFile(file, false));
876 EXPECT_TRUE(FileExists("file"));
878 // The file was already there; did nothing.
879 EXPECT_TRUE(change_observer()->HasNoChange());
882 TEST_F(FileSystemOperationImplTest, TestCreateFileSuccessExclusive) {
883 // File doesn't exist but exclusive is true.
884 EXPECT_EQ(base::File::FILE_OK, CreateFile(URLForPath("new"), true));
885 EXPECT_TRUE(FileExists("new"));
886 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
889 TEST_F(FileSystemOperationImplTest, TestCreateFileSuccessFileDoesntExist) {
890 // Non existing file.
891 EXPECT_EQ(base::File::FILE_OK, CreateFile(URLForPath("nonexistent"), false));
892 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
895 TEST_F(FileSystemOperationImplTest,
896 TestCreateDirFailureDestParentDoesntExist) {
897 // Dest. parent path does not exist.
898 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
899 CreateDirectory(URLForPath("nonexistent/dir"), false, false));
900 EXPECT_TRUE(change_observer()->HasNoChange());
903 TEST_F(FileSystemOperationImplTest, TestCreateDirFailureDirExists) {
904 // Exclusive and dir existing at path.
905 FileSystemURL dir(CreateDirectory("dir"));
906 EXPECT_EQ(base::File::FILE_ERROR_EXISTS, CreateDirectory(dir, true, false));
907 EXPECT_TRUE(change_observer()->HasNoChange());
910 TEST_F(FileSystemOperationImplTest, TestCreateDirFailureFileExists) {
911 // Exclusive true and file existing at path.
912 FileSystemURL file(CreateFile("file"));
913 EXPECT_EQ(base::File::FILE_ERROR_EXISTS, CreateDirectory(file, true, false));
914 EXPECT_TRUE(change_observer()->HasNoChange());
917 TEST_F(FileSystemOperationImplTest, TestCreateDirSuccess) {
918 // Dir exists and exclusive is false.
919 FileSystemURL dir(CreateDirectory("dir"));
920 EXPECT_EQ(base::File::FILE_OK, CreateDirectory(dir, false, false));
921 EXPECT_TRUE(change_observer()->HasNoChange());
923 // Dir doesn't exist.
924 EXPECT_EQ(base::File::FILE_OK,
925 CreateDirectory(URLForPath("new"), false, false));
926 EXPECT_TRUE(DirectoryExists("new"));
927 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
930 TEST_F(FileSystemOperationImplTest, TestCreateDirSuccessExclusive) {
931 // Dir doesn't exist.
932 EXPECT_EQ(base::File::FILE_OK,
933 CreateDirectory(URLForPath("new"), true, false));
934 EXPECT_TRUE(DirectoryExists("new"));
935 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
936 EXPECT_TRUE(change_observer()->HasNoChange());
939 TEST_F(FileSystemOperationImplTest, TestExistsAndMetadataFailure) {
940 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
941 GetMetadata(URLForPath("nonexistent")));
943 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
944 FileExists(URLForPath("nonexistent")));
946 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
947 DirectoryExists(URLForPath("nonexistent")));
948 EXPECT_TRUE(change_observer()->HasNoChange());
951 TEST_F(FileSystemOperationImplTest, TestExistsAndMetadataSuccess) {
952 FileSystemURL dir(CreateDirectory("dir"));
953 FileSystemURL file(CreateFile("dir/file"));
954 int read_access = 0;
956 EXPECT_EQ(base::File::FILE_OK, DirectoryExists(dir));
957 ++read_access;
959 EXPECT_EQ(base::File::FILE_OK, GetMetadata(dir));
960 EXPECT_TRUE(info().is_directory);
961 ++read_access;
963 EXPECT_EQ(base::File::FILE_OK, FileExists(file));
964 ++read_access;
966 EXPECT_EQ(base::File::FILE_OK, GetMetadata(file));
967 EXPECT_FALSE(info().is_directory);
968 ++read_access;
970 EXPECT_EQ(read_access,
971 quota_manager_proxy()->notify_storage_accessed_count());
972 EXPECT_TRUE(change_observer()->HasNoChange());
975 TEST_F(FileSystemOperationImplTest, TestTypeMismatchErrors) {
976 FileSystemURL dir(CreateDirectory("dir"));
977 EXPECT_EQ(base::File::FILE_ERROR_NOT_A_FILE, FileExists(dir));
979 FileSystemURL file(CreateFile("file"));
980 EXPECT_EQ(base::File::FILE_ERROR_NOT_A_DIRECTORY, DirectoryExists(file));
983 TEST_F(FileSystemOperationImplTest, TestReadDirFailure) {
984 // Path doesn't exist
985 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
986 ReadDirectory(URLForPath("nonexistent")));
988 // File exists.
989 FileSystemURL file(CreateFile("file"));
990 EXPECT_EQ(base::File::FILE_ERROR_NOT_A_DIRECTORY, ReadDirectory(file));
991 EXPECT_TRUE(change_observer()->HasNoChange());
994 TEST_F(FileSystemOperationImplTest, TestReadDirSuccess) {
995 // parent_dir
996 // | |
997 // child_dir child_file
998 // Verify reading parent_dir.
999 FileSystemURL parent_dir(CreateDirectory("dir"));
1000 FileSystemURL child_dir(CreateDirectory("dir/child_dir"));
1001 FileSystemURL child_file(CreateFile("dir/child_file"));
1003 EXPECT_EQ(base::File::FILE_OK, ReadDirectory(parent_dir));
1004 EXPECT_EQ(2u, entries().size());
1006 for (size_t i = 0; i < entries().size(); ++i) {
1007 if (entries()[i].is_directory)
1008 EXPECT_EQ(FILE_PATH_LITERAL("child_dir"), entries()[i].name);
1009 else
1010 EXPECT_EQ(FILE_PATH_LITERAL("child_file"), entries()[i].name);
1012 EXPECT_EQ(1, quota_manager_proxy()->notify_storage_accessed_count());
1013 EXPECT_TRUE(change_observer()->HasNoChange());
1016 TEST_F(FileSystemOperationImplTest, TestRemoveFailure) {
1017 // Path doesn't exist.
1018 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
1019 Remove(URLForPath("nonexistent"), false /* recursive */));
1021 // It's an error to try to remove a non-empty directory if recursive flag
1022 // is false.
1023 // parent_dir
1024 // | |
1025 // child_dir child_file
1026 // Verify deleting parent_dir.
1027 FileSystemURL parent_dir(CreateDirectory("dir"));
1028 FileSystemURL child_dir(CreateDirectory("dir/child_dir"));
1029 FileSystemURL child_file(CreateFile("dir/child_file"));
1031 EXPECT_EQ(base::File::FILE_ERROR_NOT_EMPTY,
1032 Remove(parent_dir, false /* recursive */));
1033 EXPECT_TRUE(change_observer()->HasNoChange());
1036 TEST_F(FileSystemOperationImplTest, TestRemoveSuccess) {
1037 FileSystemURL empty_dir(CreateDirectory("empty_dir"));
1038 EXPECT_TRUE(DirectoryExists("empty_dir"));
1039 EXPECT_EQ(base::File::FILE_OK, Remove(empty_dir, false /* recursive */));
1040 EXPECT_FALSE(DirectoryExists("empty_dir"));
1042 EXPECT_EQ(1, change_observer()->get_and_reset_remove_directory_count());
1043 EXPECT_TRUE(change_observer()->HasNoChange());
1046 TEST_F(FileSystemOperationImplTest, TestRemoveSuccessRecursive) {
1047 // Removing a non-empty directory with recursive flag == true should be ok.
1048 // parent_dir
1049 // | |
1050 // child_dir child_files
1051 // |
1052 // child_files
1054 // Verify deleting parent_dir.
1055 FileSystemURL parent_dir(CreateDirectory("dir"));
1056 for (int i = 0; i < 8; ++i)
1057 CreateFile(base::StringPrintf("dir/file-%d", i));
1058 FileSystemURL child_dir(CreateDirectory("dir/child_dir"));
1059 for (int i = 0; i < 8; ++i)
1060 CreateFile(base::StringPrintf("dir/child_dir/file-%d", i));
1062 EXPECT_EQ(base::File::FILE_OK, Remove(parent_dir, true /* recursive */));
1063 EXPECT_FALSE(DirectoryExists("parent_dir"));
1065 EXPECT_EQ(2, change_observer()->get_and_reset_remove_directory_count());
1066 EXPECT_EQ(16, change_observer()->get_and_reset_remove_file_count());
1067 EXPECT_TRUE(change_observer()->HasNoChange());
1070 TEST_F(FileSystemOperationImplTest, TestTruncate) {
1071 FileSystemURL file(CreateFile("file"));
1072 base::FilePath platform_path = PlatformPath("file");
1074 char test_data[] = "test data";
1075 int data_size = static_cast<int>(sizeof(test_data));
1076 EXPECT_EQ(data_size,
1077 base::WriteFile(platform_path, test_data, data_size));
1079 // Check that its length is the size of the data written.
1080 EXPECT_EQ(base::File::FILE_OK, GetMetadata(file));
1081 EXPECT_FALSE(info().is_directory);
1082 EXPECT_EQ(data_size, info().size);
1084 // Extend the file by truncating it.
1085 int length = 17;
1086 EXPECT_EQ(base::File::FILE_OK, Truncate(file, length));
1088 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
1089 EXPECT_TRUE(change_observer()->HasNoChange());
1091 // Check that its length is now 17 and that it's all zeroes after the test
1092 // data.
1093 EXPECT_EQ(length, GetFileSize("file"));
1094 char data[100];
1095 EXPECT_EQ(length, base::ReadFile(platform_path, data, length));
1096 for (int i = 0; i < length; ++i) {
1097 if (i < static_cast<int>(sizeof(test_data)))
1098 EXPECT_EQ(test_data[i], data[i]);
1099 else
1100 EXPECT_EQ(0, data[i]);
1103 // Shorten the file by truncating it.
1104 length = 3;
1105 EXPECT_EQ(base::File::FILE_OK, Truncate(file, length));
1107 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
1108 EXPECT_TRUE(change_observer()->HasNoChange());
1110 // Check that its length is now 3 and that it contains only bits of test data.
1111 EXPECT_EQ(length, GetFileSize("file"));
1112 EXPECT_EQ(length, base::ReadFile(platform_path, data, length));
1113 for (int i = 0; i < length; ++i)
1114 EXPECT_EQ(test_data[i], data[i]);
1116 // Truncate is not a 'read' access. (Here expected access count is 1
1117 // since we made 1 read access for GetMetadata.)
1118 EXPECT_EQ(1, quota_manager_proxy()->notify_storage_accessed_count());
1121 TEST_F(FileSystemOperationImplTest, TestTruncateFailureByQuota) {
1122 FileSystemURL dir(CreateDirectory("dir"));
1123 FileSystemURL file(CreateFile("dir/file"));
1125 GrantQuotaForCurrentUsage();
1126 AddQuota(10);
1128 EXPECT_EQ(base::File::FILE_OK, Truncate(file, 10));
1129 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
1130 EXPECT_TRUE(change_observer()->HasNoChange());
1132 EXPECT_EQ(10, GetFileSize("dir/file"));
1134 EXPECT_EQ(base::File::FILE_ERROR_NO_SPACE, Truncate(file, 11));
1135 EXPECT_TRUE(change_observer()->HasNoChange());
1137 EXPECT_EQ(10, GetFileSize("dir/file"));
1140 TEST_F(FileSystemOperationImplTest, TestTouchFile) {
1141 FileSystemURL file(CreateFile("file"));
1142 base::FilePath platform_path = PlatformPath("file");
1144 base::File::Info info;
1145 EXPECT_TRUE(base::GetFileInfo(platform_path, &info));
1146 EXPECT_FALSE(info.is_directory);
1147 EXPECT_EQ(0, info.size);
1148 const base::Time last_modified = info.last_modified;
1149 const base::Time last_accessed = info.last_accessed;
1151 const base::Time new_modified_time = base::Time::UnixEpoch();
1152 const base::Time new_accessed_time = new_modified_time +
1153 base::TimeDelta::FromHours(77);
1154 ASSERT_NE(last_modified, new_modified_time);
1155 ASSERT_NE(last_accessed, new_accessed_time);
1157 EXPECT_EQ(base::File::FILE_OK,
1158 TouchFile(file, new_accessed_time, new_modified_time));
1159 EXPECT_TRUE(change_observer()->HasNoChange());
1161 EXPECT_TRUE(base::GetFileInfo(platform_path, &info));
1162 // We compare as time_t here to lower our resolution, to avoid false
1163 // negatives caused by conversion to the local filesystem's native
1164 // representation and back.
1165 EXPECT_EQ(new_modified_time.ToTimeT(), info.last_modified.ToTimeT());
1166 EXPECT_EQ(new_accessed_time.ToTimeT(), info.last_accessed.ToTimeT());
1169 TEST_F(FileSystemOperationImplTest, TestCreateSnapshotFile) {
1170 FileSystemURL dir(CreateDirectory("dir"));
1172 // Create a file for the testing.
1173 EXPECT_EQ(base::File::FILE_OK, DirectoryExists(dir));
1174 FileSystemURL file(CreateFile("dir/file"));
1175 EXPECT_EQ(base::File::FILE_OK, FileExists(file));
1177 // See if we can get a 'snapshot' file info for the file.
1178 // Since FileSystemOperationImpl assumes the file exists in the local
1179 // directory it should just returns the same metadata and platform_path
1180 // as the file itself.
1181 EXPECT_EQ(base::File::FILE_OK, CreateSnapshotFile(file));
1182 EXPECT_FALSE(info().is_directory);
1183 EXPECT_EQ(PlatformPath("dir/file"), path());
1184 EXPECT_TRUE(change_observer()->HasNoChange());
1186 // The FileSystemOpration implementation does not create a
1187 // shareable file reference.
1188 EXPECT_EQ(NULL, shareable_file_ref());
1191 TEST_F(FileSystemOperationImplTest,
1192 TestMoveSuccessSrcDirRecursiveWithQuota) {
1193 FileSystemURL src(CreateDirectory("src"));
1194 int src_path_cost = GetUsage();
1196 FileSystemURL dest(CreateDirectory("dest"));
1197 FileSystemURL child_file1(CreateFile("src/file1"));
1198 FileSystemURL child_file2(CreateFile("src/file2"));
1199 FileSystemURL child_dir(CreateDirectory("src/dir"));
1200 FileSystemURL grandchild_file1(CreateFile("src/dir/file1"));
1201 FileSystemURL grandchild_file2(CreateFile("src/dir/file2"));
1203 int total_path_cost = GetUsage();
1204 EXPECT_EQ(0, GetDataSizeOnDisk());
1206 EXPECT_EQ(base::File::FILE_OK, Truncate(child_file1, 5000));
1207 EXPECT_EQ(base::File::FILE_OK, Truncate(child_file2, 400));
1208 EXPECT_EQ(base::File::FILE_OK, Truncate(grandchild_file1, 30));
1209 EXPECT_EQ(base::File::FILE_OK, Truncate(grandchild_file2, 2));
1211 const int64 all_file_size = 5000 + 400 + 30 + 2;
1212 EXPECT_EQ(all_file_size, GetDataSizeOnDisk());
1213 EXPECT_EQ(all_file_size + total_path_cost, GetUsage());
1215 EXPECT_EQ(base::File::FILE_OK,
1216 Move(src, dest, FileSystemOperation::OPTION_NONE));
1218 EXPECT_FALSE(DirectoryExists("src/dir"));
1219 EXPECT_FALSE(FileExists("src/dir/file2"));
1220 EXPECT_TRUE(DirectoryExists("dest/dir"));
1221 EXPECT_TRUE(FileExists("dest/dir/file2"));
1223 EXPECT_EQ(all_file_size, GetDataSizeOnDisk());
1224 EXPECT_EQ(all_file_size + total_path_cost - src_path_cost,
1225 GetUsage());
1228 TEST_F(FileSystemOperationImplTest,
1229 TestCopySuccessSrcDirRecursiveWithQuota) {
1230 FileSystemURL src(CreateDirectory("src"));
1231 FileSystemURL dest1(CreateDirectory("dest1"));
1232 FileSystemURL dest2(CreateDirectory("dest2"));
1234 int64 usage = GetUsage();
1235 FileSystemURL child_file1(CreateFile("src/file1"));
1236 FileSystemURL child_file2(CreateFile("src/file2"));
1237 FileSystemURL child_dir(CreateDirectory("src/dir"));
1238 int64 child_path_cost = GetUsage() - usage;
1239 usage += child_path_cost;
1241 FileSystemURL grandchild_file1(CreateFile("src/dir/file1"));
1242 FileSystemURL grandchild_file2(CreateFile("src/dir/file2"));
1243 int64 total_path_cost = GetUsage();
1244 int64 grandchild_path_cost = total_path_cost - usage;
1246 EXPECT_EQ(0, GetDataSizeOnDisk());
1248 EXPECT_EQ(base::File::FILE_OK, Truncate(child_file1, 8000));
1249 EXPECT_EQ(base::File::FILE_OK, Truncate(child_file2, 700));
1250 EXPECT_EQ(base::File::FILE_OK, Truncate(grandchild_file1, 60));
1251 EXPECT_EQ(base::File::FILE_OK, Truncate(grandchild_file2, 5));
1253 const int64 child_file_size = 8000 + 700;
1254 const int64 grandchild_file_size = 60 + 5;
1255 const int64 all_file_size = child_file_size + grandchild_file_size;
1256 int64 expected_usage = all_file_size + total_path_cost;
1258 usage = GetUsage();
1259 EXPECT_EQ(all_file_size, GetDataSizeOnDisk());
1260 EXPECT_EQ(expected_usage, usage);
1262 EXPECT_EQ(base::File::FILE_OK,
1263 Copy(src, dest1, FileSystemOperation::OPTION_NONE));
1265 expected_usage += all_file_size + child_path_cost + grandchild_path_cost;
1266 EXPECT_TRUE(DirectoryExists("src/dir"));
1267 EXPECT_TRUE(FileExists("src/dir/file2"));
1268 EXPECT_TRUE(DirectoryExists("dest1/dir"));
1269 EXPECT_TRUE(FileExists("dest1/dir/file2"));
1271 EXPECT_EQ(2 * all_file_size, GetDataSizeOnDisk());
1272 EXPECT_EQ(expected_usage, GetUsage());
1274 EXPECT_EQ(base::File::FILE_OK,
1275 Copy(child_dir, dest2, FileSystemOperation::OPTION_NONE));
1277 expected_usage += grandchild_file_size + grandchild_path_cost;
1278 usage = GetUsage();
1279 EXPECT_EQ(2 * child_file_size + 3 * grandchild_file_size,
1280 GetDataSizeOnDisk());
1281 EXPECT_EQ(expected_usage, usage);
1284 TEST_F(FileSystemOperationImplTest,
1285 TestCopySuccessSrcFileWithDifferentFileSize) {
1286 FileSystemURL src_file(CreateFile("src"));
1287 FileSystemURL dest_file(CreateFile("dest"));
1289 EXPECT_EQ(base::File::FILE_OK, Truncate(dest_file, 6));
1290 EXPECT_EQ(base::File::FILE_OK,
1291 Copy(src_file, dest_file, FileSystemOperation::OPTION_NONE));
1292 EXPECT_EQ(0, GetFileSize("dest"));
1295 } // namespace content