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