| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/module_load_analyzer.h
" | 5 #include "chrome/browser/safe_browsing/incident_reporting/module_load_analyzer.h
" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/memory/ptr_util.h" |
| 10 #include "base/memory/scoped_vector.h" | 11 #include "base/memory/scoped_vector.h" |
| 11 #include "base/path_service.h" | 12 #include "base/path_service.h" |
| 12 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 13 #include "base/scoped_native_library.h" | 14 #include "base/scoped_native_library.h" |
| 14 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 15 #include "chrome/browser/safe_browsing/incident_reporting/incident.h" | 16 #include "chrome/browser/safe_browsing/incident_reporting/incident.h" |
| 16 #include "chrome/browser/safe_browsing/incident_reporting/incident_receiver.h" | 17 #include "chrome/browser/safe_browsing/incident_reporting/incident_receiver.h" |
| 17 #include "chrome/browser/safe_browsing/incident_reporting/mock_incident_receiver
.h" | 18 #include "chrome/browser/safe_browsing/incident_reporting/mock_incident_receiver
.h" |
| 18 #include "chrome/common/safe_browsing/csd.pb.h" | 19 #include "chrome/common/safe_browsing/csd.pb.h" |
| 19 #include "components/safe_browsing_db/database_manager.h" | 20 #include "components/safe_browsing_db/database_manager.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 EXPECT_CALL(*mock_safe_browsing_database_manager_, | 59 EXPECT_CALL(*mock_safe_browsing_database_manager_, |
| 59 MatchModuleWhitelistString(_)) | 60 MatchModuleWhitelistString(_)) |
| 60 .WillRepeatedly(Return(true)); | 61 .WillRepeatedly(Return(true)); |
| 61 } | 62 } |
| 62 | 63 |
| 63 void ExpectIncident(const std::string& module_to_load) { | 64 void ExpectIncident(const std::string& module_to_load) { |
| 64 base::FilePath current_dir; | 65 base::FilePath current_dir; |
| 65 ASSERT_TRUE(PathService::Get(base::DIR_EXE, ¤t_dir)); | 66 ASSERT_TRUE(PathService::Get(base::DIR_EXE, ¤t_dir)); |
| 66 base::ScopedNativeLibrary dll1(current_dir.AppendASCII(module_to_load)); | 67 base::ScopedNativeLibrary dll1(current_dir.AppendASCII(module_to_load)); |
| 67 | 68 |
| 68 scoped_ptr<Incident> incident; | 69 std::unique_ptr<Incident> incident; |
| 69 EXPECT_CALL(*mock_incident_receiver_, DoAddIncidentForProcess(_)) | 70 EXPECT_CALL(*mock_incident_receiver_, DoAddIncidentForProcess(_)) |
| 70 .WillOnce(TakeIncident(&incident)); | 71 .WillOnce(TakeIncident(&incident)); |
| 71 | 72 |
| 72 VerifyModuleLoadState(mock_safe_browsing_database_manager_, | 73 VerifyModuleLoadState(mock_safe_browsing_database_manager_, |
| 73 make_scoped_ptr(mock_incident_receiver_)); | 74 base::WrapUnique(mock_incident_receiver_)); |
| 74 | 75 |
| 75 base::RunLoop().RunUntilIdle(); | 76 base::RunLoop().RunUntilIdle(); |
| 76 content::RunAllBlockingPoolTasksUntilIdle(); | 77 content::RunAllBlockingPoolTasksUntilIdle(); |
| 77 | 78 |
| 78 ASSERT_TRUE(incident); | 79 ASSERT_TRUE(incident); |
| 79 scoped_ptr<ClientIncidentReport_IncidentData> incident_data = | 80 std::unique_ptr<ClientIncidentReport_IncidentData> incident_data = |
| 80 incident->TakePayload(); | 81 incident->TakePayload(); |
| 81 ASSERT_TRUE(incident_data->has_suspicious_module()); | 82 ASSERT_TRUE(incident_data->has_suspicious_module()); |
| 82 const ClientIncidentReport_IncidentData_SuspiciousModuleIncident& | 83 const ClientIncidentReport_IncidentData_SuspiciousModuleIncident& |
| 83 suspicious_module_incident = incident_data->suspicious_module(); | 84 suspicious_module_incident = incident_data->suspicious_module(); |
| 84 EXPECT_TRUE(suspicious_module_incident.has_digest()); | 85 EXPECT_TRUE(suspicious_module_incident.has_digest()); |
| 85 EXPECT_TRUE(base::EndsWith(suspicious_module_incident.path(), | 86 EXPECT_TRUE(base::EndsWith(suspicious_module_incident.path(), |
| 86 module_to_load, base::CompareCase::SENSITIVE)); | 87 module_to_load, base::CompareCase::SENSITIVE)); |
| 87 } | 88 } |
| 88 | 89 |
| 89 void ExpectNoIncident(const std::string& module_to_load) { | 90 void ExpectNoIncident(const std::string& module_to_load) { |
| 90 base::FilePath current_dir; | 91 base::FilePath current_dir; |
| 91 ASSERT_TRUE(PathService::Get(base::DIR_EXE, ¤t_dir)); | 92 ASSERT_TRUE(PathService::Get(base::DIR_EXE, ¤t_dir)); |
| 92 base::ScopedNativeLibrary dll1(current_dir.AppendASCII(module_to_load)); | 93 base::ScopedNativeLibrary dll1(current_dir.AppendASCII(module_to_load)); |
| 93 | 94 |
| 94 EXPECT_CALL(*mock_incident_receiver_, DoAddIncidentForProcess(_)).Times(0); | 95 EXPECT_CALL(*mock_incident_receiver_, DoAddIncidentForProcess(_)).Times(0); |
| 95 | 96 |
| 96 VerifyModuleLoadState(mock_safe_browsing_database_manager_, | 97 VerifyModuleLoadState(mock_safe_browsing_database_manager_, |
| 97 make_scoped_ptr(mock_incident_receiver_)); | 98 base::WrapUnique(mock_incident_receiver_)); |
| 98 | 99 |
| 99 base::RunLoop().RunUntilIdle(); | 100 base::RunLoop().RunUntilIdle(); |
| 100 content::RunAllBlockingPoolTasksUntilIdle(); | 101 content::RunAllBlockingPoolTasksUntilIdle(); |
| 101 } | 102 } |
| 102 | 103 |
| 103 content::TestBrowserThreadBundle browser_thread_bundle_; | 104 content::TestBrowserThreadBundle browser_thread_bundle_; |
| 104 StrictMock<safe_browsing::MockIncidentReceiver>* mock_incident_receiver_; | 105 StrictMock<safe_browsing::MockIncidentReceiver>* mock_incident_receiver_; |
| 105 scoped_refptr<MockSafeBrowsingDatabaseManager> | 106 scoped_refptr<MockSafeBrowsingDatabaseManager> |
| 106 mock_safe_browsing_database_manager_; | 107 mock_safe_browsing_database_manager_; |
| 107 }; | 108 }; |
| 108 | 109 |
| 109 } // namespace | 110 } // namespace |
| 110 | 111 |
| 111 TEST_F(ModuleLoadAnalayzerTest, TestWhitelistedDLLs) { | 112 TEST_F(ModuleLoadAnalayzerTest, TestWhitelistedDLLs) { |
| 112 ExpectNoIncident(kWhitelistedModuleName); | 113 ExpectNoIncident(kWhitelistedModuleName); |
| 113 } | 114 } |
| 114 | 115 |
| 115 TEST_F(ModuleLoadAnalayzerTest, TestNonWhitelistedDLLs) { | 116 TEST_F(ModuleLoadAnalayzerTest, TestNonWhitelistedDLLs) { |
| 116 EXPECT_CALL(*mock_safe_browsing_database_manager_, | 117 EXPECT_CALL(*mock_safe_browsing_database_manager_, |
| 117 MatchModuleWhitelistString(kNonWhitelistedModuleName)) | 118 MatchModuleWhitelistString(kNonWhitelistedModuleName)) |
| 118 .WillOnce(Return(false)); | 119 .WillOnce(Return(false)); |
| 119 | 120 |
| 120 ExpectIncident(kNonWhitelistedModuleName); | 121 ExpectIncident(kNonWhitelistedModuleName); |
| 121 } | 122 } |
| 122 | 123 |
| 123 } // namespace safe_browsing | 124 } // namespace safe_browsing |
| OLD | NEW |