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 "crypto/sha2.h" | 13 #include "crypto/sha2.h" |
13 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/prefs/pref_service.h" |
14 #include "chrome/browser/prerender/prerender_manager.h" | 16 #include "chrome/browser/prerender/prerender_manager.h" |
| 17 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/browser/safe_browsing/safe_browsing_database.h" | 18 #include "chrome/browser/safe_browsing/safe_browsing_database.h" |
16 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 19 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
17 #include "chrome/browser/safe_browsing/safe_browsing_util.h" | 20 #include "chrome/browser/safe_browsing/safe_browsing_util.h" |
18 #include "chrome/browser/safe_browsing/protocol_manager.h" | 21 #include "chrome/browser/safe_browsing/protocol_manager.h" |
19 #include "chrome/browser/ui/browser.h" | 22 #include "chrome/browser/ui/browser.h" |
20 #include "chrome/common/chrome_switches.h" | 23 #include "chrome/common/chrome_switches.h" |
| 24 #include "chrome/common/pref_names.h" |
21 #include "chrome/test/in_process_browser_test.h" | 25 #include "chrome/test/in_process_browser_test.h" |
22 #include "chrome/test/ui_test_utils.h" | 26 #include "chrome/test/ui_test_utils.h" |
23 #include "content/browser/browser_thread.h" | 27 #include "content/browser/browser_thread.h" |
24 #include "content/browser/renderer_host/resource_dispatcher_host.h" | 28 #include "content/browser/renderer_host/resource_dispatcher_host.h" |
25 #include "content/browser/tab_contents/tab_contents.h" | 29 #include "content/browser/tab_contents/tab_contents.h" |
26 #include "content/browser/tab_contents/tab_contents_view.h" | 30 #include "content/browser/tab_contents/tab_contents_view.h" |
27 | 31 |
28 using base::Histogram; | 32 using base::Histogram; |
29 using base::StatisticsRecorder; | 33 using base::StatisticsRecorder; |
30 | 34 |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 return pm_; | 248 return pm_; |
245 } | 249 } |
246 TestProtocolManager* GetProtocolManager() { | 250 TestProtocolManager* GetProtocolManager() { |
247 return pm_; | 251 return pm_; |
248 } | 252 } |
249 private: | 253 private: |
250 // Owned by the SafebrowsingService. | 254 // Owned by the SafebrowsingService. |
251 TestProtocolManager* pm_; | 255 TestProtocolManager* pm_; |
252 }; | 256 }; |
253 | 257 |
| 258 namespace { |
| 259 |
| 260 void QuitUIThread() { |
| 261 MessageLoopForUI::current()->Quit(); |
| 262 } |
| 263 |
| 264 void QuitFromIOThread() { |
| 265 BrowserThread::PostTask( |
| 266 BrowserThread::UI, FROM_HERE, NewRunnableFunction(&QuitUIThread)); |
| 267 } |
| 268 |
| 269 } // namespace |
| 270 |
254 // Tests the safe browsing blocking page in a browser. | 271 // Tests the safe browsing blocking page in a browser. |
255 class SafeBrowsingServiceTest : public InProcessBrowserTest { | 272 class SafeBrowsingServiceTest : public InProcessBrowserTest { |
256 public: | 273 public: |
257 SafeBrowsingServiceTest() { | 274 SafeBrowsingServiceTest() { |
258 } | 275 } |
259 | 276 |
260 static void GenUrlFullhashResult(const GURL& url, | 277 static void GenUrlFullhashResult(const GURL& url, |
261 const std::string& list_name, | 278 const std::string& list_name, |
262 int add_chunk_id, | 279 int add_chunk_id, |
263 SBFullHashResult* full_hash) { | 280 SBFullHashResult* full_hash) { |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 } | 372 } |
356 | 373 |
357 void SetDownloadUrlCheckTimeout(SafeBrowsingService* sb_service, int64 ms) { | 374 void SetDownloadUrlCheckTimeout(SafeBrowsingService* sb_service, int64 ms) { |
358 sb_service->download_urlcheck_timeout_ms_ = ms; | 375 sb_service->download_urlcheck_timeout_ms_ = ms; |
359 } | 376 } |
360 | 377 |
361 void SetDownloadHashCheckTimeout(SafeBrowsingService* sb_service, int64 ms) { | 378 void SetDownloadHashCheckTimeout(SafeBrowsingService* sb_service, int64 ms) { |
362 sb_service->download_hashcheck_timeout_ms_ = ms; | 379 sb_service->download_hashcheck_timeout_ms_ = ms; |
363 } | 380 } |
364 | 381 |
| 382 // Waits for pending tasks on the IO thread to complete. This is useful |
| 383 // to wait for the SafeBrowsingService to finish loading/stopping. |
| 384 void WaitForIOThread() { |
| 385 BrowserThread::PostTask( |
| 386 BrowserThread::IO, FROM_HERE, NewRunnableFunction(&QuitFromIOThread)); |
| 387 ui_test_utils::RunMessageLoop(); // Will stop from |QuitUIThread|. |
| 388 } |
| 389 |
365 private: | 390 private: |
366 TestSafeBrowsingDatabaseFactory db_factory_; | 391 TestSafeBrowsingDatabaseFactory db_factory_; |
367 TestSBProtocolManagerFactory pm_factory_; | 392 TestSBProtocolManagerFactory pm_factory_; |
368 | 393 |
369 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceTest); | 394 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceTest); |
370 }; | 395 }; |
371 | 396 |
372 namespace { | 397 namespace { |
373 | 398 |
374 const char kEmptyPage[] = "files/empty.html"; | 399 const char kEmptyPage[] = "files/empty.html"; |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
635 SetDownloadHashCheckTimeout(sb_service, kOneMs); | 660 SetDownloadHashCheckTimeout(sb_service, kOneMs); |
636 client->CheckDownloadHash(full_hash); | 661 client->CheckDownloadHash(full_hash); |
637 | 662 |
638 // There should be a timeout and the hash would be considered as safe. | 663 // There should be a timeout and the hash would be considered as safe. |
639 EXPECT_EQ(SafeBrowsingService::SAFE, client->GetResult()); | 664 EXPECT_EQ(SafeBrowsingService::SAFE, client->GetResult()); |
640 | 665 |
641 // Need to set the timeout back to the default value. | 666 // Need to set the timeout back to the default value. |
642 SetDownloadHashCheckTimeout(sb_service, default_hashcheck_timeout); | 667 SetDownloadHashCheckTimeout(sb_service, default_hashcheck_timeout); |
643 } | 668 } |
644 | 669 |
| 670 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, StartAndStop) { |
| 671 SafeBrowsingService* sb_service = g_browser_process->safe_browsing_service(); |
| 672 PrefService* pref_service = browser()->profile()->GetPrefs(); |
| 673 |
| 674 EXPECT_TRUE(pref_service->GetBoolean(prefs::kSafeBrowsingEnabled)); |
| 675 |
| 676 // SBS might still be starting, make sure this doesn't flake. |
| 677 WaitForIOThread(); |
| 678 EXPECT_TRUE(sb_service->enabled()); |
| 679 |
| 680 // Add a new Profile. SBS should keep running. |
| 681 ScopedTempDir temp_dir; |
| 682 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 683 scoped_ptr<Profile> profile2(Profile::CreateProfile(temp_dir.path())); |
| 684 ASSERT_TRUE(profile2.get() != NULL); |
| 685 PrefService* pref_service2 = profile2->GetPrefs(); |
| 686 EXPECT_TRUE(pref_service2->GetBoolean(prefs::kSafeBrowsingEnabled)); |
| 687 // We don't expect the state to have changed, but if it did, wait for it. |
| 688 WaitForIOThread(); |
| 689 EXPECT_TRUE(sb_service->enabled()); |
| 690 |
| 691 // Change one of the prefs. SBS should keep running. |
| 692 pref_service->SetBoolean(prefs::kSafeBrowsingEnabled, false); |
| 693 WaitForIOThread(); |
| 694 EXPECT_TRUE(sb_service->enabled()); |
| 695 |
| 696 // Change the other pref. SBS should stop now. |
| 697 pref_service2->SetBoolean(prefs::kSafeBrowsingEnabled, false); |
| 698 WaitForIOThread(); |
| 699 EXPECT_FALSE(sb_service->enabled()); |
| 700 |
| 701 // Turn it back on. SBS comes back. |
| 702 pref_service2->SetBoolean(prefs::kSafeBrowsingEnabled, true); |
| 703 WaitForIOThread(); |
| 704 EXPECT_TRUE(sb_service->enabled()); |
| 705 |
| 706 // Delete the Profile. SBS stops again. |
| 707 pref_service2 = NULL; |
| 708 profile2.reset(); |
| 709 WaitForIOThread(); |
| 710 EXPECT_FALSE(sb_service->enabled()); |
| 711 } |
| 712 |
645 } // namespace | 713 } // namespace |
OLD | NEW |