Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "net/disk_cache/backend_impl.h" | 10 #include "net/disk_cache/backend_impl.h" |
| 11 #include "net/disk_cache/cache_util.h" | 11 #include "net/disk_cache/cache_util.h" |
| 12 #include "net/disk_cache/file.h" | 12 #include "net/disk_cache/file.h" |
| 13 | 13 |
| 14 using base::Time; | 14 using base::Time; |
| 15 using base::TimeDelta; | 15 using base::TimeDelta; |
| 16 | 16 |
| 17 namespace { | |
| 18 | |
| 19 std::wstring BuildCachePath(const std::wstring name) { | |
|
Alpha Left Google
2009/03/26 00:08:12
Can we name this function GetCachePath(const std::
| |
| 20 std::wstring path; | |
| 21 PathService::Get(base::DIR_TEMP, &path); | |
| 22 file_util::AppendToPath(&path, name); | |
| 23 if (!file_util::PathExists(path)) | |
| 24 file_util::CreateDirectory(path); | |
| 25 | |
| 26 return path; | |
| 27 } | |
| 28 | |
| 29 } // namespace. | |
| 30 | |
| 17 std::string GenerateKey(bool same_length) { | 31 std::string GenerateKey(bool same_length) { |
| 18 char key[200]; | 32 char key[200]; |
| 19 CacheTestFillBuffer(key, sizeof(key), same_length); | 33 CacheTestFillBuffer(key, sizeof(key), same_length); |
| 20 | 34 |
| 21 key[199] = '\0'; | 35 key[199] = '\0'; |
| 22 return std::string(key); | 36 return std::string(key); |
| 23 } | 37 } |
| 24 | 38 |
| 25 void CacheTestFillBuffer(char* buffer, size_t len, bool no_nulls) { | 39 void CacheTestFillBuffer(char* buffer, size_t len, bool no_nulls) { |
| 26 static bool called = false; | 40 static bool called = false; |
| 27 if (!called) { | 41 if (!called) { |
| 28 called = true; | 42 called = true; |
| 29 int seed = static_cast<int>(Time::Now().ToInternalValue()); | 43 int seed = static_cast<int>(Time::Now().ToInternalValue()); |
| 30 srand(seed); | 44 srand(seed); |
| 31 } | 45 } |
| 32 | 46 |
| 33 for (size_t i = 0; i < len; i++) { | 47 for (size_t i = 0; i < len; i++) { |
| 34 buffer[i] = static_cast<char>(rand()); | 48 buffer[i] = static_cast<char>(rand()); |
| 35 if (!buffer[i] && no_nulls) | 49 if (!buffer[i] && no_nulls) |
| 36 buffer[i] = 'g'; | 50 buffer[i] = 'g'; |
| 37 } | 51 } |
| 38 if (len && !buffer[0]) | 52 if (len && !buffer[0]) |
| 39 buffer[0] = 'g'; | 53 buffer[0] = 'g'; |
| 40 } | 54 } |
| 41 | 55 |
| 42 std::wstring GetCachePath() { | 56 std::wstring GetCachePath() { |
| 43 std::wstring path; | 57 return BuildCachePath(L"cache_test"); |
| 44 PathService::Get(base::DIR_TEMP, &path); | |
| 45 file_util::AppendToPath(&path, L"cache_test"); | |
| 46 if (!file_util::PathExists(path)) | |
| 47 file_util::CreateDirectory(path); | |
| 48 | |
| 49 return path; | |
| 50 } | 58 } |
| 51 | 59 |
| 52 bool CreateCacheTestFile(const wchar_t* name) { | 60 bool CreateCacheTestFile(const wchar_t* name) { |
| 53 using namespace disk_cache; | 61 using namespace disk_cache; |
| 54 int flags = base::PLATFORM_FILE_CREATE_ALWAYS | | 62 int flags = base::PLATFORM_FILE_CREATE_ALWAYS | |
| 55 base::PLATFORM_FILE_READ | | 63 base::PLATFORM_FILE_READ | |
| 56 base::PLATFORM_FILE_WRITE; | 64 base::PLATFORM_FILE_WRITE; |
| 57 | 65 |
| 58 scoped_refptr<File> file(new File( | 66 scoped_refptr<File> file(new File( |
| 59 base::CreatePlatformFile(name, flags, NULL))); | 67 base::CreatePlatformFile(name, flags, NULL))); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 76 if (!cache->Init()) | 84 if (!cache->Init()) |
| 77 return false; | 85 return false; |
| 78 return cache->SelfCheck() >= 0; | 86 return cache->SelfCheck() >= 0; |
| 79 } | 87 } |
| 80 | 88 |
| 81 ScopedTestCache::ScopedTestCache() : path_(GetCachePath()) { | 89 ScopedTestCache::ScopedTestCache() : path_(GetCachePath()) { |
| 82 bool result = DeleteCache(path_.c_str()); | 90 bool result = DeleteCache(path_.c_str()); |
| 83 DCHECK(result); | 91 DCHECK(result); |
| 84 } | 92 } |
| 85 | 93 |
| 94 ScopedTestCache::ScopedTestCache(const std::wstring& name) | |
| 95 : path_(BuildCachePath(name)) { | |
| 96 bool result = DeleteCache(path_.c_str()); | |
| 97 DCHECK(result); | |
| 98 } | |
| 99 | |
| 86 ScopedTestCache::~ScopedTestCache() { | 100 ScopedTestCache::~ScopedTestCache() { |
| 87 file_util::Delete(path(), true); | 101 file_util::Delete(path(), true); |
| 88 } | 102 } |
| 89 | 103 |
| 90 // ----------------------------------------------------------------------- | 104 // ----------------------------------------------------------------------- |
| 91 | 105 |
| 92 int g_cache_tests_max_id = 0; | 106 int g_cache_tests_max_id = 0; |
| 93 volatile int g_cache_tests_received = 0; | 107 volatile int g_cache_tests_received = 0; |
| 94 volatile bool g_cache_tests_error = 0; | 108 volatile bool g_cache_tests_error = 0; |
| 95 | 109 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 141 } else { | 155 } else { |
| 142 // Not finished yet. See if we have to abort. | 156 // Not finished yet. See if we have to abort. |
| 143 if (last_ == g_cache_tests_received) | 157 if (last_ == g_cache_tests_received) |
| 144 num_iterations_++; | 158 num_iterations_++; |
| 145 else | 159 else |
| 146 last_ = g_cache_tests_received; | 160 last_ = g_cache_tests_received; |
| 147 if (40 == num_iterations_) | 161 if (40 == num_iterations_) |
| 148 MessageLoop::current()->Quit(); | 162 MessageLoop::current()->Quit(); |
| 149 } | 163 } |
| 150 } | 164 } |
| OLD | NEW |