OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/bind.h" | 6 #include "base/bind.h" |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 scoped_refptr<FileSystemContext> file_system_context_; | 173 scoped_refptr<FileSystemContext> file_system_context_; |
174 | 174 |
175 FileSystemURL copy_src_; | 175 FileSystemURL copy_src_; |
176 FileSystemURL copy_dest_; | 176 FileSystemURL copy_dest_; |
177 FileSystemURL move_src_; | 177 FileSystemURL move_src_; |
178 FileSystemURL move_dest_; | 178 FileSystemURL move_dest_; |
179 | 179 |
180 DISALLOW_COPY_AND_ASSIGN(CopyOrMoveFileValidatorTestHelper); | 180 DISALLOW_COPY_AND_ASSIGN(CopyOrMoveFileValidatorTestHelper); |
181 }; | 181 }; |
182 | 182 |
| 183 // For TestCopyOrMoveFileValidatorFactory |
| 184 enum Validity { |
| 185 VALID, |
| 186 PRE_WRITE_INVALID, |
| 187 POST_WRITE_INVALID |
| 188 }; |
| 189 |
183 class TestCopyOrMoveFileValidatorFactory | 190 class TestCopyOrMoveFileValidatorFactory |
184 : public CopyOrMoveFileValidatorFactory { | 191 : public CopyOrMoveFileValidatorFactory { |
185 public: | 192 public: |
186 // A factory that creates validators that accept everything or nothing. | 193 // A factory that creates validators that accept everything or nothing. |
187 explicit TestCopyOrMoveFileValidatorFactory(bool all_valid, | 194 // TODO(gbillock): switch args to enum or something |
188 bool all_valid_write) | 195 explicit TestCopyOrMoveFileValidatorFactory(Validity validity) |
189 : all_valid_(all_valid), | 196 : validity_(validity) {} |
190 all_valid_write_(all_valid_write) {} | |
191 virtual ~TestCopyOrMoveFileValidatorFactory() {} | 197 virtual ~TestCopyOrMoveFileValidatorFactory() {} |
192 | 198 |
193 virtual CopyOrMoveFileValidator* CreateCopyOrMoveFileValidator( | 199 virtual CopyOrMoveFileValidator* CreateCopyOrMoveFileValidator( |
194 const FileSystemURL& /*src_url*/, | 200 const FileSystemURL& /*src_url*/, |
195 const base::FilePath& /*platform_path*/) OVERRIDE { | 201 const base::FilePath& /*platform_path*/) OVERRIDE { |
196 return new TestCopyOrMoveFileValidator(all_valid_, all_valid_write_); | 202 return new TestCopyOrMoveFileValidator(validity_); |
197 } | 203 } |
198 | 204 |
199 private: | 205 private: |
200 class TestCopyOrMoveFileValidator : public CopyOrMoveFileValidator { | 206 class TestCopyOrMoveFileValidator : public CopyOrMoveFileValidator { |
201 public: | 207 public: |
202 explicit TestCopyOrMoveFileValidator(bool pre_copy_valid, | 208 explicit TestCopyOrMoveFileValidator(Validity validity) |
203 bool post_copy_valid) | 209 : result_(validity == VALID || validity == POST_WRITE_INVALID |
204 : result_(pre_copy_valid ? base::PLATFORM_FILE_OK | 210 ? base::PLATFORM_FILE_OK |
205 : base::PLATFORM_FILE_ERROR_SECURITY), | 211 : base::PLATFORM_FILE_ERROR_SECURITY), |
206 write_result_(post_copy_valid ? base::PLATFORM_FILE_OK | 212 write_result_(validity == VALID || validity == PRE_WRITE_INVALID |
207 : base::PLATFORM_FILE_ERROR_SECURITY) { | 213 ? base::PLATFORM_FILE_OK |
| 214 : base::PLATFORM_FILE_ERROR_SECURITY) { |
208 } | 215 } |
209 virtual ~TestCopyOrMoveFileValidator() {} | 216 virtual ~TestCopyOrMoveFileValidator() {} |
210 | 217 |
211 virtual void StartPreWriteValidation( | 218 virtual void StartPreWriteValidation( |
212 const ResultCallback& result_callback) OVERRIDE { | 219 const ResultCallback& result_callback) OVERRIDE { |
213 // Post the result since a real validator must do work asynchronously. | 220 // Post the result since a real validator must do work asynchronously. |
214 base::MessageLoop::current()->PostTask( | 221 base::MessageLoop::current()->PostTask( |
215 FROM_HERE, base::Bind(result_callback, result_)); | 222 FROM_HERE, base::Bind(result_callback, result_)); |
216 } | 223 } |
217 | 224 |
218 virtual void StartPostWriteValidation( | 225 virtual void StartPostWriteValidation( |
219 const base::FilePath& dest_platform_path, | 226 const base::FilePath& dest_platform_path, |
220 const ResultCallback& result_callback) OVERRIDE { | 227 const ResultCallback& result_callback) OVERRIDE { |
221 // Post the result since a real validator must do work asynchronously. | 228 // Post the result since a real validator must do work asynchronously. |
222 base::MessageLoop::current()->PostTask( | 229 base::MessageLoop::current()->PostTask( |
223 FROM_HERE, base::Bind(result_callback, write_result_)); | 230 FROM_HERE, base::Bind(result_callback, write_result_)); |
224 } | 231 } |
225 | 232 |
226 private: | 233 private: |
227 base::PlatformFileError result_; | 234 base::PlatformFileError result_; |
228 base::PlatformFileError write_result_; | 235 base::PlatformFileError write_result_; |
229 | 236 |
230 DISALLOW_COPY_AND_ASSIGN(TestCopyOrMoveFileValidator); | 237 DISALLOW_COPY_AND_ASSIGN(TestCopyOrMoveFileValidator); |
231 }; | 238 }; |
232 | 239 |
233 bool all_valid_; | 240 Validity validity_; |
234 bool all_valid_write_; | |
235 | 241 |
236 DISALLOW_COPY_AND_ASSIGN(TestCopyOrMoveFileValidatorFactory); | 242 DISALLOW_COPY_AND_ASSIGN(TestCopyOrMoveFileValidatorFactory); |
237 }; | 243 }; |
238 | 244 |
239 } // namespace | 245 } // namespace |
240 | 246 |
241 TEST(CopyOrMoveFileValidatorTest, NoValidatorWithinSameFSType) { | 247 TEST(CopyOrMoveFileValidatorTest, NoValidatorWithinSameFSType) { |
242 // Within a file system type, validation is not expected, so it should | 248 // Within a file system type, validation is not expected, so it should |
243 // work for kWithValidatorType without a validator set. | 249 // work for kWithValidatorType without a validator set. |
244 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), | 250 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), |
(...skipping 14 matching lines...) Expand all Loading... |
259 helper.CopyTest(base::PLATFORM_FILE_ERROR_SECURITY); | 265 helper.CopyTest(base::PLATFORM_FILE_ERROR_SECURITY); |
260 helper.MoveTest(base::PLATFORM_FILE_ERROR_SECURITY); | 266 helper.MoveTest(base::PLATFORM_FILE_ERROR_SECURITY); |
261 } | 267 } |
262 | 268 |
263 TEST(CopyOrMoveFileValidatorTest, AcceptAll) { | 269 TEST(CopyOrMoveFileValidatorTest, AcceptAll) { |
264 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), | 270 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), |
265 kNoValidatorType, | 271 kNoValidatorType, |
266 kWithValidatorType); | 272 kWithValidatorType); |
267 helper.SetUp(); | 273 helper.SetUp(); |
268 scoped_ptr<CopyOrMoveFileValidatorFactory> factory( | 274 scoped_ptr<CopyOrMoveFileValidatorFactory> factory( |
269 new TestCopyOrMoveFileValidatorFactory(true, true /*accept_all*/)); | 275 new TestCopyOrMoveFileValidatorFactory(VALID)); |
270 helper.SetMediaCopyOrMoveFileValidatorFactory(factory.Pass()); | 276 helper.SetMediaCopyOrMoveFileValidatorFactory(factory.Pass()); |
271 | 277 |
272 helper.CopyTest(base::PLATFORM_FILE_OK); | 278 helper.CopyTest(base::PLATFORM_FILE_OK); |
273 helper.MoveTest(base::PLATFORM_FILE_OK); | 279 helper.MoveTest(base::PLATFORM_FILE_OK); |
274 } | 280 } |
275 | 281 |
276 TEST(CopyOrMoveFileValidatorTest, AcceptNone) { | 282 TEST(CopyOrMoveFileValidatorTest, AcceptNone) { |
277 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), | 283 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), |
278 kNoValidatorType, | 284 kNoValidatorType, |
279 kWithValidatorType); | 285 kWithValidatorType); |
280 helper.SetUp(); | 286 helper.SetUp(); |
281 scoped_ptr<CopyOrMoveFileValidatorFactory> factory( | 287 scoped_ptr<CopyOrMoveFileValidatorFactory> factory( |
282 new TestCopyOrMoveFileValidatorFactory(false, false /*accept_all*/)); | 288 new TestCopyOrMoveFileValidatorFactory(PRE_WRITE_INVALID)); |
283 helper.SetMediaCopyOrMoveFileValidatorFactory(factory.Pass()); | 289 helper.SetMediaCopyOrMoveFileValidatorFactory(factory.Pass()); |
284 | 290 |
285 helper.CopyTest(base::PLATFORM_FILE_ERROR_SECURITY); | 291 helper.CopyTest(base::PLATFORM_FILE_ERROR_SECURITY); |
286 helper.MoveTest(base::PLATFORM_FILE_ERROR_SECURITY); | 292 helper.MoveTest(base::PLATFORM_FILE_ERROR_SECURITY); |
287 } | 293 } |
288 | 294 |
289 TEST(CopyOrMoveFileValidatorTest, OverrideValidator) { | 295 TEST(CopyOrMoveFileValidatorTest, OverrideValidator) { |
290 // Once set, you can not override the validator. | 296 // Once set, you can not override the validator. |
291 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), | 297 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), |
292 kNoValidatorType, | 298 kNoValidatorType, |
293 kWithValidatorType); | 299 kWithValidatorType); |
294 helper.SetUp(); | 300 helper.SetUp(); |
295 scoped_ptr<CopyOrMoveFileValidatorFactory> reject_factory( | 301 scoped_ptr<CopyOrMoveFileValidatorFactory> reject_factory( |
296 new TestCopyOrMoveFileValidatorFactory(false, false /*accept_all*/)); | 302 new TestCopyOrMoveFileValidatorFactory(PRE_WRITE_INVALID)); |
297 helper.SetMediaCopyOrMoveFileValidatorFactory(reject_factory.Pass()); | 303 helper.SetMediaCopyOrMoveFileValidatorFactory(reject_factory.Pass()); |
298 | 304 |
299 scoped_ptr<CopyOrMoveFileValidatorFactory> accept_factory( | 305 scoped_ptr<CopyOrMoveFileValidatorFactory> accept_factory( |
300 new TestCopyOrMoveFileValidatorFactory(true, true /*accept_all*/)); | 306 new TestCopyOrMoveFileValidatorFactory(VALID)); |
301 helper.SetMediaCopyOrMoveFileValidatorFactory(accept_factory.Pass()); | 307 helper.SetMediaCopyOrMoveFileValidatorFactory(accept_factory.Pass()); |
302 | 308 |
303 helper.CopyTest(base::PLATFORM_FILE_ERROR_SECURITY); | 309 helper.CopyTest(base::PLATFORM_FILE_ERROR_SECURITY); |
304 helper.MoveTest(base::PLATFORM_FILE_ERROR_SECURITY); | 310 helper.MoveTest(base::PLATFORM_FILE_ERROR_SECURITY); |
305 } | 311 } |
306 | 312 |
307 TEST(CopyOrMoveFileValidatorTest, RejectPostWrite) { | 313 TEST(CopyOrMoveFileValidatorTest, RejectPostWrite) { |
308 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), | 314 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), |
309 kNoValidatorType, | 315 kNoValidatorType, |
310 kWithValidatorType); | 316 kWithValidatorType); |
311 helper.SetUp(); | 317 helper.SetUp(); |
312 scoped_ptr<CopyOrMoveFileValidatorFactory> factory( | 318 scoped_ptr<CopyOrMoveFileValidatorFactory> factory( |
313 // accept pre-copy, reject post-copy | 319 new TestCopyOrMoveFileValidatorFactory(POST_WRITE_INVALID)); |
314 new TestCopyOrMoveFileValidatorFactory(true, false)); | |
315 helper.SetMediaCopyOrMoveFileValidatorFactory(factory.Pass()); | 320 helper.SetMediaCopyOrMoveFileValidatorFactory(factory.Pass()); |
316 | 321 |
317 helper.CopyTest(base::PLATFORM_FILE_ERROR_SECURITY); | 322 helper.CopyTest(base::PLATFORM_FILE_ERROR_SECURITY); |
318 helper.MoveTest(base::PLATFORM_FILE_ERROR_SECURITY); | 323 helper.MoveTest(base::PLATFORM_FILE_ERROR_SECURITY); |
319 } | 324 } |
320 | 325 |
321 } // namespace fileapi | 326 } // namespace fileapi |
OLD | NEW |