OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/safe_browsing/local_database_manager.h" | 5 #include "chrome/browser/safe_browsing/local_database_manager.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <string> | 10 #include <string> |
(...skipping 24 matching lines...) Expand all Loading... |
35 | 35 |
36 bool RunSBHashTest(const ListType list_type, | 36 bool RunSBHashTest(const ListType list_type, |
37 const std::vector<SBThreatType>& expected_threats, | 37 const std::vector<SBThreatType>& expected_threats, |
38 const std::vector<std::string>& result_lists); | 38 const std::vector<std::string>& result_lists); |
39 bool RunUrlTest( | 39 bool RunUrlTest( |
40 const GURL& url, ListType list_type, | 40 const GURL& url, ListType list_type, |
41 const std::vector<SBThreatType>& expected_threats, | 41 const std::vector<SBThreatType>& expected_threats, |
42 const std::vector<HostListPair>& host_list_results); | 42 const std::vector<HostListPair>& host_list_results); |
43 | 43 |
44 private: | 44 private: |
45 bool RunTest(LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck* check, | 45 bool RunTest(std::unique_ptr< |
| 46 LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck> check, |
46 const std::vector<SBFullHashResult>& hash_results); | 47 const std::vector<SBFullHashResult>& hash_results); |
47 | 48 |
48 TestBrowserThreadBundle thread_bundle_; | 49 TestBrowserThreadBundle thread_bundle_; |
49 }; | 50 }; |
50 | 51 |
51 bool LocalDatabaseManagerTest::RunSBHashTest( | 52 bool LocalDatabaseManagerTest::RunSBHashTest( |
52 const ListType list_type, | 53 const ListType list_type, |
53 const std::vector<SBThreatType>& expected_threats, | 54 const std::vector<SBThreatType>& expected_threats, |
54 const std::vector<std::string>& result_lists) { | 55 const std::vector<std::string>& result_lists) { |
55 const SBFullHash same_full_hash = {}; | 56 const SBFullHash same_full_hash = {}; |
56 std::unique_ptr<LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck> check( | 57 std::unique_ptr<LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck> check( |
57 new LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck( | 58 new LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck( |
58 std::vector<GURL>(), std::vector<SBFullHash>(1, same_full_hash), NULL, | 59 std::vector<GURL>(), std::vector<SBFullHash>(1, same_full_hash), NULL, |
59 list_type, expected_threats)); | 60 list_type, expected_threats)); |
60 | 61 |
61 std::vector<SBFullHashResult> fake_results; | 62 std::vector<SBFullHashResult> fake_results; |
62 for (const auto& result_list : result_lists) { | 63 for (const auto& result_list : result_lists) { |
63 const SBFullHashResult full_hash_result = {same_full_hash, | 64 const SBFullHashResult full_hash_result = {same_full_hash, |
64 GetListId(result_list)}; | 65 GetListId(result_list)}; |
65 fake_results.push_back(full_hash_result); | 66 fake_results.push_back(full_hash_result); |
66 } | 67 } |
67 return RunTest(check.get(), fake_results); | 68 return RunTest(std::move(check), fake_results); |
68 } | 69 } |
69 | 70 |
70 bool LocalDatabaseManagerTest::RunUrlTest( | 71 bool LocalDatabaseManagerTest::RunUrlTest( |
71 const GURL& url, ListType list_type, | 72 const GURL& url, ListType list_type, |
72 const std::vector<SBThreatType>& expected_threats, | 73 const std::vector<SBThreatType>& expected_threats, |
73 const std::vector<HostListPair>& host_list_results) { | 74 const std::vector<HostListPair>& host_list_results) { |
74 std::unique_ptr<LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck> check( | 75 std::unique_ptr<LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck> check( |
75 new LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck( | 76 new LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck( |
76 std::vector<GURL>(1, url), std::vector<SBFullHash>(), NULL, list_type, | 77 std::vector<GURL>(1, url), std::vector<SBFullHash>(), NULL, list_type, |
77 expected_threats)); | 78 expected_threats)); |
78 std::vector<SBFullHashResult> full_hash_results; | 79 std::vector<SBFullHashResult> full_hash_results; |
79 for (const auto& host_list : host_list_results) { | 80 for (const auto& host_list : host_list_results) { |
80 SBFullHashResult hash_result = | 81 SBFullHashResult hash_result = |
81 {SBFullHashForString(host_list.host), GetListId(host_list.list_type)}; | 82 {SBFullHashForString(host_list.host), GetListId(host_list.list_type)}; |
82 full_hash_results.push_back(hash_result); | 83 full_hash_results.push_back(hash_result); |
83 } | 84 } |
84 return RunTest(check.get(), full_hash_results); | 85 return RunTest(std::move(check), full_hash_results); |
85 } | 86 } |
86 | 87 |
87 bool LocalDatabaseManagerTest::RunTest( | 88 bool LocalDatabaseManagerTest::RunTest( |
88 LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck* check, | 89 std::unique_ptr<LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck> check, |
89 const std::vector<SBFullHashResult>& hash_results) { | 90 const std::vector<SBFullHashResult>& hash_results) { |
90 scoped_refptr<SafeBrowsingService> sb_service_( | 91 scoped_refptr<SafeBrowsingService> sb_service_( |
91 SafeBrowsingService::CreateSafeBrowsingService()); | 92 SafeBrowsingService::CreateSafeBrowsingService()); |
92 scoped_refptr<LocalSafeBrowsingDatabaseManager> db_manager_( | 93 scoped_refptr<LocalSafeBrowsingDatabaseManager> db_manager_( |
93 new LocalSafeBrowsingDatabaseManager(sb_service_)); | 94 new LocalSafeBrowsingDatabaseManager(sb_service_)); |
94 db_manager_->checks_.insert(check); | 95 LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck* check_ptr = check.get(); |
| 96 db_manager_->checks_[check_ptr] = std::move(check); |
95 | 97 |
96 bool result = db_manager_->HandleOneCheck(check, hash_results); | 98 bool result = db_manager_->HandleOneCheck(check_ptr, hash_results); |
97 db_manager_->checks_.erase(check); | 99 db_manager_->checks_.erase(check_ptr); |
98 return result; | 100 return result; |
99 } | 101 } |
100 | 102 |
101 TEST_F(LocalDatabaseManagerTest, CheckCorrespondsListTypeForHash) { | 103 TEST_F(LocalDatabaseManagerTest, CheckCorrespondsListTypeForHash) { |
102 std::vector<SBThreatType> malware_threat(1, | 104 std::vector<SBThreatType> malware_threat(1, |
103 SB_THREAT_TYPE_BINARY_MALWARE_URL); | 105 SB_THREAT_TYPE_BINARY_MALWARE_URL); |
104 EXPECT_FALSE(RunSBHashTest(BINURL, malware_threat, {kMalwareList})); | 106 EXPECT_FALSE(RunSBHashTest(BINURL, malware_threat, {kMalwareList})); |
105 EXPECT_TRUE(RunSBHashTest(BINURL, malware_threat, {kBinUrlList})); | 107 EXPECT_TRUE(RunSBHashTest(BINURL, malware_threat, {kBinUrlList})); |
106 | 108 |
107 // Check for multiple threats | 109 // Check for multiple threats |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 // Stop the service without first flushing above tasks. | 331 // Stop the service without first flushing above tasks. |
330 db_manager->StopOnIOThread(false); | 332 db_manager->StopOnIOThread(false); |
331 | 333 |
332 // Now run posted tasks, whish should include the extension check which has | 334 // Now run posted tasks, whish should include the extension check which has |
333 // been posted to the safe browsing task runner. This should not crash. | 335 // been posted to the safe browsing task runner. This should not crash. |
334 content::RunAllBlockingPoolTasksUntilIdle(); | 336 content::RunAllBlockingPoolTasksUntilIdle(); |
335 base::RunLoop().RunUntilIdle(); | 337 base::RunLoop().RunUntilIdle(); |
336 } | 338 } |
337 | 339 |
338 } // namespace safe_browsing | 340 } // namespace safe_browsing |
OLD | NEW |