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 <string> | 5 #include <string> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
11 #include "chrome/browser/safe_browsing/local_database_manager.h" | 11 #include "chrome/browser/safe_browsing/local_database_manager.h" |
12 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 12 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
13 #include "content/public/test/test_browser_thread_bundle.h" | 13 #include "content/public/test/test_browser_thread_bundle.h" |
14 #include "content/public/test/test_utils.h" | 14 #include "content/public/test/test_utils.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "testing/platform_test.h" | 16 #include "testing/platform_test.h" |
17 #include "url/gurl.h" | 17 #include "url/gurl.h" |
18 | 18 |
19 using content::TestBrowserThreadBundle; | 19 using content::TestBrowserThreadBundle; |
20 | 20 |
| 21 namespace safe_browsing { |
| 22 |
21 namespace { | 23 namespace { |
22 | 24 |
23 class TestClient : public SafeBrowsingDatabaseManager::Client { | 25 class TestClient : public SafeBrowsingDatabaseManager::Client { |
24 public: | 26 public: |
25 TestClient() {} | 27 TestClient() {} |
26 ~TestClient() override {} | 28 ~TestClient() override {} |
27 | 29 |
28 void OnCheckBrowseUrlResult(const GURL& url, | 30 void OnCheckBrowseUrlResult(const GURL& url, |
29 SBThreatType threat_type, | 31 SBThreatType threat_type, |
30 const std::string& metadata) override {} | 32 const std::string& metadata) override {} |
31 | 33 |
32 void OnCheckDownloadUrlResult(const std::vector<GURL>& url_chain, | 34 void OnCheckDownloadUrlResult(const std::vector<GURL>& url_chain, |
33 SBThreatType threat_type) override {} | 35 SBThreatType threat_type) override {} |
34 | 36 |
35 private: | 37 private: |
36 DISALLOW_COPY_AND_ASSIGN(TestClient); | 38 DISALLOW_COPY_AND_ASSIGN(TestClient); |
37 }; | 39 }; |
38 | 40 |
39 } // namespace | 41 } // namespace |
40 | 42 |
41 class SafeBrowsingDatabaseManagerTest : public PlatformTest { | 43 class SafeBrowsingDatabaseManagerTest : public PlatformTest { |
42 public: | 44 public: |
43 bool RunSBHashTest(const safe_browsing::ListType list_type, | 45 bool RunSBHashTest(const ListType list_type, |
44 const std::vector<SBThreatType>& expected_threats, | 46 const std::vector<SBThreatType>& expected_threats, |
45 const std::string& result_list); | 47 const std::string& result_list); |
46 | 48 |
47 private: | 49 private: |
48 TestBrowserThreadBundle thread_bundle_; | 50 TestBrowserThreadBundle thread_bundle_; |
49 }; | 51 }; |
50 | 52 |
51 bool SafeBrowsingDatabaseManagerTest::RunSBHashTest( | 53 bool SafeBrowsingDatabaseManagerTest::RunSBHashTest( |
52 const safe_browsing::ListType list_type, | 54 const ListType list_type, |
53 const std::vector<SBThreatType>& expected_threats, | 55 const std::vector<SBThreatType>& expected_threats, |
54 const std::string& result_list) { | 56 const std::string& result_list) { |
55 scoped_refptr<SafeBrowsingService> sb_service_( | 57 scoped_refptr<SafeBrowsingService> sb_service_( |
56 SafeBrowsingService::CreateSafeBrowsingService()); | 58 SafeBrowsingService::CreateSafeBrowsingService()); |
57 scoped_refptr<LocalSafeBrowsingDatabaseManager> db_manager_( | 59 scoped_refptr<LocalSafeBrowsingDatabaseManager> db_manager_( |
58 new LocalSafeBrowsingDatabaseManager(sb_service_)); | 60 new LocalSafeBrowsingDatabaseManager(sb_service_)); |
59 const SBFullHash same_full_hash = {}; | 61 const SBFullHash same_full_hash = {}; |
60 | 62 |
61 LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck* check = | 63 LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck* check = |
62 new LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck( | 64 new LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck( |
63 std::vector<GURL>(), | 65 std::vector<GURL>(), std::vector<SBFullHash>(1, same_full_hash), NULL, |
64 std::vector<SBFullHash>(1, same_full_hash), | 66 list_type, expected_threats); |
65 NULL, | |
66 list_type, | |
67 expected_threats); | |
68 db_manager_->checks_.insert(check); | 67 db_manager_->checks_.insert(check); |
69 | 68 |
70 const SBFullHashResult full_hash_result = { | 69 const SBFullHashResult full_hash_result = {same_full_hash, |
71 same_full_hash, | 70 GetListId(result_list)}; |
72 safe_browsing::GetListId(result_list) | |
73 }; | |
74 | 71 |
75 std::vector<SBFullHashResult> fake_results(1, full_hash_result); | 72 std::vector<SBFullHashResult> fake_results(1, full_hash_result); |
76 bool result = db_manager_->HandleOneCheck(check, fake_results); | 73 bool result = db_manager_->HandleOneCheck(check, fake_results); |
77 db_manager_->checks_.erase(check); | 74 db_manager_->checks_.erase(check); |
78 delete check; | 75 delete check; |
79 return result; | 76 return result; |
80 } | 77 } |
81 | 78 |
82 TEST_F(SafeBrowsingDatabaseManagerTest, CheckCorrespondsListType) { | 79 TEST_F(SafeBrowsingDatabaseManagerTest, CheckCorrespondsListType) { |
83 std::vector<SBThreatType> malware_threat(1, | 80 std::vector<SBThreatType> malware_threat(1, |
84 SB_THREAT_TYPE_BINARY_MALWARE_URL); | 81 SB_THREAT_TYPE_BINARY_MALWARE_URL); |
85 EXPECT_FALSE(RunSBHashTest(safe_browsing::BINURL, | 82 EXPECT_FALSE(RunSBHashTest(BINURL, malware_threat, kMalwareList)); |
86 malware_threat, | 83 EXPECT_TRUE(RunSBHashTest(BINURL, malware_threat, kBinUrlList)); |
87 safe_browsing::kMalwareList)); | |
88 EXPECT_TRUE(RunSBHashTest(safe_browsing::BINURL, | |
89 malware_threat, | |
90 safe_browsing::kBinUrlList)); | |
91 | 84 |
92 // Check for multiple threats | 85 // Check for multiple threats |
93 std::vector<SBThreatType> multiple_threats; | 86 std::vector<SBThreatType> multiple_threats; |
94 multiple_threats.push_back(SB_THREAT_TYPE_URL_MALWARE); | 87 multiple_threats.push_back(SB_THREAT_TYPE_URL_MALWARE); |
95 multiple_threats.push_back(SB_THREAT_TYPE_URL_PHISHING); | 88 multiple_threats.push_back(SB_THREAT_TYPE_URL_PHISHING); |
96 EXPECT_FALSE(RunSBHashTest(safe_browsing::MALWARE, | 89 EXPECT_FALSE(RunSBHashTest(MALWARE, multiple_threats, kBinUrlList)); |
97 multiple_threats, | 90 EXPECT_TRUE(RunSBHashTest(MALWARE, multiple_threats, kMalwareList)); |
98 safe_browsing::kBinUrlList)); | |
99 EXPECT_TRUE(RunSBHashTest(safe_browsing::MALWARE, | |
100 multiple_threats, | |
101 safe_browsing::kMalwareList)); | |
102 } | 91 } |
103 | 92 |
104 TEST_F(SafeBrowsingDatabaseManagerTest, GetUrlSeverestThreatType) { | 93 TEST_F(SafeBrowsingDatabaseManagerTest, GetUrlSeverestThreatType) { |
105 std::vector<SBFullHashResult> full_hashes; | 94 std::vector<SBFullHashResult> full_hashes; |
106 | 95 |
107 const GURL kMalwareUrl("http://www.malware.com/page.html"); | 96 const GURL kMalwareUrl("http://www.malware.com/page.html"); |
108 const GURL kPhishingUrl("http://www.phishing.com/page.html"); | 97 const GURL kPhishingUrl("http://www.phishing.com/page.html"); |
109 const GURL kUnwantedUrl("http://www.unwanted.com/page.html"); | 98 const GURL kUnwantedUrl("http://www.unwanted.com/page.html"); |
110 const GURL kUnwantedAndMalwareUrl( | 99 const GURL kUnwantedAndMalwareUrl( |
111 "http://www.unwantedandmalware.com/page.html"); | 100 "http://www.unwantedandmalware.com/page.html"); |
112 const GURL kSafeUrl("http://www.safe.com/page.html"); | 101 const GURL kSafeUrl("http://www.safe.com/page.html"); |
113 | 102 |
114 const SBFullHash kMalwareHostHash = | 103 const SBFullHash kMalwareHostHash = SBFullHashForString("malware.com/"); |
115 safe_browsing::SBFullHashForString("malware.com/"); | 104 const SBFullHash kPhishingHostHash = SBFullHashForString("phishing.com/"); |
116 const SBFullHash kPhishingHostHash = | 105 const SBFullHash kUnwantedHostHash = SBFullHashForString("unwanted.com/"); |
117 safe_browsing::SBFullHashForString("phishing.com/"); | |
118 const SBFullHash kUnwantedHostHash = | |
119 safe_browsing::SBFullHashForString("unwanted.com/"); | |
120 const SBFullHash kUnwantedAndMalwareHostHash = | 106 const SBFullHash kUnwantedAndMalwareHostHash = |
121 safe_browsing::SBFullHashForString("unwantedandmalware.com/"); | 107 SBFullHashForString("unwantedandmalware.com/"); |
122 const SBFullHash kSafeHostHash = | 108 const SBFullHash kSafeHostHash = SBFullHashForString("www.safe.com/"); |
123 safe_browsing::SBFullHashForString("www.safe.com/"); | |
124 | 109 |
125 { | 110 { |
126 SBFullHashResult full_hash; | 111 SBFullHashResult full_hash; |
127 full_hash.hash = kMalwareHostHash; | 112 full_hash.hash = kMalwareHostHash; |
128 full_hash.list_id = static_cast<int>(safe_browsing::MALWARE); | 113 full_hash.list_id = static_cast<int>(MALWARE); |
129 full_hashes.push_back(full_hash); | 114 full_hashes.push_back(full_hash); |
130 } | 115 } |
131 | 116 |
132 { | 117 { |
133 SBFullHashResult full_hash; | 118 SBFullHashResult full_hash; |
134 full_hash.hash = kPhishingHostHash; | 119 full_hash.hash = kPhishingHostHash; |
135 full_hash.list_id = static_cast<int>(safe_browsing::PHISH); | 120 full_hash.list_id = static_cast<int>(PHISH); |
136 full_hashes.push_back(full_hash); | 121 full_hashes.push_back(full_hash); |
137 } | 122 } |
138 | 123 |
139 { | 124 { |
140 SBFullHashResult full_hash; | 125 SBFullHashResult full_hash; |
141 full_hash.hash = kUnwantedHostHash; | 126 full_hash.hash = kUnwantedHostHash; |
142 full_hash.list_id = static_cast<int>(safe_browsing::UNWANTEDURL); | 127 full_hash.list_id = static_cast<int>(UNWANTEDURL); |
143 full_hashes.push_back(full_hash); | 128 full_hashes.push_back(full_hash); |
144 } | 129 } |
145 | 130 |
146 { | 131 { |
147 // Add both MALWARE and UNWANTEDURL list IDs for | 132 // Add both MALWARE and UNWANTEDURL list IDs for |
148 // kUnwantedAndMalwareHostHash. | 133 // kUnwantedAndMalwareHostHash. |
149 SBFullHashResult full_hash_malware; | 134 SBFullHashResult full_hash_malware; |
150 full_hash_malware.hash = kUnwantedAndMalwareHostHash; | 135 full_hash_malware.hash = kUnwantedAndMalwareHostHash; |
151 full_hash_malware.list_id = static_cast<int>(safe_browsing::MALWARE); | 136 full_hash_malware.list_id = static_cast<int>(MALWARE); |
152 full_hashes.push_back(full_hash_malware); | 137 full_hashes.push_back(full_hash_malware); |
153 | 138 |
154 SBFullHashResult full_hash_unwanted; | 139 SBFullHashResult full_hash_unwanted; |
155 full_hash_unwanted.hash = kUnwantedAndMalwareHostHash; | 140 full_hash_unwanted.hash = kUnwantedAndMalwareHostHash; |
156 full_hash_unwanted.list_id = static_cast<int>(safe_browsing::UNWANTEDURL); | 141 full_hash_unwanted.list_id = static_cast<int>(UNWANTEDURL); |
157 full_hashes.push_back(full_hash_unwanted); | 142 full_hashes.push_back(full_hash_unwanted); |
158 } | 143 } |
159 | 144 |
160 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, | 145 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, |
161 LocalSafeBrowsingDatabaseManager::GetHashSeverestThreatType( | 146 LocalSafeBrowsingDatabaseManager::GetHashSeverestThreatType( |
162 kMalwareHostHash, full_hashes)); | 147 kMalwareHostHash, full_hashes)); |
163 | 148 |
164 EXPECT_EQ(SB_THREAT_TYPE_URL_PHISHING, | 149 EXPECT_EQ(SB_THREAT_TYPE_URL_PHISHING, |
165 LocalSafeBrowsingDatabaseManager::GetHashSeverestThreatType( | 150 LocalSafeBrowsingDatabaseManager::GetHashSeverestThreatType( |
166 kPhishingHostHash, full_hashes)); | 151 kPhishingHostHash, full_hashes)); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 db_manager->CheckExtensionIDs(extension_ids, &client); | 210 db_manager->CheckExtensionIDs(extension_ids, &client); |
226 | 211 |
227 // Stop the service without first flushing above tasks. | 212 // Stop the service without first flushing above tasks. |
228 db_manager->StopOnIOThread(false); | 213 db_manager->StopOnIOThread(false); |
229 | 214 |
230 // Now run posted tasks, whish should include the extension check which has | 215 // Now run posted tasks, whish should include the extension check which has |
231 // been posted to the safe browsing task runner. This should not crash. | 216 // been posted to the safe browsing task runner. This should not crash. |
232 content::RunAllBlockingPoolTasksUntilIdle(); | 217 content::RunAllBlockingPoolTasksUntilIdle(); |
233 base::RunLoop().RunUntilIdle(); | 218 base::RunLoop().RunUntilIdle(); |
234 } | 219 } |
| 220 |
| 221 } // namespace safe_browsing |
OLD | NEW |