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/scoped_temp_dir.h" | 6 #include "base/files/scoped_temp_dir.h" |
7 #include "base/hash.h" | 7 #include "base/hash.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/message_loop/message_loop_proxy.h" |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 EXPECT_FALSE(SimpleIndexFile::IsIndexFileStale(index_path)); | 142 EXPECT_FALSE(SimpleIndexFile::IsIndexFileStale(index_path)); |
143 | 143 |
144 EXPECT_EQ(static_cast<int>(kDummyData.size()), | 144 EXPECT_EQ(static_cast<int>(kDummyData.size()), |
145 file_util::WriteFile(temp_dir.path().AppendASCII("other_file"), | 145 file_util::WriteFile(temp_dir.path().AppendASCII("other_file"), |
146 kDummyData.data(), | 146 kDummyData.data(), |
147 kDummyData.size())); | 147 kDummyData.size())); |
148 | 148 |
149 EXPECT_TRUE(SimpleIndexFile::IsIndexFileStale(index_path)); | 149 EXPECT_TRUE(SimpleIndexFile::IsIndexFileStale(index_path)); |
150 } | 150 } |
151 | 151 |
| 152 TEST_F(SimpleIndexFileTest, WriteThenLoadIndex) { |
| 153 base::ScopedTempDir temp_dir; |
| 154 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 155 |
| 156 const base::FilePath index_path = |
| 157 temp_dir.path().AppendASCII(SimpleIndexFile::kIndexFileName); |
| 158 EXPECT_TRUE(SimpleIndexFile::IsIndexFileStale(index_path)); |
| 159 |
| 160 SimpleIndex::EntrySet entries; |
| 161 static const uint64 kHashes[] = { 11, 22, 33 }; |
| 162 static const size_t kNumHashes = arraysize(kHashes); |
| 163 EntryMetadata metadata_entries[kNumHashes]; |
| 164 for (size_t i = 0; i < kNumHashes; ++i) { |
| 165 uint64 hash = kHashes[i]; |
| 166 metadata_entries[i] = |
| 167 EntryMetadata(Time::FromInternalValue(hash), hash); |
| 168 SimpleIndex::InsertInEntrySet(hash, metadata_entries[i], &entries); |
| 169 } |
| 170 |
| 171 const uint64 kCacheSize = 456U; |
| 172 { |
| 173 SimpleIndexFile simple_index_file(base::MessageLoopProxy::current(), |
| 174 base::MessageLoopProxy::current(), |
| 175 temp_dir.path()); |
| 176 simple_index_file.WriteToDisk(entries, kCacheSize, |
| 177 base::TimeTicks(), false); |
| 178 base::RunLoop().RunUntilIdle(); |
| 179 EXPECT_TRUE(file_util::PathExists(index_path)); |
| 180 } |
| 181 |
| 182 SimpleIndexFile simple_index_file(base::MessageLoopProxy::current(), |
| 183 base::MessageLoopProxy::current(), |
| 184 temp_dir.path()); |
| 185 SimpleIndexFile::IndexCompletionCallback callback = |
| 186 base::Bind(&SimpleIndexFileTest::IndexCompletionCallback, |
| 187 base::Unretained(this)); |
| 188 simple_index_file.LoadIndexEntries(base::MessageLoopProxy::current(), |
| 189 callback); |
| 190 base::RunLoop().RunUntilIdle(); |
| 191 |
| 192 EXPECT_TRUE(file_util::PathExists(index_path)); |
| 193 ASSERT_TRUE(callback_result()); |
| 194 EXPECT_FALSE(callback_result()->force_index_flush); |
| 195 const SimpleIndex::EntrySet* read_entries = |
| 196 callback_result()->index_file_entries.get(); |
| 197 ASSERT_TRUE(read_entries); |
| 198 |
| 199 EXPECT_EQ(kNumHashes, read_entries->size()); |
| 200 for (size_t i = 0; i < kNumHashes; ++i) |
| 201 EXPECT_EQ(1U, read_entries->count(kHashes[i])); |
| 202 } |
| 203 |
152 TEST_F(SimpleIndexFileTest, IsIndexFileCorrupt) { | 204 TEST_F(SimpleIndexFileTest, IsIndexFileCorrupt) { |
153 base::ScopedTempDir temp_dir; | 205 base::ScopedTempDir temp_dir; |
154 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 206 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
155 | 207 |
156 const base::FilePath index_path = | 208 const base::FilePath index_path = |
157 temp_dir.path().AppendASCII(SimpleIndexFile::kIndexFileName); | 209 temp_dir.path().AppendASCII(SimpleIndexFile::kIndexFileName); |
158 EXPECT_TRUE(SimpleIndexFile::IsIndexFileStale(index_path)); | 210 EXPECT_TRUE(SimpleIndexFile::IsIndexFileStale(index_path)); |
159 const std::string kDummyData = "nothing to be seen here"; | 211 const std::string kDummyData = "nothing to be seen here"; |
160 EXPECT_EQ(static_cast<int>(kDummyData.size()), | 212 EXPECT_EQ(static_cast<int>(kDummyData.size()), |
161 file_util::WriteFile(index_path, | 213 file_util::WriteFile(index_path, |
162 kDummyData.data(), | 214 kDummyData.data(), |
163 kDummyData.size())); | 215 kDummyData.size())); |
164 EXPECT_FALSE(SimpleIndexFile::IsIndexFileStale(index_path)); | 216 EXPECT_FALSE(SimpleIndexFile::IsIndexFileStale(index_path)); |
165 | 217 |
166 SimpleIndexFile simple_index_file(base::MessageLoopProxy::current(), | 218 SimpleIndexFile simple_index_file(base::MessageLoopProxy::current(), |
167 base::MessageLoopProxy::current(), | 219 base::MessageLoopProxy::current(), |
168 temp_dir.path()); | 220 temp_dir.path()); |
169 | 221 |
170 SimpleIndexFile::IndexCompletionCallback callback = | 222 SimpleIndexFile::IndexCompletionCallback callback = |
171 base::Bind(&SimpleIndexFileTest::IndexCompletionCallback, | 223 base::Bind(&SimpleIndexFileTest::IndexCompletionCallback, |
172 base::Unretained(this)); | 224 base::Unretained(this)); |
173 | 225 |
174 simple_index_file.LoadIndexEntries(base::MessageLoopProxy::current(), | 226 simple_index_file.LoadIndexEntries(base::MessageLoopProxy::current(), |
175 callback); | 227 callback); |
176 base::RunLoop().RunUntilIdle(); | 228 base::RunLoop().RunUntilIdle(); |
177 | 229 |
| 230 EXPECT_FALSE(file_util::PathExists(index_path)); |
178 ASSERT_TRUE(callback_result()); | 231 ASSERT_TRUE(callback_result()); |
179 EXPECT_TRUE(callback_result()->index_file_entries); | 232 EXPECT_TRUE(callback_result()->index_file_entries); |
180 EXPECT_TRUE(callback_result()->force_index_flush); | 233 EXPECT_TRUE(callback_result()->force_index_flush); |
181 } | 234 } |
182 | 235 |
183 } // namespace disk_cache | 236 } // namespace disk_cache |
OLD | NEW |