| 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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 #include "net/base/load_flags.h" | 45 #include "net/base/load_flags.h" |
| 46 #include "net/base/net_log.h" | 46 #include "net/base/net_log.h" |
| 47 #include "net/test/python_utils.h" | 47 #include "net/test/python_utils.h" |
| 48 #include "net/url_request/url_request_status.h" | 48 #include "net/url_request/url_request_status.h" |
| 49 #include "testing/gtest/include/gtest/gtest.h" | 49 #include "testing/gtest/include/gtest/gtest.h" |
| 50 | 50 |
| 51 using content::BrowserThread; | 51 using content::BrowserThread; |
| 52 | 52 |
| 53 namespace { | 53 namespace { |
| 54 | 54 |
| 55 const FilePath::CharType kDataFile[] = FILE_PATH_LITERAL("testing_input.dat"); | 55 const FilePath::CharType kDataFile[] = |
| 56 FILE_PATH_LITERAL("testing_input_nomac.dat"); |
| 56 const char kUrlVerifyPath[] = "/safebrowsing/verify_urls"; | 57 const char kUrlVerifyPath[] = "/safebrowsing/verify_urls"; |
| 57 const char kDBVerifyPath[] = "/safebrowsing/verify_database"; | 58 const char kDBVerifyPath[] = "/safebrowsing/verify_database"; |
| 58 const char kDBResetPath[] = "/reset"; | 59 const char kDBResetPath[] = "/reset"; |
| 59 const char kTestCompletePath[] = "/test_complete"; | 60 const char kTestCompletePath[] = "/test_complete"; |
| 60 | 61 |
| 61 struct PhishingUrl { | 62 struct PhishingUrl { |
| 62 std::string url; | 63 std::string url; |
| 63 std::string list_name; | 64 std::string list_name; |
| 64 bool is_phishing; | 65 bool is_phishing; |
| 65 }; | 66 }; |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 | 200 |
| 200 const char SafeBrowsingTestServer::kHost_[] = "localhost"; | 201 const char SafeBrowsingTestServer::kHost_[] = "localhost"; |
| 201 const int SafeBrowsingTestServer::kPort_ = 40102; | 202 const int SafeBrowsingTestServer::kPort_ = 40102; |
| 202 | 203 |
| 203 // This starts the browser and keeps status of states related to SafeBrowsing. | 204 // This starts the browser and keeps status of states related to SafeBrowsing. |
| 204 class SafeBrowsingServiceTest : public InProcessBrowserTest { | 205 class SafeBrowsingServiceTest : public InProcessBrowserTest { |
| 205 public: | 206 public: |
| 206 SafeBrowsingServiceTest() | 207 SafeBrowsingServiceTest() |
| 207 : safe_browsing_service_(NULL), | 208 : safe_browsing_service_(NULL), |
| 208 is_database_ready_(true), | 209 is_database_ready_(true), |
| 209 is_initial_request_(false), | |
| 210 is_update_scheduled_(false), | 210 is_update_scheduled_(false), |
| 211 is_checked_url_in_db_(false), | 211 is_checked_url_in_db_(false), |
| 212 is_checked_url_safe_(false) { | 212 is_checked_url_safe_(false) { |
| 213 } | 213 } |
| 214 | 214 |
| 215 virtual ~SafeBrowsingServiceTest() { | 215 virtual ~SafeBrowsingServiceTest() { |
| 216 } | 216 } |
| 217 | 217 |
| 218 void UpdateSafeBrowsingStatus() { | 218 void UpdateSafeBrowsingStatus() { |
| 219 ASSERT_TRUE(safe_browsing_service_); | 219 ASSERT_TRUE(safe_browsing_service_); |
| 220 base::AutoLock lock(update_status_mutex_); | 220 base::AutoLock lock(update_status_mutex_); |
| 221 is_initial_request_ = | |
| 222 safe_browsing_service_->protocol_manager_->is_initial_request(); | |
| 223 last_update_ = safe_browsing_service_->protocol_manager_->last_update(); | 221 last_update_ = safe_browsing_service_->protocol_manager_->last_update(); |
| 224 is_update_scheduled_ = | 222 is_update_scheduled_ = |
| 225 safe_browsing_service_->protocol_manager_->update_timer_.IsRunning(); | 223 safe_browsing_service_->protocol_manager_->update_timer_.IsRunning(); |
| 226 } | 224 } |
| 227 | 225 |
| 228 void ForceUpdate() { | 226 void ForceUpdate() { |
| 229 ASSERT_TRUE(safe_browsing_service_); | 227 ASSERT_TRUE(safe_browsing_service_); |
| 230 safe_browsing_service_->protocol_manager_->ForceScheduleNextUpdate(0); | 228 safe_browsing_service_->protocol_manager_->ForceScheduleNextUpdate(0); |
| 231 } | 229 } |
| 232 | 230 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 263 bool is_checked_url_safe() { | 261 bool is_checked_url_safe() { |
| 264 base::AutoLock l(update_status_mutex_); | 262 base::AutoLock l(update_status_mutex_); |
| 265 return is_checked_url_safe_; | 263 return is_checked_url_safe_; |
| 266 } | 264 } |
| 267 | 265 |
| 268 bool is_database_ready() { | 266 bool is_database_ready() { |
| 269 base::AutoLock l(update_status_mutex_); | 267 base::AutoLock l(update_status_mutex_); |
| 270 return is_database_ready_; | 268 return is_database_ready_; |
| 271 } | 269 } |
| 272 | 270 |
| 273 bool is_initial_request() { | |
| 274 base::AutoLock l(update_status_mutex_); | |
| 275 return is_initial_request_; | |
| 276 } | |
| 277 | |
| 278 base::Time last_update() { | 271 base::Time last_update() { |
| 279 base::AutoLock l(update_status_mutex_); | 272 base::AutoLock l(update_status_mutex_); |
| 280 return last_update_; | 273 return last_update_; |
| 281 } | 274 } |
| 282 | 275 |
| 283 bool is_update_scheduled() { | 276 bool is_update_scheduled() { |
| 284 base::AutoLock l(update_status_mutex_); | 277 base::AutoLock l(update_status_mutex_); |
| 285 return is_update_scheduled_; | 278 return is_update_scheduled_; |
| 286 } | 279 } |
| 287 | 280 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 307 | 300 |
| 308 // TODO(lzheng): The test server does not understand download related | 301 // TODO(lzheng): The test server does not understand download related |
| 309 // requests. We need to fix the server. | 302 // requests. We need to fix the server. |
| 310 command_line->AppendSwitch(switches::kSbDisableDownloadProtection); | 303 command_line->AppendSwitch(switches::kSbDisableDownloadProtection); |
| 311 | 304 |
| 312 // TODO(gcasto): Generate new testing data that includes the | 305 // TODO(gcasto): Generate new testing data that includes the |
| 313 // client-side phishing whitelist. | 306 // client-side phishing whitelist. |
| 314 command_line->AppendSwitch( | 307 command_line->AppendSwitch( |
| 315 switches::kDisableClientSidePhishingDetection); | 308 switches::kDisableClientSidePhishingDetection); |
| 316 | 309 |
| 317 // In this test, we fetch SafeBrowsing data and Mac key from the same | 310 // Point to the testing server for all SafeBrowsing requests. |
| 318 // server. Although in real production, they are served from different | |
| 319 // servers. | |
| 320 std::string url_prefix = | 311 std::string url_prefix = |
| 321 base::StringPrintf("http://%s:%d/safebrowsing", | 312 base::StringPrintf("http://%s:%d/safebrowsing", |
| 322 SafeBrowsingTestServer::Host(), | 313 SafeBrowsingTestServer::Host(), |
| 323 SafeBrowsingTestServer::Port()); | 314 SafeBrowsingTestServer::Port()); |
| 324 command_line->AppendSwitchASCII(switches::kSbInfoURLPrefix, url_prefix); | 315 command_line->AppendSwitchASCII(switches::kSbURLPrefix, url_prefix); |
| 325 command_line->AppendSwitchASCII(switches::kSbMacKeyURLPrefix, url_prefix); | |
| 326 } | 316 } |
| 327 | 317 |
| 328 void SetTestStep(int step) { | 318 void SetTestStep(int step) { |
| 329 std::string test_step = base::StringPrintf("test_step=%d", step); | 319 std::string test_step = base::StringPrintf("test_step=%d", step); |
| 330 safe_browsing_service_->protocol_manager_->set_additional_query(test_step); | 320 safe_browsing_service_->protocol_manager_->set_additional_query(test_step); |
| 331 } | 321 } |
| 332 | 322 |
| 333 private: | 323 private: |
| 334 SafeBrowsingService* safe_browsing_service_; | 324 SafeBrowsingService* safe_browsing_service_; |
| 335 | 325 |
| 336 // Protects all variables below since they are read on UI thread | 326 // Protects all variables below since they are read on UI thread |
| 337 // but updated on IO thread or safebrowsing thread. | 327 // but updated on IO thread or safebrowsing thread. |
| 338 base::Lock update_status_mutex_; | 328 base::Lock update_status_mutex_; |
| 339 | 329 |
| 340 // States associated with safebrowsing service updates. | 330 // States associated with safebrowsing service updates. |
| 341 bool is_database_ready_; | 331 bool is_database_ready_; |
| 342 bool is_initial_request_; | |
| 343 base::Time last_update_; | 332 base::Time last_update_; |
| 344 bool is_update_scheduled_; | 333 bool is_update_scheduled_; |
| 345 // Indicates if there is a match between a URL's prefix and safebrowsing | 334 // Indicates if there is a match between a URL's prefix and safebrowsing |
| 346 // database (thus potentially it is a phishing URL). | 335 // database (thus potentially it is a phishing URL). |
| 347 bool is_checked_url_in_db_; | 336 bool is_checked_url_in_db_; |
| 348 // True if last verified URL is not a phishing URL and thus it is safe. | 337 // True if last verified URL is not a phishing URL and thus it is safe. |
| 349 bool is_checked_url_safe_; | 338 bool is_checked_url_safe_; |
| 350 | 339 |
| 351 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceTest); | 340 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceTest); |
| 352 }; | 341 }; |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 567 | 556 |
| 568 // Make sure the server is running. | 557 // Make sure the server is running. |
| 569 safe_browsing_helper->WaitTillServerReady(server_host, server_port); | 558 safe_browsing_helper->WaitTillServerReady(server_host, server_port); |
| 570 | 559 |
| 571 // Waits and makes sure safebrowsing update is not happening. | 560 // Waits and makes sure safebrowsing update is not happening. |
| 572 // The wait will stop once OnWaitForStatusUpdateDone in | 561 // The wait will stop once OnWaitForStatusUpdateDone in |
| 573 // safe_browsing_helper is called and status from safe_browsing_service_ | 562 // safe_browsing_helper is called and status from safe_browsing_service_ |
| 574 // is checked. | 563 // is checked. |
| 575 safe_browsing_helper->WaitForStatusUpdate(0); | 564 safe_browsing_helper->WaitForStatusUpdate(0); |
| 576 EXPECT_TRUE(is_database_ready()); | 565 EXPECT_TRUE(is_database_ready()); |
| 577 EXPECT_TRUE(is_initial_request()); | |
| 578 EXPECT_FALSE(is_update_scheduled()); | 566 EXPECT_FALSE(is_update_scheduled()); |
| 579 EXPECT_TRUE(last_update().is_null()); | 567 EXPECT_TRUE(last_update().is_null()); |
| 580 // Starts updates. After each update, the test will fetch a list of URLs with | 568 // Starts updates. After each update, the test will fetch a list of URLs with |
| 581 // expected results to verify with safebrowsing service. If there is no error, | 569 // expected results to verify with safebrowsing service. If there is no error, |
| 582 // the test moves on to the next step to get more update chunks. | 570 // the test moves on to the next step to get more update chunks. |
| 583 // This repeats till there is no update data. | 571 // This repeats till there is no update data. |
| 584 for (int step = 1;; step++) { | 572 for (int step = 1;; step++) { |
| 585 // Every step should be a fresh start. | 573 // Every step should be a fresh start. |
| 586 SCOPED_TRACE(base::StringPrintf("step=%d", step)); | 574 SCOPED_TRACE(base::StringPrintf("step=%d", step)); |
| 587 EXPECT_TRUE(is_database_ready()); | 575 EXPECT_TRUE(is_database_ready()); |
| 588 EXPECT_FALSE(is_update_scheduled()); | 576 EXPECT_FALSE(is_update_scheduled()); |
| 589 | 577 |
| 590 // Starts safebrowsing update on IO thread. Waits till scheduled | 578 // Starts safebrowsing update on IO thread. Waits till scheduled |
| 591 // update finishes. Stops waiting after kMaxWaitSecPerStep if the update | 579 // update finishes. Stops waiting after kMaxWaitSecPerStep if the update |
| 592 // could not finish. | 580 // could not finish. |
| 593 base::Time now = base::Time::Now(); | 581 base::Time now = base::Time::Now(); |
| 594 SetTestStep(step); | 582 SetTestStep(step); |
| 595 safe_browsing_helper->ForceUpdate(); | 583 safe_browsing_helper->ForceUpdate(); |
| 596 | 584 |
| 597 do { | 585 do { |
| 598 // Periodically pull the status. | 586 // Periodically pull the status. |
| 599 safe_browsing_helper->WaitForStatusUpdate( | 587 safe_browsing_helper->WaitForStatusUpdate( |
| 600 TestTimeouts::tiny_timeout_ms()); | 588 TestTimeouts::tiny_timeout_ms()); |
| 601 } while (is_update_scheduled() || is_initial_request() || | 589 } while (is_update_scheduled() || !is_database_ready()); |
| 602 !is_database_ready()); | |
| 603 | 590 |
| 604 | 591 |
| 605 if (last_update() < now) { | 592 if (last_update() < now) { |
| 606 // This means no data available anymore. | 593 // This means no data available anymore. |
| 607 break; | 594 break; |
| 608 } | 595 } |
| 609 | 596 |
| 610 // Fetches URLs to verify and waits till server responses with data. | 597 // Fetches URLs to verify and waits till server responses with data. |
| 611 EXPECT_EQ(net::URLRequestStatus::SUCCESS, | 598 EXPECT_EQ(net::URLRequestStatus::SUCCESS, |
| 612 safe_browsing_helper->FetchUrlsToVerify(server_host, | 599 safe_browsing_helper->FetchUrlsToVerify(server_host, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 648 } | 635 } |
| 649 | 636 |
| 650 // Verifies with server if test is done and waits till server responses. | 637 // Verifies with server if test is done and waits till server responses. |
| 651 EXPECT_EQ(net::URLRequestStatus::SUCCESS, | 638 EXPECT_EQ(net::URLRequestStatus::SUCCESS, |
| 652 safe_browsing_helper->VerifyTestComplete(server_host, | 639 safe_browsing_helper->VerifyTestComplete(server_host, |
| 653 server_port, | 640 server_port, |
| 654 last_step)); | 641 last_step)); |
| 655 EXPECT_EQ("yes", safe_browsing_helper->response_data()); | 642 EXPECT_EQ("yes", safe_browsing_helper->response_data()); |
| 656 test_server.Stop(); | 643 test_server.Stop(); |
| 657 } | 644 } |
| OLD | NEW |