1 // Copyright 2011 The Chromium Authors
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 "net/disk_cache/simple/simple_index_file.h"
6
7 #include <memory>
8
9 #include "base/check.h"
10 #include "base/files/file.h"
11 #include "base/files/file_util.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/functional/callback.h"
14 #include "base/hash/hash.h"
15 #include "base/location.h"
16 #include "base/pickle.h"
17 #include "base/run_loop.h"
18 #include "base/task/single_thread_task_runner.h"
19 #include "base/threading/thread.h"
20 #include "base/time/time.h"
21 #include "net/base/cache_type.h"
22 #include "net/base/test_completion_callback.h"
23 #include "net/disk_cache/backend_cleanup_tracker.h"
24 #include "net/disk_cache/disk_cache.h"
25 #include "net/disk_cache/disk_cache_test_util.h"
26 #include "net/disk_cache/simple/simple_backend_impl.h"
27 #include "net/disk_cache/simple/simple_backend_version.h"
28 #include "net/disk_cache/simple/simple_entry_format.h"
29 #include "net/disk_cache/simple/simple_index.h"
30 #include "net/disk_cache/simple/simple_util.h"
31 #include "net/disk_cache/simple/simple_version_upgrade.h"
32 #include "net/test/gtest_util.h"
33 #include "net/test/test_with_task_environment.h"
34 #include "testing/gmock/include/gmock/gmock.h"
35 #include "testing/gtest/include/gtest/gtest.h"
36
37 using net::test::IsOk;
38
39 using base::Time;
40 using disk_cache::SimpleIndexFile;
41 using disk_cache::SimpleIndex;
42
43 namespace disk_cache {
44
45 namespace {
46
RoundSize(uint32_t in)47 uint32_t RoundSize(uint32_t in) {
48 return (in + 0xFFu) & 0xFFFFFF00u;
49 }
50
51 } // namespace
52
TEST(IndexMetadataTest,Basics)53 TEST(IndexMetadataTest, Basics) {
54 SimpleIndexFile::IndexMetadata index_metadata;
55
56 EXPECT_EQ(disk_cache::kSimpleIndexMagicNumber, index_metadata.magic_number_);
57 EXPECT_EQ(disk_cache::kSimpleVersion, index_metadata.version_);
58 EXPECT_EQ(0U, index_metadata.entry_count());
59 EXPECT_EQ(0U, index_metadata.cache_size_);
60
61 // Without setting a |reason_|, the index metadata isn't valid.
62 index_metadata.reason_ = SimpleIndex::INDEX_WRITE_REASON_SHUTDOWN;
63
64 EXPECT_TRUE(index_metadata.CheckIndexMetadata());
65 }
66
TEST(IndexMetadataTest,Serialize)67 TEST(IndexMetadataTest, Serialize) {
68 SimpleIndexFile::IndexMetadata index_metadata(
69 SimpleIndex::INDEX_WRITE_REASON_SHUTDOWN, 123, 456);
70 base::Pickle pickle;
71 index_metadata.Serialize(&pickle);
72 base::PickleIterator it(pickle);
73 SimpleIndexFile::IndexMetadata new_index_metadata;
74 new_index_metadata.Deserialize(&it);
75
76 EXPECT_EQ(new_index_metadata.magic_number_, index_metadata.magic_number_);
77 EXPECT_EQ(new_index_metadata.version_, index_metadata.version_);
78 EXPECT_EQ(new_index_metadata.reason_, index_metadata.reason_);
79 EXPECT_EQ(new_index_metadata.entry_count(), index_metadata.entry_count());
80 EXPECT_EQ(new_index_metadata.cache_size_, index_metadata.cache_size_);
81
82 EXPECT_TRUE(new_index_metadata.CheckIndexMetadata());
83 }
84
85 // This derived index metadata class allows us to serialize the older V6 format
86 // of the index metadata, thus allowing us to test deserializing the old format.
87 class V6IndexMetadataForTest : public SimpleIndexFile::IndexMetadata {
88 public:
89 // Do not default to |SimpleIndex::INDEX_WRITE_REASON_MAX|, because we want to
90 // ensure we don't serialize that value and then deserialize it and have a
91 // false positive result.
V6IndexMetadataForTest(uint64_t entry_count,uint64_t cache_size)92 V6IndexMetadataForTest(uint64_t entry_count, uint64_t cache_size)
93 : SimpleIndexFile::IndexMetadata(SimpleIndex::INDEX_WRITE_REASON_SHUTDOWN,
94 entry_count,
95 cache_size) {
96 version_ = 6;
97 }
98
99 // Copied and pasted from the V6 implementation of
100 // |SimpleIndexFile::IndexMetadata()| (removing DCHECKs).
Serialize(base::Pickle * pickle) const101 void Serialize(base::Pickle* pickle) const override {
102 pickle->WriteUInt64(magic_number_);
103 pickle->WriteUInt32(version_);
104 pickle->WriteUInt64(entry_count_);
105 pickle->WriteUInt64(cache_size_);
106 }
107 };
108
TEST(IndexMetadataTest,ReadV6Format)109 TEST(IndexMetadataTest, ReadV6Format) {
110 V6IndexMetadataForTest v6_index_metadata(123, 456);
111 EXPECT_EQ(6U, v6_index_metadata.version_);
112 base::Pickle pickle;
113 v6_index_metadata.Serialize(&pickle);
114 base::PickleIterator it(pickle);
115 SimpleIndexFile::IndexMetadata new_index_metadata;
116 new_index_metadata.Deserialize(&it);
117
118 EXPECT_EQ(new_index_metadata.magic_number_, v6_index_metadata.magic_number_);
119 EXPECT_EQ(new_index_metadata.version_, v6_index_metadata.version_);
120
121 EXPECT_EQ(new_index_metadata.reason_, SimpleIndex::INDEX_WRITE_REASON_MAX);
122 EXPECT_EQ(new_index_metadata.entry_count(), v6_index_metadata.entry_count());
123 EXPECT_EQ(new_index_metadata.cache_size_, v6_index_metadata.cache_size_);
124
125 EXPECT_TRUE(new_index_metadata.CheckIndexMetadata());
126 }
127
128 // This derived index metadata class allows us to serialize the older V7 format
129 // of the index metadata, thus allowing us to test deserializing the old format.
130 class V7IndexMetadataForTest : public SimpleIndexFile::IndexMetadata {
131 public:
V7IndexMetadataForTest(uint64_t entry_count,uint64_t cache_size)132 V7IndexMetadataForTest(uint64_t entry_count, uint64_t cache_size)
133 : SimpleIndexFile::IndexMetadata(SimpleIndex::INDEX_WRITE_REASON_SHUTDOWN,
134 entry_count,
135 cache_size) {
136 version_ = 7;
137 }
138 };
139
140 class V8IndexMetadataForTest : public SimpleIndexFile::IndexMetadata {
141 public:
V8IndexMetadataForTest(uint64_t entry_count,uint64_t cache_size)142 V8IndexMetadataForTest(uint64_t entry_count, uint64_t cache_size)
143 : SimpleIndexFile::IndexMetadata(SimpleIndex::INDEX_WRITE_REASON_SHUTDOWN,
144 entry_count,
145 cache_size) {
146 version_ = 8;
147 }
148 };
149
150 // This friend derived class is able to reexport its ancestors private methods
151 // as public, for use in tests.
152 class WrappedSimpleIndexFile : public SimpleIndexFile {
153 public:
154 using SimpleIndexFile::Deserialize;
155 using SimpleIndexFile::Serialize;
156 using SimpleIndexFile::SerializeFinalData;
157
WrappedSimpleIndexFile(const base::FilePath & index_file_directory)158 explicit WrappedSimpleIndexFile(const base::FilePath& index_file_directory)
159 : SimpleIndexFile(base::SingleThreadTaskRunner::GetCurrentDefault(),
160 base::MakeRefCounted<TrivialFileOperationsFactory>(),
161 net::DISK_CACHE,
162 index_file_directory) {}
163 ~WrappedSimpleIndexFile() override = default;
164
GetIndexFilePath() const165 const base::FilePath& GetIndexFilePath() const {
166 return index_file_;
167 }
168
GetTempIndexFilePath() const169 const base::FilePath& GetTempIndexFilePath() const {
170 return temp_index_file_;
171 }
172
CreateIndexFileDirectory() const173 bool CreateIndexFileDirectory() const {
174 return base::CreateDirectory(index_file_.DirName());
175 }
176
LegacyIsIndexFileStale(base::Time cache_last_modified,const base::FilePath & index_file_path)177 static bool LegacyIsIndexFileStale(base::Time cache_last_modified,
178 const base::FilePath& index_file_path) {
179 TrivialFileOperations ops;
180 return SimpleIndexFile::LegacyIsIndexFileStale(&ops, cache_last_modified,
181 index_file_path);
182 }
183 };
184
185 class SimpleIndexFileTest : public net::TestWithTaskEnvironment {
186 public:
CompareTwoEntryMetadata(const EntryMetadata & a,const EntryMetadata & b)187 bool CompareTwoEntryMetadata(const EntryMetadata& a, const EntryMetadata& b) {
188 return a.last_used_time_seconds_since_epoch_ ==
189 b.last_used_time_seconds_since_epoch_ &&
190 a.entry_size_256b_chunks_ == b.entry_size_256b_chunks_ &&
191 a.in_memory_data_ == b.in_memory_data_;
192 }
193
CompareTwoAppCacheEntryMetadata(const EntryMetadata & a,const EntryMetadata & b)194 bool CompareTwoAppCacheEntryMetadata(const EntryMetadata& a,
195 const EntryMetadata& b) {
196 return a.trailer_prefetch_size_ == b.trailer_prefetch_size_ &&
197 a.entry_size_256b_chunks_ == b.entry_size_256b_chunks_ &&
198 a.in_memory_data_ == b.in_memory_data_;
199 }
200 };
201
TEST_F(SimpleIndexFileTest,Serialize)202 TEST_F(SimpleIndexFileTest, Serialize) {
203 SimpleIndex::EntrySet entries;
204 static const uint64_t kHashes[] = {11, 22, 33};
205 static const size_t kNumHashes = std::size(kHashes);
206 EntryMetadata metadata_entries[kNumHashes];
207
208 SimpleIndexFile::IndexMetadata index_metadata(
209 SimpleIndex::INDEX_WRITE_REASON_SHUTDOWN,
210 static_cast<uint64_t>(kNumHashes), 456);
211 for (size_t i = 0; i < kNumHashes; ++i) {
212 uint64_t hash = kHashes[i];
213 // TODO(eroman): Should restructure the test so no casting here (and same
214 // elsewhere where a hash is cast to an entry size).
215 metadata_entries[i] = EntryMetadata(Time(), static_cast<uint32_t>(hash));
216 metadata_entries[i].SetInMemoryData(static_cast<uint8_t>(i));
217 SimpleIndex::InsertInEntrySet(hash, metadata_entries[i], &entries);
218 }
219
220 std::unique_ptr<base::Pickle> pickle = WrappedSimpleIndexFile::Serialize(
221 net::DISK_CACHE, index_metadata, entries);
222 EXPECT_TRUE(pickle.get() != nullptr);
223 base::Time now = base::Time::Now();
224 WrappedSimpleIndexFile::SerializeFinalData(now, pickle.get());
225 base::Time when_index_last_saw_cache;
226 SimpleIndexLoadResult deserialize_result;
227 WrappedSimpleIndexFile::Deserialize(
228 net::DISK_CACHE, pickle->data_as_char(), pickle->size(),
229 &when_index_last_saw_cache, &deserialize_result);
230 EXPECT_TRUE(deserialize_result.did_load);
231 EXPECT_EQ(now, when_index_last_saw_cache);
232 const SimpleIndex::EntrySet& new_entries = deserialize_result.entries;
233 EXPECT_EQ(entries.size(), new_entries.size());
234
235 for (size_t i = 0; i < kNumHashes; ++i) {
236 auto it = new_entries.find(kHashes[i]);
237 EXPECT_TRUE(new_entries.end() != it);
238 EXPECT_TRUE(CompareTwoEntryMetadata(it->second, metadata_entries[i]));
239 }
240 }
241
TEST_F(SimpleIndexFileTest,SerializeAppCache)242 TEST_F(SimpleIndexFileTest, SerializeAppCache) {
243 SimpleIndex::EntrySet entries;
244 static const uint64_t kHashes[] = {11, 22, 33};
245 static const size_t kNumHashes = std::size(kHashes);
246 static const int32_t kTrailerPrefetches[] = {123, -1, 987};
247 EntryMetadata metadata_entries[kNumHashes];
248
249 SimpleIndexFile::IndexMetadata index_metadata(
250 SimpleIndex::INDEX_WRITE_REASON_SHUTDOWN,
251 static_cast<uint64_t>(kNumHashes), 456);
252 for (size_t i = 0; i < kNumHashes; ++i) {
253 uint64_t hash = kHashes[i];
254 metadata_entries[i] =
255 EntryMetadata(kTrailerPrefetches[i], static_cast<uint32_t>(hash));
256 metadata_entries[i].SetInMemoryData(static_cast<uint8_t>(i));
257 SimpleIndex::InsertInEntrySet(hash, metadata_entries[i], &entries);
258 }
259
260 std::unique_ptr<base::Pickle> pickle = WrappedSimpleIndexFile::Serialize(
261 net::APP_CACHE, index_metadata, entries);
262 EXPECT_TRUE(pickle.get() != nullptr);
263 base::Time now = base::Time::Now();
264 WrappedSimpleIndexFile::SerializeFinalData(now, pickle.get());
265 base::Time when_index_last_saw_cache;
266 SimpleIndexLoadResult deserialize_result;
267 WrappedSimpleIndexFile::Deserialize(
268 net::APP_CACHE, pickle->data_as_char(), pickle->size(),
269 &when_index_last_saw_cache, &deserialize_result);
270 EXPECT_TRUE(deserialize_result.did_load);
271 EXPECT_EQ(now, when_index_last_saw_cache);
272 const SimpleIndex::EntrySet& new_entries = deserialize_result.entries;
273 EXPECT_EQ(entries.size(), new_entries.size());
274
275 for (size_t i = 0; i < kNumHashes; ++i) {
276 auto it = new_entries.find(kHashes[i]);
277 EXPECT_TRUE(new_entries.end() != it);
278 EXPECT_TRUE(
279 CompareTwoAppCacheEntryMetadata(it->second, metadata_entries[i]));
280 }
281 }
282
TEST_F(SimpleIndexFileTest,ReadV7Format)283 TEST_F(SimpleIndexFileTest, ReadV7Format) {
284 static const uint64_t kHashes[] = {11, 22, 33};
285 static const uint32_t kSizes[] = {394, 594, 495940};
286 static_assert(std::size(kHashes) == std::size(kSizes),
287 "Need same number of hashes and sizes");
288 static const size_t kNumHashes = std::size(kHashes);
289
290 V7IndexMetadataForTest v7_metadata(kNumHashes, 100 * 1024 * 1024);
291
292 // We don't have a convenient way of serializing the actual entries in the
293 // V7 format, but we can cheat a bit by using the implementation details: if
294 // we set the 8 lower bits of size as the memory data, and upper bits
295 // as the size, the new serialization will produce what we want.
296 SimpleIndex::EntrySet entries;
297 for (size_t i = 0; i < kNumHashes; ++i) {
298 EntryMetadata entry(Time(), kSizes[i] & 0xFFFFFF00u);
299 entry.SetInMemoryData(static_cast<uint8_t>(kSizes[i] & 0xFFu));
300 SimpleIndex::InsertInEntrySet(kHashes[i], entry, &entries);
301 }
302 std::unique_ptr<base::Pickle> pickle =
303 WrappedSimpleIndexFile::Serialize(net::DISK_CACHE, v7_metadata, entries);
304 ASSERT_TRUE(pickle.get() != nullptr);
305 base::Time now = base::Time::Now();
306 WrappedSimpleIndexFile::SerializeFinalData(now, pickle.get());
307
308 // Now read it back. We should get the sizes rounded, and 0 for mem entries.
309 base::Time when_index_last_saw_cache;
310 SimpleIndexLoadResult deserialize_result;
311 WrappedSimpleIndexFile::Deserialize(
312 net::DISK_CACHE, pickle->data_as_char(), pickle->size(),
313 &when_index_last_saw_cache, &deserialize_result);
314 EXPECT_TRUE(deserialize_result.did_load);
315 EXPECT_EQ(now, when_index_last_saw_cache);
316 const SimpleIndex::EntrySet& new_entries = deserialize_result.entries;
317 ASSERT_EQ(entries.size(), new_entries.size());
318 for (size_t i = 0; i < kNumHashes; ++i) {
319 auto it = new_entries.find(kHashes[i]);
320 ASSERT_TRUE(new_entries.end() != it);
321 EXPECT_EQ(RoundSize(kSizes[i]), it->second.GetEntrySize());
322 EXPECT_EQ(0u, it->second.GetInMemoryData());
323 }
324 }
325
TEST_F(SimpleIndexFileTest,ReadV8Format)326 TEST_F(SimpleIndexFileTest, ReadV8Format) {
327 static const uint64_t kHashes[] = {11, 22, 33};
328 static const uint32_t kSizes[] = {394, 594, 495940};
329 static_assert(std::size(kHashes) == std::size(kSizes),
330 "Need same number of hashes and sizes");
331 static const size_t kNumHashes = std::size(kHashes);
332
333 // V8 to V9 should not make any modifications for non-APP_CACHE modes.
334 // Verify that the data is preserved through the migration.
335 V8IndexMetadataForTest v8_metadata(kNumHashes, 100 * 1024 * 1024);
336
337 EntryMetadata metadata_entries[kNumHashes];
338 SimpleIndex::EntrySet entries;
339 for (size_t i = 0; i < kNumHashes; ++i) {
340 metadata_entries[i] =
341 EntryMetadata(base::Time::Now(), static_cast<uint32_t>(kHashes[i]));
342 metadata_entries[i].SetInMemoryData(static_cast<uint8_t>(i));
343 SimpleIndex::InsertInEntrySet(kHashes[i], metadata_entries[i], &entries);
344 }
345 std::unique_ptr<base::Pickle> pickle =
346 WrappedSimpleIndexFile::Serialize(net::DISK_CACHE, v8_metadata, entries);
347 ASSERT_TRUE(pickle.get() != nullptr);
348 base::Time now = base::Time::Now();
349 WrappedSimpleIndexFile::SerializeFinalData(now, pickle.get());
350
351 base::Time when_index_last_saw_cache;
352 SimpleIndexLoadResult deserialize_result;
353 WrappedSimpleIndexFile::Deserialize(
354 net::DISK_CACHE, pickle->data_as_char(), pickle->size(),
355 &when_index_last_saw_cache, &deserialize_result);
356 EXPECT_TRUE(deserialize_result.did_load);
357 EXPECT_EQ(now, when_index_last_saw_cache);
358 const SimpleIndex::EntrySet& new_entries = deserialize_result.entries;
359 ASSERT_EQ(entries.size(), new_entries.size());
360 for (size_t i = 0; i < kNumHashes; ++i) {
361 auto it = new_entries.find(kHashes[i]);
362 ASSERT_TRUE(new_entries.end() != it);
363 EXPECT_TRUE(CompareTwoEntryMetadata(it->second, metadata_entries[i]));
364 }
365 }
366
TEST_F(SimpleIndexFileTest,ReadV8FormatAppCache)367 TEST_F(SimpleIndexFileTest, ReadV8FormatAppCache) {
368 static const uint64_t kHashes[] = {11, 22, 33};
369 static const uint32_t kSizes[] = {394, 594, 495940};
370 static_assert(std::size(kHashes) == std::size(kSizes),
371 "Need same number of hashes and sizes");
372 static const size_t kNumHashes = std::size(kHashes);
373
374 // To simulate an upgrade from v8 to v9 write out the v8 schema
375 // using DISK_CACHE mode. The read it back in in APP_CACHE mode.
376 // The entry access time data should be zeroed to reset it as the
377 // new trailer prefetch size.
378 V8IndexMetadataForTest v8_metadata(kNumHashes, 100 * 1024 * 1024);
379
380 EntryMetadata metadata_entries[kNumHashes];
381 SimpleIndex::EntrySet entries;
382 for (size_t i = 0; i < kNumHashes; ++i) {
383 metadata_entries[i] =
384 EntryMetadata(base::Time::Now(), static_cast<uint32_t>(kHashes[i]));
385 metadata_entries[i].SetInMemoryData(static_cast<uint8_t>(i));
386 SimpleIndex::InsertInEntrySet(kHashes[i], metadata_entries[i], &entries);
387 }
388 std::unique_ptr<base::Pickle> pickle =
389 WrappedSimpleIndexFile::Serialize(net::DISK_CACHE, v8_metadata, entries);
390 ASSERT_TRUE(pickle.get() != nullptr);
391 base::Time now = base::Time::Now();
392 WrappedSimpleIndexFile::SerializeFinalData(now, pickle.get());
393
394 // Deserialize using APP_CACHE mode. This should zero out the
395 // trailer_prefetch_size_ instead of using the time bits written
396 // out previously.
397 base::Time when_index_last_saw_cache;
398 SimpleIndexLoadResult deserialize_result;
399 WrappedSimpleIndexFile::Deserialize(
400 net::APP_CACHE, pickle->data_as_char(), pickle->size(),
401 &when_index_last_saw_cache, &deserialize_result);
402 EXPECT_TRUE(deserialize_result.did_load);
403 EXPECT_EQ(now, when_index_last_saw_cache);
404 const SimpleIndex::EntrySet& new_entries = deserialize_result.entries;
405 ASSERT_EQ(entries.size(), new_entries.size());
406 for (size_t i = 0; i < kNumHashes; ++i) {
407 auto it = new_entries.find(kHashes[i]);
408 ASSERT_TRUE(new_entries.end() != it);
409 // The trailer prefetch size should be zeroed.
410 EXPECT_NE(metadata_entries[i].trailer_prefetch_size_,
411 it->second.trailer_prefetch_size_);
412 EXPECT_EQ(0, it->second.trailer_prefetch_size_);
413 // Other data should be unaffected.
414 EXPECT_EQ(metadata_entries[i].entry_size_256b_chunks_,
415 it->second.entry_size_256b_chunks_);
416 EXPECT_EQ(metadata_entries[i].in_memory_data_, it->second.in_memory_data_);
417 }
418 }
419
TEST_F(SimpleIndexFileTest,LegacyIsIndexFileStale)420 TEST_F(SimpleIndexFileTest, LegacyIsIndexFileStale) {
421 base::ScopedTempDir cache_dir;
422 ASSERT_TRUE(cache_dir.CreateUniqueTempDir());
423 base::File::Info file_info;
424 base::Time cache_mtime;
425 const base::FilePath cache_path = cache_dir.GetPath();
426
427 ASSERT_TRUE(base::GetFileInfo(cache_path, &file_info));
428 cache_mtime = file_info.last_modified;
429 WrappedSimpleIndexFile simple_index_file(cache_path);
430 ASSERT_TRUE(simple_index_file.CreateIndexFileDirectory());
431 const base::FilePath& index_path = simple_index_file.GetIndexFilePath();
432 EXPECT_TRUE(
433 WrappedSimpleIndexFile::LegacyIsIndexFileStale(cache_mtime, index_path));
434 const std::string kDummyData = "nothing to be seen here";
435 EXPECT_TRUE(base::WriteFile(index_path, kDummyData));
436 ASSERT_TRUE(base::GetFileInfo(cache_path, &file_info));
437 cache_mtime = file_info.last_modified;
438 EXPECT_FALSE(
439 WrappedSimpleIndexFile::LegacyIsIndexFileStale(cache_mtime, index_path));
440
441 const base::Time past_time = base::Time::Now() - base::Seconds(10);
442 EXPECT_TRUE(base::TouchFile(index_path, past_time, past_time));
443 EXPECT_TRUE(base::TouchFile(cache_path, past_time, past_time));
444 ASSERT_TRUE(base::GetFileInfo(cache_path, &file_info));
445 cache_mtime = file_info.last_modified;
446 EXPECT_FALSE(
447 WrappedSimpleIndexFile::LegacyIsIndexFileStale(cache_mtime, index_path));
448 const base::Time even_older = past_time - base::Seconds(10);
449 EXPECT_TRUE(base::TouchFile(index_path, even_older, even_older));
450 EXPECT_TRUE(
451 WrappedSimpleIndexFile::LegacyIsIndexFileStale(cache_mtime, index_path));
452 }
453
TEST_F(SimpleIndexFileTest,WriteThenLoadIndex)454 TEST_F(SimpleIndexFileTest, WriteThenLoadIndex) {
455 base::ScopedTempDir cache_dir;
456 ASSERT_TRUE(cache_dir.CreateUniqueTempDir());
457
458 SimpleIndex::EntrySet entries;
459 static const uint64_t kHashes[] = {11, 22, 33};
460 static const size_t kNumHashes = std::size(kHashes);
461 EntryMetadata metadata_entries[kNumHashes];
462 for (size_t i = 0; i < kNumHashes; ++i) {
463 uint64_t hash = kHashes[i];
464 metadata_entries[i] = EntryMetadata(Time(), static_cast<uint32_t>(hash));
465 SimpleIndex::InsertInEntrySet(hash, metadata_entries[i], &entries);
466 }
467
468 const uint64_t kCacheSize = 456U;
469 net::TestClosure closure;
470 {
471 WrappedSimpleIndexFile simple_index_file(cache_dir.GetPath());
472 simple_index_file.WriteToDisk(net::DISK_CACHE,
473 SimpleIndex::INDEX_WRITE_REASON_SHUTDOWN,
474 entries, kCacheSize, closure.closure());
475 closure.WaitForResult();
476 EXPECT_TRUE(base::PathExists(simple_index_file.GetIndexFilePath()));
477 }
478
479 WrappedSimpleIndexFile simple_index_file(cache_dir.GetPath());
480 base::File::Info file_info;
481 ASSERT_TRUE(base::GetFileInfo(cache_dir.GetPath(), &file_info));
482 base::Time fake_cache_mtime = file_info.last_modified;
483 SimpleIndexLoadResult load_index_result;
484 simple_index_file.LoadIndexEntries(fake_cache_mtime, closure.closure(),
485 &load_index_result);
486 closure.WaitForResult();
487
488 EXPECT_TRUE(base::PathExists(simple_index_file.GetIndexFilePath()));
489 EXPECT_TRUE(load_index_result.did_load);
490 EXPECT_FALSE(load_index_result.flush_required);
491
492 EXPECT_EQ(kNumHashes, load_index_result.entries.size());
493 for (uint64_t hash : kHashes)
494 EXPECT_EQ(1U, load_index_result.entries.count(hash));
495 }
496
TEST_F(SimpleIndexFileTest,LoadCorruptIndex)497 TEST_F(SimpleIndexFileTest, LoadCorruptIndex) {
498 base::ScopedTempDir cache_dir;
499 ASSERT_TRUE(cache_dir.CreateUniqueTempDir());
500
501 WrappedSimpleIndexFile simple_index_file(cache_dir.GetPath());
502 ASSERT_TRUE(simple_index_file.CreateIndexFileDirectory());
503 const base::FilePath& index_path = simple_index_file.GetIndexFilePath();
504 const std::string kDummyData = "nothing to be seen here";
505 EXPECT_TRUE(base::WriteFile(index_path, kDummyData));
506 base::File::Info file_info;
507 ASSERT_TRUE(
508 base::GetFileInfo(simple_index_file.GetIndexFilePath(), &file_info));
509 base::Time fake_cache_mtime = file_info.last_modified;
510 EXPECT_FALSE(WrappedSimpleIndexFile::LegacyIsIndexFileStale(fake_cache_mtime,
511 index_path));
512 SimpleIndexLoadResult load_index_result;
513 net::TestClosure closure;
514 simple_index_file.LoadIndexEntries(fake_cache_mtime, closure.closure(),
515 &load_index_result);
516 closure.WaitForResult();
517
518 EXPECT_FALSE(base::PathExists(index_path));
519 EXPECT_TRUE(load_index_result.did_load);
520 EXPECT_TRUE(load_index_result.flush_required);
521 }
522
TEST_F(SimpleIndexFileTest,LoadCorruptIndex2)523 TEST_F(SimpleIndexFileTest, LoadCorruptIndex2) {
524 // Variant where the index looks like a pickle, but not one with right
525 // header size --- that used to hit a DCHECK on debug builds.
526 base::ScopedTempDir cache_dir;
527 ASSERT_TRUE(cache_dir.CreateUniqueTempDir());
528
529 WrappedSimpleIndexFile simple_index_file(cache_dir.GetPath());
530 ASSERT_TRUE(simple_index_file.CreateIndexFileDirectory());
531 const base::FilePath& index_path = simple_index_file.GetIndexFilePath();
532 base::Pickle bad_payload;
533 bad_payload.WriteString("nothing to be seen here");
534
535 EXPECT_TRUE(base::WriteFile(index_path, bad_payload));
536 base::File::Info file_info;
537 ASSERT_TRUE(
538 base::GetFileInfo(simple_index_file.GetIndexFilePath(), &file_info));
539 base::Time fake_cache_mtime = file_info.last_modified;
540 EXPECT_FALSE(WrappedSimpleIndexFile::LegacyIsIndexFileStale(fake_cache_mtime,
541 index_path));
542 SimpleIndexLoadResult load_index_result;
543 net::TestClosure closure;
544 simple_index_file.LoadIndexEntries(fake_cache_mtime, closure.closure(),
545 &load_index_result);
546 closure.WaitForResult();
547
548 EXPECT_FALSE(base::PathExists(index_path));
549 EXPECT_TRUE(load_index_result.did_load);
550 EXPECT_TRUE(load_index_result.flush_required);
551 }
552
553 // Tests that after an upgrade the backend has the index file put in place.
TEST_F(SimpleIndexFileTest,SimpleCacheUpgrade)554 TEST_F(SimpleIndexFileTest, SimpleCacheUpgrade) {
555 base::ScopedTempDir cache_dir;
556 ASSERT_TRUE(cache_dir.CreateUniqueTempDir());
557 const base::FilePath cache_path = cache_dir.GetPath();
558
559 // Write an old fake index file.
560 base::File file(cache_path.AppendASCII("index"),
561 base::File::FLAG_CREATE | base::File::FLAG_WRITE);
562 ASSERT_TRUE(file.IsValid());
563 disk_cache::FakeIndexData file_contents;
564 file_contents.initial_magic_number = disk_cache::kSimpleInitialMagicNumber;
565 file_contents.version = 5;
566 int bytes_written = file.Write(0, reinterpret_cast<char*>(&file_contents),
567 sizeof(file_contents));
568 ASSERT_EQ((int)sizeof(file_contents), bytes_written);
569 file.Close();
570
571 // Write the index file. The format is incorrect, but for transitioning from
572 // v5 it does not matter.
573 const std::string index_file_contents("incorrectly serialized data");
574 const base::FilePath old_index_file =
575 cache_path.AppendASCII("the-real-index");
576 ASSERT_TRUE(base::WriteFile(old_index_file, index_file_contents));
577
578 TrivialFileOperations file_operations;
579 // Upgrade the cache.
580 ASSERT_EQ(disk_cache::UpgradeSimpleCacheOnDisk(&file_operations, cache_path),
581 SimpleCacheConsistencyResult::kOK);
582
583 // Create the backend and initiate index flush by destroying the backend.
584 scoped_refptr<disk_cache::BackendCleanupTracker> cleanup_tracker =
585 disk_cache::BackendCleanupTracker::TryCreate(cache_path,
586 base::OnceClosure());
587 ASSERT_TRUE(cleanup_tracker != nullptr);
588
589 net::TestClosure post_cleanup;
590 cleanup_tracker->AddPostCleanupCallback(post_cleanup.closure());
591
592 auto simple_cache = std::make_unique<disk_cache::SimpleBackendImpl>(
593 /*file_operations_factory=*/nullptr, cache_path, cleanup_tracker,
594 /*file_tracker=*/nullptr, 0, net::DISK_CACHE,
595 /*net_log=*/nullptr);
596 net::TestCompletionCallback cb;
597 simple_cache->Init(cb.callback());
598 EXPECT_THAT(cb.WaitForResult(), IsOk());
599 simple_cache->index()->ExecuteWhenReady(cb.callback());
600 int rv = cb.WaitForResult();
601 EXPECT_THAT(rv, IsOk());
602 simple_cache.reset();
603 cleanup_tracker = nullptr;
604
605 // The backend flushes the index on destruction; it will run the post-cleanup
606 // callback set on the cleanup_tracker once that finishes.
607 post_cleanup.WaitForResult();
608
609 // Verify that the index file exists.
610 const base::FilePath& index_file_path =
611 cache_path.AppendASCII("index-dir").AppendASCII("the-real-index");
612 EXPECT_TRUE(base::PathExists(index_file_path));
613
614 // Verify that the version of the index file is correct.
615 std::string contents;
616 EXPECT_TRUE(base::ReadFileToString(index_file_path, &contents));
617 base::Time when_index_last_saw_cache;
618 SimpleIndexLoadResult deserialize_result;
619 WrappedSimpleIndexFile::Deserialize(
620 net::DISK_CACHE, contents.data(), contents.size(),
621 &when_index_last_saw_cache, &deserialize_result);
622 EXPECT_TRUE(deserialize_result.did_load);
623 }
624
TEST_F(SimpleIndexFileTest,OverwritesStaleTempFile)625 TEST_F(SimpleIndexFileTest, OverwritesStaleTempFile) {
626 base::ScopedTempDir cache_dir;
627 ASSERT_TRUE(cache_dir.CreateUniqueTempDir());
628 const base::FilePath cache_path = cache_dir.GetPath();
629 WrappedSimpleIndexFile simple_index_file(cache_path);
630 ASSERT_TRUE(simple_index_file.CreateIndexFileDirectory());
631
632 // Create an temporary index file.
633 const base::FilePath& temp_index_path =
634 simple_index_file.GetTempIndexFilePath();
635 const std::string kDummyData = "nothing to be seen here";
636 EXPECT_TRUE(base::WriteFile(temp_index_path, kDummyData));
637 ASSERT_TRUE(base::PathExists(simple_index_file.GetTempIndexFilePath()));
638
639 // Write the index file.
640 SimpleIndex::EntrySet entries;
641 SimpleIndex::InsertInEntrySet(11, EntryMetadata(Time(), 11u), &entries);
642 net::TestClosure closure;
643 simple_index_file.WriteToDisk(net::DISK_CACHE,
644 SimpleIndex::INDEX_WRITE_REASON_SHUTDOWN,
645 entries, 120U, closure.closure());
646 closure.WaitForResult();
647
648 // Check that the temporary file was deleted and the index file was created.
649 EXPECT_FALSE(base::PathExists(simple_index_file.GetTempIndexFilePath()));
650 EXPECT_TRUE(base::PathExists(simple_index_file.GetIndexFilePath()));
651 }
652
653 } // namespace disk_cache
654