| 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 "net/disk_cache/simple/simple_index_file.h" | 5 #include "net/disk_cache/simple/simple_index_file.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 SimpleIndex::EntrySet::const_iterator it = new_entries.find(kHashes[i]); | 196 SimpleIndex::EntrySet::const_iterator it = new_entries.find(kHashes[i]); |
| 197 EXPECT_TRUE(new_entries.end() != it); | 197 EXPECT_TRUE(new_entries.end() != it); |
| 198 EXPECT_TRUE(CompareTwoEntryMetadata(it->second, metadata_entries[i])); | 198 EXPECT_TRUE(CompareTwoEntryMetadata(it->second, metadata_entries[i])); |
| 199 } | 199 } |
| 200 } | 200 } |
| 201 | 201 |
| 202 TEST_F(SimpleIndexFileTest, LegacyIsIndexFileStale) { | 202 TEST_F(SimpleIndexFileTest, LegacyIsIndexFileStale) { |
| 203 base::ScopedTempDir cache_dir; | 203 base::ScopedTempDir cache_dir; |
| 204 ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); | 204 ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); |
| 205 base::Time cache_mtime; | 205 base::Time cache_mtime; |
| 206 const base::FilePath cache_path = cache_dir.path(); | 206 const base::FilePath cache_path = cache_dir.GetPath(); |
| 207 | 207 |
| 208 ASSERT_TRUE(simple_util::GetMTime(cache_path, &cache_mtime)); | 208 ASSERT_TRUE(simple_util::GetMTime(cache_path, &cache_mtime)); |
| 209 WrappedSimpleIndexFile simple_index_file(cache_path); | 209 WrappedSimpleIndexFile simple_index_file(cache_path); |
| 210 ASSERT_TRUE(simple_index_file.CreateIndexFileDirectory()); | 210 ASSERT_TRUE(simple_index_file.CreateIndexFileDirectory()); |
| 211 const base::FilePath& index_path = simple_index_file.GetIndexFilePath(); | 211 const base::FilePath& index_path = simple_index_file.GetIndexFilePath(); |
| 212 EXPECT_TRUE( | 212 EXPECT_TRUE( |
| 213 WrappedSimpleIndexFile::LegacyIsIndexFileStale(cache_mtime, index_path)); | 213 WrappedSimpleIndexFile::LegacyIsIndexFileStale(cache_mtime, index_path)); |
| 214 const std::string kDummyData = "nothing to be seen here"; | 214 const std::string kDummyData = "nothing to be seen here"; |
| 215 EXPECT_EQ(static_cast<int>(kDummyData.size()), | 215 EXPECT_EQ(static_cast<int>(kDummyData.size()), |
| 216 base::WriteFile(index_path, | 216 base::WriteFile(index_path, |
| (...skipping 25 matching lines...) Expand all Loading... |
| 242 EntryMetadata metadata_entries[kNumHashes]; | 242 EntryMetadata metadata_entries[kNumHashes]; |
| 243 for (size_t i = 0; i < kNumHashes; ++i) { | 243 for (size_t i = 0; i < kNumHashes; ++i) { |
| 244 uint64_t hash = kHashes[i]; | 244 uint64_t hash = kHashes[i]; |
| 245 metadata_entries[i] = EntryMetadata(Time(), static_cast<uint32_t>(hash)); | 245 metadata_entries[i] = EntryMetadata(Time(), static_cast<uint32_t>(hash)); |
| 246 SimpleIndex::InsertInEntrySet(hash, metadata_entries[i], &entries); | 246 SimpleIndex::InsertInEntrySet(hash, metadata_entries[i], &entries); |
| 247 } | 247 } |
| 248 | 248 |
| 249 const uint64_t kCacheSize = 456U; | 249 const uint64_t kCacheSize = 456U; |
| 250 net::TestClosure closure; | 250 net::TestClosure closure; |
| 251 { | 251 { |
| 252 WrappedSimpleIndexFile simple_index_file(cache_dir.path()); | 252 WrappedSimpleIndexFile simple_index_file(cache_dir.GetPath()); |
| 253 simple_index_file.WriteToDisk(SimpleIndex::INDEX_WRITE_REASON_SHUTDOWN, | 253 simple_index_file.WriteToDisk(SimpleIndex::INDEX_WRITE_REASON_SHUTDOWN, |
| 254 entries, kCacheSize, base::TimeTicks(), false, | 254 entries, kCacheSize, base::TimeTicks(), false, |
| 255 closure.closure()); | 255 closure.closure()); |
| 256 closure.WaitForResult(); | 256 closure.WaitForResult(); |
| 257 EXPECT_TRUE(base::PathExists(simple_index_file.GetIndexFilePath())); | 257 EXPECT_TRUE(base::PathExists(simple_index_file.GetIndexFilePath())); |
| 258 } | 258 } |
| 259 | 259 |
| 260 WrappedSimpleIndexFile simple_index_file(cache_dir.path()); | 260 WrappedSimpleIndexFile simple_index_file(cache_dir.GetPath()); |
| 261 base::Time fake_cache_mtime; | 261 base::Time fake_cache_mtime; |
| 262 ASSERT_TRUE(simple_util::GetMTime(cache_dir.path(), &fake_cache_mtime)); | 262 ASSERT_TRUE(simple_util::GetMTime(cache_dir.GetPath(), &fake_cache_mtime)); |
| 263 SimpleIndexLoadResult load_index_result; | 263 SimpleIndexLoadResult load_index_result; |
| 264 simple_index_file.LoadIndexEntries(fake_cache_mtime, closure.closure(), | 264 simple_index_file.LoadIndexEntries(fake_cache_mtime, closure.closure(), |
| 265 &load_index_result); | 265 &load_index_result); |
| 266 closure.WaitForResult(); | 266 closure.WaitForResult(); |
| 267 | 267 |
| 268 EXPECT_TRUE(base::PathExists(simple_index_file.GetIndexFilePath())); | 268 EXPECT_TRUE(base::PathExists(simple_index_file.GetIndexFilePath())); |
| 269 EXPECT_TRUE(load_index_result.did_load); | 269 EXPECT_TRUE(load_index_result.did_load); |
| 270 EXPECT_FALSE(load_index_result.flush_required); | 270 EXPECT_FALSE(load_index_result.flush_required); |
| 271 | 271 |
| 272 EXPECT_EQ(kNumHashes, load_index_result.entries.size()); | 272 EXPECT_EQ(kNumHashes, load_index_result.entries.size()); |
| 273 for (size_t i = 0; i < kNumHashes; ++i) | 273 for (size_t i = 0; i < kNumHashes; ++i) |
| 274 EXPECT_EQ(1U, load_index_result.entries.count(kHashes[i])); | 274 EXPECT_EQ(1U, load_index_result.entries.count(kHashes[i])); |
| 275 } | 275 } |
| 276 | 276 |
| 277 TEST_F(SimpleIndexFileTest, LoadCorruptIndex) { | 277 TEST_F(SimpleIndexFileTest, LoadCorruptIndex) { |
| 278 base::ScopedTempDir cache_dir; | 278 base::ScopedTempDir cache_dir; |
| 279 ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); | 279 ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); |
| 280 | 280 |
| 281 WrappedSimpleIndexFile simple_index_file(cache_dir.path()); | 281 WrappedSimpleIndexFile simple_index_file(cache_dir.GetPath()); |
| 282 ASSERT_TRUE(simple_index_file.CreateIndexFileDirectory()); | 282 ASSERT_TRUE(simple_index_file.CreateIndexFileDirectory()); |
| 283 const base::FilePath& index_path = simple_index_file.GetIndexFilePath(); | 283 const base::FilePath& index_path = simple_index_file.GetIndexFilePath(); |
| 284 const std::string kDummyData = "nothing to be seen here"; | 284 const std::string kDummyData = "nothing to be seen here"; |
| 285 EXPECT_EQ(static_cast<int>(kDummyData.size()), | 285 EXPECT_EQ(static_cast<int>(kDummyData.size()), |
| 286 base::WriteFile(index_path, kDummyData.data(), kDummyData.size())); | 286 base::WriteFile(index_path, kDummyData.data(), kDummyData.size())); |
| 287 base::Time fake_cache_mtime; | 287 base::Time fake_cache_mtime; |
| 288 ASSERT_TRUE(simple_util::GetMTime(simple_index_file.GetIndexFilePath(), | 288 ASSERT_TRUE(simple_util::GetMTime(simple_index_file.GetIndexFilePath(), |
| 289 &fake_cache_mtime)); | 289 &fake_cache_mtime)); |
| 290 EXPECT_FALSE(WrappedSimpleIndexFile::LegacyIsIndexFileStale(fake_cache_mtime, | 290 EXPECT_FALSE(WrappedSimpleIndexFile::LegacyIsIndexFileStale(fake_cache_mtime, |
| 291 index_path)); | 291 index_path)); |
| 292 SimpleIndexLoadResult load_index_result; | 292 SimpleIndexLoadResult load_index_result; |
| 293 net::TestClosure closure; | 293 net::TestClosure closure; |
| 294 simple_index_file.LoadIndexEntries(fake_cache_mtime, closure.closure(), | 294 simple_index_file.LoadIndexEntries(fake_cache_mtime, closure.closure(), |
| 295 &load_index_result); | 295 &load_index_result); |
| 296 closure.WaitForResult(); | 296 closure.WaitForResult(); |
| 297 | 297 |
| 298 EXPECT_FALSE(base::PathExists(index_path)); | 298 EXPECT_FALSE(base::PathExists(index_path)); |
| 299 EXPECT_TRUE(load_index_result.did_load); | 299 EXPECT_TRUE(load_index_result.did_load); |
| 300 EXPECT_TRUE(load_index_result.flush_required); | 300 EXPECT_TRUE(load_index_result.flush_required); |
| 301 } | 301 } |
| 302 | 302 |
| 303 // Tests that after an upgrade the backend has the index file put in place. | 303 // Tests that after an upgrade the backend has the index file put in place. |
| 304 TEST_F(SimpleIndexFileTest, SimpleCacheUpgrade) { | 304 TEST_F(SimpleIndexFileTest, SimpleCacheUpgrade) { |
| 305 base::ScopedTempDir cache_dir; | 305 base::ScopedTempDir cache_dir; |
| 306 ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); | 306 ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); |
| 307 const base::FilePath cache_path = cache_dir.path(); | 307 const base::FilePath cache_path = cache_dir.GetPath(); |
| 308 | 308 |
| 309 // Write an old fake index file. | 309 // Write an old fake index file. |
| 310 base::File file(cache_path.AppendASCII("index"), | 310 base::File file(cache_path.AppendASCII("index"), |
| 311 base::File::FLAG_CREATE | base::File::FLAG_WRITE); | 311 base::File::FLAG_CREATE | base::File::FLAG_WRITE); |
| 312 ASSERT_TRUE(file.IsValid()); | 312 ASSERT_TRUE(file.IsValid()); |
| 313 disk_cache::FakeIndexData file_contents; | 313 disk_cache::FakeIndexData file_contents; |
| 314 file_contents.initial_magic_number = disk_cache::kSimpleInitialMagicNumber; | 314 file_contents.initial_magic_number = disk_cache::kSimpleInitialMagicNumber; |
| 315 file_contents.version = 5; | 315 file_contents.version = 5; |
| 316 int bytes_written = file.Write(0, reinterpret_cast<char*>(&file_contents), | 316 int bytes_written = file.Write(0, reinterpret_cast<char*>(&file_contents), |
| 317 sizeof(file_contents)); | 317 sizeof(file_contents)); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 WrappedSimpleIndexFile::Deserialize(contents.data(), | 367 WrappedSimpleIndexFile::Deserialize(contents.data(), |
| 368 contents.size(), | 368 contents.size(), |
| 369 &when_index_last_saw_cache, | 369 &when_index_last_saw_cache, |
| 370 &deserialize_result); | 370 &deserialize_result); |
| 371 EXPECT_TRUE(deserialize_result.did_load); | 371 EXPECT_TRUE(deserialize_result.did_load); |
| 372 } | 372 } |
| 373 | 373 |
| 374 TEST_F(SimpleIndexFileTest, OverwritesStaleTempFile) { | 374 TEST_F(SimpleIndexFileTest, OverwritesStaleTempFile) { |
| 375 base::ScopedTempDir cache_dir; | 375 base::ScopedTempDir cache_dir; |
| 376 ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); | 376 ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); |
| 377 const base::FilePath cache_path = cache_dir.path(); | 377 const base::FilePath cache_path = cache_dir.GetPath(); |
| 378 WrappedSimpleIndexFile simple_index_file(cache_path); | 378 WrappedSimpleIndexFile simple_index_file(cache_path); |
| 379 ASSERT_TRUE(simple_index_file.CreateIndexFileDirectory()); | 379 ASSERT_TRUE(simple_index_file.CreateIndexFileDirectory()); |
| 380 | 380 |
| 381 // Create an temporary index file. | 381 // Create an temporary index file. |
| 382 const base::FilePath& temp_index_path = | 382 const base::FilePath& temp_index_path = |
| 383 simple_index_file.GetTempIndexFilePath(); | 383 simple_index_file.GetTempIndexFilePath(); |
| 384 const std::string kDummyData = "nothing to be seen here"; | 384 const std::string kDummyData = "nothing to be seen here"; |
| 385 EXPECT_EQ( | 385 EXPECT_EQ( |
| 386 static_cast<int>(kDummyData.size()), | 386 static_cast<int>(kDummyData.size()), |
| 387 base::WriteFile(temp_index_path, kDummyData.data(), kDummyData.size())); | 387 base::WriteFile(temp_index_path, kDummyData.data(), kDummyData.size())); |
| 388 ASSERT_TRUE(base::PathExists(simple_index_file.GetTempIndexFilePath())); | 388 ASSERT_TRUE(base::PathExists(simple_index_file.GetTempIndexFilePath())); |
| 389 | 389 |
| 390 // Write the index file. | 390 // Write the index file. |
| 391 SimpleIndex::EntrySet entries; | 391 SimpleIndex::EntrySet entries; |
| 392 SimpleIndex::InsertInEntrySet(11, EntryMetadata(Time(), 11u), &entries); | 392 SimpleIndex::InsertInEntrySet(11, EntryMetadata(Time(), 11u), &entries); |
| 393 net::TestClosure closure; | 393 net::TestClosure closure; |
| 394 simple_index_file.WriteToDisk(SimpleIndex::INDEX_WRITE_REASON_SHUTDOWN, | 394 simple_index_file.WriteToDisk(SimpleIndex::INDEX_WRITE_REASON_SHUTDOWN, |
| 395 entries, 120U, base::TimeTicks(), false, | 395 entries, 120U, base::TimeTicks(), false, |
| 396 closure.closure()); | 396 closure.closure()); |
| 397 closure.WaitForResult(); | 397 closure.WaitForResult(); |
| 398 | 398 |
| 399 // Check that the temporary file was deleted and the index file was created. | 399 // Check that the temporary file was deleted and the index file was created. |
| 400 EXPECT_FALSE(base::PathExists(simple_index_file.GetTempIndexFilePath())); | 400 EXPECT_FALSE(base::PathExists(simple_index_file.GetTempIndexFilePath())); |
| 401 EXPECT_TRUE(base::PathExists(simple_index_file.GetIndexFilePath())); | 401 EXPECT_TRUE(base::PathExists(simple_index_file.GetIndexFilePath())); |
| 402 } | 402 } |
| 403 | 403 |
| 404 #endif // defined(OS_POSIX) | 404 #endif // defined(OS_POSIX) |
| 405 | 405 |
| 406 } // namespace disk_cache | 406 } // namespace disk_cache |
| OLD | NEW |