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/port.h" | 7 #include "base/port.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 10 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); | 231 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
232 | 232 |
233 // Test the private factory method(s). | 233 // Test the private factory method(s). |
234 disk_cache::Backend* cache = NULL; | 234 disk_cache::Backend* cache = NULL; |
235 cache = disk_cache::MemBackendImpl::CreateBackend(0, NULL); | 235 cache = disk_cache::MemBackendImpl::CreateBackend(0, NULL); |
236 ASSERT_TRUE(cache); | 236 ASSERT_TRUE(cache); |
237 delete cache; | 237 delete cache; |
238 cache = NULL; | 238 cache = NULL; |
239 | 239 |
240 // Now test the public API. | 240 // Now test the public API. |
241 int rv = disk_cache::CreateCacheBackend(net::DISK_CACHE, | 241 int rv = |
242 net::CACHE_BACKEND_DEFAULT, | 242 disk_cache::CreateCacheBackend(net::DISK_CACHE, |
243 cache_path_, 0, false, | 243 net::CACHE_BACKEND_DEFAULT, |
244 cache_thread.message_loop_proxy(), | 244 cache_path_, |
245 NULL, &cache, cb.callback()); | 245 0, |
| 246 false, |
| 247 cache_thread.message_loop_proxy().get(), |
| 248 NULL, |
| 249 &cache, |
| 250 cb.callback()); |
246 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 251 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
247 ASSERT_TRUE(cache); | 252 ASSERT_TRUE(cache); |
248 delete cache; | 253 delete cache; |
249 cache = NULL; | 254 cache = NULL; |
250 | 255 |
251 rv = disk_cache::CreateCacheBackend(net::MEMORY_CACHE, | 256 rv = disk_cache::CreateCacheBackend(net::MEMORY_CACHE, |
252 net::CACHE_BACKEND_DEFAULT, | 257 net::CACHE_BACKEND_DEFAULT, |
253 base::FilePath(), 0, | 258 base::FilePath(), 0, |
254 false, NULL, NULL, &cache, | 259 false, NULL, NULL, &cache, |
255 cb.callback()); | 260 cb.callback()); |
(...skipping 10 matching lines...) Expand all Loading... |
266 ASSERT_TRUE(CopyTestCache("bad_entry")); | 271 ASSERT_TRUE(CopyTestCache("bad_entry")); |
267 base::FilePath filename = cache_path_.AppendASCII("data_1"); | 272 base::FilePath filename = cache_path_.AppendASCII("data_1"); |
268 file_util::Delete(filename, false); | 273 file_util::Delete(filename, false); |
269 base::Thread cache_thread("CacheThread"); | 274 base::Thread cache_thread("CacheThread"); |
270 ASSERT_TRUE(cache_thread.StartWithOptions( | 275 ASSERT_TRUE(cache_thread.StartWithOptions( |
271 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); | 276 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
272 net::TestCompletionCallback cb; | 277 net::TestCompletionCallback cb; |
273 | 278 |
274 bool prev = base::ThreadRestrictions::SetIOAllowed(false); | 279 bool prev = base::ThreadRestrictions::SetIOAllowed(false); |
275 disk_cache::BackendImpl* cache = new disk_cache::BackendImpl( | 280 disk_cache::BackendImpl* cache = new disk_cache::BackendImpl( |
276 cache_path_, cache_thread.message_loop_proxy(), NULL); | 281 cache_path_, cache_thread.message_loop_proxy().get(), NULL); |
277 int rv = cache->Init(cb.callback()); | 282 int rv = cache->Init(cb.callback()); |
278 ASSERT_EQ(net::ERR_FAILED, cb.GetResult(rv)); | 283 ASSERT_EQ(net::ERR_FAILED, cb.GetResult(rv)); |
279 base::ThreadRestrictions::SetIOAllowed(prev); | 284 base::ThreadRestrictions::SetIOAllowed(prev); |
280 | 285 |
281 delete cache; | 286 delete cache; |
282 DisableIntegrityCheck(); | 287 DisableIntegrityCheck(); |
283 } | 288 } |
284 | 289 |
285 TEST_F(DiskCacheBackendTest, ExternalFiles) { | 290 TEST_F(DiskCacheBackendTest, ExternalFiles) { |
286 InitCache(); | 291 InitCache(); |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
469 ASSERT_TRUE(CleanupCacheDir()); | 474 ASSERT_TRUE(CleanupCacheDir()); |
470 base::FilePath index = cache_path_.AppendASCII("index"); | 475 base::FilePath index = cache_path_.AppendASCII("index"); |
471 ASSERT_EQ(5, file_util::WriteFile(index, "hello", 5)); | 476 ASSERT_EQ(5, file_util::WriteFile(index, "hello", 5)); |
472 | 477 |
473 base::Thread cache_thread("CacheThread"); | 478 base::Thread cache_thread("CacheThread"); |
474 ASSERT_TRUE(cache_thread.StartWithOptions( | 479 ASSERT_TRUE(cache_thread.StartWithOptions( |
475 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); | 480 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
476 net::TestCompletionCallback cb; | 481 net::TestCompletionCallback cb; |
477 | 482 |
478 disk_cache::Backend* backend = NULL; | 483 disk_cache::Backend* backend = NULL; |
479 int rv = disk_cache::CreateCacheBackend( | 484 int rv = |
480 net::DISK_CACHE, net::CACHE_BACKEND_BLOCKFILE, cache_path_, 0, false, | 485 disk_cache::CreateCacheBackend(net::DISK_CACHE, |
481 cache_thread.message_loop_proxy(), NULL, &backend, cb.callback()); | 486 net::CACHE_BACKEND_BLOCKFILE, |
| 487 cache_path_, |
| 488 0, |
| 489 false, |
| 490 cache_thread.message_loop_proxy().get(), |
| 491 NULL, |
| 492 &backend, |
| 493 cb.callback()); |
482 ASSERT_NE(net::OK, cb.GetResult(rv)); | 494 ASSERT_NE(net::OK, cb.GetResult(rv)); |
483 | 495 |
484 ASSERT_TRUE(backend == NULL); | 496 ASSERT_TRUE(backend == NULL); |
485 delete backend; | 497 delete backend; |
486 } | 498 } |
487 | 499 |
488 void DiskCacheBackendTest::BackendSetSize() { | 500 void DiskCacheBackendTest::BackendSetSize() { |
489 const int cache_size = 0x10000; // 64 kB | 501 const int cache_size = 0x10000; // 64 kB |
490 SetMaxSize(cache_size); | 502 SetMaxSize(cache_size); |
491 InitCache(); | 503 InitCache(); |
(...skipping 1178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1670 | 1682 |
1671 // Tests that the |BackendImpl| fails to start with the wrong cache version. | 1683 // Tests that the |BackendImpl| fails to start with the wrong cache version. |
1672 TEST_F(DiskCacheTest, WrongVersion) { | 1684 TEST_F(DiskCacheTest, WrongVersion) { |
1673 ASSERT_TRUE(CopyTestCache("wrong_version")); | 1685 ASSERT_TRUE(CopyTestCache("wrong_version")); |
1674 base::Thread cache_thread("CacheThread"); | 1686 base::Thread cache_thread("CacheThread"); |
1675 ASSERT_TRUE(cache_thread.StartWithOptions( | 1687 ASSERT_TRUE(cache_thread.StartWithOptions( |
1676 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); | 1688 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
1677 net::TestCompletionCallback cb; | 1689 net::TestCompletionCallback cb; |
1678 | 1690 |
1679 disk_cache::BackendImpl* cache = new disk_cache::BackendImpl( | 1691 disk_cache::BackendImpl* cache = new disk_cache::BackendImpl( |
1680 cache_path_, cache_thread.message_loop_proxy(), NULL); | 1692 cache_path_, cache_thread.message_loop_proxy().get(), NULL); |
1681 int rv = cache->Init(cb.callback()); | 1693 int rv = cache->Init(cb.callback()); |
1682 ASSERT_EQ(net::ERR_FAILED, cb.GetResult(rv)); | 1694 ASSERT_EQ(net::ERR_FAILED, cb.GetResult(rv)); |
1683 | 1695 |
1684 delete cache; | 1696 delete cache; |
1685 } | 1697 } |
1686 | 1698 |
1687 // Tests that the cache is properly restarted on recovery error. | 1699 // Tests that the cache is properly restarted on recovery error. |
1688 TEST_F(DiskCacheBackendTest, DeleteOld) { | 1700 TEST_F(DiskCacheBackendTest, DeleteOld) { |
1689 ASSERT_TRUE(CopyTestCache("wrong_version")); | 1701 ASSERT_TRUE(CopyTestCache("wrong_version")); |
1690 SetNewEviction(); | 1702 SetNewEviction(); |
1691 base::Thread cache_thread("CacheThread"); | 1703 base::Thread cache_thread("CacheThread"); |
1692 ASSERT_TRUE(cache_thread.StartWithOptions( | 1704 ASSERT_TRUE(cache_thread.StartWithOptions( |
1693 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); | 1705 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
1694 | 1706 |
1695 net::TestCompletionCallback cb; | 1707 net::TestCompletionCallback cb; |
1696 bool prev = base::ThreadRestrictions::SetIOAllowed(false); | 1708 bool prev = base::ThreadRestrictions::SetIOAllowed(false); |
1697 base::FilePath path(cache_path_); | 1709 base::FilePath path(cache_path_); |
1698 int rv = disk_cache::CreateCacheBackend(net::DISK_CACHE, | 1710 int rv = |
1699 net::CACHE_BACKEND_BLOCKFILE, path, | 1711 disk_cache::CreateCacheBackend(net::DISK_CACHE, |
1700 0, true, | 1712 net::CACHE_BACKEND_BLOCKFILE, |
1701 cache_thread.message_loop_proxy(), | 1713 path, |
1702 NULL, &cache_, cb.callback()); | 1714 0, |
| 1715 true, |
| 1716 cache_thread.message_loop_proxy().get(), |
| 1717 NULL, |
| 1718 &cache_, |
| 1719 cb.callback()); |
1703 path.clear(); // Make sure path was captured by the previous call. | 1720 path.clear(); // Make sure path was captured by the previous call. |
1704 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 1721 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
1705 base::ThreadRestrictions::SetIOAllowed(prev); | 1722 base::ThreadRestrictions::SetIOAllowed(prev); |
1706 delete cache_; | 1723 delete cache_; |
1707 cache_ = NULL; | 1724 cache_ = NULL; |
1708 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_)); | 1725 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_)); |
1709 } | 1726 } |
1710 | 1727 |
1711 // We want to be able to deal with messed up entries on disk. | 1728 // We want to be able to deal with messed up entries on disk. |
1712 void DiskCacheBackendTest::BackendInvalidEntry2() { | 1729 void DiskCacheBackendTest::BackendInvalidEntry2() { |
(...skipping 736 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2449 InitCache(); | 2466 InitCache(); |
2450 BackendDisable4(); | 2467 BackendDisable4(); |
2451 } | 2468 } |
2452 | 2469 |
2453 TEST_F(DiskCacheTest, Backend_UsageStats) { | 2470 TEST_F(DiskCacheTest, Backend_UsageStats) { |
2454 MessageLoopHelper helper; | 2471 MessageLoopHelper helper; |
2455 | 2472 |
2456 ASSERT_TRUE(CleanupCacheDir()); | 2473 ASSERT_TRUE(CleanupCacheDir()); |
2457 scoped_ptr<disk_cache::BackendImpl> cache; | 2474 scoped_ptr<disk_cache::BackendImpl> cache; |
2458 cache.reset(new disk_cache::BackendImpl( | 2475 cache.reset(new disk_cache::BackendImpl( |
2459 cache_path_, base::MessageLoopProxy::current(), | 2476 cache_path_, base::MessageLoopProxy::current().get(), NULL)); |
2460 NULL)); | |
2461 ASSERT_TRUE(NULL != cache.get()); | 2477 ASSERT_TRUE(NULL != cache.get()); |
2462 cache->SetUnitTestMode(); | 2478 cache->SetUnitTestMode(); |
2463 ASSERT_EQ(net::OK, cache->SyncInit()); | 2479 ASSERT_EQ(net::OK, cache->SyncInit()); |
2464 | 2480 |
2465 // Wait for a callback that never comes... about 2 secs :). The message loop | 2481 // Wait for a callback that never comes... about 2 secs :). The message loop |
2466 // has to run to allow invocation of the usage timer. | 2482 // has to run to allow invocation of the usage timer. |
2467 helper.WaitUntilCacheIoFinished(1); | 2483 helper.WaitUntilCacheIoFinished(1); |
2468 } | 2484 } |
2469 | 2485 |
2470 void DiskCacheBackendTest::BackendDoomAll() { | 2486 void DiskCacheBackendTest::BackendDoomAll() { |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2574 ASSERT_TRUE(store2.CreateUniqueTempDir()); | 2590 ASSERT_TRUE(store2.CreateUniqueTempDir()); |
2575 | 2591 |
2576 base::Thread cache_thread("CacheThread"); | 2592 base::Thread cache_thread("CacheThread"); |
2577 ASSERT_TRUE(cache_thread.StartWithOptions( | 2593 ASSERT_TRUE(cache_thread.StartWithOptions( |
2578 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); | 2594 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
2579 net::TestCompletionCallback cb; | 2595 net::TestCompletionCallback cb; |
2580 | 2596 |
2581 const int kNumberOfCaches = 2; | 2597 const int kNumberOfCaches = 2; |
2582 disk_cache::Backend* cache[kNumberOfCaches]; | 2598 disk_cache::Backend* cache[kNumberOfCaches]; |
2583 | 2599 |
2584 int rv = disk_cache::CreateCacheBackend( | 2600 int rv = |
2585 net::DISK_CACHE, net::CACHE_BACKEND_DEFAULT, store1.path(), 0, false, | 2601 disk_cache::CreateCacheBackend(net::DISK_CACHE, |
2586 cache_thread.message_loop_proxy(), NULL, &cache[0], cb.callback()); | 2602 net::CACHE_BACKEND_DEFAULT, |
| 2603 store1.path(), |
| 2604 0, |
| 2605 false, |
| 2606 cache_thread.message_loop_proxy().get(), |
| 2607 NULL, |
| 2608 &cache[0], |
| 2609 cb.callback()); |
2587 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 2610 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
2588 rv = disk_cache::CreateCacheBackend( | 2611 rv = disk_cache::CreateCacheBackend(net::MEDIA_CACHE, |
2589 net::MEDIA_CACHE, net::CACHE_BACKEND_DEFAULT, store2.path(), 0, false, | 2612 net::CACHE_BACKEND_DEFAULT, |
2590 cache_thread.message_loop_proxy(), NULL, &cache[1], cb.callback()); | 2613 store2.path(), |
| 2614 0, |
| 2615 false, |
| 2616 cache_thread.message_loop_proxy().get(), |
| 2617 NULL, |
| 2618 &cache[1], |
| 2619 cb.callback()); |
2591 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 2620 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
2592 | 2621 |
2593 ASSERT_TRUE(cache[0] != NULL && cache[1] != NULL); | 2622 ASSERT_TRUE(cache[0] != NULL && cache[1] != NULL); |
2594 | 2623 |
2595 std::string key("the first key"); | 2624 std::string key("the first key"); |
2596 disk_cache::Entry* entry; | 2625 disk_cache::Entry* entry; |
2597 for (int i = 0; i < kNumberOfCaches; i++) { | 2626 for (int i = 0; i < kNumberOfCaches; i++) { |
2598 rv = cache[i]->CreateEntry(key, &entry, cb.callback()); | 2627 rv = cache[i]->CreateEntry(key, &entry, cb.callback()); |
2599 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 2628 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
2600 entry->Close(); | 2629 entry->Close(); |
(...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2982 ASSERT_EQ(0, WriteData(entry, 0, 0, buffer.get(), 0, false)); | 3011 ASSERT_EQ(0, WriteData(entry, 0, 0, buffer.get(), 0, false)); |
2983 entry->Close(); | 3012 entry->Close(); |
2984 delete cache_; | 3013 delete cache_; |
2985 cache_ = NULL; | 3014 cache_ = NULL; |
2986 | 3015 |
2987 // Check that the |SimpleBackendImpl| does not favor this structure. | 3016 // Check that the |SimpleBackendImpl| does not favor this structure. |
2988 base::Thread cache_thread("CacheThread"); | 3017 base::Thread cache_thread("CacheThread"); |
2989 ASSERT_TRUE(cache_thread.StartWithOptions( | 3018 ASSERT_TRUE(cache_thread.StartWithOptions( |
2990 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); | 3019 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
2991 disk_cache::SimpleBackendImpl* simple_cache = | 3020 disk_cache::SimpleBackendImpl* simple_cache = |
2992 new disk_cache::SimpleBackendImpl(cache_path_, 0, net::DISK_CACHE, | 3021 new disk_cache::SimpleBackendImpl(cache_path_, |
2993 cache_thread.message_loop_proxy(), | 3022 0, |
| 3023 net::DISK_CACHE, |
| 3024 cache_thread.message_loop_proxy().get(), |
2994 NULL); | 3025 NULL); |
2995 net::TestCompletionCallback cb; | 3026 net::TestCompletionCallback cb; |
2996 int rv = simple_cache->Init(cb.callback()); | 3027 int rv = simple_cache->Init(cb.callback()); |
2997 EXPECT_NE(net::OK, cb.GetResult(rv)); | 3028 EXPECT_NE(net::OK, cb.GetResult(rv)); |
2998 delete simple_cache; | 3029 delete simple_cache; |
2999 DisableIntegrityCheck(); | 3030 DisableIntegrityCheck(); |
3000 } | 3031 } |
3001 | 3032 |
3002 // Tests that the |BackendImpl| refuses to initialize on top of the files | 3033 // Tests that the |BackendImpl| refuses to initialize on top of the files |
3003 // generated by the Simple Cache Backend. | 3034 // generated by the Simple Cache Backend. |
3004 TEST_F(DiskCacheBackendTest, BlockfileCacheOverSimpleCache) { | 3035 TEST_F(DiskCacheBackendTest, BlockfileCacheOverSimpleCache) { |
3005 // Create a cache structure with the |SimpleBackendImpl|. | 3036 // Create a cache structure with the |SimpleBackendImpl|. |
3006 SetSimpleCacheMode(); | 3037 SetSimpleCacheMode(); |
3007 InitCache(); | 3038 InitCache(); |
3008 disk_cache::Entry* entry; | 3039 disk_cache::Entry* entry; |
3009 const int kSize = 50; | 3040 const int kSize = 50; |
3010 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); | 3041 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); |
3011 CacheTestFillBuffer(buffer->data(), kSize, false); | 3042 CacheTestFillBuffer(buffer->data(), kSize, false); |
3012 ASSERT_EQ(net::OK, CreateEntry("key", &entry)); | 3043 ASSERT_EQ(net::OK, CreateEntry("key", &entry)); |
3013 ASSERT_EQ(0, WriteData(entry, 0, 0, buffer.get(), 0, false)); | 3044 ASSERT_EQ(0, WriteData(entry, 0, 0, buffer.get(), 0, false)); |
3014 entry->Close(); | 3045 entry->Close(); |
3015 delete cache_; | 3046 delete cache_; |
3016 cache_ = NULL; | 3047 cache_ = NULL; |
3017 | 3048 |
3018 // Check that the |BackendImpl| does not favor this structure. | 3049 // Check that the |BackendImpl| does not favor this structure. |
3019 base::Thread cache_thread("CacheThread"); | 3050 base::Thread cache_thread("CacheThread"); |
3020 ASSERT_TRUE(cache_thread.StartWithOptions( | 3051 ASSERT_TRUE(cache_thread.StartWithOptions( |
3021 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); | 3052 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
3022 disk_cache::BackendImpl* cache = new disk_cache::BackendImpl( | 3053 disk_cache::BackendImpl* cache = new disk_cache::BackendImpl( |
3023 cache_path_, base::MessageLoopProxy::current(), NULL); | 3054 cache_path_, base::MessageLoopProxy::current().get(), NULL); |
3024 cache->SetUnitTestMode(); | 3055 cache->SetUnitTestMode(); |
3025 net::TestCompletionCallback cb; | 3056 net::TestCompletionCallback cb; |
3026 int rv = cache->Init(cb.callback()); | 3057 int rv = cache->Init(cb.callback()); |
3027 EXPECT_NE(net::OK, cb.GetResult(rv)); | 3058 EXPECT_NE(net::OK, cb.GetResult(rv)); |
3028 delete cache; | 3059 delete cache; |
3029 DisableIntegrityCheck(); | 3060 DisableIntegrityCheck(); |
3030 } | 3061 } |
3031 | 3062 |
3032 #endif // !defined(OS_WIN) | 3063 #endif // !defined(OS_WIN) |
OLD | NEW |