| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/incident_reporting/resource_request_detec
tor.h" | 5 #include "chrome/browser/safe_browsing/incident_reporting/resource_request_detec
tor.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/memory/ptr_util.h" |
| 10 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 11 #include "chrome/browser/safe_browsing/incident_reporting/incident.h" | 12 #include "chrome/browser/safe_browsing/incident_reporting/incident.h" |
| 12 #include "chrome/browser/safe_browsing/incident_reporting/incident_receiver.h" | 13 #include "chrome/browser/safe_browsing/incident_reporting/incident_receiver.h" |
| 13 #include "chrome/browser/safe_browsing/incident_reporting/mock_incident_receiver
.h" | 14 #include "chrome/browser/safe_browsing/incident_reporting/mock_incident_receiver
.h" |
| 14 #include "chrome/common/safe_browsing/csd.pb.h" | 15 #include "chrome/common/safe_browsing/csd.pb.h" |
| 15 #include "components/safe_browsing_db/test_database_manager.h" | 16 #include "components/safe_browsing_db/test_database_manager.h" |
| 16 #include "content/public/browser/resource_request_info.h" | 17 #include "content/public/browser/resource_request_info.h" |
| 17 #include "content/public/common/resource_type.h" | 18 #include "content/public/common/resource_type.h" |
| 18 #include "content/public/test/test_browser_thread_bundle.h" | 19 #include "content/public/test/test_browser_thread_bundle.h" |
| 19 #include "crypto/sha2.h" | 20 #include "crypto/sha2.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 32 using ::testing::_; | 33 using ::testing::_; |
| 33 | 34 |
| 34 namespace safe_browsing { | 35 namespace safe_browsing { |
| 35 | 36 |
| 36 namespace { | 37 namespace { |
| 37 | 38 |
| 38 class FakeResourceRequestDetector : public ResourceRequestDetector { | 39 class FakeResourceRequestDetector : public ResourceRequestDetector { |
| 39 public: | 40 public: |
| 40 FakeResourceRequestDetector( | 41 FakeResourceRequestDetector( |
| 41 scoped_refptr<SafeBrowsingDatabaseManager> database_manager, | 42 scoped_refptr<SafeBrowsingDatabaseManager> database_manager, |
| 42 scoped_ptr<IncidentReceiver> incident_receiver) | 43 std::unique_ptr<IncidentReceiver> incident_receiver) |
| 43 : ResourceRequestDetector(database_manager, | 44 : ResourceRequestDetector(database_manager, |
| 44 std::move(incident_receiver)) { | 45 std::move(incident_receiver)) { |
| 45 FakeResourceRequestDetector::set_allow_null_profile_for_testing(true); | 46 FakeResourceRequestDetector::set_allow_null_profile_for_testing(true); |
| 46 } | 47 } |
| 47 }; | 48 }; |
| 48 | 49 |
| 49 class MockSafeBrowsingDatabaseManager : public TestSafeBrowsingDatabaseManager { | 50 class MockSafeBrowsingDatabaseManager : public TestSafeBrowsingDatabaseManager { |
| 50 public: | 51 public: |
| 51 MockSafeBrowsingDatabaseManager() {} | 52 MockSafeBrowsingDatabaseManager() {} |
| 52 | 53 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 69 } | 70 } |
| 70 | 71 |
| 71 class ResourceRequestDetectorTest : public testing::Test { | 72 class ResourceRequestDetectorTest : public testing::Test { |
| 72 protected: | 73 protected: |
| 73 using ResourceRequestIncidentMessage = | 74 using ResourceRequestIncidentMessage = |
| 74 ClientIncidentReport::IncidentData::ResourceRequestIncident; | 75 ClientIncidentReport::IncidentData::ResourceRequestIncident; |
| 75 | 76 |
| 76 ResourceRequestDetectorTest() | 77 ResourceRequestDetectorTest() |
| 77 : mock_incident_receiver_( | 78 : mock_incident_receiver_( |
| 78 new StrictMock<safe_browsing::MockIncidentReceiver>()), | 79 new StrictMock<safe_browsing::MockIncidentReceiver>()), |
| 79 mock_database_manager_( | 80 mock_database_manager_(new StrictMock<MockSafeBrowsingDatabaseManager>), |
| 80 new StrictMock<MockSafeBrowsingDatabaseManager>), | |
| 81 fake_resource_request_detector_( | 81 fake_resource_request_detector_( |
| 82 mock_database_manager_, | 82 mock_database_manager_, |
| 83 make_scoped_ptr(mock_incident_receiver_)) {} | 83 base::WrapUnique(mock_incident_receiver_)) {} |
| 84 | 84 |
| 85 scoped_ptr<net::URLRequest> GetTestURLRequest( | 85 std::unique_ptr<net::URLRequest> GetTestURLRequest( |
| 86 const std::string& url, | 86 const std::string& url, |
| 87 content::ResourceType resource_type) const { | 87 content::ResourceType resource_type) const { |
| 88 scoped_ptr<net::URLRequest> url_request( | 88 std::unique_ptr<net::URLRequest> url_request( |
| 89 context_.CreateRequest(GURL(url), net::DEFAULT_PRIORITY, NULL)); | 89 context_.CreateRequest(GURL(url), net::DEFAULT_PRIORITY, NULL)); |
| 90 | 90 |
| 91 content::ResourceRequestInfo::AllocateForTesting( | 91 content::ResourceRequestInfo::AllocateForTesting( |
| 92 url_request.get(), resource_type, | 92 url_request.get(), resource_type, |
| 93 NULL, // resource_context | 93 NULL, // resource_context |
| 94 0, // render_process_id | 94 0, // render_process_id |
| 95 0, // render_view_id | 95 0, // render_view_id |
| 96 MSG_ROUTING_NONE, // render_frame_id | 96 MSG_ROUTING_NONE, // render_frame_id |
| 97 true, // is_main_frame | 97 true, // is_main_frame |
| 98 false, // parent_is_main_frame | 98 false, // parent_is_main_frame |
| (...skipping 20 matching lines...) Expand all Loading... |
| 119 SBThreatType threat_type = is_blacklisted | 119 SBThreatType threat_type = is_blacklisted |
| 120 ? SB_THREAT_TYPE_BLACKLISTED_RESOURCE | 120 ? SB_THREAT_TYPE_BLACKLISTED_RESOURCE |
| 121 : SB_THREAT_TYPE_SAFE; | 121 : SB_THREAT_TYPE_SAFE; |
| 122 const GURL gurl(url); | 122 const GURL gurl(url); |
| 123 EXPECT_CALL(*mock_database_manager_, CheckResourceUrl(gurl, _)) | 123 EXPECT_CALL(*mock_database_manager_, CheckResourceUrl(gurl, _)) |
| 124 .WillOnce(CallClientWithResult(gurl, threat_type, digest)); | 124 .WillOnce(CallClientWithResult(gurl, threat_type, digest)); |
| 125 } | 125 } |
| 126 | 126 |
| 127 void ExpectNoIncident(const std::string& url, | 127 void ExpectNoIncident(const std::string& url, |
| 128 content::ResourceType resource_type) { | 128 content::ResourceType resource_type) { |
| 129 scoped_ptr<net::URLRequest> request(GetTestURLRequest(url, resource_type)); | 129 std::unique_ptr<net::URLRequest> request( |
| 130 GetTestURLRequest(url, resource_type)); |
| 130 | 131 |
| 131 EXPECT_CALL(*mock_incident_receiver_, DoAddIncidentForProfile(IsNull(), _)) | 132 EXPECT_CALL(*mock_incident_receiver_, DoAddIncidentForProfile(IsNull(), _)) |
| 132 .Times(0); | 133 .Times(0); |
| 133 | 134 |
| 134 fake_resource_request_detector_.OnResourceRequest(request.get()); | 135 fake_resource_request_detector_.OnResourceRequest(request.get()); |
| 135 base::RunLoop().RunUntilIdle(); | 136 base::RunLoop().RunUntilIdle(); |
| 136 } | 137 } |
| 137 | 138 |
| 138 void ExpectIncidentAdded( | 139 void ExpectIncidentAdded( |
| 139 const std::string& url, | 140 const std::string& url, |
| 140 content::ResourceType resource_type, | 141 content::ResourceType resource_type, |
| 141 ResourceRequestIncidentMessage::Type expected_type, | 142 ResourceRequestIncidentMessage::Type expected_type, |
| 142 const std::string& expected_digest) { | 143 const std::string& expected_digest) { |
| 143 scoped_ptr<net::URLRequest> request(GetTestURLRequest(url, resource_type)); | 144 std::unique_ptr<net::URLRequest> request( |
| 144 scoped_ptr<Incident> incident; | 145 GetTestURLRequest(url, resource_type)); |
| 146 std::unique_ptr<Incident> incident; |
| 145 EXPECT_CALL(*mock_incident_receiver_, DoAddIncidentForProfile(IsNull(), _)) | 147 EXPECT_CALL(*mock_incident_receiver_, DoAddIncidentForProfile(IsNull(), _)) |
| 146 .WillOnce(WithArg<1>(TakeIncident(&incident))); | 148 .WillOnce(WithArg<1>(TakeIncident(&incident))); |
| 147 | 149 |
| 148 fake_resource_request_detector_.OnResourceRequest(request.get()); | 150 fake_resource_request_detector_.OnResourceRequest(request.get()); |
| 149 base::RunLoop().RunUntilIdle(); | 151 base::RunLoop().RunUntilIdle(); |
| 150 | 152 |
| 151 ASSERT_TRUE(incident); | 153 ASSERT_TRUE(incident); |
| 152 scoped_ptr<ClientIncidentReport_IncidentData> incident_data = | 154 std::unique_ptr<ClientIncidentReport_IncidentData> incident_data = |
| 153 incident->TakePayload(); | 155 incident->TakePayload(); |
| 154 ASSERT_TRUE(incident_data && incident_data->has_resource_request()); | 156 ASSERT_TRUE(incident_data && incident_data->has_resource_request()); |
| 155 const ResourceRequestIncidentMessage& script_request_incident = | 157 const ResourceRequestIncidentMessage& script_request_incident = |
| 156 incident_data->resource_request(); | 158 incident_data->resource_request(); |
| 157 EXPECT_TRUE(script_request_incident.has_digest()); | 159 EXPECT_TRUE(script_request_incident.has_digest()); |
| 158 EXPECT_EQ(expected_digest, script_request_incident.digest()); | 160 EXPECT_EQ(expected_digest, script_request_incident.digest()); |
| 159 EXPECT_EQ(expected_type, script_request_incident.type()); | 161 EXPECT_EQ(expected_type, script_request_incident.type()); |
| 160 } | 162 } |
| 161 | 163 |
| 162 StrictMock<safe_browsing::MockIncidentReceiver>* mock_incident_receiver_; | 164 StrictMock<safe_browsing::MockIncidentReceiver>* mock_incident_receiver_; |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 const std::string url = "http://www.example.com/"; | 220 const std::string url = "http://www.example.com/"; |
| 219 const std::string digest = "dummydigest"; | 221 const std::string digest = "dummydigest"; |
| 220 for (auto resource_type : supported_types) { | 222 for (auto resource_type : supported_types) { |
| 221 ExpectAsyncDatabaseCheck(url, true, digest); | 223 ExpectAsyncDatabaseCheck(url, true, digest); |
| 222 ExpectIncidentAdded(url, resource_type, | 224 ExpectIncidentAdded(url, resource_type, |
| 223 ResourceRequestIncidentMessage::TYPE_PATTERN, digest); | 225 ResourceRequestIncidentMessage::TYPE_PATTERN, digest); |
| 224 } | 226 } |
| 225 } | 227 } |
| 226 | 228 |
| 227 } // namespace safe_browsing | 229 } // namespace safe_browsing |
| OLD | NEW |