| OLD | NEW |
| 1 // Copyright (c) 2010 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 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 // |
| 11 // This test launches safebrowsing test server and issues several update | 11 // This test launches safebrowsing test server and issues several update |
| (...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 // A ref counted helper class that handles callbacks between IO thread and UI | 334 // A ref counted helper class that handles callbacks between IO thread and UI |
| 335 // thread. | 335 // thread. |
| 336 class SafeBrowsingServiceTestHelper | 336 class SafeBrowsingServiceTestHelper |
| 337 : public base::RefCountedThreadSafe<SafeBrowsingServiceTestHelper>, | 337 : public base::RefCountedThreadSafe<SafeBrowsingServiceTestHelper>, |
| 338 public SafeBrowsingService::Client, | 338 public SafeBrowsingService::Client, |
| 339 public URLFetcher::Delegate { | 339 public URLFetcher::Delegate { |
| 340 public: | 340 public: |
| 341 explicit SafeBrowsingServiceTestHelper( | 341 explicit SafeBrowsingServiceTestHelper( |
| 342 SafeBrowsingServiceTest* safe_browsing_test) | 342 SafeBrowsingServiceTest* safe_browsing_test) |
| 343 : safe_browsing_test_(safe_browsing_test), | 343 : safe_browsing_test_(safe_browsing_test), |
| 344 response_status_(URLRequestStatus::FAILED) { | 344 response_status_(net::URLRequestStatus::FAILED) { |
| 345 } | 345 } |
| 346 | 346 |
| 347 // Callbacks for SafeBrowsingService::Client. | 347 // Callbacks for SafeBrowsingService::Client. |
| 348 virtual void OnBrowseUrlCheckResult( | 348 virtual void OnBrowseUrlCheckResult( |
| 349 const GURL& url, SafeBrowsingService::UrlCheckResult result) { | 349 const GURL& url, SafeBrowsingService::UrlCheckResult result) { |
| 350 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 350 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 351 EXPECT_TRUE(safe_browsing_test_->is_checked_url_in_db()); | 351 EXPECT_TRUE(safe_browsing_test_->is_checked_url_in_db()); |
| 352 safe_browsing_test_->set_is_checked_url_safe( | 352 safe_browsing_test_->set_is_checked_url_safe( |
| 353 result == SafeBrowsingService::URL_SAFE); | 353 result == SafeBrowsingService::URL_SAFE); |
| 354 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 354 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 434 BrowserThread::IO, | 434 BrowserThread::IO, |
| 435 FROM_HERE, | 435 FROM_HERE, |
| 436 NewRunnableMethod(this, | 436 NewRunnableMethod(this, |
| 437 &SafeBrowsingServiceTestHelper::CheckStatusOnIOThread), | 437 &SafeBrowsingServiceTestHelper::CheckStatusOnIOThread), |
| 438 wait_time_msec); | 438 wait_time_msec); |
| 439 // Will continue after OnWaitForStatusUpdateDone(). | 439 // Will continue after OnWaitForStatusUpdateDone(). |
| 440 ui_test_utils::RunMessageLoop(); | 440 ui_test_utils::RunMessageLoop(); |
| 441 } | 441 } |
| 442 | 442 |
| 443 void WaitTillServerReady(const char* host, int port) { | 443 void WaitTillServerReady(const char* host, int port) { |
| 444 response_status_ = URLRequestStatus::FAILED; | 444 response_status_ = net::URLRequestStatus::FAILED; |
| 445 GURL url(StringPrintf("http://%s:%d%s?test_step=0", | 445 GURL url(StringPrintf("http://%s:%d%s?test_step=0", |
| 446 host, port, kDBResetPath)); | 446 host, port, kDBResetPath)); |
| 447 // TODO(lzheng): We should have a way to reliably tell when a server is | 447 // TODO(lzheng): We should have a way to reliably tell when a server is |
| 448 // ready so we could get rid of the Sleep and retry loop. | 448 // ready so we could get rid of the Sleep and retry loop. |
| 449 while (true) { | 449 while (true) { |
| 450 if (FetchUrl(url) == URLRequestStatus::SUCCESS) | 450 if (FetchUrl(url) == net::URLRequestStatus::SUCCESS) |
| 451 break; | 451 break; |
| 452 // Wait and try again if last fetch was failed. The loop will hit the | 452 // Wait and try again if last fetch was failed. The loop will hit the |
| 453 // timeout in OutOfProcTestRunner if the fetch can not get success | 453 // timeout in OutOfProcTestRunner if the fetch can not get success |
| 454 // response. | 454 // response. |
| 455 base::PlatformThread::Sleep(TestTimeouts::action_timeout_ms()); | 455 base::PlatformThread::Sleep(TestTimeouts::action_timeout_ms()); |
| 456 } | 456 } |
| 457 } | 457 } |
| 458 | 458 |
| 459 // Calls test server to fetch database for verification. | 459 // Calls test server to fetch database for verification. |
| 460 URLRequestStatus::Status FetchDBToVerify(const char* host, int port, | 460 net::URLRequestStatus::Status FetchDBToVerify(const char* host, int port, |
| 461 int test_step) { | 461 int test_step) { |
| 462 // TODO(lzheng): Remove chunk_type=add once it is not needed by the server. | 462 // TODO(lzheng): Remove chunk_type=add once it is not needed by the server. |
| 463 GURL url(StringPrintf("http://%s:%d%s?" | 463 GURL url(StringPrintf("http://%s:%d%s?" |
| 464 "client=chromium&appver=1.0&pver=2.2&test_step=%d&" | 464 "client=chromium&appver=1.0&pver=2.2&test_step=%d&" |
| 465 "chunk_type=add", | 465 "chunk_type=add", |
| 466 host, port, kDBVerifyPath, test_step)); | 466 host, port, kDBVerifyPath, test_step)); |
| 467 return FetchUrl(url); | 467 return FetchUrl(url); |
| 468 } | 468 } |
| 469 | 469 |
| 470 // Calls test server to fetch URLs for verification. | 470 // Calls test server to fetch URLs for verification. |
| 471 URLRequestStatus::Status FetchUrlsToVerify(const char* host, int port, | 471 net::URLRequestStatus::Status FetchUrlsToVerify(const char* host, int port, |
| 472 int test_step) { | 472 int test_step) { |
| 473 GURL url(StringPrintf("http://%s:%d%s?" | 473 GURL url(StringPrintf("http://%s:%d%s?" |
| 474 "client=chromium&appver=1.0&pver=2.2&test_step=%d", | 474 "client=chromium&appver=1.0&pver=2.2&test_step=%d", |
| 475 host, port, kUrlVerifyPath, test_step)); | 475 host, port, kUrlVerifyPath, test_step)); |
| 476 return FetchUrl(url); | 476 return FetchUrl(url); |
| 477 } | 477 } |
| 478 | 478 |
| 479 // Calls test server to check if test data is done. E.g.: if there is a | 479 // Calls test server to check if test data is done. E.g.: if there is a |
| 480 // bad URL that server expects test to fetch full hash but the test didn't, | 480 // bad URL that server expects test to fetch full hash but the test didn't, |
| 481 // this verification will fail. | 481 // this verification will fail. |
| 482 URLRequestStatus::Status VerifyTestComplete(const char* host, int port, | 482 net::URLRequestStatus::Status VerifyTestComplete(const char* host, int port, |
| 483 int test_step) { | 483 int test_step) { |
| 484 GURL url(StringPrintf("http://%s:%d%s?test_step=%d", | 484 GURL url(StringPrintf("http://%s:%d%s?test_step=%d", |
| 485 host, port, kTestCompletePath, test_step)); | 485 host, port, kTestCompletePath, test_step)); |
| 486 return FetchUrl(url); | 486 return FetchUrl(url); |
| 487 } | 487 } |
| 488 | 488 |
| 489 // Callback for URLFetcher. | 489 // Callback for URLFetcher. |
| 490 virtual void OnURLFetchComplete(const URLFetcher* source, | 490 virtual void OnURLFetchComplete(const URLFetcher* source, |
| 491 const GURL& url, | 491 const GURL& url, |
| 492 const URLRequestStatus& status, | 492 const net::URLRequestStatus& status, |
| 493 int response_code, | 493 int response_code, |
| 494 const ResponseCookies& cookies, | 494 const ResponseCookies& cookies, |
| 495 const std::string& data) { | 495 const std::string& data) { |
| 496 response_data_ = data; | 496 response_data_ = data; |
| 497 response_status_ = status.status(); | 497 response_status_ = status.status(); |
| 498 StopUILoop(); | 498 StopUILoop(); |
| 499 } | 499 } |
| 500 | 500 |
| 501 const std::string& response_data() { | 501 const std::string& response_data() { |
| 502 return response_data_; | 502 return response_data_; |
| 503 } | 503 } |
| 504 | 504 |
| 505 private: | 505 private: |
| 506 // Stops UI loop after desired status is updated. | 506 // Stops UI loop after desired status is updated. |
| 507 void StopUILoop() { | 507 void StopUILoop() { |
| 508 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 508 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 509 MessageLoopForUI::current()->Quit(); | 509 MessageLoopForUI::current()->Quit(); |
| 510 } | 510 } |
| 511 | 511 |
| 512 // Fetch a URL. If message_loop_started is true, starts the message loop | 512 // Fetch a URL. If message_loop_started is true, starts the message loop |
| 513 // so the caller could wait till OnURLFetchComplete is called. | 513 // so the caller could wait till OnURLFetchComplete is called. |
| 514 URLRequestStatus::Status FetchUrl(const GURL& url) { | 514 net::URLRequestStatus::Status FetchUrl(const GURL& url) { |
| 515 url_fetcher_.reset(new URLFetcher(url, URLFetcher::GET, this)); | 515 url_fetcher_.reset(new URLFetcher(url, URLFetcher::GET, this)); |
| 516 url_fetcher_->set_load_flags(net::LOAD_DISABLE_CACHE); | 516 url_fetcher_->set_load_flags(net::LOAD_DISABLE_CACHE); |
| 517 url_fetcher_->set_request_context(Profile::GetDefaultRequestContext()); | 517 url_fetcher_->set_request_context(Profile::GetDefaultRequestContext()); |
| 518 url_fetcher_->Start(); | 518 url_fetcher_->Start(); |
| 519 ui_test_utils::RunMessageLoop(); | 519 ui_test_utils::RunMessageLoop(); |
| 520 return response_status_; | 520 return response_status_; |
| 521 } | 521 } |
| 522 | 522 |
| 523 base::OneShotTimer<SafeBrowsingServiceTestHelper> check_update_timer_; | 523 base::OneShotTimer<SafeBrowsingServiceTestHelper> check_update_timer_; |
| 524 SafeBrowsingServiceTest* safe_browsing_test_; | 524 SafeBrowsingServiceTest* safe_browsing_test_; |
| 525 scoped_ptr<URLFetcher> url_fetcher_; | 525 scoped_ptr<URLFetcher> url_fetcher_; |
| 526 std::string response_data_; | 526 std::string response_data_; |
| 527 URLRequestStatus::Status response_status_; | 527 net::URLRequestStatus::Status response_status_; |
| 528 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceTestHelper); | 528 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceTestHelper); |
| 529 }; | 529 }; |
| 530 | 530 |
| 531 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, SafeBrowsingSystemTest) { | 531 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, SafeBrowsingSystemTest) { |
| 532 LOG(INFO) << "Start test"; | 532 LOG(INFO) << "Start test"; |
| 533 const char* server_host = SafeBrowsingTestServer::Host(); | 533 const char* server_host = SafeBrowsingTestServer::Host(); |
| 534 int server_port = SafeBrowsingTestServer::Port(); | 534 int server_port = SafeBrowsingTestServer::Port(); |
| 535 ASSERT_TRUE(InitSafeBrowsingService()); | 535 ASSERT_TRUE(InitSafeBrowsingService()); |
| 536 | 536 |
| 537 scoped_refptr<SafeBrowsingServiceTestHelper> safe_browsing_helper( | 537 scoped_refptr<SafeBrowsingServiceTestHelper> safe_browsing_helper( |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 577 } while (is_update_scheduled() || is_initial_request() || | 577 } while (is_update_scheduled() || is_initial_request() || |
| 578 !is_database_ready()); | 578 !is_database_ready()); |
| 579 | 579 |
| 580 | 580 |
| 581 if (last_update() < now) { | 581 if (last_update() < now) { |
| 582 // This means no data available anymore. | 582 // This means no data available anymore. |
| 583 break; | 583 break; |
| 584 } | 584 } |
| 585 | 585 |
| 586 // Fetches URLs to verify and waits till server responses with data. | 586 // Fetches URLs to verify and waits till server responses with data. |
| 587 EXPECT_EQ(URLRequestStatus::SUCCESS, | 587 EXPECT_EQ(net::URLRequestStatus::SUCCESS, |
| 588 safe_browsing_helper->FetchUrlsToVerify(server_host, | 588 safe_browsing_helper->FetchUrlsToVerify(server_host, |
| 589 server_port, | 589 server_port, |
| 590 step)); | 590 step)); |
| 591 | 591 |
| 592 std::vector<PhishingUrl> phishing_urls; | 592 std::vector<PhishingUrl> phishing_urls; |
| 593 EXPECT_TRUE(ParsePhishingUrls(safe_browsing_helper->response_data(), | 593 EXPECT_TRUE(ParsePhishingUrls(safe_browsing_helper->response_data(), |
| 594 &phishing_urls)); | 594 &phishing_urls)); |
| 595 EXPECT_GT(phishing_urls.size(), 0U); | 595 EXPECT_GT(phishing_urls.size(), 0U); |
| 596 for (size_t j = 0; j < phishing_urls.size(); ++j) { | 596 for (size_t j = 0; j < phishing_urls.size(); ++j) { |
| 597 // Verifes with server if a URL is a phishing URL and waits till server | 597 // Verifes with server if a URL is a phishing URL and waits till server |
| (...skipping 10 matching lines...) Expand all Loading... |
| 608 << " test step: " << step; | 608 << " test step: " << step; |
| 609 } else { | 609 } else { |
| 610 EXPECT_TRUE(is_checked_url_safe()) | 610 EXPECT_TRUE(is_checked_url_safe()) |
| 611 << phishing_urls[j].url | 611 << phishing_urls[j].url |
| 612 << " is_phishing: " << phishing_urls[j].is_phishing | 612 << " is_phishing: " << phishing_urls[j].is_phishing |
| 613 << " test step: " << step; | 613 << " test step: " << step; |
| 614 } | 614 } |
| 615 } | 615 } |
| 616 // TODO(lzheng): We should verify the fetched database with local | 616 // TODO(lzheng): We should verify the fetched database with local |
| 617 // database to make sure they match. | 617 // database to make sure they match. |
| 618 EXPECT_EQ(URLRequestStatus::SUCCESS, | 618 EXPECT_EQ(net::URLRequestStatus::SUCCESS, |
| 619 safe_browsing_helper->FetchDBToVerify(server_host, | 619 safe_browsing_helper->FetchDBToVerify(server_host, |
| 620 server_port, | 620 server_port, |
| 621 step)); | 621 step)); |
| 622 EXPECT_GT(safe_browsing_helper->response_data().size(), 0U); | 622 EXPECT_GT(safe_browsing_helper->response_data().size(), 0U); |
| 623 last_step = step; | 623 last_step = step; |
| 624 } | 624 } |
| 625 | 625 |
| 626 // Verifies with server if test is done and waits till server responses. | 626 // Verifies with server if test is done and waits till server responses. |
| 627 EXPECT_EQ(URLRequestStatus::SUCCESS, | 627 EXPECT_EQ(net::URLRequestStatus::SUCCESS, |
| 628 safe_browsing_helper->VerifyTestComplete(server_host, | 628 safe_browsing_helper->VerifyTestComplete(server_host, |
| 629 server_port, | 629 server_port, |
| 630 last_step)); | 630 last_step)); |
| 631 EXPECT_EQ("yes", safe_browsing_helper->response_data()); | 631 EXPECT_EQ("yes", safe_browsing_helper->response_data()); |
| 632 test_server.Stop(); | 632 test_server.Stop(); |
| 633 } | 633 } |
| OLD | NEW |