Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(46)

Side by Side Diff: net/disk_cache/simple/simple_index_file_unittest.cc

Issue 14263005: Refactor our SimpleIndex file format and serialization to use Pickle instead of the previously bugg… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: sync Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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_entry_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::SimpleIndex;
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 SimpleIndex::EntrySet entries;
63 EntryMetadata entries_array[] = {
64 EntryMetadata(11, Time::FromInternalValue(22), 33),
65 EntryMetadata(22, Time::FromInternalValue(33), 44),
66 EntryMetadata(33, Time::FromInternalValue(44), 55)
67 };
68 SimpleIndexFile::IndexMetadata index_metadata(arraysize(entries_array), 456);
69 for (uint i = 0; i < arraysize(entries_array); ++i) {
70 SimpleIndex::InsertInEntrySet(entries_array[i], &entries);
71 }
72
73 scoped_ptr<Pickle> pickle = SimpleIndexFile::Serialize(
74 index_metadata, entries);
75 EXPECT_TRUE(pickle.get() != NULL);
76
77 scoped_ptr<SimpleIndex::EntrySet> new_entries = SimpleIndexFile::Deserialize(
78 reinterpret_cast<const char*>(pickle->data()),
79 pickle->size());
80 EXPECT_TRUE(new_entries.get() != NULL);
81 EXPECT_EQ(entries.size(), new_entries->size());
82
83 for (uint i = 0; i < arraysize(entries_array); ++i) {
84 SimpleIndex::EntrySet::iterator it =
85 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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698