Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / media_galleries / fileapi / media_file_validator_browsertest.cc
blob9b6047a2d9dddd30346e3db3cb0d7d9d92d977b8
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.
5 #include "base/basictypes.h"
6 #include "base/bind.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/location.h"
11 #include "base/path_service.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h"
15 #include "chrome/test/base/in_process_browser_test.h"
16 #include "content/public/test/browser_test.h"
17 #include "content/public/test/test_file_system_backend.h"
18 #include "content/public/test/test_file_system_context.h"
19 #include "content/public/test/test_utils.h"
20 #include "storage/browser/fileapi/copy_or_move_file_validator.h"
21 #include "storage/browser/fileapi/file_system_backend.h"
22 #include "storage/browser/fileapi/file_system_context.h"
23 #include "storage/browser/fileapi/file_system_operation_runner.h"
24 #include "storage/browser/fileapi/file_system_url.h"
25 #include "storage/browser/fileapi/isolated_context.h"
26 #include "storage/common/fileapi/file_system_types.h"
27 #include "testing/gtest/include/gtest/gtest.h"
29 namespace {
31 const char kOrigin[] = "http://foo";
33 const char kValidImage[] = "RIFF0\0\0\0WEBPVP8 $\0\0\0\xB2\x02\0\x9D\x01\x2A"
34 "\x01\0\x01\0\x2F\x9D\xCE\xE7s\xA8((((\x01\x9CK(\0"
35 "\x05\xCE\xB3l\0\0\xFE\xD8\x80\0\0";
37 const char kInvalidMediaFile[] = "Not a media file";
39 const int64 kNoFileSize = -1;
41 void HandleCheckFileResult(int64 expected_size,
42 const base::Callback<void(bool success)>& callback,
43 base::File::Error result,
44 const base::File::Info& file_info) {
45 if (result == base::File::FILE_OK) {
46 if (!file_info.is_directory && expected_size != kNoFileSize &&
47 file_info.size == expected_size) {
48 callback.Run(true);
49 return;
51 } else {
52 if (expected_size == kNoFileSize) {
53 callback.Run(true);
54 return;
57 callback.Run(false);
60 base::FilePath GetMediaTestDir() {
61 base::FilePath test_file;
62 if (!PathService::Get(base::DIR_SOURCE_ROOT, &test_file))
63 return base::FilePath();
64 return test_file.AppendASCII("media").AppendASCII("test").AppendASCII("data");
67 } // namespace
69 class MediaFileValidatorTest : public InProcessBrowserTest {
70 public:
71 MediaFileValidatorTest() : test_file_size_(0) {}
73 ~MediaFileValidatorTest() override {}
75 // Write |content| into |filename| in a test file system and try to move
76 // it into a media file system. The result is compared to |expected_result|.
77 void MoveTest(const std::string& filename, const std::string& content,
78 bool expected_result) {
79 content::BrowserThread::PostTask(
80 content::BrowserThread::FILE,
81 FROM_HERE,
82 base::Bind(&MediaFileValidatorTest::SetupOnFileThread,
83 base::Unretained(this), filename, content, expected_result));
84 loop_runner_ = new content::MessageLoopRunner;
85 loop_runner_->Run();
88 // Write |source| into |filename| in a test file system and try to move it
89 // into a media file system. The result is compared to |expected_result|.
90 void MoveTestFromFile(const std::string& filename,
91 const base::FilePath& source, bool expected_result) {
92 content::BrowserThread::PostTask(
93 content::BrowserThread::FILE,
94 FROM_HERE,
95 base::Bind(&MediaFileValidatorTest::SetupFromFileOnFileThread,
96 base::Unretained(this), filename, source, expected_result));
97 loop_runner_ = new content::MessageLoopRunner;
98 loop_runner_->Run();
101 private:
102 // Create the test files, filesystem objects, etc.
103 void SetupOnFileThread(const std::string& filename,
104 const std::string& content,
105 bool expected_result) {
106 ASSERT_TRUE(base_dir_.CreateUniqueTempDir());
107 base::FilePath base = base_dir_.path();
108 base::FilePath src_path = base.AppendASCII("src_fs");
109 ASSERT_TRUE(base::CreateDirectory(src_path));
111 ScopedVector<storage::FileSystemBackend> additional_providers;
112 additional_providers.push_back(new content::TestFileSystemBackend(
113 base::ThreadTaskRunnerHandle::Get().get(), src_path));
114 additional_providers.push_back(new MediaFileSystemBackend(
115 base, base::ThreadTaskRunnerHandle::Get().get()));
116 file_system_context_ =
117 content::CreateFileSystemContextWithAdditionalProvidersForTesting(
118 NULL, additional_providers.Pass(), base);
120 move_src_ = file_system_context_->CreateCrackedFileSystemURL(
121 GURL(kOrigin),
122 storage::kFileSystemTypeTest,
123 base::FilePath::FromUTF8Unsafe(filename));
125 test_file_size_ = content.size();
126 base::FilePath test_file = src_path.AppendASCII(filename);
127 ASSERT_EQ(test_file_size_,
128 base::WriteFile(test_file, content.data(), test_file_size_));
130 base::FilePath dest_path = base.AppendASCII("dest_fs");
131 ASSERT_TRUE(base::CreateDirectory(dest_path));
132 std::string dest_fsid =
133 storage::IsolatedContext::GetInstance()->RegisterFileSystemForPath(
134 storage::kFileSystemTypeNativeMedia,
135 std::string(),
136 dest_path,
137 NULL);
139 size_t extension_index = filename.find_last_of(".");
140 ASSERT_NE(std::string::npos, extension_index);
141 std::string extension = filename.substr(extension_index);
142 std::string dest_root_fs_url = storage::GetIsolatedFileSystemRootURIString(
143 GURL(kOrigin), dest_fsid, "dest_fs/");
144 move_dest_ = file_system_context_->CrackURL(GURL(
145 dest_root_fs_url + "move_dest" + extension));
147 content::BrowserThread::PostTask(
148 content::BrowserThread::IO,
149 FROM_HERE,
150 base::Bind(&MediaFileValidatorTest::CheckFiles,
151 base::Unretained(this), true,
152 base::Bind(&MediaFileValidatorTest::OnTestFilesReady,
153 base::Unretained(this), expected_result)));
156 void SetupFromFileOnFileThread(const std::string& filename,
157 const base::FilePath& source,
158 bool expected_result) {
159 std::string content;
160 ASSERT_TRUE(base::ReadFileToString(source, &content));
161 SetupOnFileThread(filename, content, expected_result);
164 // Check that exactly one of |move_src_| and |move_dest_| exists.
165 // |src_expected| indicates which one should exist. When complete,
166 // |callback| is called with success/failure.
167 void CheckFiles(bool src_expected,
168 const base::Callback<void(bool success)>& callback) {
169 CheckFile(move_src_, src_expected ? test_file_size_ : kNoFileSize,
170 base::Bind(&MediaFileValidatorTest::OnCheckFilesFirstResult,
171 base::Unretained(this), !src_expected, callback));
174 // Helper that checks a file has the |expected_size|, which may be
175 // |kNoFileSize| if the file should not exist. |callback| is called
176 // with success/failure.
177 void CheckFile(storage::FileSystemURL url,
178 int64 expected_size,
179 const base::Callback<void(bool success)>& callback) {
180 operation_runner()->GetMetadata(url,
181 base::Bind(&HandleCheckFileResult,
182 expected_size, callback));
185 // Helper that checks the result of |move_src_| lookup and then checks
186 // |move_dest_| if all is as expected.
187 void OnCheckFilesFirstResult(bool dest_expected,
188 const base::Callback<void(bool)>& callback,
189 bool src_result) {
190 EXPECT_TRUE(src_result);
191 if (!src_result) {
192 callback.Run(false);
193 return;
195 CheckFile(move_dest_, dest_expected ? test_file_size_ : kNoFileSize,
196 callback);
199 // Assert |test_files_ready| and then do the actual test of moving
200 // |move_src_| to |move_dest_|.
201 void OnTestFilesReady(bool expected_result, bool test_files_ready) {
202 ASSERT_TRUE(test_files_ready);
203 operation_runner()->Move(move_src_,
204 move_dest_,
205 storage::FileSystemOperation::OPTION_NONE,
206 base::Bind(&MediaFileValidatorTest::OnMoveResult,
207 base::Unretained(this),
208 expected_result));
211 // Check that the move succeeded/failed based on expectation and then
212 // check that the right file exists.
213 void OnMoveResult(bool expected_result, base::File::Error result) {
214 if (expected_result)
215 EXPECT_EQ(base::File::FILE_OK, result);
216 else
217 EXPECT_EQ(base::File::FILE_ERROR_SECURITY, result);
218 CheckFiles(!expected_result,
219 base::Bind(&MediaFileValidatorTest::OnTestFilesCheckResult,
220 base::Unretained(this)));
223 // Check that the correct test file exists and then post the result back
224 // to the UI thread.
225 void OnTestFilesCheckResult(bool result) {
226 EXPECT_TRUE(result);
227 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
228 loop_runner_->QuitClosure());
231 storage::FileSystemOperationRunner* operation_runner() {
232 return file_system_context_->operation_runner();
235 base::ScopedTempDir base_dir_;
237 scoped_refptr<storage::FileSystemContext> file_system_context_;
239 int test_file_size_;
241 storage::FileSystemURL move_src_;
242 storage::FileSystemURL move_dest_;
244 scoped_refptr<content::MessageLoopRunner> loop_runner_;
246 DISALLOW_COPY_AND_ASSIGN(MediaFileValidatorTest);
249 IN_PROC_BROWSER_TEST_F(MediaFileValidatorTest, UnsupportedExtension) {
250 MoveTest("a.txt", std::string(kValidImage, arraysize(kValidImage)), false);
253 IN_PROC_BROWSER_TEST_F(MediaFileValidatorTest, ValidImage) {
254 MoveTest("a.webp", std::string(kValidImage, arraysize(kValidImage)), true);
257 IN_PROC_BROWSER_TEST_F(MediaFileValidatorTest, InvalidImage) {
258 MoveTest("a.webp", std::string(kInvalidMediaFile,
259 arraysize(kInvalidMediaFile)), false);
262 IN_PROC_BROWSER_TEST_F(MediaFileValidatorTest, InvalidAudio) {
263 MoveTest("a.ogg", std::string(kInvalidMediaFile,
264 arraysize(kInvalidMediaFile)), false);
267 IN_PROC_BROWSER_TEST_F(MediaFileValidatorTest, ValidAudio) {
268 base::FilePath test_file = GetMediaTestDir();
269 ASSERT_FALSE(test_file.empty());
270 test_file = test_file.AppendASCII("sfx.ogg");
271 MoveTestFromFile("sfx.ogg", test_file, true);
274 IN_PROC_BROWSER_TEST_F(MediaFileValidatorTest, InvalidVideo) {
275 base::FilePath test_file = GetMediaTestDir();
276 ASSERT_FALSE(test_file.empty());
277 test_file = test_file.AppendASCII("no_streams.webm");
278 MoveTestFromFile("no_streams.webm", test_file, false);
281 IN_PROC_BROWSER_TEST_F(MediaFileValidatorTest, ValidVideo) {
282 base::FilePath test_file = GetMediaTestDir();
283 ASSERT_FALSE(test_file.empty());
284 test_file = test_file.AppendASCII("bear-320x240-multitrack.webm");
285 MoveTestFromFile("multitrack.webm", test_file, true);