Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(367)

Side by Side Diff: net/disk_cache/backend_unittest.cc

Issue 542733002: Remove void** from disk_cache interface. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: narrower Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 // Perform IO operations on the cache until there is pending IO. 74 // Perform IO operations on the cache until there is pending IO.
75 int GeneratePendingIO(net::TestCompletionCallback* cb); 75 int GeneratePendingIO(net::TestCompletionCallback* cb);
76 76
77 // Adds 5 sparse entries. |doomed_start| and |doomed_end| if not NULL, 77 // Adds 5 sparse entries. |doomed_start| and |doomed_end| if not NULL,
78 // will be filled with times, used by DoomEntriesSince and DoomEntriesBetween. 78 // will be filled with times, used by DoomEntriesSince and DoomEntriesBetween.
79 // There are 4 entries after doomed_start and 2 after doomed_end. 79 // There are 4 entries after doomed_start and 2 after doomed_end.
80 void InitSparseCache(base::Time* doomed_start, base::Time* doomed_end); 80 void InitSparseCache(base::Time* doomed_start, base::Time* doomed_end);
81 81
82 bool CreateSetOfRandomEntries(std::set<std::string>* key_pool); 82 bool CreateSetOfRandomEntries(std::set<std::string>* key_pool);
83 bool EnumerateAndMatchKeys(int max_to_open, 83 bool EnumerateAndMatchKeys(int max_to_open,
84 void** iter, 84 const scoped_ptr<TestIterator>& iter,
rvargas (doing something else) 2014/09/18 02:32:11 This should not be a const ref as conceptually thi
gavinp 2014/09/18 18:13:04 Hmm, but that's not what const means though, is it
jkarlin 2014/09/18 18:41:34 The chromium guide says not to pass scoped_ptr by
gavinp 2014/09/18 19:00:39 Good catch. This forces it to TestIterator*. I wo
85 std::set<std::string>* keys_to_match, 85 std::set<std::string>* keys_to_match,
86 size_t* count); 86 size_t* count);
87 87
88 // Actual tests: 88 // Actual tests:
89 void BackendBasics(); 89 void BackendBasics();
90 void BackendKeying(); 90 void BackendKeying();
91 void BackendShutdownWithPendingFileIO(bool fast); 91 void BackendShutdownWithPendingFileIO(bool fast);
92 void BackendShutdownWithPendingIO(bool fast); 92 void BackendShutdownWithPendingIO(bool fast);
93 void BackendShutdownWithPendingCreate(bool fast); 93 void BackendShutdownWithPendingCreate(bool fast);
94 void BackendSetSize(); 94 void BackendSetSize();
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 } 249 }
250 return key_pool->size() == implicit_cast<size_t>(cache_->GetEntryCount()); 250 return key_pool->size() == implicit_cast<size_t>(cache_->GetEntryCount());
251 } 251 }
252 252
253 // Performs iteration over the backend and checks that the keys of entries 253 // Performs iteration over the backend and checks that the keys of entries
254 // opened are in |keys_to_match|, then erases them. Up to |max_to_open| entries 254 // opened are in |keys_to_match|, then erases them. Up to |max_to_open| entries
255 // will be opened, if it is positive. Otherwise, iteration will continue until 255 // will be opened, if it is positive. Otherwise, iteration will continue until
256 // OpenNextEntry stops returning net::OK. 256 // OpenNextEntry stops returning net::OK.
257 bool DiskCacheBackendTest::EnumerateAndMatchKeys( 257 bool DiskCacheBackendTest::EnumerateAndMatchKeys(
258 int max_to_open, 258 int max_to_open,
259 void** iter, 259 const scoped_ptr<TestIterator>& iter,
260 std::set<std::string>* keys_to_match, 260 std::set<std::string>* keys_to_match,
261 size_t* count) { 261 size_t* count) {
262 disk_cache::Entry* entry; 262 disk_cache::Entry* entry;
263 263
264 while (OpenNextEntry(iter, &entry) == net::OK) { 264 if (!iter)
265 return false;
266 while (iter->OpenNextEntry(&entry) == net::OK) {
265 if (!entry) 267 if (!entry)
266 return false; 268 return false;
267 EXPECT_EQ(1U, keys_to_match->erase(entry->GetKey())); 269 EXPECT_EQ(1U, keys_to_match->erase(entry->GetKey()));
268 entry->Close(); 270 entry->Close();
269 ++(*count); 271 ++(*count);
270 if (max_to_open >= 0 && implicit_cast<int>(*count) >= max_to_open) 272 if (max_to_open >= 0 && implicit_cast<int>(*count) >= max_to_open)
271 break; 273 break;
272 }; 274 };
273 275
274 return true; 276 return true;
(...skipping 955 matching lines...) Expand 10 before | Expand all | Expand 10 after
1230 for (int i = 0; i < kNumEntries; i++) { 1232 for (int i = 0; i < kNumEntries; i++) {
1231 std::string key = GenerateKey(true); 1233 std::string key = GenerateKey(true);
1232 disk_cache::Entry* entry; 1234 disk_cache::Entry* entry;
1233 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); 1235 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
1234 entry->Close(); 1236 entry->Close();
1235 } 1237 }
1236 EXPECT_EQ(kNumEntries, cache_->GetEntryCount()); 1238 EXPECT_EQ(kNumEntries, cache_->GetEntryCount());
1237 Time final = Time::Now(); 1239 Time final = Time::Now();
1238 1240
1239 disk_cache::Entry* entry; 1241 disk_cache::Entry* entry;
1240 void* iter = NULL; 1242 scoped_ptr<TestIterator> iter = CreateIterator();
1241 int count = 0; 1243 int count = 0;
1242 Time last_modified[kNumEntries]; 1244 Time last_modified[kNumEntries];
1243 Time last_used[kNumEntries]; 1245 Time last_used[kNumEntries];
1244 while (OpenNextEntry(&iter, &entry) == net::OK) { 1246 while (iter->OpenNextEntry(&entry) == net::OK) {
1245 ASSERT_TRUE(NULL != entry); 1247 ASSERT_TRUE(NULL != entry);
1246 if (count < kNumEntries) { 1248 if (count < kNumEntries) {
1247 last_modified[count] = entry->GetLastModified(); 1249 last_modified[count] = entry->GetLastModified();
1248 last_used[count] = entry->GetLastUsed(); 1250 last_used[count] = entry->GetLastUsed();
1249 EXPECT_TRUE(initial <= last_modified[count]); 1251 EXPECT_TRUE(initial <= last_modified[count]);
1250 EXPECT_TRUE(final >= last_modified[count]); 1252 EXPECT_TRUE(final >= last_modified[count]);
1251 } 1253 }
1252 1254
1253 entry->Close(); 1255 entry->Close();
1254 count++; 1256 count++;
1255 }; 1257 };
1256 EXPECT_EQ(kNumEntries, count); 1258 EXPECT_EQ(kNumEntries, count);
1257 1259
1258 iter = NULL; 1260 iter = CreateIterator();
1259 count = 0; 1261 count = 0;
1260 // The previous enumeration should not have changed the timestamps. 1262 // The previous enumeration should not have changed the timestamps.
1261 while (OpenNextEntry(&iter, &entry) == net::OK) { 1263 while (iter->OpenNextEntry(&entry) == net::OK) {
1262 ASSERT_TRUE(NULL != entry); 1264 ASSERT_TRUE(NULL != entry);
1263 if (count < kNumEntries) { 1265 if (count < kNumEntries) {
1264 EXPECT_TRUE(last_modified[count] == entry->GetLastModified()); 1266 EXPECT_TRUE(last_modified[count] == entry->GetLastModified());
1265 EXPECT_TRUE(last_used[count] == entry->GetLastUsed()); 1267 EXPECT_TRUE(last_used[count] == entry->GetLastUsed());
1266 } 1268 }
1267 entry->Close(); 1269 entry->Close();
1268 count++; 1270 count++;
1269 }; 1271 };
1270 EXPECT_EQ(kNumEntries, count); 1272 EXPECT_EQ(kNumEntries, count);
1271 } 1273 }
(...skipping 30 matching lines...) Expand all
1302 disk_cache::Entry *entry1, *entry2; 1304 disk_cache::Entry *entry1, *entry2;
1303 ASSERT_EQ(net::OK, CreateEntry(first, &entry1)); 1305 ASSERT_EQ(net::OK, CreateEntry(first, &entry1));
1304 entry1->Close(); 1306 entry1->Close();
1305 ASSERT_EQ(net::OK, CreateEntry(second, &entry2)); 1307 ASSERT_EQ(net::OK, CreateEntry(second, &entry2));
1306 entry2->Close(); 1308 entry2->Close();
1307 FlushQueueForTest(); 1309 FlushQueueForTest();
1308 1310
1309 // Make sure that the timestamp is not the same. 1311 // Make sure that the timestamp is not the same.
1310 AddDelay(); 1312 AddDelay();
1311 ASSERT_EQ(net::OK, OpenEntry(second, &entry1)); 1313 ASSERT_EQ(net::OK, OpenEntry(second, &entry1));
1312 void* iter = NULL; 1314 scoped_ptr<TestIterator> iter = CreateIterator();
1313 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry2)); 1315 ASSERT_EQ(net::OK, iter->OpenNextEntry(&entry2));
1314 EXPECT_EQ(entry2->GetKey(), second); 1316 EXPECT_EQ(entry2->GetKey(), second);
1315 1317
1316 // Two entries and the iterator pointing at "first". 1318 // Two entries and the iterator pointing at "first".
1317 entry1->Close(); 1319 entry1->Close();
1318 entry2->Close(); 1320 entry2->Close();
1319 1321
1320 // The iterator should still be valid, so we should not crash. 1322 // The iterator should still be valid, so we should not crash.
1321 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry2)); 1323 ASSERT_EQ(net::OK, iter->OpenNextEntry(&entry2));
1322 EXPECT_EQ(entry2->GetKey(), first); 1324 EXPECT_EQ(entry2->GetKey(), first);
1323 entry2->Close(); 1325 entry2->Close();
1324 cache_->EndEnumeration(&iter); 1326 iter = CreateIterator();
1325 1327
1326 // Modify the oldest entry and get the newest element. 1328 // Modify the oldest entry and get the newest element.
1327 ASSERT_EQ(net::OK, OpenEntry(first, &entry1)); 1329 ASSERT_EQ(net::OK, OpenEntry(first, &entry1));
1328 EXPECT_EQ(0, WriteData(entry1, 0, 200, NULL, 0, false)); 1330 EXPECT_EQ(0, WriteData(entry1, 0, 200, NULL, 0, false));
1329 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry2)); 1331 ASSERT_EQ(net::OK, iter->OpenNextEntry(&entry2));
1330 if (type_ == net::APP_CACHE) { 1332 if (type_ == net::APP_CACHE) {
1331 // The list is not updated. 1333 // The list is not updated.
1332 EXPECT_EQ(entry2->GetKey(), second); 1334 EXPECT_EQ(entry2->GetKey(), second);
1333 } else { 1335 } else {
1334 EXPECT_EQ(entry2->GetKey(), first); 1336 EXPECT_EQ(entry2->GetKey(), first);
1335 } 1337 }
1336 1338
1337 entry1->Close(); 1339 entry1->Close();
1338 entry2->Close(); 1340 entry2->Close();
1339 cache_->EndEnumeration(&iter);
1340 } 1341 }
1341 1342
1342 TEST_F(DiskCacheBackendTest, Enumerations2) { 1343 TEST_F(DiskCacheBackendTest, Enumerations2) {
1343 BackendEnumerations2(); 1344 BackendEnumerations2();
1344 } 1345 }
1345 1346
1346 TEST_F(DiskCacheBackendTest, NewEvictionEnumerations2) { 1347 TEST_F(DiskCacheBackendTest, NewEvictionEnumerations2) {
1347 SetNewEviction(); 1348 SetNewEviction();
1348 BackendEnumerations2(); 1349 BackendEnumerations2();
1349 } 1350 }
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1384 1385
1385 FlushQueueForTest(); 1386 FlushQueueForTest();
1386 1387
1387 // Make sure that the timestamp is not the same. 1388 // Make sure that the timestamp is not the same.
1388 AddDelay(); 1389 AddDelay();
1389 1390
1390 // Read from the last item in the LRU. 1391 // Read from the last item in the LRU.
1391 EXPECT_EQ(kSize, ReadData(entry1, 0, 0, buffer1.get(), kSize)); 1392 EXPECT_EQ(kSize, ReadData(entry1, 0, 0, buffer1.get(), kSize));
1392 entry1->Close(); 1393 entry1->Close();
1393 1394
1394 void* iter = NULL; 1395 scoped_ptr<TestIterator> iter = CreateIterator();
1395 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry2)); 1396 ASSERT_EQ(net::OK, iter->OpenNextEntry(&entry2));
1396 EXPECT_EQ(entry2->GetKey(), second); 1397 EXPECT_EQ(entry2->GetKey(), second);
1397 entry2->Close(); 1398 entry2->Close();
1398 cache_->EndEnumeration(&iter);
1399 } 1399 }
1400 1400
1401 #if !defined(LEAK_SANITIZER) 1401 #if !defined(LEAK_SANITIZER)
1402 // Verify handling of invalid entries while doing enumerations. 1402 // Verify handling of invalid entries while doing enumerations.
1403 // We'll be leaking memory from this test. 1403 // We'll be leaking memory from this test.
1404 void DiskCacheBackendTest::BackendInvalidEntryEnumeration() { 1404 void DiskCacheBackendTest::BackendInvalidEntryEnumeration() {
1405 InitCache(); 1405 InitCache();
1406 1406
1407 std::string key("Some key"); 1407 std::string key("Some key");
1408 disk_cache::Entry *entry, *entry1, *entry2; 1408 disk_cache::Entry *entry, *entry1, *entry2;
1409 ASSERT_EQ(net::OK, CreateEntry(key, &entry1)); 1409 ASSERT_EQ(net::OK, CreateEntry(key, &entry1));
1410 1410
1411 const int kSize = 50; 1411 const int kSize = 50;
1412 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize)); 1412 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize));
1413 memset(buffer1->data(), 0, kSize); 1413 memset(buffer1->data(), 0, kSize);
1414 base::strlcpy(buffer1->data(), "And the data to save", kSize); 1414 base::strlcpy(buffer1->data(), "And the data to save", kSize);
1415 EXPECT_EQ(kSize, WriteData(entry1, 0, 0, buffer1.get(), kSize, false)); 1415 EXPECT_EQ(kSize, WriteData(entry1, 0, 0, buffer1.get(), kSize, false));
1416 entry1->Close(); 1416 entry1->Close();
1417 ASSERT_EQ(net::OK, OpenEntry(key, &entry1)); 1417 ASSERT_EQ(net::OK, OpenEntry(key, &entry1));
1418 EXPECT_EQ(kSize, ReadData(entry1, 0, 0, buffer1.get(), kSize)); 1418 EXPECT_EQ(kSize, ReadData(entry1, 0, 0, buffer1.get(), kSize));
1419 1419
1420 std::string key2("Another key"); 1420 std::string key2("Another key");
1421 ASSERT_EQ(net::OK, CreateEntry(key2, &entry2)); 1421 ASSERT_EQ(net::OK, CreateEntry(key2, &entry2));
1422 entry2->Close(); 1422 entry2->Close();
1423 ASSERT_EQ(2, cache_->GetEntryCount()); 1423 ASSERT_EQ(2, cache_->GetEntryCount());
1424 1424
1425 SimulateCrash(); 1425 SimulateCrash();
1426 1426
1427 void* iter = NULL; 1427 scoped_ptr<TestIterator> iter = CreateIterator();
1428 int count = 0; 1428 int count = 0;
1429 while (OpenNextEntry(&iter, &entry) == net::OK) { 1429 while (iter->OpenNextEntry(&entry) == net::OK) {
1430 ASSERT_TRUE(NULL != entry); 1430 ASSERT_TRUE(NULL != entry);
1431 EXPECT_EQ(key2, entry->GetKey()); 1431 EXPECT_EQ(key2, entry->GetKey());
1432 entry->Close(); 1432 entry->Close();
1433 count++; 1433 count++;
1434 }; 1434 };
1435 EXPECT_EQ(1, count); 1435 EXPECT_EQ(1, count);
1436 EXPECT_EQ(1, cache_->GetEntryCount()); 1436 EXPECT_EQ(1, cache_->GetEntryCount());
1437 } 1437 }
1438 1438
1439 // We'll be leaking memory from this test. 1439 // We'll be leaking memory from this test.
(...skipping 19 matching lines...) Expand all
1459 const int kNumEntries = 10; 1459 const int kNumEntries = 10;
1460 for (int i = 0; i < kNumEntries; i++) { 1460 for (int i = 0; i < kNumEntries; i++) {
1461 std::string key = GenerateKey(true); 1461 std::string key = GenerateKey(true);
1462 disk_cache::Entry* entry; 1462 disk_cache::Entry* entry;
1463 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); 1463 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
1464 entry->Close(); 1464 entry->Close();
1465 } 1465 }
1466 EXPECT_EQ(kNumEntries, cache_->GetEntryCount()); 1466 EXPECT_EQ(kNumEntries, cache_->GetEntryCount());
1467 1467
1468 disk_cache::Entry *entry1, *entry2; 1468 disk_cache::Entry *entry1, *entry2;
1469 void* iter1 = NULL; 1469 scoped_ptr<TestIterator> iter1 = CreateIterator(), iter2 = CreateIterator();
1470 void* iter2 = NULL; 1470 ASSERT_EQ(net::OK, iter1->OpenNextEntry(&entry1));
1471 ASSERT_EQ(net::OK, OpenNextEntry(&iter1, &entry1));
1472 ASSERT_TRUE(NULL != entry1); 1471 ASSERT_TRUE(NULL != entry1);
1473 entry1->Close(); 1472 entry1->Close();
1474 entry1 = NULL; 1473 entry1 = NULL;
1475 1474
1476 // Let's go to the middle of the list. 1475 // Let's go to the middle of the list.
1477 for (int i = 0; i < kNumEntries / 2; i++) { 1476 for (int i = 0; i < kNumEntries / 2; i++) {
1478 if (entry1) 1477 if (entry1)
1479 entry1->Close(); 1478 entry1->Close();
1480 ASSERT_EQ(net::OK, OpenNextEntry(&iter1, &entry1)); 1479 ASSERT_EQ(net::OK, iter1->OpenNextEntry(&entry1));
1481 ASSERT_TRUE(NULL != entry1); 1480 ASSERT_TRUE(NULL != entry1);
1482 1481
1483 ASSERT_EQ(net::OK, OpenNextEntry(&iter2, &entry2)); 1482 ASSERT_EQ(net::OK, iter2->OpenNextEntry(&entry2));
1484 ASSERT_TRUE(NULL != entry2); 1483 ASSERT_TRUE(NULL != entry2);
1485 entry2->Close(); 1484 entry2->Close();
1486 } 1485 }
1487 1486
1488 // Messing up with entry1 will modify entry2->next. 1487 // Messing up with entry1 will modify entry2->next.
1489 entry1->Doom(); 1488 entry1->Doom();
1490 ASSERT_EQ(net::OK, OpenNextEntry(&iter2, &entry2)); 1489 ASSERT_EQ(net::OK, iter2->OpenNextEntry(&entry2));
1491 ASSERT_TRUE(NULL != entry2); 1490 ASSERT_TRUE(NULL != entry2);
1492 1491
1493 // The link entry2->entry1 should be broken. 1492 // The link entry2->entry1 should be broken.
1494 EXPECT_NE(entry2->GetKey(), entry1->GetKey()); 1493 EXPECT_NE(entry2->GetKey(), entry1->GetKey());
1495 entry1->Close(); 1494 entry1->Close();
1496 entry2->Close(); 1495 entry2->Close();
1497 1496
1498 // And the second iterator should keep working. 1497 // And the second iterator should keep working.
1499 ASSERT_EQ(net::OK, OpenNextEntry(&iter2, &entry2)); 1498 ASSERT_EQ(net::OK, iter2->OpenNextEntry(&entry2));
1500 ASSERT_TRUE(NULL != entry2); 1499 ASSERT_TRUE(NULL != entry2);
1501 entry2->Close(); 1500 entry2->Close();
1502
1503 cache_->EndEnumeration(&iter1);
1504 cache_->EndEnumeration(&iter2);
1505 } 1501 }
1506 1502
1507 TEST_F(DiskCacheBackendTest, FixEnumerators) { 1503 TEST_F(DiskCacheBackendTest, FixEnumerators) {
1508 BackendFixEnumerators(); 1504 BackendFixEnumerators();
1509 } 1505 }
1510 1506
1511 TEST_F(DiskCacheBackendTest, NewEvictionFixEnumerators) { 1507 TEST_F(DiskCacheBackendTest, NewEvictionFixEnumerators) {
1512 SetNewEviction(); 1508 SetNewEviction();
1513 BackendFixEnumerators(); 1509 BackendFixEnumerators();
1514 } 1510 }
(...skipping 505 matching lines...) Expand 10 before | Expand all | Expand 10 after
2020 } 2016 }
2021 2017
2022 // 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.
2023 void DiskCacheBackendTest::BackendInvalidEntry3() { 2019 void DiskCacheBackendTest::BackendInvalidEntry3() {
2024 SetMask(0x1); // 2-entry table. 2020 SetMask(0x1); // 2-entry table.
2025 SetMaxSize(0x3000); // 12 kB. 2021 SetMaxSize(0x3000); // 12 kB.
2026 DisableFirstCleanup(); 2022 DisableFirstCleanup();
2027 InitCache(); 2023 InitCache();
2028 2024
2029 disk_cache::Entry* entry; 2025 disk_cache::Entry* entry;
2030 void* iter = NULL; 2026 scoped_ptr<TestIterator> iter = CreateIterator();
2031 while (OpenNextEntry(&iter, &entry) == net::OK) { 2027 while (iter->OpenNextEntry(&entry) == net::OK) {
2032 entry->Close(); 2028 entry->Close();
2033 } 2029 }
2034 } 2030 }
2035 2031
2036 TEST_F(DiskCacheBackendTest, InvalidEntry3) { 2032 TEST_F(DiskCacheBackendTest, InvalidEntry3) {
2037 ASSERT_TRUE(CopyTestCache("dirty_entry3")); 2033 ASSERT_TRUE(CopyTestCache("dirty_entry3"));
2038 BackendInvalidEntry3(); 2034 BackendInvalidEntry3();
2039 } 2035 }
2040 2036
2041 TEST_F(DiskCacheBackendTest, NewEvictionInvalidEntry3) { 2037 TEST_F(DiskCacheBackendTest, NewEvictionInvalidEntry3) {
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
2165 entry_impl->rankings()->Store(); 2161 entry_impl->rankings()->Store();
2166 entry->Close(); 2162 entry->Close();
2167 FlushQueueForTest(); 2163 FlushQueueForTest();
2168 EXPECT_EQ(2, cache_->GetEntryCount()); 2164 EXPECT_EQ(2, cache_->GetEntryCount());
2169 2165
2170 // This should detect the bad entry. 2166 // This should detect the bad entry.
2171 EXPECT_NE(net::OK, OpenEntry(second, &entry)); 2167 EXPECT_NE(net::OK, OpenEntry(second, &entry));
2172 EXPECT_EQ(1, cache_->GetEntryCount()); 2168 EXPECT_EQ(1, cache_->GetEntryCount());
2173 2169
2174 // 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.
2175 void* iter = NULL; 2171 scoped_ptr<TestIterator> iter = CreateIterator();
2176 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry)); 2172 EXPECT_NE(net::OK, iter->OpenNextEntry(&entry));
2177 FlushQueueForTest(); 2173 FlushQueueForTest();
2178 EXPECT_EQ(0, cache_->GetEntryCount()); 2174 EXPECT_EQ(0, cache_->GetEntryCount());
2179 } 2175 }
2180 2176
2181 TEST_F(DiskCacheBackendTest, InvalidEntry7) { 2177 TEST_F(DiskCacheBackendTest, InvalidEntry7) {
2182 BackendInvalidEntry7(); 2178 BackendInvalidEntry7();
2183 } 2179 }
2184 2180
2185 TEST_F(DiskCacheBackendTest, NewEvictionInvalidEntry7) { 2181 TEST_F(DiskCacheBackendTest, NewEvictionInvalidEntry7) {
2186 SetNewEviction(); 2182 SetNewEviction();
(...skipping 22 matching lines...) Expand all
2209 entry_impl->rankings()->Store(); 2205 entry_impl->rankings()->Store();
2210 entry->Close(); 2206 entry->Close();
2211 FlushQueueForTest(); 2207 FlushQueueForTest();
2212 EXPECT_EQ(2, cache_->GetEntryCount()); 2208 EXPECT_EQ(2, cache_->GetEntryCount());
2213 2209
2214 // This should detect the bad entry. 2210 // This should detect the bad entry.
2215 EXPECT_NE(net::OK, OpenEntry(second, &entry)); 2211 EXPECT_NE(net::OK, OpenEntry(second, &entry));
2216 EXPECT_EQ(1, cache_->GetEntryCount()); 2212 EXPECT_EQ(1, cache_->GetEntryCount());
2217 2213
2218 // We should not delete the cache. 2214 // We should not delete the cache.
2219 void* iter = NULL; 2215 scoped_ptr<TestIterator> iter = CreateIterator();
2220 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry)); 2216 ASSERT_EQ(net::OK, iter->OpenNextEntry(&entry));
2221 entry->Close(); 2217 entry->Close();
2222 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry)); 2218 EXPECT_NE(net::OK, iter->OpenNextEntry(&entry));
2223 EXPECT_EQ(1, cache_->GetEntryCount()); 2219 EXPECT_EQ(1, cache_->GetEntryCount());
2224 } 2220 }
2225 2221
2226 TEST_F(DiskCacheBackendTest, InvalidEntry8) { 2222 TEST_F(DiskCacheBackendTest, InvalidEntry8) {
2227 BackendInvalidEntry8(); 2223 BackendInvalidEntry8();
2228 } 2224 }
2229 2225
2230 TEST_F(DiskCacheBackendTest, NewEvictionInvalidEntry8) { 2226 TEST_F(DiskCacheBackendTest, NewEvictionInvalidEntry8) {
2231 SetNewEviction(); 2227 SetNewEviction();
2232 BackendInvalidEntry8(); 2228 BackendInvalidEntry8();
(...skipping 26 matching lines...) Expand all
2259 EXPECT_EQ(2, cache_->GetEntryCount()); 2255 EXPECT_EQ(2, cache_->GetEntryCount());
2260 2256
2261 if (eviction) { 2257 if (eviction) {
2262 TrimForTest(false); 2258 TrimForTest(false);
2263 EXPECT_EQ(1, cache_->GetEntryCount()); 2259 EXPECT_EQ(1, cache_->GetEntryCount());
2264 TrimForTest(false); 2260 TrimForTest(false);
2265 EXPECT_EQ(1, cache_->GetEntryCount()); 2261 EXPECT_EQ(1, cache_->GetEntryCount());
2266 } else { 2262 } else {
2267 // 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
2268 // the entry, just fail the iteration. 2264 // the entry, just fail the iteration.
2269 void* iter = NULL; 2265 scoped_ptr<TestIterator> iter = CreateIterator();
2270 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry)); 2266 EXPECT_NE(net::OK, iter->OpenNextEntry(&entry));
2271 2267
2272 // Now a full iteration will work, and return one entry. 2268 // Now a full iteration will work, and return one entry.
2273 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry)); 2269 ASSERT_EQ(net::OK, iter->OpenNextEntry(&entry));
2274 entry->Close(); 2270 entry->Close();
2275 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry)); 2271 EXPECT_NE(net::OK, iter->OpenNextEntry(&entry));
2276 2272
2277 // This should detect what's left of the bad entry. 2273 // This should detect what's left of the bad entry.
2278 EXPECT_NE(net::OK, OpenEntry(second, &entry)); 2274 EXPECT_NE(net::OK, OpenEntry(second, &entry));
2279 EXPECT_EQ(2, cache_->GetEntryCount()); 2275 EXPECT_EQ(2, cache_->GetEntryCount());
2280 } 2276 }
2281 DisableIntegrityCheck(); 2277 DisableIntegrityCheck();
2282 } 2278 }
2283 2279
2284 TEST_F(DiskCacheBackendTest, InvalidEntry9) { 2280 TEST_F(DiskCacheBackendTest, InvalidEntry9) {
2285 BackendInvalidEntry9(false); 2281 BackendInvalidEntry9(false);
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
2336 TrimForTest(false); 2332 TrimForTest(false);
2337 EXPECT_EQ(3, cache_->GetEntryCount()); 2333 EXPECT_EQ(3, cache_->GetEntryCount());
2338 TrimForTest(false); 2334 TrimForTest(false);
2339 EXPECT_EQ(2, cache_->GetEntryCount()); 2335 EXPECT_EQ(2, cache_->GetEntryCount());
2340 TrimForTest(false); 2336 TrimForTest(false);
2341 EXPECT_EQ(1, cache_->GetEntryCount()); 2337 EXPECT_EQ(1, cache_->GetEntryCount());
2342 } else { 2338 } else {
2343 // Detection order: third -> second -> first. 2339 // Detection order: third -> second -> first.
2344 // 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
2345 // the entry. 2341 // the entry.
2346 void* iter = NULL; 2342 scoped_ptr<TestIterator> iter = CreateIterator();
2347 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry)); 2343 ASSERT_EQ(net::OK, iter->OpenNextEntry(&entry));
2348 entry->Close(); 2344 entry->Close();
2349 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry)); 2345 ASSERT_EQ(net::OK, iter->OpenNextEntry(&entry));
2350 EXPECT_EQ(first, entry->GetKey()); 2346 EXPECT_EQ(first, entry->GetKey());
2351 entry->Close(); 2347 entry->Close();
2352 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry)); 2348 EXPECT_NE(net::OK, iter->OpenNextEntry(&entry));
2353 } 2349 }
2354 DisableIntegrityCheck(); 2350 DisableIntegrityCheck();
2355 } 2351 }
2356 2352
2357 TEST_F(DiskCacheBackendTest, InvalidEntry10) { 2353 TEST_F(DiskCacheBackendTest, InvalidEntry10) {
2358 BackendInvalidEntry10(false); 2354 BackendInvalidEntry10(false);
2359 } 2355 }
2360 2356
2361 TEST_F(DiskCacheBackendTest, TrimInvalidEntry10) { 2357 TEST_F(DiskCacheBackendTest, TrimInvalidEntry10) {
2362 BackendInvalidEntry10(true); 2358 BackendInvalidEntry10(true);
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
2403 TrimForTest(false); 2399 TrimForTest(false);
2404 EXPECT_EQ(2, cache_->GetEntryCount()); 2400 EXPECT_EQ(2, cache_->GetEntryCount());
2405 TrimForTest(false); 2401 TrimForTest(false);
2406 EXPECT_EQ(1, cache_->GetEntryCount()); 2402 EXPECT_EQ(1, cache_->GetEntryCount());
2407 TrimForTest(false); 2403 TrimForTest(false);
2408 EXPECT_EQ(1, cache_->GetEntryCount()); 2404 EXPECT_EQ(1, cache_->GetEntryCount());
2409 } else { 2405 } else {
2410 // Detection order: third -> second. 2406 // Detection order: third -> second.
2411 // 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
2412 // the entry, just fail the iteration. 2408 // the entry, just fail the iteration.
2413 void* iter = NULL; 2409 scoped_ptr<TestIterator> iter = CreateIterator();
2414 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry)); 2410 ASSERT_EQ(net::OK, iter->OpenNextEntry(&entry));
2415 entry->Close(); 2411 entry->Close();
2416 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry)); 2412 EXPECT_NE(net::OK, iter->OpenNextEntry(&entry));
2417 2413
2418 // Now a full iteration will work, and return two entries. 2414 // Now a full iteration will work, and return two entries.
2419 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry)); 2415 ASSERT_EQ(net::OK, iter->OpenNextEntry(&entry));
2420 entry->Close(); 2416 entry->Close();
2421 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry)); 2417 ASSERT_EQ(net::OK, iter->OpenNextEntry(&entry));
2422 entry->Close(); 2418 entry->Close();
2423 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry)); 2419 EXPECT_NE(net::OK, iter->OpenNextEntry(&entry));
2424 } 2420 }
2425 DisableIntegrityCheck(); 2421 DisableIntegrityCheck();
2426 } 2422 }
2427 2423
2428 TEST_F(DiskCacheBackendTest, InvalidEntry11) { 2424 TEST_F(DiskCacheBackendTest, InvalidEntry11) {
2429 BackendInvalidEntry11(false); 2425 BackendInvalidEntry11(false);
2430 } 2426 }
2431 2427
2432 TEST_F(DiskCacheBackendTest, TrimInvalidEntry11) { 2428 TEST_F(DiskCacheBackendTest, TrimInvalidEntry11) {
2433 BackendInvalidEntry11(true); 2429 BackendInvalidEntry11(true);
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
2491 } 2487 }
2492 2488
2493 TEST_F(DiskCacheBackendTest, NewEvictionInvalidRankings2) { 2489 TEST_F(DiskCacheBackendTest, NewEvictionInvalidRankings2) {
2494 SetNewEviction(); 2490 SetNewEviction();
2495 BackendInvalidRankings2(); 2491 BackendInvalidRankings2();
2496 } 2492 }
2497 2493
2498 // If the LRU is corrupt, we delete the cache. 2494 // If the LRU is corrupt, we delete the cache.
2499 void DiskCacheBackendTest::BackendInvalidRankings() { 2495 void DiskCacheBackendTest::BackendInvalidRankings() {
2500 disk_cache::Entry* entry; 2496 disk_cache::Entry* entry;
2501 void* iter = NULL; 2497 scoped_ptr<TestIterator> iter = CreateIterator();
2502 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry)); 2498 ASSERT_EQ(net::OK, iter->OpenNextEntry(&entry));
2503 entry->Close(); 2499 entry->Close();
2504 EXPECT_EQ(2, cache_->GetEntryCount()); 2500 EXPECT_EQ(2, cache_->GetEntryCount());
2505 2501
2506 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry)); 2502 EXPECT_NE(net::OK, iter->OpenNextEntry(&entry));
2507 FlushQueueForTest(); // Allow the restart to finish. 2503 FlushQueueForTest(); // Allow the restart to finish.
2508 EXPECT_EQ(0, cache_->GetEntryCount()); 2504 EXPECT_EQ(0, cache_->GetEntryCount());
2509 } 2505 }
2510 2506
2511 TEST_F(DiskCacheBackendTest, InvalidRankingsSuccess) { 2507 TEST_F(DiskCacheBackendTest, InvalidRankingsSuccess) {
2512 ASSERT_TRUE(CopyTestCache("bad_rankings")); 2508 ASSERT_TRUE(CopyTestCache("bad_rankings"));
2513 DisableFirstCleanup(); 2509 DisableFirstCleanup();
2514 InitCache(); 2510 InitCache();
2515 BackendInvalidRankings(); 2511 BackendInvalidRankings();
2516 } 2512 }
(...skipping 19 matching lines...) Expand all
2536 DisableFirstCleanup(); 2532 DisableFirstCleanup();
2537 SetNewEviction(); 2533 SetNewEviction();
2538 InitCache(); 2534 InitCache();
2539 SetTestMode(); // Fail cache reinitialization. 2535 SetTestMode(); // Fail cache reinitialization.
2540 BackendInvalidRankings(); 2536 BackendInvalidRankings();
2541 } 2537 }
2542 2538
2543 // 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.
2544 void DiskCacheBackendTest::BackendDisable() { 2540 void DiskCacheBackendTest::BackendDisable() {
2545 disk_cache::Entry *entry1, *entry2; 2541 disk_cache::Entry *entry1, *entry2;
2546 void* iter = NULL; 2542 scoped_ptr<TestIterator> iter = CreateIterator();
2547 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry1)); 2543 ASSERT_EQ(net::OK, iter->OpenNextEntry(&entry1));
2548 2544
2549 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry2)); 2545 EXPECT_NE(net::OK, iter->OpenNextEntry(&entry2));
2550 EXPECT_EQ(0, cache_->GetEntryCount()); 2546 EXPECT_EQ(0, cache_->GetEntryCount());
2551 EXPECT_NE(net::OK, CreateEntry("Something new", &entry2)); 2547 EXPECT_NE(net::OK, CreateEntry("Something new", &entry2));
2552 2548
2553 entry1->Close(); 2549 entry1->Close();
2554 FlushQueueForTest(); // Flushing the Close posts a task to restart the cache. 2550 FlushQueueForTest(); // Flushing the Close posts a task to restart the cache.
2555 FlushQueueForTest(); // This one actually allows that task to complete. 2551 FlushQueueForTest(); // This one actually allows that task to complete.
2556 2552
2557 EXPECT_EQ(0, cache_->GetEntryCount()); 2553 EXPECT_EQ(0, cache_->GetEntryCount());
2558 } 2554 }
2559 2555
(...skipping 27 matching lines...) Expand all
2587 InitCache(); 2583 InitCache();
2588 SetTestMode(); // Fail cache reinitialization. 2584 SetTestMode(); // Fail cache reinitialization.
2589 BackendDisable(); 2585 BackendDisable();
2590 } 2586 }
2591 2587
2592 // 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.
2593 void DiskCacheBackendTest::BackendDisable2() { 2589 void DiskCacheBackendTest::BackendDisable2() {
2594 EXPECT_EQ(8, cache_->GetEntryCount()); 2590 EXPECT_EQ(8, cache_->GetEntryCount());
2595 2591
2596 disk_cache::Entry* entry; 2592 disk_cache::Entry* entry;
2597 void* iter = NULL; 2593 scoped_ptr<TestIterator> iter = CreateIterator();
2598 int count = 0; 2594 int count = 0;
2599 while (OpenNextEntry(&iter, &entry) == net::OK) { 2595 while (iter->OpenNextEntry(&entry) == net::OK) {
2600 ASSERT_TRUE(NULL != entry); 2596 ASSERT_TRUE(NULL != entry);
2601 entry->Close(); 2597 entry->Close();
2602 count++; 2598 count++;
2603 ASSERT_LT(count, 9); 2599 ASSERT_LT(count, 9);
2604 }; 2600 };
2605 2601
2606 FlushQueueForTest(); 2602 FlushQueueForTest();
2607 EXPECT_EQ(0, cache_->GetEntryCount()); 2603 EXPECT_EQ(0, cache_->GetEntryCount());
2608 } 2604 }
2609 2605
(...skipping 25 matching lines...) Expand all
2635 DisableFirstCleanup(); 2631 DisableFirstCleanup();
2636 SetNewEviction(); 2632 SetNewEviction();
2637 InitCache(); 2633 InitCache();
2638 SetTestMode(); // Fail cache reinitialization. 2634 SetTestMode(); // Fail cache reinitialization.
2639 BackendDisable2(); 2635 BackendDisable2();
2640 } 2636 }
2641 2637
2642 // 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.
2643 void DiskCacheBackendTest::BackendDisable3() { 2639 void DiskCacheBackendTest::BackendDisable3() {
2644 disk_cache::Entry *entry1, *entry2; 2640 disk_cache::Entry *entry1, *entry2;
2645 void* iter = NULL; 2641 scoped_ptr<TestIterator> iter = CreateIterator();
2646 EXPECT_EQ(2, cache_->GetEntryCount()); 2642 EXPECT_EQ(2, cache_->GetEntryCount());
2647 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry1)); 2643 ASSERT_EQ(net::OK, iter->OpenNextEntry(&entry1));
2648 entry1->Close(); 2644 entry1->Close();
2649 2645
2650 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry2)); 2646 EXPECT_NE(net::OK, iter->OpenNextEntry(&entry2));
2651 FlushQueueForTest(); 2647 FlushQueueForTest();
2652 2648
2653 ASSERT_EQ(net::OK, CreateEntry("Something new", &entry2)); 2649 ASSERT_EQ(net::OK, CreateEntry("Something new", &entry2));
2654 entry2->Close(); 2650 entry2->Close();
2655 2651
2656 EXPECT_EQ(1, cache_->GetEntryCount()); 2652 EXPECT_EQ(1, cache_->GetEntryCount());
2657 } 2653 }
2658 2654
2659 TEST_F(DiskCacheBackendTest, DisableSuccess3) { 2655 TEST_F(DiskCacheBackendTest, DisableSuccess3) {
2660 ASSERT_TRUE(CopyTestCache("bad_rankings2")); 2656 ASSERT_TRUE(CopyTestCache("bad_rankings2"));
2661 DisableFirstCleanup(); 2657 DisableFirstCleanup();
2662 SetMaxSize(20 * 1024 * 1024); 2658 SetMaxSize(20 * 1024 * 1024);
2663 InitCache(); 2659 InitCache();
2664 BackendDisable3(); 2660 BackendDisable3();
2665 } 2661 }
2666 2662
2667 TEST_F(DiskCacheBackendTest, NewEvictionDisableSuccess3) { 2663 TEST_F(DiskCacheBackendTest, NewEvictionDisableSuccess3) {
2668 ASSERT_TRUE(CopyTestCache("bad_rankings2")); 2664 ASSERT_TRUE(CopyTestCache("bad_rankings2"));
2669 DisableFirstCleanup(); 2665 DisableFirstCleanup();
2670 SetMaxSize(20 * 1024 * 1024); 2666 SetMaxSize(20 * 1024 * 1024);
2671 SetNewEviction(); 2667 SetNewEviction();
2672 InitCache(); 2668 InitCache();
2673 BackendDisable3(); 2669 BackendDisable3();
2674 } 2670 }
2675 2671
2676 // 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.
2677 void DiskCacheBackendTest::BackendDisable4() { 2673 void DiskCacheBackendTest::BackendDisable4() {
2678 disk_cache::Entry *entry1, *entry2, *entry3, *entry4; 2674 disk_cache::Entry *entry1, *entry2, *entry3, *entry4;
2679 void* iter = NULL; 2675 scoped_ptr<TestIterator> iter = CreateIterator();
2680 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry1)); 2676 ASSERT_EQ(net::OK, iter->OpenNextEntry(&entry1));
2681 2677
2682 char key2[2000]; 2678 char key2[2000];
2683 char key3[20000]; 2679 char key3[20000];
2684 CacheTestFillBuffer(key2, sizeof(key2), true); 2680 CacheTestFillBuffer(key2, sizeof(key2), true);
2685 CacheTestFillBuffer(key3, sizeof(key3), true); 2681 CacheTestFillBuffer(key3, sizeof(key3), true);
2686 key2[sizeof(key2) - 1] = '\0'; 2682 key2[sizeof(key2) - 1] = '\0';
2687 key3[sizeof(key3) - 1] = '\0'; 2683 key3[sizeof(key3) - 1] = '\0';
2688 ASSERT_EQ(net::OK, CreateEntry(key2, &entry2)); 2684 ASSERT_EQ(net::OK, CreateEntry(key2, &entry2));
2689 ASSERT_EQ(net::OK, CreateEntry(key3, &entry3)); 2685 ASSERT_EQ(net::OK, CreateEntry(key3, &entry3));
2690 2686
2691 const int kBufSize = 20000; 2687 const int kBufSize = 20000;
2692 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kBufSize)); 2688 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kBufSize));
2693 memset(buf->data(), 0, kBufSize); 2689 memset(buf->data(), 0, kBufSize);
2694 EXPECT_EQ(100, WriteData(entry2, 0, 0, buf.get(), 100, false)); 2690 EXPECT_EQ(100, WriteData(entry2, 0, 0, buf.get(), 100, false));
2695 EXPECT_EQ(kBufSize, WriteData(entry3, 0, 0, buf.get(), kBufSize, false)); 2691 EXPECT_EQ(kBufSize, WriteData(entry3, 0, 0, buf.get(), kBufSize, false));
2696 2692
2697 // This line should disable the cache but not delete it. 2693 // This line should disable the cache but not delete it.
2698 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry4)); 2694 EXPECT_NE(net::OK, iter->OpenNextEntry(&entry4));
2699 EXPECT_EQ(0, cache_->GetEntryCount()); 2695 EXPECT_EQ(0, cache_->GetEntryCount());
2700 2696
2701 EXPECT_NE(net::OK, CreateEntry("cache is disabled", &entry4)); 2697 EXPECT_NE(net::OK, CreateEntry("cache is disabled", &entry4));
2702 2698
2703 EXPECT_EQ(100, ReadData(entry2, 0, 0, buf.get(), 100)); 2699 EXPECT_EQ(100, ReadData(entry2, 0, 0, buf.get(), 100));
2704 EXPECT_EQ(100, WriteData(entry2, 0, 0, buf.get(), 100, false)); 2700 EXPECT_EQ(100, WriteData(entry2, 0, 0, buf.get(), 100, false));
2705 EXPECT_EQ(100, WriteData(entry2, 1, 0, buf.get(), 100, false)); 2701 EXPECT_EQ(100, WriteData(entry2, 1, 0, buf.get(), 100, false));
2706 2702
2707 EXPECT_EQ(kBufSize, ReadData(entry3, 0, 0, buf.get(), kBufSize)); 2703 EXPECT_EQ(kBufSize, ReadData(entry3, 0, 0, buf.get(), kBufSize));
2708 EXPECT_EQ(kBufSize, WriteData(entry3, 0, 0, buf.get(), kBufSize, false)); 2704 EXPECT_EQ(kBufSize, WriteData(entry3, 0, 0, buf.get(), kBufSize, false));
(...skipping 660 matching lines...) Expand 10 before | Expand all | Expand 10 after
3369 // Tests basic functionality of the SimpleBackend implementation of the 3365 // Tests basic functionality of the SimpleBackend implementation of the
3370 // enumeration API. 3366 // enumeration API.
3371 TEST_F(DiskCacheBackendTest, SimpleCacheEnumerationBasics) { 3367 TEST_F(DiskCacheBackendTest, SimpleCacheEnumerationBasics) {
3372 SetSimpleCacheMode(); 3368 SetSimpleCacheMode();
3373 InitCache(); 3369 InitCache();
3374 std::set<std::string> key_pool; 3370 std::set<std::string> key_pool;
3375 ASSERT_TRUE(CreateSetOfRandomEntries(&key_pool)); 3371 ASSERT_TRUE(CreateSetOfRandomEntries(&key_pool));
3376 3372
3377 // Check that enumeration returns all entries. 3373 // Check that enumeration returns all entries.
3378 std::set<std::string> keys_to_match(key_pool); 3374 std::set<std::string> keys_to_match(key_pool);
3379 void* iter = NULL; 3375 scoped_ptr<TestIterator> iter = CreateIterator();
3380 size_t count = 0; 3376 size_t count = 0;
3381 ASSERT_TRUE(EnumerateAndMatchKeys(-1, &iter, &keys_to_match, &count)); 3377 ASSERT_TRUE(EnumerateAndMatchKeys(-1, iter, &keys_to_match, &count));
3382 cache_->EndEnumeration(&iter); 3378 iter.reset();
3383 EXPECT_EQ(key_pool.size(), count); 3379 EXPECT_EQ(key_pool.size(), count);
3384 EXPECT_TRUE(keys_to_match.empty()); 3380 EXPECT_TRUE(keys_to_match.empty());
3385 3381
3386 // Check that opening entries does not affect enumeration. 3382 // Check that opening entries does not affect enumeration.
3387 keys_to_match = key_pool; 3383 keys_to_match = key_pool;
3388 iter = NULL; 3384 iter = CreateIterator();
3389 count = 0; 3385 count = 0;
3390 disk_cache::Entry* entry_opened_before; 3386 disk_cache::Entry* entry_opened_before;
3391 ASSERT_EQ(net::OK, OpenEntry(*(key_pool.begin()), &entry_opened_before)); 3387 ASSERT_EQ(net::OK, OpenEntry(*(key_pool.begin()), &entry_opened_before));
3392 ASSERT_TRUE(EnumerateAndMatchKeys(key_pool.size()/2, 3388 ASSERT_TRUE(EnumerateAndMatchKeys(key_pool.size()/2,
3393 &iter, 3389 iter,
3394 &keys_to_match, 3390 &keys_to_match,
3395 &count)); 3391 &count));
3396 3392
3397 disk_cache::Entry* entry_opened_middle; 3393 disk_cache::Entry* entry_opened_middle;
3398 ASSERT_EQ(net::OK, 3394 ASSERT_EQ(net::OK,
3399 OpenEntry(*(keys_to_match.begin()), &entry_opened_middle)); 3395 OpenEntry(*(keys_to_match.begin()), &entry_opened_middle));
3400 ASSERT_TRUE(EnumerateAndMatchKeys(-1, &iter, &keys_to_match, &count)); 3396 ASSERT_TRUE(EnumerateAndMatchKeys(-1, iter, &keys_to_match, &count));
3401 cache_->EndEnumeration(&iter); 3397 iter.reset();
3402 entry_opened_before->Close(); 3398 entry_opened_before->Close();
3403 entry_opened_middle->Close(); 3399 entry_opened_middle->Close();
3404 3400
3405 EXPECT_EQ(key_pool.size(), count); 3401 EXPECT_EQ(key_pool.size(), count);
3406 EXPECT_TRUE(keys_to_match.empty()); 3402 EXPECT_TRUE(keys_to_match.empty());
3407 } 3403 }
3408 3404
3409 // Tests that the enumerations are not affected by dooming an entry in the 3405 // Tests that the enumerations are not affected by dooming an entry in the
3410 // middle. 3406 // middle.
3411 TEST_F(DiskCacheBackendTest, SimpleCacheEnumerationWhileDoomed) { 3407 TEST_F(DiskCacheBackendTest, SimpleCacheEnumerationWhileDoomed) {
3412 SetSimpleCacheMode(); 3408 SetSimpleCacheMode();
3413 InitCache(); 3409 InitCache();
3414 std::set<std::string> key_pool; 3410 std::set<std::string> key_pool;
3415 ASSERT_TRUE(CreateSetOfRandomEntries(&key_pool)); 3411 ASSERT_TRUE(CreateSetOfRandomEntries(&key_pool));
3416 3412
3417 // Check that enumeration returns all entries but the doomed one. 3413 // Check that enumeration returns all entries but the doomed one.
3418 std::set<std::string> keys_to_match(key_pool); 3414 std::set<std::string> keys_to_match(key_pool);
3419 void* iter = NULL; 3415 scoped_ptr<TestIterator> iter = CreateIterator();
3420 size_t count = 0; 3416 size_t count = 0;
3421 ASSERT_TRUE(EnumerateAndMatchKeys(key_pool.size()/2, 3417 ASSERT_TRUE(EnumerateAndMatchKeys(key_pool.size()/2,
3422 &iter, 3418 iter,
3423 &keys_to_match, 3419 &keys_to_match,
3424 &count)); 3420 &count));
3425 3421
3426 std::string key_to_delete = *(keys_to_match.begin()); 3422 std::string key_to_delete = *(keys_to_match.begin());
3427 DoomEntry(key_to_delete); 3423 DoomEntry(key_to_delete);
3428 keys_to_match.erase(key_to_delete); 3424 keys_to_match.erase(key_to_delete);
3429 key_pool.erase(key_to_delete); 3425 key_pool.erase(key_to_delete);
3430 ASSERT_TRUE(EnumerateAndMatchKeys(-1, &iter, &keys_to_match, &count)); 3426 ASSERT_TRUE(EnumerateAndMatchKeys(-1, iter, &keys_to_match, &count));
3431 cache_->EndEnumeration(&iter); 3427 iter.reset();
3432 3428
3433 EXPECT_EQ(key_pool.size(), count); 3429 EXPECT_EQ(key_pool.size(), count);
3434 EXPECT_TRUE(keys_to_match.empty()); 3430 EXPECT_TRUE(keys_to_match.empty());
3435 } 3431 }
3436 3432
3437 // Tests that enumerations are not affected by corrupt files. 3433 // Tests that enumerations are not affected by corrupt files.
3438 TEST_F(DiskCacheBackendTest, SimpleCacheEnumerationCorruption) { 3434 TEST_F(DiskCacheBackendTest, SimpleCacheEnumerationCorruption) {
3439 SetSimpleCacheMode(); 3435 SetSimpleCacheMode();
3440 InitCache(); 3436 InitCache();
3441 std::set<std::string> key_pool; 3437 std::set<std::string> key_pool;
(...skipping 15 matching lines...) Expand all
3457 ASSERT_EQ(kSize, ReadData(corrupted_entry, 0, 0, buffer.get(), kSize)); 3453 ASSERT_EQ(kSize, ReadData(corrupted_entry, 0, 0, buffer.get(), kSize));
3458 corrupted_entry->Close(); 3454 corrupted_entry->Close();
3459 3455
3460 EXPECT_TRUE(disk_cache::simple_util::CreateCorruptFileForTests( 3456 EXPECT_TRUE(disk_cache::simple_util::CreateCorruptFileForTests(
3461 key, cache_path_)); 3457 key, cache_path_));
3462 EXPECT_EQ(key_pool.size() + 1, 3458 EXPECT_EQ(key_pool.size() + 1,
3463 implicit_cast<size_t>(cache_->GetEntryCount())); 3459 implicit_cast<size_t>(cache_->GetEntryCount()));
3464 3460
3465 // Check that enumeration returns all entries but the corrupt one. 3461 // Check that enumeration returns all entries but the corrupt one.
3466 std::set<std::string> keys_to_match(key_pool); 3462 std::set<std::string> keys_to_match(key_pool);
3467 void* iter = NULL; 3463 scoped_ptr<TestIterator> iter = CreateIterator();
3468 size_t count = 0; 3464 size_t count = 0;
3469 ASSERT_TRUE(EnumerateAndMatchKeys(-1, &iter, &keys_to_match, &count)); 3465 ASSERT_TRUE(EnumerateAndMatchKeys(-1, iter, &keys_to_match, &count));
3470 cache_->EndEnumeration(&iter); 3466 iter.reset();
3471 3467
3472 EXPECT_EQ(key_pool.size(), count); 3468 EXPECT_EQ(key_pool.size(), count);
3473 EXPECT_TRUE(keys_to_match.empty()); 3469 EXPECT_TRUE(keys_to_match.empty());
3474 } 3470 }
3475 3471
3476 // Tests that enumerations don't leak memory when the backend is destructed 3472 // Tests that enumerations don't leak memory when the backend is destructed
3477 // mid-enumeration. 3473 // mid-enumeration.
3478 TEST_F(DiskCacheBackendTest, SimpleCacheEnumerationDestruction) { 3474 TEST_F(DiskCacheBackendTest, SimpleCacheEnumerationDestruction) {
3479 SetSimpleCacheMode(); 3475 SetSimpleCacheMode();
3480 InitCache(); 3476 InitCache();
3481 std::set<std::string> key_pool; 3477 std::set<std::string> key_pool;
3482 ASSERT_TRUE(CreateSetOfRandomEntries(&key_pool)); 3478 ASSERT_TRUE(CreateSetOfRandomEntries(&key_pool));
3483 3479
3484 void* iter = NULL; 3480 scoped_ptr<TestIterator> iter = CreateIterator();
3485 disk_cache::Entry* entry = NULL; 3481 disk_cache::Entry* entry = NULL;
3486 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry)); 3482 ASSERT_EQ(net::OK, iter->OpenNextEntry(&entry));
3487 EXPECT_TRUE(entry); 3483 EXPECT_TRUE(entry);
3488 disk_cache::ScopedEntryPtr entry_closer(entry); 3484 disk_cache::ScopedEntryPtr entry_closer(entry);
3489 3485
3490 cache_.reset(); 3486 cache_.reset();
3491 // This test passes if we don't leak memory. 3487 // This test passes if we don't leak memory.
3492 } 3488 }
3493 3489
3494 #endif // defined(OS_POSIX) 3490 #endif // defined(OS_POSIX)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698