| 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 #include "chrome/browser/safe_browsing/client_side_detection_service.h" | 5 #include "chrome/browser/safe_browsing/client_side_detection_service.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <queue> | 11 #include <queue> |
| 12 #include <string> | 12 #include <string> |
| 13 | 13 |
| 14 #include "base/bind.h" | 14 #include "base/bind.h" |
| 15 #include "base/callback.h" | 15 #include "base/callback.h" |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/macros.h" | 17 #include "base/macros.h" |
| 18 #include "base/message_loop/message_loop.h" | 18 #include "base/message_loop/message_loop.h" |
| 19 #include "base/metrics/field_trial.h" | 19 #include "base/metrics/field_trial.h" |
| 20 #include "base/run_loop.h" |
| 20 #include "base/strings/string_number_conversions.h" | 21 #include "base/strings/string_number_conversions.h" |
| 21 #include "base/time/time.h" | 22 #include "base/time/time.h" |
| 22 #include "chrome/common/safe_browsing/client_model.pb.h" | 23 #include "chrome/common/safe_browsing/client_model.pb.h" |
| 23 #include "chrome/common/safe_browsing/csd.pb.h" | 24 #include "chrome/common/safe_browsing/csd.pb.h" |
| 24 #include "components/variations/variations_associated_data.h" | 25 #include "components/variations/variations_associated_data.h" |
| 25 #include "content/public/test/test_browser_thread.h" | 26 #include "content/public/test/test_browser_thread.h" |
| 26 #include "crypto/sha2.h" | 27 #include "crypto/sha2.h" |
| 27 #include "net/http/http_status_code.h" | 28 #include "net/http/http_status_code.h" |
| 28 #include "net/url_request/test_url_fetcher_factory.h" | 29 #include "net/url_request/test_url_fetcher_factory.h" |
| 29 #include "net/url_request/url_request_status.h" | 30 #include "net/url_request/url_request_status.h" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 protected: | 70 protected: |
| 70 void SetUp() override { | 71 void SetUp() override { |
| 71 file_thread_.reset(new content::TestBrowserThread(BrowserThread::FILE, | 72 file_thread_.reset(new content::TestBrowserThread(BrowserThread::FILE, |
| 72 &msg_loop_)); | 73 &msg_loop_)); |
| 73 factory_.reset(new net::FakeURLFetcherFactory(NULL)); | 74 factory_.reset(new net::FakeURLFetcherFactory(NULL)); |
| 74 browser_thread_.reset(new content::TestBrowserThread(BrowserThread::UI, | 75 browser_thread_.reset(new content::TestBrowserThread(BrowserThread::UI, |
| 75 &msg_loop_)); | 76 &msg_loop_)); |
| 76 } | 77 } |
| 77 | 78 |
| 78 void TearDown() override { | 79 void TearDown() override { |
| 79 msg_loop_.RunUntilIdle(); | 80 base::RunLoop().RunUntilIdle(); |
| 80 csd_service_.reset(); | 81 csd_service_.reset(); |
| 81 file_thread_.reset(); | 82 file_thread_.reset(); |
| 82 browser_thread_.reset(); | 83 browser_thread_.reset(); |
| 83 } | 84 } |
| 84 | 85 |
| 85 bool SendClientReportPhishingRequest(const GURL& phishing_url, | 86 bool SendClientReportPhishingRequest(const GURL& phishing_url, |
| 86 float score) { | 87 float score) { |
| 87 ClientPhishingRequest* request = new ClientPhishingRequest(); | 88 ClientPhishingRequest* request = new ClientPhishingRequest(); |
| 88 request->set_url(phishing_url.spec()); | 89 request->set_url(phishing_url.spec()); |
| 89 request->set_client_score(score); | 90 request->set_client_score(score); |
| 90 request->set_is_phishing(true); // client thinks the URL is phishing. | 91 request->set_is_phishing(true); // client thinks the URL is phishing. |
| 91 csd_service_->SendClientReportPhishingRequest( | 92 csd_service_->SendClientReportPhishingRequest( |
| 92 request, | 93 request, |
| 93 false, | 94 false, |
| 94 base::Bind(&ClientSideDetectionServiceTest::SendRequestDone, | 95 base::Bind(&ClientSideDetectionServiceTest::SendRequestDone, |
| 95 base::Unretained(this))); | 96 base::Unretained(this))); |
| 96 phishing_url_ = phishing_url; | 97 phishing_url_ = phishing_url; |
| 97 msg_loop_.Run(); // Waits until callback is called. | 98 base::RunLoop().Run(); // Waits until callback is called. |
| 98 return is_phishing_; | 99 return is_phishing_; |
| 99 } | 100 } |
| 100 | 101 |
| 101 bool SendClientReportMalwareRequest(const GURL& url) { | 102 bool SendClientReportMalwareRequest(const GURL& url) { |
| 102 std::unique_ptr<ClientMalwareRequest> request(new ClientMalwareRequest()); | 103 std::unique_ptr<ClientMalwareRequest> request(new ClientMalwareRequest()); |
| 103 request->set_url(url.spec()); | 104 request->set_url(url.spec()); |
| 104 csd_service_->SendClientReportMalwareRequest( | 105 csd_service_->SendClientReportMalwareRequest( |
| 105 request.release(), | 106 request.release(), |
| 106 base::Bind(&ClientSideDetectionServiceTest::SendMalwareRequestDone, | 107 base::Bind(&ClientSideDetectionServiceTest::SendMalwareRequestDone, |
| 107 base::Unretained(this))); | 108 base::Unretained(this))); |
| 108 phishing_url_ = url; | 109 phishing_url_ = url; |
| 109 msg_loop_.Run(); // Waits until callback is called. | 110 base::RunLoop().Run(); // Waits until callback is called. |
| 110 return is_malware_; | 111 return is_malware_; |
| 111 } | 112 } |
| 112 | 113 |
| 113 void SetModelFetchResponses() { | 114 void SetModelFetchResponses() { |
| 114 // Set reponses for both models. | 115 // Set reponses for both models. |
| 115 factory_->SetFakeResponse(GURL(ModelLoader::kClientModelUrlPrefix + | 116 factory_->SetFakeResponse(GURL(ModelLoader::kClientModelUrlPrefix + |
| 116 ModelLoader::FillInModelName(false, 0)), | 117 ModelLoader::FillInModelName(false, 0)), |
| 117 "bogusmodel", net::HTTP_OK, | 118 "bogusmodel", net::HTTP_OK, |
| 118 net::URLRequestStatus::SUCCESS); | 119 net::URLRequestStatus::SUCCESS); |
| 119 factory_->SetFakeResponse(GURL(ModelLoader::kClientModelUrlPrefix + | 120 factory_->SetFakeResponse(GURL(ModelLoader::kClientModelUrlPrefix + |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 TEST_F(ClientSideDetectionServiceTest, ServiceObjectDeletedBeforeCallbackDone) { | 266 TEST_F(ClientSideDetectionServiceTest, ServiceObjectDeletedBeforeCallbackDone) { |
| 266 SetModelFetchResponses(); | 267 SetModelFetchResponses(); |
| 267 csd_service_.reset(ClientSideDetectionService::Create(NULL)); | 268 csd_service_.reset(ClientSideDetectionService::Create(NULL)); |
| 268 csd_service_->SetEnabledAndRefreshState(true); | 269 csd_service_->SetEnabledAndRefreshState(true); |
| 269 EXPECT_TRUE(csd_service_.get() != NULL); | 270 EXPECT_TRUE(csd_service_.get() != NULL); |
| 270 // We delete the client-side detection service class even though the callbacks | 271 // We delete the client-side detection service class even though the callbacks |
| 271 // haven't run yet. | 272 // haven't run yet. |
| 272 csd_service_.reset(); | 273 csd_service_.reset(); |
| 273 // Waiting for the callbacks to run should not crash even if the service | 274 // Waiting for the callbacks to run should not crash even if the service |
| 274 // object is gone. | 275 // object is gone. |
| 275 msg_loop_.RunUntilIdle(); | 276 base::RunLoop().RunUntilIdle(); |
| 276 } | 277 } |
| 277 | 278 |
| 278 TEST_F(ClientSideDetectionServiceTest, SendClientReportPhishingRequest) { | 279 TEST_F(ClientSideDetectionServiceTest, SendClientReportPhishingRequest) { |
| 279 SetModelFetchResponses(); | 280 SetModelFetchResponses(); |
| 280 csd_service_.reset(ClientSideDetectionService::Create(NULL)); | 281 csd_service_.reset(ClientSideDetectionService::Create(NULL)); |
| 281 csd_service_->SetEnabledAndRefreshState(true); | 282 csd_service_->SetEnabledAndRefreshState(true); |
| 282 | 283 |
| 283 GURL url("http://a.com/"); | 284 GURL url("http://a.com/"); |
| 284 float score = 0.4f; // Some random client score. | 285 float score = 0.4f; // Some random client score. |
| 285 | 286 |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 450 Mock::VerifyAndClearExpectations(loader_2); | 451 Mock::VerifyAndClearExpectations(loader_2); |
| 451 | 452 |
| 452 // Check that initial ScheduleFetch() calls are made. | 453 // Check that initial ScheduleFetch() calls are made. |
| 453 EXPECT_CALL(*loader_1, | 454 EXPECT_CALL(*loader_1, |
| 454 ScheduleFetch( | 455 ScheduleFetch( |
| 455 ClientSideDetectionService::kInitialClientModelFetchDelayMs)); | 456 ClientSideDetectionService::kInitialClientModelFetchDelayMs)); |
| 456 EXPECT_CALL(*loader_2, | 457 EXPECT_CALL(*loader_2, |
| 457 ScheduleFetch( | 458 ScheduleFetch( |
| 458 ClientSideDetectionService::kInitialClientModelFetchDelayMs)); | 459 ClientSideDetectionService::kInitialClientModelFetchDelayMs)); |
| 459 csd_service_->SetEnabledAndRefreshState(true); | 460 csd_service_->SetEnabledAndRefreshState(true); |
| 460 msg_loop_.RunUntilIdle(); | 461 base::RunLoop().RunUntilIdle(); |
| 461 Mock::VerifyAndClearExpectations(service); | 462 Mock::VerifyAndClearExpectations(service); |
| 462 Mock::VerifyAndClearExpectations(loader_1); | 463 Mock::VerifyAndClearExpectations(loader_1); |
| 463 Mock::VerifyAndClearExpectations(loader_2); | 464 Mock::VerifyAndClearExpectations(loader_2); |
| 464 | 465 |
| 465 // Check that enabling again doesn't request the model. | 466 // Check that enabling again doesn't request the model. |
| 466 csd_service_->SetEnabledAndRefreshState(true); | 467 csd_service_->SetEnabledAndRefreshState(true); |
| 467 // No calls expected. | 468 // No calls expected. |
| 468 msg_loop_.RunUntilIdle(); | 469 base::RunLoop().RunUntilIdle(); |
| 469 Mock::VerifyAndClearExpectations(service); | 470 Mock::VerifyAndClearExpectations(service); |
| 470 Mock::VerifyAndClearExpectations(loader_1); | 471 Mock::VerifyAndClearExpectations(loader_1); |
| 471 Mock::VerifyAndClearExpectations(loader_2); | 472 Mock::VerifyAndClearExpectations(loader_2); |
| 472 | 473 |
| 473 // Check that disabling the service cancels pending requests. | 474 // Check that disabling the service cancels pending requests. |
| 474 EXPECT_CALL(*loader_1, CancelFetcher()); | 475 EXPECT_CALL(*loader_1, CancelFetcher()); |
| 475 EXPECT_CALL(*loader_2, CancelFetcher()); | 476 EXPECT_CALL(*loader_2, CancelFetcher()); |
| 476 csd_service_->SetEnabledAndRefreshState(false); | 477 csd_service_->SetEnabledAndRefreshState(false); |
| 477 msg_loop_.RunUntilIdle(); | 478 base::RunLoop().RunUntilIdle(); |
| 478 Mock::VerifyAndClearExpectations(service); | 479 Mock::VerifyAndClearExpectations(service); |
| 479 Mock::VerifyAndClearExpectations(loader_1); | 480 Mock::VerifyAndClearExpectations(loader_1); |
| 480 Mock::VerifyAndClearExpectations(loader_2); | 481 Mock::VerifyAndClearExpectations(loader_2); |
| 481 | 482 |
| 482 // Check that disabling again doesn't request the model. | 483 // Check that disabling again doesn't request the model. |
| 483 csd_service_->SetEnabledAndRefreshState(false); | 484 csd_service_->SetEnabledAndRefreshState(false); |
| 484 // No calls expected. | 485 // No calls expected. |
| 485 msg_loop_.RunUntilIdle(); | 486 base::RunLoop().RunUntilIdle(); |
| 486 Mock::VerifyAndClearExpectations(service); | 487 Mock::VerifyAndClearExpectations(service); |
| 487 Mock::VerifyAndClearExpectations(loader_1); | 488 Mock::VerifyAndClearExpectations(loader_1); |
| 488 Mock::VerifyAndClearExpectations(loader_2); | 489 Mock::VerifyAndClearExpectations(loader_2); |
| 489 } | 490 } |
| 490 } // namespace safe_browsing | 491 } // namespace safe_browsing |
| OLD | NEW |