Chromium Code Reviews| Index: net/disk_cache/simple/simple_index_file_unittest.cc |
| diff --git a/net/disk_cache/simple/simple_index_file_unittest.cc b/net/disk_cache/simple/simple_index_file_unittest.cc |
| index f4b574bb670f6e9319a000c80057216dd76d9742..9991d13b5a94db8beeaecf1c2f53c7c8e37750a8 100644 |
| --- a/net/disk_cache/simple/simple_index_file_unittest.cc |
| +++ b/net/disk_cache/simple/simple_index_file_unittest.cc |
| @@ -45,6 +45,14 @@ namespace disk_cache { |
| // general on Windows. |
| #if defined(OS_POSIX) |
| +namespace { |
| + |
| +uint32_t RoundSize(uint32_t in) { |
| + 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.
|
| +} |
| + |
| +} // namespace |
| + |
| TEST(IndexMetadataTest, Basics) { |
| SimpleIndexFile::IndexMetadata index_metadata; |
| @@ -120,6 +128,18 @@ TEST(IndexMetadataTest, ReadV6Format) { |
| EXPECT_TRUE(new_index_metadata.CheckIndexMetadata()); |
| } |
| +// This derived index metadata class allows us to serialize the older V7 format |
| +// of the index metadata, thus allowing us to test deserializing the old format. |
| +class V7IndexMetadataForTest : public SimpleIndexFile::IndexMetadata { |
| + public: |
| + V7IndexMetadataForTest(uint64_t entry_count, uint64_t cache_size) |
| + : SimpleIndexFile::IndexMetadata(SimpleIndex::INDEX_WRITE_REASON_SHUTDOWN, |
| + entry_count, |
| + cache_size) { |
| + version_ = 7; |
| + } |
| +}; |
| + |
| // This friend derived class is able to reexport its ancestors private methods |
| // as public, for use in tests. |
| class WrappedSimpleIndexFile : public SimpleIndexFile { |
| @@ -152,10 +172,10 @@ class WrappedSimpleIndexFile : public SimpleIndexFile { |
| class SimpleIndexFileTest : public testing::Test { |
| public: |
| bool CompareTwoEntryMetadata(const EntryMetadata& a, const EntryMetadata& b) { |
| - return |
| - a.last_used_time_seconds_since_epoch_ == |
| - b.last_used_time_seconds_since_epoch_ && |
| - a.entry_size_ == b.entry_size_; |
| + return a.last_used_time_seconds_since_epoch_ == |
| + b.last_used_time_seconds_since_epoch_ && |
| + a.entry_size_ == b.entry_size_ && |
| + a.memory_entry_data_ == b.memory_entry_data_; |
| } |
| }; |
| @@ -173,6 +193,7 @@ TEST_F(SimpleIndexFileTest, Serialize) { |
| // TODO(eroman): Should restructure the test so no casting here (and same |
| // elsewhere where a hash is cast to an entry size). |
| metadata_entries[i] = EntryMetadata(Time(), static_cast<uint32_t>(hash)); |
| + metadata_entries[i].SetMemoryEntryData(static_cast<uint8_t>(i)); |
| SimpleIndex::InsertInEntrySet(hash, metadata_entries[i], &entries); |
| } |
| @@ -199,6 +220,49 @@ TEST_F(SimpleIndexFileTest, Serialize) { |
| } |
| } |
| +TEST_F(SimpleIndexFileTest, ReadV7Format) { |
| + static const uint64_t kHashes[] = {11, 22, 33}; |
| + static const uint32_t kSizes[] = {394, 594, 495940}; |
| + static_assert(arraysize(kHashes) == arraysize(kSizes), |
| + "Need same number of hashes and sizes"); |
| + static const size_t kNumHashes = arraysize(kHashes); |
| + |
| + V7IndexMetadataForTest v7_metadata(kNumHashes, 100 * 1024 * 1024); |
| + |
| + // We don't have a convenient way of serializing the actual entries in the |
| + // V7 format, but we can cheat a bit by using the implementation details: if |
| + // we set the 8 lower bits of size as the memory data, and upper bits |
| + // as the size, the new serialization will produce what we want. |
| + SimpleIndex::EntrySet entries; |
| + for (size_t i = 0; i < kNumHashes; ++i) { |
| + EntryMetadata entry(Time(), kSizes[i] & 0xFFFFFF00u); |
| + entry.SetMemoryEntryData(static_cast<uint8_t>(kSizes[i] & 0xFFu)); |
| + SimpleIndex::InsertInEntrySet(kHashes[i], entry, &entries); |
| + } |
| + std::unique_ptr<base::Pickle> pickle = |
| + WrappedSimpleIndexFile::Serialize(v7_metadata, entries); |
| + ASSERT_TRUE(pickle.get() != NULL); |
| + base::Time now = base::Time::Now(); |
| + ASSERT_TRUE(WrappedSimpleIndexFile::SerializeFinalData(now, pickle.get())); |
| + |
| + // Now read it back. We should get the sizes rounded, and 0 for mem entries. |
| + base::Time when_index_last_saw_cache; |
| + SimpleIndexLoadResult deserialize_result; |
| + WrappedSimpleIndexFile::Deserialize( |
| + static_cast<const char*>(pickle->data()), pickle->size(), |
| + &when_index_last_saw_cache, &deserialize_result); |
| + EXPECT_TRUE(deserialize_result.did_load); |
| + EXPECT_EQ(now, when_index_last_saw_cache); |
| + const SimpleIndex::EntrySet& new_entries = deserialize_result.entries; |
| + ASSERT_EQ(entries.size(), new_entries.size()); |
| + for (size_t i = 0; i < kNumHashes; ++i) { |
| + SimpleIndex::EntrySet::const_iterator it = new_entries.find(kHashes[i]); |
| + ASSERT_TRUE(new_entries.end() != it); |
| + EXPECT_EQ(RoundSize(kSizes[i]), it->second.GetEntrySize()); |
| + EXPECT_EQ(0u, it->second.GetMemoryEntryData()); |
| + } |
| +} |
| + |
| TEST_F(SimpleIndexFileTest, LegacyIsIndexFileStale) { |
| base::ScopedTempDir cache_dir; |
| ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); |