| 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 27 matching lines...) Expand all Loading... |
| 38 #include "chrome/common/url_constants.h" | 38 #include "chrome/common/url_constants.h" |
| 39 #include "chrome/test/base/in_process_browser_test.h" | 39 #include "chrome/test/base/in_process_browser_test.h" |
| 40 #include "chrome/test/base/ui_test_utils.h" | 40 #include "chrome/test/base/ui_test_utils.h" |
| 41 #include "content/public/common/url_fetcher.h" | 41 #include "content/public/common/url_fetcher.h" |
| 42 #include "content/public/common/url_fetcher_delegate.h" | 42 #include "content/public/common/url_fetcher_delegate.h" |
| 43 #include "content/test/test_browser_thread.h" | 43 #include "content/test/test_browser_thread.h" |
| 44 #include "net/base/host_resolver.h" | 44 #include "net/base/host_resolver.h" |
| 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/test/test_server.h" |
| 48 #include "net/url_request/url_request_status.h" | 49 #include "net/url_request/url_request_status.h" |
| 49 #include "testing/gtest/include/gtest/gtest.h" | 50 #include "testing/gtest/include/gtest/gtest.h" |
| 50 | 51 |
| 51 using content::BrowserThread; | 52 using content::BrowserThread; |
| 52 | 53 |
| 53 namespace { | 54 namespace { |
| 54 | 55 |
| 55 const FilePath::CharType kDataFile[] = | 56 const FilePath::CharType kDataFile[] = |
| 56 FILE_PATH_LITERAL("testing_input_nomac.dat"); | 57 FILE_PATH_LITERAL("testing_input_nomac.dat"); |
| 57 const char kUrlVerifyPath[] = "/safebrowsing/verify_urls"; | 58 const char kUrlVerifyPath[] = "/safebrowsing/verify_urls"; |
| 58 const char kDBVerifyPath[] = "/safebrowsing/verify_database"; | 59 const char kDBVerifyPath[] = "/safebrowsing/verify_database"; |
| 59 const char kDBResetPath[] = "/reset"; | |
| 60 const char kTestCompletePath[] = "/test_complete"; | 60 const char kTestCompletePath[] = "/test_complete"; |
| 61 | 61 |
| 62 struct PhishingUrl { | 62 struct PhishingUrl { |
| 63 std::string url; | 63 std::string url; |
| 64 std::string list_name; | 64 std::string list_name; |
| 65 bool is_phishing; | 65 bool is_phishing; |
| 66 }; | 66 }; |
| 67 | 67 |
| 68 // Parses server response for verify_urls. The expected format is: | 68 // Parses server response for verify_urls. The expected format is: |
| 69 // | 69 // |
| (...skipping 30 matching lines...) Expand all Loading... |
| 100 << ": " << record_parts[2]; | 100 << ": " << record_parts[2]; |
| 101 return false; | 101 return false; |
| 102 } | 102 } |
| 103 phishing_urls->push_back(phishing_url); | 103 phishing_urls->push_back(phishing_url); |
| 104 } | 104 } |
| 105 return true; | 105 return true; |
| 106 } | 106 } |
| 107 | 107 |
| 108 } // namespace | 108 } // namespace |
| 109 | 109 |
| 110 class SafeBrowsingTestServer { | |
| 111 public: | |
| 112 explicit SafeBrowsingTestServer(const FilePath& datafile) | |
| 113 : datafile_(datafile), | |
| 114 server_handle_(base::kNullProcessHandle) { | |
| 115 } | |
| 116 | |
| 117 ~SafeBrowsingTestServer() { | |
| 118 EXPECT_EQ(base::kNullProcessHandle, server_handle_); | |
| 119 } | |
| 120 | |
| 121 // Start the python server test suite. | |
| 122 bool Start() { | |
| 123 // Get path to python server script | |
| 124 FilePath testserver_path; | |
| 125 if (!PathService::Get(base::DIR_SOURCE_ROOT, &testserver_path)) { | |
| 126 LOG(ERROR) << "Failed to get DIR_SOURCE_ROOT"; | |
| 127 return false; | |
| 128 } | |
| 129 testserver_path = testserver_path | |
| 130 .Append(FILE_PATH_LITERAL("third_party")) | |
| 131 .Append(FILE_PATH_LITERAL("safe_browsing")) | |
| 132 .Append(FILE_PATH_LITERAL("testing")); | |
| 133 AppendToPythonPath(testserver_path); | |
| 134 FilePath testserver = testserver_path.Append( | |
| 135 FILE_PATH_LITERAL("safebrowsing_test_server.py")); | |
| 136 | |
| 137 FilePath pyproto_code_dir; | |
| 138 if (!GetPyProtoPath(&pyproto_code_dir)) { | |
| 139 LOG(ERROR) << "Failed to get generated python protobuf dir"; | |
| 140 return false; | |
| 141 } | |
| 142 AppendToPythonPath(pyproto_code_dir); | |
| 143 pyproto_code_dir = pyproto_code_dir.Append(FILE_PATH_LITERAL("google")); | |
| 144 AppendToPythonPath(pyproto_code_dir); | |
| 145 | |
| 146 FilePath python_runtime; | |
| 147 EXPECT_TRUE(GetPythonRunTime(&python_runtime)); | |
| 148 CommandLine cmd_line(python_runtime); | |
| 149 // Make python stdout and stderr unbuffered, to prevent incomplete stderr on | |
| 150 // win bots, and also fix mixed up ordering of stdout and stderr. | |
| 151 cmd_line.AppendSwitch("-u"); | |
| 152 FilePath datafile = testserver_path.Append(datafile_); | |
| 153 cmd_line.AppendArgPath(testserver); | |
| 154 cmd_line.AppendArg(base::StringPrintf("--port=%d", kPort_)); | |
| 155 cmd_line.AppendArgNative(FILE_PATH_LITERAL("--datafile=") + | |
| 156 datafile.value()); | |
| 157 | |
| 158 base::LaunchOptions options; | |
| 159 #if defined(OS_WIN) | |
| 160 options.start_hidden = true; | |
| 161 #endif | |
| 162 if (!base::LaunchProcess(cmd_line, options, &server_handle_)) { | |
| 163 LOG(ERROR) << "Failed to launch server: " | |
| 164 << cmd_line.GetCommandLineString(); | |
| 165 return false; | |
| 166 } | |
| 167 return true; | |
| 168 } | |
| 169 | |
| 170 // Stop the python server test suite. | |
| 171 bool Stop() { | |
| 172 if (server_handle_ == base::kNullProcessHandle) | |
| 173 return true; | |
| 174 | |
| 175 // First check if the process has already terminated. | |
| 176 if (!base::WaitForSingleProcess(server_handle_, 0) && | |
| 177 !base::KillProcess(server_handle_, 1, true)) { | |
| 178 VLOG(1) << "Kill failed?"; | |
| 179 return false; | |
| 180 } | |
| 181 | |
| 182 base::CloseProcessHandle(server_handle_); | |
| 183 server_handle_ = base::kNullProcessHandle; | |
| 184 VLOG(1) << "Stopped."; | |
| 185 return true; | |
| 186 } | |
| 187 | |
| 188 static const char* Host() { | |
| 189 return kHost_; | |
| 190 } | |
| 191 | |
| 192 static int Port() { | |
| 193 return kPort_; | |
| 194 } | |
| 195 | |
| 196 private: | |
| 197 static const char kHost_[]; | |
| 198 static const int kPort_; | |
| 199 FilePath datafile_; | |
| 200 base::ProcessHandle server_handle_; | |
| 201 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingTestServer); | |
| 202 }; | |
| 203 | |
| 204 const char SafeBrowsingTestServer::kHost_[] = "localhost"; | |
| 205 const int SafeBrowsingTestServer::kPort_ = 40102; | |
| 206 | |
| 207 // This starts the browser and keeps status of states related to SafeBrowsing. | 110 // This starts the browser and keeps status of states related to SafeBrowsing. |
| 208 class SafeBrowsingServiceTest : public InProcessBrowserTest { | 111 class SafeBrowsingServiceTest : public InProcessBrowserTest { |
| 209 public: | 112 public: |
| 210 SafeBrowsingServiceTest() | 113 SafeBrowsingServiceTest() |
| 211 : safe_browsing_service_(NULL), | 114 : safe_browsing_service_(NULL), |
| 212 is_database_ready_(true), | 115 is_database_ready_(true), |
| 213 is_update_scheduled_(false), | 116 is_update_scheduled_(false), |
| 214 is_checked_url_in_db_(false), | 117 is_checked_url_in_db_(false), |
| 215 is_checked_url_safe_(false) { | 118 is_checked_url_safe_(false) { |
| 216 } | 119 } |
| 217 | 120 |
| 218 virtual ~SafeBrowsingServiceTest() { | 121 virtual ~SafeBrowsingServiceTest() { |
| 219 } | 122 } |
| 220 | 123 |
| 124 virtual void SetUp() OVERRIDE { |
| 125 FilePath datafile_path = FilePath(FILE_PATH_LITERAL("third_party")) |
| 126 .Append(FILE_PATH_LITERAL("safe_browsing")) |
| 127 .Append(FILE_PATH_LITERAL("testing")) |
| 128 .Append(kDataFile); |
| 129 test_server_.reset(new net::TestServer(net::TestServer::TYPE_SAFEBROWSING, |
| 130 net::TestServer::kLocalhost, |
| 131 datafile_path)); |
| 132 ASSERT_TRUE(test_server_->Start()); |
| 133 |
| 134 LOG(INFO) << "server is " << server_host() << ":" << server_port(); |
| 135 InProcessBrowserTest::SetUp(); |
| 136 } |
| 137 |
| 138 virtual void TearDown() OVERRIDE { |
| 139 InProcessBrowserTest::TearDown(); |
| 140 test_server_->Stop(); |
| 141 } |
| 142 |
| 221 void UpdateSafeBrowsingStatus() { | 143 void UpdateSafeBrowsingStatus() { |
| 222 ASSERT_TRUE(safe_browsing_service_); | 144 ASSERT_TRUE(safe_browsing_service_); |
| 223 base::AutoLock lock(update_status_mutex_); | 145 base::AutoLock lock(update_status_mutex_); |
| 224 last_update_ = safe_browsing_service_->protocol_manager_->last_update(); | 146 last_update_ = safe_browsing_service_->protocol_manager_->last_update(); |
| 225 is_update_scheduled_ = | 147 is_update_scheduled_ = |
| 226 safe_browsing_service_->protocol_manager_->update_timer_.IsRunning(); | 148 safe_browsing_service_->protocol_manager_->update_timer_.IsRunning(); |
| 227 } | 149 } |
| 228 | 150 |
| 229 void ForceUpdate() { | 151 void ForceUpdate() { |
| 230 ASSERT_TRUE(safe_browsing_service_); | 152 ASSERT_TRUE(safe_browsing_service_); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 | 200 |
| 279 bool is_update_scheduled() { | 201 bool is_update_scheduled() { |
| 280 base::AutoLock l(update_status_mutex_); | 202 base::AutoLock l(update_status_mutex_); |
| 281 return is_update_scheduled_; | 203 return is_update_scheduled_; |
| 282 } | 204 } |
| 283 | 205 |
| 284 MessageLoop* SafeBrowsingMessageLoop() { | 206 MessageLoop* SafeBrowsingMessageLoop() { |
| 285 return safe_browsing_service_->safe_browsing_thread_->message_loop(); | 207 return safe_browsing_service_->safe_browsing_thread_->message_loop(); |
| 286 } | 208 } |
| 287 | 209 |
| 210 const std::string& server_host() const { |
| 211 return test_server_->host_port_pair().host(); |
| 212 } |
| 213 |
| 214 int server_port() const { |
| 215 return test_server_->host_port_pair().port(); |
| 216 } |
| 217 |
| 288 protected: | 218 protected: |
| 289 bool InitSafeBrowsingService() { | 219 bool InitSafeBrowsingService() { |
| 290 safe_browsing_service_ = g_browser_process->safe_browsing_service(); | 220 safe_browsing_service_ = g_browser_process->safe_browsing_service(); |
| 291 return safe_browsing_service_ != NULL; | 221 return safe_browsing_service_ != NULL; |
| 292 } | 222 } |
| 293 | 223 |
| 294 virtual void SetUpCommandLine(CommandLine* command_line) { | 224 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 295 // Makes sure the auto update is not triggered. This test will force the | 225 // Makes sure the auto update is not triggered. This test will force the |
| 296 // update when needed. | 226 // update when needed. |
| 297 command_line->AppendSwitch(switches::kSbDisableAutoUpdate); | 227 command_line->AppendSwitch(switches::kSbDisableAutoUpdate); |
| 298 | 228 |
| 299 // This test uses loopback. No need to use IPv6 especially it makes | 229 // This test uses loopback. No need to use IPv6 especially it makes |
| 300 // local requests slow on Windows trybot when ipv6 local address [::1] | 230 // local requests slow on Windows trybot when ipv6 local address [::1] |
| 301 // is not setup. | 231 // is not setup. |
| 302 command_line->AppendSwitch(switches::kDisableIPv6); | 232 command_line->AppendSwitch(switches::kDisableIPv6); |
| 303 | 233 |
| 304 // TODO(lzheng): The test server does not understand download related | 234 // TODO(lzheng): The test server does not understand download related |
| 305 // requests. We need to fix the server. | 235 // requests. We need to fix the server. |
| 306 command_line->AppendSwitch(switches::kSbDisableDownloadProtection); | 236 command_line->AppendSwitch(switches::kSbDisableDownloadProtection); |
| 307 | 237 |
| 308 // TODO(gcasto): Generate new testing data that includes the | 238 // TODO(gcasto): Generate new testing data that includes the |
| 309 // client-side phishing whitelist. | 239 // client-side phishing whitelist. |
| 310 command_line->AppendSwitch( | 240 command_line->AppendSwitch( |
| 311 switches::kDisableClientSidePhishingDetection); | 241 switches::kDisableClientSidePhishingDetection); |
| 312 | 242 |
| 313 // Point to the testing server for all SafeBrowsing requests. | 243 // Point to the testing server for all SafeBrowsing requests. |
| 314 std::string url_prefix = | 244 std::string url_prefix = |
| 315 base::StringPrintf("http://%s:%d/safebrowsing", | 245 base::StringPrintf("http://%s:%d/safebrowsing", |
| 316 SafeBrowsingTestServer::Host(), | 246 server_host().c_str(), |
| 317 SafeBrowsingTestServer::Port()); | 247 server_port()); |
| 318 command_line->AppendSwitchASCII(switches::kSbURLPrefix, url_prefix); | 248 command_line->AppendSwitchASCII(switches::kSbURLPrefix, url_prefix); |
| 319 } | 249 } |
| 320 | 250 |
| 321 void SetTestStep(int step) { | 251 void SetTestStep(int step) { |
| 322 std::string test_step = base::StringPrintf("test_step=%d", step); | 252 std::string test_step = base::StringPrintf("test_step=%d", step); |
| 323 safe_browsing_service_->protocol_manager_->set_additional_query(test_step); | 253 safe_browsing_service_->protocol_manager_->set_additional_query(test_step); |
| 324 } | 254 } |
| 325 | 255 |
| 326 private: | 256 private: |
| 327 SafeBrowsingService* safe_browsing_service_; | 257 SafeBrowsingService* safe_browsing_service_; |
| 328 | 258 |
| 259 scoped_ptr<net::TestServer> test_server_; |
| 260 |
| 329 // Protects all variables below since they are read on UI thread | 261 // Protects all variables below since they are read on UI thread |
| 330 // but updated on IO thread or safebrowsing thread. | 262 // but updated on IO thread or safebrowsing thread. |
| 331 base::Lock update_status_mutex_; | 263 base::Lock update_status_mutex_; |
| 332 | 264 |
| 333 // States associated with safebrowsing service updates. | 265 // States associated with safebrowsing service updates. |
| 334 bool is_database_ready_; | 266 bool is_database_ready_; |
| 335 base::Time last_update_; | 267 base::Time last_update_; |
| 336 bool is_update_scheduled_; | 268 bool is_update_scheduled_; |
| 337 // 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 |
| 338 // database (thus potentially it is a phishing URL). | 270 // database (thus potentially it is a phishing URL). |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 448 BrowserThread::PostDelayedTask( | 380 BrowserThread::PostDelayedTask( |
| 449 BrowserThread::IO, | 381 BrowserThread::IO, |
| 450 FROM_HERE, | 382 FROM_HERE, |
| 451 base::Bind(&SafeBrowsingServiceTestHelper::CheckStatusOnIOThread, | 383 base::Bind(&SafeBrowsingServiceTestHelper::CheckStatusOnIOThread, |
| 452 this), | 384 this), |
| 453 base::TimeDelta::FromMilliseconds(wait_time_msec)); | 385 base::TimeDelta::FromMilliseconds(wait_time_msec)); |
| 454 // Will continue after OnWaitForStatusUpdateDone(). | 386 // Will continue after OnWaitForStatusUpdateDone(). |
| 455 ui_test_utils::RunMessageLoop(); | 387 ui_test_utils::RunMessageLoop(); |
| 456 } | 388 } |
| 457 | 389 |
| 458 void WaitTillServerReady(const char* host, int port) { | |
| 459 response_status_ = net::URLRequestStatus::FAILED; | |
| 460 GURL url(base::StringPrintf("http://%s:%d%s?test_step=0", | |
| 461 host, port, kDBResetPath)); | |
| 462 // TODO(lzheng): We should have a way to reliably tell when a server is | |
| 463 // ready so we could get rid of the Sleep and retry loop. | |
| 464 while (true) { | |
| 465 if (FetchUrl(url) == net::URLRequestStatus::SUCCESS) | |
| 466 break; | |
| 467 // Wait and try again if last fetch was failed. The loop will hit the | |
| 468 // timeout in OutOfProcTestRunner if the fetch can not get success | |
| 469 // response. | |
| 470 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout()); | |
| 471 } | |
| 472 } | |
| 473 | |
| 474 // Calls test server to fetch database for verification. | 390 // Calls test server to fetch database for verification. |
| 475 net::URLRequestStatus::Status FetchDBToVerify(const char* host, int port, | 391 net::URLRequestStatus::Status FetchDBToVerify( |
| 476 int test_step) { | 392 const std::string& host, int port, int test_step) { |
| 477 // TODO(lzheng): Remove chunk_type=add once it is not needed by the server. | 393 // TODO(lzheng): Remove chunk_type=add once it is not needed by the server. |
| 478 GURL url(base::StringPrintf( | 394 GURL url(base::StringPrintf( |
| 479 "http://%s:%d%s?" | 395 "http://%s:%d%s?" |
| 480 "client=chromium&appver=1.0&pver=2.2&test_step=%d&" | 396 "client=chromium&appver=1.0&pver=2.2&test_step=%d&" |
| 481 "chunk_type=add", | 397 "chunk_type=add", |
| 482 host, port, kDBVerifyPath, test_step)); | 398 host.c_str(), port, kDBVerifyPath, test_step)); |
| 483 return FetchUrl(url); | 399 return FetchUrl(url); |
| 484 } | 400 } |
| 485 | 401 |
| 486 // Calls test server to fetch URLs for verification. | 402 // Calls test server to fetch URLs for verification. |
| 487 net::URLRequestStatus::Status FetchUrlsToVerify(const char* host, int port, | 403 net::URLRequestStatus::Status FetchUrlsToVerify( |
| 488 int test_step) { | 404 const std::string& host, int port, int test_step) { |
| 489 GURL url(base::StringPrintf( | 405 GURL url(base::StringPrintf( |
| 490 "http://%s:%d%s?" | 406 "http://%s:%d%s?" |
| 491 "client=chromium&appver=1.0&pver=2.2&test_step=%d", | 407 "client=chromium&appver=1.0&pver=2.2&test_step=%d", |
| 492 host, port, kUrlVerifyPath, test_step)); | 408 host.c_str(), port, kUrlVerifyPath, test_step)); |
| 493 return FetchUrl(url); | 409 return FetchUrl(url); |
| 494 } | 410 } |
| 495 | 411 |
| 496 // Calls test server to check if test data is done. E.g.: if there is a | 412 // Calls test server to check if test data is done. E.g.: if there is a |
| 497 // bad URL that server expects test to fetch full hash but the test didn't, | 413 // bad URL that server expects test to fetch full hash but the test didn't, |
| 498 // this verification will fail. | 414 // this verification will fail. |
| 499 net::URLRequestStatus::Status VerifyTestComplete(const char* host, int port, | 415 net::URLRequestStatus::Status VerifyTestComplete( |
| 500 int test_step) { | 416 const std::string& host, int port, int test_step) { |
| 501 GURL url(StringPrintf("http://%s:%d%s?test_step=%d", | 417 GURL url(StringPrintf("http://%s:%d%s?test_step=%d", |
| 502 host, port, kTestCompletePath, test_step)); | 418 host.c_str(), port, kTestCompletePath, test_step)); |
| 503 return FetchUrl(url); | 419 return FetchUrl(url); |
| 504 } | 420 } |
| 505 | 421 |
| 506 // Callback for URLFetcher. | 422 // Callback for URLFetcher. |
| 507 virtual void OnURLFetchComplete(const content::URLFetcher* source) { | 423 virtual void OnURLFetchComplete(const content::URLFetcher* source) { |
| 508 source->GetResponseAsString(&response_data_); | 424 source->GetResponseAsString(&response_data_); |
| 509 response_status_ = source->GetStatus().status(); | 425 response_status_ = source->GetStatus().status(); |
| 510 StopUILoop(); | 426 StopUILoop(); |
| 511 } | 427 } |
| 512 | 428 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 539 scoped_ptr<content::URLFetcher> url_fetcher_; | 455 scoped_ptr<content::URLFetcher> url_fetcher_; |
| 540 std::string response_data_; | 456 std::string response_data_; |
| 541 net::URLRequestStatus::Status response_status_; | 457 net::URLRequestStatus::Status response_status_; |
| 542 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceTestHelper); | 458 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceTestHelper); |
| 543 }; | 459 }; |
| 544 | 460 |
| 545 // See http://crbug.com/96459 | 461 // See http://crbug.com/96459 |
| 546 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, | 462 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, |
| 547 DISABLED_SafeBrowsingSystemTest) { | 463 DISABLED_SafeBrowsingSystemTest) { |
| 548 LOG(INFO) << "Start test"; | 464 LOG(INFO) << "Start test"; |
| 549 const char* server_host = SafeBrowsingTestServer::Host(); | |
| 550 int server_port = SafeBrowsingTestServer::Port(); | |
| 551 ASSERT_TRUE(InitSafeBrowsingService()); | 465 ASSERT_TRUE(InitSafeBrowsingService()); |
| 552 | 466 |
| 553 scoped_refptr<SafeBrowsingServiceTestHelper> safe_browsing_helper( | 467 scoped_refptr<SafeBrowsingServiceTestHelper> safe_browsing_helper( |
| 554 new SafeBrowsingServiceTestHelper(this)); | 468 new SafeBrowsingServiceTestHelper(this)); |
| 555 int last_step = 0; | 469 int last_step = 0; |
| 556 FilePath datafile_path = FilePath(kDataFile); | |
| 557 SafeBrowsingTestServer test_server(datafile_path); | |
| 558 ASSERT_TRUE(test_server.Start()); | |
| 559 | |
| 560 // Make sure the server is running. | |
| 561 safe_browsing_helper->WaitTillServerReady(server_host, server_port); | |
| 562 | 470 |
| 563 // Waits and makes sure safebrowsing update is not happening. | 471 // Waits and makes sure safebrowsing update is not happening. |
| 564 // The wait will stop once OnWaitForStatusUpdateDone in | 472 // The wait will stop once OnWaitForStatusUpdateDone in |
| 565 // safe_browsing_helper is called and status from safe_browsing_service_ | 473 // safe_browsing_helper is called and status from safe_browsing_service_ |
| 566 // is checked. | 474 // is checked. |
| 567 safe_browsing_helper->WaitForStatusUpdate(0); | 475 safe_browsing_helper->WaitForStatusUpdate(0); |
| 568 EXPECT_TRUE(is_database_ready()); | 476 EXPECT_TRUE(is_database_ready()); |
| 569 EXPECT_FALSE(is_update_scheduled()); | 477 EXPECT_FALSE(is_update_scheduled()); |
| 570 EXPECT_TRUE(last_update().is_null()); | 478 EXPECT_TRUE(last_update().is_null()); |
| 571 // Starts updates. After each update, the test will fetch a list of URLs with | 479 // Starts updates. After each update, the test will fetch a list of URLs with |
| (...skipping 20 matching lines...) Expand all Loading... |
| 592 } while (is_update_scheduled() || !is_database_ready()); | 500 } while (is_update_scheduled() || !is_database_ready()); |
| 593 | 501 |
| 594 | 502 |
| 595 if (last_update() < now) { | 503 if (last_update() < now) { |
| 596 // This means no data available anymore. | 504 // This means no data available anymore. |
| 597 break; | 505 break; |
| 598 } | 506 } |
| 599 | 507 |
| 600 // Fetches URLs to verify and waits till server responses with data. | 508 // Fetches URLs to verify and waits till server responses with data. |
| 601 EXPECT_EQ(net::URLRequestStatus::SUCCESS, | 509 EXPECT_EQ(net::URLRequestStatus::SUCCESS, |
| 602 safe_browsing_helper->FetchUrlsToVerify(server_host, | 510 safe_browsing_helper->FetchUrlsToVerify(server_host(), |
| 603 server_port, | 511 server_port(), |
| 604 step)); | 512 step)); |
| 605 | 513 |
| 606 std::vector<PhishingUrl> phishing_urls; | 514 std::vector<PhishingUrl> phishing_urls; |
| 607 EXPECT_TRUE(ParsePhishingUrls(safe_browsing_helper->response_data(), | 515 EXPECT_TRUE(ParsePhishingUrls(safe_browsing_helper->response_data(), |
| 608 &phishing_urls)); | 516 &phishing_urls)); |
| 609 EXPECT_GT(phishing_urls.size(), 0U); | 517 EXPECT_GT(phishing_urls.size(), 0U); |
| 610 for (size_t j = 0; j < phishing_urls.size(); ++j) { | 518 for (size_t j = 0; j < phishing_urls.size(); ++j) { |
| 611 // Verifes with server if a URL is a phishing URL and waits till server | 519 // Verifes with server if a URL is a phishing URL and waits till server |
| 612 // responses. | 520 // responses. |
| 613 safe_browsing_helper->CheckUrl(GURL(phishing_urls[j].url)); | 521 safe_browsing_helper->CheckUrl(GURL(phishing_urls[j].url)); |
| 614 if (phishing_urls[j].is_phishing) { | 522 if (phishing_urls[j].is_phishing) { |
| 615 EXPECT_TRUE(is_checked_url_in_db()) | 523 EXPECT_TRUE(is_checked_url_in_db()) |
| 616 << phishing_urls[j].url | 524 << phishing_urls[j].url |
| 617 << " is_phishing: " << phishing_urls[j].is_phishing | 525 << " is_phishing: " << phishing_urls[j].is_phishing |
| 618 << " test step: " << step; | 526 << " test step: " << step; |
| 619 EXPECT_FALSE(is_checked_url_safe()) | 527 EXPECT_FALSE(is_checked_url_safe()) |
| 620 << phishing_urls[j].url | 528 << phishing_urls[j].url |
| 621 << " is_phishing: " << phishing_urls[j].is_phishing | 529 << " is_phishing: " << phishing_urls[j].is_phishing |
| 622 << " test step: " << step; | 530 << " test step: " << step; |
| 623 } else { | 531 } else { |
| 624 EXPECT_TRUE(is_checked_url_safe()) | 532 EXPECT_TRUE(is_checked_url_safe()) |
| 625 << phishing_urls[j].url | 533 << phishing_urls[j].url |
| 626 << " is_phishing: " << phishing_urls[j].is_phishing | 534 << " is_phishing: " << phishing_urls[j].is_phishing |
| 627 << " test step: " << step; | 535 << " test step: " << step; |
| 628 } | 536 } |
| 629 } | 537 } |
| 630 // TODO(lzheng): We should verify the fetched database with local | 538 // TODO(lzheng): We should verify the fetched database with local |
| 631 // database to make sure they match. | 539 // database to make sure they match. |
| 632 EXPECT_EQ(net::URLRequestStatus::SUCCESS, | 540 EXPECT_EQ(net::URLRequestStatus::SUCCESS, |
| 633 safe_browsing_helper->FetchDBToVerify(server_host, | 541 safe_browsing_helper->FetchDBToVerify(server_host(), |
| 634 server_port, | 542 server_port(), |
| 635 step)); | 543 step)); |
| 636 EXPECT_GT(safe_browsing_helper->response_data().size(), 0U); | 544 EXPECT_GT(safe_browsing_helper->response_data().size(), 0U); |
| 637 last_step = step; | 545 last_step = step; |
| 638 } | 546 } |
| 639 | 547 |
| 640 // Verifies with server if test is done and waits till server responses. | 548 // Verifies with server if test is done and waits till server responses. |
| 641 EXPECT_EQ(net::URLRequestStatus::SUCCESS, | 549 EXPECT_EQ(net::URLRequestStatus::SUCCESS, |
| 642 safe_browsing_helper->VerifyTestComplete(server_host, | 550 safe_browsing_helper->VerifyTestComplete(server_host(), |
| 643 server_port, | 551 server_port(), |
| 644 last_step)); | 552 last_step)); |
| 645 EXPECT_EQ("yes", safe_browsing_helper->response_data()); | 553 EXPECT_EQ("yes", safe_browsing_helper->response_data()); |
| 646 test_server.Stop(); | |
| 647 } | 554 } |
| OLD | NEW |