| 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 <errno.h> | 5 #include <errno.h> |
| 6 #include <string> | 6 #include <string> |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 // Expected CacheSubDirectoryType of cached file. | 64 // Expected CacheSubDirectoryType of cached file. |
| 65 gdata::GDataRootDirectory::CacheSubDirectoryType expected_sub_dir_type; | 65 gdata::GDataRootDirectory::CacheSubDirectoryType expected_sub_dir_type; |
| 66 } const initial_cache_resources[] = { | 66 } const initial_cache_resources[] = { |
| 67 // Cache resource in tmp dir, i.e. not pinned or dirty. | 67 // Cache resource in tmp dir, i.e. not pinned or dirty. |
| 68 { "root_feed.json", "tmp:resource_id", "md5_tmp_alphanumeric", | 68 { "root_feed.json", "tmp:resource_id", "md5_tmp_alphanumeric", |
| 69 gdata::GDataFile::CACHE_STATE_PRESENT, | 69 gdata::GDataFile::CACHE_STATE_PRESENT, |
| 70 "md5_tmp_alphanumeric", gdata::GDataRootDirectory::CACHE_TYPE_TMP }, | 70 "md5_tmp_alphanumeric", gdata::GDataRootDirectory::CACHE_TYPE_TMP }, |
| 71 // Cache resource in tmp dir, i.e. not pinned or dirty, with resource_id | 71 // Cache resource in tmp dir, i.e. not pinned or dirty, with resource_id |
| 72 // containing non-alphanumeric characters, to test resource_id is escaped and | 72 // containing non-alphanumeric characters, to test resource_id is escaped and |
| 73 // unescaped correctly. | 73 // unescaped correctly. |
| 74 { "subdir_feed.json", "tmp:`~!@#$%^&*()-_=+[{|]}\\;',<.>/?", | 74 { "subdir_feed.json", "tmp:`~!@#$%^&*()-_=+[{|]}\\;',<>/?", |
| 75 "md5_tmp_non_alphanumeric", | 75 "md5_tmp_non_alphanumeric", |
| 76 gdata::GDataFile::CACHE_STATE_PRESENT, | 76 gdata::GDataFile::CACHE_STATE_PRESENT, |
| 77 "md5_tmp_non_alphanumeric", gdata::GDataRootDirectory::CACHE_TYPE_TMP }, | 77 "md5_tmp_non_alphanumeric", gdata::GDataRootDirectory::CACHE_TYPE_TMP }, |
| 78 // Cache resource that is pinned, to test a pinned file is in persistent dir | 78 // Cache resource that is pinned, to test a pinned file is in persistent dir |
| 79 // with a symlink in pinned dir referencing it. | 79 // with a symlink in pinned dir referencing it. |
| 80 { "directory_entry_atom.json", "pinned:existing", "md5_pinned_existing", | 80 { "directory_entry_atom.json", "pinned:existing", "md5_pinned_existing", |
| 81 gdata::GDataFile::CACHE_STATE_PRESENT | | 81 gdata::GDataFile::CACHE_STATE_PRESENT | |
| 82 gdata::GDataFile::CACHE_STATE_PINNED, | 82 gdata::GDataFile::CACHE_STATE_PINNED, |
| 83 "md5_pinned_existing", gdata::GDataRootDirectory::CACHE_TYPE_PERSISTENT }, | 83 "md5_pinned_existing", gdata::GDataRootDirectory::CACHE_TYPE_PERSISTENT }, |
| 84 // Cache resource with a non-existent source file that is pinned, to test that | 84 // Cache resource with a non-existent source file that is pinned, to test that |
| (...skipping 546 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 631 expected_sub_dir_type_ = expected_sub_dir_type; | 631 expected_sub_dir_type_ = expected_sub_dir_type; |
| 632 expect_outgoing_symlink_ = false; | 632 expect_outgoing_symlink_ = false; |
| 633 | 633 |
| 634 file_system_->ClearDirtyInCache(resource_id, md5, | 634 file_system_->ClearDirtyInCache(resource_id, md5, |
| 635 base::Bind(&GDataFileSystemTest::VerifyCacheFileState, | 635 base::Bind(&GDataFileSystemTest::VerifyCacheFileState, |
| 636 base::Unretained(this))); | 636 base::Unretained(this))); |
| 637 | 637 |
| 638 RunAllPendingForIO(); | 638 RunAllPendingForIO(); |
| 639 } | 639 } |
| 640 | 640 |
| 641 void VerifySetMountedState(const std::string& resource_id, |
| 642 const std::string& md5, |
| 643 bool to_mount, |
| 644 base::PlatformFileError error, |
| 645 const FilePath& file_path) { |
| 646 ++num_callback_invocations_; |
| 647 EXPECT_TRUE(file_util::PathExists(file_path)); |
| 648 EXPECT_TRUE(file_path == file_system_->GetCacheFilePath( |
| 649 resource_id, |
| 650 md5, |
| 651 expected_sub_dir_type_, |
| 652 to_mount ? |
| 653 GDataFileSystemInterface::CACHED_FILE_MOUNTED : |
| 654 GDataFileSystemInterface::CACHED_FILE_FROM_SERVER)); |
| 655 } |
| 656 |
| 657 void TestSetMountedState( |
| 658 const std::string& resource_id, |
| 659 const std::string& md5, |
| 660 const FilePath& file_path, |
| 661 bool to_mount, |
| 662 base::PlatformFileError expected_error, |
| 663 int expected_cache_state, |
| 664 GDataRootDirectory::CacheSubDirectoryType expected_sub_dir_type) { |
| 665 expected_error_ = expected_error; |
| 666 expected_cache_state_ = expected_cache_state; |
| 667 expected_sub_dir_type_ = expected_sub_dir_type; |
| 668 expect_outgoing_symlink_ = false; |
| 669 |
| 670 file_system_->SetMountedState(file_path, to_mount, |
| 671 base::Bind(&GDataFileSystemTest::VerifySetMountedState, |
| 672 base::Unretained(this), resource_id, md5, to_mount)); |
| 673 |
| 674 RunAllPendingForIO(); |
| 675 } |
| 676 |
| 641 void PrepareForInitCacheTest() { | 677 void PrepareForInitCacheTest() { |
| 642 // Create gdata cache sub directories. | 678 // Create gdata cache sub directories. |
| 643 ASSERT_TRUE(file_util::CreateDirectory( | 679 ASSERT_TRUE(file_util::CreateDirectory( |
| 644 file_system_->cache_paths_[GDataRootDirectory::CACHE_TYPE_PERSISTENT])); | 680 file_system_->cache_paths_[GDataRootDirectory::CACHE_TYPE_PERSISTENT])); |
| 645 ASSERT_TRUE(file_util::CreateDirectory( | 681 ASSERT_TRUE(file_util::CreateDirectory( |
| 646 file_system_->cache_paths_[GDataRootDirectory::CACHE_TYPE_TMP])); | 682 file_system_->cache_paths_[GDataRootDirectory::CACHE_TYPE_TMP])); |
| 647 ASSERT_TRUE(file_util::CreateDirectory( | 683 ASSERT_TRUE(file_util::CreateDirectory( |
| 648 file_system_->cache_paths_[GDataRootDirectory::CACHE_TYPE_PINNED])); | 684 file_system_->cache_paths_[GDataRootDirectory::CACHE_TYPE_PINNED])); |
| 649 ASSERT_TRUE(file_util::CreateDirectory( | 685 ASSERT_TRUE(file_util::CreateDirectory( |
| 650 file_system_->cache_paths_[GDataRootDirectory::CACHE_TYPE_OUTGOING])); | 686 file_system_->cache_paths_[GDataRootDirectory::CACHE_TYPE_OUTGOING])); |
| (...skipping 2364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3015 EXPECT_EQ(12345, callback_helper_->quota_bytes_total_); | 3051 EXPECT_EQ(12345, callback_helper_->quota_bytes_total_); |
| 3016 | 3052 |
| 3017 // Verify account meta feed is saved to cache. | 3053 // Verify account meta feed is saved to cache. |
| 3018 RunAllPendingForIO(); | 3054 RunAllPendingForIO(); |
| 3019 FilePath path = file_system_->cache_paths_[ | 3055 FilePath path = file_system_->cache_paths_[ |
| 3020 GDataRootDirectory::CACHE_TYPE_META].Append( | 3056 GDataRootDirectory::CACHE_TYPE_META].Append( |
| 3021 FILE_PATH_LITERAL("account_metadata.json")); | 3057 FILE_PATH_LITERAL("account_metadata.json")); |
| 3022 EXPECT_TRUE(file_util::PathExists(path)); | 3058 EXPECT_TRUE(file_util::PathExists(path)); |
| 3023 } | 3059 } |
| 3024 | 3060 |
| 3061 TEST_F(GDataFileSystemTest, MountUnmount) { |
| 3062 EXPECT_CALL(*mock_sync_client_, OnCacheInitialized()).Times(1); |
| 3063 |
| 3064 FilePath file_path; |
| 3065 std::string resource_id("pdf:1a2b"); |
| 3066 std::string md5("abcdef0123456789"); |
| 3067 |
| 3068 // First store a file to cache in the tmp subdir. |
| 3069 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), |
| 3070 base::PLATFORM_FILE_OK, GDataFile::CACHE_STATE_PRESENT, |
| 3071 GDataRootDirectory::CACHE_TYPE_TMP); |
| 3072 |
| 3073 // Mark the file mounted. |
| 3074 num_callback_invocations_ = 0; |
| 3075 file_path = file_system_->GetCacheFilePath( |
| 3076 resource_id, md5, |
| 3077 GDataRootDirectory::CACHE_TYPE_TMP, |
| 3078 GDataFileSystemInterface::CACHED_FILE_FROM_SERVER); |
| 3079 TestSetMountedState(resource_id, md5, file_path, true, |
| 3080 base::PLATFORM_FILE_OK, |
| 3081 GDataFile::CACHE_STATE_PRESENT | |
| 3082 GDataFile::CACHE_STATE_MOUNTED, |
| 3083 GDataRootDirectory::CACHE_TYPE_PERSISTENT); |
| 3084 EXPECT_EQ(1, num_callback_invocations_); |
| 3085 EXPECT_TRUE(CacheEntryExists(resource_id, md5)); |
| 3086 |
| 3087 // Clear mounted state of the file. |
| 3088 num_callback_invocations_ = 0; |
| 3089 file_path = file_system_->GetCacheFilePath( |
| 3090 resource_id, |
| 3091 md5, |
| 3092 GDataRootDirectory::CACHE_TYPE_PERSISTENT, |
| 3093 GDataFileSystemInterface::CACHED_FILE_MOUNTED); |
| 3094 TestSetMountedState(resource_id, md5, file_path, false, |
| 3095 base::PLATFORM_FILE_OK, |
| 3096 GDataFile::CACHE_STATE_PRESENT, |
| 3097 GDataRootDirectory::CACHE_TYPE_TMP); |
| 3098 EXPECT_EQ(1, num_callback_invocations_); |
| 3099 EXPECT_TRUE(CacheEntryExists(resource_id, md5)); |
| 3100 |
| 3101 // Try to remove the file. |
| 3102 num_callback_invocations_ = 0; |
| 3103 TestRemoveFromCache(resource_id, base::PLATFORM_FILE_OK); |
| 3104 EXPECT_EQ(1, num_callback_invocations_); |
| 3105 } |
| 3106 |
| 3025 } // namespace gdata | 3107 } // namespace gdata |
| OLD | NEW |