| 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 0eff5fa2a45238e1bd8ab7e3935a0aa20b420345..d39d40a41efcb6a8ca412eb71f631b880da74d38 100644
|
| --- a/net/disk_cache/simple/simple_index_file_unittest.cc
|
| +++ b/net/disk_cache/simple/simple_index_file_unittest.cc
|
| @@ -46,6 +46,14 @@ namespace disk_cache {
|
| // general on Windows.
|
| #if defined(OS_POSIX)
|
|
|
| +namespace {
|
| +
|
| +uint32_t RoundSize(uint32_t in) {
|
| + return (in + 0xFFu) & 0xFFFFFF00u;
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| TEST(IndexMetadataTest, Basics) {
|
| SimpleIndexFile::IndexMetadata index_metadata;
|
|
|
| @@ -121,6 +129,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 {
|
| @@ -153,10 +173,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_256b_chunks_ == b.entry_size_256b_chunks_ &&
|
| + a.in_memory_data_ == b.in_memory_data_;
|
| }
|
| };
|
|
|
| @@ -174,6 +194,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].SetInMemoryData(static_cast<uint8_t>(i));
|
| SimpleIndex::InsertInEntrySet(hash, metadata_entries[i], &entries);
|
| }
|
|
|
| @@ -200,6 +221,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.SetInMemoryData(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.GetInMemoryData());
|
| + }
|
| +}
|
| +
|
| TEST_F(SimpleIndexFileTest, LegacyIsIndexFileStale) {
|
| base::ScopedTempDir cache_dir;
|
| ASSERT_TRUE(cache_dir.CreateUniqueTempDir());
|
|
|