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