1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "webkit/fileapi/file_system_directory_database.h"
10 #include "base/file_util.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/platform_file.h"
14 #include "base/string_util.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/leveldatabase/src/include/leveldb/db.h"
18 #include "webkit/fileapi/file_system_database_test_helper.h"
19 #include "webkit/fileapi/file_system_util.h"
21 #define FPL(x) FILE_PATH_LITERAL(x)
26 const base::FilePath::CharType kDirectoryDatabaseName
[] = FPL("Paths");
29 class FileSystemDirectoryDatabaseTest
: public testing::Test
{
31 typedef FileSystemDirectoryDatabase::FileId FileId
;
32 typedef FileSystemDirectoryDatabase::FileInfo FileInfo
;
34 FileSystemDirectoryDatabaseTest() {
35 EXPECT_TRUE(base_
.CreateUniqueTempDir());
39 FileSystemDirectoryDatabase
* db() {
44 // Call CloseDatabase() to avoid having multiple database instances for
45 // single directory at once.
47 db_
.reset(new FileSystemDirectoryDatabase(path()));
50 void CloseDatabase() {
54 bool AddFileInfo(FileId parent_id
, const base::FilePath::StringType
& name
) {
57 info
.parent_id
= parent_id
;
59 return db_
->AddFileInfo(info
, &file_id
);
62 void CreateDirectory(FileId parent_id
,
63 const base::FilePath::StringType
& name
,
64 FileId
* file_id_out
) {
66 info
.parent_id
= parent_id
;
68 ASSERT_TRUE(db_
->AddFileInfo(info
, file_id_out
));
71 void CreateFile(FileId parent_id
,
72 const base::FilePath::StringType
& name
,
73 const base::FilePath::StringType
& data_path
,
74 FileId
* file_id_out
) {
78 info
.parent_id
= parent_id
;
80 info
.data_path
= base::FilePath(data_path
).NormalizePathSeparators();
81 ASSERT_TRUE(db_
->AddFileInfo(info
, &file_id
));
83 base::FilePath local_path
= path().Append(data_path
);
84 if (!file_util::DirectoryExists(local_path
.DirName()))
85 ASSERT_TRUE(file_util::CreateDirectory(local_path
.DirName()));
88 base::PlatformFileError error
= base::PLATFORM_FILE_ERROR_FAILED
;
89 base::PlatformFile file
= base::CreatePlatformFile(
91 base::PLATFORM_FILE_CREATE
| base::PLATFORM_FILE_WRITE
,
93 ASSERT_EQ(base::PLATFORM_FILE_OK
, error
);
95 ASSERT_TRUE(base::ClosePlatformFile(file
));
98 *file_id_out
= file_id
;
101 void ClearDatabaseAndDirectory() {
103 ASSERT_TRUE(file_util::Delete(path(), true /* recursive */));
104 ASSERT_TRUE(file_util::CreateDirectory(path()));
105 db_
.reset(new FileSystemDirectoryDatabase(path()));
108 bool RepairDatabase() {
109 return db()->RepairDatabase(
110 FilePathToString(path().Append(kDirectoryDatabaseName
)));
113 const base::FilePath
& path() {
117 // Makes link from |parent_id| to |child_id| with |name|.
118 void MakeHierarchyLink(FileId parent_id
,
120 const base::FilePath::StringType
& name
) {
121 ASSERT_TRUE(db()->db_
->Put(
122 leveldb::WriteOptions(),
123 "CHILD_OF:" + base::Int64ToString(parent_id
) + ":" +
124 FilePathToString(base::FilePath(name
)),
125 base::Int64ToString(child_id
)).ok());
128 // Deletes link from parent of |file_id| to |file_id|.
129 void DeleteHierarchyLink(FileId file_id
) {
131 ASSERT_TRUE(db()->GetFileInfo(file_id
, &file_info
));
132 ASSERT_TRUE(db()->db_
->Delete(
133 leveldb::WriteOptions(),
134 "CHILD_OF:" + base::Int64ToString(file_info
.parent_id
) + ":" +
135 FilePathToString(base::FilePath(file_info
.name
))).ok());
139 // Common temp base for nondestructive uses.
140 base::ScopedTempDir base_
;
141 scoped_ptr
<FileSystemDirectoryDatabase
> db_
;
143 DISALLOW_COPY_AND_ASSIGN(FileSystemDirectoryDatabaseTest
);
146 TEST_F(FileSystemDirectoryDatabaseTest
, TestMissingFileGetInfo
) {
147 FileId file_id
= 888;
149 EXPECT_FALSE(db()->GetFileInfo(file_id
, &info
));
152 TEST_F(FileSystemDirectoryDatabaseTest
, TestGetRootFileInfoBeforeCreate
) {
155 EXPECT_TRUE(db()->GetFileInfo(file_id
, &info
));
156 EXPECT_EQ(0, info
.parent_id
);
157 EXPECT_TRUE(info
.name
.empty());
158 EXPECT_TRUE(info
.data_path
.empty());
161 TEST_F(FileSystemDirectoryDatabaseTest
, TestMissingParentAddFileInfo
) {
162 FileId parent_id
= 7;
163 EXPECT_FALSE(AddFileInfo(parent_id
, FILE_PATH_LITERAL("foo")));
166 TEST_F(FileSystemDirectoryDatabaseTest
, TestAddNameClash
) {
170 info
.name
= FILE_PATH_LITERAL("dir 0");
171 EXPECT_TRUE(db()->AddFileInfo(info
, &file_id
));
173 // Check for name clash in the root directory.
174 base::FilePath::StringType name
= info
.name
;
175 EXPECT_FALSE(AddFileInfo(0, name
));
176 name
= FILE_PATH_LITERAL("dir 1");
177 EXPECT_TRUE(AddFileInfo(0, name
));
179 name
= FILE_PATH_LITERAL("subdir 0");
180 EXPECT_TRUE(AddFileInfo(file_id
, name
));
182 // Check for name clash in a subdirectory.
183 EXPECT_FALSE(AddFileInfo(file_id
, name
));
184 name
= FILE_PATH_LITERAL("subdir 1");
185 EXPECT_TRUE(AddFileInfo(file_id
, name
));
188 TEST_F(FileSystemDirectoryDatabaseTest
, TestRenameNoMoveNameClash
) {
191 base::FilePath::StringType name0
= FILE_PATH_LITERAL("foo");
192 base::FilePath::StringType name1
= FILE_PATH_LITERAL("bar");
193 base::FilePath::StringType name2
= FILE_PATH_LITERAL("bas");
196 EXPECT_TRUE(db()->AddFileInfo(info
, &file_id0
));
197 EXPECT_TRUE(AddFileInfo(0, name1
));
199 EXPECT_FALSE(db()->UpdateFileInfo(file_id0
, info
));
201 EXPECT_TRUE(db()->UpdateFileInfo(file_id0
, info
));
204 TEST_F(FileSystemDirectoryDatabaseTest
, TestMoveSameNameNameClash
) {
208 base::FilePath::StringType name0
= FILE_PATH_LITERAL("foo");
209 base::FilePath::StringType name1
= FILE_PATH_LITERAL("bar");
212 EXPECT_TRUE(db()->AddFileInfo(info
, &file_id0
));
213 info
.parent_id
= file_id0
;
214 EXPECT_TRUE(db()->AddFileInfo(info
, &file_id1
));
216 EXPECT_FALSE(db()->UpdateFileInfo(file_id1
, info
));
218 EXPECT_TRUE(db()->UpdateFileInfo(file_id1
, info
));
221 TEST_F(FileSystemDirectoryDatabaseTest
, TestMoveRenameNameClash
) {
225 base::FilePath::StringType name0
= FILE_PATH_LITERAL("foo");
226 base::FilePath::StringType name1
= FILE_PATH_LITERAL("bar");
227 base::FilePath::StringType name2
= FILE_PATH_LITERAL("bas");
230 EXPECT_TRUE(db()->AddFileInfo(info
, &file_id0
));
231 info
.parent_id
= file_id0
;
233 EXPECT_TRUE(db()->AddFileInfo(info
, &file_id1
));
236 EXPECT_FALSE(db()->UpdateFileInfo(file_id1
, info
));
238 EXPECT_TRUE(db()->UpdateFileInfo(file_id1
, info
));
239 // Also test a successful move+rename.
240 info
.parent_id
= file_id0
;
242 EXPECT_TRUE(db()->UpdateFileInfo(file_id1
, info
));
245 TEST_F(FileSystemDirectoryDatabaseTest
, TestRemoveWithChildren
) {
250 info
.name
= FILE_PATH_LITERAL("foo");
251 EXPECT_TRUE(db()->AddFileInfo(info
, &file_id0
));
252 info
.parent_id
= file_id0
;
253 EXPECT_TRUE(db()->AddFileInfo(info
, &file_id1
));
254 EXPECT_FALSE(db()->RemoveFileInfo(file_id0
));
255 EXPECT_TRUE(db()->RemoveFileInfo(file_id1
));
256 EXPECT_TRUE(db()->RemoveFileInfo(file_id0
));
259 TEST_F(FileSystemDirectoryDatabaseTest
, TestGetChildWithName
) {
263 base::FilePath::StringType name0
= FILE_PATH_LITERAL("foo");
264 base::FilePath::StringType name1
= FILE_PATH_LITERAL("bar");
267 EXPECT_TRUE(db()->AddFileInfo(info
, &file_id0
));
268 info
.parent_id
= file_id0
;
270 EXPECT_TRUE(db()->AddFileInfo(info
, &file_id1
));
271 EXPECT_NE(file_id0
, file_id1
);
273 FileId check_file_id
;
274 EXPECT_FALSE(db()->GetChildWithName(0, name1
, &check_file_id
));
275 EXPECT_TRUE(db()->GetChildWithName(0, name0
, &check_file_id
));
276 EXPECT_EQ(file_id0
, check_file_id
);
277 EXPECT_FALSE(db()->GetChildWithName(file_id0
, name0
, &check_file_id
));
278 EXPECT_TRUE(db()->GetChildWithName(file_id0
, name1
, &check_file_id
));
279 EXPECT_EQ(file_id1
, check_file_id
);
282 TEST_F(FileSystemDirectoryDatabaseTest
, TestGetFileWithPath
) {
287 base::FilePath::StringType name0
= FILE_PATH_LITERAL("foo");
288 base::FilePath::StringType name1
= FILE_PATH_LITERAL("bar");
289 base::FilePath::StringType name2
= FILE_PATH_LITERAL("dog");
293 EXPECT_TRUE(db()->AddFileInfo(info
, &file_id0
));
294 info
.parent_id
= file_id0
;
296 EXPECT_TRUE(db()->AddFileInfo(info
, &file_id1
));
297 EXPECT_NE(file_id0
, file_id1
);
298 info
.parent_id
= file_id1
;
300 EXPECT_TRUE(db()->AddFileInfo(info
, &file_id2
));
301 EXPECT_NE(file_id0
, file_id2
);
302 EXPECT_NE(file_id1
, file_id2
);
304 FileId check_file_id
;
305 base::FilePath path
= base::FilePath(name0
);
306 EXPECT_TRUE(db()->GetFileWithPath(path
, &check_file_id
));
307 EXPECT_EQ(file_id0
, check_file_id
);
309 path
= path
.Append(name1
);
310 EXPECT_TRUE(db()->GetFileWithPath(path
, &check_file_id
));
311 EXPECT_EQ(file_id1
, check_file_id
);
313 path
= path
.Append(name2
);
314 EXPECT_TRUE(db()->GetFileWithPath(path
, &check_file_id
));
315 EXPECT_EQ(file_id2
, check_file_id
);
318 TEST_F(FileSystemDirectoryDatabaseTest
, TestListChildren
) {
319 // No children in the root.
320 std::vector
<FileId
> children
;
321 EXPECT_TRUE(db()->ListChildren(0, &children
));
322 EXPECT_TRUE(children
.empty());
324 // One child in the root.
328 info
.name
= FILE_PATH_LITERAL("foo");
329 EXPECT_TRUE(db()->AddFileInfo(info
, &file_id0
));
330 EXPECT_TRUE(db()->ListChildren(0, &children
));
331 EXPECT_EQ(children
.size(), 1UL);
332 EXPECT_EQ(children
[0], file_id0
);
334 // Two children in the root.
336 info
.name
= FILE_PATH_LITERAL("bar");
337 EXPECT_TRUE(db()->AddFileInfo(info
, &file_id1
));
338 EXPECT_TRUE(db()->ListChildren(0, &children
));
339 EXPECT_EQ(2UL, children
.size());
340 if (children
[0] == file_id0
) {
341 EXPECT_EQ(children
[1], file_id1
);
343 EXPECT_EQ(children
[1], file_id0
);
344 EXPECT_EQ(children
[0], file_id1
);
347 // No children in a subdirectory.
348 EXPECT_TRUE(db()->ListChildren(file_id0
, &children
));
349 EXPECT_TRUE(children
.empty());
351 // One child in a subdirectory.
352 info
.parent_id
= file_id0
;
353 info
.name
= FILE_PATH_LITERAL("foo");
356 EXPECT_TRUE(db()->AddFileInfo(info
, &file_id2
));
357 EXPECT_TRUE(db()->ListChildren(file_id0
, &children
));
358 EXPECT_EQ(1UL, children
.size());
359 EXPECT_EQ(children
[0], file_id2
);
361 // Two children in a subdirectory.
362 info
.name
= FILE_PATH_LITERAL("bar");
363 EXPECT_TRUE(db()->AddFileInfo(info
, &file_id3
));
364 EXPECT_TRUE(db()->ListChildren(file_id0
, &children
));
365 EXPECT_EQ(2UL, children
.size());
366 if (children
[0] == file_id2
) {
367 EXPECT_EQ(children
[1], file_id3
);
369 EXPECT_EQ(children
[1], file_id2
);
370 EXPECT_EQ(children
[0], file_id3
);
374 TEST_F(FileSystemDirectoryDatabaseTest
, TestUpdateModificationTime
) {
378 info0
.name
= FILE_PATH_LITERAL("name");
379 info0
.data_path
= base::FilePath(FILE_PATH_LITERAL("fake path"));
380 info0
.modification_time
= base::Time::Now();
381 EXPECT_TRUE(db()->AddFileInfo(info0
, &file_id
));
383 EXPECT_TRUE(db()->GetFileInfo(file_id
, &info1
));
384 EXPECT_EQ(info0
.name
, info1
.name
);
385 EXPECT_EQ(info0
.parent_id
, info1
.parent_id
);
386 EXPECT_EQ(info0
.data_path
, info1
.data_path
);
388 floor(info0
.modification_time
.ToDoubleT()),
389 info1
.modification_time
.ToDoubleT());
391 EXPECT_TRUE(db()->UpdateModificationTime(file_id
, base::Time::UnixEpoch()));
392 EXPECT_TRUE(db()->GetFileInfo(file_id
, &info1
));
393 EXPECT_EQ(info0
.name
, info1
.name
);
394 EXPECT_EQ(info0
.parent_id
, info1
.parent_id
);
395 EXPECT_EQ(info0
.data_path
, info1
.data_path
);
396 EXPECT_NE(info0
.modification_time
, info1
.modification_time
);
398 info1
.modification_time
.ToDoubleT(),
399 floor(base::Time::UnixEpoch().ToDoubleT()));
401 EXPECT_FALSE(db()->UpdateModificationTime(999, base::Time::UnixEpoch()));
404 TEST_F(FileSystemDirectoryDatabaseTest
, TestSimpleFileOperations
) {
405 FileId file_id
= 888;
407 EXPECT_FALSE(db()->GetFileInfo(file_id
, &info0
));
409 info0
.data_path
= base::FilePath(FILE_PATH_LITERAL("foo"));
410 info0
.name
= FILE_PATH_LITERAL("file name");
411 info0
.modification_time
= base::Time::Now();
412 EXPECT_TRUE(db()->AddFileInfo(info0
, &file_id
));
414 EXPECT_TRUE(db()->GetFileInfo(file_id
, &info1
));
415 EXPECT_EQ(info0
.parent_id
, info1
.parent_id
);
416 EXPECT_EQ(info0
.data_path
, info1
.data_path
);
417 EXPECT_EQ(info0
.name
, info1
.name
);
419 floor(info0
.modification_time
.ToDoubleT()),
420 info1
.modification_time
.ToDoubleT());
423 TEST_F(FileSystemDirectoryDatabaseTest
, TestOverwritingMoveFileSrcDirectory
) {
427 info0
.name
= FILE_PATH_LITERAL("directory");
428 info0
.modification_time
= base::Time::Now();
429 EXPECT_TRUE(db()->AddFileInfo(info0
, &directory_id
));
434 info1
.data_path
= base::FilePath(FILE_PATH_LITERAL("bar"));
435 info1
.name
= FILE_PATH_LITERAL("file");
436 info1
.modification_time
= base::Time::UnixEpoch();
437 EXPECT_TRUE(db()->AddFileInfo(info1
, &file_id
));
439 EXPECT_FALSE(db()->OverwritingMoveFile(directory_id
, file_id
));
442 TEST_F(FileSystemDirectoryDatabaseTest
, TestOverwritingMoveFileDestDirectory
) {
446 info0
.name
= FILE_PATH_LITERAL("file");
447 info0
.data_path
= base::FilePath(FILE_PATH_LITERAL("bar"));
448 info0
.modification_time
= base::Time::Now();
449 EXPECT_TRUE(db()->AddFileInfo(info0
, &file_id
));
454 info1
.name
= FILE_PATH_LITERAL("directory");
455 info1
.modification_time
= base::Time::UnixEpoch();
456 EXPECT_TRUE(db()->AddFileInfo(info1
, &directory_id
));
458 EXPECT_FALSE(db()->OverwritingMoveFile(file_id
, directory_id
));
461 TEST_F(FileSystemDirectoryDatabaseTest
, TestOverwritingMoveFileSuccess
) {
465 info0
.data_path
= base::FilePath(FILE_PATH_LITERAL("foo"));
466 info0
.name
= FILE_PATH_LITERAL("file name 0");
467 info0
.modification_time
= base::Time::Now();
468 EXPECT_TRUE(db()->AddFileInfo(info0
, &file_id0
));
472 dir_info
.parent_id
= 0;
473 dir_info
.name
= FILE_PATH_LITERAL("directory name");
474 EXPECT_TRUE(db()->AddFileInfo(dir_info
, &dir_id
));
478 info1
.parent_id
= dir_id
;
479 info1
.data_path
= base::FilePath(FILE_PATH_LITERAL("bar"));
480 info1
.name
= FILE_PATH_LITERAL("file name 1");
481 info1
.modification_time
= base::Time::UnixEpoch();
482 EXPECT_TRUE(db()->AddFileInfo(info1
, &file_id1
));
484 EXPECT_TRUE(db()->OverwritingMoveFile(file_id0
, file_id1
));
489 EXPECT_FALSE(db()->GetFileWithPath(base::FilePath(info0
.name
), &check_id
));
490 EXPECT_TRUE(db()->GetFileWithPath(
491 base::FilePath(dir_info
.name
).Append(info1
.name
), &check_id
));
492 EXPECT_TRUE(db()->GetFileInfo(check_id
, &check_info
));
494 EXPECT_EQ(info0
.data_path
, check_info
.data_path
);
497 TEST_F(FileSystemDirectoryDatabaseTest
, TestGetNextInteger
) {
499 EXPECT_TRUE(db()->GetNextInteger(&next
));
501 EXPECT_TRUE(db()->GetNextInteger(&next
));
504 EXPECT_TRUE(db()->GetNextInteger(&next
));
506 EXPECT_TRUE(db()->GetNextInteger(&next
));
509 EXPECT_TRUE(db()->GetNextInteger(&next
));
513 TEST_F(FileSystemDirectoryDatabaseTest
, TestConsistencyCheck_Empty
) {
514 EXPECT_TRUE(db()->IsFileSystemConsistent());
517 EXPECT_TRUE(db()->GetNextInteger(&next
));
519 EXPECT_TRUE(db()->IsFileSystemConsistent());
522 TEST_F(FileSystemDirectoryDatabaseTest
, TestConsistencyCheck_Consistent
) {
524 CreateFile(0, FPL("foo"), FPL("hoge"), NULL
);
525 CreateDirectory(0, FPL("bar"), &dir_id
);
526 CreateFile(dir_id
, FPL("baz"), FPL("fuga"), NULL
);
527 CreateFile(dir_id
, FPL("fizz"), FPL("buzz"), NULL
);
529 EXPECT_TRUE(db()->IsFileSystemConsistent());
532 TEST_F(FileSystemDirectoryDatabaseTest
,
533 TestConsistencyCheck_BackingMultiEntry
) {
534 const base::FilePath::CharType kBackingFileName
[] = FPL("the celeb");
535 CreateFile(0, FPL("foo"), kBackingFileName
, NULL
);
537 EXPECT_TRUE(db()->IsFileSystemConsistent());
538 ASSERT_TRUE(file_util::Delete(path().Append(kBackingFileName
), false));
539 CreateFile(0, FPL("bar"), kBackingFileName
, NULL
);
540 EXPECT_FALSE(db()->IsFileSystemConsistent());
543 TEST_F(FileSystemDirectoryDatabaseTest
, TestConsistencyCheck_FileLost
) {
544 const base::FilePath::CharType kBackingFileName
[] = FPL("hoge");
545 CreateFile(0, FPL("foo"), kBackingFileName
, NULL
);
547 EXPECT_TRUE(db()->IsFileSystemConsistent());
548 ASSERT_TRUE(file_util::Delete(path().Append(kBackingFileName
), false));
549 EXPECT_TRUE(db()->IsFileSystemConsistent());
552 TEST_F(FileSystemDirectoryDatabaseTest
, TestConsistencyCheck_OrphanFile
) {
553 CreateFile(0, FPL("foo"), FPL("hoge"), NULL
);
555 EXPECT_TRUE(db()->IsFileSystemConsistent());
557 bool created
= false;
558 base::PlatformFileError error
= base::PLATFORM_FILE_ERROR_FAILED
;
559 base::PlatformFile file
= base::CreatePlatformFile(
560 path().Append(FPL("Orphan File")),
561 base::PLATFORM_FILE_CREATE
| base::PLATFORM_FILE_WRITE
,
563 ASSERT_EQ(base::PLATFORM_FILE_OK
, error
);
564 ASSERT_TRUE(created
);
565 ASSERT_TRUE(base::ClosePlatformFile(file
));
567 EXPECT_TRUE(db()->IsFileSystemConsistent());
570 TEST_F(FileSystemDirectoryDatabaseTest
, TestConsistencyCheck_RootLoop
) {
571 EXPECT_TRUE(db()->IsFileSystemConsistent());
572 MakeHierarchyLink(0, 0, base::FilePath::StringType());
573 EXPECT_FALSE(db()->IsFileSystemConsistent());
576 TEST_F(FileSystemDirectoryDatabaseTest
, TestConsistencyCheck_DirectoryLoop
) {
579 base::FilePath::StringType dir1_name
= FPL("foo");
580 CreateDirectory(0, dir1_name
, &dir1_id
);
581 CreateDirectory(dir1_id
, FPL("bar"), &dir2_id
);
583 EXPECT_TRUE(db()->IsFileSystemConsistent());
584 MakeHierarchyLink(dir2_id
, dir1_id
, dir1_name
);
585 EXPECT_FALSE(db()->IsFileSystemConsistent());
588 TEST_F(FileSystemDirectoryDatabaseTest
, TestConsistencyCheck_NameMismatch
) {
591 CreateDirectory(0, FPL("foo"), &dir_id
);
592 CreateFile(dir_id
, FPL("bar"), FPL("hoge/fuga/piyo"), &file_id
);
594 EXPECT_TRUE(db()->IsFileSystemConsistent());
595 DeleteHierarchyLink(file_id
);
596 MakeHierarchyLink(dir_id
, file_id
, FPL("baz"));
597 EXPECT_FALSE(db()->IsFileSystemConsistent());
600 TEST_F(FileSystemDirectoryDatabaseTest
, TestConsistencyCheck_WreckedEntries
) {
603 CreateDirectory(0, FPL("foo"), &dir1_id
);
604 CreateDirectory(dir1_id
, FPL("bar"), &dir2_id
);
605 CreateFile(dir2_id
, FPL("baz"), FPL("fizz/buzz"), NULL
);
607 EXPECT_TRUE(db()->IsFileSystemConsistent());
608 DeleteHierarchyLink(dir2_id
); // Delete link from |dir1_id| to |dir2_id|.
609 EXPECT_FALSE(db()->IsFileSystemConsistent());
612 TEST_F(FileSystemDirectoryDatabaseTest
, TestRepairDatabase_Success
) {
613 base::FilePath::StringType kFileName
= FPL("bar");
616 CreateFile(0, FPL("foo"), FPL("hoge"), NULL
);
617 CreateFile(0, kFileName
, FPL("fuga"), &file_id_prev
);
619 const base::FilePath kDatabaseDirectory
= path().Append(kDirectoryDatabaseName
);
621 CorruptDatabase(kDatabaseDirectory
, leveldb::kDescriptorFile
,
622 0, std::numeric_limits
<size_t>::max());
624 EXPECT_FALSE(db()->IsFileSystemConsistent());
627 EXPECT_TRUE(db()->GetChildWithName(0, kFileName
, &file_id
));
628 EXPECT_EQ(file_id_prev
, file_id
);
630 EXPECT_TRUE(db()->IsFileSystemConsistent());
633 TEST_F(FileSystemDirectoryDatabaseTest
, TestRepairDatabase_Failure
) {
634 base::FilePath::StringType kFileName
= FPL("bar");
636 CreateFile(0, FPL("foo"), FPL("hoge"), NULL
);
637 CreateFile(0, kFileName
, FPL("fuga"), NULL
);
639 const base::FilePath kDatabaseDirectory
= path().Append(kDirectoryDatabaseName
);
641 CorruptDatabase(kDatabaseDirectory
, leveldb::kDescriptorFile
,
642 0, std::numeric_limits
<size_t>::max());
643 CorruptDatabase(kDatabaseDirectory
, leveldb::kLogFile
,
646 EXPECT_FALSE(db()->IsFileSystemConsistent());
649 EXPECT_FALSE(db()->GetChildWithName(0, kFileName
, &file_id
));
650 EXPECT_TRUE(db()->IsFileSystemConsistent());
653 TEST_F(FileSystemDirectoryDatabaseTest
, TestRepairDatabase_MissingManifest
) {
654 base::FilePath::StringType kFileName
= FPL("bar");
657 CreateFile(0, FPL("foo"), FPL("hoge"), NULL
);
658 CreateFile(0, kFileName
, FPL("fuga"), &file_id_prev
);
660 const base::FilePath kDatabaseDirectory
=
661 path().Append(kDirectoryDatabaseName
);
664 DeleteDatabaseFile(kDatabaseDirectory
, leveldb::kDescriptorFile
);
667 EXPECT_FALSE(db()->IsFileSystemConsistent());
670 EXPECT_TRUE(db()->GetChildWithName(0, kFileName
, &file_id
));
671 EXPECT_EQ(file_id_prev
, file_id
);
673 EXPECT_TRUE(db()->IsFileSystemConsistent());
676 } // namespace fileapi