OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/files/file.h" | 5 #include "base/files/file.h" |
6 #include "base/files/file_util.h" | 6 #include "base/files/file_util.h" |
7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
8 #include "base/hash.h" | 8 #include "base/hash.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 EXPECT_EQ(disk_cache::kSimpleIndexMagicNumber, index_metadata.magic_number_); | 43 EXPECT_EQ(disk_cache::kSimpleIndexMagicNumber, index_metadata.magic_number_); |
44 EXPECT_EQ(disk_cache::kSimpleVersion, index_metadata.version_); | 44 EXPECT_EQ(disk_cache::kSimpleVersion, index_metadata.version_); |
45 EXPECT_EQ(0U, index_metadata.GetNumberOfEntries()); | 45 EXPECT_EQ(0U, index_metadata.GetNumberOfEntries()); |
46 EXPECT_EQ(0U, index_metadata.cache_size_); | 46 EXPECT_EQ(0U, index_metadata.cache_size_); |
47 | 47 |
48 EXPECT_TRUE(index_metadata.CheckIndexMetadata()); | 48 EXPECT_TRUE(index_metadata.CheckIndexMetadata()); |
49 } | 49 } |
50 | 50 |
51 TEST(IndexMetadataTest, Serialize) { | 51 TEST(IndexMetadataTest, Serialize) { |
52 SimpleIndexFile::IndexMetadata index_metadata(123, 456); | 52 SimpleIndexFile::IndexMetadata index_metadata(123, 456); |
53 Pickle pickle; | 53 base::Pickle pickle; |
54 index_metadata.Serialize(&pickle); | 54 index_metadata.Serialize(&pickle); |
55 PickleIterator it(pickle); | 55 base::PickleIterator it(pickle); |
56 SimpleIndexFile::IndexMetadata new_index_metadata; | 56 SimpleIndexFile::IndexMetadata new_index_metadata; |
57 new_index_metadata.Deserialize(&it); | 57 new_index_metadata.Deserialize(&it); |
58 | 58 |
59 EXPECT_EQ(new_index_metadata.magic_number_, index_metadata.magic_number_); | 59 EXPECT_EQ(new_index_metadata.magic_number_, index_metadata.magic_number_); |
60 EXPECT_EQ(new_index_metadata.version_, index_metadata.version_); | 60 EXPECT_EQ(new_index_metadata.version_, index_metadata.version_); |
61 EXPECT_EQ(new_index_metadata.GetNumberOfEntries(), | 61 EXPECT_EQ(new_index_metadata.GetNumberOfEntries(), |
62 index_metadata.GetNumberOfEntries()); | 62 index_metadata.GetNumberOfEntries()); |
63 EXPECT_EQ(new_index_metadata.cache_size_, index_metadata.cache_size_); | 63 EXPECT_EQ(new_index_metadata.cache_size_, index_metadata.cache_size_); |
64 | 64 |
65 EXPECT_TRUE(new_index_metadata.CheckIndexMetadata()); | 65 EXPECT_TRUE(new_index_metadata.CheckIndexMetadata()); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 EntryMetadata metadata_entries[kNumHashes]; | 107 EntryMetadata metadata_entries[kNumHashes]; |
108 | 108 |
109 SimpleIndexFile::IndexMetadata index_metadata(static_cast<uint64>(kNumHashes), | 109 SimpleIndexFile::IndexMetadata index_metadata(static_cast<uint64>(kNumHashes), |
110 456); | 110 456); |
111 for (size_t i = 0; i < kNumHashes; ++i) { | 111 for (size_t i = 0; i < kNumHashes; ++i) { |
112 uint64 hash = kHashes[i]; | 112 uint64 hash = kHashes[i]; |
113 metadata_entries[i] = EntryMetadata(Time(), hash); | 113 metadata_entries[i] = EntryMetadata(Time(), hash); |
114 SimpleIndex::InsertInEntrySet(hash, metadata_entries[i], &entries); | 114 SimpleIndex::InsertInEntrySet(hash, metadata_entries[i], &entries); |
115 } | 115 } |
116 | 116 |
117 scoped_ptr<Pickle> pickle = WrappedSimpleIndexFile::Serialize( | 117 scoped_ptr<base::Pickle> pickle = |
118 index_metadata, entries); | 118 WrappedSimpleIndexFile::Serialize(index_metadata, entries); |
119 EXPECT_TRUE(pickle.get() != NULL); | 119 EXPECT_TRUE(pickle.get() != NULL); |
120 base::Time now = base::Time::Now(); | 120 base::Time now = base::Time::Now(); |
121 EXPECT_TRUE(WrappedSimpleIndexFile::SerializeFinalData(now, pickle.get())); | 121 EXPECT_TRUE(WrappedSimpleIndexFile::SerializeFinalData(now, pickle.get())); |
122 base::Time when_index_last_saw_cache; | 122 base::Time when_index_last_saw_cache; |
123 SimpleIndexLoadResult deserialize_result; | 123 SimpleIndexLoadResult deserialize_result; |
124 WrappedSimpleIndexFile::Deserialize(static_cast<const char*>(pickle->data()), | 124 WrappedSimpleIndexFile::Deserialize(static_cast<const char*>(pickle->data()), |
125 pickle->size(), | 125 pickle->size(), |
126 &when_index_last_saw_cache, | 126 &when_index_last_saw_cache, |
127 &deserialize_result); | 127 &deserialize_result); |
128 EXPECT_TRUE(deserialize_result.did_load); | 128 EXPECT_TRUE(deserialize_result.did_load); |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 index_file_contents.size())); | 267 index_file_contents.size())); |
268 | 268 |
269 // Upgrade the cache. | 269 // Upgrade the cache. |
270 ASSERT_TRUE(disk_cache::UpgradeSimpleCacheOnDisk(cache_path)); | 270 ASSERT_TRUE(disk_cache::UpgradeSimpleCacheOnDisk(cache_path)); |
271 | 271 |
272 // Create the backend and initiate index flush by destroying the backend. | 272 // Create the backend and initiate index flush by destroying the backend. |
273 base::Thread cache_thread("CacheThread"); | 273 base::Thread cache_thread("CacheThread"); |
274 ASSERT_TRUE(cache_thread.StartWithOptions( | 274 ASSERT_TRUE(cache_thread.StartWithOptions( |
275 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); | 275 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
276 disk_cache::SimpleBackendImpl* simple_cache = | 276 disk_cache::SimpleBackendImpl* simple_cache = |
277 new disk_cache::SimpleBackendImpl(cache_path, | 277 new disk_cache::SimpleBackendImpl(cache_path, 0, net::DISK_CACHE, |
278 0, | 278 cache_thread.task_runner(), NULL); |
279 net::DISK_CACHE, | |
280 cache_thread.message_loop_proxy().get(), | |
281 NULL); | |
282 net::TestCompletionCallback cb; | 279 net::TestCompletionCallback cb; |
283 int rv = simple_cache->Init(cb.callback()); | 280 int rv = simple_cache->Init(cb.callback()); |
284 EXPECT_EQ(net::OK, cb.GetResult(rv)); | 281 EXPECT_EQ(net::OK, cb.GetResult(rv)); |
285 rv = simple_cache->index()->ExecuteWhenReady(cb.callback()); | 282 rv = simple_cache->index()->ExecuteWhenReady(cb.callback()); |
286 EXPECT_EQ(net::OK, cb.GetResult(rv)); | 283 EXPECT_EQ(net::OK, cb.GetResult(rv)); |
287 delete simple_cache; | 284 delete simple_cache; |
288 | 285 |
289 // The backend flushes the index on destruction and does so on the cache | 286 // The backend flushes the index on destruction and does so on the cache |
290 // thread, wait for the flushing to finish by posting a callback to the cache | 287 // thread, wait for the flushing to finish by posting a callback to the cache |
291 // thread after that. | 288 // thread after that. |
292 MessageLoopHelper helper; | 289 MessageLoopHelper helper; |
293 CallbackTest cb_shutdown(&helper, false); | 290 CallbackTest cb_shutdown(&helper, false); |
294 cache_thread.message_loop_proxy()->PostTask( | 291 cache_thread.task_runner()->PostTask( |
295 FROM_HERE, | 292 FROM_HERE, |
296 base::Bind(&CallbackTest::Run, base::Unretained(&cb_shutdown), net::OK)); | 293 base::Bind(&CallbackTest::Run, base::Unretained(&cb_shutdown), net::OK)); |
297 helper.WaitUntilCacheIoFinished(1); | 294 helper.WaitUntilCacheIoFinished(1); |
298 | 295 |
299 // Verify that the index file exists. | 296 // Verify that the index file exists. |
300 const base::FilePath& index_file_path = | 297 const base::FilePath& index_file_path = |
301 cache_path.AppendASCII("index-dir").AppendASCII("the-real-index"); | 298 cache_path.AppendASCII("index-dir").AppendASCII("the-real-index"); |
302 EXPECT_TRUE(base::PathExists(index_file_path)); | 299 EXPECT_TRUE(base::PathExists(index_file_path)); |
303 | 300 |
304 // Verify that the version of the index file is correct. | 301 // Verify that the version of the index file is correct. |
305 std::string contents; | 302 std::string contents; |
306 EXPECT_TRUE(base::ReadFileToString(index_file_path, &contents)); | 303 EXPECT_TRUE(base::ReadFileToString(index_file_path, &contents)); |
307 base::Time when_index_last_saw_cache; | 304 base::Time when_index_last_saw_cache; |
308 SimpleIndexLoadResult deserialize_result; | 305 SimpleIndexLoadResult deserialize_result; |
309 WrappedSimpleIndexFile::Deserialize(contents.data(), | 306 WrappedSimpleIndexFile::Deserialize(contents.data(), |
310 contents.size(), | 307 contents.size(), |
311 &when_index_last_saw_cache, | 308 &when_index_last_saw_cache, |
312 &deserialize_result); | 309 &deserialize_result); |
313 EXPECT_TRUE(deserialize_result.did_load); | 310 EXPECT_TRUE(deserialize_result.did_load); |
314 } | 311 } |
315 | 312 |
316 #endif // defined(OS_POSIX) | 313 #endif // defined(OS_POSIX) |
317 | 314 |
318 } // namespace disk_cache | 315 } // namespace disk_cache |
OLD | NEW |