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