OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 |
(...skipping 12 matching lines...) Expand all Loading... |
84 if (!cache.get()) | 66 if (!cache.get()) |
85 return false; | 67 return false; |
86 if (new_eviction) | 68 if (new_eviction) |
87 cache->SetNewEviction(); | 69 cache->SetNewEviction(); |
88 cache->SetFlags(disk_cache::kNoRandom); | 70 cache->SetFlags(disk_cache::kNoRandom); |
89 if (cache->SyncInit() != net::OK) | 71 if (cache->SyncInit() != net::OK) |
90 return false; | 72 return false; |
91 return cache->SelfCheck() >= 0; | 73 return cache->SelfCheck() >= 0; |
92 } | 74 } |
93 | 75 |
94 ScopedTestCache::ScopedTestCache(const FilePath& path) : path_(path) { | |
95 bool result = DeleteCache(path_); | |
96 DCHECK(result); | |
97 } | |
98 | |
99 ScopedTestCache::ScopedTestCache(const std::string& name) | |
100 : path_(BuildCachePath(name)) { | |
101 bool result = DeleteCache(path_); | |
102 DCHECK(result); | |
103 } | |
104 | |
105 ScopedTestCache::~ScopedTestCache() { | |
106 file_util::Delete(path(), true); | |
107 } | |
108 | |
109 // ----------------------------------------------------------------------- | 76 // ----------------------------------------------------------------------- |
110 | 77 |
111 MessageLoopHelper::MessageLoopHelper() | 78 MessageLoopHelper::MessageLoopHelper() |
112 : num_callbacks_(0), | 79 : num_callbacks_(0), |
113 num_iterations_(0), | 80 num_iterations_(0), |
114 last_(0), | 81 last_(0), |
115 completed_(false), | 82 completed_(false), |
116 callback_reused_error_(false), | 83 callback_reused_error_(false), |
117 callbacks_called_(0) { | 84 callbacks_called_(0) { |
118 } | 85 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 void CallbackTest::Run(int params) { | 134 void CallbackTest::Run(int params) { |
168 if (reuse_) { | 135 if (reuse_) { |
169 DCHECK_EQ(1, reuse_); | 136 DCHECK_EQ(1, reuse_); |
170 if (2 == reuse_) | 137 if (2 == reuse_) |
171 helper_->set_callback_reused_error(true); | 138 helper_->set_callback_reused_error(true); |
172 reuse_++; | 139 reuse_++; |
173 } | 140 } |
174 | 141 |
175 helper_->CallbackWasCalled(); | 142 helper_->CallbackWasCalled(); |
176 } | 143 } |
OLD | NEW |