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 <stdint.h> | 5 #include <stdint.h> |
6 | 6 |
7 #include "base/files/file.h" | 7 #include "base/files/file.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 #include "net/disk_cache/blockfile/experiments.h" | 26 #include "net/disk_cache/blockfile/experiments.h" |
27 #include "net/disk_cache/blockfile/histogram_macros.h" | 27 #include "net/disk_cache/blockfile/histogram_macros.h" |
28 #include "net/disk_cache/blockfile/mapped_file.h" | 28 #include "net/disk_cache/blockfile/mapped_file.h" |
29 #include "net/disk_cache/cache_util.h" | 29 #include "net/disk_cache/cache_util.h" |
30 #include "net/disk_cache/disk_cache_test_base.h" | 30 #include "net/disk_cache/disk_cache_test_base.h" |
31 #include "net/disk_cache/disk_cache_test_util.h" | 31 #include "net/disk_cache/disk_cache_test_util.h" |
32 #include "net/disk_cache/memory/mem_backend_impl.h" | 32 #include "net/disk_cache/memory/mem_backend_impl.h" |
33 #include "net/disk_cache/simple/simple_backend_impl.h" | 33 #include "net/disk_cache/simple/simple_backend_impl.h" |
34 #include "net/disk_cache/simple/simple_entry_format.h" | 34 #include "net/disk_cache/simple/simple_entry_format.h" |
35 #include "net/disk_cache/simple/simple_index.h" | 35 #include "net/disk_cache/simple/simple_index.h" |
| 36 #include "net/disk_cache/simple/simple_synchronous_entry.h" |
36 #include "net/disk_cache/simple/simple_test_util.h" | 37 #include "net/disk_cache/simple/simple_test_util.h" |
37 #include "net/disk_cache/simple/simple_util.h" | 38 #include "net/disk_cache/simple/simple_util.h" |
38 #include "testing/gtest/include/gtest/gtest.h" | 39 #include "testing/gtest/include/gtest/gtest.h" |
39 | 40 |
40 #if defined(OS_WIN) | 41 #if defined(OS_WIN) |
41 #include "base/win/scoped_handle.h" | 42 #include "base/win/scoped_handle.h" |
42 #endif | 43 #endif |
43 | 44 |
44 // Provide a BackendImpl object to macros from histogram_macros.h. | 45 // Provide a BackendImpl object to macros from histogram_macros.h. |
45 #define CACHE_UMA_BACKEND_IMPL_OBJ backend_ | 46 #define CACHE_UMA_BACKEND_IMPL_OBJ backend_ |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 entry4->Close(); | 247 entry4->Close(); |
247 | 248 |
248 FlushQueueForTest(); | 249 FlushQueueForTest(); |
249 AddDelay(); | 250 AddDelay(); |
250 } | 251 } |
251 | 252 |
252 // Creates entries based on random keys. Stores these keys in |key_pool|. | 253 // Creates entries based on random keys. Stores these keys in |key_pool|. |
253 bool DiskCacheBackendTest::CreateSetOfRandomEntries( | 254 bool DiskCacheBackendTest::CreateSetOfRandomEntries( |
254 std::set<std::string>* key_pool) { | 255 std::set<std::string>* key_pool) { |
255 const int kNumEntries = 10; | 256 const int kNumEntries = 10; |
| 257 const int initial_entry_count = cache_->GetEntryCount(); |
256 | 258 |
257 for (int i = 0; i < kNumEntries; ++i) { | 259 for (int i = 0; i < kNumEntries; ++i) { |
258 std::string key = GenerateKey(true); | 260 std::string key = GenerateKey(true); |
259 disk_cache::Entry* entry; | 261 disk_cache::Entry* entry; |
260 if (CreateEntry(key, &entry) != net::OK) | 262 if (CreateEntry(key, &entry) != net::OK) { |
261 return false; | 263 return false; |
| 264 } |
262 key_pool->insert(key); | 265 key_pool->insert(key); |
263 entry->Close(); | 266 entry->Close(); |
264 } | 267 } |
265 return key_pool->size() == static_cast<size_t>(cache_->GetEntryCount()); | 268 return key_pool->size() == |
| 269 static_cast<size_t>(cache_->GetEntryCount() - initial_entry_count); |
266 } | 270 } |
267 | 271 |
268 // Performs iteration over the backend and checks that the keys of entries | 272 // Performs iteration over the backend and checks that the keys of entries |
269 // opened are in |keys_to_match|, then erases them. Up to |max_to_open| entries | 273 // opened are in |keys_to_match|, then erases them. Up to |max_to_open| entries |
270 // will be opened, if it is positive. Otherwise, iteration will continue until | 274 // will be opened, if it is positive. Otherwise, iteration will continue until |
271 // OpenNextEntry stops returning net::OK. | 275 // OpenNextEntry stops returning net::OK. |
272 bool DiskCacheBackendTest::EnumerateAndMatchKeys( | 276 bool DiskCacheBackendTest::EnumerateAndMatchKeys( |
273 int max_to_open, | 277 int max_to_open, |
274 TestIterator* iter, | 278 TestIterator* iter, |
275 std::set<std::string>* keys_to_match, | 279 std::set<std::string>* keys_to_match, |
(...skipping 3405 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3681 iter.reset(); | 3685 iter.reset(); |
3682 | 3686 |
3683 EXPECT_EQ(key_pool.size(), count); | 3687 EXPECT_EQ(key_pool.size(), count); |
3684 EXPECT_TRUE(keys_to_match.empty()); | 3688 EXPECT_TRUE(keys_to_match.empty()); |
3685 } | 3689 } |
3686 | 3690 |
3687 // Tests that enumerations are not affected by corrupt files. | 3691 // Tests that enumerations are not affected by corrupt files. |
3688 TEST_F(DiskCacheBackendTest, SimpleCacheEnumerationCorruption) { | 3692 TEST_F(DiskCacheBackendTest, SimpleCacheEnumerationCorruption) { |
3689 SetSimpleCacheMode(); | 3693 SetSimpleCacheMode(); |
3690 InitCache(); | 3694 InitCache(); |
3691 std::set<std::string> key_pool; | |
3692 ASSERT_TRUE(CreateSetOfRandomEntries(&key_pool)); | |
3693 | |
3694 // Create a corrupt entry. The write/read sequence ensures that the entry will | 3695 // Create a corrupt entry. The write/read sequence ensures that the entry will |
3695 // have been created before corrupting the platform files, in the case of | 3696 // have been created before corrupting the platform files, in the case of |
3696 // optimistic operations. | 3697 // optimistic operations. |
3697 const std::string key = "the key"; | 3698 const std::string key = "the key"; |
3698 disk_cache::Entry* corrupted_entry; | 3699 disk_cache::Entry* corrupted_entry; |
3699 | 3700 |
3700 ASSERT_EQ(net::OK, CreateEntry(key, &corrupted_entry)); | 3701 ASSERT_EQ(net::OK, CreateEntry(key, &corrupted_entry)); |
3701 ASSERT_TRUE(corrupted_entry); | 3702 ASSERT_TRUE(corrupted_entry); |
3702 const int kSize = 50; | 3703 const int kSize = 50; |
3703 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); | 3704 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); |
3704 CacheTestFillBuffer(buffer->data(), kSize, false); | 3705 CacheTestFillBuffer(buffer->data(), kSize, false); |
3705 ASSERT_EQ(kSize, | 3706 ASSERT_EQ(kSize, |
3706 WriteData(corrupted_entry, 0, 0, buffer.get(), kSize, false)); | 3707 WriteData(corrupted_entry, 0, 0, buffer.get(), kSize, false)); |
3707 ASSERT_EQ(kSize, ReadData(corrupted_entry, 0, 0, buffer.get(), kSize)); | 3708 ASSERT_EQ(kSize, ReadData(corrupted_entry, 0, 0, buffer.get(), kSize)); |
3708 corrupted_entry->Close(); | 3709 corrupted_entry->Close(); |
3709 | 3710 |
| 3711 std::set<std::string> key_pool; |
| 3712 ASSERT_TRUE(CreateSetOfRandomEntries(&key_pool)); |
| 3713 |
3710 EXPECT_TRUE(disk_cache::simple_util::CreateCorruptFileForTests( | 3714 EXPECT_TRUE(disk_cache::simple_util::CreateCorruptFileForTests( |
3711 key, cache_path_)); | 3715 key, cache_path_)); |
3712 EXPECT_EQ(key_pool.size() + 1, static_cast<size_t>(cache_->GetEntryCount())); | 3716 EXPECT_EQ(key_pool.size() + 1, static_cast<size_t>(cache_->GetEntryCount())); |
3713 | 3717 |
3714 // Check that enumeration returns all entries but the corrupt one. | 3718 // Check that enumeration returns all entries but the corrupt one. |
3715 std::set<std::string> keys_to_match(key_pool); | 3719 std::set<std::string> keys_to_match(key_pool); |
3716 std::unique_ptr<TestIterator> iter = CreateIterator(); | 3720 std::unique_ptr<TestIterator> iter = CreateIterator(); |
3717 size_t count = 0; | 3721 size_t count = 0; |
3718 ASSERT_TRUE(EnumerateAndMatchKeys(-1, iter.get(), &keys_to_match, &count)); | 3722 ASSERT_TRUE(EnumerateAndMatchKeys(-1, iter.get(), &keys_to_match, &count)); |
3719 iter.reset(); | 3723 iter.reset(); |
(...skipping 13 matching lines...) Expand all Loading... |
3733 std::unique_ptr<TestIterator> iter = CreateIterator(); | 3737 std::unique_ptr<TestIterator> iter = CreateIterator(); |
3734 disk_cache::Entry* entry = NULL; | 3738 disk_cache::Entry* entry = NULL; |
3735 ASSERT_EQ(net::OK, iter->OpenNextEntry(&entry)); | 3739 ASSERT_EQ(net::OK, iter->OpenNextEntry(&entry)); |
3736 EXPECT_TRUE(entry); | 3740 EXPECT_TRUE(entry); |
3737 disk_cache::ScopedEntryPtr entry_closer(entry); | 3741 disk_cache::ScopedEntryPtr entry_closer(entry); |
3738 | 3742 |
3739 cache_.reset(); | 3743 cache_.reset(); |
3740 // This test passes if we don't leak memory. | 3744 // This test passes if we don't leak memory. |
3741 } | 3745 } |
3742 | 3746 |
| 3747 // Tests that enumerations include entries with long keys. |
| 3748 TEST_F(DiskCacheBackendTest, SimpleCacheEnumerationLongKeys) { |
| 3749 SetSimpleCacheMode(); |
| 3750 InitCache(); |
| 3751 std::set<std::string> key_pool; |
| 3752 ASSERT_TRUE(CreateSetOfRandomEntries(&key_pool)); |
| 3753 |
| 3754 const size_t long_key_length = |
| 3755 disk_cache::SimpleSynchronousEntry::kInitialHeaderRead + 10; |
| 3756 std::string long_key(long_key_length, 'X'); |
| 3757 key_pool.insert(long_key); |
| 3758 disk_cache::Entry* entry = NULL; |
| 3759 ASSERT_EQ(net::OK, CreateEntry(long_key.c_str(), &entry)); |
| 3760 entry->Close(); |
| 3761 |
| 3762 std::unique_ptr<TestIterator> iter = CreateIterator(); |
| 3763 size_t count = 0; |
| 3764 EXPECT_TRUE(EnumerateAndMatchKeys(-1, iter.get(), &key_pool, &count)); |
| 3765 EXPECT_TRUE(key_pool.empty()); |
| 3766 } |
| 3767 |
3743 // Tests that a SimpleCache doesn't crash when files are deleted very quickly | 3768 // Tests that a SimpleCache doesn't crash when files are deleted very quickly |
3744 // after closing. | 3769 // after closing. |
3745 // NOTE: IF THIS TEST IS FLAKY THEN IT IS FAILING. See https://crbug.com/416940 | 3770 // NOTE: IF THIS TEST IS FLAKY THEN IT IS FAILING. See https://crbug.com/416940 |
3746 TEST_F(DiskCacheBackendTest, SimpleCacheDeleteQuickly) { | 3771 TEST_F(DiskCacheBackendTest, SimpleCacheDeleteQuickly) { |
3747 SetSimpleCacheMode(); | 3772 SetSimpleCacheMode(); |
3748 for (int i = 0; i < 100; ++i) { | 3773 for (int i = 0; i < 100; ++i) { |
3749 InitCache(); | 3774 InitCache(); |
3750 cache_.reset(); | 3775 cache_.reset(); |
3751 EXPECT_TRUE(CleanupCacheDir()); | 3776 EXPECT_TRUE(CleanupCacheDir()); |
3752 } | 3777 } |
(...skipping 23 matching lines...) Expand all Loading... |
3776 // because that would advance the cache directory mtime and invalidate the | 3801 // because that would advance the cache directory mtime and invalidate the |
3777 // index. | 3802 // index. |
3778 entry2->Doom(); | 3803 entry2->Doom(); |
3779 entry2->Close(); | 3804 entry2->Close(); |
3780 | 3805 |
3781 DisableFirstCleanup(); | 3806 DisableFirstCleanup(); |
3782 InitCache(); | 3807 InitCache(); |
3783 EXPECT_EQ(disk_cache::SimpleIndex::INITIALIZE_METHOD_LOADED, | 3808 EXPECT_EQ(disk_cache::SimpleIndex::INITIALIZE_METHOD_LOADED, |
3784 simple_cache_impl_->index()->init_method()); | 3809 simple_cache_impl_->index()->init_method()); |
3785 } | 3810 } |
OLD | NEW |