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" |
11 | 11 |
12 using base::Time; | 12 using base::Time; |
13 | 13 |
| 14 namespace { |
| 15 |
| 16 // Returns the number of files in this folder. |
| 17 int NumberOfFiles(const std::wstring path) { |
| 18 file_util::FileEnumerator iter(FilePath::FromWStringHack(path), false, |
| 19 file_util::FileEnumerator::FILES); |
| 20 int count = 0; |
| 21 for (FilePath file = iter.Next(); !file.value().empty(); file = iter.Next()) { |
| 22 count++; |
| 23 } |
| 24 return count; |
| 25 } |
| 26 |
| 27 } // namespace; |
| 28 |
14 TEST_F(DiskCacheTest, BlockFiles_Grow) { | 29 TEST_F(DiskCacheTest, BlockFiles_Grow) { |
15 std::wstring path = GetCachePath(); | 30 std::wstring path = GetCachePath(); |
16 ASSERT_TRUE(DeleteCache(path.c_str())); | 31 ASSERT_TRUE(DeleteCache(path.c_str())); |
17 ASSERT_TRUE(file_util::CreateDirectory(path)); | 32 ASSERT_TRUE(file_util::CreateDirectory(path)); |
18 | 33 |
19 disk_cache::BlockFiles files(path); | 34 disk_cache::BlockFiles files(path); |
20 ASSERT_TRUE(files.Init(true)); | 35 ASSERT_TRUE(files.Init(true)); |
21 | 36 |
| 37 const int kMaxSize = 35000; |
| 38 disk_cache::Addr address[kMaxSize]; |
| 39 |
22 // Fill up the 32-byte block file (use three files). | 40 // Fill up the 32-byte block file (use three files). |
23 for (int i = 0; i < 35000; i++) { | 41 for (int i = 0; i < kMaxSize; i++) { |
24 disk_cache::Addr address(0); | 42 EXPECT_TRUE(files.CreateBlock(disk_cache::RANKINGS, 4, &address[i])); |
25 EXPECT_TRUE(files.CreateBlock(disk_cache::RANKINGS, 4, &address)); | |
26 } | 43 } |
| 44 EXPECT_EQ(6, NumberOfFiles(path)); |
| 45 |
| 46 // Make sure we don't keep adding files. |
| 47 for (int i = 0; i < kMaxSize * 4; i += 2) { |
| 48 int target = i % kMaxSize; |
| 49 files.DeleteBlock(address[target], false); |
| 50 EXPECT_TRUE(files.CreateBlock(disk_cache::RANKINGS, 4, &address[target])); |
| 51 } |
| 52 EXPECT_EQ(6, NumberOfFiles(path)); |
| 53 } |
| 54 |
| 55 // We should be able to delete empty block files. |
| 56 TEST_F(DiskCacheTest, BlockFiles_Shrink) { |
| 57 std::wstring path = GetCachePath(); |
| 58 ASSERT_TRUE(DeleteCache(path.c_str())); |
| 59 ASSERT_TRUE(file_util::CreateDirectory(path)); |
| 60 |
| 61 disk_cache::BlockFiles files(path); |
| 62 ASSERT_TRUE(files.Init(true)); |
| 63 |
| 64 const int kMaxSize = 35000; |
| 65 disk_cache::Addr address[kMaxSize]; |
| 66 |
| 67 // Fill up the 32-byte block file (use three files). |
| 68 for (int i = 0; i < kMaxSize; i++) { |
| 69 EXPECT_TRUE(files.CreateBlock(disk_cache::RANKINGS, 4, &address[i])); |
| 70 } |
| 71 |
| 72 // Now delete all the blocks, so that we can delete the two extra files. |
| 73 for (int i = 0; i < kMaxSize; i++) { |
| 74 files.DeleteBlock(address[i], false); |
| 75 } |
| 76 EXPECT_EQ(4, NumberOfFiles(path)); |
27 } | 77 } |
28 | 78 |
29 // Handling of block files not properly closed. | 79 // Handling of block files not properly closed. |
30 TEST_F(DiskCacheTest, BlockFiles_Recover) { | 80 TEST_F(DiskCacheTest, BlockFiles_Recover) { |
31 std::wstring path = GetCachePath(); | 81 std::wstring path = GetCachePath(); |
32 ASSERT_TRUE(DeleteCache(path.c_str())); | 82 ASSERT_TRUE(DeleteCache(path.c_str())); |
33 ASSERT_TRUE(file_util::CreateDirectory(path)); | 83 ASSERT_TRUE(file_util::CreateDirectory(path)); |
34 | 84 |
35 disk_cache::BlockFiles files(path); | 85 disk_cache::BlockFiles files(path); |
36 ASSERT_TRUE(files.Init(true)); | 86 ASSERT_TRUE(files.Init(true)); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 ASSERT_TRUE(NULL != header); | 146 ASSERT_TRUE(NULL != header); |
97 | 147 |
98 ASSERT_EQ(0, header->updating); | 148 ASSERT_EQ(0, header->updating); |
99 | 149 |
100 EXPECT_EQ(max_entries, header->max_entries); | 150 EXPECT_EQ(max_entries, header->max_entries); |
101 EXPECT_EQ(empty_1, header->empty[0]); | 151 EXPECT_EQ(empty_1, header->empty[0]); |
102 EXPECT_EQ(empty_2, header->empty[1]); | 152 EXPECT_EQ(empty_2, header->empty[1]); |
103 EXPECT_EQ(empty_3, header->empty[2]); | 153 EXPECT_EQ(empty_3, header->empty[2]); |
104 EXPECT_EQ(empty_4, header->empty[3]); | 154 EXPECT_EQ(empty_4, header->empty[3]); |
105 } | 155 } |
OLD | NEW |