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