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/file_util.h" | 6 #include "base/file_util.h" |
7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
8 #include "base/stringprintf.h" | 8 #include "base/stringprintf.h" |
9 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 9 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
10 #include "base/threading/platform_thread.h" | 10 #include "base/threading/platform_thread.h" |
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 false, NULL, NULL, &cache, | 238 false, NULL, NULL, &cache, |
239 cb.callback()); | 239 cb.callback()); |
240 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 240 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
241 ASSERT_TRUE(cache); | 241 ASSERT_TRUE(cache); |
242 delete cache; | 242 delete cache; |
243 } | 243 } |
244 | 244 |
245 MessageLoop::current()->RunUntilIdle(); | 245 MessageLoop::current()->RunUntilIdle(); |
246 } | 246 } |
247 | 247 |
248 // Testst that re-creating the cache performs the expected cleanup. | 248 // Tests that |BackendImpl| fails to initialize with a missing file. |
249 TEST_F(DiskCacheBackendTest, CreateBackend_MissingFile) { | 249 TEST_F(DiskCacheBackendTest, CreateBackend_MissingFile) { |
250 ASSERT_TRUE(CopyTestCache("bad_entry")); | 250 ASSERT_TRUE(CopyTestCache("bad_entry")); |
251 base::FilePath filename = cache_path_.AppendASCII("data_1"); | 251 base::FilePath filename = cache_path_.AppendASCII("data_1"); |
252 file_util::Delete(filename, false); | 252 file_util::Delete(filename, false); |
253 DisableFirstCleanup(); | 253 base::Thread cache_thread("CacheThread"); |
254 SetForceCreation(); | 254 ASSERT_TRUE(cache_thread.StartWithOptions( |
| 255 base::Thread::Options(MessageLoop::TYPE_IO, 0))); |
| 256 net::TestCompletionCallback cb; |
255 | 257 |
256 bool prev = base::ThreadRestrictions::SetIOAllowed(false); | 258 bool prev = base::ThreadRestrictions::SetIOAllowed(false); |
257 InitDefaultCacheViaCreator(); | 259 disk_cache::BackendImpl* cache = new disk_cache::BackendImpl( |
| 260 cache_path_, cache_thread.message_loop_proxy(), NULL); |
| 261 int rv = cache->Init(cb.callback()); |
| 262 ASSERT_EQ(net::ERR_FAILED, cb.GetResult(rv)); |
258 base::ThreadRestrictions::SetIOAllowed(prev); | 263 base::ThreadRestrictions::SetIOAllowed(prev); |
| 264 |
| 265 delete cache; |
| 266 DisableIntegrityCheck(); |
259 } | 267 } |
260 | 268 |
261 TEST_F(DiskCacheBackendTest, ExternalFiles) { | 269 TEST_F(DiskCacheBackendTest, ExternalFiles) { |
262 InitCache(); | 270 InitCache(); |
263 // First, let's create a file on the folder. | 271 // First, let's create a file on the folder. |
264 base::FilePath filename = cache_path_.AppendASCII("f_000001"); | 272 base::FilePath filename = cache_path_.AppendASCII("f_000001"); |
265 | 273 |
266 const int kSize = 50; | 274 const int kSize = 50; |
267 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize)); | 275 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize)); |
268 CacheTestFillBuffer(buffer1->data(), kSize, false); | 276 CacheTestFillBuffer(buffer1->data(), kSize, false); |
(...skipping 1265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1534 cache_path_, cache_thread.message_loop_proxy(), NULL); | 1542 cache_path_, cache_thread.message_loop_proxy(), NULL); |
1535 int rv = cache->Init(cb.callback()); | 1543 int rv = cache->Init(cb.callback()); |
1536 ASSERT_EQ(net::ERR_FAILED, cb.GetResult(rv)); | 1544 ASSERT_EQ(net::ERR_FAILED, cb.GetResult(rv)); |
1537 | 1545 |
1538 delete cache; | 1546 delete cache; |
1539 } | 1547 } |
1540 | 1548 |
1541 // Tests that the cache is properly restarted on recovery error. | 1549 // Tests that the cache is properly restarted on recovery error. |
1542 TEST_F(DiskCacheBackendTest, DeleteOld) { | 1550 TEST_F(DiskCacheBackendTest, DeleteOld) { |
1543 ASSERT_TRUE(CopyTestCache("wrong_version")); | 1551 ASSERT_TRUE(CopyTestCache("wrong_version")); |
1544 InitDefaultCacheViaCreator(); | 1552 SetNewEviction(); |
| 1553 base::Thread cache_thread("CacheThread"); |
| 1554 ASSERT_TRUE(cache_thread.StartWithOptions( |
| 1555 base::Thread::Options(MessageLoop::TYPE_IO, 0))); |
| 1556 |
| 1557 net::TestCompletionCallback cb; |
| 1558 bool prev = base::ThreadRestrictions::SetIOAllowed(false); |
| 1559 int rv = disk_cache::CreateCacheBackend(net::DISK_CACHE, cache_path_, 0, true, |
| 1560 cache_thread.message_loop_proxy(), |
| 1561 NULL, &cache_, cb.callback()); |
| 1562 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| 1563 base::ThreadRestrictions::SetIOAllowed(prev); |
| 1564 delete cache_; |
| 1565 cache_ = NULL; |
| 1566 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_)); |
1545 } | 1567 } |
1546 | 1568 |
1547 // We want to be able to deal with messed up entries on disk. | 1569 // We want to be able to deal with messed up entries on disk. |
1548 void DiskCacheBackendTest::BackendInvalidEntry2() { | 1570 void DiskCacheBackendTest::BackendInvalidEntry2() { |
1549 ASSERT_TRUE(CopyTestCache("bad_entry")); | 1571 ASSERT_TRUE(CopyTestCache("bad_entry")); |
1550 DisableFirstCleanup(); | 1572 DisableFirstCleanup(); |
1551 InitCache(); | 1573 InitCache(); |
1552 | 1574 |
1553 disk_cache::Entry *entry1, *entry2; | 1575 disk_cache::Entry *entry1, *entry2; |
1554 ASSERT_EQ(net::OK, OpenEntry("the first key", &entry1)); | 1576 ASSERT_EQ(net::OK, OpenEntry("the first key", &entry1)); |
(...skipping 1079 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2634 // Ping the oldest entry. | 2656 // Ping the oldest entry. |
2635 cache_->OnExternalCacheHit("key0"); | 2657 cache_->OnExternalCacheHit("key0"); |
2636 | 2658 |
2637 TrimForTest(false); | 2659 TrimForTest(false); |
2638 | 2660 |
2639 // Make sure the older key remains. | 2661 // Make sure the older key remains. |
2640 EXPECT_EQ(1, cache_->GetEntryCount()); | 2662 EXPECT_EQ(1, cache_->GetEntryCount()); |
2641 ASSERT_EQ(net::OK, OpenEntry("key0", &entry)); | 2663 ASSERT_EQ(net::OK, OpenEntry("key0", &entry)); |
2642 entry->Close(); | 2664 entry->Close(); |
2643 } | 2665 } |
OLD | NEW |