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" |
11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
12 #include "crypto/sha2.h" | 12 #include "crypto/sha2.h" |
13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
14 #include "chrome/browser/prerender/prerender_manager.h" | 14 #include "chrome/browser/prerender/prerender_manager.h" |
15 #include "chrome/browser/safe_browsing/safe_browsing_database.h" | 15 #include "chrome/browser/safe_browsing/safe_browsing_database.h" |
16 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 16 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
17 #include "chrome/browser/safe_browsing/safe_browsing_util.h" | 17 #include "chrome/browser/safe_browsing/safe_browsing_util.h" |
18 #include "chrome/browser/safe_browsing/protocol_manager.h" | 18 #include "chrome/browser/safe_browsing/protocol_manager.h" |
19 #include "chrome/browser/ui/browser.h" | 19 #include "chrome/browser/ui/browser.h" |
20 #include "chrome/common/chrome_switches.h" | 20 #include "chrome/common/chrome_switches.h" |
21 #include "chrome/test/in_process_browser_test.h" | 21 #include "chrome/test/in_process_browser_test.h" |
22 #include "chrome/test/ui_test_utils.h" | 22 #include "chrome/test/ui_test_utils.h" |
23 #include "content/browser/browser_thread.h" | 23 #include "content/browser/browser_thread.h" |
24 #include "content/browser/renderer_host/resource_dispatcher_host.h" | 24 #include "content/browser/renderer_host/resource_dispatcher_host.h" |
25 #include "content/browser/tab_contents/tab_contents.h" | 25 #include "content/browser/tab_contents/tab_contents.h" |
26 #include "content/browser/tab_contents/tab_contents_view.h" | 26 #include "content/browser/tab_contents/tab_contents_view.h" |
27 #include "testing/gmock/include/gmock/gmock.h" | |
27 | 28 |
28 using base::Histogram; | 29 using base::Histogram; |
29 using base::StatisticsRecorder; | 30 using base::StatisticsRecorder; |
30 | 31 |
32 using ::testing::_; | |
33 using ::testing::Mock; | |
34 using ::testing::StrictMock; | |
35 | |
31 // A SafeBrowingDatabase class that allows us to inject the malicious URLs. | 36 // A SafeBrowingDatabase class that allows us to inject the malicious URLs. |
32 class TestSafeBrowsingDatabase : public SafeBrowsingDatabase { | 37 class TestSafeBrowsingDatabase : public SafeBrowsingDatabase { |
33 public: | 38 public: |
34 TestSafeBrowsingDatabase() {} | 39 TestSafeBrowsingDatabase() {} |
35 | 40 |
36 virtual ~TestSafeBrowsingDatabase() {} | 41 virtual ~TestSafeBrowsingDatabase() {} |
37 | 42 |
38 // Initializes the database with the given filename. | 43 // Initializes the database with the given filename. |
39 virtual void Init(const FilePath& filename) {} | 44 virtual void Init(const FilePath& filename) {} |
40 | 45 |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
244 return pm_; | 249 return pm_; |
245 } | 250 } |
246 TestProtocolManager* GetProtocolManager() { | 251 TestProtocolManager* GetProtocolManager() { |
247 return pm_; | 252 return pm_; |
248 } | 253 } |
249 private: | 254 private: |
250 // Owned by the SafebrowsingService. | 255 // Owned by the SafebrowsingService. |
251 TestProtocolManager* pm_; | 256 TestProtocolManager* pm_; |
252 }; | 257 }; |
253 | 258 |
259 class MockObserver : public SafeBrowsingService::Observer { | |
260 public: | |
261 MockObserver() {} | |
262 virtual ~MockObserver() {} | |
263 MOCK_METHOD1(OnSafeBrowsingHit, | |
264 void(const SafeBrowsingService::UnsafeResource&)); | |
265 }; | |
266 | |
267 MATCHER_P(IsUnsafeResourceFor, url, "") { | |
268 return (arg.url.spec() == url.spec() && | |
269 arg.threat_type != SafeBrowsingService::SAFE); | |
270 } | |
271 | |
254 // Tests the safe browsing blocking page in a browser. | 272 // Tests the safe browsing blocking page in a browser. |
255 class SafeBrowsingServiceTest : public InProcessBrowserTest { | 273 class SafeBrowsingServiceTest : public InProcessBrowserTest { |
256 public: | 274 public: |
257 SafeBrowsingServiceTest() { | 275 SafeBrowsingServiceTest() { |
258 } | 276 } |
259 | 277 |
260 static void GenUrlFullhashResult(const GURL& url, | 278 static void GenUrlFullhashResult(const GURL& url, |
261 const std::string& list_name, | 279 const std::string& list_name, |
262 int add_chunk_id, | 280 int add_chunk_id, |
263 SBFullHashResult* full_hash) { | 281 SBFullHashResult* full_hash) { |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
355 } | 373 } |
356 | 374 |
357 void SetDownloadUrlCheckTimeout(SafeBrowsingService* sb_service, int64 ms) { | 375 void SetDownloadUrlCheckTimeout(SafeBrowsingService* sb_service, int64 ms) { |
358 sb_service->download_urlcheck_timeout_ms_ = ms; | 376 sb_service->download_urlcheck_timeout_ms_ = ms; |
359 } | 377 } |
360 | 378 |
361 void SetDownloadHashCheckTimeout(SafeBrowsingService* sb_service, int64 ms) { | 379 void SetDownloadHashCheckTimeout(SafeBrowsingService* sb_service, int64 ms) { |
362 sb_service->download_hashcheck_timeout_ms_ = ms; | 380 sb_service->download_hashcheck_timeout_ms_ = ms; |
363 } | 381 } |
364 | 382 |
383 protected: | |
384 StrictMock<MockObserver> observer_; | |
385 | |
365 private: | 386 private: |
366 TestSafeBrowsingDatabaseFactory db_factory_; | 387 TestSafeBrowsingDatabaseFactory db_factory_; |
367 TestSBProtocolManagerFactory pm_factory_; | 388 TestSBProtocolManagerFactory pm_factory_; |
368 | 389 |
369 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceTest); | 390 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceTest); |
370 }; | 391 }; |
371 | 392 |
372 namespace { | 393 namespace { |
373 | 394 |
374 const char kEmptyPage[] = "files/empty.html"; | 395 const char kEmptyPage[] = "files/empty.html"; |
375 const char kMalwareFile[] = "files/downloads/dangerous/dangerous.exe"; | 396 const char kMalwareFile[] = "files/downloads/dangerous/dangerous.exe"; |
376 const char kMalwareIframe[] = "files/safe_browsing/malware_iframe.html"; | 397 const char kMalwareIframe[] = "files/safe_browsing/malware_iframe.html"; |
377 const char kMalwarePage[] = "files/safe_browsing/malware.html"; | 398 const char kMalwarePage[] = "files/safe_browsing/malware.html"; |
378 | 399 |
379 // This test goes through DownloadResourceHandler. | 400 // This test goes through DownloadResourceHandler. |
380 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, Malware) { | 401 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, Malware) { |
381 GURL url = test_server()->GetURL(kEmptyPage); | 402 GURL url = test_server()->GetURL(kEmptyPage); |
403 g_browser_process->safe_browsing_service()->AddObserver(&observer_); | |
382 | 404 |
383 // After adding the url to safebrowsing database and getfullhash result, | 405 // After adding the url to safebrowsing database and getfullhash result, |
384 // we should see the interstitial page. | 406 // we should see the interstitial page. |
385 SBFullHashResult malware_full_hash; | 407 SBFullHashResult malware_full_hash; |
386 int chunk_id = 0; | 408 int chunk_id = 0; |
387 GenUrlFullhashResult(url, safe_browsing_util::kMalwareList, chunk_id, | 409 GenUrlFullhashResult(url, safe_browsing_util::kMalwareList, chunk_id, |
388 &malware_full_hash); | 410 &malware_full_hash); |
411 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(url))).Times(1); | |
389 SetupResponseForUrl(url, malware_full_hash); | 412 SetupResponseForUrl(url, malware_full_hash); |
390 ui_test_utils::NavigateToURL(browser(), url); | 413 ui_test_utils::NavigateToURL(browser(), url); |
391 EXPECT_TRUE(ShowingInterstitialPage()); | 414 EXPECT_TRUE(ShowingInterstitialPage()); |
415 g_browser_process->safe_browsing_service()->RemoveObserver(&observer_); | |
392 } | 416 } |
393 | 417 |
394 const char kPrefetchMalwarePage[] = "files/safe_browsing/prefetch_malware.html"; | 418 const char kPrefetchMalwarePage[] = "files/safe_browsing/prefetch_malware.html"; |
395 | 419 |
396 // This test confirms that prefetches don't themselves get the | 420 // This test confirms that prefetches don't themselves get the |
397 // interstitial treatment. | 421 // interstitial treatment. |
398 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, Prefetch) { | 422 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, Prefetch) { |
399 GURL url = test_server()->GetURL(kPrefetchMalwarePage); | 423 GURL url = test_server()->GetURL(kPrefetchMalwarePage); |
400 GURL malware_url = test_server()->GetURL(kMalwarePage); | 424 GURL malware_url = test_server()->GetURL(kMalwarePage); |
425 g_browser_process->safe_browsing_service()->AddObserver(&observer_); | |
401 | 426 |
402 class SetPrefetchForTest { | 427 class SetPrefetchForTest { |
403 public: | 428 public: |
404 explicit SetPrefetchForTest(bool prefetch) | 429 explicit SetPrefetchForTest(bool prefetch) |
405 : old_prefetch_state_(ResourceDispatcherHost::is_prefetch_enabled()), | 430 : old_prefetch_state_(ResourceDispatcherHost::is_prefetch_enabled()), |
406 old_prerender_mode_(prerender::PrerenderManager::GetMode()) { | 431 old_prerender_mode_(prerender::PrerenderManager::GetMode()) { |
407 ResourceDispatcherHost::set_is_prefetch_enabled(prefetch); | 432 ResourceDispatcherHost::set_is_prefetch_enabled(prefetch); |
408 prerender::PrerenderManager::SetMode( | 433 prerender::PrerenderManager::SetMode( |
409 prerender::PrerenderManager::PRERENDER_MODE_DISABLED); | 434 prerender::PrerenderManager::PRERENDER_MODE_DISABLED); |
410 } | 435 } |
411 | 436 |
412 ~SetPrefetchForTest() { | 437 ~SetPrefetchForTest() { |
413 ResourceDispatcherHost::set_is_prefetch_enabled(old_prefetch_state_); | 438 ResourceDispatcherHost::set_is_prefetch_enabled(old_prefetch_state_); |
414 prerender::PrerenderManager::SetMode(old_prerender_mode_); | 439 prerender::PrerenderManager::SetMode(old_prerender_mode_); |
415 } | 440 } |
416 private: | 441 private: |
417 bool old_prefetch_state_; | 442 bool old_prefetch_state_; |
418 prerender::PrerenderManager::PrerenderManagerMode old_prerender_mode_; | 443 prerender::PrerenderManager::PrerenderManagerMode old_prerender_mode_; |
419 } set_prefetch_for_test(true); | 444 } set_prefetch_for_test(true); |
420 | 445 |
421 // Even though we have added this uri to the safebrowsing database and | 446 // Even though we have added this uri to the safebrowsing database and |
422 // getfullhash result, we should not see the interstitial page since the | 447 // getfullhash result, we should not see the interstitial page since the |
423 // only malware was a prefetch target. | 448 // only malware was a prefetch target. |
424 SBFullHashResult malware_full_hash; | 449 SBFullHashResult malware_full_hash; |
425 int chunk_id = 0; | 450 int chunk_id = 0; |
426 GenUrlFullhashResult(malware_url, safe_browsing_util::kMalwareList, | 451 GenUrlFullhashResult(malware_url, safe_browsing_util::kMalwareList, |
427 chunk_id, &malware_full_hash); | 452 chunk_id, &malware_full_hash); |
428 SetupResponseForUrl(malware_url, malware_full_hash); | 453 SetupResponseForUrl(malware_url, malware_full_hash); |
454 EXPECT_CALL(observer_, OnSafeBrowsingHit(_)).Times(0); | |
mattm
2011/07/19 21:25:55
Is this unnecessary due to using StrictMock?
noelutz
2011/07/19 22:28:08
Done.
| |
429 ui_test_utils::NavigateToURL(browser(), url); | 455 ui_test_utils::NavigateToURL(browser(), url); |
430 EXPECT_FALSE(ShowingInterstitialPage()); | 456 EXPECT_FALSE(ShowingInterstitialPage()); |
457 Mock::VerifyAndClear(&observer_); | |
431 | 458 |
432 // However, when we navigate to the malware page, we should still get | 459 // However, when we navigate to the malware page, we should still get |
433 // the interstitial. | 460 // the interstitial. |
461 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(malware_url))) | |
462 .Times(1); | |
434 ui_test_utils::NavigateToURL(browser(), malware_url); | 463 ui_test_utils::NavigateToURL(browser(), malware_url); |
435 EXPECT_TRUE(ShowingInterstitialPage()); | 464 EXPECT_TRUE(ShowingInterstitialPage()); |
465 Mock::VerifyAndClear(&observer_); | |
466 g_browser_process->safe_browsing_service()->RemoveObserver(&observer_); | |
436 } | 467 } |
437 | 468 |
438 } // namespace | 469 } // namespace |
439 | 470 |
440 class TestSBClient | 471 class TestSBClient |
441 : public base::RefCountedThreadSafe<TestSBClient>, | 472 : public base::RefCountedThreadSafe<TestSBClient>, |
442 public SafeBrowsingService::Client { | 473 public SafeBrowsingService::Client { |
443 public: | 474 public: |
444 TestSBClient() : result_(SafeBrowsingService::SAFE), | 475 TestSBClient() : result_(SafeBrowsingService::SAFE), |
445 safe_browsing_service_(g_browser_process-> | 476 safe_browsing_service_(g_browser_process-> |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
636 client->CheckDownloadHash(full_hash); | 667 client->CheckDownloadHash(full_hash); |
637 | 668 |
638 // There should be a timeout and the hash would be considered as safe. | 669 // There should be a timeout and the hash would be considered as safe. |
639 EXPECT_EQ(SafeBrowsingService::SAFE, client->GetResult()); | 670 EXPECT_EQ(SafeBrowsingService::SAFE, client->GetResult()); |
640 | 671 |
641 // Need to set the timeout back to the default value. | 672 // Need to set the timeout back to the default value. |
642 SetDownloadHashCheckTimeout(sb_service, default_hashcheck_timeout); | 673 SetDownloadHashCheckTimeout(sb_service, default_hashcheck_timeout); |
643 } | 674 } |
644 | 675 |
645 } // namespace | 676 } // namespace |
OLD | NEW |