Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / net / disk_cache / simple / simple_version_upgrade_unittest.cc
blobfebf527573470bf066162416294a3736423bce05
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/disk_cache/simple/simple_version_upgrade.h"
7 #include <stdint.h>
9 #include "base/basictypes.h"
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/format_macros.h"
14 #include "base/strings/stringprintf.h"
15 #include "net/base/net_errors.h"
16 #include "net/disk_cache/simple/simple_backend_version.h"
17 #include "net/disk_cache/simple/simple_entry_format_history.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 // The migration process relies on ability to rename newly created files, which
21 // could be problematic on Windows XP.
22 #if defined(OS_POSIX)
24 namespace {
26 // Same as |disk_cache::kSimpleInitialMagicNumber|.
27 const uint64 kSimpleInitialMagicNumber = UINT64_C(0xfcfb6d1ba7725c30);
29 // The "fake index" file that cache backends use to distinguish whether the
30 // cache belongs to one backend or another.
31 const char kFakeIndexFileName[] = "index";
33 // Same as |SimpleIndexFile::kIndexFileName|.
34 const char kIndexFileName[] = "the-real-index";
36 bool WriteFakeIndexFileV5(const base::FilePath& cache_path) {
37 disk_cache::FakeIndexData data;
38 data.version = 5;
39 data.initial_magic_number = kSimpleInitialMagicNumber;
40 data.unused_must_be_zero1 = 0;
41 data.unused_must_be_zero2 = 0;
42 const base::FilePath file_name = cache_path.AppendASCII("index");
43 return sizeof(data) ==
44 base::WriteFile(
45 file_name, reinterpret_cast<const char*>(&data), sizeof(data));
48 TEST(SimpleVersionUpgradeTest, FailsToMigrateBackwards) {
49 base::ScopedTempDir cache_dir;
50 ASSERT_TRUE(cache_dir.CreateUniqueTempDir());
51 const base::FilePath cache_path = cache_dir.path();
53 disk_cache::FakeIndexData data;
54 data.version = 100500;
55 data.initial_magic_number = kSimpleInitialMagicNumber;
56 data.unused_must_be_zero1 = 0;
57 data.unused_must_be_zero2 = 0;
58 const base::FilePath file_name = cache_path.AppendASCII(kFakeIndexFileName);
59 ASSERT_EQ(implicit_cast<int>(sizeof(data)),
60 base::WriteFile(
61 file_name, reinterpret_cast<const char*>(&data), sizeof(data)));
62 EXPECT_FALSE(disk_cache::UpgradeSimpleCacheOnDisk(cache_dir.path()));
65 TEST(SimpleVersionUpgradeTest, FakeIndexVersionGetsUpdated) {
66 base::ScopedTempDir cache_dir;
67 ASSERT_TRUE(cache_dir.CreateUniqueTempDir());
68 const base::FilePath cache_path = cache_dir.path();
70 WriteFakeIndexFileV5(cache_path);
71 const std::string file_contents("incorrectly serialized data");
72 const base::FilePath index_file = cache_path.AppendASCII(kIndexFileName);
73 ASSERT_EQ(implicit_cast<int>(file_contents.size()),
74 base::WriteFile(
75 index_file, file_contents.data(), file_contents.size()));
77 // Upgrade.
78 ASSERT_TRUE(disk_cache::UpgradeSimpleCacheOnDisk(cache_path));
80 // Check that the version in the fake index file is updated.
81 std::string new_fake_index_contents;
82 ASSERT_TRUE(base::ReadFileToString(cache_path.AppendASCII(kFakeIndexFileName),
83 &new_fake_index_contents));
84 const disk_cache::FakeIndexData* fake_index_header;
85 EXPECT_EQ(sizeof(*fake_index_header), new_fake_index_contents.size());
86 fake_index_header = reinterpret_cast<const disk_cache::FakeIndexData*>(
87 new_fake_index_contents.data());
88 EXPECT_EQ(disk_cache::kSimpleVersion, fake_index_header->version);
89 EXPECT_EQ(kSimpleInitialMagicNumber, fake_index_header->initial_magic_number);
92 TEST(SimpleVersionUpgradeTest, UpgradeV5V6IndexMustDisappear) {
93 base::ScopedTempDir cache_dir;
94 ASSERT_TRUE(cache_dir.CreateUniqueTempDir());
95 const base::FilePath cache_path = cache_dir.path();
97 WriteFakeIndexFileV5(cache_path);
98 const std::string file_contents("incorrectly serialized data");
99 const base::FilePath index_file = cache_path.AppendASCII(kIndexFileName);
100 ASSERT_EQ(implicit_cast<int>(file_contents.size()),
101 base::WriteFile(
102 index_file, file_contents.data(), file_contents.size()));
104 // Create a few entry-like files.
105 const uint64 kEntries = 5;
106 for (uint64 entry_hash = 0; entry_hash < kEntries; ++entry_hash) {
107 for (int index = 0; index < 3; ++index) {
108 std::string file_name =
109 base::StringPrintf("%016" PRIx64 "_%1d", entry_hash, index);
110 std::string entry_contents =
111 file_contents +
112 base::StringPrintf(" %" PRIx64, implicit_cast<uint64>(entry_hash));
113 ASSERT_EQ(implicit_cast<int>(entry_contents.size()),
114 base::WriteFile(cache_path.AppendASCII(file_name),
115 entry_contents.data(),
116 entry_contents.size()));
120 // Upgrade.
121 ASSERT_TRUE(disk_cache::UpgradeIndexV5V6(cache_path));
123 // Check that the old index disappeared but the files remain unchanged.
124 EXPECT_FALSE(base::PathExists(index_file));
125 for (uint64 entry_hash = 0; entry_hash < kEntries; ++entry_hash) {
126 for (int index = 0; index < 3; ++index) {
127 std::string file_name =
128 base::StringPrintf("%016" PRIx64 "_%1d", entry_hash, index);
129 std::string expected_contents =
130 file_contents +
131 base::StringPrintf(" %" PRIx64, implicit_cast<uint64>(entry_hash));
132 std::string real_contents;
133 EXPECT_TRUE(base::ReadFileToString(cache_path.AppendASCII(file_name),
134 &real_contents));
135 EXPECT_EQ(expected_contents, real_contents);
140 } // namespace
142 #endif // defined(OS_POSIX)