[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
yawano | 3513e14 | 2016-04-20 00:42:42 | [diff] [blame] | 5 | #include "components/drive/chromeos/file_cache.h" |
[email protected] | 21ed9907 | 2012-09-13 01:49:33 | [diff] [blame] | 6 | |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 7 | #include <linux/fs.h> |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 10 | #include <sys/ioctl.h> |
| 11 | #include <sys/xattr.h> |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 12 | |
Stuart Langley | be403574 | 2018-05-10 05:32:27 | [diff] [blame] | 13 | #include <memory> |
[email protected] | 2333b2a1 | 2013-04-26 07:22:22 | [diff] [blame] | 14 | #include <string> |
[email protected] | 79c3752d | 2012-07-17 12:10:08 | [diff] [blame] | 15 | #include <vector> |
| 16 | |
[email protected] | 8b03ab3a | 2014-01-15 17:52:45 | [diff] [blame] | 17 | #include "base/callback_helpers.h" |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 18 | #include "base/files/file.h" |
[email protected] | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame] | 19 | #include "base/files/file_enumerator.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 20 | #include "base/files/file_util.h" |
[email protected] | 2333b2a1 | 2013-04-26 07:22:22 | [diff] [blame] | 21 | #include "base/files/scoped_temp_dir.h" |
Daniel Cheng | d07ef71 | 2019-03-28 23:13:41 | [diff] [blame] | 22 | #include "base/hash/md5.h" |
[email protected] | b7af4f1 | 2013-10-31 06:57:45 | [diff] [blame] | 23 | #include "base/path_service.h" |
Gabriel Charette | 078e366 | 2017-08-28 22:59:04 | [diff] [blame] | 24 | #include "base/run_loop.h" |
pranay.kumar | 229290b5 | 2015-05-14 05:21:04 | [diff] [blame] | 25 | #include "base/single_thread_task_runner.h" |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 26 | #include "base/stl_util.h" |
yawano | 4c36b9a0 | 2015-10-23 06:17:23 | [diff] [blame] | 27 | #include "base/strings/stringprintf.h" |
gab | 7966d31 | 2016-05-11 20:35:01 | [diff] [blame] | 28 | #include "base/threading/thread_task_runner_handle.h" |
yawano | 4c36b9a0 | 2015-10-23 06:17:23 | [diff] [blame] | 29 | #include "base/time/time.h" |
yawano | 3513e14 | 2016-04-20 00:42:42 | [diff] [blame] | 30 | #include "components/drive/chromeos/drive_test_util.h" |
| 31 | #include "components/drive/chromeos/fake_free_disk_space_getter.h" |
lukasza | 01b9d55a | 2015-07-21 15:19:25 | [diff] [blame] | 32 | #include "components/drive/drive.pb.h" |
lukasza | 6364a02 | 2015-08-21 01:13:24 | [diff] [blame] | 33 | #include "components/drive/file_system_core_util.h" |
| 34 | #include "components/drive/resource_metadata_storage.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 35 | #include "content/public/test/browser_task_environment.h" |
[email protected] | e196bef3 | 2013-12-03 05:33:13 | [diff] [blame] | 36 | #include "google_apis/drive/test_util.h" |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 37 | #include "testing/gtest/include/gtest/gtest.h" |
| 38 | |
[email protected] | d9d04df | 2012-10-12 07:06:35 | [diff] [blame] | 39 | namespace drive { |
[email protected] | 59c7cdec | 2013-05-07 04:17:13 | [diff] [blame] | 40 | namespace internal { |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 41 | namespace { |
| 42 | |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 43 | typedef long FileAttributes; // NOLINT(runtime/int) |
| 44 | |
yawano | 9fd1e63 | 2016-02-04 09:00:06 | [diff] [blame] | 45 | const base::FilePath::CharType kCacheFileDirectory[] = |
| 46 | FILE_PATH_LITERAL("files"); |
yawano | 9fd1e63 | 2016-02-04 09:00:06 | [diff] [blame] | 47 | |
yawano | 4c36b9a0 | 2015-10-23 06:17:23 | [diff] [blame] | 48 | const int kTemporaryFileSizeInBytes = 10; |
[email protected] | 3e8d64d | 2013-09-19 10:09:05 | [diff] [blame] | 49 | |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 50 | FileAttributes GetFileAttributes(const base::FilePath& file_path) { |
| 51 | base::File file(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 52 | if (!file.IsValid()) { |
| 53 | ADD_FAILURE() << "Failed to open file: " << file_path.value(); |
yawano | 1c325bf | 2016-04-20 06:37:03 | [diff] [blame] | 54 | return -1; |
| 55 | } |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 56 | FileAttributes flags = 0; |
| 57 | if (ioctl(file.GetPlatformFile(), FS_IOC_GETFLAGS, &flags) < 0) { |
| 58 | ADD_FAILURE() << "Failed to get attributes: " << file_path.value(); |
| 59 | return -1; |
| 60 | } |
| 61 | return flags; |
| 62 | } |
| 63 | |
| 64 | bool HasRemovableFlag(const base::FilePath& file_path) { |
| 65 | return (GetFileAttributes(file_path) & FS_NODUMP_FL) == FS_NODUMP_FL; |
yawano | 1c325bf | 2016-04-20 06:37:03 | [diff] [blame] | 66 | } |
| 67 | |
Sergei Datsenko | e5a99eb | 2018-08-31 06:30:39 | [diff] [blame] | 68 | bool HasRemovableAttribute(const base::FilePath& file_path) { |
| 69 | return getxattr(file_path.value().c_str(), |
| 70 | FileCache::kGCacheRemovableAttribute, nullptr, 0) >= 0; |
| 71 | } |
| 72 | |
| 73 | bool IsMarkedAsRemovable(const base::FilePath& path) { |
| 74 | return HasRemovableFlag(path) && HasRemovableAttribute(path); |
| 75 | } |
| 76 | |
[email protected] | 4743aa1c | 2012-07-13 14:54:23 | [diff] [blame] | 77 | } // namespace |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 78 | |
[email protected] | f8b1a53 | 2013-06-06 08:35:08 | [diff] [blame] | 79 | // Tests FileCache methods working with the blocking task runner. |
| 80 | class FileCacheTest : public testing::Test { |
| 81 | protected: |
dcheng | fe77330 | 2015-01-22 19:45:52 | [diff] [blame] | 82 | void SetUp() override { |
[email protected] | f8b1a53 | 2013-06-06 08:35:08 | [diff] [blame] | 83 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
vabr | 96fd0c0f | 2016-09-13 14:21:31 | [diff] [blame] | 84 | const base::FilePath metadata_dir = temp_dir_.GetPath().AppendASCII("meta"); |
| 85 | cache_files_dir_ = temp_dir_.GetPath().Append(kCacheFileDirectory); |
[email protected] | 04727b6 | 2013-09-09 10:02:15 | [diff] [blame] | 86 | |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 87 | ASSERT_TRUE(base::CreateDirectory(metadata_dir)); |
| 88 | ASSERT_TRUE(base::CreateDirectory(cache_files_dir_)); |
[email protected] | e07f7b7b | 2013-06-19 03:43:12 | [diff] [blame] | 89 | |
Stuart Langley | be403574 | 2018-05-10 05:32:27 | [diff] [blame] | 90 | fake_free_disk_space_getter_ = std::make_unique<FakeFreeDiskSpaceGetter>(); |
[email protected] | f8b1a53 | 2013-06-06 08:35:08 | [diff] [blame] | 91 | |
[email protected] | 2df61e1 | 2013-06-21 16:00:09 | [diff] [blame] | 92 | metadata_storage_.reset(new ResourceMetadataStorage( |
[email protected] | 04727b6 | 2013-09-09 10:02:15 | [diff] [blame] | 93 | metadata_dir, |
pranay.kumar | 229290b5 | 2015-05-14 05:21:04 | [diff] [blame] | 94 | base::ThreadTaskRunnerHandle::Get().get())); |
[email protected] | 2df61e1 | 2013-06-21 16:00:09 | [diff] [blame] | 95 | ASSERT_TRUE(metadata_storage_->Initialize()); |
| 96 | |
yawano | 4c36b9a0 | 2015-10-23 06:17:23 | [diff] [blame] | 97 | cache_.reset(new FileCache(metadata_storage_.get(), cache_files_dir_, |
| 98 | base::ThreadTaskRunnerHandle::Get().get(), |
| 99 | fake_free_disk_space_getter_.get())); |
[email protected] | 34a1bbf3 | 2013-06-17 07:24:02 | [diff] [blame] | 100 | ASSERT_TRUE(cache_->Initialize()); |
[email protected] | f8b1a53 | 2013-06-06 08:35:08 | [diff] [blame] | 101 | } |
| 102 | |
[email protected] | f2731d1 | 2013-10-22 03:23:15 | [diff] [blame] | 103 | static bool RenameCacheFilesToNewFormat(FileCache* cache) { |
| 104 | return cache->RenameCacheFilesToNewFormat(); |
[email protected] | 91a464e6 | 2013-07-10 09:30:06 | [diff] [blame] | 105 | } |
| 106 | |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 107 | base::FilePath GetCacheFilePath(const std::string& id) { |
| 108 | return cache_->GetCacheFilePath(id); |
| 109 | } |
| 110 | |
yawano | 4c36b9a0 | 2015-10-23 06:17:23 | [diff] [blame] | 111 | base::FilePath AddTestEntry(const std::string id, |
| 112 | const std::string md5, |
| 113 | const time_t last_accessed, |
| 114 | const base::FilePath& src_file) { |
| 115 | ResourceEntry entry; |
| 116 | entry.set_local_id(id); |
| 117 | entry.mutable_file_info()->set_last_accessed(last_accessed); |
| 118 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry)); |
| 119 | EXPECT_EQ(FILE_ERROR_OK, |
| 120 | cache_->Store(id, md5, src_file, FileCache::FILE_OPERATION_COPY)); |
| 121 | |
| 122 | base::FilePath path; |
| 123 | EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(id, &path)); |
| 124 | |
| 125 | // Update last modified and accessed time. |
| 126 | base::Time time = base::Time::FromTimeT(last_accessed); |
| 127 | EXPECT_TRUE(base::TouchFile(path, time, time)); |
| 128 | |
| 129 | return path; |
| 130 | } |
| 131 | |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 132 | content::BrowserTaskEnvironment task_environment_; |
[email protected] | f8b1a53 | 2013-06-06 08:35:08 | [diff] [blame] | 133 | base::ScopedTempDir temp_dir_; |
[email protected] | 823ca971 | 2013-09-13 10:09:09 | [diff] [blame] | 134 | base::FilePath cache_files_dir_; |
[email protected] | f8b1a53 | 2013-06-06 08:35:08 | [diff] [blame] | 135 | |
dcheng | f4275023 | 2016-04-12 04:12:27 | [diff] [blame] | 136 | std::unique_ptr<ResourceMetadataStorage, test_util::DestroyHelperForTests> |
[email protected] | 2df61e1 | 2013-06-21 16:00:09 | [diff] [blame] | 137 | metadata_storage_; |
dcheng | f4275023 | 2016-04-12 04:12:27 | [diff] [blame] | 138 | std::unique_ptr<FileCache, test_util::DestroyHelperForTests> cache_; |
| 139 | std::unique_ptr<FakeFreeDiskSpaceGetter> fake_free_disk_space_getter_; |
[email protected] | f8b1a53 | 2013-06-06 08:35:08 | [diff] [blame] | 140 | }; |
| 141 | |
[email protected] | b7af4f1 | 2013-10-31 06:57:45 | [diff] [blame] | 142 | TEST_F(FileCacheTest, RecoverFilesFromCacheDirectory) { |
| 143 | base::FilePath dir_source_root; |
Avi Drissman | f617d01 | 2018-05-02 18:48:53 | [diff] [blame] | 144 | EXPECT_TRUE(base::PathService::Get(base::DIR_SOURCE_ROOT, &dir_source_root)); |
[email protected] | b7af4f1 | 2013-10-31 06:57:45 | [diff] [blame] | 145 | const base::FilePath src_path = |
| 146 | dir_source_root.AppendASCII("chrome/test/data/chromeos/drive/image.png"); |
| 147 | |
| 148 | // Store files. This file should not be moved. |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 149 | ResourceEntry entry; |
| 150 | entry.set_local_id("id_foo"); |
| 151 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry)); |
[email protected] | b7af4f1 | 2013-10-31 06:57:45 | [diff] [blame] | 152 | EXPECT_EQ(FILE_ERROR_OK, cache_->Store("id_foo", "md5", src_path, |
| 153 | FileCache::FILE_OPERATION_COPY)); |
| 154 | |
| 155 | // Set up files in the cache directory. These files should be moved. |
[email protected] | 3e8d64d | 2013-09-19 10:09:05 | [diff] [blame] | 156 | const base::FilePath file_directory = |
vabr | 96fd0c0f | 2016-09-13 14:21:31 | [diff] [blame] | 157 | temp_dir_.GetPath().Append(kCacheFileDirectory); |
[email protected] | b7af4f1 | 2013-10-31 06:57:45 | [diff] [blame] | 158 | ASSERT_TRUE(base::CopyFile(src_path, file_directory.AppendASCII("id_bar"))); |
| 159 | ASSERT_TRUE(base::CopyFile(src_path, file_directory.AppendASCII("id_baz"))); |
[email protected] | e8842b19 | 2013-06-11 04:05:14 | [diff] [blame] | 160 | |
[email protected] | 026d4a52 | 2013-11-05 14:22:18 | [diff] [blame] | 161 | // Insert a dirty entry with "id_baz" to |recovered_cache_info|. |
[email protected] | 760abc3 | 2013-11-01 05:13:01 | [diff] [blame] | 162 | // This should not prevent the file from being recovered. |
[email protected] | 026d4a52 | 2013-11-05 14:22:18 | [diff] [blame] | 163 | ResourceMetadataStorage::RecoveredCacheInfoMap recovered_cache_info; |
| 164 | recovered_cache_info["id_baz"].is_dirty = true; |
| 165 | recovered_cache_info["id_baz"].title = "baz.png"; |
[email protected] | 760abc3 | 2013-11-01 05:13:01 | [diff] [blame] | 166 | |
[email protected] | b7af4f1 | 2013-10-31 06:57:45 | [diff] [blame] | 167 | // Recover files. |
vabr | 96fd0c0f | 2016-09-13 14:21:31 | [diff] [blame] | 168 | const base::FilePath dest_directory = temp_dir_.GetPath().AppendASCII("dest"); |
[email protected] | 760abc3 | 2013-11-01 05:13:01 | [diff] [blame] | 169 | EXPECT_TRUE(cache_->RecoverFilesFromCacheDirectory(dest_directory, |
[email protected] | 026d4a52 | 2013-11-05 14:22:18 | [diff] [blame] | 170 | recovered_cache_info)); |
[email protected] | 2df61e1 | 2013-06-21 16:00:09 | [diff] [blame] | 171 | |
[email protected] | b7af4f1 | 2013-10-31 06:57:45 | [diff] [blame] | 172 | // Only two files should be recovered. |
| 173 | EXPECT_TRUE(base::PathExists(dest_directory)); |
[email protected] | 026d4a52 | 2013-11-05 14:22:18 | [diff] [blame] | 174 | // base::FileEnumerator does not guarantee the order. |
| 175 | if (base::PathExists(dest_directory.AppendASCII("baz00000001.png"))) { |
| 176 | EXPECT_TRUE(base::ContentsEqual( |
| 177 | src_path, |
| 178 | dest_directory.AppendASCII("baz00000001.png"))); |
| 179 | EXPECT_TRUE(base::ContentsEqual( |
| 180 | src_path, |
| 181 | dest_directory.AppendASCII("image00000002.png"))); |
| 182 | } else { |
| 183 | EXPECT_TRUE(base::ContentsEqual( |
| 184 | src_path, |
| 185 | dest_directory.AppendASCII("image00000001.png"))); |
| 186 | EXPECT_TRUE(base::ContentsEqual( |
| 187 | src_path, |
| 188 | dest_directory.AppendASCII("baz00000002.png"))); |
| 189 | } |
| 190 | EXPECT_FALSE(base::PathExists( |
| 191 | dest_directory.AppendASCII("image00000003.png"))); |
[email protected] | e8842b19 | 2013-06-11 04:05:14 | [diff] [blame] | 192 | } |
| 193 | |
[email protected] | f8b1a53 | 2013-06-06 08:35:08 | [diff] [blame] | 194 | TEST_F(FileCacheTest, FreeDiskSpaceIfNeededFor) { |
| 195 | base::FilePath src_file; |
vabr | 96fd0c0f | 2016-09-13 14:21:31 | [diff] [blame] | 196 | ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &src_file)); |
[email protected] | f8b1a53 | 2013-06-06 08:35:08 | [diff] [blame] | 197 | |
| 198 | // Store a file as a 'temporary' file and remember the path. |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 199 | const std::string id_tmp = "id_tmp", md5_tmp = "md5_tmp"; |
yawano | 4c36b9a0 | 2015-10-23 06:17:23 | [diff] [blame] | 200 | const time_t last_accessed_tmp = 1; |
| 201 | const base::FilePath& tmp_path = |
| 202 | AddTestEntry(id_tmp, md5_tmp, last_accessed_tmp, src_file); |
[email protected] | f8b1a53 | 2013-06-06 08:35:08 | [diff] [blame] | 203 | |
| 204 | // Store a file as a pinned file and remember the path. |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 205 | const std::string id_pinned = "id_pinned", md5_pinned = "md5_pinned"; |
yawano | 4c36b9a0 | 2015-10-23 06:17:23 | [diff] [blame] | 206 | const time_t last_accessed_pinned = 1; |
| 207 | const base::FilePath& pinned_path = |
| 208 | AddTestEntry(id_pinned, md5_pinned, last_accessed_pinned, src_file); |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 209 | ASSERT_EQ(FILE_ERROR_OK, cache_->Pin(id_pinned)); |
[email protected] | f8b1a53 | 2013-06-06 08:35:08 | [diff] [blame] | 210 | |
| 211 | // Call FreeDiskSpaceIfNeededFor(). |
| 212 | fake_free_disk_space_getter_->set_default_value(test_util::kLotsOfSpace); |
| 213 | fake_free_disk_space_getter_->PushFakeValue(0); |
yawano | 4c36b9a0 | 2015-10-23 06:17:23 | [diff] [blame] | 214 | fake_free_disk_space_getter_->PushFakeValue(0); |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 215 | const int64_t kNeededBytes = 1; |
[email protected] | f8b1a53 | 2013-06-06 08:35:08 | [diff] [blame] | 216 | EXPECT_TRUE(cache_->FreeDiskSpaceIfNeededFor(kNeededBytes)); |
| 217 | |
| 218 | // Only 'temporary' file gets removed. |
yawano | 4c36b9a0 | 2015-10-23 06:17:23 | [diff] [blame] | 219 | ResourceEntry entry; |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 220 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_tmp, &entry)); |
| 221 | EXPECT_FALSE(entry.file_specific_info().cache_state().is_present()); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 222 | EXPECT_FALSE(base::PathExists(tmp_path)); |
[email protected] | f8b1a53 | 2013-06-06 08:35:08 | [diff] [blame] | 223 | |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 224 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_pinned, &entry)); |
| 225 | EXPECT_TRUE(entry.file_specific_info().cache_state().is_present()); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 226 | EXPECT_TRUE(base::PathExists(pinned_path)); |
[email protected] | f8b1a53 | 2013-06-06 08:35:08 | [diff] [blame] | 227 | |
| 228 | // Returns false when disk space cannot be freed. |
| 229 | fake_free_disk_space_getter_->set_default_value(0); |
| 230 | EXPECT_FALSE(cache_->FreeDiskSpaceIfNeededFor(kNeededBytes)); |
| 231 | } |
| 232 | |
yawano | 4c36b9a0 | 2015-10-23 06:17:23 | [diff] [blame] | 233 | TEST_F(FileCacheTest, EvictDriveCacheInLRU) { |
| 234 | // Create temporary file. |
| 235 | base::FilePath src_file; |
vabr | 96fd0c0f | 2016-09-13 14:21:31 | [diff] [blame] | 236 | ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &src_file)); |
yawano | 4c36b9a0 | 2015-10-23 06:17:23 | [diff] [blame] | 237 | ASSERT_EQ(kTemporaryFileSizeInBytes, |
| 238 | base::WriteFile(src_file, "abcdefghij", kTemporaryFileSizeInBytes)); |
| 239 | |
| 240 | // Add entries. |
| 241 | const std::string id_a = "id_a", md5_a = "md5_a"; |
| 242 | const time_t last_accessed_a = 1; |
| 243 | const base::FilePath& a_path = |
| 244 | AddTestEntry(id_a, md5_a, last_accessed_a, src_file); |
| 245 | |
| 246 | const std::string id_pinned = "id_pinned", md5_pinned = "md5_pinned"; |
| 247 | const time_t last_accessed_pinned = 2; |
| 248 | const base::FilePath& pinned_path = |
| 249 | AddTestEntry(id_pinned, md5_pinned, last_accessed_pinned, src_file); |
| 250 | ASSERT_EQ(FILE_ERROR_OK, cache_->Pin(id_pinned)); |
| 251 | |
| 252 | const std::string id_b = "id_b", md5_b = "md5_b"; |
| 253 | const time_t last_accessed_b = 3; |
| 254 | const base::FilePath& b_path = |
| 255 | AddTestEntry(id_b, md5_b, last_accessed_b, src_file); |
| 256 | |
| 257 | const std::string id_c = "id_c", md5_c = "md5_c"; |
| 258 | const time_t last_accessed_c = 4; |
| 259 | const base::FilePath& c_path = |
| 260 | AddTestEntry(id_c, md5_c, last_accessed_c, src_file); |
| 261 | |
| 262 | // Call FreeDiskSpaceIfNeededFor. |
| 263 | fake_free_disk_space_getter_->set_default_value(test_util::kLotsOfSpace); |
| 264 | fake_free_disk_space_getter_->PushFakeValue(kMinFreeSpaceInBytes); |
| 265 | fake_free_disk_space_getter_->PushFakeValue(kMinFreeSpaceInBytes); |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 266 | const int64_t kNeededBytes = kTemporaryFileSizeInBytes * 3 / 2; |
yawano | 4c36b9a0 | 2015-10-23 06:17:23 | [diff] [blame] | 267 | EXPECT_TRUE(cache_->FreeDiskSpaceIfNeededFor(kNeededBytes)); |
| 268 | |
| 269 | // Entry A is evicted. |
| 270 | ResourceEntry entry; |
| 271 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_a, &entry)); |
| 272 | EXPECT_FALSE(entry.file_specific_info().cache_state().is_present()); |
| 273 | EXPECT_FALSE(base::PathExists(a_path)); |
| 274 | |
| 275 | // Pinned entry should not be evicted. |
| 276 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_pinned, &entry)); |
| 277 | EXPECT_TRUE(entry.file_specific_info().cache_state().is_present()); |
| 278 | EXPECT_TRUE(base::PathExists(pinned_path)); |
| 279 | |
| 280 | // Entry B is evicted. |
| 281 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_b, &entry)); |
| 282 | EXPECT_FALSE(entry.file_specific_info().cache_state().is_present()); |
| 283 | EXPECT_FALSE(base::PathExists(b_path)); |
| 284 | |
| 285 | // Entry C should not be evicted. |
| 286 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_c, &entry)); |
| 287 | EXPECT_TRUE(entry.file_specific_info().cache_state().is_present()); |
| 288 | EXPECT_TRUE(base::PathExists(c_path)); |
| 289 | } |
| 290 | |
| 291 | // Test case for deleting invalid cache files which don't have corresponding |
| 292 | // metadata. |
| 293 | TEST_F(FileCacheTest, EvictInvalidCacheFile) { |
| 294 | base::FilePath src_file; |
vabr | 96fd0c0f | 2016-09-13 14:21:31 | [diff] [blame] | 295 | ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &src_file)); |
yawano | 4c36b9a0 | 2015-10-23 06:17:23 | [diff] [blame] | 296 | |
| 297 | // Add entries. |
| 298 | const std::string id_a = "id_a", md5_a = "md5_a"; |
| 299 | const time_t last_accessed_a = 1; |
| 300 | const base::FilePath& a_path = |
| 301 | AddTestEntry(id_a, md5_a, last_accessed_a, src_file); |
| 302 | |
| 303 | const std::string id_b = "id_b", md5_b = "md5_b"; |
| 304 | const time_t last_accessed_b = 2; |
| 305 | const base::FilePath& b_path = |
| 306 | AddTestEntry(id_b, md5_b, last_accessed_b, src_file); |
| 307 | |
| 308 | // Remove metadata of entry B. |
| 309 | ASSERT_EQ(FILE_ERROR_OK, metadata_storage_->RemoveEntry(id_b)); |
| 310 | |
| 311 | // Confirm cache file of entry B exists. |
| 312 | ASSERT_TRUE(base::PathExists(b_path)); |
| 313 | |
| 314 | // Run FreeDiskSpaceIfNeededFor. |
| 315 | fake_free_disk_space_getter_->set_default_value(test_util::kLotsOfSpace); |
| 316 | fake_free_disk_space_getter_->PushFakeValue(kMinFreeSpaceInBytes); |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 317 | const int64_t kNeededBytes = 1; |
yawano | 4c36b9a0 | 2015-10-23 06:17:23 | [diff] [blame] | 318 | EXPECT_TRUE(cache_->FreeDiskSpaceIfNeededFor(kNeededBytes)); |
| 319 | |
| 320 | // Entry A is not evicted. |
| 321 | ResourceEntry entry; |
| 322 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_a, &entry)); |
| 323 | EXPECT_TRUE(entry.file_specific_info().cache_state().is_present()); |
| 324 | EXPECT_TRUE(base::PathExists(a_path)); |
| 325 | |
| 326 | // Entry B is evicted. |
| 327 | EXPECT_EQ(FILE_ERROR_NOT_FOUND, metadata_storage_->GetEntry(id_b, &entry)); |
| 328 | EXPECT_FALSE(base::PathExists(b_path)); |
| 329 | } |
| 330 | |
| 331 | TEST_F(FileCacheTest, TooManyCacheFiles) { |
| 332 | const size_t kMaxNumOfEvictedCacheFiles = 50; |
| 333 | cache_->SetMaxNumOfEvictedCacheFilesForTest(kMaxNumOfEvictedCacheFiles); |
| 334 | |
| 335 | // Create temporary file. |
| 336 | base::FilePath src_file; |
vabr | 96fd0c0f | 2016-09-13 14:21:31 | [diff] [blame] | 337 | ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &src_file)); |
yawano | 4c36b9a0 | 2015-10-23 06:17:23 | [diff] [blame] | 338 | ASSERT_EQ(kTemporaryFileSizeInBytes, |
| 339 | base::WriteFile(src_file, "abcdefghij", kTemporaryFileSizeInBytes)); |
| 340 | |
| 341 | // Add kNumOfTestFiles=kMaxNumOfEvictedCacheFiles*2 entries. |
| 342 | std::vector<base::FilePath> paths; |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 343 | const int32_t kNumOfTestFiles = kMaxNumOfEvictedCacheFiles * 2; |
yawano | 4c36b9a0 | 2015-10-23 06:17:23 | [diff] [blame] | 344 | for (int i = 0; i < kNumOfTestFiles; ++i) { |
| 345 | // Set last accessed in reverse order to the file name. i.e. If you sort |
| 346 | // files in name-asc order, they will be last access desc order. |
| 347 | paths.push_back(AddTestEntry( |
| 348 | base::StringPrintf("id_%02d", i), base::StringPrintf("md5_%02d", i), |
| 349 | kNumOfTestFiles - i /* last accessed */, src_file)); |
| 350 | } |
| 351 | |
| 352 | // Confirm cache files of kNumOfTestFiles actually exist. |
| 353 | for (const auto& path : paths) { |
| 354 | ASSERT_TRUE(base::PathExists(path)) << path.value(); |
| 355 | } |
| 356 | |
| 357 | // Try to free kMaxNumOfEvictedCacheFiles * 3 / 2. |
| 358 | fake_free_disk_space_getter_->set_default_value(test_util::kLotsOfSpace); |
| 359 | fake_free_disk_space_getter_->PushFakeValue(kMinFreeSpaceInBytes); |
| 360 | fake_free_disk_space_getter_->PushFakeValue(kMinFreeSpaceInBytes); |
| 361 | fake_free_disk_space_getter_->PushFakeValue( |
| 362 | kMinFreeSpaceInBytes + |
| 363 | (kMaxNumOfEvictedCacheFiles * kTemporaryFileSizeInBytes)); |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 364 | const int64_t kNeededBytes = |
yawano | 4c36b9a0 | 2015-10-23 06:17:23 | [diff] [blame] | 365 | (kMaxNumOfEvictedCacheFiles * 3 / 2) * kTemporaryFileSizeInBytes; |
| 366 | EXPECT_FALSE(cache_->FreeDiskSpaceIfNeededFor(kNeededBytes)); |
| 367 | |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 368 | for (uint32_t i = 0; i < kNumOfTestFiles; ++i) { |
yawano | 4c36b9a0 | 2015-10-23 06:17:23 | [diff] [blame] | 369 | // Assert that only first kMaxNumOfEvictedCacheFiles exist. |
| 370 | ASSERT_EQ(i < kMaxNumOfEvictedCacheFiles, base::PathExists(paths[i])); |
| 371 | } |
| 372 | } |
| 373 | |
[email protected] | 9cbea703 | 2013-09-24 07:54:53 | [diff] [blame] | 374 | TEST_F(FileCacheTest, GetFile) { |
vabr | 96fd0c0f | 2016-09-13 14:21:31 | [diff] [blame] | 375 | const base::FilePath src_file_path = temp_dir_.GetPath().Append("test.dat"); |
[email protected] | 9cbea703 | 2013-09-24 07:54:53 | [diff] [blame] | 376 | const std::string src_contents = "test"; |
| 377 | EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path, |
| 378 | src_contents)); |
| 379 | std::string id("id1"); |
| 380 | std::string md5(base::MD5String(src_contents)); |
| 381 | |
| 382 | const base::FilePath cache_file_directory = |
vabr | 96fd0c0f | 2016-09-13 14:21:31 | [diff] [blame] | 383 | temp_dir_.GetPath().Append(kCacheFileDirectory); |
[email protected] | 9cbea703 | 2013-09-24 07:54:53 | [diff] [blame] | 384 | |
| 385 | // Try to get an existing file from cache. |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 386 | ResourceEntry entry; |
| 387 | entry.set_local_id(id); |
| 388 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry)); |
[email protected] | 9cbea703 | 2013-09-24 07:54:53 | [diff] [blame] | 389 | EXPECT_EQ(FILE_ERROR_OK, cache_->Store(id, md5, src_file_path, |
| 390 | FileCache::FILE_OPERATION_COPY)); |
| 391 | base::FilePath cache_file_path; |
| 392 | EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(id, &cache_file_path)); |
| 393 | EXPECT_EQ( |
| 394 | cache_file_directory.AppendASCII(util::EscapeCacheFileName(id)).value(), |
| 395 | cache_file_path.value()); |
| 396 | |
| 397 | std::string contents; |
| 398 | EXPECT_TRUE(base::ReadFileToString(cache_file_path, &contents)); |
| 399 | EXPECT_EQ(src_contents, contents); |
| 400 | |
| 401 | // Get file from cache with different id. |
| 402 | id = "id2"; |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 403 | entry.Clear(); |
| 404 | entry.set_local_id(id); |
| 405 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry)); |
[email protected] | 9cbea703 | 2013-09-24 07:54:53 | [diff] [blame] | 406 | EXPECT_EQ(FILE_ERROR_NOT_FOUND, cache_->GetFile(id, &cache_file_path)); |
| 407 | |
| 408 | // Pin a non-existent file. |
| 409 | EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(id)); |
| 410 | |
| 411 | // Get the non-existent pinned file from cache. |
| 412 | EXPECT_EQ(FILE_ERROR_NOT_FOUND, cache_->GetFile(id, &cache_file_path)); |
| 413 | |
| 414 | // Get a previously pinned and stored file from cache. |
| 415 | EXPECT_EQ(FILE_ERROR_OK, cache_->Store(id, md5, src_file_path, |
| 416 | FileCache::FILE_OPERATION_COPY)); |
| 417 | |
| 418 | EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(id, &cache_file_path)); |
| 419 | EXPECT_EQ( |
| 420 | cache_file_directory.AppendASCII(util::EscapeCacheFileName(id)).value(), |
| 421 | cache_file_path.value()); |
| 422 | |
| 423 | contents.clear(); |
| 424 | EXPECT_TRUE(base::ReadFileToString(cache_file_path, &contents)); |
| 425 | EXPECT_EQ(src_contents, contents); |
| 426 | } |
| 427 | |
[email protected] | 834673b | 2014-02-20 18:34:43 | [diff] [blame] | 428 | TEST_F(FileCacheTest, Store) { |
vabr | 96fd0c0f | 2016-09-13 14:21:31 | [diff] [blame] | 429 | const base::FilePath src_file_path = temp_dir_.GetPath().Append("test.dat"); |
[email protected] | 834673b | 2014-02-20 18:34:43 | [diff] [blame] | 430 | const std::string src_contents = "test"; |
| 431 | EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path, |
| 432 | src_contents)); |
| 433 | std::string id("id"); |
| 434 | std::string md5(base::MD5String(src_contents)); |
| 435 | |
| 436 | // Store a file. |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 437 | ResourceEntry entry; |
| 438 | entry.set_local_id(id); |
| 439 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry)); |
[email protected] | 834673b | 2014-02-20 18:34:43 | [diff] [blame] | 440 | EXPECT_EQ(FILE_ERROR_OK, cache_->Store( |
| 441 | id, md5, src_file_path, FileCache::FILE_OPERATION_COPY)); |
| 442 | |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 443 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry)); |
| 444 | EXPECT_TRUE(entry.file_specific_info().cache_state().is_present()); |
| 445 | EXPECT_EQ(md5, entry.file_specific_info().cache_state().md5()); |
[email protected] | 834673b | 2014-02-20 18:34:43 | [diff] [blame] | 446 | |
| 447 | base::FilePath cache_file_path; |
| 448 | EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(id, &cache_file_path)); |
| 449 | EXPECT_TRUE(base::ContentsEqual(src_file_path, cache_file_path)); |
| 450 | |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 451 | base::FilePath dest_file_path = GetCacheFilePath(id); |
Sergei Datsenko | e5a99eb | 2018-08-31 06:30:39 | [diff] [blame] | 452 | EXPECT_TRUE(IsMarkedAsRemovable(dest_file_path)); |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 453 | |
[email protected] | 834673b | 2014-02-20 18:34:43 | [diff] [blame] | 454 | // Store a non-existent file. |
| 455 | EXPECT_EQ(FILE_ERROR_FAILED, cache_->Store( |
| 456 | id, md5, base::FilePath::FromUTF8Unsafe("non_existent_file"), |
| 457 | FileCache::FILE_OPERATION_COPY)); |
| 458 | |
| 459 | // Passing empty MD5 marks the entry as dirty. |
| 460 | EXPECT_EQ(FILE_ERROR_OK, cache_->Store( |
| 461 | id, std::string(), src_file_path, FileCache::FILE_OPERATION_COPY)); |
| 462 | |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 463 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry)); |
| 464 | EXPECT_TRUE(entry.file_specific_info().cache_state().is_present()); |
| 465 | EXPECT_TRUE(entry.file_specific_info().cache_state().md5().empty()); |
| 466 | EXPECT_TRUE(entry.file_specific_info().cache_state().is_dirty()); |
Sergei Datsenko | e5a99eb | 2018-08-31 06:30:39 | [diff] [blame] | 467 | EXPECT_FALSE(IsMarkedAsRemovable(dest_file_path)); |
[email protected] | 834673b | 2014-02-20 18:34:43 | [diff] [blame] | 468 | |
| 469 | // No free space available. |
| 470 | fake_free_disk_space_getter_->set_default_value(0); |
| 471 | |
| 472 | EXPECT_EQ(FILE_ERROR_NO_LOCAL_SPACE, cache_->Store( |
| 473 | id, md5, src_file_path, FileCache::FILE_OPERATION_COPY)); |
| 474 | } |
| 475 | |
| 476 | TEST_F(FileCacheTest, PinAndUnpin) { |
vabr | 96fd0c0f | 2016-09-13 14:21:31 | [diff] [blame] | 477 | const base::FilePath src_file_path = temp_dir_.GetPath().Append("test.dat"); |
[email protected] | 834673b | 2014-02-20 18:34:43 | [diff] [blame] | 478 | const std::string src_contents = "test"; |
| 479 | EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path, |
| 480 | src_contents)); |
| 481 | std::string id("id_present"); |
| 482 | std::string md5(base::MD5String(src_contents)); |
| 483 | |
| 484 | // Store a file. |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 485 | ResourceEntry entry; |
| 486 | entry.set_local_id(id); |
| 487 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry)); |
[email protected] | 834673b | 2014-02-20 18:34:43 | [diff] [blame] | 488 | EXPECT_EQ(FILE_ERROR_OK, cache_->Store( |
| 489 | id, md5, src_file_path, FileCache::FILE_OPERATION_COPY)); |
| 490 | |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 491 | const base::FilePath dest_file_path = GetCacheFilePath(id); |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 492 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry)); |
| 493 | EXPECT_FALSE(entry.file_specific_info().cache_state().is_pinned()); |
Sergei Datsenko | e5a99eb | 2018-08-31 06:30:39 | [diff] [blame] | 494 | EXPECT_TRUE(IsMarkedAsRemovable(dest_file_path)); |
[email protected] | 834673b | 2014-02-20 18:34:43 | [diff] [blame] | 495 | |
| 496 | // Pin the existing file. |
| 497 | EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(id)); |
| 498 | |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 499 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry)); |
| 500 | EXPECT_TRUE(entry.file_specific_info().cache_state().is_pinned()); |
Sergei Datsenko | e5a99eb | 2018-08-31 06:30:39 | [diff] [blame] | 501 | EXPECT_FALSE(IsMarkedAsRemovable(dest_file_path)); |
[email protected] | 834673b | 2014-02-20 18:34:43 | [diff] [blame] | 502 | |
| 503 | // Unpin the file. |
| 504 | EXPECT_EQ(FILE_ERROR_OK, cache_->Unpin(id)); |
| 505 | |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 506 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry)); |
| 507 | EXPECT_FALSE(entry.file_specific_info().cache_state().is_pinned()); |
Sergei Datsenko | e5a99eb | 2018-08-31 06:30:39 | [diff] [blame] | 508 | EXPECT_TRUE(IsMarkedAsRemovable(dest_file_path)); |
[email protected] | 834673b | 2014-02-20 18:34:43 | [diff] [blame] | 509 | |
| 510 | // Pin a non-present file. |
| 511 | std::string id_non_present = "id_non_present"; |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 512 | entry.Clear(); |
| 513 | entry.set_local_id(id_non_present); |
| 514 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry)); |
[email protected] | 834673b | 2014-02-20 18:34:43 | [diff] [blame] | 515 | EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(id_non_present)); |
| 516 | |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 517 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_non_present, &entry)); |
| 518 | EXPECT_TRUE(entry.file_specific_info().cache_state().is_pinned()); |
[email protected] | 834673b | 2014-02-20 18:34:43 | [diff] [blame] | 519 | |
| 520 | // Unpin the previously pinned non-existent file. |
| 521 | EXPECT_EQ(FILE_ERROR_OK, cache_->Unpin(id_non_present)); |
| 522 | |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 523 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_non_present, &entry)); |
| 524 | EXPECT_FALSE(entry.file_specific_info().has_cache_state()); |
[email protected] | 834673b | 2014-02-20 18:34:43 | [diff] [blame] | 525 | |
| 526 | // Unpin a file that doesn't exist in cache and is not pinned. |
| 527 | EXPECT_EQ(FILE_ERROR_NOT_FOUND, cache_->Unpin("id_non_existent")); |
| 528 | } |
| 529 | |
| 530 | TEST_F(FileCacheTest, MountUnmount) { |
vabr | 96fd0c0f | 2016-09-13 14:21:31 | [diff] [blame] | 531 | const base::FilePath src_file_path = temp_dir_.GetPath().Append("test.dat"); |
[email protected] | 834673b | 2014-02-20 18:34:43 | [diff] [blame] | 532 | const std::string src_contents = "test"; |
| 533 | EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path, |
| 534 | src_contents)); |
| 535 | std::string id("id_present"); |
| 536 | std::string md5(base::MD5String(src_contents)); |
| 537 | |
| 538 | // Store a file. |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 539 | ResourceEntry entry; |
| 540 | entry.set_local_id(id); |
| 541 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry)); |
[email protected] | 834673b | 2014-02-20 18:34:43 | [diff] [blame] | 542 | EXPECT_EQ(FILE_ERROR_OK, cache_->Store( |
| 543 | id, md5, src_file_path, FileCache::FILE_OPERATION_COPY)); |
| 544 | |
| 545 | // Mark the file mounted. |
| 546 | base::FilePath cache_file_path; |
| 547 | EXPECT_EQ(FILE_ERROR_OK, cache_->MarkAsMounted(id, &cache_file_path)); |
| 548 | |
Tatsuhisa Yamaguchi | 4810128 | 2018-03-20 02:18:39 | [diff] [blame] | 549 | EXPECT_TRUE(cache_->IsMarkedAsMounted(id)); |
| 550 | |
[email protected] | 834673b | 2014-02-20 18:34:43 | [diff] [blame] | 551 | // Try to remove it. |
| 552 | EXPECT_EQ(FILE_ERROR_IN_USE, cache_->Remove(id)); |
| 553 | |
| 554 | // Clear mounted state of the file. |
| 555 | EXPECT_EQ(FILE_ERROR_OK, cache_->MarkAsUnmounted(cache_file_path)); |
| 556 | |
Tatsuhisa Yamaguchi | 4810128 | 2018-03-20 02:18:39 | [diff] [blame] | 557 | EXPECT_FALSE(cache_->IsMarkedAsMounted(id)); |
| 558 | |
[email protected] | 834673b | 2014-02-20 18:34:43 | [diff] [blame] | 559 | // Try to remove again. |
| 560 | EXPECT_EQ(FILE_ERROR_OK, cache_->Remove(id)); |
| 561 | } |
| 562 | |
[email protected] | 8b03ab3a | 2014-01-15 17:52:45 | [diff] [blame] | 563 | TEST_F(FileCacheTest, OpenForWrite) { |
| 564 | // Prepare a file. |
| 565 | base::FilePath src_file; |
vabr | 96fd0c0f | 2016-09-13 14:21:31 | [diff] [blame] | 566 | ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &src_file)); |
[email protected] | 8b03ab3a | 2014-01-15 17:52:45 | [diff] [blame] | 567 | |
| 568 | const std::string id = "id"; |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 569 | ResourceEntry entry; |
| 570 | entry.set_local_id(id); |
| 571 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry)); |
[email protected] | 8b03ab3a | 2014-01-15 17:52:45 | [diff] [blame] | 572 | ASSERT_EQ(FILE_ERROR_OK, cache_->Store(id, "md5", src_file, |
| 573 | FileCache::FILE_OPERATION_COPY)); |
[email protected] | f92367ae | 2014-06-02 07:35:43 | [diff] [blame] | 574 | EXPECT_EQ(0, entry.file_info().last_modified()); |
Shuhei Takahashi | a25ace56 | 2017-09-15 09:22:17 | [diff] [blame] | 575 | EXPECT_EQ(0, entry.last_modified_by_me()); |
[email protected] | 8b03ab3a | 2014-01-15 17:52:45 | [diff] [blame] | 576 | |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 577 | // Entry is not dirty nor opened. |
[email protected] | 8b03ab3a | 2014-01-15 17:52:45 | [diff] [blame] | 578 | EXPECT_FALSE(cache_->IsOpenedForWrite(id)); |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 579 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry)); |
| 580 | EXPECT_FALSE(entry.file_specific_info().cache_state().is_dirty()); |
[email protected] | 8b03ab3a | 2014-01-15 17:52:45 | [diff] [blame] | 581 | |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 582 | const base::FilePath dest_file = GetCacheFilePath(id); |
Sergei Datsenko | e5a99eb | 2018-08-31 06:30:39 | [diff] [blame] | 583 | EXPECT_TRUE(IsMarkedAsRemovable(dest_file)); |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 584 | |
[email protected] | 8b03ab3a | 2014-01-15 17:52:45 | [diff] [blame] | 585 | // Open (1). |
dcheng | f4275023 | 2016-04-12 04:12:27 | [diff] [blame] | 586 | std::unique_ptr<base::ScopedClosureRunner> file_closer1; |
[email protected] | 8b03ab3a | 2014-01-15 17:52:45 | [diff] [blame] | 587 | EXPECT_EQ(FILE_ERROR_OK, cache_->OpenForWrite(id, &file_closer1)); |
| 588 | EXPECT_TRUE(cache_->IsOpenedForWrite(id)); |
| 589 | |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 590 | // Entry is dirty. |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 591 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry)); |
| 592 | EXPECT_TRUE(entry.file_specific_info().cache_state().is_dirty()); |
Sergei Datsenko | e5a99eb | 2018-08-31 06:30:39 | [diff] [blame] | 593 | EXPECT_FALSE(IsMarkedAsRemovable((dest_file))); |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 594 | |
[email protected] | 8b03ab3a | 2014-01-15 17:52:45 | [diff] [blame] | 595 | // Open (2). |
dcheng | f4275023 | 2016-04-12 04:12:27 | [diff] [blame] | 596 | std::unique_ptr<base::ScopedClosureRunner> file_closer2; |
[email protected] | 8b03ab3a | 2014-01-15 17:52:45 | [diff] [blame] | 597 | EXPECT_EQ(FILE_ERROR_OK, cache_->OpenForWrite(id, &file_closer2)); |
| 598 | EXPECT_TRUE(cache_->IsOpenedForWrite(id)); |
| 599 | |
| 600 | // Close (1). |
| 601 | file_closer1.reset(); |
[email protected] | c929c93 | 2014-07-23 06:06:05 | [diff] [blame] | 602 | base::RunLoop().RunUntilIdle(); |
[email protected] | 8b03ab3a | 2014-01-15 17:52:45 | [diff] [blame] | 603 | EXPECT_TRUE(cache_->IsOpenedForWrite(id)); |
| 604 | |
Shuhei Takahashi | a25ace56 | 2017-09-15 09:22:17 | [diff] [blame] | 605 | // last_modified and last_modified_by_me are updated. |
[email protected] | f92367ae | 2014-06-02 07:35:43 | [diff] [blame] | 606 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry)); |
| 607 | EXPECT_NE(0, entry.file_info().last_modified()); |
Shuhei Takahashi | a25ace56 | 2017-09-15 09:22:17 | [diff] [blame] | 608 | EXPECT_NE(0, entry.last_modified_by_me()); |
| 609 | EXPECT_EQ(entry.file_info().last_modified(), entry.last_modified_by_me()); |
[email protected] | f92367ae | 2014-06-02 07:35:43 | [diff] [blame] | 610 | |
[email protected] | 8b03ab3a | 2014-01-15 17:52:45 | [diff] [blame] | 611 | // Close (2). |
| 612 | file_closer2.reset(); |
[email protected] | c929c93 | 2014-07-23 06:06:05 | [diff] [blame] | 613 | base::RunLoop().RunUntilIdle(); |
[email protected] | 8b03ab3a | 2014-01-15 17:52:45 | [diff] [blame] | 614 | EXPECT_FALSE(cache_->IsOpenedForWrite(id)); |
| 615 | |
| 616 | // Try to open non-existent file. |
| 617 | EXPECT_EQ(FILE_ERROR_NOT_FOUND, |
| 618 | cache_->OpenForWrite("nonexistent_id", &file_closer1)); |
| 619 | } |
| 620 | |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 621 | TEST_F(FileCacheTest, UpdateMd5) { |
| 622 | // Store test data. |
vabr | 96fd0c0f | 2016-09-13 14:21:31 | [diff] [blame] | 623 | const base::FilePath src_file_path = temp_dir_.GetPath().Append("test.dat"); |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 624 | const std::string contents_before = "before"; |
| 625 | EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path, |
| 626 | contents_before)); |
| 627 | std::string id("id1"); |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 628 | ResourceEntry entry; |
| 629 | entry.set_local_id(id); |
| 630 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry)); |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 631 | EXPECT_EQ(FILE_ERROR_OK, cache_->Store(id, base::MD5String(contents_before), |
| 632 | src_file_path, |
| 633 | FileCache::FILE_OPERATION_COPY)); |
| 634 | |
| 635 | // Modify the cache file. |
dcheng | f4275023 | 2016-04-12 04:12:27 | [diff] [blame] | 636 | std::unique_ptr<base::ScopedClosureRunner> file_closer; |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 637 | EXPECT_EQ(FILE_ERROR_OK, cache_->OpenForWrite(id, &file_closer)); |
| 638 | base::FilePath cache_file_path; |
| 639 | EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(id, &cache_file_path)); |
| 640 | const std::string contents_after = "after"; |
| 641 | EXPECT_TRUE(google_apis::test_util::WriteStringToFile(cache_file_path, |
| 642 | contents_after)); |
| 643 | |
| 644 | // Cannot update MD5 of an opend file. |
| 645 | EXPECT_EQ(FILE_ERROR_IN_USE, cache_->UpdateMd5(id)); |
| 646 | |
| 647 | // Close file. |
| 648 | file_closer.reset(); |
[email protected] | c929c93 | 2014-07-23 06:06:05 | [diff] [blame] | 649 | base::RunLoop().RunUntilIdle(); |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 650 | |
| 651 | // MD5 was cleared by OpenForWrite(). |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 652 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry)); |
| 653 | EXPECT_TRUE(entry.file_specific_info().cache_state().md5().empty()); |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 654 | |
| 655 | // Update MD5. |
| 656 | EXPECT_EQ(FILE_ERROR_OK, cache_->UpdateMd5(id)); |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 657 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry)); |
| 658 | EXPECT_EQ(base::MD5String(contents_after), |
| 659 | entry.file_specific_info().cache_state().md5()); |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | TEST_F(FileCacheTest, ClearDirty) { |
| 663 | // Prepare a file. |
| 664 | base::FilePath src_file; |
vabr | 96fd0c0f | 2016-09-13 14:21:31 | [diff] [blame] | 665 | ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &src_file)); |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 666 | |
| 667 | const std::string id = "id"; |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 668 | ResourceEntry entry; |
| 669 | entry.set_local_id(id); |
| 670 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry)); |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 671 | ASSERT_EQ(FILE_ERROR_OK, cache_->Store(id, "md5", src_file, |
| 672 | FileCache::FILE_OPERATION_COPY)); |
| 673 | |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 674 | const base::FilePath dest_file = GetCacheFilePath(id); |
Sergei Datsenko | e5a99eb | 2018-08-31 06:30:39 | [diff] [blame] | 675 | EXPECT_TRUE(IsMarkedAsRemovable(dest_file)); |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 676 | |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 677 | // Open the file. |
dcheng | f4275023 | 2016-04-12 04:12:27 | [diff] [blame] | 678 | std::unique_ptr<base::ScopedClosureRunner> file_closer; |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 679 | EXPECT_EQ(FILE_ERROR_OK, cache_->OpenForWrite(id, &file_closer)); |
| 680 | |
| 681 | // Entry is dirty. |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 682 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry)); |
| 683 | EXPECT_TRUE(entry.file_specific_info().cache_state().is_dirty()); |
Sergei Datsenko | e5a99eb | 2018-08-31 06:30:39 | [diff] [blame] | 684 | EXPECT_FALSE(IsMarkedAsRemovable(dest_file)); |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 685 | |
| 686 | // Cannot clear the dirty bit of an opened entry. |
| 687 | EXPECT_EQ(FILE_ERROR_IN_USE, cache_->ClearDirty(id)); |
Sergei Datsenko | e5a99eb | 2018-08-31 06:30:39 | [diff] [blame] | 688 | EXPECT_FALSE(IsMarkedAsRemovable(dest_file)); |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 689 | |
| 690 | // Close the file and clear the dirty bit. |
| 691 | file_closer.reset(); |
[email protected] | c929c93 | 2014-07-23 06:06:05 | [diff] [blame] | 692 | base::RunLoop().RunUntilIdle(); |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 693 | EXPECT_EQ(FILE_ERROR_OK, cache_->ClearDirty(id)); |
| 694 | |
| 695 | // Entry is not dirty. |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 696 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry)); |
| 697 | EXPECT_FALSE(entry.file_specific_info().cache_state().is_dirty()); |
Sergei Datsenko | e5a99eb | 2018-08-31 06:30:39 | [diff] [blame] | 698 | EXPECT_TRUE(IsMarkedAsRemovable(dest_file)); |
[email protected] | b1bf19a | 2014-01-21 04:45:19 | [diff] [blame] | 699 | } |
| 700 | |
[email protected] | 834673b | 2014-02-20 18:34:43 | [diff] [blame] | 701 | TEST_F(FileCacheTest, Remove) { |
vabr | 96fd0c0f | 2016-09-13 14:21:31 | [diff] [blame] | 702 | const base::FilePath src_file_path = temp_dir_.GetPath().Append("test.dat"); |
[email protected] | 834673b | 2014-02-20 18:34:43 | [diff] [blame] | 703 | const std::string src_contents = "test"; |
| 704 | EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path, |
| 705 | src_contents)); |
| 706 | std::string id("id"); |
| 707 | std::string md5(base::MD5String(src_contents)); |
| 708 | |
| 709 | // First store a file to cache. |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 710 | ResourceEntry entry; |
| 711 | entry.set_local_id(id); |
| 712 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry)); |
[email protected] | 834673b | 2014-02-20 18:34:43 | [diff] [blame] | 713 | base::FilePath src_file; |
vabr | 96fd0c0f | 2016-09-13 14:21:31 | [diff] [blame] | 714 | ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &src_file)); |
[email protected] | 834673b | 2014-02-20 18:34:43 | [diff] [blame] | 715 | EXPECT_EQ(FILE_ERROR_OK, cache_->Store( |
| 716 | id, md5, src_file_path, FileCache::FILE_OPERATION_COPY)); |
| 717 | |
| 718 | base::FilePath cache_file_path; |
| 719 | EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(id, &cache_file_path)); |
| 720 | |
| 721 | // Then try to remove existing file from cache. |
| 722 | EXPECT_EQ(FILE_ERROR_OK, cache_->Remove(id)); |
| 723 | EXPECT_FALSE(base::PathExists(cache_file_path)); |
| 724 | } |
| 725 | |
[email protected] | 91a464e6 | 2013-07-10 09:30:06 | [diff] [blame] | 726 | TEST_F(FileCacheTest, RenameCacheFilesToNewFormat) { |
[email protected] | 3e8d64d | 2013-09-19 10:09:05 | [diff] [blame] | 727 | const base::FilePath file_directory = |
vabr | 96fd0c0f | 2016-09-13 14:21:31 | [diff] [blame] | 728 | temp_dir_.GetPath().Append(kCacheFileDirectory); |
[email protected] | 91a464e6 | 2013-07-10 09:30:06 | [diff] [blame] | 729 | |
[email protected] | f2731d1 | 2013-10-22 03:23:15 | [diff] [blame] | 730 | // File with an old style "<prefix>:<ID>.<MD5>" name. |
[email protected] | 91a464e6 | 2013-07-10 09:30:06 | [diff] [blame] | 731 | ASSERT_TRUE(google_apis::test_util::WriteStringToFile( |
[email protected] | f2731d1 | 2013-10-22 03:23:15 | [diff] [blame] | 732 | file_directory.AppendASCII("file:id_koo.md5"), "koo")); |
[email protected] | 91a464e6 | 2013-07-10 09:30:06 | [diff] [blame] | 733 | |
| 734 | // File with multiple extensions should be removed. |
| 735 | ASSERT_TRUE(google_apis::test_util::WriteStringToFile( |
| 736 | file_directory.AppendASCII("id_kyu.md5.mounted"), "kyu (mounted)")); |
| 737 | ASSERT_TRUE(google_apis::test_util::WriteStringToFile( |
| 738 | file_directory.AppendASCII("id_kyu.md5"), "kyu")); |
| 739 | |
| 740 | // Rename and verify the result. |
[email protected] | f2731d1 | 2013-10-22 03:23:15 | [diff] [blame] | 741 | EXPECT_TRUE(RenameCacheFilesToNewFormat(cache_.get())); |
[email protected] | 91a464e6 | 2013-07-10 09:30:06 | [diff] [blame] | 742 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 743 | EXPECT_TRUE(base::ReadFileToString(file_directory.AppendASCII("id_koo"), |
[email protected] | f2731d1 | 2013-10-22 03:23:15 | [diff] [blame] | 744 | &contents)); |
[email protected] | 91a464e6 | 2013-07-10 09:30:06 | [diff] [blame] | 745 | EXPECT_EQ("koo", contents); |
| 746 | contents.clear(); |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 747 | EXPECT_TRUE(base::ReadFileToString(file_directory.AppendASCII("id_kyu"), |
[email protected] | f2731d1 | 2013-10-22 03:23:15 | [diff] [blame] | 748 | &contents)); |
[email protected] | 91a464e6 | 2013-07-10 09:30:06 | [diff] [blame] | 749 | EXPECT_EQ("kyu", contents); |
| 750 | |
| 751 | // Rename again. |
[email protected] | f2731d1 | 2013-10-22 03:23:15 | [diff] [blame] | 752 | EXPECT_TRUE(RenameCacheFilesToNewFormat(cache_.get())); |
[email protected] | 91a464e6 | 2013-07-10 09:30:06 | [diff] [blame] | 753 | |
| 754 | // Files with new style names are not affected. |
| 755 | contents.clear(); |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 756 | EXPECT_TRUE(base::ReadFileToString(file_directory.AppendASCII("id_koo"), |
[email protected] | f2731d1 | 2013-10-22 03:23:15 | [diff] [blame] | 757 | &contents)); |
[email protected] | 91a464e6 | 2013-07-10 09:30:06 | [diff] [blame] | 758 | EXPECT_EQ("koo", contents); |
| 759 | contents.clear(); |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 760 | EXPECT_TRUE(base::ReadFileToString(file_directory.AppendASCII("id_kyu"), |
[email protected] | f2731d1 | 2013-10-22 03:23:15 | [diff] [blame] | 761 | &contents)); |
[email protected] | 91a464e6 | 2013-07-10 09:30:06 | [diff] [blame] | 762 | EXPECT_EQ("kyu", contents); |
| 763 | } |
| 764 | |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 765 | TEST_F(FileCacheTest, FixMetadataAndFileAttributes) { |
yawano | 9fd1e63 | 2016-02-04 09:00:06 | [diff] [blame] | 766 | // Create test files and metadata. |
| 767 | base::FilePath temp_file; |
vabr | 96fd0c0f | 2016-09-13 14:21:31 | [diff] [blame] | 768 | ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &temp_file)); |
yawano | 9fd1e63 | 2016-02-04 09:00:06 | [diff] [blame] | 769 | |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 770 | // Entry A: pinned cache file. |
yawano | 9fd1e63 | 2016-02-04 09:00:06 | [diff] [blame] | 771 | const std::string id_a = "id_a"; |
| 772 | ResourceEntry entry_a; |
| 773 | entry_a.set_local_id(id_a); |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 774 | FileCacheEntry* file_cache_entry_a = |
| 775 | entry_a.mutable_file_specific_info()->mutable_cache_state(); |
| 776 | file_cache_entry_a->set_is_present(true); |
| 777 | file_cache_entry_a->set_is_pinned(true); |
| 778 | file_cache_entry_a->set_is_dirty(false); |
yawano | 9fd1e63 | 2016-02-04 09:00:06 | [diff] [blame] | 779 | ASSERT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry_a)); |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 780 | const base::FilePath file_path_a = GetCacheFilePath(id_a); |
| 781 | ASSERT_TRUE(base::CopyFile(temp_file, file_path_a)); |
yawano | 9fd1e63 | 2016-02-04 09:00:06 | [diff] [blame] | 782 | |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 783 | // Entry B: dirty cache file. |
yawano | 9fd1e63 | 2016-02-04 09:00:06 | [diff] [blame] | 784 | const std::string id_b = "id_b"; |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 785 | ResourceEntry entry_b; |
| 786 | entry_b.set_local_id(id_b); |
| 787 | FileCacheEntry* file_cache_entry_b = |
| 788 | entry_b.mutable_file_specific_info()->mutable_cache_state(); |
| 789 | file_cache_entry_b->set_is_present(true); |
| 790 | file_cache_entry_b->set_is_pinned(false); |
| 791 | file_cache_entry_b->set_is_dirty(true); |
| 792 | ASSERT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry_b)); |
| 793 | const base::FilePath file_path_b = GetCacheFilePath(id_b); |
| 794 | ASSERT_TRUE(base::CopyFile(temp_file, file_path_b)); |
yawano | 9fd1e63 | 2016-02-04 09:00:06 | [diff] [blame] | 795 | |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 796 | // Entry C: not pinned nor dirty cache file. |
yawano | 9fd1e63 | 2016-02-04 09:00:06 | [diff] [blame] | 797 | const std::string id_c = "id_c"; |
| 798 | ResourceEntry entry_c; |
| 799 | entry_c.set_local_id(id_c); |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 800 | FileCacheEntry* file_cache_entry_c = |
| 801 | entry_c.mutable_file_specific_info()->mutable_cache_state(); |
| 802 | file_cache_entry_c->set_is_present(true); |
| 803 | file_cache_entry_c->set_is_pinned(false); |
| 804 | file_cache_entry_c->set_is_dirty(false); |
yawano | 9fd1e63 | 2016-02-04 09:00:06 | [diff] [blame] | 805 | ASSERT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry_c)); |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 806 | const base::FilePath file_path_c = GetCacheFilePath(id_c); |
| 807 | ASSERT_TRUE(base::CopyFile(temp_file, file_path_c)); |
yawano | 9fd1e63 | 2016-02-04 09:00:06 | [diff] [blame] | 808 | |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 809 | // Entry D: pinned cache file somehow having removable flag. |
yawano | 9fd1e63 | 2016-02-04 09:00:06 | [diff] [blame] | 810 | const std::string id_d = "id_d"; |
| 811 | ResourceEntry entry_d; |
| 812 | entry_d.set_local_id(id_d); |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 813 | FileCacheEntry* file_cache_entry_d = |
| 814 | entry_d.mutable_file_specific_info()->mutable_cache_state(); |
| 815 | file_cache_entry_d->set_is_present(true); |
| 816 | file_cache_entry_d->set_is_pinned(true); |
| 817 | file_cache_entry_d->set_is_dirty(false); |
yawano | 9fd1e63 | 2016-02-04 09:00:06 | [diff] [blame] | 818 | ASSERT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry_d)); |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 819 | const base::FilePath file_path_d = GetCacheFilePath(id_d); |
| 820 | ASSERT_TRUE(base::CopyFile(temp_file, file_path_d)); |
yawano | 9fd1e63 | 2016-02-04 09:00:06 | [diff] [blame] | 821 | |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 822 | // Set removable flag. |
| 823 | FileAttributes flags = GetFileAttributes(file_path_d); |
| 824 | ASSERT_GE(flags, 0); |
| 825 | flags |= FS_NODUMP_FL; |
| 826 | base::File file_d(file_path_d, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 827 | ASSERT_EQ(ioctl(file_d.GetPlatformFile(), FS_IOC_SETFLAGS, &flags), 0); |
| 828 | |
| 829 | // Entry E: there is no file; removed by cryptohome. |
yawano | 1c325bf | 2016-04-20 06:37:03 | [diff] [blame] | 830 | const std::string id_e = "id_e"; |
| 831 | ResourceEntry entry_e; |
| 832 | entry_e.set_local_id(id_e); |
| 833 | FileCacheEntry* file_cache_entry_e = |
| 834 | entry_e.mutable_file_specific_info()->mutable_cache_state(); |
| 835 | file_cache_entry_e->set_is_present(true); |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 836 | file_cache_entry_e->set_is_pinned(false); |
yawano | 1c325bf | 2016-04-20 06:37:03 | [diff] [blame] | 837 | file_cache_entry_e->set_is_dirty(false); |
| 838 | ASSERT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry_e)); |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 839 | const base::FilePath file_path_e = GetCacheFilePath(id_e); |
yawano | 1c325bf | 2016-04-20 06:37:03 | [diff] [blame] | 840 | |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 841 | // Entry F: there is a file, but metadata says not. |
yawano | 1c325bf | 2016-04-20 06:37:03 | [diff] [blame] | 842 | const std::string id_f = "id_f"; |
| 843 | ResourceEntry entry_f; |
| 844 | entry_f.set_local_id(id_f); |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 845 | entry_f.mutable_file_specific_info()->mutable_cache_state()->set_is_present( |
| 846 | false); |
yawano | 1c325bf | 2016-04-20 06:37:03 | [diff] [blame] | 847 | ASSERT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry_f)); |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 848 | const base::FilePath file_path_f = GetCacheFilePath(id_f); |
| 849 | ASSERT_TRUE(base::CopyFile(temp_file, file_path_f)); |
yawano | 1c325bf | 2016-04-20 06:37:03 | [diff] [blame] | 850 | |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 851 | // Entry G: no file nor metadata. |
yawano | 1c325bf | 2016-04-20 06:37:03 | [diff] [blame] | 852 | const std::string id_g = "id_g"; |
| 853 | ResourceEntry entry_g; |
| 854 | entry_g.set_local_id(id_g); |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 855 | entry_f.mutable_file_specific_info()->mutable_cache_state()->set_is_present( |
| 856 | false); |
| 857 | ASSERT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry_f)); |
yawano | 1c325bf | 2016-04-20 06:37:03 | [diff] [blame] | 858 | |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 859 | // Initialize fixes inconsistency between metadata and cache file attributes |
| 860 | // as well as adding specific file attributes to the cache directory. |
| 861 | ASSERT_TRUE(cache_->Initialize()); |
yawano | 9fd1e63 | 2016-02-04 09:00:06 | [diff] [blame] | 862 | |
| 863 | // Check result. |
Sergei Datsenko | e5a99eb | 2018-08-31 06:30:39 | [diff] [blame] | 864 | EXPECT_FALSE(IsMarkedAsRemovable(file_path_a)); |
| 865 | EXPECT_FALSE(IsMarkedAsRemovable(file_path_b)); |
| 866 | EXPECT_TRUE(IsMarkedAsRemovable(file_path_c)); |
| 867 | EXPECT_FALSE(IsMarkedAsRemovable(file_path_d)); |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 868 | EXPECT_FALSE(base::PathExists(file_path_f)); |
| 869 | |
| 870 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_e, &entry_e)); |
| 871 | EXPECT_FALSE(entry_e.file_specific_info().cache_state().is_present()); |
| 872 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_f, &entry_f)); |
| 873 | EXPECT_FALSE(entry_f.file_specific_info().cache_state().is_present()); |
| 874 | |
| 875 | // Check the cache dir has appropriate attributes. |
Sergei Datsenko | e5a99eb | 2018-08-31 06:30:39 | [diff] [blame] | 876 | EXPECT_TRUE(HasRemovableFlag(cache_files_dir_)); |
oka | c6aac50 | 2016-05-23 12:16:58 | [diff] [blame] | 877 | EXPECT_GE(getxattr(cache_files_dir_.value().c_str(), |
| 878 | FileCache::kGCacheFilesAttribute, nullptr, 0), 0); |
yawano | 9fd1e63 | 2016-02-04 09:00:06 | [diff] [blame] | 879 | } |
| 880 | |
[email protected] | 823ca971 | 2013-09-13 10:09:09 | [diff] [blame] | 881 | TEST_F(FileCacheTest, ClearAll) { |
[email protected] | a70a91e | 2014-08-04 11:49:20 | [diff] [blame] | 882 | const std::string id("1a2b"); |
[email protected] | 823ca971 | 2013-09-13 10:09:09 | [diff] [blame] | 883 | const std::string md5("abcdef0123456789"); |
| 884 | |
| 885 | // Store an existing file. |
[email protected] | cd8fd37f | 2014-05-20 15:45:21 | [diff] [blame] | 886 | ResourceEntry entry; |
| 887 | entry.set_local_id(id); |
| 888 | EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry)); |
[email protected] | 823ca971 | 2013-09-13 10:09:09 | [diff] [blame] | 889 | base::FilePath src_file; |
vabr | 96fd0c0f | 2016-09-13 14:21:31 | [diff] [blame] | 890 | ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &src_file)); |
[email protected] | 823ca971 | 2013-09-13 10:09:09 | [diff] [blame] | 891 | ASSERT_EQ(FILE_ERROR_OK, |
| 892 | cache_->Store(id, md5, src_file, FileCache::FILE_OPERATION_COPY)); |
| 893 | |
[email protected] | 823ca971 | 2013-09-13 10:09:09 | [diff] [blame] | 894 | // Clear cache. |
| 895 | EXPECT_TRUE(cache_->ClearAll()); |
| 896 | |
| 897 | // Verify that the cache is removed. |
[email protected] | fb4bcfa3 | 2013-12-02 18:55:49 | [diff] [blame] | 898 | EXPECT_TRUE(base::IsDirectoryEmpty(cache_files_dir_)); |
[email protected] | 823ca971 | 2013-09-13 10:09:09 | [diff] [blame] | 899 | } |
| 900 | |
[email protected] | 59c7cdec | 2013-05-07 04:17:13 | [diff] [blame] | 901 | } // namespace internal |
| 902 | } // namespace drive |