| 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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/files/file_util.h" | 6 #include "base/files/file_util.h" |
| 7 #include "base/metrics/field_trial.h" | 7 #include "base/metrics/field_trial.h" |
| 8 #include "base/port.h" | 8 #include "base/port.h" |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 // Perform IO operations on the cache until there is pending IO. | 75 // Perform IO operations on the cache until there is pending IO. |
| 76 int GeneratePendingIO(net::TestCompletionCallback* cb); | 76 int GeneratePendingIO(net::TestCompletionCallback* cb); |
| 77 | 77 |
| 78 // Adds 5 sparse entries. |doomed_start| and |doomed_end| if not NULL, | 78 // Adds 5 sparse entries. |doomed_start| and |doomed_end| if not NULL, |
| 79 // will be filled with times, used by DoomEntriesSince and DoomEntriesBetween. | 79 // will be filled with times, used by DoomEntriesSince and DoomEntriesBetween. |
| 80 // There are 4 entries after doomed_start and 2 after doomed_end. | 80 // There are 4 entries after doomed_start and 2 after doomed_end. |
| 81 void InitSparseCache(base::Time* doomed_start, base::Time* doomed_end); | 81 void InitSparseCache(base::Time* doomed_start, base::Time* doomed_end); |
| 82 | 82 |
| 83 bool CreateSetOfRandomEntries(std::set<std::string>* key_pool); | 83 bool CreateSetOfRandomEntries(std::set<std::string>* key_pool); |
| 84 bool EnumerateAndMatchKeys(int max_to_open, | 84 bool EnumerateAndMatchKeys(int max_to_open, |
| 85 void** iter, | 85 disk_cache::Backend::Iterator* iter, |
| 86 std::set<std::string>* keys_to_match, | 86 std::set<std::string>* keys_to_match, |
| 87 size_t* count); | 87 size_t* count); |
| 88 | 88 |
| 89 // Actual tests: | 89 // Actual tests: |
| 90 void BackendBasics(); | 90 void BackendBasics(); |
| 91 void BackendKeying(); | 91 void BackendKeying(); |
| 92 void BackendShutdownWithPendingFileIO(bool fast); | 92 void BackendShutdownWithPendingFileIO(bool fast); |
| 93 void BackendShutdownWithPendingIO(bool fast); | 93 void BackendShutdownWithPendingIO(bool fast); |
| 94 void BackendShutdownWithPendingCreate(bool fast); | 94 void BackendShutdownWithPendingCreate(bool fast); |
| 95 void BackendSetSize(); | 95 void BackendSetSize(); |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 } | 251 } |
| 252 return key_pool->size() == implicit_cast<size_t>(cache_->GetEntryCount()); | 252 return key_pool->size() == implicit_cast<size_t>(cache_->GetEntryCount()); |
| 253 } | 253 } |
| 254 | 254 |
| 255 // Performs iteration over the backend and checks that the keys of entries | 255 // Performs iteration over the backend and checks that the keys of entries |
| 256 // opened are in |keys_to_match|, then erases them. Up to |max_to_open| entries | 256 // opened are in |keys_to_match|, then erases them. Up to |max_to_open| entries |
| 257 // will be opened, if it is positive. Otherwise, iteration will continue until | 257 // will be opened, if it is positive. Otherwise, iteration will continue until |
| 258 // OpenNextEntry stops returning net::OK. | 258 // OpenNextEntry stops returning net::OK. |
| 259 bool DiskCacheBackendTest::EnumerateAndMatchKeys( | 259 bool DiskCacheBackendTest::EnumerateAndMatchKeys( |
| 260 int max_to_open, | 260 int max_to_open, |
| 261 void** iter, | 261 disk_cache::Backend::Iterator* iter, |
| 262 std::set<std::string>* keys_to_match, | 262 std::set<std::string>* keys_to_match, |
| 263 size_t* count) { | 263 size_t* count) { |
| 264 disk_cache::Entry* entry; | 264 disk_cache::Entry* entry; |
| 265 | 265 |
| 266 while (OpenNextEntry(iter, &entry) == net::OK) { | 266 while (OpenNextEntry(iter, &entry) == net::OK) { |
| 267 if (!entry) | 267 if (!entry) |
| 268 return false; | 268 return false; |
| 269 EXPECT_EQ(1U, keys_to_match->erase(entry->GetKey())); | 269 EXPECT_EQ(1U, keys_to_match->erase(entry->GetKey())); |
| 270 entry->Close(); | 270 entry->Close(); |
| 271 ++(*count); | 271 ++(*count); |
| (...skipping 960 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1232 for (int i = 0; i < kNumEntries; i++) { | 1232 for (int i = 0; i < kNumEntries; i++) { |
| 1233 std::string key = GenerateKey(true); | 1233 std::string key = GenerateKey(true); |
| 1234 disk_cache::Entry* entry; | 1234 disk_cache::Entry* entry; |
| 1235 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); | 1235 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); |
| 1236 entry->Close(); | 1236 entry->Close(); |
| 1237 } | 1237 } |
| 1238 EXPECT_EQ(kNumEntries, cache_->GetEntryCount()); | 1238 EXPECT_EQ(kNumEntries, cache_->GetEntryCount()); |
| 1239 Time final = Time::Now(); | 1239 Time final = Time::Now(); |
| 1240 | 1240 |
| 1241 disk_cache::Entry* entry; | 1241 disk_cache::Entry* entry; |
| 1242 void* iter = NULL; | 1242 disk_cache::Backend::Iterator iter; |
| 1243 int count = 0; | 1243 int count = 0; |
| 1244 Time last_modified[kNumEntries]; | 1244 Time last_modified[kNumEntries]; |
| 1245 Time last_used[kNumEntries]; | 1245 Time last_used[kNumEntries]; |
| 1246 while (OpenNextEntry(&iter, &entry) == net::OK) { | 1246 while (OpenNextEntry(&iter, &entry) == net::OK) { |
| 1247 ASSERT_TRUE(NULL != entry); | 1247 ASSERT_TRUE(NULL != entry); |
| 1248 if (count < kNumEntries) { | 1248 if (count < kNumEntries) { |
| 1249 last_modified[count] = entry->GetLastModified(); | 1249 last_modified[count] = entry->GetLastModified(); |
| 1250 last_used[count] = entry->GetLastUsed(); | 1250 last_used[count] = entry->GetLastUsed(); |
| 1251 EXPECT_TRUE(initial <= last_modified[count]); | 1251 EXPECT_TRUE(initial <= last_modified[count]); |
| 1252 EXPECT_TRUE(final >= last_modified[count]); | 1252 EXPECT_TRUE(final >= last_modified[count]); |
| 1253 } | 1253 } |
| 1254 | 1254 |
| 1255 entry->Close(); | 1255 entry->Close(); |
| 1256 count++; | 1256 count++; |
| 1257 }; | 1257 }; |
| 1258 EXPECT_EQ(kNumEntries, count); | 1258 EXPECT_EQ(kNumEntries, count); |
| 1259 | 1259 |
| 1260 iter = NULL; | 1260 iter.reset(); |
| 1261 count = 0; | 1261 count = 0; |
| 1262 // The previous enumeration should not have changed the timestamps. | 1262 // The previous enumeration should not have changed the timestamps. |
| 1263 while (OpenNextEntry(&iter, &entry) == net::OK) { | 1263 while (OpenNextEntry(&iter, &entry) == net::OK) { |
| 1264 ASSERT_TRUE(NULL != entry); | 1264 ASSERT_TRUE(NULL != entry); |
| 1265 if (count < kNumEntries) { | 1265 if (count < kNumEntries) { |
| 1266 EXPECT_TRUE(last_modified[count] == entry->GetLastModified()); | 1266 EXPECT_TRUE(last_modified[count] == entry->GetLastModified()); |
| 1267 EXPECT_TRUE(last_used[count] == entry->GetLastUsed()); | 1267 EXPECT_TRUE(last_used[count] == entry->GetLastUsed()); |
| 1268 } | 1268 } |
| 1269 entry->Close(); | 1269 entry->Close(); |
| 1270 count++; | 1270 count++; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1304 disk_cache::Entry *entry1, *entry2; | 1304 disk_cache::Entry *entry1, *entry2; |
| 1305 ASSERT_EQ(net::OK, CreateEntry(first, &entry1)); | 1305 ASSERT_EQ(net::OK, CreateEntry(first, &entry1)); |
| 1306 entry1->Close(); | 1306 entry1->Close(); |
| 1307 ASSERT_EQ(net::OK, CreateEntry(second, &entry2)); | 1307 ASSERT_EQ(net::OK, CreateEntry(second, &entry2)); |
| 1308 entry2->Close(); | 1308 entry2->Close(); |
| 1309 FlushQueueForTest(); | 1309 FlushQueueForTest(); |
| 1310 | 1310 |
| 1311 // Make sure that the timestamp is not the same. | 1311 // Make sure that the timestamp is not the same. |
| 1312 AddDelay(); | 1312 AddDelay(); |
| 1313 ASSERT_EQ(net::OK, OpenEntry(second, &entry1)); | 1313 ASSERT_EQ(net::OK, OpenEntry(second, &entry1)); |
| 1314 void* iter = NULL; | 1314 disk_cache::Backend::Iterator iter; |
| 1315 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry2)); | 1315 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry2)); |
| 1316 EXPECT_EQ(entry2->GetKey(), second); | 1316 EXPECT_EQ(entry2->GetKey(), second); |
| 1317 | 1317 |
| 1318 // Two entries and the iterator pointing at "first". | 1318 // Two entries and the iterator pointing at "first". |
| 1319 entry1->Close(); | 1319 entry1->Close(); |
| 1320 entry2->Close(); | 1320 entry2->Close(); |
| 1321 | 1321 |
| 1322 // The iterator should still be valid, so we should not crash. | 1322 // The iterator should still be valid, so we should not crash. |
| 1323 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry2)); | 1323 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry2)); |
| 1324 EXPECT_EQ(entry2->GetKey(), first); | 1324 EXPECT_EQ(entry2->GetKey(), first); |
| 1325 entry2->Close(); | 1325 entry2->Close(); |
| 1326 cache_->EndEnumeration(&iter); | 1326 iter.reset(); |
| 1327 | 1327 |
| 1328 // Modify the oldest entry and get the newest element. | 1328 // Modify the oldest entry and get the newest element. |
| 1329 ASSERT_EQ(net::OK, OpenEntry(first, &entry1)); | 1329 ASSERT_EQ(net::OK, OpenEntry(first, &entry1)); |
| 1330 EXPECT_EQ(0, WriteData(entry1, 0, 200, NULL, 0, false)); | 1330 EXPECT_EQ(0, WriteData(entry1, 0, 200, NULL, 0, false)); |
| 1331 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry2)); | 1331 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry2)); |
| 1332 if (type_ == net::APP_CACHE) { | 1332 if (type_ == net::APP_CACHE) { |
| 1333 // The list is not updated. | 1333 // The list is not updated. |
| 1334 EXPECT_EQ(entry2->GetKey(), second); | 1334 EXPECT_EQ(entry2->GetKey(), second); |
| 1335 } else { | 1335 } else { |
| 1336 EXPECT_EQ(entry2->GetKey(), first); | 1336 EXPECT_EQ(entry2->GetKey(), first); |
| 1337 } | 1337 } |
| 1338 | 1338 |
| 1339 entry1->Close(); | 1339 entry1->Close(); |
| 1340 entry2->Close(); | 1340 entry2->Close(); |
| 1341 cache_->EndEnumeration(&iter); | |
| 1342 } | 1341 } |
| 1343 | 1342 |
| 1344 TEST_F(DiskCacheBackendTest, Enumerations2) { | 1343 TEST_F(DiskCacheBackendTest, Enumerations2) { |
| 1345 BackendEnumerations2(); | 1344 BackendEnumerations2(); |
| 1346 } | 1345 } |
| 1347 | 1346 |
| 1348 TEST_F(DiskCacheBackendTest, NewEvictionEnumerations2) { | 1347 TEST_F(DiskCacheBackendTest, NewEvictionEnumerations2) { |
| 1349 SetNewEviction(); | 1348 SetNewEviction(); |
| 1350 BackendEnumerations2(); | 1349 BackendEnumerations2(); |
| 1351 } | 1350 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1386 | 1385 |
| 1387 FlushQueueForTest(); | 1386 FlushQueueForTest(); |
| 1388 | 1387 |
| 1389 // Make sure that the timestamp is not the same. | 1388 // Make sure that the timestamp is not the same. |
| 1390 AddDelay(); | 1389 AddDelay(); |
| 1391 | 1390 |
| 1392 // Read from the last item in the LRU. | 1391 // Read from the last item in the LRU. |
| 1393 EXPECT_EQ(kSize, ReadData(entry1, 0, 0, buffer1.get(), kSize)); | 1392 EXPECT_EQ(kSize, ReadData(entry1, 0, 0, buffer1.get(), kSize)); |
| 1394 entry1->Close(); | 1393 entry1->Close(); |
| 1395 | 1394 |
| 1396 void* iter = NULL; | 1395 disk_cache::Backend::Iterator iter; |
| 1397 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry2)); | 1396 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry2)); |
| 1398 EXPECT_EQ(entry2->GetKey(), second); | 1397 EXPECT_EQ(entry2->GetKey(), second); |
| 1399 entry2->Close(); | 1398 entry2->Close(); |
| 1400 cache_->EndEnumeration(&iter); | |
| 1401 } | 1399 } |
| 1402 | 1400 |
| 1403 #if !defined(LEAK_SANITIZER) | 1401 #if !defined(LEAK_SANITIZER) |
| 1404 // Verify handling of invalid entries while doing enumerations. | 1402 // Verify handling of invalid entries while doing enumerations. |
| 1405 // We'll be leaking memory from this test. | 1403 // We'll be leaking memory from this test. |
| 1406 void DiskCacheBackendTest::BackendInvalidEntryEnumeration() { | 1404 void DiskCacheBackendTest::BackendInvalidEntryEnumeration() { |
| 1407 InitCache(); | 1405 InitCache(); |
| 1408 | 1406 |
| 1409 std::string key("Some key"); | 1407 std::string key("Some key"); |
| 1410 disk_cache::Entry *entry, *entry1, *entry2; | 1408 disk_cache::Entry *entry, *entry1, *entry2; |
| 1411 ASSERT_EQ(net::OK, CreateEntry(key, &entry1)); | 1409 ASSERT_EQ(net::OK, CreateEntry(key, &entry1)); |
| 1412 | 1410 |
| 1413 const int kSize = 50; | 1411 const int kSize = 50; |
| 1414 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize)); | 1412 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize)); |
| 1415 memset(buffer1->data(), 0, kSize); | 1413 memset(buffer1->data(), 0, kSize); |
| 1416 base::strlcpy(buffer1->data(), "And the data to save", kSize); | 1414 base::strlcpy(buffer1->data(), "And the data to save", kSize); |
| 1417 EXPECT_EQ(kSize, WriteData(entry1, 0, 0, buffer1.get(), kSize, false)); | 1415 EXPECT_EQ(kSize, WriteData(entry1, 0, 0, buffer1.get(), kSize, false)); |
| 1418 entry1->Close(); | 1416 entry1->Close(); |
| 1419 ASSERT_EQ(net::OK, OpenEntry(key, &entry1)); | 1417 ASSERT_EQ(net::OK, OpenEntry(key, &entry1)); |
| 1420 EXPECT_EQ(kSize, ReadData(entry1, 0, 0, buffer1.get(), kSize)); | 1418 EXPECT_EQ(kSize, ReadData(entry1, 0, 0, buffer1.get(), kSize)); |
| 1421 | 1419 |
| 1422 std::string key2("Another key"); | 1420 std::string key2("Another key"); |
| 1423 ASSERT_EQ(net::OK, CreateEntry(key2, &entry2)); | 1421 ASSERT_EQ(net::OK, CreateEntry(key2, &entry2)); |
| 1424 entry2->Close(); | 1422 entry2->Close(); |
| 1425 ASSERT_EQ(2, cache_->GetEntryCount()); | 1423 ASSERT_EQ(2, cache_->GetEntryCount()); |
| 1426 | 1424 |
| 1427 SimulateCrash(); | 1425 SimulateCrash(); |
| 1428 | 1426 |
| 1429 void* iter = NULL; | 1427 disk_cache::Backend::Iterator iter; |
| 1430 int count = 0; | 1428 int count = 0; |
| 1431 while (OpenNextEntry(&iter, &entry) == net::OK) { | 1429 while (OpenNextEntry(&iter, &entry) == net::OK) { |
| 1432 ASSERT_TRUE(NULL != entry); | 1430 ASSERT_TRUE(NULL != entry); |
| 1433 EXPECT_EQ(key2, entry->GetKey()); | 1431 EXPECT_EQ(key2, entry->GetKey()); |
| 1434 entry->Close(); | 1432 entry->Close(); |
| 1435 count++; | 1433 count++; |
| 1436 }; | 1434 }; |
| 1437 EXPECT_EQ(1, count); | 1435 EXPECT_EQ(1, count); |
| 1438 EXPECT_EQ(1, cache_->GetEntryCount()); | 1436 EXPECT_EQ(1, cache_->GetEntryCount()); |
| 1439 } | 1437 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 1461 const int kNumEntries = 10; | 1459 const int kNumEntries = 10; |
| 1462 for (int i = 0; i < kNumEntries; i++) { | 1460 for (int i = 0; i < kNumEntries; i++) { |
| 1463 std::string key = GenerateKey(true); | 1461 std::string key = GenerateKey(true); |
| 1464 disk_cache::Entry* entry; | 1462 disk_cache::Entry* entry; |
| 1465 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); | 1463 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); |
| 1466 entry->Close(); | 1464 entry->Close(); |
| 1467 } | 1465 } |
| 1468 EXPECT_EQ(kNumEntries, cache_->GetEntryCount()); | 1466 EXPECT_EQ(kNumEntries, cache_->GetEntryCount()); |
| 1469 | 1467 |
| 1470 disk_cache::Entry *entry1, *entry2; | 1468 disk_cache::Entry *entry1, *entry2; |
| 1471 void* iter1 = NULL; | 1469 disk_cache::Backend::Iterator iter1, iter2; |
| 1472 void* iter2 = NULL; | |
| 1473 ASSERT_EQ(net::OK, OpenNextEntry(&iter1, &entry1)); | 1470 ASSERT_EQ(net::OK, OpenNextEntry(&iter1, &entry1)); |
| 1474 ASSERT_TRUE(NULL != entry1); | 1471 ASSERT_TRUE(NULL != entry1); |
| 1475 entry1->Close(); | 1472 entry1->Close(); |
| 1476 entry1 = NULL; | 1473 entry1 = NULL; |
| 1477 | 1474 |
| 1478 // Let's go to the middle of the list. | 1475 // Let's go to the middle of the list. |
| 1479 for (int i = 0; i < kNumEntries / 2; i++) { | 1476 for (int i = 0; i < kNumEntries / 2; i++) { |
| 1480 if (entry1) | 1477 if (entry1) |
| 1481 entry1->Close(); | 1478 entry1->Close(); |
| 1482 ASSERT_EQ(net::OK, OpenNextEntry(&iter1, &entry1)); | 1479 ASSERT_EQ(net::OK, OpenNextEntry(&iter1, &entry1)); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 1494 | 1491 |
| 1495 // The link entry2->entry1 should be broken. | 1492 // The link entry2->entry1 should be broken. |
| 1496 EXPECT_NE(entry2->GetKey(), entry1->GetKey()); | 1493 EXPECT_NE(entry2->GetKey(), entry1->GetKey()); |
| 1497 entry1->Close(); | 1494 entry1->Close(); |
| 1498 entry2->Close(); | 1495 entry2->Close(); |
| 1499 | 1496 |
| 1500 // And the second iterator should keep working. | 1497 // And the second iterator should keep working. |
| 1501 ASSERT_EQ(net::OK, OpenNextEntry(&iter2, &entry2)); | 1498 ASSERT_EQ(net::OK, OpenNextEntry(&iter2, &entry2)); |
| 1502 ASSERT_TRUE(NULL != entry2); | 1499 ASSERT_TRUE(NULL != entry2); |
| 1503 entry2->Close(); | 1500 entry2->Close(); |
| 1504 | |
| 1505 cache_->EndEnumeration(&iter1); | |
| 1506 cache_->EndEnumeration(&iter2); | |
| 1507 } | 1501 } |
| 1508 | 1502 |
| 1509 TEST_F(DiskCacheBackendTest, FixEnumerators) { | 1503 TEST_F(DiskCacheBackendTest, FixEnumerators) { |
| 1510 BackendFixEnumerators(); | 1504 BackendFixEnumerators(); |
| 1511 } | 1505 } |
| 1512 | 1506 |
| 1513 TEST_F(DiskCacheBackendTest, NewEvictionFixEnumerators) { | 1507 TEST_F(DiskCacheBackendTest, NewEvictionFixEnumerators) { |
| 1514 SetNewEviction(); | 1508 SetNewEviction(); |
| 1515 BackendFixEnumerators(); | 1509 BackendFixEnumerators(); |
| 1516 } | 1510 } |
| (...skipping 505 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2022 } | 2016 } |
| 2023 | 2017 |
| 2024 // Tests that we don't crash or hang when enumerating this cache. | 2018 // Tests that we don't crash or hang when enumerating this cache. |
| 2025 void DiskCacheBackendTest::BackendInvalidEntry3() { | 2019 void DiskCacheBackendTest::BackendInvalidEntry3() { |
| 2026 SetMask(0x1); // 2-entry table. | 2020 SetMask(0x1); // 2-entry table. |
| 2027 SetMaxSize(0x3000); // 12 kB. | 2021 SetMaxSize(0x3000); // 12 kB. |
| 2028 DisableFirstCleanup(); | 2022 DisableFirstCleanup(); |
| 2029 InitCache(); | 2023 InitCache(); |
| 2030 | 2024 |
| 2031 disk_cache::Entry* entry; | 2025 disk_cache::Entry* entry; |
| 2032 void* iter = NULL; | 2026 disk_cache::Backend::Iterator iter; |
| 2033 while (OpenNextEntry(&iter, &entry) == net::OK) { | 2027 while (OpenNextEntry(&iter, &entry) == net::OK) { |
| 2034 entry->Close(); | 2028 entry->Close(); |
| 2035 } | 2029 } |
| 2036 } | 2030 } |
| 2037 | 2031 |
| 2038 TEST_F(DiskCacheBackendTest, InvalidEntry3) { | 2032 TEST_F(DiskCacheBackendTest, InvalidEntry3) { |
| 2039 ASSERT_TRUE(CopyTestCache("dirty_entry3")); | 2033 ASSERT_TRUE(CopyTestCache("dirty_entry3")); |
| 2040 BackendInvalidEntry3(); | 2034 BackendInvalidEntry3(); |
| 2041 } | 2035 } |
| 2042 | 2036 |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2167 entry_impl->rankings()->Store(); | 2161 entry_impl->rankings()->Store(); |
| 2168 entry->Close(); | 2162 entry->Close(); |
| 2169 FlushQueueForTest(); | 2163 FlushQueueForTest(); |
| 2170 EXPECT_EQ(2, cache_->GetEntryCount()); | 2164 EXPECT_EQ(2, cache_->GetEntryCount()); |
| 2171 | 2165 |
| 2172 // This should detect the bad entry. | 2166 // This should detect the bad entry. |
| 2173 EXPECT_NE(net::OK, OpenEntry(second, &entry)); | 2167 EXPECT_NE(net::OK, OpenEntry(second, &entry)); |
| 2174 EXPECT_EQ(1, cache_->GetEntryCount()); | 2168 EXPECT_EQ(1, cache_->GetEntryCount()); |
| 2175 | 2169 |
| 2176 // We should delete the cache. The list still has a corrupt node. | 2170 // We should delete the cache. The list still has a corrupt node. |
| 2177 void* iter = NULL; | 2171 disk_cache::Backend::Iterator iter; |
| 2178 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry)); | 2172 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry)); |
| 2179 FlushQueueForTest(); | 2173 FlushQueueForTest(); |
| 2180 EXPECT_EQ(0, cache_->GetEntryCount()); | 2174 EXPECT_EQ(0, cache_->GetEntryCount()); |
| 2181 } | 2175 } |
| 2182 | 2176 |
| 2183 TEST_F(DiskCacheBackendTest, InvalidEntry7) { | 2177 TEST_F(DiskCacheBackendTest, InvalidEntry7) { |
| 2184 BackendInvalidEntry7(); | 2178 BackendInvalidEntry7(); |
| 2185 } | 2179 } |
| 2186 | 2180 |
| 2187 TEST_F(DiskCacheBackendTest, NewEvictionInvalidEntry7) { | 2181 TEST_F(DiskCacheBackendTest, NewEvictionInvalidEntry7) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 2211 entry_impl->rankings()->Store(); | 2205 entry_impl->rankings()->Store(); |
| 2212 entry->Close(); | 2206 entry->Close(); |
| 2213 FlushQueueForTest(); | 2207 FlushQueueForTest(); |
| 2214 EXPECT_EQ(2, cache_->GetEntryCount()); | 2208 EXPECT_EQ(2, cache_->GetEntryCount()); |
| 2215 | 2209 |
| 2216 // This should detect the bad entry. | 2210 // This should detect the bad entry. |
| 2217 EXPECT_NE(net::OK, OpenEntry(second, &entry)); | 2211 EXPECT_NE(net::OK, OpenEntry(second, &entry)); |
| 2218 EXPECT_EQ(1, cache_->GetEntryCount()); | 2212 EXPECT_EQ(1, cache_->GetEntryCount()); |
| 2219 | 2213 |
| 2220 // We should not delete the cache. | 2214 // We should not delete the cache. |
| 2221 void* iter = NULL; | 2215 disk_cache::Backend::Iterator iter; |
| 2222 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry)); | 2216 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry)); |
| 2223 entry->Close(); | 2217 entry->Close(); |
| 2224 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry)); | 2218 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry)); |
| 2225 EXPECT_EQ(1, cache_->GetEntryCount()); | 2219 EXPECT_EQ(1, cache_->GetEntryCount()); |
| 2226 } | 2220 } |
| 2227 | 2221 |
| 2228 TEST_F(DiskCacheBackendTest, InvalidEntry8) { | 2222 TEST_F(DiskCacheBackendTest, InvalidEntry8) { |
| 2229 BackendInvalidEntry8(); | 2223 BackendInvalidEntry8(); |
| 2230 } | 2224 } |
| 2231 | 2225 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 2261 EXPECT_EQ(2, cache_->GetEntryCount()); | 2255 EXPECT_EQ(2, cache_->GetEntryCount()); |
| 2262 | 2256 |
| 2263 if (eviction) { | 2257 if (eviction) { |
| 2264 TrimForTest(false); | 2258 TrimForTest(false); |
| 2265 EXPECT_EQ(1, cache_->GetEntryCount()); | 2259 EXPECT_EQ(1, cache_->GetEntryCount()); |
| 2266 TrimForTest(false); | 2260 TrimForTest(false); |
| 2267 EXPECT_EQ(1, cache_->GetEntryCount()); | 2261 EXPECT_EQ(1, cache_->GetEntryCount()); |
| 2268 } else { | 2262 } else { |
| 2269 // We should detect the problem through the list, but we should not delete | 2263 // We should detect the problem through the list, but we should not delete |
| 2270 // the entry, just fail the iteration. | 2264 // the entry, just fail the iteration. |
| 2271 void* iter = NULL; | 2265 disk_cache::Backend::Iterator iter; |
| 2272 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry)); | 2266 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry)); |
| 2273 | 2267 |
| 2274 // Now a full iteration will work, and return one entry. | 2268 // Now a full iteration will work, and return one entry. |
| 2275 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry)); | 2269 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry)); |
| 2276 entry->Close(); | 2270 entry->Close(); |
| 2277 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry)); | 2271 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry)); |
| 2278 | 2272 |
| 2279 // This should detect what's left of the bad entry. | 2273 // This should detect what's left of the bad entry. |
| 2280 EXPECT_NE(net::OK, OpenEntry(second, &entry)); | 2274 EXPECT_NE(net::OK, OpenEntry(second, &entry)); |
| 2281 EXPECT_EQ(2, cache_->GetEntryCount()); | 2275 EXPECT_EQ(2, cache_->GetEntryCount()); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2338 TrimForTest(false); | 2332 TrimForTest(false); |
| 2339 EXPECT_EQ(3, cache_->GetEntryCount()); | 2333 EXPECT_EQ(3, cache_->GetEntryCount()); |
| 2340 TrimForTest(false); | 2334 TrimForTest(false); |
| 2341 EXPECT_EQ(2, cache_->GetEntryCount()); | 2335 EXPECT_EQ(2, cache_->GetEntryCount()); |
| 2342 TrimForTest(false); | 2336 TrimForTest(false); |
| 2343 EXPECT_EQ(1, cache_->GetEntryCount()); | 2337 EXPECT_EQ(1, cache_->GetEntryCount()); |
| 2344 } else { | 2338 } else { |
| 2345 // Detection order: third -> second -> first. | 2339 // Detection order: third -> second -> first. |
| 2346 // We should detect the problem through the list, but we should not delete | 2340 // We should detect the problem through the list, but we should not delete |
| 2347 // the entry. | 2341 // the entry. |
| 2348 void* iter = NULL; | 2342 disk_cache::Backend::Iterator iter; |
| 2349 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry)); | 2343 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry)); |
| 2350 entry->Close(); | 2344 entry->Close(); |
| 2351 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry)); | 2345 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry)); |
| 2352 EXPECT_EQ(first, entry->GetKey()); | 2346 EXPECT_EQ(first, entry->GetKey()); |
| 2353 entry->Close(); | 2347 entry->Close(); |
| 2354 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry)); | 2348 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry)); |
| 2355 } | 2349 } |
| 2356 DisableIntegrityCheck(); | 2350 DisableIntegrityCheck(); |
| 2357 } | 2351 } |
| 2358 | 2352 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2405 TrimForTest(false); | 2399 TrimForTest(false); |
| 2406 EXPECT_EQ(2, cache_->GetEntryCount()); | 2400 EXPECT_EQ(2, cache_->GetEntryCount()); |
| 2407 TrimForTest(false); | 2401 TrimForTest(false); |
| 2408 EXPECT_EQ(1, cache_->GetEntryCount()); | 2402 EXPECT_EQ(1, cache_->GetEntryCount()); |
| 2409 TrimForTest(false); | 2403 TrimForTest(false); |
| 2410 EXPECT_EQ(1, cache_->GetEntryCount()); | 2404 EXPECT_EQ(1, cache_->GetEntryCount()); |
| 2411 } else { | 2405 } else { |
| 2412 // Detection order: third -> second. | 2406 // Detection order: third -> second. |
| 2413 // We should detect the problem through the list, but we should not delete | 2407 // We should detect the problem through the list, but we should not delete |
| 2414 // the entry, just fail the iteration. | 2408 // the entry, just fail the iteration. |
| 2415 void* iter = NULL; | 2409 disk_cache::Backend::Iterator iter; |
| 2416 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry)); | 2410 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry)); |
| 2417 entry->Close(); | 2411 entry->Close(); |
| 2418 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry)); | 2412 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry)); |
| 2419 | 2413 |
| 2420 // Now a full iteration will work, and return two entries. | 2414 // Now a full iteration will work, and return two entries. |
| 2421 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry)); | 2415 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry)); |
| 2422 entry->Close(); | 2416 entry->Close(); |
| 2423 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry)); | 2417 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry)); |
| 2424 entry->Close(); | 2418 entry->Close(); |
| 2425 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry)); | 2419 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry)); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2493 } | 2487 } |
| 2494 | 2488 |
| 2495 TEST_F(DiskCacheBackendTest, NewEvictionInvalidRankings2) { | 2489 TEST_F(DiskCacheBackendTest, NewEvictionInvalidRankings2) { |
| 2496 SetNewEviction(); | 2490 SetNewEviction(); |
| 2497 BackendInvalidRankings2(); | 2491 BackendInvalidRankings2(); |
| 2498 } | 2492 } |
| 2499 | 2493 |
| 2500 // If the LRU is corrupt, we delete the cache. | 2494 // If the LRU is corrupt, we delete the cache. |
| 2501 void DiskCacheBackendTest::BackendInvalidRankings() { | 2495 void DiskCacheBackendTest::BackendInvalidRankings() { |
| 2502 disk_cache::Entry* entry; | 2496 disk_cache::Entry* entry; |
| 2503 void* iter = NULL; | 2497 disk_cache::Backend::Iterator iter; |
| 2504 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry)); | 2498 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry)); |
| 2505 entry->Close(); | 2499 entry->Close(); |
| 2506 EXPECT_EQ(2, cache_->GetEntryCount()); | 2500 EXPECT_EQ(2, cache_->GetEntryCount()); |
| 2507 | 2501 |
| 2508 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry)); | 2502 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry)); |
| 2509 FlushQueueForTest(); // Allow the restart to finish. | 2503 FlushQueueForTest(); // Allow the restart to finish. |
| 2510 EXPECT_EQ(0, cache_->GetEntryCount()); | 2504 EXPECT_EQ(0, cache_->GetEntryCount()); |
| 2511 } | 2505 } |
| 2512 | 2506 |
| 2513 TEST_F(DiskCacheBackendTest, InvalidRankingsSuccess) { | 2507 TEST_F(DiskCacheBackendTest, InvalidRankingsSuccess) { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 2538 DisableFirstCleanup(); | 2532 DisableFirstCleanup(); |
| 2539 SetNewEviction(); | 2533 SetNewEviction(); |
| 2540 InitCache(); | 2534 InitCache(); |
| 2541 SetTestMode(); // Fail cache reinitialization. | 2535 SetTestMode(); // Fail cache reinitialization. |
| 2542 BackendInvalidRankings(); | 2536 BackendInvalidRankings(); |
| 2543 } | 2537 } |
| 2544 | 2538 |
| 2545 // If the LRU is corrupt and we have open entries, we disable the cache. | 2539 // If the LRU is corrupt and we have open entries, we disable the cache. |
| 2546 void DiskCacheBackendTest::BackendDisable() { | 2540 void DiskCacheBackendTest::BackendDisable() { |
| 2547 disk_cache::Entry *entry1, *entry2; | 2541 disk_cache::Entry *entry1, *entry2; |
| 2548 void* iter = NULL; | 2542 disk_cache::Backend::Iterator iter; |
| 2549 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry1)); | 2543 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry1)); |
| 2550 | 2544 |
| 2551 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry2)); | 2545 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry2)); |
| 2552 EXPECT_EQ(0, cache_->GetEntryCount()); | 2546 EXPECT_EQ(0, cache_->GetEntryCount()); |
| 2553 EXPECT_NE(net::OK, CreateEntry("Something new", &entry2)); | 2547 EXPECT_NE(net::OK, CreateEntry("Something new", &entry2)); |
| 2554 | 2548 |
| 2555 entry1->Close(); | 2549 entry1->Close(); |
| 2556 FlushQueueForTest(); // Flushing the Close posts a task to restart the cache. | 2550 FlushQueueForTest(); // Flushing the Close posts a task to restart the cache. |
| 2557 FlushQueueForTest(); // This one actually allows that task to complete. | 2551 FlushQueueForTest(); // This one actually allows that task to complete. |
| 2558 | 2552 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 2589 InitCache(); | 2583 InitCache(); |
| 2590 SetTestMode(); // Fail cache reinitialization. | 2584 SetTestMode(); // Fail cache reinitialization. |
| 2591 BackendDisable(); | 2585 BackendDisable(); |
| 2592 } | 2586 } |
| 2593 | 2587 |
| 2594 // This is another type of corruption on the LRU; disable the cache. | 2588 // This is another type of corruption on the LRU; disable the cache. |
| 2595 void DiskCacheBackendTest::BackendDisable2() { | 2589 void DiskCacheBackendTest::BackendDisable2() { |
| 2596 EXPECT_EQ(8, cache_->GetEntryCount()); | 2590 EXPECT_EQ(8, cache_->GetEntryCount()); |
| 2597 | 2591 |
| 2598 disk_cache::Entry* entry; | 2592 disk_cache::Entry* entry; |
| 2599 void* iter = NULL; | 2593 disk_cache::Backend::Iterator iter; |
| 2600 int count = 0; | 2594 int count = 0; |
| 2601 while (OpenNextEntry(&iter, &entry) == net::OK) { | 2595 while (OpenNextEntry(&iter, &entry) == net::OK) { |
| 2602 ASSERT_TRUE(NULL != entry); | 2596 ASSERT_TRUE(NULL != entry); |
| 2603 entry->Close(); | 2597 entry->Close(); |
| 2604 count++; | 2598 count++; |
| 2605 ASSERT_LT(count, 9); | 2599 ASSERT_LT(count, 9); |
| 2606 }; | 2600 }; |
| 2607 | 2601 |
| 2608 FlushQueueForTest(); | 2602 FlushQueueForTest(); |
| 2609 EXPECT_EQ(0, cache_->GetEntryCount()); | 2603 EXPECT_EQ(0, cache_->GetEntryCount()); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 2637 DisableFirstCleanup(); | 2631 DisableFirstCleanup(); |
| 2638 SetNewEviction(); | 2632 SetNewEviction(); |
| 2639 InitCache(); | 2633 InitCache(); |
| 2640 SetTestMode(); // Fail cache reinitialization. | 2634 SetTestMode(); // Fail cache reinitialization. |
| 2641 BackendDisable2(); | 2635 BackendDisable2(); |
| 2642 } | 2636 } |
| 2643 | 2637 |
| 2644 // If the index size changes when we disable the cache, we should not crash. | 2638 // If the index size changes when we disable the cache, we should not crash. |
| 2645 void DiskCacheBackendTest::BackendDisable3() { | 2639 void DiskCacheBackendTest::BackendDisable3() { |
| 2646 disk_cache::Entry *entry1, *entry2; | 2640 disk_cache::Entry *entry1, *entry2; |
| 2647 void* iter = NULL; | 2641 disk_cache::Backend::Iterator iter; |
| 2648 EXPECT_EQ(2, cache_->GetEntryCount()); | 2642 EXPECT_EQ(2, cache_->GetEntryCount()); |
| 2649 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry1)); | 2643 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry1)); |
| 2650 entry1->Close(); | 2644 entry1->Close(); |
| 2651 | 2645 |
| 2652 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry2)); | 2646 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry2)); |
| 2653 FlushQueueForTest(); | 2647 FlushQueueForTest(); |
| 2654 | 2648 |
| 2655 ASSERT_EQ(net::OK, CreateEntry("Something new", &entry2)); | 2649 ASSERT_EQ(net::OK, CreateEntry("Something new", &entry2)); |
| 2656 entry2->Close(); | 2650 entry2->Close(); |
| 2657 | 2651 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 2671 DisableFirstCleanup(); | 2665 DisableFirstCleanup(); |
| 2672 SetMaxSize(20 * 1024 * 1024); | 2666 SetMaxSize(20 * 1024 * 1024); |
| 2673 SetNewEviction(); | 2667 SetNewEviction(); |
| 2674 InitCache(); | 2668 InitCache(); |
| 2675 BackendDisable3(); | 2669 BackendDisable3(); |
| 2676 } | 2670 } |
| 2677 | 2671 |
| 2678 // If we disable the cache, already open entries should work as far as possible. | 2672 // If we disable the cache, already open entries should work as far as possible. |
| 2679 void DiskCacheBackendTest::BackendDisable4() { | 2673 void DiskCacheBackendTest::BackendDisable4() { |
| 2680 disk_cache::Entry *entry1, *entry2, *entry3, *entry4; | 2674 disk_cache::Entry *entry1, *entry2, *entry3, *entry4; |
| 2681 void* iter = NULL; | 2675 disk_cache::Backend::Iterator iter; |
| 2682 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry1)); | 2676 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry1)); |
| 2683 | 2677 |
| 2684 char key2[2000]; | 2678 char key2[2000]; |
| 2685 char key3[20000]; | 2679 char key3[20000]; |
| 2686 CacheTestFillBuffer(key2, sizeof(key2), true); | 2680 CacheTestFillBuffer(key2, sizeof(key2), true); |
| 2687 CacheTestFillBuffer(key3, sizeof(key3), true); | 2681 CacheTestFillBuffer(key3, sizeof(key3), true); |
| 2688 key2[sizeof(key2) - 1] = '\0'; | 2682 key2[sizeof(key2) - 1] = '\0'; |
| 2689 key3[sizeof(key3) - 1] = '\0'; | 2683 key3[sizeof(key3) - 1] = '\0'; |
| 2690 ASSERT_EQ(net::OK, CreateEntry(key2, &entry2)); | 2684 ASSERT_EQ(net::OK, CreateEntry(key2, &entry2)); |
| 2691 ASSERT_EQ(net::OK, CreateEntry(key3, &entry3)); | 2685 ASSERT_EQ(net::OK, CreateEntry(key3, &entry3)); |
| (...skipping 720 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3412 // Tests basic functionality of the SimpleBackend implementation of the | 3406 // Tests basic functionality of the SimpleBackend implementation of the |
| 3413 // enumeration API. | 3407 // enumeration API. |
| 3414 TEST_F(DiskCacheBackendTest, SimpleCacheEnumerationBasics) { | 3408 TEST_F(DiskCacheBackendTest, SimpleCacheEnumerationBasics) { |
| 3415 SetSimpleCacheMode(); | 3409 SetSimpleCacheMode(); |
| 3416 InitCache(); | 3410 InitCache(); |
| 3417 std::set<std::string> key_pool; | 3411 std::set<std::string> key_pool; |
| 3418 ASSERT_TRUE(CreateSetOfRandomEntries(&key_pool)); | 3412 ASSERT_TRUE(CreateSetOfRandomEntries(&key_pool)); |
| 3419 | 3413 |
| 3420 // Check that enumeration returns all entries. | 3414 // Check that enumeration returns all entries. |
| 3421 std::set<std::string> keys_to_match(key_pool); | 3415 std::set<std::string> keys_to_match(key_pool); |
| 3422 void* iter = NULL; | 3416 disk_cache::Backend::Iterator iter; |
| 3423 size_t count = 0; | 3417 size_t count = 0; |
| 3424 ASSERT_TRUE(EnumerateAndMatchKeys(-1, &iter, &keys_to_match, &count)); | 3418 ASSERT_TRUE(EnumerateAndMatchKeys(-1, &iter, &keys_to_match, &count)); |
| 3425 cache_->EndEnumeration(&iter); | 3419 iter.reset(); |
| 3426 EXPECT_EQ(key_pool.size(), count); | 3420 EXPECT_EQ(key_pool.size(), count); |
| 3427 EXPECT_TRUE(keys_to_match.empty()); | 3421 EXPECT_TRUE(keys_to_match.empty()); |
| 3428 | 3422 |
| 3429 // Check that opening entries does not affect enumeration. | 3423 // Check that opening entries does not affect enumeration. |
| 3430 keys_to_match = key_pool; | 3424 keys_to_match = key_pool; |
| 3431 iter = NULL; | 3425 iter.reset(); |
| 3432 count = 0; | 3426 count = 0; |
| 3433 disk_cache::Entry* entry_opened_before; | 3427 disk_cache::Entry* entry_opened_before; |
| 3434 ASSERT_EQ(net::OK, OpenEntry(*(key_pool.begin()), &entry_opened_before)); | 3428 ASSERT_EQ(net::OK, OpenEntry(*(key_pool.begin()), &entry_opened_before)); |
| 3435 ASSERT_TRUE(EnumerateAndMatchKeys(key_pool.size()/2, | 3429 ASSERT_TRUE(EnumerateAndMatchKeys(key_pool.size()/2, |
| 3436 &iter, | 3430 &iter, |
| 3437 &keys_to_match, | 3431 &keys_to_match, |
| 3438 &count)); | 3432 &count)); |
| 3439 | 3433 |
| 3440 disk_cache::Entry* entry_opened_middle; | 3434 disk_cache::Entry* entry_opened_middle; |
| 3441 ASSERT_EQ(net::OK, | 3435 ASSERT_EQ(net::OK, |
| 3442 OpenEntry(*(keys_to_match.begin()), &entry_opened_middle)); | 3436 OpenEntry(*(keys_to_match.begin()), &entry_opened_middle)); |
| 3443 ASSERT_TRUE(EnumerateAndMatchKeys(-1, &iter, &keys_to_match, &count)); | 3437 ASSERT_TRUE(EnumerateAndMatchKeys(-1, &iter, &keys_to_match, &count)); |
| 3444 cache_->EndEnumeration(&iter); | 3438 iter.reset(); |
| 3445 entry_opened_before->Close(); | 3439 entry_opened_before->Close(); |
| 3446 entry_opened_middle->Close(); | 3440 entry_opened_middle->Close(); |
| 3447 | 3441 |
| 3448 EXPECT_EQ(key_pool.size(), count); | 3442 EXPECT_EQ(key_pool.size(), count); |
| 3449 EXPECT_TRUE(keys_to_match.empty()); | 3443 EXPECT_TRUE(keys_to_match.empty()); |
| 3450 } | 3444 } |
| 3451 | 3445 |
| 3452 // Tests that the enumerations are not affected by dooming an entry in the | 3446 // Tests that the enumerations are not affected by dooming an entry in the |
| 3453 // middle. | 3447 // middle. |
| 3454 TEST_F(DiskCacheBackendTest, SimpleCacheEnumerationWhileDoomed) { | 3448 TEST_F(DiskCacheBackendTest, SimpleCacheEnumerationWhileDoomed) { |
| 3455 SetSimpleCacheMode(); | 3449 SetSimpleCacheMode(); |
| 3456 InitCache(); | 3450 InitCache(); |
| 3457 std::set<std::string> key_pool; | 3451 std::set<std::string> key_pool; |
| 3458 ASSERT_TRUE(CreateSetOfRandomEntries(&key_pool)); | 3452 ASSERT_TRUE(CreateSetOfRandomEntries(&key_pool)); |
| 3459 | 3453 |
| 3460 // Check that enumeration returns all entries but the doomed one. | 3454 // Check that enumeration returns all entries but the doomed one. |
| 3461 std::set<std::string> keys_to_match(key_pool); | 3455 std::set<std::string> keys_to_match(key_pool); |
| 3462 void* iter = NULL; | 3456 disk_cache::Backend::Iterator iter; |
| 3463 size_t count = 0; | 3457 size_t count = 0; |
| 3464 ASSERT_TRUE(EnumerateAndMatchKeys(key_pool.size()/2, | 3458 ASSERT_TRUE(EnumerateAndMatchKeys(key_pool.size()/2, |
| 3465 &iter, | 3459 &iter, |
| 3466 &keys_to_match, | 3460 &keys_to_match, |
| 3467 &count)); | 3461 &count)); |
| 3468 | 3462 |
| 3469 std::string key_to_delete = *(keys_to_match.begin()); | 3463 std::string key_to_delete = *(keys_to_match.begin()); |
| 3470 DoomEntry(key_to_delete); | 3464 DoomEntry(key_to_delete); |
| 3471 keys_to_match.erase(key_to_delete); | 3465 keys_to_match.erase(key_to_delete); |
| 3472 key_pool.erase(key_to_delete); | 3466 key_pool.erase(key_to_delete); |
| 3473 ASSERT_TRUE(EnumerateAndMatchKeys(-1, &iter, &keys_to_match, &count)); | 3467 ASSERT_TRUE(EnumerateAndMatchKeys(-1, &iter, &keys_to_match, &count)); |
| 3474 cache_->EndEnumeration(&iter); | 3468 iter.reset(); |
| 3475 | 3469 |
| 3476 EXPECT_EQ(key_pool.size(), count); | 3470 EXPECT_EQ(key_pool.size(), count); |
| 3477 EXPECT_TRUE(keys_to_match.empty()); | 3471 EXPECT_TRUE(keys_to_match.empty()); |
| 3478 } | 3472 } |
| 3479 | 3473 |
| 3480 // Tests that enumerations are not affected by corrupt files. | 3474 // Tests that enumerations are not affected by corrupt files. |
| 3481 TEST_F(DiskCacheBackendTest, SimpleCacheEnumerationCorruption) { | 3475 TEST_F(DiskCacheBackendTest, SimpleCacheEnumerationCorruption) { |
| 3482 SetSimpleCacheMode(); | 3476 SetSimpleCacheMode(); |
| 3483 InitCache(); | 3477 InitCache(); |
| 3484 std::set<std::string> key_pool; | 3478 std::set<std::string> key_pool; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 3500 ASSERT_EQ(kSize, ReadData(corrupted_entry, 0, 0, buffer.get(), kSize)); | 3494 ASSERT_EQ(kSize, ReadData(corrupted_entry, 0, 0, buffer.get(), kSize)); |
| 3501 corrupted_entry->Close(); | 3495 corrupted_entry->Close(); |
| 3502 | 3496 |
| 3503 EXPECT_TRUE(disk_cache::simple_util::CreateCorruptFileForTests( | 3497 EXPECT_TRUE(disk_cache::simple_util::CreateCorruptFileForTests( |
| 3504 key, cache_path_)); | 3498 key, cache_path_)); |
| 3505 EXPECT_EQ(key_pool.size() + 1, | 3499 EXPECT_EQ(key_pool.size() + 1, |
| 3506 implicit_cast<size_t>(cache_->GetEntryCount())); | 3500 implicit_cast<size_t>(cache_->GetEntryCount())); |
| 3507 | 3501 |
| 3508 // Check that enumeration returns all entries but the corrupt one. | 3502 // Check that enumeration returns all entries but the corrupt one. |
| 3509 std::set<std::string> keys_to_match(key_pool); | 3503 std::set<std::string> keys_to_match(key_pool); |
| 3510 void* iter = NULL; | 3504 disk_cache::Backend::Iterator iter; |
| 3511 size_t count = 0; | 3505 size_t count = 0; |
| 3512 ASSERT_TRUE(EnumerateAndMatchKeys(-1, &iter, &keys_to_match, &count)); | 3506 ASSERT_TRUE(EnumerateAndMatchKeys(-1, &iter, &keys_to_match, &count)); |
| 3513 cache_->EndEnumeration(&iter); | 3507 iter.reset(); |
| 3514 | 3508 |
| 3515 EXPECT_EQ(key_pool.size(), count); | 3509 EXPECT_EQ(key_pool.size(), count); |
| 3516 EXPECT_TRUE(keys_to_match.empty()); | 3510 EXPECT_TRUE(keys_to_match.empty()); |
| 3517 } | 3511 } |
| 3518 | 3512 |
| 3513 // Tests that enumerations don't leak memory when the backend is destructed |
| 3514 // mid-enumeration. |
| 3515 TEST_F(DiskCacheBackendTest, SimpleCacheEnumerationDestruction) { |
| 3516 SetSimpleCacheMode(); |
| 3517 InitCache(); |
| 3518 std::set<std::string> key_pool; |
| 3519 ASSERT_TRUE(CreateSetOfRandomEntries(&key_pool)); |
| 3520 |
| 3521 disk_cache::Backend::Iterator iter; |
| 3522 disk_cache::Entry* entry = NULL; |
| 3523 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry)); |
| 3524 EXPECT_TRUE(entry); |
| 3525 disk_cache::ScopedEntryPtr entry_closer(entry); |
| 3526 |
| 3527 cache_.reset(); |
| 3528 // This test passes if we don't leak memory. |
| 3529 } |
| 3530 |
| 3519 #endif // defined(OS_POSIX) | 3531 #endif // defined(OS_POSIX) |
| OLD | NEW |