| 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 20 matching lines...) Expand all Loading... |
| 31 #include "base/test/test_timeouts.h" | 31 #include "base/test/test_timeouts.h" |
| 32 #include "base/threading/platform_thread.h" | 32 #include "base/threading/platform_thread.h" |
| 33 #include "base/threading/thread.h" | 33 #include "base/threading/thread.h" |
| 34 #include "base/time/time.h" | 34 #include "base/time/time.h" |
| 35 #include "chrome/browser/browser_process.h" | 35 #include "chrome/browser/browser_process.h" |
| 36 #include "chrome/browser/chrome_notification_types.h" | 36 #include "chrome/browser/chrome_notification_types.h" |
| 37 #include "chrome/browser/profiles/profile.h" | 37 #include "chrome/browser/profiles/profile.h" |
| 38 #include "chrome/browser/safe_browsing/local_database_manager.h" | 38 #include "chrome/browser/safe_browsing/local_database_manager.h" |
| 39 #include "chrome/browser/safe_browsing/local_safebrowsing_test_server.h" | 39 #include "chrome/browser/safe_browsing/local_safebrowsing_test_server.h" |
| 40 #include "chrome/browser/safe_browsing/protocol_manager.h" | 40 #include "chrome/browser/safe_browsing/protocol_manager.h" |
| 41 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 41 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h" |
| 42 #include "chrome/browser/ui/browser.h" | 42 #include "chrome/browser/ui/browser.h" |
| 43 #include "chrome/common/chrome_switches.h" | 43 #include "chrome/common/chrome_switches.h" |
| 44 #include "chrome/common/url_constants.h" | 44 #include "chrome/common/url_constants.h" |
| 45 #include "chrome/test/base/in_process_browser_test.h" | 45 #include "chrome/test/base/in_process_browser_test.h" |
| 46 #include "components/safe_browsing_db/database_manager.h" | 46 #include "components/safe_browsing_db/database_manager.h" |
| 47 #include "content/public/browser/browser_context.h" | 47 #include "content/public/browser/browser_context.h" |
| 48 #include "content/public/test/test_browser_thread.h" | 48 #include "content/public/test/test_browser_thread.h" |
| 49 #include "content/public/test/test_utils.h" | 49 #include "content/public/test/test_utils.h" |
| 50 #include "net/base/load_flags.h" | 50 #include "net/base/load_flags.h" |
| 51 #include "net/dns/host_resolver.h" | 51 #include "net/dns/host_resolver.h" |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 } else { | 108 } else { |
| 109 LOG(ERROR) << "Unrecognized expectation in " << url_str.as_string() | 109 LOG(ERROR) << "Unrecognized expectation in " << url_str.as_string() |
| 110 << ": " << record_parts[2]; | 110 << ": " << record_parts[2]; |
| 111 return false; | 111 return false; |
| 112 } | 112 } |
| 113 phishing_urls->push_back(phishing_url); | 113 phishing_urls->push_back(phishing_url); |
| 114 } | 114 } |
| 115 return true; | 115 return true; |
| 116 } | 116 } |
| 117 | 117 |
| 118 class FakeSafeBrowsingService : public SafeBrowsingService { | |
| 119 public: | |
| 120 explicit FakeSafeBrowsingService(const std::string& url_prefix) | |
| 121 : url_prefix_(url_prefix) {} | |
| 122 | |
| 123 SafeBrowsingProtocolConfig GetProtocolConfig() const override { | |
| 124 SafeBrowsingProtocolConfig config; | |
| 125 config.url_prefix = url_prefix_; | |
| 126 // Makes sure the auto update is not triggered. The tests will force the | |
| 127 // update when needed. | |
| 128 config.disable_auto_update = true; | |
| 129 config.client_name = "browser_tests"; | |
| 130 return config; | |
| 131 } | |
| 132 | |
| 133 private: | |
| 134 ~FakeSafeBrowsingService() override {} | |
| 135 | |
| 136 std::string url_prefix_; | |
| 137 | |
| 138 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingService); | |
| 139 }; | |
| 140 | |
| 141 // Factory that creates FakeSafeBrowsingService instances. | |
| 142 class TestSafeBrowsingServiceFactory : public SafeBrowsingServiceFactory { | |
| 143 public: | |
| 144 explicit TestSafeBrowsingServiceFactory(const std::string& url_prefix) | |
| 145 : url_prefix_(url_prefix) {} | |
| 146 | |
| 147 SafeBrowsingService* CreateSafeBrowsingService() override { | |
| 148 return new FakeSafeBrowsingService(url_prefix_); | |
| 149 } | |
| 150 | |
| 151 private: | |
| 152 std::string url_prefix_; | |
| 153 }; | |
| 154 | |
| 155 } // namespace | 118 } // namespace |
| 156 | 119 |
| 157 // This starts the browser and keeps status of states related to SafeBrowsing. | 120 // This starts the browser and keeps status of states related to SafeBrowsing. |
| 158 class SafeBrowsingServerTest : public InProcessBrowserTest { | 121 class SafeBrowsingServerTest : public InProcessBrowserTest { |
| 159 public: | 122 public: |
| 160 SafeBrowsingServerTest() | 123 SafeBrowsingServerTest() |
| 161 : safe_browsing_service_(NULL), | 124 : is_database_ready_(true), |
| 162 is_database_ready_(true), | 125 is_update_scheduled_(false), |
| 163 is_update_scheduled_(false), | 126 is_checked_url_in_db_(false), |
| 164 is_checked_url_in_db_(false), | 127 is_checked_url_safe_(false) {} |
| 165 is_checked_url_safe_(false) { | |
| 166 } | |
| 167 | 128 |
| 168 ~SafeBrowsingServerTest() override {} | 129 ~SafeBrowsingServerTest() override {} |
| 169 | 130 |
| 170 void UpdateSafeBrowsingStatus() { | 131 void UpdateSafeBrowsingStatus() { |
| 171 ASSERT_TRUE(safe_browsing_service_); | 132 ASSERT_TRUE(sb_factory_->test_safe_browsing_service()); |
| 172 base::AutoLock lock(update_status_mutex_); | 133 base::AutoLock lock(update_status_mutex_); |
| 173 last_update_ = safe_browsing_service_->protocol_manager_->last_update(); | 134 last_update_ = sb_factory_->test_safe_browsing_service() |
| 174 is_update_scheduled_ = | 135 ->protocol_manager_->last_update(); |
| 175 safe_browsing_service_->protocol_manager_->update_timer_.IsRunning(); | 136 is_update_scheduled_ = sb_factory_->test_safe_browsing_service() |
| 137 ->protocol_manager_->update_timer_.IsRunning(); |
| 176 } | 138 } |
| 177 | 139 |
| 178 void ForceUpdate() { | 140 void ForceUpdate() { |
| 179 content::WindowedNotificationObserver observer( | 141 content::WindowedNotificationObserver observer( |
| 180 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, | 142 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, |
| 181 content::Source<SafeBrowsingDatabaseManager>(database_manager())); | 143 content::Source<SafeBrowsingDatabaseManager>(database_manager())); |
| 182 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 144 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 183 base::Bind(&SafeBrowsingServerTest::ForceUpdateOnIOThread, | 145 base::Bind(&SafeBrowsingServerTest::ForceUpdateOnIOThread, |
| 184 this)); | 146 this)); |
| 185 observer.Wait(); | 147 observer.Wait(); |
| 186 } | 148 } |
| 187 | 149 |
| 188 void ForceUpdateOnIOThread() { | 150 void ForceUpdateOnIOThread() { |
| 189 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 151 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 190 ASSERT_TRUE(safe_browsing_service_); | 152 ASSERT_TRUE(sb_factory_->test_safe_browsing_service()); |
| 191 safe_browsing_service_->protocol_manager_->ForceScheduleNextUpdate( | 153 sb_factory_->test_safe_browsing_service() |
| 192 base::TimeDelta::FromSeconds(0)); | 154 ->protocol_manager_->ForceScheduleNextUpdate( |
| 155 base::TimeDelta::FromSeconds(0)); |
| 193 } | 156 } |
| 194 | 157 |
| 195 | 158 |
| 196 void CheckIsDatabaseReady() { | 159 void CheckIsDatabaseReady() { |
| 197 base::AutoLock lock(update_status_mutex_); | 160 base::AutoLock lock(update_status_mutex_); |
| 198 is_database_ready_ = | 161 is_database_ready_ = |
| 199 !local_database_manager()->database_update_in_progress_; | 162 !local_database_manager()->database_update_in_progress_; |
| 200 } | 163 } |
| 201 | 164 |
| 202 void CheckUrl(SafeBrowsingDatabaseManager::Client* helper, const GURL& url) { | 165 void CheckUrl(SafeBrowsingDatabaseManager::Client* helper, const GURL& url) { |
| 203 ASSERT_TRUE(safe_browsing_service_); | 166 ASSERT_TRUE(sb_factory_->test_safe_browsing_service()); |
| 204 base::AutoLock lock(update_status_mutex_); | 167 base::AutoLock lock(update_status_mutex_); |
| 205 if (database_manager()->CheckBrowseUrl(url, helper)) { | 168 if (database_manager()->CheckBrowseUrl(url, helper)) { |
| 206 is_checked_url_in_db_ = false; | 169 is_checked_url_in_db_ = false; |
| 207 is_checked_url_safe_ = true; | 170 is_checked_url_safe_ = true; |
| 208 } else { | 171 } else { |
| 209 // In this case, Safebrowsing service will fetch the full hash | 172 // In this case, Safebrowsing service will fetch the full hash |
| 210 // from the server and examine that. Once it is done, | 173 // from the server and examine that. Once it is done, |
| 211 // set_is_checked_url_safe() will be called via callback. | 174 // set_is_checked_url_safe() will be called via callback. |
| 212 is_checked_url_in_db_ = true; | 175 is_checked_url_in_db_ = true; |
| 213 } | 176 } |
| 214 } | 177 } |
| 215 | 178 |
| 216 SafeBrowsingDatabaseManager* database_manager() { | 179 SafeBrowsingDatabaseManager* database_manager() { |
| 217 return safe_browsing_service_->database_manager().get(); | 180 return sb_factory_->test_safe_browsing_service()->database_manager().get(); |
| 218 } | 181 } |
| 219 | 182 |
| 220 // TODO(nparker): Remove the need for this by wiring in our own | 183 // TODO(nparker): Remove the need for this by wiring in our own |
| 221 // SafeBrowsingDatabaseManager factory and keep a ptr to the subclass. | 184 // SafeBrowsingDatabaseManager factory and keep a ptr to the subclass. |
| 222 LocalSafeBrowsingDatabaseManager* local_database_manager() { | 185 LocalSafeBrowsingDatabaseManager* local_database_manager() { |
| 223 return static_cast<LocalSafeBrowsingDatabaseManager*>(database_manager()); | 186 return static_cast<LocalSafeBrowsingDatabaseManager*>(database_manager()); |
| 224 } | 187 } |
| 225 | 188 |
| 226 | 189 |
| 227 bool is_checked_url_in_db() { | 190 bool is_checked_url_in_db() { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 256 | 219 |
| 257 scoped_refptr<base::SequencedTaskRunner> SafeBrowsingTaskRunner() { | 220 scoped_refptr<base::SequencedTaskRunner> SafeBrowsingTaskRunner() { |
| 258 return local_database_manager()->safe_browsing_task_runner_; | 221 return local_database_manager()->safe_browsing_task_runner_; |
| 259 } | 222 } |
| 260 | 223 |
| 261 const net::SpawnedTestServer& spawned_test_server() const { | 224 const net::SpawnedTestServer& spawned_test_server() const { |
| 262 return *test_server_; | 225 return *test_server_; |
| 263 } | 226 } |
| 264 | 227 |
| 265 protected: | 228 protected: |
| 266 bool InitSafeBrowsingService() { | 229 void SetProtocolConfigURLPrefix(const std::string& url_prefix) { |
| 267 safe_browsing_service_ = g_browser_process->safe_browsing_service(); | 230 SafeBrowsingProtocolConfig config; |
| 268 return safe_browsing_service_ != NULL; | 231 config.url_prefix = url_prefix; |
| 232 // Makes sure the auto update is not triggered. The tests will force the |
| 233 // update when needed. |
| 234 config.disable_auto_update = true; |
| 235 config.client_name = "browser_tests"; |
| 236 sb_factory_->SetTestProtocolConfig(config); |
| 269 } | 237 } |
| 270 | 238 |
| 271 void SetUp() override { | 239 void SetUp() override { |
| 272 base::FilePath datafile_path; | 240 base::FilePath datafile_path; |
| 273 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &datafile_path)); | 241 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &datafile_path)); |
| 274 | 242 |
| 275 datafile_path = datafile_path.Append(FILE_PATH_LITERAL("third_party")) | 243 datafile_path = datafile_path.Append(FILE_PATH_LITERAL("third_party")) |
| 276 .Append(FILE_PATH_LITERAL("safe_browsing")) | 244 .Append(FILE_PATH_LITERAL("safe_browsing")) |
| 277 .Append(FILE_PATH_LITERAL("testing")) | 245 .Append(FILE_PATH_LITERAL("testing")) |
| 278 .Append(kDataFile); | 246 .Append(kDataFile); |
| 279 test_server_.reset(new LocalSafeBrowsingTestServer(datafile_path)); | 247 test_server_.reset(new LocalSafeBrowsingTestServer(datafile_path)); |
| 280 ASSERT_TRUE(test_server_->Start()); | 248 ASSERT_TRUE(test_server_->Start()); |
| 281 LOG(INFO) << "server is " << test_server_->host_port_pair().ToString(); | 249 LOG(INFO) << "server is " << test_server_->host_port_pair().ToString(); |
| 282 | 250 |
| 251 sb_factory_.reset(new TestSafeBrowsingServiceFactory()); |
| 283 // Point to the testing server for all SafeBrowsing requests. | 252 // Point to the testing server for all SafeBrowsing requests. |
| 284 std::string url_prefix = test_server_->GetURL("safebrowsing").spec(); | 253 SetProtocolConfigURLPrefix(test_server_->GetURL("safebrowsing").spec()); |
| 285 sb_factory_.reset(new TestSafeBrowsingServiceFactory(url_prefix)); | |
| 286 SafeBrowsingService::RegisterFactory(sb_factory_.get()); | 254 SafeBrowsingService::RegisterFactory(sb_factory_.get()); |
| 287 | 255 |
| 288 InProcessBrowserTest::SetUp(); | 256 InProcessBrowserTest::SetUp(); |
| 289 } | 257 } |
| 290 | 258 |
| 291 void TearDown() override { | 259 void TearDown() override { |
| 292 InProcessBrowserTest::TearDown(); | 260 InProcessBrowserTest::TearDown(); |
| 293 | 261 |
| 294 SafeBrowsingService::RegisterFactory(NULL); | 262 SafeBrowsingService::RegisterFactory(NULL); |
| 295 } | 263 } |
| 296 | 264 |
| 297 void SetUpCommandLine(base::CommandLine* command_line) override { | 265 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 298 // TODO(lzheng): The test server does not understand download related | 266 // TODO(lzheng): The test server does not understand download related |
| 299 // requests. We need to fix the server. | 267 // requests. We need to fix the server. |
| 300 command_line->AppendSwitch(switches::kSbDisableDownloadProtection); | 268 command_line->AppendSwitch(switches::kSbDisableDownloadProtection); |
| 301 | 269 |
| 302 // TODO(gcasto): Generate new testing data that includes the | 270 // TODO(gcasto): Generate new testing data that includes the |
| 303 // client-side phishing whitelist. | 271 // client-side phishing whitelist. |
| 304 command_line->AppendSwitch( | 272 command_line->AppendSwitch( |
| 305 switches::kDisableClientSidePhishingDetection); | 273 switches::kDisableClientSidePhishingDetection); |
| 306 | 274 |
| 307 // TODO(kalman): Generate new testing data that includes the extension | 275 // TODO(kalman): Generate new testing data that includes the extension |
| 308 // blacklist. | 276 // blacklist. |
| 309 command_line->AppendSwitch(switches::kSbDisableExtensionBlacklist); | 277 command_line->AppendSwitch(switches::kSbDisableExtensionBlacklist); |
| 310 } | 278 } |
| 311 | 279 |
| 312 void SetTestStep(int step) { | 280 void SetTestStep(int step) { |
| 313 std::string test_step = base::StringPrintf("test_step=%d", step); | 281 std::string test_step = base::StringPrintf("test_step=%d", step); |
| 314 safe_browsing_service_->protocol_manager_->set_additional_query(test_step); | 282 sb_factory_->test_safe_browsing_service() |
| 283 ->protocol_manager_->set_additional_query(test_step); |
| 315 } | 284 } |
| 316 | 285 |
| 317 private: | |
| 318 std::unique_ptr<TestSafeBrowsingServiceFactory> sb_factory_; | 286 std::unique_ptr<TestSafeBrowsingServiceFactory> sb_factory_; |
| 319 | 287 |
| 320 SafeBrowsingService* safe_browsing_service_; | 288 private: |
| 321 | |
| 322 std::unique_ptr<net::SpawnedTestServer> test_server_; | 289 std::unique_ptr<net::SpawnedTestServer> test_server_; |
| 323 | 290 |
| 324 // Protects all variables below since they are read on UI thread | 291 // Protects all variables below since they are read on UI thread |
| 325 // but updated on IO thread or safebrowsing thread. | 292 // but updated on IO thread or safebrowsing thread. |
| 326 base::Lock update_status_mutex_; | 293 base::Lock update_status_mutex_; |
| 327 | 294 |
| 328 // States associated with safebrowsing service updates. | 295 // States associated with safebrowsing service updates. |
| 329 bool is_database_ready_; | 296 bool is_database_ready_; |
| 330 base::Time last_update_; | 297 base::Time last_update_; |
| 331 bool is_update_scheduled_; | 298 bool is_update_scheduled_; |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 494 std::unique_ptr<net::URLFetcher> url_fetcher_; | 461 std::unique_ptr<net::URLFetcher> url_fetcher_; |
| 495 std::string response_data_; | 462 std::string response_data_; |
| 496 net::URLRequestStatus::Status response_status_; | 463 net::URLRequestStatus::Status response_status_; |
| 497 net::URLRequestContextGetter* request_context_; | 464 net::URLRequestContextGetter* request_context_; |
| 498 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServerTestHelper); | 465 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServerTestHelper); |
| 499 }; | 466 }; |
| 500 | 467 |
| 501 // TODO(shess): Disabled pending new data for third_party/safe_browsing/testing/ | 468 // TODO(shess): Disabled pending new data for third_party/safe_browsing/testing/ |
| 502 IN_PROC_BROWSER_TEST_F(SafeBrowsingServerTest, | 469 IN_PROC_BROWSER_TEST_F(SafeBrowsingServerTest, |
| 503 DISABLED_SafeBrowsingServerTest) { | 470 DISABLED_SafeBrowsingServerTest) { |
| 504 LOG(INFO) << "Start test"; | 471 ASSERT_TRUE(sb_factory_->test_safe_browsing_service() != NULL); |
| 505 ASSERT_TRUE(InitSafeBrowsingService()); | |
| 506 | 472 |
| 507 net::URLRequestContextGetter* request_context = | 473 net::URLRequestContextGetter* request_context = |
| 508 browser()->profile()->GetRequestContext(); | 474 browser()->profile()->GetRequestContext(); |
| 509 scoped_refptr<SafeBrowsingServerTestHelper> safe_browsing_helper( | 475 scoped_refptr<SafeBrowsingServerTestHelper> safe_browsing_helper( |
| 510 new SafeBrowsingServerTestHelper(this, request_context)); | 476 new SafeBrowsingServerTestHelper(this, request_context)); |
| 511 int last_step = 0; | 477 int last_step = 0; |
| 512 | 478 |
| 513 // Waits and makes sure safebrowsing update is not happening. | 479 // Waits and makes sure safebrowsing update is not happening. |
| 514 // The wait will stop once OnWaitForStatusUpdateDone in | 480 // The wait will stop once OnWaitForStatusUpdateDone in |
| 515 // safe_browsing_helper is called and status from safe_browsing_service_ | 481 // safe_browsing_helper is called and status from test_safe_browsing_service |
| 516 // is checked. | 482 // is checked. |
| 517 safe_browsing_helper->UpdateStatus(); | 483 safe_browsing_helper->UpdateStatus(); |
| 518 EXPECT_TRUE(is_database_ready()); | 484 EXPECT_TRUE(is_database_ready()); |
| 519 EXPECT_FALSE(is_update_scheduled()); | 485 EXPECT_FALSE(is_update_scheduled()); |
| 520 EXPECT_TRUE(last_update().is_null()); | 486 EXPECT_TRUE(last_update().is_null()); |
| 521 // Starts updates. After each update, the test will fetch a list of URLs with | 487 // Starts updates. After each update, the test will fetch a list of URLs with |
| 522 // expected results to verify with safebrowsing service. If there is no error, | 488 // expected results to verify with safebrowsing service. If there is no error, |
| 523 // the test moves on to the next step to get more update chunks. | 489 // the test moves on to the next step to get more update chunks. |
| 524 // This repeats till there is no update data. | 490 // This repeats till there is no update data. |
| 525 for (int step = 1;; step++) { | 491 for (int step = 1;; step++) { |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 582 } | 548 } |
| 583 | 549 |
| 584 // Verifies with server if test is done and waits till server responses. | 550 // Verifies with server if test is done and waits till server responses. |
| 585 EXPECT_EQ(net::URLRequestStatus::SUCCESS, | 551 EXPECT_EQ(net::URLRequestStatus::SUCCESS, |
| 586 safe_browsing_helper->VerifyTestComplete(spawned_test_server(), | 552 safe_browsing_helper->VerifyTestComplete(spawned_test_server(), |
| 587 last_step)); | 553 last_step)); |
| 588 EXPECT_EQ("yes", safe_browsing_helper->response_data()); | 554 EXPECT_EQ("yes", safe_browsing_helper->response_data()); |
| 589 } | 555 } |
| 590 | 556 |
| 591 } // namespace safe_browsing | 557 } // namespace safe_browsing |
| OLD | NEW |