| OLD | NEW |
| 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/file_cache.h" | 5 #include "components/drive/file_cache.h" |
| 6 | 6 |
| 7 #include <stddef.h> |
| 8 #include <stdint.h> |
| 9 |
| 7 #include <string> | 10 #include <string> |
| 8 #include <vector> | 11 #include <vector> |
| 9 | 12 |
| 10 #include "base/callback_helpers.h" | 13 #include "base/callback_helpers.h" |
| 11 #include "base/files/file_enumerator.h" | 14 #include "base/files/file_enumerator.h" |
| 12 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
| 13 #include "base/files/scoped_temp_dir.h" | 16 #include "base/files/scoped_temp_dir.h" |
| 14 #include "base/md5.h" | 17 #include "base/md5.h" |
| 15 #include "base/path_service.h" | 18 #include "base/path_service.h" |
| 16 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 const std::string id_pinned = "id_pinned", md5_pinned = "md5_pinned"; | 163 const std::string id_pinned = "id_pinned", md5_pinned = "md5_pinned"; |
| 161 const time_t last_accessed_pinned = 1; | 164 const time_t last_accessed_pinned = 1; |
| 162 const base::FilePath& pinned_path = | 165 const base::FilePath& pinned_path = |
| 163 AddTestEntry(id_pinned, md5_pinned, last_accessed_pinned, src_file); | 166 AddTestEntry(id_pinned, md5_pinned, last_accessed_pinned, src_file); |
| 164 ASSERT_EQ(FILE_ERROR_OK, cache_->Pin(id_pinned)); | 167 ASSERT_EQ(FILE_ERROR_OK, cache_->Pin(id_pinned)); |
| 165 | 168 |
| 166 // Call FreeDiskSpaceIfNeededFor(). | 169 // Call FreeDiskSpaceIfNeededFor(). |
| 167 fake_free_disk_space_getter_->set_default_value(test_util::kLotsOfSpace); | 170 fake_free_disk_space_getter_->set_default_value(test_util::kLotsOfSpace); |
| 168 fake_free_disk_space_getter_->PushFakeValue(0); | 171 fake_free_disk_space_getter_->PushFakeValue(0); |
| 169 fake_free_disk_space_getter_->PushFakeValue(0); | 172 fake_free_disk_space_getter_->PushFakeValue(0); |
| 170 const int64 kNeededBytes = 1; | 173 const int64_t kNeededBytes = 1; |
| 171 EXPECT_TRUE(cache_->FreeDiskSpaceIfNeededFor(kNeededBytes)); | 174 EXPECT_TRUE(cache_->FreeDiskSpaceIfNeededFor(kNeededBytes)); |
| 172 | 175 |
| 173 // Only 'temporary' file gets removed. | 176 // Only 'temporary' file gets removed. |
| 174 ResourceEntry entry; | 177 ResourceEntry entry; |
| 175 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_tmp, &entry)); | 178 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_tmp, &entry)); |
| 176 EXPECT_FALSE(entry.file_specific_info().cache_state().is_present()); | 179 EXPECT_FALSE(entry.file_specific_info().cache_state().is_present()); |
| 177 EXPECT_FALSE(base::PathExists(tmp_path)); | 180 EXPECT_FALSE(base::PathExists(tmp_path)); |
| 178 | 181 |
| 179 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_pinned, &entry)); | 182 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_pinned, &entry)); |
| 180 EXPECT_TRUE(entry.file_specific_info().cache_state().is_present()); | 183 EXPECT_TRUE(entry.file_specific_info().cache_state().is_present()); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 211 | 214 |
| 212 const std::string id_c = "id_c", md5_c = "md5_c"; | 215 const std::string id_c = "id_c", md5_c = "md5_c"; |
| 213 const time_t last_accessed_c = 4; | 216 const time_t last_accessed_c = 4; |
| 214 const base::FilePath& c_path = | 217 const base::FilePath& c_path = |
| 215 AddTestEntry(id_c, md5_c, last_accessed_c, src_file); | 218 AddTestEntry(id_c, md5_c, last_accessed_c, src_file); |
| 216 | 219 |
| 217 // Call FreeDiskSpaceIfNeededFor. | 220 // Call FreeDiskSpaceIfNeededFor. |
| 218 fake_free_disk_space_getter_->set_default_value(test_util::kLotsOfSpace); | 221 fake_free_disk_space_getter_->set_default_value(test_util::kLotsOfSpace); |
| 219 fake_free_disk_space_getter_->PushFakeValue(kMinFreeSpaceInBytes); | 222 fake_free_disk_space_getter_->PushFakeValue(kMinFreeSpaceInBytes); |
| 220 fake_free_disk_space_getter_->PushFakeValue(kMinFreeSpaceInBytes); | 223 fake_free_disk_space_getter_->PushFakeValue(kMinFreeSpaceInBytes); |
| 221 const int64 kNeededBytes = kTemporaryFileSizeInBytes * 3 / 2; | 224 const int64_t kNeededBytes = kTemporaryFileSizeInBytes * 3 / 2; |
| 222 EXPECT_TRUE(cache_->FreeDiskSpaceIfNeededFor(kNeededBytes)); | 225 EXPECT_TRUE(cache_->FreeDiskSpaceIfNeededFor(kNeededBytes)); |
| 223 | 226 |
| 224 // Entry A is evicted. | 227 // Entry A is evicted. |
| 225 ResourceEntry entry; | 228 ResourceEntry entry; |
| 226 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_a, &entry)); | 229 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_a, &entry)); |
| 227 EXPECT_FALSE(entry.file_specific_info().cache_state().is_present()); | 230 EXPECT_FALSE(entry.file_specific_info().cache_state().is_present()); |
| 228 EXPECT_FALSE(base::PathExists(a_path)); | 231 EXPECT_FALSE(base::PathExists(a_path)); |
| 229 | 232 |
| 230 // Pinned entry should not be evicted. | 233 // Pinned entry should not be evicted. |
| 231 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_pinned, &entry)); | 234 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_pinned, &entry)); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 262 | 265 |
| 263 // Remove metadata of entry B. | 266 // Remove metadata of entry B. |
| 264 ASSERT_EQ(FILE_ERROR_OK, metadata_storage_->RemoveEntry(id_b)); | 267 ASSERT_EQ(FILE_ERROR_OK, metadata_storage_->RemoveEntry(id_b)); |
| 265 | 268 |
| 266 // Confirm cache file of entry B exists. | 269 // Confirm cache file of entry B exists. |
| 267 ASSERT_TRUE(base::PathExists(b_path)); | 270 ASSERT_TRUE(base::PathExists(b_path)); |
| 268 | 271 |
| 269 // Run FreeDiskSpaceIfNeededFor. | 272 // Run FreeDiskSpaceIfNeededFor. |
| 270 fake_free_disk_space_getter_->set_default_value(test_util::kLotsOfSpace); | 273 fake_free_disk_space_getter_->set_default_value(test_util::kLotsOfSpace); |
| 271 fake_free_disk_space_getter_->PushFakeValue(kMinFreeSpaceInBytes); | 274 fake_free_disk_space_getter_->PushFakeValue(kMinFreeSpaceInBytes); |
| 272 const int64 kNeededBytes = 1; | 275 const int64_t kNeededBytes = 1; |
| 273 EXPECT_TRUE(cache_->FreeDiskSpaceIfNeededFor(kNeededBytes)); | 276 EXPECT_TRUE(cache_->FreeDiskSpaceIfNeededFor(kNeededBytes)); |
| 274 | 277 |
| 275 // Entry A is not evicted. | 278 // Entry A is not evicted. |
| 276 ResourceEntry entry; | 279 ResourceEntry entry; |
| 277 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_a, &entry)); | 280 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_a, &entry)); |
| 278 EXPECT_TRUE(entry.file_specific_info().cache_state().is_present()); | 281 EXPECT_TRUE(entry.file_specific_info().cache_state().is_present()); |
| 279 EXPECT_TRUE(base::PathExists(a_path)); | 282 EXPECT_TRUE(base::PathExists(a_path)); |
| 280 | 283 |
| 281 // Entry B is evicted. | 284 // Entry B is evicted. |
| 282 EXPECT_EQ(FILE_ERROR_NOT_FOUND, metadata_storage_->GetEntry(id_b, &entry)); | 285 EXPECT_EQ(FILE_ERROR_NOT_FOUND, metadata_storage_->GetEntry(id_b, &entry)); |
| 283 EXPECT_FALSE(base::PathExists(b_path)); | 286 EXPECT_FALSE(base::PathExists(b_path)); |
| 284 } | 287 } |
| 285 | 288 |
| 286 TEST_F(FileCacheTest, TooManyCacheFiles) { | 289 TEST_F(FileCacheTest, TooManyCacheFiles) { |
| 287 const size_t kMaxNumOfEvictedCacheFiles = 50; | 290 const size_t kMaxNumOfEvictedCacheFiles = 50; |
| 288 cache_->SetMaxNumOfEvictedCacheFilesForTest(kMaxNumOfEvictedCacheFiles); | 291 cache_->SetMaxNumOfEvictedCacheFilesForTest(kMaxNumOfEvictedCacheFiles); |
| 289 | 292 |
| 290 // Create temporary file. | 293 // Create temporary file. |
| 291 base::FilePath src_file; | 294 base::FilePath src_file; |
| 292 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file)); | 295 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file)); |
| 293 ASSERT_EQ(kTemporaryFileSizeInBytes, | 296 ASSERT_EQ(kTemporaryFileSizeInBytes, |
| 294 base::WriteFile(src_file, "abcdefghij", kTemporaryFileSizeInBytes)); | 297 base::WriteFile(src_file, "abcdefghij", kTemporaryFileSizeInBytes)); |
| 295 | 298 |
| 296 // Add kNumOfTestFiles=kMaxNumOfEvictedCacheFiles*2 entries. | 299 // Add kNumOfTestFiles=kMaxNumOfEvictedCacheFiles*2 entries. |
| 297 std::vector<base::FilePath> paths; | 300 std::vector<base::FilePath> paths; |
| 298 const int32 kNumOfTestFiles = kMaxNumOfEvictedCacheFiles * 2; | 301 const int32_t kNumOfTestFiles = kMaxNumOfEvictedCacheFiles * 2; |
| 299 for (int i = 0; i < kNumOfTestFiles; ++i) { | 302 for (int i = 0; i < kNumOfTestFiles; ++i) { |
| 300 // Set last accessed in reverse order to the file name. i.e. If you sort | 303 // Set last accessed in reverse order to the file name. i.e. If you sort |
| 301 // files in name-asc order, they will be last access desc order. | 304 // files in name-asc order, they will be last access desc order. |
| 302 paths.push_back(AddTestEntry( | 305 paths.push_back(AddTestEntry( |
| 303 base::StringPrintf("id_%02d", i), base::StringPrintf("md5_%02d", i), | 306 base::StringPrintf("id_%02d", i), base::StringPrintf("md5_%02d", i), |
| 304 kNumOfTestFiles - i /* last accessed */, src_file)); | 307 kNumOfTestFiles - i /* last accessed */, src_file)); |
| 305 } | 308 } |
| 306 | 309 |
| 307 // Confirm cache files of kNumOfTestFiles actually exist. | 310 // Confirm cache files of kNumOfTestFiles actually exist. |
| 308 for (const auto& path : paths) { | 311 for (const auto& path : paths) { |
| 309 ASSERT_TRUE(base::PathExists(path)) << path.value(); | 312 ASSERT_TRUE(base::PathExists(path)) << path.value(); |
| 310 } | 313 } |
| 311 | 314 |
| 312 // Try to free kMaxNumOfEvictedCacheFiles * 3 / 2. | 315 // Try to free kMaxNumOfEvictedCacheFiles * 3 / 2. |
| 313 fake_free_disk_space_getter_->set_default_value(test_util::kLotsOfSpace); | 316 fake_free_disk_space_getter_->set_default_value(test_util::kLotsOfSpace); |
| 314 fake_free_disk_space_getter_->PushFakeValue(kMinFreeSpaceInBytes); | 317 fake_free_disk_space_getter_->PushFakeValue(kMinFreeSpaceInBytes); |
| 315 fake_free_disk_space_getter_->PushFakeValue(kMinFreeSpaceInBytes); | 318 fake_free_disk_space_getter_->PushFakeValue(kMinFreeSpaceInBytes); |
| 316 fake_free_disk_space_getter_->PushFakeValue( | 319 fake_free_disk_space_getter_->PushFakeValue( |
| 317 kMinFreeSpaceInBytes + | 320 kMinFreeSpaceInBytes + |
| 318 (kMaxNumOfEvictedCacheFiles * kTemporaryFileSizeInBytes)); | 321 (kMaxNumOfEvictedCacheFiles * kTemporaryFileSizeInBytes)); |
| 319 const int64 kNeededBytes = | 322 const int64_t kNeededBytes = |
| 320 (kMaxNumOfEvictedCacheFiles * 3 / 2) * kTemporaryFileSizeInBytes; | 323 (kMaxNumOfEvictedCacheFiles * 3 / 2) * kTemporaryFileSizeInBytes; |
| 321 EXPECT_FALSE(cache_->FreeDiskSpaceIfNeededFor(kNeededBytes)); | 324 EXPECT_FALSE(cache_->FreeDiskSpaceIfNeededFor(kNeededBytes)); |
| 322 | 325 |
| 323 for (uint32 i = 0; i < kNumOfTestFiles; ++i) { | 326 for (uint32_t i = 0; i < kNumOfTestFiles; ++i) { |
| 324 // Assert that only first kMaxNumOfEvictedCacheFiles exist. | 327 // Assert that only first kMaxNumOfEvictedCacheFiles exist. |
| 325 ASSERT_EQ(i < kMaxNumOfEvictedCacheFiles, base::PathExists(paths[i])); | 328 ASSERT_EQ(i < kMaxNumOfEvictedCacheFiles, base::PathExists(paths[i])); |
| 326 } | 329 } |
| 327 } | 330 } |
| 328 | 331 |
| 329 TEST_F(FileCacheTest, GetFile) { | 332 TEST_F(FileCacheTest, GetFile) { |
| 330 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat"); | 333 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat"); |
| 331 const std::string src_contents = "test"; | 334 const std::string src_contents = "test"; |
| 332 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path, | 335 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path, |
| 333 src_contents)); | 336 src_contents)); |
| (...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 707 | 710 |
| 708 // Clear cache. | 711 // Clear cache. |
| 709 EXPECT_TRUE(cache_->ClearAll()); | 712 EXPECT_TRUE(cache_->ClearAll()); |
| 710 | 713 |
| 711 // Verify that the cache is removed. | 714 // Verify that the cache is removed. |
| 712 EXPECT_TRUE(base::IsDirectoryEmpty(cache_files_dir_)); | 715 EXPECT_TRUE(base::IsDirectoryEmpty(cache_files_dir_)); |
| 713 } | 716 } |
| 714 | 717 |
| 715 } // namespace internal | 718 } // namespace internal |
| 716 } // namespace drive | 719 } // namespace drive |
| OLD | NEW |