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
|
// Copyright 2013 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_version_upgrade.h"
#include <stdint.h>
#include <string>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/format_macros.h"
#include "base/strings/stringprintf.h"
#include "net/base/net_errors.h"
#include "net/disk_cache/disk_cache.h"
#include "net/disk_cache/simple/simple_backend_version.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
// Same as |disk_cache::kSimpleInitialMagicNumber|.
const uint64_t kSimpleInitialMagicNumber = UINT64_C(0xfcfb6d1ba7725c30);
// The "fake index" file that cache backends use to distinguish whether the
// cache belongs to one backend or another.
const char kFakeIndexFileName[] = "index";
// Same as |SimpleIndexFile::kIndexDirectory|.
const char kIndexDirName[] = "index-dir";
// Same as |SimpleIndexFile::kIndexFileName|.
const char kIndexFileName[] = "the-real-index";
bool WriteFakeIndexFileV8(const base::FilePath& cache_path) {
disk_cache::FakeIndexData data;
data.version = 8;
data.initial_magic_number = kSimpleInitialMagicNumber;
data.zero = 0;
data.zero2 = 0;
const base::FilePath file_name = cache_path.AppendASCII("index");
return base::WriteFile(file_name, base::byte_span_from_ref(data));
}
TEST(SimpleVersionUpgradeTest, FailsToMigrateBackwards) {
base::ScopedTempDir cache_dir;
ASSERT_TRUE(cache_dir.CreateUniqueTempDir());
const base::FilePath cache_path = cache_dir.GetPath();
disk_cache::FakeIndexData data;
data.version = 100500;
data.initial_magic_number = kSimpleInitialMagicNumber;
data.zero = 0;
data.zero2 = 0;
const base::FilePath file_name = cache_path.AppendASCII(kFakeIndexFileName);
ASSERT_TRUE(base::WriteFile(file_name, base::byte_span_from_ref(data)));
disk_cache::TrivialFileOperations file_operations;
EXPECT_EQ(disk_cache::SimpleCacheConsistencyResult::kVersionFromTheFuture,
disk_cache::UpgradeSimpleCacheOnDisk(&file_operations,
cache_dir.GetPath()));
}
TEST(SimpleVersionUpgradeTest, ExperimentBacktoDefault) {
base::ScopedTempDir cache_dir;
ASSERT_TRUE(cache_dir.CreateUniqueTempDir());
const base::FilePath cache_path = cache_dir.GetPath();
disk_cache::FakeIndexData data;
data.version = disk_cache::kSimpleVersion;
data.initial_magic_number = kSimpleInitialMagicNumber;
data.zero = 2;
data.zero2 = 4;
const base::FilePath file_name = cache_path.AppendASCII(kFakeIndexFileName);
ASSERT_TRUE(base::WriteFile(file_name, base::byte_span_from_ref(data)));
disk_cache::TrivialFileOperations file_operations;
// The cache needs to transition from a deprecated experiment back to not
// having one.
EXPECT_EQ(disk_cache::SimpleCacheConsistencyResult::kBadZeroCheck,
disk_cache::UpgradeSimpleCacheOnDisk(&file_operations,
cache_dir.GetPath()));
}
TEST(SimpleVersionUpgradeTest, FakeIndexVersionGetsUpdated) {
base::ScopedTempDir cache_dir;
ASSERT_TRUE(cache_dir.CreateUniqueTempDir());
const base::FilePath cache_path = cache_dir.GetPath();
ASSERT_TRUE(WriteFakeIndexFileV8(cache_path));
const std::string file_contents("incorrectly serialized data");
const base::FilePath index_file = cache_path.AppendASCII(kIndexFileName);
ASSERT_TRUE(base::WriteFile(index_file, file_contents));
disk_cache::TrivialFileOperations file_operations;
// Upgrade.
ASSERT_EQ(disk_cache::SimpleCacheConsistencyResult::kOK,
disk_cache::UpgradeSimpleCacheOnDisk(&file_operations, cache_path));
// Check that the version in the fake index file is updated.
std::string new_fake_index_contents;
ASSERT_TRUE(base::ReadFileToString(cache_path.AppendASCII(kFakeIndexFileName),
&new_fake_index_contents));
const disk_cache::FakeIndexData* fake_index_header;
EXPECT_EQ(sizeof(*fake_index_header), new_fake_index_contents.size());
fake_index_header = reinterpret_cast<const disk_cache::FakeIndexData*>(
new_fake_index_contents.data());
EXPECT_EQ(disk_cache::kSimpleVersion, fake_index_header->version);
EXPECT_EQ(kSimpleInitialMagicNumber, fake_index_header->initial_magic_number);
}
TEST(SimpleVersionUpgradeTest, DeleteAllIndexFilesWhenCacheIsEmpty) {
const std::string kCorruptData("corrupt");
base::ScopedTempDir cache_dir;
ASSERT_TRUE(cache_dir.CreateUniqueTempDir());
const base::FilePath cache_path = cache_dir.GetPath();
const base::FilePath fake_index = cache_path.AppendASCII(kFakeIndexFileName);
ASSERT_TRUE(base::WriteFile(fake_index, kCorruptData));
const base::FilePath index_path = cache_path.AppendASCII(kIndexDirName);
ASSERT_TRUE(base::CreateDirectory(index_path));
const base::FilePath index = index_path.AppendASCII(kIndexFileName);
ASSERT_TRUE(base::WriteFile(index, kCorruptData));
EXPECT_TRUE(disk_cache::DeleteIndexFilesIfCacheIsEmpty(cache_path));
EXPECT_TRUE(base::PathExists(cache_path));
EXPECT_TRUE(base::IsDirectoryEmpty(cache_path));
}
TEST(SimpleVersionUpgradeTest, DoesNotDeleteIndexFilesWhenCacheIsNotEmpty) {
const std::string kCorruptData("corrupt");
base::ScopedTempDir cache_dir;
ASSERT_TRUE(cache_dir.CreateUniqueTempDir());
const base::FilePath cache_path = cache_dir.GetPath();
const base::FilePath fake_index = cache_path.AppendASCII(kFakeIndexFileName);
ASSERT_TRUE(base::WriteFile(fake_index, kCorruptData));
const base::FilePath index_path = cache_path.AppendASCII(kIndexDirName);
ASSERT_TRUE(base::CreateDirectory(index_path));
const base::FilePath index = index_path.AppendASCII(kIndexFileName);
ASSERT_TRUE(base::WriteFile(index, kCorruptData));
const base::FilePath entry_file = cache_path.AppendASCII("01234567_0");
ASSERT_TRUE(base::WriteFile(entry_file, kCorruptData));
EXPECT_FALSE(disk_cache::DeleteIndexFilesIfCacheIsEmpty(cache_path));
EXPECT_TRUE(base::PathExists(cache_path));
EXPECT_FALSE(base::IsDirectoryEmpty(cache_path));
EXPECT_TRUE(base::PathExists(fake_index));
EXPECT_TRUE(base::PathExists(index_path));
EXPECT_TRUE(base::PathExists(index));
EXPECT_TRUE(base::PathExists(entry_file));
}
} // namespace
|