OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 <algorithm> | |
6 #include <set> | |
7 #include <string> | |
8 | |
9 #include "base/file_path.h" | |
10 #include "base/file_util.h" | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "base/memory/scoped_callback_factory.h" | |
13 #include "base/message_loop.h" | |
14 #include "base/platform_file.h" | |
15 #include "base/scoped_temp_dir.h" | |
16 #include "base/sys_string_conversions.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 #include "webkit/fileapi/file_system_context.h" | |
19 #include "webkit/fileapi/file_system_operation_context.h" | |
20 #include "webkit/fileapi/file_system_path_manager.h" | |
21 #include "webkit/fileapi/file_system_test_helper.h" | |
22 #include "webkit/fileapi/file_system_usage_cache.h" | |
23 #include "webkit/fileapi/obfuscated_file_system_file_util.h" | |
24 #include "webkit/quota/mock_special_storage_policy.h" | |
25 #include "webkit/quota/quota_manager.h" | |
26 #include "webkit/quota/quota_types.h" | |
27 | |
28 using namespace fileapi; | |
29 | |
30 namespace { | |
31 | |
32 FilePath UTF8ToFilePath(const std::string& str) { | |
33 FilePath::StringType result; | |
34 #if defined(OS_POSIX) | |
35 result = str; | |
36 #elif defined(OS_WIN) | |
37 result = base::SysUTF8ToWide(str); | |
38 #endif | |
39 return FilePath(result); | |
40 } | |
41 | |
42 bool FileExists(const FilePath& path) { | |
43 return file_util::PathExists(path) && !file_util::DirectoryExists(path); | |
44 } | |
45 | |
46 int64 GetSize(const FilePath& path) { | |
47 int64 size; | |
48 EXPECT_TRUE(file_util::GetFileSize(path, &size)); | |
49 return size; | |
50 } | |
51 | |
52 // After a move, the dest exists and the source doesn't. | |
53 // After a copy, both source and dest exist. | |
54 struct CopyMoveTestCaseRecord { | |
55 bool is_copy_not_move; | |
56 const char source_path[64]; | |
57 const char dest_path[64]; | |
58 bool cause_overwrite; | |
59 }; | |
60 | |
61 const CopyMoveTestCaseRecord kCopyMoveTestCases[] = { | |
62 // This is the combinatoric set of: | |
63 // rename vs. same-name | |
64 // different directory vs. same directory | |
65 // overwrite vs. no-overwrite | |
66 // copy vs. move | |
67 // We can never be called with source and destination paths identical, so | |
68 // those cases are omitted. | |
69 {true, "dir0/file0", "dir0/file1", false}, | |
70 {false, "dir0/file0", "dir0/file1", false}, | |
71 {true, "dir0/file0", "dir0/file1", true}, | |
72 {false, "dir0/file0", "dir0/file1", true}, | |
73 | |
74 {true, "dir0/file0", "dir1/file0", false}, | |
75 {false, "dir0/file0", "dir1/file0", false}, | |
76 {true, "dir0/file0", "dir1/file0", true}, | |
77 {false, "dir0/file0", "dir1/file0", true}, | |
78 {true, "dir0/file0", "dir1/file1", false}, | |
79 {false, "dir0/file0", "dir1/file1", false}, | |
80 {true, "dir0/file0", "dir1/file1", true}, | |
81 {false, "dir0/file0", "dir1/file1", true}, | |
82 }; | |
83 | |
84 struct MigrationTestCaseRecord { | |
85 bool is_directory; | |
86 const FilePath::CharType path[64]; | |
87 int64 data_file_size; | |
88 }; | |
89 | |
90 const MigrationTestCaseRecord kMigrationTestCases[] = { | |
91 {true, FILE_PATH_LITERAL("dir a"), 0}, | |
92 {true, FILE_PATH_LITERAL("dir a/dir a"), 0}, | |
93 {true, FILE_PATH_LITERAL("dir a/dir d"), 0}, | |
94 {true, FILE_PATH_LITERAL("dir a/dir d/dir e"), 0}, | |
95 {true, FILE_PATH_LITERAL("dir a/dir d/dir e/dir f"), 0}, | |
96 {true, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g"), 0}, | |
97 {true, FILE_PATH_LITERAL("dir a/dir d/dir e/dir h"), 0}, | |
98 {true, FILE_PATH_LITERAL("dir b"), 0}, | |
99 {true, FILE_PATH_LITERAL("dir b/dir a"), 0}, | |
100 {true, FILE_PATH_LITERAL("dir c"), 0}, | |
101 {false, FILE_PATH_LITERAL("file 0"), 38}, | |
102 {false, FILE_PATH_LITERAL("file 2"), 60}, | |
103 {false, FILE_PATH_LITERAL("file 3"), 0}, | |
104 {false, FILE_PATH_LITERAL("dir a/file 0"), 39}, | |
105 {false, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g/file 0"), 40}, | |
106 {false, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g/file 1"), 41}, | |
107 {false, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g/file 2"), 42}, | |
108 {false, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g/file 3"), 50}, | |
109 }; | |
110 | |
111 struct OriginEnumerationTestRecord { | |
112 std::string origin_url; | |
113 bool has_temporary; | |
114 bool has_persistent; | |
115 }; | |
116 | |
117 const OriginEnumerationTestRecord kOriginEnumerationTestRecords[] = { | |
118 {"http://example.com", false, true}, | |
119 {"http://example1.com", true, false}, | |
120 {"https://example1.com", true, true}, | |
121 {"file://", false, true}, | |
122 {"http://example.com:8000", false, true}, | |
123 }; | |
124 | |
125 } // namespace (anonymous) | |
126 | |
127 // TODO(ericu): The vast majority of this and the other FSFU subclass tests | |
128 // could theoretically be shared. It would basically be a FSFU interface | |
129 // compliance test, and only the subclass-specific bits that look into the | |
130 // implementation would need to be written per-subclass. | |
131 class ObfuscatedFileSystemFileUtilTest : public testing::Test { | |
132 public: | |
133 ObfuscatedFileSystemFileUtilTest() | |
134 : origin_(GURL("http://www.example.com")), | |
135 type_(kFileSystemTypeTemporary), | |
136 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | |
137 test_helper_(origin_, type_), | |
138 quota_status_(quota::kQuotaStatusUnknown), | |
139 usage_(-1) { | |
140 } | |
141 | |
142 void SetUp() { | |
143 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); | |
144 | |
145 quota_manager_ = new quota::QuotaManager( | |
146 false /* is_incognito */, | |
147 data_dir_.path(), | |
148 base::MessageLoopProxy::current(), | |
149 base::MessageLoopProxy::current(), | |
150 NULL /* special storage policy */); | |
151 | |
152 // Every time we create a new helper, it creates another context, which | |
153 // creates another path manager, another sandbox_mount_point_provider, and | |
154 // another OFSFU. We need to pass in the context to skip all that. | |
155 file_system_context_ = new FileSystemContext( | |
156 base::MessageLoopProxy::current(), | |
157 base::MessageLoopProxy::current(), | |
158 new quota::MockSpecialStoragePolicy(), | |
159 quota_manager_->proxy(), | |
160 data_dir_.path(), | |
161 false /* incognito */, | |
162 true /* allow_file_access_from_files */, | |
163 false /* unlimited_quota */, | |
164 NULL /* path_manager */); | |
165 | |
166 obfuscated_file_system_file_util_ = | |
167 static_cast<ObfuscatedFileSystemFileUtil*>( | |
168 file_system_context_->path_manager()->GetFileSystemFileUtil(type_)); | |
169 | |
170 | |
171 test_helper_.SetUp(file_system_context_.get(), | |
172 obfuscated_file_system_file_util_.get()); | |
173 } | |
174 | |
175 FileSystemOperationContext* NewContext(FileSystemTestOriginHelper* helper) { | |
176 FileSystemOperationContext* context; | |
177 if (helper) | |
178 context = helper->NewOperationContext(); | |
179 else | |
180 context = test_helper_.NewOperationContext(); | |
181 context->set_allowed_bytes_growth(1024 * 1024); // Big enough for all tests. | |
182 return context; | |
183 } | |
184 | |
185 // This can only be used after SetUp has run and created file_system_context_ | |
186 // and obfuscated_file_system_file_util_. | |
187 // Use this for tests which need to run in multiple origins; we need a test | |
188 // helper per origin. | |
189 FileSystemTestOriginHelper* NewHelper( | |
190 const GURL& origin, fileapi::FileSystemType type) { | |
191 FileSystemTestOriginHelper* helper = | |
192 new FileSystemTestOriginHelper(origin, type); | |
193 | |
194 helper->SetUp(file_system_context_.get(), | |
195 obfuscated_file_system_file_util_.get()); | |
196 return helper; | |
197 } | |
198 | |
199 ObfuscatedFileSystemFileUtil* ofsfu() { | |
200 return obfuscated_file_system_file_util_.get(); | |
201 } | |
202 | |
203 const FilePath& test_directory() const { | |
204 return data_dir_.path(); | |
205 } | |
206 | |
207 const GURL& origin() const { | |
208 return origin_; | |
209 } | |
210 | |
211 fileapi::FileSystemType type() const { | |
212 return type_; | |
213 } | |
214 | |
215 void GetUsageFromQuotaManager() { | |
216 quota_manager_->GetUsageAndQuota( | |
217 origin(), test_helper_.storage_type(), | |
218 callback_factory_.NewCallback( | |
219 &ObfuscatedFileSystemFileUtilTest::OnGetUsage)); | |
220 MessageLoop::current()->RunAllPending(); | |
221 EXPECT_EQ(quota::kQuotaStatusOk, quota_status_); | |
222 } | |
223 | |
224 void RevokeUsageCache() { | |
225 quota_manager_->ResetUsageTracker(test_helper_.storage_type()); | |
226 ASSERT_TRUE(test_helper_.RevokeUsageCache()); | |
227 } | |
228 | |
229 int64 SizeInUsageFile() { | |
230 return test_helper_.GetCachedOriginUsage(); | |
231 } | |
232 | |
233 int64 usage() const { return usage_; } | |
234 | |
235 void OnGetUsage(quota::QuotaStatusCode status, int64 usage, int64 unused) { | |
236 EXPECT_EQ(quota::kQuotaStatusOk, status); | |
237 quota_status_ = status; | |
238 usage_ = usage; | |
239 } | |
240 | |
241 void CheckFileAndCloseHandle( | |
242 const FilePath& virtual_path, PlatformFile file_handle) { | |
243 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | |
244 FilePath local_path; | |
245 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetLocalFilePath( | |
246 context.get(), virtual_path, &local_path)); | |
247 | |
248 base::PlatformFileInfo file_info0; | |
249 FilePath data_path; | |
250 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo( | |
251 context.get(), virtual_path, &file_info0, &data_path)); | |
252 EXPECT_EQ(data_path, local_path); | |
253 EXPECT_TRUE(FileExists(data_path)); | |
254 EXPECT_EQ(0, GetSize(data_path)); | |
255 | |
256 const char data[] = "test data"; | |
257 const int length = arraysize(data) - 1; | |
258 | |
259 if (base::kInvalidPlatformFileValue == file_handle) { | |
260 bool created = true; | |
261 PlatformFileError error; | |
262 file_handle = base::CreatePlatformFile( | |
263 data_path, | |
264 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, | |
265 &created, | |
266 &error); | |
267 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle); | |
268 ASSERT_EQ(base::PLATFORM_FILE_OK, error); | |
269 EXPECT_FALSE(created); | |
270 } | |
271 ASSERT_EQ(length, base::WritePlatformFile(file_handle, 0, data, length)); | |
272 EXPECT_TRUE(base::ClosePlatformFile(file_handle)); | |
273 | |
274 base::PlatformFileInfo file_info1; | |
275 EXPECT_EQ(length, GetSize(data_path)); | |
276 context.reset(NewContext(NULL)); | |
277 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo( | |
278 context.get(), virtual_path, &file_info1, &data_path)); | |
279 EXPECT_EQ(data_path, local_path); | |
280 | |
281 EXPECT_FALSE(file_info0.is_directory); | |
282 EXPECT_FALSE(file_info1.is_directory); | |
283 EXPECT_FALSE(file_info0.is_symbolic_link); | |
284 EXPECT_FALSE(file_info1.is_symbolic_link); | |
285 EXPECT_EQ(0, file_info0.size); | |
286 EXPECT_EQ(length, file_info1.size); | |
287 EXPECT_LE(file_info0.last_modified, file_info1.last_modified); | |
288 | |
289 context.reset(NewContext(NULL)); | |
290 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->Truncate( | |
291 context.get(), virtual_path, length * 2)); | |
292 EXPECT_EQ(length * 2, GetSize(data_path)); | |
293 | |
294 context.reset(NewContext(NULL)); | |
295 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->Truncate( | |
296 context.get(), virtual_path, 0)); | |
297 EXPECT_EQ(0, GetSize(data_path)); | |
298 } | |
299 | |
300 void ValidateTestDirectory( | |
301 const FilePath& root_path, | |
302 const std::set<FilePath::StringType>& files, | |
303 const std::set<FilePath::StringType>& directories) { | |
304 scoped_ptr<FileSystemOperationContext> context; | |
305 std::set<FilePath::StringType>::const_iterator iter; | |
306 for (iter = files.begin(); iter != files.end(); ++iter) { | |
307 bool created = true; | |
308 context.reset(NewContext(NULL)); | |
309 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
310 ofsfu()->EnsureFileExists( | |
311 context.get(), root_path.Append(*iter), | |
312 &created)); | |
313 ASSERT_FALSE(created); | |
314 } | |
315 for (iter = directories.begin(); iter != directories.end(); ++iter) { | |
316 context.reset(NewContext(NULL)); | |
317 EXPECT_TRUE(ofsfu()->DirectoryExists(context.get(), | |
318 root_path.Append(*iter))); | |
319 } | |
320 } | |
321 | |
322 void FillTestDirectory( | |
323 const FilePath& root_path, | |
324 std::set<FilePath::StringType>* files, | |
325 std::set<FilePath::StringType>* directories) { | |
326 scoped_ptr<FileSystemOperationContext> context; | |
327 context.reset(NewContext(NULL)); | |
328 std::vector<base::FileUtilProxy::Entry> entries; | |
329 EXPECT_EQ(base::PLATFORM_FILE_OK, | |
330 ofsfu()->ReadDirectory(context.get(), root_path, &entries)); | |
331 EXPECT_EQ(0UL, entries.size()); | |
332 | |
333 files->clear(); | |
334 files->insert(FILE_PATH_LITERAL("first")); | |
335 files->insert(FILE_PATH_LITERAL("second")); | |
336 files->insert(FILE_PATH_LITERAL("third")); | |
337 directories->clear(); | |
338 directories->insert(FILE_PATH_LITERAL("fourth")); | |
339 directories->insert(FILE_PATH_LITERAL("fifth")); | |
340 directories->insert(FILE_PATH_LITERAL("sixth")); | |
341 std::set<FilePath::StringType>::iterator iter; | |
342 for (iter = files->begin(); iter != files->end(); ++iter) { | |
343 bool created = false; | |
344 context.reset(NewContext(NULL)); | |
345 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
346 ofsfu()->EnsureFileExists( | |
347 context.get(), root_path.Append(*iter), &created)); | |
348 ASSERT_TRUE(created); | |
349 } | |
350 for (iter = directories->begin(); iter != directories->end(); ++iter) { | |
351 bool exclusive = true; | |
352 bool recursive = false; | |
353 context.reset(NewContext(NULL)); | |
354 EXPECT_EQ(base::PLATFORM_FILE_OK, | |
355 ofsfu()->CreateDirectory( | |
356 context.get(), root_path.Append(*iter), exclusive, recursive)); | |
357 } | |
358 ValidateTestDirectory(root_path, *files, *directories); | |
359 } | |
360 | |
361 void TestReadDirectoryHelper(const FilePath& root_path) { | |
362 std::set<FilePath::StringType> files; | |
363 std::set<FilePath::StringType> directories; | |
364 FillTestDirectory(root_path, &files, &directories); | |
365 | |
366 scoped_ptr<FileSystemOperationContext> context; | |
367 std::vector<base::FileUtilProxy::Entry> entries; | |
368 context.reset(NewContext(NULL)); | |
369 EXPECT_EQ(base::PLATFORM_FILE_OK, | |
370 ofsfu()->ReadDirectory(context.get(), root_path, &entries)); | |
371 std::vector<base::FileUtilProxy::Entry>::iterator entry_iter; | |
372 EXPECT_EQ(files.size() + directories.size(), entries.size()); | |
373 for (entry_iter = entries.begin(); entry_iter != entries.end(); | |
374 ++entry_iter) { | |
375 const base::FileUtilProxy::Entry& entry = *entry_iter; | |
376 std::set<FilePath::StringType>::iterator iter = files.find(entry.name); | |
377 if (iter != files.end()) { | |
378 EXPECT_FALSE(entry.is_directory); | |
379 files.erase(iter); | |
380 continue; | |
381 } | |
382 iter = directories.find(entry.name); | |
383 EXPECT_FALSE(directories.end() == iter); | |
384 EXPECT_TRUE(entry.is_directory); | |
385 directories.erase(iter); | |
386 } | |
387 } | |
388 | |
389 void TestTouchHelper(const FilePath& path, bool new_file) { | |
390 base::Time last_access_time = base::Time::Now(); // Ignored, so not tested. | |
391 base::Time last_modified_time = base::Time::Now(); | |
392 scoped_ptr<FileSystemOperationContext> context; | |
393 | |
394 if (new_file) { | |
395 // Verify that file creation requires sufficient quota for the path. | |
396 context.reset(NewContext(NULL)); | |
397 context->set_allowed_bytes_growth( | |
398 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(path) - 1); | |
399 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, | |
400 ofsfu()->Touch( | |
401 context.get(), path, last_access_time, last_modified_time)); | |
402 } | |
403 | |
404 context.reset(NewContext(NULL)); | |
405 context->set_allowed_bytes_growth( | |
406 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(path)); | |
407 EXPECT_EQ(base::PLATFORM_FILE_OK, | |
408 ofsfu()->Touch( | |
409 context.get(), path, last_access_time, last_modified_time)); | |
410 FilePath local_path; | |
411 base::PlatformFileInfo file_info; | |
412 context.reset(NewContext(NULL)); | |
413 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo( | |
414 context.get(), path, &file_info, &local_path)); | |
415 // We compare as time_t here to lower our resolution, to avoid false | |
416 // negatives caused by conversion to the local filesystem's native | |
417 // representation and back. | |
418 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT()); | |
419 | |
420 context.reset(NewContext(NULL)); | |
421 last_modified_time += base::TimeDelta::FromHours(1); | |
422 EXPECT_EQ(base::PLATFORM_FILE_OK, | |
423 ofsfu()->Touch( | |
424 context.get(), path, last_access_time, last_modified_time)); | |
425 context.reset(NewContext(NULL)); | |
426 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo( | |
427 context.get(), path, &file_info, &local_path)); | |
428 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT()); | |
429 } | |
430 | |
431 void TestCopyInForeignFileHelper(bool overwrite) { | |
432 ScopedTempDir source_dir; | |
433 ASSERT_TRUE(source_dir.CreateUniqueTempDir()); | |
434 FilePath root_path = source_dir.path(); | |
435 FilePath src_path = root_path.AppendASCII("file_name"); | |
436 FilePath dest_path(FILE_PATH_LITERAL("new file")); | |
437 int64 src_file_length = 87; | |
438 | |
439 base::PlatformFileError error_code; | |
440 bool created = false; | |
441 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE; | |
442 base::PlatformFile file_handle = | |
443 base::CreatePlatformFile( | |
444 src_path, file_flags, &created, &error_code); | |
445 EXPECT_TRUE(created); | |
446 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code); | |
447 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle); | |
448 ASSERT_TRUE(base::TruncatePlatformFile(file_handle, src_file_length)); | |
449 EXPECT_TRUE(base::ClosePlatformFile(file_handle)); | |
450 | |
451 scoped_ptr<FileSystemOperationContext> context; | |
452 | |
453 if (overwrite) { | |
454 context.reset(NewContext(NULL)); | |
455 EXPECT_EQ(base::PLATFORM_FILE_OK, | |
456 ofsfu()->EnsureFileExists(context.get(), dest_path, &created)); | |
457 EXPECT_TRUE(created); | |
458 } | |
459 | |
460 const int64 path_cost = | |
461 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(dest_path); | |
462 if (!overwrite) { | |
463 // Verify that file creation requires sufficient quota for the path. | |
464 context.reset(NewContext(NULL)); | |
465 context->set_allowed_bytes_growth(path_cost + src_file_length - 1); | |
466 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, | |
467 ofsfu()->CopyInForeignFile(context.get(), src_path, dest_path)); | |
468 } | |
469 | |
470 context.reset(NewContext(NULL)); | |
471 context->set_allowed_bytes_growth(path_cost + src_file_length); | |
472 EXPECT_EQ(base::PLATFORM_FILE_OK, | |
473 ofsfu()->CopyInForeignFile(context.get(), src_path, dest_path)); | |
474 | |
475 context.reset(NewContext(NULL)); | |
476 EXPECT_TRUE(ofsfu()->PathExists(context.get(), dest_path)); | |
477 context.reset(NewContext(NULL)); | |
478 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), dest_path)); | |
479 context.reset(NewContext(NULL)); | |
480 base::PlatformFileInfo file_info; | |
481 FilePath data_path; | |
482 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo( | |
483 context.get(), dest_path, &file_info, &data_path)); | |
484 EXPECT_NE(data_path, src_path); | |
485 EXPECT_TRUE(FileExists(data_path)); | |
486 EXPECT_EQ(src_file_length, GetSize(data_path)); | |
487 | |
488 EXPECT_EQ(base::PLATFORM_FILE_OK, | |
489 ofsfu()->DeleteFile(context.get(), dest_path)); | |
490 } | |
491 | |
492 private: | |
493 ScopedTempDir data_dir_; | |
494 scoped_refptr<ObfuscatedFileSystemFileUtil> obfuscated_file_system_file_util_; | |
495 scoped_refptr<quota::QuotaManager> quota_manager_; | |
496 scoped_refptr<FileSystemContext> file_system_context_; | |
497 GURL origin_; | |
498 fileapi::FileSystemType type_; | |
499 base::ScopedCallbackFactory<ObfuscatedFileSystemFileUtilTest> | |
500 callback_factory_; | |
501 FileSystemTestOriginHelper test_helper_; | |
502 quota::QuotaStatusCode quota_status_; | |
503 int64 usage_; | |
504 | |
505 DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileSystemFileUtilTest); | |
506 }; | |
507 | |
508 TEST_F(ObfuscatedFileSystemFileUtilTest, TestCreateAndDeleteFile) { | |
509 base::PlatformFile file_handle = base::kInvalidPlatformFileValue; | |
510 bool created; | |
511 FilePath path = UTF8ToFilePath("fake/file"); | |
512 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | |
513 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE; | |
514 | |
515 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, | |
516 ofsfu()->CreateOrOpen( | |
517 context.get(), path, file_flags, &file_handle, | |
518 &created)); | |
519 | |
520 context.reset(NewContext(NULL)); | |
521 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, | |
522 ofsfu()->DeleteFile(context.get(), path)); | |
523 | |
524 path = UTF8ToFilePath("test file"); | |
525 | |
526 // Verify that file creation requires sufficient quota for the path. | |
527 context.reset(NewContext(NULL)); | |
528 context->set_allowed_bytes_growth( | |
529 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(path) - 1); | |
530 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, | |
531 ofsfu()->CreateOrOpen( | |
532 context.get(), path, file_flags, &file_handle, &created)); | |
533 | |
534 context.reset(NewContext(NULL)); | |
535 context->set_allowed_bytes_growth( | |
536 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(path)); | |
537 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
538 ofsfu()->CreateOrOpen( | |
539 context.get(), path, file_flags, &file_handle, &created)); | |
540 ASSERT_TRUE(created); | |
541 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle); | |
542 | |
543 CheckFileAndCloseHandle(path, file_handle); | |
544 | |
545 context.reset(NewContext(NULL)); | |
546 FilePath local_path; | |
547 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetLocalFilePath( | |
548 context.get(), path, &local_path)); | |
549 EXPECT_TRUE(file_util::PathExists(local_path)); | |
550 | |
551 // Verify that deleting a file isn't stopped by zero quota, and that it frees | |
552 // up quote from its path. | |
553 context.reset(NewContext(NULL)); | |
554 context->set_allowed_bytes_growth(0); | |
555 EXPECT_EQ(base::PLATFORM_FILE_OK, | |
556 ofsfu()->DeleteFile(context.get(), path)); | |
557 EXPECT_FALSE(file_util::PathExists(local_path)); | |
558 EXPECT_EQ(ObfuscatedFileSystemFileUtil::ComputeFilePathCost(path), | |
559 context->allowed_bytes_growth()); | |
560 | |
561 context.reset(NewContext(NULL)); | |
562 bool exclusive = true; | |
563 bool recursive = true; | |
564 FilePath directory_path = UTF8ToFilePath("series/of/directories"); | |
565 path = directory_path.AppendASCII("file name"); | |
566 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( | |
567 context.get(), directory_path, exclusive, recursive)); | |
568 | |
569 context.reset(NewContext(NULL)); | |
570 file_handle = base::kInvalidPlatformFileValue; | |
571 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
572 ofsfu()->CreateOrOpen( | |
573 context.get(), path, file_flags, &file_handle, &created)); | |
574 ASSERT_TRUE(created); | |
575 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle); | |
576 | |
577 CheckFileAndCloseHandle(path, file_handle); | |
578 | |
579 context.reset(NewContext(NULL)); | |
580 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetLocalFilePath( | |
581 context.get(), path, &local_path)); | |
582 EXPECT_TRUE(file_util::PathExists(local_path)); | |
583 | |
584 context.reset(NewContext(NULL)); | |
585 EXPECT_EQ(base::PLATFORM_FILE_OK, | |
586 ofsfu()->DeleteFile(context.get(), path)); | |
587 EXPECT_FALSE(file_util::PathExists(local_path)); | |
588 } | |
589 | |
590 TEST_F(ObfuscatedFileSystemFileUtilTest, TestTruncate) { | |
591 bool created = false; | |
592 FilePath path = UTF8ToFilePath("file"); | |
593 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | |
594 | |
595 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, | |
596 ofsfu()->Truncate(context.get(), path, 4)); | |
597 | |
598 context.reset(NewContext(NULL)); | |
599 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
600 ofsfu()->EnsureFileExists(context.get(), path, &created)); | |
601 ASSERT_TRUE(created); | |
602 | |
603 context.reset(NewContext(NULL)); | |
604 FilePath local_path; | |
605 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetLocalFilePath( | |
606 context.get(), path, &local_path)); | |
607 EXPECT_EQ(0, GetSize(local_path)); | |
608 | |
609 context.reset(NewContext(NULL)); | |
610 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->Truncate( | |
611 context.get(), path, 10)); | |
612 EXPECT_EQ(10, GetSize(local_path)); | |
613 | |
614 context.reset(NewContext(NULL)); | |
615 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->Truncate( | |
616 context.get(), path, 1)); | |
617 EXPECT_EQ(1, GetSize(local_path)); | |
618 | |
619 context.reset(NewContext(NULL)); | |
620 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), path)); | |
621 context.reset(NewContext(NULL)); | |
622 EXPECT_TRUE(ofsfu()->PathExists(context.get(), path)); | |
623 } | |
624 | |
625 TEST_F(ObfuscatedFileSystemFileUtilTest, TestEnsureFileExists) { | |
626 FilePath path = UTF8ToFilePath("fake/file"); | |
627 bool created = false; | |
628 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | |
629 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, | |
630 ofsfu()->EnsureFileExists( | |
631 context.get(), path, &created)); | |
632 | |
633 // Verify that file creation requires sufficient quota for the path. | |
634 context.reset(NewContext(NULL)); | |
635 path = UTF8ToFilePath("test file"); | |
636 created = false; | |
637 context->set_allowed_bytes_growth( | |
638 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(path) - 1); | |
639 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, | |
640 ofsfu()->EnsureFileExists(context.get(), path, &created)); | |
641 ASSERT_FALSE(created); | |
642 | |
643 context.reset(NewContext(NULL)); | |
644 context->set_allowed_bytes_growth( | |
645 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(path)); | |
646 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
647 ofsfu()->EnsureFileExists(context.get(), path, &created)); | |
648 ASSERT_TRUE(created); | |
649 | |
650 CheckFileAndCloseHandle(path, base::kInvalidPlatformFileValue); | |
651 | |
652 context.reset(NewContext(NULL)); | |
653 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
654 ofsfu()->EnsureFileExists(context.get(), path, &created)); | |
655 ASSERT_FALSE(created); | |
656 | |
657 // Also test in a subdirectory. | |
658 path = UTF8ToFilePath("path/to/file.txt"); | |
659 context.reset(NewContext(NULL)); | |
660 bool exclusive = true; | |
661 bool recursive = true; | |
662 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( | |
663 context.get(), path.DirName(), exclusive, recursive)); | |
664 | |
665 context.reset(NewContext(NULL)); | |
666 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
667 ofsfu()->EnsureFileExists(context.get(), path, &created)); | |
668 ASSERT_TRUE(created); | |
669 context.reset(NewContext(NULL)); | |
670 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), path)); | |
671 context.reset(NewContext(NULL)); | |
672 EXPECT_TRUE(ofsfu()->PathExists(context.get(), path)); | |
673 } | |
674 | |
675 TEST_F(ObfuscatedFileSystemFileUtilTest, TestDirectoryOps) { | |
676 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | |
677 | |
678 bool exclusive = false; | |
679 bool recursive = false; | |
680 FilePath path = UTF8ToFilePath("foo/bar"); | |
681 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofsfu()->CreateDirectory( | |
682 context.get(), path, exclusive, recursive)); | |
683 | |
684 context.reset(NewContext(NULL)); | |
685 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, | |
686 ofsfu()->DeleteSingleDirectory(context.get(), path)); | |
687 | |
688 FilePath root = UTF8ToFilePath(""); | |
689 context.reset(NewContext(NULL)); | |
690 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), path)); | |
691 context.reset(NewContext(NULL)); | |
692 EXPECT_FALSE(ofsfu()->PathExists(context.get(), path)); | |
693 context.reset(NewContext(NULL)); | |
694 EXPECT_TRUE(ofsfu()->IsDirectoryEmpty(context.get(), root)); | |
695 | |
696 context.reset(NewContext(NULL)); | |
697 exclusive = false; | |
698 recursive = true; | |
699 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( | |
700 context.get(), path, exclusive, recursive)); | |
701 | |
702 context.reset(NewContext(NULL)); | |
703 EXPECT_TRUE(ofsfu()->DirectoryExists(context.get(), path)); | |
704 context.reset(NewContext(NULL)); | |
705 EXPECT_TRUE(ofsfu()->PathExists(context.get(), path)); | |
706 context.reset(NewContext(NULL)); | |
707 EXPECT_FALSE(ofsfu()->IsDirectoryEmpty(context.get(), root)); | |
708 context.reset(NewContext(NULL)); | |
709 EXPECT_TRUE(ofsfu()->DirectoryExists(context.get(), path.DirName())); | |
710 context.reset(NewContext(NULL)); | |
711 EXPECT_FALSE(ofsfu()->IsDirectoryEmpty(context.get(), path.DirName())); | |
712 | |
713 // Can't remove a non-empty directory. | |
714 context.reset(NewContext(NULL)); | |
715 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY, | |
716 ofsfu()->DeleteSingleDirectory(context.get(), path.DirName())); | |
717 | |
718 base::PlatformFileInfo file_info; | |
719 FilePath local_path; | |
720 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo( | |
721 context.get(), path, &file_info, &local_path)); | |
722 EXPECT_TRUE(local_path.empty()); | |
723 EXPECT_TRUE(file_info.is_directory); | |
724 EXPECT_FALSE(file_info.is_symbolic_link); | |
725 | |
726 // Same create again should succeed, since exclusive is false. | |
727 context.reset(NewContext(NULL)); | |
728 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( | |
729 context.get(), path, exclusive, recursive)); | |
730 | |
731 exclusive = true; | |
732 recursive = true; | |
733 context.reset(NewContext(NULL)); | |
734 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofsfu()->CreateDirectory( | |
735 context.get(), path, exclusive, recursive)); | |
736 | |
737 // Verify that deleting a directory isn't stopped by zero quota, and that it | |
738 // frees up quota from its path. | |
739 context.reset(NewContext(NULL)); | |
740 context->set_allowed_bytes_growth(0); | |
741 EXPECT_EQ(base::PLATFORM_FILE_OK, | |
742 ofsfu()->DeleteSingleDirectory(context.get(), path)); | |
743 EXPECT_EQ(ObfuscatedFileSystemFileUtil::ComputeFilePathCost(path), | |
744 context->allowed_bytes_growth()); | |
745 | |
746 path = UTF8ToFilePath("foo/bop"); | |
747 | |
748 context.reset(NewContext(NULL)); | |
749 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), path)); | |
750 context.reset(NewContext(NULL)); | |
751 EXPECT_FALSE(ofsfu()->PathExists(context.get(), path)); | |
752 context.reset(NewContext(NULL)); | |
753 EXPECT_TRUE(ofsfu()->IsDirectoryEmpty(context.get(), path)); | |
754 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofsfu()->GetFileInfo( | |
755 context.get(), path, &file_info, &local_path)); | |
756 | |
757 // Verify that file creation requires sufficient quota for the path. | |
758 exclusive = true; | |
759 recursive = false; | |
760 context.reset(NewContext(NULL)); | |
761 context->set_allowed_bytes_growth( | |
762 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(path) - 1); | |
763 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, ofsfu()->CreateDirectory( | |
764 context.get(), path, exclusive, recursive)); | |
765 | |
766 context.reset(NewContext(NULL)); | |
767 context->set_allowed_bytes_growth( | |
768 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(path)); | |
769 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( | |
770 context.get(), path, exclusive, recursive)); | |
771 | |
772 context.reset(NewContext(NULL)); | |
773 EXPECT_TRUE(ofsfu()->DirectoryExists(context.get(), path)); | |
774 context.reset(NewContext(NULL)); | |
775 EXPECT_TRUE(ofsfu()->PathExists(context.get(), path)); | |
776 | |
777 exclusive = true; | |
778 recursive = false; | |
779 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofsfu()->CreateDirectory( | |
780 context.get(), path, exclusive, recursive)); | |
781 | |
782 exclusive = true; | |
783 recursive = false; | |
784 path = UTF8ToFilePath("foo"); | |
785 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofsfu()->CreateDirectory( | |
786 context.get(), path, exclusive, recursive)); | |
787 | |
788 path = UTF8ToFilePath("blah"); | |
789 | |
790 context.reset(NewContext(NULL)); | |
791 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), path)); | |
792 context.reset(NewContext(NULL)); | |
793 EXPECT_FALSE(ofsfu()->PathExists(context.get(), path)); | |
794 | |
795 exclusive = true; | |
796 recursive = false; | |
797 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( | |
798 context.get(), path, exclusive, recursive)); | |
799 | |
800 context.reset(NewContext(NULL)); | |
801 EXPECT_TRUE(ofsfu()->DirectoryExists(context.get(), path)); | |
802 context.reset(NewContext(NULL)); | |
803 EXPECT_TRUE(ofsfu()->PathExists(context.get(), path)); | |
804 | |
805 exclusive = true; | |
806 recursive = false; | |
807 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofsfu()->CreateDirectory( | |
808 context.get(), path, exclusive, recursive)); | |
809 } | |
810 | |
811 TEST_F(ObfuscatedFileSystemFileUtilTest, TestReadDirectory) { | |
812 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | |
813 bool exclusive = true; | |
814 bool recursive = true; | |
815 FilePath path = UTF8ToFilePath("directory/to/use"); | |
816 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( | |
817 context.get(), path, exclusive, recursive)); | |
818 TestReadDirectoryHelper(path); | |
819 } | |
820 | |
821 TEST_F(ObfuscatedFileSystemFileUtilTest, TestReadRootWithSlash) { | |
822 TestReadDirectoryHelper(UTF8ToFilePath("")); | |
823 } | |
824 | |
825 TEST_F(ObfuscatedFileSystemFileUtilTest, TestReadRootWithEmptyString) { | |
826 TestReadDirectoryHelper(UTF8ToFilePath("/")); | |
827 } | |
828 | |
829 TEST_F(ObfuscatedFileSystemFileUtilTest, TestReadDirectoryOnFile) { | |
830 FilePath path = UTF8ToFilePath("file"); | |
831 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | |
832 | |
833 bool created = false; | |
834 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
835 ofsfu()->EnsureFileExists(context.get(), path, &created)); | |
836 ASSERT_TRUE(created); | |
837 | |
838 context.reset(NewContext(NULL)); | |
839 std::vector<base::FileUtilProxy::Entry> entries; | |
840 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, | |
841 ofsfu()->ReadDirectory(context.get(), path, &entries)); | |
842 | |
843 EXPECT_TRUE(ofsfu()->IsDirectoryEmpty(context.get(), path)); | |
844 } | |
845 | |
846 TEST_F(ObfuscatedFileSystemFileUtilTest, TestTouch) { | |
847 FilePath path = UTF8ToFilePath("fake/file"); | |
848 base::Time last_access_time = base::Time::Now(); // Ignored, so not tested. | |
849 base::Time last_modified_time = base::Time::Now(); | |
850 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | |
851 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, | |
852 ofsfu()->Touch( | |
853 context.get(), path, last_access_time, last_modified_time)); | |
854 | |
855 // Touch will create a file if it's not there but its parent is. | |
856 bool new_file = true; | |
857 path = UTF8ToFilePath("file name"); | |
858 TestTouchHelper(path, new_file); | |
859 | |
860 bool exclusive = true; | |
861 bool recursive = true; | |
862 path = UTF8ToFilePath("directory/to/use"); | |
863 context.reset(NewContext(NULL)); | |
864 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( | |
865 context.get(), path, exclusive, recursive)); | |
866 new_file = false; | |
867 TestTouchHelper(path, new_file); | |
868 } | |
869 | |
870 TEST_F(ObfuscatedFileSystemFileUtilTest, TestPathQuotas) { | |
871 FilePath path = UTF8ToFilePath("fake/file"); | |
872 base::Time last_access_time = base::Time::Now(); | |
873 base::Time last_modified_time = base::Time::Now(); | |
874 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | |
875 | |
876 // Touch will create a file if it's not there but its parent is. | |
877 path = UTF8ToFilePath("file name"); | |
878 context->set_allowed_bytes_growth(5); | |
879 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, | |
880 ofsfu()->Touch( | |
881 context.get(), path, last_access_time, last_modified_time)); | |
882 context->set_allowed_bytes_growth(1024); | |
883 EXPECT_EQ(base::PLATFORM_FILE_OK, | |
884 ofsfu()->Touch( | |
885 context.get(), path, last_access_time, last_modified_time)); | |
886 int64 path_cost = ObfuscatedFileSystemFileUtil::ComputeFilePathCost(path); | |
887 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth()); | |
888 | |
889 context->set_allowed_bytes_growth(1024); | |
890 bool exclusive = true; | |
891 bool recursive = true; | |
892 path = UTF8ToFilePath("directory/to/use"); | |
893 std::vector<FilePath::StringType> components; | |
894 path.GetComponents(&components); | |
895 path_cost = 0; | |
896 for (std::vector<FilePath::StringType>::iterator iter = components.begin(); | |
897 iter != components.end(); ++iter) { | |
898 path_cost += ObfuscatedFileSystemFileUtil::ComputeFilePathCost( | |
899 FilePath(*iter)); | |
900 } | |
901 context.reset(NewContext(NULL)); | |
902 context->set_allowed_bytes_growth(1024); | |
903 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( | |
904 context.get(), path, exclusive, recursive)); | |
905 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth()); | |
906 } | |
907 | |
908 TEST_F(ObfuscatedFileSystemFileUtilTest, TestCopyOrMoveFileNotFound) { | |
909 FilePath source_path = UTF8ToFilePath("path0.txt"); | |
910 FilePath dest_path = UTF8ToFilePath("path1.txt"); | |
911 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | |
912 | |
913 bool is_copy_not_move = false; | |
914 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, | |
915 ofsfu()->CopyOrMoveFile(context.get(), source_path, dest_path, | |
916 is_copy_not_move)); | |
917 context.reset(NewContext(NULL)); | |
918 is_copy_not_move = true; | |
919 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, | |
920 ofsfu()->CopyOrMoveFile(context.get(), source_path, dest_path, | |
921 is_copy_not_move)); | |
922 source_path = UTF8ToFilePath("dir/dir/file"); | |
923 bool exclusive = true; | |
924 bool recursive = true; | |
925 context.reset(NewContext(NULL)); | |
926 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( | |
927 context.get(), source_path.DirName(), exclusive, recursive)); | |
928 is_copy_not_move = false; | |
929 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, | |
930 ofsfu()->CopyOrMoveFile(context.get(), source_path, dest_path, | |
931 is_copy_not_move)); | |
932 context.reset(NewContext(NULL)); | |
933 is_copy_not_move = true; | |
934 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, | |
935 ofsfu()->CopyOrMoveFile(context.get(), source_path, dest_path, | |
936 is_copy_not_move)); | |
937 } | |
938 | |
939 TEST_F(ObfuscatedFileSystemFileUtilTest, TestCopyOrMoveFileSuccess) { | |
940 const int64 kSourceLength = 5; | |
941 const int64 kDestLength = 50; | |
942 | |
943 for (size_t i = 0; i < arraysize(kCopyMoveTestCases); ++i) { | |
944 SCOPED_TRACE(testing::Message() << "kCopyMoveTestCase " << i); | |
945 const CopyMoveTestCaseRecord& test_case = kCopyMoveTestCases[i]; | |
946 SCOPED_TRACE(testing::Message() << "\t is_copy_not_move " << | |
947 test_case.is_copy_not_move); | |
948 SCOPED_TRACE(testing::Message() << "\t source_path " << | |
949 test_case.source_path); | |
950 SCOPED_TRACE(testing::Message() << "\t dest_path " << | |
951 test_case.dest_path); | |
952 SCOPED_TRACE(testing::Message() << "\t cause_overwrite " << | |
953 test_case.cause_overwrite); | |
954 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | |
955 | |
956 bool exclusive = false; | |
957 bool recursive = true; | |
958 FilePath source_path = UTF8ToFilePath(test_case.source_path); | |
959 FilePath dest_path = UTF8ToFilePath(test_case.dest_path); | |
960 | |
961 context.reset(NewContext(NULL)); | |
962 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( | |
963 context.get(), source_path.DirName(), exclusive, recursive)); | |
964 context.reset(NewContext(NULL)); | |
965 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( | |
966 context.get(), dest_path.DirName(), exclusive, recursive)); | |
967 | |
968 bool created = false; | |
969 context.reset(NewContext(NULL)); | |
970 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
971 ofsfu()->EnsureFileExists(context.get(), source_path, &created)); | |
972 ASSERT_TRUE(created); | |
973 context.reset(NewContext(NULL)); | |
974 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
975 ofsfu()->Truncate(context.get(), source_path, kSourceLength)); | |
976 | |
977 if (test_case.cause_overwrite) { | |
978 context.reset(NewContext(NULL)); | |
979 created = false; | |
980 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
981 ofsfu()->EnsureFileExists(context.get(), dest_path, &created)); | |
982 ASSERT_TRUE(created); | |
983 context.reset(NewContext(NULL)); | |
984 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
985 ofsfu()->Truncate(context.get(), dest_path, kDestLength)); | |
986 } | |
987 | |
988 context.reset(NewContext(NULL)); | |
989 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CopyOrMoveFile(context.get(), | |
990 source_path, dest_path, test_case.is_copy_not_move)); | |
991 if (test_case.is_copy_not_move) { | |
992 base::PlatformFileInfo file_info; | |
993 FilePath local_path; | |
994 context.reset(NewContext(NULL)); | |
995 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo( | |
996 context.get(), source_path, &file_info, &local_path)); | |
997 EXPECT_EQ(kSourceLength, file_info.size); | |
998 EXPECT_EQ(base::PLATFORM_FILE_OK, | |
999 ofsfu()->DeleteFile(context.get(), source_path)); | |
1000 } else { | |
1001 base::PlatformFileInfo file_info; | |
1002 FilePath local_path; | |
1003 context.reset(NewContext(NULL)); | |
1004 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofsfu()->GetFileInfo( | |
1005 context.get(), source_path, &file_info, &local_path)); | |
1006 } | |
1007 base::PlatformFileInfo file_info; | |
1008 FilePath local_path; | |
1009 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo( | |
1010 context.get(), dest_path, &file_info, &local_path)); | |
1011 EXPECT_EQ(kSourceLength, file_info.size); | |
1012 | |
1013 EXPECT_EQ(base::PLATFORM_FILE_OK, | |
1014 ofsfu()->DeleteFile(context.get(), dest_path)); | |
1015 } | |
1016 } | |
1017 | |
1018 TEST_F(ObfuscatedFileSystemFileUtilTest, TestCopyPathQuotas) { | |
1019 FilePath src_path = UTF8ToFilePath("src path"); | |
1020 FilePath dest_path = UTF8ToFilePath("destination path"); | |
1021 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | |
1022 bool created = false; | |
1023 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->EnsureFileExists( | |
1024 context.get(), src_path, &created)); | |
1025 | |
1026 bool is_copy = true; | |
1027 // Copy, no overwrite. | |
1028 context->set_allowed_bytes_growth( | |
1029 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(dest_path) - 1); | |
1030 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, | |
1031 ofsfu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); | |
1032 context.reset(NewContext(NULL)); | |
1033 context->set_allowed_bytes_growth( | |
1034 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(dest_path)); | |
1035 EXPECT_EQ(base::PLATFORM_FILE_OK, | |
1036 ofsfu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); | |
1037 | |
1038 // Copy, with overwrite. | |
1039 context.reset(NewContext(NULL)); | |
1040 context->set_allowed_bytes_growth(0); | |
1041 EXPECT_EQ(base::PLATFORM_FILE_OK, | |
1042 ofsfu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); | |
1043 } | |
1044 | |
1045 TEST_F(ObfuscatedFileSystemFileUtilTest, TestMovePathQuotasWithRename) { | |
1046 FilePath src_path = UTF8ToFilePath("src path"); | |
1047 FilePath dest_path = UTF8ToFilePath("destination path"); | |
1048 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | |
1049 bool created = false; | |
1050 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->EnsureFileExists( | |
1051 context.get(), src_path, &created)); | |
1052 | |
1053 bool is_copy = false; | |
1054 // Move, rename, no overwrite. | |
1055 context.reset(NewContext(NULL)); | |
1056 context->set_allowed_bytes_growth( | |
1057 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(dest_path) - | |
1058 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(src_path) - 1); | |
1059 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, | |
1060 ofsfu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); | |
1061 context.reset(NewContext(NULL)); | |
1062 context->set_allowed_bytes_growth( | |
1063 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(dest_path) - | |
1064 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(src_path)); | |
1065 EXPECT_EQ(base::PLATFORM_FILE_OK, | |
1066 ofsfu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); | |
1067 | |
1068 context.reset(NewContext(NULL)); | |
1069 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->EnsureFileExists( | |
1070 context.get(), src_path, &created)); | |
1071 | |
1072 // Move, rename, with overwrite. | |
1073 context.reset(NewContext(NULL)); | |
1074 context->set_allowed_bytes_growth(0); | |
1075 EXPECT_EQ(base::PLATFORM_FILE_OK, | |
1076 ofsfu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); | |
1077 } | |
1078 | |
1079 TEST_F(ObfuscatedFileSystemFileUtilTest, TestMovePathQuotasWithoutRename) { | |
1080 FilePath src_path = UTF8ToFilePath("src path"); | |
1081 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | |
1082 bool created = false; | |
1083 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->EnsureFileExists( | |
1084 context.get(), src_path, &created)); | |
1085 | |
1086 bool exclusive = true; | |
1087 bool recursive = false; | |
1088 FilePath dir_path = UTF8ToFilePath("directory path"); | |
1089 context.reset(NewContext(NULL)); | |
1090 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( | |
1091 context.get(), dir_path, exclusive, recursive)); | |
1092 | |
1093 FilePath dest_path = dir_path.Append(src_path); | |
1094 | |
1095 bool is_copy = false; | |
1096 int64 allowed_bytes_growth = -1000; // Over quota, this should still work. | |
1097 // Move, no rename, no overwrite. | |
1098 context.reset(NewContext(NULL)); | |
1099 context->set_allowed_bytes_growth(allowed_bytes_growth); | |
1100 EXPECT_EQ(base::PLATFORM_FILE_OK, | |
1101 ofsfu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); | |
1102 EXPECT_EQ(allowed_bytes_growth, context->allowed_bytes_growth()); | |
1103 | |
1104 // Move, no rename, with overwrite. | |
1105 context.reset(NewContext(NULL)); | |
1106 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->EnsureFileExists( | |
1107 context.get(), src_path, &created)); | |
1108 context.reset(NewContext(NULL)); | |
1109 context->set_allowed_bytes_growth(allowed_bytes_growth); | |
1110 EXPECT_EQ(base::PLATFORM_FILE_OK, | |
1111 ofsfu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); | |
1112 EXPECT_EQ( | |
1113 allowed_bytes_growth + | |
1114 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(src_path), | |
1115 context->allowed_bytes_growth()); | |
1116 } | |
1117 | |
1118 TEST_F(ObfuscatedFileSystemFileUtilTest, TestCopyInForeignFile) { | |
1119 TestCopyInForeignFileHelper(false /* overwrite */); | |
1120 TestCopyInForeignFileHelper(true /* overwrite */); | |
1121 } | |
1122 | |
1123 TEST_F(ObfuscatedFileSystemFileUtilTest, TestEnumerator) { | |
1124 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | |
1125 FilePath src_path = UTF8ToFilePath("source dir"); | |
1126 bool exclusive = true; | |
1127 bool recursive = false; | |
1128 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( | |
1129 context.get(), src_path, exclusive, recursive)); | |
1130 | |
1131 std::set<FilePath::StringType> files; | |
1132 std::set<FilePath::StringType> directories; | |
1133 FillTestDirectory(src_path, &files, &directories); | |
1134 | |
1135 FilePath dest_path = UTF8ToFilePath("destination dir"); | |
1136 | |
1137 context.reset(NewContext(NULL)); | |
1138 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), dest_path)); | |
1139 context.reset(NewContext(NULL)); | |
1140 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
1141 ofsfu()->Copy(context.get(), src_path, dest_path)); | |
1142 | |
1143 ValidateTestDirectory(dest_path, files, directories); | |
1144 context.reset(NewContext(NULL)); | |
1145 EXPECT_TRUE(ofsfu()->DirectoryExists(context.get(), src_path)); | |
1146 context.reset(NewContext(NULL)); | |
1147 EXPECT_TRUE(ofsfu()->DirectoryExists(context.get(), dest_path)); | |
1148 context.reset(NewContext(NULL)); | |
1149 recursive = true; | |
1150 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
1151 ofsfu()->Delete(context.get(), dest_path, recursive)); | |
1152 context.reset(NewContext(NULL)); | |
1153 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), dest_path)); | |
1154 } | |
1155 | |
1156 TEST_F(ObfuscatedFileSystemFileUtilTest, TestMigration) { | |
1157 ScopedTempDir source_dir; | |
1158 ASSERT_TRUE(source_dir.CreateUniqueTempDir()); | |
1159 FilePath root_path = source_dir.path().AppendASCII("chrome-pLmnMWXE7NzTFRsn"); | |
1160 ASSERT_TRUE(file_util::CreateDirectory(root_path)); | |
1161 | |
1162 for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) { | |
1163 SCOPED_TRACE(testing::Message() << "Creating kMigrationTestPath " << i); | |
1164 const MigrationTestCaseRecord& test_case = kMigrationTestCases[i]; | |
1165 FilePath local_src_path = root_path.Append(test_case.path); | |
1166 if (test_case.is_directory) { | |
1167 ASSERT_TRUE( | |
1168 file_util::CreateDirectory(local_src_path)); | |
1169 } else { | |
1170 base::PlatformFileError error_code; | |
1171 bool created = false; | |
1172 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE; | |
1173 base::PlatformFile file_handle = | |
1174 base::CreatePlatformFile( | |
1175 local_src_path, file_flags, &created, &error_code); | |
1176 EXPECT_TRUE(created); | |
1177 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle); | |
1178 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code); | |
1179 ASSERT_TRUE( | |
1180 base::TruncatePlatformFile(file_handle, test_case.data_file_size)); | |
1181 EXPECT_TRUE(base::ClosePlatformFile(file_handle)); | |
1182 } | |
1183 } | |
1184 | |
1185 EXPECT_TRUE(ofsfu()->MigrateFromOldSandbox(origin(), type(), root_path)); | |
1186 | |
1187 FilePath new_root = | |
1188 test_directory().AppendASCII("File System").AppendASCII("000").Append( | |
1189 ofsfu()->GetDirectoryNameForType(type())).AppendASCII("Legacy"); | |
1190 for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) { | |
1191 SCOPED_TRACE(testing::Message() << "Validating kMigrationTestPath " << i); | |
1192 const MigrationTestCaseRecord& test_case = kMigrationTestCases[i]; | |
1193 FilePath local_data_path = new_root.Append(test_case.path); | |
1194 #if defined(OS_WIN) | |
1195 local_data_path = local_data_path.NormalizeWindowsPathSeparators(); | |
1196 #endif | |
1197 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | |
1198 base::PlatformFileInfo ofsfu_file_info; | |
1199 FilePath data_path; | |
1200 SCOPED_TRACE(testing::Message() << "Path is " << test_case.path); | |
1201 EXPECT_EQ(base::PLATFORM_FILE_OK, | |
1202 ofsfu()->GetFileInfo(context.get(), FilePath(test_case.path), | |
1203 &ofsfu_file_info, &data_path)); | |
1204 if (test_case.is_directory) { | |
1205 EXPECT_TRUE(ofsfu_file_info.is_directory); | |
1206 } else { | |
1207 base::PlatformFileInfo platform_file_info; | |
1208 SCOPED_TRACE(testing::Message() << "local_data_path is " << | |
1209 local_data_path.value()); | |
1210 SCOPED_TRACE(testing::Message() << "data_path is " << data_path.value()); | |
1211 ASSERT_TRUE(file_util::GetFileInfo(local_data_path, &platform_file_info)); | |
1212 EXPECT_EQ(test_case.data_file_size, platform_file_info.size); | |
1213 EXPECT_FALSE(platform_file_info.is_directory); | |
1214 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | |
1215 EXPECT_EQ(local_data_path, data_path); | |
1216 EXPECT_EQ(platform_file_info.size, ofsfu_file_info.size); | |
1217 EXPECT_FALSE(ofsfu_file_info.is_directory); | |
1218 } | |
1219 } | |
1220 } | |
1221 | |
1222 TEST_F(ObfuscatedFileSystemFileUtilTest, TestOriginEnumerator) { | |
1223 scoped_ptr<ObfuscatedFileSystemFileUtil::AbstractOriginEnumerator> | |
1224 enumerator(ofsfu()->CreateOriginEnumerator()); | |
1225 // The test helper starts out with a single filesystem. | |
1226 EXPECT_TRUE(enumerator.get()); | |
1227 EXPECT_EQ(origin(), enumerator->Next()); | |
1228 ASSERT_TRUE(type() == kFileSystemTypeTemporary); | |
1229 EXPECT_TRUE(enumerator->HasFileSystemType(kFileSystemTypeTemporary)); | |
1230 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent)); | |
1231 EXPECT_EQ(GURL(), enumerator->Next()); | |
1232 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypeTemporary)); | |
1233 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent)); | |
1234 | |
1235 std::set<GURL> origins_expected; | |
1236 origins_expected.insert(origin()); | |
1237 | |
1238 for (size_t i = 0; i < arraysize(kOriginEnumerationTestRecords); ++i) { | |
1239 SCOPED_TRACE(testing::Message() << | |
1240 "Validating kOriginEnumerationTestRecords " << i); | |
1241 const OriginEnumerationTestRecord& record = | |
1242 kOriginEnumerationTestRecords[i]; | |
1243 GURL origin_url(record.origin_url); | |
1244 origins_expected.insert(origin_url); | |
1245 if (record.has_temporary) { | |
1246 scoped_ptr<FileSystemTestOriginHelper> helper( | |
1247 NewHelper(origin_url, kFileSystemTypeTemporary)); | |
1248 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get())); | |
1249 context->set_src_origin_url(origin_url); | |
1250 context->set_src_type(kFileSystemTypeTemporary); | |
1251 bool created = false; | |
1252 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
1253 ofsfu()->EnsureFileExists(context.get(), | |
1254 FilePath().AppendASCII("file"), &created)); | |
1255 EXPECT_TRUE(created); | |
1256 } | |
1257 if (record.has_persistent) { | |
1258 scoped_ptr<FileSystemTestOriginHelper> helper( | |
1259 NewHelper(origin_url, kFileSystemTypePersistent)); | |
1260 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get())); | |
1261 context->set_src_origin_url(origin_url); | |
1262 context->set_src_type(kFileSystemTypePersistent); | |
1263 bool created = false; | |
1264 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
1265 ofsfu()->EnsureFileExists(context.get(), | |
1266 FilePath().AppendASCII("file"), &created)); | |
1267 EXPECT_TRUE(created); | |
1268 } | |
1269 } | |
1270 enumerator.reset(ofsfu()->CreateOriginEnumerator()); | |
1271 EXPECT_TRUE(enumerator.get()); | |
1272 std::set<GURL> origins_found; | |
1273 GURL origin_url; | |
1274 while (!(origin_url = enumerator->Next()).is_empty()) { | |
1275 origins_found.insert(origin_url); | |
1276 SCOPED_TRACE(testing::Message() << "Handling " << origin_url.spec()); | |
1277 bool found = false; | |
1278 for (size_t i = 0; !found && i < arraysize(kOriginEnumerationTestRecords); | |
1279 ++i) { | |
1280 const OriginEnumerationTestRecord& record = | |
1281 kOriginEnumerationTestRecords[i]; | |
1282 if (GURL(record.origin_url) != origin_url) | |
1283 continue; | |
1284 found = true; | |
1285 EXPECT_EQ(record.has_temporary, | |
1286 enumerator->HasFileSystemType(kFileSystemTypeTemporary)); | |
1287 EXPECT_EQ(record.has_persistent, | |
1288 enumerator->HasFileSystemType(kFileSystemTypePersistent)); | |
1289 } | |
1290 // Deal with the default filesystem created by the test helper. | |
1291 if (!found && origin_url == origin()) { | |
1292 ASSERT_TRUE(type() == kFileSystemTypeTemporary); | |
1293 EXPECT_EQ(true, | |
1294 enumerator->HasFileSystemType(kFileSystemTypeTemporary)); | |
1295 EXPECT_EQ(false, | |
1296 enumerator->HasFileSystemType(kFileSystemTypePersistent)); | |
1297 found = true; | |
1298 } | |
1299 EXPECT_TRUE(found); | |
1300 } | |
1301 | |
1302 std::set<GURL> diff; | |
1303 std::set_symmetric_difference(origins_expected.begin(), | |
1304 origins_expected.end(), origins_found.begin(), origins_found.end(), | |
1305 inserter(diff, diff.begin())); | |
1306 EXPECT_TRUE(diff.empty()); | |
1307 } | |
1308 | |
1309 TEST_F(ObfuscatedFileSystemFileUtilTest, TestRevokeUsageCache) { | |
1310 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | |
1311 | |
1312 int64 expected_quota = 0; | |
1313 | |
1314 for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) { | |
1315 SCOPED_TRACE(testing::Message() << "Creating kMigrationTestPath " << i); | |
1316 const MigrationTestCaseRecord& test_case = kMigrationTestCases[i]; | |
1317 FilePath path(test_case.path); | |
1318 expected_quota += ObfuscatedFileSystemFileUtil::ComputeFilePathCost(path); | |
1319 if (test_case.is_directory) { | |
1320 bool exclusive = true; | |
1321 bool recursive = false; | |
1322 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
1323 ofsfu()->CreateDirectory(context.get(), path, exclusive, recursive)); | |
1324 } else { | |
1325 bool created = false; | |
1326 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
1327 ofsfu()->EnsureFileExists(context.get(), path, &created)); | |
1328 ASSERT_TRUE(created); | |
1329 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
1330 ofsfu()->Truncate(context.get(), path, | |
1331 test_case.data_file_size)); | |
1332 expected_quota += test_case.data_file_size; | |
1333 } | |
1334 } | |
1335 EXPECT_EQ(expected_quota, SizeInUsageFile()); | |
1336 RevokeUsageCache(); | |
1337 EXPECT_EQ(-1, SizeInUsageFile()); | |
1338 GetUsageFromQuotaManager(); | |
1339 EXPECT_EQ(expected_quota, SizeInUsageFile()); | |
1340 EXPECT_EQ(expected_quota, usage()); | |
1341 } | |
OLD | NEW |