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

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: to commit again, since the last dcommit failed 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.h"
13 #include "net/disk_cache/simple/simple_index_file.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 using base::Time;
17 using disk_cache::SimpleIndexFile;
18 using disk_cache::SimpleIndex;
19
20 namespace disk_cache {
21
22 class IndexMetadataTest : public testing::Test {};
23
24 TEST_F(IndexMetadataTest, Basics) {
25 SimpleIndexFile::IndexMetadata index_metadata;
26
27 EXPECT_EQ(disk_cache::kSimpleIndexMagicNumber, index_metadata.magic_number_);
28 EXPECT_EQ(disk_cache::kSimpleVersion, index_metadata.version_);
29 EXPECT_EQ(0U, index_metadata.GetNumberOfEntries());
30 EXPECT_EQ(0U, index_metadata.cache_size_);
31
32 EXPECT_TRUE(index_metadata.CheckIndexMetadata());
33 }
34
35 TEST_F(IndexMetadataTest, Serialize) {
36 SimpleIndexFile::IndexMetadata index_metadata(123, 456);
37 Pickle pickle;
38 index_metadata.Serialize(&pickle);
39 PickleIterator it(pickle);
40 SimpleIndexFile::IndexMetadata new_index_metadata;
41 new_index_metadata.Deserialize(&it);
42
43 EXPECT_EQ(new_index_metadata.magic_number_, index_metadata.magic_number_);
44 EXPECT_EQ(new_index_metadata.version_, index_metadata.version_);
45 EXPECT_EQ(new_index_metadata.GetNumberOfEntries(),
46 index_metadata.GetNumberOfEntries());
47 EXPECT_EQ(new_index_metadata.cache_size_, index_metadata.cache_size_);
48
49 EXPECT_TRUE(new_index_metadata.CheckIndexMetadata());
50 }
51
52 class SimpleIndexFileTest : public testing::Test {
53 public:
54 bool CompareTwoEntryMetadata(const EntryMetadata& a, const EntryMetadata& b) {
55 return a.hash_key_ == b.hash_key_ &&
56 a.last_used_time_ == b.last_used_time_ &&
57 a.entry_size_ == b.entry_size_;
58 }
59
60 };
61
62 TEST_F(SimpleIndexFileTest, Serialize) {
63 SimpleIndex::EntrySet entries;
64 EntryMetadata entries_array[] = {
65 EntryMetadata(11, Time::FromInternalValue(22), 33),
66 EntryMetadata(22, Time::FromInternalValue(33), 44),
67 EntryMetadata(33, Time::FromInternalValue(44), 55)
68 };
69 SimpleIndexFile::IndexMetadata index_metadata(arraysize(entries_array), 456);
70 for (uint32 i = 0; i < arraysize(entries_array); ++i) {
71 SimpleIndex::InsertInEntrySet(entries_array[i], &entries);
72 }
73
74 scoped_ptr<Pickle> pickle = SimpleIndexFile::Serialize(
75 index_metadata, entries);
76 EXPECT_TRUE(pickle.get() != NULL);
77
78 scoped_ptr<SimpleIndex::EntrySet> new_entries = SimpleIndexFile::Deserialize(
79 reinterpret_cast<const char*>(pickle->data()),
80 pickle->size());
81 EXPECT_TRUE(new_entries.get() != NULL);
82 EXPECT_EQ(entries.size(), new_entries->size());
83
84 for (uint32 i = 0; i < arraysize(entries_array); ++i) {
85 SimpleIndex::EntrySet::iterator it =
86 new_entries->find(entries_array[i].GetHashKey());
87 EXPECT_TRUE(new_entries->end() != it);
88 EXPECT_TRUE(CompareTwoEntryMetadata(it->second, entries_array[i]));
89 }
90 }
91
92 }
OLDNEW
« no previous file with comments | « net/disk_cache/simple/simple_index_file.cc ('k') | net/disk_cache/simple/simple_index_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698