| 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 <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 using content::WebContents; | 52 using content::WebContents; |
| 53 using ::testing::_; | 53 using ::testing::_; |
| 54 using ::testing::Mock; | 54 using ::testing::Mock; |
| 55 using ::testing::StrictMock; | 55 using ::testing::StrictMock; |
| 56 | 56 |
| 57 namespace { | 57 namespace { |
| 58 | 58 |
| 59 void InvokeFullHashCallback( | 59 void InvokeFullHashCallback( |
| 60 SafeBrowsingProtocolManager::FullHashCallback callback, | 60 SafeBrowsingProtocolManager::FullHashCallback callback, |
| 61 const std::vector<SBFullHashResult>& result) { | 61 const std::vector<SBFullHashResult>& result) { |
| 62 callback.Run(result, true); | 62 callback.Run(result, base::TimeDelta::FromMinutes(45)); |
| 63 } | 63 } |
| 64 | 64 |
| 65 } // namespace | 65 } // namespace |
| 66 | 66 |
| 67 // A SafeBrowingDatabase class that allows us to inject the malicious URLs. | 67 // A SafeBrowingDatabase class that allows us to inject the malicious URLs. |
| 68 class TestSafeBrowsingDatabase : public SafeBrowsingDatabase { | 68 class TestSafeBrowsingDatabase : public SafeBrowsingDatabase { |
| 69 public: | 69 public: |
| 70 TestSafeBrowsingDatabase() {} | 70 TestSafeBrowsingDatabase() {} |
| 71 | 71 |
| 72 virtual ~TestSafeBrowsingDatabase() {} | 72 virtual ~TestSafeBrowsingDatabase() {} |
| 73 | 73 |
| 74 // Initializes the database with the given filename. | 74 // Initializes the database with the given filename. |
| 75 virtual void Init(const base::FilePath& filename) OVERRIDE {} | 75 virtual void Init(const base::FilePath& filename) OVERRIDE {} |
| 76 | 76 |
| 77 // Deletes the current database and creates a new one. | 77 // Deletes the current database and creates a new one. |
| 78 virtual bool ResetDatabase() OVERRIDE { | 78 virtual bool ResetDatabase() OVERRIDE { |
| 79 badurls_.clear(); | 79 badurls_.clear(); |
| 80 return true; | 80 return true; |
| 81 } | 81 } |
| 82 | 82 |
| 83 // Called on the IO thread to check if the given URL is safe or not. If we | 83 // Called on the IO thread to check if the given URL is safe or not. If we |
| 84 // can synchronously determine that the URL is safe, CheckUrl returns true, | 84 // can synchronously determine that the URL is safe, CheckUrl returns true, |
| 85 // otherwise it returns false. | 85 // otherwise it returns false. |
| 86 virtual bool ContainsBrowseUrl(const GURL& url, | 86 virtual bool ContainsBrowseUrl(const GURL& url, |
| 87 std::string* matching_list, | |
| 88 std::vector<SBPrefix>* prefix_hits, | 87 std::vector<SBPrefix>* prefix_hits, |
| 89 std::vector<SBFullHashResult>* full_hits, | 88 std::vector<SBFullHashResult>* cache_hits) |
| 90 base::Time last_update) OVERRIDE { | 89 OVERRIDE { |
| 90 cache_hits->clear(); |
| 91 std::vector<GURL> urls(1, url); | 91 std::vector<GURL> urls(1, url); |
| 92 return ContainsUrl(safe_browsing_util::kMalwareList, | 92 return ContainsUrl(safe_browsing_util::kMalwareList, |
| 93 safe_browsing_util::kPhishingList, | 93 safe_browsing_util::kPhishingList, |
| 94 urls, prefix_hits, full_hits); | 94 urls, |
| 95 prefix_hits); |
| 95 } | 96 } |
| 96 virtual bool ContainsDownloadUrl( | 97 virtual bool ContainsDownloadUrl( |
| 97 const std::vector<GURL>& urls, | 98 const std::vector<GURL>& urls, |
| 98 std::vector<SBPrefix>* prefix_hits) OVERRIDE { | 99 std::vector<SBPrefix>* prefix_hits) OVERRIDE { |
| 99 std::vector<SBFullHashResult> full_hits; | |
| 100 bool found = ContainsUrl(safe_browsing_util::kBinUrlList, | 100 bool found = ContainsUrl(safe_browsing_util::kBinUrlList, |
| 101 safe_browsing_util::kBinUrlList, | 101 safe_browsing_util::kBinUrlList, |
| 102 urls, prefix_hits, &full_hits); | 102 urls, |
| 103 prefix_hits); |
| 103 if (!found) | 104 if (!found) |
| 104 return false; | 105 return false; |
| 105 DCHECK_LE(1U, prefix_hits->size()); | 106 DCHECK_LE(1U, prefix_hits->size()); |
| 106 return true; | 107 return true; |
| 107 } | 108 } |
| 108 virtual bool ContainsCsdWhitelistedUrl(const GURL& url) OVERRIDE { | 109 virtual bool ContainsCsdWhitelistedUrl(const GURL& url) OVERRIDE { |
| 109 return true; | 110 return true; |
| 110 } | 111 } |
| 111 virtual bool ContainsDownloadWhitelistedString( | 112 virtual bool ContainsDownloadWhitelistedString( |
| 112 const std::string& str) OVERRIDE { | 113 const std::string& str) OVERRIDE { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 135 ADD_FAILURE() << "Not implemented."; | 136 ADD_FAILURE() << "Not implemented."; |
| 136 } | 137 } |
| 137 virtual void DeleteChunks( | 138 virtual void DeleteChunks( |
| 138 const std::vector<SBChunkDelete>& chunk_deletes) OVERRIDE { | 139 const std::vector<SBChunkDelete>& chunk_deletes) OVERRIDE { |
| 139 ADD_FAILURE() << "Not implemented."; | 140 ADD_FAILURE() << "Not implemented."; |
| 140 } | 141 } |
| 141 virtual void UpdateFinished(bool update_succeeded) OVERRIDE { | 142 virtual void UpdateFinished(bool update_succeeded) OVERRIDE { |
| 142 ADD_FAILURE() << "Not implemented."; | 143 ADD_FAILURE() << "Not implemented."; |
| 143 } | 144 } |
| 144 virtual void CacheHashResults(const std::vector<SBPrefix>& prefixes, | 145 virtual void CacheHashResults(const std::vector<SBPrefix>& prefixes, |
| 145 const std::vector<SBFullHashResult>& full_hits) OVERRIDE { | 146 const std::vector<SBFullHashResult>& full_hits, |
| 147 const base::TimeDelta& cache_lifetime) |
| 148 OVERRIDE { |
| 146 // Do nothing for the cache. | 149 // Do nothing for the cache. |
| 147 } | 150 } |
| 148 virtual bool IsMalwareIPMatchKillSwitchOn() OVERRIDE { | 151 virtual bool IsMalwareIPMatchKillSwitchOn() OVERRIDE { |
| 149 return false; | 152 return false; |
| 150 } | 153 } |
| 151 | 154 |
| 152 // Fill up the database with test URL. | 155 // Fill up the database with test URL. |
| 153 void AddUrl(const GURL& url, | 156 void AddUrl(const GURL& url, |
| 154 const std::string& list_name, | 157 const std::string& list_name, |
| 155 const std::vector<SBPrefix>& prefix_hits, | 158 const std::vector<SBPrefix>& prefix_hits) { |
| 156 const std::vector<SBFullHashResult>& full_hits) { | |
| 157 badurls_[url.spec()].list_name = list_name; | 159 badurls_[url.spec()].list_name = list_name; |
| 158 badurls_[url.spec()].prefix_hits = prefix_hits; | 160 badurls_[url.spec()].prefix_hits = prefix_hits; |
| 159 badurls_[url.spec()].full_hits = full_hits; | |
| 160 } | 161 } |
| 161 | 162 |
| 162 // Fill up the database with test hash digest. | 163 // Fill up the database with test hash digest. |
| 163 void AddDownloadPrefix(SBPrefix prefix) { | 164 void AddDownloadPrefix(SBPrefix prefix) { |
| 164 download_digest_prefix_.insert(prefix); | 165 download_digest_prefix_.insert(prefix); |
| 165 } | 166 } |
| 166 | 167 |
| 167 private: | 168 private: |
| 168 struct Hits { | 169 struct Hits { |
| 169 std::string list_name; | 170 std::string list_name; |
| 170 std::vector<SBPrefix> prefix_hits; | 171 std::vector<SBPrefix> prefix_hits; |
| 171 std::vector<SBFullHashResult> full_hits; | |
| 172 }; | 172 }; |
| 173 | 173 |
| 174 bool ContainsUrl(const std::string& list_name0, | 174 bool ContainsUrl(const std::string& list_name0, |
| 175 const std::string& list_name1, | 175 const std::string& list_name1, |
| 176 const std::vector<GURL>& urls, | 176 const std::vector<GURL>& urls, |
| 177 std::vector<SBPrefix>* prefix_hits, | 177 std::vector<SBPrefix>* prefix_hits) { |
| 178 std::vector<SBFullHashResult>* full_hits) { | |
| 179 bool hit = false; | 178 bool hit = false; |
| 180 for (size_t i = 0; i < urls.size(); ++i) { | 179 for (size_t i = 0; i < urls.size(); ++i) { |
| 181 const GURL& url = urls[i]; | 180 const GURL& url = urls[i]; |
| 182 base::hash_map<std::string, Hits>::const_iterator | 181 base::hash_map<std::string, Hits>::const_iterator |
| 183 badurls_it = badurls_.find(url.spec()); | 182 badurls_it = badurls_.find(url.spec()); |
| 184 | 183 |
| 185 if (badurls_it == badurls_.end()) | 184 if (badurls_it == badurls_.end()) |
| 186 continue; | 185 continue; |
| 187 | 186 |
| 188 if (badurls_it->second.list_name == list_name0 || | 187 if (badurls_it->second.list_name == list_name0 || |
| 189 badurls_it->second.list_name == list_name1) { | 188 badurls_it->second.list_name == list_name1) { |
| 190 prefix_hits->insert(prefix_hits->end(), | 189 prefix_hits->insert(prefix_hits->end(), |
| 191 badurls_it->second.prefix_hits.begin(), | 190 badurls_it->second.prefix_hits.begin(), |
| 192 badurls_it->second.prefix_hits.end()); | 191 badurls_it->second.prefix_hits.end()); |
| 193 full_hits->insert(full_hits->end(), | |
| 194 badurls_it->second.full_hits.begin(), | |
| 195 badurls_it->second.full_hits.end()); | |
| 196 hit = true; | 192 hit = true; |
| 197 } | 193 } |
| 198 | 194 |
| 199 } | 195 } |
| 200 return hit; | 196 return hit; |
| 201 } | 197 } |
| 202 | 198 |
| 203 base::hash_map<std::string, Hits> badurls_; | 199 base::hash_map<std::string, Hits> badurls_; |
| 204 base::hash_set<SBPrefix> download_digest_prefix_; | 200 base::hash_set<SBPrefix> download_digest_prefix_; |
| 205 }; | 201 }; |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 | 330 |
| 335 static void GenUrlFullhashResult(const GURL& url, | 331 static void GenUrlFullhashResult(const GURL& url, |
| 336 const std::string& list_name, | 332 const std::string& list_name, |
| 337 int add_chunk_id, | 333 int add_chunk_id, |
| 338 SBFullHashResult* full_hash) { | 334 SBFullHashResult* full_hash) { |
| 339 std::string host; | 335 std::string host; |
| 340 std::string path; | 336 std::string path; |
| 341 safe_browsing_util::CanonicalizeUrl(url, &host, &path, NULL); | 337 safe_browsing_util::CanonicalizeUrl(url, &host, &path, NULL); |
| 342 full_hash->hash = SBFullHashForString(host + path); | 338 full_hash->hash = SBFullHashForString(host + path); |
| 343 full_hash->list_name = list_name; | 339 full_hash->list_name = list_name; |
| 344 full_hash->add_chunk_id = add_chunk_id; | |
| 345 } | 340 } |
| 346 | 341 |
| 347 static void GenDigestFullhashResult(const std::string& full_digest, | 342 static void GenDigestFullhashResult(const std::string& full_digest, |
| 348 const std::string& list_name, | 343 const std::string& list_name, |
| 349 int add_chunk_id, | 344 int add_chunk_id, |
| 350 SBFullHashResult* full_hash) { | 345 SBFullHashResult* full_hash) { |
| 351 full_hash->hash = safe_browsing_util::StringToSBFullHash(full_digest); | 346 full_hash->hash = safe_browsing_util::StringToSBFullHash(full_digest); |
| 352 full_hash->list_name = list_name; | 347 full_hash->list_name = list_name; |
| 353 full_hash->add_chunk_id = add_chunk_id; | |
| 354 } | 348 } |
| 355 | 349 |
| 356 virtual void SetUp() { | 350 virtual void SetUp() { |
| 357 // InProcessBrowserTest::SetUp() instantiates SafebrowsingService and | 351 // InProcessBrowserTest::SetUp() instantiates SafebrowsingService and |
| 358 // RegisterFactory has to be called before SafeBrowsingService is created. | 352 // RegisterFactory has to be called before SafeBrowsingService is created. |
| 359 SafeBrowsingDatabase::RegisterFactory(&db_factory_); | 353 SafeBrowsingDatabase::RegisterFactory(&db_factory_); |
| 360 SafeBrowsingProtocolManager::RegisterFactory(&pm_factory_); | 354 SafeBrowsingProtocolManager::RegisterFactory(&pm_factory_); |
| 361 InProcessBrowserTest::SetUp(); | 355 InProcessBrowserTest::SetUp(); |
| 362 } | 356 } |
| 363 | 357 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 384 // This will setup the "url" prefix in database and prepare protocol manager | 378 // This will setup the "url" prefix in database and prepare protocol manager |
| 385 // to response with |full_hash| for get full hash request. | 379 // to response with |full_hash| for get full hash request. |
| 386 void SetupResponseForUrl(const GURL& url, const SBFullHashResult& full_hash) { | 380 void SetupResponseForUrl(const GURL& url, const SBFullHashResult& full_hash) { |
| 387 std::vector<SBPrefix> prefix_hits; | 381 std::vector<SBPrefix> prefix_hits; |
| 388 prefix_hits.push_back(full_hash.hash.prefix); | 382 prefix_hits.push_back(full_hash.hash.prefix); |
| 389 | 383 |
| 390 // Make sure the full hits is empty unless we need to test the | 384 // Make sure the full hits is empty unless we need to test the |
| 391 // full hash is hit in database's local cache. | 385 // full hash is hit in database's local cache. |
| 392 std::vector<SBFullHashResult> empty_full_hits; | 386 std::vector<SBFullHashResult> empty_full_hits; |
| 393 TestSafeBrowsingDatabase* db = db_factory_.GetDb(); | 387 TestSafeBrowsingDatabase* db = db_factory_.GetDb(); |
| 394 db->AddUrl(url, full_hash.list_name, prefix_hits, empty_full_hits); | 388 db->AddUrl(url, full_hash.list_name, prefix_hits); |
| 395 | 389 |
| 396 TestProtocolManager* pm = pm_factory_.GetProtocolManager(); | 390 TestProtocolManager* pm = pm_factory_.GetProtocolManager(); |
| 397 pm->SetGetFullHashResponse(full_hash); | 391 pm->SetGetFullHashResponse(full_hash); |
| 398 } | 392 } |
| 399 | 393 |
| 400 // This will setup the binary digest prefix in database and prepare protocol | 394 // This will setup the binary digest prefix in database and prepare protocol |
| 401 // manager to respond with the result hash. | 395 // manager to respond with the result hash. |
| 402 void SetupResponseForDigest(const std::string& digest, | 396 void SetupResponseForDigest(const std::string& digest, |
| 403 const SBFullHashResult& hash_result) { | 397 const SBFullHashResult& hash_result) { |
| 404 TestSafeBrowsingDatabase* db = db_factory_.GetDb(); | 398 TestSafeBrowsingDatabase* db = db_factory_.GetDb(); |
| (...skipping 568 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 973 content::WindowedNotificationObserver observer( | 967 content::WindowedNotificationObserver observer( |
| 974 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, | 968 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, |
| 975 content::Source<SafeBrowsingDatabaseManager>( | 969 content::Source<SafeBrowsingDatabaseManager>( |
| 976 sb_service_->database_manager().get())); | 970 sb_service_->database_manager().get())); |
| 977 BrowserThread::PostTask( | 971 BrowserThread::PostTask( |
| 978 BrowserThread::IO, | 972 BrowserThread::IO, |
| 979 FROM_HERE, | 973 FROM_HERE, |
| 980 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::ForceUpdate, this)); | 974 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::ForceUpdate, this)); |
| 981 observer.Wait(); | 975 observer.Wait(); |
| 982 } | 976 } |
| OLD | NEW |