OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 uses the safebrowsing test server published at | 5 // This test uses the safebrowsing test server published at |
6 // http://code.google.com/p/google-safe-browsing/ to test the safebrowsing | 6 // http://code.google.com/p/google-safe-browsing/ to test the safebrowsing |
7 // protocol implemetation. Details of the safebrowsing testing flow is | 7 // protocol implemetation. Details of the safebrowsing testing flow is |
8 // documented at | 8 // documented at |
9 // http://code.google.com/p/google-safe-browsing/wiki/ProtocolTesting | 9 // http://code.google.com/p/google-safe-browsing/wiki/ProtocolTesting |
10 // | 10 // |
(...skipping 15 matching lines...) Expand all Loading... | |
26 #include "base/stringprintf.h" | 26 #include "base/stringprintf.h" |
27 #include "base/synchronization/lock.h" | 27 #include "base/synchronization/lock.h" |
28 #include "base/test/test_timeouts.h" | 28 #include "base/test/test_timeouts.h" |
29 #include "base/threading/platform_thread.h" | 29 #include "base/threading/platform_thread.h" |
30 #include "base/threading/thread.h" | 30 #include "base/threading/thread.h" |
31 #include "base/time.h" | 31 #include "base/time.h" |
32 #include "base/utf_string_conversions.h" | 32 #include "base/utf_string_conversions.h" |
33 #include "chrome/browser/browser_process.h" | 33 #include "chrome/browser/browser_process.h" |
34 #include "chrome/browser/safe_browsing/protocol_manager.h" | 34 #include "chrome/browser/safe_browsing/protocol_manager.h" |
35 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 35 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
36 #include "chrome/common/chrome_notification_types.h" | |
36 #include "chrome/common/chrome_switches.h" | 37 #include "chrome/common/chrome_switches.h" |
37 #include "chrome/common/url_constants.h" | 38 #include "chrome/common/url_constants.h" |
38 #include "chrome/test/base/in_process_browser_test.h" | 39 #include "chrome/test/base/in_process_browser_test.h" |
39 #include "chrome/test/base/ui_test_utils.h" | 40 #include "chrome/test/base/ui_test_utils.h" |
40 #include "content/public/browser/browser_context.h" | 41 #include "content/public/browser/browser_context.h" |
41 #include "content/public/test/test_browser_thread.h" | 42 #include "content/public/test/test_browser_thread.h" |
42 #include "net/base/host_resolver.h" | 43 #include "net/base/host_resolver.h" |
43 #include "net/base/load_flags.h" | 44 #include "net/base/load_flags.h" |
44 #include "net/base/net_log.h" | 45 #include "net/base/net_log.h" |
45 #include "net/test/python_utils.h" | 46 #include "net/test/python_utils.h" |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
220 | 221 |
221 void UpdateSafeBrowsingStatus() { | 222 void UpdateSafeBrowsingStatus() { |
222 ASSERT_TRUE(safe_browsing_service_); | 223 ASSERT_TRUE(safe_browsing_service_); |
223 base::AutoLock lock(update_status_mutex_); | 224 base::AutoLock lock(update_status_mutex_); |
224 last_update_ = safe_browsing_service_->protocol_manager_->last_update(); | 225 last_update_ = safe_browsing_service_->protocol_manager_->last_update(); |
225 is_update_scheduled_ = | 226 is_update_scheduled_ = |
226 safe_browsing_service_->protocol_manager_->update_timer_.IsRunning(); | 227 safe_browsing_service_->protocol_manager_->update_timer_.IsRunning(); |
227 } | 228 } |
228 | 229 |
229 void ForceUpdate() { | 230 void ForceUpdate() { |
231 content::WindowedNotificationObserver observer( | |
232 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, | |
233 content::Source<SafeBrowsingService>(safe_browsing_service_)); | |
234 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
235 base::Bind(&SafeBrowsingServiceTest::ForceUpdateOnIOThread, | |
236 this)); | |
237 observer.Wait(); | |
238 } | |
239 | |
240 void ForceUpdateOnIOThread() { | |
241 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
230 ASSERT_TRUE(safe_browsing_service_); | 242 ASSERT_TRUE(safe_browsing_service_); |
231 safe_browsing_service_->protocol_manager_->ForceScheduleNextUpdate(0); | 243 safe_browsing_service_->protocol_manager_->ForceScheduleNextUpdate(0); |
232 } | 244 } |
233 | 245 |
234 void CheckIsDatabaseReady() { | 246 void CheckIsDatabaseReady() { |
235 base::AutoLock lock(update_status_mutex_); | 247 base::AutoLock lock(update_status_mutex_); |
236 is_database_ready_ = | 248 is_database_ready_ = |
237 !safe_browsing_service_->database_update_in_progress_; | 249 !safe_browsing_service_->database_update_in_progress_; |
238 } | 250 } |
239 | 251 |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
371 virtual void OnDownloadUrlCheckResult( | 383 virtual void OnDownloadUrlCheckResult( |
372 const std::vector<GURL>& url_chain, | 384 const std::vector<GURL>& url_chain, |
373 SafeBrowsingService::UrlCheckResult result) { | 385 SafeBrowsingService::UrlCheckResult result) { |
374 // TODO(lzheng): Add test for DownloadUrl. | 386 // TODO(lzheng): Add test for DownloadUrl. |
375 } | 387 } |
376 | 388 |
377 virtual void OnBlockingPageComplete(bool proceed) { | 389 virtual void OnBlockingPageComplete(bool proceed) { |
378 NOTREACHED() << "Not implemented."; | 390 NOTREACHED() << "Not implemented."; |
379 } | 391 } |
380 | 392 |
381 // Functions and callbacks to start the safebrowsing database update. | |
382 void ForceUpdate() { | |
383 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
384 base::Bind(&SafeBrowsingServiceTestHelper::ForceUpdateInIOThread, | |
385 this)); | |
386 // Will continue after OnForceUpdateDone(). | |
387 content::RunMessageLoop(); | |
388 } | |
389 void ForceUpdateInIOThread() { | |
390 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
391 safe_browsing_test_->ForceUpdate(); | |
392 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
393 base::Bind(&SafeBrowsingServiceTestHelper::OnForceUpdateDone, | |
394 this)); | |
395 } | |
396 void OnForceUpdateDone() { | |
397 StopUILoop(); | |
398 } | |
399 | |
400 // Functions and callbacks related to CheckUrl. These are used to verify | 393 // Functions and callbacks related to CheckUrl. These are used to verify |
401 // phishing URLs. | 394 // phishing URLs. |
402 void CheckUrl(const GURL& url) { | 395 void CheckUrl(const GURL& url) { |
403 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 396 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
404 base::Bind(&SafeBrowsingServiceTestHelper::CheckUrlOnIOThread, | 397 base::Bind(&SafeBrowsingServiceTestHelper::CheckUrlOnIOThread, |
405 this, url)); | 398 this, url)); |
406 content::RunMessageLoop(); | 399 content::RunMessageLoop(); |
407 } | 400 } |
408 void CheckUrlOnIOThread(const GURL& url) { | 401 void CheckUrlOnIOThread(const GURL& url) { |
409 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 402 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
(...skipping 27 matching lines...) Expand all Loading... | |
437 safe_browsing_test_->CheckIsDatabaseReady(); | 430 safe_browsing_test_->CheckIsDatabaseReady(); |
438 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 431 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
439 base::Bind(&SafeBrowsingServiceTestHelper::OnWaitForStatusUpdateDone, | 432 base::Bind(&SafeBrowsingServiceTestHelper::OnWaitForStatusUpdateDone, |
440 this)); | 433 this)); |
441 } | 434 } |
442 | 435 |
443 void OnWaitForStatusUpdateDone() { | 436 void OnWaitForStatusUpdateDone() { |
444 StopUILoop(); | 437 StopUILoop(); |
445 } | 438 } |
446 | 439 |
447 // Wait for a given period to get safebrowsing status updated. | 440 // Wait for a given period to get safebrowsing status updated. |
Brian Ryner
2012/08/09 18:10:36
Is the "for a given period" part of this comment s
mattm
2012/08/10 00:12:53
nope, removed. thanks
| |
448 void WaitForStatusUpdate(base::TimeDelta wait_time) { | 441 void UpdateStatus() { |
449 BrowserThread::PostDelayedTask( | 442 BrowserThread::PostTask( |
450 BrowserThread::IO, | 443 BrowserThread::IO, |
451 FROM_HERE, | 444 FROM_HERE, |
452 base::Bind(&SafeBrowsingServiceTestHelper::CheckStatusOnIOThread, | 445 base::Bind(&SafeBrowsingServiceTestHelper::CheckStatusOnIOThread, |
453 this), | 446 this)); |
454 wait_time); | |
455 // Will continue after OnWaitForStatusUpdateDone(). | 447 // Will continue after OnWaitForStatusUpdateDone(). |
456 content::RunMessageLoop(); | 448 content::RunMessageLoop(); |
457 } | 449 } |
458 | 450 |
459 void WaitTillServerReady(const char* host, int port) { | 451 void WaitTillServerReady(const char* host, int port) { |
460 response_status_ = net::URLRequestStatus::FAILED; | 452 response_status_ = net::URLRequestStatus::FAILED; |
461 GURL url(base::StringPrintf("http://%s:%d%s?test_step=0", | 453 GURL url(base::StringPrintf("http://%s:%d%s?test_step=0", |
462 host, port, kDBResetPath)); | 454 host, port, kDBResetPath)); |
463 // TODO(lzheng): We should have a way to reliably tell when a server is | 455 // TODO(lzheng): We should have a way to reliably tell when a server is |
464 // ready so we could get rid of the Sleep and retry loop. | 456 // ready so we could get rid of the Sleep and retry loop. |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
563 SafeBrowsingTestServer test_server(datafile_path); | 555 SafeBrowsingTestServer test_server(datafile_path); |
564 ASSERT_TRUE(test_server.Start()); | 556 ASSERT_TRUE(test_server.Start()); |
565 | 557 |
566 // Make sure the server is running. | 558 // Make sure the server is running. |
567 safe_browsing_helper->WaitTillServerReady(server_host, server_port); | 559 safe_browsing_helper->WaitTillServerReady(server_host, server_port); |
568 | 560 |
569 // Waits and makes sure safebrowsing update is not happening. | 561 // Waits and makes sure safebrowsing update is not happening. |
570 // The wait will stop once OnWaitForStatusUpdateDone in | 562 // The wait will stop once OnWaitForStatusUpdateDone in |
571 // safe_browsing_helper is called and status from safe_browsing_service_ | 563 // safe_browsing_helper is called and status from safe_browsing_service_ |
572 // is checked. | 564 // is checked. |
573 safe_browsing_helper->WaitForStatusUpdate(base::TimeDelta()); | 565 safe_browsing_helper->UpdateStatus(); |
574 EXPECT_TRUE(is_database_ready()); | 566 EXPECT_TRUE(is_database_ready()); |
575 EXPECT_FALSE(is_update_scheduled()); | 567 EXPECT_FALSE(is_update_scheduled()); |
576 EXPECT_TRUE(last_update().is_null()); | 568 EXPECT_TRUE(last_update().is_null()); |
577 // Starts updates. After each update, the test will fetch a list of URLs with | 569 // Starts updates. After each update, the test will fetch a list of URLs with |
578 // expected results to verify with safebrowsing service. If there is no error, | 570 // expected results to verify with safebrowsing service. If there is no error, |
579 // the test moves on to the next step to get more update chunks. | 571 // the test moves on to the next step to get more update chunks. |
580 // This repeats till there is no update data. | 572 // This repeats till there is no update data. |
581 for (int step = 1;; step++) { | 573 for (int step = 1;; step++) { |
582 // Every step should be a fresh start. | 574 // Every step should be a fresh start. |
583 SCOPED_TRACE(base::StringPrintf("step=%d", step)); | 575 SCOPED_TRACE(base::StringPrintf("step=%d", step)); |
584 EXPECT_TRUE(is_database_ready()); | 576 EXPECT_TRUE(is_database_ready()); |
585 EXPECT_FALSE(is_update_scheduled()); | 577 EXPECT_FALSE(is_update_scheduled()); |
586 | 578 |
587 // Starts safebrowsing update on IO thread. Waits till scheduled | 579 // Starts safebrowsing update on IO thread. Waits till scheduled |
588 // update finishes. Stops waiting after kMaxWaitSecPerStep if the update | 580 // update finishes. Stops waiting after kMaxWaitSecPerStep if the update |
589 // could not finish. | 581 // could not finish. |
590 base::Time now = base::Time::Now(); | 582 base::Time now = base::Time::Now(); |
591 SetTestStep(step); | 583 SetTestStep(step); |
592 safe_browsing_helper->ForceUpdate(); | 584 ForceUpdate(); |
593 | 585 |
594 // TODO(mattm): use NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE instead. | 586 safe_browsing_helper->UpdateStatus(); |
595 do { | 587 EXPECT_TRUE(is_database_ready()); |
596 // Periodically pull the status. | 588 EXPECT_FALSE(is_update_scheduled()); |
597 safe_browsing_helper->WaitForStatusUpdate( | 589 EXPECT_FALSE(last_update().is_null()); |
598 TestTimeouts::tiny_timeout()); | |
599 } while (is_update_scheduled() || !is_database_ready()); | |
600 | |
601 | |
602 if (last_update() < now) { | 590 if (last_update() < now) { |
603 // This means no data available anymore. | 591 // This means no data available anymore. |
604 break; | 592 break; |
605 } | 593 } |
606 | 594 |
607 // Fetches URLs to verify and waits till server responses with data. | 595 // Fetches URLs to verify and waits till server responses with data. |
608 EXPECT_EQ(net::URLRequestStatus::SUCCESS, | 596 EXPECT_EQ(net::URLRequestStatus::SUCCESS, |
609 safe_browsing_helper->FetchUrlsToVerify(server_host, | 597 safe_browsing_helper->FetchUrlsToVerify(server_host, |
610 server_port, | 598 server_port, |
611 step)); | 599 step)); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
645 } | 633 } |
646 | 634 |
647 // Verifies with server if test is done and waits till server responses. | 635 // Verifies with server if test is done and waits till server responses. |
648 EXPECT_EQ(net::URLRequestStatus::SUCCESS, | 636 EXPECT_EQ(net::URLRequestStatus::SUCCESS, |
649 safe_browsing_helper->VerifyTestComplete(server_host, | 637 safe_browsing_helper->VerifyTestComplete(server_host, |
650 server_port, | 638 server_port, |
651 last_step)); | 639 last_step)); |
652 EXPECT_EQ("yes", safe_browsing_helper->response_data()); | 640 EXPECT_EQ("yes", safe_browsing_helper->response_data()); |
653 test_server.Stop(); | 641 test_server.Stop(); |
654 } | 642 } |
OLD | NEW |