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

Side by Side Diff: webkit/fileapi/obfuscated_file_system_file_util_unittest.cc

Issue 7470037: [Refactor] to rename and re-layer the file_util stack layers. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Fixed for ChromeOS again. Created 9 years, 4 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
(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/platform_file.h"
13 #include "base/scoped_temp_dir.h"
14 #include "base/sys_string_conversions.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "webkit/fileapi/file_system_context.h"
17 #include "webkit/fileapi/file_system_operation_context.h"
18 #include "webkit/fileapi/file_system_test_helper.h"
19 #include "webkit/fileapi/obfuscated_file_system_file_util.h"
20
21 using namespace fileapi;
22
23 namespace {
24
25 FilePath UTF8ToFilePath(const std::string& str) {
26 FilePath::StringType result;
27 #if defined(OS_POSIX)
28 result = str;
29 #elif defined(OS_WIN)
30 result = base::SysUTF8ToWide(str);
31 #endif
32 return FilePath(result);
33 }
34
35 bool FileExists(const FilePath& path) {
36 return file_util::PathExists(path) && !file_util::DirectoryExists(path);
37 }
38
39 int64 GetSize(const FilePath& path) {
40 int64 size;
41 EXPECT_TRUE(file_util::GetFileSize(path, &size));
42 return size;
43 }
44
45 // After a move, the dest exists and the source doesn't.
46 // After a copy, both source and dest exist.
47 struct CopyMoveTestCaseRecord {
48 bool is_copy_not_move;
49 const char source_path[64];
50 const char dest_path[64];
51 bool cause_overwrite;
52 };
53
54 const CopyMoveTestCaseRecord kCopyMoveTestCases[] = {
55 // This is the combinatoric set of:
56 // rename vs. same-name
57 // different directory vs. same directory
58 // overwrite vs. no-overwrite
59 // copy vs. move
60 // We can never be called with source and destination paths identical, so
61 // those cases are omitted.
62 {true, "dir0/file0", "dir0/file1", false},
63 {false, "dir0/file0", "dir0/file1", false},
64 {true, "dir0/file0", "dir0/file1", true},
65 {false, "dir0/file0", "dir0/file1", true},
66
67 {true, "dir0/file0", "dir1/file0", false},
68 {false, "dir0/file0", "dir1/file0", false},
69 {true, "dir0/file0", "dir1/file0", true},
70 {false, "dir0/file0", "dir1/file0", true},
71 {true, "dir0/file0", "dir1/file1", false},
72 {false, "dir0/file0", "dir1/file1", false},
73 {true, "dir0/file0", "dir1/file1", true},
74 {false, "dir0/file0", "dir1/file1", true},
75 };
76
77 struct MigrationTestCaseRecord {
78 bool is_directory;
79 const FilePath::CharType path[64];
80 int64 data_file_size;
81 };
82
83 const MigrationTestCaseRecord kMigrationTestCases[] = {
84 {true, FILE_PATH_LITERAL("dir a"), 0},
85 {true, FILE_PATH_LITERAL("dir a/dir a"), 0},
86 {true, FILE_PATH_LITERAL("dir a/dir d"), 0},
87 {true, FILE_PATH_LITERAL("dir a/dir d/dir e"), 0},
88 {true, FILE_PATH_LITERAL("dir a/dir d/dir e/dir f"), 0},
89 {true, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g"), 0},
90 {true, FILE_PATH_LITERAL("dir a/dir d/dir e/dir h"), 0},
91 {true, FILE_PATH_LITERAL("dir b"), 0},
92 {true, FILE_PATH_LITERAL("dir b/dir a"), 0},
93 {true, FILE_PATH_LITERAL("dir c"), 0},
94 {false, FILE_PATH_LITERAL("file 0"), 38},
95 {false, FILE_PATH_LITERAL("file 2"), 60},
96 {false, FILE_PATH_LITERAL("file 3"), 0},
97 {false, FILE_PATH_LITERAL("dir a/file 0"), 39},
98 {false, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g/file 0"), 40},
99 {false, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g/file 1"), 41},
100 {false, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g/file 2"), 42},
101 {false, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g/file 3"), 50},
102 };
103
104 struct OriginEnumerationTestRecord {
105 std::string origin_url;
106 bool has_temporary;
107 bool has_persistent;
108 };
109
110 const OriginEnumerationTestRecord kOriginEnumerationTestRecords[] = {
111 {"http://example.com", false, true},
112 {"http://example1.com", true, false},
113 {"https://example1.com", true, true},
114 {"file://", false, true},
115 {"http://example.com:8000", false, true},
116 };
117
118 } // namespace (anonymous)
119
120 // TODO(ericu): The vast majority of this and the other FSFU subclass tests
121 // could theoretically be shared. It would basically be a FSFU interface
122 // compliance test, and only the subclass-specific bits that look into the
123 // implementation would need to be written per-subclass.
124 class ObfuscatedFileSystemFileUtilTest : public testing::Test {
125 public:
126 ObfuscatedFileSystemFileUtilTest()
127 : origin_(GURL("http://www.example.com")),
128 type_(kFileSystemTypeTemporary),
129 test_helper_(origin_, type_) {
130 }
131
132 void SetUp() {
133 ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
134
135 obfuscated_file_system_file_util_ =
136 new ObfuscatedFileSystemFileUtil(data_dir_.path(),
137 new FileSystemFileUtil());
138 test_helper_.SetUp(data_dir_.path(),
139 false, // incognito
140 false, // unlimited quota
141 NULL, // quota::QuotaManagerProxy
142 obfuscated_file_system_file_util_.get());
143 }
144
145 FileSystemOperationContext* NewContext() {
146 FileSystemOperationContext* context = test_helper_.NewOperationContext();
147 return context;
148 }
149
150 ObfuscatedFileSystemFileUtil* ofsfu() {
151 return obfuscated_file_system_file_util_.get();
152 }
153
154 const FilePath& test_directory() const {
155 return data_dir_.path();
156 }
157
158 const GURL& origin_url() const {
159 return origin_;
160 }
161
162 fileapi::FileSystemType type() const {
163 return type_;
164 }
165
166 void CheckFileAndCloseHandle(
167 const FilePath& virtual_path, PlatformFile file_handle) {
168 scoped_ptr<FileSystemOperationContext> context(NewContext());
169 FilePath local_path;
170 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetLocalFilePath(
171 context.get(), virtual_path, &local_path));
172
173 base::PlatformFileInfo file_info0;
174 FilePath data_path;
175 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo(
176 context.get(), virtual_path, &file_info0, &data_path));
177 EXPECT_EQ(data_path, local_path);
178 EXPECT_TRUE(FileExists(data_path));
179 EXPECT_EQ(0, GetSize(data_path));
180
181 const char data[] = "test data";
182 const int length = arraysize(data) - 1;
183
184 if (base::kInvalidPlatformFileValue == file_handle) {
185 bool created = true;
186 PlatformFileError error;
187 file_handle = base::CreatePlatformFile(
188 data_path,
189 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
190 &created,
191 &error);
192 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
193 ASSERT_EQ(base::PLATFORM_FILE_OK, error);
194 EXPECT_FALSE(created);
195 }
196 ASSERT_EQ(length, base::WritePlatformFile(file_handle, 0, data, length));
197 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
198
199 base::PlatformFileInfo file_info1;
200 EXPECT_EQ(length, GetSize(data_path));
201 context.reset(NewContext());
202 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo(
203 context.get(), virtual_path, &file_info1, &data_path));
204 EXPECT_EQ(data_path, local_path);
205
206 EXPECT_FALSE(file_info0.is_directory);
207 EXPECT_FALSE(file_info1.is_directory);
208 EXPECT_FALSE(file_info0.is_symbolic_link);
209 EXPECT_FALSE(file_info1.is_symbolic_link);
210 EXPECT_EQ(0, file_info0.size);
211 EXPECT_EQ(length, file_info1.size);
212 EXPECT_LE(file_info0.last_modified, file_info1.last_modified);
213
214 context.reset(NewContext());
215 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->Truncate(
216 context.get(), virtual_path, length * 2));
217 EXPECT_EQ(length * 2, GetSize(data_path));
218
219 context.reset(NewContext());
220 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->Truncate(
221 context.get(), virtual_path, 1));
222 EXPECT_EQ(1, GetSize(data_path));
223 }
224
225 void ValidateTestDirectory(
226 const FilePath& root_path,
227 const std::set<FilePath::StringType>& files,
228 const std::set<FilePath::StringType>& directories) {
229 scoped_ptr<FileSystemOperationContext> context;
230 std::set<FilePath::StringType>::const_iterator iter;
231 for (iter = files.begin(); iter != files.end(); ++iter) {
232 bool created = true;
233 context.reset(NewContext());
234 ASSERT_EQ(base::PLATFORM_FILE_OK,
235 ofsfu()->EnsureFileExists(
236 context.get(), root_path.Append(*iter),
237 &created));
238 ASSERT_FALSE(created);
239 }
240 for (iter = directories.begin(); iter != directories.end(); ++iter) {
241 context.reset(NewContext());
242 EXPECT_TRUE(ofsfu()->DirectoryExists(context.get(),
243 root_path.Append(*iter)));
244 }
245 }
246
247 void FillTestDirectory(
248 const FilePath& root_path,
249 std::set<FilePath::StringType>* files,
250 std::set<FilePath::StringType>* directories) {
251 scoped_ptr<FileSystemOperationContext> context;
252 context.reset(NewContext());
253 std::vector<base::FileUtilProxy::Entry> entries;
254 EXPECT_EQ(base::PLATFORM_FILE_OK,
255 ofsfu()->ReadDirectory(context.get(), root_path, &entries));
256 EXPECT_EQ(0UL, entries.size());
257
258 files->clear();
259 files->insert(FILE_PATH_LITERAL("first"));
260 files->insert(FILE_PATH_LITERAL("second"));
261 files->insert(FILE_PATH_LITERAL("third"));
262 directories->clear();
263 directories->insert(FILE_PATH_LITERAL("fourth"));
264 directories->insert(FILE_PATH_LITERAL("fifth"));
265 directories->insert(FILE_PATH_LITERAL("sixth"));
266 std::set<FilePath::StringType>::iterator iter;
267 for (iter = files->begin(); iter != files->end(); ++iter) {
268 bool created = false;
269 context.reset(NewContext());
270 ASSERT_EQ(base::PLATFORM_FILE_OK,
271 ofsfu()->EnsureFileExists(
272 context.get(), root_path.Append(*iter), &created));
273 ASSERT_TRUE(created);
274 }
275 for (iter = directories->begin(); iter != directories->end(); ++iter) {
276 bool exclusive = true;
277 bool recursive = false;
278 context.reset(NewContext());
279 EXPECT_EQ(base::PLATFORM_FILE_OK,
280 ofsfu()->CreateDirectory(
281 context.get(), root_path.Append(*iter), exclusive, recursive));
282 }
283 ValidateTestDirectory(root_path, *files, *directories);
284 }
285
286 void TestReadDirectoryHelper(const FilePath& root_path) {
287 std::set<FilePath::StringType> files;
288 std::set<FilePath::StringType> directories;
289 FillTestDirectory(root_path, &files, &directories);
290
291 scoped_ptr<FileSystemOperationContext> context;
292 std::vector<base::FileUtilProxy::Entry> entries;
293 context.reset(NewContext());
294 EXPECT_EQ(base::PLATFORM_FILE_OK,
295 ofsfu()->ReadDirectory(context.get(), root_path, &entries));
296 std::vector<base::FileUtilProxy::Entry>::iterator entry_iter;
297 EXPECT_EQ(files.size() + directories.size(), entries.size());
298 for (entry_iter = entries.begin(); entry_iter != entries.end();
299 ++entry_iter) {
300 const base::FileUtilProxy::Entry& entry = *entry_iter;
301 std::set<FilePath::StringType>::iterator iter = files.find(entry.name);
302 if (iter != files.end()) {
303 EXPECT_FALSE(entry.is_directory);
304 files.erase(iter);
305 continue;
306 }
307 iter = directories.find(entry.name);
308 EXPECT_FALSE(directories.end() == iter);
309 EXPECT_TRUE(entry.is_directory);
310 directories.erase(iter);
311 }
312 }
313
314 void TestTouchHelper(const FilePath& path) {
315 base::Time last_access_time = base::Time::Now(); // Ignored, so not tested.
316 base::Time last_modified_time = base::Time::Now();
317 scoped_ptr<FileSystemOperationContext> context(NewContext());
318 EXPECT_EQ(base::PLATFORM_FILE_OK,
319 ofsfu()->Touch(
320 context.get(), path, last_access_time, last_modified_time));
321 FilePath local_path;
322 base::PlatformFileInfo file_info;
323 context.reset(NewContext());
324 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo(
325 context.get(), path, &file_info, &local_path));
326 // We compare as time_t here to lower our resolution, to avoid false
327 // negatives caused by conversion to the local filesystem's native
328 // representation and back.
329 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
330
331 context.reset(NewContext());
332 last_modified_time += base::TimeDelta::FromHours(1);
333 EXPECT_EQ(base::PLATFORM_FILE_OK,
334 ofsfu()->Touch(
335 context.get(), path, last_access_time, last_modified_time));
336 context.reset(NewContext());
337 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo(
338 context.get(), path, &file_info, &local_path));
339 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
340 }
341
342 void TestCopyInForeignFileHelper(bool overwrite) {
343 ScopedTempDir source_dir;
344 ASSERT_TRUE(source_dir.CreateUniqueTempDir());
345 FilePath root_path = source_dir.path();
346 FilePath src_path = root_path.AppendASCII("file_name");
347 FilePath dest_path(FILE_PATH_LITERAL("new file"));
348 int64 src_file_length = 87;
349
350 base::PlatformFileError error_code;
351 bool created = false;
352 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
353 base::PlatformFile file_handle =
354 base::CreatePlatformFile(
355 src_path, file_flags, &created, &error_code);
356 EXPECT_TRUE(created);
357 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code);
358 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
359 ASSERT_TRUE(base::TruncatePlatformFile(file_handle, src_file_length));
360 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
361
362 scoped_ptr<FileSystemOperationContext> context;
363
364 if (overwrite) {
365 context.reset(NewContext());
366 EXPECT_EQ(base::PLATFORM_FILE_OK,
367 ofsfu()->EnsureFileExists(context.get(), dest_path, &created));
368 EXPECT_TRUE(created);
369 }
370
371 context.reset(NewContext());
372 EXPECT_EQ(base::PLATFORM_FILE_OK,
373 ofsfu()->CopyInForeignFile(context.get(), src_path, dest_path));
374 context.reset(NewContext());
375 EXPECT_TRUE(ofsfu()->PathExists(context.get(), dest_path));
376 context.reset(NewContext());
377 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), dest_path));
378 context.reset(NewContext());
379 base::PlatformFileInfo file_info;
380 FilePath data_path;
381 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo(
382 context.get(), dest_path, &file_info, &data_path));
383 EXPECT_NE(data_path, src_path);
384 EXPECT_TRUE(FileExists(data_path));
385 EXPECT_EQ(src_file_length, GetSize(data_path));
386
387 EXPECT_EQ(base::PLATFORM_FILE_OK,
388 ofsfu()->DeleteFile(context.get(), dest_path));
389 }
390
391 private:
392 ScopedTempDir data_dir_;
393 scoped_refptr<ObfuscatedFileSystemFileUtil> obfuscated_file_system_file_util_;
394 GURL origin_;
395 fileapi::FileSystemType type_;
396 FileSystemTestOriginHelper test_helper_;
397
398 DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileSystemFileUtilTest);
399 };
400
401 TEST_F(ObfuscatedFileSystemFileUtilTest, TestCreateAndDeleteFile) {
402 base::PlatformFile file_handle = base::kInvalidPlatformFileValue;
403 bool created;
404 FilePath path = UTF8ToFilePath("fake/file");
405 scoped_ptr<FileSystemOperationContext> context(NewContext());
406 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
407
408 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
409 ofsfu()->CreateOrOpen(
410 context.get(), path, file_flags, &file_handle,
411 &created));
412
413 context.reset(NewContext());
414 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
415 ofsfu()->DeleteFile(context.get(), path));
416
417 path = UTF8ToFilePath("test file");
418
419 context.reset(NewContext());
420 ASSERT_EQ(base::PLATFORM_FILE_OK,
421 ofsfu()->CreateOrOpen(
422 context.get(), path, file_flags, &file_handle, &created));
423 ASSERT_TRUE(created);
424 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
425
426 CheckFileAndCloseHandle(path, file_handle);
427
428 context.reset(NewContext());
429 FilePath local_path;
430 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetLocalFilePath(
431 context.get(), path, &local_path));
432 EXPECT_TRUE(file_util::PathExists(local_path));
433
434 context.reset(NewContext());
435 EXPECT_EQ(base::PLATFORM_FILE_OK,
436 ofsfu()->DeleteFile(context.get(), path));
437 EXPECT_FALSE(file_util::PathExists(local_path));
438
439 context.reset(NewContext());
440 bool exclusive = true;
441 bool recursive = true;
442 FilePath directory_path = UTF8ToFilePath("series/of/directories");
443 path = directory_path.AppendASCII("file name");
444 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory(
445 context.get(), directory_path, exclusive, recursive));
446
447 context.reset(NewContext());
448 file_handle = base::kInvalidPlatformFileValue;
449 ASSERT_EQ(base::PLATFORM_FILE_OK,
450 ofsfu()->CreateOrOpen(
451 context.get(), path, file_flags, &file_handle, &created));
452 ASSERT_TRUE(created);
453 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
454
455 CheckFileAndCloseHandle(path, file_handle);
456
457 context.reset(NewContext());
458 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetLocalFilePath(
459 context.get(), path, &local_path));
460 EXPECT_TRUE(file_util::PathExists(local_path));
461
462 context.reset(NewContext());
463 EXPECT_EQ(base::PLATFORM_FILE_OK,
464 ofsfu()->DeleteFile(context.get(), path));
465 EXPECT_FALSE(file_util::PathExists(local_path));
466 }
467
468 TEST_F(ObfuscatedFileSystemFileUtilTest, TestTruncate) {
469 bool created = false;
470 FilePath path = UTF8ToFilePath("file");
471 scoped_ptr<FileSystemOperationContext> context(NewContext());
472
473 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
474 ofsfu()->Truncate(context.get(), path, 4));
475
476 context.reset(NewContext());
477 ASSERT_EQ(base::PLATFORM_FILE_OK,
478 ofsfu()->EnsureFileExists(context.get(), path, &created));
479 ASSERT_TRUE(created);
480
481 context.reset(NewContext());
482 FilePath local_path;
483 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetLocalFilePath(
484 context.get(), path, &local_path));
485 EXPECT_EQ(0, GetSize(local_path));
486
487 context.reset(NewContext());
488 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->Truncate(
489 context.get(), path, 10));
490 EXPECT_EQ(10, GetSize(local_path));
491
492 context.reset(NewContext());
493 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->Truncate(
494 context.get(), path, 1));
495 EXPECT_EQ(1, GetSize(local_path));
496
497 context.reset(NewContext());
498 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), path));
499 context.reset(NewContext());
500 EXPECT_TRUE(ofsfu()->PathExists(context.get(), path));
501 }
502
503 TEST_F(ObfuscatedFileSystemFileUtilTest, TestEnsureFileExists) {
504 FilePath path = UTF8ToFilePath("fake/file");
505 bool created = false;
506 scoped_ptr<FileSystemOperationContext> context(NewContext());
507 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
508 ofsfu()->EnsureFileExists(
509 context.get(), path, &created));
510
511 context.reset(NewContext());
512 path = UTF8ToFilePath("test file");
513 created = false;
514 ASSERT_EQ(base::PLATFORM_FILE_OK,
515 ofsfu()->EnsureFileExists(context.get(), path, &created));
516 ASSERT_TRUE(created);
517
518 CheckFileAndCloseHandle(path, base::kInvalidPlatformFileValue);
519
520 context.reset(NewContext());
521 ASSERT_EQ(base::PLATFORM_FILE_OK,
522 ofsfu()->EnsureFileExists(context.get(), path, &created));
523 ASSERT_FALSE(created);
524
525 // Also test in a subdirectory.
526 path = UTF8ToFilePath("path/to/file.txt");
527 context.reset(NewContext());
528 bool exclusive = true;
529 bool recursive = true;
530 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory(
531 context.get(), path.DirName(), exclusive, recursive));
532
533 context.reset(NewContext());
534 ASSERT_EQ(base::PLATFORM_FILE_OK,
535 ofsfu()->EnsureFileExists(context.get(), path, &created));
536 ASSERT_TRUE(created);
537 context.reset(NewContext());
538 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), path));
539 context.reset(NewContext());
540 EXPECT_TRUE(ofsfu()->PathExists(context.get(), path));
541 }
542
543 TEST_F(ObfuscatedFileSystemFileUtilTest, TestDirectoryOps) {
544 scoped_ptr<FileSystemOperationContext> context(NewContext());
545
546 bool exclusive = false;
547 bool recursive = false;
548 FilePath path = UTF8ToFilePath("foo/bar");
549 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofsfu()->CreateDirectory(
550 context.get(), path, exclusive, recursive));
551
552 context.reset(NewContext());
553 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
554 ofsfu()->DeleteSingleDirectory(context.get(), path));
555
556 FilePath root = UTF8ToFilePath("");
557 context.reset(NewContext());
558 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), path));
559 context.reset(NewContext());
560 EXPECT_FALSE(ofsfu()->PathExists(context.get(), path));
561 context.reset(NewContext());
562 EXPECT_TRUE(ofsfu()->IsDirectoryEmpty(context.get(), root));
563
564 context.reset(NewContext());
565 exclusive = false;
566 recursive = true;
567 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory(
568 context.get(), path, exclusive, recursive));
569
570 context.reset(NewContext());
571 EXPECT_TRUE(ofsfu()->DirectoryExists(context.get(), path));
572 context.reset(NewContext());
573 EXPECT_TRUE(ofsfu()->PathExists(context.get(), path));
574 context.reset(NewContext());
575 EXPECT_FALSE(ofsfu()->IsDirectoryEmpty(context.get(), root));
576 context.reset(NewContext());
577 EXPECT_TRUE(ofsfu()->DirectoryExists(context.get(), path.DirName()));
578 context.reset(NewContext());
579 EXPECT_FALSE(ofsfu()->IsDirectoryEmpty(context.get(), path.DirName()));
580
581 // Can't remove a non-empty directory.
582 context.reset(NewContext());
583 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY,
584 ofsfu()->DeleteSingleDirectory(context.get(), path.DirName()));
585
586 base::PlatformFileInfo file_info;
587 FilePath local_path;
588 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo(
589 context.get(), path, &file_info, &local_path));
590 EXPECT_TRUE(local_path.empty());
591 EXPECT_TRUE(file_info.is_directory);
592 EXPECT_FALSE(file_info.is_symbolic_link);
593
594 // Same create again should succeed, since exclusive is false.
595 context.reset(NewContext());
596 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory(
597 context.get(), path, exclusive, recursive));
598
599 exclusive = true;
600 recursive = true;
601 context.reset(NewContext());
602 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofsfu()->CreateDirectory(
603 context.get(), path, exclusive, recursive));
604
605 context.reset(NewContext());
606 EXPECT_EQ(base::PLATFORM_FILE_OK,
607 ofsfu()->DeleteSingleDirectory(context.get(), path));
608
609 path = UTF8ToFilePath("foo/bop");
610
611 context.reset(NewContext());
612 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), path));
613 context.reset(NewContext());
614 EXPECT_FALSE(ofsfu()->PathExists(context.get(), path));
615 context.reset(NewContext());
616 EXPECT_TRUE(ofsfu()->IsDirectoryEmpty(context.get(), path));
617 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofsfu()->GetFileInfo(
618 context.get(), path, &file_info, &local_path));
619
620 exclusive = true;
621 recursive = false;
622 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory(
623 context.get(), path, exclusive, recursive));
624
625 context.reset(NewContext());
626 EXPECT_TRUE(ofsfu()->DirectoryExists(context.get(), path));
627 context.reset(NewContext());
628 EXPECT_TRUE(ofsfu()->PathExists(context.get(), path));
629
630 exclusive = true;
631 recursive = false;
632 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofsfu()->CreateDirectory(
633 context.get(), path, exclusive, recursive));
634
635 exclusive = true;
636 recursive = false;
637 path = UTF8ToFilePath("foo");
638 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofsfu()->CreateDirectory(
639 context.get(), path, exclusive, recursive));
640
641 path = UTF8ToFilePath("blah");
642
643 context.reset(NewContext());
644 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), path));
645 context.reset(NewContext());
646 EXPECT_FALSE(ofsfu()->PathExists(context.get(), path));
647
648 exclusive = true;
649 recursive = false;
650 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory(
651 context.get(), path, exclusive, recursive));
652
653 context.reset(NewContext());
654 EXPECT_TRUE(ofsfu()->DirectoryExists(context.get(), path));
655 context.reset(NewContext());
656 EXPECT_TRUE(ofsfu()->PathExists(context.get(), path));
657
658 exclusive = true;
659 recursive = false;
660 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofsfu()->CreateDirectory(
661 context.get(), path, exclusive, recursive));
662 }
663
664 TEST_F(ObfuscatedFileSystemFileUtilTest, TestReadDirectory) {
665 scoped_ptr<FileSystemOperationContext> context(NewContext());
666 bool exclusive = true;
667 bool recursive = true;
668 FilePath path = UTF8ToFilePath("directory/to/use");
669 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory(
670 context.get(), path, exclusive, recursive));
671 TestReadDirectoryHelper(path);
672 }
673
674 TEST_F(ObfuscatedFileSystemFileUtilTest, TestReadRootWithSlash) {
675 TestReadDirectoryHelper(UTF8ToFilePath(""));
676 }
677
678 TEST_F(ObfuscatedFileSystemFileUtilTest, TestReadRootWithEmptyString) {
679 TestReadDirectoryHelper(UTF8ToFilePath("/"));
680 }
681
682 TEST_F(ObfuscatedFileSystemFileUtilTest, TestReadDirectoryOnFile) {
683 FilePath path = UTF8ToFilePath("file");
684 scoped_ptr<FileSystemOperationContext> context(NewContext());
685
686 bool created = false;
687 ASSERT_EQ(base::PLATFORM_FILE_OK,
688 ofsfu()->EnsureFileExists(context.get(), path, &created));
689 ASSERT_TRUE(created);
690
691 context.reset(NewContext());
692 std::vector<base::FileUtilProxy::Entry> entries;
693 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
694 ofsfu()->ReadDirectory(context.get(), path, &entries));
695
696 EXPECT_TRUE(ofsfu()->IsDirectoryEmpty(context.get(), path));
697 }
698
699 TEST_F(ObfuscatedFileSystemFileUtilTest, TestTouch) {
700 FilePath path = UTF8ToFilePath("fake/file");
701 base::Time last_access_time = base::Time::Now(); // Ignored, so not tested.
702 base::Time last_modified_time = base::Time::Now();
703 scoped_ptr<FileSystemOperationContext> context(NewContext());
704 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
705 ofsfu()->Touch(
706 context.get(), path, last_access_time, last_modified_time));
707
708 // Touch will create a file if it's not there but its parent is.
709 path = UTF8ToFilePath("file name");
710 TestTouchHelper(path);
711
712 bool exclusive = true;
713 bool recursive = true;
714 path = UTF8ToFilePath("directory/to/use");
715 context.reset(NewContext());
716 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory(
717 context.get(), path, exclusive, recursive));
718 TestTouchHelper(path);
719 }
720
721 TEST_F(ObfuscatedFileSystemFileUtilTest, TestCopyOrMoveFileNotFound) {
722 FilePath source_path = UTF8ToFilePath("path0.txt");
723 FilePath dest_path = UTF8ToFilePath("path1.txt");
724 scoped_ptr<FileSystemOperationContext> context(NewContext());
725
726 bool is_copy_not_move = false;
727 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
728 ofsfu()->CopyOrMoveFile(context.get(), source_path, dest_path,
729 is_copy_not_move));
730 context.reset(NewContext());
731 is_copy_not_move = true;
732 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
733 ofsfu()->CopyOrMoveFile(context.get(), source_path, dest_path,
734 is_copy_not_move));
735 source_path = UTF8ToFilePath("dir/dir/file");
736 bool exclusive = true;
737 bool recursive = true;
738 context.reset(NewContext());
739 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory(
740 context.get(), source_path.DirName(), exclusive, recursive));
741 is_copy_not_move = false;
742 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
743 ofsfu()->CopyOrMoveFile(context.get(), source_path, dest_path,
744 is_copy_not_move));
745 context.reset(NewContext());
746 is_copy_not_move = true;
747 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
748 ofsfu()->CopyOrMoveFile(context.get(), source_path, dest_path,
749 is_copy_not_move));
750 }
751
752 TEST_F(ObfuscatedFileSystemFileUtilTest, TestCopyOrMoveFileSuccess) {
753 const int64 kSourceLength = 5;
754 const int64 kDestLength = 50;
755
756 for (size_t i = 0; i < arraysize(kCopyMoveTestCases); ++i) {
757 SCOPED_TRACE(testing::Message() << "kCopyMoveTestCase " << i);
758 const CopyMoveTestCaseRecord& test_case = kCopyMoveTestCases[i];
759 SCOPED_TRACE(testing::Message() << "\t is_copy_not_move " <<
760 test_case.is_copy_not_move);
761 SCOPED_TRACE(testing::Message() << "\t source_path " <<
762 test_case.source_path);
763 SCOPED_TRACE(testing::Message() << "\t dest_path " <<
764 test_case.dest_path);
765 SCOPED_TRACE(testing::Message() << "\t cause_overwrite " <<
766 test_case.cause_overwrite);
767 scoped_ptr<FileSystemOperationContext> context(NewContext());
768
769 bool exclusive = false;
770 bool recursive = true;
771 FilePath source_path = UTF8ToFilePath(test_case.source_path);
772 FilePath dest_path = UTF8ToFilePath(test_case.dest_path);
773
774 context.reset(NewContext());
775 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory(
776 context.get(), source_path.DirName(), exclusive, recursive));
777 context.reset(NewContext());
778 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory(
779 context.get(), dest_path.DirName(), exclusive, recursive));
780
781 bool created = false;
782 context.reset(NewContext());
783 ASSERT_EQ(base::PLATFORM_FILE_OK,
784 ofsfu()->EnsureFileExists(context.get(), source_path, &created));
785 ASSERT_TRUE(created);
786 context.reset(NewContext());
787 ASSERT_EQ(base::PLATFORM_FILE_OK,
788 ofsfu()->Truncate(context.get(), source_path, kSourceLength));
789
790 if (test_case.cause_overwrite) {
791 context.reset(NewContext());
792 created = false;
793 ASSERT_EQ(base::PLATFORM_FILE_OK,
794 ofsfu()->EnsureFileExists(context.get(), dest_path, &created));
795 ASSERT_TRUE(created);
796 context.reset(NewContext());
797 ASSERT_EQ(base::PLATFORM_FILE_OK,
798 ofsfu()->Truncate(context.get(), dest_path, kDestLength));
799 }
800
801 context.reset(NewContext());
802 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CopyOrMoveFile(context.get(),
803 source_path, dest_path, test_case.is_copy_not_move));
804 if (test_case.is_copy_not_move) {
805 base::PlatformFileInfo file_info;
806 FilePath local_path;
807 context.reset(NewContext());
808 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo(
809 context.get(), source_path, &file_info, &local_path));
810 EXPECT_EQ(kSourceLength, file_info.size);
811 EXPECT_EQ(base::PLATFORM_FILE_OK,
812 ofsfu()->DeleteFile(context.get(), source_path));
813 } else {
814 base::PlatformFileInfo file_info;
815 FilePath local_path;
816 context.reset(NewContext());
817 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofsfu()->GetFileInfo(
818 context.get(), source_path, &file_info, &local_path));
819 }
820 base::PlatformFileInfo file_info;
821 FilePath local_path;
822 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo(
823 context.get(), dest_path, &file_info, &local_path));
824 EXPECT_EQ(kSourceLength, file_info.size);
825
826 EXPECT_EQ(base::PLATFORM_FILE_OK,
827 ofsfu()->DeleteFile(context.get(), dest_path));
828 }
829 }
830
831 TEST_F(ObfuscatedFileSystemFileUtilTest, TestCopyInForeignFile) {
832 TestCopyInForeignFileHelper(false /* overwrite */);
833 TestCopyInForeignFileHelper(true /* overwrite */);
834 }
835
836 TEST_F(ObfuscatedFileSystemFileUtilTest, TestEnumerator) {
837 scoped_ptr<FileSystemOperationContext> context(NewContext());
838 FilePath src_path = UTF8ToFilePath("source dir");
839 bool exclusive = true;
840 bool recursive = false;
841 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory(
842 context.get(), src_path, exclusive, recursive));
843
844 std::set<FilePath::StringType> files;
845 std::set<FilePath::StringType> directories;
846 FillTestDirectory(src_path, &files, &directories);
847
848 FilePath dest_path = UTF8ToFilePath("destination dir");
849
850 context.reset(NewContext());
851 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), dest_path));
852 context.reset(NewContext());
853 ASSERT_EQ(base::PLATFORM_FILE_OK,
854 ofsfu()->Copy(context.get(), src_path, dest_path));
855
856 ValidateTestDirectory(dest_path, files, directories);
857 context.reset(NewContext());
858 EXPECT_TRUE(ofsfu()->DirectoryExists(context.get(), src_path));
859 context.reset(NewContext());
860 EXPECT_TRUE(ofsfu()->DirectoryExists(context.get(), dest_path));
861 context.reset(NewContext());
862 recursive = true;
863 ASSERT_EQ(base::PLATFORM_FILE_OK,
864 ofsfu()->Delete(context.get(), dest_path, recursive));
865 context.reset(NewContext());
866 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), dest_path));
867 }
868
869 TEST_F(ObfuscatedFileSystemFileUtilTest, TestMigration) {
870 ScopedTempDir source_dir;
871 ASSERT_TRUE(source_dir.CreateUniqueTempDir());
872 FilePath root_path = source_dir.path().AppendASCII("chrome-pLmnMWXE7NzTFRsn");
873 ASSERT_TRUE(file_util::CreateDirectory(root_path));
874
875 for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) {
876 SCOPED_TRACE(testing::Message() << "Creating kMigrationTestPath " << i);
877 const MigrationTestCaseRecord& test_case = kMigrationTestCases[i];
878 FilePath local_src_path = root_path.Append(test_case.path);
879 if (test_case.is_directory) {
880 ASSERT_TRUE(
881 file_util::CreateDirectory(local_src_path));
882 } else {
883 base::PlatformFileError error_code;
884 bool created = false;
885 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
886 base::PlatformFile file_handle =
887 base::CreatePlatformFile(
888 local_src_path, file_flags, &created, &error_code);
889 EXPECT_TRUE(created);
890 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
891 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code);
892 ASSERT_TRUE(
893 base::TruncatePlatformFile(file_handle, test_case.data_file_size));
894 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
895 }
896 }
897
898 EXPECT_TRUE(ofsfu()->MigrateFromOldSandbox(origin_url(), type(), root_path));
899
900 FilePath new_root =
901 test_directory().AppendASCII("000").Append(
902 ofsfu()->GetDirectoryNameForType(type())).AppendASCII("Legacy");
903 for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) {
904 SCOPED_TRACE(testing::Message() << "Validating kMigrationTestPath " << i);
905 const MigrationTestCaseRecord& test_case = kMigrationTestCases[i];
906 FilePath local_data_path = new_root.Append(test_case.path);
907 #if defined(OS_WIN)
908 local_data_path = local_data_path.NormalizeWindowsPathSeparators();
909 #endif
910 scoped_ptr<FileSystemOperationContext> context(NewContext());
911 base::PlatformFileInfo ofsfu_file_info;
912 FilePath data_path;
913 SCOPED_TRACE(testing::Message() << "Path is " << test_case.path);
914 EXPECT_EQ(base::PLATFORM_FILE_OK,
915 ofsfu()->GetFileInfo(context.get(), FilePath(test_case.path),
916 &ofsfu_file_info, &data_path));
917 if (test_case.is_directory) {
918 EXPECT_TRUE(ofsfu_file_info.is_directory);
919 } else {
920 base::PlatformFileInfo platform_file_info;
921 SCOPED_TRACE(testing::Message() << "local_data_path is " <<
922 local_data_path.value());
923 SCOPED_TRACE(testing::Message() << "data_path is " << data_path.value());
924 ASSERT_TRUE(file_util::GetFileInfo(local_data_path, &platform_file_info));
925 EXPECT_EQ(test_case.data_file_size, platform_file_info.size);
926 EXPECT_FALSE(platform_file_info.is_directory);
927 scoped_ptr<FileSystemOperationContext> context(NewContext());
928 EXPECT_EQ(local_data_path, data_path);
929 EXPECT_EQ(platform_file_info.size, ofsfu_file_info.size);
930 EXPECT_FALSE(ofsfu_file_info.is_directory);
931 }
932 }
933 }
934
935 TEST_F(ObfuscatedFileSystemFileUtilTest, TestOriginEnumerator) {
936 scoped_ptr<ObfuscatedFileSystemFileUtil::AbstractOriginEnumerator>
937 enumerator(ofsfu()->CreateOriginEnumerator());
938 EXPECT_TRUE(enumerator.get());
939 EXPECT_EQ(GURL(), enumerator->Next());
940 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
941 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
942
943 std::set<GURL> origins_expected;
944
945 for (size_t i = 0; i < arraysize(kOriginEnumerationTestRecords); ++i) {
946 SCOPED_TRACE(testing::Message() <<
947 "Validating kOriginEnumerationTestRecords " << i);
948 const OriginEnumerationTestRecord& record =
949 kOriginEnumerationTestRecords[i];
950 GURL origin_url(record.origin_url);
951 origins_expected.insert(origin_url);
952 if (record.has_temporary) {
953 scoped_ptr<FileSystemOperationContext> context(NewContext());
954 context->set_src_origin_url(origin_url);
955 context->set_src_type(kFileSystemTypeTemporary);
956 bool created = false;
957 ASSERT_EQ(base::PLATFORM_FILE_OK,
958 ofsfu()->EnsureFileExists(context.get(),
959 FilePath().AppendASCII("file"), &created));
960 EXPECT_TRUE(created);
961 }
962 if (record.has_persistent) {
963 scoped_ptr<FileSystemOperationContext> context(NewContext());
964 context->set_src_origin_url(origin_url);
965 context->set_src_type(kFileSystemTypePersistent);
966 bool created = false;
967 ASSERT_EQ(base::PLATFORM_FILE_OK,
968 ofsfu()->EnsureFileExists(context.get(),
969 FilePath().AppendASCII("file"), &created));
970 EXPECT_TRUE(created);
971 }
972 }
973 enumerator.reset(ofsfu()->CreateOriginEnumerator());
974 EXPECT_TRUE(enumerator.get());
975 std::set<GURL> origins_found;
976 GURL origin;
977 while (!(origin = enumerator->Next()).is_empty()) {
978 origins_found.insert(origin);
979 SCOPED_TRACE(testing::Message() << "Handling " << origin.spec());
980 bool found = false;
981 for (size_t i = 0; !found && i < arraysize(kOriginEnumerationTestRecords);
982 ++i) {
983 const OriginEnumerationTestRecord& record =
984 kOriginEnumerationTestRecords[i];
985 if (GURL(record.origin_url) != origin)
986 continue;
987 found = true;
988 EXPECT_EQ(record.has_temporary,
989 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
990 EXPECT_EQ(record.has_persistent,
991 enumerator->HasFileSystemType(kFileSystemTypePersistent));
992 }
993 EXPECT_TRUE(found);
994 }
995
996 std::set<GURL> diff;
997 std::set_symmetric_difference(origins_expected.begin(),
998 origins_expected.end(), origins_found.begin(), origins_found.end(),
999 inserter(diff, diff.begin()));
1000 EXPECT_TRUE(diff.empty());
1001 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698