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

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

Issue 145303002: Convert Media Galleries to use base::File (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 6 years, 11 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 | Annotate | Revision Log
OLDNEW
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/test_file_system_backend.h" 10 #include "content/public/test/test_file_system_backend.h"
(...skipping 18 matching lines...) Expand all
29 29
30 namespace content { 30 namespace content {
31 31
32 namespace { 32 namespace {
33 33
34 const FileSystemType kNoValidatorType = fileapi::kFileSystemTypeTemporary; 34 const FileSystemType kNoValidatorType = fileapi::kFileSystemTypeTemporary;
35 const FileSystemType kWithValidatorType = fileapi::kFileSystemTypeTest; 35 const FileSystemType kWithValidatorType = fileapi::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::PlatformFileError error) { 39 base::File::Error error) {
40 ASSERT_EQ(base::PLATFORM_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( 45 CopyOrMoveFileValidatorTestHelper(
46 const GURL& origin, 46 const GURL& origin,
47 FileSystemType src_type, 47 FileSystemType src_type,
48 FileSystemType dest_type) 48 FileSystemType dest_type)
49 : origin_(origin), 49 : origin_(origin),
50 src_type_(src_type), 50 src_type_(src_type),
(...skipping 17 matching lines...) Expand all
68 set_require_copy_or_move_validator(true); 68 set_require_copy_or_move_validator(true);
69 69
70 // Sets up source. 70 // Sets up source.
71 fileapi::FileSystemBackend* src_file_system_backend = 71 fileapi::FileSystemBackend* src_file_system_backend =
72 file_system_context_->GetFileSystemBackend(src_type_); 72 file_system_context_->GetFileSystemBackend(src_type_);
73 src_file_system_backend->OpenFileSystem( 73 src_file_system_backend->OpenFileSystem(
74 origin_, src_type_, 74 origin_, src_type_,
75 fileapi::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, 75 fileapi::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
76 base::Bind(&ExpectOk)); 76 base::Bind(&ExpectOk));
77 base::RunLoop().RunUntilIdle(); 77 base::RunLoop().RunUntilIdle();
78 ASSERT_EQ(base::PLATFORM_FILE_OK, CreateDirectory(SourceURL(""))); 78 ASSERT_EQ(base::File::FILE_OK, CreateDirectory(SourceURL("")));
79 79
80 // Sets up dest. 80 // Sets up dest.
81 DCHECK_EQ(kWithValidatorType, dest_type_); 81 DCHECK_EQ(kWithValidatorType, dest_type_);
82 ASSERT_EQ(base::PLATFORM_FILE_OK, CreateDirectory(DestURL(""))); 82 ASSERT_EQ(base::File::FILE_OK, CreateDirectory(DestURL("")));
83 83
84 copy_src_ = SourceURL("copy_src.jpg"); 84 copy_src_ = SourceURL("copy_src.jpg");
85 move_src_ = SourceURL("move_src.jpg"); 85 move_src_ = SourceURL("move_src.jpg");
86 copy_dest_ = DestURL("copy_dest.jpg"); 86 copy_dest_ = DestURL("copy_dest.jpg");
87 move_dest_ = DestURL("move_dest.jpg"); 87 move_dest_ = DestURL("move_dest.jpg");
88 88
89 ASSERT_EQ(base::PLATFORM_FILE_OK, CreateFile(copy_src_, 10)); 89 ASSERT_EQ(base::File::FILE_OK, CreateFile(copy_src_, 10));
90 ASSERT_EQ(base::PLATFORM_FILE_OK, CreateFile(move_src_, 10)); 90 ASSERT_EQ(base::File::FILE_OK, CreateFile(move_src_, 10));
91 91
92 ASSERT_TRUE(FileExists(copy_src_, 10)); 92 ASSERT_TRUE(FileExists(copy_src_, 10));
93 ASSERT_TRUE(FileExists(move_src_, 10)); 93 ASSERT_TRUE(FileExists(move_src_, 10));
94 ASSERT_FALSE(FileExists(copy_dest_, 10)); 94 ASSERT_FALSE(FileExists(copy_dest_, 10));
95 ASSERT_FALSE(FileExists(move_dest_, 10)); 95 ASSERT_FALSE(FileExists(move_dest_, 10));
96 } 96 }
97 97
98 void SetMediaCopyOrMoveFileValidatorFactory( 98 void SetMediaCopyOrMoveFileValidatorFactory(
99 scoped_ptr<fileapi::CopyOrMoveFileValidatorFactory> factory) { 99 scoped_ptr<fileapi::CopyOrMoveFileValidatorFactory> factory) {
100 TestFileSystemBackend* backend = static_cast<TestFileSystemBackend*>( 100 TestFileSystemBackend* backend = static_cast<TestFileSystemBackend*>(
101 file_system_context_->GetFileSystemBackend(kWithValidatorType)); 101 file_system_context_->GetFileSystemBackend(kWithValidatorType));
102 backend->InitializeCopyOrMoveFileValidatorFactory(factory.Pass()); 102 backend->InitializeCopyOrMoveFileValidatorFactory(factory.Pass());
103 } 103 }
104 104
105 void CopyTest(base::PlatformFileError expected) { 105 void CopyTest(base::File::Error expected) {
106 ASSERT_TRUE(FileExists(copy_src_, 10)); 106 ASSERT_TRUE(FileExists(copy_src_, 10));
107 ASSERT_FALSE(FileExists(copy_dest_, 10)); 107 ASSERT_FALSE(FileExists(copy_dest_, 10));
108 108
109 EXPECT_EQ(expected, 109 EXPECT_EQ(expected,
110 AsyncFileTestHelper::Copy( 110 AsyncFileTestHelper::Copy(
111 file_system_context_.get(), copy_src_, copy_dest_)); 111 file_system_context_.get(), copy_src_, copy_dest_));
112 112
113 EXPECT_TRUE(FileExists(copy_src_, 10)); 113 EXPECT_TRUE(FileExists(copy_src_, 10));
114 if (expected == base::PLATFORM_FILE_OK) 114 if (expected == base::File::FILE_OK)
115 EXPECT_TRUE(FileExists(copy_dest_, 10)); 115 EXPECT_TRUE(FileExists(copy_dest_, 10));
116 else 116 else
117 EXPECT_FALSE(FileExists(copy_dest_, 10)); 117 EXPECT_FALSE(FileExists(copy_dest_, 10));
118 }; 118 };
119 119
120 void MoveTest(base::PlatformFileError expected) { 120 void MoveTest(base::File::Error expected) {
121 ASSERT_TRUE(FileExists(move_src_, 10)); 121 ASSERT_TRUE(FileExists(move_src_, 10));
122 ASSERT_FALSE(FileExists(move_dest_, 10)); 122 ASSERT_FALSE(FileExists(move_dest_, 10));
123 123
124 EXPECT_EQ(expected, 124 EXPECT_EQ(expected,
125 AsyncFileTestHelper::Move( 125 AsyncFileTestHelper::Move(
126 file_system_context_.get(), move_src_, move_dest_)); 126 file_system_context_.get(), move_src_, move_dest_));
127 127
128 if (expected == base::PLATFORM_FILE_OK) { 128 if (expected == base::File::FILE_OK) {
129 EXPECT_FALSE(FileExists(move_src_, 10)); 129 EXPECT_FALSE(FileExists(move_src_, 10));
130 EXPECT_TRUE(FileExists(move_dest_, 10)); 130 EXPECT_TRUE(FileExists(move_dest_, 10));
131 } else { 131 } else {
132 EXPECT_TRUE(FileExists(move_src_, 10)); 132 EXPECT_TRUE(FileExists(move_src_, 10));
133 EXPECT_FALSE(FileExists(move_dest_, 10)); 133 EXPECT_FALSE(FileExists(move_dest_, 10));
134 } 134 }
135 }; 135 };
136 136
137 private: 137 private:
138 FileSystemURL SourceURL(const std::string& path) { 138 FileSystemURL SourceURL(const std::string& path) {
139 return file_system_context_->CreateCrackedFileSystemURL( 139 return file_system_context_->CreateCrackedFileSystemURL(
140 origin_, src_type_, 140 origin_, src_type_,
141 base::FilePath().AppendASCII("src").AppendASCII(path)); 141 base::FilePath().AppendASCII("src").AppendASCII(path));
142 } 142 }
143 143
144 FileSystemURL DestURL(const std::string& path) { 144 FileSystemURL DestURL(const std::string& path) {
145 return file_system_context_->CreateCrackedFileSystemURL( 145 return file_system_context_->CreateCrackedFileSystemURL(
146 origin_, dest_type_, 146 origin_, dest_type_,
147 base::FilePath().AppendASCII("dest").AppendASCII(path)); 147 base::FilePath().AppendASCII("dest").AppendASCII(path));
148 } 148 }
149 149
150 base::PlatformFileError CreateFile(const FileSystemURL& url, size_t size) { 150 base::File::Error CreateFile(const FileSystemURL& url, size_t size) {
151 base::PlatformFileError result = 151 base::File::Error result =
152 AsyncFileTestHelper::CreateFile(file_system_context_.get(), url); 152 AsyncFileTestHelper::CreateFile(file_system_context_.get(), url);
153 if (result != base::PLATFORM_FILE_OK) 153 if (result != base::File::FILE_OK)
154 return result; 154 return result;
155 return AsyncFileTestHelper::TruncateFile( 155 return AsyncFileTestHelper::TruncateFile(
156 file_system_context_.get(), url, size); 156 file_system_context_.get(), url, size);
157 } 157 }
158 158
159 base::PlatformFileError CreateDirectory(const FileSystemURL& url) { 159 base::File::Error CreateDirectory(const FileSystemURL& url) {
160 return AsyncFileTestHelper::CreateDirectory(file_system_context_.get(), 160 return AsyncFileTestHelper::CreateDirectory(file_system_context_.get(),
161 url); 161 url);
162 } 162 }
163 163
164 bool FileExists(const FileSystemURL& url, int64 expected_size) { 164 bool FileExists(const FileSystemURL& url, int64 expected_size) {
165 return AsyncFileTestHelper::FileExists( 165 return AsyncFileTestHelper::FileExists(
166 file_system_context_.get(), url, expected_size); 166 file_system_context_.get(), url, expected_size);
167 } 167 }
168 168
169 base::ScopedTempDir base_; 169 base::ScopedTempDir base_;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 virtual fileapi::CopyOrMoveFileValidator* CreateCopyOrMoveFileValidator( 205 virtual fileapi::CopyOrMoveFileValidator* CreateCopyOrMoveFileValidator(
206 const FileSystemURL& /*src_url*/, 206 const FileSystemURL& /*src_url*/,
207 const base::FilePath& /*platform_path*/) OVERRIDE { 207 const base::FilePath& /*platform_path*/) OVERRIDE {
208 return new TestCopyOrMoveFileValidator(validity_); 208 return new TestCopyOrMoveFileValidator(validity_);
209 } 209 }
210 210
211 private: 211 private:
212 class TestCopyOrMoveFileValidator : public CopyOrMoveFileValidator { 212 class TestCopyOrMoveFileValidator : public CopyOrMoveFileValidator {
213 public: 213 public:
214 explicit TestCopyOrMoveFileValidator(Validity validity) 214 explicit TestCopyOrMoveFileValidator(Validity validity)
215 : result_(validity == VALID || validity == POST_WRITE_INVALID 215 : result_(validity == VALID || validity == POST_WRITE_INVALID ?
216 ? base::PLATFORM_FILE_OK 216 base::File::FILE_OK :
217 : base::PLATFORM_FILE_ERROR_SECURITY), 217 base::File::FILE_ERROR_SECURITY),
218 write_result_(validity == VALID || validity == PRE_WRITE_INVALID 218 write_result_(validity == VALID || validity == PRE_WRITE_INVALID ?
219 ? base::PLATFORM_FILE_OK 219 base::File::FILE_OK :
220 : base::PLATFORM_FILE_ERROR_SECURITY) { 220 base::File::FILE_ERROR_SECURITY) {
221 } 221 }
222 virtual ~TestCopyOrMoveFileValidator() {} 222 virtual ~TestCopyOrMoveFileValidator() {}
223 223
224 virtual void StartPreWriteValidation( 224 virtual void StartPreWriteValidation(
225 const ResultCallback& result_callback) OVERRIDE { 225 const ResultCallback& result_callback) OVERRIDE {
226 // Post the result since a real validator must do work asynchronously. 226 // Post the result since a real validator must do work asynchronously.
227 base::MessageLoop::current()->PostTask( 227 base::MessageLoop::current()->PostTask(
228 FROM_HERE, base::Bind(result_callback, result_)); 228 FROM_HERE, base::Bind(result_callback, result_));
229 } 229 }
230 230
231 virtual void StartPostWriteValidation( 231 virtual void StartPostWriteValidation(
232 const base::FilePath& dest_platform_path, 232 const base::FilePath& dest_platform_path,
233 const ResultCallback& result_callback) OVERRIDE { 233 const ResultCallback& result_callback) OVERRIDE {
234 // Post the result since a real validator must do work asynchronously. 234 // Post the result since a real validator must do work asynchronously.
235 base::MessageLoop::current()->PostTask( 235 base::MessageLoop::current()->PostTask(
236 FROM_HERE, base::Bind(result_callback, write_result_)); 236 FROM_HERE, base::Bind(result_callback, write_result_));
237 } 237 }
238 238
239 private: 239 private:
240 base::PlatformFileError result_; 240 base::File::Error result_;
241 base::PlatformFileError write_result_; 241 base::File::Error write_result_;
242 242
243 DISALLOW_COPY_AND_ASSIGN(TestCopyOrMoveFileValidator); 243 DISALLOW_COPY_AND_ASSIGN(TestCopyOrMoveFileValidator);
244 }; 244 };
245 245
246 Validity validity_; 246 Validity validity_;
247 247
248 DISALLOW_COPY_AND_ASSIGN(TestCopyOrMoveFileValidatorFactory); 248 DISALLOW_COPY_AND_ASSIGN(TestCopyOrMoveFileValidatorFactory);
249 }; 249 };
250 250
251 } // namespace 251 } // namespace
252 252
253 TEST(CopyOrMoveFileValidatorTest, NoValidatorWithinSameFSType) { 253 TEST(CopyOrMoveFileValidatorTest, NoValidatorWithinSameFSType) {
254 // Within a file system type, validation is not expected, so it should 254 // Within a file system type, validation is not expected, so it should
255 // work for kWithValidatorType without a validator set. 255 // work for kWithValidatorType without a validator set.
256 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), 256 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"),
257 kWithValidatorType, 257 kWithValidatorType,
258 kWithValidatorType); 258 kWithValidatorType);
259 helper.SetUp(); 259 helper.SetUp();
260 helper.CopyTest(base::PLATFORM_FILE_OK); 260 helper.CopyTest(base::File::FILE_OK);
261 helper.MoveTest(base::PLATFORM_FILE_OK); 261 helper.MoveTest(base::File::FILE_OK);
262 } 262 }
263 263
264 TEST(CopyOrMoveFileValidatorTest, MissingValidator) { 264 TEST(CopyOrMoveFileValidatorTest, MissingValidator) {
265 // Copying or moving into a kWithValidatorType requires a file 265 // Copying or moving into a kWithValidatorType requires a file
266 // validator. An error is expected if copy is attempted without a validator. 266 // validator. An error is expected if copy is attempted without a validator.
267 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), 267 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"),
268 kNoValidatorType, 268 kNoValidatorType,
269 kWithValidatorType); 269 kWithValidatorType);
270 helper.SetUp(); 270 helper.SetUp();
271 helper.CopyTest(base::PLATFORM_FILE_ERROR_SECURITY); 271 helper.CopyTest(base::File::FILE_ERROR_SECURITY);
272 helper.MoveTest(base::PLATFORM_FILE_ERROR_SECURITY); 272 helper.MoveTest(base::File::FILE_ERROR_SECURITY);
273 } 273 }
274 274
275 TEST(CopyOrMoveFileValidatorTest, AcceptAll) { 275 TEST(CopyOrMoveFileValidatorTest, AcceptAll) {
276 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), 276 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"),
277 kNoValidatorType, 277 kNoValidatorType,
278 kWithValidatorType); 278 kWithValidatorType);
279 helper.SetUp(); 279 helper.SetUp();
280 scoped_ptr<CopyOrMoveFileValidatorFactory> factory( 280 scoped_ptr<CopyOrMoveFileValidatorFactory> factory(
281 new TestCopyOrMoveFileValidatorFactory(VALID)); 281 new TestCopyOrMoveFileValidatorFactory(VALID));
282 helper.SetMediaCopyOrMoveFileValidatorFactory(factory.Pass()); 282 helper.SetMediaCopyOrMoveFileValidatorFactory(factory.Pass());
283 283
284 helper.CopyTest(base::PLATFORM_FILE_OK); 284 helper.CopyTest(base::File::FILE_OK);
285 helper.MoveTest(base::PLATFORM_FILE_OK); 285 helper.MoveTest(base::File::FILE_OK);
286 } 286 }
287 287
288 TEST(CopyOrMoveFileValidatorTest, AcceptNone) { 288 TEST(CopyOrMoveFileValidatorTest, AcceptNone) {
289 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), 289 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"),
290 kNoValidatorType, 290 kNoValidatorType,
291 kWithValidatorType); 291 kWithValidatorType);
292 helper.SetUp(); 292 helper.SetUp();
293 scoped_ptr<CopyOrMoveFileValidatorFactory> factory( 293 scoped_ptr<CopyOrMoveFileValidatorFactory> factory(
294 new TestCopyOrMoveFileValidatorFactory(PRE_WRITE_INVALID)); 294 new TestCopyOrMoveFileValidatorFactory(PRE_WRITE_INVALID));
295 helper.SetMediaCopyOrMoveFileValidatorFactory(factory.Pass()); 295 helper.SetMediaCopyOrMoveFileValidatorFactory(factory.Pass());
296 296
297 helper.CopyTest(base::PLATFORM_FILE_ERROR_SECURITY); 297 helper.CopyTest(base::File::FILE_ERROR_SECURITY);
298 helper.MoveTest(base::PLATFORM_FILE_ERROR_SECURITY); 298 helper.MoveTest(base::File::FILE_ERROR_SECURITY);
299 } 299 }
300 300
301 TEST(CopyOrMoveFileValidatorTest, OverrideValidator) { 301 TEST(CopyOrMoveFileValidatorTest, OverrideValidator) {
302 // Once set, you can not override the validator. 302 // Once set, you can not override the validator.
303 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), 303 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"),
304 kNoValidatorType, 304 kNoValidatorType,
305 kWithValidatorType); 305 kWithValidatorType);
306 helper.SetUp(); 306 helper.SetUp();
307 scoped_ptr<CopyOrMoveFileValidatorFactory> reject_factory( 307 scoped_ptr<CopyOrMoveFileValidatorFactory> reject_factory(
308 new TestCopyOrMoveFileValidatorFactory(PRE_WRITE_INVALID)); 308 new TestCopyOrMoveFileValidatorFactory(PRE_WRITE_INVALID));
309 helper.SetMediaCopyOrMoveFileValidatorFactory(reject_factory.Pass()); 309 helper.SetMediaCopyOrMoveFileValidatorFactory(reject_factory.Pass());
310 310
311 scoped_ptr<CopyOrMoveFileValidatorFactory> accept_factory( 311 scoped_ptr<CopyOrMoveFileValidatorFactory> accept_factory(
312 new TestCopyOrMoveFileValidatorFactory(VALID)); 312 new TestCopyOrMoveFileValidatorFactory(VALID));
313 helper.SetMediaCopyOrMoveFileValidatorFactory(accept_factory.Pass()); 313 helper.SetMediaCopyOrMoveFileValidatorFactory(accept_factory.Pass());
314 314
315 helper.CopyTest(base::PLATFORM_FILE_ERROR_SECURITY); 315 helper.CopyTest(base::File::FILE_ERROR_SECURITY);
316 helper.MoveTest(base::PLATFORM_FILE_ERROR_SECURITY); 316 helper.MoveTest(base::File::FILE_ERROR_SECURITY);
317 } 317 }
318 318
319 TEST(CopyOrMoveFileValidatorTest, RejectPostWrite) { 319 TEST(CopyOrMoveFileValidatorTest, RejectPostWrite) {
320 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), 320 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"),
321 kNoValidatorType, 321 kNoValidatorType,
322 kWithValidatorType); 322 kWithValidatorType);
323 helper.SetUp(); 323 helper.SetUp();
324 scoped_ptr<CopyOrMoveFileValidatorFactory> factory( 324 scoped_ptr<CopyOrMoveFileValidatorFactory> factory(
325 new TestCopyOrMoveFileValidatorFactory(POST_WRITE_INVALID)); 325 new TestCopyOrMoveFileValidatorFactory(POST_WRITE_INVALID));
326 helper.SetMediaCopyOrMoveFileValidatorFactory(factory.Pass()); 326 helper.SetMediaCopyOrMoveFileValidatorFactory(factory.Pass());
327 327
328 helper.CopyTest(base::PLATFORM_FILE_ERROR_SECURITY); 328 helper.CopyTest(base::File::FILE_ERROR_SECURITY);
329 helper.MoveTest(base::PLATFORM_FILE_ERROR_SECURITY); 329 helper.MoveTest(base::File::FILE_ERROR_SECURITY);
330 } 330 }
331 331
332 } // namespace content 332 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698