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

Side by Side Diff: components/drive/chromeos/file_cache_unittest.cc

Issue 2006503002: Reland of Mark removable Drive caches for cryptohome. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 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
« no previous file with comments | « components/drive/chromeos/file_cache.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/drive/chromeos/file_cache.h" 5 #include "components/drive/chromeos/file_cache.h"
6 6
7 #include <linux/fs.h>
7 #include <stddef.h> 8 #include <stddef.h>
8 #include <stdint.h> 9 #include <stdint.h>
9 #include <sys/stat.h> 10 #include <sys/ioctl.h>
10 #include <sys/types.h> 11 #include <sys/xattr.h>
11 #include <unistd.h>
12 12
13 #include <string> 13 #include <string>
14 #include <vector> 14 #include <vector>
15 15
16 #include "base/callback_helpers.h" 16 #include "base/callback_helpers.h"
17 #include "base/files/file.h"
17 #include "base/files/file_enumerator.h" 18 #include "base/files/file_enumerator.h"
18 #include "base/files/file_util.h" 19 #include "base/files/file_util.h"
19 #include "base/files/scoped_temp_dir.h" 20 #include "base/files/scoped_temp_dir.h"
20 #include "base/md5.h" 21 #include "base/md5.h"
21 #include "base/path_service.h" 22 #include "base/path_service.h"
22 #include "base/single_thread_task_runner.h" 23 #include "base/single_thread_task_runner.h"
24 #include "base/stl_util.h"
23 #include "base/strings/stringprintf.h" 25 #include "base/strings/stringprintf.h"
24 #include "base/threading/thread_task_runner_handle.h" 26 #include "base/threading/thread_task_runner_handle.h"
25 #include "base/time/time.h" 27 #include "base/time/time.h"
26 #include "components/drive/chromeos/drive_test_util.h" 28 #include "components/drive/chromeos/drive_test_util.h"
27 #include "components/drive/chromeos/fake_free_disk_space_getter.h" 29 #include "components/drive/chromeos/fake_free_disk_space_getter.h"
28 #include "components/drive/drive.pb.h" 30 #include "components/drive/drive.pb.h"
29 #include "components/drive/file_system_core_util.h" 31 #include "components/drive/file_system_core_util.h"
30 #include "components/drive/resource_metadata_storage.h" 32 #include "components/drive/resource_metadata_storage.h"
31 #include "content/public/test/test_browser_thread_bundle.h" 33 #include "content/public/test/test_browser_thread_bundle.h"
32 #include "google_apis/drive/test_util.h" 34 #include "google_apis/drive/test_util.h"
33 #include "testing/gtest/include/gtest/gtest.h" 35 #include "testing/gtest/include/gtest/gtest.h"
34 36
35 namespace drive { 37 namespace drive {
36 namespace internal { 38 namespace internal {
37 namespace { 39 namespace {
38 40
41 typedef long FileAttributes; // NOLINT(runtime/int)
42
39 const base::FilePath::CharType kCacheFileDirectory[] = 43 const base::FilePath::CharType kCacheFileDirectory[] =
40 FILE_PATH_LITERAL("files"); 44 FILE_PATH_LITERAL("files");
41 const base::FilePath::CharType kNewCacheFileDirectory[] =
42 FILE_PATH_LITERAL("blobs");
43 const base::FilePath::CharType kLinkDirectory[] = FILE_PATH_LITERAL("links");
44 45
45 const int kTemporaryFileSizeInBytes = 10; 46 const int kTemporaryFileSizeInBytes = 10;
46 47
47 int GetNumberOfLinks(const base::FilePath& file_path) { 48 FileAttributes GetFileAttributes(const base::FilePath& file_path) {
48 struct stat result; 49 base::File file(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ);
49 if (stat(file_path.AsUTF8Unsafe().c_str(), &result) != 0) { 50 if (!file.IsValid()) {
51 ADD_FAILURE() << "Failed to open file: " << file_path.value();
50 return -1; 52 return -1;
51 } 53 }
52 return result.st_nlink; 54 FileAttributes flags = 0;
55 if (ioctl(file.GetPlatformFile(), FS_IOC_GETFLAGS, &flags) < 0) {
56 ADD_FAILURE() << "Failed to get attributes: " << file_path.value();
57 return -1;
58 }
59 return flags;
60 }
61
62 bool HasRemovableFlag(const base::FilePath& file_path) {
63 return (GetFileAttributes(file_path) & FS_NODUMP_FL) == FS_NODUMP_FL;
53 } 64 }
54 65
55 } // namespace 66 } // namespace
56 67
57 // Tests FileCache methods working with the blocking task runner. 68 // Tests FileCache methods working with the blocking task runner.
58 class FileCacheTest : public testing::Test { 69 class FileCacheTest : public testing::Test {
59 protected: 70 protected:
60 void SetUp() override { 71 void SetUp() override {
61 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 72 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
62 const base::FilePath metadata_dir = temp_dir_.path().AppendASCII("meta"); 73 const base::FilePath metadata_dir = temp_dir_.path().AppendASCII("meta");
(...skipping 12 matching lines...) Expand all
75 cache_.reset(new FileCache(metadata_storage_.get(), cache_files_dir_, 86 cache_.reset(new FileCache(metadata_storage_.get(), cache_files_dir_,
76 base::ThreadTaskRunnerHandle::Get().get(), 87 base::ThreadTaskRunnerHandle::Get().get(),
77 fake_free_disk_space_getter_.get())); 88 fake_free_disk_space_getter_.get()));
78 ASSERT_TRUE(cache_->Initialize()); 89 ASSERT_TRUE(cache_->Initialize());
79 } 90 }
80 91
81 static bool RenameCacheFilesToNewFormat(FileCache* cache) { 92 static bool RenameCacheFilesToNewFormat(FileCache* cache) {
82 return cache->RenameCacheFilesToNewFormat(); 93 return cache->RenameCacheFilesToNewFormat();
83 } 94 }
84 95
96 base::FilePath GetCacheFilePath(const std::string& id) {
97 return cache_->GetCacheFilePath(id);
98 }
99
85 base::FilePath AddTestEntry(const std::string id, 100 base::FilePath AddTestEntry(const std::string id,
86 const std::string md5, 101 const std::string md5,
87 const time_t last_accessed, 102 const time_t last_accessed,
88 const base::FilePath& src_file) { 103 const base::FilePath& src_file) {
89 ResourceEntry entry; 104 ResourceEntry entry;
90 entry.set_local_id(id); 105 entry.set_local_id(id);
91 entry.mutable_file_info()->set_last_accessed(last_accessed); 106 entry.mutable_file_info()->set_last_accessed(last_accessed);
92 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry)); 107 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
93 EXPECT_EQ(FILE_ERROR_OK, 108 EXPECT_EQ(FILE_ERROR_OK,
94 cache_->Store(id, md5, src_file, FileCache::FILE_OPERATION_COPY)); 109 cache_->Store(id, md5, src_file, FileCache::FILE_OPERATION_COPY));
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after
415 id, md5, src_file_path, FileCache::FILE_OPERATION_COPY)); 430 id, md5, src_file_path, FileCache::FILE_OPERATION_COPY));
416 431
417 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry)); 432 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry));
418 EXPECT_TRUE(entry.file_specific_info().cache_state().is_present()); 433 EXPECT_TRUE(entry.file_specific_info().cache_state().is_present());
419 EXPECT_EQ(md5, entry.file_specific_info().cache_state().md5()); 434 EXPECT_EQ(md5, entry.file_specific_info().cache_state().md5());
420 435
421 base::FilePath cache_file_path; 436 base::FilePath cache_file_path;
422 EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(id, &cache_file_path)); 437 EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(id, &cache_file_path));
423 EXPECT_TRUE(base::ContentsEqual(src_file_path, cache_file_path)); 438 EXPECT_TRUE(base::ContentsEqual(src_file_path, cache_file_path));
424 439
440 base::FilePath dest_file_path = GetCacheFilePath(id);
441 EXPECT_TRUE(HasRemovableFlag((dest_file_path)));
442
425 // Store a non-existent file. 443 // Store a non-existent file.
426 EXPECT_EQ(FILE_ERROR_FAILED, cache_->Store( 444 EXPECT_EQ(FILE_ERROR_FAILED, cache_->Store(
427 id, md5, base::FilePath::FromUTF8Unsafe("non_existent_file"), 445 id, md5, base::FilePath::FromUTF8Unsafe("non_existent_file"),
428 FileCache::FILE_OPERATION_COPY)); 446 FileCache::FILE_OPERATION_COPY));
429 447
430 // Passing empty MD5 marks the entry as dirty. 448 // Passing empty MD5 marks the entry as dirty.
431 EXPECT_EQ(FILE_ERROR_OK, cache_->Store( 449 EXPECT_EQ(FILE_ERROR_OK, cache_->Store(
432 id, std::string(), src_file_path, FileCache::FILE_OPERATION_COPY)); 450 id, std::string(), src_file_path, FileCache::FILE_OPERATION_COPY));
433 451
434 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry)); 452 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry));
435 EXPECT_TRUE(entry.file_specific_info().cache_state().is_present()); 453 EXPECT_TRUE(entry.file_specific_info().cache_state().is_present());
436 EXPECT_TRUE(entry.file_specific_info().cache_state().md5().empty()); 454 EXPECT_TRUE(entry.file_specific_info().cache_state().md5().empty());
437 EXPECT_TRUE(entry.file_specific_info().cache_state().is_dirty()); 455 EXPECT_TRUE(entry.file_specific_info().cache_state().is_dirty());
456 EXPECT_FALSE(HasRemovableFlag((dest_file_path)));
438 457
439 // No free space available. 458 // No free space available.
440 fake_free_disk_space_getter_->set_default_value(0); 459 fake_free_disk_space_getter_->set_default_value(0);
441 460
442 EXPECT_EQ(FILE_ERROR_NO_LOCAL_SPACE, cache_->Store( 461 EXPECT_EQ(FILE_ERROR_NO_LOCAL_SPACE, cache_->Store(
443 id, md5, src_file_path, FileCache::FILE_OPERATION_COPY)); 462 id, md5, src_file_path, FileCache::FILE_OPERATION_COPY));
444 } 463 }
445 464
446 TEST_F(FileCacheTest, PinAndUnpin) { 465 TEST_F(FileCacheTest, PinAndUnpin) {
447 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat"); 466 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat");
448 const std::string src_contents = "test"; 467 const std::string src_contents = "test";
449 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path, 468 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path,
450 src_contents)); 469 src_contents));
451 std::string id("id_present"); 470 std::string id("id_present");
452 std::string md5(base::MD5String(src_contents)); 471 std::string md5(base::MD5String(src_contents));
453 472
454 // Store a file. 473 // Store a file.
455 ResourceEntry entry; 474 ResourceEntry entry;
456 entry.set_local_id(id); 475 entry.set_local_id(id);
457 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry)); 476 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
458 EXPECT_EQ(FILE_ERROR_OK, cache_->Store( 477 EXPECT_EQ(FILE_ERROR_OK, cache_->Store(
459 id, md5, src_file_path, FileCache::FILE_OPERATION_COPY)); 478 id, md5, src_file_path, FileCache::FILE_OPERATION_COPY));
460 479
480 const base::FilePath dest_file_path = GetCacheFilePath(id);
461 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry)); 481 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry));
462 EXPECT_FALSE(entry.file_specific_info().cache_state().is_pinned()); 482 EXPECT_FALSE(entry.file_specific_info().cache_state().is_pinned());
483 EXPECT_TRUE(HasRemovableFlag((dest_file_path)));
463 484
464 // Pin the existing file. 485 // Pin the existing file.
465 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(id)); 486 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(id));
466 487
467 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry)); 488 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry));
468 EXPECT_TRUE(entry.file_specific_info().cache_state().is_pinned()); 489 EXPECT_TRUE(entry.file_specific_info().cache_state().is_pinned());
490 EXPECT_FALSE(HasRemovableFlag((dest_file_path)));
469 491
470 // Unpin the file. 492 // Unpin the file.
471 EXPECT_EQ(FILE_ERROR_OK, cache_->Unpin(id)); 493 EXPECT_EQ(FILE_ERROR_OK, cache_->Unpin(id));
472 494
473 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry)); 495 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry));
474 EXPECT_FALSE(entry.file_specific_info().cache_state().is_pinned()); 496 EXPECT_FALSE(entry.file_specific_info().cache_state().is_pinned());
497 EXPECT_TRUE(HasRemovableFlag((dest_file_path)));
475 498
476 // Pin a non-present file. 499 // Pin a non-present file.
477 std::string id_non_present = "id_non_present"; 500 std::string id_non_present = "id_non_present";
478 entry.Clear(); 501 entry.Clear();
479 entry.set_local_id(id_non_present); 502 entry.set_local_id(id_non_present);
480 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry)); 503 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
481 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(id_non_present)); 504 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(id_non_present));
482 505
483 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_non_present, &entry)); 506 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_non_present, &entry));
484 EXPECT_TRUE(entry.file_specific_info().cache_state().is_pinned()); 507 EXPECT_TRUE(entry.file_specific_info().cache_state().is_pinned());
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
533 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry)); 556 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
534 ASSERT_EQ(FILE_ERROR_OK, cache_->Store(id, "md5", src_file, 557 ASSERT_EQ(FILE_ERROR_OK, cache_->Store(id, "md5", src_file,
535 FileCache::FILE_OPERATION_COPY)); 558 FileCache::FILE_OPERATION_COPY));
536 EXPECT_EQ(0, entry.file_info().last_modified()); 559 EXPECT_EQ(0, entry.file_info().last_modified());
537 560
538 // Entry is not dirty nor opened. 561 // Entry is not dirty nor opened.
539 EXPECT_FALSE(cache_->IsOpenedForWrite(id)); 562 EXPECT_FALSE(cache_->IsOpenedForWrite(id));
540 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry)); 563 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry));
541 EXPECT_FALSE(entry.file_specific_info().cache_state().is_dirty()); 564 EXPECT_FALSE(entry.file_specific_info().cache_state().is_dirty());
542 565
566 const base::FilePath dest_file = GetCacheFilePath(id);
567 EXPECT_TRUE(HasRemovableFlag((dest_file)));
568
543 // Open (1). 569 // Open (1).
544 std::unique_ptr<base::ScopedClosureRunner> file_closer1; 570 std::unique_ptr<base::ScopedClosureRunner> file_closer1;
545 EXPECT_EQ(FILE_ERROR_OK, cache_->OpenForWrite(id, &file_closer1)); 571 EXPECT_EQ(FILE_ERROR_OK, cache_->OpenForWrite(id, &file_closer1));
546 EXPECT_TRUE(cache_->IsOpenedForWrite(id)); 572 EXPECT_TRUE(cache_->IsOpenedForWrite(id));
547 573
548 // Entry is dirty. 574 // Entry is dirty.
549 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry)); 575 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry));
550 EXPECT_TRUE(entry.file_specific_info().cache_state().is_dirty()); 576 EXPECT_TRUE(entry.file_specific_info().cache_state().is_dirty());
577 EXPECT_FALSE(HasRemovableFlag((dest_file)));
551 578
552 // Open (2). 579 // Open (2).
553 std::unique_ptr<base::ScopedClosureRunner> file_closer2; 580 std::unique_ptr<base::ScopedClosureRunner> file_closer2;
554 EXPECT_EQ(FILE_ERROR_OK, cache_->OpenForWrite(id, &file_closer2)); 581 EXPECT_EQ(FILE_ERROR_OK, cache_->OpenForWrite(id, &file_closer2));
555 EXPECT_TRUE(cache_->IsOpenedForWrite(id)); 582 EXPECT_TRUE(cache_->IsOpenedForWrite(id));
556 583
557 // Close (1). 584 // Close (1).
558 file_closer1.reset(); 585 file_closer1.reset();
559 base::RunLoop().RunUntilIdle(); 586 base::RunLoop().RunUntilIdle();
560 EXPECT_TRUE(cache_->IsOpenedForWrite(id)); 587 EXPECT_TRUE(cache_->IsOpenedForWrite(id));
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
619 base::FilePath src_file; 646 base::FilePath src_file;
620 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file)); 647 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file));
621 648
622 const std::string id = "id"; 649 const std::string id = "id";
623 ResourceEntry entry; 650 ResourceEntry entry;
624 entry.set_local_id(id); 651 entry.set_local_id(id);
625 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry)); 652 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
626 ASSERT_EQ(FILE_ERROR_OK, cache_->Store(id, "md5", src_file, 653 ASSERT_EQ(FILE_ERROR_OK, cache_->Store(id, "md5", src_file,
627 FileCache::FILE_OPERATION_COPY)); 654 FileCache::FILE_OPERATION_COPY));
628 655
656 const base::FilePath dest_file = GetCacheFilePath(id);
657 EXPECT_TRUE(HasRemovableFlag((dest_file)));
658
629 // Open the file. 659 // Open the file.
630 std::unique_ptr<base::ScopedClosureRunner> file_closer; 660 std::unique_ptr<base::ScopedClosureRunner> file_closer;
631 EXPECT_EQ(FILE_ERROR_OK, cache_->OpenForWrite(id, &file_closer)); 661 EXPECT_EQ(FILE_ERROR_OK, cache_->OpenForWrite(id, &file_closer));
632 662
633 // Entry is dirty. 663 // Entry is dirty.
634 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry)); 664 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry));
635 EXPECT_TRUE(entry.file_specific_info().cache_state().is_dirty()); 665 EXPECT_TRUE(entry.file_specific_info().cache_state().is_dirty());
666 EXPECT_FALSE(HasRemovableFlag((dest_file)));
636 667
637 // Cannot clear the dirty bit of an opened entry. 668 // Cannot clear the dirty bit of an opened entry.
638 EXPECT_EQ(FILE_ERROR_IN_USE, cache_->ClearDirty(id)); 669 EXPECT_EQ(FILE_ERROR_IN_USE, cache_->ClearDirty(id));
670 EXPECT_FALSE(HasRemovableFlag((dest_file)));
639 671
640 // Close the file and clear the dirty bit. 672 // Close the file and clear the dirty bit.
641 file_closer.reset(); 673 file_closer.reset();
642 base::RunLoop().RunUntilIdle(); 674 base::RunLoop().RunUntilIdle();
643 EXPECT_EQ(FILE_ERROR_OK, cache_->ClearDirty(id)); 675 EXPECT_EQ(FILE_ERROR_OK, cache_->ClearDirty(id));
644 676
645 // Entry is not dirty. 677 // Entry is not dirty.
646 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry)); 678 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry));
647 EXPECT_FALSE(entry.file_specific_info().cache_state().is_dirty()); 679 EXPECT_FALSE(entry.file_specific_info().cache_state().is_dirty());
680 EXPECT_TRUE(HasRemovableFlag((dest_file)));
648 } 681 }
649 682
650 TEST_F(FileCacheTest, Remove) { 683 TEST_F(FileCacheTest, Remove) {
651 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat"); 684 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat");
652 const std::string src_contents = "test"; 685 const std::string src_contents = "test";
653 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path, 686 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path,
654 src_contents)); 687 src_contents));
655 std::string id("id"); 688 std::string id("id");
656 std::string md5(base::MD5String(src_contents)); 689 std::string md5(base::MD5String(src_contents));
657 690
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
704 contents.clear(); 737 contents.clear();
705 EXPECT_TRUE(base::ReadFileToString(file_directory.AppendASCII("id_koo"), 738 EXPECT_TRUE(base::ReadFileToString(file_directory.AppendASCII("id_koo"),
706 &contents)); 739 &contents));
707 EXPECT_EQ("koo", contents); 740 EXPECT_EQ("koo", contents);
708 contents.clear(); 741 contents.clear();
709 EXPECT_TRUE(base::ReadFileToString(file_directory.AppendASCII("id_kyu"), 742 EXPECT_TRUE(base::ReadFileToString(file_directory.AppendASCII("id_kyu"),
710 &contents)); 743 &contents));
711 EXPECT_EQ("kyu", contents); 744 EXPECT_EQ("kyu", contents);
712 } 745 }
713 746
714 // Test for migrating cache files from files to blobs. 747 TEST_F(FileCacheTest, FixMetadataAndFileAttributes) {
715 TEST_F(FileCacheTest, MigrateCacheFiles) {
716 // Create test files and metadata. 748 // Create test files and metadata.
717 base::FilePath temp_file; 749 base::FilePath temp_file;
718 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &temp_file)); 750 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &temp_file));
719 751
720 const base::FilePath old_cache_dir = 752 // Entry A: pinned cache file.
721 temp_dir_.path().Append(kCacheFileDirectory);
722 const base::FilePath new_cache_dir =
723 temp_dir_.path().Append(kNewCacheFileDirectory);
724 const base::FilePath link_dir = temp_dir_.path().Append(kLinkDirectory);
725 ASSERT_TRUE(base::CreateDirectory(old_cache_dir));
726 ASSERT_TRUE(base::CreateDirectory(new_cache_dir));
727 ASSERT_TRUE(base::CreateDirectory(link_dir));
728
729 // Entry A: cache file in old cache directory with metadata.
730 const std::string id_a = "id_a"; 753 const std::string id_a = "id_a";
731 ResourceEntry entry_a; 754 ResourceEntry entry_a;
732 entry_a.set_local_id(id_a); 755 entry_a.set_local_id(id_a);
733 entry_a.mutable_file_specific_info()->mutable_cache_state()->set_is_present( 756 FileCacheEntry* file_cache_entry_a =
734 true); 757 entry_a.mutable_file_specific_info()->mutable_cache_state();
758 file_cache_entry_a->set_is_present(true);
759 file_cache_entry_a->set_is_pinned(true);
760 file_cache_entry_a->set_is_dirty(false);
735 ASSERT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry_a)); 761 ASSERT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry_a));
736 const base::FilePath old_file_path_a = old_cache_dir.AppendASCII(id_a); 762 const base::FilePath file_path_a = GetCacheFilePath(id_a);
737 const base::FilePath new_file_path_a = new_cache_dir.AppendASCII(id_a); 763 ASSERT_TRUE(base::CopyFile(temp_file, file_path_a));
738 ASSERT_TRUE(base::CopyFile(temp_file, old_file_path_a));
739 764
740 // Entry B: cache file in old cache directory without metadata. 765 // Entry B: dirty cache file.
741 const std::string id_b = "id_b"; 766 const std::string id_b = "id_b";
742 const base::FilePath old_file_path_b = old_cache_dir.AppendASCII(id_b); 767 ResourceEntry entry_b;
743 ASSERT_TRUE(base::CopyFile(temp_file, old_file_path_b)); 768 entry_b.set_local_id(id_b);
769 FileCacheEntry* file_cache_entry_b =
770 entry_b.mutable_file_specific_info()->mutable_cache_state();
771 file_cache_entry_b->set_is_present(true);
772 file_cache_entry_b->set_is_pinned(false);
773 file_cache_entry_b->set_is_dirty(true);
774 ASSERT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry_b));
775 const base::FilePath file_path_b = GetCacheFilePath(id_b);
776 ASSERT_TRUE(base::CopyFile(temp_file, file_path_b));
744 777
745 // Entry C: already migrated cache file. 778 // Entry C: not pinned nor dirty cache file.
746 const std::string id_c = "id_c"; 779 const std::string id_c = "id_c";
747 ResourceEntry entry_c; 780 ResourceEntry entry_c;
748 entry_c.set_local_id(id_c); 781 entry_c.set_local_id(id_c);
749 entry_c.mutable_file_specific_info()->mutable_cache_state()->set_is_present( 782 FileCacheEntry* file_cache_entry_c =
750 true); 783 entry_c.mutable_file_specific_info()->mutable_cache_state();
784 file_cache_entry_c->set_is_present(true);
785 file_cache_entry_c->set_is_pinned(false);
786 file_cache_entry_c->set_is_dirty(false);
751 ASSERT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry_c)); 787 ASSERT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry_c));
752 const base::FilePath new_file_path_c = new_cache_dir.AppendASCII(id_c); 788 const base::FilePath file_path_c = GetCacheFilePath(id_c);
753 ASSERT_TRUE(base::CopyFile(temp_file, new_file_path_c)); 789 ASSERT_TRUE(base::CopyFile(temp_file, file_path_c));
754 790
755 // Entry D: metadata entry without cache file. 791 // Entry D: pinned cache file somehow having removable flag.
756 const std::string id_d = "id_d"; 792 const std::string id_d = "id_d";
757 ResourceEntry entry_d; 793 ResourceEntry entry_d;
758 entry_d.set_local_id(id_d); 794 entry_d.set_local_id(id_d);
759 entry_d.mutable_file_specific_info()->mutable_cache_state()->set_is_present( 795 FileCacheEntry* file_cache_entry_d =
760 true); 796 entry_d.mutable_file_specific_info()->mutable_cache_state();
797 file_cache_entry_d->set_is_present(true);
798 file_cache_entry_d->set_is_pinned(true);
799 file_cache_entry_d->set_is_dirty(false);
761 ASSERT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry_d)); 800 ASSERT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry_d));
801 const base::FilePath file_path_d = GetCacheFilePath(id_d);
802 ASSERT_TRUE(base::CopyFile(temp_file, file_path_d));
762 803
763 // Entry E: pinned cache file. 804 // Set removable flag.
805 FileAttributes flags = GetFileAttributes(file_path_d);
806 ASSERT_GE(flags, 0);
807 flags |= FS_NODUMP_FL;
808 base::File file_d(file_path_d, base::File::FLAG_OPEN | base::File::FLAG_READ);
809 ASSERT_EQ(ioctl(file_d.GetPlatformFile(), FS_IOC_SETFLAGS, &flags), 0);
810
811 // Entry E: there is no file; removed by cryptohome.
764 const std::string id_e = "id_e"; 812 const std::string id_e = "id_e";
765 ResourceEntry entry_e; 813 ResourceEntry entry_e;
766 entry_e.set_local_id(id_e); 814 entry_e.set_local_id(id_e);
767 FileCacheEntry* file_cache_entry_e = 815 FileCacheEntry* file_cache_entry_e =
768 entry_e.mutable_file_specific_info()->mutable_cache_state(); 816 entry_e.mutable_file_specific_info()->mutable_cache_state();
769 file_cache_entry_e->set_is_present(true); 817 file_cache_entry_e->set_is_present(true);
770 file_cache_entry_e->set_is_pinned(true); 818 file_cache_entry_e->set_is_pinned(false);
771 file_cache_entry_e->set_is_dirty(false); 819 file_cache_entry_e->set_is_dirty(false);
772 ASSERT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry_e)); 820 ASSERT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry_e));
773 const base::FilePath old_file_path_e = old_cache_dir.AppendASCII(id_e); 821 const base::FilePath file_path_e = GetCacheFilePath(id_e);
774 const base::FilePath new_file_path_e = new_cache_dir.AppendASCII(id_e);
775 const base::FilePath link_path_e = link_dir.AppendASCII(id_e);
776 ASSERT_TRUE(base::CopyFile(temp_file, old_file_path_e));
777 822
778 // Entry F: dirty cache file. 823 // Entry F: there is a file, but metadata says not.
779 const std::string id_f = "id_f"; 824 const std::string id_f = "id_f";
780 ResourceEntry entry_f; 825 ResourceEntry entry_f;
781 entry_f.set_local_id(id_f); 826 entry_f.set_local_id(id_f);
782 FileCacheEntry* file_cache_entry_f = 827 entry_f.mutable_file_specific_info()->mutable_cache_state()->set_is_present(
783 entry_f.mutable_file_specific_info()->mutable_cache_state(); 828 false);
784 file_cache_entry_f->set_is_present(true);
785 file_cache_entry_f->set_is_pinned(false);
786 file_cache_entry_f->set_is_dirty(true);
787 ASSERT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry_f)); 829 ASSERT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry_f));
788 const base::FilePath old_file_path_f = old_cache_dir.AppendASCII(id_f); 830 const base::FilePath file_path_f = GetCacheFilePath(id_f);
789 const base::FilePath new_file_path_f = new_cache_dir.AppendASCII(id_f); 831 ASSERT_TRUE(base::CopyFile(temp_file, file_path_f));
790 const base::FilePath link_path_f = link_dir.AppendASCII(id_f);
791 ASSERT_TRUE(base::CopyFile(temp_file, old_file_path_f));
792 832
793 // Entry G: partially migrated pinned cache file. 833 // Entry G: no file nor metadata.
794 const std::string id_g = "id_g"; 834 const std::string id_g = "id_g";
795 ResourceEntry entry_g; 835 ResourceEntry entry_g;
796 entry_g.set_local_id(id_g); 836 entry_g.set_local_id(id_g);
797 FileCacheEntry* file_cache_entry_g = 837 entry_f.mutable_file_specific_info()->mutable_cache_state()->set_is_present(
798 entry_g.mutable_file_specific_info()->mutable_cache_state(); 838 false);
799 file_cache_entry_g->set_is_present(true); 839 ASSERT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry_f));
800 file_cache_entry_g->set_is_pinned(true);
801 file_cache_entry_g->set_is_dirty(false);
802 ASSERT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry_g));
803 const base::FilePath old_file_path_g = old_cache_dir.AppendASCII(id_g);
804 const base::FilePath new_file_path_g = new_cache_dir.AppendASCII(id_g);
805 const base::FilePath link_path_g = link_dir.AppendASCII(id_g);
806 ASSERT_TRUE(base::CopyFile(temp_file, old_file_path_g));
807 ASSERT_EQ(0, link(old_file_path_g.AsUTF8Unsafe().c_str(),
808 link_path_g.AsUTF8Unsafe().c_str()));
809 840
810 // Entry H: pinned entry without cache file. 841 // Initialize fixes inconsistency between metadata and cache file attributes
811 const std::string id_h = "id_h"; 842 // as well as adding specific file attributes to the cache directory.
812 ResourceEntry entry_h; 843 ASSERT_TRUE(cache_->Initialize());
813 entry_h.set_local_id(id_h);
814 FileCacheEntry* file_cache_entry_h =
815 entry_h.mutable_file_specific_info()->mutable_cache_state();
816 file_cache_entry_h->set_is_present(true);
817 file_cache_entry_h->set_is_pinned(true);
818 file_cache_entry_h->set_is_dirty(false);
819 ASSERT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry_h));
820
821 // Entry I: already migrated pinned cache file.
822 const std::string id_i = "id_i";
823 ResourceEntry entry_i;
824 entry_i.set_local_id(id_i);
825 FileCacheEntry* file_cache_entry_i =
826 entry_i.mutable_file_specific_info()->mutable_cache_state();
827 file_cache_entry_i->set_is_present(true);
828 file_cache_entry_i->set_is_pinned(true);
829 file_cache_entry_i->set_is_dirty(false);
830 ASSERT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry_i));
831 const base::FilePath new_file_path_i = new_cache_dir.AppendASCII(id_i);
832 const base::FilePath link_path_i = link_dir.AppendASCII(id_i);
833 ASSERT_TRUE(base::CopyFile(temp_file, new_file_path_i));
834 ASSERT_EQ(0, link(new_file_path_i.AsUTF8Unsafe().c_str(),
835 link_path_i.AsUTF8Unsafe().c_str()));
836
837 // Run migration.
838 ASSERT_TRUE(FileCache::MigrateCacheFiles(old_cache_dir, new_cache_dir,
839 link_dir, metadata_storage_.get()));
840 844
841 // Check result. 845 // Check result.
842 EXPECT_FALSE(base::PathExists(old_file_path_a)); 846 EXPECT_FALSE(HasRemovableFlag(file_path_a));
843 EXPECT_TRUE(base::PathExists(new_file_path_a)); 847 EXPECT_FALSE(HasRemovableFlag((file_path_b)));
844 EXPECT_EQ(1, GetNumberOfLinks(new_file_path_a)); 848 EXPECT_TRUE(HasRemovableFlag((file_path_c)));
845 // MigrateCacheFiles doesn't delete invalid cache file. 849 EXPECT_FALSE(HasRemovableFlag((file_path_d)));
846 EXPECT_TRUE(base::PathExists(old_file_path_b)); 850 EXPECT_FALSE(base::PathExists(file_path_f));
847 EXPECT_TRUE(base::PathExists(new_file_path_c)); 851
848 EXPECT_EQ(1, GetNumberOfLinks(new_file_path_c)); 852 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_e, &entry_e));
849 EXPECT_FALSE(base::PathExists(old_file_path_e)); 853 EXPECT_FALSE(entry_e.file_specific_info().cache_state().is_present());
850 EXPECT_TRUE(base::PathExists(new_file_path_e)); 854 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_f, &entry_f));
851 EXPECT_TRUE(base::PathExists(link_path_e)); 855 EXPECT_FALSE(entry_f.file_specific_info().cache_state().is_present());
852 EXPECT_EQ(2, GetNumberOfLinks(new_file_path_e)); 856
853 EXPECT_FALSE(base::PathExists(old_file_path_f)); 857 // Check the cache dir has appropriate attributes.
854 EXPECT_TRUE(base::PathExists(new_file_path_f)); 858 EXPECT_TRUE(HasRemovableFlag((cache_files_dir_)));
855 EXPECT_TRUE(base::PathExists(link_path_f)); 859 EXPECT_GE(getxattr(cache_files_dir_.value().c_str(),
856 EXPECT_EQ(2, GetNumberOfLinks(new_file_path_f)); 860 FileCache::kGCacheFilesAttribute, nullptr, 0), 0);
857 EXPECT_FALSE(base::PathExists(old_file_path_g));
858 EXPECT_TRUE(base::PathExists(new_file_path_g));
859 EXPECT_TRUE(base::PathExists(link_path_g));
860 EXPECT_EQ(2, GetNumberOfLinks(new_file_path_g));
861 EXPECT_TRUE(base::PathExists(new_file_path_i));
862 EXPECT_TRUE(base::PathExists(link_path_i));
863 EXPECT_EQ(2, GetNumberOfLinks(new_file_path_i));
864 } 861 }
865 862
866 TEST_F(FileCacheTest, ClearAll) { 863 TEST_F(FileCacheTest, ClearAll) {
867 const std::string id("1a2b"); 864 const std::string id("1a2b");
868 const std::string md5("abcdef0123456789"); 865 const std::string md5("abcdef0123456789");
869 866
870 // Store an existing file. 867 // Store an existing file.
871 ResourceEntry entry; 868 ResourceEntry entry;
872 entry.set_local_id(id); 869 entry.set_local_id(id);
873 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry)); 870 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
874 base::FilePath src_file; 871 base::FilePath src_file;
875 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file)); 872 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file));
876 ASSERT_EQ(FILE_ERROR_OK, 873 ASSERT_EQ(FILE_ERROR_OK,
877 cache_->Store(id, md5, src_file, FileCache::FILE_OPERATION_COPY)); 874 cache_->Store(id, md5, src_file, FileCache::FILE_OPERATION_COPY));
878 875
879 // Clear cache. 876 // Clear cache.
880 EXPECT_TRUE(cache_->ClearAll()); 877 EXPECT_TRUE(cache_->ClearAll());
881 878
882 // Verify that the cache is removed. 879 // Verify that the cache is removed.
883 EXPECT_TRUE(base::IsDirectoryEmpty(cache_files_dir_)); 880 EXPECT_TRUE(base::IsDirectoryEmpty(cache_files_dir_));
884 } 881 }
885 882
886 } // namespace internal 883 } // namespace internal
887 } // namespace drive 884 } // namespace drive
OLDNEW
« no previous file with comments | « components/drive/chromeos/file_cache.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698