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 return context; | |
68 } | |
69 | |
70 void TestCrossFileSystemCopyMoveHelper( | |
71 const GURL& src_origin, fileapi::FileSystemType src_type, | |
72 const GURL& dest_origin, fileapi::FileSystemType dest_type, | |
73 bool copy) { | |
74 ScopedTempDir base_dir; | |
75 ASSERT_TRUE(base_dir.CreateUniqueTempDir()); | |
76 scoped_refptr<ObfuscatedFileSystemFileUtil> file_util( | |
77 new ObfuscatedFileSystemFileUtil(base_dir.path(), | |
78 new FileSystemFileUtil())); | |
79 FileSystemTestOriginHelper src_helper(src_origin, src_type); | |
80 src_helper.SetUp(base_dir.path(), | |
81 false, // incognito | |
82 false, // unlimited quota | |
83 NULL, // quota::QuotaManagerProxy | |
84 file_util.get()); | |
85 FileSystemTestOriginHelper dest_helper(dest_origin, dest_type); | |
86 dest_helper.SetUp(src_helper.file_system_context(), NULL); | |
87 | |
88 // Set up all the source data. | |
89 scoped_ptr<FileSystemOperationContext> context; | |
90 FilePath test_root(FILE_PATH_LITERAL("root directory")); | |
91 for (size_t i = 0; i < arraysize(kCopyMoveTestCases); ++i) { | |
92 const CopyMoveTestCaseRecord& test_case = kCopyMoveTestCases[i]; | |
93 FilePath path = test_root.Append(test_case.path); | |
94 if (test_case.is_directory) { | |
95 context.reset(NewContext(&src_helper)); | |
96 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
97 file_util->CreateDirectory(context.get(), path, true, true)); | |
98 } else { | |
99 context.reset(NewContext(&src_helper)); | |
100 bool created = false; | |
101 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
102 file_util->EnsureFileExists(context.get(), path, &created)); | |
103 ASSERT_TRUE(created); | |
104 context.reset(NewContext(&src_helper)); | |
105 ASSERT_EQ(base::PLATFORM_FILE_OK, file_util->Truncate( | |
106 context.get(), path, test_case.data_file_size)); | |
107 } | |
108 } | |
109 | |
110 // Do the actual copy or move. | |
111 FileSystemContext* file_system_context = dest_helper.file_system_context(); | |
112 scoped_ptr<FileSystemOperationContext> copy_context( | |
113 new FileSystemOperationContext(file_system_context, NULL)); | |
114 copy_context->set_src_file_system_file_util(file_util); | |
115 copy_context->set_dest_file_system_file_util(file_util); | |
116 copy_context->set_src_origin_url(src_helper.origin()); | |
117 copy_context->set_dest_origin_url(dest_helper.origin()); | |
118 copy_context->set_src_type(src_helper.type()); | |
119 copy_context->set_dest_type(dest_helper.type()); | |
120 | |
121 if (copy) | |
122 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
123 file_util->Copy(copy_context.get(), test_root, test_root)); | |
124 else | |
125 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
126 file_util->Move(copy_context.get(), test_root, test_root)); | |
127 | |
128 // Validate that the destination paths are correct. | |
129 for (size_t i = 0; i < arraysize(kCopyMoveTestCases); ++i) { | |
130 const CopyMoveTestCaseRecord& test_case = kCopyMoveTestCases[i]; | |
131 FilePath path = test_root.Append(test_case.path); | |
132 | |
133 base::PlatformFileInfo dest_file_info; | |
134 FilePath data_path; | |
135 context.reset(NewContext(&dest_helper)); | |
136 EXPECT_EQ(base::PLATFORM_FILE_OK, | |
137 file_util->GetFileInfo( | |
138 context.get(), path, &dest_file_info, &data_path)); | |
139 if (test_case.is_directory) { | |
140 EXPECT_TRUE(dest_file_info.is_directory); | |
141 } else { | |
142 base::PlatformFileInfo platform_file_info; | |
143 ASSERT_TRUE(file_util::GetFileInfo(data_path, &platform_file_info)); | |
144 EXPECT_EQ(test_case.data_file_size, platform_file_info.size); | |
145 EXPECT_FALSE(platform_file_info.is_directory); | |
146 EXPECT_EQ(platform_file_info.size, dest_file_info.size); | |
147 EXPECT_FALSE(dest_file_info.is_directory); | |
148 } | |
149 } | |
150 | |
151 // Validate that the source paths are still there [for a copy] or gone [for | |
152 // a move]. | |
153 for (size_t i = 0; i < arraysize(kCopyMoveTestCases); ++i) { | |
154 const CopyMoveTestCaseRecord& test_case = kCopyMoveTestCases[i]; | |
155 FilePath path = test_root.Append(test_case.path); | |
156 base::PlatformFileInfo src_file_info; | |
157 FilePath data_path; | |
158 context.reset(NewContext(&src_helper)); | |
159 base::PlatformFileError expected_result; | |
160 if (copy) | |
161 expected_result = base::PLATFORM_FILE_OK; | |
162 else | |
163 expected_result = base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
164 EXPECT_EQ(expected_result, | |
165 file_util->GetFileInfo( | |
166 context.get(), path, &src_file_info, &data_path)); | |
167 } | |
168 } | |
169 | |
170 private: | |
171 DISALLOW_COPY_AND_ASSIGN(FileSystemFileUtilTest); | |
172 }; | |
173 | |
174 TEST_F(FileSystemFileUtilTest, TestCrossFileSystemCopyDifferentOrigins) { | |
175 GURL src_origin("http://www.example.com"); | |
176 fileapi::FileSystemType type = kFileSystemTypePersistent; | |
177 GURL dest_origin("http://www.not.the.same.domain.com"); | |
178 | |
179 TestCrossFileSystemCopyMoveHelper(src_origin, type, dest_origin, type, true); | |
180 } | |
181 | |
182 TEST_F(FileSystemFileUtilTest, TestCrossFileSystemCopySameOrigin) { | |
183 GURL origin("http://www.example.com"); | |
184 fileapi::FileSystemType src_type = kFileSystemTypePersistent; | |
185 fileapi::FileSystemType dest_type = kFileSystemTypeTemporary; | |
186 | |
187 TestCrossFileSystemCopyMoveHelper(origin, src_type, origin, dest_type, true); | |
188 } | |
189 | |
190 TEST_F(FileSystemFileUtilTest, TestCrossFileSystemMoveDifferentOrigins) { | |
191 GURL src_origin("http://www.example.com"); | |
192 fileapi::FileSystemType type = kFileSystemTypePersistent; | |
193 GURL dest_origin("http://www.not.the.same.domain.com"); | |
194 | |
195 TestCrossFileSystemCopyMoveHelper(src_origin, type, dest_origin, type, false); | |
196 } | |
197 | |
198 TEST_F(FileSystemFileUtilTest, TestCrossFileSystemMoveSameOrigin) { | |
199 GURL origin("http://www.example.com"); | |
200 fileapi::FileSystemType src_type = kFileSystemTypePersistent; | |
201 fileapi::FileSystemType dest_type = kFileSystemTypeTemporary; | |
202 | |
203 TestCrossFileSystemCopyMoveHelper(origin, src_type, origin, dest_type, false); | |
204 } | |
OLD | NEW |