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

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

Issue 2103693002: SafeBrowsing PVer4: Send mutable response to the database and the stores (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@02_ReadFromDisk
Patch Set: Created 4 years, 5 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 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 } 121 }
122 122
123 EXPECT_EQ(expected_resets_successfully_, v4_database->ResetDatabase()); 123 EXPECT_EQ(expected_resets_successfully_, v4_database->ResetDatabase());
124 124
125 EXPECT_FALSE(created_and_called_back_); 125 EXPECT_FALSE(created_and_called_back_);
126 created_and_called_back_ = true; 126 created_and_called_back_ = true;
127 127
128 v4_database_ = std::move(v4_database); 128 v4_database_ = std::move(v4_database);
129 } 129 }
130 130
131 void PopulateFakeUpdateResponse(StoreStateMap store_state_map, 131 std::unique_ptr<ParsedServerResponse> CreateFakeServerResponse(
132 std::vector<ListUpdateResponse>* responses) { 132 StoreStateMap store_state_map) {
133 std::unique_ptr<ParsedServerResponse> parsed_server_response(
134 new ParsedServerResponse);
Scott Hess - ex-Googler 2016/06/28 03:58:43 Is this your formatting or auto-formatting? Once
Nathan Parker 2016/06/28 19:00:38 I'd say if the auto-formatting is non-optimal, we
vakh (use Gerrit instead) 2016/06/28 21:34:14 I wouldn't call it a bug. It's just something that
vakh (use Gerrit instead) 2016/06/28 21:34:14 $ git cl format
Scott Hess - ex-Googler 2016/06/28 22:22:25 Acknowledged. All hail our formatting overlords.
133 for (const auto& store_state_iter : store_state_map) { 135 for (const auto& store_state_iter : store_state_map) {
134 UpdateListIdentifier identifier = store_state_iter.first; 136 UpdateListIdentifier identifier = store_state_iter.first;
135 ListUpdateResponse list_update_response; 137 ListUpdateResponse* list_update_response = new ListUpdateResponse;
136 list_update_response.set_platform_type(identifier.platform_type); 138 list_update_response->set_platform_type(identifier.platform_type);
137 list_update_response.set_threat_entry_type(identifier.threat_entry_type); 139 list_update_response->set_threat_entry_type(identifier.threat_entry_type);
138 list_update_response.set_threat_type(identifier.threat_type); 140 list_update_response->set_threat_type(identifier.threat_type);
139 list_update_response.set_new_client_state(store_state_iter.second); 141 list_update_response->set_new_client_state(store_state_iter.second);
140 responses->push_back(list_update_response); 142 parsed_server_response->push_back(base::WrapUnique(list_update_response));
141 } 143 }
144 return parsed_server_response;
142 } 145 }
143 146
144 void VerifyExpectedStoresState(bool expect_new_stores) { 147 void VerifyExpectedStoresState(bool expect_new_stores) {
145 const StoreMap* new_store_map = v4_database_->store_map_.get(); 148 const StoreMap* new_store_map = v4_database_->store_map_.get();
146 std::unique_ptr<StoreStateMap> new_store_state_map = 149 std::unique_ptr<StoreStateMap> new_store_state_map =
147 v4_database_->GetStoreStateMap(); 150 v4_database_->GetStoreStateMap();
148 EXPECT_EQ(expected_store_state_map_.size(), new_store_map->size()); 151 EXPECT_EQ(expected_store_state_map_.size(), new_store_map->size());
149 EXPECT_EQ(expected_store_state_map_.size(), new_store_state_map->size()); 152 EXPECT_EQ(expected_store_state_map_.size(), new_store_state_map->size());
150 for (const auto& expected_iter : expected_store_state_map_) { 153 for (const auto& expected_iter : expected_store_state_map_) {
151 const UpdateListIdentifier& identifier = expected_iter.first; 154 const UpdateListIdentifier& identifier = expected_iter.first;
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 // The database has now been created. Time to try to update it. 232 // The database has now been created. Time to try to update it.
230 EXPECT_TRUE(v4_database_); 233 EXPECT_TRUE(v4_database_);
231 const StoreMap* db_stores = v4_database_->store_map_.get(); 234 const StoreMap* db_stores = v4_database_->store_map_.get();
232 EXPECT_EQ(expected_store_paths_.size(), db_stores->size()); 235 EXPECT_EQ(expected_store_paths_.size(), db_stores->size());
233 for (const auto& store_iter : *db_stores) { 236 for (const auto& store_iter : *db_stores) {
234 V4Store* store = store_iter.second.get(); 237 V4Store* store = store_iter.second.get();
235 expected_store_state_map_[store_iter.first] = store->state() + "_fake"; 238 expected_store_state_map_[store_iter.first] = store->state() + "_fake";
236 old_stores_map_[store_iter.first] = store; 239 old_stores_map_[store_iter.first] = store;
237 } 240 }
238 241
239 std::vector<ListUpdateResponse> update_response; 242 std::unique_ptr<ParsedServerResponse> parsed_server_response =
240 PopulateFakeUpdateResponse(expected_store_state_map_, &update_response); 243 CreateFakeServerResponse(expected_store_state_map_);
241 v4_database_->ApplyUpdate(update_response, callback_db_updated_); 244 v4_database_->ApplyUpdate(std::move(parsed_server_response),
245 callback_db_updated_);
Scott Hess - ex-Googler 2016/06/28 03:58:43 Would the following work as: v4_database_->Apply
vakh (use Gerrit instead) 2016/06/28 21:34:14 Done.
242 246
243 task_runner_->RunPendingTasks(); 247 task_runner_->RunPendingTasks();
244 base::RunLoop().RunUntilIdle(); 248 base::RunLoop().RunUntilIdle();
245 249
246 VerifyExpectedStoresState(true); 250 VerifyExpectedStoresState(true);
247 } 251 }
248 252
249 // Test to ensure no state updates leads to no store updates. 253 // Test to ensure no state updates leads to no store updates.
250 TEST_F(SafeBrowsingV4DatabaseTest, TestApplyUpdateWithNoNewState) { 254 TEST_F(SafeBrowsingV4DatabaseTest, TestApplyUpdateWithNoNewState) {
251 expected_resets_successfully_ = true; 255 expected_resets_successfully_ = true;
252 RegisterFactory(!expected_resets_successfully_); 256 RegisterFactory(!expected_resets_successfully_);
253 257
254 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, 258 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_,
255 callback_db_ready_); 259 callback_db_ready_);
256 created_but_not_called_back_ = true; 260 created_but_not_called_back_ = true;
257 task_runner_->RunPendingTasks(); 261 task_runner_->RunPendingTasks();
258 base::RunLoop().RunUntilIdle(); 262 base::RunLoop().RunUntilIdle();
259 263
260 // The database has now been created. Time to try to update it. 264 // The database has now been created. Time to try to update it.
261 EXPECT_TRUE(v4_database_); 265 EXPECT_TRUE(v4_database_);
262 const StoreMap* db_stores = v4_database_->store_map_.get(); 266 const StoreMap* db_stores = v4_database_->store_map_.get();
263 EXPECT_EQ(expected_store_paths_.size(), db_stores->size()); 267 EXPECT_EQ(expected_store_paths_.size(), db_stores->size());
264 for (const auto& store_iter : *db_stores) { 268 for (const auto& store_iter : *db_stores) {
265 V4Store* store = store_iter.second.get(); 269 V4Store* store = store_iter.second.get();
266 expected_store_state_map_[store_iter.first] = store->state(); 270 expected_store_state_map_[store_iter.first] = store->state();
267 old_stores_map_[store_iter.first] = store; 271 old_stores_map_[store_iter.first] = store;
268 } 272 }
269 273
270 std::vector<ListUpdateResponse> update_response; 274 std::unique_ptr<ParsedServerResponse> parsed_server_response =
271 PopulateFakeUpdateResponse(expected_store_state_map_, &update_response); 275 CreateFakeServerResponse(expected_store_state_map_);
272 v4_database_->ApplyUpdate(update_response, callback_db_updated_); 276 v4_database_->ApplyUpdate(std::move(parsed_server_response),
277 callback_db_updated_);
273 278
274 task_runner_->RunPendingTasks(); 279 task_runner_->RunPendingTasks();
275 base::RunLoop().RunUntilIdle(); 280 base::RunLoop().RunUntilIdle();
276 281
277 VerifyExpectedStoresState(false); 282 VerifyExpectedStoresState(false);
278 } 283 }
279 284
280 // Test to ensure no updates leads to no store updates. 285 // Test to ensure no updates leads to no store updates.
281 TEST_F(SafeBrowsingV4DatabaseTest, TestApplyUpdateWithEmptyUpdate) { 286 TEST_F(SafeBrowsingV4DatabaseTest, TestApplyUpdateWithEmptyUpdate) {
282 expected_resets_successfully_ = true; 287 expected_resets_successfully_ = true;
283 RegisterFactory(!expected_resets_successfully_); 288 RegisterFactory(!expected_resets_successfully_);
284 289
285 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, 290 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_,
286 callback_db_ready_); 291 callback_db_ready_);
287 created_but_not_called_back_ = true; 292 created_but_not_called_back_ = true;
288 task_runner_->RunPendingTasks(); 293 task_runner_->RunPendingTasks();
289 base::RunLoop().RunUntilIdle(); 294 base::RunLoop().RunUntilIdle();
290 295
291 // The database has now been created. Time to try to update it. 296 // The database has now been created. Time to try to update it.
292 EXPECT_TRUE(v4_database_); 297 EXPECT_TRUE(v4_database_);
293 const StoreMap* db_stores = v4_database_->store_map_.get(); 298 const StoreMap* db_stores = v4_database_->store_map_.get();
294 EXPECT_EQ(expected_store_paths_.size(), db_stores->size()); 299 EXPECT_EQ(expected_store_paths_.size(), db_stores->size());
295 for (const auto& store_iter : *db_stores) { 300 for (const auto& store_iter : *db_stores) {
296 V4Store* store = store_iter.second.get(); 301 V4Store* store = store_iter.second.get();
297 expected_store_state_map_[store_iter.first] = store->state(); 302 expected_store_state_map_[store_iter.first] = store->state();
298 old_stores_map_[store_iter.first] = store; 303 old_stores_map_[store_iter.first] = store;
299 } 304 }
300 305
301 std::vector<ListUpdateResponse> update_response; 306 std::unique_ptr<ParsedServerResponse> parsed_server_response(
302 v4_database_->ApplyUpdate(update_response, callback_db_updated_); 307 new ParsedServerResponse);
308 v4_database_->ApplyUpdate(std::move(parsed_server_response),
309 callback_db_updated_);
303 310
304 task_runner_->RunPendingTasks(); 311 task_runner_->RunPendingTasks();
305 base::RunLoop().RunUntilIdle(); 312 base::RunLoop().RunUntilIdle();
306 313
307 VerifyExpectedStoresState(false); 314 VerifyExpectedStoresState(false);
308 } 315 }
309 316
310 } // namespace safe_browsing 317 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698