| OLD | NEW |
| 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 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 } | 119 } |
| 120 | 120 |
| 121 EXPECT_EQ(expected_resets_successfully_, v4_database->ResetDatabase()); | 121 EXPECT_EQ(expected_resets_successfully_, v4_database->ResetDatabase()); |
| 122 | 122 |
| 123 EXPECT_FALSE(created_and_called_back_); | 123 EXPECT_FALSE(created_and_called_back_); |
| 124 created_and_called_back_ = true; | 124 created_and_called_back_ = true; |
| 125 | 125 |
| 126 v4_database_ = std::move(v4_database); | 126 v4_database_ = std::move(v4_database); |
| 127 } | 127 } |
| 128 | 128 |
| 129 void PopulateFakeUpdateResponse(StoreStateMap store_state_map, | 129 std::unique_ptr<ParsedServerResponse> CreateFakeServerResponse( |
| 130 std::vector<ListUpdateResponse>* responses) { | 130 StoreStateMap store_state_map) { |
| 131 std::unique_ptr<ParsedServerResponse> parsed_server_response( |
| 132 new ParsedServerResponse); |
| 131 for (const auto& store_state_iter : store_state_map) { | 133 for (const auto& store_state_iter : store_state_map) { |
| 132 UpdateListIdentifier identifier = store_state_iter.first; | 134 UpdateListIdentifier identifier = store_state_iter.first; |
| 133 ListUpdateResponse list_update_response; | 135 ListUpdateResponse* list_update_response = new ListUpdateResponse; |
| 134 list_update_response.set_platform_type(identifier.platform_type); | 136 list_update_response->set_platform_type(identifier.platform_type); |
| 135 list_update_response.set_threat_entry_type(identifier.threat_entry_type); | 137 list_update_response->set_threat_entry_type(identifier.threat_entry_type); |
| 136 list_update_response.set_threat_type(identifier.threat_type); | 138 list_update_response->set_threat_type(identifier.threat_type); |
| 137 list_update_response.set_new_client_state(store_state_iter.second); | 139 list_update_response->set_new_client_state(store_state_iter.second); |
| 138 responses->push_back(list_update_response); | 140 parsed_server_response->push_back(base::WrapUnique(list_update_response)); |
| 139 } | 141 } |
| 142 return parsed_server_response; |
| 140 } | 143 } |
| 141 | 144 |
| 142 void VerifyExpectedStoresState(bool expect_new_stores) { | 145 void VerifyExpectedStoresState(bool expect_new_stores) { |
| 143 const StoreMap* new_store_map = v4_database_->store_map_.get(); | 146 const StoreMap* new_store_map = v4_database_->store_map_.get(); |
| 144 std::unique_ptr<StoreStateMap> new_store_state_map = | 147 std::unique_ptr<StoreStateMap> new_store_state_map = |
| 145 v4_database_->GetStoreStateMap(); | 148 v4_database_->GetStoreStateMap(); |
| 146 EXPECT_EQ(expected_store_state_map_.size(), new_store_map->size()); | 149 EXPECT_EQ(expected_store_state_map_.size(), new_store_map->size()); |
| 147 EXPECT_EQ(expected_store_state_map_.size(), new_store_state_map->size()); | 150 EXPECT_EQ(expected_store_state_map_.size(), new_store_state_map->size()); |
| 148 for (const auto& expected_iter : expected_store_state_map_) { | 151 for (const auto& expected_iter : expected_store_state_map_) { |
| 149 const UpdateListIdentifier& identifier = expected_iter.first; | 152 const UpdateListIdentifier& identifier = expected_iter.first; |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 // The database has now been created. Time to try to update it. | 230 // The database has now been created. Time to try to update it. |
| 228 EXPECT_TRUE(v4_database_); | 231 EXPECT_TRUE(v4_database_); |
| 229 const StoreMap* db_stores = v4_database_->store_map_.get(); | 232 const StoreMap* db_stores = v4_database_->store_map_.get(); |
| 230 EXPECT_EQ(expected_store_paths_.size(), db_stores->size()); | 233 EXPECT_EQ(expected_store_paths_.size(), db_stores->size()); |
| 231 for (const auto& store_iter : *db_stores) { | 234 for (const auto& store_iter : *db_stores) { |
| 232 V4Store* store = store_iter.second.get(); | 235 V4Store* store = store_iter.second.get(); |
| 233 expected_store_state_map_[store_iter.first] = store->state() + "_fake"; | 236 expected_store_state_map_[store_iter.first] = store->state() + "_fake"; |
| 234 old_stores_map_[store_iter.first] = store; | 237 old_stores_map_[store_iter.first] = store; |
| 235 } | 238 } |
| 236 | 239 |
| 237 std::vector<ListUpdateResponse> update_response; | 240 v4_database_->ApplyUpdate(CreateFakeServerResponse(expected_store_state_map_), |
| 238 PopulateFakeUpdateResponse(expected_store_state_map_, &update_response); | 241 callback_db_updated_); |
| 239 v4_database_->ApplyUpdate(update_response, callback_db_updated_); | |
| 240 | 242 |
| 241 task_runner_->RunPendingTasks(); | 243 task_runner_->RunPendingTasks(); |
| 242 base::RunLoop().RunUntilIdle(); | 244 base::RunLoop().RunUntilIdle(); |
| 243 | 245 |
| 244 VerifyExpectedStoresState(true); | 246 VerifyExpectedStoresState(true); |
| 245 } | 247 } |
| 246 | 248 |
| 247 // Test to ensure no state updates leads to no store updates. | 249 // Test to ensure no state updates leads to no store updates. |
| 248 TEST_F(V4DatabaseTest, TestApplyUpdateWithNoNewState) { | 250 TEST_F(V4DatabaseTest, TestApplyUpdateWithNoNewState) { |
| 249 expected_resets_successfully_ = true; | 251 expected_resets_successfully_ = true; |
| 250 RegisterFactory(!expected_resets_successfully_); | 252 RegisterFactory(!expected_resets_successfully_); |
| 251 | 253 |
| 252 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, | 254 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, |
| 253 callback_db_ready_); | 255 callback_db_ready_); |
| 254 created_but_not_called_back_ = true; | 256 created_but_not_called_back_ = true; |
| 255 task_runner_->RunPendingTasks(); | 257 task_runner_->RunPendingTasks(); |
| 256 base::RunLoop().RunUntilIdle(); | 258 base::RunLoop().RunUntilIdle(); |
| 257 | 259 |
| 258 // The database has now been created. Time to try to update it. | 260 // The database has now been created. Time to try to update it. |
| 259 EXPECT_TRUE(v4_database_); | 261 EXPECT_TRUE(v4_database_); |
| 260 const StoreMap* db_stores = v4_database_->store_map_.get(); | 262 const StoreMap* db_stores = v4_database_->store_map_.get(); |
| 261 EXPECT_EQ(expected_store_paths_.size(), db_stores->size()); | 263 EXPECT_EQ(expected_store_paths_.size(), db_stores->size()); |
| 262 for (const auto& store_iter : *db_stores) { | 264 for (const auto& store_iter : *db_stores) { |
| 263 V4Store* store = store_iter.second.get(); | 265 V4Store* store = store_iter.second.get(); |
| 264 expected_store_state_map_[store_iter.first] = store->state(); | 266 expected_store_state_map_[store_iter.first] = store->state(); |
| 265 old_stores_map_[store_iter.first] = store; | 267 old_stores_map_[store_iter.first] = store; |
| 266 } | 268 } |
| 267 | 269 |
| 268 std::vector<ListUpdateResponse> update_response; | 270 v4_database_->ApplyUpdate(CreateFakeServerResponse(expected_store_state_map_), |
| 269 PopulateFakeUpdateResponse(expected_store_state_map_, &update_response); | 271 callback_db_updated_); |
| 270 v4_database_->ApplyUpdate(update_response, callback_db_updated_); | |
| 271 | 272 |
| 272 task_runner_->RunPendingTasks(); | 273 task_runner_->RunPendingTasks(); |
| 273 base::RunLoop().RunUntilIdle(); | 274 base::RunLoop().RunUntilIdle(); |
| 274 | 275 |
| 275 VerifyExpectedStoresState(false); | 276 VerifyExpectedStoresState(false); |
| 276 } | 277 } |
| 277 | 278 |
| 278 // Test to ensure no updates leads to no store updates. | 279 // Test to ensure no updates leads to no store updates. |
| 279 TEST_F(V4DatabaseTest, TestApplyUpdateWithEmptyUpdate) { | 280 TEST_F(V4DatabaseTest, TestApplyUpdateWithEmptyUpdate) { |
| 280 expected_resets_successfully_ = true; | 281 expected_resets_successfully_ = true; |
| 281 RegisterFactory(!expected_resets_successfully_); | 282 RegisterFactory(!expected_resets_successfully_); |
| 282 | 283 |
| 283 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, | 284 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, |
| 284 callback_db_ready_); | 285 callback_db_ready_); |
| 285 created_but_not_called_back_ = true; | 286 created_but_not_called_back_ = true; |
| 286 task_runner_->RunPendingTasks(); | 287 task_runner_->RunPendingTasks(); |
| 287 base::RunLoop().RunUntilIdle(); | 288 base::RunLoop().RunUntilIdle(); |
| 288 | 289 |
| 289 // The database has now been created. Time to try to update it. | 290 // The database has now been created. Time to try to update it. |
| 290 EXPECT_TRUE(v4_database_); | 291 EXPECT_TRUE(v4_database_); |
| 291 const StoreMap* db_stores = v4_database_->store_map_.get(); | 292 const StoreMap* db_stores = v4_database_->store_map_.get(); |
| 292 EXPECT_EQ(expected_store_paths_.size(), db_stores->size()); | 293 EXPECT_EQ(expected_store_paths_.size(), db_stores->size()); |
| 293 for (const auto& store_iter : *db_stores) { | 294 for (const auto& store_iter : *db_stores) { |
| 294 V4Store* store = store_iter.second.get(); | 295 V4Store* store = store_iter.second.get(); |
| 295 expected_store_state_map_[store_iter.first] = store->state(); | 296 expected_store_state_map_[store_iter.first] = store->state(); |
| 296 old_stores_map_[store_iter.first] = store; | 297 old_stores_map_[store_iter.first] = store; |
| 297 } | 298 } |
| 298 | 299 |
| 299 std::vector<ListUpdateResponse> update_response; | 300 std::unique_ptr<ParsedServerResponse> parsed_server_response( |
| 300 v4_database_->ApplyUpdate(update_response, callback_db_updated_); | 301 new ParsedServerResponse); |
| 302 v4_database_->ApplyUpdate(std::move(parsed_server_response), |
| 303 callback_db_updated_); |
| 301 | 304 |
| 302 task_runner_->RunPendingTasks(); | 305 task_runner_->RunPendingTasks(); |
| 303 base::RunLoop().RunUntilIdle(); | 306 base::RunLoop().RunUntilIdle(); |
| 304 | 307 |
| 305 VerifyExpectedStoresState(false); | 308 VerifyExpectedStoresState(false); |
| 306 } | 309 } |
| 307 | 310 |
| 308 } // namespace safe_browsing | 311 } // namespace safe_browsing |
| OLD | NEW |