| 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/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "content/public/test/async_file_test_helper.h" | 10 #include "content/public/test/async_file_test_helper.h" |
| 11 #include "content/public/test/mock_special_storage_policy.h" | 11 #include "content/public/test/mock_special_storage_policy.h" |
| 12 #include "content/public/test/test_file_system_backend.h" | 12 #include "content/public/test/test_file_system_backend.h" |
| 13 #include "content/public/test/test_file_system_context.h" | 13 #include "content/public/test/test_file_system_context.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "webkit/browser/fileapi/copy_or_move_file_validator.h" | 15 #include "storage/browser/fileapi/copy_or_move_file_validator.h" |
| 16 #include "webkit/browser/fileapi/external_mount_points.h" | 16 #include "storage/browser/fileapi/external_mount_points.h" |
| 17 #include "webkit/browser/fileapi/file_system_backend.h" | 17 #include "storage/browser/fileapi/file_system_backend.h" |
| 18 #include "webkit/browser/fileapi/file_system_context.h" | 18 #include "storage/browser/fileapi/file_system_context.h" |
| 19 #include "webkit/browser/fileapi/file_system_url.h" | 19 #include "storage/browser/fileapi/file_system_url.h" |
| 20 #include "webkit/browser/fileapi/isolated_context.h" | 20 #include "storage/browser/fileapi/isolated_context.h" |
| 21 #include "webkit/common/blob/shareable_file_reference.h" | 21 #include "storage/common/blob/shareable_file_reference.h" |
| 22 #include "webkit/common/fileapi/file_system_util.h" | 22 #include "storage/common/fileapi/file_system_util.h" |
| 23 | 23 |
| 24 using content::AsyncFileTestHelper; | 24 using content::AsyncFileTestHelper; |
| 25 using fileapi::CopyOrMoveFileValidator; | 25 using storage::CopyOrMoveFileValidator; |
| 26 using fileapi::CopyOrMoveFileValidatorFactory; | 26 using storage::CopyOrMoveFileValidatorFactory; |
| 27 using fileapi::FileSystemURL; | 27 using storage::FileSystemURL; |
| 28 | 28 |
| 29 namespace content { | 29 namespace content { |
| 30 | 30 |
| 31 namespace { | 31 namespace { |
| 32 | 32 |
| 33 const fileapi::FileSystemType kNoValidatorType = | 33 const storage::FileSystemType kNoValidatorType = |
| 34 fileapi::kFileSystemTypeTemporary; | 34 storage::kFileSystemTypeTemporary; |
| 35 const fileapi::FileSystemType kWithValidatorType = fileapi::kFileSystemTypeTest; | 35 const storage::FileSystemType kWithValidatorType = storage::kFileSystemTypeTest; |
| 36 | 36 |
| 37 void ExpectOk(const GURL& origin_url, | 37 void ExpectOk(const GURL& origin_url, |
| 38 const std::string& name, | 38 const std::string& name, |
| 39 base::File::Error error) { | 39 base::File::Error error) { |
| 40 ASSERT_EQ(base::File::FILE_OK, error); | 40 ASSERT_EQ(base::File::FILE_OK, error); |
| 41 } | 41 } |
| 42 | 42 |
| 43 class CopyOrMoveFileValidatorTestHelper { | 43 class CopyOrMoveFileValidatorTestHelper { |
| 44 public: | 44 public: |
| 45 CopyOrMoveFileValidatorTestHelper(const GURL& origin, | 45 CopyOrMoveFileValidatorTestHelper(const GURL& origin, |
| 46 fileapi::FileSystemType src_type, | 46 storage::FileSystemType src_type, |
| 47 fileapi::FileSystemType dest_type) | 47 storage::FileSystemType dest_type) |
| 48 : origin_(origin), src_type_(src_type), dest_type_(dest_type) {} | 48 : origin_(origin), src_type_(src_type), dest_type_(dest_type) {} |
| 49 | 49 |
| 50 ~CopyOrMoveFileValidatorTestHelper() { | 50 ~CopyOrMoveFileValidatorTestHelper() { |
| 51 file_system_context_ = NULL; | 51 file_system_context_ = NULL; |
| 52 base::RunLoop().RunUntilIdle(); | 52 base::RunLoop().RunUntilIdle(); |
| 53 } | 53 } |
| 54 | 54 |
| 55 void SetUp() { | 55 void SetUp() { |
| 56 ASSERT_TRUE(base_.CreateUniqueTempDir()); | 56 ASSERT_TRUE(base_.CreateUniqueTempDir()); |
| 57 base::FilePath base_dir = base_.path(); | 57 base::FilePath base_dir = base_.path(); |
| 58 | 58 |
| 59 file_system_context_ = CreateFileSystemContextForTesting(NULL, base_dir); | 59 file_system_context_ = CreateFileSystemContextForTesting(NULL, base_dir); |
| 60 | 60 |
| 61 // Set up TestFileSystemBackend to require CopyOrMoveFileValidator. | 61 // Set up TestFileSystemBackend to require CopyOrMoveFileValidator. |
| 62 fileapi::FileSystemBackend* test_file_system_backend = | 62 storage::FileSystemBackend* test_file_system_backend = |
| 63 file_system_context_->GetFileSystemBackend(kWithValidatorType); | 63 file_system_context_->GetFileSystemBackend(kWithValidatorType); |
| 64 static_cast<TestFileSystemBackend*>(test_file_system_backend)-> | 64 static_cast<TestFileSystemBackend*>(test_file_system_backend)-> |
| 65 set_require_copy_or_move_validator(true); | 65 set_require_copy_or_move_validator(true); |
| 66 | 66 |
| 67 // Sets up source. | 67 // Sets up source. |
| 68 fileapi::FileSystemBackend* src_file_system_backend = | 68 storage::FileSystemBackend* src_file_system_backend = |
| 69 file_system_context_->GetFileSystemBackend(src_type_); | 69 file_system_context_->GetFileSystemBackend(src_type_); |
| 70 src_file_system_backend->ResolveURL( | 70 src_file_system_backend->ResolveURL( |
| 71 FileSystemURL::CreateForTest(origin_, src_type_, base::FilePath()), | 71 FileSystemURL::CreateForTest(origin_, src_type_, base::FilePath()), |
| 72 fileapi::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | 72 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, |
| 73 base::Bind(&ExpectOk)); | 73 base::Bind(&ExpectOk)); |
| 74 base::RunLoop().RunUntilIdle(); | 74 base::RunLoop().RunUntilIdle(); |
| 75 ASSERT_EQ(base::File::FILE_OK, CreateDirectory(SourceURL(""))); | 75 ASSERT_EQ(base::File::FILE_OK, CreateDirectory(SourceURL(""))); |
| 76 | 76 |
| 77 // Sets up dest. | 77 // Sets up dest. |
| 78 DCHECK_EQ(kWithValidatorType, dest_type_); | 78 DCHECK_EQ(kWithValidatorType, dest_type_); |
| 79 ASSERT_EQ(base::File::FILE_OK, CreateDirectory(DestURL(""))); | 79 ASSERT_EQ(base::File::FILE_OK, CreateDirectory(DestURL(""))); |
| 80 | 80 |
| 81 copy_src_ = SourceURL("copy_src.jpg"); | 81 copy_src_ = SourceURL("copy_src.jpg"); |
| 82 move_src_ = SourceURL("move_src.jpg"); | 82 move_src_ = SourceURL("move_src.jpg"); |
| 83 copy_dest_ = DestURL("copy_dest.jpg"); | 83 copy_dest_ = DestURL("copy_dest.jpg"); |
| 84 move_dest_ = DestURL("move_dest.jpg"); | 84 move_dest_ = DestURL("move_dest.jpg"); |
| 85 | 85 |
| 86 ASSERT_EQ(base::File::FILE_OK, CreateFile(copy_src_, 10)); | 86 ASSERT_EQ(base::File::FILE_OK, CreateFile(copy_src_, 10)); |
| 87 ASSERT_EQ(base::File::FILE_OK, CreateFile(move_src_, 10)); | 87 ASSERT_EQ(base::File::FILE_OK, CreateFile(move_src_, 10)); |
| 88 | 88 |
| 89 ASSERT_TRUE(FileExists(copy_src_, 10)); | 89 ASSERT_TRUE(FileExists(copy_src_, 10)); |
| 90 ASSERT_TRUE(FileExists(move_src_, 10)); | 90 ASSERT_TRUE(FileExists(move_src_, 10)); |
| 91 ASSERT_FALSE(FileExists(copy_dest_, 10)); | 91 ASSERT_FALSE(FileExists(copy_dest_, 10)); |
| 92 ASSERT_FALSE(FileExists(move_dest_, 10)); | 92 ASSERT_FALSE(FileExists(move_dest_, 10)); |
| 93 } | 93 } |
| 94 | 94 |
| 95 void SetMediaCopyOrMoveFileValidatorFactory( | 95 void SetMediaCopyOrMoveFileValidatorFactory( |
| 96 scoped_ptr<fileapi::CopyOrMoveFileValidatorFactory> factory) { | 96 scoped_ptr<storage::CopyOrMoveFileValidatorFactory> factory) { |
| 97 TestFileSystemBackend* backend = static_cast<TestFileSystemBackend*>( | 97 TestFileSystemBackend* backend = static_cast<TestFileSystemBackend*>( |
| 98 file_system_context_->GetFileSystemBackend(kWithValidatorType)); | 98 file_system_context_->GetFileSystemBackend(kWithValidatorType)); |
| 99 backend->InitializeCopyOrMoveFileValidatorFactory(factory.Pass()); | 99 backend->InitializeCopyOrMoveFileValidatorFactory(factory.Pass()); |
| 100 } | 100 } |
| 101 | 101 |
| 102 void CopyTest(base::File::Error expected) { | 102 void CopyTest(base::File::Error expected) { |
| 103 ASSERT_TRUE(FileExists(copy_src_, 10)); | 103 ASSERT_TRUE(FileExists(copy_src_, 10)); |
| 104 ASSERT_FALSE(FileExists(copy_dest_, 10)); | 104 ASSERT_FALSE(FileExists(copy_dest_, 10)); |
| 105 | 105 |
| 106 EXPECT_EQ(expected, | 106 EXPECT_EQ(expected, |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 | 160 |
| 161 bool FileExists(const FileSystemURL& url, int64 expected_size) { | 161 bool FileExists(const FileSystemURL& url, int64 expected_size) { |
| 162 return AsyncFileTestHelper::FileExists( | 162 return AsyncFileTestHelper::FileExists( |
| 163 file_system_context_.get(), url, expected_size); | 163 file_system_context_.get(), url, expected_size); |
| 164 } | 164 } |
| 165 | 165 |
| 166 base::ScopedTempDir base_; | 166 base::ScopedTempDir base_; |
| 167 | 167 |
| 168 const GURL origin_; | 168 const GURL origin_; |
| 169 | 169 |
| 170 const fileapi::FileSystemType src_type_; | 170 const storage::FileSystemType src_type_; |
| 171 const fileapi::FileSystemType dest_type_; | 171 const storage::FileSystemType dest_type_; |
| 172 std::string src_fsid_; | 172 std::string src_fsid_; |
| 173 std::string dest_fsid_; | 173 std::string dest_fsid_; |
| 174 | 174 |
| 175 base::MessageLoop message_loop_; | 175 base::MessageLoop message_loop_; |
| 176 scoped_refptr<fileapi::FileSystemContext> file_system_context_; | 176 scoped_refptr<storage::FileSystemContext> file_system_context_; |
| 177 | 177 |
| 178 FileSystemURL copy_src_; | 178 FileSystemURL copy_src_; |
| 179 FileSystemURL copy_dest_; | 179 FileSystemURL copy_dest_; |
| 180 FileSystemURL move_src_; | 180 FileSystemURL move_src_; |
| 181 FileSystemURL move_dest_; | 181 FileSystemURL move_dest_; |
| 182 | 182 |
| 183 DISALLOW_COPY_AND_ASSIGN(CopyOrMoveFileValidatorTestHelper); | 183 DISALLOW_COPY_AND_ASSIGN(CopyOrMoveFileValidatorTestHelper); |
| 184 }; | 184 }; |
| 185 | 185 |
| 186 // For TestCopyOrMoveFileValidatorFactory | 186 // For TestCopyOrMoveFileValidatorFactory |
| 187 enum Validity { | 187 enum Validity { |
| 188 VALID, | 188 VALID, |
| 189 PRE_WRITE_INVALID, | 189 PRE_WRITE_INVALID, |
| 190 POST_WRITE_INVALID | 190 POST_WRITE_INVALID |
| 191 }; | 191 }; |
| 192 | 192 |
| 193 class TestCopyOrMoveFileValidatorFactory | 193 class TestCopyOrMoveFileValidatorFactory |
| 194 : public fileapi::CopyOrMoveFileValidatorFactory { | 194 : public storage::CopyOrMoveFileValidatorFactory { |
| 195 public: | 195 public: |
| 196 // A factory that creates validators that accept everything or nothing. | 196 // A factory that creates validators that accept everything or nothing. |
| 197 // TODO(gbillock): switch args to enum or something | 197 // TODO(gbillock): switch args to enum or something |
| 198 explicit TestCopyOrMoveFileValidatorFactory(Validity validity) | 198 explicit TestCopyOrMoveFileValidatorFactory(Validity validity) |
| 199 : validity_(validity) {} | 199 : validity_(validity) {} |
| 200 virtual ~TestCopyOrMoveFileValidatorFactory() {} | 200 virtual ~TestCopyOrMoveFileValidatorFactory() {} |
| 201 | 201 |
| 202 virtual fileapi::CopyOrMoveFileValidator* CreateCopyOrMoveFileValidator( | 202 virtual storage::CopyOrMoveFileValidator* CreateCopyOrMoveFileValidator( |
| 203 const FileSystemURL& /*src_url*/, | 203 const FileSystemURL& /*src_url*/, |
| 204 const base::FilePath& /*platform_path*/) OVERRIDE { | 204 const base::FilePath& /*platform_path*/) OVERRIDE { |
| 205 return new TestCopyOrMoveFileValidator(validity_); | 205 return new TestCopyOrMoveFileValidator(validity_); |
| 206 } | 206 } |
| 207 | 207 |
| 208 private: | 208 private: |
| 209 class TestCopyOrMoveFileValidator : public CopyOrMoveFileValidator { | 209 class TestCopyOrMoveFileValidator : public CopyOrMoveFileValidator { |
| 210 public: | 210 public: |
| 211 explicit TestCopyOrMoveFileValidator(Validity validity) | 211 explicit TestCopyOrMoveFileValidator(Validity validity) |
| 212 : result_(validity == VALID || validity == POST_WRITE_INVALID ? | 212 : result_(validity == VALID || validity == POST_WRITE_INVALID ? |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 helper.SetUp(); | 320 helper.SetUp(); |
| 321 scoped_ptr<CopyOrMoveFileValidatorFactory> factory( | 321 scoped_ptr<CopyOrMoveFileValidatorFactory> factory( |
| 322 new TestCopyOrMoveFileValidatorFactory(POST_WRITE_INVALID)); | 322 new TestCopyOrMoveFileValidatorFactory(POST_WRITE_INVALID)); |
| 323 helper.SetMediaCopyOrMoveFileValidatorFactory(factory.Pass()); | 323 helper.SetMediaCopyOrMoveFileValidatorFactory(factory.Pass()); |
| 324 | 324 |
| 325 helper.CopyTest(base::File::FILE_ERROR_SECURITY); | 325 helper.CopyTest(base::File::FILE_ERROR_SECURITY); |
| 326 helper.MoveTest(base::File::FILE_ERROR_SECURITY); | 326 helper.MoveTest(base::File::FILE_ERROR_SECURITY); |
| 327 } | 327 } |
| 328 | 328 |
| 329 } // namespace content | 329 } // namespace content |
| OLD | NEW |