Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(99)

Side by Side Diff: content/browser/fileapi/copy_or_move_file_validator_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698