| 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 "chrome/browser/chromeos/drive/file_cache.h" | 5 #include "chrome/browser/chromeos/drive/file_cache.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 void VerifyRemoveFromCache(FileError error, | 160 void VerifyRemoveFromCache(FileError error, |
| 161 const std::string& resource_id, | 161 const std::string& resource_id, |
| 162 const std::string& md5) { | 162 const std::string& md5) { |
| 163 EXPECT_EQ(expected_error_, error); | 163 EXPECT_EQ(expected_error_, error); |
| 164 | 164 |
| 165 FileCacheEntry cache_entry; | 165 FileCacheEntry cache_entry; |
| 166 if (!GetCacheEntryFromOriginThread(resource_id, md5, &cache_entry)) { | 166 if (!GetCacheEntryFromOriginThread(resource_id, md5, &cache_entry)) { |
| 167 EXPECT_EQ(FILE_ERROR_OK, error); | 167 EXPECT_EQ(FILE_ERROR_OK, error); |
| 168 | 168 |
| 169 const base::FilePath path = cache_->GetCacheFilePath(resource_id); | 169 const base::FilePath path = cache_->GetCacheFilePath(resource_id); |
| 170 EXPECT_FALSE(file_util::PathExists(path)); | 170 EXPECT_FALSE(base::PathExists(path)); |
| 171 } | 171 } |
| 172 } | 172 } |
| 173 | 173 |
| 174 void TestPin(const std::string& resource_id, | 174 void TestPin(const std::string& resource_id, |
| 175 FileError expected_error, | 175 FileError expected_error, |
| 176 int expected_cache_state) { | 176 int expected_cache_state) { |
| 177 expected_error_ = expected_error; | 177 expected_error_ = expected_error; |
| 178 expected_cache_state_ = expected_cache_state; | 178 expected_cache_state_ = expected_cache_state; |
| 179 | 179 |
| 180 FileError error = FILE_ERROR_OK; | 180 FileError error = FILE_ERROR_OK; |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 &entry)); | 260 &entry)); |
| 261 | 261 |
| 262 FileError error = FILE_ERROR_OK; | 262 FileError error = FILE_ERROR_OK; |
| 263 base::FilePath cache_file_path; | 263 base::FilePath cache_file_path; |
| 264 cache_->MarkAsMountedOnUIThread( | 264 cache_->MarkAsMountedOnUIThread( |
| 265 resource_id, | 265 resource_id, |
| 266 google_apis::test_util::CreateCopyResultCallback( | 266 google_apis::test_util::CreateCopyResultCallback( |
| 267 &error, &cache_file_path)); | 267 &error, &cache_file_path)); |
| 268 test_util::RunBlockingPoolTask(); | 268 test_util::RunBlockingPoolTask(); |
| 269 | 269 |
| 270 EXPECT_TRUE(file_util::PathExists(cache_file_path)); | 270 EXPECT_TRUE(base::PathExists(cache_file_path)); |
| 271 EXPECT_EQ(cache_file_path, cache_->GetCacheFilePath(resource_id)); | 271 EXPECT_EQ(cache_file_path, cache_->GetCacheFilePath(resource_id)); |
| 272 } | 272 } |
| 273 | 273 |
| 274 void TestMarkAsUnmounted(const std::string& resource_id, | 274 void TestMarkAsUnmounted(const std::string& resource_id, |
| 275 const std::string& md5, | 275 const std::string& md5, |
| 276 const base::FilePath& file_path, | 276 const base::FilePath& file_path, |
| 277 FileError expected_error, | 277 FileError expected_error, |
| 278 int expected_cache_state) { | 278 int expected_cache_state) { |
| 279 expected_error_ = expected_error; | 279 expected_error_ = expected_error; |
| 280 expected_cache_state_ = expected_cache_state; | 280 expected_cache_state_ = expected_cache_state; |
| 281 | 281 |
| 282 FileError error = FILE_ERROR_OK; | 282 FileError error = FILE_ERROR_OK; |
| 283 cache_->MarkAsUnmountedOnUIThread( | 283 cache_->MarkAsUnmountedOnUIThread( |
| 284 file_path, | 284 file_path, |
| 285 google_apis::test_util::CreateCopyResultCallback(&error)); | 285 google_apis::test_util::CreateCopyResultCallback(&error)); |
| 286 test_util::RunBlockingPoolTask(); | 286 test_util::RunBlockingPoolTask(); |
| 287 | 287 |
| 288 base::FilePath cache_file_path; | 288 base::FilePath cache_file_path; |
| 289 cache_->GetFileOnUIThread( | 289 cache_->GetFileOnUIThread( |
| 290 resource_id, md5, | 290 resource_id, md5, |
| 291 google_apis::test_util::CreateCopyResultCallback( | 291 google_apis::test_util::CreateCopyResultCallback( |
| 292 &error, &cache_file_path)); | 292 &error, &cache_file_path)); |
| 293 test_util::RunBlockingPoolTask(); | 293 test_util::RunBlockingPoolTask(); |
| 294 EXPECT_EQ(FILE_ERROR_OK, error); | 294 EXPECT_EQ(FILE_ERROR_OK, error); |
| 295 | 295 |
| 296 EXPECT_TRUE(file_util::PathExists(cache_file_path)); | 296 EXPECT_TRUE(base::PathExists(cache_file_path)); |
| 297 EXPECT_EQ(cache_file_path, cache_->GetCacheFilePath(resource_id)); | 297 EXPECT_EQ(cache_file_path, cache_->GetCacheFilePath(resource_id)); |
| 298 } | 298 } |
| 299 | 299 |
| 300 void VerifyCacheFileState(FileError error, | 300 void VerifyCacheFileState(FileError error, |
| 301 const std::string& resource_id, | 301 const std::string& resource_id, |
| 302 const std::string& md5) { | 302 const std::string& md5) { |
| 303 EXPECT_EQ(expected_error_, error); | 303 EXPECT_EQ(expected_error_, error); |
| 304 | 304 |
| 305 // Verify cache map. | 305 // Verify cache map. |
| 306 FileCacheEntry cache_entry; | 306 FileCacheEntry cache_entry; |
| 307 const bool cache_entry_found = | 307 const bool cache_entry_found = |
| 308 GetCacheEntryFromOriginThread(resource_id, md5, &cache_entry); | 308 GetCacheEntryFromOriginThread(resource_id, md5, &cache_entry); |
| 309 if ((expected_cache_state_ & TEST_CACHE_STATE_PRESENT) || | 309 if ((expected_cache_state_ & TEST_CACHE_STATE_PRESENT) || |
| 310 (expected_cache_state_ & TEST_CACHE_STATE_PINNED)) { | 310 (expected_cache_state_ & TEST_CACHE_STATE_PINNED)) { |
| 311 ASSERT_TRUE(cache_entry_found); | 311 ASSERT_TRUE(cache_entry_found); |
| 312 EXPECT_EQ((expected_cache_state_ & TEST_CACHE_STATE_PINNED) != 0, | 312 EXPECT_EQ((expected_cache_state_ & TEST_CACHE_STATE_PINNED) != 0, |
| 313 cache_entry.is_pinned()); | 313 cache_entry.is_pinned()); |
| 314 EXPECT_EQ((expected_cache_state_ & TEST_CACHE_STATE_PRESENT) != 0, | 314 EXPECT_EQ((expected_cache_state_ & TEST_CACHE_STATE_PRESENT) != 0, |
| 315 cache_entry.is_present()); | 315 cache_entry.is_present()); |
| 316 EXPECT_EQ((expected_cache_state_ & TEST_CACHE_STATE_DIRTY) != 0, | 316 EXPECT_EQ((expected_cache_state_ & TEST_CACHE_STATE_DIRTY) != 0, |
| 317 cache_entry.is_dirty()); | 317 cache_entry.is_dirty()); |
| 318 } else { | 318 } else { |
| 319 EXPECT_FALSE(cache_entry_found); | 319 EXPECT_FALSE(cache_entry_found); |
| 320 } | 320 } |
| 321 | 321 |
| 322 // Verify actual cache file. | 322 // Verify actual cache file. |
| 323 base::FilePath dest_path = cache_->GetCacheFilePath(resource_id); | 323 base::FilePath dest_path = cache_->GetCacheFilePath(resource_id); |
| 324 EXPECT_EQ((expected_cache_state_ & TEST_CACHE_STATE_PRESENT) != 0, | 324 EXPECT_EQ((expected_cache_state_ & TEST_CACHE_STATE_PRESENT) != 0, |
| 325 file_util::PathExists(dest_path)); | 325 base::PathExists(dest_path)); |
| 326 } | 326 } |
| 327 | 327 |
| 328 // Helper function to call GetCacheEntry from origin thread. | 328 // Helper function to call GetCacheEntry from origin thread. |
| 329 bool GetCacheEntryFromOriginThread(const std::string& resource_id, | 329 bool GetCacheEntryFromOriginThread(const std::string& resource_id, |
| 330 const std::string& md5, | 330 const std::string& md5, |
| 331 FileCacheEntry* cache_entry) { | 331 FileCacheEntry* cache_entry) { |
| 332 bool result = false; | 332 bool result = false; |
| 333 cache_->GetCacheEntryOnUIThread( | 333 cache_->GetCacheEntryOnUIThread( |
| 334 resource_id, md5, | 334 resource_id, md5, |
| 335 google_apis::test_util::CreateCopyResultCallback(&result, cache_entry)); | 335 google_apis::test_util::CreateCopyResultCallback(&result, cache_entry)); |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 593 TEST_CACHE_STATE_PRESENT | TEST_CACHE_STATE_DIRTY); | 593 TEST_CACHE_STATE_PRESENT | TEST_CACHE_STATE_DIRTY); |
| 594 | 594 |
| 595 // Verifies dirty file exists. | 595 // Verifies dirty file exists. |
| 596 base::FilePath dirty_path; | 596 base::FilePath dirty_path; |
| 597 FileError error = FILE_ERROR_FAILED; | 597 FileError error = FILE_ERROR_FAILED; |
| 598 cache_->GetFileOnUIThread( | 598 cache_->GetFileOnUIThread( |
| 599 resource_id, md5, | 599 resource_id, md5, |
| 600 google_apis::test_util::CreateCopyResultCallback(&error, &dirty_path)); | 600 google_apis::test_util::CreateCopyResultCallback(&error, &dirty_path)); |
| 601 test_util::RunBlockingPoolTask(); | 601 test_util::RunBlockingPoolTask(); |
| 602 EXPECT_EQ(FILE_ERROR_OK, error); | 602 EXPECT_EQ(FILE_ERROR_OK, error); |
| 603 EXPECT_TRUE(file_util::PathExists(dirty_path)); | 603 EXPECT_TRUE(base::PathExists(dirty_path)); |
| 604 | 604 |
| 605 // Pin the dirty file. | 605 // Pin the dirty file. |
| 606 TestPin(resource_id, FILE_ERROR_OK, | 606 TestPin(resource_id, FILE_ERROR_OK, |
| 607 TEST_CACHE_STATE_PRESENT | | 607 TEST_CACHE_STATE_PRESENT | |
| 608 TEST_CACHE_STATE_DIRTY | | 608 TEST_CACHE_STATE_DIRTY | |
| 609 TEST_CACHE_STATE_PINNED); | 609 TEST_CACHE_STATE_PINNED); |
| 610 | 610 |
| 611 // Verify dirty file still exist at the same pathname. | 611 // Verify dirty file still exist at the same pathname. |
| 612 EXPECT_TRUE(file_util::PathExists(dirty_path)); | 612 EXPECT_TRUE(base::PathExists(dirty_path)); |
| 613 | 613 |
| 614 // Unpin the dirty file. | 614 // Unpin the dirty file. |
| 615 TestUnpin(resource_id, FILE_ERROR_OK, | 615 TestUnpin(resource_id, FILE_ERROR_OK, |
| 616 TEST_CACHE_STATE_PRESENT | TEST_CACHE_STATE_DIRTY); | 616 TEST_CACHE_STATE_PRESENT | TEST_CACHE_STATE_DIRTY); |
| 617 | 617 |
| 618 // Verify dirty file still exist at the same pathname. | 618 // Verify dirty file still exist at the same pathname. |
| 619 EXPECT_TRUE(file_util::PathExists(dirty_path)); | 619 EXPECT_TRUE(base::PathExists(dirty_path)); |
| 620 } | 620 } |
| 621 | 621 |
| 622 TEST_F(FileCacheTestOnUIThread, DirtyCacheRepetitive) { | 622 TEST_F(FileCacheTestOnUIThread, DirtyCacheRepetitive) { |
| 623 std::string resource_id("pdf:1a2b"); | 623 std::string resource_id("pdf:1a2b"); |
| 624 std::string md5("abcdef0123456789"); | 624 std::string md5("abcdef0123456789"); |
| 625 | 625 |
| 626 // First store a file to cache. | 626 // First store a file to cache. |
| 627 TestStoreToCache(resource_id, md5, dummy_file_path_, | 627 TestStoreToCache(resource_id, md5, dummy_file_path_, |
| 628 FILE_ERROR_OK, TEST_CACHE_STATE_PRESENT); | 628 FILE_ERROR_OK, TEST_CACHE_STATE_PRESENT); |
| 629 | 629 |
| (...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 908 | 908 |
| 909 // Call FreeDiskSpaceIfNeededFor(). | 909 // Call FreeDiskSpaceIfNeededFor(). |
| 910 fake_free_disk_space_getter_->set_default_value(test_util::kLotsOfSpace); | 910 fake_free_disk_space_getter_->set_default_value(test_util::kLotsOfSpace); |
| 911 fake_free_disk_space_getter_->PushFakeValue(0); | 911 fake_free_disk_space_getter_->PushFakeValue(0); |
| 912 const int64 kNeededBytes = 1; | 912 const int64 kNeededBytes = 1; |
| 913 EXPECT_TRUE(cache_->FreeDiskSpaceIfNeededFor(kNeededBytes)); | 913 EXPECT_TRUE(cache_->FreeDiskSpaceIfNeededFor(kNeededBytes)); |
| 914 | 914 |
| 915 // Only 'temporary' file gets removed. | 915 // Only 'temporary' file gets removed. |
| 916 FileCacheEntry entry; | 916 FileCacheEntry entry; |
| 917 EXPECT_FALSE(cache_->GetCacheEntry(resource_id_tmp, md5_tmp, &entry)); | 917 EXPECT_FALSE(cache_->GetCacheEntry(resource_id_tmp, md5_tmp, &entry)); |
| 918 EXPECT_FALSE(file_util::PathExists(tmp_path)); | 918 EXPECT_FALSE(base::PathExists(tmp_path)); |
| 919 | 919 |
| 920 EXPECT_TRUE(cache_->GetCacheEntry(resource_id_pinned, md5_pinned, &entry)); | 920 EXPECT_TRUE(cache_->GetCacheEntry(resource_id_pinned, md5_pinned, &entry)); |
| 921 EXPECT_TRUE(file_util::PathExists(pinned_path)); | 921 EXPECT_TRUE(base::PathExists(pinned_path)); |
| 922 | 922 |
| 923 // Returns false when disk space cannot be freed. | 923 // Returns false when disk space cannot be freed. |
| 924 fake_free_disk_space_getter_->set_default_value(0); | 924 fake_free_disk_space_getter_->set_default_value(0); |
| 925 EXPECT_FALSE(cache_->FreeDiskSpaceIfNeededFor(kNeededBytes)); | 925 EXPECT_FALSE(cache_->FreeDiskSpaceIfNeededFor(kNeededBytes)); |
| 926 } | 926 } |
| 927 | 927 |
| 928 TEST_F(FileCacheTest, ImportOldDB) { | 928 TEST_F(FileCacheTest, ImportOldDB) { |
| 929 const base::FilePath old_db_path = temp_dir_.path().AppendASCII("old_db.db"); | 929 const base::FilePath old_db_path = temp_dir_.path().AppendASCII("old_db.db"); |
| 930 | 930 |
| 931 const std::string key1 = "key1"; | 931 const std::string key1 = "key1"; |
| 932 const std::string md5_1 = "md5_1"; | 932 const std::string md5_1 = "md5_1"; |
| 933 const std::string key2 = "key2"; | 933 const std::string key2 = "key2"; |
| 934 const std::string md5_2 = "md5_2"; | 934 const std::string md5_2 = "md5_2"; |
| 935 | 935 |
| 936 // Set up data to be imported. | 936 // Set up data to be imported. |
| 937 { | 937 { |
| 938 FileCacheMetadata old_metadata(NULL); | 938 FileCacheMetadata old_metadata(NULL); |
| 939 ASSERT_TRUE(old_metadata.Initialize(old_db_path)); | 939 ASSERT_TRUE(old_metadata.Initialize(old_db_path)); |
| 940 | 940 |
| 941 FileCacheEntry entry; | 941 FileCacheEntry entry; |
| 942 entry.set_md5(md5_1); | 942 entry.set_md5(md5_1); |
| 943 old_metadata.AddOrUpdateCacheEntry(key1, entry); | 943 old_metadata.AddOrUpdateCacheEntry(key1, entry); |
| 944 | 944 |
| 945 entry.set_md5(md5_2); | 945 entry.set_md5(md5_2); |
| 946 old_metadata.AddOrUpdateCacheEntry(key2, entry); | 946 old_metadata.AddOrUpdateCacheEntry(key2, entry); |
| 947 } | 947 } |
| 948 EXPECT_TRUE(file_util::PathExists(old_db_path)); | 948 EXPECT_TRUE(base::PathExists(old_db_path)); |
| 949 | 949 |
| 950 // Do import. | 950 // Do import. |
| 951 EXPECT_TRUE(ImportOldDB(cache_.get(), old_db_path)); | 951 EXPECT_TRUE(ImportOldDB(cache_.get(), old_db_path)); |
| 952 | 952 |
| 953 // Old DB should be removed. | 953 // Old DB should be removed. |
| 954 EXPECT_FALSE(file_util::PathExists(old_db_path)); | 954 EXPECT_FALSE(base::PathExists(old_db_path)); |
| 955 | 955 |
| 956 // Data is imported correctly. | 956 // Data is imported correctly. |
| 957 FileCacheEntry entry; | 957 FileCacheEntry entry; |
| 958 EXPECT_TRUE(cache_->GetCacheEntry(key1, std::string(), &entry)); | 958 EXPECT_TRUE(cache_->GetCacheEntry(key1, std::string(), &entry)); |
| 959 EXPECT_EQ(md5_1, entry.md5()); | 959 EXPECT_EQ(md5_1, entry.md5()); |
| 960 EXPECT_TRUE(cache_->GetCacheEntry(key2, std::string(), &entry)); | 960 EXPECT_TRUE(cache_->GetCacheEntry(key2, std::string(), &entry)); |
| 961 EXPECT_EQ(md5_2, entry.md5()); | 961 EXPECT_EQ(md5_2, entry.md5()); |
| 962 } | 962 } |
| 963 | 963 |
| 964 TEST_F(FileCacheTest, RenameCacheFilesToNewFormat) { | 964 TEST_F(FileCacheTest, RenameCacheFilesToNewFormat) { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 995 &contents)); | 995 &contents)); |
| 996 EXPECT_EQ("koo", contents); | 996 EXPECT_EQ("koo", contents); |
| 997 contents.clear(); | 997 contents.clear(); |
| 998 EXPECT_TRUE(file_util::ReadFileToString(file_directory.AppendASCII("id_kyu"), | 998 EXPECT_TRUE(file_util::ReadFileToString(file_directory.AppendASCII("id_kyu"), |
| 999 &contents)); | 999 &contents)); |
| 1000 EXPECT_EQ("kyu", contents); | 1000 EXPECT_EQ("kyu", contents); |
| 1001 } | 1001 } |
| 1002 | 1002 |
| 1003 } // namespace internal | 1003 } // namespace internal |
| 1004 } // namespace drive | 1004 } // namespace drive |
| OLD | NEW |