| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // This test creates a safebrowsing service using test safebrowsing database | 5 // This test creates a safebrowsing service using test safebrowsing database |
| 6 // and a test protocol manager. It is used to test logics in safebrowsing | 6 // and a test protocol manager. It is used to test logics in safebrowsing |
| 7 // service. | 7 // service. |
| 8 | 8 |
| 9 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 9 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 10 | 10 |
| 11 #include <map> |
| 12 #include <set> |
| 11 #include <utility> | 13 #include <utility> |
| 12 | 14 |
| 13 #include "base/bind.h" | 15 #include "base/bind.h" |
| 14 #include "base/command_line.h" | 16 #include "base/command_line.h" |
| 15 #include "base/files/file_path.h" | 17 #include "base/files/file_path.h" |
| 16 #include "base/files/scoped_temp_dir.h" | 18 #include "base/files/scoped_temp_dir.h" |
| 17 #include "base/macros.h" | 19 #include "base/macros.h" |
| 18 #include "base/memory/ptr_util.h" | 20 #include "base/memory/ptr_util.h" |
| 19 #include "base/memory/ref_counted.h" | 21 #include "base/memory/ref_counted.h" |
| 20 #include "base/message_loop/message_loop.h" | 22 #include "base/message_loop/message_loop.h" |
| (...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 std::vector<int> list_ids; | 298 std::vector<int> list_ids; |
| 297 std::vector<SBPrefix> prefix_hits; | 299 std::vector<SBPrefix> prefix_hits; |
| 298 }; | 300 }; |
| 299 | 301 |
| 300 bool ContainsUrl(int list_id0, | 302 bool ContainsUrl(int list_id0, |
| 301 int list_id1, | 303 int list_id1, |
| 302 const std::vector<GURL>& urls, | 304 const std::vector<GURL>& urls, |
| 303 std::vector<SBPrefix>* prefix_hits) { | 305 std::vector<SBPrefix>* prefix_hits) { |
| 304 bool hit = false; | 306 bool hit = false; |
| 305 for (const GURL& url : urls) { | 307 for (const GURL& url : urls) { |
| 306 base::hash_map<std::string, Hits>::const_iterator badurls_it = | 308 const auto badurls_it = badurls_.find(url.spec()); |
| 307 badurls_.find(url.spec()); | |
| 308 | |
| 309 if (badurls_it == badurls_.end()) | 309 if (badurls_it == badurls_.end()) |
| 310 continue; | 310 continue; |
| 311 | 311 |
| 312 std::vector<int> list_ids_for_url = badurls_it->second.list_ids; | 312 const std::vector<int>& list_ids_for_url = badurls_it->second.list_ids; |
| 313 if (base::ContainsValue(list_ids_for_url, list_id0) || | 313 if (base::ContainsValue(list_ids_for_url, list_id0) || |
| 314 base::ContainsValue(list_ids_for_url, list_id1)) { | 314 base::ContainsValue(list_ids_for_url, list_id1)) { |
| 315 prefix_hits->insert(prefix_hits->end(), | 315 prefix_hits->insert(prefix_hits->end(), |
| 316 badurls_it->second.prefix_hits.begin(), | 316 badurls_it->second.prefix_hits.begin(), |
| 317 badurls_it->second.prefix_hits.end()); | 317 badurls_it->second.prefix_hits.end()); |
| 318 hit = true; | 318 hit = true; |
| 319 } | 319 } |
| 320 } | 320 } |
| 321 return hit; | 321 return hit; |
| 322 } | 322 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 342 if (entry.second == prefix && | 342 if (entry.second == prefix && |
| 343 (entry.first == list_id0 || entry.first == list_id1)) { | 343 (entry.first == list_id0 || entry.first == list_id1)) { |
| 344 prefix_hits->push_back(prefix); | 344 prefix_hits->push_back(prefix); |
| 345 hit = true; | 345 hit = true; |
| 346 } | 346 } |
| 347 } | 347 } |
| 348 } | 348 } |
| 349 return hit; | 349 return hit; |
| 350 } | 350 } |
| 351 | 351 |
| 352 base::hash_map<std::string, Hits> badurls_; | 352 std::map<std::string, Hits> badurls_; |
| 353 base::hash_set<std::pair<int, SBPrefix>> bad_prefixes_; | 353 std::set<std::pair<int, SBPrefix>> bad_prefixes_; |
| 354 base::hash_map<std::string, GURL> urls_by_hash_; | 354 std::map<std::string, GURL> urls_by_hash_; |
| 355 | 355 |
| 356 DISALLOW_COPY_AND_ASSIGN(TestSafeBrowsingDatabase); | 356 DISALLOW_COPY_AND_ASSIGN(TestSafeBrowsingDatabase); |
| 357 }; | 357 }; |
| 358 | 358 |
| 359 // Factory that creates TestSafeBrowsingDatabase instances. | 359 // Factory that creates TestSafeBrowsingDatabase instances. |
| 360 class TestSafeBrowsingDatabaseFactory : public SafeBrowsingDatabaseFactory { | 360 class TestSafeBrowsingDatabaseFactory : public SafeBrowsingDatabaseFactory { |
| 361 public: | 361 public: |
| 362 TestSafeBrowsingDatabaseFactory() : db_(nullptr) {} | 362 TestSafeBrowsingDatabaseFactory() : db_(nullptr) {} |
| 363 ~TestSafeBrowsingDatabaseFactory() override {} | 363 ~TestSafeBrowsingDatabaseFactory() override {} |
| 364 | 364 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 389 net::URLRequestContextGetter* request_context_getter, | 389 net::URLRequestContextGetter* request_context_getter, |
| 390 const SafeBrowsingProtocolConfig& config) | 390 const SafeBrowsingProtocolConfig& config) |
| 391 : SafeBrowsingProtocolManager(delegate, request_context_getter, config) { | 391 : SafeBrowsingProtocolManager(delegate, request_context_getter, config) { |
| 392 create_count_++; | 392 create_count_++; |
| 393 } | 393 } |
| 394 | 394 |
| 395 ~TestProtocolManager() override { delete_count_++; } | 395 ~TestProtocolManager() override { delete_count_++; } |
| 396 | 396 |
| 397 // This function is called when there is a prefix hit in local safebrowsing | 397 // This function is called when there is a prefix hit in local safebrowsing |
| 398 // database and safebrowsing service issues a get hash request to backends. | 398 // database and safebrowsing service issues a get hash request to backends. |
| 399 // We return a result from the prefilled full_hashes_ hash_map to simulate | 399 // We return a result from the prefilled full_hashes_ map to simulate |
| 400 // server's response. At the same time, latency is added to simulate real | 400 // server's response. At the same time, latency is added to simulate real |
| 401 // life network issues. | 401 // life network issues. |
| 402 void GetFullHash(const std::vector<SBPrefix>& prefixes, | 402 void GetFullHash(const std::vector<SBPrefix>& prefixes, |
| 403 SafeBrowsingProtocolManager::FullHashCallback callback, | 403 SafeBrowsingProtocolManager::FullHashCallback callback, |
| 404 bool is_download, | 404 bool is_download, |
| 405 ExtendedReportingLevel reporting_level) override { | 405 ExtendedReportingLevel reporting_level) override { |
| 406 BrowserThread::PostDelayedTask( | 406 BrowserThread::PostDelayedTask( |
| 407 BrowserThread::IO, FROM_HERE, | 407 BrowserThread::IO, FROM_HERE, |
| 408 base::BindOnce(InvokeFullHashCallback, callback, full_hashes_), delay_); | 408 base::BindOnce(InvokeFullHashCallback, callback, full_hashes_), delay_); |
| 409 } | 409 } |
| (...skipping 2066 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2476 } | 2476 } |
| 2477 | 2477 |
| 2478 INSTANTIATE_TEST_CASE_P( | 2478 INSTANTIATE_TEST_CASE_P( |
| 2479 MaybeSetMetadata, | 2479 MaybeSetMetadata, |
| 2480 V4SafeBrowsingServiceMetadataTest, | 2480 V4SafeBrowsingServiceMetadataTest, |
| 2481 testing::Values(ThreatPatternType::NONE, | 2481 testing::Values(ThreatPatternType::NONE, |
| 2482 ThreatPatternType::MALWARE_LANDING, | 2482 ThreatPatternType::MALWARE_LANDING, |
| 2483 ThreatPatternType::MALWARE_DISTRIBUTION)); | 2483 ThreatPatternType::MALWARE_DISTRIBUTION)); |
| 2484 | 2484 |
| 2485 } // namespace safe_browsing | 2485 } // namespace safe_browsing |
| OLD | NEW |