| OLD | NEW |
| 1 // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2010 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 "net/disk_cache/disk_cache_test_util.h" | 5 #include "net/disk_cache/disk_cache_test_util.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/message_loop_proxy.h" | 9 #include "base/message_loop_proxy.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
| 12 #include "net/disk_cache/backend_impl.h" | 12 #include "net/disk_cache/backend_impl.h" |
| 13 #include "net/disk_cache/cache_util.h" | 13 #include "net/disk_cache/cache_util.h" |
| 14 #include "net/disk_cache/file.h" | 14 #include "net/disk_cache/file.h" |
| 15 | 15 |
| 16 using base::Time; | 16 using base::Time; |
| 17 using base::TimeDelta; | 17 using base::TimeDelta; |
| 18 | 18 |
| 19 namespace { |
| 20 |
| 21 FilePath BuildCachePath(const std::string& name) { |
| 22 FilePath path; |
| 23 PathService::Get(base::DIR_TEMP, &path); // Ignore return value; |
| 24 path = path.AppendASCII(name); |
| 25 if (!file_util::PathExists(path)) |
| 26 file_util::CreateDirectory(path); |
| 27 |
| 28 return path; |
| 29 } |
| 30 |
| 31 } // namespace. |
| 32 |
| 19 std::string GenerateKey(bool same_length) { | 33 std::string GenerateKey(bool same_length) { |
| 20 char key[200]; | 34 char key[200]; |
| 21 CacheTestFillBuffer(key, sizeof(key), same_length); | 35 CacheTestFillBuffer(key, sizeof(key), same_length); |
| 22 | 36 |
| 23 key[199] = '\0'; | 37 key[199] = '\0'; |
| 24 return std::string(key); | 38 return std::string(key); |
| 25 } | 39 } |
| 26 | 40 |
| 27 void CacheTestFillBuffer(char* buffer, size_t len, bool no_nulls) { | 41 void CacheTestFillBuffer(char* buffer, size_t len, bool no_nulls) { |
| 28 static bool called = false; | 42 static bool called = false; |
| 29 if (!called) { | 43 if (!called) { |
| 30 called = true; | 44 called = true; |
| 31 int seed = static_cast<int>(Time::Now().ToInternalValue()); | 45 int seed = static_cast<int>(Time::Now().ToInternalValue()); |
| 32 srand(seed); | 46 srand(seed); |
| 33 } | 47 } |
| 34 | 48 |
| 35 for (size_t i = 0; i < len; i++) { | 49 for (size_t i = 0; i < len; i++) { |
| 36 buffer[i] = static_cast<char>(rand()); | 50 buffer[i] = static_cast<char>(rand()); |
| 37 if (!buffer[i] && no_nulls) | 51 if (!buffer[i] && no_nulls) |
| 38 buffer[i] = 'g'; | 52 buffer[i] = 'g'; |
| 39 } | 53 } |
| 40 if (len && !buffer[0]) | 54 if (len && !buffer[0]) |
| 41 buffer[0] = 'g'; | 55 buffer[0] = 'g'; |
| 42 } | 56 } |
| 43 | 57 |
| 58 FilePath GetCacheFilePath() { |
| 59 return BuildCachePath("cache_test"); |
| 60 } |
| 61 |
| 44 bool CreateCacheTestFile(const FilePath& name) { | 62 bool CreateCacheTestFile(const FilePath& name) { |
| 45 int flags = base::PLATFORM_FILE_CREATE_ALWAYS | | 63 int flags = base::PLATFORM_FILE_CREATE_ALWAYS | |
| 46 base::PLATFORM_FILE_READ | | 64 base::PLATFORM_FILE_READ | |
| 47 base::PLATFORM_FILE_WRITE; | 65 base::PLATFORM_FILE_WRITE; |
| 48 | 66 |
| 49 scoped_refptr<disk_cache::File> file(new disk_cache::File( | 67 scoped_refptr<disk_cache::File> file(new disk_cache::File( |
| 50 base::CreatePlatformFile(name, flags, NULL, NULL))); | 68 base::CreatePlatformFile(name, flags, NULL, NULL))); |
| 51 if (!file->IsValid()) | 69 if (!file->IsValid()) |
| 52 return false; | 70 return false; |
| 53 | 71 |
| 54 file->SetLength(4 * 1024 * 1024); | 72 file->SetLength(4 * 1024 * 1024); |
| 55 return true; | 73 return true; |
| 56 } | 74 } |
| 57 | 75 |
| 58 bool DeleteCache(const FilePath& path) { | 76 bool DeleteCache(const FilePath& path) { |
| 59 disk_cache::DeleteCache(path, false); | 77 disk_cache::DeleteCache(path, false); |
| 60 return true; | 78 return true; |
| 61 } | 79 } |
| 62 | 80 |
| 63 bool CopyTestCache(const std::string& name, const FilePath& destination) { | 81 bool CopyTestCache(const std::string& name) { |
| 64 FilePath path; | 82 FilePath path; |
| 65 PathService::Get(base::DIR_SOURCE_ROOT, &path); | 83 PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 66 path = path.AppendASCII("net"); | 84 path = path.AppendASCII("net"); |
| 67 path = path.AppendASCII("data"); | 85 path = path.AppendASCII("data"); |
| 68 path = path.AppendASCII("cache_tests"); | 86 path = path.AppendASCII("cache_tests"); |
| 69 path = path.AppendASCII(name); | 87 path = path.AppendASCII(name); |
| 70 | 88 |
| 71 if (!DeleteCache(destination)) | 89 FilePath dest = GetCacheFilePath(); |
| 90 if (!DeleteCache(dest)) |
| 72 return false; | 91 return false; |
| 73 return file_util::CopyDirectory(path, destination, false); | 92 return file_util::CopyDirectory(path, dest, false); |
| 74 } | 93 } |
| 75 | 94 |
| 76 bool CheckCacheIntegrity(const FilePath& path, bool new_eviction) { | 95 bool CheckCacheIntegrity(const FilePath& path, bool new_eviction) { |
| 77 scoped_ptr<disk_cache::BackendImpl> cache(new disk_cache::BackendImpl( | 96 scoped_ptr<disk_cache::BackendImpl> cache(new disk_cache::BackendImpl( |
| 78 path, base::MessageLoopProxy::CreateForCurrentThread())); | 97 path, base::MessageLoopProxy::CreateForCurrentThread())); |
| 79 if (!cache.get()) | 98 if (!cache.get()) |
| 80 return false; | 99 return false; |
| 81 if (new_eviction) | 100 if (new_eviction) |
| 82 cache->SetNewEviction(); | 101 cache->SetNewEviction(); |
| 83 cache->SetFlags(disk_cache::kNoRandom); | 102 cache->SetFlags(disk_cache::kNoRandom); |
| 84 if (cache->SyncInit() != net::OK) | 103 if (cache->SyncInit() != net::OK) |
| 85 return false; | 104 return false; |
| 86 return cache->SelfCheck() >= 0; | 105 return cache->SelfCheck() >= 0; |
| 87 } | 106 } |
| 88 | 107 |
| 89 ScopedTestCache::ScopedTestCache() { | 108 ScopedTestCache::ScopedTestCache() : path_(GetCacheFilePath()) { |
| 90 temp_dir_.CreateUniqueTempDir(); | 109 bool result = DeleteCache(path_); |
| 110 DCHECK(result); |
| 111 } |
| 112 |
| 113 ScopedTestCache::ScopedTestCache(const std::string& name) |
| 114 : path_(BuildCachePath(name)) { |
| 115 bool result = DeleteCache(path_); |
| 116 DCHECK(result); |
| 91 } | 117 } |
| 92 | 118 |
| 93 ScopedTestCache::~ScopedTestCache() { | 119 ScopedTestCache::~ScopedTestCache() { |
| 120 file_util::Delete(path(), true); |
| 94 } | 121 } |
| 95 | 122 |
| 96 // ----------------------------------------------------------------------- | 123 // ----------------------------------------------------------------------- |
| 97 | 124 |
| 98 volatile int g_cache_tests_received = 0; | 125 volatile int g_cache_tests_received = 0; |
| 99 volatile bool g_cache_tests_error = 0; | 126 volatile bool g_cache_tests_error = 0; |
| 100 | 127 |
| 101 // On the actual callback, increase the number of tests received and check for | 128 // On the actual callback, increase the number of tests received and check for |
| 102 // errors (an unexpected test received) | 129 // errors (an unexpected test received) |
| 103 void CallbackTest::RunWithParams(const Tuple1<int>& params) { | 130 void CallbackTest::RunWithParams(const Tuple1<int>& params) { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 } else { | 174 } else { |
| 148 // Not finished yet. See if we have to abort. | 175 // Not finished yet. See if we have to abort. |
| 149 if (last_ == g_cache_tests_received) | 176 if (last_ == g_cache_tests_received) |
| 150 num_iterations_++; | 177 num_iterations_++; |
| 151 else | 178 else |
| 152 last_ = g_cache_tests_received; | 179 last_ = g_cache_tests_received; |
| 153 if (40 == num_iterations_) | 180 if (40 == num_iterations_) |
| 154 MessageLoop::current()->Quit(); | 181 MessageLoop::current()->Quit(); |
| 155 } | 182 } |
| 156 } | 183 } |
| OLD | NEW |