Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_index_file.h" | 5 #include "net/disk_cache/simple/simple_index_file.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 38 using disk_cache::SimpleIndexFile; | 38 using disk_cache::SimpleIndexFile; |
| 39 using disk_cache::SimpleIndex; | 39 using disk_cache::SimpleIndex; |
| 40 | 40 |
| 41 namespace disk_cache { | 41 namespace disk_cache { |
| 42 | 42 |
| 43 // The Simple Cache backend requires a few guarantees from the filesystem like | 43 // The Simple Cache backend requires a few guarantees from the filesystem like |
| 44 // atomic renaming of recently open files. Those guarantees are not provided in | 44 // atomic renaming of recently open files. Those guarantees are not provided in |
| 45 // general on Windows. | 45 // general on Windows. |
| 46 #if defined(OS_POSIX) | 46 #if defined(OS_POSIX) |
| 47 | 47 |
| 48 namespace { | |
| 49 | |
| 50 uint32_t RoundSize(uint32_t in) { | |
| 51 return (in + 255u) & 0xFFFFFF00u; | |
|
gavinp
2017/08/04 18:42:54
Nit: mixing decimal and hex here is a bit offputti
Maks Orlovich
2017/08/23 19:29:06
Done.
| |
| 52 } | |
| 53 | |
| 54 } // namespace | |
| 55 | |
| 48 TEST(IndexMetadataTest, Basics) { | 56 TEST(IndexMetadataTest, Basics) { |
| 49 SimpleIndexFile::IndexMetadata index_metadata; | 57 SimpleIndexFile::IndexMetadata index_metadata; |
| 50 | 58 |
| 51 EXPECT_EQ(disk_cache::kSimpleIndexMagicNumber, index_metadata.magic_number_); | 59 EXPECT_EQ(disk_cache::kSimpleIndexMagicNumber, index_metadata.magic_number_); |
| 52 EXPECT_EQ(disk_cache::kSimpleVersion, index_metadata.version_); | 60 EXPECT_EQ(disk_cache::kSimpleVersion, index_metadata.version_); |
| 53 EXPECT_EQ(0U, index_metadata.entry_count()); | 61 EXPECT_EQ(0U, index_metadata.entry_count()); |
| 54 EXPECT_EQ(0U, index_metadata.cache_size_); | 62 EXPECT_EQ(0U, index_metadata.cache_size_); |
| 55 | 63 |
| 56 // Without setting a |reason_|, the index metadata isn't valid. | 64 // Without setting a |reason_|, the index metadata isn't valid. |
| 57 index_metadata.reason_ = SimpleIndex::INDEX_WRITE_REASON_SHUTDOWN; | 65 index_metadata.reason_ = SimpleIndex::INDEX_WRITE_REASON_SHUTDOWN; |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 113 EXPECT_EQ(new_index_metadata.magic_number_, v6_index_metadata.magic_number_); | 121 EXPECT_EQ(new_index_metadata.magic_number_, v6_index_metadata.magic_number_); |
| 114 EXPECT_EQ(new_index_metadata.version_, v6_index_metadata.version_); | 122 EXPECT_EQ(new_index_metadata.version_, v6_index_metadata.version_); |
| 115 | 123 |
| 116 EXPECT_EQ(new_index_metadata.reason_, SimpleIndex::INDEX_WRITE_REASON_MAX); | 124 EXPECT_EQ(new_index_metadata.reason_, SimpleIndex::INDEX_WRITE_REASON_MAX); |
| 117 EXPECT_EQ(new_index_metadata.entry_count(), v6_index_metadata.entry_count()); | 125 EXPECT_EQ(new_index_metadata.entry_count(), v6_index_metadata.entry_count()); |
| 118 EXPECT_EQ(new_index_metadata.cache_size_, v6_index_metadata.cache_size_); | 126 EXPECT_EQ(new_index_metadata.cache_size_, v6_index_metadata.cache_size_); |
| 119 | 127 |
| 120 EXPECT_TRUE(new_index_metadata.CheckIndexMetadata()); | 128 EXPECT_TRUE(new_index_metadata.CheckIndexMetadata()); |
| 121 } | 129 } |
| 122 | 130 |
| 131 // This derived index metadata class allows us to serialize the older V7 format | |
| 132 // of the index metadata, thus allowing us to test deserializing the old format. | |
| 133 class V7IndexMetadataForTest : public SimpleIndexFile::IndexMetadata { | |
| 134 public: | |
| 135 V7IndexMetadataForTest(uint64_t entry_count, uint64_t cache_size) | |
| 136 : SimpleIndexFile::IndexMetadata(SimpleIndex::INDEX_WRITE_REASON_SHUTDOWN, | |
| 137 entry_count, | |
| 138 cache_size) { | |
| 139 version_ = 7; | |
| 140 } | |
| 141 }; | |
| 142 | |
| 123 // This friend derived class is able to reexport its ancestors private methods | 143 // This friend derived class is able to reexport its ancestors private methods |
| 124 // as public, for use in tests. | 144 // as public, for use in tests. |
| 125 class WrappedSimpleIndexFile : public SimpleIndexFile { | 145 class WrappedSimpleIndexFile : public SimpleIndexFile { |
| 126 public: | 146 public: |
| 127 using SimpleIndexFile::Deserialize; | 147 using SimpleIndexFile::Deserialize; |
| 128 using SimpleIndexFile::LegacyIsIndexFileStale; | 148 using SimpleIndexFile::LegacyIsIndexFileStale; |
| 129 using SimpleIndexFile::Serialize; | 149 using SimpleIndexFile::Serialize; |
| 130 using SimpleIndexFile::SerializeFinalData; | 150 using SimpleIndexFile::SerializeFinalData; |
| 131 | 151 |
| 132 explicit WrappedSimpleIndexFile(const base::FilePath& index_file_directory) | 152 explicit WrappedSimpleIndexFile(const base::FilePath& index_file_directory) |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 145 } | 165 } |
| 146 | 166 |
| 147 bool CreateIndexFileDirectory() const { | 167 bool CreateIndexFileDirectory() const { |
| 148 return base::CreateDirectory(index_file_.DirName()); | 168 return base::CreateDirectory(index_file_.DirName()); |
| 149 } | 169 } |
| 150 }; | 170 }; |
| 151 | 171 |
| 152 class SimpleIndexFileTest : public testing::Test { | 172 class SimpleIndexFileTest : public testing::Test { |
| 153 public: | 173 public: |
| 154 bool CompareTwoEntryMetadata(const EntryMetadata& a, const EntryMetadata& b) { | 174 bool CompareTwoEntryMetadata(const EntryMetadata& a, const EntryMetadata& b) { |
| 155 return | 175 return a.last_used_time_seconds_since_epoch_ == |
| 156 a.last_used_time_seconds_since_epoch_ == | 176 b.last_used_time_seconds_since_epoch_ && |
| 157 b.last_used_time_seconds_since_epoch_ && | 177 a.entry_size_ == b.entry_size_ && |
| 158 a.entry_size_ == b.entry_size_; | 178 a.memory_entry_data_ == b.memory_entry_data_; |
| 159 } | 179 } |
| 160 }; | 180 }; |
| 161 | 181 |
| 162 TEST_F(SimpleIndexFileTest, Serialize) { | 182 TEST_F(SimpleIndexFileTest, Serialize) { |
| 163 SimpleIndex::EntrySet entries; | 183 SimpleIndex::EntrySet entries; |
| 164 static const uint64_t kHashes[] = {11, 22, 33}; | 184 static const uint64_t kHashes[] = {11, 22, 33}; |
| 165 static const size_t kNumHashes = arraysize(kHashes); | 185 static const size_t kNumHashes = arraysize(kHashes); |
| 166 EntryMetadata metadata_entries[kNumHashes]; | 186 EntryMetadata metadata_entries[kNumHashes]; |
| 167 | 187 |
| 168 SimpleIndexFile::IndexMetadata index_metadata( | 188 SimpleIndexFile::IndexMetadata index_metadata( |
| 169 SimpleIndex::INDEX_WRITE_REASON_SHUTDOWN, | 189 SimpleIndex::INDEX_WRITE_REASON_SHUTDOWN, |
| 170 static_cast<uint64_t>(kNumHashes), 456); | 190 static_cast<uint64_t>(kNumHashes), 456); |
| 171 for (size_t i = 0; i < kNumHashes; ++i) { | 191 for (size_t i = 0; i < kNumHashes; ++i) { |
| 172 uint64_t hash = kHashes[i]; | 192 uint64_t hash = kHashes[i]; |
| 173 // TODO(eroman): Should restructure the test so no casting here (and same | 193 // TODO(eroman): Should restructure the test so no casting here (and same |
| 174 // elsewhere where a hash is cast to an entry size). | 194 // elsewhere where a hash is cast to an entry size). |
| 175 metadata_entries[i] = EntryMetadata(Time(), static_cast<uint32_t>(hash)); | 195 metadata_entries[i] = EntryMetadata(Time(), static_cast<uint32_t>(hash)); |
| 196 metadata_entries[i].SetMemoryEntryData(static_cast<uint8_t>(i)); | |
| 176 SimpleIndex::InsertInEntrySet(hash, metadata_entries[i], &entries); | 197 SimpleIndex::InsertInEntrySet(hash, metadata_entries[i], &entries); |
| 177 } | 198 } |
| 178 | 199 |
| 179 std::unique_ptr<base::Pickle> pickle = | 200 std::unique_ptr<base::Pickle> pickle = |
| 180 WrappedSimpleIndexFile::Serialize(index_metadata, entries); | 201 WrappedSimpleIndexFile::Serialize(index_metadata, entries); |
| 181 EXPECT_TRUE(pickle.get() != NULL); | 202 EXPECT_TRUE(pickle.get() != NULL); |
| 182 base::Time now = base::Time::Now(); | 203 base::Time now = base::Time::Now(); |
| 183 EXPECT_TRUE(WrappedSimpleIndexFile::SerializeFinalData(now, pickle.get())); | 204 EXPECT_TRUE(WrappedSimpleIndexFile::SerializeFinalData(now, pickle.get())); |
| 184 base::Time when_index_last_saw_cache; | 205 base::Time when_index_last_saw_cache; |
| 185 SimpleIndexLoadResult deserialize_result; | 206 SimpleIndexLoadResult deserialize_result; |
| 186 WrappedSimpleIndexFile::Deserialize(static_cast<const char*>(pickle->data()), | 207 WrappedSimpleIndexFile::Deserialize(static_cast<const char*>(pickle->data()), |
| 187 pickle->size(), | 208 pickle->size(), |
| 188 &when_index_last_saw_cache, | 209 &when_index_last_saw_cache, |
| 189 &deserialize_result); | 210 &deserialize_result); |
| 190 EXPECT_TRUE(deserialize_result.did_load); | 211 EXPECT_TRUE(deserialize_result.did_load); |
| 191 EXPECT_EQ(now, when_index_last_saw_cache); | 212 EXPECT_EQ(now, when_index_last_saw_cache); |
| 192 const SimpleIndex::EntrySet& new_entries = deserialize_result.entries; | 213 const SimpleIndex::EntrySet& new_entries = deserialize_result.entries; |
| 193 EXPECT_EQ(entries.size(), new_entries.size()); | 214 EXPECT_EQ(entries.size(), new_entries.size()); |
| 194 | 215 |
| 195 for (size_t i = 0; i < kNumHashes; ++i) { | 216 for (size_t i = 0; i < kNumHashes; ++i) { |
| 196 SimpleIndex::EntrySet::const_iterator it = new_entries.find(kHashes[i]); | 217 SimpleIndex::EntrySet::const_iterator it = new_entries.find(kHashes[i]); |
| 197 EXPECT_TRUE(new_entries.end() != it); | 218 EXPECT_TRUE(new_entries.end() != it); |
| 198 EXPECT_TRUE(CompareTwoEntryMetadata(it->second, metadata_entries[i])); | 219 EXPECT_TRUE(CompareTwoEntryMetadata(it->second, metadata_entries[i])); |
| 199 } | 220 } |
| 200 } | 221 } |
| 201 | 222 |
| 223 TEST_F(SimpleIndexFileTest, ReadV7Format) { | |
| 224 static const uint64_t kHashes[] = {11, 22, 33}; | |
| 225 static const uint32_t kSizes[] = {394, 594, 495940}; | |
| 226 static_assert(arraysize(kHashes) == arraysize(kSizes), | |
| 227 "Need same number of hashes and sizes"); | |
| 228 static const size_t kNumHashes = arraysize(kHashes); | |
| 229 | |
| 230 V7IndexMetadataForTest v7_metadata(kNumHashes, 100 * 1024 * 1024); | |
| 231 | |
| 232 // We don't have a convenient way of serializing the actual entries in the | |
| 233 // V7 format, but we can cheat a bit by using the implementation details: if | |
| 234 // we set the 8 lower bits of size as the memory data, and upper bits | |
| 235 // as the size, the new serialization will produce what we want. | |
| 236 SimpleIndex::EntrySet entries; | |
| 237 for (size_t i = 0; i < kNumHashes; ++i) { | |
| 238 EntryMetadata entry(Time(), kSizes[i] & 0xFFFFFF00u); | |
| 239 entry.SetMemoryEntryData(static_cast<uint8_t>(kSizes[i] & 0xFFu)); | |
| 240 SimpleIndex::InsertInEntrySet(kHashes[i], entry, &entries); | |
| 241 } | |
| 242 std::unique_ptr<base::Pickle> pickle = | |
| 243 WrappedSimpleIndexFile::Serialize(v7_metadata, entries); | |
| 244 ASSERT_TRUE(pickle.get() != NULL); | |
| 245 base::Time now = base::Time::Now(); | |
| 246 ASSERT_TRUE(WrappedSimpleIndexFile::SerializeFinalData(now, pickle.get())); | |
| 247 | |
| 248 // Now read it back. We should get the sizes rounded, and 0 for mem entries. | |
| 249 base::Time when_index_last_saw_cache; | |
| 250 SimpleIndexLoadResult deserialize_result; | |
| 251 WrappedSimpleIndexFile::Deserialize( | |
| 252 static_cast<const char*>(pickle->data()), pickle->size(), | |
| 253 &when_index_last_saw_cache, &deserialize_result); | |
| 254 EXPECT_TRUE(deserialize_result.did_load); | |
| 255 EXPECT_EQ(now, when_index_last_saw_cache); | |
| 256 const SimpleIndex::EntrySet& new_entries = deserialize_result.entries; | |
| 257 ASSERT_EQ(entries.size(), new_entries.size()); | |
| 258 for (size_t i = 0; i < kNumHashes; ++i) { | |
| 259 SimpleIndex::EntrySet::const_iterator it = new_entries.find(kHashes[i]); | |
| 260 ASSERT_TRUE(new_entries.end() != it); | |
| 261 EXPECT_EQ(RoundSize(kSizes[i]), it->second.GetEntrySize()); | |
| 262 EXPECT_EQ(0u, it->second.GetMemoryEntryData()); | |
| 263 } | |
| 264 } | |
| 265 | |
| 202 TEST_F(SimpleIndexFileTest, LegacyIsIndexFileStale) { | 266 TEST_F(SimpleIndexFileTest, LegacyIsIndexFileStale) { |
| 203 base::ScopedTempDir cache_dir; | 267 base::ScopedTempDir cache_dir; |
| 204 ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); | 268 ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); |
| 205 base::Time cache_mtime; | 269 base::Time cache_mtime; |
| 206 const base::FilePath cache_path = cache_dir.GetPath(); | 270 const base::FilePath cache_path = cache_dir.GetPath(); |
| 207 | 271 |
| 208 ASSERT_TRUE(simple_util::GetMTime(cache_path, &cache_mtime)); | 272 ASSERT_TRUE(simple_util::GetMTime(cache_path, &cache_mtime)); |
| 209 WrappedSimpleIndexFile simple_index_file(cache_path); | 273 WrappedSimpleIndexFile simple_index_file(cache_path); |
| 210 ASSERT_TRUE(simple_index_file.CreateIndexFileDirectory()); | 274 ASSERT_TRUE(simple_index_file.CreateIndexFileDirectory()); |
| 211 const base::FilePath& index_path = simple_index_file.GetIndexFilePath(); | 275 const base::FilePath& index_path = simple_index_file.GetIndexFilePath(); |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 398 closure.WaitForResult(); | 462 closure.WaitForResult(); |
| 399 | 463 |
| 400 // Check that the temporary file was deleted and the index file was created. | 464 // Check that the temporary file was deleted and the index file was created. |
| 401 EXPECT_FALSE(base::PathExists(simple_index_file.GetTempIndexFilePath())); | 465 EXPECT_FALSE(base::PathExists(simple_index_file.GetTempIndexFilePath())); |
| 402 EXPECT_TRUE(base::PathExists(simple_index_file.GetIndexFilePath())); | 466 EXPECT_TRUE(base::PathExists(simple_index_file.GetIndexFilePath())); |
| 403 } | 467 } |
| 404 | 468 |
| 405 #endif // defined(OS_POSIX) | 469 #endif // defined(OS_POSIX) |
| 406 | 470 |
| 407 } // namespace disk_cache | 471 } // namespace disk_cache |
| OLD | NEW |