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 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 16 matching lines...) Expand all Loading... |
27 FILE_PATH_LITERAL("File System"); | 27 FILE_PATH_LITERAL("File System"); |
28 const base::FilePath::CharType kOriginDatabaseName[] = | 28 const base::FilePath::CharType kOriginDatabaseName[] = |
29 FILE_PATH_LITERAL("Origins"); | 29 FILE_PATH_LITERAL("Origins"); |
30 } // namespace | 30 } // namespace |
31 | 31 |
32 TEST(SandboxOriginDatabaseTest, BasicTest) { | 32 TEST(SandboxOriginDatabaseTest, BasicTest) { |
33 base::ScopedTempDir dir; | 33 base::ScopedTempDir dir; |
34 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 34 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
35 const base::FilePath kFSDir = dir.path().Append(kFileSystemDirName); | 35 const base::FilePath kFSDir = dir.path().Append(kFileSystemDirName); |
36 EXPECT_FALSE(base::PathExists(kFSDir)); | 36 EXPECT_FALSE(base::PathExists(kFSDir)); |
37 EXPECT_TRUE(file_util::CreateDirectory(kFSDir)); | 37 EXPECT_TRUE(base::CreateDirectory(kFSDir)); |
38 | 38 |
39 SandboxOriginDatabase database(kFSDir); | 39 SandboxOriginDatabase database(kFSDir); |
40 std::string origin("origin"); | 40 std::string origin("origin"); |
41 | 41 |
42 EXPECT_FALSE(database.HasOriginPath(origin)); | 42 EXPECT_FALSE(database.HasOriginPath(origin)); |
43 // Double-check to make sure that had no side effects. | 43 // Double-check to make sure that had no side effects. |
44 EXPECT_FALSE(database.HasOriginPath(origin)); | 44 EXPECT_FALSE(database.HasOriginPath(origin)); |
45 | 45 |
46 base::FilePath path0; | 46 base::FilePath path0; |
47 base::FilePath path1; | 47 base::FilePath path1; |
48 | 48 |
49 // Empty strings aren't valid origins. | 49 // Empty strings aren't valid origins. |
50 EXPECT_FALSE(database.GetPathForOrigin(std::string(), &path0)); | 50 EXPECT_FALSE(database.GetPathForOrigin(std::string(), &path0)); |
51 | 51 |
52 EXPECT_TRUE(database.GetPathForOrigin(origin, &path0)); | 52 EXPECT_TRUE(database.GetPathForOrigin(origin, &path0)); |
53 EXPECT_TRUE(database.HasOriginPath(origin)); | 53 EXPECT_TRUE(database.HasOriginPath(origin)); |
54 EXPECT_TRUE(database.GetPathForOrigin(origin, &path1)); | 54 EXPECT_TRUE(database.GetPathForOrigin(origin, &path1)); |
55 EXPECT_FALSE(path0.empty()); | 55 EXPECT_FALSE(path0.empty()); |
56 EXPECT_FALSE(path1.empty()); | 56 EXPECT_FALSE(path1.empty()); |
57 EXPECT_EQ(path0, path1); | 57 EXPECT_EQ(path0, path1); |
58 | 58 |
59 EXPECT_TRUE(base::PathExists(kFSDir.Append(kOriginDatabaseName))); | 59 EXPECT_TRUE(base::PathExists(kFSDir.Append(kOriginDatabaseName))); |
60 } | 60 } |
61 | 61 |
62 TEST(SandboxOriginDatabaseTest, TwoPathTest) { | 62 TEST(SandboxOriginDatabaseTest, TwoPathTest) { |
63 base::ScopedTempDir dir; | 63 base::ScopedTempDir dir; |
64 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 64 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
65 const base::FilePath kFSDir = dir.path().Append(kFileSystemDirName); | 65 const base::FilePath kFSDir = dir.path().Append(kFileSystemDirName); |
66 EXPECT_FALSE(base::PathExists(kFSDir)); | 66 EXPECT_FALSE(base::PathExists(kFSDir)); |
67 EXPECT_TRUE(file_util::CreateDirectory(kFSDir)); | 67 EXPECT_TRUE(base::CreateDirectory(kFSDir)); |
68 | 68 |
69 SandboxOriginDatabase database(kFSDir); | 69 SandboxOriginDatabase database(kFSDir); |
70 std::string origin0("origin0"); | 70 std::string origin0("origin0"); |
71 std::string origin1("origin1"); | 71 std::string origin1("origin1"); |
72 | 72 |
73 EXPECT_FALSE(database.HasOriginPath(origin0)); | 73 EXPECT_FALSE(database.HasOriginPath(origin0)); |
74 EXPECT_FALSE(database.HasOriginPath(origin1)); | 74 EXPECT_FALSE(database.HasOriginPath(origin1)); |
75 | 75 |
76 base::FilePath path0; | 76 base::FilePath path0; |
77 base::FilePath path1; | 77 base::FilePath path1; |
78 EXPECT_TRUE(database.GetPathForOrigin(origin0, &path0)); | 78 EXPECT_TRUE(database.GetPathForOrigin(origin0, &path0)); |
79 EXPECT_TRUE(database.HasOriginPath(origin0)); | 79 EXPECT_TRUE(database.HasOriginPath(origin0)); |
80 EXPECT_FALSE(database.HasOriginPath(origin1)); | 80 EXPECT_FALSE(database.HasOriginPath(origin1)); |
81 EXPECT_TRUE(database.GetPathForOrigin(origin1, &path1)); | 81 EXPECT_TRUE(database.GetPathForOrigin(origin1, &path1)); |
82 EXPECT_TRUE(database.HasOriginPath(origin1)); | 82 EXPECT_TRUE(database.HasOriginPath(origin1)); |
83 EXPECT_FALSE(path0.empty()); | 83 EXPECT_FALSE(path0.empty()); |
84 EXPECT_FALSE(path1.empty()); | 84 EXPECT_FALSE(path1.empty()); |
85 EXPECT_NE(path0, path1); | 85 EXPECT_NE(path0, path1); |
86 | 86 |
87 EXPECT_TRUE(base::PathExists(kFSDir.Append(kOriginDatabaseName))); | 87 EXPECT_TRUE(base::PathExists(kFSDir.Append(kOriginDatabaseName))); |
88 } | 88 } |
89 | 89 |
90 TEST(SandboxOriginDatabaseTest, DropDatabaseTest) { | 90 TEST(SandboxOriginDatabaseTest, DropDatabaseTest) { |
91 base::ScopedTempDir dir; | 91 base::ScopedTempDir dir; |
92 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 92 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
93 const base::FilePath kFSDir = dir.path().Append(kFileSystemDirName); | 93 const base::FilePath kFSDir = dir.path().Append(kFileSystemDirName); |
94 EXPECT_FALSE(base::PathExists(kFSDir)); | 94 EXPECT_FALSE(base::PathExists(kFSDir)); |
95 EXPECT_TRUE(file_util::CreateDirectory(kFSDir)); | 95 EXPECT_TRUE(base::CreateDirectory(kFSDir)); |
96 | 96 |
97 SandboxOriginDatabase database(kFSDir); | 97 SandboxOriginDatabase database(kFSDir); |
98 std::string origin("origin"); | 98 std::string origin("origin"); |
99 | 99 |
100 EXPECT_FALSE(database.HasOriginPath(origin)); | 100 EXPECT_FALSE(database.HasOriginPath(origin)); |
101 | 101 |
102 base::FilePath path0; | 102 base::FilePath path0; |
103 EXPECT_TRUE(database.GetPathForOrigin(origin, &path0)); | 103 EXPECT_TRUE(database.GetPathForOrigin(origin, &path0)); |
104 EXPECT_TRUE(database.HasOriginPath(origin)); | 104 EXPECT_TRUE(database.HasOriginPath(origin)); |
105 EXPECT_FALSE(path0.empty()); | 105 EXPECT_FALSE(path0.empty()); |
106 | 106 |
107 EXPECT_TRUE(base::PathExists(kFSDir.Append(kOriginDatabaseName))); | 107 EXPECT_TRUE(base::PathExists(kFSDir.Append(kOriginDatabaseName))); |
108 | 108 |
109 database.DropDatabase(); | 109 database.DropDatabase(); |
110 | 110 |
111 base::FilePath path1; | 111 base::FilePath path1; |
112 EXPECT_TRUE(database.HasOriginPath(origin)); | 112 EXPECT_TRUE(database.HasOriginPath(origin)); |
113 EXPECT_TRUE(database.GetPathForOrigin(origin, &path1)); | 113 EXPECT_TRUE(database.GetPathForOrigin(origin, &path1)); |
114 EXPECT_FALSE(path1.empty()); | 114 EXPECT_FALSE(path1.empty()); |
115 EXPECT_EQ(path0, path1); | 115 EXPECT_EQ(path0, path1); |
116 } | 116 } |
117 | 117 |
118 TEST(SandboxOriginDatabaseTest, DeleteOriginTest) { | 118 TEST(SandboxOriginDatabaseTest, DeleteOriginTest) { |
119 base::ScopedTempDir dir; | 119 base::ScopedTempDir dir; |
120 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 120 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
121 const base::FilePath kFSDir = dir.path().Append(kFileSystemDirName); | 121 const base::FilePath kFSDir = dir.path().Append(kFileSystemDirName); |
122 EXPECT_FALSE(base::PathExists(kFSDir)); | 122 EXPECT_FALSE(base::PathExists(kFSDir)); |
123 EXPECT_TRUE(file_util::CreateDirectory(kFSDir)); | 123 EXPECT_TRUE(base::CreateDirectory(kFSDir)); |
124 | 124 |
125 SandboxOriginDatabase database(kFSDir); | 125 SandboxOriginDatabase database(kFSDir); |
126 std::string origin("origin"); | 126 std::string origin("origin"); |
127 | 127 |
128 EXPECT_FALSE(database.HasOriginPath(origin)); | 128 EXPECT_FALSE(database.HasOriginPath(origin)); |
129 EXPECT_TRUE(database.RemovePathForOrigin(origin)); | 129 EXPECT_TRUE(database.RemovePathForOrigin(origin)); |
130 | 130 |
131 base::FilePath path0; | 131 base::FilePath path0; |
132 EXPECT_TRUE(database.GetPathForOrigin(origin, &path0)); | 132 EXPECT_TRUE(database.GetPathForOrigin(origin, &path0)); |
133 EXPECT_TRUE(database.HasOriginPath(origin)); | 133 EXPECT_TRUE(database.HasOriginPath(origin)); |
134 EXPECT_FALSE(path0.empty()); | 134 EXPECT_FALSE(path0.empty()); |
135 | 135 |
136 EXPECT_TRUE(database.RemovePathForOrigin(origin)); | 136 EXPECT_TRUE(database.RemovePathForOrigin(origin)); |
137 EXPECT_FALSE(database.HasOriginPath(origin)); | 137 EXPECT_FALSE(database.HasOriginPath(origin)); |
138 | 138 |
139 base::FilePath path1; | 139 base::FilePath path1; |
140 EXPECT_TRUE(database.GetPathForOrigin(origin, &path1)); | 140 EXPECT_TRUE(database.GetPathForOrigin(origin, &path1)); |
141 EXPECT_FALSE(path1.empty()); | 141 EXPECT_FALSE(path1.empty()); |
142 EXPECT_NE(path0, path1); | 142 EXPECT_NE(path0, path1); |
143 } | 143 } |
144 | 144 |
145 TEST(SandboxOriginDatabaseTest, ListOriginsTest) { | 145 TEST(SandboxOriginDatabaseTest, ListOriginsTest) { |
146 base::ScopedTempDir dir; | 146 base::ScopedTempDir dir; |
147 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 147 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
148 const base::FilePath kFSDir = dir.path().Append(kFileSystemDirName); | 148 const base::FilePath kFSDir = dir.path().Append(kFileSystemDirName); |
149 EXPECT_FALSE(base::PathExists(kFSDir)); | 149 EXPECT_FALSE(base::PathExists(kFSDir)); |
150 EXPECT_TRUE(file_util::CreateDirectory(kFSDir)); | 150 EXPECT_TRUE(base::CreateDirectory(kFSDir)); |
151 | 151 |
152 std::vector<SandboxOriginDatabase::OriginRecord> origins; | 152 std::vector<SandboxOriginDatabase::OriginRecord> origins; |
153 | 153 |
154 SandboxOriginDatabase database(kFSDir); | 154 SandboxOriginDatabase database(kFSDir); |
155 EXPECT_TRUE(database.ListAllOrigins(&origins)); | 155 EXPECT_TRUE(database.ListAllOrigins(&origins)); |
156 EXPECT_TRUE(origins.empty()); | 156 EXPECT_TRUE(origins.empty()); |
157 origins.clear(); | 157 origins.clear(); |
158 | 158 |
159 std::string origin0("origin0"); | 159 std::string origin0("origin0"); |
160 std::string origin1("origin1"); | 160 std::string origin1("origin1"); |
(...skipping 29 matching lines...) Expand all Loading... |
190 // In this test, we'll register some origins to the origin database, then | 190 // In this test, we'll register some origins to the origin database, then |
191 // corrupt database and its log file. | 191 // corrupt database and its log file. |
192 // After repairing, the origin database should be consistent even when some | 192 // After repairing, the origin database should be consistent even when some |
193 // entries lost. | 193 // entries lost. |
194 | 194 |
195 base::ScopedTempDir dir; | 195 base::ScopedTempDir dir; |
196 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 196 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
197 const base::FilePath kFSDir = dir.path().Append(kFileSystemDirName); | 197 const base::FilePath kFSDir = dir.path().Append(kFileSystemDirName); |
198 const base::FilePath kDBDir = kFSDir.Append(kOriginDatabaseName); | 198 const base::FilePath kDBDir = kFSDir.Append(kOriginDatabaseName); |
199 EXPECT_FALSE(base::PathExists(kFSDir)); | 199 EXPECT_FALSE(base::PathExists(kFSDir)); |
200 EXPECT_TRUE(file_util::CreateDirectory(kFSDir)); | 200 EXPECT_TRUE(base::CreateDirectory(kFSDir)); |
201 | 201 |
202 const std::string kOrigins[] = { | 202 const std::string kOrigins[] = { |
203 "foo.example.com", | 203 "foo.example.com", |
204 "bar.example.com", | 204 "bar.example.com", |
205 "baz.example.com", | 205 "baz.example.com", |
206 "hoge.example.com", | 206 "hoge.example.com", |
207 "fuga.example.com", | 207 "fuga.example.com", |
208 }; | 208 }; |
209 | 209 |
210 scoped_ptr<SandboxOriginDatabase> database( | 210 scoped_ptr<SandboxOriginDatabase> database( |
211 new SandboxOriginDatabase(kFSDir)); | 211 new SandboxOriginDatabase(kFSDir)); |
212 for (size_t i = 0; i < arraysize(kOrigins); ++i) { | 212 for (size_t i = 0; i < arraysize(kOrigins); ++i) { |
213 base::FilePath path; | 213 base::FilePath path; |
214 EXPECT_FALSE(database->HasOriginPath(kOrigins[i])); | 214 EXPECT_FALSE(database->HasOriginPath(kOrigins[i])); |
215 EXPECT_TRUE(database->GetPathForOrigin(kOrigins[i], &path)); | 215 EXPECT_TRUE(database->GetPathForOrigin(kOrigins[i], &path)); |
216 EXPECT_FALSE(path.empty()); | 216 EXPECT_FALSE(path.empty()); |
217 EXPECT_TRUE(database->GetPathForOrigin(kOrigins[i], &path)); | 217 EXPECT_TRUE(database->GetPathForOrigin(kOrigins[i], &path)); |
218 | 218 |
219 if (i != 1) | 219 if (i != 1) |
220 EXPECT_TRUE(file_util::CreateDirectory(kFSDir.Append(path))); | 220 EXPECT_TRUE(base::CreateDirectory(kFSDir.Append(path))); |
221 } | 221 } |
222 database.reset(); | 222 database.reset(); |
223 | 223 |
224 const base::FilePath kGarbageDir = kFSDir.AppendASCII("foo"); | 224 const base::FilePath kGarbageDir = kFSDir.AppendASCII("foo"); |
225 const base::FilePath kGarbageFile = kGarbageDir.AppendASCII("bar"); | 225 const base::FilePath kGarbageFile = kGarbageDir.AppendASCII("bar"); |
226 EXPECT_TRUE(file_util::CreateDirectory(kGarbageDir)); | 226 EXPECT_TRUE(base::CreateDirectory(kGarbageDir)); |
227 bool created = false; | 227 bool created = false; |
228 base::PlatformFileError error; | 228 base::PlatformFileError error; |
229 base::PlatformFile file = base::CreatePlatformFile( | 229 base::PlatformFile file = base::CreatePlatformFile( |
230 kGarbageFile, | 230 kGarbageFile, |
231 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE, | 231 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE, |
232 &created, &error); | 232 &created, &error); |
233 EXPECT_EQ(base::PLATFORM_FILE_OK, error); | 233 EXPECT_EQ(base::PLATFORM_FILE_OK, error); |
234 EXPECT_TRUE(created); | 234 EXPECT_TRUE(created); |
235 EXPECT_TRUE(base::ClosePlatformFile(file)); | 235 EXPECT_TRUE(base::ClosePlatformFile(file)); |
236 | 236 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 leveldb::kTempFile, | 270 leveldb::kTempFile, |
271 leveldb::kInfoLogFile, | 271 leveldb::kInfoLogFile, |
272 }; | 272 }; |
273 | 273 |
274 for (size_t i = 0; i < arraysize(kLevelDBFileTypes); ++i) { | 274 for (size_t i = 0; i < arraysize(kLevelDBFileTypes); ++i) { |
275 base::ScopedTempDir dir; | 275 base::ScopedTempDir dir; |
276 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 276 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
277 const base::FilePath kFSDir = dir.path().Append(kFileSystemDirName); | 277 const base::FilePath kFSDir = dir.path().Append(kFileSystemDirName); |
278 const base::FilePath kDBDir = kFSDir.Append(kOriginDatabaseName); | 278 const base::FilePath kDBDir = kFSDir.Append(kOriginDatabaseName); |
279 EXPECT_FALSE(base::PathExists(kFSDir)); | 279 EXPECT_FALSE(base::PathExists(kFSDir)); |
280 EXPECT_TRUE(file_util::CreateDirectory(kFSDir)); | 280 EXPECT_TRUE(base::CreateDirectory(kFSDir)); |
281 | 281 |
282 const std::string kOrigin = "foo.example.com"; | 282 const std::string kOrigin = "foo.example.com"; |
283 base::FilePath path; | 283 base::FilePath path; |
284 | 284 |
285 scoped_ptr<SandboxOriginDatabase> database( | 285 scoped_ptr<SandboxOriginDatabase> database( |
286 new SandboxOriginDatabase(kFSDir)); | 286 new SandboxOriginDatabase(kFSDir)); |
287 EXPECT_FALSE(database->HasOriginPath(kOrigin)); | 287 EXPECT_FALSE(database->HasOriginPath(kOrigin)); |
288 EXPECT_TRUE(database->GetPathForOrigin(kOrigin, &path)); | 288 EXPECT_TRUE(database->GetPathForOrigin(kOrigin, &path)); |
289 EXPECT_FALSE(path.empty()); | 289 EXPECT_FALSE(path.empty()); |
290 EXPECT_TRUE(database->GetPathForOrigin(kOrigin, &path)); | 290 EXPECT_TRUE(database->GetPathForOrigin(kOrigin, &path)); |
291 EXPECT_TRUE(file_util::CreateDirectory(kFSDir.Append(path))); | 291 EXPECT_TRUE(base::CreateDirectory(kFSDir.Append(path))); |
292 database.reset(); | 292 database.reset(); |
293 | 293 |
294 DeleteDatabaseFile(kDBDir, kLevelDBFileTypes[i]); | 294 DeleteDatabaseFile(kDBDir, kLevelDBFileTypes[i]); |
295 | 295 |
296 database.reset(new SandboxOriginDatabase(kFSDir)); | 296 database.reset(new SandboxOriginDatabase(kFSDir)); |
297 std::vector<SandboxOriginDatabase::OriginRecord> origins_in_db; | 297 std::vector<SandboxOriginDatabase::OriginRecord> origins_in_db; |
298 EXPECT_TRUE(database->ListAllOrigins(&origins_in_db)); | 298 EXPECT_TRUE(database->ListAllOrigins(&origins_in_db)); |
299 | 299 |
300 const std::string kOrigin2("piyo.example.org"); | 300 const std::string kOrigin2("piyo.example.org"); |
301 EXPECT_FALSE(database->HasOriginPath(kOrigin2)); | 301 EXPECT_FALSE(database->HasOriginPath(kOrigin2)); |
302 EXPECT_TRUE(database->GetPathForOrigin(kOrigin2, &path)); | 302 EXPECT_TRUE(database->GetPathForOrigin(kOrigin2, &path)); |
303 EXPECT_FALSE(path.empty()); | 303 EXPECT_FALSE(path.empty()); |
304 EXPECT_TRUE(database->HasOriginPath(kOrigin2)); | 304 EXPECT_TRUE(database->HasOriginPath(kOrigin2)); |
305 } | 305 } |
306 } | 306 } |
307 | 307 |
308 } // namespace fileapi | 308 } // namespace fileapi |
OLD | NEW |