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/message_loop.h" | 11 #include "base/message_loop/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")); |
| 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.BaseName().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 24 matching lines...) Expand all Loading... |
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->InitializeFileSystem( | 132 mount_point_provider->InitializeFileSystem( |
76 origin_, src_type_, | 133 origin_, src_type_, |
77 OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | 134 OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, |
78 NULL /* context */, | 135 NULL /* context */, |
79 base::Bind(&ExpectOk)); | 136 base::Bind(&ExpectOk)); |
80 mount_point_provider = | 137 mount_point_provider = |
81 file_system_context_->GetFileSystemBackend(dest_type_); | 138 file_system_context_->GetFileSystemBackend(dest_type_); |
| 139 if (dest_type_ == kFileSystemTypeTest) { |
| 140 TestFileSystemBackend* test_provider = |
| 141 static_cast<TestFileSystemBackend*>(mount_point_provider); |
| 142 scoped_ptr<CopyOrMoveFileValidatorFactory> factory( |
| 143 new TestValidatorFactory); |
| 144 test_provider->set_require_copy_or_move_validator(true); |
| 145 test_provider->InitializeCopyOrMoveFileValidatorFactory(factory.Pass()); |
| 146 } |
82 mount_point_provider->InitializeFileSystem( | 147 mount_point_provider->InitializeFileSystem( |
83 origin_, dest_type_, | 148 origin_, dest_type_, |
84 OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | 149 OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, |
85 NULL /* context */, | 150 NULL /* context */, |
86 base::Bind(&ExpectOk)); | 151 base::Bind(&ExpectOk)); |
87 base::MessageLoop::current()->RunUntilIdle(); | 152 base::MessageLoop::current()->RunUntilIdle(); |
88 | 153 |
89 // Grant relatively big quota initially. | 154 // Grant relatively big quota initially. |
90 quota_manager_->SetQuota(origin_, | 155 quota_manager_->SetQuota(origin_, |
91 FileSystemTypeToQuotaStorageType(src_type_), | 156 FileSystemTypeToQuotaStorageType(src_type_), |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 return result; | 212 return result; |
148 } | 213 } |
149 return result; | 214 return result; |
150 } | 215 } |
151 | 216 |
152 void VerifyTestCaseFiles( | 217 void VerifyTestCaseFiles( |
153 const FileSystemURL& root, | 218 const FileSystemURL& root, |
154 const test::TestCaseRecord* const test_cases, | 219 const test::TestCaseRecord* const test_cases, |
155 size_t test_case_size) { | 220 size_t test_case_size) { |
156 std::map<base::FilePath, const test::TestCaseRecord*> test_case_map; | 221 std::map<base::FilePath, const test::TestCaseRecord*> test_case_map; |
157 for (size_t i = 0; i < test_case_size; ++i) | 222 for (size_t i = 0; i < test_case_size; ++i) { |
158 test_case_map[ | 223 test_case_map[ |
159 base::FilePath(test_cases[i].path).NormalizePathSeparators()] = | 224 base::FilePath(test_cases[i].path).NormalizePathSeparators()] = |
160 &test_cases[i]; | 225 &test_cases[i]; |
| 226 } |
161 | 227 |
162 std::queue<FileSystemURL> directories; | 228 std::queue<FileSystemURL> directories; |
163 FileEntryList entries; | 229 FileEntryList entries; |
164 directories.push(root); | 230 directories.push(root); |
165 while (!directories.empty()) { | 231 while (!directories.empty()) { |
166 FileSystemURL dir = directories.front(); | 232 FileSystemURL dir = directories.front(); |
167 directories.pop(); | 233 directories.pop(); |
168 ASSERT_EQ(base::PLATFORM_FILE_OK, ReadDirectory(dir, &entries)); | 234 ASSERT_EQ(base::PLATFORM_FILE_OK, ReadDirectory(dir, &entries)); |
169 for (size_t i = 0; i < entries.size(); ++i) { | 235 for (size_t i = 0; i < entries.size(); ++i) { |
170 FileSystemURL url = file_system_context_->CreateCrackedFileSystemURL( | 236 FileSystemURL url = file_system_context_->CreateCrackedFileSystemURL( |
171 dir.origin(), | 237 dir.origin(), |
172 dir.mount_type(), | 238 dir.mount_type(), |
173 dir.virtual_path().Append(entries[i].name)); | 239 dir.virtual_path().Append(entries[i].name)); |
174 base::FilePath relative; | 240 base::FilePath relative; |
175 root.virtual_path().AppendRelativePath(url.virtual_path(), &relative); | 241 root.virtual_path().AppendRelativePath(url.virtual_path(), &relative); |
176 relative = relative.NormalizePathSeparators(); | 242 relative = relative.NormalizePathSeparators(); |
177 ASSERT_TRUE(ContainsKey(test_case_map, relative)); | 243 ASSERT_TRUE(ContainsKey(test_case_map, relative)); |
178 if (entries[i].is_directory) { | 244 if (entries[i].is_directory) { |
179 EXPECT_TRUE(test_case_map[relative]->is_directory); | 245 EXPECT_TRUE(test_case_map[relative]->is_directory); |
180 directories.push(url); | 246 directories.push(url); |
181 } else { | 247 } else { |
182 EXPECT_FALSE(test_case_map[relative]->is_directory); | 248 EXPECT_FALSE(test_case_map[relative]->is_directory); |
183 EXPECT_TRUE(FileExists(url, test_case_map[relative]->data_file_size)); | 249 EXPECT_TRUE(FileExists(url, test_case_map[relative]->data_file_size)); |
184 } | 250 } |
185 test_case_map.erase(relative); | 251 test_case_map.erase(relative); |
186 } | 252 } |
187 } | 253 } |
188 EXPECT_TRUE(test_case_map.empty()); | 254 EXPECT_TRUE(test_case_map.empty()); |
| 255 std::map<base::FilePath, const test::TestCaseRecord*>::const_iterator it; |
| 256 for (it = test_case_map.begin(); it != test_case_map.end(); ++it) { |
| 257 LOG(ERROR) << "Extra entry: " << it->first.LossyDisplayName(); |
| 258 } |
189 } | 259 } |
190 | 260 |
191 base::PlatformFileError ReadDirectory(const FileSystemURL& url, | 261 base::PlatformFileError ReadDirectory(const FileSystemURL& url, |
192 FileEntryList* entries) { | 262 FileEntryList* entries) { |
193 return AsyncFileTestHelper::ReadDirectory( | 263 return AsyncFileTestHelper::ReadDirectory( |
194 file_system_context_.get(), url, entries); | 264 file_system_context_.get(), url, entries); |
195 } | 265 } |
196 | 266 |
197 base::PlatformFileError CreateDirectory(const FileSystemURL& url) { | 267 base::PlatformFileError CreateDirectory(const FileSystemURL& url) { |
198 return AsyncFileTestHelper::CreateDirectory(file_system_context_.get(), | 268 return AsyncFileTestHelper::CreateDirectory(file_system_context_.get(), |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
423 test::kRegularTestCases, | 493 test::kRegularTestCases, |
424 test::kRegularTestCaseSize); | 494 test::kRegularTestCaseSize); |
425 | 495 |
426 int64 src_new_usage = helper.GetSourceUsage(); | 496 int64 src_new_usage = helper.GetSourceUsage(); |
427 ASSERT_EQ(src_initial_usage, src_new_usage); | 497 ASSERT_EQ(src_initial_usage, src_new_usage); |
428 | 498 |
429 int64 dest_increase = helper.GetDestUsage() - dest_initial_usage; | 499 int64 dest_increase = helper.GetDestUsage() - dest_initial_usage; |
430 ASSERT_EQ(src_increase, dest_increase); | 500 ASSERT_EQ(src_increase, dest_increase); |
431 } | 501 } |
432 | 502 |
| 503 TEST(LocalFileSystemCopyOrMoveOperationTest, |
| 504 MoveDirectoryFailPostWriteValidation) { |
| 505 CopyOrMoveOperationTestHelper helper(GURL("http://foo"), |
| 506 kFileSystemTypeTemporary, |
| 507 kFileSystemTypeTest); |
| 508 helper.SetUp(); |
| 509 |
| 510 FileSystemURL src = helper.SourceURL("a"); |
| 511 FileSystemURL dest = helper.DestURL("b"); |
| 512 |
| 513 // Set up a source directory. |
| 514 ASSERT_EQ(base::PLATFORM_FILE_OK, helper.CreateDirectory(src)); |
| 515 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 516 helper.SetUpTestCaseFiles(src, |
| 517 test::kRegularTestCases, |
| 518 test::kRegularTestCaseSize)); |
| 519 |
| 520 // Move it. |
| 521 helper.Move(src, dest); |
| 522 |
| 523 // Verify. |
| 524 ASSERT_TRUE(helper.DirectoryExists(src)); |
| 525 ASSERT_TRUE(helper.DirectoryExists(dest)); |
| 526 |
| 527 test::TestCaseRecord kMoveDirResultCases[] = { |
| 528 {false, FILE_PATH_LITERAL("file 0"), 38}, |
| 529 {false, FILE_PATH_LITERAL("file 3"), 0}, |
| 530 }; |
| 531 |
| 532 helper.VerifyTestCaseFiles(dest, |
| 533 kMoveDirResultCases, |
| 534 arraysize(kMoveDirResultCases)); |
| 535 } |
| 536 |
433 } // namespace fileapi | 537 } // namespace fileapi |
OLD | NEW |