1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/hash.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop_proxy.h"
#include "base/pickle.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "net/disk_cache/simple/simple_entry_format.h"
#include "net/disk_cache/simple/simple_index.h"
#include "net/disk_cache/simple/simple_index_file.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::Time;
using disk_cache::SimpleIndexFile;
using disk_cache::SimpleIndex;
namespace disk_cache {
TEST(IndexMetadataTest, Basics) {
SimpleIndexFile::IndexMetadata index_metadata;
EXPECT_EQ(disk_cache::kSimpleIndexMagicNumber, index_metadata.magic_number_);
EXPECT_EQ(disk_cache::kSimpleVersion, index_metadata.version_);
EXPECT_EQ(0U, index_metadata.GetNumberOfEntries());
EXPECT_EQ(0U, index_metadata.cache_size_);
EXPECT_TRUE(index_metadata.CheckIndexMetadata());
}
TEST(IndexMetadataTest, Serialize) {
SimpleIndexFile::IndexMetadata index_metadata(123, 456);
Pickle pickle;
index_metadata.Serialize(&pickle);
PickleIterator it(pickle);
SimpleIndexFile::IndexMetadata new_index_metadata;
new_index_metadata.Deserialize(&it);
EXPECT_EQ(new_index_metadata.magic_number_, index_metadata.magic_number_);
EXPECT_EQ(new_index_metadata.version_, index_metadata.version_);
EXPECT_EQ(new_index_metadata.GetNumberOfEntries(),
index_metadata.GetNumberOfEntries());
EXPECT_EQ(new_index_metadata.cache_size_, index_metadata.cache_size_);
EXPECT_TRUE(new_index_metadata.CheckIndexMetadata());
}
// This friend derived class is able to reexport its ancestors private methods
// as public, for use in tests.
class WrappedSimpleIndexFile : public SimpleIndexFile {
public:
using SimpleIndexFile::Deserialize;
using SimpleIndexFile::IsIndexFileStale;
using SimpleIndexFile::kIndexFileName;
using SimpleIndexFile::Serialize;
explicit WrappedSimpleIndexFile(const base::FilePath& index_file_directory)
: SimpleIndexFile(base::MessageLoopProxy::current().get(),
base::MessageLoopProxy::current().get(),
index_file_directory) {}
virtual ~WrappedSimpleIndexFile() {
}
};
class SimpleIndexFileTest : public testing::Test {
public:
bool CompareTwoEntryMetadata(const EntryMetadata& a, const EntryMetadata& b) {
return a.last_used_time_ == b.last_used_time_ &&
a.entry_size_ == b.entry_size_;
}
protected:
struct IndexCompletionCallbackResult {
IndexCompletionCallbackResult(
scoped_ptr<SimpleIndex::EntrySet> index_file_entries,
bool force_index_flush)
: index_file_entries(index_file_entries.Pass()),
force_index_flush(force_index_flush) {
}
const scoped_ptr<SimpleIndex::EntrySet> index_file_entries;
const bool force_index_flush;
};
SimpleIndexFile::IndexCompletionCallback GetCallback() {
return base::Bind(&SimpleIndexFileTest::IndexCompletionCallback,
base::Unretained(this));
}
IndexCompletionCallbackResult* callback_result() {
return callback_result_.get();
}
private:
void IndexCompletionCallback(
scoped_ptr<SimpleIndex::EntrySet> index_file_entries,
bool force_index_flush) {
EXPECT_FALSE(callback_result_);
callback_result_.reset(
new IndexCompletionCallbackResult(index_file_entries.Pass(),
force_index_flush));
}
scoped_ptr<IndexCompletionCallbackResult> callback_result_;
};
TEST_F(SimpleIndexFileTest, Serialize) {
SimpleIndex::EntrySet entries;
static const uint64 kHashes[] = { 11, 22, 33 };
static const size_t kNumHashes = arraysize(kHashes);
EntryMetadata metadata_entries[kNumHashes];
SimpleIndexFile::IndexMetadata index_metadata(static_cast<uint64>(kNumHashes),
456);
for (size_t i = 0; i < kNumHashes; ++i) {
uint64 hash = kHashes[i];
metadata_entries[i] =
EntryMetadata(Time::FromInternalValue(hash), hash);
SimpleIndex::InsertInEntrySet(hash, metadata_entries[i], &entries);
}
scoped_ptr<Pickle> pickle = WrappedSimpleIndexFile::Serialize(
index_metadata, entries);
EXPECT_TRUE(pickle.get() != NULL);
scoped_ptr<SimpleIndex::EntrySet> new_entries =
WrappedSimpleIndexFile::Deserialize(
static_cast<const char*>(pickle->data()), pickle->size());
EXPECT_TRUE(new_entries.get() != NULL);
EXPECT_EQ(entries.size(), new_entries->size());
for (size_t i = 0; i < kNumHashes; ++i) {
SimpleIndex::EntrySet::iterator it = new_entries->find(kHashes[i]);
EXPECT_TRUE(new_entries->end() != it);
EXPECT_TRUE(CompareTwoEntryMetadata(it->second, metadata_entries[i]));
}
}
TEST_F(SimpleIndexFileTest, IsIndexFileStale) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
const std::string kIndexFileName = "simple-index";
const base::FilePath index_path =
temp_dir.path().AppendASCII(kIndexFileName);
EXPECT_TRUE(WrappedSimpleIndexFile::IsIndexFileStale(index_path));
const std::string kDummyData = "nothing to be seen here";
EXPECT_EQ(static_cast<int>(kDummyData.size()),
file_util::WriteFile(index_path,
kDummyData.data(),
kDummyData.size()));
EXPECT_FALSE(WrappedSimpleIndexFile::IsIndexFileStale(index_path));
const base::Time past_time = base::Time::Now() -
base::TimeDelta::FromSeconds(10);
EXPECT_TRUE(file_util::TouchFile(index_path, past_time, past_time));
EXPECT_TRUE(file_util::TouchFile(temp_dir.path(), past_time, past_time));
EXPECT_FALSE(WrappedSimpleIndexFile::IsIndexFileStale(index_path));
EXPECT_EQ(static_cast<int>(kDummyData.size()),
file_util::WriteFile(temp_dir.path().AppendASCII("other_file"),
kDummyData.data(),
kDummyData.size()));
EXPECT_TRUE(WrappedSimpleIndexFile::IsIndexFileStale(index_path));
}
TEST_F(SimpleIndexFileTest, WriteThenLoadIndex) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
const base::FilePath index_path =
temp_dir.path().AppendASCII(WrappedSimpleIndexFile::kIndexFileName);
EXPECT_TRUE(WrappedSimpleIndexFile::IsIndexFileStale(index_path));
SimpleIndex::EntrySet entries;
static const uint64 kHashes[] = { 11, 22, 33 };
static const size_t kNumHashes = arraysize(kHashes);
EntryMetadata metadata_entries[kNumHashes];
for (size_t i = 0; i < kNumHashes; ++i) {
uint64 hash = kHashes[i];
metadata_entries[i] =
EntryMetadata(Time::FromInternalValue(hash), hash);
SimpleIndex::InsertInEntrySet(hash, metadata_entries[i], &entries);
}
const uint64 kCacheSize = 456U;
{
WrappedSimpleIndexFile simple_index_file(temp_dir.path());
simple_index_file.WriteToDisk(entries, kCacheSize,
base::TimeTicks(), false);
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(base::PathExists(index_path));
}
WrappedSimpleIndexFile simple_index_file(temp_dir.path());
simple_index_file.LoadIndexEntries(base::MessageLoopProxy::current(),
GetCallback());
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(base::PathExists(index_path));
ASSERT_TRUE(callback_result());
EXPECT_FALSE(callback_result()->force_index_flush);
const SimpleIndex::EntrySet* read_entries =
callback_result()->index_file_entries.get();
ASSERT_TRUE(read_entries);
EXPECT_EQ(kNumHashes, read_entries->size());
for (size_t i = 0; i < kNumHashes; ++i)
EXPECT_EQ(1U, read_entries->count(kHashes[i]));
}
TEST_F(SimpleIndexFileTest, LoadCorruptIndex) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
const base::FilePath index_path =
temp_dir.path().AppendASCII(WrappedSimpleIndexFile::kIndexFileName);
EXPECT_TRUE(WrappedSimpleIndexFile::IsIndexFileStale(index_path));
const std::string kDummyData = "nothing to be seen here";
EXPECT_EQ(static_cast<int>(kDummyData.size()),
file_util::WriteFile(index_path,
kDummyData.data(),
kDummyData.size()));
EXPECT_FALSE(WrappedSimpleIndexFile::IsIndexFileStale(index_path));
WrappedSimpleIndexFile simple_index_file(temp_dir.path());
simple_index_file.LoadIndexEntries(base::MessageLoopProxy::current().get(),
GetCallback());
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(base::PathExists(index_path));
ASSERT_TRUE(callback_result());
EXPECT_TRUE(callback_result()->force_index_flush);
EXPECT_TRUE(callback_result()->index_file_entries);
}
} // namespace disk_cache
|