| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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_version_upgrade.h" | 5 #include "net/disk_cache/simple/simple_version_upgrade.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 base::ScopedTempDir cache_dir; | 49 base::ScopedTempDir cache_dir; |
| 50 ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); | 50 ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); |
| 51 const base::FilePath cache_path = cache_dir.path(); | 51 const base::FilePath cache_path = cache_dir.path(); |
| 52 | 52 |
| 53 disk_cache::FakeIndexData data; | 53 disk_cache::FakeIndexData data; |
| 54 data.version = 100500; | 54 data.version = 100500; |
| 55 data.initial_magic_number = kSimpleInitialMagicNumber; | 55 data.initial_magic_number = kSimpleInitialMagicNumber; |
| 56 data.unused_must_be_zero1 = 0; | 56 data.unused_must_be_zero1 = 0; |
| 57 data.unused_must_be_zero2 = 0; | 57 data.unused_must_be_zero2 = 0; |
| 58 const base::FilePath file_name = cache_path.AppendASCII(kFakeIndexFileName); | 58 const base::FilePath file_name = cache_path.AppendASCII(kFakeIndexFileName); |
| 59 ASSERT_EQ(implicit_cast<int>(sizeof(data)), | 59 ASSERT_EQ(static_cast<int>(sizeof(data)), |
| 60 base::WriteFile( | 60 base::WriteFile(file_name, reinterpret_cast<const char*>(&data), |
| 61 file_name, reinterpret_cast<const char*>(&data), sizeof(data))); | 61 sizeof(data))); |
| 62 EXPECT_FALSE(disk_cache::UpgradeSimpleCacheOnDisk(cache_dir.path())); | 62 EXPECT_FALSE(disk_cache::UpgradeSimpleCacheOnDisk(cache_dir.path())); |
| 63 } | 63 } |
| 64 | 64 |
| 65 TEST(SimpleVersionUpgradeTest, FakeIndexVersionGetsUpdated) { | 65 TEST(SimpleVersionUpgradeTest, FakeIndexVersionGetsUpdated) { |
| 66 base::ScopedTempDir cache_dir; | 66 base::ScopedTempDir cache_dir; |
| 67 ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); | 67 ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); |
| 68 const base::FilePath cache_path = cache_dir.path(); | 68 const base::FilePath cache_path = cache_dir.path(); |
| 69 | 69 |
| 70 WriteFakeIndexFileV5(cache_path); | 70 WriteFakeIndexFileV5(cache_path); |
| 71 const std::string file_contents("incorrectly serialized data"); | 71 const std::string file_contents("incorrectly serialized data"); |
| 72 const base::FilePath index_file = cache_path.AppendASCII(kIndexFileName); | 72 const base::FilePath index_file = cache_path.AppendASCII(kIndexFileName); |
| 73 ASSERT_EQ(implicit_cast<int>(file_contents.size()), | 73 ASSERT_EQ( |
| 74 base::WriteFile( | 74 static_cast<int>(file_contents.size()), |
| 75 index_file, file_contents.data(), file_contents.size())); | 75 base::WriteFile(index_file, file_contents.data(), file_contents.size())); |
| 76 | 76 |
| 77 // Upgrade. | 77 // Upgrade. |
| 78 ASSERT_TRUE(disk_cache::UpgradeSimpleCacheOnDisk(cache_path)); | 78 ASSERT_TRUE(disk_cache::UpgradeSimpleCacheOnDisk(cache_path)); |
| 79 | 79 |
| 80 // Check that the version in the fake index file is updated. | 80 // Check that the version in the fake index file is updated. |
| 81 std::string new_fake_index_contents; | 81 std::string new_fake_index_contents; |
| 82 ASSERT_TRUE(base::ReadFileToString(cache_path.AppendASCII(kFakeIndexFileName), | 82 ASSERT_TRUE(base::ReadFileToString(cache_path.AppendASCII(kFakeIndexFileName), |
| 83 &new_fake_index_contents)); | 83 &new_fake_index_contents)); |
| 84 const disk_cache::FakeIndexData* fake_index_header; | 84 const disk_cache::FakeIndexData* fake_index_header; |
| 85 EXPECT_EQ(sizeof(*fake_index_header), new_fake_index_contents.size()); | 85 EXPECT_EQ(sizeof(*fake_index_header), new_fake_index_contents.size()); |
| 86 fake_index_header = reinterpret_cast<const disk_cache::FakeIndexData*>( | 86 fake_index_header = reinterpret_cast<const disk_cache::FakeIndexData*>( |
| 87 new_fake_index_contents.data()); | 87 new_fake_index_contents.data()); |
| 88 EXPECT_EQ(disk_cache::kSimpleVersion, fake_index_header->version); | 88 EXPECT_EQ(disk_cache::kSimpleVersion, fake_index_header->version); |
| 89 EXPECT_EQ(kSimpleInitialMagicNumber, fake_index_header->initial_magic_number); | 89 EXPECT_EQ(kSimpleInitialMagicNumber, fake_index_header->initial_magic_number); |
| 90 } | 90 } |
| 91 | 91 |
| 92 TEST(SimpleVersionUpgradeTest, UpgradeV5V6IndexMustDisappear) { | 92 TEST(SimpleVersionUpgradeTest, UpgradeV5V6IndexMustDisappear) { |
| 93 base::ScopedTempDir cache_dir; | 93 base::ScopedTempDir cache_dir; |
| 94 ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); | 94 ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); |
| 95 const base::FilePath cache_path = cache_dir.path(); | 95 const base::FilePath cache_path = cache_dir.path(); |
| 96 | 96 |
| 97 WriteFakeIndexFileV5(cache_path); | 97 WriteFakeIndexFileV5(cache_path); |
| 98 const std::string file_contents("incorrectly serialized data"); | 98 const std::string file_contents("incorrectly serialized data"); |
| 99 const base::FilePath index_file = cache_path.AppendASCII(kIndexFileName); | 99 const base::FilePath index_file = cache_path.AppendASCII(kIndexFileName); |
| 100 ASSERT_EQ(implicit_cast<int>(file_contents.size()), | 100 ASSERT_EQ( |
| 101 base::WriteFile( | 101 static_cast<int>(file_contents.size()), |
| 102 index_file, file_contents.data(), file_contents.size())); | 102 base::WriteFile(index_file, file_contents.data(), file_contents.size())); |
| 103 | 103 |
| 104 // Create a few entry-like files. | 104 // Create a few entry-like files. |
| 105 const uint64 kEntries = 5; | 105 const uint64 kEntries = 5; |
| 106 for (uint64 entry_hash = 0; entry_hash < kEntries; ++entry_hash) { | 106 for (uint64 entry_hash = 0; entry_hash < kEntries; ++entry_hash) { |
| 107 for (int index = 0; index < 3; ++index) { | 107 for (int index = 0; index < 3; ++index) { |
| 108 std::string file_name = | 108 std::string file_name = |
| 109 base::StringPrintf("%016" PRIx64 "_%1d", entry_hash, index); | 109 base::StringPrintf("%016" PRIx64 "_%1d", entry_hash, index); |
| 110 std::string entry_contents = | 110 std::string entry_contents = |
| 111 file_contents + | 111 file_contents + |
| 112 base::StringPrintf(" %" PRIx64, implicit_cast<uint64>(entry_hash)); | 112 base::StringPrintf(" %" PRIx64, static_cast<uint64>(entry_hash)); |
| 113 ASSERT_EQ(implicit_cast<int>(entry_contents.size()), | 113 ASSERT_EQ(static_cast<int>(entry_contents.size()), |
| 114 base::WriteFile(cache_path.AppendASCII(file_name), | 114 base::WriteFile(cache_path.AppendASCII(file_name), |
| 115 entry_contents.data(), | 115 entry_contents.data(), entry_contents.size())); |
| 116 entry_contents.size())); | |
| 117 } | 116 } |
| 118 } | 117 } |
| 119 | 118 |
| 120 // Upgrade. | 119 // Upgrade. |
| 121 ASSERT_TRUE(disk_cache::UpgradeIndexV5V6(cache_path)); | 120 ASSERT_TRUE(disk_cache::UpgradeIndexV5V6(cache_path)); |
| 122 | 121 |
| 123 // Check that the old index disappeared but the files remain unchanged. | 122 // Check that the old index disappeared but the files remain unchanged. |
| 124 EXPECT_FALSE(base::PathExists(index_file)); | 123 EXPECT_FALSE(base::PathExists(index_file)); |
| 125 for (uint64 entry_hash = 0; entry_hash < kEntries; ++entry_hash) { | 124 for (uint64 entry_hash = 0; entry_hash < kEntries; ++entry_hash) { |
| 126 for (int index = 0; index < 3; ++index) { | 125 for (int index = 0; index < 3; ++index) { |
| 127 std::string file_name = | 126 std::string file_name = |
| 128 base::StringPrintf("%016" PRIx64 "_%1d", entry_hash, index); | 127 base::StringPrintf("%016" PRIx64 "_%1d", entry_hash, index); |
| 129 std::string expected_contents = | 128 std::string expected_contents = |
| 130 file_contents + | 129 file_contents + |
| 131 base::StringPrintf(" %" PRIx64, implicit_cast<uint64>(entry_hash)); | 130 base::StringPrintf(" %" PRIx64, static_cast<uint64>(entry_hash)); |
| 132 std::string real_contents; | 131 std::string real_contents; |
| 133 EXPECT_TRUE(base::ReadFileToString(cache_path.AppendASCII(file_name), | 132 EXPECT_TRUE(base::ReadFileToString(cache_path.AppendASCII(file_name), |
| 134 &real_contents)); | 133 &real_contents)); |
| 135 EXPECT_EQ(expected_contents, real_contents); | 134 EXPECT_EQ(expected_contents, real_contents); |
| 136 } | 135 } |
| 137 } | 136 } |
| 138 } | 137 } |
| 139 | 138 |
| 140 } // namespace | 139 } // namespace |
| 141 | 140 |
| 142 #endif // defined(OS_POSIX) | 141 #endif // defined(OS_POSIX) |
| OLD | NEW |