| 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/bind.h" |
| 9 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 10 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 11 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 12 #include "base/scoped_temp_dir.h" | 13 #include "base/scoped_temp_dir.h" |
| 13 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/prefs/pref_service.h" | 15 #include "chrome/browser/prefs/pref_service.h" |
| 15 #include "chrome/browser/prerender/prerender_manager.h" | 16 #include "chrome/browser/prerender/prerender_manager.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/safe_browsing/client_side_detection_service.h" | 18 #include "chrome/browser/safe_browsing/client_side_detection_service.h" |
| 18 #include "chrome/browser/safe_browsing/protocol_manager.h" | 19 #include "chrome/browser/safe_browsing/protocol_manager.h" |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 // database and safebrowsing service issues a get hash request to backends. | 212 // database and safebrowsing service issues a get hash request to backends. |
| 212 // We return a result from the prefilled full_hashes_ hash_map to simulate | 213 // We return a result from the prefilled full_hashes_ hash_map to simulate |
| 213 // server's response. At the same time, latency is added to simulate real | 214 // server's response. At the same time, latency is added to simulate real |
| 214 // life network issues. | 215 // life network issues. |
| 215 virtual void GetFullHash(SafeBrowsingService::SafeBrowsingCheck* check, | 216 virtual void GetFullHash(SafeBrowsingService::SafeBrowsingCheck* check, |
| 216 const std::vector<SBPrefix>& prefixes) { | 217 const std::vector<SBPrefix>& prefixes) { |
| 217 // When we get a valid response, always cache the result. | 218 // When we get a valid response, always cache the result. |
| 218 bool cancache = true; | 219 bool cancache = true; |
| 219 BrowserThread::PostDelayedTask( | 220 BrowserThread::PostDelayedTask( |
| 220 BrowserThread::IO, FROM_HERE, | 221 BrowserThread::IO, FROM_HERE, |
| 221 NewRunnableMethod( | 222 base::Bind(&SafeBrowsingService::HandleGetHashResults, |
| 222 sb_service_, &SafeBrowsingService::HandleGetHashResults, | 223 sb_service_, check, full_hashes_, cancache), |
| 223 check, full_hashes_, cancache), | |
| 224 delay_ms_); | 224 delay_ms_); |
| 225 } | 225 } |
| 226 | 226 |
| 227 // Prepare the GetFullHash results for the next request. | 227 // Prepare the GetFullHash results for the next request. |
| 228 void SetGetFullHashResponse(const SBFullHashResult& full_hash_result) { | 228 void SetGetFullHashResponse(const SBFullHashResult& full_hash_result) { |
| 229 full_hashes_.clear(); | 229 full_hashes_.clear(); |
| 230 full_hashes_.push_back(full_hash_result); | 230 full_hashes_.push_back(full_hash_result); |
| 231 } | 231 } |
| 232 | 232 |
| 233 void IntroduceDelay(int64 ms) { | 233 void IntroduceDelay(int64 ms) { |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 } | 283 } |
| 284 | 284 |
| 285 namespace { | 285 namespace { |
| 286 | 286 |
| 287 void QuitUIThread() { | 287 void QuitUIThread() { |
| 288 MessageLoopForUI::current()->Quit(); | 288 MessageLoopForUI::current()->Quit(); |
| 289 } | 289 } |
| 290 | 290 |
| 291 void QuitFromIOThread() { | 291 void QuitFromIOThread() { |
| 292 BrowserThread::PostTask( | 292 BrowserThread::PostTask( |
| 293 BrowserThread::UI, FROM_HERE, NewRunnableFunction(&QuitUIThread)); | 293 BrowserThread::UI, FROM_HERE, base::Bind(&QuitUIThread)); |
| 294 } | 294 } |
| 295 | 295 |
| 296 } // namespace | 296 } // namespace |
| 297 | 297 |
| 298 // Tests the safe browsing blocking page in a browser. | 298 // Tests the safe browsing blocking page in a browser. |
| 299 class SafeBrowsingServiceTest : public InProcessBrowserTest { | 299 class SafeBrowsingServiceTest : public InProcessBrowserTest { |
| 300 public: | 300 public: |
| 301 SafeBrowsingServiceTest() { | 301 SafeBrowsingServiceTest() { |
| 302 } | 302 } |
| 303 | 303 |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 415 sb_service->RefreshState(); | 415 sb_service->RefreshState(); |
| 416 } | 416 } |
| 417 | 417 |
| 418 protected: | 418 protected: |
| 419 StrictMock<MockObserver> observer_; | 419 StrictMock<MockObserver> observer_; |
| 420 | 420 |
| 421 // Waits for pending tasks on the IO thread to complete. This is useful | 421 // Waits for pending tasks on the IO thread to complete. This is useful |
| 422 // to wait for the SafeBrowsingService to finish loading/stopping. | 422 // to wait for the SafeBrowsingService to finish loading/stopping. |
| 423 void WaitForIOThread() { | 423 void WaitForIOThread() { |
| 424 BrowserThread::PostTask( | 424 BrowserThread::PostTask( |
| 425 BrowserThread::IO, FROM_HERE, NewRunnableFunction(&QuitFromIOThread)); | 425 BrowserThread::IO, FROM_HERE, base::Bind(&QuitFromIOThread)); |
| 426 ui_test_utils::RunMessageLoop(); // Will stop from |QuitUIThread|. | 426 ui_test_utils::RunMessageLoop(); // Will stop from |QuitUIThread|. |
| 427 } | 427 } |
| 428 | 428 |
| 429 private: | 429 private: |
| 430 TestSafeBrowsingDatabaseFactory db_factory_; | 430 TestSafeBrowsingDatabaseFactory db_factory_; |
| 431 TestSBProtocolManagerFactory pm_factory_; | 431 TestSBProtocolManagerFactory pm_factory_; |
| 432 | 432 |
| 433 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceTest); | 433 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceTest); |
| 434 }; | 434 }; |
| 435 | 435 |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 519 safe_browsing_service()) { | 519 safe_browsing_service()) { |
| 520 } | 520 } |
| 521 | 521 |
| 522 int GetResult() { | 522 int GetResult() { |
| 523 return result_; | 523 return result_; |
| 524 } | 524 } |
| 525 | 525 |
| 526 void CheckDownloadUrl(const std::vector<GURL>& url_chain) { | 526 void CheckDownloadUrl(const std::vector<GURL>& url_chain) { |
| 527 BrowserThread::PostTask( | 527 BrowserThread::PostTask( |
| 528 BrowserThread::IO, FROM_HERE, | 528 BrowserThread::IO, FROM_HERE, |
| 529 NewRunnableMethod(this, | 529 base::Bind(&TestSBClient::CheckDownloadUrlOnIOThread, |
| 530 &TestSBClient::CheckDownloadUrlOnIOThread, | 530 this, url_chain)); |
| 531 url_chain)); | |
| 532 ui_test_utils::RunMessageLoop(); // Will stop in OnDownloadUrlCheckResult. | 531 ui_test_utils::RunMessageLoop(); // Will stop in OnDownloadUrlCheckResult. |
| 533 } | 532 } |
| 534 | 533 |
| 535 void CheckDownloadHash(const std::string& full_hash) { | 534 void CheckDownloadHash(const std::string& full_hash) { |
| 536 BrowserThread::PostTask( | 535 BrowserThread::PostTask( |
| 537 BrowserThread::IO, FROM_HERE, | 536 BrowserThread::IO, FROM_HERE, |
| 538 NewRunnableMethod(this, | 537 base::Bind(&TestSBClient::CheckDownloadHashOnIOThread, |
| 539 &TestSBClient::CheckDownloadHashOnIOThread, | 538 this, full_hash)); |
| 540 full_hash)); | |
| 541 ui_test_utils::RunMessageLoop(); // Will stop in OnDownloadHashCheckResult. | 539 ui_test_utils::RunMessageLoop(); // Will stop in OnDownloadHashCheckResult. |
| 542 } | 540 } |
| 543 | 541 |
| 544 private: | 542 private: |
| 545 void CheckDownloadUrlOnIOThread(const std::vector<GURL>& url_chain) { | 543 void CheckDownloadUrlOnIOThread(const std::vector<GURL>& url_chain) { |
| 546 safe_browsing_service_->CheckDownloadUrl(url_chain, this); | 544 safe_browsing_service_->CheckDownloadUrl(url_chain, this); |
| 547 } | 545 } |
| 548 | 546 |
| 549 void CheckDownloadHashOnIOThread(const std::string& full_hash) { | 547 void CheckDownloadHashOnIOThread(const std::string& full_hash) { |
| 550 safe_browsing_service_->CheckDownloadHash(full_hash, this); | 548 safe_browsing_service_->CheckDownloadHash(full_hash, this); |
| 551 } | 549 } |
| 552 | 550 |
| 553 // Called when the result of checking a download URL is known. | 551 // Called when the result of checking a download URL is known. |
| 554 void OnDownloadUrlCheckResult(const std::vector<GURL>& url_chain, | 552 void OnDownloadUrlCheckResult(const std::vector<GURL>& url_chain, |
| 555 SafeBrowsingService::UrlCheckResult result) { | 553 SafeBrowsingService::UrlCheckResult result) { |
| 556 result_ = result; | 554 result_ = result; |
| 557 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 555 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 558 NewRunnableMethod(this, &TestSBClient::DownloadCheckDone)); | 556 base::Bind(&TestSBClient::DownloadCheckDone, this)); |
| 559 } | 557 } |
| 560 | 558 |
| 561 // Called when the result of checking a download hash is known. | 559 // Called when the result of checking a download hash is known. |
| 562 void OnDownloadHashCheckResult(const std::string& hash, | 560 void OnDownloadHashCheckResult(const std::string& hash, |
| 563 SafeBrowsingService::UrlCheckResult result) { | 561 SafeBrowsingService::UrlCheckResult result) { |
| 564 result_ = result; | 562 result_ = result; |
| 565 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 563 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 566 NewRunnableMethod(this, &TestSBClient::DownloadCheckDone)); | 564 base::Bind(&TestSBClient::DownloadCheckDone, this)); |
| 567 } | 565 } |
| 568 | 566 |
| 569 void DownloadCheckDone() { | 567 void DownloadCheckDone() { |
| 570 MessageLoopForUI::current()->Quit(); | 568 MessageLoopForUI::current()->Quit(); |
| 571 } | 569 } |
| 572 | 570 |
| 573 SafeBrowsingService::UrlCheckResult result_; | 571 SafeBrowsingService::UrlCheckResult result_; |
| 574 SafeBrowsingService* safe_browsing_service_; | 572 SafeBrowsingService* safe_browsing_service_; |
| 575 | 573 |
| 576 DISALLOW_COPY_AND_ASSIGN(TestSBClient); | 574 DISALLOW_COPY_AND_ASSIGN(TestSBClient); |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 765 | 763 |
| 766 // Delete the Profile. SBS stops again. | 764 // Delete the Profile. SBS stops again. |
| 767 pref_service2 = NULL; | 765 pref_service2 = NULL; |
| 768 profile2.reset(); | 766 profile2.reset(); |
| 769 WaitForIOThread(); | 767 WaitForIOThread(); |
| 770 EXPECT_FALSE(sb_service->enabled()); | 768 EXPECT_FALSE(sb_service->enabled()); |
| 771 EXPECT_FALSE(csd_service->enabled()); | 769 EXPECT_FALSE(csd_service->enabled()); |
| 772 } | 770 } |
| 773 | 771 |
| 774 } // namespace | 772 } // namespace |
| OLD | NEW |