1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
|
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/disk_cache/simple/simple_index_file.h"
#include <memory>
#include "base/check.h"
#include "base/files/file.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/callback.h"
#include "base/hash/hash.h"
#include "base/location.h"
#include "base/pickle.h"
#include "base/run_loop.h"
#include "base/task/single_thread_task_runner.h"
#include "base/threading/thread.h"
#include "base/time/time.h"
#include "net/base/cache_type.h"
#include "net/base/test_completion_callback.h"
#include "net/disk_cache/backend_cleanup_tracker.h"
#include "net/disk_cache/disk_cache.h"
#include "net/disk_cache/disk_cache_test_util.h"
#include "net/disk_cache/simple/simple_backend_impl.h"
#include "net/disk_cache/simple/simple_backend_version.h"
#include "net/disk_cache/simple/simple_entry_format.h"
#include "net/disk_cache/simple/simple_index.h"
#include "net/disk_cache/simple/simple_util.h"
#include "net/disk_cache/simple/simple_version_upgrade.h"
#include "net/test/gtest_util.h"
#include "net/test/test_with_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using net::test::IsOk;
using base::Time;
using disk_cache::SimpleIndexFile;
using disk_cache::SimpleIndex;
namespace disk_cache {
TEST(IndexMetadataTest, Basics) {
SimpleIndexFile::IndexMetadata index_metadata;
EXPECT_EQ(disk_cache::kSimpleIndexMagicNumber, index_metadata.magic_number_);
EXPECT_EQ(disk_cache::kSimpleIndexFileVersion, index_metadata.version_);
EXPECT_EQ(0U, index_metadata.entry_count());
EXPECT_EQ(0U, index_metadata.cache_size_);
// Without setting a |reason_|, the index metadata isn't valid.
index_metadata.reason_ = SimpleIndex::INDEX_WRITE_REASON_SHUTDOWN;
EXPECT_TRUE(index_metadata.CheckIndexMetadata());
}
TEST(IndexMetadataTest, Serialize) {
SimpleIndexFile::IndexMetadata index_metadata(
SimpleIndex::INDEX_WRITE_REASON_SHUTDOWN, 123, 456);
base::Pickle pickle;
index_metadata.Serialize(&pickle);
base::PickleIterator it(pickle);
SimpleIndexFile::IndexMetadata new_index_metadata;
new_index_metadata.Deserialize(&it);
EXPECT_EQ(new_index_metadata.magic_number_, index_metadata.magic_number_);
EXPECT_EQ(new_index_metadata.version_, index_metadata.version_);
EXPECT_EQ(new_index_metadata.reason_, index_metadata.reason_);
EXPECT_EQ(new_index_metadata.entry_count(), index_metadata.entry_count());
EXPECT_EQ(new_index_metadata.cache_size_, index_metadata.cache_size_);
EXPECT_TRUE(new_index_metadata.CheckIndexMetadata());
}
class V8IndexMetadataForTest : public SimpleIndexFile::IndexMetadata {
public:
V8IndexMetadataForTest(uint64_t entry_count, uint64_t cache_size)
: SimpleIndexFile::IndexMetadata(SimpleIndex::INDEX_WRITE_REASON_SHUTDOWN,
entry_count,
cache_size) {
version_ = 8;
}
};
// This friend derived class is able to reexport its ancestors private methods
// as public, for use in tests.
class WrappedSimpleIndexFile : public SimpleIndexFile {
public:
using SimpleIndexFile::Deserialize;
using SimpleIndexFile::Serialize;
using SimpleIndexFile::SerializeFinalData;
explicit WrappedSimpleIndexFile(const base::FilePath& index_file_directory)
: SimpleIndexFile(base::SingleThreadTaskRunner::GetCurrentDefault(),
base::MakeRefCounted<TrivialFileOperationsFactory>(),
net::DISK_CACHE,
index_file_directory) {}
~WrappedSimpleIndexFile() override = default;
const base::FilePath& GetIndexFilePath() const {
return index_file_;
}
const base::FilePath& GetTempIndexFilePath() const {
return temp_index_file_;
}
bool CreateIndexFileDirectory() const {
return base::CreateDirectory(index_file_.DirName());
}
static bool LegacyIsIndexFileStale(base::Time cache_last_modified,
const base::FilePath& index_file_path) {
TrivialFileOperations ops;
return SimpleIndexFile::LegacyIsIndexFileStale(&ops, cache_last_modified,
index_file_path);
}
};
class SimpleIndexFileTest : public net::TestWithTaskEnvironment {
public:
bool CompareTwoEntryMetadata(const EntryMetadata& a, const EntryMetadata& b) {
return a.last_used_time_seconds_since_epoch_ ==
b.last_used_time_seconds_since_epoch_ &&
a.entry_size_256b_chunks_ == b.entry_size_256b_chunks_ &&
a.in_memory_data_ == b.in_memory_data_;
}
bool CompareTwoAppCacheEntryMetadata(const EntryMetadata& a,
const EntryMetadata& b) {
return a.trailer_prefetch_size_ == b.trailer_prefetch_size_ &&
a.entry_size_256b_chunks_ == b.entry_size_256b_chunks_ &&
a.in_memory_data_ == b.in_memory_data_;
}
};
TEST_F(SimpleIndexFileTest, Serialize) {
SimpleIndex::EntrySet entries;
static const size_t kNumHashes = 3u;
const std::array<uint64_t, kNumHashes> kHashes = {11, 22, 33};
std::array<EntryMetadata, kNumHashes> metadata_entries;
SimpleIndexFile::IndexMetadata index_metadata(
SimpleIndex::INDEX_WRITE_REASON_SHUTDOWN,
static_cast<uint64_t>(kNumHashes), 456);
for (size_t i = 0; i < kNumHashes; ++i) {
uint64_t hash = kHashes[i];
metadata_entries[i] = EntryMetadata(Time(), hash);
metadata_entries[i].SetInMemoryData(static_cast<uint8_t>(i));
SimpleIndex::InsertInEntrySet(hash, metadata_entries[i], &entries);
}
std::unique_ptr<base::Pickle> pickle = WrappedSimpleIndexFile::Serialize(
net::DISK_CACHE, index_metadata, entries);
EXPECT_TRUE(pickle.get() != nullptr);
base::Time now = base::Time::Now();
WrappedSimpleIndexFile::SerializeFinalData(now, pickle.get());
base::Time when_index_last_saw_cache;
SimpleIndexLoadResult deserialize_result;
WrappedSimpleIndexFile::Deserialize(net::DISK_CACHE, *pickle,
&when_index_last_saw_cache,
&deserialize_result);
EXPECT_TRUE(deserialize_result.did_load);
EXPECT_EQ(now, when_index_last_saw_cache);
const SimpleIndex::EntrySet& new_entries = deserialize_result.entries;
EXPECT_EQ(entries.size(), new_entries.size());
for (size_t i = 0; i < kNumHashes; ++i) {
auto it = new_entries.find(kHashes[i]);
EXPECT_TRUE(new_entries.end() != it);
EXPECT_TRUE(CompareTwoEntryMetadata(it->second, metadata_entries[i]));
}
}
TEST_F(SimpleIndexFileTest, SerializeAppCache) {
SimpleIndex::EntrySet entries;
static const size_t kNumHashes = 3u;
const std::array<uint64_t, kNumHashes> kHashes = {11, 22, 33};
const std::array<int32_t, kNumHashes> kTrailerPrefetches = {123, -1, 987};
std::array<EntryMetadata, kNumHashes> metadata_entries;
SimpleIndexFile::IndexMetadata index_metadata(
SimpleIndex::INDEX_WRITE_REASON_SHUTDOWN,
static_cast<uint64_t>(kNumHashes), 456);
for (size_t i = 0; i < kNumHashes; ++i) {
uint64_t hash = kHashes[i];
metadata_entries[i] = EntryMetadata(kTrailerPrefetches[i], hash);
metadata_entries[i].SetInMemoryData(static_cast<uint8_t>(i));
SimpleIndex::InsertInEntrySet(hash, metadata_entries[i], &entries);
}
std::unique_ptr<base::Pickle> pickle = WrappedSimpleIndexFile::Serialize(
net::APP_CACHE, index_metadata, entries);
EXPECT_TRUE(pickle.get() != nullptr);
base::Time now = base::Time::Now();
WrappedSimpleIndexFile::SerializeFinalData(now, pickle.get());
base::Time when_index_last_saw_cache;
SimpleIndexLoadResult deserialize_result;
WrappedSimpleIndexFile::Deserialize(
net::APP_CACHE, *pickle, &when_index_last_saw_cache, &deserialize_result);
EXPECT_TRUE(deserialize_result.did_load);
EXPECT_EQ(now, when_index_last_saw_cache);
const SimpleIndex::EntrySet& new_entries = deserialize_result.entries;
EXPECT_EQ(entries.size(), new_entries.size());
for (size_t i = 0; i < kNumHashes; ++i) {
auto it = new_entries.find(kHashes[i]);
EXPECT_TRUE(new_entries.end() != it);
EXPECT_TRUE(
CompareTwoAppCacheEntryMetadata(it->second, metadata_entries[i]));
}
}
TEST_F(SimpleIndexFileTest, ReadV8Format) {
static const size_t kNumHashes = 3u;
const std::array<uint64_t, kNumHashes> kHashes = {11, 22, 33};
std::array<EntryMetadata, kNumHashes> metadata_entries;
// V8 to V9 should not make any modifications for non-APP_CACHE modes.
// Verify that the data is preserved through the migration.
V8IndexMetadataForTest v8_metadata(kNumHashes, 100 * 1024 * 1024);
SimpleIndex::EntrySet entries;
for (size_t i = 0; i < kNumHashes; ++i) {
metadata_entries[i] = EntryMetadata(base::Time::Now(), kHashes[i]);
metadata_entries[i].SetInMemoryData(static_cast<uint8_t>(i));
SimpleIndex::InsertInEntrySet(kHashes[i], metadata_entries[i], &entries);
}
std::unique_ptr<base::Pickle> pickle =
WrappedSimpleIndexFile::Serialize(net::DISK_CACHE, v8_metadata, entries);
ASSERT_TRUE(pickle.get() != nullptr);
base::Time now = base::Time::Now();
WrappedSimpleIndexFile::SerializeFinalData(now, pickle.get());
base::Time when_index_last_saw_cache;
SimpleIndexLoadResult deserialize_result;
WrappedSimpleIndexFile::Deserialize(net::DISK_CACHE, *pickle,
&when_index_last_saw_cache,
&deserialize_result);
EXPECT_TRUE(deserialize_result.did_load);
EXPECT_EQ(now, when_index_last_saw_cache);
const SimpleIndex::EntrySet& new_entries = deserialize_result.entries;
ASSERT_EQ(entries.size(), new_entries.size());
for (size_t i = 0; i < kNumHashes; ++i) {
auto it = new_entries.find(kHashes[i]);
ASSERT_TRUE(new_entries.end() != it);
EXPECT_TRUE(CompareTwoEntryMetadata(it->second, metadata_entries[i]));
}
}
TEST_F(SimpleIndexFileTest, ReadV8FormatAppCache) {
static const size_t kNumHashes = 3u;
const std::array<uint64_t, kNumHashes> kHashes = {11, 22, 33};
std::array<EntryMetadata, kNumHashes> metadata_entries;
// To simulate an upgrade from v8 to v9 write out the v8 schema
// using DISK_CACHE mode. The read it back in in APP_CACHE mode.
// The entry access time data should be zeroed to reset it as the
// new trailer prefetch size.
V8IndexMetadataForTest v8_metadata(kNumHashes, 100 * 1024 * 1024);
SimpleIndex::EntrySet entries;
for (size_t i = 0; i < kNumHashes; ++i) {
metadata_entries[i] = EntryMetadata(base::Time::Now(), kHashes[i]);
metadata_entries[i].SetInMemoryData(static_cast<uint8_t>(i));
SimpleIndex::InsertInEntrySet(kHashes[i], metadata_entries[i], &entries);
}
std::unique_ptr<base::Pickle> pickle =
WrappedSimpleIndexFile::Serialize(net::DISK_CACHE, v8_metadata, entries);
ASSERT_TRUE(pickle.get() != nullptr);
base::Time now = base::Time::Now();
WrappedSimpleIndexFile::SerializeFinalData(now, pickle.get());
// Deserialize using APP_CACHE mode. This should zero out the
// trailer_prefetch_size_ instead of using the time bits written
// out previously.
base::Time when_index_last_saw_cache;
SimpleIndexLoadResult deserialize_result;
WrappedSimpleIndexFile::Deserialize(
net::APP_CACHE, *pickle, &when_index_last_saw_cache, &deserialize_result);
EXPECT_TRUE(deserialize_result.did_load);
EXPECT_EQ(now, when_index_last_saw_cache);
const SimpleIndex::EntrySet& new_entries = deserialize_result.entries;
ASSERT_EQ(entries.size(), new_entries.size());
for (size_t i = 0; i < kNumHashes; ++i) {
auto it = new_entries.find(kHashes[i]);
ASSERT_TRUE(new_entries.end() != it);
// The trailer prefetch size should be zeroed.
EXPECT_NE(metadata_entries[i].trailer_prefetch_size_,
it->second.trailer_prefetch_size_);
EXPECT_EQ(0, it->second.trailer_prefetch_size_);
// Other data should be unaffected.
EXPECT_EQ(metadata_entries[i].entry_size_256b_chunks_,
it->second.entry_size_256b_chunks_);
EXPECT_EQ(metadata_entries[i].in_memory_data_, it->second.in_memory_data_);
}
}
TEST_F(SimpleIndexFileTest, LegacyIsIndexFileStale) {
base::ScopedTempDir cache_dir;
ASSERT_TRUE(cache_dir.CreateUniqueTempDir());
base::File::Info file_info;
base::Time cache_mtime;
const base::FilePath cache_path = cache_dir.GetPath();
ASSERT_TRUE(base::GetFileInfo(cache_path, &file_info));
cache_mtime = file_info.last_modified;
WrappedSimpleIndexFile simple_index_file(cache_path);
ASSERT_TRUE(simple_index_file.CreateIndexFileDirectory());
const base::FilePath& index_path = simple_index_file.GetIndexFilePath();
EXPECT_TRUE(
WrappedSimpleIndexFile::LegacyIsIndexFileStale(cache_mtime, index_path));
const std::string kDummyData = "nothing to be seen here";
EXPECT_TRUE(base::WriteFile(index_path, kDummyData));
ASSERT_TRUE(base::GetFileInfo(cache_path, &file_info));
cache_mtime = file_info.last_modified;
EXPECT_FALSE(
WrappedSimpleIndexFile::LegacyIsIndexFileStale(cache_mtime, index_path));
const base::Time past_time = base::Time::Now() - base::Seconds(10);
EXPECT_TRUE(base::TouchFile(index_path, past_time, past_time));
EXPECT_TRUE(base::TouchFile(cache_path, past_time, past_time));
ASSERT_TRUE(base::GetFileInfo(cache_path, &file_info));
cache_mtime = file_info.last_modified;
EXPECT_FALSE(
WrappedSimpleIndexFile::LegacyIsIndexFileStale(cache_mtime, index_path));
const base::Time even_older = past_time - base::Seconds(10);
EXPECT_TRUE(base::TouchFile(index_path, even_older, even_older));
EXPECT_TRUE(
WrappedSimpleIndexFile::LegacyIsIndexFileStale(cache_mtime, index_path));
}
TEST_F(SimpleIndexFileTest, WriteThenLoadIndex) {
base::ScopedTempDir cache_dir;
ASSERT_TRUE(cache_dir.CreateUniqueTempDir());
SimpleIndex::EntrySet entries;
static const size_t kNumHashes = 3u;
const std::array<uint64_t, kNumHashes> kHashes = {11, 22, 33};
std::array<EntryMetadata, kNumHashes> metadata_entries;
for (size_t i = 0; i < kNumHashes; ++i) {
uint64_t hash = kHashes[i];
metadata_entries[i] = EntryMetadata(Time(), static_cast<uint32_t>(hash));
SimpleIndex::InsertInEntrySet(hash, metadata_entries[i], &entries);
}
const uint64_t kCacheSize = 456U;
net::TestClosure closure;
{
WrappedSimpleIndexFile simple_index_file(cache_dir.GetPath());
simple_index_file.WriteToDisk(net::DISK_CACHE,
SimpleIndex::INDEX_WRITE_REASON_SHUTDOWN,
entries, kCacheSize, closure.closure());
closure.WaitForResult();
EXPECT_TRUE(base::PathExists(simple_index_file.GetIndexFilePath()));
}
WrappedSimpleIndexFile simple_index_file(cache_dir.GetPath());
base::File::Info file_info;
ASSERT_TRUE(base::GetFileInfo(cache_dir.GetPath(), &file_info));
base::Time fake_cache_mtime = file_info.last_modified;
SimpleIndexLoadResult load_index_result;
simple_index_file.LoadIndexEntries(fake_cache_mtime, closure.closure(),
&load_index_result);
closure.WaitForResult();
EXPECT_TRUE(base::PathExists(simple_index_file.GetIndexFilePath()));
EXPECT_TRUE(load_index_result.did_load);
EXPECT_FALSE(load_index_result.flush_required);
EXPECT_EQ(kNumHashes, load_index_result.entries.size());
for (uint64_t hash : kHashes)
EXPECT_EQ(1U, load_index_result.entries.count(hash));
}
TEST_F(SimpleIndexFileTest, LoadCorruptIndex) {
base::ScopedTempDir cache_dir;
ASSERT_TRUE(cache_dir.CreateUniqueTempDir());
WrappedSimpleIndexFile simple_index_file(cache_dir.GetPath());
ASSERT_TRUE(simple_index_file.CreateIndexFileDirectory());
const base::FilePath& index_path = simple_index_file.GetIndexFilePath();
const std::string kDummyData = "nothing to be seen here";
EXPECT_TRUE(base::WriteFile(index_path, kDummyData));
base::File::Info file_info;
ASSERT_TRUE(
base::GetFileInfo(simple_index_file.GetIndexFilePath(), &file_info));
base::Time fake_cache_mtime = file_info.last_modified;
EXPECT_FALSE(WrappedSimpleIndexFile::LegacyIsIndexFileStale(fake_cache_mtime,
index_path));
SimpleIndexLoadResult load_index_result;
net::TestClosure closure;
simple_index_file.LoadIndexEntries(fake_cache_mtime, closure.closure(),
&load_index_result);
closure.WaitForResult();
EXPECT_FALSE(base::PathExists(index_path));
EXPECT_TRUE(load_index_result.did_load);
EXPECT_TRUE(load_index_result.flush_required);
}
TEST_F(SimpleIndexFileTest, LoadCorruptIndex2) {
// Variant where the index looks like a pickle, but not one with right
// header size --- that used to hit a DCHECK on debug builds.
base::ScopedTempDir cache_dir;
ASSERT_TRUE(cache_dir.CreateUniqueTempDir());
WrappedSimpleIndexFile simple_index_file(cache_dir.GetPath());
ASSERT_TRUE(simple_index_file.CreateIndexFileDirectory());
const base::FilePath& index_path = simple_index_file.GetIndexFilePath();
base::Pickle bad_payload;
bad_payload.WriteString("nothing to be seen here");
EXPECT_TRUE(base::WriteFile(index_path, bad_payload));
base::File::Info file_info;
ASSERT_TRUE(
base::GetFileInfo(simple_index_file.GetIndexFilePath(), &file_info));
base::Time fake_cache_mtime = file_info.last_modified;
EXPECT_FALSE(WrappedSimpleIndexFile::LegacyIsIndexFileStale(fake_cache_mtime,
index_path));
SimpleIndexLoadResult load_index_result;
net::TestClosure closure;
simple_index_file.LoadIndexEntries(fake_cache_mtime, closure.closure(),
&load_index_result);
closure.WaitForResult();
EXPECT_FALSE(base::PathExists(index_path));
EXPECT_TRUE(load_index_result.did_load);
EXPECT_TRUE(load_index_result.flush_required);
}
// Tests that after an upgrade the backend has the index file put in place.
TEST_F(SimpleIndexFileTest, SimpleCacheUpgrade) {
base::ScopedTempDir cache_dir;
ASSERT_TRUE(cache_dir.CreateUniqueTempDir());
const base::FilePath cache_path = cache_dir.GetPath();
// Write an old fake index file.
base::File file(cache_path.AppendASCII("index"),
base::File::FLAG_CREATE | base::File::FLAG_WRITE);
ASSERT_TRUE(file.IsValid());
disk_cache::FakeIndexData file_contents;
file_contents.initial_magic_number = disk_cache::kSimpleInitialMagicNumber;
file_contents.version = 8;
auto bytes_written = file.Write(0, base::byte_span_from_ref(file_contents));
ASSERT_EQ(sizeof(file_contents), bytes_written);
file.Close();
// Write the index file. The format is incorrect, but for transitioning from
// the old version it does not matter.
const std::string index_file_contents("incorrectly serialized data");
const base::FilePath old_index_file =
cache_path.AppendASCII("the-real-index");
ASSERT_TRUE(base::WriteFile(old_index_file, index_file_contents));
TrivialFileOperations file_operations;
// Upgrade the cache.
ASSERT_EQ(disk_cache::UpgradeSimpleCacheOnDisk(&file_operations, cache_path),
SimpleCacheConsistencyResult::kOK);
// Create the backend and initiate index flush by destroying the backend.
scoped_refptr<disk_cache::BackendCleanupTracker> cleanup_tracker =
disk_cache::BackendCleanupTracker::TryCreate(cache_path,
base::OnceClosure());
ASSERT_TRUE(cleanup_tracker != nullptr);
net::TestClosure post_cleanup;
cleanup_tracker->AddPostCleanupCallback(post_cleanup.closure());
auto simple_cache = std::make_unique<disk_cache::SimpleBackendImpl>(
/*file_operations_factory=*/nullptr, cache_path, cleanup_tracker,
/*file_tracker=*/nullptr, 0, net::DISK_CACHE,
/*net_log=*/nullptr);
net::TestCompletionCallback cb;
simple_cache->Init(cb.callback());
EXPECT_THAT(cb.WaitForResult(), IsOk());
simple_cache->index()->ExecuteWhenReady(cb.callback());
int rv = cb.WaitForResult();
EXPECT_THAT(rv, IsOk());
simple_cache.reset();
cleanup_tracker = nullptr;
// The backend flushes the index on destruction; it will run the post-cleanup
// callback set on the cleanup_tracker once that finishes.
post_cleanup.WaitForResult();
// Verify that the index file exists.
const base::FilePath& index_file_path =
cache_path.AppendASCII("index-dir").AppendASCII("the-real-index");
EXPECT_TRUE(base::PathExists(index_file_path));
// Verify that the version of the index file is correct.
std::string contents;
EXPECT_TRUE(base::ReadFileToString(index_file_path, &contents));
base::Time when_index_last_saw_cache;
SimpleIndexLoadResult deserialize_result;
WrappedSimpleIndexFile::Deserialize(
net::DISK_CACHE, base::as_byte_span(contents), &when_index_last_saw_cache,
&deserialize_result);
EXPECT_TRUE(deserialize_result.did_load);
}
TEST_F(SimpleIndexFileTest, OverwritesStaleTempFile) {
base::ScopedTempDir cache_dir;
ASSERT_TRUE(cache_dir.CreateUniqueTempDir());
const base::FilePath cache_path = cache_dir.GetPath();
WrappedSimpleIndexFile simple_index_file(cache_path);
ASSERT_TRUE(simple_index_file.CreateIndexFileDirectory());
// Create an temporary index file.
const base::FilePath& temp_index_path =
simple_index_file.GetTempIndexFilePath();
const std::string kDummyData = "nothing to be seen here";
EXPECT_TRUE(base::WriteFile(temp_index_path, kDummyData));
ASSERT_TRUE(base::PathExists(simple_index_file.GetTempIndexFilePath()));
// Write the index file.
SimpleIndex::EntrySet entries;
SimpleIndex::InsertInEntrySet(11, EntryMetadata(Time(), 11u), &entries);
net::TestClosure closure;
simple_index_file.WriteToDisk(net::DISK_CACHE,
SimpleIndex::INDEX_WRITE_REASON_SHUTDOWN,
entries, 120U, closure.closure());
closure.WaitForResult();
// Check that the temporary file was deleted and the index file was created.
EXPECT_FALSE(base::PathExists(simple_index_file.GetTempIndexFilePath()));
EXPECT_TRUE(base::PathExists(simple_index_file.GetIndexFilePath()));
}
} // namespace disk_cache
|