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/file_util.h" | 5 #include "base/file_util.h" |
6 #include "base/files/file.h" | 6 #include "base/files/file.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 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 const base::FilePath cache_path = cache_dir.path(); | 163 const base::FilePath cache_path = cache_dir.path(); |
164 | 164 |
165 ASSERT_TRUE(simple_util::GetMTime(cache_path, &cache_mtime)); | 165 ASSERT_TRUE(simple_util::GetMTime(cache_path, &cache_mtime)); |
166 WrappedSimpleIndexFile simple_index_file(cache_path); | 166 WrappedSimpleIndexFile simple_index_file(cache_path); |
167 ASSERT_TRUE(simple_index_file.CreateIndexFileDirectory()); | 167 ASSERT_TRUE(simple_index_file.CreateIndexFileDirectory()); |
168 const base::FilePath& index_path = simple_index_file.GetIndexFilePath(); | 168 const base::FilePath& index_path = simple_index_file.GetIndexFilePath(); |
169 EXPECT_TRUE( | 169 EXPECT_TRUE( |
170 WrappedSimpleIndexFile::LegacyIsIndexFileStale(cache_mtime, index_path)); | 170 WrappedSimpleIndexFile::LegacyIsIndexFileStale(cache_mtime, index_path)); |
171 const std::string kDummyData = "nothing to be seen here"; | 171 const std::string kDummyData = "nothing to be seen here"; |
172 EXPECT_EQ(static_cast<int>(kDummyData.size()), | 172 EXPECT_EQ(static_cast<int>(kDummyData.size()), |
173 file_util::WriteFile(index_path, | 173 base::WriteFile(index_path, |
174 kDummyData.data(), | 174 kDummyData.data(), kDummyData.size())); |
175 kDummyData.size())); | |
176 ASSERT_TRUE(simple_util::GetMTime(cache_path, &cache_mtime)); | 175 ASSERT_TRUE(simple_util::GetMTime(cache_path, &cache_mtime)); |
177 EXPECT_FALSE( | 176 EXPECT_FALSE( |
178 WrappedSimpleIndexFile::LegacyIsIndexFileStale(cache_mtime, index_path)); | 177 WrappedSimpleIndexFile::LegacyIsIndexFileStale(cache_mtime, index_path)); |
179 | 178 |
180 const base::Time past_time = base::Time::Now() - | 179 const base::Time past_time = base::Time::Now() - |
181 base::TimeDelta::FromSeconds(10); | 180 base::TimeDelta::FromSeconds(10); |
182 EXPECT_TRUE(base::TouchFile(index_path, past_time, past_time)); | 181 EXPECT_TRUE(base::TouchFile(index_path, past_time, past_time)); |
183 EXPECT_TRUE(base::TouchFile(cache_path, past_time, past_time)); | 182 EXPECT_TRUE(base::TouchFile(cache_path, past_time, past_time)); |
184 ASSERT_TRUE(simple_util::GetMTime(cache_path, &cache_mtime)); | 183 ASSERT_TRUE(simple_util::GetMTime(cache_path, &cache_mtime)); |
185 EXPECT_FALSE( | 184 EXPECT_FALSE( |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 TEST_F(SimpleIndexFileTest, LoadCorruptIndex) { | 236 TEST_F(SimpleIndexFileTest, LoadCorruptIndex) { |
238 base::ScopedTempDir cache_dir; | 237 base::ScopedTempDir cache_dir; |
239 ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); | 238 ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); |
240 | 239 |
241 WrappedSimpleIndexFile simple_index_file(cache_dir.path()); | 240 WrappedSimpleIndexFile simple_index_file(cache_dir.path()); |
242 ASSERT_TRUE(simple_index_file.CreateIndexFileDirectory()); | 241 ASSERT_TRUE(simple_index_file.CreateIndexFileDirectory()); |
243 const base::FilePath& index_path = simple_index_file.GetIndexFilePath(); | 242 const base::FilePath& index_path = simple_index_file.GetIndexFilePath(); |
244 const std::string kDummyData = "nothing to be seen here"; | 243 const std::string kDummyData = "nothing to be seen here"; |
245 EXPECT_EQ( | 244 EXPECT_EQ( |
246 implicit_cast<int>(kDummyData.size()), | 245 implicit_cast<int>(kDummyData.size()), |
247 file_util::WriteFile(index_path, kDummyData.data(), kDummyData.size())); | 246 base::WriteFile(index_path, kDummyData.data(), kDummyData.size())); |
248 base::Time fake_cache_mtime; | 247 base::Time fake_cache_mtime; |
249 ASSERT_TRUE(simple_util::GetMTime(simple_index_file.GetIndexFilePath(), | 248 ASSERT_TRUE(simple_util::GetMTime(simple_index_file.GetIndexFilePath(), |
250 &fake_cache_mtime)); | 249 &fake_cache_mtime)); |
251 EXPECT_FALSE(WrappedSimpleIndexFile::LegacyIsIndexFileStale(fake_cache_mtime, | 250 EXPECT_FALSE(WrappedSimpleIndexFile::LegacyIsIndexFileStale(fake_cache_mtime, |
252 index_path)); | 251 index_path)); |
253 | 252 |
254 SimpleIndexLoadResult load_index_result; | 253 SimpleIndexLoadResult load_index_result; |
255 simple_index_file.LoadIndexEntries(fake_cache_mtime, | 254 simple_index_file.LoadIndexEntries(fake_cache_mtime, |
256 GetCallback(), | 255 GetCallback(), |
257 &load_index_result); | 256 &load_index_result); |
(...skipping 22 matching lines...) Expand all Loading... |
280 sizeof(file_contents)); | 279 sizeof(file_contents)); |
281 ASSERT_EQ((int)sizeof(file_contents), bytes_written); | 280 ASSERT_EQ((int)sizeof(file_contents), bytes_written); |
282 file.Close(); | 281 file.Close(); |
283 | 282 |
284 // Write the index file. The format is incorrect, but for transitioning from | 283 // Write the index file. The format is incorrect, but for transitioning from |
285 // v5 it does not matter. | 284 // v5 it does not matter. |
286 const std::string index_file_contents("incorrectly serialized data"); | 285 const std::string index_file_contents("incorrectly serialized data"); |
287 const base::FilePath old_index_file = | 286 const base::FilePath old_index_file = |
288 cache_path.AppendASCII("the-real-index"); | 287 cache_path.AppendASCII("the-real-index"); |
289 ASSERT_EQ(implicit_cast<int>(index_file_contents.size()), | 288 ASSERT_EQ(implicit_cast<int>(index_file_contents.size()), |
290 file_util::WriteFile(old_index_file, | 289 base::WriteFile(old_index_file, |
291 index_file_contents.data(), | 290 index_file_contents.data(), |
292 index_file_contents.size())); | 291 index_file_contents.size())); |
293 | 292 |
294 // Upgrade the cache. | 293 // Upgrade the cache. |
295 ASSERT_TRUE(disk_cache::UpgradeSimpleCacheOnDisk(cache_path)); | 294 ASSERT_TRUE(disk_cache::UpgradeSimpleCacheOnDisk(cache_path)); |
296 | 295 |
297 // Create the backend and initiate index flush by destroying the backend. | 296 // Create the backend and initiate index flush by destroying the backend. |
298 base::Thread cache_thread("CacheThread"); | 297 base::Thread cache_thread("CacheThread"); |
299 ASSERT_TRUE(cache_thread.StartWithOptions( | 298 ASSERT_TRUE(cache_thread.StartWithOptions( |
300 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); | 299 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
301 disk_cache::SimpleBackendImpl* simple_cache = | 300 disk_cache::SimpleBackendImpl* simple_cache = |
302 new disk_cache::SimpleBackendImpl(cache_path, | 301 new disk_cache::SimpleBackendImpl(cache_path, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 WrappedSimpleIndexFile::Deserialize(contents.data(), | 333 WrappedSimpleIndexFile::Deserialize(contents.data(), |
335 contents.size(), | 334 contents.size(), |
336 &when_index_last_saw_cache, | 335 &when_index_last_saw_cache, |
337 &deserialize_result); | 336 &deserialize_result); |
338 EXPECT_TRUE(deserialize_result.did_load); | 337 EXPECT_TRUE(deserialize_result.did_load); |
339 } | 338 } |
340 | 339 |
341 #endif // defined(OS_POSIX) | 340 #endif // defined(OS_POSIX) |
342 | 341 |
343 } // namespace disk_cache | 342 } // namespace disk_cache |
OLD | NEW |