Chromium Code Reviews| 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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 58 void SetUp() override { | 58 void SetUp() override { |
| 59 PlatformTest::SetUp(); | 59 PlatformTest::SetUp(); |
| 60 | 60 |
| 61 // Setup a database in a temporary directory. | 61 // Setup a database in a temporary directory. |
| 62 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 62 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 63 database_dirname_ = | 63 database_dirname_ = |
| 64 temp_dir_.path().AppendASCII("SafeBrowsingV4DatabaseTest"); | 64 temp_dir_.path().AppendASCII("SafeBrowsingV4DatabaseTest"); |
| 65 | 65 |
| 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 | |
| 69 callback_db_updated_ = base::Bind( | |
| 70 &SafeBrowsingV4DatabaseTest::DatabaseUpdated, base::Unretained(this)); | |
| 71 | |
| 72 callback_db_ready_ = | |
| 73 base::Bind(&SafeBrowsingV4DatabaseTest:: | |
| 74 NewDatabaseReadyWithExpectedStorePathsAndIds, | |
| 75 base::Unretained(this)); | |
| 76 | |
| 77 SetupInfoMapAndExpectedState(); | |
| 78 } | |
| 79 | |
| 80 void RegisterFactory(bool fails_first_reset) { | |
| 81 factory_.reset(new FakeV4StoreFactory(fails_first_reset)); | |
| 82 V4Database::RegisterStoreFactoryForTest(factory_.get()); | |
|
Scott Hess - ex-Googler
2016/06/24 23:01:19
I think your point about not wanting to put the Re
vakh (use Gerrit instead)
2016/06/27 19:38:27
Done.
| |
| 68 } | 83 } |
| 69 | 84 |
| 70 void SetupInfoMapAndExpectedState() { | 85 void SetupInfoMapAndExpectedState() { |
| 71 UpdateListIdentifier update_list_identifier; | 86 UpdateListIdentifier win_malware_id(WINDOWS_PLATFORM, URL, MALWARE_THREAT); |
| 72 | 87 store_file_name_map_[win_malware_id] = "win_url_malware"; |
| 73 update_list_identifier.platform_type = WINDOWS_PLATFORM; | 88 expected_identifiers_.push_back(win_malware_id); |
| 74 update_list_identifier.threat_entry_type = URL; | |
| 75 update_list_identifier.threat_type = MALWARE_THREAT; | |
| 76 store_file_name_map_[update_list_identifier] = "win_url_malware"; | |
| 77 expected_identifiers_.push_back(update_list_identifier); | |
| 78 expected_store_paths_.push_back( | 89 expected_store_paths_.push_back( |
| 79 database_dirname_.AppendASCII("win_url_malware.fake")); | 90 database_dirname_.AppendASCII("win_url_malware.fake")); |
| 80 | 91 |
| 81 update_list_identifier.platform_type = LINUX_PLATFORM; | 92 UpdateListIdentifier linux_malware_id(LINUX_PLATFORM, URL, MALWARE_THREAT); |
| 82 update_list_identifier.threat_entry_type = URL; | 93 store_file_name_map_[linux_malware_id] = "linux_url_malware"; |
| 83 update_list_identifier.threat_type = MALWARE_THREAT; | 94 expected_identifiers_.push_back(linux_malware_id); |
| 84 store_file_name_map_[update_list_identifier] = "linux_url_malware"; | |
| 85 expected_identifiers_.push_back(update_list_identifier); | |
| 86 expected_store_paths_.push_back( | 95 expected_store_paths_.push_back( |
| 87 database_dirname_.AppendASCII("linux_url_malware.fake")); | 96 database_dirname_.AppendASCII("linux_url_malware.fake")); |
| 88 } | 97 } |
| 89 | 98 |
| 99 void DatabaseUpdated() {} | |
| 90 void NewDatabaseReadyWithExpectedStorePathsAndIds( | 100 void NewDatabaseReadyWithExpectedStorePathsAndIds( |
| 91 std::vector<base::FilePath> expected_store_paths, | |
| 92 std::vector<UpdateListIdentifier> expected_identifiers, | |
| 93 bool expected_resets_successfully, | |
| 94 std::unique_ptr<V4Database> v4_database) { | 101 std::unique_ptr<V4Database> v4_database) { |
| 95 ASSERT_TRUE(v4_database); | 102 ASSERT_TRUE(v4_database); |
| 96 ASSERT_TRUE(v4_database->store_map_); | 103 ASSERT_TRUE(v4_database->store_map_); |
| 97 | 104 |
| 98 // The following check ensures that the callback was called asynchronously. | 105 // The following check ensures that the callback was called asynchronously. |
| 99 EXPECT_TRUE(created_but_not_called_back_); | 106 EXPECT_TRUE(created_but_not_called_back_); |
| 100 | 107 |
| 101 ASSERT_EQ(expected_store_paths.size(), v4_database->store_map_->size()); | 108 ASSERT_EQ(expected_store_paths_.size(), v4_database->store_map_->size()); |
| 102 ASSERT_EQ(expected_identifiers.size(), v4_database->store_map_->size()); | 109 ASSERT_EQ(expected_identifiers_.size(), v4_database->store_map_->size()); |
| 103 for (size_t i = 0; i < expected_identifiers.size(); i++) { | 110 for (size_t i = 0; i < expected_identifiers_.size(); i++) { |
| 104 const auto& expected_identifier = expected_identifiers[i]; | 111 const auto& expected_identifier = expected_identifiers_[i]; |
| 105 const auto& store = (*v4_database->store_map_)[expected_identifier]; | 112 const auto& store = (*v4_database->store_map_)[expected_identifier]; |
| 106 ASSERT_TRUE(store); | 113 ASSERT_TRUE(store); |
| 107 const auto& expected_store_path = expected_store_paths[i]; | 114 const auto& expected_store_path = expected_store_paths_[i]; |
| 108 EXPECT_EQ(expected_store_path, store->store_path()); | 115 EXPECT_EQ(expected_store_path, store->store_path()); |
| 109 } | 116 } |
| 110 | 117 |
| 111 EXPECT_EQ(expected_resets_successfully, v4_database->ResetDatabase()); | 118 EXPECT_EQ(expected_resets_successfully_, v4_database->ResetDatabase()); |
| 112 | 119 |
| 113 EXPECT_FALSE(created_and_called_back_); | 120 EXPECT_FALSE(created_and_called_back_); |
| 114 created_and_called_back_ = true; | 121 created_and_called_back_ = true; |
| 122 | |
| 123 v4_database_ = std::move(v4_database); | |
| 124 } | |
| 125 | |
| 126 void PopulateFakeUpdateResponse(StoreStateMap store_state_map, | |
| 127 std::vector<ListUpdateResponse>* responses) { | |
| 128 for (const auto& store_state_iter : store_state_map) { | |
| 129 UpdateListIdentifier identifier = store_state_iter.first; | |
| 130 ListUpdateResponse list_update_response; | |
| 131 list_update_response.set_platform_type(identifier.platform_type); | |
| 132 list_update_response.set_threat_entry_type(identifier.threat_entry_type); | |
| 133 list_update_response.set_threat_type(identifier.threat_type); | |
| 134 list_update_response.set_new_client_state(store_state_iter.second); | |
| 135 responses->push_back(list_update_response); | |
| 136 } | |
| 137 } | |
| 138 | |
| 139 void VerifyExpectedStoresState(bool expect_new_stores) { | |
| 140 const StoreMap* new_store_map = v4_database_->store_map_.get(); | |
| 141 std::unique_ptr<StoreStateMap> new_store_state_map = | |
| 142 v4_database_->GetStoreStateMap(); | |
| 143 EXPECT_EQ(expected_store_state_map_.size(), new_store_map->size()); | |
| 144 EXPECT_EQ(expected_store_state_map_.size(), new_store_state_map->size()); | |
| 145 for (const auto& expected_iter : expected_store_state_map_) { | |
| 146 const UpdateListIdentifier& identifier = expected_iter.first; | |
| 147 const std::string& state = expected_iter.second; | |
| 148 ASSERT_EQ(1u, new_store_map->count(identifier)); | |
| 149 ASSERT_EQ(1u, new_store_state_map->count(identifier)); | |
| 150 | |
| 151 // Verify the expected state in the store map and the state map. | |
| 152 EXPECT_EQ(state, new_store_map->at(identifier)->state()); | |
| 153 EXPECT_EQ(state, new_store_state_map->at(identifier)); | |
| 154 | |
| 155 if (expect_new_stores) { | |
| 156 // Verify that a new store was created. | |
| 157 EXPECT_NE(old_stores_map_.at(identifier), | |
| 158 new_store_map->at(identifier).get()); | |
| 159 } else { | |
| 160 // Verify that NO new store was created. | |
| 161 EXPECT_EQ(old_stores_map_.at(identifier), | |
| 162 new_store_map->at(identifier).get()); | |
| 163 } | |
| 164 } | |
| 115 } | 165 } |
| 116 | 166 |
| 117 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 167 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 118 std::unique_ptr<V4Database> v4_database_; | 168 std::unique_ptr<V4Database> v4_database_; |
| 119 base::FilePath database_dirname_; | 169 base::FilePath database_dirname_; |
| 120 base::ScopedTempDir temp_dir_; | 170 base::ScopedTempDir temp_dir_; |
| 121 content::TestBrowserThreadBundle thread_bundle_; | 171 content::TestBrowserThreadBundle thread_bundle_; |
| 122 bool created_but_not_called_back_; | 172 bool created_but_not_called_back_; |
| 123 bool created_and_called_back_; | 173 bool created_and_called_back_; |
| 124 StoreFileNameMap store_file_name_map_; | 174 StoreFileNameMap store_file_name_map_; |
| 125 std::vector<UpdateListIdentifier> expected_identifiers_; | 175 std::vector<UpdateListIdentifier> expected_identifiers_; |
| 126 std::vector<base::FilePath> expected_store_paths_; | 176 std::vector<base::FilePath> expected_store_paths_; |
| 177 bool expected_resets_successfully_; | |
| 178 std::unique_ptr<FakeV4StoreFactory> factory_; | |
| 179 DatabaseUpdatedCallback callback_db_updated_; | |
| 180 NewDatabaseReadyCallback callback_db_ready_; | |
| 181 StoreStateMap expected_store_state_map_; | |
| 182 base::hash_map<UpdateListIdentifier, V4Store*> old_stores_map_; | |
| 127 }; | 183 }; |
| 128 | 184 |
| 129 // Test to set up the database with no stores. | 185 // Test to set up the database with fake stores. |
| 130 TEST_F(SafeBrowsingV4DatabaseTest, TestSetupDatabaseWithNoStores) { | 186 TEST_F(SafeBrowsingV4DatabaseTest, TestSetupDatabaseWithFakeStores) { |
| 131 NewDatabaseReadyCallback callback_db_ready = base::Bind( | 187 expected_resets_successfully_ = true; |
| 132 &SafeBrowsingV4DatabaseTest::NewDatabaseReadyWithExpectedStorePathsAndIds, | 188 RegisterFactory(!expected_resets_successfully_); |
| 133 base::Unretained(this), expected_store_paths_, expected_identifiers_, | 189 |
| 134 true); | |
| 135 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, | 190 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, |
| 136 callback_db_ready); | 191 callback_db_updated_, callback_db_ready_); |
| 137 created_but_not_called_back_ = true; | 192 created_but_not_called_back_ = true; |
| 138 task_runner_->RunPendingTasks(); | 193 task_runner_->RunPendingTasks(); |
| 139 | 194 |
| 140 base::RunLoop().RunUntilIdle(); | 195 base::RunLoop().RunUntilIdle(); |
| 141 EXPECT_EQ(true, created_and_called_back_); | 196 EXPECT_EQ(true, created_and_called_back_); |
| 142 } | 197 } |
| 143 | 198 |
| 144 // Test to set up the database with fake stores. | 199 // Test to set up the database with fake stores that fail to reset. |
| 145 TEST_F(SafeBrowsingV4DatabaseTest, TestSetupDatabaseWithFakeStores) { | 200 TEST_F(SafeBrowsingV4DatabaseTest, TestSetupDatabaseWithFakeStoresFailsReset) { |
| 146 SetupInfoMapAndExpectedState(); | 201 expected_resets_successfully_ = false; |
| 202 RegisterFactory(!expected_resets_successfully_); | |
| 147 | 203 |
| 148 NewDatabaseReadyCallback callback_db_ready = base::Bind( | |
| 149 &SafeBrowsingV4DatabaseTest::NewDatabaseReadyWithExpectedStorePathsAndIds, | |
| 150 base::Unretained(this), expected_store_paths_, expected_identifiers_, | |
| 151 true); | |
| 152 | |
| 153 FakeV4StoreFactory* factory = new FakeV4StoreFactory(false); | |
| 154 ANNOTATE_LEAKING_OBJECT_PTR(factory); | |
| 155 V4Database::RegisterStoreFactoryForTest(factory); | |
| 156 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, | 204 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, |
| 157 callback_db_ready); | 205 callback_db_updated_, callback_db_ready_); |
| 158 created_but_not_called_back_ = true; | 206 created_but_not_called_back_ = true; |
| 159 task_runner_->RunPendingTasks(); | 207 task_runner_->RunPendingTasks(); |
| 160 | 208 |
| 161 base::RunLoop().RunUntilIdle(); | 209 base::RunLoop().RunUntilIdle(); |
| 162 EXPECT_EQ(true, created_and_called_back_); | 210 EXPECT_EQ(true, created_and_called_back_); |
| 163 } | 211 } |
| 164 | 212 |
| 165 // Test to set up the database with fake stores that fail to reset. | 213 // Test to check database updates as expected. |
| 166 TEST_F(SafeBrowsingV4DatabaseTest, TestSetupDatabaseWithFakeStoresFailsReset) { | 214 TEST_F(SafeBrowsingV4DatabaseTest, TestApplyUpdateWithNewStates) { |
| 167 SetupInfoMapAndExpectedState(); | 215 expected_resets_successfully_ = true; |
| 216 RegisterFactory(!expected_resets_successfully_); | |
| 168 | 217 |
| 169 NewDatabaseReadyCallback callback_db_ready = base::Bind( | |
| 170 &SafeBrowsingV4DatabaseTest::NewDatabaseReadyWithExpectedStorePathsAndIds, | |
| 171 base::Unretained(this), expected_store_paths_, expected_identifiers_, | |
| 172 false); | |
| 173 | |
| 174 FakeV4StoreFactory* factory = new FakeV4StoreFactory(true); | |
| 175 ANNOTATE_LEAKING_OBJECT_PTR(factory); | |
| 176 V4Database::RegisterStoreFactoryForTest(factory); | |
| 177 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, | 218 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, |
| 178 callback_db_ready); | 219 callback_db_updated_, callback_db_ready_); |
| 179 created_but_not_called_back_ = true; | 220 created_but_not_called_back_ = true; |
| 180 task_runner_->RunPendingTasks(); | 221 task_runner_->RunPendingTasks(); |
| 222 base::RunLoop().RunUntilIdle(); | |
| 181 | 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 for (const auto& store_iter : *db_stores) { | |
| 229 V4Store* store = store_iter.second.get(); | |
| 230 expected_store_state_map_[store_iter.first] = store->state() + "_fake"; | |
| 231 old_stores_map_[store_iter.first] = store; | |
| 232 } | |
| 233 | |
| 234 std::vector<ListUpdateResponse> update_response; | |
| 235 PopulateFakeUpdateResponse(expected_store_state_map_, &update_response); | |
| 236 v4_database_->ApplyUpdate(update_response); | |
| 237 | |
| 238 task_runner_->RunPendingTasks(); | |
| 182 base::RunLoop().RunUntilIdle(); | 239 base::RunLoop().RunUntilIdle(); |
| 183 EXPECT_EQ(true, created_and_called_back_); | 240 |
| 241 VerifyExpectedStoresState(true); | |
| 242 } | |
| 243 | |
| 244 // Test to ensure no state updates leads to no store updates. | |
| 245 TEST_F(SafeBrowsingV4DatabaseTest, TestApplyUpdateWithNoNewState) { | |
| 246 expected_resets_successfully_ = true; | |
| 247 RegisterFactory(!expected_resets_successfully_); | |
| 248 | |
| 249 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, | |
| 250 callback_db_updated_, callback_db_ready_); | |
| 251 created_but_not_called_back_ = true; | |
| 252 task_runner_->RunPendingTasks(); | |
| 253 base::RunLoop().RunUntilIdle(); | |
| 254 | |
| 255 // The database has now been created. Time to try to update it. | |
| 256 EXPECT_TRUE(v4_database_); | |
| 257 const StoreMap* db_stores = v4_database_->store_map_.get(); | |
| 258 EXPECT_EQ(expected_store_paths_.size(), db_stores->size()); | |
| 259 for (const auto& store_iter : *db_stores) { | |
| 260 V4Store* store = store_iter.second.get(); | |
| 261 expected_store_state_map_[store_iter.first] = store->state(); | |
| 262 old_stores_map_[store_iter.first] = store; | |
| 263 } | |
| 264 | |
| 265 std::vector<ListUpdateResponse> update_response; | |
| 266 PopulateFakeUpdateResponse(expected_store_state_map_, &update_response); | |
| 267 v4_database_->ApplyUpdate(update_response); | |
| 268 | |
| 269 task_runner_->RunPendingTasks(); | |
| 270 base::RunLoop().RunUntilIdle(); | |
| 271 | |
| 272 VerifyExpectedStoresState(false); | |
| 273 } | |
| 274 | |
| 275 // Test to ensure no updates leads to no store updates. | |
| 276 TEST_F(SafeBrowsingV4DatabaseTest, TestApplyUpdateWithEmptyUpdate) { | |
| 277 expected_resets_successfully_ = true; | |
| 278 RegisterFactory(!expected_resets_successfully_); | |
| 279 | |
| 280 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, | |
| 281 callback_db_updated_, callback_db_ready_); | |
| 282 created_but_not_called_back_ = true; | |
| 283 task_runner_->RunPendingTasks(); | |
| 284 base::RunLoop().RunUntilIdle(); | |
| 285 | |
| 286 // The database has now been created. Time to try to update it. | |
| 287 EXPECT_TRUE(v4_database_); | |
| 288 const StoreMap* db_stores = v4_database_->store_map_.get(); | |
| 289 EXPECT_EQ(expected_store_paths_.size(), db_stores->size()); | |
| 290 for (const auto& store_iter : *db_stores) { | |
| 291 V4Store* store = store_iter.second.get(); | |
| 292 expected_store_state_map_[store_iter.first] = store->state(); | |
| 293 old_stores_map_[store_iter.first] = store; | |
| 294 } | |
| 295 | |
| 296 std::vector<ListUpdateResponse> update_response; | |
| 297 v4_database_->ApplyUpdate(update_response); | |
| 298 | |
| 299 task_runner_->RunPendingTasks(); | |
| 300 base::RunLoop().RunUntilIdle(); | |
| 301 | |
| 302 VerifyExpectedStoresState(false); | |
| 184 } | 303 } |
| 185 | 304 |
| 186 } // namespace safe_browsing | 305 } // namespace safe_browsing |
| OLD | NEW |