OLD | NEW |
| (Empty) |
1 // Copyright 2013 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/basictypes.h" | |
6 #include "base/bind.h" | |
7 #include "base/files/file_path.h" | |
8 #include "base/files/scoped_temp_dir.h" | |
9 #include "base/message_loop.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 #include "webkit/browser/fileapi/file_system_mount_point_provider.h" | |
12 #include "webkit/fileapi/async_file_test_helper.h" | |
13 #include "webkit/fileapi/copy_or_move_file_validator.h" | |
14 #include "webkit/fileapi/external_mount_points.h" | |
15 #include "webkit/fileapi/file_system_context.h" | |
16 #include "webkit/fileapi/file_system_url.h" | |
17 #include "webkit/fileapi/file_system_util.h" | |
18 #include "webkit/fileapi/isolated_context.h" | |
19 #include "webkit/fileapi/mock_file_system_context.h" | |
20 #include "webkit/fileapi/test_mount_point_provider.h" | |
21 #include "webkit/quota/mock_special_storage_policy.h" | |
22 | |
23 namespace fileapi { | |
24 | |
25 namespace { | |
26 | |
27 const FileSystemType kNoValidatorType = kFileSystemTypeTemporary; | |
28 const FileSystemType kWithValidatorType = kFileSystemTypeTest; | |
29 | |
30 class CopyOrMoveFileValidatorTestHelper { | |
31 public: | |
32 CopyOrMoveFileValidatorTestHelper( | |
33 const GURL& origin, | |
34 FileSystemType src_type, | |
35 FileSystemType dest_type) | |
36 : origin_(origin), | |
37 src_type_(src_type), | |
38 dest_type_(dest_type) {} | |
39 | |
40 ~CopyOrMoveFileValidatorTestHelper() { | |
41 file_system_context_ = NULL; | |
42 base::MessageLoop::current()->RunUntilIdle(); | |
43 } | |
44 | |
45 void SetUp() { | |
46 ASSERT_TRUE(base_.CreateUniqueTempDir()); | |
47 base::FilePath base_dir = base_.path(); | |
48 | |
49 file_system_context_ = CreateFileSystemContextForTesting(NULL, base_dir); | |
50 | |
51 // Set up TestMountPointProvider to require CopyOrMoveFileValidator. | |
52 FileSystemMountPointProvider* test_mount_point_provider = | |
53 file_system_context_->GetMountPointProvider(kWithValidatorType); | |
54 static_cast<TestMountPointProvider*>(test_mount_point_provider)-> | |
55 set_require_copy_or_move_validator(true); | |
56 | |
57 // Sets up source. | |
58 FileSystemMountPointProvider* src_mount_point_provider = | |
59 file_system_context_->GetMountPointProvider(src_type_); | |
60 src_mount_point_provider->GetFileSystemRootPathOnFileThread( | |
61 SourceURL(std::string()), true /* create */); | |
62 ASSERT_EQ(base::PLATFORM_FILE_OK, CreateDirectory(SourceURL(""))); | |
63 | |
64 // Sets up dest. | |
65 DCHECK_EQ(kWithValidatorType, dest_type_); | |
66 ASSERT_EQ(base::PLATFORM_FILE_OK, CreateDirectory(DestURL(""))); | |
67 | |
68 copy_src_ = SourceURL("copy_src.jpg"); | |
69 move_src_ = SourceURL("move_src.jpg"); | |
70 copy_dest_ = DestURL("copy_dest.jpg"); | |
71 move_dest_ = DestURL("move_dest.jpg"); | |
72 | |
73 ASSERT_EQ(base::PLATFORM_FILE_OK, CreateFile(copy_src_, 10)); | |
74 ASSERT_EQ(base::PLATFORM_FILE_OK, CreateFile(move_src_, 10)); | |
75 | |
76 ASSERT_TRUE(FileExists(copy_src_, 10)); | |
77 ASSERT_TRUE(FileExists(move_src_, 10)); | |
78 ASSERT_FALSE(FileExists(copy_dest_, 10)); | |
79 ASSERT_FALSE(FileExists(move_dest_, 10)); | |
80 } | |
81 | |
82 void SetMediaCopyOrMoveFileValidatorFactory( | |
83 scoped_ptr<CopyOrMoveFileValidatorFactory> factory) { | |
84 FileSystemMountPointProvider* mount_point_provider = | |
85 file_system_context_->GetMountPointProvider(kWithValidatorType); | |
86 mount_point_provider->InitializeCopyOrMoveFileValidatorFactory( | |
87 kWithValidatorType, factory.Pass()); | |
88 } | |
89 | |
90 void CopyTest(base::PlatformFileError expected) { | |
91 ASSERT_TRUE(FileExists(copy_src_, 10)); | |
92 ASSERT_FALSE(FileExists(copy_dest_, 10)); | |
93 | |
94 EXPECT_EQ(expected, | |
95 AsyncFileTestHelper::Copy(file_system_context_, copy_src_, | |
96 copy_dest_)); | |
97 | |
98 EXPECT_TRUE(FileExists(copy_src_, 10)); | |
99 if (expected == base::PLATFORM_FILE_OK) | |
100 EXPECT_TRUE(FileExists(copy_dest_, 10)); | |
101 else | |
102 EXPECT_FALSE(FileExists(copy_dest_, 10)); | |
103 }; | |
104 | |
105 void MoveTest(base::PlatformFileError expected) { | |
106 ASSERT_TRUE(FileExists(move_src_, 10)); | |
107 ASSERT_FALSE(FileExists(move_dest_, 10)); | |
108 | |
109 EXPECT_EQ(expected, | |
110 AsyncFileTestHelper::Move(file_system_context_, move_src_, | |
111 move_dest_)); | |
112 | |
113 if (expected == base::PLATFORM_FILE_OK) { | |
114 EXPECT_FALSE(FileExists(move_src_, 10)); | |
115 EXPECT_TRUE(FileExists(move_dest_, 10)); | |
116 } else { | |
117 EXPECT_TRUE(FileExists(move_src_, 10)); | |
118 EXPECT_FALSE(FileExists(move_dest_, 10)); | |
119 } | |
120 }; | |
121 | |
122 private: | |
123 FileSystemURL SourceURL(const std::string& path) { | |
124 return file_system_context_->CreateCrackedFileSystemURL( | |
125 origin_, src_type_, | |
126 base::FilePath().AppendASCII("src").AppendASCII(path)); | |
127 } | |
128 | |
129 FileSystemURL DestURL(const std::string& path) { | |
130 return file_system_context_->CreateCrackedFileSystemURL( | |
131 origin_, dest_type_, | |
132 base::FilePath().AppendASCII("dest").AppendASCII(path)); | |
133 } | |
134 | |
135 base::PlatformFileError CreateFile(const FileSystemURL& url, size_t size) { | |
136 base::PlatformFileError result = | |
137 AsyncFileTestHelper::CreateFile(file_system_context_, url); | |
138 if (result != base::PLATFORM_FILE_OK) | |
139 return result; | |
140 return AsyncFileTestHelper::TruncateFile(file_system_context_, url, size); | |
141 } | |
142 | |
143 base::PlatformFileError CreateDirectory(const FileSystemURL& url) { | |
144 return AsyncFileTestHelper::CreateDirectory(file_system_context_, url); | |
145 } | |
146 | |
147 bool FileExists(const FileSystemURL& url, int64 expected_size) { | |
148 return AsyncFileTestHelper::FileExists( | |
149 file_system_context_, url, expected_size); | |
150 } | |
151 | |
152 base::ScopedTempDir base_; | |
153 | |
154 const GURL origin_; | |
155 | |
156 const FileSystemType src_type_; | |
157 const FileSystemType dest_type_; | |
158 std::string src_fsid_; | |
159 std::string dest_fsid_; | |
160 | |
161 base::MessageLoop message_loop_; | |
162 scoped_refptr<FileSystemContext> file_system_context_; | |
163 | |
164 FileSystemURL copy_src_; | |
165 FileSystemURL copy_dest_; | |
166 FileSystemURL move_src_; | |
167 FileSystemURL move_dest_; | |
168 | |
169 DISALLOW_COPY_AND_ASSIGN(CopyOrMoveFileValidatorTestHelper); | |
170 }; | |
171 | |
172 class TestCopyOrMoveFileValidatorFactory | |
173 : public CopyOrMoveFileValidatorFactory { | |
174 public: | |
175 // A factory that creates validators that accept everything or nothing. | |
176 TestCopyOrMoveFileValidatorFactory(bool all_valid) : all_valid_(all_valid) {} | |
177 virtual ~TestCopyOrMoveFileValidatorFactory() {} | |
178 | |
179 virtual CopyOrMoveFileValidator* CreateCopyOrMoveFileValidator( | |
180 const FileSystemURL& /*src_url*/, | |
181 const base::FilePath& /*platform_path*/) OVERRIDE { | |
182 return new TestCopyOrMoveFileValidator(all_valid_); | |
183 } | |
184 | |
185 private: | |
186 class TestCopyOrMoveFileValidator : public CopyOrMoveFileValidator { | |
187 public: | |
188 TestCopyOrMoveFileValidator(bool all_valid) | |
189 : result_(all_valid ? base::PLATFORM_FILE_OK | |
190 : base::PLATFORM_FILE_ERROR_SECURITY) { | |
191 } | |
192 virtual ~TestCopyOrMoveFileValidator() {} | |
193 | |
194 virtual void StartValidation( | |
195 const ResultCallback& result_callback) OVERRIDE { | |
196 // Post the result since a real validator must do work asynchronously. | |
197 base::MessageLoop::current()->PostTask( | |
198 FROM_HERE, base::Bind(result_callback, result_)); | |
199 } | |
200 | |
201 private: | |
202 base::PlatformFileError result_; | |
203 | |
204 DISALLOW_COPY_AND_ASSIGN(TestCopyOrMoveFileValidator); | |
205 }; | |
206 | |
207 bool all_valid_; | |
208 | |
209 DISALLOW_COPY_AND_ASSIGN(TestCopyOrMoveFileValidatorFactory); | |
210 }; | |
211 | |
212 } // namespace | |
213 | |
214 TEST(CopyOrMoveFileValidatorTest, NoValidatorWithin6ameFSType) { | |
215 // Within a file system type, validation is not expected, so it should | |
216 // work for kWithValidatorType without a validator set. | |
217 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), | |
218 kWithValidatorType, | |
219 kWithValidatorType); | |
220 helper.SetUp(); | |
221 helper.CopyTest(base::PLATFORM_FILE_OK); | |
222 helper.MoveTest(base::PLATFORM_FILE_OK); | |
223 } | |
224 | |
225 TEST(CopyOrMoveFileValidatorTest, MissingValidator) { | |
226 // Copying or moving into a kWithValidatorType requires a file | |
227 // validator. An error is expect if copy is attempted without a validator. | |
228 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), | |
229 kNoValidatorType, | |
230 kWithValidatorType); | |
231 helper.SetUp(); | |
232 helper.CopyTest(base::PLATFORM_FILE_ERROR_SECURITY); | |
233 helper.MoveTest(base::PLATFORM_FILE_ERROR_SECURITY); | |
234 } | |
235 | |
236 TEST(CopyOrMoveFileValidatorTest, AcceptAll) { | |
237 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), | |
238 kNoValidatorType, | |
239 kWithValidatorType); | |
240 helper.SetUp(); | |
241 scoped_ptr<CopyOrMoveFileValidatorFactory> factory( | |
242 new TestCopyOrMoveFileValidatorFactory(true /*accept_all*/)); | |
243 helper.SetMediaCopyOrMoveFileValidatorFactory(factory.Pass()); | |
244 | |
245 helper.CopyTest(base::PLATFORM_FILE_OK); | |
246 helper.MoveTest(base::PLATFORM_FILE_OK); | |
247 } | |
248 | |
249 TEST(CopyOrMoveFileValidatorTest, AcceptNone) { | |
250 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), | |
251 kNoValidatorType, | |
252 kWithValidatorType); | |
253 helper.SetUp(); | |
254 scoped_ptr<CopyOrMoveFileValidatorFactory> factory( | |
255 new TestCopyOrMoveFileValidatorFactory(false /*accept_all*/)); | |
256 helper.SetMediaCopyOrMoveFileValidatorFactory(factory.Pass()); | |
257 | |
258 helper.CopyTest(base::PLATFORM_FILE_ERROR_SECURITY); | |
259 helper.MoveTest(base::PLATFORM_FILE_ERROR_SECURITY); | |
260 } | |
261 | |
262 TEST(CopyOrMoveFileValidatorTest, OverrideValidator) { | |
263 // Once set, you can not override the validator. | |
264 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), | |
265 kNoValidatorType, | |
266 kWithValidatorType); | |
267 helper.SetUp(); | |
268 scoped_ptr<CopyOrMoveFileValidatorFactory> reject_factory( | |
269 new TestCopyOrMoveFileValidatorFactory(false /*accept_all*/)); | |
270 helper.SetMediaCopyOrMoveFileValidatorFactory(reject_factory.Pass()); | |
271 | |
272 scoped_ptr<CopyOrMoveFileValidatorFactory> accept_factory( | |
273 new TestCopyOrMoveFileValidatorFactory(true /*accept_all*/)); | |
274 helper.SetMediaCopyOrMoveFileValidatorFactory(accept_factory.Pass()); | |
275 | |
276 helper.CopyTest(base::PLATFORM_FILE_ERROR_SECURITY); | |
277 helper.MoveTest(base::PLATFORM_FILE_ERROR_SECURITY); | |
278 } | |
279 | |
280 } // namespace fileapi | |
OLD | NEW |