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) | 22 const bool reset_succeeds, |
23 const bool hash_prefix_matches) | |
23 : V4Store( | 24 : V4Store( |
24 task_runner, | 25 task_runner, |
25 base::FilePath(store_path.value() + FILE_PATH_LITERAL(".fake"))), | 26 base::FilePath(store_path.value() + FILE_PATH_LITERAL(".fake"))), |
27 hash_prefix_matches_(hash_prefix_matches), | |
26 reset_succeeds_(reset_succeeds) {} | 28 reset_succeeds_(reset_succeeds) {} |
27 | 29 |
28 bool Reset() override { return reset_succeeds_; } | 30 bool Reset() override { return reset_succeeds_; } |
29 | 31 |
32 HashPrefix GetMatchingHashPrefix(const FullHash& full_hash) override { | |
33 return hash_prefix_matches_ ? full_hash : HashPrefix(); | |
34 } | |
35 | |
36 void set_hash_prefix_matches(bool hash_prefix_matches) { | |
37 hash_prefix_matches_ = hash_prefix_matches; | |
38 } | |
39 | |
30 private: | 40 private: |
41 bool hash_prefix_matches_; | |
Nathan Parker
2016/07/20 20:44:13
nit: "matches" here sounds like a plural of match,
vakh (use Gerrit instead)
2016/07/20 21:10:27
Done.
| |
31 bool reset_succeeds_; | 42 bool reset_succeeds_; |
32 }; | 43 }; |
33 | 44 |
34 // This factory creates a "fake" store. It allows the caller to specify that the | 45 // This factory creates a "fake" store. It allows the caller to specify that the |
35 // first store should fail on Reset() i.e. return false. All subsequent stores | 46 // first store should fail on Reset() i.e. return false. All subsequent stores |
36 // always return true. This is used to test the Reset() method in V4Database. | 47 // always return true. This is used to test the Reset() method in V4Database. |
37 class FakeV4StoreFactory : public V4StoreFactory { | 48 class FakeV4StoreFactory : public V4StoreFactory { |
38 public: | 49 public: |
39 FakeV4StoreFactory(bool next_store_reset_fails) | 50 FakeV4StoreFactory(bool next_store_reset_fails, bool hash_prefix_matches) |
40 : next_store_reset_fails_(next_store_reset_fails) {} | 51 : hash_prefix_matches_(hash_prefix_matches), |
52 next_store_reset_fails_(next_store_reset_fails) {} | |
41 | 53 |
42 V4Store* CreateV4Store( | 54 V4Store* CreateV4Store( |
43 const scoped_refptr<base::SequencedTaskRunner>& task_runner, | 55 const scoped_refptr<base::SequencedTaskRunner>& task_runner, |
44 const base::FilePath& store_path) override { | 56 const base::FilePath& store_path) override { |
45 bool reset_succeeds = !next_store_reset_fails_; | 57 bool reset_succeeds = !next_store_reset_fails_; |
46 next_store_reset_fails_ = false; | 58 next_store_reset_fails_ = false; |
47 return new FakeV4Store(task_runner, store_path, reset_succeeds); | 59 return new FakeV4Store(task_runner, store_path, reset_succeeds, |
60 hash_prefix_matches_); | |
48 } | 61 } |
49 | 62 |
50 private: | 63 private: |
64 bool hash_prefix_matches_; | |
51 bool next_store_reset_fails_; | 65 bool next_store_reset_fails_; |
52 }; | 66 }; |
53 | 67 |
54 class V4DatabaseTest : public PlatformTest { | 68 class V4DatabaseTest : public PlatformTest { |
55 public: | 69 public: |
56 V4DatabaseTest() : task_runner_(new base::TestSimpleTaskRunner) {} | 70 V4DatabaseTest() |
71 : task_runner_(new base::TestSimpleTaskRunner), | |
72 linux_malware_id_(LINUX_PLATFORM, URL, MALWARE_THREAT), | |
73 win_malware_id_(WINDOWS_PLATFORM, URL, MALWARE_THREAT) {} | |
57 | 74 |
58 void SetUp() override { | 75 void SetUp() override { |
59 PlatformTest::SetUp(); | 76 PlatformTest::SetUp(); |
60 | 77 |
61 // Setup a database in a temporary directory. | 78 // Setup a database in a temporary directory. |
62 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 79 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
63 database_dirname_ = temp_dir_.path().AppendASCII("V4DatabaseTest"); | 80 database_dirname_ = temp_dir_.path().AppendASCII("V4DatabaseTest"); |
64 | 81 |
65 created_but_not_called_back_ = false; | 82 created_but_not_called_back_ = false; |
66 created_and_called_back_ = false; | 83 created_and_called_back_ = false; |
67 | 84 |
68 callback_db_updated_ = | 85 callback_db_updated_ = |
69 base::Bind(&V4DatabaseTest::DatabaseUpdated, base::Unretained(this)); | 86 base::Bind(&V4DatabaseTest::DatabaseUpdated, base::Unretained(this)); |
70 | 87 |
71 callback_db_ready_ = base::Bind( | 88 callback_db_ready_ = base::Bind( |
72 &V4DatabaseTest::NewDatabaseReadyWithExpectedStorePathsAndIds, | 89 &V4DatabaseTest::NewDatabaseReadyWithExpectedStorePathsAndIds, |
73 base::Unretained(this)); | 90 base::Unretained(this)); |
74 | 91 |
75 SetupInfoMapAndExpectedState(); | 92 SetupInfoMapAndExpectedState(); |
76 } | 93 } |
77 | 94 |
78 void TearDown() override { | 95 void TearDown() override { |
79 V4Database::RegisterStoreFactoryForTest(nullptr); | 96 V4Database::RegisterStoreFactoryForTest(nullptr); |
80 PlatformTest::TearDown(); | 97 PlatformTest::TearDown(); |
81 } | 98 } |
82 | 99 |
83 void RegisterFactory(bool fails_first_reset) { | 100 void RegisterFactory(bool fails_first_reset, |
84 factory_.reset(new FakeV4StoreFactory(fails_first_reset)); | 101 bool hash_prefix_matches = true) { |
102 factory_.reset( | |
103 new FakeV4StoreFactory(fails_first_reset, hash_prefix_matches)); | |
85 V4Database::RegisterStoreFactoryForTest(factory_.get()); | 104 V4Database::RegisterStoreFactoryForTest(factory_.get()); |
86 } | 105 } |
87 | 106 |
88 void SetupInfoMapAndExpectedState() { | 107 void SetupInfoMapAndExpectedState() { |
89 UpdateListIdentifier win_malware_id(WINDOWS_PLATFORM, URL, MALWARE_THREAT); | 108 store_file_name_map_[win_malware_id_] = "win_url_malware"; |
90 store_file_name_map_[win_malware_id] = "win_url_malware"; | 109 expected_identifiers_.push_back(win_malware_id_); |
91 expected_identifiers_.push_back(win_malware_id); | |
92 expected_store_paths_.push_back( | 110 expected_store_paths_.push_back( |
93 database_dirname_.AppendASCII("win_url_malware.fake")); | 111 database_dirname_.AppendASCII("win_url_malware.fake")); |
94 | 112 |
95 UpdateListIdentifier linux_malware_id(LINUX_PLATFORM, URL, MALWARE_THREAT); | 113 store_file_name_map_[linux_malware_id_] = "linux_url_malware"; |
96 store_file_name_map_[linux_malware_id] = "linux_url_malware"; | 114 expected_identifiers_.push_back(linux_malware_id_); |
97 expected_identifiers_.push_back(linux_malware_id); | |
98 expected_store_paths_.push_back( | 115 expected_store_paths_.push_back( |
99 database_dirname_.AppendASCII("linux_url_malware.fake")); | 116 database_dirname_.AppendASCII("linux_url_malware.fake")); |
100 } | 117 } |
101 | 118 |
102 void DatabaseUpdated() {} | 119 void DatabaseUpdated() {} |
103 void NewDatabaseReadyWithExpectedStorePathsAndIds( | 120 void NewDatabaseReadyWithExpectedStorePathsAndIds( |
104 std::unique_ptr<V4Database> v4_database) { | 121 std::unique_ptr<V4Database> v4_database) { |
105 ASSERT_TRUE(v4_database); | 122 ASSERT_TRUE(v4_database); |
106 ASSERT_TRUE(v4_database->store_map_); | 123 ASSERT_TRUE(v4_database->store_map_); |
107 | 124 |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
185 bool created_and_called_back_; | 202 bool created_and_called_back_; |
186 StoreFileNameMap store_file_name_map_; | 203 StoreFileNameMap store_file_name_map_; |
187 std::vector<UpdateListIdentifier> expected_identifiers_; | 204 std::vector<UpdateListIdentifier> expected_identifiers_; |
188 std::vector<base::FilePath> expected_store_paths_; | 205 std::vector<base::FilePath> expected_store_paths_; |
189 bool expected_resets_successfully_; | 206 bool expected_resets_successfully_; |
190 std::unique_ptr<FakeV4StoreFactory> factory_; | 207 std::unique_ptr<FakeV4StoreFactory> factory_; |
191 DatabaseUpdatedCallback callback_db_updated_; | 208 DatabaseUpdatedCallback callback_db_updated_; |
192 NewDatabaseReadyCallback callback_db_ready_; | 209 NewDatabaseReadyCallback callback_db_ready_; |
193 StoreStateMap expected_store_state_map_; | 210 StoreStateMap expected_store_state_map_; |
194 base::hash_map<UpdateListIdentifier, V4Store*> old_stores_map_; | 211 base::hash_map<UpdateListIdentifier, V4Store*> old_stores_map_; |
212 const UpdateListIdentifier linux_malware_id_, win_malware_id_; | |
195 }; | 213 }; |
196 | 214 |
197 // Test to set up the database with fake stores. | 215 // Test to set up the database with fake stores. |
198 TEST_F(V4DatabaseTest, TestSetupDatabaseWithFakeStores) { | 216 TEST_F(V4DatabaseTest, TestSetupDatabaseWithFakeStores) { |
199 expected_resets_successfully_ = true; | 217 expected_resets_successfully_ = true; |
200 RegisterFactory(!expected_resets_successfully_); | 218 RegisterFactory(!expected_resets_successfully_); |
201 | 219 |
202 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, | 220 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, |
203 callback_db_ready_); | 221 callback_db_ready_); |
204 created_but_not_called_back_ = true; | 222 created_but_not_called_back_ = true; |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
339 | 357 |
340 v4_database_->ApplyUpdate( | 358 v4_database_->ApplyUpdate( |
341 CreateFakeServerResponse(expected_store_state_map_, false), | 359 CreateFakeServerResponse(expected_store_state_map_, false), |
342 callback_db_updated_); | 360 callback_db_updated_); |
343 task_runner_->RunPendingTasks(); | 361 task_runner_->RunPendingTasks(); |
344 base::RunLoop().RunUntilIdle(); | 362 base::RunLoop().RunUntilIdle(); |
345 | 363 |
346 VerifyExpectedStoresState(false); | 364 VerifyExpectedStoresState(false); |
347 } | 365 } |
348 | 366 |
367 // Test to ensure the case that all stores match a given full hash. | |
368 TEST_F(V4DatabaseTest, TestAllStoresMatchFullHash) { | |
369 bool hash_prefix_matches = true; | |
370 expected_resets_successfully_ = true; | |
371 RegisterFactory(!expected_resets_successfully_, hash_prefix_matches); | |
372 | |
373 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, | |
374 callback_db_ready_); | |
375 created_but_not_called_back_ = true; | |
376 task_runner_->RunPendingTasks(); | |
377 | |
378 base::RunLoop().RunUntilIdle(); | |
379 EXPECT_EQ(true, created_and_called_back_); | |
380 | |
381 base::hash_set<UpdateListIdentifier> stores_to_look; | |
Nathan Parker
2016/07/20 20:44:13
could do
stores_to_look = {linux_malware_id, win_m
vakh (use Gerrit instead)
2016/07/20 21:10:27
Done.
| |
382 stores_to_look.insert(linux_malware_id_); | |
383 stores_to_look.insert(win_malware_id_); | |
384 MatchedHashPrefixMap matched_hash_prefix_map; | |
385 v4_database_->GetStoresMatchingFullHash("anything", stores_to_look, | |
386 &matched_hash_prefix_map); | |
387 EXPECT_EQ(2u, matched_hash_prefix_map.size()); | |
388 EXPECT_FALSE(matched_hash_prefix_map[linux_malware_id_].empty()); | |
389 EXPECT_FALSE(matched_hash_prefix_map[win_malware_id_].empty()); | |
390 } | |
391 | |
392 // Test to ensure the case that no stores match a given full hash. | |
393 TEST_F(V4DatabaseTest, TestNoStoreMatchesFullHash) { | |
394 bool hash_prefix_matches = false; | |
395 expected_resets_successfully_ = true; | |
396 RegisterFactory(!expected_resets_successfully_, hash_prefix_matches); | |
397 | |
398 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, | |
399 callback_db_ready_); | |
400 created_but_not_called_back_ = true; | |
401 task_runner_->RunPendingTasks(); | |
402 | |
403 base::RunLoop().RunUntilIdle(); | |
404 EXPECT_EQ(true, created_and_called_back_); | |
405 | |
406 base::hash_set<UpdateListIdentifier> stores_to_look; | |
407 stores_to_look.insert(linux_malware_id_); | |
408 stores_to_look.insert(win_malware_id_); | |
409 MatchedHashPrefixMap matched_hash_prefix_map; | |
410 v4_database_->GetStoresMatchingFullHash("anything", stores_to_look, | |
411 &matched_hash_prefix_map); | |
412 EXPECT_TRUE(matched_hash_prefix_map.empty()); | |
413 } | |
414 | |
415 // Test to ensure the case that some stores match a given full hash. | |
416 TEST_F(V4DatabaseTest, TestSomeStoresMatchFullHash) { | |
417 // Setup stores to not match the full hash. | |
418 bool hash_prefix_matches = false; | |
419 expected_resets_successfully_ = true; | |
420 RegisterFactory(!expected_resets_successfully_, hash_prefix_matches); | |
421 | |
422 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, | |
423 callback_db_ready_); | |
424 created_but_not_called_back_ = true; | |
425 task_runner_->RunPendingTasks(); | |
426 | |
427 base::RunLoop().RunUntilIdle(); | |
428 EXPECT_EQ(true, created_and_called_back_); | |
429 | |
430 // Set the store corresponding to linux_malware_id_ to match the full hash. | |
431 FakeV4Store* store = static_cast<FakeV4Store*>( | |
432 v4_database_->store_map_->at(linux_malware_id_).get()); | |
433 store->set_hash_prefix_matches(true); | |
434 | |
435 base::hash_set<UpdateListIdentifier> stores_to_look; | |
436 stores_to_look.insert(linux_malware_id_); | |
437 stores_to_look.insert(win_malware_id_); | |
438 MatchedHashPrefixMap matched_hash_prefix_map; | |
439 v4_database_->GetStoresMatchingFullHash("anything", stores_to_look, | |
440 &matched_hash_prefix_map); | |
441 EXPECT_EQ(1u, matched_hash_prefix_map.size()); | |
442 EXPECT_FALSE(matched_hash_prefix_map[linux_malware_id_].empty()); | |
443 } | |
444 | |
445 // Test to ensure the case that only some stores are reported to match a given | |
446 // full hash because of stores_to_look. | |
447 TEST_F(V4DatabaseTest, TestSomeStoresMatchFullHashBecauseOfStoresToMatch) { | |
448 // Setup all stores to match the full hash. | |
449 bool hash_prefix_matches = true; | |
450 expected_resets_successfully_ = true; | |
451 RegisterFactory(!expected_resets_successfully_, hash_prefix_matches); | |
452 | |
453 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, | |
454 callback_db_ready_); | |
455 created_but_not_called_back_ = true; | |
456 task_runner_->RunPendingTasks(); | |
457 | |
458 base::RunLoop().RunUntilIdle(); | |
459 EXPECT_EQ(true, created_and_called_back_); | |
460 | |
461 base::hash_set<UpdateListIdentifier> stores_to_look; | |
462 stores_to_look.insert(linux_malware_id_); | |
463 // Don't add win_malware_id_ to the stores_to_look. | |
464 MatchedHashPrefixMap matched_hash_prefix_map; | |
465 v4_database_->GetStoresMatchingFullHash("anything", stores_to_look, | |
466 &matched_hash_prefix_map); | |
467 EXPECT_EQ(1u, matched_hash_prefix_map.size()); | |
468 EXPECT_FALSE(matched_hash_prefix_map[linux_malware_id_].empty()); | |
469 } | |
470 | |
349 } // namespace safe_browsing | 471 } // namespace safe_browsing |
OLD | NEW |