| 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 "base/scoped_temp_dir.h" | 12 #include "base/scoped_temp_dir.h" |
| 13 #include "crypto/sha2.h" | 13 #include "crypto/sha2.h" |
| 14 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/prefs/pref_service.h" | 15 #include "chrome/browser/prefs/pref_service.h" |
| 16 #include "chrome/browser/prerender/prerender_manager.h" | 16 #include "chrome/browser/prerender/prerender_manager.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/safe_browsing/client_side_detection_service.h" |
| 18 #include "chrome/browser/safe_browsing/safe_browsing_database.h" | 19 #include "chrome/browser/safe_browsing/safe_browsing_database.h" |
| 19 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 20 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 20 #include "chrome/browser/safe_browsing/safe_browsing_util.h" | 21 #include "chrome/browser/safe_browsing/safe_browsing_util.h" |
| 21 #include "chrome/browser/safe_browsing/protocol_manager.h" | 22 #include "chrome/browser/safe_browsing/protocol_manager.h" |
| 22 #include "chrome/browser/ui/browser.h" | 23 #include "chrome/browser/ui/browser.h" |
| 23 #include "chrome/common/chrome_switches.h" | 24 #include "chrome/common/chrome_switches.h" |
| 24 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
| 25 #include "chrome/test/base/in_process_browser_test.h" | 26 #include "chrome/test/base/in_process_browser_test.h" |
| 26 #include "chrome/test/base/ui_test_utils.h" | 27 #include "chrome/test/base/ui_test_utils.h" |
| 27 #include "content/browser/browser_thread.h" | 28 #include "content/browser/browser_thread.h" |
| (...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 390 } | 391 } |
| 391 | 392 |
| 392 void SetDownloadUrlCheckTimeout(SafeBrowsingService* sb_service, int64 ms) { | 393 void SetDownloadUrlCheckTimeout(SafeBrowsingService* sb_service, int64 ms) { |
| 393 sb_service->download_urlcheck_timeout_ms_ = ms; | 394 sb_service->download_urlcheck_timeout_ms_ = ms; |
| 394 } | 395 } |
| 395 | 396 |
| 396 void SetDownloadHashCheckTimeout(SafeBrowsingService* sb_service, int64 ms) { | 397 void SetDownloadHashCheckTimeout(SafeBrowsingService* sb_service, int64 ms) { |
| 397 sb_service->download_hashcheck_timeout_ms_ = ms; | 398 sb_service->download_hashcheck_timeout_ms_ = ms; |
| 398 } | 399 } |
| 399 | 400 |
| 401 safe_browsing::ClientSideDetectionService* CreateCSDService() { |
| 402 safe_browsing::ClientSideDetectionService* csd_service = |
| 403 safe_browsing::ClientSideDetectionService::Create(NULL); |
| 404 SafeBrowsingService* sb_service = |
| 405 g_browser_process->safe_browsing_service(); |
| 406 sb_service->csd_service_ = csd_service; |
| 407 sb_service->RefreshState(); |
| 408 return csd_service; |
| 409 } |
| 410 |
| 400 protected: | 411 protected: |
| 401 StrictMock<MockObserver> observer_; | 412 StrictMock<MockObserver> observer_; |
| 402 | 413 |
| 403 // Waits for pending tasks on the IO thread to complete. This is useful | 414 // Waits for pending tasks on the IO thread to complete. This is useful |
| 404 // to wait for the SafeBrowsingService to finish loading/stopping. | 415 // to wait for the SafeBrowsingService to finish loading/stopping. |
| 405 void WaitForIOThread() { | 416 void WaitForIOThread() { |
| 406 BrowserThread::PostTask( | 417 BrowserThread::PostTask( |
| 407 BrowserThread::IO, FROM_HERE, NewRunnableFunction(&QuitFromIOThread)); | 418 BrowserThread::IO, FROM_HERE, NewRunnableFunction(&QuitFromIOThread)); |
| 408 ui_test_utils::RunMessageLoop(); // Will stop from |QuitUIThread|. | 419 ui_test_utils::RunMessageLoop(); // Will stop from |QuitUIThread|. |
| 409 } | 420 } |
| (...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 692 | 703 |
| 693 // There should be a timeout and the hash would be considered as safe. | 704 // There should be a timeout and the hash would be considered as safe. |
| 694 EXPECT_EQ(SafeBrowsingService::SAFE, client->GetResult()); | 705 EXPECT_EQ(SafeBrowsingService::SAFE, client->GetResult()); |
| 695 | 706 |
| 696 // Need to set the timeout back to the default value. | 707 // Need to set the timeout back to the default value. |
| 697 SetDownloadHashCheckTimeout(sb_service, default_hashcheck_timeout); | 708 SetDownloadHashCheckTimeout(sb_service, default_hashcheck_timeout); |
| 698 } | 709 } |
| 699 | 710 |
| 700 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, StartAndStop) { | 711 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, StartAndStop) { |
| 701 SafeBrowsingService* sb_service = g_browser_process->safe_browsing_service(); | 712 SafeBrowsingService* sb_service = g_browser_process->safe_browsing_service(); |
| 713 scoped_ptr<safe_browsing::ClientSideDetectionService> csd_service( |
| 714 CreateCSDService()); |
| 702 PrefService* pref_service = browser()->profile()->GetPrefs(); | 715 PrefService* pref_service = browser()->profile()->GetPrefs(); |
| 703 | 716 |
| 717 ASSERT_TRUE(sb_service != NULL); |
| 718 ASSERT_TRUE(csd_service != NULL); |
| 719 ASSERT_TRUE(pref_service != NULL); |
| 720 |
| 704 EXPECT_TRUE(pref_service->GetBoolean(prefs::kSafeBrowsingEnabled)); | 721 EXPECT_TRUE(pref_service->GetBoolean(prefs::kSafeBrowsingEnabled)); |
| 705 | 722 |
| 706 // SBS might still be starting, make sure this doesn't flake. | 723 // SBS might still be starting, make sure this doesn't flake. |
| 707 WaitForIOThread(); | 724 WaitForIOThread(); |
| 708 EXPECT_TRUE(sb_service->enabled()); | 725 EXPECT_TRUE(sb_service->enabled()); |
| 726 EXPECT_TRUE(csd_service->enabled()); |
| 709 | 727 |
| 710 // Add a new Profile. SBS should keep running. | 728 // Add a new Profile. SBS should keep running. |
| 711 ScopedTempDir temp_dir; | 729 ScopedTempDir temp_dir; |
| 712 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 730 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 713 scoped_ptr<Profile> profile2(Profile::CreateProfile(temp_dir.path())); | 731 scoped_ptr<Profile> profile2(Profile::CreateProfile(temp_dir.path())); |
| 714 ASSERT_TRUE(profile2.get() != NULL); | 732 ASSERT_TRUE(profile2.get() != NULL); |
| 715 PrefService* pref_service2 = profile2->GetPrefs(); | 733 PrefService* pref_service2 = profile2->GetPrefs(); |
| 716 EXPECT_TRUE(pref_service2->GetBoolean(prefs::kSafeBrowsingEnabled)); | 734 EXPECT_TRUE(pref_service2->GetBoolean(prefs::kSafeBrowsingEnabled)); |
| 717 // We don't expect the state to have changed, but if it did, wait for it. | 735 // We don't expect the state to have changed, but if it did, wait for it. |
| 718 WaitForIOThread(); | 736 WaitForIOThread(); |
| 719 EXPECT_TRUE(sb_service->enabled()); | 737 EXPECT_TRUE(sb_service->enabled()); |
| 738 EXPECT_TRUE(csd_service->enabled()); |
| 720 | 739 |
| 721 // Change one of the prefs. SBS should keep running. | 740 // Change one of the prefs. SBS should keep running. |
| 722 pref_service->SetBoolean(prefs::kSafeBrowsingEnabled, false); | 741 pref_service->SetBoolean(prefs::kSafeBrowsingEnabled, false); |
| 723 WaitForIOThread(); | 742 WaitForIOThread(); |
| 724 EXPECT_TRUE(sb_service->enabled()); | 743 EXPECT_TRUE(sb_service->enabled()); |
| 744 EXPECT_TRUE(csd_service->enabled()); |
| 725 | 745 |
| 726 // Change the other pref. SBS should stop now. | 746 // Change the other pref. SBS should stop now. |
| 727 pref_service2->SetBoolean(prefs::kSafeBrowsingEnabled, false); | 747 pref_service2->SetBoolean(prefs::kSafeBrowsingEnabled, false); |
| 728 WaitForIOThread(); | 748 WaitForIOThread(); |
| 729 EXPECT_FALSE(sb_service->enabled()); | 749 EXPECT_FALSE(sb_service->enabled()); |
| 750 EXPECT_FALSE(csd_service->enabled()); |
| 730 | 751 |
| 731 // Turn it back on. SBS comes back. | 752 // Turn it back on. SBS comes back. |
| 732 pref_service2->SetBoolean(prefs::kSafeBrowsingEnabled, true); | 753 pref_service2->SetBoolean(prefs::kSafeBrowsingEnabled, true); |
| 733 WaitForIOThread(); | 754 WaitForIOThread(); |
| 734 EXPECT_TRUE(sb_service->enabled()); | 755 EXPECT_TRUE(sb_service->enabled()); |
| 756 EXPECT_TRUE(csd_service->enabled()); |
| 735 | 757 |
| 736 // Delete the Profile. SBS stops again. | 758 // Delete the Profile. SBS stops again. |
| 737 pref_service2 = NULL; | 759 pref_service2 = NULL; |
| 738 profile2.reset(); | 760 profile2.reset(); |
| 739 WaitForIOThread(); | 761 WaitForIOThread(); |
| 740 EXPECT_FALSE(sb_service->enabled()); | 762 EXPECT_FALSE(sb_service->enabled()); |
| 763 EXPECT_FALSE(csd_service->enabled()); |
| 741 } | 764 } |
| 742 | 765 |
| 743 } // namespace | 766 } // namespace |
| OLD | NEW |