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 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
373 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, | 373 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, |
374 callback_db_ready_); | 374 callback_db_ready_); |
375 created_but_not_called_back_ = true; | 375 created_but_not_called_back_ = true; |
376 task_runner_->RunPendingTasks(); | 376 task_runner_->RunPendingTasks(); |
377 | 377 |
378 base::RunLoop().RunUntilIdle(); | 378 base::RunLoop().RunUntilIdle(); |
379 EXPECT_EQ(true, created_and_called_back_); | 379 EXPECT_EQ(true, created_and_called_back_); |
380 | 380 |
381 base::hash_set<UpdateListIdentifier> stores_to_look( | 381 base::hash_set<UpdateListIdentifier> stores_to_look( |
382 {linux_malware_id_, win_malware_id_}); | 382 {linux_malware_id_, win_malware_id_}); |
383 MatchedHashPrefixMap matched_hash_prefix_map; | 383 StoreAndHashPrefixes store_and_hash_prefixes; |
384 v4_database_->GetStoresMatchingFullHash("anything", stores_to_look, | 384 v4_database_->GetStoresMatchingFullHash("anything", stores_to_look, |
385 &matched_hash_prefix_map); | 385 &store_and_hash_prefixes); |
386 EXPECT_EQ(2u, matched_hash_prefix_map.size()); | 386 EXPECT_EQ(2u, store_and_hash_prefixes.size()); |
387 EXPECT_FALSE(matched_hash_prefix_map[linux_malware_id_].empty()); | 387 base::hash_set<UpdateListIdentifier> stores_found; |
388 EXPECT_FALSE(matched_hash_prefix_map[win_malware_id_].empty()); | 388 for (const auto& it : store_and_hash_prefixes) { |
| 389 stores_found.insert(it.list_id); |
| 390 } |
| 391 EXPECT_EQ(stores_to_look, stores_found); |
389 } | 392 } |
390 | 393 |
391 // Test to ensure the case that no stores match a given full hash. | 394 // Test to ensure the case that no stores match a given full hash. |
392 TEST_F(V4DatabaseTest, TestNoStoreMatchesFullHash) { | 395 TEST_F(V4DatabaseTest, TestNoStoreMatchesFullHash) { |
393 bool hash_prefix_matches = false; | 396 bool hash_prefix_matches = false; |
394 expected_resets_successfully_ = true; | 397 expected_resets_successfully_ = true; |
395 RegisterFactory(!expected_resets_successfully_, hash_prefix_matches); | 398 RegisterFactory(!expected_resets_successfully_, hash_prefix_matches); |
396 | 399 |
397 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, | 400 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, |
398 callback_db_ready_); | 401 callback_db_ready_); |
399 created_but_not_called_back_ = true; | 402 created_but_not_called_back_ = true; |
400 task_runner_->RunPendingTasks(); | 403 task_runner_->RunPendingTasks(); |
401 | 404 |
402 base::RunLoop().RunUntilIdle(); | 405 base::RunLoop().RunUntilIdle(); |
403 EXPECT_EQ(true, created_and_called_back_); | 406 EXPECT_EQ(true, created_and_called_back_); |
404 | 407 |
405 base::hash_set<UpdateListIdentifier> stores_to_look( | 408 base::hash_set<UpdateListIdentifier> stores_to_look( |
406 {linux_malware_id_, win_malware_id_}); | 409 {linux_malware_id_, win_malware_id_}); |
407 MatchedHashPrefixMap matched_hash_prefix_map; | 410 StoreAndHashPrefixes store_and_hash_prefixes; |
408 v4_database_->GetStoresMatchingFullHash("anything", stores_to_look, | 411 v4_database_->GetStoresMatchingFullHash("anything", stores_to_look, |
409 &matched_hash_prefix_map); | 412 &store_and_hash_prefixes); |
410 EXPECT_TRUE(matched_hash_prefix_map.empty()); | 413 EXPECT_TRUE(store_and_hash_prefixes.empty()); |
411 } | 414 } |
412 | 415 |
413 // Test to ensure the case that some stores match a given full hash. | 416 // Test to ensure the case that some stores match a given full hash. |
414 TEST_F(V4DatabaseTest, TestSomeStoresMatchFullHash) { | 417 TEST_F(V4DatabaseTest, TestSomeStoresMatchFullHash) { |
415 // Setup stores to not match the full hash. | 418 // Setup stores to not match the full hash. |
416 bool hash_prefix_matches = false; | 419 bool hash_prefix_matches = false; |
417 expected_resets_successfully_ = true; | 420 expected_resets_successfully_ = true; |
418 RegisterFactory(!expected_resets_successfully_, hash_prefix_matches); | 421 RegisterFactory(!expected_resets_successfully_, hash_prefix_matches); |
419 | 422 |
420 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, | 423 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, |
421 callback_db_ready_); | 424 callback_db_ready_); |
422 created_but_not_called_back_ = true; | 425 created_but_not_called_back_ = true; |
423 task_runner_->RunPendingTasks(); | 426 task_runner_->RunPendingTasks(); |
424 | 427 |
425 base::RunLoop().RunUntilIdle(); | 428 base::RunLoop().RunUntilIdle(); |
426 EXPECT_EQ(true, created_and_called_back_); | 429 EXPECT_EQ(true, created_and_called_back_); |
427 | 430 |
428 // Set the store corresponding to linux_malware_id_ to match the full hash. | 431 // Set the store corresponding to linux_malware_id_ to match the full hash. |
429 FakeV4Store* store = static_cast<FakeV4Store*>( | 432 FakeV4Store* store = static_cast<FakeV4Store*>( |
430 v4_database_->store_map_->at(linux_malware_id_).get()); | 433 v4_database_->store_map_->at(win_malware_id_).get()); |
431 store->set_hash_prefix_matches(true); | 434 store->set_hash_prefix_matches(true); |
432 | 435 |
433 base::hash_set<UpdateListIdentifier> stores_to_look( | 436 base::hash_set<UpdateListIdentifier> stores_to_look( |
434 {linux_malware_id_, win_malware_id_}); | 437 {linux_malware_id_, win_malware_id_}); |
435 MatchedHashPrefixMap matched_hash_prefix_map; | 438 StoreAndHashPrefixes store_and_hash_prefixes; |
436 v4_database_->GetStoresMatchingFullHash("anything", stores_to_look, | 439 v4_database_->GetStoresMatchingFullHash("anything", stores_to_look, |
437 &matched_hash_prefix_map); | 440 &store_and_hash_prefixes); |
438 EXPECT_EQ(1u, matched_hash_prefix_map.size()); | 441 EXPECT_EQ(1u, store_and_hash_prefixes.size()); |
439 EXPECT_FALSE(matched_hash_prefix_map[linux_malware_id_].empty()); | 442 EXPECT_EQ(store_and_hash_prefixes.begin()->list_id, win_malware_id_); |
| 443 EXPECT_FALSE(store_and_hash_prefixes.begin()->hash_prefix.empty()); |
440 } | 444 } |
441 | 445 |
442 // Test to ensure the case that only some stores are reported to match a given | 446 // Test to ensure the case that only some stores are reported to match a given |
443 // full hash because of stores_to_look. | 447 // full hash because of stores_to_look. |
444 TEST_F(V4DatabaseTest, TestSomeStoresMatchFullHashBecauseOfStoresToMatch) { | 448 TEST_F(V4DatabaseTest, TestSomeStoresMatchFullHashBecauseOfStoresToMatch) { |
445 // Setup all stores to match the full hash. | 449 // Setup all stores to match the full hash. |
446 bool hash_prefix_matches = true; | 450 bool hash_prefix_matches = true; |
447 expected_resets_successfully_ = true; | 451 expected_resets_successfully_ = true; |
448 RegisterFactory(!expected_resets_successfully_, hash_prefix_matches); | 452 RegisterFactory(!expected_resets_successfully_, hash_prefix_matches); |
449 | 453 |
450 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, | 454 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, |
451 callback_db_ready_); | 455 callback_db_ready_); |
452 created_but_not_called_back_ = true; | 456 created_but_not_called_back_ = true; |
453 task_runner_->RunPendingTasks(); | 457 task_runner_->RunPendingTasks(); |
454 | 458 |
455 base::RunLoop().RunUntilIdle(); | 459 base::RunLoop().RunUntilIdle(); |
456 EXPECT_EQ(true, created_and_called_back_); | 460 EXPECT_EQ(true, created_and_called_back_); |
457 | 461 |
458 base::hash_set<UpdateListIdentifier> stores_to_look({linux_malware_id_}); | 462 base::hash_set<UpdateListIdentifier> stores_to_look({linux_malware_id_}); |
459 // Don't add win_malware_id_ to the stores_to_look. | 463 // Don't add win_malware_id_ to the stores_to_look. |
460 MatchedHashPrefixMap matched_hash_prefix_map; | 464 StoreAndHashPrefixes store_and_hash_prefixes; |
461 v4_database_->GetStoresMatchingFullHash("anything", stores_to_look, | 465 v4_database_->GetStoresMatchingFullHash("anything", stores_to_look, |
462 &matched_hash_prefix_map); | 466 &store_and_hash_prefixes); |
463 EXPECT_EQ(1u, matched_hash_prefix_map.size()); | 467 EXPECT_EQ(1u, store_and_hash_prefixes.size()); |
464 EXPECT_FALSE(matched_hash_prefix_map[linux_malware_id_].empty()); | 468 EXPECT_EQ(store_and_hash_prefixes.begin()->list_id, linux_malware_id_); |
| 469 EXPECT_FALSE(store_and_hash_prefixes.begin()->hash_prefix.empty()); |
465 } | 470 } |
466 | 471 |
467 } // namespace safe_browsing | 472 } // namespace safe_browsing |
OLD | NEW |