| 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" |
| 11 #include "components/safe_browsing_db/v4_database.h" | 11 #include "components/safe_browsing_db/v4_database.h" |
| 12 #include "components/safe_browsing_db/v4_store.h" | 12 #include "components/safe_browsing_db/v4_store.h" |
| 13 #include "content/public/test/test_browser_thread_bundle.h" | 13 #include "content/public/test/test_browser_thread_bundle.h" |
| 14 #include "testing/platform_test.h" | 14 #include "testing/platform_test.h" |
| 15 | 15 |
| 16 namespace safe_browsing { | 16 namespace safe_browsing { |
| 17 | 17 |
| 18 class FakeV4Store : public V4Store { | 18 class FakeV4Store : public V4Store { |
| 19 public: | 19 public: |
| 20 FakeV4Store(const scoped_refptr<base::SequencedTaskRunner>& task_runner, | 20 FakeV4Store(const scoped_refptr<base::SequencedTaskRunner>& task_runner, |
| 21 const base::FilePath& store_path, | 21 const base::FilePath& store_path, |
| 22 const bool reset_succeeds, | |
| 23 const bool hash_prefix_matches) | 22 const bool hash_prefix_matches) |
| 24 : V4Store( | 23 : V4Store( |
| 25 task_runner, | 24 task_runner, |
| 26 base::FilePath(store_path.value() + FILE_PATH_LITERAL(".fake"))), | 25 base::FilePath(store_path.value() + FILE_PATH_LITERAL(".fake"))), |
| 27 hash_prefix_should_match_(hash_prefix_matches), | 26 hash_prefix_should_match_(hash_prefix_matches) {} |
| 28 reset_succeeds_(reset_succeeds) {} | |
| 29 | |
| 30 bool Reset() override { return reset_succeeds_; } | |
| 31 | 27 |
| 32 HashPrefix GetMatchingHashPrefix(const FullHash& full_hash) override { | 28 HashPrefix GetMatchingHashPrefix(const FullHash& full_hash) override { |
| 33 return hash_prefix_should_match_ ? full_hash : HashPrefix(); | 29 return hash_prefix_should_match_ ? full_hash : HashPrefix(); |
| 34 } | 30 } |
| 35 | 31 |
| 36 void set_hash_prefix_matches(bool hash_prefix_matches) { | 32 void set_hash_prefix_matches(bool hash_prefix_matches) { |
| 37 hash_prefix_should_match_ = hash_prefix_matches; | 33 hash_prefix_should_match_ = hash_prefix_matches; |
| 38 } | 34 } |
| 39 | 35 |
| 40 private: | 36 private: |
| 41 bool hash_prefix_should_match_; | 37 bool hash_prefix_should_match_; |
| 42 bool reset_succeeds_; | |
| 43 }; | 38 }; |
| 44 | 39 |
| 45 // This factory creates a "fake" store. It allows the caller to specify that the | 40 // This factory creates a "fake" store. It allows the caller to specify whether |
| 46 // first store should fail on Reset() i.e. return false. All subsequent stores | 41 // the store has a hash prefix matching a full hash. This is used to test the |
| 47 // always return true. This is used to test the Reset() method in V4Database. | 42 // |GetStoresMatchingFullHash()| method in |V4Database|. |
| 48 class FakeV4StoreFactory : public V4StoreFactory { | 43 class FakeV4StoreFactory : public V4StoreFactory { |
| 49 public: | 44 public: |
| 50 FakeV4StoreFactory(bool next_store_reset_fails, bool hash_prefix_matches) | 45 FakeV4StoreFactory(bool hash_prefix_matches) |
| 51 : hash_prefix_should_match_(hash_prefix_matches), | 46 : hash_prefix_should_match_(hash_prefix_matches) {} |
| 52 next_store_reset_fails_(next_store_reset_fails) {} | |
| 53 | 47 |
| 54 V4Store* CreateV4Store( | 48 V4Store* CreateV4Store( |
| 55 const scoped_refptr<base::SequencedTaskRunner>& task_runner, | 49 const scoped_refptr<base::SequencedTaskRunner>& task_runner, |
| 56 const base::FilePath& store_path) override { | 50 const base::FilePath& store_path) override { |
| 57 bool reset_succeeds = !next_store_reset_fails_; | 51 return new FakeV4Store(task_runner, store_path, hash_prefix_should_match_); |
| 58 next_store_reset_fails_ = false; | |
| 59 return new FakeV4Store(task_runner, store_path, reset_succeeds, | |
| 60 hash_prefix_should_match_); | |
| 61 } | 52 } |
| 62 | 53 |
| 63 private: | 54 private: |
| 64 bool hash_prefix_should_match_; | 55 bool hash_prefix_should_match_; |
| 65 bool next_store_reset_fails_; | |
| 66 }; | 56 }; |
| 67 | 57 |
| 68 class V4DatabaseTest : public PlatformTest { | 58 class V4DatabaseTest : public PlatformTest { |
| 69 public: | 59 public: |
| 70 V4DatabaseTest() | 60 V4DatabaseTest() |
| 71 : task_runner_(new base::TestSimpleTaskRunner), | 61 : task_runner_(new base::TestSimpleTaskRunner), |
| 72 linux_malware_id_(LINUX_PLATFORM, URL, MALWARE_THREAT), | 62 linux_malware_id_(LINUX_PLATFORM, URL, MALWARE_THREAT), |
| 73 win_malware_id_(WINDOWS_PLATFORM, URL, MALWARE_THREAT) {} | 63 win_malware_id_(WINDOWS_PLATFORM, URL, MALWARE_THREAT) {} |
| 74 | 64 |
| 75 void SetUp() override { | 65 void SetUp() override { |
| 76 PlatformTest::SetUp(); | 66 PlatformTest::SetUp(); |
| 77 | 67 |
| 78 // Setup a database in a temporary directory. | 68 // Setup a database in a temporary directory. |
| 79 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 69 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 80 database_dirname_ = temp_dir_.GetPath().AppendASCII("V4DatabaseTest"); | 70 database_dirname_ = temp_dir_.GetPath().AppendASCII("V4DatabaseTest"); |
| 81 | 71 |
| 82 created_but_not_called_back_ = false; | 72 created_but_not_called_back_ = false; |
| 83 created_and_called_back_ = false; | 73 created_and_called_back_ = false; |
| 84 | 74 |
| 85 callback_db_updated_ = | 75 callback_db_updated_ = |
| 86 base::Bind(&V4DatabaseTest::DatabaseUpdated, base::Unretained(this)); | 76 base::Bind(&V4DatabaseTest::DatabaseUpdated, base::Unretained(this)); |
| 87 | 77 |
| 88 callback_db_ready_ = base::Bind( | 78 callback_db_ready_ = base::Bind( |
| 89 &V4DatabaseTest::NewDatabaseReadyWithExpectedStorePathsAndIds, | 79 &V4DatabaseTest::NewDatabaseReadyWithExpectedStorePathsAndIds, |
| 90 base::Unretained(this)); | 80 base::Unretained(this)); |
| 91 | 81 |
| 82 callback_db_ready_for_updates_ = base::Bind( |
| 83 &V4DatabaseTest::DatabaseReadyForUpdates, base::Unretained(this)); |
| 84 |
| 92 SetupInfoMapAndExpectedState(); | 85 SetupInfoMapAndExpectedState(); |
| 93 } | 86 } |
| 94 | 87 |
| 95 void TearDown() override { | 88 void TearDown() override { |
| 96 V4Database::RegisterStoreFactoryForTest(nullptr); | 89 V4Database::RegisterStoreFactoryForTest(nullptr); |
| 97 PlatformTest::TearDown(); | 90 PlatformTest::TearDown(); |
| 98 } | 91 } |
| 99 | 92 |
| 100 void RegisterFactory(bool fails_first_reset, | 93 void RegisterFactory(bool hash_prefix_matches = true) { |
| 101 bool hash_prefix_matches = true) { | 94 factory_.reset(new FakeV4StoreFactory(hash_prefix_matches)); |
| 102 factory_.reset( | |
| 103 new FakeV4StoreFactory(fails_first_reset, hash_prefix_matches)); | |
| 104 V4Database::RegisterStoreFactoryForTest(factory_.get()); | 95 V4Database::RegisterStoreFactoryForTest(factory_.get()); |
| 105 } | 96 } |
| 106 | 97 |
| 107 void SetupInfoMapAndExpectedState() { | 98 void SetupInfoMapAndExpectedState() { |
| 108 list_infos_.emplace_back(true, "win_url_malware", win_malware_id_, | 99 list_infos_.emplace_back(true, "win_url_malware", win_malware_id_, |
| 109 SB_THREAT_TYPE_URL_MALWARE); | 100 SB_THREAT_TYPE_URL_MALWARE); |
| 110 expected_identifiers_.push_back(win_malware_id_); | 101 expected_identifiers_.push_back(win_malware_id_); |
| 111 expected_store_paths_.push_back( | 102 expected_store_paths_.push_back( |
| 112 database_dirname_.AppendASCII("win_url_malware.fake")); | 103 database_dirname_.AppendASCII("win_url_malware.fake")); |
| 113 | 104 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 130 ASSERT_EQ(expected_store_paths_.size(), v4_database->store_map_->size()); | 121 ASSERT_EQ(expected_store_paths_.size(), v4_database->store_map_->size()); |
| 131 ASSERT_EQ(expected_identifiers_.size(), v4_database->store_map_->size()); | 122 ASSERT_EQ(expected_identifiers_.size(), v4_database->store_map_->size()); |
| 132 for (size_t i = 0; i < expected_identifiers_.size(); i++) { | 123 for (size_t i = 0; i < expected_identifiers_.size(); i++) { |
| 133 const auto& expected_identifier = expected_identifiers_[i]; | 124 const auto& expected_identifier = expected_identifiers_[i]; |
| 134 const auto& store = (*v4_database->store_map_)[expected_identifier]; | 125 const auto& store = (*v4_database->store_map_)[expected_identifier]; |
| 135 ASSERT_TRUE(store); | 126 ASSERT_TRUE(store); |
| 136 const auto& expected_store_path = expected_store_paths_[i]; | 127 const auto& expected_store_path = expected_store_paths_[i]; |
| 137 EXPECT_EQ(expected_store_path, store->store_path()); | 128 EXPECT_EQ(expected_store_path, store->store_path()); |
| 138 } | 129 } |
| 139 | 130 |
| 140 EXPECT_EQ(expected_resets_successfully_, v4_database->ResetDatabase()); | |
| 141 | |
| 142 EXPECT_FALSE(created_and_called_back_); | 131 EXPECT_FALSE(created_and_called_back_); |
| 143 created_and_called_back_ = true; | 132 created_and_called_back_ = true; |
| 144 | 133 |
| 145 v4_database_ = std::move(v4_database); | 134 v4_database_ = std::move(v4_database); |
| 146 } | 135 } |
| 147 | 136 |
| 137 void DatabaseReadyForUpdates() {} |
| 138 |
| 148 std::unique_ptr<ParsedServerResponse> CreateFakeServerResponse( | 139 std::unique_ptr<ParsedServerResponse> CreateFakeServerResponse( |
| 149 StoreStateMap store_state_map, | 140 StoreStateMap store_state_map, |
| 150 bool use_valid_response_type) { | 141 bool use_valid_response_type) { |
| 151 std::unique_ptr<ParsedServerResponse> parsed_server_response( | 142 std::unique_ptr<ParsedServerResponse> parsed_server_response( |
| 152 new ParsedServerResponse); | 143 new ParsedServerResponse); |
| 153 for (const auto& store_state_iter : store_state_map) { | 144 for (const auto& store_state_iter : store_state_map) { |
| 154 ListIdentifier identifier = store_state_iter.first; | 145 ListIdentifier identifier = store_state_iter.first; |
| 155 ListUpdateResponse* lur = new ListUpdateResponse; | 146 ListUpdateResponse* lur = new ListUpdateResponse; |
| 156 lur->set_platform_type(identifier.platform_type()); | 147 lur->set_platform_type(identifier.platform_type()); |
| 157 lur->set_threat_entry_type(identifier.threat_entry_type()); | 148 lur->set_threat_entry_type(identifier.threat_entry_type()); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 189 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 199 std::unique_ptr<V4Database> v4_database_; | 190 std::unique_ptr<V4Database> v4_database_; |
| 200 base::FilePath database_dirname_; | 191 base::FilePath database_dirname_; |
| 201 base::ScopedTempDir temp_dir_; | 192 base::ScopedTempDir temp_dir_; |
| 202 content::TestBrowserThreadBundle thread_bundle_; | 193 content::TestBrowserThreadBundle thread_bundle_; |
| 203 bool created_but_not_called_back_; | 194 bool created_but_not_called_back_; |
| 204 bool created_and_called_back_; | 195 bool created_and_called_back_; |
| 205 ListInfos list_infos_; | 196 ListInfos list_infos_; |
| 206 std::vector<ListIdentifier> expected_identifiers_; | 197 std::vector<ListIdentifier> expected_identifiers_; |
| 207 std::vector<base::FilePath> expected_store_paths_; | 198 std::vector<base::FilePath> expected_store_paths_; |
| 208 bool expected_resets_successfully_; | |
| 209 std::unique_ptr<FakeV4StoreFactory> factory_; | 199 std::unique_ptr<FakeV4StoreFactory> factory_; |
| 210 DatabaseUpdatedCallback callback_db_updated_; | 200 DatabaseUpdatedCallback callback_db_updated_; |
| 211 NewDatabaseReadyCallback callback_db_ready_; | 201 NewDatabaseReadyCallback callback_db_ready_; |
| 202 DatabaseReadyForUpdatesCallback callback_db_ready_for_updates_; |
| 212 StoreStateMap expected_store_state_map_; | 203 StoreStateMap expected_store_state_map_; |
| 213 base::hash_map<ListIdentifier, V4Store*> old_stores_map_; | 204 base::hash_map<ListIdentifier, V4Store*> old_stores_map_; |
| 214 const ListIdentifier linux_malware_id_, win_malware_id_; | 205 const ListIdentifier linux_malware_id_, win_malware_id_; |
| 215 }; | 206 }; |
| 216 | 207 |
| 217 // Test to set up the database with fake stores. | 208 // Test to set up the database with fake stores. |
| 218 TEST_F(V4DatabaseTest, TestSetupDatabaseWithFakeStores) { | 209 TEST_F(V4DatabaseTest, TestSetupDatabaseWithFakeStores) { |
| 219 expected_resets_successfully_ = true; | 210 RegisterFactory(); |
| 220 RegisterFactory(!expected_resets_successfully_); | |
| 221 | 211 |
| 222 V4Database::Create(task_runner_, database_dirname_, list_infos_, | 212 V4Database::Create(task_runner_, database_dirname_, list_infos_, |
| 223 callback_db_ready_); | 213 callback_db_ready_, callback_db_ready_for_updates_); |
| 224 created_but_not_called_back_ = true; | 214 created_but_not_called_back_ = true; |
| 225 task_runner_->RunPendingTasks(); | 215 task_runner_->RunPendingTasks(); |
| 226 | 216 |
| 227 base::RunLoop().RunUntilIdle(); | |
| 228 EXPECT_EQ(true, created_and_called_back_); | |
| 229 } | |
| 230 | |
| 231 // Test to set up the database with fake stores that fail to reset. | |
| 232 TEST_F(V4DatabaseTest, TestSetupDatabaseWithFakeStoresFailsReset) { | |
| 233 expected_resets_successfully_ = false; | |
| 234 RegisterFactory(!expected_resets_successfully_); | |
| 235 | |
| 236 V4Database::Create(task_runner_, database_dirname_, list_infos_, | |
| 237 callback_db_ready_); | |
| 238 created_but_not_called_back_ = true; | |
| 239 task_runner_->RunPendingTasks(); | |
| 240 | |
| 241 base::RunLoop().RunUntilIdle(); | 217 base::RunLoop().RunUntilIdle(); |
| 242 EXPECT_EQ(true, created_and_called_back_); | 218 EXPECT_EQ(true, created_and_called_back_); |
| 243 } | 219 } |
| 244 | 220 |
| 245 // Test to check database updates as expected. | 221 // Test to check database updates as expected. |
| 246 TEST_F(V4DatabaseTest, TestApplyUpdateWithNewStates) { | 222 TEST_F(V4DatabaseTest, TestApplyUpdateWithNewStates) { |
| 247 expected_resets_successfully_ = true; | 223 RegisterFactory(); |
| 248 RegisterFactory(!expected_resets_successfully_); | |
| 249 | 224 |
| 250 V4Database::Create(task_runner_, database_dirname_, list_infos_, | 225 V4Database::Create(task_runner_, database_dirname_, list_infos_, |
| 251 callback_db_ready_); | 226 callback_db_ready_, callback_db_ready_for_updates_); |
| 252 created_but_not_called_back_ = true; | 227 created_but_not_called_back_ = true; |
| 253 task_runner_->RunPendingTasks(); | 228 task_runner_->RunPendingTasks(); |
| 254 base::RunLoop().RunUntilIdle(); | 229 base::RunLoop().RunUntilIdle(); |
| 255 | 230 |
| 256 // The database has now been created. Time to try to update it. | 231 // The database has now been created. Time to try to update it. |
| 257 EXPECT_TRUE(v4_database_); | 232 EXPECT_TRUE(v4_database_); |
| 258 const StoreMap* db_stores = v4_database_->store_map_.get(); | 233 const StoreMap* db_stores = v4_database_->store_map_.get(); |
| 259 EXPECT_EQ(expected_store_paths_.size(), db_stores->size()); | 234 EXPECT_EQ(expected_store_paths_.size(), db_stores->size()); |
| 260 for (const auto& store_iter : *db_stores) { | 235 for (const auto& store_iter : *db_stores) { |
| 261 V4Store* store = store_iter.second.get(); | 236 V4Store* store = store_iter.second.get(); |
| 262 expected_store_state_map_[store_iter.first] = store->state() + "_fake"; | 237 expected_store_state_map_[store_iter.first] = store->state() + "_fake"; |
| 263 old_stores_map_[store_iter.first] = store; | 238 old_stores_map_[store_iter.first] = store; |
| 264 } | 239 } |
| 265 | 240 |
| 266 v4_database_->ApplyUpdate( | 241 v4_database_->ApplyUpdate( |
| 267 CreateFakeServerResponse(expected_store_state_map_, true), | 242 CreateFakeServerResponse(expected_store_state_map_, true), |
| 268 callback_db_updated_); | 243 callback_db_updated_); |
| 269 | 244 |
| 270 task_runner_->RunPendingTasks(); | 245 task_runner_->RunPendingTasks(); |
| 271 base::RunLoop().RunUntilIdle(); | 246 base::RunLoop().RunUntilIdle(); |
| 272 | 247 |
| 273 VerifyExpectedStoresState(true); | 248 VerifyExpectedStoresState(true); |
| 274 } | 249 } |
| 275 | 250 |
| 276 // Test to ensure no state updates leads to no store updates. | 251 // Test to ensure no state updates leads to no store updates. |
| 277 TEST_F(V4DatabaseTest, TestApplyUpdateWithNoNewState) { | 252 TEST_F(V4DatabaseTest, TestApplyUpdateWithNoNewState) { |
| 278 expected_resets_successfully_ = true; | 253 RegisterFactory(); |
| 279 RegisterFactory(!expected_resets_successfully_); | |
| 280 | 254 |
| 281 V4Database::Create(task_runner_, database_dirname_, list_infos_, | 255 V4Database::Create(task_runner_, database_dirname_, list_infos_, |
| 282 callback_db_ready_); | 256 callback_db_ready_, callback_db_ready_for_updates_); |
| 283 created_but_not_called_back_ = true; | 257 created_but_not_called_back_ = true; |
| 284 task_runner_->RunPendingTasks(); | 258 task_runner_->RunPendingTasks(); |
| 285 base::RunLoop().RunUntilIdle(); | 259 base::RunLoop().RunUntilIdle(); |
| 286 | 260 |
| 287 // The database has now been created. Time to try to update it. | 261 // The database has now been created. Time to try to update it. |
| 288 EXPECT_TRUE(v4_database_); | 262 EXPECT_TRUE(v4_database_); |
| 289 const StoreMap* db_stores = v4_database_->store_map_.get(); | 263 const StoreMap* db_stores = v4_database_->store_map_.get(); |
| 290 EXPECT_EQ(expected_store_paths_.size(), db_stores->size()); | 264 EXPECT_EQ(expected_store_paths_.size(), db_stores->size()); |
| 291 for (const auto& store_iter : *db_stores) { | 265 for (const auto& store_iter : *db_stores) { |
| 292 V4Store* store = store_iter.second.get(); | 266 V4Store* store = store_iter.second.get(); |
| 293 expected_store_state_map_[store_iter.first] = store->state(); | 267 expected_store_state_map_[store_iter.first] = store->state(); |
| 294 old_stores_map_[store_iter.first] = store; | 268 old_stores_map_[store_iter.first] = store; |
| 295 } | 269 } |
| 296 | 270 |
| 297 v4_database_->ApplyUpdate( | 271 v4_database_->ApplyUpdate( |
| 298 CreateFakeServerResponse(expected_store_state_map_, true), | 272 CreateFakeServerResponse(expected_store_state_map_, true), |
| 299 callback_db_updated_); | 273 callback_db_updated_); |
| 300 | 274 |
| 301 task_runner_->RunPendingTasks(); | 275 task_runner_->RunPendingTasks(); |
| 302 base::RunLoop().RunUntilIdle(); | 276 base::RunLoop().RunUntilIdle(); |
| 303 | 277 |
| 304 VerifyExpectedStoresState(false); | 278 VerifyExpectedStoresState(false); |
| 305 } | 279 } |
| 306 | 280 |
| 307 // Test to ensure no updates leads to no store updates. | 281 // Test to ensure no updates leads to no store updates. |
| 308 TEST_F(V4DatabaseTest, TestApplyUpdateWithEmptyUpdate) { | 282 TEST_F(V4DatabaseTest, TestApplyUpdateWithEmptyUpdate) { |
| 309 expected_resets_successfully_ = true; | 283 RegisterFactory(); |
| 310 RegisterFactory(!expected_resets_successfully_); | |
| 311 | 284 |
| 312 V4Database::Create(task_runner_, database_dirname_, list_infos_, | 285 V4Database::Create(task_runner_, database_dirname_, list_infos_, |
| 313 callback_db_ready_); | 286 callback_db_ready_, callback_db_ready_for_updates_); |
| 314 created_but_not_called_back_ = true; | 287 created_but_not_called_back_ = true; |
| 315 task_runner_->RunPendingTasks(); | 288 task_runner_->RunPendingTasks(); |
| 316 base::RunLoop().RunUntilIdle(); | 289 base::RunLoop().RunUntilIdle(); |
| 317 | 290 |
| 318 // The database has now been created. Time to try to update it. | 291 // The database has now been created. Time to try to update it. |
| 319 EXPECT_TRUE(v4_database_); | 292 EXPECT_TRUE(v4_database_); |
| 320 const StoreMap* db_stores = v4_database_->store_map_.get(); | 293 const StoreMap* db_stores = v4_database_->store_map_.get(); |
| 321 EXPECT_EQ(expected_store_paths_.size(), db_stores->size()); | 294 EXPECT_EQ(expected_store_paths_.size(), db_stores->size()); |
| 322 for (const auto& store_iter : *db_stores) { | 295 for (const auto& store_iter : *db_stores) { |
| 323 V4Store* store = store_iter.second.get(); | 296 V4Store* store = store_iter.second.get(); |
| 324 expected_store_state_map_[store_iter.first] = store->state(); | 297 expected_store_state_map_[store_iter.first] = store->state(); |
| 325 old_stores_map_[store_iter.first] = store; | 298 old_stores_map_[store_iter.first] = store; |
| 326 } | 299 } |
| 327 | 300 |
| 328 std::unique_ptr<ParsedServerResponse> parsed_server_response( | 301 std::unique_ptr<ParsedServerResponse> parsed_server_response( |
| 329 new ParsedServerResponse); | 302 new ParsedServerResponse); |
| 330 v4_database_->ApplyUpdate(std::move(parsed_server_response), | 303 v4_database_->ApplyUpdate(std::move(parsed_server_response), |
| 331 callback_db_updated_); | 304 callback_db_updated_); |
| 332 | 305 |
| 333 task_runner_->RunPendingTasks(); | 306 task_runner_->RunPendingTasks(); |
| 334 base::RunLoop().RunUntilIdle(); | 307 base::RunLoop().RunUntilIdle(); |
| 335 | 308 |
| 336 VerifyExpectedStoresState(false); | 309 VerifyExpectedStoresState(false); |
| 337 } | 310 } |
| 338 | 311 |
| 339 // Test to ensure invalid update leads to no store changes. | 312 // Test to ensure invalid update leads to no store changes. |
| 340 TEST_F(V4DatabaseTest, TestApplyUpdateWithInvalidUpdate) { | 313 TEST_F(V4DatabaseTest, TestApplyUpdateWithInvalidUpdate) { |
| 341 expected_resets_successfully_ = true; | 314 RegisterFactory(); |
| 342 RegisterFactory(!expected_resets_successfully_); | |
| 343 | 315 |
| 344 V4Database::Create(task_runner_, database_dirname_, list_infos_, | 316 V4Database::Create(task_runner_, database_dirname_, list_infos_, |
| 345 callback_db_ready_); | 317 callback_db_ready_, callback_db_ready_for_updates_); |
| 346 created_but_not_called_back_ = true; | 318 created_but_not_called_back_ = true; |
| 347 task_runner_->RunPendingTasks(); | 319 task_runner_->RunPendingTasks(); |
| 348 base::RunLoop().RunUntilIdle(); | 320 base::RunLoop().RunUntilIdle(); |
| 349 | 321 |
| 350 // The database has now been created. Time to try to update it. | 322 // The database has now been created. Time to try to update it. |
| 351 EXPECT_TRUE(v4_database_); | 323 EXPECT_TRUE(v4_database_); |
| 352 const StoreMap* db_stores = v4_database_->store_map_.get(); | 324 const StoreMap* db_stores = v4_database_->store_map_.get(); |
| 353 EXPECT_EQ(expected_store_paths_.size(), db_stores->size()); | 325 EXPECT_EQ(expected_store_paths_.size(), db_stores->size()); |
| 354 for (const auto& store_iter : *db_stores) { | 326 for (const auto& store_iter : *db_stores) { |
| 355 V4Store* store = store_iter.second.get(); | 327 V4Store* store = store_iter.second.get(); |
| 356 expected_store_state_map_[store_iter.first] = store->state(); | 328 expected_store_state_map_[store_iter.first] = store->state(); |
| 357 old_stores_map_[store_iter.first] = store; | 329 old_stores_map_[store_iter.first] = store; |
| 358 } | 330 } |
| 359 | 331 |
| 360 v4_database_->ApplyUpdate( | 332 v4_database_->ApplyUpdate( |
| 361 CreateFakeServerResponse(expected_store_state_map_, false), | 333 CreateFakeServerResponse(expected_store_state_map_, false), |
| 362 callback_db_updated_); | 334 callback_db_updated_); |
| 363 task_runner_->RunPendingTasks(); | 335 task_runner_->RunPendingTasks(); |
| 364 base::RunLoop().RunUntilIdle(); | 336 base::RunLoop().RunUntilIdle(); |
| 365 | 337 |
| 366 VerifyExpectedStoresState(false); | 338 VerifyExpectedStoresState(false); |
| 367 } | 339 } |
| 368 | 340 |
| 369 // Test to ensure the case that all stores match a given full hash. | 341 // Test to ensure the case that all stores match a given full hash. |
| 370 TEST_F(V4DatabaseTest, TestAllStoresMatchFullHash) { | 342 TEST_F(V4DatabaseTest, TestAllStoresMatchFullHash) { |
| 371 bool hash_prefix_matches = true; | 343 bool hash_prefix_matches = true; |
| 372 expected_resets_successfully_ = true; | 344 RegisterFactory(hash_prefix_matches); |
| 373 RegisterFactory(!expected_resets_successfully_, hash_prefix_matches); | |
| 374 | 345 |
| 375 V4Database::Create(task_runner_, database_dirname_, list_infos_, | 346 V4Database::Create(task_runner_, database_dirname_, list_infos_, |
| 376 callback_db_ready_); | 347 callback_db_ready_, callback_db_ready_for_updates_); |
| 377 created_but_not_called_back_ = true; | 348 created_but_not_called_back_ = true; |
| 378 task_runner_->RunPendingTasks(); | 349 task_runner_->RunPendingTasks(); |
| 379 | 350 |
| 380 base::RunLoop().RunUntilIdle(); | 351 base::RunLoop().RunUntilIdle(); |
| 381 EXPECT_EQ(true, created_and_called_back_); | 352 EXPECT_EQ(true, created_and_called_back_); |
| 382 | 353 |
| 383 StoresToCheck stores_to_check({linux_malware_id_, win_malware_id_}); | 354 StoresToCheck stores_to_check({linux_malware_id_, win_malware_id_}); |
| 384 StoreAndHashPrefixes store_and_hash_prefixes; | 355 StoreAndHashPrefixes store_and_hash_prefixes; |
| 385 v4_database_->GetStoresMatchingFullHash("anything", stores_to_check, | 356 v4_database_->GetStoresMatchingFullHash("anything", stores_to_check, |
| 386 &store_and_hash_prefixes); | 357 &store_and_hash_prefixes); |
| 387 EXPECT_EQ(2u, store_and_hash_prefixes.size()); | 358 EXPECT_EQ(2u, store_and_hash_prefixes.size()); |
| 388 StoresToCheck stores_found; | 359 StoresToCheck stores_found; |
| 389 for (const auto& it : store_and_hash_prefixes) { | 360 for (const auto& it : store_and_hash_prefixes) { |
| 390 stores_found.insert(it.list_id); | 361 stores_found.insert(it.list_id); |
| 391 } | 362 } |
| 392 EXPECT_EQ(stores_to_check, stores_found); | 363 EXPECT_EQ(stores_to_check, stores_found); |
| 393 } | 364 } |
| 394 | 365 |
| 395 // Test to ensure the case that no stores match a given full hash. | 366 // Test to ensure the case that no stores match a given full hash. |
| 396 TEST_F(V4DatabaseTest, TestNoStoreMatchesFullHash) { | 367 TEST_F(V4DatabaseTest, TestNoStoreMatchesFullHash) { |
| 397 bool hash_prefix_matches = false; | 368 bool hash_prefix_matches = false; |
| 398 expected_resets_successfully_ = true; | 369 RegisterFactory(hash_prefix_matches); |
| 399 RegisterFactory(!expected_resets_successfully_, hash_prefix_matches); | |
| 400 | 370 |
| 401 V4Database::Create(task_runner_, database_dirname_, list_infos_, | 371 V4Database::Create(task_runner_, database_dirname_, list_infos_, |
| 402 callback_db_ready_); | 372 callback_db_ready_, callback_db_ready_for_updates_); |
| 403 created_but_not_called_back_ = true; | 373 created_but_not_called_back_ = true; |
| 404 task_runner_->RunPendingTasks(); | 374 task_runner_->RunPendingTasks(); |
| 405 | 375 |
| 406 base::RunLoop().RunUntilIdle(); | 376 base::RunLoop().RunUntilIdle(); |
| 407 EXPECT_EQ(true, created_and_called_back_); | 377 EXPECT_EQ(true, created_and_called_back_); |
| 408 | 378 |
| 409 StoreAndHashPrefixes store_and_hash_prefixes; | 379 StoreAndHashPrefixes store_and_hash_prefixes; |
| 410 v4_database_->GetStoresMatchingFullHash( | 380 v4_database_->GetStoresMatchingFullHash( |
| 411 "anything", StoresToCheck({linux_malware_id_, win_malware_id_}), | 381 "anything", StoresToCheck({linux_malware_id_, win_malware_id_}), |
| 412 &store_and_hash_prefixes); | 382 &store_and_hash_prefixes); |
| 413 EXPECT_TRUE(store_and_hash_prefixes.empty()); | 383 EXPECT_TRUE(store_and_hash_prefixes.empty()); |
| 414 } | 384 } |
| 415 | 385 |
| 416 // Test to ensure the case that some stores match a given full hash. | 386 // Test to ensure the case that some stores match a given full hash. |
| 417 TEST_F(V4DatabaseTest, TestSomeStoresMatchFullHash) { | 387 TEST_F(V4DatabaseTest, TestSomeStoresMatchFullHash) { |
| 418 // Setup stores to not match the full hash. | 388 // Setup stores to not match the full hash. |
| 419 bool hash_prefix_matches = false; | 389 bool hash_prefix_matches = false; |
| 420 expected_resets_successfully_ = true; | 390 RegisterFactory(hash_prefix_matches); |
| 421 RegisterFactory(!expected_resets_successfully_, hash_prefix_matches); | |
| 422 | 391 |
| 423 V4Database::Create(task_runner_, database_dirname_, list_infos_, | 392 V4Database::Create(task_runner_, database_dirname_, list_infos_, |
| 424 callback_db_ready_); | 393 callback_db_ready_, callback_db_ready_for_updates_); |
| 425 created_but_not_called_back_ = true; | 394 created_but_not_called_back_ = true; |
| 426 task_runner_->RunPendingTasks(); | 395 task_runner_->RunPendingTasks(); |
| 427 | 396 |
| 428 base::RunLoop().RunUntilIdle(); | 397 base::RunLoop().RunUntilIdle(); |
| 429 EXPECT_EQ(true, created_and_called_back_); | 398 EXPECT_EQ(true, created_and_called_back_); |
| 430 | 399 |
| 431 // Set the store corresponding to linux_malware_id_ to match the full hash. | 400 // Set the store corresponding to linux_malware_id_ to match the full hash. |
| 432 FakeV4Store* store = static_cast<FakeV4Store*>( | 401 FakeV4Store* store = static_cast<FakeV4Store*>( |
| 433 v4_database_->store_map_->at(win_malware_id_).get()); | 402 v4_database_->store_map_->at(win_malware_id_).get()); |
| 434 store->set_hash_prefix_matches(true); | 403 store->set_hash_prefix_matches(true); |
| 435 | 404 |
| 436 StoreAndHashPrefixes store_and_hash_prefixes; | 405 StoreAndHashPrefixes store_and_hash_prefixes; |
| 437 v4_database_->GetStoresMatchingFullHash( | 406 v4_database_->GetStoresMatchingFullHash( |
| 438 "anything", StoresToCheck({linux_malware_id_, win_malware_id_}), | 407 "anything", StoresToCheck({linux_malware_id_, win_malware_id_}), |
| 439 &store_and_hash_prefixes); | 408 &store_and_hash_prefixes); |
| 440 EXPECT_EQ(1u, store_and_hash_prefixes.size()); | 409 EXPECT_EQ(1u, store_and_hash_prefixes.size()); |
| 441 EXPECT_EQ(store_and_hash_prefixes.begin()->list_id, win_malware_id_); | 410 EXPECT_EQ(store_and_hash_prefixes.begin()->list_id, win_malware_id_); |
| 442 EXPECT_FALSE(store_and_hash_prefixes.begin()->hash_prefix.empty()); | 411 EXPECT_FALSE(store_and_hash_prefixes.begin()->hash_prefix.empty()); |
| 443 } | 412 } |
| 444 | 413 |
| 445 // Test to ensure the case that only some stores are reported to match a given | 414 // Test to ensure the case that only some stores are reported to match a given |
| 446 // full hash because of StoresToCheck. | 415 // full hash because of StoresToCheck. |
| 447 TEST_F(V4DatabaseTest, TestSomeStoresMatchFullHashBecauseOfStoresToMatch) { | 416 TEST_F(V4DatabaseTest, TestSomeStoresMatchFullHashBecauseOfStoresToMatch) { |
| 448 // Setup all stores to match the full hash. | 417 // Setup all stores to match the full hash. |
| 449 bool hash_prefix_matches = true; | 418 bool hash_prefix_matches = true; |
| 450 expected_resets_successfully_ = true; | 419 RegisterFactory(hash_prefix_matches); |
| 451 RegisterFactory(!expected_resets_successfully_, hash_prefix_matches); | |
| 452 | 420 |
| 453 V4Database::Create(task_runner_, database_dirname_, list_infos_, | 421 V4Database::Create(task_runner_, database_dirname_, list_infos_, |
| 454 callback_db_ready_); | 422 callback_db_ready_, callback_db_ready_for_updates_); |
| 455 created_but_not_called_back_ = true; | 423 created_but_not_called_back_ = true; |
| 456 task_runner_->RunPendingTasks(); | 424 task_runner_->RunPendingTasks(); |
| 457 | 425 |
| 458 base::RunLoop().RunUntilIdle(); | 426 base::RunLoop().RunUntilIdle(); |
| 459 EXPECT_EQ(true, created_and_called_back_); | 427 EXPECT_EQ(true, created_and_called_back_); |
| 460 | 428 |
| 461 // Don't add win_malware_id_ to the StoresToCheck. | 429 // Don't add win_malware_id_ to the StoresToCheck. |
| 462 StoreAndHashPrefixes store_and_hash_prefixes; | 430 StoreAndHashPrefixes store_and_hash_prefixes; |
| 463 v4_database_->GetStoresMatchingFullHash( | 431 v4_database_->GetStoresMatchingFullHash( |
| 464 "anything", StoresToCheck({linux_malware_id_}), &store_and_hash_prefixes); | 432 "anything", StoresToCheck({linux_malware_id_}), &store_and_hash_prefixes); |
| 465 EXPECT_EQ(1u, store_and_hash_prefixes.size()); | 433 EXPECT_EQ(1u, store_and_hash_prefixes.size()); |
| 466 EXPECT_EQ(store_and_hash_prefixes.begin()->list_id, linux_malware_id_); | 434 EXPECT_EQ(store_and_hash_prefixes.begin()->list_id, linux_malware_id_); |
| 467 EXPECT_FALSE(store_and_hash_prefixes.begin()->hash_prefix.empty()); | 435 EXPECT_FALSE(store_and_hash_prefixes.begin()->hash_prefix.empty()); |
| 468 } | 436 } |
| 469 | 437 |
| 470 } // namespace safe_browsing | 438 } // namespace safe_browsing |
| OLD | NEW |