Index: components/safe_browsing_db/v4_database_unittest.cc |
diff --git a/components/safe_browsing_db/v4_database_unittest.cc b/components/safe_browsing_db/v4_database_unittest.cc |
index 83ea5f41224802a4e7b4991ec6283ef5f2ab21d5..6f64f29cdead4d50bc06bb2c7684e28628aeaf28 100644 |
--- a/components/safe_browsing_db/v4_database_unittest.cc |
+++ b/components/safe_browsing_db/v4_database_unittest.cc |
@@ -65,6 +65,14 @@ class SafeBrowsingV4DatabaseTest : public PlatformTest { |
created_but_not_called_back_ = false; |
created_and_called_back_ = false; |
+ |
+ callback_db_updated_ = base::Bind( |
+ &SafeBrowsingV4DatabaseTest::DatabaseUpdated, base::Unretained(this)); |
+ } |
+ |
+ void RegisterFactory(bool fails_first_reset) { |
+ factory_.reset(new FakeV4StoreFactory(fails_first_reset)); |
+ V4Database::RegisterStoreFactoryForTest(factory_.get()); |
} |
void SetupInfoMapAndExpectedState() { |
@@ -73,7 +81,7 @@ class SafeBrowsingV4DatabaseTest : public PlatformTest { |
update_list_identifier.platform_type = WINDOWS_PLATFORM; |
update_list_identifier.threat_entry_type = URL; |
update_list_identifier.threat_type = MALWARE_THREAT; |
- list_info_map_[update_list_identifier] = "win_url_malware"; |
+ store_file_name_map_[update_list_identifier] = "win_url_malware"; |
expected_identifiers_.push_back(update_list_identifier); |
expected_store_paths_.push_back( |
database_dirname_.AppendASCII("win_url_malware.fake")); |
@@ -81,12 +89,13 @@ class SafeBrowsingV4DatabaseTest : public PlatformTest { |
update_list_identifier.platform_type = LINUX_PLATFORM; |
update_list_identifier.threat_entry_type = URL; |
update_list_identifier.threat_type = MALWARE_THREAT; |
- list_info_map_[update_list_identifier] = "linux_url_malware"; |
+ store_file_name_map_[update_list_identifier] = "linux_url_malware"; |
expected_identifiers_.push_back(update_list_identifier); |
expected_store_paths_.push_back( |
database_dirname_.AppendASCII("linux_url_malware.fake")); |
} |
+ void DatabaseUpdated() {} |
void NewDatabaseReadyWithExpectedStorePathsAndIds( |
std::vector<base::FilePath> expected_store_paths, |
std::vector<UpdateListIdentifier> expected_identifiers, |
@@ -112,6 +121,56 @@ class SafeBrowsingV4DatabaseTest : public PlatformTest { |
EXPECT_FALSE(created_and_called_back_); |
created_and_called_back_ = true; |
+ |
+ v4_database_ = std::move(v4_database); |
+ } |
+ |
+ void PopulateFakeUpdateResponse(StoreStateMap store_state_map, |
+ std::vector<ListUpdateResponse>* responses) { |
+ for (const auto& store_state_iter : store_state_map) { |
+ UpdateListIdentifier identifier = store_state_iter.first; |
+ ListUpdateResponse list_update_response; |
+ list_update_response.set_platform_type(identifier.platform_type); |
+ list_update_response.set_threat_entry_type(identifier.threat_entry_type); |
+ list_update_response.set_threat_type(identifier.threat_type); |
+ list_update_response.set_new_client_state(store_state_iter.second); |
+ responses->push_back(list_update_response); |
+ } |
+ } |
+ |
+ void SetupNewDatabaseReadyCallback(bool expected_resets_successfully) { |
+ callback_db_ready_ = |
+ base::Bind(&SafeBrowsingV4DatabaseTest:: |
+ NewDatabaseReadyWithExpectedStorePathsAndIds, |
+ base::Unretained(this), expected_store_paths_, |
+ expected_identifiers_, expected_resets_successfully); |
+ } |
+ |
+ void VerifyExpectedStoresState(bool expect_new_stores) { |
+ const StoreMap* new_store_map = v4_database_->store_map_.get(); |
+ const StoreStateMap* new_store_state_map = v4_database_->store_state_map(); |
+ EXPECT_EQ(expected_store_state_map_.size(), new_store_map->size()); |
+ EXPECT_EQ(expected_store_state_map_.size(), new_store_state_map->size()); |
+ for (const auto& expected_iter : expected_store_state_map_) { |
+ const UpdateListIdentifier& identifier = expected_iter.first; |
+ const std::string& state = expected_iter.second; |
+ EXPECT_EQ(1ul, new_store_map->count(identifier)); |
+ EXPECT_EQ(1ul, new_store_state_map->count(identifier)); |
+ |
+ // Verify the expected state in the store map and the state map. |
+ EXPECT_EQ(state, new_store_map->at(identifier)->state()); |
+ EXPECT_EQ(state, new_store_state_map->at(identifier)); |
+ |
+ if (expect_new_stores) { |
+ // Verify that a new store was created. |
+ EXPECT_NE(old_stores_map_[identifier], |
+ new_store_map->at(identifier).get()); |
+ } else { |
+ // Verify that NO new store was created. |
+ EXPECT_EQ(old_stores_map_[identifier], |
+ new_store_map->at(identifier).get()); |
+ } |
+ } |
} |
scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
@@ -121,19 +180,24 @@ class SafeBrowsingV4DatabaseTest : public PlatformTest { |
content::TestBrowserThreadBundle thread_bundle_; |
bool created_but_not_called_back_; |
bool created_and_called_back_; |
- ListInfoMap list_info_map_; |
+ StoreFileNameMap store_file_name_map_; |
std::vector<UpdateListIdentifier> expected_identifiers_; |
std::vector<base::FilePath> expected_store_paths_; |
+ std::unique_ptr<FakeV4StoreFactory> factory_; |
+ DatabaseUpdatedCallback callback_db_updated_; |
+ NewDatabaseReadyCallback callback_db_ready_; |
+ StoreStateMap expected_store_state_map_; |
+ base::hash_map<UpdateListIdentifier, V4Store*> old_stores_map_; |
}; |
-// Test to set up the database with no stores. |
-TEST_F(SafeBrowsingV4DatabaseTest, TestSetupDatabaseWithNoStores) { |
- NewDatabaseReadyCallback callback_db_ready = base::Bind( |
- &SafeBrowsingV4DatabaseTest::NewDatabaseReadyWithExpectedStorePathsAndIds, |
- base::Unretained(this), expected_store_paths_, expected_identifiers_, |
- true); |
- V4Database::Create(task_runner_, database_dirname_, list_info_map_, |
- callback_db_ready); |
+// Test to set up the database with fake stores. |
+TEST_F(SafeBrowsingV4DatabaseTest, TestSetupDatabaseWithFakeStores) { |
+ RegisterFactory(false); |
+ SetupInfoMapAndExpectedState(); |
+ SetupNewDatabaseReadyCallback(true); |
+ |
+ V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, |
+ callback_db_updated_, callback_db_ready_); |
created_but_not_called_back_ = true; |
task_runner_->RunPendingTasks(); |
@@ -141,20 +205,14 @@ TEST_F(SafeBrowsingV4DatabaseTest, TestSetupDatabaseWithNoStores) { |
EXPECT_EQ(true, created_and_called_back_); |
} |
-// Test to set up the database with fake stores. |
-TEST_F(SafeBrowsingV4DatabaseTest, TestSetupDatabaseWithFakeStores) { |
+// Test to set up the database with fake stores that fail to reset. |
+TEST_F(SafeBrowsingV4DatabaseTest, TestSetupDatabaseWithFakeStoresFailsReset) { |
+ RegisterFactory(true); |
SetupInfoMapAndExpectedState(); |
+ SetupNewDatabaseReadyCallback(false); |
- NewDatabaseReadyCallback callback_db_ready = base::Bind( |
- &SafeBrowsingV4DatabaseTest::NewDatabaseReadyWithExpectedStorePathsAndIds, |
- base::Unretained(this), expected_store_paths_, expected_identifiers_, |
- true); |
- |
- FakeV4StoreFactory* factory = new FakeV4StoreFactory(false); |
- ANNOTATE_LEAKING_OBJECT_PTR(factory); |
- V4Database::RegisterStoreFactoryForTest(factory); |
- V4Database::Create(task_runner_, database_dirname_, list_info_map_, |
- callback_db_ready); |
+ V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, |
+ callback_db_updated_, callback_db_ready_); |
created_but_not_called_back_ = true; |
task_runner_->RunPendingTasks(); |
@@ -162,25 +220,99 @@ TEST_F(SafeBrowsingV4DatabaseTest, TestSetupDatabaseWithFakeStores) { |
EXPECT_EQ(true, created_and_called_back_); |
} |
-// Test to set up the database with fake stores that fail to reset. |
-TEST_F(SafeBrowsingV4DatabaseTest, TestSetupDatabaseWithFakeStoresFailsReset) { |
+// Test to check database updates as expected. |
+TEST_F(SafeBrowsingV4DatabaseTest, TestApplyUpdateWithNewStates) { |
+ RegisterFactory(false); |
SetupInfoMapAndExpectedState(); |
+ SetupNewDatabaseReadyCallback(true); |
+ |
+ V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, |
+ callback_db_updated_, callback_db_ready_); |
+ created_but_not_called_back_ = true; |
+ task_runner_->RunPendingTasks(); |
+ base::RunLoop().RunUntilIdle(); |
+ |
+ // The database has now been created. Time to try to update it. |
+ EXPECT_TRUE(v4_database_); |
+ const StoreMap* db_stores = v4_database_->store_map_.get(); |
+ EXPECT_EQ(expected_store_paths_.size(), db_stores->size()); |
+ for (const auto& store_iter : *db_stores) { |
+ V4Store* store = store_iter.second.get(); |
+ expected_store_state_map_[store_iter.first] = store->state() + "_fake"; |
+ old_stores_map_[store_iter.first] = store; |
+ } |
- NewDatabaseReadyCallback callback_db_ready = base::Bind( |
- &SafeBrowsingV4DatabaseTest::NewDatabaseReadyWithExpectedStorePathsAndIds, |
- base::Unretained(this), expected_store_paths_, expected_identifiers_, |
- false); |
+ std::vector<ListUpdateResponse> update_response; |
+ PopulateFakeUpdateResponse(expected_store_state_map_, &update_response); |
+ v4_database_->ApplyUpdate(update_response); |
- FakeV4StoreFactory* factory = new FakeV4StoreFactory(true); |
- ANNOTATE_LEAKING_OBJECT_PTR(factory); |
- V4Database::RegisterStoreFactoryForTest(factory); |
- V4Database::Create(task_runner_, database_dirname_, list_info_map_, |
- callback_db_ready); |
+ task_runner_->RunPendingTasks(); |
+ base::RunLoop().RunUntilIdle(); |
+ |
+ VerifyExpectedStoresState(true); |
+} |
+ |
+// Test to ensure no state updates leads to no store updates. |
+TEST_F(SafeBrowsingV4DatabaseTest, TestApplyUpdateWithNoNewState) { |
+ RegisterFactory(false); |
+ SetupInfoMapAndExpectedState(); |
+ SetupNewDatabaseReadyCallback(true); |
+ |
+ V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, |
+ callback_db_updated_, callback_db_ready_); |
created_but_not_called_back_ = true; |
task_runner_->RunPendingTasks(); |
+ base::RunLoop().RunUntilIdle(); |
+ |
+ // The database has now been created. Time to try to update it. |
+ EXPECT_TRUE(v4_database_); |
+ const StoreMap* db_stores = v4_database_->store_map_.get(); |
+ EXPECT_EQ(expected_store_paths_.size(), db_stores->size()); |
+ for (const auto& store_iter : *db_stores) { |
+ V4Store* store = store_iter.second.get(); |
+ expected_store_state_map_[store_iter.first] = store->state(); |
+ old_stores_map_[store_iter.first] = store; |
+ } |
+ |
+ std::vector<ListUpdateResponse> update_response; |
+ PopulateFakeUpdateResponse(expected_store_state_map_, &update_response); |
+ v4_database_->ApplyUpdate(update_response); |
+ task_runner_->RunPendingTasks(); |
base::RunLoop().RunUntilIdle(); |
- EXPECT_EQ(true, created_and_called_back_); |
+ |
+ VerifyExpectedStoresState(false); |
+} |
+ |
+// Test to ensure no updates leads to no store updates. |
+TEST_F(SafeBrowsingV4DatabaseTest, TestApplyUpdateWithEmptyUpdate) { |
+ RegisterFactory(false); |
+ SetupInfoMapAndExpectedState(); |
+ SetupNewDatabaseReadyCallback(true); |
+ |
+ V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, |
+ callback_db_updated_, callback_db_ready_); |
+ created_but_not_called_back_ = true; |
+ task_runner_->RunPendingTasks(); |
+ base::RunLoop().RunUntilIdle(); |
+ |
+ // The database has now been created. Time to try to update it. |
+ EXPECT_TRUE(v4_database_); |
+ const StoreMap* db_stores = v4_database_->store_map_.get(); |
+ EXPECT_EQ(expected_store_paths_.size(), db_stores->size()); |
+ for (const auto& store_iter : *db_stores) { |
+ V4Store* store = store_iter.second.get(); |
+ expected_store_state_map_[store_iter.first] = store->state(); |
+ old_stores_map_[store_iter.first] = store; |
+ } |
+ |
+ std::vector<ListUpdateResponse> update_response; |
+ v4_database_->ApplyUpdate(update_response); |
+ |
+ task_runner_->RunPendingTasks(); |
+ base::RunLoop().RunUntilIdle(); |
+ |
+ VerifyExpectedStoresState(false); |
} |
} // namespace safe_browsing |