| 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 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 return false; | 104 return false; |
| 105 } | 105 } |
| 106 phishing_urls->push_back(phishing_url); | 106 phishing_urls->push_back(phishing_url); |
| 107 } | 107 } |
| 108 return true; | 108 return true; |
| 109 } | 109 } |
| 110 | 110 |
| 111 } // namespace | 111 } // namespace |
| 112 | 112 |
| 113 // This starts the browser and keeps status of states related to SafeBrowsing. | 113 // This starts the browser and keeps status of states related to SafeBrowsing. |
| 114 class SafeBrowsingServiceTest : public InProcessBrowserTest { | 114 class SafeBrowsingServerTest : public InProcessBrowserTest { |
| 115 public: | 115 public: |
| 116 SafeBrowsingServiceTest() | 116 SafeBrowsingServerTest() |
| 117 : safe_browsing_service_(NULL), | 117 : safe_browsing_service_(NULL), |
| 118 is_database_ready_(true), | 118 is_database_ready_(true), |
| 119 is_update_scheduled_(false), | 119 is_update_scheduled_(false), |
| 120 is_checked_url_in_db_(false), | 120 is_checked_url_in_db_(false), |
| 121 is_checked_url_safe_(false) { | 121 is_checked_url_safe_(false) { |
| 122 } | 122 } |
| 123 | 123 |
| 124 virtual ~SafeBrowsingServiceTest() { | 124 virtual ~SafeBrowsingServerTest() { |
| 125 } | 125 } |
| 126 | 126 |
| 127 void UpdateSafeBrowsingStatus() { | 127 void UpdateSafeBrowsingStatus() { |
| 128 ASSERT_TRUE(safe_browsing_service_); | 128 ASSERT_TRUE(safe_browsing_service_); |
| 129 base::AutoLock lock(update_status_mutex_); | 129 base::AutoLock lock(update_status_mutex_); |
| 130 last_update_ = safe_browsing_service_->protocol_manager_->last_update(); | 130 last_update_ = safe_browsing_service_->protocol_manager_->last_update(); |
| 131 is_update_scheduled_ = | 131 is_update_scheduled_ = |
| 132 safe_browsing_service_->protocol_manager_->update_timer_.IsRunning(); | 132 safe_browsing_service_->protocol_manager_->update_timer_.IsRunning(); |
| 133 } | 133 } |
| 134 | 134 |
| 135 void ForceUpdate() { | 135 void ForceUpdate() { |
| 136 content::WindowedNotificationObserver observer( | 136 content::WindowedNotificationObserver observer( |
| 137 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, | 137 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, |
| 138 content::Source<SafeBrowsingService>(safe_browsing_service_)); | 138 content::Source<SafeBrowsingService>(safe_browsing_service_)); |
| 139 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 139 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 140 base::Bind(&SafeBrowsingServiceTest::ForceUpdateOnIOThread, | 140 base::Bind(&SafeBrowsingServerTest::ForceUpdateOnIOThread, |
| 141 this)); | 141 this)); |
| 142 observer.Wait(); | 142 observer.Wait(); |
| 143 } | 143 } |
| 144 | 144 |
| 145 void ForceUpdateOnIOThread() { | 145 void ForceUpdateOnIOThread() { |
| 146 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 146 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 147 ASSERT_TRUE(safe_browsing_service_); | 147 ASSERT_TRUE(safe_browsing_service_); |
| 148 safe_browsing_service_->protocol_manager_->ForceScheduleNextUpdate( | 148 safe_browsing_service_->protocol_manager_->ForceScheduleNextUpdate( |
| 149 base::TimeDelta::FromSeconds(0)); | 149 base::TimeDelta::FromSeconds(0)); |
| 150 } | 150 } |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 // States associated with safebrowsing service updates. | 265 // States associated with safebrowsing service updates. |
| 266 bool is_database_ready_; | 266 bool is_database_ready_; |
| 267 base::Time last_update_; | 267 base::Time last_update_; |
| 268 bool is_update_scheduled_; | 268 bool is_update_scheduled_; |
| 269 // Indicates if there is a match between a URL's prefix and safebrowsing | 269 // Indicates if there is a match between a URL's prefix and safebrowsing |
| 270 // database (thus potentially it is a phishing URL). | 270 // database (thus potentially it is a phishing URL). |
| 271 bool is_checked_url_in_db_; | 271 bool is_checked_url_in_db_; |
| 272 // True if last verified URL is not a phishing URL and thus it is safe. | 272 // True if last verified URL is not a phishing URL and thus it is safe. |
| 273 bool is_checked_url_safe_; | 273 bool is_checked_url_safe_; |
| 274 | 274 |
| 275 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceTest); | 275 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServerTest); |
| 276 }; | 276 }; |
| 277 | 277 |
| 278 // A ref counted helper class that handles callbacks between IO thread and UI | 278 // A ref counted helper class that handles callbacks between IO thread and UI |
| 279 // thread. | 279 // thread. |
| 280 class SafeBrowsingServiceTestHelper | 280 class SafeBrowsingServerTestHelper |
| 281 : public base::RefCountedThreadSafe<SafeBrowsingServiceTestHelper>, | 281 : public base::RefCountedThreadSafe<SafeBrowsingServerTestHelper>, |
| 282 public SafeBrowsingService::Client, | 282 public SafeBrowsingService::Client, |
| 283 public net::URLFetcherDelegate { | 283 public net::URLFetcherDelegate { |
| 284 public: | 284 public: |
| 285 SafeBrowsingServiceTestHelper(SafeBrowsingServiceTest* safe_browsing_test, | 285 SafeBrowsingServerTestHelper(SafeBrowsingServerTest* safe_browsing_test, |
| 286 net::URLRequestContextGetter* request_context) | 286 net::URLRequestContextGetter* request_context) |
| 287 : safe_browsing_test_(safe_browsing_test), | 287 : safe_browsing_test_(safe_browsing_test), |
| 288 response_status_(net::URLRequestStatus::FAILED), | 288 response_status_(net::URLRequestStatus::FAILED), |
| 289 request_context_(request_context) { | 289 request_context_(request_context) { |
| 290 } | 290 } |
| 291 | 291 |
| 292 // Callbacks for SafeBrowsingService::Client. | 292 // Callbacks for SafeBrowsingService::Client. |
| 293 virtual void OnCheckBrowseUrlResult(const GURL& url, | 293 virtual void OnCheckBrowseUrlResult(const GURL& url, |
| 294 SBThreatType threat_type) OVERRIDE { | 294 SBThreatType threat_type) OVERRIDE { |
| 295 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 295 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 296 EXPECT_TRUE(safe_browsing_test_->is_checked_url_in_db()); | 296 EXPECT_TRUE(safe_browsing_test_->is_checked_url_in_db()); |
| 297 safe_browsing_test_->set_is_checked_url_safe( | 297 safe_browsing_test_->set_is_checked_url_safe( |
| 298 threat_type == SB_THREAT_TYPE_SAFE); | 298 threat_type == SB_THREAT_TYPE_SAFE); |
| 299 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 299 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 300 base::Bind(&SafeBrowsingServiceTestHelper::OnCheckUrlDone, | 300 base::Bind(&SafeBrowsingServerTestHelper::OnCheckUrlDone, this)); |
| 301 this)); | |
| 302 } | 301 } |
| 303 | 302 |
| 304 virtual void OnBlockingPageComplete(bool proceed) { | 303 virtual void OnBlockingPageComplete(bool proceed) { |
| 305 NOTREACHED() << "Not implemented."; | 304 NOTREACHED() << "Not implemented."; |
| 306 } | 305 } |
| 307 | 306 |
| 308 // Functions and callbacks related to CheckUrl. These are used to verify | 307 // Functions and callbacks related to CheckUrl. These are used to verify |
| 309 // phishing URLs. | 308 // phishing URLs. |
| 310 void CheckUrl(const GURL& url) { | 309 void CheckUrl(const GURL& url) { |
| 311 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 310 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 312 base::Bind(&SafeBrowsingServiceTestHelper::CheckUrlOnIOThread, | 311 base::Bind(&SafeBrowsingServerTestHelper::CheckUrlOnIOThread, |
| 313 this, url)); | 312 this, url)); |
| 314 content::RunMessageLoop(); | 313 content::RunMessageLoop(); |
| 315 } | 314 } |
| 316 void CheckUrlOnIOThread(const GURL& url) { | 315 void CheckUrlOnIOThread(const GURL& url) { |
| 317 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 316 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 318 safe_browsing_test_->CheckUrl(this, url); | 317 safe_browsing_test_->CheckUrl(this, url); |
| 319 if (!safe_browsing_test_->is_checked_url_in_db()) { | 318 if (!safe_browsing_test_->is_checked_url_in_db()) { |
| 320 // Ends the checking since this URL's prefix is not in database. | 319 // Ends the checking since this URL's prefix is not in database. |
| 321 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 320 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 322 base::Bind(&SafeBrowsingServiceTestHelper::OnCheckUrlDone, | 321 base::Bind(&SafeBrowsingServerTestHelper::OnCheckUrlDone, this)); |
| 323 this)); | |
| 324 } | 322 } |
| 325 // Otherwise, OnCheckUrlDone is called in OnUrlCheckResult since | 323 // Otherwise, OnCheckUrlDone is called in OnUrlCheckResult since |
| 326 // safebrowsing service further fetches hashes from safebrowsing server. | 324 // safebrowsing service further fetches hashes from safebrowsing server. |
| 327 } | 325 } |
| 328 | 326 |
| 329 void OnCheckUrlDone() { | 327 void OnCheckUrlDone() { |
| 330 StopUILoop(); | 328 StopUILoop(); |
| 331 } | 329 } |
| 332 | 330 |
| 333 // Updates status from IO Thread. | 331 // Updates status from IO Thread. |
| 334 void CheckStatusOnIOThread() { | 332 void CheckStatusOnIOThread() { |
| 335 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 333 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 336 safe_browsing_test_->UpdateSafeBrowsingStatus(); | 334 safe_browsing_test_->UpdateSafeBrowsingStatus(); |
| 337 safe_browsing_test_->SafeBrowsingMessageLoop()->PostTask(FROM_HERE, | 335 safe_browsing_test_->SafeBrowsingMessageLoop()->PostTask(FROM_HERE, |
| 338 base::Bind(&SafeBrowsingServiceTestHelper::CheckIsDatabaseReady, this)); | 336 base::Bind(&SafeBrowsingServerTestHelper::CheckIsDatabaseReady, this)); |
| 339 } | 337 } |
| 340 | 338 |
| 341 // Checks status in SafeBrowsing Thread. | 339 // Checks status in SafeBrowsing Thread. |
| 342 void CheckIsDatabaseReady() { | 340 void CheckIsDatabaseReady() { |
| 343 EXPECT_EQ(MessageLoop::current(), | 341 EXPECT_EQ(MessageLoop::current(), |
| 344 safe_browsing_test_->SafeBrowsingMessageLoop()); | 342 safe_browsing_test_->SafeBrowsingMessageLoop()); |
| 345 safe_browsing_test_->CheckIsDatabaseReady(); | 343 safe_browsing_test_->CheckIsDatabaseReady(); |
| 346 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 344 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 347 base::Bind(&SafeBrowsingServiceTestHelper::OnWaitForStatusUpdateDone, | 345 base::Bind(&SafeBrowsingServerTestHelper::OnWaitForStatusUpdateDone, |
| 348 this)); | 346 this)); |
| 349 } | 347 } |
| 350 | 348 |
| 351 void OnWaitForStatusUpdateDone() { | 349 void OnWaitForStatusUpdateDone() { |
| 352 StopUILoop(); | 350 StopUILoop(); |
| 353 } | 351 } |
| 354 | 352 |
| 355 // Update safebrowsing status. | 353 // Update safebrowsing status. |
| 356 void UpdateStatus() { | 354 void UpdateStatus() { |
| 357 BrowserThread::PostTask( | 355 BrowserThread::PostTask( |
| 358 BrowserThread::IO, | 356 BrowserThread::IO, |
| 359 FROM_HERE, | 357 FROM_HERE, |
| 360 base::Bind(&SafeBrowsingServiceTestHelper::CheckStatusOnIOThread, | 358 base::Bind(&SafeBrowsingServerTestHelper::CheckStatusOnIOThread, this)); |
| 361 this)); | |
| 362 // Will continue after OnWaitForStatusUpdateDone(). | 359 // Will continue after OnWaitForStatusUpdateDone(). |
| 363 content::RunMessageLoop(); | 360 content::RunMessageLoop(); |
| 364 } | 361 } |
| 365 | 362 |
| 366 // Calls test server to fetch database for verification. | 363 // Calls test server to fetch database for verification. |
| 367 net::URLRequestStatus::Status FetchDBToVerify( | 364 net::URLRequestStatus::Status FetchDBToVerify( |
| 368 const net::TestServer& test_server, | 365 const net::TestServer& test_server, |
| 369 int test_step) { | 366 int test_step) { |
| 370 // TODO(lzheng): Remove chunk_type=add once it is not needed by the server. | 367 // TODO(lzheng): Remove chunk_type=add once it is not needed by the server. |
| 371 std::string path = base::StringPrintf( | 368 std::string path = base::StringPrintf( |
| (...skipping 28 matching lines...) Expand all Loading... |
| 400 source->GetResponseAsString(&response_data_); | 397 source->GetResponseAsString(&response_data_); |
| 401 response_status_ = source->GetStatus().status(); | 398 response_status_ = source->GetStatus().status(); |
| 402 StopUILoop(); | 399 StopUILoop(); |
| 403 } | 400 } |
| 404 | 401 |
| 405 const std::string& response_data() { | 402 const std::string& response_data() { |
| 406 return response_data_; | 403 return response_data_; |
| 407 } | 404 } |
| 408 | 405 |
| 409 private: | 406 private: |
| 410 friend class base::RefCountedThreadSafe<SafeBrowsingServiceTestHelper>; | 407 friend class base::RefCountedThreadSafe<SafeBrowsingServerTestHelper>; |
| 411 virtual ~SafeBrowsingServiceTestHelper() {} | 408 virtual ~SafeBrowsingServerTestHelper() {} |
| 412 | 409 |
| 413 // Stops UI loop after desired status is updated. | 410 // Stops UI loop after desired status is updated. |
| 414 void StopUILoop() { | 411 void StopUILoop() { |
| 415 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 412 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 416 MessageLoopForUI::current()->Quit(); | 413 MessageLoopForUI::current()->Quit(); |
| 417 } | 414 } |
| 418 | 415 |
| 419 // Fetch a URL. If message_loop_started is true, starts the message loop | 416 // Fetch a URL. If message_loop_started is true, starts the message loop |
| 420 // so the caller could wait till OnURLFetchComplete is called. | 417 // so the caller could wait till OnURLFetchComplete is called. |
| 421 net::URLRequestStatus::Status FetchUrl(const GURL& url) { | 418 net::URLRequestStatus::Status FetchUrl(const GURL& url) { |
| 422 url_fetcher_.reset(net::URLFetcher::Create( | 419 url_fetcher_.reset(net::URLFetcher::Create( |
| 423 url, net::URLFetcher::GET, this)); | 420 url, net::URLFetcher::GET, this)); |
| 424 url_fetcher_->SetLoadFlags(net::LOAD_DISABLE_CACHE); | 421 url_fetcher_->SetLoadFlags(net::LOAD_DISABLE_CACHE); |
| 425 url_fetcher_->SetRequestContext(request_context_); | 422 url_fetcher_->SetRequestContext(request_context_); |
| 426 url_fetcher_->Start(); | 423 url_fetcher_->Start(); |
| 427 content::RunMessageLoop(); | 424 content::RunMessageLoop(); |
| 428 return response_status_; | 425 return response_status_; |
| 429 } | 426 } |
| 430 | 427 |
| 431 base::OneShotTimer<SafeBrowsingServiceTestHelper> check_update_timer_; | 428 base::OneShotTimer<SafeBrowsingServerTestHelper> check_update_timer_; |
| 432 SafeBrowsingServiceTest* safe_browsing_test_; | 429 SafeBrowsingServerTest* safe_browsing_test_; |
| 433 scoped_ptr<net::URLFetcher> url_fetcher_; | 430 scoped_ptr<net::URLFetcher> url_fetcher_; |
| 434 std::string response_data_; | 431 std::string response_data_; |
| 435 net::URLRequestStatus::Status response_status_; | 432 net::URLRequestStatus::Status response_status_; |
| 436 net::URLRequestContextGetter* request_context_; | 433 net::URLRequestContextGetter* request_context_; |
| 437 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceTestHelper); | 434 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServerTestHelper); |
| 438 }; | 435 }; |
| 439 | 436 |
| 440 // See http://crbug.com/96459 | 437 // See http://crbug.com/96459 |
| 441 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, | 438 IN_PROC_BROWSER_TEST_F(SafeBrowsingServerTest, |
| 442 DISABLED_SafeBrowsingSystemTest) { | 439 DISABLED_SafeBrowsingServerTest) { |
| 443 LOG(INFO) << "Start test"; | 440 LOG(INFO) << "Start test"; |
| 444 ASSERT_TRUE(InitSafeBrowsingService()); | 441 ASSERT_TRUE(InitSafeBrowsingService()); |
| 445 | 442 |
| 446 net::URLRequestContextGetter* request_context = | 443 net::URLRequestContextGetter* request_context = |
| 447 browser()->profile()->GetRequestContext(); | 444 browser()->profile()->GetRequestContext(); |
| 448 scoped_refptr<SafeBrowsingServiceTestHelper> safe_browsing_helper( | 445 scoped_refptr<SafeBrowsingServerTestHelper> safe_browsing_helper( |
| 449 new SafeBrowsingServiceTestHelper(this, request_context)); | 446 new SafeBrowsingServerTestHelper(this, request_context)); |
| 450 int last_step = 0; | 447 int last_step = 0; |
| 451 | 448 |
| 452 // Waits and makes sure safebrowsing update is not happening. | 449 // Waits and makes sure safebrowsing update is not happening. |
| 453 // The wait will stop once OnWaitForStatusUpdateDone in | 450 // The wait will stop once OnWaitForStatusUpdateDone in |
| 454 // safe_browsing_helper is called and status from safe_browsing_service_ | 451 // safe_browsing_helper is called and status from safe_browsing_service_ |
| 455 // is checked. | 452 // is checked. |
| 456 safe_browsing_helper->UpdateStatus(); | 453 safe_browsing_helper->UpdateStatus(); |
| 457 EXPECT_TRUE(is_database_ready()); | 454 EXPECT_TRUE(is_database_ready()); |
| 458 EXPECT_FALSE(is_update_scheduled()); | 455 EXPECT_FALSE(is_update_scheduled()); |
| 459 EXPECT_TRUE(last_update().is_null()); | 456 EXPECT_TRUE(last_update().is_null()); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 516 safe_browsing_helper->FetchDBToVerify(test_server(), step)); | 513 safe_browsing_helper->FetchDBToVerify(test_server(), step)); |
| 517 EXPECT_GT(safe_browsing_helper->response_data().size(), 0U); | 514 EXPECT_GT(safe_browsing_helper->response_data().size(), 0U); |
| 518 last_step = step; | 515 last_step = step; |
| 519 } | 516 } |
| 520 | 517 |
| 521 // Verifies with server if test is done and waits till server responses. | 518 // Verifies with server if test is done and waits till server responses. |
| 522 EXPECT_EQ(net::URLRequestStatus::SUCCESS, | 519 EXPECT_EQ(net::URLRequestStatus::SUCCESS, |
| 523 safe_browsing_helper->VerifyTestComplete(test_server(), last_step)); | 520 safe_browsing_helper->VerifyTestComplete(test_server(), last_step)); |
| 524 EXPECT_EQ("yes", safe_browsing_helper->response_data()); | 521 EXPECT_EQ("yes", safe_browsing_helper->response_data()); |
| 525 } | 522 } |
| OLD | NEW |