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_util.h" | 10 #include "base/file_util.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 const base::FilePath::CharType kFileSystemDirName[] = | 25 const base::FilePath::CharType kFileSystemDirName[] = |
26 FILE_PATH_LITERAL("File System"); | 26 FILE_PATH_LITERAL("File System"); |
27 const base::FilePath::CharType kOriginDatabaseName[] = | 27 const base::FilePath::CharType kOriginDatabaseName[] = |
28 FILE_PATH_LITERAL("Origins"); | 28 FILE_PATH_LITERAL("Origins"); |
29 } // namespace | 29 } // namespace |
30 | 30 |
31 TEST(SandboxOriginDatabaseTest, BasicTest) { | 31 TEST(SandboxOriginDatabaseTest, BasicTest) { |
32 base::ScopedTempDir dir; | 32 base::ScopedTempDir dir; |
33 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 33 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
34 const base::FilePath kFSDir = dir.path().Append(kFileSystemDirName); | 34 const base::FilePath kFSDir = dir.path().Append(kFileSystemDirName); |
35 EXPECT_FALSE(file_util::PathExists(kFSDir)); | 35 EXPECT_FALSE(base::PathExists(kFSDir)); |
36 EXPECT_TRUE(file_util::CreateDirectory(kFSDir)); | 36 EXPECT_TRUE(file_util::CreateDirectory(kFSDir)); |
37 | 37 |
38 SandboxOriginDatabase database(kFSDir); | 38 SandboxOriginDatabase database(kFSDir); |
39 std::string origin("origin"); | 39 std::string origin("origin"); |
40 | 40 |
41 EXPECT_FALSE(database.HasOriginPath(origin)); | 41 EXPECT_FALSE(database.HasOriginPath(origin)); |
42 // Double-check to make sure that had no side effects. | 42 // Double-check to make sure that had no side effects. |
43 EXPECT_FALSE(database.HasOriginPath(origin)); | 43 EXPECT_FALSE(database.HasOriginPath(origin)); |
44 | 44 |
45 base::FilePath path0; | 45 base::FilePath path0; |
46 base::FilePath path1; | 46 base::FilePath path1; |
47 | 47 |
48 // Empty strings aren't valid origins. | 48 // Empty strings aren't valid origins. |
49 EXPECT_FALSE(database.GetPathForOrigin(std::string(), &path0)); | 49 EXPECT_FALSE(database.GetPathForOrigin(std::string(), &path0)); |
50 | 50 |
51 EXPECT_TRUE(database.GetPathForOrigin(origin, &path0)); | 51 EXPECT_TRUE(database.GetPathForOrigin(origin, &path0)); |
52 EXPECT_TRUE(database.HasOriginPath(origin)); | 52 EXPECT_TRUE(database.HasOriginPath(origin)); |
53 EXPECT_TRUE(database.GetPathForOrigin(origin, &path1)); | 53 EXPECT_TRUE(database.GetPathForOrigin(origin, &path1)); |
54 EXPECT_FALSE(path0.empty()); | 54 EXPECT_FALSE(path0.empty()); |
55 EXPECT_FALSE(path1.empty()); | 55 EXPECT_FALSE(path1.empty()); |
56 EXPECT_EQ(path0, path1); | 56 EXPECT_EQ(path0, path1); |
57 | 57 |
58 EXPECT_TRUE(file_util::PathExists(kFSDir.Append(kOriginDatabaseName))); | 58 EXPECT_TRUE(base::PathExists(kFSDir.Append(kOriginDatabaseName))); |
59 } | 59 } |
60 | 60 |
61 TEST(SandboxOriginDatabaseTest, TwoPathTest) { | 61 TEST(SandboxOriginDatabaseTest, TwoPathTest) { |
62 base::ScopedTempDir dir; | 62 base::ScopedTempDir dir; |
63 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 63 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
64 const base::FilePath kFSDir = dir.path().Append(kFileSystemDirName); | 64 const base::FilePath kFSDir = dir.path().Append(kFileSystemDirName); |
65 EXPECT_FALSE(file_util::PathExists(kFSDir)); | 65 EXPECT_FALSE(base::PathExists(kFSDir)); |
66 EXPECT_TRUE(file_util::CreateDirectory(kFSDir)); | 66 EXPECT_TRUE(file_util::CreateDirectory(kFSDir)); |
67 | 67 |
68 SandboxOriginDatabase database(kFSDir); | 68 SandboxOriginDatabase database(kFSDir); |
69 std::string origin0("origin0"); | 69 std::string origin0("origin0"); |
70 std::string origin1("origin1"); | 70 std::string origin1("origin1"); |
71 | 71 |
72 EXPECT_FALSE(database.HasOriginPath(origin0)); | 72 EXPECT_FALSE(database.HasOriginPath(origin0)); |
73 EXPECT_FALSE(database.HasOriginPath(origin1)); | 73 EXPECT_FALSE(database.HasOriginPath(origin1)); |
74 | 74 |
75 base::FilePath path0; | 75 base::FilePath path0; |
76 base::FilePath path1; | 76 base::FilePath path1; |
77 EXPECT_TRUE(database.GetPathForOrigin(origin0, &path0)); | 77 EXPECT_TRUE(database.GetPathForOrigin(origin0, &path0)); |
78 EXPECT_TRUE(database.HasOriginPath(origin0)); | 78 EXPECT_TRUE(database.HasOriginPath(origin0)); |
79 EXPECT_FALSE(database.HasOriginPath(origin1)); | 79 EXPECT_FALSE(database.HasOriginPath(origin1)); |
80 EXPECT_TRUE(database.GetPathForOrigin(origin1, &path1)); | 80 EXPECT_TRUE(database.GetPathForOrigin(origin1, &path1)); |
81 EXPECT_TRUE(database.HasOriginPath(origin1)); | 81 EXPECT_TRUE(database.HasOriginPath(origin1)); |
82 EXPECT_FALSE(path0.empty()); | 82 EXPECT_FALSE(path0.empty()); |
83 EXPECT_FALSE(path1.empty()); | 83 EXPECT_FALSE(path1.empty()); |
84 EXPECT_NE(path0, path1); | 84 EXPECT_NE(path0, path1); |
85 | 85 |
86 EXPECT_TRUE(file_util::PathExists(kFSDir.Append(kOriginDatabaseName))); | 86 EXPECT_TRUE(base::PathExists(kFSDir.Append(kOriginDatabaseName))); |
87 } | 87 } |
88 | 88 |
89 TEST(SandboxOriginDatabaseTest, DropDatabaseTest) { | 89 TEST(SandboxOriginDatabaseTest, DropDatabaseTest) { |
90 base::ScopedTempDir dir; | 90 base::ScopedTempDir dir; |
91 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 91 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
92 const base::FilePath kFSDir = dir.path().Append(kFileSystemDirName); | 92 const base::FilePath kFSDir = dir.path().Append(kFileSystemDirName); |
93 EXPECT_FALSE(file_util::PathExists(kFSDir)); | 93 EXPECT_FALSE(base::PathExists(kFSDir)); |
94 EXPECT_TRUE(file_util::CreateDirectory(kFSDir)); | 94 EXPECT_TRUE(file_util::CreateDirectory(kFSDir)); |
95 | 95 |
96 SandboxOriginDatabase database(kFSDir); | 96 SandboxOriginDatabase database(kFSDir); |
97 std::string origin("origin"); | 97 std::string origin("origin"); |
98 | 98 |
99 EXPECT_FALSE(database.HasOriginPath(origin)); | 99 EXPECT_FALSE(database.HasOriginPath(origin)); |
100 | 100 |
101 base::FilePath path0; | 101 base::FilePath path0; |
102 EXPECT_TRUE(database.GetPathForOrigin(origin, &path0)); | 102 EXPECT_TRUE(database.GetPathForOrigin(origin, &path0)); |
103 EXPECT_TRUE(database.HasOriginPath(origin)); | 103 EXPECT_TRUE(database.HasOriginPath(origin)); |
104 EXPECT_FALSE(path0.empty()); | 104 EXPECT_FALSE(path0.empty()); |
105 | 105 |
106 EXPECT_TRUE(file_util::PathExists(kFSDir.Append(kOriginDatabaseName))); | 106 EXPECT_TRUE(base::PathExists(kFSDir.Append(kOriginDatabaseName))); |
107 | 107 |
108 database.DropDatabase(); | 108 database.DropDatabase(); |
109 | 109 |
110 base::FilePath path1; | 110 base::FilePath path1; |
111 EXPECT_TRUE(database.HasOriginPath(origin)); | 111 EXPECT_TRUE(database.HasOriginPath(origin)); |
112 EXPECT_TRUE(database.GetPathForOrigin(origin, &path1)); | 112 EXPECT_TRUE(database.GetPathForOrigin(origin, &path1)); |
113 EXPECT_FALSE(path1.empty()); | 113 EXPECT_FALSE(path1.empty()); |
114 EXPECT_EQ(path0, path1); | 114 EXPECT_EQ(path0, path1); |
115 } | 115 } |
116 | 116 |
117 TEST(SandboxOriginDatabaseTest, DeleteOriginTest) { | 117 TEST(SandboxOriginDatabaseTest, DeleteOriginTest) { |
118 base::ScopedTempDir dir; | 118 base::ScopedTempDir dir; |
119 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 119 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
120 const base::FilePath kFSDir = dir.path().Append(kFileSystemDirName); | 120 const base::FilePath kFSDir = dir.path().Append(kFileSystemDirName); |
121 EXPECT_FALSE(file_util::PathExists(kFSDir)); | 121 EXPECT_FALSE(base::PathExists(kFSDir)); |
122 EXPECT_TRUE(file_util::CreateDirectory(kFSDir)); | 122 EXPECT_TRUE(file_util::CreateDirectory(kFSDir)); |
123 | 123 |
124 SandboxOriginDatabase database(kFSDir); | 124 SandboxOriginDatabase database(kFSDir); |
125 std::string origin("origin"); | 125 std::string origin("origin"); |
126 | 126 |
127 EXPECT_FALSE(database.HasOriginPath(origin)); | 127 EXPECT_FALSE(database.HasOriginPath(origin)); |
128 EXPECT_TRUE(database.RemovePathForOrigin(origin)); | 128 EXPECT_TRUE(database.RemovePathForOrigin(origin)); |
129 | 129 |
130 base::FilePath path0; | 130 base::FilePath path0; |
131 EXPECT_TRUE(database.GetPathForOrigin(origin, &path0)); | 131 EXPECT_TRUE(database.GetPathForOrigin(origin, &path0)); |
132 EXPECT_TRUE(database.HasOriginPath(origin)); | 132 EXPECT_TRUE(database.HasOriginPath(origin)); |
133 EXPECT_FALSE(path0.empty()); | 133 EXPECT_FALSE(path0.empty()); |
134 | 134 |
135 EXPECT_TRUE(database.RemovePathForOrigin(origin)); | 135 EXPECT_TRUE(database.RemovePathForOrigin(origin)); |
136 EXPECT_FALSE(database.HasOriginPath(origin)); | 136 EXPECT_FALSE(database.HasOriginPath(origin)); |
137 | 137 |
138 base::FilePath path1; | 138 base::FilePath path1; |
139 EXPECT_TRUE(database.GetPathForOrigin(origin, &path1)); | 139 EXPECT_TRUE(database.GetPathForOrigin(origin, &path1)); |
140 EXPECT_FALSE(path1.empty()); | 140 EXPECT_FALSE(path1.empty()); |
141 EXPECT_NE(path0, path1); | 141 EXPECT_NE(path0, path1); |
142 } | 142 } |
143 | 143 |
144 TEST(SandboxOriginDatabaseTest, ListOriginsTest) { | 144 TEST(SandboxOriginDatabaseTest, ListOriginsTest) { |
145 base::ScopedTempDir dir; | 145 base::ScopedTempDir dir; |
146 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 146 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
147 const base::FilePath kFSDir = dir.path().Append(kFileSystemDirName); | 147 const base::FilePath kFSDir = dir.path().Append(kFileSystemDirName); |
148 EXPECT_FALSE(file_util::PathExists(kFSDir)); | 148 EXPECT_FALSE(base::PathExists(kFSDir)); |
149 EXPECT_TRUE(file_util::CreateDirectory(kFSDir)); | 149 EXPECT_TRUE(file_util::CreateDirectory(kFSDir)); |
150 | 150 |
151 std::vector<SandboxOriginDatabase::OriginRecord> origins; | 151 std::vector<SandboxOriginDatabase::OriginRecord> origins; |
152 | 152 |
153 SandboxOriginDatabase database(kFSDir); | 153 SandboxOriginDatabase database(kFSDir); |
154 EXPECT_TRUE(database.ListAllOrigins(&origins)); | 154 EXPECT_TRUE(database.ListAllOrigins(&origins)); |
155 EXPECT_TRUE(origins.empty()); | 155 EXPECT_TRUE(origins.empty()); |
156 origins.clear(); | 156 origins.clear(); |
157 | 157 |
158 std::string origin0("origin0"); | 158 std::string origin0("origin0"); |
(...skipping 29 matching lines...) Expand all Loading... |
188 // Checks if SandboxOriginDatabase properly handles database corruption. | 188 // Checks if SandboxOriginDatabase properly handles database corruption. |
189 // In this test, we'll register some origins to the origin database, then | 189 // In this test, we'll register some origins to the origin database, then |
190 // corrupt database and its log file. | 190 // corrupt database and its log file. |
191 // After repairing, the origin database should be consistent even when some | 191 // After repairing, the origin database should be consistent even when some |
192 // entries lost. | 192 // entries lost. |
193 | 193 |
194 base::ScopedTempDir dir; | 194 base::ScopedTempDir dir; |
195 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 195 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
196 const base::FilePath kFSDir = dir.path().Append(kFileSystemDirName); | 196 const base::FilePath kFSDir = dir.path().Append(kFileSystemDirName); |
197 const base::FilePath kDBDir = kFSDir.Append(kOriginDatabaseName); | 197 const base::FilePath kDBDir = kFSDir.Append(kOriginDatabaseName); |
198 EXPECT_FALSE(file_util::PathExists(kFSDir)); | 198 EXPECT_FALSE(base::PathExists(kFSDir)); |
199 EXPECT_TRUE(file_util::CreateDirectory(kFSDir)); | 199 EXPECT_TRUE(file_util::CreateDirectory(kFSDir)); |
200 | 200 |
201 const std::string kOrigins[] = { | 201 const std::string kOrigins[] = { |
202 "foo.example.com", | 202 "foo.example.com", |
203 "bar.example.com", | 203 "bar.example.com", |
204 "baz.example.com", | 204 "baz.example.com", |
205 "hoge.example.com", | 205 "hoge.example.com", |
206 "fuga.example.com", | 206 "fuga.example.com", |
207 }; | 207 }; |
208 | 208 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 // Expect all but last added origin will be repaired back, and kOrigins[1] | 248 // Expect all but last added origin will be repaired back, and kOrigins[1] |
249 // should be dropped due to absence of backing directory. | 249 // should be dropped due to absence of backing directory. |
250 EXPECT_EQ(arraysize(kOrigins) - 2, origins_in_db.size()); | 250 EXPECT_EQ(arraysize(kOrigins) - 2, origins_in_db.size()); |
251 | 251 |
252 const std::string kOrigin("piyo.example.org"); | 252 const std::string kOrigin("piyo.example.org"); |
253 EXPECT_FALSE(database->HasOriginPath(kOrigin)); | 253 EXPECT_FALSE(database->HasOriginPath(kOrigin)); |
254 EXPECT_TRUE(database->GetPathForOrigin(kOrigin, &path)); | 254 EXPECT_TRUE(database->GetPathForOrigin(kOrigin, &path)); |
255 EXPECT_FALSE(path.empty()); | 255 EXPECT_FALSE(path.empty()); |
256 EXPECT_TRUE(database->HasOriginPath(kOrigin)); | 256 EXPECT_TRUE(database->HasOriginPath(kOrigin)); |
257 | 257 |
258 EXPECT_FALSE(file_util::PathExists(kGarbageFile)); | 258 EXPECT_FALSE(base::PathExists(kGarbageFile)); |
259 EXPECT_FALSE(file_util::PathExists(kGarbageDir)); | 259 EXPECT_FALSE(base::PathExists(kGarbageDir)); |
260 } | 260 } |
261 | 261 |
262 TEST(SandboxOriginDatabaseTest, DatabaseRecoveryForMissingDBFileTest) { | 262 TEST(SandboxOriginDatabaseTest, DatabaseRecoveryForMissingDBFileTest) { |
263 const leveldb::FileType kLevelDBFileTypes[] = { | 263 const leveldb::FileType kLevelDBFileTypes[] = { |
264 leveldb::kLogFile, | 264 leveldb::kLogFile, |
265 leveldb::kDBLockFile, | 265 leveldb::kDBLockFile, |
266 leveldb::kTableFile, | 266 leveldb::kTableFile, |
267 leveldb::kDescriptorFile, | 267 leveldb::kDescriptorFile, |
268 leveldb::kCurrentFile, | 268 leveldb::kCurrentFile, |
269 leveldb::kTempFile, | 269 leveldb::kTempFile, |
270 leveldb::kInfoLogFile, | 270 leveldb::kInfoLogFile, |
271 }; | 271 }; |
272 | 272 |
273 for (size_t i = 0; i < arraysize(kLevelDBFileTypes); ++i) { | 273 for (size_t i = 0; i < arraysize(kLevelDBFileTypes); ++i) { |
274 base::ScopedTempDir dir; | 274 base::ScopedTempDir dir; |
275 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 275 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
276 const base::FilePath kFSDir = dir.path().Append(kFileSystemDirName); | 276 const base::FilePath kFSDir = dir.path().Append(kFileSystemDirName); |
277 const base::FilePath kDBDir = kFSDir.Append(kOriginDatabaseName); | 277 const base::FilePath kDBDir = kFSDir.Append(kOriginDatabaseName); |
278 EXPECT_FALSE(file_util::PathExists(kFSDir)); | 278 EXPECT_FALSE(base::PathExists(kFSDir)); |
279 EXPECT_TRUE(file_util::CreateDirectory(kFSDir)); | 279 EXPECT_TRUE(file_util::CreateDirectory(kFSDir)); |
280 | 280 |
281 const std::string kOrigin = "foo.example.com"; | 281 const std::string kOrigin = "foo.example.com"; |
282 base::FilePath path; | 282 base::FilePath path; |
283 | 283 |
284 scoped_ptr<SandboxOriginDatabase> database( | 284 scoped_ptr<SandboxOriginDatabase> database( |
285 new SandboxOriginDatabase(kFSDir)); | 285 new SandboxOriginDatabase(kFSDir)); |
286 EXPECT_FALSE(database->HasOriginPath(kOrigin)); | 286 EXPECT_FALSE(database->HasOriginPath(kOrigin)); |
287 EXPECT_TRUE(database->GetPathForOrigin(kOrigin, &path)); | 287 EXPECT_TRUE(database->GetPathForOrigin(kOrigin, &path)); |
288 EXPECT_FALSE(path.empty()); | 288 EXPECT_FALSE(path.empty()); |
289 EXPECT_TRUE(database->GetPathForOrigin(kOrigin, &path)); | 289 EXPECT_TRUE(database->GetPathForOrigin(kOrigin, &path)); |
290 EXPECT_TRUE(file_util::CreateDirectory(kFSDir.Append(path))); | 290 EXPECT_TRUE(file_util::CreateDirectory(kFSDir.Append(path))); |
291 database.reset(); | 291 database.reset(); |
292 | 292 |
293 DeleteDatabaseFile(kDBDir, kLevelDBFileTypes[i]); | 293 DeleteDatabaseFile(kDBDir, kLevelDBFileTypes[i]); |
294 | 294 |
295 database.reset(new SandboxOriginDatabase(kFSDir)); | 295 database.reset(new SandboxOriginDatabase(kFSDir)); |
296 std::vector<SandboxOriginDatabase::OriginRecord> origins_in_db; | 296 std::vector<SandboxOriginDatabase::OriginRecord> origins_in_db; |
297 EXPECT_TRUE(database->ListAllOrigins(&origins_in_db)); | 297 EXPECT_TRUE(database->ListAllOrigins(&origins_in_db)); |
298 | 298 |
299 const std::string kOrigin2("piyo.example.org"); | 299 const std::string kOrigin2("piyo.example.org"); |
300 EXPECT_FALSE(database->HasOriginPath(kOrigin2)); | 300 EXPECT_FALSE(database->HasOriginPath(kOrigin2)); |
301 EXPECT_TRUE(database->GetPathForOrigin(kOrigin2, &path)); | 301 EXPECT_TRUE(database->GetPathForOrigin(kOrigin2, &path)); |
302 EXPECT_FALSE(path.empty()); | 302 EXPECT_FALSE(path.empty()); |
303 EXPECT_TRUE(database->HasOriginPath(kOrigin2)); | 303 EXPECT_TRUE(database->HasOriginPath(kOrigin2)); |
304 } | 304 } |
305 } | 305 } |
306 | 306 |
307 } // namespace fileapi | 307 } // namespace fileapi |
OLD | NEW |