OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <iterator> | 6 #include <iterator> |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "app/sql/connection.h" | 9 #include "app/sql/connection.h" |
| 10 #include "app/sql/statement.h" |
| 11 #include "app/sql/transaction.h" |
| 12 #include "base/bind.h" |
| 13 #include "base/callback.h" |
10 #include "base/file_util.h" | 14 #include "base/file_util.h" |
11 #include "base/scoped_temp_dir.h" | 15 #include "base/scoped_temp_dir.h" |
12 #include "googleurl/src/gurl.h" | 16 #include "googleurl/src/gurl.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
14 #include "webkit/quota/quota_database.h" | 18 #include "webkit/quota/quota_database.h" |
15 | 19 |
16 namespace { | 20 namespace { |
17 | 21 |
18 const base::Time kZeroTime; | 22 const base::Time kZeroTime; |
19 | 23 |
20 class TestErrorDelegate : public sql::ErrorDelegate { | 24 class TestErrorDelegate : public sql::ErrorDelegate { |
21 public: | 25 public: |
22 virtual ~TestErrorDelegate() { } | 26 virtual ~TestErrorDelegate() { } |
23 virtual int OnError( | 27 virtual int OnError( |
24 int error, sql::Connection* connection, sql::Statement* stmt) { | 28 int error, sql::Connection* connection, sql::Statement* stmt) { |
25 return error; | 29 return error; |
26 } | 30 } |
27 }; | 31 }; |
28 | |
29 } // namespace | 32 } // namespace |
30 | 33 |
31 namespace quota { | 34 namespace quota { |
32 | 35 |
33 class QuotaDatabaseTest : public testing::Test { | 36 class QuotaDatabaseTest : public testing::Test { |
34 protected: | 37 protected: |
| 38 typedef QuotaDatabase::QuotaTableEntry QuotaTableEntry; |
| 39 typedef QuotaDatabase::QuotaTableCallback QuotaTableCallback; |
| 40 typedef QuotaDatabase::LastAccessTimeTableEntry LastAccessTimeTableEntry; |
| 41 typedef QuotaDatabase::LastAccessTimeTableCallback |
| 42 LastAccessTimeTableCallback; |
| 43 |
| 44 template <typename Iterator> |
| 45 bool AssignQuotaTable( |
| 46 QuotaDatabase* quota_database, Iterator itr, Iterator end) { |
| 47 if (!quota_database->LazyOpen(true)) |
| 48 return false; |
| 49 |
| 50 for (; itr != end; ++itr) { |
| 51 const char* kSql = |
| 52 "INSERT INTO HostQuotaTable" |
| 53 " (host, type, quota)" |
| 54 " VALUES (?, ?, ?)"; |
| 55 sql::Statement statement; |
| 56 statement.Assign( |
| 57 quota_database->db_->GetCachedStatement( |
| 58 SQL_FROM_HERE, kSql)); |
| 59 EXPECT_TRUE(statement.is_valid()); |
| 60 |
| 61 statement.BindString(0, itr->host); |
| 62 statement.BindInt(1, static_cast<int>(itr->type)); |
| 63 statement.BindInt64(2, itr->quota); |
| 64 if (!statement.Run()) |
| 65 return false; |
| 66 } |
| 67 |
| 68 quota_database->Commit(); |
| 69 return true; |
| 70 } |
| 71 |
| 72 template <typename Iterator> |
| 73 bool AssignLastAccessTimeTable( |
| 74 QuotaDatabase* quota_database, Iterator itr, Iterator end) { |
| 75 if (!quota_database->LazyOpen(true)) |
| 76 return false; |
| 77 |
| 78 for (; itr != end; ++itr) { |
| 79 const char* kSql = |
| 80 "INSERT INTO OriginLastAccessTable" |
| 81 " (origin, type, used_count, last_access_time)" |
| 82 " VALUES (?, ?, ?, ?)"; |
| 83 sql::Statement statement; |
| 84 statement.Assign( |
| 85 quota_database->db_->GetCachedStatement( |
| 86 SQL_FROM_HERE, kSql)); |
| 87 EXPECT_TRUE(statement.is_valid()); |
| 88 |
| 89 statement.BindString(0, itr->origin.spec()); |
| 90 statement.BindInt(1, static_cast<int>(itr->type)); |
| 91 statement.BindInt(2, itr->used_count); |
| 92 statement.BindInt64(3, itr->last_access_time.ToInternalValue()); |
| 93 if (!statement.Run()) |
| 94 return false; |
| 95 } |
| 96 |
| 97 quota_database->Commit(); |
| 98 return true; |
| 99 } |
| 100 |
35 void LazyOpen(const FilePath& kDbFile) { | 101 void LazyOpen(const FilePath& kDbFile) { |
36 QuotaDatabase db(kDbFile); | 102 QuotaDatabase db(kDbFile); |
37 EXPECT_FALSE(db.LazyOpen(false)); | 103 EXPECT_FALSE(db.LazyOpen(false)); |
38 ASSERT_TRUE(db.LazyOpen(true)); | 104 ASSERT_TRUE(db.LazyOpen(true)); |
39 EXPECT_TRUE(db.db_.get()); | 105 EXPECT_TRUE(db.db_.get()); |
40 EXPECT_TRUE(kDbFile.empty() || file_util::PathExists(kDbFile)); | 106 EXPECT_TRUE(kDbFile.empty() || file_util::PathExists(kDbFile)); |
41 } | 107 } |
42 | 108 |
43 void HostQuota(const FilePath& kDbFile) { | 109 void HostQuota(const FilePath& kDbFile) { |
44 QuotaDatabase db(kDbFile); | 110 QuotaDatabase db(kDbFile); |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 EXPECT_TRUE(db.RegisterOrigins(origins, | 251 EXPECT_TRUE(db.RegisterOrigins(origins, |
186 kStorageTypeTemporary, | 252 kStorageTypeTemporary, |
187 base::Time())); | 253 base::Time())); |
188 | 254 |
189 used_count = -1; | 255 used_count = -1; |
190 EXPECT_TRUE(db.FindOriginUsedCount(GURL("http://a/"), | 256 EXPECT_TRUE(db.FindOriginUsedCount(GURL("http://a/"), |
191 kStorageTypeTemporary, | 257 kStorageTypeTemporary, |
192 &used_count)); | 258 &used_count)); |
193 EXPECT_EQ(1, used_count); | 259 EXPECT_EQ(1, used_count); |
194 } | 260 } |
| 261 |
| 262 template <typename EntryType> |
| 263 struct EntryVerifier { |
| 264 std::set<EntryType> table; |
| 265 |
| 266 template <typename Iterator> |
| 267 EntryVerifier(Iterator itr, Iterator end) |
| 268 : table(itr, end) {} |
| 269 |
| 270 bool Run(const EntryType& entry) { |
| 271 EXPECT_EQ(1u, table.erase(entry)); |
| 272 return true; |
| 273 } |
| 274 }; |
| 275 |
| 276 void DumpQuotaTable(const FilePath& kDbFile) { |
| 277 QuotaTableEntry kTableEntries[] = { |
| 278 {"http://go/", kStorageTypeTemporary, 1}, |
| 279 {"http://oo/", kStorageTypeTemporary, 2}, |
| 280 {"http://gle/", kStorageTypePersistent, 3} |
| 281 }; |
| 282 QuotaTableEntry* begin = kTableEntries; |
| 283 QuotaTableEntry* end = kTableEntries + ARRAYSIZE_UNSAFE(kTableEntries); |
| 284 |
| 285 QuotaDatabase db(kDbFile); |
| 286 EXPECT_TRUE(AssignQuotaTable(&db, begin, end)); |
| 287 |
| 288 typedef EntryVerifier<QuotaTableEntry> Verifier; |
| 289 Verifier verifier(begin, end); |
| 290 EXPECT_TRUE(db.DumpQuotaTable( |
| 291 new QuotaTableCallback( |
| 292 base::Bind(&Verifier::Run, |
| 293 base::Unretained(&verifier))))); |
| 294 EXPECT_TRUE(verifier.table.empty()); |
| 295 } |
| 296 |
| 297 void DumpLastAccessTimeTable(const FilePath& kDbFile) { |
| 298 base::Time now(base::Time::Now()); |
| 299 LastAccessTimeTableEntry kTableEntries[] = { |
| 300 {GURL("http://go/"), kStorageTypeTemporary, 2147483647, now}, |
| 301 {GURL("http://oo/"), kStorageTypeTemporary, 0, now}, |
| 302 {GURL("http://gle/"), kStorageTypeTemporary, 1, now}, |
| 303 }; |
| 304 LastAccessTimeTableEntry* begin = kTableEntries; |
| 305 LastAccessTimeTableEntry* end = kTableEntries + |
| 306 ARRAYSIZE_UNSAFE(kTableEntries); |
| 307 |
| 308 QuotaDatabase db(kDbFile); |
| 309 EXPECT_TRUE(AssignLastAccessTimeTable(&db, begin, end)); |
| 310 |
| 311 typedef EntryVerifier<LastAccessTimeTableEntry> Verifier; |
| 312 Verifier verifier(begin, end); |
| 313 EXPECT_TRUE(db.DumpLastAccessTimeTable( |
| 314 new LastAccessTimeTableCallback( |
| 315 base::Bind(&Verifier::Run, |
| 316 base::Unretained(&verifier))))); |
| 317 EXPECT_TRUE(verifier.table.empty()); |
| 318 } |
195 }; | 319 }; |
196 | 320 |
197 TEST_F(QuotaDatabaseTest, LazyOpen) { | 321 TEST_F(QuotaDatabaseTest, LazyOpen) { |
198 ScopedTempDir data_dir; | 322 ScopedTempDir data_dir; |
199 ASSERT_TRUE(data_dir.CreateUniqueTempDir()); | 323 ASSERT_TRUE(data_dir.CreateUniqueTempDir()); |
200 const FilePath kDbFile = data_dir.path().AppendASCII("quota_manager.db"); | 324 const FilePath kDbFile = data_dir.path().AppendASCII("quota_manager.db"); |
201 LazyOpen(kDbFile); | 325 LazyOpen(kDbFile); |
202 LazyOpen(FilePath()); | 326 LazyOpen(FilePath()); |
203 } | 327 } |
204 | 328 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 EXPECT_FALSE(db.IsOriginDatabaseBootstrapped()); | 364 EXPECT_FALSE(db.IsOriginDatabaseBootstrapped()); |
241 } | 365 } |
242 | 366 |
243 TEST_F(QuotaDatabaseTest, RegisterOrigins) { | 367 TEST_F(QuotaDatabaseTest, RegisterOrigins) { |
244 ScopedTempDir data_dir; | 368 ScopedTempDir data_dir; |
245 ASSERT_TRUE(data_dir.CreateUniqueTempDir()); | 369 ASSERT_TRUE(data_dir.CreateUniqueTempDir()); |
246 const FilePath kDbFile = data_dir.path().AppendASCII("quota_manager.db"); | 370 const FilePath kDbFile = data_dir.path().AppendASCII("quota_manager.db"); |
247 RegisterOrigins(kDbFile); | 371 RegisterOrigins(kDbFile); |
248 RegisterOrigins(FilePath()); | 372 RegisterOrigins(FilePath()); |
249 } | 373 } |
| 374 |
| 375 TEST_F(QuotaDatabaseTest, DumpQuotaTable) { |
| 376 ScopedTempDir data_dir; |
| 377 ASSERT_TRUE(data_dir.CreateUniqueTempDir()); |
| 378 const FilePath kDbFile = data_dir.path().AppendASCII("quota_manager.db"); |
| 379 DumpQuotaTable(kDbFile); |
| 380 DumpQuotaTable(FilePath()); |
| 381 } |
| 382 |
| 383 TEST_F(QuotaDatabaseTest, DumpLastAccessTimeTable) { |
| 384 ScopedTempDir data_dir; |
| 385 ASSERT_TRUE(data_dir.CreateUniqueTempDir()); |
| 386 const FilePath kDbFile = data_dir.path().AppendASCII("quota_manager.db"); |
| 387 DumpLastAccessTimeTable(kDbFile); |
| 388 DumpLastAccessTimeTable(FilePath()); |
| 389 } |
250 } // namespace quota | 390 } // namespace quota |
OLD | NEW |