Experimental push messaging api reference docs.
[chromium-blink-merge.git] / webkit / fileapi / file_system_directory_database_unittest.cc
blobfc52bc6bf37aff7e61cbbdd16e099118aa68118a
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"
7 #include <math.h>
9 #include "base/file_util.h"
10 #include "base/platform_file.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/scoped_temp_dir.h"
13 #include "base/string_number_conversions.h"
14 #include "base/string_util.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/leveldatabase/src/include/leveldb/db.h"
17 #include "webkit/fileapi/file_system_database_test_helper.h"
18 #include "webkit/fileapi/file_system_util.h"
20 #define FPL(x) FILE_PATH_LITERAL(x)
22 namespace fileapi {
24 namespace {
25 const FilePath::CharType kDirectoryDatabaseName[] = FPL("Paths");
28 class FileSystemDirectoryDatabaseTest : public testing::Test {
29 public:
30 typedef FileSystemDirectoryDatabase::FileId FileId;
31 typedef FileSystemDirectoryDatabase::FileInfo FileInfo;
33 FileSystemDirectoryDatabaseTest() {
34 EXPECT_TRUE(base_.CreateUniqueTempDir());
35 InitDatabase();
38 FileSystemDirectoryDatabase* db() {
39 return db_.get();
42 void InitDatabase() {
43 // Call CloseDatabase() to avoid having multiple database instances for
44 // single directory at once.
45 CloseDatabase();
46 db_.reset(new FileSystemDirectoryDatabase(path()));
49 void CloseDatabase() {
50 db_.reset();
53 bool AddFileInfo(FileId parent_id, const FilePath::StringType& name) {
54 FileId file_id;
55 FileInfo info;
56 info.parent_id = parent_id;
57 info.name = name;
58 return db_->AddFileInfo(info, &file_id);
61 void CreateDirectory(FileId parent_id,
62 const FilePath::StringType& name,
63 FileId* file_id_out) {
64 FileInfo info;
65 info.parent_id = parent_id;
66 info.name = name;
67 ASSERT_TRUE(db_->AddFileInfo(info, file_id_out));
70 void CreateFile(FileId parent_id,
71 const FilePath::StringType& name,
72 const FilePath::StringType& data_path,
73 FileId* file_id_out) {
74 FileId file_id;
76 FileInfo info;
77 info.parent_id = parent_id;
78 info.name = name;
79 info.data_path = FilePath(data_path).NormalizePathSeparators();
80 ASSERT_TRUE(db_->AddFileInfo(info, &file_id));
82 FilePath local_path = path().Append(data_path);
83 if (!file_util::DirectoryExists(local_path.DirName()))
84 ASSERT_TRUE(file_util::CreateDirectory(local_path.DirName()));
86 bool created = false;
87 base::PlatformFileError error = base::PLATFORM_FILE_ERROR_FAILED;
88 base::PlatformFile file = base::CreatePlatformFile(
89 local_path,
90 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
91 &created, &error);
92 ASSERT_EQ(base::PLATFORM_FILE_OK, error);
93 ASSERT_TRUE(created);
94 ASSERT_TRUE(base::ClosePlatformFile(file));
96 if (file_id_out)
97 *file_id_out = file_id;
100 void ClearDatabaseAndDirectory() {
101 db_.reset();
102 ASSERT_TRUE(file_util::Delete(path(), true /* recursive */));
103 ASSERT_TRUE(file_util::CreateDirectory(path()));
104 db_.reset(new FileSystemDirectoryDatabase(path()));
107 bool RepairDatabase() {
108 return db()->RepairDatabase(
109 FilePathToString(path().Append(kDirectoryDatabaseName)));
112 const FilePath& path() {
113 return base_.path();
116 // Makes link from |parent_id| to |child_id| with |name|.
117 void MakeHierarchyLink(FileId parent_id,
118 FileId child_id,
119 const FilePath::StringType& name) {
120 ASSERT_TRUE(db()->db_->Put(
121 leveldb::WriteOptions(),
122 "CHILD_OF:" + base::Int64ToString(parent_id) + ":" +
123 FilePathToString(FilePath(name)),
124 base::Int64ToString(child_id)).ok());
127 // Deletes link from parent of |file_id| to |file_id|.
128 void DeleteHierarchyLink(FileId file_id) {
129 FileInfo file_info;
130 ASSERT_TRUE(db()->GetFileInfo(file_id, &file_info));
131 ASSERT_TRUE(db()->db_->Delete(
132 leveldb::WriteOptions(),
133 "CHILD_OF:" + base::Int64ToString(file_info.parent_id) + ":" +
134 FilePathToString(FilePath(file_info.name))).ok());
137 protected:
138 // Common temp base for nondestructive uses.
139 ScopedTempDir base_;
140 scoped_ptr<FileSystemDirectoryDatabase> db_;
142 DISALLOW_COPY_AND_ASSIGN(FileSystemDirectoryDatabaseTest);
145 TEST_F(FileSystemDirectoryDatabaseTest, TestMissingFileGetInfo) {
146 FileId file_id = 888;
147 FileInfo info;
148 EXPECT_FALSE(db()->GetFileInfo(file_id, &info));
151 TEST_F(FileSystemDirectoryDatabaseTest, TestGetRootFileInfoBeforeCreate) {
152 FileId file_id = 0;
153 FileInfo info;
154 EXPECT_TRUE(db()->GetFileInfo(file_id, &info));
155 EXPECT_EQ(0, info.parent_id);
156 EXPECT_TRUE(info.name.empty());
157 EXPECT_TRUE(info.data_path.empty());
160 TEST_F(FileSystemDirectoryDatabaseTest, TestMissingParentAddFileInfo) {
161 FileId parent_id = 7;
162 EXPECT_FALSE(AddFileInfo(parent_id, FILE_PATH_LITERAL("foo")));
165 TEST_F(FileSystemDirectoryDatabaseTest, TestAddNameClash) {
166 FileInfo info;
167 FileId file_id;
168 info.parent_id = 0;
169 info.name = FILE_PATH_LITERAL("dir 0");
170 EXPECT_TRUE(db()->AddFileInfo(info, &file_id));
172 // Check for name clash in the root directory.
173 FilePath::StringType name = info.name;
174 EXPECT_FALSE(AddFileInfo(0, name));
175 name = FILE_PATH_LITERAL("dir 1");
176 EXPECT_TRUE(AddFileInfo(0, name));
178 name = FILE_PATH_LITERAL("subdir 0");
179 EXPECT_TRUE(AddFileInfo(file_id, name));
181 // Check for name clash in a subdirectory.
182 EXPECT_FALSE(AddFileInfo(file_id, name));
183 name = FILE_PATH_LITERAL("subdir 1");
184 EXPECT_TRUE(AddFileInfo(file_id, name));
187 TEST_F(FileSystemDirectoryDatabaseTest, TestRenameNoMoveNameClash) {
188 FileInfo info;
189 FileId file_id0;
190 FilePath::StringType name0 = FILE_PATH_LITERAL("foo");
191 FilePath::StringType name1 = FILE_PATH_LITERAL("bar");
192 FilePath::StringType name2 = FILE_PATH_LITERAL("bas");
193 info.parent_id = 0;
194 info.name = name0;
195 EXPECT_TRUE(db()->AddFileInfo(info, &file_id0));
196 EXPECT_TRUE(AddFileInfo(0, name1));
197 info.name = name1;
198 EXPECT_FALSE(db()->UpdateFileInfo(file_id0, info));
199 info.name = name2;
200 EXPECT_TRUE(db()->UpdateFileInfo(file_id0, info));
203 TEST_F(FileSystemDirectoryDatabaseTest, TestMoveSameNameNameClash) {
204 FileInfo info;
205 FileId file_id0;
206 FileId file_id1;
207 FilePath::StringType name0 = FILE_PATH_LITERAL("foo");
208 FilePath::StringType name1 = FILE_PATH_LITERAL("bar");
209 info.parent_id = 0;
210 info.name = name0;
211 EXPECT_TRUE(db()->AddFileInfo(info, &file_id0));
212 info.parent_id = file_id0;
213 EXPECT_TRUE(db()->AddFileInfo(info, &file_id1));
214 info.parent_id = 0;
215 EXPECT_FALSE(db()->UpdateFileInfo(file_id1, info));
216 info.name = name1;
217 EXPECT_TRUE(db()->UpdateFileInfo(file_id1, info));
220 TEST_F(FileSystemDirectoryDatabaseTest, TestMoveRenameNameClash) {
221 FileInfo info;
222 FileId file_id0;
223 FileId file_id1;
224 FilePath::StringType name0 = FILE_PATH_LITERAL("foo");
225 FilePath::StringType name1 = FILE_PATH_LITERAL("bar");
226 FilePath::StringType name2 = FILE_PATH_LITERAL("bas");
227 info.parent_id = 0;
228 info.name = name0;
229 EXPECT_TRUE(db()->AddFileInfo(info, &file_id0));
230 info.parent_id = file_id0;
231 info.name = name1;
232 EXPECT_TRUE(db()->AddFileInfo(info, &file_id1));
233 info.parent_id = 0;
234 info.name = name0;
235 EXPECT_FALSE(db()->UpdateFileInfo(file_id1, info));
236 info.name = name1;
237 EXPECT_TRUE(db()->UpdateFileInfo(file_id1, info));
238 // Also test a successful move+rename.
239 info.parent_id = file_id0;
240 info.name = name2;
241 EXPECT_TRUE(db()->UpdateFileInfo(file_id1, info));
244 TEST_F(FileSystemDirectoryDatabaseTest, TestRemoveWithChildren) {
245 FileInfo info;
246 FileId file_id0;
247 FileId file_id1;
248 info.parent_id = 0;
249 info.name = FILE_PATH_LITERAL("foo");
250 EXPECT_TRUE(db()->AddFileInfo(info, &file_id0));
251 info.parent_id = file_id0;
252 EXPECT_TRUE(db()->AddFileInfo(info, &file_id1));
253 EXPECT_FALSE(db()->RemoveFileInfo(file_id0));
254 EXPECT_TRUE(db()->RemoveFileInfo(file_id1));
255 EXPECT_TRUE(db()->RemoveFileInfo(file_id0));
258 TEST_F(FileSystemDirectoryDatabaseTest, TestGetChildWithName) {
259 FileInfo info;
260 FileId file_id0;
261 FileId file_id1;
262 FilePath::StringType name0 = FILE_PATH_LITERAL("foo");
263 FilePath::StringType name1 = FILE_PATH_LITERAL("bar");
264 info.parent_id = 0;
265 info.name = name0;
266 EXPECT_TRUE(db()->AddFileInfo(info, &file_id0));
267 info.parent_id = file_id0;
268 info.name = name1;
269 EXPECT_TRUE(db()->AddFileInfo(info, &file_id1));
270 EXPECT_NE(file_id0, file_id1);
272 FileId check_file_id;
273 EXPECT_FALSE(db()->GetChildWithName(0, name1, &check_file_id));
274 EXPECT_TRUE(db()->GetChildWithName(0, name0, &check_file_id));
275 EXPECT_EQ(file_id0, check_file_id);
276 EXPECT_FALSE(db()->GetChildWithName(file_id0, name0, &check_file_id));
277 EXPECT_TRUE(db()->GetChildWithName(file_id0, name1, &check_file_id));
278 EXPECT_EQ(file_id1, check_file_id);
281 TEST_F(FileSystemDirectoryDatabaseTest, TestGetFileWithPath) {
282 FileInfo info;
283 FileId file_id0;
284 FileId file_id1;
285 FileId file_id2;
286 FilePath::StringType name0 = FILE_PATH_LITERAL("foo");
287 FilePath::StringType name1 = FILE_PATH_LITERAL("bar");
288 FilePath::StringType name2 = FILE_PATH_LITERAL("dog");
290 info.parent_id = 0;
291 info.name = name0;
292 EXPECT_TRUE(db()->AddFileInfo(info, &file_id0));
293 info.parent_id = file_id0;
294 info.name = name1;
295 EXPECT_TRUE(db()->AddFileInfo(info, &file_id1));
296 EXPECT_NE(file_id0, file_id1);
297 info.parent_id = file_id1;
298 info.name = name2;
299 EXPECT_TRUE(db()->AddFileInfo(info, &file_id2));
300 EXPECT_NE(file_id0, file_id2);
301 EXPECT_NE(file_id1, file_id2);
303 FileId check_file_id;
304 FilePath path = FilePath(name0);
305 EXPECT_TRUE(db()->GetFileWithPath(path, &check_file_id));
306 EXPECT_EQ(file_id0, check_file_id);
308 path = path.Append(name1);
309 EXPECT_TRUE(db()->GetFileWithPath(path, &check_file_id));
310 EXPECT_EQ(file_id1, check_file_id);
312 path = path.Append(name2);
313 EXPECT_TRUE(db()->GetFileWithPath(path, &check_file_id));
314 EXPECT_EQ(file_id2, check_file_id);
317 TEST_F(FileSystemDirectoryDatabaseTest, TestListChildren) {
318 // No children in the root.
319 std::vector<FileId> children;
320 EXPECT_TRUE(db()->ListChildren(0, &children));
321 EXPECT_TRUE(children.empty());
323 // One child in the root.
324 FileId file_id0;
325 FileInfo info;
326 info.parent_id = 0;
327 info.name = FILE_PATH_LITERAL("foo");
328 EXPECT_TRUE(db()->AddFileInfo(info, &file_id0));
329 EXPECT_TRUE(db()->ListChildren(0, &children));
330 EXPECT_EQ(children.size(), 1UL);
331 EXPECT_EQ(children[0], file_id0);
333 // Two children in the root.
334 FileId file_id1;
335 info.name = FILE_PATH_LITERAL("bar");
336 EXPECT_TRUE(db()->AddFileInfo(info, &file_id1));
337 EXPECT_TRUE(db()->ListChildren(0, &children));
338 EXPECT_EQ(2UL, children.size());
339 if (children[0] == file_id0) {
340 EXPECT_EQ(children[1], file_id1);
341 } else {
342 EXPECT_EQ(children[1], file_id0);
343 EXPECT_EQ(children[0], file_id1);
346 // No children in a subdirectory.
347 EXPECT_TRUE(db()->ListChildren(file_id0, &children));
348 EXPECT_TRUE(children.empty());
350 // One child in a subdirectory.
351 info.parent_id = file_id0;
352 info.name = FILE_PATH_LITERAL("foo");
353 FileId file_id2;
354 FileId file_id3;
355 EXPECT_TRUE(db()->AddFileInfo(info, &file_id2));
356 EXPECT_TRUE(db()->ListChildren(file_id0, &children));
357 EXPECT_EQ(1UL, children.size());
358 EXPECT_EQ(children[0], file_id2);
360 // Two children in a subdirectory.
361 info.name = FILE_PATH_LITERAL("bar");
362 EXPECT_TRUE(db()->AddFileInfo(info, &file_id3));
363 EXPECT_TRUE(db()->ListChildren(file_id0, &children));
364 EXPECT_EQ(2UL, children.size());
365 if (children[0] == file_id2) {
366 EXPECT_EQ(children[1], file_id3);
367 } else {
368 EXPECT_EQ(children[1], file_id2);
369 EXPECT_EQ(children[0], file_id3);
373 TEST_F(FileSystemDirectoryDatabaseTest, TestUpdateModificationTime) {
374 FileInfo info0;
375 FileId file_id;
376 info0.parent_id = 0;
377 info0.name = FILE_PATH_LITERAL("name");
378 info0.data_path = FilePath(FILE_PATH_LITERAL("fake path"));
379 info0.modification_time = base::Time::Now();
380 EXPECT_TRUE(db()->AddFileInfo(info0, &file_id));
381 FileInfo info1;
382 EXPECT_TRUE(db()->GetFileInfo(file_id, &info1));
383 EXPECT_EQ(info0.name, info1.name);
384 EXPECT_EQ(info0.parent_id, info1.parent_id);
385 EXPECT_EQ(info0.data_path, info1.data_path);
386 EXPECT_EQ(
387 floor(info0.modification_time.ToDoubleT()),
388 info1.modification_time.ToDoubleT());
390 EXPECT_TRUE(db()->UpdateModificationTime(file_id, base::Time::UnixEpoch()));
391 EXPECT_TRUE(db()->GetFileInfo(file_id, &info1));
392 EXPECT_EQ(info0.name, info1.name);
393 EXPECT_EQ(info0.parent_id, info1.parent_id);
394 EXPECT_EQ(info0.data_path, info1.data_path);
395 EXPECT_NE(info0.modification_time, info1.modification_time);
396 EXPECT_EQ(
397 info1.modification_time.ToDoubleT(),
398 floor(base::Time::UnixEpoch().ToDoubleT()));
400 EXPECT_FALSE(db()->UpdateModificationTime(999, base::Time::UnixEpoch()));
403 TEST_F(FileSystemDirectoryDatabaseTest, TestSimpleFileOperations) {
404 FileId file_id = 888;
405 FileInfo info0;
406 EXPECT_FALSE(db()->GetFileInfo(file_id, &info0));
407 info0.parent_id = 0;
408 info0.data_path = FilePath(FILE_PATH_LITERAL("foo"));
409 info0.name = FILE_PATH_LITERAL("file name");
410 info0.modification_time = base::Time::Now();
411 EXPECT_TRUE(db()->AddFileInfo(info0, &file_id));
412 FileInfo info1;
413 EXPECT_TRUE(db()->GetFileInfo(file_id, &info1));
414 EXPECT_EQ(info0.parent_id, info1.parent_id);
415 EXPECT_EQ(info0.data_path, info1.data_path);
416 EXPECT_EQ(info0.name, info1.name);
417 EXPECT_EQ(
418 floor(info0.modification_time.ToDoubleT()),
419 info1.modification_time.ToDoubleT());
422 TEST_F(FileSystemDirectoryDatabaseTest, TestOverwritingMoveFileSrcDirectory) {
423 FileId directory_id;
424 FileInfo info0;
425 info0.parent_id = 0;
426 info0.name = FILE_PATH_LITERAL("directory");
427 info0.modification_time = base::Time::Now();
428 EXPECT_TRUE(db()->AddFileInfo(info0, &directory_id));
430 FileId file_id;
431 FileInfo info1;
432 info1.parent_id = 0;
433 info1.data_path = FilePath(FILE_PATH_LITERAL("bar"));
434 info1.name = FILE_PATH_LITERAL("file");
435 info1.modification_time = base::Time::UnixEpoch();
436 EXPECT_TRUE(db()->AddFileInfo(info1, &file_id));
438 EXPECT_FALSE(db()->OverwritingMoveFile(directory_id, file_id));
441 TEST_F(FileSystemDirectoryDatabaseTest, TestOverwritingMoveFileDestDirectory) {
442 FileId file_id;
443 FileInfo info0;
444 info0.parent_id = 0;
445 info0.name = FILE_PATH_LITERAL("file");
446 info0.data_path = FilePath(FILE_PATH_LITERAL("bar"));
447 info0.modification_time = base::Time::Now();
448 EXPECT_TRUE(db()->AddFileInfo(info0, &file_id));
450 FileId directory_id;
451 FileInfo info1;
452 info1.parent_id = 0;
453 info1.name = FILE_PATH_LITERAL("directory");
454 info1.modification_time = base::Time::UnixEpoch();
455 EXPECT_TRUE(db()->AddFileInfo(info1, &directory_id));
457 EXPECT_FALSE(db()->OverwritingMoveFile(file_id, directory_id));
460 TEST_F(FileSystemDirectoryDatabaseTest, TestOverwritingMoveFileSuccess) {
461 FileId file_id0;
462 FileInfo info0;
463 info0.parent_id = 0;
464 info0.data_path = FilePath(FILE_PATH_LITERAL("foo"));
465 info0.name = FILE_PATH_LITERAL("file name 0");
466 info0.modification_time = base::Time::Now();
467 EXPECT_TRUE(db()->AddFileInfo(info0, &file_id0));
469 FileInfo dir_info;
470 FileId dir_id;
471 dir_info.parent_id = 0;
472 dir_info.name = FILE_PATH_LITERAL("directory name");
473 EXPECT_TRUE(db()->AddFileInfo(dir_info, &dir_id));
475 FileId file_id1;
476 FileInfo info1;
477 info1.parent_id = dir_id;
478 info1.data_path = FilePath(FILE_PATH_LITERAL("bar"));
479 info1.name = FILE_PATH_LITERAL("file name 1");
480 info1.modification_time = base::Time::UnixEpoch();
481 EXPECT_TRUE(db()->AddFileInfo(info1, &file_id1));
483 EXPECT_TRUE(db()->OverwritingMoveFile(file_id0, file_id1));
485 FileInfo check_info;
486 FileId check_id;
488 EXPECT_FALSE(db()->GetFileWithPath(FilePath(info0.name), &check_id));
489 EXPECT_TRUE(db()->GetFileWithPath(
490 FilePath(dir_info.name).Append(info1.name), &check_id));
491 EXPECT_TRUE(db()->GetFileInfo(check_id, &check_info));
493 EXPECT_EQ(info0.data_path, check_info.data_path);
496 TEST_F(FileSystemDirectoryDatabaseTest, TestGetNextInteger) {
497 int64 next = -1;
498 EXPECT_TRUE(db()->GetNextInteger(&next));
499 EXPECT_EQ(0, next);
500 EXPECT_TRUE(db()->GetNextInteger(&next));
501 EXPECT_EQ(1, next);
502 InitDatabase();
503 EXPECT_TRUE(db()->GetNextInteger(&next));
504 EXPECT_EQ(2, next);
505 EXPECT_TRUE(db()->GetNextInteger(&next));
506 EXPECT_EQ(3, next);
507 InitDatabase();
508 EXPECT_TRUE(db()->GetNextInteger(&next));
509 EXPECT_EQ(4, next);
512 TEST_F(FileSystemDirectoryDatabaseTest, TestConsistencyCheck_Empty) {
513 EXPECT_TRUE(db()->IsFileSystemConsistent());
515 int64 next = -1;
516 EXPECT_TRUE(db()->GetNextInteger(&next));
517 EXPECT_EQ(0, next);
518 EXPECT_TRUE(db()->IsFileSystemConsistent());
521 TEST_F(FileSystemDirectoryDatabaseTest, TestConsistencyCheck_Consistent) {
522 FileId dir_id;
523 CreateFile(0, FPL("foo"), FPL("hoge"), NULL);
524 CreateDirectory(0, FPL("bar"), &dir_id);
525 CreateFile(dir_id, FPL("baz"), FPL("fuga"), NULL);
526 CreateFile(dir_id, FPL("fizz"), FPL("buzz"), NULL);
528 EXPECT_TRUE(db()->IsFileSystemConsistent());
531 TEST_F(FileSystemDirectoryDatabaseTest,
532 TestConsistencyCheck_BackingMultiEntry) {
533 const FilePath::CharType kBackingFileName[] = FPL("the celeb");
534 CreateFile(0, FPL("foo"), kBackingFileName, NULL);
536 EXPECT_TRUE(db()->IsFileSystemConsistent());
537 ASSERT_TRUE(file_util::Delete(path().Append(kBackingFileName), false));
538 CreateFile(0, FPL("bar"), kBackingFileName, NULL);
539 EXPECT_FALSE(db()->IsFileSystemConsistent());
542 TEST_F(FileSystemDirectoryDatabaseTest, TestConsistencyCheck_FileLost) {
543 const FilePath::CharType kBackingFileName[] = FPL("hoge");
544 CreateFile(0, FPL("foo"), kBackingFileName, NULL);
546 EXPECT_TRUE(db()->IsFileSystemConsistent());
547 ASSERT_TRUE(file_util::Delete(path().Append(kBackingFileName), false));
548 EXPECT_TRUE(db()->IsFileSystemConsistent());
551 TEST_F(FileSystemDirectoryDatabaseTest, TestConsistencyCheck_OrphanFile) {
552 CreateFile(0, FPL("foo"), FPL("hoge"), NULL);
554 EXPECT_TRUE(db()->IsFileSystemConsistent());
556 bool created = false;
557 base::PlatformFileError error = base::PLATFORM_FILE_ERROR_FAILED;
558 base::PlatformFile file = base::CreatePlatformFile(
559 path().Append(FPL("Orphan File")),
560 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
561 &created, &error);
562 ASSERT_EQ(base::PLATFORM_FILE_OK, error);
563 ASSERT_TRUE(created);
564 ASSERT_TRUE(base::ClosePlatformFile(file));
566 EXPECT_TRUE(db()->IsFileSystemConsistent());
569 TEST_F(FileSystemDirectoryDatabaseTest, TestConsistencyCheck_RootLoop) {
570 EXPECT_TRUE(db()->IsFileSystemConsistent());
571 MakeHierarchyLink(0, 0, FPL(""));
572 EXPECT_FALSE(db()->IsFileSystemConsistent());
575 TEST_F(FileSystemDirectoryDatabaseTest, TestConsistencyCheck_DirectoryLoop) {
576 FileId dir1_id;
577 FileId dir2_id;
578 FilePath::StringType dir1_name = FPL("foo");
579 CreateDirectory(0, dir1_name, &dir1_id);
580 CreateDirectory(dir1_id, FPL("bar"), &dir2_id);
582 EXPECT_TRUE(db()->IsFileSystemConsistent());
583 MakeHierarchyLink(dir2_id, dir1_id, dir1_name);
584 EXPECT_FALSE(db()->IsFileSystemConsistent());
587 TEST_F(FileSystemDirectoryDatabaseTest, TestConsistencyCheck_NameMismatch) {
588 FileId dir_id;
589 FileId file_id;
590 CreateDirectory(0, FPL("foo"), &dir_id);
591 CreateFile(dir_id, FPL("bar"), FPL("hoge/fuga/piyo"), &file_id);
593 EXPECT_TRUE(db()->IsFileSystemConsistent());
594 DeleteHierarchyLink(file_id);
595 MakeHierarchyLink(dir_id, file_id, FPL("baz"));
596 EXPECT_FALSE(db()->IsFileSystemConsistent());
599 TEST_F(FileSystemDirectoryDatabaseTest, TestConsistencyCheck_WreckedEntries) {
600 FileId dir1_id;
601 FileId dir2_id;
602 CreateDirectory(0, FPL("foo"), &dir1_id);
603 CreateDirectory(dir1_id, FPL("bar"), &dir2_id);
604 CreateFile(dir2_id, FPL("baz"), FPL("fizz/buzz"), NULL);
606 EXPECT_TRUE(db()->IsFileSystemConsistent());
607 DeleteHierarchyLink(dir2_id); // Delete link from |dir1_id| to |dir2_id|.
608 EXPECT_FALSE(db()->IsFileSystemConsistent());
611 TEST_F(FileSystemDirectoryDatabaseTest, TestRepairDatabase_Success) {
612 FilePath::StringType kFileName = FPL("bar");
614 FileId file_id_prev;
615 CreateFile(0, FPL("foo"), FPL("hoge"), NULL);
616 CreateFile(0, kFileName, FPL("fuga"), &file_id_prev);
618 const FilePath kDatabaseDirectory = path().Append(kDirectoryDatabaseName);
619 CloseDatabase();
620 CorruptDatabase(kDatabaseDirectory, leveldb::kDescriptorFile,
621 0, std::numeric_limits<size_t>::max());
622 InitDatabase();
623 EXPECT_FALSE(db()->IsFileSystemConsistent());
625 FileId file_id;
626 EXPECT_TRUE(db()->GetChildWithName(0, kFileName, &file_id));
627 EXPECT_EQ(file_id_prev, file_id);
629 EXPECT_TRUE(db()->IsFileSystemConsistent());
632 TEST_F(FileSystemDirectoryDatabaseTest, TestRepairDatabase_Failure) {
633 FilePath::StringType kFileName = FPL("bar");
635 CreateFile(0, FPL("foo"), FPL("hoge"), NULL);
636 CreateFile(0, kFileName, FPL("fuga"), NULL);
638 const FilePath kDatabaseDirectory = path().Append(kDirectoryDatabaseName);
639 CloseDatabase();
640 CorruptDatabase(kDatabaseDirectory, leveldb::kDescriptorFile,
641 0, std::numeric_limits<size_t>::max());
642 CorruptDatabase(kDatabaseDirectory, leveldb::kLogFile,
643 -1, 1);
644 InitDatabase();
645 EXPECT_FALSE(db()->IsFileSystemConsistent());
647 FileId file_id;
648 EXPECT_FALSE(db()->GetChildWithName(0, kFileName, &file_id));
649 EXPECT_TRUE(db()->IsFileSystemConsistent());
652 } // namespace fileapi