| 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/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 data.unused_must_be_zero2 = 0; | 40 data.unused_must_be_zero2 = 0; |
| 41 const base::FilePath file_name = cache_path.AppendASCII("index"); | 41 const base::FilePath file_name = cache_path.AppendASCII("index"); |
| 42 return sizeof(data) == | 42 return sizeof(data) == |
| 43 base::WriteFile( | 43 base::WriteFile( |
| 44 file_name, reinterpret_cast<const char*>(&data), sizeof(data)); | 44 file_name, reinterpret_cast<const char*>(&data), sizeof(data)); |
| 45 } | 45 } |
| 46 | 46 |
| 47 TEST(SimpleVersionUpgradeTest, FailsToMigrateBackwards) { | 47 TEST(SimpleVersionUpgradeTest, FailsToMigrateBackwards) { |
| 48 base::ScopedTempDir cache_dir; | 48 base::ScopedTempDir cache_dir; |
| 49 ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); | 49 ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); |
| 50 const base::FilePath cache_path = cache_dir.path(); | 50 const base::FilePath cache_path = cache_dir.GetPath(); |
| 51 | 51 |
| 52 disk_cache::FakeIndexData data; | 52 disk_cache::FakeIndexData data; |
| 53 data.version = 100500; | 53 data.version = 100500; |
| 54 data.initial_magic_number = kSimpleInitialMagicNumber; | 54 data.initial_magic_number = kSimpleInitialMagicNumber; |
| 55 data.unused_must_be_zero1 = 0; | 55 data.unused_must_be_zero1 = 0; |
| 56 data.unused_must_be_zero2 = 0; | 56 data.unused_must_be_zero2 = 0; |
| 57 const base::FilePath file_name = cache_path.AppendASCII(kFakeIndexFileName); | 57 const base::FilePath file_name = cache_path.AppendASCII(kFakeIndexFileName); |
| 58 ASSERT_EQ(static_cast<int>(sizeof(data)), | 58 ASSERT_EQ(static_cast<int>(sizeof(data)), |
| 59 base::WriteFile(file_name, reinterpret_cast<const char*>(&data), | 59 base::WriteFile(file_name, reinterpret_cast<const char*>(&data), |
| 60 sizeof(data))); | 60 sizeof(data))); |
| 61 EXPECT_FALSE(disk_cache::UpgradeSimpleCacheOnDisk(cache_dir.path())); | 61 EXPECT_FALSE(disk_cache::UpgradeSimpleCacheOnDisk(cache_dir.GetPath())); |
| 62 } | 62 } |
| 63 | 63 |
| 64 TEST(SimpleVersionUpgradeTest, FakeIndexVersionGetsUpdated) { | 64 TEST(SimpleVersionUpgradeTest, FakeIndexVersionGetsUpdated) { |
| 65 base::ScopedTempDir cache_dir; | 65 base::ScopedTempDir cache_dir; |
| 66 ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); | 66 ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); |
| 67 const base::FilePath cache_path = cache_dir.path(); | 67 const base::FilePath cache_path = cache_dir.GetPath(); |
| 68 | 68 |
| 69 WriteFakeIndexFileV5(cache_path); | 69 WriteFakeIndexFileV5(cache_path); |
| 70 const std::string file_contents("incorrectly serialized data"); | 70 const std::string file_contents("incorrectly serialized data"); |
| 71 const base::FilePath index_file = cache_path.AppendASCII(kIndexFileName); | 71 const base::FilePath index_file = cache_path.AppendASCII(kIndexFileName); |
| 72 ASSERT_EQ( | 72 ASSERT_EQ( |
| 73 static_cast<int>(file_contents.size()), | 73 static_cast<int>(file_contents.size()), |
| 74 base::WriteFile(index_file, file_contents.data(), file_contents.size())); | 74 base::WriteFile(index_file, file_contents.data(), file_contents.size())); |
| 75 | 75 |
| 76 // Upgrade. | 76 // Upgrade. |
| 77 ASSERT_TRUE(disk_cache::UpgradeSimpleCacheOnDisk(cache_path)); | 77 ASSERT_TRUE(disk_cache::UpgradeSimpleCacheOnDisk(cache_path)); |
| 78 | 78 |
| 79 // Check that the version in the fake index file is updated. | 79 // Check that the version in the fake index file is updated. |
| 80 std::string new_fake_index_contents; | 80 std::string new_fake_index_contents; |
| 81 ASSERT_TRUE(base::ReadFileToString(cache_path.AppendASCII(kFakeIndexFileName), | 81 ASSERT_TRUE(base::ReadFileToString(cache_path.AppendASCII(kFakeIndexFileName), |
| 82 &new_fake_index_contents)); | 82 &new_fake_index_contents)); |
| 83 const disk_cache::FakeIndexData* fake_index_header; | 83 const disk_cache::FakeIndexData* fake_index_header; |
| 84 EXPECT_EQ(sizeof(*fake_index_header), new_fake_index_contents.size()); | 84 EXPECT_EQ(sizeof(*fake_index_header), new_fake_index_contents.size()); |
| 85 fake_index_header = reinterpret_cast<const disk_cache::FakeIndexData*>( | 85 fake_index_header = reinterpret_cast<const disk_cache::FakeIndexData*>( |
| 86 new_fake_index_contents.data()); | 86 new_fake_index_contents.data()); |
| 87 EXPECT_EQ(disk_cache::kSimpleVersion, fake_index_header->version); | 87 EXPECT_EQ(disk_cache::kSimpleVersion, fake_index_header->version); |
| 88 EXPECT_EQ(kSimpleInitialMagicNumber, fake_index_header->initial_magic_number); | 88 EXPECT_EQ(kSimpleInitialMagicNumber, fake_index_header->initial_magic_number); |
| 89 } | 89 } |
| 90 | 90 |
| 91 TEST(SimpleVersionUpgradeTest, UpgradeV5V6IndexMustDisappear) { | 91 TEST(SimpleVersionUpgradeTest, UpgradeV5V6IndexMustDisappear) { |
| 92 base::ScopedTempDir cache_dir; | 92 base::ScopedTempDir cache_dir; |
| 93 ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); | 93 ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); |
| 94 const base::FilePath cache_path = cache_dir.path(); | 94 const base::FilePath cache_path = cache_dir.GetPath(); |
| 95 | 95 |
| 96 WriteFakeIndexFileV5(cache_path); | 96 WriteFakeIndexFileV5(cache_path); |
| 97 const std::string file_contents("incorrectly serialized data"); | 97 const std::string file_contents("incorrectly serialized data"); |
| 98 const base::FilePath index_file = cache_path.AppendASCII(kIndexFileName); | 98 const base::FilePath index_file = cache_path.AppendASCII(kIndexFileName); |
| 99 ASSERT_EQ( | 99 ASSERT_EQ( |
| 100 static_cast<int>(file_contents.size()), | 100 static_cast<int>(file_contents.size()), |
| 101 base::WriteFile(index_file, file_contents.data(), file_contents.size())); | 101 base::WriteFile(index_file, file_contents.data(), file_contents.size())); |
| 102 | 102 |
| 103 // Create a few entry-like files. | 103 // Create a few entry-like files. |
| 104 const uint64_t kEntries = 5; | 104 const uint64_t kEntries = 5; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 131 EXPECT_TRUE(base::ReadFileToString(cache_path.AppendASCII(file_name), | 131 EXPECT_TRUE(base::ReadFileToString(cache_path.AppendASCII(file_name), |
| 132 &real_contents)); | 132 &real_contents)); |
| 133 EXPECT_EQ(expected_contents, real_contents); | 133 EXPECT_EQ(expected_contents, real_contents); |
| 134 } | 134 } |
| 135 } | 135 } |
| 136 } | 136 } |
| 137 | 137 |
| 138 } // namespace | 138 } // namespace |
| 139 | 139 |
| 140 #endif // defined(OS_POSIX) | 140 #endif // defined(OS_POSIX) |
| OLD | NEW |