OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 #include "chrome/browser/safe_browsing/protocol_manager.h" | 21 #include "chrome/browser/safe_browsing/protocol_manager.h" |
22 #include "chrome/browser/ui/browser.h" | 22 #include "chrome/browser/ui/browser.h" |
23 #include "chrome/common/chrome_switches.h" | 23 #include "chrome/common/chrome_switches.h" |
24 #include "chrome/common/pref_names.h" | 24 #include "chrome/common/pref_names.h" |
25 #include "chrome/test/in_process_browser_test.h" | 25 #include "chrome/test/in_process_browser_test.h" |
26 #include "chrome/test/ui_test_utils.h" | 26 #include "chrome/test/ui_test_utils.h" |
27 #include "content/browser/browser_thread.h" | 27 #include "content/browser/browser_thread.h" |
28 #include "content/browser/renderer_host/resource_dispatcher_host.h" | 28 #include "content/browser/renderer_host/resource_dispatcher_host.h" |
29 #include "content/browser/tab_contents/tab_contents.h" | 29 #include "content/browser/tab_contents/tab_contents.h" |
30 #include "content/browser/tab_contents/tab_contents_view.h" | 30 #include "content/browser/tab_contents/tab_contents_view.h" |
| 31 #include "testing/gmock/include/gmock/gmock.h" |
31 | 32 |
32 using base::Histogram; | 33 using base::Histogram; |
33 using base::StatisticsRecorder; | 34 using base::StatisticsRecorder; |
34 | 35 |
| 36 using ::testing::_; |
| 37 using ::testing::Mock; |
| 38 using ::testing::StrictMock; |
| 39 |
35 // A SafeBrowingDatabase class that allows us to inject the malicious URLs. | 40 // A SafeBrowingDatabase class that allows us to inject the malicious URLs. |
36 class TestSafeBrowsingDatabase : public SafeBrowsingDatabase { | 41 class TestSafeBrowsingDatabase : public SafeBrowsingDatabase { |
37 public: | 42 public: |
38 TestSafeBrowsingDatabase() {} | 43 TestSafeBrowsingDatabase() {} |
39 | 44 |
40 virtual ~TestSafeBrowsingDatabase() {} | 45 virtual ~TestSafeBrowsingDatabase() {} |
41 | 46 |
42 // Initializes the database with the given filename. | 47 // Initializes the database with the given filename. |
43 virtual void Init(const FilePath& filename) {} | 48 virtual void Init(const FilePath& filename) {} |
44 | 49 |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 return pm_; | 253 return pm_; |
249 } | 254 } |
250 TestProtocolManager* GetProtocolManager() { | 255 TestProtocolManager* GetProtocolManager() { |
251 return pm_; | 256 return pm_; |
252 } | 257 } |
253 private: | 258 private: |
254 // Owned by the SafebrowsingService. | 259 // Owned by the SafebrowsingService. |
255 TestProtocolManager* pm_; | 260 TestProtocolManager* pm_; |
256 }; | 261 }; |
257 | 262 |
| 263 class MockObserver : public SafeBrowsingService::Observer { |
| 264 public: |
| 265 MockObserver() {} |
| 266 virtual ~MockObserver() {} |
| 267 MOCK_METHOD1(OnSafeBrowsingHit, |
| 268 void(const SafeBrowsingService::UnsafeResource&)); |
| 269 }; |
| 270 |
| 271 MATCHER_P(IsUnsafeResourceFor, url, "") { |
| 272 return (arg.url.spec() == url.spec() && |
| 273 arg.threat_type != SafeBrowsingService::SAFE); |
| 274 } |
| 275 |
258 namespace { | 276 namespace { |
259 | 277 |
260 void QuitUIThread() { | 278 void QuitUIThread() { |
261 MessageLoopForUI::current()->Quit(); | 279 MessageLoopForUI::current()->Quit(); |
262 } | 280 } |
263 | 281 |
264 void QuitFromIOThread() { | 282 void QuitFromIOThread() { |
265 BrowserThread::PostTask( | 283 BrowserThread::PostTask( |
266 BrowserThread::UI, FROM_HERE, NewRunnableFunction(&QuitUIThread)); | 284 BrowserThread::UI, FROM_HERE, NewRunnableFunction(&QuitUIThread)); |
267 } | 285 } |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
372 } | 390 } |
373 | 391 |
374 void SetDownloadUrlCheckTimeout(SafeBrowsingService* sb_service, int64 ms) { | 392 void SetDownloadUrlCheckTimeout(SafeBrowsingService* sb_service, int64 ms) { |
375 sb_service->download_urlcheck_timeout_ms_ = ms; | 393 sb_service->download_urlcheck_timeout_ms_ = ms; |
376 } | 394 } |
377 | 395 |
378 void SetDownloadHashCheckTimeout(SafeBrowsingService* sb_service, int64 ms) { | 396 void SetDownloadHashCheckTimeout(SafeBrowsingService* sb_service, int64 ms) { |
379 sb_service->download_hashcheck_timeout_ms_ = ms; | 397 sb_service->download_hashcheck_timeout_ms_ = ms; |
380 } | 398 } |
381 | 399 |
| 400 protected: |
| 401 StrictMock<MockObserver> observer_; |
| 402 |
382 // Waits for pending tasks on the IO thread to complete. This is useful | 403 // Waits for pending tasks on the IO thread to complete. This is useful |
383 // to wait for the SafeBrowsingService to finish loading/stopping. | 404 // to wait for the SafeBrowsingService to finish loading/stopping. |
384 void WaitForIOThread() { | 405 void WaitForIOThread() { |
385 BrowserThread::PostTask( | 406 BrowserThread::PostTask( |
386 BrowserThread::IO, FROM_HERE, NewRunnableFunction(&QuitFromIOThread)); | 407 BrowserThread::IO, FROM_HERE, NewRunnableFunction(&QuitFromIOThread)); |
387 ui_test_utils::RunMessageLoop(); // Will stop from |QuitUIThread|. | 408 ui_test_utils::RunMessageLoop(); // Will stop from |QuitUIThread|. |
388 } | 409 } |
389 | 410 |
390 private: | 411 private: |
391 TestSafeBrowsingDatabaseFactory db_factory_; | 412 TestSafeBrowsingDatabaseFactory db_factory_; |
392 TestSBProtocolManagerFactory pm_factory_; | 413 TestSBProtocolManagerFactory pm_factory_; |
393 | 414 |
394 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceTest); | 415 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceTest); |
395 }; | 416 }; |
396 | 417 |
397 namespace { | 418 namespace { |
398 | 419 |
399 const char kEmptyPage[] = "files/empty.html"; | 420 const char kEmptyPage[] = "files/empty.html"; |
400 const char kMalwareFile[] = "files/downloads/dangerous/dangerous.exe"; | 421 const char kMalwareFile[] = "files/downloads/dangerous/dangerous.exe"; |
401 const char kMalwareIframe[] = "files/safe_browsing/malware_iframe.html"; | 422 const char kMalwareIframe[] = "files/safe_browsing/malware_iframe.html"; |
402 const char kMalwarePage[] = "files/safe_browsing/malware.html"; | 423 const char kMalwarePage[] = "files/safe_browsing/malware.html"; |
403 | 424 |
404 // This test goes through DownloadResourceHandler. | 425 // This test goes through DownloadResourceHandler. |
405 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, Malware) { | 426 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, Malware) { |
406 GURL url = test_server()->GetURL(kEmptyPage); | 427 GURL url = test_server()->GetURL(kEmptyPage); |
| 428 g_browser_process->safe_browsing_service()->AddObserver(&observer_); |
407 | 429 |
408 // After adding the url to safebrowsing database and getfullhash result, | 430 // After adding the url to safebrowsing database and getfullhash result, |
409 // we should see the interstitial page. | 431 // we should see the interstitial page. |
410 SBFullHashResult malware_full_hash; | 432 SBFullHashResult malware_full_hash; |
411 int chunk_id = 0; | 433 int chunk_id = 0; |
412 GenUrlFullhashResult(url, safe_browsing_util::kMalwareList, chunk_id, | 434 GenUrlFullhashResult(url, safe_browsing_util::kMalwareList, chunk_id, |
413 &malware_full_hash); | 435 &malware_full_hash); |
| 436 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(url))).Times(1); |
414 SetupResponseForUrl(url, malware_full_hash); | 437 SetupResponseForUrl(url, malware_full_hash); |
415 ui_test_utils::NavigateToURL(browser(), url); | 438 ui_test_utils::NavigateToURL(browser(), url); |
416 EXPECT_TRUE(ShowingInterstitialPage()); | 439 EXPECT_TRUE(ShowingInterstitialPage()); |
| 440 g_browser_process->safe_browsing_service()->RemoveObserver(&observer_); |
417 } | 441 } |
418 | 442 |
419 const char kPrefetchMalwarePage[] = "files/safe_browsing/prefetch_malware.html"; | 443 const char kPrefetchMalwarePage[] = "files/safe_browsing/prefetch_malware.html"; |
420 | 444 |
421 // This test confirms that prefetches don't themselves get the | 445 // This test confirms that prefetches don't themselves get the |
422 // interstitial treatment. | 446 // interstitial treatment. |
423 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, Prefetch) { | 447 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, Prefetch) { |
424 GURL url = test_server()->GetURL(kPrefetchMalwarePage); | 448 GURL url = test_server()->GetURL(kPrefetchMalwarePage); |
425 GURL malware_url = test_server()->GetURL(kMalwarePage); | 449 GURL malware_url = test_server()->GetURL(kMalwarePage); |
| 450 g_browser_process->safe_browsing_service()->AddObserver(&observer_); |
426 | 451 |
427 class SetPrefetchForTest { | 452 class SetPrefetchForTest { |
428 public: | 453 public: |
429 explicit SetPrefetchForTest(bool prefetch) | 454 explicit SetPrefetchForTest(bool prefetch) |
430 : old_prefetch_state_(ResourceDispatcherHost::is_prefetch_enabled()), | 455 : old_prefetch_state_(ResourceDispatcherHost::is_prefetch_enabled()), |
431 old_prerender_mode_(prerender::PrerenderManager::GetMode()) { | 456 old_prerender_mode_(prerender::PrerenderManager::GetMode()) { |
432 ResourceDispatcherHost::set_is_prefetch_enabled(prefetch); | 457 ResourceDispatcherHost::set_is_prefetch_enabled(prefetch); |
433 prerender::PrerenderManager::SetMode( | 458 prerender::PrerenderManager::SetMode( |
434 prerender::PrerenderManager::PRERENDER_MODE_DISABLED); | 459 prerender::PrerenderManager::PRERENDER_MODE_DISABLED); |
435 } | 460 } |
(...skipping 10 matching lines...) Expand all Loading... |
446 // Even though we have added this uri to the safebrowsing database and | 471 // Even though we have added this uri to the safebrowsing database and |
447 // getfullhash result, we should not see the interstitial page since the | 472 // getfullhash result, we should not see the interstitial page since the |
448 // only malware was a prefetch target. | 473 // only malware was a prefetch target. |
449 SBFullHashResult malware_full_hash; | 474 SBFullHashResult malware_full_hash; |
450 int chunk_id = 0; | 475 int chunk_id = 0; |
451 GenUrlFullhashResult(malware_url, safe_browsing_util::kMalwareList, | 476 GenUrlFullhashResult(malware_url, safe_browsing_util::kMalwareList, |
452 chunk_id, &malware_full_hash); | 477 chunk_id, &malware_full_hash); |
453 SetupResponseForUrl(malware_url, malware_full_hash); | 478 SetupResponseForUrl(malware_url, malware_full_hash); |
454 ui_test_utils::NavigateToURL(browser(), url); | 479 ui_test_utils::NavigateToURL(browser(), url); |
455 EXPECT_FALSE(ShowingInterstitialPage()); | 480 EXPECT_FALSE(ShowingInterstitialPage()); |
| 481 Mock::VerifyAndClear(&observer_); |
456 | 482 |
457 // However, when we navigate to the malware page, we should still get | 483 // However, when we navigate to the malware page, we should still get |
458 // the interstitial. | 484 // the interstitial. |
| 485 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(malware_url))) |
| 486 .Times(1); |
459 ui_test_utils::NavigateToURL(browser(), malware_url); | 487 ui_test_utils::NavigateToURL(browser(), malware_url); |
460 EXPECT_TRUE(ShowingInterstitialPage()); | 488 EXPECT_TRUE(ShowingInterstitialPage()); |
| 489 Mock::VerifyAndClear(&observer_); |
| 490 g_browser_process->safe_browsing_service()->RemoveObserver(&observer_); |
461 } | 491 } |
462 | 492 |
463 } // namespace | 493 } // namespace |
464 | 494 |
465 class TestSBClient | 495 class TestSBClient |
466 : public base::RefCountedThreadSafe<TestSBClient>, | 496 : public base::RefCountedThreadSafe<TestSBClient>, |
467 public SafeBrowsingService::Client { | 497 public SafeBrowsingService::Client { |
468 public: | 498 public: |
469 TestSBClient() : result_(SafeBrowsingService::SAFE), | 499 TestSBClient() : result_(SafeBrowsingService::SAFE), |
470 safe_browsing_service_(g_browser_process-> | 500 safe_browsing_service_(g_browser_process-> |
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
704 EXPECT_TRUE(sb_service->enabled()); | 734 EXPECT_TRUE(sb_service->enabled()); |
705 | 735 |
706 // Delete the Profile. SBS stops again. | 736 // Delete the Profile. SBS stops again. |
707 pref_service2 = NULL; | 737 pref_service2 = NULL; |
708 profile2.reset(); | 738 profile2.reset(); |
709 WaitForIOThread(); | 739 WaitForIOThread(); |
710 EXPECT_FALSE(sb_service->enabled()); | 740 EXPECT_FALSE(sb_service->enabled()); |
711 } | 741 } |
712 | 742 |
713 } // namespace | 743 } // namespace |
OLD | NEW |