OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/files/file_enumerator.h" | 6 #include "base/files/file_enumerator.h" |
7 #include "net/disk_cache/block_files.h" | 7 #include "net/disk_cache/block_files.h" |
8 #include "net/disk_cache/disk_cache.h" | 8 #include "net/disk_cache/disk_cache.h" |
9 #include "net/disk_cache/disk_cache_test_base.h" | 9 #include "net/disk_cache/disk_cache_test_base.h" |
10 #include "net/disk_cache/disk_cache_test_util.h" | 10 #include "net/disk_cache/disk_cache_test_util.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 } | 24 } |
25 return count; | 25 return count; |
26 } | 26 } |
27 | 27 |
28 } // namespace; | 28 } // namespace; |
29 | 29 |
30 namespace disk_cache { | 30 namespace disk_cache { |
31 | 31 |
32 TEST_F(DiskCacheTest, BlockFiles_Grow) { | 32 TEST_F(DiskCacheTest, BlockFiles_Grow) { |
33 ASSERT_TRUE(CleanupCacheDir()); | 33 ASSERT_TRUE(CleanupCacheDir()); |
34 ASSERT_TRUE(file_util::CreateDirectory(cache_path_)); | 34 ASSERT_TRUE(base::CreateDirectory(cache_path_)); |
35 | 35 |
36 BlockFiles files(cache_path_); | 36 BlockFiles files(cache_path_); |
37 ASSERT_TRUE(files.Init(true)); | 37 ASSERT_TRUE(files.Init(true)); |
38 | 38 |
39 const int kMaxSize = 35000; | 39 const int kMaxSize = 35000; |
40 Addr address[kMaxSize]; | 40 Addr address[kMaxSize]; |
41 | 41 |
42 // Fill up the 32-byte block file (use three files). | 42 // Fill up the 32-byte block file (use three files). |
43 for (int i = 0; i < kMaxSize; i++) { | 43 for (int i = 0; i < kMaxSize; i++) { |
44 EXPECT_TRUE(files.CreateBlock(RANKINGS, 4, &address[i])); | 44 EXPECT_TRUE(files.CreateBlock(RANKINGS, 4, &address[i])); |
45 } | 45 } |
46 EXPECT_EQ(6, NumberOfFiles(cache_path_)); | 46 EXPECT_EQ(6, NumberOfFiles(cache_path_)); |
47 | 47 |
48 // Make sure we don't keep adding files. | 48 // Make sure we don't keep adding files. |
49 for (int i = 0; i < kMaxSize * 4; i += 2) { | 49 for (int i = 0; i < kMaxSize * 4; i += 2) { |
50 int target = i % kMaxSize; | 50 int target = i % kMaxSize; |
51 files.DeleteBlock(address[target], false); | 51 files.DeleteBlock(address[target], false); |
52 EXPECT_TRUE(files.CreateBlock(RANKINGS, 4, &address[target])); | 52 EXPECT_TRUE(files.CreateBlock(RANKINGS, 4, &address[target])); |
53 } | 53 } |
54 EXPECT_EQ(6, NumberOfFiles(cache_path_)); | 54 EXPECT_EQ(6, NumberOfFiles(cache_path_)); |
55 } | 55 } |
56 | 56 |
57 // We should be able to delete empty block files. | 57 // We should be able to delete empty block files. |
58 TEST_F(DiskCacheTest, BlockFiles_Shrink) { | 58 TEST_F(DiskCacheTest, BlockFiles_Shrink) { |
59 ASSERT_TRUE(CleanupCacheDir()); | 59 ASSERT_TRUE(CleanupCacheDir()); |
60 ASSERT_TRUE(file_util::CreateDirectory(cache_path_)); | 60 ASSERT_TRUE(base::CreateDirectory(cache_path_)); |
61 | 61 |
62 BlockFiles files(cache_path_); | 62 BlockFiles files(cache_path_); |
63 ASSERT_TRUE(files.Init(true)); | 63 ASSERT_TRUE(files.Init(true)); |
64 | 64 |
65 const int kMaxSize = 35000; | 65 const int kMaxSize = 35000; |
66 Addr address[kMaxSize]; | 66 Addr address[kMaxSize]; |
67 | 67 |
68 // Fill up the 32-byte block file (use three files). | 68 // Fill up the 32-byte block file (use three files). |
69 for (int i = 0; i < kMaxSize; i++) { | 69 for (int i = 0; i < kMaxSize; i++) { |
70 EXPECT_TRUE(files.CreateBlock(RANKINGS, 4, &address[i])); | 70 EXPECT_TRUE(files.CreateBlock(RANKINGS, 4, &address[i])); |
71 } | 71 } |
72 | 72 |
73 // Now delete all the blocks, so that we can delete the two extra files. | 73 // Now delete all the blocks, so that we can delete the two extra files. |
74 for (int i = 0; i < kMaxSize; i++) { | 74 for (int i = 0; i < kMaxSize; i++) { |
75 files.DeleteBlock(address[i], false); | 75 files.DeleteBlock(address[i], false); |
76 } | 76 } |
77 EXPECT_EQ(4, NumberOfFiles(cache_path_)); | 77 EXPECT_EQ(4, NumberOfFiles(cache_path_)); |
78 } | 78 } |
79 | 79 |
80 // Handling of block files not properly closed. | 80 // Handling of block files not properly closed. |
81 TEST_F(DiskCacheTest, BlockFiles_Recover) { | 81 TEST_F(DiskCacheTest, BlockFiles_Recover) { |
82 ASSERT_TRUE(CleanupCacheDir()); | 82 ASSERT_TRUE(CleanupCacheDir()); |
83 ASSERT_TRUE(file_util::CreateDirectory(cache_path_)); | 83 ASSERT_TRUE(base::CreateDirectory(cache_path_)); |
84 | 84 |
85 BlockFiles files(cache_path_); | 85 BlockFiles files(cache_path_); |
86 ASSERT_TRUE(files.Init(true)); | 86 ASSERT_TRUE(files.Init(true)); |
87 | 87 |
88 const int kNumEntries = 2000; | 88 const int kNumEntries = 2000; |
89 CacheAddr entries[kNumEntries]; | 89 CacheAddr entries[kNumEntries]; |
90 | 90 |
91 int seed = static_cast<int>(Time::Now().ToInternalValue()); | 91 int seed = static_cast<int>(Time::Now().ToInternalValue()); |
92 srand(seed); | 92 srand(seed); |
93 for (int i = 0; i < kNumEntries; i++) { | 93 for (int i = 0; i < kNumEntries; i++) { |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 EXPECT_EQ(max_entries, header->max_entries); | 150 EXPECT_EQ(max_entries, header->max_entries); |
151 EXPECT_EQ(empty_1, header->empty[0]); | 151 EXPECT_EQ(empty_1, header->empty[0]); |
152 EXPECT_EQ(empty_2, header->empty[1]); | 152 EXPECT_EQ(empty_2, header->empty[1]); |
153 EXPECT_EQ(empty_3, header->empty[2]); | 153 EXPECT_EQ(empty_3, header->empty[2]); |
154 EXPECT_EQ(empty_4, header->empty[3]); | 154 EXPECT_EQ(empty_4, header->empty[3]); |
155 } | 155 } |
156 | 156 |
157 // Handling of truncated files. | 157 // Handling of truncated files. |
158 TEST_F(DiskCacheTest, BlockFiles_ZeroSizeFile) { | 158 TEST_F(DiskCacheTest, BlockFiles_ZeroSizeFile) { |
159 ASSERT_TRUE(CleanupCacheDir()); | 159 ASSERT_TRUE(CleanupCacheDir()); |
160 ASSERT_TRUE(file_util::CreateDirectory(cache_path_)); | 160 ASSERT_TRUE(base::CreateDirectory(cache_path_)); |
161 | 161 |
162 BlockFiles files(cache_path_); | 162 BlockFiles files(cache_path_); |
163 ASSERT_TRUE(files.Init(true)); | 163 ASSERT_TRUE(files.Init(true)); |
164 | 164 |
165 base::FilePath filename = files.Name(0); | 165 base::FilePath filename = files.Name(0); |
166 files.CloseFiles(); | 166 files.CloseFiles(); |
167 // Truncate one of the files. | 167 // Truncate one of the files. |
168 { | 168 { |
169 scoped_refptr<File> file(new File); | 169 scoped_refptr<File> file(new File); |
170 ASSERT_TRUE(file->Init(filename)); | 170 ASSERT_TRUE(file->Init(filename)); |
171 EXPECT_TRUE(file->SetLength(0)); | 171 EXPECT_TRUE(file->SetLength(0)); |
172 } | 172 } |
173 | 173 |
174 // Initializing should fail, not crash. | 174 // Initializing should fail, not crash. |
175 ASSERT_FALSE(files.Init(false)); | 175 ASSERT_FALSE(files.Init(false)); |
176 } | 176 } |
177 | 177 |
178 // Handling of truncated files (non empty). | 178 // Handling of truncated files (non empty). |
179 TEST_F(DiskCacheTest, BlockFiles_TruncatedFile) { | 179 TEST_F(DiskCacheTest, BlockFiles_TruncatedFile) { |
180 ASSERT_TRUE(CleanupCacheDir()); | 180 ASSERT_TRUE(CleanupCacheDir()); |
181 ASSERT_TRUE(file_util::CreateDirectory(cache_path_)); | 181 ASSERT_TRUE(base::CreateDirectory(cache_path_)); |
182 | 182 |
183 BlockFiles files(cache_path_); | 183 BlockFiles files(cache_path_); |
184 ASSERT_TRUE(files.Init(true)); | 184 ASSERT_TRUE(files.Init(true)); |
185 Addr address; | 185 Addr address; |
186 EXPECT_TRUE(files.CreateBlock(RANKINGS, 2, &address)); | 186 EXPECT_TRUE(files.CreateBlock(RANKINGS, 2, &address)); |
187 | 187 |
188 base::FilePath filename = files.Name(0); | 188 base::FilePath filename = files.Name(0); |
189 files.CloseFiles(); | 189 files.CloseFiles(); |
190 // Truncate one of the files. | 190 // Truncate one of the files. |
191 { | 191 { |
192 scoped_refptr<File> file(new File); | 192 scoped_refptr<File> file(new File); |
193 ASSERT_TRUE(file->Init(filename)); | 193 ASSERT_TRUE(file->Init(filename)); |
194 EXPECT_TRUE(file->SetLength(15000)); | 194 EXPECT_TRUE(file->SetLength(15000)); |
195 } | 195 } |
196 | 196 |
197 // Initializing should fail, not crash. | 197 // Initializing should fail, not crash. |
198 ASSERT_FALSE(files.Init(false)); | 198 ASSERT_FALSE(files.Init(false)); |
199 } | 199 } |
200 | 200 |
201 // Tests detection of out of sync counters. | 201 // Tests detection of out of sync counters. |
202 TEST_F(DiskCacheTest, BlockFiles_Counters) { | 202 TEST_F(DiskCacheTest, BlockFiles_Counters) { |
203 ASSERT_TRUE(CleanupCacheDir()); | 203 ASSERT_TRUE(CleanupCacheDir()); |
204 ASSERT_TRUE(file_util::CreateDirectory(cache_path_)); | 204 ASSERT_TRUE(base::CreateDirectory(cache_path_)); |
205 | 205 |
206 BlockFiles files(cache_path_); | 206 BlockFiles files(cache_path_); |
207 ASSERT_TRUE(files.Init(true)); | 207 ASSERT_TRUE(files.Init(true)); |
208 | 208 |
209 // Create a block of size 2. | 209 // Create a block of size 2. |
210 Addr address(0); | 210 Addr address(0); |
211 EXPECT_TRUE(files.CreateBlock(RANKINGS, 2, &address)); | 211 EXPECT_TRUE(files.CreateBlock(RANKINGS, 2, &address)); |
212 | 212 |
213 MappedFile* file = files.GetFile(address); | 213 MappedFile* file = files.GetFile(address); |
214 ASSERT_TRUE(NULL != file); | 214 ASSERT_TRUE(NULL != file); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 header->updating = 1; | 249 header->updating = 1; |
250 files.CloseFiles(); | 250 files.CloseFiles(); |
251 | 251 |
252 // Detect the error. | 252 // Detect the error. |
253 ASSERT_FALSE(files.Init(false)); | 253 ASSERT_FALSE(files.Init(false)); |
254 } | 254 } |
255 | 255 |
256 // An invalid file can be detected after init. | 256 // An invalid file can be detected after init. |
257 TEST_F(DiskCacheTest, BlockFiles_InvalidFile) { | 257 TEST_F(DiskCacheTest, BlockFiles_InvalidFile) { |
258 ASSERT_TRUE(CleanupCacheDir()); | 258 ASSERT_TRUE(CleanupCacheDir()); |
259 ASSERT_TRUE(file_util::CreateDirectory(cache_path_)); | 259 ASSERT_TRUE(base::CreateDirectory(cache_path_)); |
260 | 260 |
261 BlockFiles files(cache_path_); | 261 BlockFiles files(cache_path_); |
262 ASSERT_TRUE(files.Init(true)); | 262 ASSERT_TRUE(files.Init(true)); |
263 | 263 |
264 // Let's access block 10 of file 5. (There is no file). | 264 // Let's access block 10 of file 5. (There is no file). |
265 Addr addr(BLOCK_256, 1, 5, 10); | 265 Addr addr(BLOCK_256, 1, 5, 10); |
266 EXPECT_TRUE(NULL == files.GetFile(addr)); | 266 EXPECT_TRUE(NULL == files.GetFile(addr)); |
267 | 267 |
268 // Let's create an invalid file. | 268 // Let's create an invalid file. |
269 base::FilePath filename(files.Name(5)); | 269 base::FilePath filename(files.Name(5)); |
(...skipping 25 matching lines...) Expand all Loading... |
295 EXPECT_EQ(19, load); | 295 EXPECT_EQ(19, load); |
296 | 296 |
297 files.GetFileStats(2, &used, &load); | 297 files.GetFileStats(2, &used, &load); |
298 EXPECT_EQ(0, used); | 298 EXPECT_EQ(0, used); |
299 EXPECT_EQ(0, load); | 299 EXPECT_EQ(0, load); |
300 } | 300 } |
301 | 301 |
302 // Tests that we add and remove blocks correctly. | 302 // Tests that we add and remove blocks correctly. |
303 TEST_F(DiskCacheTest, AllocationMap) { | 303 TEST_F(DiskCacheTest, AllocationMap) { |
304 ASSERT_TRUE(CleanupCacheDir()); | 304 ASSERT_TRUE(CleanupCacheDir()); |
305 ASSERT_TRUE(file_util::CreateDirectory(cache_path_)); | 305 ASSERT_TRUE(base::CreateDirectory(cache_path_)); |
306 | 306 |
307 BlockFiles files(cache_path_); | 307 BlockFiles files(cache_path_); |
308 ASSERT_TRUE(files.Init(true)); | 308 ASSERT_TRUE(files.Init(true)); |
309 | 309 |
310 // Create a bunch of entries. | 310 // Create a bunch of entries. |
311 const int kSize = 100; | 311 const int kSize = 100; |
312 Addr address[kSize]; | 312 Addr address[kSize]; |
313 for (int i = 0; i < kSize; i++) { | 313 for (int i = 0; i < kSize; i++) { |
314 SCOPED_TRACE(i); | 314 SCOPED_TRACE(i); |
315 int block_size = i % 4 + 1; | 315 int block_size = i % 4 + 1; |
(...skipping 25 matching lines...) Expand all Loading... |
341 } | 341 } |
342 | 342 |
343 // The allocation map should be empty. | 343 // The allocation map should be empty. |
344 for (int i =0; i < 50; i++) { | 344 for (int i =0; i < 50; i++) { |
345 SCOPED_TRACE(i); | 345 SCOPED_TRACE(i); |
346 EXPECT_EQ(0, buffer[i]); | 346 EXPECT_EQ(0, buffer[i]); |
347 } | 347 } |
348 } | 348 } |
349 | 349 |
350 } // namespace disk_cache | 350 } // namespace disk_cache |
OLD | NEW |