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 "base/file_util.h" | 5 #include "base/file_util.h" |
6 #include "net/disk_cache/block_files.h" | 6 #include "net/disk_cache/block_files.h" |
7 #include "net/disk_cache/disk_cache.h" | 7 #include "net/disk_cache/disk_cache.h" |
8 #include "net/disk_cache/disk_cache_test_base.h" | 8 #include "net/disk_cache/disk_cache_test_base.h" |
9 #include "net/disk_cache/disk_cache_test_util.h" | 9 #include "net/disk_cache/disk_cache_test_util.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 count++; | 21 count++; |
22 } | 22 } |
23 return count; | 23 return count; |
24 } | 24 } |
25 | 25 |
26 } // namespace; | 26 } // namespace; |
27 | 27 |
28 namespace disk_cache { | 28 namespace disk_cache { |
29 | 29 |
30 TEST_F(DiskCacheTest, BlockFiles_Grow) { | 30 TEST_F(DiskCacheTest, BlockFiles_Grow) { |
31 FilePath path = GetCacheFilePath(); | 31 ASSERT_TRUE(CleanupCacheDir()); |
32 ASSERT_TRUE(DeleteCache(path)); | 32 ASSERT_TRUE(file_util::CreateDirectory(cache_path_)); |
33 ASSERT_TRUE(file_util::CreateDirectory(path)); | |
34 | 33 |
35 BlockFiles files(path); | 34 BlockFiles files(cache_path_); |
36 ASSERT_TRUE(files.Init(true)); | 35 ASSERT_TRUE(files.Init(true)); |
37 | 36 |
38 const int kMaxSize = 35000; | 37 const int kMaxSize = 35000; |
39 Addr address[kMaxSize]; | 38 Addr address[kMaxSize]; |
40 | 39 |
41 // Fill up the 32-byte block file (use three files). | 40 // Fill up the 32-byte block file (use three files). |
42 for (int i = 0; i < kMaxSize; i++) { | 41 for (int i = 0; i < kMaxSize; i++) { |
43 EXPECT_TRUE(files.CreateBlock(RANKINGS, 4, &address[i])); | 42 EXPECT_TRUE(files.CreateBlock(RANKINGS, 4, &address[i])); |
44 } | 43 } |
45 EXPECT_EQ(6, NumberOfFiles(path)); | 44 EXPECT_EQ(6, NumberOfFiles(cache_path_)); |
46 | 45 |
47 // Make sure we don't keep adding files. | 46 // Make sure we don't keep adding files. |
48 for (int i = 0; i < kMaxSize * 4; i += 2) { | 47 for (int i = 0; i < kMaxSize * 4; i += 2) { |
49 int target = i % kMaxSize; | 48 int target = i % kMaxSize; |
50 files.DeleteBlock(address[target], false); | 49 files.DeleteBlock(address[target], false); |
51 EXPECT_TRUE(files.CreateBlock(RANKINGS, 4, &address[target])); | 50 EXPECT_TRUE(files.CreateBlock(RANKINGS, 4, &address[target])); |
52 } | 51 } |
53 EXPECT_EQ(6, NumberOfFiles(path)); | 52 EXPECT_EQ(6, NumberOfFiles(cache_path_)); |
54 } | 53 } |
55 | 54 |
56 // We should be able to delete empty block files. | 55 // We should be able to delete empty block files. |
57 TEST_F(DiskCacheTest, BlockFiles_Shrink) { | 56 TEST_F(DiskCacheTest, BlockFiles_Shrink) { |
58 FilePath path = GetCacheFilePath(); | 57 ASSERT_TRUE(CleanupCacheDir()); |
59 ASSERT_TRUE(DeleteCache(path)); | 58 ASSERT_TRUE(file_util::CreateDirectory(cache_path_)); |
60 ASSERT_TRUE(file_util::CreateDirectory(path)); | |
61 | 59 |
62 BlockFiles files(path); | 60 BlockFiles files(cache_path_); |
63 ASSERT_TRUE(files.Init(true)); | 61 ASSERT_TRUE(files.Init(true)); |
64 | 62 |
65 const int kMaxSize = 35000; | 63 const int kMaxSize = 35000; |
66 Addr address[kMaxSize]; | 64 Addr address[kMaxSize]; |
67 | 65 |
68 // Fill up the 32-byte block file (use three files). | 66 // Fill up the 32-byte block file (use three files). |
69 for (int i = 0; i < kMaxSize; i++) { | 67 for (int i = 0; i < kMaxSize; i++) { |
70 EXPECT_TRUE(files.CreateBlock(RANKINGS, 4, &address[i])); | 68 EXPECT_TRUE(files.CreateBlock(RANKINGS, 4, &address[i])); |
71 } | 69 } |
72 | 70 |
73 // Now delete all the blocks, so that we can delete the two extra files. | 71 // Now delete all the blocks, so that we can delete the two extra files. |
74 for (int i = 0; i < kMaxSize; i++) { | 72 for (int i = 0; i < kMaxSize; i++) { |
75 files.DeleteBlock(address[i], false); | 73 files.DeleteBlock(address[i], false); |
76 } | 74 } |
77 EXPECT_EQ(4, NumberOfFiles(path)); | 75 EXPECT_EQ(4, NumberOfFiles(cache_path_)); |
78 } | 76 } |
79 | 77 |
80 // Handling of block files not properly closed. | 78 // Handling of block files not properly closed. |
81 TEST_F(DiskCacheTest, BlockFiles_Recover) { | 79 TEST_F(DiskCacheTest, BlockFiles_Recover) { |
82 FilePath path = GetCacheFilePath(); | 80 ASSERT_TRUE(CleanupCacheDir()); |
83 ASSERT_TRUE(DeleteCache(path)); | 81 ASSERT_TRUE(file_util::CreateDirectory(cache_path_)); |
84 ASSERT_TRUE(file_util::CreateDirectory(path)); | |
85 | 82 |
86 BlockFiles files(path); | 83 BlockFiles files(cache_path_); |
87 ASSERT_TRUE(files.Init(true)); | 84 ASSERT_TRUE(files.Init(true)); |
88 | 85 |
89 const int kNumEntries = 2000; | 86 const int kNumEntries = 2000; |
90 CacheAddr entries[kNumEntries]; | 87 CacheAddr entries[kNumEntries]; |
91 | 88 |
92 int seed = static_cast<int>(Time::Now().ToInternalValue()); | 89 int seed = static_cast<int>(Time::Now().ToInternalValue()); |
93 srand(seed); | 90 srand(seed); |
94 for (int i = 0; i < kNumEntries; i++) { | 91 for (int i = 0; i < kNumEntries; i++) { |
95 Addr address(0); | 92 Addr address(0); |
96 int size = (rand() % 4) + 1; | 93 int size = (rand() % 4) + 1; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 | 147 |
151 EXPECT_EQ(max_entries, header->max_entries); | 148 EXPECT_EQ(max_entries, header->max_entries); |
152 EXPECT_EQ(empty_1, header->empty[0]); | 149 EXPECT_EQ(empty_1, header->empty[0]); |
153 EXPECT_EQ(empty_2, header->empty[1]); | 150 EXPECT_EQ(empty_2, header->empty[1]); |
154 EXPECT_EQ(empty_3, header->empty[2]); | 151 EXPECT_EQ(empty_3, header->empty[2]); |
155 EXPECT_EQ(empty_4, header->empty[3]); | 152 EXPECT_EQ(empty_4, header->empty[3]); |
156 } | 153 } |
157 | 154 |
158 // Handling of truncated files. | 155 // Handling of truncated files. |
159 TEST_F(DiskCacheTest, BlockFiles_ZeroSizeFile) { | 156 TEST_F(DiskCacheTest, BlockFiles_ZeroSizeFile) { |
160 FilePath path = GetCacheFilePath(); | 157 ASSERT_TRUE(CleanupCacheDir()); |
161 ASSERT_TRUE(DeleteCache(path)); | 158 ASSERT_TRUE(file_util::CreateDirectory(cache_path_)); |
162 ASSERT_TRUE(file_util::CreateDirectory(path)); | |
163 | 159 |
164 BlockFiles files(path); | 160 BlockFiles files(cache_path_); |
165 ASSERT_TRUE(files.Init(true)); | 161 ASSERT_TRUE(files.Init(true)); |
166 | 162 |
167 FilePath filename = files.Name(0); | 163 FilePath filename = files.Name(0); |
168 files.CloseFiles(); | 164 files.CloseFiles(); |
169 // Truncate one of the files. | 165 // Truncate one of the files. |
170 { | 166 { |
171 scoped_refptr<File> file(new File); | 167 scoped_refptr<File> file(new File); |
172 ASSERT_TRUE(file->Init(filename)); | 168 ASSERT_TRUE(file->Init(filename)); |
173 EXPECT_TRUE(file->SetLength(0)); | 169 EXPECT_TRUE(file->SetLength(0)); |
174 } | 170 } |
175 | 171 |
176 // Initializing should fail, not crash. | 172 // Initializing should fail, not crash. |
177 ASSERT_FALSE(files.Init(false)); | 173 ASSERT_FALSE(files.Init(false)); |
178 } | 174 } |
179 | 175 |
180 // Handling of truncated files (non empty). | 176 // Handling of truncated files (non empty). |
181 TEST_F(DiskCacheTest, BlockFiles_TruncatedFile) { | 177 TEST_F(DiskCacheTest, BlockFiles_TruncatedFile) { |
182 FilePath path = GetCacheFilePath(); | 178 ASSERT_TRUE(CleanupCacheDir()); |
183 ASSERT_TRUE(DeleteCache(path)); | 179 ASSERT_TRUE(file_util::CreateDirectory(cache_path_)); |
184 ASSERT_TRUE(file_util::CreateDirectory(path)); | |
185 | 180 |
186 BlockFiles files(path); | 181 BlockFiles files(cache_path_); |
187 ASSERT_TRUE(files.Init(true)); | 182 ASSERT_TRUE(files.Init(true)); |
188 Addr address; | 183 Addr address; |
189 EXPECT_TRUE(files.CreateBlock(RANKINGS, 2, &address)); | 184 EXPECT_TRUE(files.CreateBlock(RANKINGS, 2, &address)); |
190 | 185 |
191 FilePath filename = files.Name(0); | 186 FilePath filename = files.Name(0); |
192 files.CloseFiles(); | 187 files.CloseFiles(); |
193 // Truncate one of the files. | 188 // Truncate one of the files. |
194 { | 189 { |
195 scoped_refptr<File> file(new File); | 190 scoped_refptr<File> file(new File); |
196 ASSERT_TRUE(file->Init(filename)); | 191 ASSERT_TRUE(file->Init(filename)); |
197 EXPECT_TRUE(file->SetLength(15000)); | 192 EXPECT_TRUE(file->SetLength(15000)); |
198 } | 193 } |
199 | 194 |
200 // Initializing should fail, not crash. | 195 // Initializing should fail, not crash. |
201 ASSERT_FALSE(files.Init(false)); | 196 ASSERT_FALSE(files.Init(false)); |
202 } | 197 } |
203 | 198 |
204 // An invalid file can be detected after init. | 199 // An invalid file can be detected after init. |
205 TEST_F(DiskCacheTest, BlockFiles_InvalidFile) { | 200 TEST_F(DiskCacheTest, BlockFiles_InvalidFile) { |
206 FilePath path = GetCacheFilePath(); | 201 ASSERT_TRUE(CleanupCacheDir()); |
207 ASSERT_TRUE(DeleteCache(path)); | 202 ASSERT_TRUE(file_util::CreateDirectory(cache_path_)); |
208 ASSERT_TRUE(file_util::CreateDirectory(path)); | |
209 | 203 |
210 BlockFiles files(path); | 204 BlockFiles files(cache_path_); |
211 ASSERT_TRUE(files.Init(true)); | 205 ASSERT_TRUE(files.Init(true)); |
212 | 206 |
213 // Let's access block 10 of file 5. (There is no file). | 207 // Let's access block 10 of file 5. (There is no file). |
214 Addr addr(BLOCK_256, 1, 5, 10); | 208 Addr addr(BLOCK_256, 1, 5, 10); |
215 EXPECT_TRUE(NULL == files.GetFile(addr)); | 209 EXPECT_TRUE(NULL == files.GetFile(addr)); |
216 | 210 |
217 // Let's create an invalid file. | 211 // Let's create an invalid file. |
218 FilePath filename(files.Name(5)); | 212 FilePath filename(files.Name(5)); |
219 char header[kBlockHeaderSize]; | 213 char header[kBlockHeaderSize]; |
220 memset(header, 'a', kBlockHeaderSize); | 214 memset(header, 'a', kBlockHeaderSize); |
221 EXPECT_EQ(kBlockHeaderSize, | 215 EXPECT_EQ(kBlockHeaderSize, |
222 file_util::WriteFile(filename, header, kBlockHeaderSize)); | 216 file_util::WriteFile(filename, header, kBlockHeaderSize)); |
223 | 217 |
224 EXPECT_TRUE(NULL == files.GetFile(addr)); | 218 EXPECT_TRUE(NULL == files.GetFile(addr)); |
225 | 219 |
226 // The file should not have been cached (it is still invalid). | 220 // The file should not have been cached (it is still invalid). |
227 EXPECT_TRUE(NULL == files.GetFile(addr)); | 221 EXPECT_TRUE(NULL == files.GetFile(addr)); |
228 } | 222 } |
229 | 223 |
230 // Tests that we generate the correct file stats. | 224 // Tests that we generate the correct file stats. |
231 TEST_F(DiskCacheTest, BlockFiles_Stats) { | 225 TEST_F(DiskCacheTest, BlockFiles_Stats) { |
232 ASSERT_TRUE(CopyTestCache("remove_load1")); | 226 ASSERT_TRUE(CopyTestCache("remove_load1")); |
233 FilePath path = GetCacheFilePath(); | |
234 | 227 |
235 BlockFiles files(path); | 228 BlockFiles files(cache_path_); |
236 ASSERT_TRUE(files.Init(false)); | 229 ASSERT_TRUE(files.Init(false)); |
237 int used, load; | 230 int used, load; |
238 | 231 |
239 files.GetFileStats(0, &used, &load); | 232 files.GetFileStats(0, &used, &load); |
240 EXPECT_EQ(101, used); | 233 EXPECT_EQ(101, used); |
241 EXPECT_EQ(9, load); | 234 EXPECT_EQ(9, load); |
242 | 235 |
243 files.GetFileStats(1, &used, &load); | 236 files.GetFileStats(1, &used, &load); |
244 EXPECT_EQ(203, used); | 237 EXPECT_EQ(203, used); |
245 EXPECT_EQ(19, load); | 238 EXPECT_EQ(19, load); |
246 | 239 |
247 files.GetFileStats(2, &used, &load); | 240 files.GetFileStats(2, &used, &load); |
248 EXPECT_EQ(0, used); | 241 EXPECT_EQ(0, used); |
249 EXPECT_EQ(0, load); | 242 EXPECT_EQ(0, load); |
250 } | 243 } |
251 | 244 |
252 // Tests that we add and remove blocks correctly. | 245 // Tests that we add and remove blocks correctly. |
253 TEST_F(DiskCacheTest, AllocationMap) { | 246 TEST_F(DiskCacheTest, AllocationMap) { |
254 FilePath path = GetCacheFilePath(); | 247 ASSERT_TRUE(CleanupCacheDir()); |
255 ASSERT_TRUE(DeleteCache(path)); | 248 ASSERT_TRUE(file_util::CreateDirectory(cache_path_)); |
256 ASSERT_TRUE(file_util::CreateDirectory(path)); | |
257 | 249 |
258 BlockFiles files(path); | 250 BlockFiles files(cache_path_); |
259 ASSERT_TRUE(files.Init(true)); | 251 ASSERT_TRUE(files.Init(true)); |
260 | 252 |
261 // Create a bunch of entries. | 253 // Create a bunch of entries. |
262 const int kSize = 100; | 254 const int kSize = 100; |
263 Addr address[kSize]; | 255 Addr address[kSize]; |
264 for (int i = 0; i < kSize; i++) { | 256 for (int i = 0; i < kSize; i++) { |
265 SCOPED_TRACE(i); | 257 SCOPED_TRACE(i); |
266 int block_size = i % 4 + 1; | 258 int block_size = i % 4 + 1; |
267 EXPECT_TRUE(files.CreateBlock(BLOCK_1K, block_size, &address[i])); | 259 EXPECT_TRUE(files.CreateBlock(BLOCK_1K, block_size, &address[i])); |
268 EXPECT_EQ(BLOCK_1K, address[i].file_type()); | 260 EXPECT_EQ(BLOCK_1K, address[i].file_type()); |
(...skipping 23 matching lines...) Expand all Loading... |
292 } | 284 } |
293 | 285 |
294 // The allocation map should be empty. | 286 // The allocation map should be empty. |
295 for (int i =0; i < 50; i++) { | 287 for (int i =0; i < 50; i++) { |
296 SCOPED_TRACE(i); | 288 SCOPED_TRACE(i); |
297 EXPECT_EQ(0, buffer[i]); | 289 EXPECT_EQ(0, buffer[i]); |
298 } | 290 } |
299 } | 291 } |
300 | 292 |
301 } // namespace disk_cache | 293 } // namespace disk_cache |
OLD | NEW |