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 { | 14 namespace { |
15 | 15 |
16 // Returns the number of files in this folder. | 16 // Returns the number of files in this folder. |
17 int NumberOfFiles(const std::wstring& path) { | 17 int NumberOfFiles(const FilePath& path) { |
18 file_util::FileEnumerator iter(FilePath::FromWStringHack(path), false, | 18 file_util::FileEnumerator iter(path, false, file_util::FileEnumerator::FILES); |
19 file_util::FileEnumerator::FILES); | |
20 int count = 0; | 19 int count = 0; |
21 for (FilePath file = iter.Next(); !file.value().empty(); file = iter.Next()) { | 20 for (FilePath file = iter.Next(); !file.value().empty(); file = iter.Next()) { |
22 count++; | 21 count++; |
23 } | 22 } |
24 return count; | 23 return count; |
25 } | 24 } |
26 | 25 |
27 } // namespace; | 26 } // namespace; |
28 | 27 |
29 namespace disk_cache { | 28 namespace disk_cache { |
30 | 29 |
31 TEST_F(DiskCacheTest, BlockFiles_Grow) { | 30 TEST_F(DiskCacheTest, BlockFiles_Grow) { |
32 std::wstring path = GetCachePath(); | 31 FilePath path = FilePath::FromWStringHack(GetCachePath()); |
33 ASSERT_TRUE(DeleteCache(path.c_str())); | 32 ASSERT_TRUE(DeleteCache(path)); |
34 ASSERT_TRUE(file_util::CreateDirectory(path)); | 33 ASSERT_TRUE(file_util::CreateDirectory(path)); |
35 | 34 |
36 BlockFiles files(path); | 35 BlockFiles files(path); |
37 ASSERT_TRUE(files.Init(true)); | 36 ASSERT_TRUE(files.Init(true)); |
38 | 37 |
39 const int kMaxSize = 35000; | 38 const int kMaxSize = 35000; |
40 Addr address[kMaxSize]; | 39 Addr address[kMaxSize]; |
41 | 40 |
42 // Fill up the 32-byte block file (use three files). | 41 // Fill up the 32-byte block file (use three files). |
43 for (int i = 0; i < kMaxSize; i++) { | 42 for (int i = 0; i < kMaxSize; i++) { |
44 EXPECT_TRUE(files.CreateBlock(RANKINGS, 4, &address[i])); | 43 EXPECT_TRUE(files.CreateBlock(RANKINGS, 4, &address[i])); |
45 } | 44 } |
46 EXPECT_EQ(6, NumberOfFiles(path)); | 45 EXPECT_EQ(6, NumberOfFiles(path)); |
47 | 46 |
48 // Make sure we don't keep adding files. | 47 // Make sure we don't keep adding files. |
49 for (int i = 0; i < kMaxSize * 4; i += 2) { | 48 for (int i = 0; i < kMaxSize * 4; i += 2) { |
50 int target = i % kMaxSize; | 49 int target = i % kMaxSize; |
51 files.DeleteBlock(address[target], false); | 50 files.DeleteBlock(address[target], false); |
52 EXPECT_TRUE(files.CreateBlock(RANKINGS, 4, &address[target])); | 51 EXPECT_TRUE(files.CreateBlock(RANKINGS, 4, &address[target])); |
53 } | 52 } |
54 EXPECT_EQ(6, NumberOfFiles(path)); | 53 EXPECT_EQ(6, NumberOfFiles(path)); |
55 } | 54 } |
56 | 55 |
57 // We should be able to delete empty block files. | 56 // We should be able to delete empty block files. |
58 TEST_F(DiskCacheTest, BlockFiles_Shrink) { | 57 TEST_F(DiskCacheTest, BlockFiles_Shrink) { |
59 std::wstring path = GetCachePath(); | 58 FilePath path = FilePath::FromWStringHack(GetCachePath()); |
60 ASSERT_TRUE(DeleteCache(path.c_str())); | 59 ASSERT_TRUE(DeleteCache(path)); |
61 ASSERT_TRUE(file_util::CreateDirectory(path)); | 60 ASSERT_TRUE(file_util::CreateDirectory(path)); |
62 | 61 |
63 BlockFiles files(path); | 62 BlockFiles files(path); |
64 ASSERT_TRUE(files.Init(true)); | 63 ASSERT_TRUE(files.Init(true)); |
65 | 64 |
66 const int kMaxSize = 35000; | 65 const int kMaxSize = 35000; |
67 Addr address[kMaxSize]; | 66 Addr address[kMaxSize]; |
68 | 67 |
69 // Fill up the 32-byte block file (use three files). | 68 // Fill up the 32-byte block file (use three files). |
70 for (int i = 0; i < kMaxSize; i++) { | 69 for (int i = 0; i < kMaxSize; i++) { |
71 EXPECT_TRUE(files.CreateBlock(RANKINGS, 4, &address[i])); | 70 EXPECT_TRUE(files.CreateBlock(RANKINGS, 4, &address[i])); |
72 } | 71 } |
73 | 72 |
74 // 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. |
75 for (int i = 0; i < kMaxSize; i++) { | 74 for (int i = 0; i < kMaxSize; i++) { |
76 files.DeleteBlock(address[i], false); | 75 files.DeleteBlock(address[i], false); |
77 } | 76 } |
78 EXPECT_EQ(4, NumberOfFiles(path)); | 77 EXPECT_EQ(4, NumberOfFiles(path)); |
79 } | 78 } |
80 | 79 |
81 // Handling of block files not properly closed. | 80 // Handling of block files not properly closed. |
82 TEST_F(DiskCacheTest, BlockFiles_Recover) { | 81 TEST_F(DiskCacheTest, BlockFiles_Recover) { |
83 std::wstring path = GetCachePath(); | 82 FilePath path = FilePath::FromWStringHack(GetCachePath()); |
84 ASSERT_TRUE(DeleteCache(path.c_str())); | 83 ASSERT_TRUE(DeleteCache(path)); |
85 ASSERT_TRUE(file_util::CreateDirectory(path)); | 84 ASSERT_TRUE(file_util::CreateDirectory(path)); |
86 | 85 |
87 BlockFiles files(path); | 86 BlockFiles files(path); |
88 ASSERT_TRUE(files.Init(true)); | 87 ASSERT_TRUE(files.Init(true)); |
89 | 88 |
90 const int kNumEntries = 2000; | 89 const int kNumEntries = 2000; |
91 CacheAddr entries[kNumEntries]; | 90 CacheAddr entries[kNumEntries]; |
92 | 91 |
93 int seed = static_cast<int>(Time::Now().ToInternalValue()); | 92 int seed = static_cast<int>(Time::Now().ToInternalValue()); |
94 srand(seed); | 93 srand(seed); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 | 150 |
152 EXPECT_EQ(max_entries, header->max_entries); | 151 EXPECT_EQ(max_entries, header->max_entries); |
153 EXPECT_EQ(empty_1, header->empty[0]); | 152 EXPECT_EQ(empty_1, header->empty[0]); |
154 EXPECT_EQ(empty_2, header->empty[1]); | 153 EXPECT_EQ(empty_2, header->empty[1]); |
155 EXPECT_EQ(empty_3, header->empty[2]); | 154 EXPECT_EQ(empty_3, header->empty[2]); |
156 EXPECT_EQ(empty_4, header->empty[3]); | 155 EXPECT_EQ(empty_4, header->empty[3]); |
157 } | 156 } |
158 | 157 |
159 // Handling of truncated files. | 158 // Handling of truncated files. |
160 TEST_F(DiskCacheTest, BlockFiles_ZeroSizeFile) { | 159 TEST_F(DiskCacheTest, BlockFiles_ZeroSizeFile) { |
161 std::wstring path = GetCachePath(); | 160 FilePath path = FilePath::FromWStringHack(GetCachePath()); |
162 ASSERT_TRUE(DeleteCache(path.c_str())); | 161 ASSERT_TRUE(DeleteCache(path)); |
163 ASSERT_TRUE(file_util::CreateDirectory(path)); | 162 ASSERT_TRUE(file_util::CreateDirectory(path)); |
164 | 163 |
165 BlockFiles files(path); | 164 BlockFiles files(path); |
166 ASSERT_TRUE(files.Init(true)); | 165 ASSERT_TRUE(files.Init(true)); |
167 | 166 |
168 std::wstring filename = files.Name(0); | 167 FilePath filename = files.Name(0); |
169 files.CloseFiles(); | 168 files.CloseFiles(); |
170 // Truncate one of the files. | 169 // Truncate one of the files. |
171 { | 170 { |
172 scoped_refptr<File> file(new File); | 171 scoped_refptr<File> file(new File); |
173 ASSERT_TRUE(file->Init(FilePath::FromWStringHack(filename))); | 172 ASSERT_TRUE(file->Init(filename)); |
174 EXPECT_TRUE(file->SetLength(0)); | 173 EXPECT_TRUE(file->SetLength(0)); |
175 } | 174 } |
176 | 175 |
177 // Initializing should fail, not crash. | 176 // Initializing should fail, not crash. |
178 ASSERT_FALSE(files.Init(false)); | 177 ASSERT_FALSE(files.Init(false)); |
179 } | 178 } |
180 | 179 |
181 // An invalid file can be detected after init. | 180 // An invalid file can be detected after init. |
182 TEST_F(DiskCacheTest, BlockFiles_InvalidFile) { | 181 TEST_F(DiskCacheTest, BlockFiles_InvalidFile) { |
183 std::wstring path = GetCachePath(); | 182 FilePath path = FilePath::FromWStringHack(GetCachePath()); |
184 ASSERT_TRUE(DeleteCache(path.c_str())); | 183 ASSERT_TRUE(DeleteCache(path)); |
185 ASSERT_TRUE(file_util::CreateDirectory(path)); | 184 ASSERT_TRUE(file_util::CreateDirectory(path)); |
186 | 185 |
187 BlockFiles files(path); | 186 BlockFiles files(path); |
188 ASSERT_TRUE(files.Init(true)); | 187 ASSERT_TRUE(files.Init(true)); |
189 | 188 |
190 // 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). |
191 Addr addr(BLOCK_256, 1, 5, 10); | 190 Addr addr(BLOCK_256, 1, 5, 10); |
192 EXPECT_TRUE(NULL == files.GetFile(addr)); | 191 EXPECT_TRUE(NULL == files.GetFile(addr)); |
193 | 192 |
194 // Let's create an invalid file. | 193 // Let's create an invalid file. |
195 FilePath filename(FilePath::FromWStringHack(files.Name(5))); | 194 FilePath filename(files.Name(5)); |
196 char header[kBlockHeaderSize]; | 195 char header[kBlockHeaderSize]; |
197 memset(header, 'a', kBlockHeaderSize); | 196 memset(header, 'a', kBlockHeaderSize); |
198 EXPECT_EQ(kBlockHeaderSize, | 197 EXPECT_EQ(kBlockHeaderSize, |
199 file_util::WriteFile(filename, header, kBlockHeaderSize)); | 198 file_util::WriteFile(filename, header, kBlockHeaderSize)); |
200 | 199 |
201 EXPECT_TRUE(NULL == files.GetFile(addr)); | 200 EXPECT_TRUE(NULL == files.GetFile(addr)); |
202 | 201 |
203 // The file should not have been cached (it is still invalid). | 202 // The file should not have been cached (it is still invalid). |
204 EXPECT_TRUE(NULL == files.GetFile(addr)); | 203 EXPECT_TRUE(NULL == files.GetFile(addr)); |
205 } | 204 } |
206 | 205 |
207 } // namespace disk_cache | 206 } // namespace disk_cache |
OLD | NEW |