Chromium Code Reviews| 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" | 10 #include "app/sql/statement.h" |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 31 } | 31 } |
| 32 }; | 32 }; |
| 33 } // namespace | 33 } // namespace |
| 34 | 34 |
| 35 namespace quota { | 35 namespace quota { |
| 36 | 36 |
| 37 class QuotaDatabaseTest : public testing::Test { | 37 class QuotaDatabaseTest : public testing::Test { |
| 38 protected: | 38 protected: |
| 39 typedef QuotaDatabase::QuotaTableEntry QuotaTableEntry; | 39 typedef QuotaDatabase::QuotaTableEntry QuotaTableEntry; |
| 40 typedef QuotaDatabase::QuotaTableCallback QuotaTableCallback; | 40 typedef QuotaDatabase::QuotaTableCallback QuotaTableCallback; |
| 41 typedef QuotaDatabase::LastAccessTimeTableEntry LastAccessTimeTableEntry; | 41 typedef QuotaDatabase::OriginInfoTableEntry OriginInfoTableEntry; |
| 42 typedef QuotaDatabase::LastAccessTimeTableCallback | 42 typedef QuotaDatabase::OriginInfoTableCallback |
| 43 LastAccessTimeTableCallback; | 43 OriginInfoTableCallback; |
| 44 | 44 |
| 45 template <typename Iterator> | 45 template <typename Iterator> |
| 46 bool AssignQuotaTable( | 46 bool AssignQuotaTable( |
| 47 QuotaDatabase* quota_database, Iterator itr, Iterator end) { | 47 QuotaDatabase* quota_database, Iterator itr, Iterator end) { |
| 48 if (!quota_database->LazyOpen(true)) | 48 if (!quota_database->LazyOpen(true)) |
| 49 return false; | 49 return false; |
| 50 | 50 |
| 51 for (; itr != end; ++itr) { | 51 for (; itr != end; ++itr) { |
| 52 const char* kSql = | 52 const char* kSql = |
| 53 "INSERT INTO HostQuotaTable" | 53 "INSERT INTO HostQuotaTable" |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 64 statement.BindInt64(2, itr->quota); | 64 statement.BindInt64(2, itr->quota); |
| 65 if (!statement.Run()) | 65 if (!statement.Run()) |
| 66 return false; | 66 return false; |
| 67 } | 67 } |
| 68 | 68 |
| 69 quota_database->Commit(); | 69 quota_database->Commit(); |
| 70 return true; | 70 return true; |
| 71 } | 71 } |
| 72 | 72 |
| 73 template <typename Iterator> | 73 template <typename Iterator> |
| 74 bool AssignLastAccessTimeTable( | 74 bool AssignOriginInfoTable( |
| 75 QuotaDatabase* quota_database, Iterator itr, Iterator end) { | 75 QuotaDatabase* quota_database, Iterator itr, Iterator end) { |
| 76 if (!quota_database->LazyOpen(true)) | 76 if (!quota_database->LazyOpen(true)) |
| 77 return false; | 77 return false; |
| 78 | 78 |
| 79 for (; itr != end; ++itr) { | 79 for (; itr != end; ++itr) { |
| 80 const char* kSql = | 80 const char* kSql = |
| 81 "INSERT INTO OriginLastAccessTable" | 81 "INSERT INTO OriginInfoTable" |
| 82 " (origin, type, used_count, last_access_time)" | 82 " (origin, type, used_count, last_access_time, last_modified_time)" |
| 83 " VALUES (?, ?, ?, ?)"; | 83 " VALUES (?, ?, ?, ?, ?)"; |
| 84 sql::Statement statement; | 84 sql::Statement statement; |
| 85 statement.Assign( | 85 statement.Assign( |
| 86 quota_database->db_->GetCachedStatement( | 86 quota_database->db_->GetCachedStatement( |
| 87 SQL_FROM_HERE, kSql)); | 87 SQL_FROM_HERE, kSql)); |
| 88 EXPECT_TRUE(statement.is_valid()); | 88 EXPECT_TRUE(statement.is_valid()); |
| 89 | 89 |
| 90 statement.BindString(0, itr->origin.spec()); | 90 statement.BindString(0, itr->origin.spec()); |
| 91 statement.BindInt(1, static_cast<int>(itr->type)); | 91 statement.BindInt(1, static_cast<int>(itr->type)); |
| 92 statement.BindInt(2, itr->used_count); | 92 statement.BindInt(2, itr->used_count); |
| 93 statement.BindInt64(3, itr->last_access_time.ToInternalValue()); | 93 statement.BindInt64(3, itr->last_access_time.ToInternalValue()); |
| 94 statement.BindInt64(4, itr->last_modified_time.ToInternalValue()); | |
| 94 if (!statement.Run()) | 95 if (!statement.Run()) |
| 95 return false; | 96 return false; |
| 96 } | 97 } |
| 97 | 98 |
| 98 quota_database->Commit(); | 99 quota_database->Commit(); |
| 99 return true; | 100 return true; |
| 100 } | 101 } |
| 101 | 102 |
| 102 void LazyOpen(const FilePath& kDbFile) { | 103 void LazyOpen(const FilePath& kDbFile) { |
| 103 QuotaDatabase db(kDbFile); | 104 QuotaDatabase db(kDbFile); |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 216 | 217 |
| 217 exceptions.insert(kOrigin3); | 218 exceptions.insert(kOrigin3); |
| 218 EXPECT_TRUE(db.GetLRUOrigin(kStorageTypeTemporary, exceptions, | 219 EXPECT_TRUE(db.GetLRUOrigin(kStorageTypeTemporary, exceptions, |
| 219 NULL, &origin)); | 220 NULL, &origin)); |
| 220 EXPECT_TRUE(origin.is_empty()); | 221 EXPECT_TRUE(origin.is_empty()); |
| 221 | 222 |
| 222 EXPECT_TRUE(db.SetOriginLastAccessTime( | 223 EXPECT_TRUE(db.SetOriginLastAccessTime( |
| 223 kOrigin1, kStorageTypeTemporary, base::Time::Now())); | 224 kOrigin1, kStorageTypeTemporary, base::Time::Now())); |
| 224 | 225 |
| 225 // Delete origin/type last access time information. | 226 // Delete origin/type last access time information. |
| 226 EXPECT_TRUE(db.DeleteOriginLastAccessTime(kOrigin3, kStorageTypeTemporary)); | 227 EXPECT_TRUE(db.DeleteOriginInfo(kOrigin3, kStorageTypeTemporary)); |
| 227 | 228 |
| 228 // Querying again to see if the deletion has worked. | 229 // Querying again to see if the deletion has worked. |
| 229 exceptions.clear(); | 230 exceptions.clear(); |
| 230 EXPECT_TRUE(db.GetLRUOrigin(kStorageTypeTemporary, exceptions, | 231 EXPECT_TRUE(db.GetLRUOrigin(kStorageTypeTemporary, exceptions, |
| 231 NULL, &origin)); | 232 NULL, &origin)); |
| 232 EXPECT_EQ(kOrigin2.spec(), origin.spec()); | 233 EXPECT_EQ(kOrigin2.spec(), origin.spec()); |
| 233 | 234 |
| 234 exceptions.insert(kOrigin1); | 235 exceptions.insert(kOrigin1); |
| 235 exceptions.insert(kOrigin2); | 236 exceptions.insert(kOrigin2); |
| 236 EXPECT_TRUE(db.GetLRUOrigin(kStorageTypeTemporary, exceptions, | 237 EXPECT_TRUE(db.GetLRUOrigin(kStorageTypeTemporary, exceptions, |
| 237 NULL, &origin)); | 238 NULL, &origin)); |
| 238 EXPECT_TRUE(origin.is_empty()); | 239 EXPECT_TRUE(origin.is_empty()); |
| 239 } | 240 } |
| 240 | 241 |
| 242 void OriginLastModifiedSince(const FilePath& kDbFile) { | |
| 243 QuotaDatabase db(kDbFile); | |
| 244 ASSERT_TRUE(db.LazyOpen(true)); | |
| 245 | |
| 246 std::set<GURL> origins; | |
| 247 EXPECT_TRUE(db.GetOriginsModifiedSince( | |
| 248 kStorageTypeTemporary, &origins, base::Time())); | |
| 249 EXPECT_TRUE(origins.empty()); | |
| 250 | |
| 251 const GURL kOrigin1("http://a/"); | |
| 252 const GURL kOrigin2("http://b/"); | |
| 253 const GURL kOrigin3("http://c/"); | |
| 254 | |
| 255 // Report last mod time for the test origins. | |
| 256 EXPECT_TRUE(db.SetOriginLastModifiedTime( | |
| 257 kOrigin1, kStorageTypeTemporary, base::Time::FromInternalValue(10))); | |
| 258 EXPECT_TRUE(db.SetOriginLastModifiedTime( | |
| 259 kOrigin2, kStorageTypeTemporary, base::Time::FromInternalValue(20))); | |
| 260 EXPECT_TRUE(db.SetOriginLastModifiedTime( | |
| 261 kOrigin3, kStorageTypeTemporary, base::Time::FromInternalValue(30))); | |
| 262 | |
| 263 EXPECT_TRUE(db.GetOriginsModifiedSince( | |
| 264 kStorageTypeTemporary, &origins, base::Time::FromInternalValue(15))); | |
| 265 EXPECT_EQ(2U, origins.size()); | |
| 266 EXPECT_EQ(0U, origins.count(kOrigin1)); | |
| 267 EXPECT_EQ(1U, origins.count(kOrigin2)); | |
| 268 EXPECT_EQ(1U, origins.count(kOrigin3)); | |
| 269 | |
| 270 EXPECT_TRUE(db.GetOriginsModifiedSince( | |
| 271 kStorageTypeTemporary, &origins, base::Time::FromInternalValue(25))); | |
| 272 EXPECT_EQ(1U, origins.size()); | |
| 273 EXPECT_EQ(0U, origins.count(kOrigin1)); | |
| 274 EXPECT_EQ(0U, origins.count(kOrigin2)); | |
| 275 EXPECT_EQ(1U, origins.count(kOrigin3)); | |
| 276 | |
| 277 EXPECT_TRUE(db.GetOriginsModifiedSince( | |
| 278 kStorageTypeTemporary, &origins, base::Time::FromInternalValue(35))); | |
| 279 EXPECT_TRUE(origins.empty()); | |
| 280 | |
| 281 // Update origin1's mod time but for persistent storage. | |
| 282 EXPECT_TRUE(db.SetOriginLastModifiedTime( | |
| 283 kOrigin1, kStorageTypePersistent, base::Time::FromInternalValue(40))); | |
|
Mike West
2011/06/16 09:39:33
This is the only place you do anything with persis
kinuko
2011/06/16 12:09:06
Done.
| |
| 284 | |
| 285 // Must have no effects on temporary origins info. | |
| 286 EXPECT_TRUE(db.GetOriginsModifiedSince( | |
| 287 kStorageTypeTemporary, &origins, base::Time::FromInternalValue(15))); | |
| 288 EXPECT_EQ(2U, origins.size()); | |
| 289 EXPECT_EQ(0U, origins.count(kOrigin1)); | |
| 290 EXPECT_EQ(1U, origins.count(kOrigin2)); | |
| 291 EXPECT_EQ(1U, origins.count(kOrigin3)); | |
| 292 } | |
| 293 | |
| 241 void RegisterOrigins(const FilePath& kDbFile) { | 294 void RegisterOrigins(const FilePath& kDbFile) { |
| 242 QuotaDatabase db(kDbFile); | 295 QuotaDatabase db(kDbFile); |
| 243 | 296 |
| 244 const GURL kOrigins[] = { | 297 const GURL kOrigins[] = { |
| 245 GURL("http://a/"), | 298 GURL("http://a/"), |
| 246 GURL("http://b/"), | 299 GURL("http://b/"), |
| 247 GURL("http://c/") }; | 300 GURL("http://c/") }; |
| 248 std::set<GURL> origins(kOrigins, kOrigins + ARRAYSIZE_UNSAFE(kOrigins)); | 301 std::set<GURL> origins(kOrigins, kOrigins + ARRAYSIZE_UNSAFE(kOrigins)); |
| 249 | 302 |
| 250 EXPECT_TRUE(db.RegisterOrigins(origins, | 303 EXPECT_TRUE(db.RegisterOrigins(origins, |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 305 | 358 |
| 306 typedef EntryVerifier<QuotaTableEntry> Verifier; | 359 typedef EntryVerifier<QuotaTableEntry> Verifier; |
| 307 Verifier verifier(begin, end); | 360 Verifier verifier(begin, end); |
| 308 EXPECT_TRUE(db.DumpQuotaTable( | 361 EXPECT_TRUE(db.DumpQuotaTable( |
| 309 new QuotaTableCallback( | 362 new QuotaTableCallback( |
| 310 base::Bind(&Verifier::Run, | 363 base::Bind(&Verifier::Run, |
| 311 base::Unretained(&verifier))))); | 364 base::Unretained(&verifier))))); |
| 312 EXPECT_TRUE(verifier.table.empty()); | 365 EXPECT_TRUE(verifier.table.empty()); |
| 313 } | 366 } |
| 314 | 367 |
| 315 void DumpLastAccessTimeTable(const FilePath& kDbFile) { | 368 void DumpOriginInfoTable(const FilePath& kDbFile) { |
| 316 base::Time now(base::Time::Now()); | 369 base::Time now(base::Time::Now()); |
| 317 LastAccessTimeTableEntry kTableEntries[] = { | 370 OriginInfoTableEntry kTableEntries[] = { |
| 318 {GURL("http://go/"), kStorageTypeTemporary, 2147483647, now}, | 371 {GURL("http://go/"), kStorageTypeTemporary, 2147483647, now, now}, |
| 319 {GURL("http://oo/"), kStorageTypeTemporary, 0, now}, | 372 {GURL("http://oo/"), kStorageTypeTemporary, 0, now, now}, |
| 320 {GURL("http://gle/"), kStorageTypeTemporary, 1, now}, | 373 {GURL("http://gle/"), kStorageTypeTemporary, 1, now, now}, |
| 321 }; | 374 }; |
| 322 LastAccessTimeTableEntry* begin = kTableEntries; | 375 OriginInfoTableEntry* begin = kTableEntries; |
| 323 LastAccessTimeTableEntry* end = kTableEntries + | 376 OriginInfoTableEntry* end = kTableEntries + ARRAYSIZE_UNSAFE(kTableEntries); |
| 324 ARRAYSIZE_UNSAFE(kTableEntries); | |
| 325 | 377 |
| 326 QuotaDatabase db(kDbFile); | 378 QuotaDatabase db(kDbFile); |
| 327 EXPECT_TRUE(AssignLastAccessTimeTable(&db, begin, end)); | 379 EXPECT_TRUE(AssignOriginInfoTable(&db, begin, end)); |
| 328 | 380 |
| 329 typedef EntryVerifier<LastAccessTimeTableEntry> Verifier; | 381 typedef EntryVerifier<OriginInfoTableEntry> Verifier; |
| 330 Verifier verifier(begin, end); | 382 Verifier verifier(begin, end); |
| 331 EXPECT_TRUE(db.DumpLastAccessTimeTable( | 383 EXPECT_TRUE(db.DumpOriginInfoTable( |
| 332 new LastAccessTimeTableCallback( | 384 new OriginInfoTableCallback( |
| 333 base::Bind(&Verifier::Run, | 385 base::Bind(&Verifier::Run, |
| 334 base::Unretained(&verifier))))); | 386 base::Unretained(&verifier))))); |
| 335 EXPECT_TRUE(verifier.table.empty()); | 387 EXPECT_TRUE(verifier.table.empty()); |
| 336 } | 388 } |
| 337 }; | 389 }; |
| 338 | 390 |
| 339 TEST_F(QuotaDatabaseTest, LazyOpen) { | 391 TEST_F(QuotaDatabaseTest, LazyOpen) { |
| 340 ScopedTempDir data_dir; | 392 ScopedTempDir data_dir; |
| 341 ASSERT_TRUE(data_dir.CreateUniqueTempDir()); | 393 ASSERT_TRUE(data_dir.CreateUniqueTempDir()); |
| 342 const FilePath kDbFile = data_dir.path().AppendASCII("quota_manager.db"); | 394 const FilePath kDbFile = data_dir.path().AppendASCII("quota_manager.db"); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 361 } | 413 } |
| 362 | 414 |
| 363 TEST_F(QuotaDatabaseTest, OriginLastAccessTimeLRU) { | 415 TEST_F(QuotaDatabaseTest, OriginLastAccessTimeLRU) { |
| 364 ScopedTempDir data_dir; | 416 ScopedTempDir data_dir; |
| 365 ASSERT_TRUE(data_dir.CreateUniqueTempDir()); | 417 ASSERT_TRUE(data_dir.CreateUniqueTempDir()); |
| 366 const FilePath kDbFile = data_dir.path().AppendASCII("quota_manager.db"); | 418 const FilePath kDbFile = data_dir.path().AppendASCII("quota_manager.db"); |
| 367 OriginLastAccessTimeLRU(kDbFile); | 419 OriginLastAccessTimeLRU(kDbFile); |
| 368 OriginLastAccessTimeLRU(FilePath()); | 420 OriginLastAccessTimeLRU(FilePath()); |
| 369 } | 421 } |
| 370 | 422 |
| 423 TEST_F(QuotaDatabaseTest, OriginLastModifiedSince) { | |
| 424 ScopedTempDir data_dir; | |
| 425 ASSERT_TRUE(data_dir.CreateUniqueTempDir()); | |
| 426 const FilePath kDbFile = data_dir.path().AppendASCII("quota_manager.db"); | |
| 427 OriginLastModifiedSince(kDbFile); | |
| 428 OriginLastModifiedSince(FilePath()); | |
| 429 } | |
| 430 | |
| 371 TEST_F(QuotaDatabaseTest, BootstrapFlag) { | 431 TEST_F(QuotaDatabaseTest, BootstrapFlag) { |
| 372 ScopedTempDir data_dir; | 432 ScopedTempDir data_dir; |
| 373 ASSERT_TRUE(data_dir.CreateUniqueTempDir()); | 433 ASSERT_TRUE(data_dir.CreateUniqueTempDir()); |
| 374 | 434 |
| 375 const FilePath kDbFile = data_dir.path().AppendASCII("quota_manager.db"); | 435 const FilePath kDbFile = data_dir.path().AppendASCII("quota_manager.db"); |
| 376 QuotaDatabase db(kDbFile); | 436 QuotaDatabase db(kDbFile); |
| 377 | 437 |
| 378 EXPECT_FALSE(db.IsOriginDatabaseBootstrapped()); | 438 EXPECT_FALSE(db.IsOriginDatabaseBootstrapped()); |
| 379 EXPECT_TRUE(db.SetOriginDatabaseBootstrapped(true)); | 439 EXPECT_TRUE(db.SetOriginDatabaseBootstrapped(true)); |
| 380 EXPECT_TRUE(db.IsOriginDatabaseBootstrapped()); | 440 EXPECT_TRUE(db.IsOriginDatabaseBootstrapped()); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 391 } | 451 } |
| 392 | 452 |
| 393 TEST_F(QuotaDatabaseTest, DumpQuotaTable) { | 453 TEST_F(QuotaDatabaseTest, DumpQuotaTable) { |
| 394 ScopedTempDir data_dir; | 454 ScopedTempDir data_dir; |
| 395 ASSERT_TRUE(data_dir.CreateUniqueTempDir()); | 455 ASSERT_TRUE(data_dir.CreateUniqueTempDir()); |
| 396 const FilePath kDbFile = data_dir.path().AppendASCII("quota_manager.db"); | 456 const FilePath kDbFile = data_dir.path().AppendASCII("quota_manager.db"); |
| 397 DumpQuotaTable(kDbFile); | 457 DumpQuotaTable(kDbFile); |
| 398 DumpQuotaTable(FilePath()); | 458 DumpQuotaTable(FilePath()); |
| 399 } | 459 } |
| 400 | 460 |
| 401 TEST_F(QuotaDatabaseTest, DumpLastAccessTimeTable) { | 461 TEST_F(QuotaDatabaseTest, DumpOriginInfoTable) { |
| 402 ScopedTempDir data_dir; | 462 ScopedTempDir data_dir; |
| 403 ASSERT_TRUE(data_dir.CreateUniqueTempDir()); | 463 ASSERT_TRUE(data_dir.CreateUniqueTempDir()); |
| 404 const FilePath kDbFile = data_dir.path().AppendASCII("quota_manager.db"); | 464 const FilePath kDbFile = data_dir.path().AppendASCII("quota_manager.db"); |
| 405 DumpLastAccessTimeTable(kDbFile); | 465 DumpOriginInfoTable(kDbFile); |
| 406 DumpLastAccessTimeTable(FilePath()); | 466 DumpOriginInfoTable(FilePath()); |
| 407 } | 467 } |
| 408 } // namespace quota | 468 } // namespace quota |
| OLD | NEW |