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 "base/basictypes.h" |
| 6 #include "base/bind.h" |
| 7 #include "base/file_util.h" |
| 8 #include "base/files/file_path.h" |
| 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/message_loop.h" |
| 11 #include "chrome/browser/media_galleries/fileapi/media_file_system_mount_point_p
rovider.h" |
| 12 #include "chrome/test/base/in_process_browser_test.h" |
| 13 #include "content/public/test/browser_test.h" |
| 14 #include "content/public/test/test_utils.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "webkit/browser/fileapi/copy_or_move_file_validator.h" |
| 17 #include "webkit/browser/fileapi/file_system_context.h" |
| 18 #include "webkit/browser/fileapi/file_system_mount_point_provider.h" |
| 19 #include "webkit/browser/fileapi/file_system_operation.h" |
| 20 #include "webkit/browser/fileapi/file_system_url.h" |
| 21 #include "webkit/browser/fileapi/isolated_context.h" |
| 22 #include "webkit/browser/fileapi/mock_file_system_context.h" |
| 23 #include "webkit/browser/fileapi/test_mount_point_provider.h" |
| 24 #include "webkit/common/fileapi/file_system_types.h" |
| 25 |
| 26 namespace { |
| 27 |
| 28 const char kOrigin[] = "http://foo"; |
| 29 |
| 30 const char kValidImage[] = "RIFF0\0\0\0WEBPVP8 $\0\0\0\xB2\x02\0\x9D\x01\x2A" |
| 31 "\x01\0\x01\0\x2F\x9D\xCE\xE7s\xA8((((\x01\x9CK(\0" |
| 32 "\x05\xCE\xB3l\0\0\xFE\xD8\x80\0\0"; |
| 33 |
| 34 const char kInvalidImage[] = "Not an image"; |
| 35 |
| 36 const int64 kNoFileSize = -1; |
| 37 |
| 38 void HandleCheckFileResult(int64 expected_size, |
| 39 const base::Callback<void(bool success)>& callback, |
| 40 base::PlatformFileError result, |
| 41 const base::PlatformFileInfo& file_info, |
| 42 const base::FilePath& /*platform_path*/) { |
| 43 if (result == base::PLATFORM_FILE_OK) { |
| 44 if (!file_info.is_directory && expected_size != kNoFileSize && |
| 45 file_info.size == expected_size) { |
| 46 callback.Run(true); |
| 47 return; |
| 48 } |
| 49 } else { |
| 50 if (expected_size == kNoFileSize) { |
| 51 callback.Run(true); |
| 52 return; |
| 53 } |
| 54 } |
| 55 callback.Run(false); |
| 56 } |
| 57 |
| 58 } // namespace |
| 59 |
| 60 namespace chrome { |
| 61 |
| 62 class MediaFileValidatorTest : public InProcessBrowserTest { |
| 63 public: |
| 64 MediaFileValidatorTest() : test_file_size_(0) {} |
| 65 |
| 66 virtual ~MediaFileValidatorTest() {} |
| 67 |
| 68 // Write |content| into |filename| in a test file system and try to move |
| 69 // it into a media file system. The result is compared to |expected_result|. |
| 70 void MoveTest(const std::string& filename, const std::string& content, |
| 71 bool expected_result) { |
| 72 content::BrowserThread::PostTask( |
| 73 content::BrowserThread::FILE, |
| 74 FROM_HERE, |
| 75 base::Bind(&MediaFileValidatorTest::SetupOnFileThread, |
| 76 base::Unretained(this), filename, content, expected_result)); |
| 77 loop_runner_ = new content::MessageLoopRunner; |
| 78 loop_runner_->Run(); |
| 79 } |
| 80 |
| 81 private: |
| 82 // Create the test files, filesystem objects, etc. |
| 83 void SetupOnFileThread(const std::string& filename, |
| 84 const std::string& content, |
| 85 bool expected_result) { |
| 86 ASSERT_TRUE(base_dir_.CreateUniqueTempDir()); |
| 87 base::FilePath base = base_dir_.path(); |
| 88 base::FilePath src_path = base.AppendASCII("src_fs"); |
| 89 ASSERT_TRUE(file_util::CreateDirectory(src_path)); |
| 90 |
| 91 ScopedVector<fileapi::FileSystemMountPointProvider> additional_providers; |
| 92 additional_providers.push_back(new fileapi::TestMountPointProvider( |
| 93 base::MessageLoopProxy::current(), src_path)); |
| 94 additional_providers.push_back( |
| 95 new MediaFileSystemMountPointProvider(base)); |
| 96 file_system_context_ = |
| 97 fileapi::CreateFileSystemContextWithAdditionalProvidersForTesting( |
| 98 NULL, additional_providers.Pass(), base); |
| 99 |
| 100 move_src_ = file_system_context_->CreateCrackedFileSystemURL( |
| 101 GURL(kOrigin), fileapi::kFileSystemTypeTest, |
| 102 base::FilePath::FromUTF8Unsafe(filename)); |
| 103 |
| 104 test_file_size_ = content.size(); |
| 105 base::FilePath test_file = src_path.AppendASCII(filename); |
| 106 ASSERT_EQ(test_file_size_, |
| 107 file_util::WriteFile(test_file, content.data(), test_file_size_)); |
| 108 |
| 109 base::FilePath dest_path = base.AppendASCII("dest_fs"); |
| 110 ASSERT_TRUE(file_util::CreateDirectory(dest_path)); |
| 111 std::string dest_fsid = |
| 112 fileapi::IsolatedContext::GetInstance()->RegisterFileSystemForPath( |
| 113 fileapi::kFileSystemTypeNativeMedia, dest_path, NULL); |
| 114 |
| 115 size_t extension_index = filename.find_last_of("."); |
| 116 ASSERT_NE(std::string::npos, extension_index); |
| 117 std::string extension = filename.substr(extension_index); |
| 118 std::string dest_root_fs_url = fileapi::GetIsolatedFileSystemRootURIString( |
| 119 GURL(kOrigin), dest_fsid, "dest_fs/"); |
| 120 move_dest_ = file_system_context_->CrackURL(GURL( |
| 121 dest_root_fs_url + "move_dest" + extension)); |
| 122 |
| 123 content::BrowserThread::PostTask( |
| 124 content::BrowserThread::IO, |
| 125 FROM_HERE, |
| 126 base::Bind(&MediaFileValidatorTest::CheckFiles, |
| 127 base::Unretained(this), true, |
| 128 base::Bind(&MediaFileValidatorTest::OnTestFilesReady, |
| 129 base::Unretained(this), expected_result))); |
| 130 } |
| 131 |
| 132 // Check that exactly one of |move_src_| and |move_dest_| exists. |
| 133 // |src_expected| indicates which one should exist. When complete, |
| 134 // |callback| is called with success/failure. |
| 135 void CheckFiles(bool src_expected, |
| 136 const base::Callback<void(bool success)>& callback) { |
| 137 CheckFile(move_src_, src_expected ? test_file_size_ : kNoFileSize, |
| 138 base::Bind(&MediaFileValidatorTest::OnCheckFilesFirstResult, |
| 139 base::Unretained(this), !src_expected, callback)); |
| 140 } |
| 141 |
| 142 // Helper that checks a file has the |expected_size|, which may be |
| 143 // |kNoFileSize| if the file should not exist. |callback| is called |
| 144 // with success/failure. |
| 145 void CheckFile(fileapi::FileSystemURL url, |
| 146 int64 expected_size, |
| 147 const base::Callback<void(bool success)>& callback) { |
| 148 CreateFSOp(url)->GetMetadata(url, |
| 149 base::Bind(&HandleCheckFileResult, |
| 150 expected_size, callback)); |
| 151 } |
| 152 |
| 153 // Helper that checks the result of |move_src_| lookup and then checks |
| 154 // |move_dest_| if all is as expected. |
| 155 void OnCheckFilesFirstResult(bool dest_expected, |
| 156 const base::Callback<void(bool)>& callback, |
| 157 bool src_result) { |
| 158 EXPECT_TRUE(src_result); |
| 159 if (!src_result) { |
| 160 callback.Run(false); |
| 161 return; |
| 162 } |
| 163 CheckFile(move_dest_, dest_expected ? test_file_size_ : kNoFileSize, |
| 164 callback); |
| 165 } |
| 166 |
| 167 // Assert |test_files_ready| and then do the actual test of moving |
| 168 // |move_src_| to |move_dest_|. |
| 169 void OnTestFilesReady(bool expected_result, bool test_files_ready) { |
| 170 ASSERT_TRUE(test_files_ready); |
| 171 CreateFSOp(move_dest_)->Move( |
| 172 move_src_, move_dest_, |
| 173 base::Bind(&MediaFileValidatorTest::OnMoveResult, |
| 174 base::Unretained(this), expected_result)); |
| 175 } |
| 176 |
| 177 // Check that the move succeeded/failed based on expectation and then |
| 178 // check that the right file exists. |
| 179 void OnMoveResult(bool expected_result, base::PlatformFileError result) { |
| 180 if (expected_result) |
| 181 EXPECT_EQ(base::PLATFORM_FILE_OK, result); |
| 182 else |
| 183 EXPECT_EQ(base::PLATFORM_FILE_ERROR_SECURITY, result); |
| 184 CheckFiles(!expected_result, |
| 185 base::Bind(&MediaFileValidatorTest::OnTestFilesCheckResult, |
| 186 base::Unretained(this))); |
| 187 } |
| 188 |
| 189 // Check that the correct test file exists and then post the result back |
| 190 // to the UI thread. |
| 191 void OnTestFilesCheckResult(bool result) { |
| 192 EXPECT_TRUE(result); |
| 193 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 194 loop_runner_->QuitClosure()); |
| 195 } |
| 196 |
| 197 fileapi::FileSystemOperation* CreateFSOp(const fileapi::FileSystemURL& url) { |
| 198 return file_system_context_->CreateFileSystemOperation(url, NULL); |
| 199 } |
| 200 |
| 201 base::ScopedTempDir base_dir_; |
| 202 |
| 203 scoped_refptr<fileapi::FileSystemContext> file_system_context_; |
| 204 |
| 205 int test_file_size_; |
| 206 |
| 207 fileapi::FileSystemURL move_src_; |
| 208 fileapi::FileSystemURL move_dest_; |
| 209 |
| 210 scoped_refptr<content::MessageLoopRunner> loop_runner_; |
| 211 |
| 212 DISALLOW_COPY_AND_ASSIGN(MediaFileValidatorTest); |
| 213 }; |
| 214 |
| 215 IN_PROC_BROWSER_TEST_F(MediaFileValidatorTest, ValidImage) { |
| 216 MoveTest("a.webp", std::string(kValidImage, arraysize(kValidImage)), true); |
| 217 } |
| 218 |
| 219 IN_PROC_BROWSER_TEST_F(MediaFileValidatorTest, InvalidImage) { |
| 220 MoveTest("a.webp", std::string(kInvalidImage, arraysize(kInvalidImage)), |
| 221 false); |
| 222 } |
| 223 |
| 224 IN_PROC_BROWSER_TEST_F(MediaFileValidatorTest, UnsupportedExtension) { |
| 225 MoveTest("a.txt", std::string(kValidImage, arraysize(kValidImage)), false); |
| 226 } |
| 227 |
| 228 } // namespace chrome |
OLD | NEW |