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