| 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 <stddef.h> | |
| 6 #include <stdint.h> | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/files/file_path.h" | |
| 11 #include "base/files/scoped_temp_dir.h" | |
| 12 #include "base/location.h" | |
| 13 #include "base/macros.h" | |
| 14 #include "base/run_loop.h" | |
| 15 #include "base/single_thread_task_runner.h" | |
| 16 #include "base/threading/thread_task_runner_handle.h" | |
| 17 #include "content/public/test/async_file_test_helper.h" | |
| 18 #include "content/public/test/mock_special_storage_policy.h" | |
| 19 #include "content/public/test/test_file_system_backend.h" | |
| 20 #include "content/public/test/test_file_system_context.h" | |
| 21 #include "storage/browser/blob/shareable_file_reference.h" | |
| 22 #include "storage/browser/fileapi/copy_or_move_file_validator.h" | |
| 23 #include "storage/browser/fileapi/external_mount_points.h" | |
| 24 #include "storage/browser/fileapi/file_system_backend.h" | |
| 25 #include "storage/browser/fileapi/file_system_context.h" | |
| 26 #include "storage/browser/fileapi/file_system_url.h" | |
| 27 #include "storage/browser/fileapi/isolated_context.h" | |
| 28 #include "storage/common/fileapi/file_system_util.h" | |
| 29 #include "testing/gtest/include/gtest/gtest.h" | |
| 30 | |
| 31 using content::AsyncFileTestHelper; | |
| 32 using storage::CopyOrMoveFileValidator; | |
| 33 using storage::CopyOrMoveFileValidatorFactory; | |
| 34 using storage::FileSystemURL; | |
| 35 | |
| 36 namespace content { | |
| 37 | |
| 38 namespace { | |
| 39 | |
| 40 const storage::FileSystemType kNoValidatorType = | |
| 41 storage::kFileSystemTypeTemporary; | |
| 42 const storage::FileSystemType kWithValidatorType = storage::kFileSystemTypeTest; | |
| 43 | |
| 44 void ExpectOk(const GURL& origin_url, | |
| 45 const std::string& name, | |
| 46 base::File::Error error) { | |
| 47 ASSERT_EQ(base::File::FILE_OK, error); | |
| 48 } | |
| 49 | |
| 50 class CopyOrMoveFileValidatorTestHelper { | |
| 51 public: | |
| 52 CopyOrMoveFileValidatorTestHelper(const GURL& origin, | |
| 53 storage::FileSystemType src_type, | |
| 54 storage::FileSystemType dest_type) | |
| 55 : origin_(origin), src_type_(src_type), dest_type_(dest_type) {} | |
| 56 | |
| 57 ~CopyOrMoveFileValidatorTestHelper() { | |
| 58 file_system_context_ = NULL; | |
| 59 base::RunLoop().RunUntilIdle(); | |
| 60 } | |
| 61 | |
| 62 void SetUp() { | |
| 63 ASSERT_TRUE(base_.CreateUniqueTempDir()); | |
| 64 base::FilePath base_dir = base_.GetPath(); | |
| 65 | |
| 66 file_system_context_ = CreateFileSystemContextForTesting(NULL, base_dir); | |
| 67 | |
| 68 // Set up TestFileSystemBackend to require CopyOrMoveFileValidator. | |
| 69 storage::FileSystemBackend* test_file_system_backend = | |
| 70 file_system_context_->GetFileSystemBackend(kWithValidatorType); | |
| 71 static_cast<TestFileSystemBackend*>(test_file_system_backend)-> | |
| 72 set_require_copy_or_move_validator(true); | |
| 73 | |
| 74 // Sets up source. | |
| 75 storage::FileSystemBackend* src_file_system_backend = | |
| 76 file_system_context_->GetFileSystemBackend(src_type_); | |
| 77 src_file_system_backend->ResolveURL( | |
| 78 FileSystemURL::CreateForTest(origin_, src_type_, base::FilePath()), | |
| 79 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | |
| 80 base::Bind(&ExpectOk)); | |
| 81 base::RunLoop().RunUntilIdle(); | |
| 82 ASSERT_EQ(base::File::FILE_OK, CreateDirectory(SourceURL(""))); | |
| 83 | |
| 84 // Sets up dest. | |
| 85 DCHECK_EQ(kWithValidatorType, dest_type_); | |
| 86 ASSERT_EQ(base::File::FILE_OK, CreateDirectory(DestURL(""))); | |
| 87 | |
| 88 copy_src_ = SourceURL("copy_src.jpg"); | |
| 89 move_src_ = SourceURL("move_src.jpg"); | |
| 90 copy_dest_ = DestURL("copy_dest.jpg"); | |
| 91 move_dest_ = DestURL("move_dest.jpg"); | |
| 92 | |
| 93 ASSERT_EQ(base::File::FILE_OK, CreateFile(copy_src_, 10)); | |
| 94 ASSERT_EQ(base::File::FILE_OK, CreateFile(move_src_, 10)); | |
| 95 | |
| 96 ASSERT_TRUE(FileExists(copy_src_, 10)); | |
| 97 ASSERT_TRUE(FileExists(move_src_, 10)); | |
| 98 ASSERT_FALSE(FileExists(copy_dest_, 10)); | |
| 99 ASSERT_FALSE(FileExists(move_dest_, 10)); | |
| 100 } | |
| 101 | |
| 102 void SetMediaCopyOrMoveFileValidatorFactory( | |
| 103 std::unique_ptr<storage::CopyOrMoveFileValidatorFactory> factory) { | |
| 104 TestFileSystemBackend* backend = static_cast<TestFileSystemBackend*>( | |
| 105 file_system_context_->GetFileSystemBackend(kWithValidatorType)); | |
| 106 backend->InitializeCopyOrMoveFileValidatorFactory(std::move(factory)); | |
| 107 } | |
| 108 | |
| 109 void CopyTest(base::File::Error expected) { | |
| 110 ASSERT_TRUE(FileExists(copy_src_, 10)); | |
| 111 ASSERT_FALSE(FileExists(copy_dest_, 10)); | |
| 112 | |
| 113 EXPECT_EQ(expected, | |
| 114 AsyncFileTestHelper::Copy( | |
| 115 file_system_context_.get(), copy_src_, copy_dest_)); | |
| 116 | |
| 117 EXPECT_TRUE(FileExists(copy_src_, 10)); | |
| 118 if (expected == base::File::FILE_OK) | |
| 119 EXPECT_TRUE(FileExists(copy_dest_, 10)); | |
| 120 else | |
| 121 EXPECT_FALSE(FileExists(copy_dest_, 10)); | |
| 122 }; | |
| 123 | |
| 124 void MoveTest(base::File::Error expected) { | |
| 125 ASSERT_TRUE(FileExists(move_src_, 10)); | |
| 126 ASSERT_FALSE(FileExists(move_dest_, 10)); | |
| 127 | |
| 128 EXPECT_EQ(expected, | |
| 129 AsyncFileTestHelper::Move( | |
| 130 file_system_context_.get(), move_src_, move_dest_)); | |
| 131 | |
| 132 if (expected == base::File::FILE_OK) { | |
| 133 EXPECT_FALSE(FileExists(move_src_, 10)); | |
| 134 EXPECT_TRUE(FileExists(move_dest_, 10)); | |
| 135 } else { | |
| 136 EXPECT_TRUE(FileExists(move_src_, 10)); | |
| 137 EXPECT_FALSE(FileExists(move_dest_, 10)); | |
| 138 } | |
| 139 }; | |
| 140 | |
| 141 private: | |
| 142 FileSystemURL SourceURL(const std::string& path) { | |
| 143 return file_system_context_->CreateCrackedFileSystemURL( | |
| 144 origin_, src_type_, | |
| 145 base::FilePath().AppendASCII("src").AppendASCII(path)); | |
| 146 } | |
| 147 | |
| 148 FileSystemURL DestURL(const std::string& path) { | |
| 149 return file_system_context_->CreateCrackedFileSystemURL( | |
| 150 origin_, dest_type_, | |
| 151 base::FilePath().AppendASCII("dest").AppendASCII(path)); | |
| 152 } | |
| 153 | |
| 154 base::File::Error CreateFile(const FileSystemURL& url, size_t size) { | |
| 155 base::File::Error result = | |
| 156 AsyncFileTestHelper::CreateFile(file_system_context_.get(), url); | |
| 157 if (result != base::File::FILE_OK) | |
| 158 return result; | |
| 159 return AsyncFileTestHelper::TruncateFile( | |
| 160 file_system_context_.get(), url, size); | |
| 161 } | |
| 162 | |
| 163 base::File::Error CreateDirectory(const FileSystemURL& url) { | |
| 164 return AsyncFileTestHelper::CreateDirectory(file_system_context_.get(), | |
| 165 url); | |
| 166 } | |
| 167 | |
| 168 bool FileExists(const FileSystemURL& url, int64_t expected_size) { | |
| 169 return AsyncFileTestHelper::FileExists( | |
| 170 file_system_context_.get(), url, expected_size); | |
| 171 } | |
| 172 | |
| 173 base::ScopedTempDir base_; | |
| 174 | |
| 175 const GURL origin_; | |
| 176 | |
| 177 const storage::FileSystemType src_type_; | |
| 178 const storage::FileSystemType dest_type_; | |
| 179 std::string src_fsid_; | |
| 180 std::string dest_fsid_; | |
| 181 | |
| 182 base::MessageLoop message_loop_; | |
| 183 scoped_refptr<storage::FileSystemContext> file_system_context_; | |
| 184 | |
| 185 FileSystemURL copy_src_; | |
| 186 FileSystemURL copy_dest_; | |
| 187 FileSystemURL move_src_; | |
| 188 FileSystemURL move_dest_; | |
| 189 | |
| 190 DISALLOW_COPY_AND_ASSIGN(CopyOrMoveFileValidatorTestHelper); | |
| 191 }; | |
| 192 | |
| 193 // For TestCopyOrMoveFileValidatorFactory | |
| 194 enum Validity { | |
| 195 VALID, | |
| 196 PRE_WRITE_INVALID, | |
| 197 POST_WRITE_INVALID | |
| 198 }; | |
| 199 | |
| 200 class TestCopyOrMoveFileValidatorFactory | |
| 201 : public storage::CopyOrMoveFileValidatorFactory { | |
| 202 public: | |
| 203 // A factory that creates validators that accept everything or nothing. | |
| 204 // TODO(gbillock): switch args to enum or something | |
| 205 explicit TestCopyOrMoveFileValidatorFactory(Validity validity) | |
| 206 : validity_(validity) {} | |
| 207 ~TestCopyOrMoveFileValidatorFactory() override {} | |
| 208 | |
| 209 storage::CopyOrMoveFileValidator* CreateCopyOrMoveFileValidator( | |
| 210 const FileSystemURL& /*src_url*/, | |
| 211 const base::FilePath& /*platform_path*/) override { | |
| 212 return new TestCopyOrMoveFileValidator(validity_); | |
| 213 } | |
| 214 | |
| 215 private: | |
| 216 class TestCopyOrMoveFileValidator : public CopyOrMoveFileValidator { | |
| 217 public: | |
| 218 explicit TestCopyOrMoveFileValidator(Validity validity) | |
| 219 : result_(validity == VALID || validity == POST_WRITE_INVALID ? | |
| 220 base::File::FILE_OK : | |
| 221 base::File::FILE_ERROR_SECURITY), | |
| 222 write_result_(validity == VALID || validity == PRE_WRITE_INVALID ? | |
| 223 base::File::FILE_OK : | |
| 224 base::File::FILE_ERROR_SECURITY) { | |
| 225 } | |
| 226 ~TestCopyOrMoveFileValidator() override {} | |
| 227 | |
| 228 void StartPreWriteValidation( | |
| 229 const ResultCallback& result_callback) override { | |
| 230 // Post the result since a real validator must do work asynchronously. | |
| 231 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 232 FROM_HERE, base::Bind(result_callback, result_)); | |
| 233 } | |
| 234 | |
| 235 void StartPostWriteValidation( | |
| 236 const base::FilePath& dest_platform_path, | |
| 237 const ResultCallback& result_callback) override { | |
| 238 // Post the result since a real validator must do work asynchronously. | |
| 239 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 240 FROM_HERE, base::Bind(result_callback, write_result_)); | |
| 241 } | |
| 242 | |
| 243 private: | |
| 244 base::File::Error result_; | |
| 245 base::File::Error write_result_; | |
| 246 | |
| 247 DISALLOW_COPY_AND_ASSIGN(TestCopyOrMoveFileValidator); | |
| 248 }; | |
| 249 | |
| 250 Validity validity_; | |
| 251 | |
| 252 DISALLOW_COPY_AND_ASSIGN(TestCopyOrMoveFileValidatorFactory); | |
| 253 }; | |
| 254 | |
| 255 } // namespace | |
| 256 | |
| 257 TEST(CopyOrMoveFileValidatorTest, NoValidatorWithinSameFSType) { | |
| 258 // Within a file system type, validation is not expected, so it should | |
| 259 // work for kWithValidatorType without a validator set. | |
| 260 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), | |
| 261 kWithValidatorType, | |
| 262 kWithValidatorType); | |
| 263 helper.SetUp(); | |
| 264 helper.CopyTest(base::File::FILE_OK); | |
| 265 helper.MoveTest(base::File::FILE_OK); | |
| 266 } | |
| 267 | |
| 268 TEST(CopyOrMoveFileValidatorTest, MissingValidator) { | |
| 269 // Copying or moving into a kWithValidatorType requires a file | |
| 270 // validator. An error is expected if copy is attempted without a validator. | |
| 271 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), | |
| 272 kNoValidatorType, | |
| 273 kWithValidatorType); | |
| 274 helper.SetUp(); | |
| 275 helper.CopyTest(base::File::FILE_ERROR_SECURITY); | |
| 276 helper.MoveTest(base::File::FILE_ERROR_SECURITY); | |
| 277 } | |
| 278 | |
| 279 TEST(CopyOrMoveFileValidatorTest, AcceptAll) { | |
| 280 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), | |
| 281 kNoValidatorType, | |
| 282 kWithValidatorType); | |
| 283 helper.SetUp(); | |
| 284 std::unique_ptr<CopyOrMoveFileValidatorFactory> factory( | |
| 285 new TestCopyOrMoveFileValidatorFactory(VALID)); | |
| 286 helper.SetMediaCopyOrMoveFileValidatorFactory(std::move(factory)); | |
| 287 | |
| 288 helper.CopyTest(base::File::FILE_OK); | |
| 289 helper.MoveTest(base::File::FILE_OK); | |
| 290 } | |
| 291 | |
| 292 TEST(CopyOrMoveFileValidatorTest, AcceptNone) { | |
| 293 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), | |
| 294 kNoValidatorType, | |
| 295 kWithValidatorType); | |
| 296 helper.SetUp(); | |
| 297 std::unique_ptr<CopyOrMoveFileValidatorFactory> factory( | |
| 298 new TestCopyOrMoveFileValidatorFactory(PRE_WRITE_INVALID)); | |
| 299 helper.SetMediaCopyOrMoveFileValidatorFactory(std::move(factory)); | |
| 300 | |
| 301 helper.CopyTest(base::File::FILE_ERROR_SECURITY); | |
| 302 helper.MoveTest(base::File::FILE_ERROR_SECURITY); | |
| 303 } | |
| 304 | |
| 305 TEST(CopyOrMoveFileValidatorTest, OverrideValidator) { | |
| 306 // Once set, you can not override the validator. | |
| 307 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), | |
| 308 kNoValidatorType, | |
| 309 kWithValidatorType); | |
| 310 helper.SetUp(); | |
| 311 std::unique_ptr<CopyOrMoveFileValidatorFactory> reject_factory( | |
| 312 new TestCopyOrMoveFileValidatorFactory(PRE_WRITE_INVALID)); | |
| 313 helper.SetMediaCopyOrMoveFileValidatorFactory(std::move(reject_factory)); | |
| 314 | |
| 315 std::unique_ptr<CopyOrMoveFileValidatorFactory> accept_factory( | |
| 316 new TestCopyOrMoveFileValidatorFactory(VALID)); | |
| 317 helper.SetMediaCopyOrMoveFileValidatorFactory(std::move(accept_factory)); | |
| 318 | |
| 319 helper.CopyTest(base::File::FILE_ERROR_SECURITY); | |
| 320 helper.MoveTest(base::File::FILE_ERROR_SECURITY); | |
| 321 } | |
| 322 | |
| 323 TEST(CopyOrMoveFileValidatorTest, RejectPostWrite) { | |
| 324 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), | |
| 325 kNoValidatorType, | |
| 326 kWithValidatorType); | |
| 327 helper.SetUp(); | |
| 328 std::unique_ptr<CopyOrMoveFileValidatorFactory> factory( | |
| 329 new TestCopyOrMoveFileValidatorFactory(POST_WRITE_INVALID)); | |
| 330 helper.SetMediaCopyOrMoveFileValidatorFactory(std::move(factory)); | |
| 331 | |
| 332 helper.CopyTest(base::File::FILE_ERROR_SECURITY); | |
| 333 helper.MoveTest(base::File::FILE_ERROR_SECURITY); | |
| 334 } | |
| 335 | |
| 336 } // namespace content | |
| OLD | NEW |