Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(566)

Side by Side Diff: net/disk_cache/simple/simple_index_file_unittest.cc

Issue 17265007: Unlink corrupt SimpleCache index files immediately after load. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: tryable Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 44
45 EXPECT_EQ(new_index_metadata.magic_number_, index_metadata.magic_number_); 45 EXPECT_EQ(new_index_metadata.magic_number_, index_metadata.magic_number_);
46 EXPECT_EQ(new_index_metadata.version_, index_metadata.version_); 46 EXPECT_EQ(new_index_metadata.version_, index_metadata.version_);
47 EXPECT_EQ(new_index_metadata.GetNumberOfEntries(), 47 EXPECT_EQ(new_index_metadata.GetNumberOfEntries(),
48 index_metadata.GetNumberOfEntries()); 48 index_metadata.GetNumberOfEntries());
49 EXPECT_EQ(new_index_metadata.cache_size_, index_metadata.cache_size_); 49 EXPECT_EQ(new_index_metadata.cache_size_, index_metadata.cache_size_);
50 50
51 EXPECT_TRUE(new_index_metadata.CheckIndexMetadata()); 51 EXPECT_TRUE(new_index_metadata.CheckIndexMetadata());
52 } 52 }
53 53
54 class TestSimpleIndexFile : public SimpleIndexFile {
Randy Smith (Not in Mondays) 2013/06/19 21:06:00 The pattern I've seen more often is friending the
gavinp 2013/06/19 23:21:25 Done.
55 public:
56 using SimpleIndexFile::Deserialize;
57 using SimpleIndexFile::IsIndexFileStale;
58 using SimpleIndexFile::kIndexFileName;
59 using SimpleIndexFile::LoadFromDisk;
60 using SimpleIndexFile::Serialize;
61
62 explicit TestSimpleIndexFile(const base::FilePath& index_file_directory)
63 : SimpleIndexFile(base::MessageLoopProxy::current(),
64 base::MessageLoopProxy::current(),
65 index_file_directory) {
66 }
67 virtual ~TestSimpleIndexFile() {
68 }
69 };
70
54 class SimpleIndexFileTest : public testing::Test { 71 class SimpleIndexFileTest : public testing::Test {
55 public: 72 public:
56 bool CompareTwoEntryMetadata(const EntryMetadata& a, const EntryMetadata& b) { 73 bool CompareTwoEntryMetadata(const EntryMetadata& a, const EntryMetadata& b) {
57 return a.last_used_time_ == b.last_used_time_ && 74 return a.last_used_time_ == b.last_used_time_ &&
58 a.entry_size_ == b.entry_size_; 75 a.entry_size_ == b.entry_size_;
59 } 76 }
60 77
61 void IndexCompletionCallback(
62 scoped_ptr<SimpleIndex::EntrySet> index_file_entries,
63 bool force_index_flush) {
64 EXPECT_FALSE(callback_result_);
65 callback_result_.reset(
66 new IndexCompletionCallbackResult(index_file_entries.Pass(),
67 force_index_flush));
68 }
69
70 protected: 78 protected:
71 struct IndexCompletionCallbackResult { 79 struct IndexCompletionCallbackResult {
72 IndexCompletionCallbackResult( 80 IndexCompletionCallbackResult(
73 scoped_ptr<SimpleIndex::EntrySet> index_file_entries, 81 scoped_ptr<SimpleIndex::EntrySet> index_file_entries,
74 bool force_index_flush) 82 bool force_index_flush)
75 : index_file_entries(index_file_entries.Pass()), 83 : index_file_entries(index_file_entries.Pass()),
76 force_index_flush(force_index_flush) { 84 force_index_flush(force_index_flush) {
77 } 85 }
78 86
79 const scoped_ptr<SimpleIndex::EntrySet> index_file_entries; 87 const scoped_ptr<SimpleIndex::EntrySet> index_file_entries;
80 const bool force_index_flush; 88 const bool force_index_flush;
81 }; 89 };
82 90
91 SimpleIndexFile::IndexCompletionCallback GetCallback() {
gavinp 2013/06/19 17:17:31 rdsmith: This fixes the funny scoping you complain
Randy Smith (Not in Mondays) 2013/06/19 21:06:00 Amusing. Now that I understand what's going on, I
gavinp 2013/06/19 23:21:25 I prefer this; it makes the tests more succinct bu
Randy Smith (Not in Mondays) 2013/06/20 12:44:30 SG; no worries.
92 return base::Bind(&SimpleIndexFileTest::IndexCompletionCallback,
93 base::Unretained(this));
94 }
95
83 IndexCompletionCallbackResult* callback_result() { 96 IndexCompletionCallbackResult* callback_result() {
84 return callback_result_.get(); 97 return callback_result_.get();
85 } 98 }
86 99
87 private: 100 private:
101 void IndexCompletionCallback(
102 scoped_ptr<SimpleIndex::EntrySet> index_file_entries,
103 bool force_index_flush) {
104 EXPECT_FALSE(callback_result_);
105 callback_result_.reset(
106 new IndexCompletionCallbackResult(index_file_entries.Pass(),
107 force_index_flush));
108 }
109
88 scoped_ptr<IndexCompletionCallbackResult> callback_result_; 110 scoped_ptr<IndexCompletionCallbackResult> callback_result_;
89 }; 111 };
90 112
91 TEST_F(SimpleIndexFileTest, Serialize) { 113 TEST_F(SimpleIndexFileTest, Serialize) {
92 SimpleIndex::EntrySet entries; 114 SimpleIndex::EntrySet entries;
93 static const uint64 kHashes[] = { 11, 22, 33 }; 115 static const uint64 kHashes[] = { 11, 22, 33 };
94 static const size_t kNumHashes = arraysize(kHashes); 116 static const size_t kNumHashes = arraysize(kHashes);
95 EntryMetadata metadata_entries[kNumHashes]; 117 EntryMetadata metadata_entries[kNumHashes];
96 118
97 SimpleIndexFile::IndexMetadata index_metadata(static_cast<uint64>(kNumHashes), 119 SimpleIndexFile::IndexMetadata index_metadata(static_cast<uint64>(kNumHashes),
98 456); 120 456);
99 for (size_t i = 0; i < kNumHashes; ++i) { 121 for (size_t i = 0; i < kNumHashes; ++i) {
100 uint64 hash = kHashes[i]; 122 uint64 hash = kHashes[i];
101 metadata_entries[i] = 123 metadata_entries[i] =
102 EntryMetadata(Time::FromInternalValue(hash), hash); 124 EntryMetadata(Time::FromInternalValue(hash), hash);
103 SimpleIndex::InsertInEntrySet(hash, metadata_entries[i], &entries); 125 SimpleIndex::InsertInEntrySet(hash, metadata_entries[i], &entries);
104 } 126 }
105 127
106 scoped_ptr<Pickle> pickle = SimpleIndexFile::Serialize( 128 scoped_ptr<Pickle> pickle = TestSimpleIndexFile::Serialize(
107 index_metadata, entries); 129 index_metadata, entries);
108 EXPECT_TRUE(pickle.get() != NULL); 130 EXPECT_TRUE(pickle.get() != NULL);
109 131
110 scoped_ptr<SimpleIndex::EntrySet> new_entries = SimpleIndexFile::Deserialize( 132 scoped_ptr<SimpleIndex::EntrySet> new_entries =
111 reinterpret_cast<const char*>(pickle->data()), 133 TestSimpleIndexFile::Deserialize(static_cast<const char*>(pickle->data()),
112 pickle->size()); 134 pickle->size());
113 EXPECT_TRUE(new_entries.get() != NULL); 135 EXPECT_TRUE(new_entries.get() != NULL);
114 EXPECT_EQ(entries.size(), new_entries->size()); 136 EXPECT_EQ(entries.size(), new_entries->size());
115 137
116 for (size_t i = 0; i < kNumHashes; ++i) { 138 for (size_t i = 0; i < kNumHashes; ++i) {
117 SimpleIndex::EntrySet::iterator it = new_entries->find(kHashes[i]); 139 SimpleIndex::EntrySet::iterator it = new_entries->find(kHashes[i]);
118 EXPECT_TRUE(new_entries->end() != it); 140 EXPECT_TRUE(new_entries->end() != it);
119 EXPECT_TRUE(CompareTwoEntryMetadata(it->second, metadata_entries[i])); 141 EXPECT_TRUE(CompareTwoEntryMetadata(it->second, metadata_entries[i]));
120 } 142 }
121 } 143 }
122 144
123 TEST_F(SimpleIndexFileTest, IsIndexFileStale) { 145 TEST_F(SimpleIndexFileTest, IsIndexFileStale) {
124 base::ScopedTempDir temp_dir; 146 base::ScopedTempDir temp_dir;
125 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 147 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
126 148
127 const std::string kIndexFileName = "simple-index"; 149 const std::string kIndexFileName = "simple-index";
128 const base::FilePath index_path = 150 const base::FilePath index_path =
129 temp_dir.path().AppendASCII(kIndexFileName); 151 temp_dir.path().AppendASCII(kIndexFileName);
130 EXPECT_TRUE(SimpleIndexFile::IsIndexFileStale(index_path)); 152 EXPECT_TRUE(TestSimpleIndexFile::IsIndexFileStale(index_path));
131 const std::string kDummyData = "nothing to be seen here"; 153 const std::string kDummyData = "nothing to be seen here";
132 EXPECT_EQ(static_cast<int>(kDummyData.size()), 154 EXPECT_EQ(static_cast<int>(kDummyData.size()),
133 file_util::WriteFile(index_path, 155 file_util::WriteFile(index_path,
134 kDummyData.data(), 156 kDummyData.data(),
135 kDummyData.size())); 157 kDummyData.size()));
136 EXPECT_FALSE(SimpleIndexFile::IsIndexFileStale(index_path)); 158 EXPECT_FALSE(TestSimpleIndexFile::IsIndexFileStale(index_path));
137 159
138 const base::Time past_time = base::Time::Now() - 160 const base::Time past_time = base::Time::Now() -
139 base::TimeDelta::FromSeconds(10); 161 base::TimeDelta::FromSeconds(10);
140 EXPECT_TRUE(file_util::TouchFile(index_path, past_time, past_time)); 162 EXPECT_TRUE(file_util::TouchFile(index_path, past_time, past_time));
141 EXPECT_TRUE(file_util::TouchFile(temp_dir.path(), past_time, past_time)); 163 EXPECT_TRUE(file_util::TouchFile(temp_dir.path(), past_time, past_time));
142 EXPECT_FALSE(SimpleIndexFile::IsIndexFileStale(index_path)); 164 EXPECT_FALSE(TestSimpleIndexFile::IsIndexFileStale(index_path));
143 165
144 EXPECT_EQ(static_cast<int>(kDummyData.size()), 166 EXPECT_EQ(static_cast<int>(kDummyData.size()),
145 file_util::WriteFile(temp_dir.path().AppendASCII("other_file"), 167 file_util::WriteFile(temp_dir.path().AppendASCII("other_file"),
146 kDummyData.data(), 168 kDummyData.data(),
147 kDummyData.size())); 169 kDummyData.size()));
148 170
149 EXPECT_TRUE(SimpleIndexFile::IsIndexFileStale(index_path)); 171 EXPECT_TRUE(TestSimpleIndexFile::IsIndexFileStale(index_path));
150 } 172 }
151 173
152 TEST_F(SimpleIndexFileTest, IsIndexFileCorrupt) { 174 TEST_F(SimpleIndexFileTest, WriteThenLoadIndex) {
153 base::ScopedTempDir temp_dir; 175 base::ScopedTempDir temp_dir;
154 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 176 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
155 177
156 const base::FilePath index_path = 178 const base::FilePath index_path =
157 temp_dir.path().AppendASCII(SimpleIndexFile::kIndexFileName); 179 temp_dir.path().AppendASCII(TestSimpleIndexFile::kIndexFileName);
158 EXPECT_TRUE(SimpleIndexFile::IsIndexFileStale(index_path)); 180 EXPECT_TRUE(TestSimpleIndexFile::IsIndexFileStale(index_path));
181
182 SimpleIndex::EntrySet entries;
183 static const uint64 kHashes[] = { 11, 22, 33 };
184 static const size_t kNumHashes = arraysize(kHashes);
185 EntryMetadata metadata_entries[kNumHashes];
186 for (size_t i = 0; i < kNumHashes; ++i) {
187 uint64 hash = kHashes[i];
188 metadata_entries[i] =
189 EntryMetadata(Time::FromInternalValue(hash), hash);
190 SimpleIndex::InsertInEntrySet(hash, metadata_entries[i], &entries);
191 }
192
193 const uint64 kCacheSize = 456U;
194 {
195 TestSimpleIndexFile simple_index_file(temp_dir.path());
196 simple_index_file.WriteToDisk(entries, kCacheSize,
197 base::TimeTicks(), false);
198 base::RunLoop().RunUntilIdle();
199 EXPECT_TRUE(file_util::PathExists(index_path));
200 }
201
202 TestSimpleIndexFile simple_index_file(temp_dir.path());
203 simple_index_file.LoadIndexEntries(base::MessageLoopProxy::current(),
204 GetCallback());
205 base::RunLoop().RunUntilIdle();
206
207 EXPECT_TRUE(file_util::PathExists(index_path));
208 ASSERT_TRUE(callback_result());
209 EXPECT_FALSE(callback_result()->force_index_flush);
210 const SimpleIndex::EntrySet* read_entries =
211 callback_result()->index_file_entries.get();
212 ASSERT_TRUE(read_entries);
213
214 EXPECT_EQ(kNumHashes, read_entries->size());
215 for (size_t i = 0; i < kNumHashes; ++i)
216 EXPECT_EQ(1U, read_entries->count(kHashes[i]));
217 }
218
219 TEST_F(SimpleIndexFileTest, LoadCorruptIndex) {
220 base::ScopedTempDir temp_dir;
221 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
222
223 const base::FilePath index_path =
224 temp_dir.path().AppendASCII(TestSimpleIndexFile::kIndexFileName);
225 EXPECT_TRUE(TestSimpleIndexFile::IsIndexFileStale(index_path));
159 const std::string kDummyData = "nothing to be seen here"; 226 const std::string kDummyData = "nothing to be seen here";
160 EXPECT_EQ(static_cast<int>(kDummyData.size()), 227 EXPECT_EQ(static_cast<int>(kDummyData.size()),
161 file_util::WriteFile(index_path, 228 file_util::WriteFile(index_path,
162 kDummyData.data(), 229 kDummyData.data(),
163 kDummyData.size())); 230 kDummyData.size()));
164 EXPECT_FALSE(SimpleIndexFile::IsIndexFileStale(index_path)); 231 EXPECT_FALSE(TestSimpleIndexFile::IsIndexFileStale(index_path));
165 232
166 SimpleIndexFile simple_index_file(base::MessageLoopProxy::current(), 233 TestSimpleIndexFile simple_index_file(temp_dir.path());
167 base::MessageLoopProxy::current(),
168 temp_dir.path());
169
170 SimpleIndexFile::IndexCompletionCallback callback =
171 base::Bind(&SimpleIndexFileTest::IndexCompletionCallback,
172 base::Unretained(this));
173
174 simple_index_file.LoadIndexEntries(base::MessageLoopProxy::current(), 234 simple_index_file.LoadIndexEntries(base::MessageLoopProxy::current(),
175 callback); 235 GetCallback());
176 base::RunLoop().RunUntilIdle(); 236 base::RunLoop().RunUntilIdle();
177 237
238 EXPECT_FALSE(file_util::PathExists(index_path));
178 ASSERT_TRUE(callback_result()); 239 ASSERT_TRUE(callback_result());
240 EXPECT_TRUE(callback_result()->force_index_flush);
179 EXPECT_TRUE(callback_result()->index_file_entries); 241 EXPECT_TRUE(callback_result()->index_file_entries);
180 EXPECT_TRUE(callback_result()->force_index_flush);
181 } 242 }
182 243
183 } // namespace disk_cache 244 } // namespace disk_cache
OLDNEW
« no previous file with comments | « net/disk_cache/simple/simple_index_file.cc ('k') | net/disk_cache/simple/simple_index_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698