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

Side by Side Diff: components/safe_browsing_db/v4_database_unittest.cc

Issue 2062013002: Fetch incremental updates. Store new state in V4Store. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Nit: Use base::SStringPrintf instead of string concat Created 4 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/bind.h" 5 #include "base/bind.h"
6 #include "base/debug/leak_annotations.h" 6 #include "base/debug/leak_annotations.h"
7 #include "base/files/scoped_temp_dir.h" 7 #include "base/files/scoped_temp_dir.h"
8 #include "base/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "base/test/test_simple_task_runner.h" 10 #include "base/test/test_simple_task_runner.h"
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 created_but_not_called_back_ = false; 66 created_but_not_called_back_ = false;
67 created_and_called_back_ = false; 67 created_and_called_back_ = false;
68 } 68 }
69 69
70 void SetupInfoMapAndExpectedState() { 70 void SetupInfoMapAndExpectedState() {
71 UpdateListIdentifier update_list_identifier; 71 UpdateListIdentifier update_list_identifier;
72 72
73 update_list_identifier.platform_type = WINDOWS_PLATFORM; 73 update_list_identifier.platform_type = WINDOWS_PLATFORM;
74 update_list_identifier.threat_entry_type = URL; 74 update_list_identifier.threat_entry_type = URL;
75 update_list_identifier.threat_type = MALWARE_THREAT; 75 update_list_identifier.threat_type = MALWARE_THREAT;
76 list_info_map_[update_list_identifier] = "win_url_malware"; 76 store_file_name_map_[update_list_identifier] = "win_url_malware";
77 expected_identifiers_.push_back(update_list_identifier); 77 expected_identifiers_.push_back(update_list_identifier);
78 expected_store_paths_.push_back( 78 expected_store_paths_.push_back(
79 database_dirname_.AppendASCII("win_url_malware.fake")); 79 database_dirname_.AppendASCII("win_url_malware.fake"));
80 80
81 update_list_identifier.platform_type = LINUX_PLATFORM; 81 update_list_identifier.platform_type = LINUX_PLATFORM;
82 update_list_identifier.threat_entry_type = URL; 82 update_list_identifier.threat_entry_type = URL;
83 update_list_identifier.threat_type = MALWARE_THREAT; 83 update_list_identifier.threat_type = MALWARE_THREAT;
84 list_info_map_[update_list_identifier] = "linux_url_malware"; 84 store_file_name_map_[update_list_identifier] = "linux_url_malware";
85 expected_identifiers_.push_back(update_list_identifier); 85 expected_identifiers_.push_back(update_list_identifier);
86 expected_store_paths_.push_back( 86 expected_store_paths_.push_back(
87 database_dirname_.AppendASCII("linux_url_malware.fake")); 87 database_dirname_.AppendASCII("linux_url_malware.fake"));
88 } 88 }
89 89
90 void DatabaseUpdated() {}
90 void NewDatabaseReadyWithExpectedStorePathsAndIds( 91 void NewDatabaseReadyWithExpectedStorePathsAndIds(
91 std::vector<base::FilePath> expected_store_paths, 92 std::vector<base::FilePath> expected_store_paths,
92 std::vector<UpdateListIdentifier> expected_identifiers, 93 std::vector<UpdateListIdentifier> expected_identifiers,
93 bool expected_resets_successfully, 94 bool expected_resets_successfully,
94 std::unique_ptr<V4Database> v4_database) { 95 std::unique_ptr<V4Database> v4_database) {
95 ASSERT_TRUE(v4_database); 96 ASSERT_TRUE(v4_database);
96 ASSERT_TRUE(v4_database->store_map_); 97 ASSERT_TRUE(v4_database->store_map_);
97 98
98 // The following check ensures that the callback was called asynchronously. 99 // The following check ensures that the callback was called asynchronously.
99 EXPECT_TRUE(created_but_not_called_back_); 100 EXPECT_TRUE(created_but_not_called_back_);
100 101
101 ASSERT_EQ(expected_store_paths.size(), v4_database->store_map_->size()); 102 ASSERT_EQ(expected_store_paths.size(), v4_database->store_map_->size());
102 ASSERT_EQ(expected_identifiers.size(), v4_database->store_map_->size()); 103 ASSERT_EQ(expected_identifiers.size(), v4_database->store_map_->size());
103 for (size_t i = 0; i < expected_identifiers.size(); i++) { 104 for (size_t i = 0; i < expected_identifiers.size(); i++) {
104 const auto& expected_identifier = expected_identifiers[i]; 105 const auto& expected_identifier = expected_identifiers[i];
105 const auto& store = (*v4_database->store_map_)[expected_identifier]; 106 const auto& store = (*v4_database->store_map_)[expected_identifier];
106 ASSERT_TRUE(store); 107 ASSERT_TRUE(store);
107 const auto& expected_store_path = expected_store_paths[i]; 108 const auto& expected_store_path = expected_store_paths[i];
108 EXPECT_EQ(expected_store_path, store->store_path()); 109 EXPECT_EQ(expected_store_path, store->store_path());
109 } 110 }
110 111
111 EXPECT_EQ(expected_resets_successfully, v4_database->ResetDatabase()); 112 EXPECT_EQ(expected_resets_successfully, v4_database->ResetDatabase());
112 113
113 EXPECT_FALSE(created_and_called_back_); 114 EXPECT_FALSE(created_and_called_back_);
114 created_and_called_back_ = true; 115 created_and_called_back_ = true;
116
117 v4_database_ = std::move(v4_database);
118 }
119
120 void PopulateFakeUpdateResponse(StoreStateMap store_state_map,
121 std::vector<ListUpdateResponse>* responses) {
122 for (const auto& store_state_iter : store_state_map) {
123 UpdateListIdentifier identifier = store_state_iter.first;
124 ListUpdateResponse list_update_response;
125 list_update_response.set_platform_type(identifier.platform_type);
126 list_update_response.set_threat_entry_type(identifier.threat_entry_type);
127 list_update_response.set_threat_type(identifier.threat_type);
128 list_update_response.set_new_client_state(store_state_iter.second);
129 responses->push_back(list_update_response);
130 }
115 } 131 }
116 132
117 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; 133 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
118 std::unique_ptr<V4Database> v4_database_; 134 std::unique_ptr<V4Database> v4_database_;
119 base::FilePath database_dirname_; 135 base::FilePath database_dirname_;
120 base::ScopedTempDir temp_dir_; 136 base::ScopedTempDir temp_dir_;
121 content::TestBrowserThreadBundle thread_bundle_; 137 content::TestBrowserThreadBundle thread_bundle_;
122 bool created_but_not_called_back_; 138 bool created_but_not_called_back_;
123 bool created_and_called_back_; 139 bool created_and_called_back_;
124 ListInfoMap list_info_map_; 140 StoreFileNameMap store_file_name_map_;
125 std::vector<UpdateListIdentifier> expected_identifiers_; 141 std::vector<UpdateListIdentifier> expected_identifiers_;
126 std::vector<base::FilePath> expected_store_paths_; 142 std::vector<base::FilePath> expected_store_paths_;
127 }; 143 };
128 144
129 // Test to set up the database with no stores. 145 // Test to set up the database with no stores, which returns false right away.
130 TEST_F(SafeBrowsingV4DatabaseTest, TestSetupDatabaseWithNoStores) { 146 TEST_F(SafeBrowsingV4DatabaseTest, TestSetupDatabaseWithNoStores) {
147 DatabaseUpdatedCallback callback_db_updated = base::Bind(
148 &SafeBrowsingV4DatabaseTest::DatabaseUpdated, base::Unretained(this));
131 NewDatabaseReadyCallback callback_db_ready = base::Bind( 149 NewDatabaseReadyCallback callback_db_ready = base::Bind(
132 &SafeBrowsingV4DatabaseTest::NewDatabaseReadyWithExpectedStorePathsAndIds, 150 &SafeBrowsingV4DatabaseTest::NewDatabaseReadyWithExpectedStorePathsAndIds,
133 base::Unretained(this), expected_store_paths_, expected_identifiers_, 151 base::Unretained(this), expected_store_paths_, expected_identifiers_,
134 true); 152 true);
135 V4Database::Create(task_runner_, database_dirname_, list_info_map_, 153 EXPECT_FALSE(V4Database::Create(task_runner_, database_dirname_,
136 callback_db_ready); 154 store_file_name_map_, callback_db_updated,
137 created_but_not_called_back_ = true; 155 callback_db_ready));
138 task_runner_->RunPendingTasks();
139
140 base::RunLoop().RunUntilIdle();
141 EXPECT_EQ(true, created_and_called_back_);
142 } 156 }
143 157
144 // Test to set up the database with fake stores. 158 // Test to set up the database with fake stores.
145 TEST_F(SafeBrowsingV4DatabaseTest, TestSetupDatabaseWithFakeStores) { 159 TEST_F(SafeBrowsingV4DatabaseTest, TestSetupDatabaseWithFakeStores) {
146 SetupInfoMapAndExpectedState(); 160 SetupInfoMapAndExpectedState();
147 161
162 DatabaseUpdatedCallback callback_db_updated = base::Bind(
163 &SafeBrowsingV4DatabaseTest::DatabaseUpdated, base::Unretained(this));
148 NewDatabaseReadyCallback callback_db_ready = base::Bind( 164 NewDatabaseReadyCallback callback_db_ready = base::Bind(
149 &SafeBrowsingV4DatabaseTest::NewDatabaseReadyWithExpectedStorePathsAndIds, 165 &SafeBrowsingV4DatabaseTest::NewDatabaseReadyWithExpectedStorePathsAndIds,
150 base::Unretained(this), expected_store_paths_, expected_identifiers_, 166 base::Unretained(this), expected_store_paths_, expected_identifiers_,
151 true); 167 true);
152 168
153 FakeV4StoreFactory* factory = new FakeV4StoreFactory(false); 169 FakeV4StoreFactory* factory = new FakeV4StoreFactory(false);
154 ANNOTATE_LEAKING_OBJECT_PTR(factory); 170 ANNOTATE_LEAKING_OBJECT_PTR(factory);
155 V4Database::RegisterStoreFactoryForTest(factory); 171 V4Database::RegisterStoreFactoryForTest(factory);
156 V4Database::Create(task_runner_, database_dirname_, list_info_map_, 172 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_,
157 callback_db_ready); 173 callback_db_updated, callback_db_ready);
158 created_but_not_called_back_ = true; 174 created_but_not_called_back_ = true;
159 task_runner_->RunPendingTasks(); 175 task_runner_->RunPendingTasks();
160 176
161 base::RunLoop().RunUntilIdle(); 177 base::RunLoop().RunUntilIdle();
162 EXPECT_EQ(true, created_and_called_back_); 178 EXPECT_EQ(true, created_and_called_back_);
163 } 179 }
164 180
165 // Test to set up the database with fake stores that fail to reset. 181 // Test to set up the database with fake stores that fail to reset.
166 TEST_F(SafeBrowsingV4DatabaseTest, TestSetupDatabaseWithFakeStoresFailsReset) { 182 TEST_F(SafeBrowsingV4DatabaseTest, TestSetupDatabaseWithFakeStoresFailsReset) {
167 SetupInfoMapAndExpectedState(); 183 SetupInfoMapAndExpectedState();
168 184
185 DatabaseUpdatedCallback callback_db_updated = base::Bind(
186 &SafeBrowsingV4DatabaseTest::DatabaseUpdated, base::Unretained(this));
169 NewDatabaseReadyCallback callback_db_ready = base::Bind( 187 NewDatabaseReadyCallback callback_db_ready = base::Bind(
170 &SafeBrowsingV4DatabaseTest::NewDatabaseReadyWithExpectedStorePathsAndIds, 188 &SafeBrowsingV4DatabaseTest::NewDatabaseReadyWithExpectedStorePathsAndIds,
171 base::Unretained(this), expected_store_paths_, expected_identifiers_, 189 base::Unretained(this), expected_store_paths_, expected_identifiers_,
172 false); 190 false);
173 191
174 FakeV4StoreFactory* factory = new FakeV4StoreFactory(true); 192 FakeV4StoreFactory* factory = new FakeV4StoreFactory(true);
175 ANNOTATE_LEAKING_OBJECT_PTR(factory); 193 ANNOTATE_LEAKING_OBJECT_PTR(factory);
176 V4Database::RegisterStoreFactoryForTest(factory); 194 V4Database::RegisterStoreFactoryForTest(factory);
177 V4Database::Create(task_runner_, database_dirname_, list_info_map_, 195 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_,
178 callback_db_ready); 196 callback_db_updated, callback_db_ready);
179 created_but_not_called_back_ = true; 197 created_but_not_called_back_ = true;
180 task_runner_->RunPendingTasks(); 198 task_runner_->RunPendingTasks();
181 199
182 base::RunLoop().RunUntilIdle(); 200 base::RunLoop().RunUntilIdle();
183 EXPECT_EQ(true, created_and_called_back_); 201 EXPECT_EQ(true, created_and_called_back_);
184 } 202 }
185 203
204 // Test to check database updates as expected.
205 TEST_F(SafeBrowsingV4DatabaseTest, TestApplyUpdateWithNewStates) {
206 SetupInfoMapAndExpectedState();
207
208 DatabaseUpdatedCallback callback_db_updated = base::Bind(
209 &SafeBrowsingV4DatabaseTest::DatabaseUpdated, base::Unretained(this));
210 NewDatabaseReadyCallback callback_db_ready = base::Bind(
211 &SafeBrowsingV4DatabaseTest::NewDatabaseReadyWithExpectedStorePathsAndIds,
212 base::Unretained(this), expected_store_paths_, expected_identifiers_,
213 true);
214
215 FakeV4StoreFactory* factory = new FakeV4StoreFactory(false);
Nathan Parker 2016/06/15 21:24:17 Could this go in SetUp(), since you probably don't
vakh (use Gerrit instead) 2016/06/16 01:07:35 Done.
216 ANNOTATE_LEAKING_OBJECT_PTR(factory);
217 V4Database::RegisterStoreFactoryForTest(factory);
218 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_,
219 callback_db_updated, callback_db_ready);
220 created_but_not_called_back_ = true;
221 task_runner_->RunPendingTasks();
222 base::RunLoop().RunUntilIdle();
Nathan Parker 2016/06/15 21:24:17 A chunk of this code is repeated in tests below.
vakh (use Gerrit instead) 2016/06/16 01:07:35 Done.
223
224 // The database has now been created. Time to try to update it.
225 EXPECT_TRUE(v4_database_);
226 const StoreMap* db_stores = v4_database_->store_map_.get();
227 EXPECT_EQ(expected_store_paths_.size(), db_stores->size());
228 StoreStateMap expected_store_state_map;
229 base::hash_map<UpdateListIdentifier, V4Store*> old_stores_map;
230 for (const auto& store_iter : *db_stores) {
231 V4Store* store = store_iter.second.get();
232 expected_store_state_map[store_iter.first] = store->state() + "_fake";
233 old_stores_map[store_iter.first] = store;
234 }
235
236 std::vector<ListUpdateResponse> update_response;
237 PopulateFakeUpdateResponse(expected_store_state_map, &update_response);
238 v4_database_->ApplyUpdate(update_response);
239
240 task_runner_->RunPendingTasks();
241 base::RunLoop().RunUntilIdle();
242
243 const StoreMap* new_store_map = v4_database_->store_map_.get();
244 const StoreStateMap* new_store_state_map = v4_database_->store_state_map();
245 EXPECT_EQ(expected_store_state_map.size(), new_store_map->size());
246 EXPECT_EQ(expected_store_state_map.size(), new_store_state_map->size());
247 for (const auto& expected_iter : expected_store_state_map) {
248 const UpdateListIdentifier& identifier = expected_iter.first;
249 const std::string& state = expected_iter.second;
250 EXPECT_EQ(1ul, new_store_map->count(identifier));
251 EXPECT_EQ(1ul, new_store_state_map->count(identifier));
252
253 // Verify the expected state in the store map and the state map.
254 EXPECT_EQ(state, new_store_map->at(identifier)->state());
255 EXPECT_EQ(state, new_store_state_map->at(identifier));
256
257 // Verify that a new store was created.
Nathan Parker 2016/06/15 21:24:17 Is new one created by the update? I thought the li
vakh (use Gerrit instead) 2016/06/16 01:07:35 A new store is created but since the map holds uni
258 EXPECT_NE(old_stores_map[identifier], new_store_map->at(identifier).get());
259 }
260 }
261
262 // Test to ensure no state updates leads to no store updates.
263 TEST_F(SafeBrowsingV4DatabaseTest, TestApplyUpdateWithNoNewState) {
264 SetupInfoMapAndExpectedState();
265
266 DatabaseUpdatedCallback callback_db_updated = base::Bind(
267 &SafeBrowsingV4DatabaseTest::DatabaseUpdated, base::Unretained(this));
268 NewDatabaseReadyCallback callback_db_ready = base::Bind(
269 &SafeBrowsingV4DatabaseTest::NewDatabaseReadyWithExpectedStorePathsAndIds,
270 base::Unretained(this), expected_store_paths_, expected_identifiers_,
271 true);
272
273 FakeV4StoreFactory* factory = new FakeV4StoreFactory(false);
274 ANNOTATE_LEAKING_OBJECT_PTR(factory);
275 V4Database::RegisterStoreFactoryForTest(factory);
276 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_,
277 callback_db_updated, callback_db_ready);
278 created_but_not_called_back_ = true;
279 task_runner_->RunPendingTasks();
280 base::RunLoop().RunUntilIdle();
281
282 // The database has now been created. Time to try to update it.
283 EXPECT_TRUE(v4_database_);
284 const StoreMap* db_stores = v4_database_->store_map_.get();
285 EXPECT_EQ(expected_store_paths_.size(), db_stores->size());
286 StoreStateMap expected_store_state_map;
287 base::hash_map<UpdateListIdentifier, V4Store*> old_stores_map;
288 for (const auto& store_iter : *db_stores) {
289 V4Store* store = store_iter.second.get();
290 expected_store_state_map[store_iter.first] = store->state();
291 old_stores_map[store_iter.first] = store;
292 }
293
294 std::vector<ListUpdateResponse> update_response;
295 PopulateFakeUpdateResponse(expected_store_state_map, &update_response);
296 v4_database_->ApplyUpdate(update_response);
297
298 task_runner_->RunPendingTasks();
299 base::RunLoop().RunUntilIdle();
300
301 const StoreMap* new_store_map = v4_database_->store_map_.get();
302 const StoreStateMap* new_store_state_map = v4_database_->store_state_map();
303 EXPECT_EQ(expected_store_state_map.size(), new_store_map->size());
304 EXPECT_EQ(expected_store_state_map.size(), new_store_state_map->size());
305 for (const auto& expected_iter : expected_store_state_map) {
306 const UpdateListIdentifier& identifier = expected_iter.first;
307 const std::string& state = expected_iter.second;
308 EXPECT_EQ(1ul, new_store_map->count(identifier));
309 EXPECT_EQ(1ul, new_store_state_map->count(identifier));
310
311 // Verify the expected state in the store map and the state map.
312 EXPECT_EQ(state, new_store_map->at(identifier)->state());
313 EXPECT_EQ(state, new_store_state_map->at(identifier));
314
315 // Verify that NO new store was created.
316 EXPECT_EQ(old_stores_map[identifier], new_store_map->at(identifier).get());
317 }
318 }
319
320 // Test to ensure no updates leads to no store updates.
321 TEST_F(SafeBrowsingV4DatabaseTest, TestApplyUpdateWithEmptyUpdate) {
322 SetupInfoMapAndExpectedState();
323
324 DatabaseUpdatedCallback callback_db_updated = base::Bind(
325 &SafeBrowsingV4DatabaseTest::DatabaseUpdated, base::Unretained(this));
326 NewDatabaseReadyCallback callback_db_ready = base::Bind(
327 &SafeBrowsingV4DatabaseTest::NewDatabaseReadyWithExpectedStorePathsAndIds,
328 base::Unretained(this), expected_store_paths_, expected_identifiers_,
329 true);
330
331 FakeV4StoreFactory* factory = new FakeV4StoreFactory(false);
332 ANNOTATE_LEAKING_OBJECT_PTR(factory);
333 V4Database::RegisterStoreFactoryForTest(factory);
334 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_,
335 callback_db_updated, callback_db_ready);
336 created_but_not_called_back_ = true;
337 task_runner_->RunPendingTasks();
338 base::RunLoop().RunUntilIdle();
339
340 // The database has now been created. Time to try to update it.
341 EXPECT_TRUE(v4_database_);
342 const StoreMap* db_stores = v4_database_->store_map_.get();
343 EXPECT_EQ(expected_store_paths_.size(), db_stores->size());
344 StoreStateMap expected_store_state_map;
345 base::hash_map<UpdateListIdentifier, V4Store*> old_stores_map;
346 for (const auto& store_iter : *db_stores) {
347 V4Store* store = store_iter.second.get();
348 expected_store_state_map[store_iter.first] = store->state();
349 old_stores_map[store_iter.first] = store;
350 }
351
352 std::vector<ListUpdateResponse> update_response;
353 v4_database_->ApplyUpdate(update_response);
354
355 task_runner_->RunPendingTasks();
356 base::RunLoop().RunUntilIdle();
357
358 const StoreMap* new_store_map = v4_database_->store_map_.get();
359 const StoreStateMap* new_store_state_map = v4_database_->store_state_map();
360 EXPECT_EQ(expected_store_state_map.size(), new_store_map->size());
361 EXPECT_EQ(expected_store_state_map.size(), new_store_state_map->size());
362 for (const auto& expected_iter : expected_store_state_map) {
363 const UpdateListIdentifier& identifier = expected_iter.first;
364 const std::string& state = expected_iter.second;
365 EXPECT_EQ(1ul, new_store_map->count(identifier));
366 EXPECT_EQ(1ul, new_store_state_map->count(identifier));
367
368 // Verify the expected state in the store map and the state map.
369 EXPECT_EQ(state, new_store_map->at(identifier)->state());
370 EXPECT_EQ(state, new_store_state_map->at(identifier));
371
372 // Verify that NO new store was created.
373 EXPECT_EQ(old_stores_map[identifier], new_store_map->at(identifier).get());
374 }
375 }
376
186 } // namespace safe_browsing 377 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698