| 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/file_util.h" | 5 #include "base/file_util.h" |
| 6 #include "base/files/file.h" | 6 #include "base/files/file.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 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 using SimpleIndexFile::Deserialize; | 72 using SimpleIndexFile::Deserialize; |
| 73 using SimpleIndexFile::LegacyIsIndexFileStale; | 73 using SimpleIndexFile::LegacyIsIndexFileStale; |
| 74 using SimpleIndexFile::Serialize; | 74 using SimpleIndexFile::Serialize; |
| 75 using SimpleIndexFile::SerializeFinalData; | 75 using SimpleIndexFile::SerializeFinalData; |
| 76 | 76 |
| 77 explicit WrappedSimpleIndexFile(const base::FilePath& index_file_directory) | 77 explicit WrappedSimpleIndexFile(const base::FilePath& index_file_directory) |
| 78 : SimpleIndexFile(base::MessageLoopProxy::current().get(), | 78 : SimpleIndexFile(base::MessageLoopProxy::current().get(), |
| 79 base::MessageLoopProxy::current().get(), | 79 base::MessageLoopProxy::current().get(), |
| 80 net::DISK_CACHE, | 80 net::DISK_CACHE, |
| 81 index_file_directory) {} | 81 index_file_directory) {} |
| 82 virtual ~WrappedSimpleIndexFile() { | 82 virtual ~WrappedSimpleIndexFile() {} |
| 83 } | |
| 84 | 83 |
| 85 const base::FilePath& GetIndexFilePath() const { | 84 const base::FilePath& GetIndexFilePath() const { return index_file_; } |
| 86 return index_file_; | |
| 87 } | |
| 88 | 85 |
| 89 bool CreateIndexFileDirectory() const { | 86 bool CreateIndexFileDirectory() const { |
| 90 return base::CreateDirectory(index_file_.DirName()); | 87 return base::CreateDirectory(index_file_.DirName()); |
| 91 } | 88 } |
| 92 }; | 89 }; |
| 93 | 90 |
| 94 class SimpleIndexFileTest : public testing::Test { | 91 class SimpleIndexFileTest : public testing::Test { |
| 95 public: | 92 public: |
| 96 bool CompareTwoEntryMetadata(const EntryMetadata& a, const EntryMetadata& b) { | 93 bool CompareTwoEntryMetadata(const EntryMetadata& a, const EntryMetadata& b) { |
| 97 return | 94 return a.last_used_time_seconds_since_epoch_ == |
| 98 a.last_used_time_seconds_since_epoch_ == | 95 b.last_used_time_seconds_since_epoch_ && |
| 99 b.last_used_time_seconds_since_epoch_ && | 96 a.entry_size_ == b.entry_size_; |
| 100 a.entry_size_ == b.entry_size_; | |
| 101 } | 97 } |
| 102 | 98 |
| 103 protected: | 99 protected: |
| 104 SimpleIndexFileTest() : callback_called_(false) {} | 100 SimpleIndexFileTest() : callback_called_(false) {} |
| 105 | 101 |
| 106 base::Closure GetCallback() { | 102 base::Closure GetCallback() { |
| 107 return base::Bind(&SimpleIndexFileTest::LoadIndexEntriesCallback, | 103 return base::Bind(&SimpleIndexFileTest::LoadIndexEntriesCallback, |
| 108 base::Unretained(this)); | 104 base::Unretained(this)); |
| 109 } | 105 } |
| 110 | 106 |
| 111 bool callback_called() { return callback_called_; } | 107 bool callback_called() { return callback_called_; } |
| 112 | 108 |
| 113 private: | 109 private: |
| 114 void LoadIndexEntriesCallback() { | 110 void LoadIndexEntriesCallback() { |
| 115 EXPECT_FALSE(callback_called_); | 111 EXPECT_FALSE(callback_called_); |
| 116 callback_called_ = true; | 112 callback_called_ = true; |
| 117 } | 113 } |
| 118 | 114 |
| 119 bool callback_called_; | 115 bool callback_called_; |
| 120 }; | 116 }; |
| 121 | 117 |
| 122 TEST_F(SimpleIndexFileTest, Serialize) { | 118 TEST_F(SimpleIndexFileTest, Serialize) { |
| 123 SimpleIndex::EntrySet entries; | 119 SimpleIndex::EntrySet entries; |
| 124 static const uint64 kHashes[] = { 11, 22, 33 }; | 120 static const uint64 kHashes[] = {11, 22, 33}; |
| 125 static const size_t kNumHashes = arraysize(kHashes); | 121 static const size_t kNumHashes = arraysize(kHashes); |
| 126 EntryMetadata metadata_entries[kNumHashes]; | 122 EntryMetadata metadata_entries[kNumHashes]; |
| 127 | 123 |
| 128 SimpleIndexFile::IndexMetadata index_metadata(static_cast<uint64>(kNumHashes), | 124 SimpleIndexFile::IndexMetadata index_metadata(static_cast<uint64>(kNumHashes), |
| 129 456); | 125 456); |
| 130 for (size_t i = 0; i < kNumHashes; ++i) { | 126 for (size_t i = 0; i < kNumHashes; ++i) { |
| 131 uint64 hash = kHashes[i]; | 127 uint64 hash = kHashes[i]; |
| 132 metadata_entries[i] = EntryMetadata(Time(), hash); | 128 metadata_entries[i] = EntryMetadata(Time(), hash); |
| 133 SimpleIndex::InsertInEntrySet(hash, metadata_entries[i], &entries); | 129 SimpleIndex::InsertInEntrySet(hash, metadata_entries[i], &entries); |
| 134 } | 130 } |
| 135 | 131 |
| 136 scoped_ptr<Pickle> pickle = WrappedSimpleIndexFile::Serialize( | 132 scoped_ptr<Pickle> pickle = |
| 137 index_metadata, entries); | 133 WrappedSimpleIndexFile::Serialize(index_metadata, entries); |
| 138 EXPECT_TRUE(pickle.get() != NULL); | 134 EXPECT_TRUE(pickle.get() != NULL); |
| 139 base::Time now = base::Time::Now(); | 135 base::Time now = base::Time::Now(); |
| 140 EXPECT_TRUE(WrappedSimpleIndexFile::SerializeFinalData(now, pickle.get())); | 136 EXPECT_TRUE(WrappedSimpleIndexFile::SerializeFinalData(now, pickle.get())); |
| 141 base::Time when_index_last_saw_cache; | 137 base::Time when_index_last_saw_cache; |
| 142 SimpleIndexLoadResult deserialize_result; | 138 SimpleIndexLoadResult deserialize_result; |
| 143 WrappedSimpleIndexFile::Deserialize(static_cast<const char*>(pickle->data()), | 139 WrappedSimpleIndexFile::Deserialize(static_cast<const char*>(pickle->data()), |
| 144 pickle->size(), | 140 pickle->size(), |
| 145 &when_index_last_saw_cache, | 141 &when_index_last_saw_cache, |
| 146 &deserialize_result); | 142 &deserialize_result); |
| 147 EXPECT_TRUE(deserialize_result.did_load); | 143 EXPECT_TRUE(deserialize_result.did_load); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 163 const base::FilePath cache_path = cache_dir.path(); | 159 const base::FilePath cache_path = cache_dir.path(); |
| 164 | 160 |
| 165 ASSERT_TRUE(simple_util::GetMTime(cache_path, &cache_mtime)); | 161 ASSERT_TRUE(simple_util::GetMTime(cache_path, &cache_mtime)); |
| 166 WrappedSimpleIndexFile simple_index_file(cache_path); | 162 WrappedSimpleIndexFile simple_index_file(cache_path); |
| 167 ASSERT_TRUE(simple_index_file.CreateIndexFileDirectory()); | 163 ASSERT_TRUE(simple_index_file.CreateIndexFileDirectory()); |
| 168 const base::FilePath& index_path = simple_index_file.GetIndexFilePath(); | 164 const base::FilePath& index_path = simple_index_file.GetIndexFilePath(); |
| 169 EXPECT_TRUE( | 165 EXPECT_TRUE( |
| 170 WrappedSimpleIndexFile::LegacyIsIndexFileStale(cache_mtime, index_path)); | 166 WrappedSimpleIndexFile::LegacyIsIndexFileStale(cache_mtime, index_path)); |
| 171 const std::string kDummyData = "nothing to be seen here"; | 167 const std::string kDummyData = "nothing to be seen here"; |
| 172 EXPECT_EQ(static_cast<int>(kDummyData.size()), | 168 EXPECT_EQ(static_cast<int>(kDummyData.size()), |
| 173 base::WriteFile(index_path, | 169 base::WriteFile(index_path, kDummyData.data(), kDummyData.size())); |
| 174 kDummyData.data(), kDummyData.size())); | |
| 175 ASSERT_TRUE(simple_util::GetMTime(cache_path, &cache_mtime)); | 170 ASSERT_TRUE(simple_util::GetMTime(cache_path, &cache_mtime)); |
| 176 EXPECT_FALSE( | 171 EXPECT_FALSE( |
| 177 WrappedSimpleIndexFile::LegacyIsIndexFileStale(cache_mtime, index_path)); | 172 WrappedSimpleIndexFile::LegacyIsIndexFileStale(cache_mtime, index_path)); |
| 178 | 173 |
| 179 const base::Time past_time = base::Time::Now() - | 174 const base::Time past_time = |
| 180 base::TimeDelta::FromSeconds(10); | 175 base::Time::Now() - base::TimeDelta::FromSeconds(10); |
| 181 EXPECT_TRUE(base::TouchFile(index_path, past_time, past_time)); | 176 EXPECT_TRUE(base::TouchFile(index_path, past_time, past_time)); |
| 182 EXPECT_TRUE(base::TouchFile(cache_path, past_time, past_time)); | 177 EXPECT_TRUE(base::TouchFile(cache_path, past_time, past_time)); |
| 183 ASSERT_TRUE(simple_util::GetMTime(cache_path, &cache_mtime)); | 178 ASSERT_TRUE(simple_util::GetMTime(cache_path, &cache_mtime)); |
| 184 EXPECT_FALSE( | 179 EXPECT_FALSE( |
| 185 WrappedSimpleIndexFile::LegacyIsIndexFileStale(cache_mtime, index_path)); | 180 WrappedSimpleIndexFile::LegacyIsIndexFileStale(cache_mtime, index_path)); |
| 186 const base::Time even_older = past_time - base::TimeDelta::FromSeconds(10); | 181 const base::Time even_older = past_time - base::TimeDelta::FromSeconds(10); |
| 187 EXPECT_TRUE(base::TouchFile(index_path, even_older, even_older)); | 182 EXPECT_TRUE(base::TouchFile(index_path, even_older, even_older)); |
| 188 EXPECT_TRUE( | 183 EXPECT_TRUE( |
| 189 WrappedSimpleIndexFile::LegacyIsIndexFileStale(cache_mtime, index_path)); | 184 WrappedSimpleIndexFile::LegacyIsIndexFileStale(cache_mtime, index_path)); |
| 190 } | 185 } |
| 191 | 186 |
| 192 // This test is flaky, see http://crbug.com/255775. | 187 // This test is flaky, see http://crbug.com/255775. |
| 193 TEST_F(SimpleIndexFileTest, DISABLED_WriteThenLoadIndex) { | 188 TEST_F(SimpleIndexFileTest, DISABLED_WriteThenLoadIndex) { |
| 194 base::ScopedTempDir cache_dir; | 189 base::ScopedTempDir cache_dir; |
| 195 ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); | 190 ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); |
| 196 | 191 |
| 197 SimpleIndex::EntrySet entries; | 192 SimpleIndex::EntrySet entries; |
| 198 static const uint64 kHashes[] = { 11, 22, 33 }; | 193 static const uint64 kHashes[] = {11, 22, 33}; |
| 199 static const size_t kNumHashes = arraysize(kHashes); | 194 static const size_t kNumHashes = arraysize(kHashes); |
| 200 EntryMetadata metadata_entries[kNumHashes]; | 195 EntryMetadata metadata_entries[kNumHashes]; |
| 201 for (size_t i = 0; i < kNumHashes; ++i) { | 196 for (size_t i = 0; i < kNumHashes; ++i) { |
| 202 uint64 hash = kHashes[i]; | 197 uint64 hash = kHashes[i]; |
| 203 metadata_entries[i] = EntryMetadata(Time(), hash); | 198 metadata_entries[i] = EntryMetadata(Time(), hash); |
| 204 SimpleIndex::InsertInEntrySet(hash, metadata_entries[i], &entries); | 199 SimpleIndex::InsertInEntrySet(hash, metadata_entries[i], &entries); |
| 205 } | 200 } |
| 206 | 201 |
| 207 const uint64 kCacheSize = 456U; | 202 const uint64 kCacheSize = 456U; |
| 208 { | 203 { |
| 209 WrappedSimpleIndexFile simple_index_file(cache_dir.path()); | 204 WrappedSimpleIndexFile simple_index_file(cache_dir.path()); |
| 210 simple_index_file.WriteToDisk(entries, kCacheSize, | 205 simple_index_file.WriteToDisk( |
| 211 base::TimeTicks(), false); | 206 entries, kCacheSize, base::TimeTicks(), false); |
| 212 base::RunLoop().RunUntilIdle(); | 207 base::RunLoop().RunUntilIdle(); |
| 213 EXPECT_TRUE(base::PathExists(simple_index_file.GetIndexFilePath())); | 208 EXPECT_TRUE(base::PathExists(simple_index_file.GetIndexFilePath())); |
| 214 } | 209 } |
| 215 | 210 |
| 216 WrappedSimpleIndexFile simple_index_file(cache_dir.path()); | 211 WrappedSimpleIndexFile simple_index_file(cache_dir.path()); |
| 217 base::Time fake_cache_mtime; | 212 base::Time fake_cache_mtime; |
| 218 ASSERT_TRUE(simple_util::GetMTime(simple_index_file.GetIndexFilePath(), | 213 ASSERT_TRUE(simple_util::GetMTime(simple_index_file.GetIndexFilePath(), |
| 219 &fake_cache_mtime)); | 214 &fake_cache_mtime)); |
| 220 SimpleIndexLoadResult load_index_result; | 215 SimpleIndexLoadResult load_index_result; |
| 221 simple_index_file.LoadIndexEntries(fake_cache_mtime, | 216 simple_index_file.LoadIndexEntries( |
| 222 GetCallback(), | 217 fake_cache_mtime, GetCallback(), &load_index_result); |
| 223 &load_index_result); | |
| 224 base::RunLoop().RunUntilIdle(); | 218 base::RunLoop().RunUntilIdle(); |
| 225 | 219 |
| 226 EXPECT_TRUE(base::PathExists(simple_index_file.GetIndexFilePath())); | 220 EXPECT_TRUE(base::PathExists(simple_index_file.GetIndexFilePath())); |
| 227 ASSERT_TRUE(callback_called()); | 221 ASSERT_TRUE(callback_called()); |
| 228 EXPECT_TRUE(load_index_result.did_load); | 222 EXPECT_TRUE(load_index_result.did_load); |
| 229 EXPECT_FALSE(load_index_result.flush_required); | 223 EXPECT_FALSE(load_index_result.flush_required); |
| 230 | 224 |
| 231 EXPECT_EQ(kNumHashes, load_index_result.entries.size()); | 225 EXPECT_EQ(kNumHashes, load_index_result.entries.size()); |
| 232 for (size_t i = 0; i < kNumHashes; ++i) | 226 for (size_t i = 0; i < kNumHashes; ++i) |
| 233 EXPECT_EQ(1U, load_index_result.entries.count(kHashes[i])); | 227 EXPECT_EQ(1U, load_index_result.entries.count(kHashes[i])); |
| 234 } | 228 } |
| 235 | 229 |
| 236 TEST_F(SimpleIndexFileTest, LoadCorruptIndex) { | 230 TEST_F(SimpleIndexFileTest, LoadCorruptIndex) { |
| 237 base::ScopedTempDir cache_dir; | 231 base::ScopedTempDir cache_dir; |
| 238 ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); | 232 ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); |
| 239 | 233 |
| 240 WrappedSimpleIndexFile simple_index_file(cache_dir.path()); | 234 WrappedSimpleIndexFile simple_index_file(cache_dir.path()); |
| 241 ASSERT_TRUE(simple_index_file.CreateIndexFileDirectory()); | 235 ASSERT_TRUE(simple_index_file.CreateIndexFileDirectory()); |
| 242 const base::FilePath& index_path = simple_index_file.GetIndexFilePath(); | 236 const base::FilePath& index_path = simple_index_file.GetIndexFilePath(); |
| 243 const std::string kDummyData = "nothing to be seen here"; | 237 const std::string kDummyData = "nothing to be seen here"; |
| 244 EXPECT_EQ( | 238 EXPECT_EQ(implicit_cast<int>(kDummyData.size()), |
| 245 implicit_cast<int>(kDummyData.size()), | 239 base::WriteFile(index_path, kDummyData.data(), kDummyData.size())); |
| 246 base::WriteFile(index_path, kDummyData.data(), kDummyData.size())); | |
| 247 base::Time fake_cache_mtime; | 240 base::Time fake_cache_mtime; |
| 248 ASSERT_TRUE(simple_util::GetMTime(simple_index_file.GetIndexFilePath(), | 241 ASSERT_TRUE(simple_util::GetMTime(simple_index_file.GetIndexFilePath(), |
| 249 &fake_cache_mtime)); | 242 &fake_cache_mtime)); |
| 250 EXPECT_FALSE(WrappedSimpleIndexFile::LegacyIsIndexFileStale(fake_cache_mtime, | 243 EXPECT_FALSE(WrappedSimpleIndexFile::LegacyIsIndexFileStale(fake_cache_mtime, |
| 251 index_path)); | 244 index_path)); |
| 252 | 245 |
| 253 SimpleIndexLoadResult load_index_result; | 246 SimpleIndexLoadResult load_index_result; |
| 254 simple_index_file.LoadIndexEntries(fake_cache_mtime, | 247 simple_index_file.LoadIndexEntries( |
| 255 GetCallback(), | 248 fake_cache_mtime, GetCallback(), &load_index_result); |
| 256 &load_index_result); | |
| 257 base::RunLoop().RunUntilIdle(); | 249 base::RunLoop().RunUntilIdle(); |
| 258 | 250 |
| 259 EXPECT_FALSE(base::PathExists(index_path)); | 251 EXPECT_FALSE(base::PathExists(index_path)); |
| 260 ASSERT_TRUE(callback_called()); | 252 ASSERT_TRUE(callback_called()); |
| 261 EXPECT_TRUE(load_index_result.did_load); | 253 EXPECT_TRUE(load_index_result.did_load); |
| 262 EXPECT_TRUE(load_index_result.flush_required); | 254 EXPECT_TRUE(load_index_result.flush_required); |
| 263 } | 255 } |
| 264 | 256 |
| 265 // Tests that after an upgrade the backend has the index file put in place. | 257 // Tests that after an upgrade the backend has the index file put in place. |
| 266 TEST_F(SimpleIndexFileTest, SimpleCacheUpgrade) { | 258 TEST_F(SimpleIndexFileTest, SimpleCacheUpgrade) { |
| 267 base::ScopedTempDir cache_dir; | 259 base::ScopedTempDir cache_dir; |
| 268 ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); | 260 ASSERT_TRUE(cache_dir.CreateUniqueTempDir()); |
| 269 const base::FilePath cache_path = cache_dir.path(); | 261 const base::FilePath cache_path = cache_dir.path(); |
| 270 | 262 |
| 271 // Write an old fake index file. | 263 // Write an old fake index file. |
| 272 base::File file(cache_path.AppendASCII("index"), | 264 base::File file(cache_path.AppendASCII("index"), |
| 273 base::File::FLAG_CREATE | base::File::FLAG_WRITE); | 265 base::File::FLAG_CREATE | base::File::FLAG_WRITE); |
| 274 ASSERT_TRUE(file.IsValid()); | 266 ASSERT_TRUE(file.IsValid()); |
| 275 disk_cache::FakeIndexData file_contents; | 267 disk_cache::FakeIndexData file_contents; |
| 276 file_contents.initial_magic_number = disk_cache::kSimpleInitialMagicNumber; | 268 file_contents.initial_magic_number = disk_cache::kSimpleInitialMagicNumber; |
| 277 file_contents.version = 5; | 269 file_contents.version = 5; |
| 278 int bytes_written = file.Write(0, reinterpret_cast<char*>(&file_contents), | 270 int bytes_written = file.Write( |
| 279 sizeof(file_contents)); | 271 0, reinterpret_cast<char*>(&file_contents), sizeof(file_contents)); |
| 280 ASSERT_EQ((int)sizeof(file_contents), bytes_written); | 272 ASSERT_EQ((int)sizeof(file_contents), bytes_written); |
| 281 file.Close(); | 273 file.Close(); |
| 282 | 274 |
| 283 // Write the index file. The format is incorrect, but for transitioning from | 275 // Write the index file. The format is incorrect, but for transitioning from |
| 284 // v5 it does not matter. | 276 // v5 it does not matter. |
| 285 const std::string index_file_contents("incorrectly serialized data"); | 277 const std::string index_file_contents("incorrectly serialized data"); |
| 286 const base::FilePath old_index_file = | 278 const base::FilePath old_index_file = |
| 287 cache_path.AppendASCII("the-real-index"); | 279 cache_path.AppendASCII("the-real-index"); |
| 288 ASSERT_EQ(implicit_cast<int>(index_file_contents.size()), | 280 ASSERT_EQ(implicit_cast<int>(index_file_contents.size()), |
| 289 base::WriteFile(old_index_file, | 281 base::WriteFile(old_index_file, |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 WrappedSimpleIndexFile::Deserialize(contents.data(), | 325 WrappedSimpleIndexFile::Deserialize(contents.data(), |
| 334 contents.size(), | 326 contents.size(), |
| 335 &when_index_last_saw_cache, | 327 &when_index_last_saw_cache, |
| 336 &deserialize_result); | 328 &deserialize_result); |
| 337 EXPECT_TRUE(deserialize_result.did_load); | 329 EXPECT_TRUE(deserialize_result.did_load); |
| 338 } | 330 } |
| 339 | 331 |
| 340 #endif // defined(OS_POSIX) | 332 #endif // defined(OS_POSIX) |
| 341 | 333 |
| 342 } // namespace disk_cache | 334 } // namespace disk_cache |
| OLD | NEW |