| 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> |
| 6 |
| 5 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 6 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 7 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
| 8 #include "base/port.h" | |
| 9 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 10 #include "base/strings/string_split.h" | 11 #include "base/strings/string_split.h" |
| 11 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 12 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 13 #include "base/test/mock_entropy_provider.h" | 14 #include "base/test/mock_entropy_provider.h" |
| 14 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 15 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
| 15 #include "base/thread_task_runner_handle.h" | 16 #include "base/thread_task_runner_handle.h" |
| 16 #include "base/threading/platform_thread.h" | 17 #include "base/threading/platform_thread.h" |
| 17 #include "base/threading/thread_restrictions.h" | 18 #include "base/threading/thread_restrictions.h" |
| 18 #include "net/base/cache_type.h" | 19 #include "net/base/cache_type.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 46 namespace { | 47 namespace { |
| 47 | 48 |
| 48 const char kExistingEntryKey[] = "existing entry key"; | 49 const char kExistingEntryKey[] = "existing entry key"; |
| 49 | 50 |
| 50 scoped_ptr<disk_cache::BackendImpl> CreateExistingEntryCache( | 51 scoped_ptr<disk_cache::BackendImpl> CreateExistingEntryCache( |
| 51 const base::Thread& cache_thread, | 52 const base::Thread& cache_thread, |
| 52 base::FilePath& cache_path) { | 53 base::FilePath& cache_path) { |
| 53 net::TestCompletionCallback cb; | 54 net::TestCompletionCallback cb; |
| 54 | 55 |
| 55 scoped_ptr<disk_cache::BackendImpl> cache(new disk_cache::BackendImpl( | 56 scoped_ptr<disk_cache::BackendImpl> cache(new disk_cache::BackendImpl( |
| 56 cache_path, cache_thread.message_loop_proxy(), NULL)); | 57 cache_path, cache_thread.task_runner(), NULL)); |
| 57 int rv = cache->Init(cb.callback()); | 58 int rv = cache->Init(cb.callback()); |
| 58 if (cb.GetResult(rv) != net::OK) | 59 if (cb.GetResult(rv) != net::OK) |
| 59 return scoped_ptr<disk_cache::BackendImpl>(); | 60 return scoped_ptr<disk_cache::BackendImpl>(); |
| 60 | 61 |
| 61 disk_cache::Entry* entry = NULL; | 62 disk_cache::Entry* entry = NULL; |
| 62 rv = cache->CreateEntry(kExistingEntryKey, &entry, cb.callback()); | 63 rv = cache->CreateEntry(kExistingEntryKey, &entry, cb.callback()); |
| 63 if (cb.GetResult(rv) != net::OK) | 64 if (cb.GetResult(rv) != net::OK) |
| 64 return scoped_ptr<disk_cache::BackendImpl>(); | 65 return scoped_ptr<disk_cache::BackendImpl>(); |
| 65 entry->Close(); | 66 entry->Close(); |
| 66 | 67 |
| (...skipping 1825 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1892 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); | 1893 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
| 1893 | 1894 |
| 1894 scoped_ptr<disk_cache::BackendImpl> cache = | 1895 scoped_ptr<disk_cache::BackendImpl> cache = |
| 1895 CreateExistingEntryCache(cache_thread, cache_path_); | 1896 CreateExistingEntryCache(cache_thread, cache_path_); |
| 1896 ASSERT_TRUE(cache.get()); | 1897 ASSERT_TRUE(cache.get()); |
| 1897 | 1898 |
| 1898 net::TestCompletionCallback cb; | 1899 net::TestCompletionCallback cb; |
| 1899 | 1900 |
| 1900 const int kRestartCount = 5; | 1901 const int kRestartCount = 5; |
| 1901 for (int i = 0; i < kRestartCount; ++i) { | 1902 for (int i = 0; i < kRestartCount; ++i) { |
| 1902 cache.reset(new disk_cache::BackendImpl( | 1903 cache.reset(new disk_cache::BackendImpl(cache_path_, |
| 1903 cache_path_, cache_thread.message_loop_proxy(), NULL)); | 1904 cache_thread.task_runner(), NULL)); |
| 1904 int rv = cache->Init(cb.callback()); | 1905 int rv = cache->Init(cb.callback()); |
| 1905 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 1906 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| 1906 EXPECT_EQ(1, cache->GetEntryCount()); | 1907 EXPECT_EQ(1, cache->GetEntryCount()); |
| 1907 | 1908 |
| 1908 disk_cache::Entry* entry = NULL; | 1909 disk_cache::Entry* entry = NULL; |
| 1909 rv = cache->OpenEntry(kExistingEntryKey, &entry, cb.callback()); | 1910 rv = cache->OpenEntry(kExistingEntryKey, &entry, cb.callback()); |
| 1910 EXPECT_EQ(net::OK, cb.GetResult(rv)); | 1911 EXPECT_EQ(net::OK, cb.GetResult(rv)); |
| 1911 EXPECT_TRUE(entry); | 1912 EXPECT_TRUE(entry); |
| 1912 entry->Close(); | 1913 entry->Close(); |
| 1913 } | 1914 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 1934 | 1935 |
| 1935 // Instantiate the SimpleCacheTrial, forcing this run into the | 1936 // Instantiate the SimpleCacheTrial, forcing this run into the |
| 1936 // ExperimentNo group. | 1937 // ExperimentNo group. |
| 1937 base::FieldTrialList field_trial_list(new base::MockEntropyProvider()); | 1938 base::FieldTrialList field_trial_list(new base::MockEntropyProvider()); |
| 1938 base::FieldTrialList::CreateFieldTrial("SimpleCacheTrial", "ExperimentNo"); | 1939 base::FieldTrialList::CreateFieldTrial("SimpleCacheTrial", "ExperimentNo"); |
| 1939 net::TestCompletionCallback cb; | 1940 net::TestCompletionCallback cb; |
| 1940 | 1941 |
| 1941 const int kRestartCount = 5; | 1942 const int kRestartCount = 5; |
| 1942 for (int i = 0; i < kRestartCount; ++i) { | 1943 for (int i = 0; i < kRestartCount; ++i) { |
| 1943 scoped_ptr<disk_cache::BackendImpl> cache(new disk_cache::BackendImpl( | 1944 scoped_ptr<disk_cache::BackendImpl> cache(new disk_cache::BackendImpl( |
| 1944 cache_path_, cache_thread.message_loop_proxy(), NULL)); | 1945 cache_path_, cache_thread.task_runner(), NULL)); |
| 1945 int rv = cache->Init(cb.callback()); | 1946 int rv = cache->Init(cb.callback()); |
| 1946 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 1947 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| 1947 EXPECT_EQ(1, cache->GetEntryCount()); | 1948 EXPECT_EQ(1, cache->GetEntryCount()); |
| 1948 | 1949 |
| 1949 disk_cache::Entry* entry = NULL; | 1950 disk_cache::Entry* entry = NULL; |
| 1950 rv = cache->OpenEntry(kExistingEntryKey, &entry, cb.callback()); | 1951 rv = cache->OpenEntry(kExistingEntryKey, &entry, cb.callback()); |
| 1951 EXPECT_EQ(net::OK, cb.GetResult(rv)); | 1952 EXPECT_EQ(net::OK, cb.GetResult(rv)); |
| 1952 EXPECT_TRUE(entry); | 1953 EXPECT_TRUE(entry); |
| 1953 entry->Close(); | 1954 entry->Close(); |
| 1954 } | 1955 } |
| (...skipping 1368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3323 | 3324 |
| 3324 // The entry is being closed on the Simple Cache worker pool | 3325 // The entry is being closed on the Simple Cache worker pool |
| 3325 disk_cache::SimpleBackendImpl::FlushWorkerPoolForTesting(); | 3326 disk_cache::SimpleBackendImpl::FlushWorkerPoolForTesting(); |
| 3326 base::RunLoop().RunUntilIdle(); | 3327 base::RunLoop().RunUntilIdle(); |
| 3327 | 3328 |
| 3328 // Write an invalid header for stream 0 and stream 1. | 3329 // Write an invalid header for stream 0 and stream 1. |
| 3329 base::FilePath entry_file1_path = cache_path_.AppendASCII( | 3330 base::FilePath entry_file1_path = cache_path_.AppendASCII( |
| 3330 disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0)); | 3331 disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0)); |
| 3331 | 3332 |
| 3332 disk_cache::SimpleFileHeader header; | 3333 disk_cache::SimpleFileHeader header; |
| 3333 header.initial_magic_number = GG_UINT64_C(0xbadf00d); | 3334 header.initial_magic_number = UINT64_C(0xbadf00d); |
| 3334 EXPECT_EQ( | 3335 EXPECT_EQ( |
| 3335 implicit_cast<int>(sizeof(header)), | 3336 implicit_cast<int>(sizeof(header)), |
| 3336 base::WriteFile(entry_file1_path, reinterpret_cast<char*>(&header), | 3337 base::WriteFile(entry_file1_path, reinterpret_cast<char*>(&header), |
| 3337 sizeof(header))); | 3338 sizeof(header))); |
| 3338 ASSERT_EQ(net::ERR_FAILED, OpenEntry(key, &entry)); | 3339 ASSERT_EQ(net::ERR_FAILED, OpenEntry(key, &entry)); |
| 3339 } | 3340 } |
| 3340 | 3341 |
| 3341 // Tests that the Simple Cache Backend fails to initialize with non-matching | 3342 // Tests that the Simple Cache Backend fails to initialize with non-matching |
| 3342 // file structure on disk. | 3343 // file structure on disk. |
| 3343 TEST_F(DiskCacheBackendTest, SimpleCacheOverBlockfileCache) { | 3344 TEST_F(DiskCacheBackendTest, SimpleCacheOverBlockfileCache) { |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3529 // after closing. | 3530 // after closing. |
| 3530 // NOTE: IF THIS TEST IS FLAKY THEN IT IS FAILING. See https://crbug.com/416940 | 3531 // NOTE: IF THIS TEST IS FLAKY THEN IT IS FAILING. See https://crbug.com/416940 |
| 3531 TEST_F(DiskCacheBackendTest, SimpleCacheDeleteQuickly) { | 3532 TEST_F(DiskCacheBackendTest, SimpleCacheDeleteQuickly) { |
| 3532 SetSimpleCacheMode(); | 3533 SetSimpleCacheMode(); |
| 3533 for (int i = 0; i < 100; ++i) { | 3534 for (int i = 0; i < 100; ++i) { |
| 3534 InitCache(); | 3535 InitCache(); |
| 3535 cache_.reset(); | 3536 cache_.reset(); |
| 3536 EXPECT_TRUE(CleanupCacheDir()); | 3537 EXPECT_TRUE(CleanupCacheDir()); |
| 3537 } | 3538 } |
| 3538 } | 3539 } |
| OLD | NEW |