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