| 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" |
| 6 |
| 5 #include <stdint.h> | 7 #include <stdint.h> |
| 6 | 8 |
| 7 #include <map> | 9 #include <map> |
| 10 #include <memory> |
| 8 #include <queue> | 11 #include <queue> |
| 9 #include <string> | 12 #include <string> |
| 10 | 13 |
| 11 #include "base/bind.h" | 14 #include "base/bind.h" |
| 12 #include "base/callback.h" | 15 #include "base/callback.h" |
| 13 #include "base/logging.h" | 16 #include "base/logging.h" |
| 14 #include "base/macros.h" | 17 #include "base/macros.h" |
| 15 #include "base/memory/scoped_ptr.h" | |
| 16 #include "base/message_loop/message_loop.h" | 18 #include "base/message_loop/message_loop.h" |
| 17 #include "base/metrics/field_trial.h" | 19 #include "base/metrics/field_trial.h" |
| 18 #include "base/strings/string_number_conversions.h" | 20 #include "base/strings/string_number_conversions.h" |
| 19 #include "base/time/time.h" | 21 #include "base/time/time.h" |
| 20 #include "chrome/browser/safe_browsing/client_side_detection_service.h" | |
| 21 #include "chrome/common/safe_browsing/client_model.pb.h" | 22 #include "chrome/common/safe_browsing/client_model.pb.h" |
| 22 #include "chrome/common/safe_browsing/csd.pb.h" | 23 #include "chrome/common/safe_browsing/csd.pb.h" |
| 23 #include "components/variations/variations_associated_data.h" | 24 #include "components/variations/variations_associated_data.h" |
| 24 #include "content/public/test/test_browser_thread.h" | 25 #include "content/public/test/test_browser_thread.h" |
| 25 #include "crypto/sha2.h" | 26 #include "crypto/sha2.h" |
| 26 #include "net/http/http_status_code.h" | 27 #include "net/http/http_status_code.h" |
| 27 #include "net/url_request/test_url_fetcher_factory.h" | 28 #include "net/url_request/test_url_fetcher_factory.h" |
| 28 #include "net/url_request/url_request_status.h" | 29 #include "net/url_request/url_request_status.h" |
| 29 #include "testing/gmock/include/gmock/gmock.h" | 30 #include "testing/gmock/include/gmock/gmock.h" |
| 30 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 request, | 92 request, |
| 92 false, | 93 false, |
| 93 base::Bind(&ClientSideDetectionServiceTest::SendRequestDone, | 94 base::Bind(&ClientSideDetectionServiceTest::SendRequestDone, |
| 94 base::Unretained(this))); | 95 base::Unretained(this))); |
| 95 phishing_url_ = phishing_url; | 96 phishing_url_ = phishing_url; |
| 96 msg_loop_.Run(); // Waits until callback is called. | 97 msg_loop_.Run(); // Waits until callback is called. |
| 97 return is_phishing_; | 98 return is_phishing_; |
| 98 } | 99 } |
| 99 | 100 |
| 100 bool SendClientReportMalwareRequest(const GURL& url) { | 101 bool SendClientReportMalwareRequest(const GURL& url) { |
| 101 scoped_ptr<ClientMalwareRequest> request(new ClientMalwareRequest()); | 102 std::unique_ptr<ClientMalwareRequest> request(new ClientMalwareRequest()); |
| 102 request->set_url(url.spec()); | 103 request->set_url(url.spec()); |
| 103 csd_service_->SendClientReportMalwareRequest( | 104 csd_service_->SendClientReportMalwareRequest( |
| 104 request.release(), | 105 request.release(), |
| 105 base::Bind(&ClientSideDetectionServiceTest::SendMalwareRequestDone, | 106 base::Bind(&ClientSideDetectionServiceTest::SendMalwareRequestDone, |
| 106 base::Unretained(this))); | 107 base::Unretained(this))); |
| 107 phishing_url_ = url; | 108 phishing_url_ = url; |
| 108 msg_loop_.Run(); // Waits until callback is called. | 109 msg_loop_.Run(); // Waits until callback is called. |
| 109 return is_malware_; | 110 return is_malware_; |
| 110 } | 111 } |
| 111 | 112 |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 request->add_non_model_feature_map(); | 225 request->add_non_model_feature_map(); |
| 225 feature->set_name(name); | 226 feature->set_name(name); |
| 226 feature->set_value(value); | 227 feature->set_value(value); |
| 227 } | 228 } |
| 228 | 229 |
| 229 void CheckConfirmedMalwareUrl(GURL url) { | 230 void CheckConfirmedMalwareUrl(GURL url) { |
| 230 ASSERT_EQ(confirmed_malware_url_, url); | 231 ASSERT_EQ(confirmed_malware_url_, url); |
| 231 } | 232 } |
| 232 | 233 |
| 233 protected: | 234 protected: |
| 234 scoped_ptr<ClientSideDetectionService> csd_service_; | 235 std::unique_ptr<ClientSideDetectionService> csd_service_; |
| 235 scoped_ptr<net::FakeURLFetcherFactory> factory_; | 236 std::unique_ptr<net::FakeURLFetcherFactory> factory_; |
| 236 base::MessageLoop msg_loop_; | 237 base::MessageLoop msg_loop_; |
| 237 | 238 |
| 238 private: | 239 private: |
| 239 void SendRequestDone(GURL phishing_url, bool is_phishing) { | 240 void SendRequestDone(GURL phishing_url, bool is_phishing) { |
| 240 ASSERT_EQ(phishing_url, phishing_url_); | 241 ASSERT_EQ(phishing_url, phishing_url_); |
| 241 is_phishing_ = is_phishing; | 242 is_phishing_ = is_phishing; |
| 242 msg_loop_.QuitWhenIdle(); | 243 msg_loop_.QuitWhenIdle(); |
| 243 } | 244 } |
| 244 | 245 |
| 245 void SendMalwareRequestDone(GURL original_url, GURL malware_url, | 246 void SendMalwareRequestDone(GURL original_url, GURL malware_url, |
| 246 bool is_malware) { | 247 bool is_malware) { |
| 247 ASSERT_EQ(phishing_url_, original_url); | 248 ASSERT_EQ(phishing_url_, original_url); |
| 248 confirmed_malware_url_ = malware_url; | 249 confirmed_malware_url_ = malware_url; |
| 249 is_malware_ = is_malware; | 250 is_malware_ = is_malware; |
| 250 msg_loop_.QuitWhenIdle(); | 251 msg_loop_.QuitWhenIdle(); |
| 251 } | 252 } |
| 252 | 253 |
| 253 scoped_ptr<content::TestBrowserThread> browser_thread_; | 254 std::unique_ptr<content::TestBrowserThread> browser_thread_; |
| 254 scoped_ptr<content::TestBrowserThread> file_thread_; | 255 std::unique_ptr<content::TestBrowserThread> file_thread_; |
| 255 scoped_ptr<base::FieldTrialList> field_trials_; | 256 std::unique_ptr<base::FieldTrialList> field_trials_; |
| 256 | 257 |
| 257 GURL phishing_url_; | 258 GURL phishing_url_; |
| 258 GURL confirmed_malware_url_; | 259 GURL confirmed_malware_url_; |
| 259 bool is_phishing_; | 260 bool is_phishing_; |
| 260 bool is_malware_; | 261 bool is_malware_; |
| 261 }; | 262 }; |
| 262 | 263 |
| 263 | 264 |
| 264 TEST_F(ClientSideDetectionServiceTest, ServiceObjectDeletedBeforeCallbackDone) { | 265 TEST_F(ClientSideDetectionServiceTest, ServiceObjectDeletedBeforeCallbackDone) { |
| 265 SetModelFetchResponses(); | 266 SetModelFetchResponses(); |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 480 | 481 |
| 481 // Check that disabling again doesn't request the model. | 482 // Check that disabling again doesn't request the model. |
| 482 csd_service_->SetEnabledAndRefreshState(false); | 483 csd_service_->SetEnabledAndRefreshState(false); |
| 483 // No calls expected. | 484 // No calls expected. |
| 484 msg_loop_.RunUntilIdle(); | 485 msg_loop_.RunUntilIdle(); |
| 485 Mock::VerifyAndClearExpectations(service); | 486 Mock::VerifyAndClearExpectations(service); |
| 486 Mock::VerifyAndClearExpectations(loader_1); | 487 Mock::VerifyAndClearExpectations(loader_1); |
| 487 Mock::VerifyAndClearExpectations(loader_2); | 488 Mock::VerifyAndClearExpectations(loader_2); |
| 488 } | 489 } |
| 489 } // namespace safe_browsing | 490 } // namespace safe_browsing |
| OLD | NEW |