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 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 #include "chrome/browser/ui/browser_navigator_params.h" | 44 #include "chrome/browser/ui/browser_navigator_params.h" |
45 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 45 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
46 #include "chrome/common/chrome_paths.h" | 46 #include "chrome/common/chrome_paths.h" |
47 #include "chrome/common/chrome_switches.h" | 47 #include "chrome/common/chrome_switches.h" |
48 #include "chrome/common/pref_names.h" | 48 #include "chrome/common/pref_names.h" |
49 #include "chrome/test/base/in_process_browser_test.h" | 49 #include "chrome/test/base/in_process_browser_test.h" |
50 #include "chrome/test/base/ui_test_utils.h" | 50 #include "chrome/test/base/ui_test_utils.h" |
51 #include "components/bookmarks/browser/startup_task_runner_service.h" | 51 #include "components/bookmarks/browser/startup_task_runner_service.h" |
52 #include "components/content_settings/core/browser/host_content_settings_map.h" | 52 #include "components/content_settings/core/browser/host_content_settings_map.h" |
53 #include "components/safe_browsing_db/database_manager.h" | 53 #include "components/safe_browsing_db/database_manager.h" |
| 54 #include "components/safe_browsing_db/util.h" |
54 #include "content/public/browser/interstitial_page.h" | 55 #include "content/public/browser/interstitial_page.h" |
55 #include "content/public/browser/navigation_entry.h" | 56 #include "content/public/browser/navigation_entry.h" |
56 #include "content/public/browser/render_frame_host.h" | 57 #include "content/public/browser/render_frame_host.h" |
57 #include "content/public/browser/web_contents.h" | 58 #include "content/public/browser/web_contents.h" |
58 #include "content/public/test/browser_test_utils.h" | 59 #include "content/public/test/browser_test_utils.h" |
59 #include "net/cookies/cookie_store.h" | 60 #include "net/cookies/cookie_store.h" |
60 #include "net/cookies/cookie_util.h" | 61 #include "net/cookies/cookie_util.h" |
61 #include "net/test/embedded_test_server/embedded_test_server.h" | 62 #include "net/test/embedded_test_server/embedded_test_server.h" |
62 #include "net/test/embedded_test_server/http_request.h" | 63 #include "net/test/embedded_test_server/http_request.h" |
63 #include "net/test/embedded_test_server/http_response.h" | 64 #include "net/test/embedded_test_server/http_response.h" |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 TestSafeBrowsingDatabase() {} | 189 TestSafeBrowsingDatabase() {} |
189 | 190 |
190 ~TestSafeBrowsingDatabase() override {} | 191 ~TestSafeBrowsingDatabase() override {} |
191 | 192 |
192 // Initializes the database with the given filename. | 193 // Initializes the database with the given filename. |
193 void Init(const base::FilePath& filename) override {} | 194 void Init(const base::FilePath& filename) override {} |
194 | 195 |
195 // Deletes the current database and creates a new one. | 196 // Deletes the current database and creates a new one. |
196 bool ResetDatabase() override { | 197 bool ResetDatabase() override { |
197 badurls_.clear(); | 198 badurls_.clear(); |
| 199 urls_by_hash_.clear(); |
198 return true; | 200 return true; |
199 } | 201 } |
200 | 202 |
201 // Called on the IO thread to check if the given URL is safe or not. If we | 203 // Called on the IO thread to check if the given URL is safe or not. If we |
202 // can synchronously determine that the URL is safe, CheckUrl returns true, | 204 // can synchronously determine that the URL is safe, CheckUrl returns true, |
203 // otherwise it returns false. | 205 // otherwise it returns false. |
204 bool ContainsBrowseUrl(const GURL& url, | 206 bool ContainsBrowseUrl(const GURL& url, |
205 std::vector<SBPrefix>* prefix_hits, | 207 std::vector<SBPrefix>* prefix_hits, |
206 std::vector<SBFullHashResult>* cache_hits) override { | 208 std::vector<SBFullHashResult>* cache_hits) override { |
207 cache_hits->clear(); | 209 cache_hits->clear(); |
208 return ContainsUrl(MALWARE, PHISH, std::vector<GURL>(1, url), prefix_hits); | 210 return ContainsUrl(MALWARE, PHISH, std::vector<GURL>(1, url), prefix_hits); |
209 } | 211 } |
| 212 |
| 213 bool ContainsBrowseHashes( |
| 214 const std::vector<SBFullHash>& full_hashes, |
| 215 std::vector<SBPrefix>* prefix_hits, |
| 216 std::vector<SBFullHashResult>* cache_hits) override { |
| 217 cache_hits->clear(); |
| 218 return ContainsUrl(MALWARE, PHISH, UrlsForHashes(full_hashes), prefix_hits); |
| 219 } |
| 220 |
210 bool ContainsUnwantedSoftwareUrl( | 221 bool ContainsUnwantedSoftwareUrl( |
211 const GURL& url, | 222 const GURL& url, |
212 std::vector<SBPrefix>* prefix_hits, | 223 std::vector<SBPrefix>* prefix_hits, |
213 std::vector<SBFullHashResult>* cache_hits) override { | 224 std::vector<SBFullHashResult>* cache_hits) override { |
214 cache_hits->clear(); | 225 cache_hits->clear(); |
215 return ContainsUrl(UNWANTEDURL, UNWANTEDURL, std::vector<GURL>(1, url), | 226 return ContainsUrl(UNWANTEDURL, UNWANTEDURL, std::vector<GURL>(1, url), |
216 prefix_hits); | 227 prefix_hits); |
217 } | 228 } |
| 229 |
| 230 bool ContainsUnwantedSoftwareHashes( |
| 231 const std::vector<SBFullHash>& full_hashes, |
| 232 std::vector<SBPrefix>* prefix_hits, |
| 233 std::vector<SBFullHashResult>* cache_hits) override { |
| 234 cache_hits->clear(); |
| 235 return ContainsUrl(UNWANTEDURL, UNWANTEDURL, UrlsForHashes(full_hashes), |
| 236 prefix_hits); |
| 237 } |
| 238 |
218 bool ContainsDownloadUrlPrefixes( | 239 bool ContainsDownloadUrlPrefixes( |
219 const std::vector<SBPrefix>& prefixes, | 240 const std::vector<SBPrefix>& prefixes, |
220 std::vector<SBPrefix>* prefix_hits) override { | 241 std::vector<SBPrefix>* prefix_hits) override { |
221 bool found = ContainsUrlPrefixes(BINURL, BINURL, prefixes, prefix_hits); | 242 bool found = ContainsUrlPrefixes(BINURL, BINURL, prefixes, prefix_hits); |
222 if (!found) | 243 if (!found) |
223 return false; | 244 return false; |
224 DCHECK_LE(1U, prefix_hits->size()); | 245 DCHECK_LE(1U, prefix_hits->size()); |
225 return true; | 246 return true; |
226 } | 247 } |
227 bool ContainsCsdWhitelistedUrl(const GURL& url) override { return true; } | 248 bool ContainsCsdWhitelistedUrl(const GURL& url) override { return true; } |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 // Fill up the database with test URL. | 286 // Fill up the database with test URL. |
266 void AddUrl(const GURL& url, | 287 void AddUrl(const GURL& url, |
267 const SBFullHashResult& full_hash, | 288 const SBFullHashResult& full_hash, |
268 const std::vector<SBPrefix>& prefix_hits) { | 289 const std::vector<SBPrefix>& prefix_hits) { |
269 Hits* hits_for_url = &badurls_[url.spec()]; | 290 Hits* hits_for_url = &badurls_[url.spec()]; |
270 hits_for_url->list_ids.push_back(full_hash.list_id); | 291 hits_for_url->list_ids.push_back(full_hash.list_id); |
271 hits_for_url->prefix_hits.insert(hits_for_url->prefix_hits.end(), | 292 hits_for_url->prefix_hits.insert(hits_for_url->prefix_hits.end(), |
272 prefix_hits.begin(), prefix_hits.end()); | 293 prefix_hits.begin(), prefix_hits.end()); |
273 bad_prefixes_.insert( | 294 bad_prefixes_.insert( |
274 std::make_pair(full_hash.list_id, full_hash.hash.prefix)); | 295 std::make_pair(full_hash.list_id, full_hash.hash.prefix)); |
| 296 urls_by_hash_[SBFullHashToString(full_hash.hash)] = url; |
275 } | 297 } |
276 | 298 |
277 private: | 299 private: |
278 // Stores |list_ids| of safe browsing lists that match some |prefix_hits|. | 300 // Stores |list_ids| of safe browsing lists that match some |prefix_hits|. |
279 struct Hits { | 301 struct Hits { |
280 std::vector<int> list_ids; | 302 std::vector<int> list_ids; |
281 std::vector<SBPrefix> prefix_hits; | 303 std::vector<SBPrefix> prefix_hits; |
282 }; | 304 }; |
283 | 305 |
284 bool ContainsUrl(int list_id0, | 306 bool ContainsUrl(int list_id0, |
(...skipping 15 matching lines...) Expand all Loading... |
300 != list_ids_for_url.end()) { | 322 != list_ids_for_url.end()) { |
301 prefix_hits->insert(prefix_hits->end(), | 323 prefix_hits->insert(prefix_hits->end(), |
302 badurls_it->second.prefix_hits.begin(), | 324 badurls_it->second.prefix_hits.begin(), |
303 badurls_it->second.prefix_hits.end()); | 325 badurls_it->second.prefix_hits.end()); |
304 hit = true; | 326 hit = true; |
305 } | 327 } |
306 } | 328 } |
307 return hit; | 329 return hit; |
308 } | 330 } |
309 | 331 |
| 332 std::vector<GURL> UrlsForHashes(const std::vector<SBFullHash>& full_hashes) { |
| 333 std::vector<GURL> urls; |
| 334 for (auto hash : full_hashes) { |
| 335 auto url_it = urls_by_hash_.find(SBFullHashToString(hash)); |
| 336 if (url_it != urls_by_hash_.end()) { |
| 337 urls.push_back(url_it->second); |
| 338 } |
| 339 } |
| 340 return urls; |
| 341 } |
| 342 |
310 bool ContainsUrlPrefixes(int list_id0, | 343 bool ContainsUrlPrefixes(int list_id0, |
311 int list_id1, | 344 int list_id1, |
312 const std::vector<SBPrefix>& prefixes, | 345 const std::vector<SBPrefix>& prefixes, |
313 std::vector<SBPrefix>* prefix_hits) { | 346 std::vector<SBPrefix>* prefix_hits) { |
314 bool hit = false; | 347 bool hit = false; |
315 for (const SBPrefix& prefix : prefixes) { | 348 for (const SBPrefix& prefix : prefixes) { |
316 for (const std::pair<int, SBPrefix>& entry : bad_prefixes_) { | 349 for (const std::pair<int, SBPrefix>& entry : bad_prefixes_) { |
317 if (entry.second == prefix && | 350 if (entry.second == prefix && |
318 (entry.first == list_id0 || entry.first == list_id1)) { | 351 (entry.first == list_id0 || entry.first == list_id1)) { |
319 prefix_hits->push_back(prefix); | 352 prefix_hits->push_back(prefix); |
320 hit = true; | 353 hit = true; |
321 } | 354 } |
322 } | 355 } |
323 } | 356 } |
324 return hit; | 357 return hit; |
325 } | 358 } |
326 | 359 |
327 base::hash_map<std::string, Hits> badurls_; | 360 base::hash_map<std::string, Hits> badurls_; |
328 base::hash_set<std::pair<int, SBPrefix>> bad_prefixes_; | 361 base::hash_set<std::pair<int, SBPrefix>> bad_prefixes_; |
| 362 base::hash_map<std::string, GURL> urls_by_hash_; |
329 | 363 |
330 DISALLOW_COPY_AND_ASSIGN(TestSafeBrowsingDatabase); | 364 DISALLOW_COPY_AND_ASSIGN(TestSafeBrowsingDatabase); |
331 }; | 365 }; |
332 | 366 |
333 // Factory that creates TestSafeBrowsingDatabase instances. | 367 // Factory that creates TestSafeBrowsingDatabase instances. |
334 class TestSafeBrowsingDatabaseFactory : public SafeBrowsingDatabaseFactory { | 368 class TestSafeBrowsingDatabaseFactory : public SafeBrowsingDatabaseFactory { |
335 public: | 369 public: |
336 TestSafeBrowsingDatabaseFactory() : db_(NULL) {} | 370 TestSafeBrowsingDatabaseFactory() : db_(NULL) {} |
337 ~TestSafeBrowsingDatabaseFactory() override {} | 371 ~TestSafeBrowsingDatabaseFactory() override {} |
338 | 372 |
(...skipping 1294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1633 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, | 1667 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, |
1634 content::Source<SafeBrowsingDatabaseManager>( | 1668 content::Source<SafeBrowsingDatabaseManager>( |
1635 sb_service_->database_manager().get())); | 1669 sb_service_->database_manager().get())); |
1636 BrowserThread::PostTask( | 1670 BrowserThread::PostTask( |
1637 BrowserThread::IO, FROM_HERE, | 1671 BrowserThread::IO, FROM_HERE, |
1638 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::ForceUpdate, this)); | 1672 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::ForceUpdate, this)); |
1639 observer.Wait(); | 1673 observer.Wait(); |
1640 } | 1674 } |
1641 | 1675 |
1642 } // namespace safe_browsing | 1676 } // namespace safe_browsing |
OLD | NEW |