| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 | |
| 5 #include "base/file_path.h" | |
| 6 #include "base/memory/ref_counted.h" | |
| 7 #include "base/memory/scoped_ptr.h" | |
| 8 #include "base/platform_file.h" | |
| 9 #include "base/scoped_temp_dir.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | |
| 11 #include "webkit/fileapi/file_system_context.h" | |
| 12 #include "webkit/fileapi/file_system_operation_context.h" | |
| 13 #include "webkit/fileapi/file_system_test_helper.h" | |
| 14 #include "webkit/fileapi/obfuscated_file_system_file_util.h" | |
| 15 | |
| 16 using namespace fileapi; | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 struct CopyMoveTestCaseRecord { | |
| 21 bool is_directory; | |
| 22 const FilePath::CharType path[64]; | |
| 23 int64 data_file_size; | |
| 24 }; | |
| 25 | |
| 26 const CopyMoveTestCaseRecord kCopyMoveTestCases[] = { | |
| 27 {true, FILE_PATH_LITERAL("dir a"), 0}, | |
| 28 {true, FILE_PATH_LITERAL("dir a/dir a"), 0}, | |
| 29 {true, FILE_PATH_LITERAL("dir a/dir d"), 0}, | |
| 30 {true, FILE_PATH_LITERAL("dir a/dir d/dir e"), 0}, | |
| 31 {true, FILE_PATH_LITERAL("dir a/dir d/dir e/dir f"), 0}, | |
| 32 {true, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g"), 0}, | |
| 33 {true, FILE_PATH_LITERAL("dir a/dir d/dir e/dir h"), 0}, | |
| 34 {true, FILE_PATH_LITERAL("dir b"), 0}, | |
| 35 {true, FILE_PATH_LITERAL("dir b/dir a"), 0}, | |
| 36 {true, FILE_PATH_LITERAL("dir c"), 0}, | |
| 37 {false, FILE_PATH_LITERAL("file 0"), 38}, | |
| 38 {false, FILE_PATH_LITERAL("file 2"), 60}, | |
| 39 {false, FILE_PATH_LITERAL("file 3"), 0}, | |
| 40 {false, FILE_PATH_LITERAL("dir a/file 0"), 39}, | |
| 41 {false, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g/file 0"), 40}, | |
| 42 {false, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g/file 1"), 41}, | |
| 43 {false, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g/file 2"), 42}, | |
| 44 {false, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g/file 3"), 50}, | |
| 45 }; | |
| 46 | |
| 47 } // namespace (anonymous) | |
| 48 | |
| 49 // This is not yet a full unit test for FileSystemFileUtil. TODO(ericu): Adapt | |
| 50 // the other subclasses' unit tests, as mentioned in the comments in | |
| 51 // ObfuscatedFileSystemFileUtil's unit test. | |
| 52 // Currently this is just a test of cross-filesystem copy and move, which | |
| 53 // actually exercises subclasses of FileSystemFileUtil as well as the class | |
| 54 // itself. We currently only test copies between obfuscated filesystems. | |
| 55 // TODO(ericu): Add a test for copying between obfuscated and local filesystems, | |
| 56 // and between different local filesystems. | |
| 57 class FileSystemFileUtilTest : public testing::Test { | |
| 58 public: | |
| 59 FileSystemFileUtilTest() { | |
| 60 } | |
| 61 | |
| 62 void SetUp() { | |
| 63 } | |
| 64 | |
| 65 FileSystemOperationContext* NewContext(FileSystemTestOriginHelper* helper) { | |
| 66 FileSystemOperationContext* context = helper->NewOperationContext(); | |
| 67 // We need to allocate quota for paths for | |
| 68 // TestCrossFileSystemCopyMoveHelper, since it calls into OFSFU, which | |
| 69 // charges quota for paths. | |
| 70 context->set_allowed_bytes_growth(1024 * 1024); | |
| 71 return context; | |
| 72 } | |
| 73 | |
| 74 void TestCrossFileSystemCopyMoveHelper( | |
| 75 const GURL& src_origin, fileapi::FileSystemType src_type, | |
| 76 const GURL& dest_origin, fileapi::FileSystemType dest_type, | |
| 77 bool copy) { | |
| 78 ScopedTempDir base_dir; | |
| 79 ASSERT_TRUE(base_dir.CreateUniqueTempDir()); | |
| 80 scoped_refptr<ObfuscatedFileSystemFileUtil> file_util( | |
| 81 new ObfuscatedFileSystemFileUtil(base_dir.path(), | |
| 82 new FileSystemFileUtil())); | |
| 83 FileSystemTestOriginHelper src_helper(src_origin, src_type); | |
| 84 src_helper.SetUp(base_dir.path(), | |
| 85 false, // incognito | |
| 86 false, // unlimited quota | |
| 87 NULL, // quota::QuotaManagerProxy | |
| 88 file_util.get()); | |
| 89 FileSystemTestOriginHelper dest_helper(dest_origin, dest_type); | |
| 90 dest_helper.SetUp(src_helper.file_system_context(), NULL); | |
| 91 | |
| 92 // Set up all the source data. | |
| 93 scoped_ptr<FileSystemOperationContext> context; | |
| 94 FilePath test_root(FILE_PATH_LITERAL("root directory")); | |
| 95 for (size_t i = 0; i < arraysize(kCopyMoveTestCases); ++i) { | |
| 96 const CopyMoveTestCaseRecord& test_case = kCopyMoveTestCases[i]; | |
| 97 FilePath path = test_root.Append(test_case.path); | |
| 98 if (test_case.is_directory) { | |
| 99 context.reset(NewContext(&src_helper)); | |
| 100 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
| 101 file_util->CreateDirectory(context.get(), path, true, true)); | |
| 102 } else { | |
| 103 context.reset(NewContext(&src_helper)); | |
| 104 bool created = false; | |
| 105 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
| 106 file_util->EnsureFileExists(context.get(), path, &created)); | |
| 107 ASSERT_TRUE(created); | |
| 108 context.reset(NewContext(&src_helper)); | |
| 109 ASSERT_EQ(base::PLATFORM_FILE_OK, file_util->Truncate( | |
| 110 context.get(), path, test_case.data_file_size)); | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 // Do the actual copy or move. | |
| 115 FileSystemContext* file_system_context = dest_helper.file_system_context(); | |
| 116 scoped_ptr<FileSystemOperationContext> copy_context( | |
| 117 new FileSystemOperationContext(file_system_context, NULL)); | |
| 118 copy_context->set_src_file_system_file_util(file_util); | |
| 119 copy_context->set_dest_file_system_file_util(file_util); | |
| 120 copy_context->set_src_origin_url(src_helper.origin()); | |
| 121 copy_context->set_dest_origin_url(dest_helper.origin()); | |
| 122 copy_context->set_src_type(src_helper.type()); | |
| 123 copy_context->set_dest_type(dest_helper.type()); | |
| 124 copy_context->set_allowed_bytes_growth(1024 * 1024); // OFSFU path quota. | |
| 125 | |
| 126 if (copy) | |
| 127 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
| 128 file_util->Copy(copy_context.get(), test_root, test_root)); | |
| 129 else | |
| 130 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
| 131 file_util->Move(copy_context.get(), test_root, test_root)); | |
| 132 | |
| 133 // Validate that the destination paths are correct. | |
| 134 for (size_t i = 0; i < arraysize(kCopyMoveTestCases); ++i) { | |
| 135 const CopyMoveTestCaseRecord& test_case = kCopyMoveTestCases[i]; | |
| 136 FilePath path = test_root.Append(test_case.path); | |
| 137 | |
| 138 base::PlatformFileInfo dest_file_info; | |
| 139 FilePath data_path; | |
| 140 context.reset(NewContext(&dest_helper)); | |
| 141 EXPECT_EQ(base::PLATFORM_FILE_OK, | |
| 142 file_util->GetFileInfo( | |
| 143 context.get(), path, &dest_file_info, &data_path)); | |
| 144 if (test_case.is_directory) { | |
| 145 EXPECT_TRUE(dest_file_info.is_directory); | |
| 146 } else { | |
| 147 base::PlatformFileInfo platform_file_info; | |
| 148 ASSERT_TRUE(file_util::GetFileInfo(data_path, &platform_file_info)); | |
| 149 EXPECT_EQ(test_case.data_file_size, platform_file_info.size); | |
| 150 EXPECT_FALSE(platform_file_info.is_directory); | |
| 151 EXPECT_EQ(platform_file_info.size, dest_file_info.size); | |
| 152 EXPECT_FALSE(dest_file_info.is_directory); | |
| 153 } | |
| 154 } | |
| 155 | |
| 156 // Validate that the source paths are still there [for a copy] or gone [for | |
| 157 // a move]. | |
| 158 for (size_t i = 0; i < arraysize(kCopyMoveTestCases); ++i) { | |
| 159 const CopyMoveTestCaseRecord& test_case = kCopyMoveTestCases[i]; | |
| 160 FilePath path = test_root.Append(test_case.path); | |
| 161 base::PlatformFileInfo src_file_info; | |
| 162 FilePath data_path; | |
| 163 context.reset(NewContext(&src_helper)); | |
| 164 base::PlatformFileError expected_result; | |
| 165 if (copy) | |
| 166 expected_result = base::PLATFORM_FILE_OK; | |
| 167 else | |
| 168 expected_result = base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 169 EXPECT_EQ(expected_result, | |
| 170 file_util->GetFileInfo( | |
| 171 context.get(), path, &src_file_info, &data_path)); | |
| 172 } | |
| 173 } | |
| 174 | |
| 175 private: | |
| 176 DISALLOW_COPY_AND_ASSIGN(FileSystemFileUtilTest); | |
| 177 }; | |
| 178 | |
| 179 TEST_F(FileSystemFileUtilTest, TestCrossFileSystemCopyDifferentOrigins) { | |
| 180 GURL src_origin("http://www.example.com"); | |
| 181 fileapi::FileSystemType type = kFileSystemTypePersistent; | |
| 182 GURL dest_origin("http://www.not.the.same.domain.com"); | |
| 183 | |
| 184 TestCrossFileSystemCopyMoveHelper(src_origin, type, dest_origin, type, true); | |
| 185 } | |
| 186 | |
| 187 TEST_F(FileSystemFileUtilTest, TestCrossFileSystemCopySameOrigin) { | |
| 188 GURL origin("http://www.example.com"); | |
| 189 fileapi::FileSystemType src_type = kFileSystemTypePersistent; | |
| 190 fileapi::FileSystemType dest_type = kFileSystemTypeTemporary; | |
| 191 | |
| 192 TestCrossFileSystemCopyMoveHelper(origin, src_type, origin, dest_type, true); | |
| 193 } | |
| 194 | |
| 195 TEST_F(FileSystemFileUtilTest, TestCrossFileSystemMoveDifferentOrigins) { | |
| 196 GURL src_origin("http://www.example.com"); | |
| 197 fileapi::FileSystemType type = kFileSystemTypePersistent; | |
| 198 GURL dest_origin("http://www.not.the.same.domain.com"); | |
| 199 | |
| 200 TestCrossFileSystemCopyMoveHelper(src_origin, type, dest_origin, type, false); | |
| 201 } | |
| 202 | |
| 203 TEST_F(FileSystemFileUtilTest, TestCrossFileSystemMoveSameOrigin) { | |
| 204 GURL origin("http://www.example.com"); | |
| 205 fileapi::FileSystemType src_type = kFileSystemTypePersistent; | |
| 206 fileapi::FileSystemType dest_type = kFileSystemTypeTemporary; | |
| 207 | |
| 208 TestCrossFileSystemCopyMoveHelper(origin, src_type, origin, dest_type, false); | |
| 209 } | |
| OLD | NEW |