OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <algorithm> | 5 #include <algorithm> |
6 #include <functional> | 6 #include <functional> |
7 #include <limits> | 7 #include <limits> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
12 #include "base/platform_file.h" | 12 #include "base/platform_file.h" |
13 #include "base/scoped_temp_dir.h" | 13 #include "base/scoped_temp_dir.h" |
14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "third_party/leveldatabase/src/db/filename.h" | 16 #include "third_party/leveldatabase/src/db/filename.h" |
17 #include "third_party/leveldatabase/src/include/leveldb/db.h" | 17 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
| 18 #include "webkit/fileapi/file_system_database_test_helper.h" |
18 #include "webkit/fileapi/file_system_origin_database.h" | 19 #include "webkit/fileapi/file_system_origin_database.h" |
19 #include "webkit/fileapi/file_system_util.h" | 20 #include "webkit/fileapi/file_system_util.h" |
20 | 21 |
21 namespace fileapi { | 22 namespace fileapi { |
22 | 23 |
23 namespace { | 24 namespace { |
24 | |
25 const FilePath::CharType kFileSystemDirName[] = | 25 const FilePath::CharType kFileSystemDirName[] = |
26 FILE_PATH_LITERAL("File System"); | 26 FILE_PATH_LITERAL("File System"); |
27 const FilePath::CharType kOriginDatabaseName[] = FILE_PATH_LITERAL("Origins"); | 27 const FilePath::CharType kOriginDatabaseName[] = FILE_PATH_LITERAL("Origins"); |
28 | |
29 void CorruptDatabase(const FilePath& db_path, | |
30 leveldb::FileType type, | |
31 ptrdiff_t offset, | |
32 size_t size) { | |
33 file_util::FileEnumerator file_enum( | |
34 db_path, false /* recursive */, | |
35 static_cast<file_util::FileEnumerator::FileType>( | |
36 file_util::FileEnumerator::DIRECTORIES | | |
37 file_util::FileEnumerator::FILES)); | |
38 FilePath file_path; | |
39 FilePath picked_file_path; | |
40 uint64 picked_file_number = kuint64max; | |
41 | |
42 while (!(file_path = file_enum.Next()).empty()) { | |
43 uint64 number = kuint64max; | |
44 leveldb::FileType file_type; | |
45 EXPECT_TRUE(leveldb::ParseFileName(FilePathToString(file_path.BaseName()), | |
46 &number, &file_type)); | |
47 if (file_type == type && | |
48 (picked_file_number == kuint64max || picked_file_number < number)) { | |
49 picked_file_path = file_path; | |
50 picked_file_number = number; | |
51 } | |
52 } | |
53 | |
54 EXPECT_FALSE(picked_file_path.empty()); | |
55 EXPECT_NE(kuint64max, picked_file_number); | |
56 | |
57 bool created = true; | |
58 base::PlatformFileError error = base::PLATFORM_FILE_ERROR_FAILED; | |
59 base::PlatformFile file = | |
60 CreatePlatformFile(picked_file_path, | |
61 base::PLATFORM_FILE_OPEN | | |
62 base::PLATFORM_FILE_READ | | |
63 base::PLATFORM_FILE_WRITE, | |
64 &created, &error); | |
65 EXPECT_EQ(base::PLATFORM_FILE_OK, error); | |
66 EXPECT_FALSE(created); | |
67 | |
68 base::PlatformFileInfo file_info; | |
69 EXPECT_TRUE(base::GetPlatformFileInfo(file, &file_info)); | |
70 if (offset < 0) | |
71 offset += file_info.size; | |
72 EXPECT_GE(offset, 0); | |
73 EXPECT_LE(offset, file_info.size); | |
74 | |
75 size = std::min(size, static_cast<size_t>(file_info.size - offset)); | |
76 | |
77 std::vector<char> buf(size); | |
78 int read_size = base::ReadPlatformFile(file, offset, | |
79 vector_as_array(&buf), buf.size()); | |
80 EXPECT_LT(0, read_size); | |
81 EXPECT_GE(buf.size(), static_cast<size_t>(read_size)); | |
82 buf.resize(read_size); | |
83 | |
84 std::transform(buf.begin(), buf.end(), buf.begin(), | |
85 std::logical_not<char>()); | |
86 | |
87 int written_size = base::WritePlatformFile(file, offset, | |
88 vector_as_array(&buf), buf.size()); | |
89 EXPECT_GT(written_size, 0); | |
90 EXPECT_EQ(buf.size(), static_cast<size_t>(written_size)); | |
91 | |
92 base::ClosePlatformFile(file); | |
93 } | |
94 | |
95 } // namespace | 28 } // namespace |
96 | 29 |
97 TEST(FileSystemOriginDatabaseTest, BasicTest) { | 30 TEST(FileSystemOriginDatabaseTest, BasicTest) { |
98 ScopedTempDir dir; | 31 ScopedTempDir dir; |
99 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 32 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
100 const FilePath kFSDir = dir.path().Append(kFileSystemDirName); | 33 const FilePath kFSDir = dir.path().Append(kFileSystemDirName); |
101 EXPECT_FALSE(file_util::PathExists(kFSDir)); | 34 EXPECT_FALSE(file_util::PathExists(kFSDir)); |
102 EXPECT_TRUE(file_util::CreateDirectory(kFSDir)); | 35 EXPECT_TRUE(file_util::CreateDirectory(kFSDir)); |
103 | 36 |
104 FileSystemOriginDatabase database(kFSDir); | 37 FileSystemOriginDatabase database(kFSDir); |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
319 EXPECT_FALSE(database->HasOriginPath(kOrigin)); | 252 EXPECT_FALSE(database->HasOriginPath(kOrigin)); |
320 EXPECT_TRUE(database->GetPathForOrigin(kOrigin, &path)); | 253 EXPECT_TRUE(database->GetPathForOrigin(kOrigin, &path)); |
321 EXPECT_FALSE(path.empty()); | 254 EXPECT_FALSE(path.empty()); |
322 EXPECT_TRUE(database->HasOriginPath(kOrigin)); | 255 EXPECT_TRUE(database->HasOriginPath(kOrigin)); |
323 | 256 |
324 EXPECT_FALSE(file_util::PathExists(kGarbageFile)); | 257 EXPECT_FALSE(file_util::PathExists(kGarbageFile)); |
325 EXPECT_FALSE(file_util::PathExists(kGarbageDir)); | 258 EXPECT_FALSE(file_util::PathExists(kGarbageDir)); |
326 } | 259 } |
327 | 260 |
328 } // namespace fileapi | 261 } // namespace fileapi |
OLD | NEW |