Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(34)

Side by Side Diff: chrome/browser/safe_browsing/safe_browsing_service_browsertest.cc

Issue 7583007: Add "enabled" state to the ClientSideDetectionService. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/safe_browsing/safe_browsing_service.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 280 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 static void GenDigestFullhashResult(const std::string& full_digest, 309 static void GenDigestFullhashResult(const std::string& full_digest,
309 const std::string& list_name, 310 const std::string& list_name,
310 int add_chunk_id, 311 int add_chunk_id,
311 SBFullHashResult* full_hash) { 312 SBFullHashResult* full_hash) {
312 safe_browsing_util::StringToSBFullHash(full_digest, &full_hash->hash); 313 safe_browsing_util::StringToSBFullHash(full_digest, &full_hash->hash);
313 full_hash->list_name = list_name; 314 full_hash->list_name = list_name;
314 full_hash->add_chunk_id = add_chunk_id; 315 full_hash->add_chunk_id = add_chunk_id;
315 } 316 }
316 317
317 virtual void SetUp() { 318 virtual void SetUp() {
318 // InProcessBrowserTest::SetUp() intantiates SafebrowsingService and 319 // InProcessBrowserTest::SetUp() instantiates SafebrowsingService and
319 // RegisterFactory has to be called before SafeBrowsingService is created. 320 // RegisterFactory has to be called before SafeBrowsingService is created.
320 SafeBrowsingDatabase::RegisterFactory(&db_factory_); 321 SafeBrowsingDatabase::RegisterFactory(&db_factory_);
321 SafeBrowsingProtocolManager::RegisterFactory(&pm_factory_); 322 SafeBrowsingProtocolManager::RegisterFactory(&pm_factory_);
322 InProcessBrowserTest::SetUp(); 323 InProcessBrowserTest::SetUp();
323 } 324 }
324 325
325 virtual void TearDown() { 326 virtual void TearDown() {
326 InProcessBrowserTest::TearDown(); 327 InProcessBrowserTest::TearDown();
327 328
328 // Unregister test factories after InProcessBrowserTest::TearDown 329 // Unregister test factories after InProcessBrowserTest::TearDown
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 void 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_.reset(csd_service);
407 sb_service->RefreshState();
408 }
409
400 protected: 410 protected:
401 StrictMock<MockObserver> observer_; 411 StrictMock<MockObserver> observer_;
402 412
403 // Waits for pending tasks on the IO thread to complete. This is useful 413 // Waits for pending tasks on the IO thread to complete. This is useful
404 // to wait for the SafeBrowsingService to finish loading/stopping. 414 // to wait for the SafeBrowsingService to finish loading/stopping.
405 void WaitForIOThread() { 415 void WaitForIOThread() {
406 BrowserThread::PostTask( 416 BrowserThread::PostTask(
407 BrowserThread::IO, FROM_HERE, NewRunnableFunction(&QuitFromIOThread)); 417 BrowserThread::IO, FROM_HERE, NewRunnableFunction(&QuitFromIOThread));
408 ui_test_utils::RunMessageLoop(); // Will stop from |QuitUIThread|. 418 ui_test_utils::RunMessageLoop(); // Will stop from |QuitUIThread|.
409 } 419 }
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after
691 client->CheckDownloadHash(full_hash); 701 client->CheckDownloadHash(full_hash);
692 702
693 // There should be a timeout and the hash would be considered as safe. 703 // There should be a timeout and the hash would be considered as safe.
694 EXPECT_EQ(SafeBrowsingService::SAFE, client->GetResult()); 704 EXPECT_EQ(SafeBrowsingService::SAFE, client->GetResult());
695 705
696 // Need to set the timeout back to the default value. 706 // Need to set the timeout back to the default value.
697 SetDownloadHashCheckTimeout(sb_service, default_hashcheck_timeout); 707 SetDownloadHashCheckTimeout(sb_service, default_hashcheck_timeout);
698 } 708 }
699 709
700 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, StartAndStop) { 710 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, StartAndStop) {
711 CreateCSDService();
701 SafeBrowsingService* sb_service = g_browser_process->safe_browsing_service(); 712 SafeBrowsingService* sb_service = g_browser_process->safe_browsing_service();
713 safe_browsing::ClientSideDetectionService* csd_service =
714 sb_service->safe_browsing_detection_service();
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
OLDNEW
« no previous file with comments | « chrome/browser/safe_browsing/safe_browsing_service.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698