OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/files/file.h" | 5 #include "base/files/file.h" |
6 #include "base/files/file_util.h" | 6 #include "base/files/file_util.h" |
7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
8 #include "base/hash.h" | 8 #include "base/hash.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 }; | 91 }; |
92 | 92 |
93 class SimpleIndexFileTest : public testing::Test { | 93 class SimpleIndexFileTest : public testing::Test { |
94 public: | 94 public: |
95 bool CompareTwoEntryMetadata(const EntryMetadata& a, const EntryMetadata& b) { | 95 bool CompareTwoEntryMetadata(const EntryMetadata& a, const EntryMetadata& b) { |
96 return | 96 return |
97 a.last_used_time_seconds_since_epoch_ == | 97 a.last_used_time_seconds_since_epoch_ == |
98 b.last_used_time_seconds_since_epoch_ && | 98 b.last_used_time_seconds_since_epoch_ && |
99 a.entry_size_ == b.entry_size_; | 99 a.entry_size_ == b.entry_size_; |
100 } | 100 } |
101 | |
102 protected: | |
103 SimpleIndexFileTest() : callback_called_(false) {} | |
104 | |
105 base::Closure GetCallback() { | |
106 return base::Bind(&SimpleIndexFileTest::LoadIndexEntriesCallback, | |
107 base::Unretained(this)); | |
108 } | |
109 | |
110 bool callback_called() { return callback_called_; } | |
111 | |
112 private: | |
113 void LoadIndexEntriesCallback() { | |
114 EXPECT_FALSE(callback_called_); | |
115 callback_called_ = true; | |
116 } | |
117 | |
118 bool callback_called_; | |
119 }; | 101 }; |
120 | 102 |
121 TEST_F(SimpleIndexFileTest, Serialize) { | 103 TEST_F(SimpleIndexFileTest, Serialize) { |
122 SimpleIndex::EntrySet entries; | 104 SimpleIndex::EntrySet entries; |
123 static const uint64 kHashes[] = { 11, 22, 33 }; | 105 static const uint64 kHashes[] = { 11, 22, 33 }; |
124 static const size_t kNumHashes = arraysize(kHashes); | 106 static const size_t kNumHashes = arraysize(kHashes); |
125 EntryMetadata metadata_entries[kNumHashes]; | 107 EntryMetadata metadata_entries[kNumHashes]; |
126 | 108 |
127 SimpleIndexFile::IndexMetadata index_metadata(static_cast<uint64>(kNumHashes), | 109 SimpleIndexFile::IndexMetadata index_metadata(static_cast<uint64>(kNumHashes), |
128 456); | 110 456); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 static const uint64 kHashes[] = { 11, 22, 33 }; | 178 static const uint64 kHashes[] = { 11, 22, 33 }; |
197 static const size_t kNumHashes = arraysize(kHashes); | 179 static const size_t kNumHashes = arraysize(kHashes); |
198 EntryMetadata metadata_entries[kNumHashes]; | 180 EntryMetadata metadata_entries[kNumHashes]; |
199 for (size_t i = 0; i < kNumHashes; ++i) { | 181 for (size_t i = 0; i < kNumHashes; ++i) { |
200 uint64 hash = kHashes[i]; | 182 uint64 hash = kHashes[i]; |
201 metadata_entries[i] = EntryMetadata(Time(), hash); | 183 metadata_entries[i] = EntryMetadata(Time(), hash); |
202 SimpleIndex::InsertInEntrySet(hash, metadata_entries[i], &entries); | 184 SimpleIndex::InsertInEntrySet(hash, metadata_entries[i], &entries); |
203 } | 185 } |
204 | 186 |
205 const uint64 kCacheSize = 456U; | 187 const uint64 kCacheSize = 456U; |
| 188 net::TestClosure closure; |
206 { | 189 { |
207 WrappedSimpleIndexFile simple_index_file(cache_dir.path()); | 190 WrappedSimpleIndexFile simple_index_file(cache_dir.path()); |
208 simple_index_file.WriteToDisk(entries, kCacheSize, | 191 simple_index_file.WriteToDisk(entries, kCacheSize, base::TimeTicks(), |
209 base::TimeTicks(), false); | 192 false, closure.closure()); |
210 base::RunLoop().RunUntilIdle(); | 193 closure.WaitForResult(); |
211 EXPECT_TRUE(base::PathExists(simple_index_file.GetIndexFilePath())); | 194 EXPECT_TRUE(base::PathExists(simple_index_file.GetIndexFilePath())); |
212 } | 195 } |
213 | 196 |
214 WrappedSimpleIndexFile simple_index_file(cache_dir.path()); | 197 WrappedSimpleIndexFile simple_index_file(cache_dir.path()); |
215 base::Time fake_cache_mtime; | 198 base::Time fake_cache_mtime; |
216 ASSERT_TRUE(simple_util::GetMTime(cache_dir.path(), &fake_cache_mtime)); | 199 ASSERT_TRUE(simple_util::GetMTime(cache_dir.path(), &fake_cache_mtime)); |
217 SimpleIndexLoadResult load_index_result; | 200 SimpleIndexLoadResult load_index_result; |
218 simple_index_file.LoadIndexEntries(fake_cache_mtime, | 201 simple_index_file.LoadIndexEntries(fake_cache_mtime, closure.closure(), |
219 GetCallback(), | |
220 &load_index_result); | 202 &load_index_result); |
221 base::RunLoop().RunUntilIdle(); | 203 closure.WaitForResult(); |
222 | 204 |
223 EXPECT_TRUE(base::PathExists(simple_index_file.GetIndexFilePath())); | 205 EXPECT_TRUE(base::PathExists(simple_index_file.GetIndexFilePath())); |
224 ASSERT_TRUE(callback_called()); | |
225 EXPECT_TRUE(load_index_result.did_load); | 206 EXPECT_TRUE(load_index_result.did_load); |
226 EXPECT_FALSE(load_index_result.flush_required); | 207 EXPECT_FALSE(load_index_result.flush_required); |
227 | 208 |
228 EXPECT_EQ(kNumHashes, load_index_result.entries.size()); | 209 EXPECT_EQ(kNumHashes, load_index_result.entries.size()); |
229 for (size_t i = 0; i < kNumHashes; ++i) | 210 for (size_t i = 0; i < kNumHashes; ++i) |
230 EXPECT_EQ(1U, load_index_result.entries.count(kHashes[i])); | 211 EXPECT_EQ(1U, load_index_result.entries.count(kHashes[i])); |
231 } | 212 } |
232 | 213 |
233 TEST_F(SimpleIndexFileTest, LoadCorruptIndex) { | 214 TEST_F(SimpleIndexFileTest, LoadCorruptIndex) { |
234 base::ScopedTempDir cache_dir; | 215 base::ScopedTempDir cache_dir; |
235 ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); | 216 ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); |
236 | 217 |
237 WrappedSimpleIndexFile simple_index_file(cache_dir.path()); | 218 WrappedSimpleIndexFile simple_index_file(cache_dir.path()); |
238 ASSERT_TRUE(simple_index_file.CreateIndexFileDirectory()); | 219 ASSERT_TRUE(simple_index_file.CreateIndexFileDirectory()); |
239 const base::FilePath& index_path = simple_index_file.GetIndexFilePath(); | 220 const base::FilePath& index_path = simple_index_file.GetIndexFilePath(); |
240 const std::string kDummyData = "nothing to be seen here"; | 221 const std::string kDummyData = "nothing to be seen here"; |
241 EXPECT_EQ( | 222 EXPECT_EQ( |
242 implicit_cast<int>(kDummyData.size()), | 223 implicit_cast<int>(kDummyData.size()), |
243 base::WriteFile(index_path, kDummyData.data(), kDummyData.size())); | 224 base::WriteFile(index_path, kDummyData.data(), kDummyData.size())); |
244 base::Time fake_cache_mtime; | 225 base::Time fake_cache_mtime; |
245 ASSERT_TRUE(simple_util::GetMTime(simple_index_file.GetIndexFilePath(), | 226 ASSERT_TRUE(simple_util::GetMTime(simple_index_file.GetIndexFilePath(), |
246 &fake_cache_mtime)); | 227 &fake_cache_mtime)); |
247 EXPECT_FALSE(WrappedSimpleIndexFile::LegacyIsIndexFileStale(fake_cache_mtime, | 228 EXPECT_FALSE(WrappedSimpleIndexFile::LegacyIsIndexFileStale(fake_cache_mtime, |
248 index_path)); | 229 index_path)); |
249 | |
250 SimpleIndexLoadResult load_index_result; | 230 SimpleIndexLoadResult load_index_result; |
251 simple_index_file.LoadIndexEntries(fake_cache_mtime, | 231 net::TestClosure closure; |
252 GetCallback(), | 232 simple_index_file.LoadIndexEntries(fake_cache_mtime, closure.closure(), |
253 &load_index_result); | 233 &load_index_result); |
254 base::RunLoop().RunUntilIdle(); | 234 closure.WaitForResult(); |
255 | 235 |
256 EXPECT_FALSE(base::PathExists(index_path)); | 236 EXPECT_FALSE(base::PathExists(index_path)); |
257 ASSERT_TRUE(callback_called()); | |
258 EXPECT_TRUE(load_index_result.did_load); | 237 EXPECT_TRUE(load_index_result.did_load); |
259 EXPECT_TRUE(load_index_result.flush_required); | 238 EXPECT_TRUE(load_index_result.flush_required); |
260 } | 239 } |
261 | 240 |
262 // Tests that after an upgrade the backend has the index file put in place. | 241 // Tests that after an upgrade the backend has the index file put in place. |
263 TEST_F(SimpleIndexFileTest, SimpleCacheUpgrade) { | 242 TEST_F(SimpleIndexFileTest, SimpleCacheUpgrade) { |
264 base::ScopedTempDir cache_dir; | 243 base::ScopedTempDir cache_dir; |
265 ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); | 244 ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); |
266 const base::FilePath cache_path = cache_dir.path(); | 245 const base::FilePath cache_path = cache_dir.path(); |
267 | 246 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 WrappedSimpleIndexFile::Deserialize(contents.data(), | 309 WrappedSimpleIndexFile::Deserialize(contents.data(), |
331 contents.size(), | 310 contents.size(), |
332 &when_index_last_saw_cache, | 311 &when_index_last_saw_cache, |
333 &deserialize_result); | 312 &deserialize_result); |
334 EXPECT_TRUE(deserialize_result.did_load); | 313 EXPECT_TRUE(deserialize_result.did_load); |
335 } | 314 } |
336 | 315 |
337 #endif // defined(OS_POSIX) | 316 #endif // defined(OS_POSIX) |
338 | 317 |
339 } // namespace disk_cache | 318 } // namespace disk_cache |
OLD | NEW |