OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/hash.h" |
| 6 #include "base/logging.h" |
| 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/pickle.h" |
| 9 #include "base/stringprintf.h" |
| 10 #include "base/time.h" |
| 11 #include "net/disk_cache/simple/simple_disk_format.h" |
| 12 #include "net/disk_cache/simple/simple_index_file.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 using base::Time; |
| 16 using disk_cache::SimpleIndexFile; |
| 17 using disk_cache::EntrySet; |
| 18 |
| 19 namespace disk_cache { |
| 20 |
| 21 class IndexMetadataTest : public testing::Test {}; |
| 22 |
| 23 TEST_F(IndexMetadataTest, Basics) { |
| 24 SimpleIndexFile::IndexMetadata index_metadata; |
| 25 |
| 26 EXPECT_EQ(disk_cache::kSimpleIndexMagicNumber, index_metadata.magic_number_); |
| 27 EXPECT_EQ(disk_cache::kSimpleVersion, index_metadata.version_); |
| 28 EXPECT_EQ(0U, index_metadata.GetNumberOfEntries()); |
| 29 EXPECT_EQ(0U, index_metadata.cache_size_); |
| 30 |
| 31 EXPECT_TRUE(index_metadata.CheckIndexMetadata()); |
| 32 } |
| 33 |
| 34 TEST_F(IndexMetadataTest, Serialize) { |
| 35 SimpleIndexFile::IndexMetadata index_metadata(123, 456); |
| 36 Pickle pickle; |
| 37 index_metadata.Serialize(&pickle); |
| 38 PickleIterator it(pickle); |
| 39 SimpleIndexFile::IndexMetadata new_index_metadata; |
| 40 new_index_metadata.Deserialize(&it); |
| 41 |
| 42 EXPECT_EQ(new_index_metadata.magic_number_, index_metadata.magic_number_); |
| 43 EXPECT_EQ(new_index_metadata.version_, index_metadata.version_); |
| 44 EXPECT_EQ(new_index_metadata.GetNumberOfEntries(), |
| 45 index_metadata.GetNumberOfEntries()); |
| 46 EXPECT_EQ(new_index_metadata.cache_size_, index_metadata.cache_size_); |
| 47 |
| 48 EXPECT_TRUE(new_index_metadata.CheckIndexMetadata()); |
| 49 } |
| 50 |
| 51 class SimpleIndexFileTest : public testing::Test { |
| 52 public: |
| 53 bool CompareTwoEntryMetadata(const EntryMetadata& a, const EntryMetadata& b) { |
| 54 return a.hash_key_ == b.hash_key_ && |
| 55 a.last_used_time_ == b.last_used_time_ && |
| 56 a.entry_size_ == b.entry_size_; |
| 57 } |
| 58 |
| 59 }; |
| 60 |
| 61 TEST_F(SimpleIndexFileTest, Serialize) { |
| 62 SimpleIndexFile::IndexMetadata index_metadata(123, 456); |
| 63 |
| 64 EntrySet entries; |
| 65 EntryMetadata entries_array[] = { |
| 66 EntryMetadata(11, Time::FromInternalValue(22), 33), |
| 67 EntryMetadata(22, Time::FromInternalValue(33), 44), |
| 68 EntryMetadata(33, Time::FromInternalValue(44), 55) |
| 69 }; |
| 70 |
| 71 for (uint i = 0; i < arraysize(entries_array); ++i) { |
| 72 InsertInEntrySet(entries_array[i], &entries); |
| 73 } |
| 74 |
| 75 scoped_ptr<Pickle> pickle = SimpleIndexFile::Serialize( |
| 76 index_metadata, entries); |
| 77 |
| 78 scoped_ptr<EntrySet> new_entries = SimpleIndexFile::Deserialize( |
| 79 reinterpret_cast<const char*>(pickle->data()), |
| 80 pickle->size()); |
| 81 |
| 82 EXPECT_EQ(entries.size(), new_entries->size()); |
| 83 |
| 84 for (uint i = 0; i < arraysize(entries_array); ++i) { |
| 85 EntrySet::iterator it = new_entries->find(entries_array[i].GetHashKey()); |
| 86 EXPECT_TRUE(new_entries->end() != it); |
| 87 EXPECT_TRUE(CompareTwoEntryMetadata(it->second, entries_array[i])); |
| 88 } |
| 89 } |
| 90 |
| 91 } |
OLD | NEW |