OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 <map> | 5 #include <map> |
6 #include <queue> | 6 #include <queue> |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 #include "webkit/browser/fileapi/async_file_test_helper.h" | 15 #include "webkit/browser/fileapi/async_file_test_helper.h" |
16 #include "webkit/browser/fileapi/copy_or_move_file_validator.h" | |
16 #include "webkit/browser/fileapi/file_system_backend.h" | 17 #include "webkit/browser/fileapi/file_system_backend.h" |
17 #include "webkit/browser/fileapi/file_system_context.h" | 18 #include "webkit/browser/fileapi/file_system_context.h" |
18 #include "webkit/browser/fileapi/file_system_operation.h" | 19 #include "webkit/browser/fileapi/file_system_operation.h" |
19 #include "webkit/browser/fileapi/file_system_url.h" | 20 #include "webkit/browser/fileapi/file_system_url.h" |
20 #include "webkit/browser/fileapi/mock_file_system_context.h" | 21 #include "webkit/browser/fileapi/mock_file_system_context.h" |
21 #include "webkit/browser/fileapi/test_file_set.h" | 22 #include "webkit/browser/fileapi/test_file_set.h" |
23 #include "webkit/browser/fileapi/test_file_system_backend.h" | |
22 #include "webkit/browser/quota/mock_quota_manager.h" | 24 #include "webkit/browser/quota/mock_quota_manager.h" |
23 #include "webkit/browser/quota/quota_manager.h" | 25 #include "webkit/browser/quota/quota_manager.h" |
24 #include "webkit/common/fileapi/file_system_util.h" | 26 #include "webkit/common/fileapi/file_system_util.h" |
25 | 27 |
26 namespace fileapi { | 28 namespace fileapi { |
27 | 29 |
28 typedef FileSystemOperation::FileEntryList FileEntryList; | 30 typedef FileSystemOperation::FileEntryList FileEntryList; |
29 | 31 |
30 namespace { | 32 namespace { |
31 | 33 |
32 void ExpectOk(const GURL& origin_url, | 34 void ExpectOk(const GURL& origin_url, |
33 const std::string& name, | 35 const std::string& name, |
34 base::PlatformFileError error) { | 36 base::PlatformFileError error) { |
35 ASSERT_EQ(base::PLATFORM_FILE_OK, error); | 37 ASSERT_EQ(base::PLATFORM_FILE_OK, error); |
36 } | 38 } |
37 | 39 |
40 class TestValidatorFactory : public CopyOrMoveFileValidatorFactory { | |
41 public: | |
42 // A factory that creates validators that accept everything or nothing. | |
43 TestValidatorFactory() {} | |
44 virtual ~TestValidatorFactory() {} | |
45 | |
46 virtual CopyOrMoveFileValidator* CreateCopyOrMoveFileValidator( | |
47 const FileSystemURL& /*src_url*/, | |
48 const base::FilePath& /*platform_path*/) OVERRIDE { | |
49 return new TestValidator(true, true, std::string("2")); | |
vandebo (ex-Chrome)
2013/07/18 17:35:14
nit: The args are all static, why not omit the fun
Greg Billock
2013/07/30 23:18:31
You mean contain it there?
| |
50 } | |
51 | |
52 private: | |
53 class TestValidator : public CopyOrMoveFileValidator { | |
54 public: | |
55 explicit TestValidator(bool pre_copy_valid, | |
56 bool post_copy_valid, | |
57 const std::string& reject_string) | |
58 : result_(pre_copy_valid ? base::PLATFORM_FILE_OK | |
59 : base::PLATFORM_FILE_ERROR_SECURITY), | |
60 write_result_(post_copy_valid ? base::PLATFORM_FILE_OK | |
61 : base::PLATFORM_FILE_ERROR_SECURITY), | |
62 reject_string_(reject_string) { | |
63 } | |
64 virtual ~TestValidator() {} | |
65 | |
66 virtual void StartPreWriteValidation( | |
67 const ResultCallback& result_callback) OVERRIDE { | |
68 // Post the result since a real validator must do work asynchronously. | |
69 base::MessageLoop::current()->PostTask( | |
70 FROM_HERE, base::Bind(result_callback, result_)); | |
71 } | |
72 | |
73 virtual void StartPostWriteValidation( | |
74 const base::FilePath& dest_platform_path, | |
75 const ResultCallback& result_callback) OVERRIDE { | |
76 base::PlatformFileError result = write_result_; | |
77 std::string unsafe = dest_platform_path.AsUTF8Unsafe(); | |
78 if (unsafe.find(reject_string_) != std::string::npos) { | |
79 result = base::PLATFORM_FILE_ERROR_SECURITY; | |
80 } | |
81 // Post the result since a real validator must do work asynchronously. | |
82 base::MessageLoop::current()->PostTask( | |
83 FROM_HERE, base::Bind(result_callback, result)); | |
84 } | |
85 | |
86 private: | |
87 base::PlatformFileError result_; | |
88 base::PlatformFileError write_result_; | |
89 std::string reject_string_; | |
90 | |
91 DISALLOW_COPY_AND_ASSIGN(TestValidator); | |
92 }; | |
93 }; | |
94 | |
38 } // namespace | 95 } // namespace |
39 | 96 |
40 class CopyOrMoveOperationTestHelper { | 97 class CopyOrMoveOperationTestHelper { |
41 public: | 98 public: |
42 CopyOrMoveOperationTestHelper( | 99 CopyOrMoveOperationTestHelper( |
43 const GURL& origin, | 100 const GURL& origin, |
44 FileSystemType src_type, | 101 FileSystemType src_type, |
45 FileSystemType dest_type) | 102 FileSystemType dest_type) |
46 : origin_(origin), | 103 : origin_(origin), |
47 src_type_(src_type), | 104 src_type_(src_type), |
(...skipping 23 matching lines...) Expand all Loading... | |
71 | 128 |
72 // Prepare the origin's root directory. | 129 // Prepare the origin's root directory. |
73 FileSystemBackend* mount_point_provider = | 130 FileSystemBackend* mount_point_provider = |
74 file_system_context_->GetFileSystemBackend(src_type_); | 131 file_system_context_->GetFileSystemBackend(src_type_); |
75 mount_point_provider->OpenFileSystem( | 132 mount_point_provider->OpenFileSystem( |
76 origin_, src_type_, | 133 origin_, src_type_, |
77 OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | 134 OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, |
78 base::Bind(&ExpectOk)); | 135 base::Bind(&ExpectOk)); |
79 mount_point_provider = | 136 mount_point_provider = |
80 file_system_context_->GetFileSystemBackend(dest_type_); | 137 file_system_context_->GetFileSystemBackend(dest_type_); |
138 if (dest_type_ == kFileSystemTypeTest) { | |
139 TestFileSystemBackend* test_provider = | |
140 static_cast<TestFileSystemBackend*>(mount_point_provider); | |
141 scoped_ptr<CopyOrMoveFileValidatorFactory> factory( | |
142 new TestValidatorFactory); | |
143 test_provider->set_require_copy_or_move_validator(true); | |
144 test_provider->InitializeCopyOrMoveFileValidatorFactory(factory.Pass()); | |
145 } | |
81 mount_point_provider->OpenFileSystem( | 146 mount_point_provider->OpenFileSystem( |
82 origin_, dest_type_, | 147 origin_, dest_type_, |
83 OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | 148 OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, |
84 base::Bind(&ExpectOk)); | 149 base::Bind(&ExpectOk)); |
85 base::MessageLoop::current()->RunUntilIdle(); | 150 base::MessageLoop::current()->RunUntilIdle(); |
86 | 151 |
87 // Grant relatively big quota initially. | 152 // Grant relatively big quota initially. |
88 quota_manager_->SetQuota(origin_, | 153 quota_manager_->SetQuota(origin_, |
89 FileSystemTypeToQuotaStorageType(src_type_), | 154 FileSystemTypeToQuotaStorageType(src_type_), |
90 1024 * 1024); | 155 1024 * 1024); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
145 return result; | 210 return result; |
146 } | 211 } |
147 return result; | 212 return result; |
148 } | 213 } |
149 | 214 |
150 void VerifyTestCaseFiles( | 215 void VerifyTestCaseFiles( |
151 const FileSystemURL& root, | 216 const FileSystemURL& root, |
152 const test::TestCaseRecord* const test_cases, | 217 const test::TestCaseRecord* const test_cases, |
153 size_t test_case_size) { | 218 size_t test_case_size) { |
154 std::map<base::FilePath, const test::TestCaseRecord*> test_case_map; | 219 std::map<base::FilePath, const test::TestCaseRecord*> test_case_map; |
155 for (size_t i = 0; i < test_case_size; ++i) | 220 for (size_t i = 0; i < test_case_size; ++i) { |
156 test_case_map[ | 221 test_case_map[ |
157 base::FilePath(test_cases[i].path).NormalizePathSeparators()] = | 222 base::FilePath(test_cases[i].path).NormalizePathSeparators()] = |
158 &test_cases[i]; | 223 &test_cases[i]; |
224 } | |
159 | 225 |
160 std::queue<FileSystemURL> directories; | 226 std::queue<FileSystemURL> directories; |
161 FileEntryList entries; | 227 FileEntryList entries; |
162 directories.push(root); | 228 directories.push(root); |
163 while (!directories.empty()) { | 229 while (!directories.empty()) { |
164 FileSystemURL dir = directories.front(); | 230 FileSystemURL dir = directories.front(); |
165 directories.pop(); | 231 directories.pop(); |
166 ASSERT_EQ(base::PLATFORM_FILE_OK, ReadDirectory(dir, &entries)); | 232 ASSERT_EQ(base::PLATFORM_FILE_OK, ReadDirectory(dir, &entries)); |
167 for (size_t i = 0; i < entries.size(); ++i) { | 233 for (size_t i = 0; i < entries.size(); ++i) { |
168 FileSystemURL url = file_system_context_->CreateCrackedFileSystemURL( | 234 FileSystemURL url = file_system_context_->CreateCrackedFileSystemURL( |
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
421 test::kRegularTestCases, | 487 test::kRegularTestCases, |
422 test::kRegularTestCaseSize); | 488 test::kRegularTestCaseSize); |
423 | 489 |
424 int64 src_new_usage = helper.GetSourceUsage(); | 490 int64 src_new_usage = helper.GetSourceUsage(); |
425 ASSERT_EQ(src_initial_usage, src_new_usage); | 491 ASSERT_EQ(src_initial_usage, src_new_usage); |
426 | 492 |
427 int64 dest_increase = helper.GetDestUsage() - dest_initial_usage; | 493 int64 dest_increase = helper.GetDestUsage() - dest_initial_usage; |
428 ASSERT_EQ(src_increase, dest_increase); | 494 ASSERT_EQ(src_increase, dest_increase); |
429 } | 495 } |
430 | 496 |
497 TEST(LocalFileSystemCopyOrMoveOperationTest, | |
498 MoveDirectoryFailPostWriteValidation) { | |
499 CopyOrMoveOperationTestHelper helper(GURL("http://foo"), | |
500 kFileSystemTypeTemporary, | |
501 kFileSystemTypeTest); | |
502 helper.SetUp(); | |
503 | |
504 FileSystemURL src = helper.SourceURL("a"); | |
505 FileSystemURL dest = helper.DestURL("b"); | |
506 | |
507 // Set up a source directory. | |
508 ASSERT_EQ(base::PLATFORM_FILE_OK, helper.CreateDirectory(src)); | |
509 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
510 helper.SetUpTestCaseFiles(src, | |
511 test::kRegularTestCases, | |
512 test::kRegularTestCaseSize)); | |
513 | |
514 // Move it. | |
515 helper.Move(src, dest); | |
516 | |
517 // Verify. | |
518 ASSERT_TRUE(helper.DirectoryExists(src)); | |
519 ASSERT_TRUE(helper.DirectoryExists(dest)); | |
520 | |
521 test::TestCaseRecord kMoveDirResultCases[] = { | |
522 {false, FILE_PATH_LITERAL("file 0"), 38}, | |
523 {false, FILE_PATH_LITERAL("file 3"), 0}, | |
524 }; | |
525 | |
526 helper.VerifyTestCaseFiles(dest, | |
527 kMoveDirResultCases, | |
528 arraysize(kMoveDirResultCases)); | |
529 } | |
530 | |
431 } // namespace fileapi | 531 } // namespace fileapi |
OLD | NEW |