Index: net/disk_cache/backend_unittest.cc |
diff --git a/net/disk_cache/backend_unittest.cc b/net/disk_cache/backend_unittest.cc |
index 4f4a442aa1ee95e2be79a8a6a78b4628e03e996b..e6c84c1c21f58d89cdf502342db79522f5522d3a 100644 |
--- a/net/disk_cache/backend_unittest.cc |
+++ b/net/disk_cache/backend_unittest.cc |
@@ -81,7 +81,7 @@ class DiskCacheBackendTest : public DiskCacheTestWithCache { |
bool CreateSetOfRandomEntries(std::set<std::string>* key_pool); |
bool EnumerateAndMatchKeys(int max_to_open, |
- void** iter, |
+ disk_cache::Backend::Iterator* iter, |
std::set<std::string>* keys_to_match, |
size_t* count); |
@@ -256,7 +256,7 @@ bool DiskCacheBackendTest::CreateSetOfRandomEntries( |
// OpenNextEntry stops returning net::OK. |
bool DiskCacheBackendTest::EnumerateAndMatchKeys( |
int max_to_open, |
- void** iter, |
+ disk_cache::Backend::Iterator* iter, |
std::set<std::string>* keys_to_match, |
size_t* count) { |
disk_cache::Entry* entry; |
@@ -1237,7 +1237,7 @@ void DiskCacheBackendTest::BackendEnumerations() { |
Time final = Time::Now(); |
disk_cache::Entry* entry; |
- void* iter = NULL; |
+ disk_cache::Backend::Iterator iter; |
int count = 0; |
Time last_modified[kNumEntries]; |
Time last_used[kNumEntries]; |
@@ -1255,7 +1255,7 @@ void DiskCacheBackendTest::BackendEnumerations() { |
}; |
EXPECT_EQ(kNumEntries, count); |
- iter = NULL; |
+ iter.reset(); |
count = 0; |
// The previous enumeration should not have changed the timestamps. |
while (OpenNextEntry(&iter, &entry) == net::OK) { |
@@ -1309,7 +1309,7 @@ void DiskCacheBackendTest::BackendEnumerations2() { |
// Make sure that the timestamp is not the same. |
AddDelay(); |
ASSERT_EQ(net::OK, OpenEntry(second, &entry1)); |
- void* iter = NULL; |
+ disk_cache::Backend::Iterator iter; |
ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry2)); |
EXPECT_EQ(entry2->GetKey(), second); |
@@ -1321,7 +1321,7 @@ void DiskCacheBackendTest::BackendEnumerations2() { |
ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry2)); |
EXPECT_EQ(entry2->GetKey(), first); |
entry2->Close(); |
- cache_->EndEnumeration(&iter); |
+ iter.reset(); |
// Modify the oldest entry and get the newest element. |
ASSERT_EQ(net::OK, OpenEntry(first, &entry1)); |
@@ -1336,7 +1336,6 @@ void DiskCacheBackendTest::BackendEnumerations2() { |
entry1->Close(); |
entry2->Close(); |
- cache_->EndEnumeration(&iter); |
} |
TEST_F(DiskCacheBackendTest, Enumerations2) { |
@@ -1391,11 +1390,10 @@ TEST_F(DiskCacheBackendTest, ShaderCacheEnumerationReadData) { |
EXPECT_EQ(kSize, ReadData(entry1, 0, 0, buffer1.get(), kSize)); |
entry1->Close(); |
- void* iter = NULL; |
+ disk_cache::Backend::Iterator iter; |
ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry2)); |
EXPECT_EQ(entry2->GetKey(), second); |
entry2->Close(); |
- cache_->EndEnumeration(&iter); |
} |
#if !defined(LEAK_SANITIZER) |
@@ -1424,7 +1422,7 @@ void DiskCacheBackendTest::BackendInvalidEntryEnumeration() { |
SimulateCrash(); |
- void* iter = NULL; |
+ disk_cache::Backend::Iterator iter; |
int count = 0; |
while (OpenNextEntry(&iter, &entry) == net::OK) { |
ASSERT_TRUE(NULL != entry); |
@@ -1466,8 +1464,7 @@ void DiskCacheBackendTest::BackendFixEnumerators() { |
EXPECT_EQ(kNumEntries, cache_->GetEntryCount()); |
disk_cache::Entry *entry1, *entry2; |
- void* iter1 = NULL; |
- void* iter2 = NULL; |
+ disk_cache::Backend::Iterator iter1, iter2; |
ASSERT_EQ(net::OK, OpenNextEntry(&iter1, &entry1)); |
ASSERT_TRUE(NULL != entry1); |
entry1->Close(); |
@@ -1499,9 +1496,6 @@ void DiskCacheBackendTest::BackendFixEnumerators() { |
ASSERT_EQ(net::OK, OpenNextEntry(&iter2, &entry2)); |
ASSERT_TRUE(NULL != entry2); |
entry2->Close(); |
- |
- cache_->EndEnumeration(&iter1); |
- cache_->EndEnumeration(&iter2); |
} |
TEST_F(DiskCacheBackendTest, FixEnumerators) { |
@@ -2027,7 +2021,7 @@ void DiskCacheBackendTest::BackendInvalidEntry3() { |
InitCache(); |
disk_cache::Entry* entry; |
- void* iter = NULL; |
+ disk_cache::Backend::Iterator iter; |
while (OpenNextEntry(&iter, &entry) == net::OK) { |
entry->Close(); |
} |
@@ -2172,7 +2166,7 @@ void DiskCacheBackendTest::BackendInvalidEntry7() { |
EXPECT_EQ(1, cache_->GetEntryCount()); |
// We should delete the cache. The list still has a corrupt node. |
- void* iter = NULL; |
+ disk_cache::Backend::Iterator iter; |
EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry)); |
FlushQueueForTest(); |
EXPECT_EQ(0, cache_->GetEntryCount()); |
@@ -2216,7 +2210,7 @@ void DiskCacheBackendTest::BackendInvalidEntry8() { |
EXPECT_EQ(1, cache_->GetEntryCount()); |
// We should not delete the cache. |
- void* iter = NULL; |
+ disk_cache::Backend::Iterator iter; |
ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry)); |
entry->Close(); |
EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry)); |
@@ -2266,7 +2260,7 @@ void DiskCacheBackendTest::BackendInvalidEntry9(bool eviction) { |
} else { |
// We should detect the problem through the list, but we should not delete |
// the entry, just fail the iteration. |
- void* iter = NULL; |
+ disk_cache::Backend::Iterator iter; |
EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry)); |
// Now a full iteration will work, and return one entry. |
@@ -2343,7 +2337,7 @@ void DiskCacheBackendTest::BackendInvalidEntry10(bool eviction) { |
// Detection order: third -> second -> first. |
// We should detect the problem through the list, but we should not delete |
// the entry. |
- void* iter = NULL; |
+ disk_cache::Backend::Iterator iter; |
ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry)); |
entry->Close(); |
ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry)); |
@@ -2410,7 +2404,7 @@ void DiskCacheBackendTest::BackendInvalidEntry11(bool eviction) { |
// Detection order: third -> second. |
// We should detect the problem through the list, but we should not delete |
// the entry, just fail the iteration. |
- void* iter = NULL; |
+ disk_cache::Backend::Iterator iter; |
ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry)); |
entry->Close(); |
EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry)); |
@@ -2498,7 +2492,7 @@ TEST_F(DiskCacheBackendTest, NewEvictionInvalidRankings2) { |
// If the LRU is corrupt, we delete the cache. |
void DiskCacheBackendTest::BackendInvalidRankings() { |
disk_cache::Entry* entry; |
- void* iter = NULL; |
+ disk_cache::Backend::Iterator iter; |
ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry)); |
entry->Close(); |
EXPECT_EQ(2, cache_->GetEntryCount()); |
@@ -2543,7 +2537,7 @@ TEST_F(DiskCacheBackendTest, NewEvictionInvalidRankingsFailure) { |
// If the LRU is corrupt and we have open entries, we disable the cache. |
void DiskCacheBackendTest::BackendDisable() { |
disk_cache::Entry *entry1, *entry2; |
- void* iter = NULL; |
+ disk_cache::Backend::Iterator iter; |
ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry1)); |
EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry2)); |
@@ -2594,7 +2588,7 @@ void DiskCacheBackendTest::BackendDisable2() { |
EXPECT_EQ(8, cache_->GetEntryCount()); |
disk_cache::Entry* entry; |
- void* iter = NULL; |
+ disk_cache::Backend::Iterator iter; |
int count = 0; |
while (OpenNextEntry(&iter, &entry) == net::OK) { |
ASSERT_TRUE(NULL != entry); |
@@ -2642,7 +2636,7 @@ TEST_F(DiskCacheBackendTest, NewEvictionDisableFailure2) { |
// If the index size changes when we disable the cache, we should not crash. |
void DiskCacheBackendTest::BackendDisable3() { |
disk_cache::Entry *entry1, *entry2; |
- void* iter = NULL; |
+ disk_cache::Backend::Iterator iter; |
EXPECT_EQ(2, cache_->GetEntryCount()); |
ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry1)); |
entry1->Close(); |
@@ -2676,7 +2670,7 @@ TEST_F(DiskCacheBackendTest, NewEvictionDisableSuccess3) { |
// If we disable the cache, already open entries should work as far as possible. |
void DiskCacheBackendTest::BackendDisable4() { |
disk_cache::Entry *entry1, *entry2, *entry3, *entry4; |
- void* iter = NULL; |
+ disk_cache::Backend::Iterator iter; |
ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry1)); |
char key2[2000]; |
@@ -3376,16 +3370,16 @@ TEST_F(DiskCacheBackendTest, SimpleCacheEnumerationBasics) { |
// Check that enumeration returns all entries. |
std::set<std::string> keys_to_match(key_pool); |
- void* iter = NULL; |
+ disk_cache::Backend::Iterator iter; |
size_t count = 0; |
ASSERT_TRUE(EnumerateAndMatchKeys(-1, &iter, &keys_to_match, &count)); |
- cache_->EndEnumeration(&iter); |
+ iter.reset(); |
EXPECT_EQ(key_pool.size(), count); |
EXPECT_TRUE(keys_to_match.empty()); |
// Check that opening entries does not affect enumeration. |
keys_to_match = key_pool; |
- iter = NULL; |
+ iter.reset(); |
count = 0; |
disk_cache::Entry* entry_opened_before; |
ASSERT_EQ(net::OK, OpenEntry(*(key_pool.begin()), &entry_opened_before)); |
@@ -3398,7 +3392,7 @@ TEST_F(DiskCacheBackendTest, SimpleCacheEnumerationBasics) { |
ASSERT_EQ(net::OK, |
OpenEntry(*(keys_to_match.begin()), &entry_opened_middle)); |
ASSERT_TRUE(EnumerateAndMatchKeys(-1, &iter, &keys_to_match, &count)); |
- cache_->EndEnumeration(&iter); |
+ iter.reset(); |
entry_opened_before->Close(); |
entry_opened_middle->Close(); |
@@ -3416,7 +3410,7 @@ TEST_F(DiskCacheBackendTest, SimpleCacheEnumerationWhileDoomed) { |
// Check that enumeration returns all entries but the doomed one. |
std::set<std::string> keys_to_match(key_pool); |
- void* iter = NULL; |
+ disk_cache::Backend::Iterator iter; |
size_t count = 0; |
ASSERT_TRUE(EnumerateAndMatchKeys(key_pool.size()/2, |
&iter, |
@@ -3428,7 +3422,7 @@ TEST_F(DiskCacheBackendTest, SimpleCacheEnumerationWhileDoomed) { |
keys_to_match.erase(key_to_delete); |
key_pool.erase(key_to_delete); |
ASSERT_TRUE(EnumerateAndMatchKeys(-1, &iter, &keys_to_match, &count)); |
- cache_->EndEnumeration(&iter); |
+ iter.reset(); |
EXPECT_EQ(key_pool.size(), count); |
EXPECT_TRUE(keys_to_match.empty()); |
@@ -3464,13 +3458,31 @@ TEST_F(DiskCacheBackendTest, SimpleCacheEnumerationCorruption) { |
// Check that enumeration returns all entries but the corrupt one. |
std::set<std::string> keys_to_match(key_pool); |
- void* iter = NULL; |
+ disk_cache::Backend::Iterator iter; |
size_t count = 0; |
ASSERT_TRUE(EnumerateAndMatchKeys(-1, &iter, &keys_to_match, &count)); |
- cache_->EndEnumeration(&iter); |
+ iter.reset(); |
EXPECT_EQ(key_pool.size(), count); |
EXPECT_TRUE(keys_to_match.empty()); |
} |
+// Tests that enumerations don't leak memory when the backend is destructed |
+// mid-enumeration. |
+TEST_F(DiskCacheBackendTest, SimpleCacheEnumerationDestruction) { |
+ SetSimpleCacheMode(); |
+ InitCache(); |
+ std::set<std::string> key_pool; |
+ ASSERT_TRUE(CreateSetOfRandomEntries(&key_pool)); |
+ |
+ disk_cache::Backend::Iterator iter; |
+ disk_cache::Entry* entry = NULL; |
+ ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry)); |
+ EXPECT_TRUE(entry); |
+ disk_cache::ScopedEntryPtr entry_closer(entry); |
+ |
+ cache_.reset(); |
+ // This test passes if we don't leak memory. |
+} |
+ |
#endif // defined(OS_POSIX) |