| 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 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 std::vector<int> list_ids; | 295 std::vector<int> list_ids; |
| 294 std::vector<SBPrefix> prefix_hits; | 296 std::vector<SBPrefix> prefix_hits; |
| 295 }; | 297 }; |
| 296 | 298 |
| 297 bool ContainsUrl(int list_id0, | 299 bool ContainsUrl(int list_id0, |
| 298 int list_id1, | 300 int list_id1, |
| 299 const std::vector<GURL>& urls, | 301 const std::vector<GURL>& urls, |
| 300 std::vector<SBPrefix>* prefix_hits) { | 302 std::vector<SBPrefix>* prefix_hits) { |
| 301 bool hit = false; | 303 bool hit = false; |
| 302 for (const GURL& url : urls) { | 304 for (const GURL& url : urls) { |
| 303 base::hash_map<std::string, Hits>::const_iterator badurls_it = | 305 const auto badurls_it = badurls_.find(url.spec()); |
| 304 badurls_.find(url.spec()); | |
| 305 | |
| 306 if (badurls_it == badurls_.end()) | 306 if (badurls_it == badurls_.end()) |
| 307 continue; | 307 continue; |
| 308 | 308 |
| 309 std::vector<int> list_ids_for_url = badurls_it->second.list_ids; | 309 const std::vector<int>& list_ids_for_url = badurls_it->second.list_ids; |
| 310 if (base::ContainsValue(list_ids_for_url, list_id0) || | 310 if (base::ContainsValue(list_ids_for_url, list_id0) || |
| 311 base::ContainsValue(list_ids_for_url, list_id1)) { | 311 base::ContainsValue(list_ids_for_url, list_id1)) { |
| 312 prefix_hits->insert(prefix_hits->end(), | 312 prefix_hits->insert(prefix_hits->end(), |
| 313 badurls_it->second.prefix_hits.begin(), | 313 badurls_it->second.prefix_hits.begin(), |
| 314 badurls_it->second.prefix_hits.end()); | 314 badurls_it->second.prefix_hits.end()); |
| 315 hit = true; | 315 hit = true; |
| 316 } | 316 } |
| 317 } | 317 } |
| 318 return hit; | 318 return hit; |
| 319 } | 319 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 339 if (entry.second == prefix && | 339 if (entry.second == prefix && |
| 340 (entry.first == list_id0 || entry.first == list_id1)) { | 340 (entry.first == list_id0 || entry.first == list_id1)) { |
| 341 prefix_hits->push_back(prefix); | 341 prefix_hits->push_back(prefix); |
| 342 hit = true; | 342 hit = true; |
| 343 } | 343 } |
| 344 } | 344 } |
| 345 } | 345 } |
| 346 return hit; | 346 return hit; |
| 347 } | 347 } |
| 348 | 348 |
| 349 base::hash_map<std::string, Hits> badurls_; | 349 std::map<std::string, Hits> badurls_; |
| 350 base::hash_set<std::pair<int, SBPrefix>> bad_prefixes_; | 350 std::set<std::pair<int, SBPrefix>> bad_prefixes_; |
| 351 base::hash_map<std::string, GURL> urls_by_hash_; | 351 std::map<std::string, GURL> urls_by_hash_; |
| 352 | 352 |
| 353 DISALLOW_COPY_AND_ASSIGN(TestSafeBrowsingDatabase); | 353 DISALLOW_COPY_AND_ASSIGN(TestSafeBrowsingDatabase); |
| 354 }; | 354 }; |
| 355 | 355 |
| 356 // Factory that creates TestSafeBrowsingDatabase instances. | 356 // Factory that creates TestSafeBrowsingDatabase instances. |
| 357 class TestSafeBrowsingDatabaseFactory : public SafeBrowsingDatabaseFactory { | 357 class TestSafeBrowsingDatabaseFactory : public SafeBrowsingDatabaseFactory { |
| 358 public: | 358 public: |
| 359 TestSafeBrowsingDatabaseFactory() : db_(nullptr) {} | 359 TestSafeBrowsingDatabaseFactory() : db_(nullptr) {} |
| 360 ~TestSafeBrowsingDatabaseFactory() override {} | 360 ~TestSafeBrowsingDatabaseFactory() override {} |
| 361 | 361 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 386 net::URLRequestContextGetter* request_context_getter, | 386 net::URLRequestContextGetter* request_context_getter, |
| 387 const SafeBrowsingProtocolConfig& config) | 387 const SafeBrowsingProtocolConfig& config) |
| 388 : SafeBrowsingProtocolManager(delegate, request_context_getter, config) { | 388 : SafeBrowsingProtocolManager(delegate, request_context_getter, config) { |
| 389 create_count_++; | 389 create_count_++; |
| 390 } | 390 } |
| 391 | 391 |
| 392 ~TestProtocolManager() override { delete_count_++; } | 392 ~TestProtocolManager() override { delete_count_++; } |
| 393 | 393 |
| 394 // This function is called when there is a prefix hit in local safebrowsing | 394 // This function is called when there is a prefix hit in local safebrowsing |
| 395 // database and safebrowsing service issues a get hash request to backends. | 395 // database and safebrowsing service issues a get hash request to backends. |
| 396 // We return a result from the prefilled full_hashes_ hash_map to simulate | 396 // We return a result from the prefilled full_hashes_ map to simulate |
| 397 // server's response. At the same time, latency is added to simulate real | 397 // server's response. At the same time, latency is added to simulate real |
| 398 // life network issues. | 398 // life network issues. |
| 399 void GetFullHash(const std::vector<SBPrefix>& prefixes, | 399 void GetFullHash(const std::vector<SBPrefix>& prefixes, |
| 400 SafeBrowsingProtocolManager::FullHashCallback callback, | 400 SafeBrowsingProtocolManager::FullHashCallback callback, |
| 401 bool is_download, | 401 bool is_download, |
| 402 ExtendedReportingLevel reporting_level) override { | 402 ExtendedReportingLevel reporting_level) override { |
| 403 BrowserThread::PostDelayedTask( | 403 BrowserThread::PostDelayedTask( |
| 404 BrowserThread::IO, FROM_HERE, | 404 BrowserThread::IO, FROM_HERE, |
| 405 base::BindOnce(InvokeFullHashCallback, callback, full_hashes_), delay_); | 405 base::BindOnce(InvokeFullHashCallback, callback, full_hashes_), delay_); |
| 406 } | 406 } |
| (...skipping 1940 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2347 } | 2347 } |
| 2348 | 2348 |
| 2349 INSTANTIATE_TEST_CASE_P( | 2349 INSTANTIATE_TEST_CASE_P( |
| 2350 MaybeSetMetadata, | 2350 MaybeSetMetadata, |
| 2351 V4SafeBrowsingServiceMetadataTest, | 2351 V4SafeBrowsingServiceMetadataTest, |
| 2352 testing::Values(ThreatPatternType::NONE, | 2352 testing::Values(ThreatPatternType::NONE, |
| 2353 ThreatPatternType::MALWARE_LANDING, | 2353 ThreatPatternType::MALWARE_LANDING, |
| 2354 ThreatPatternType::MALWARE_DISTRIBUTION)); | 2354 ThreatPatternType::MALWARE_DISTRIBUTION)); |
| 2355 | 2355 |
| 2356 } // namespace safe_browsing | 2356 } // namespace safe_browsing |
| OLD | NEW |