OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/safe_browsing/environment_data_collection_win.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/files/file_path.h" |
| 10 #include "base/scoped_native_library.h" |
| 11 #include "chrome/common/safe_browsing/csd.pb.h" |
| 12 #include "net/base/winsock_init.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace { |
| 16 |
| 17 // Helper function that returns true if a dll with filename |dll_name| is |
| 18 // found in |process_report|. |
| 19 bool ProcessReportContainsDll( |
| 20 const safe_browsing::ClientIncidentReport_EnvironmentData_Process& |
| 21 process_report, |
| 22 const base::FilePath& dll_name) { |
| 23 for (int i = 0; i < process_report.dll_size(); ++i) { |
| 24 base::FilePath current_dll = |
| 25 base::FilePath::FromUTF8Unsafe(process_report.dll(i).path()); |
| 26 |
| 27 if (current_dll.BaseName() == dll_name) |
| 28 return true; |
| 29 } |
| 30 |
| 31 return false; |
| 32 } |
| 33 |
| 34 } // namespace |
| 35 |
| 36 TEST(SafeBrowsingEnvironmentDataCollectionWinTest, CollectDlls) { |
| 37 // This test will check if the CollectDlls method works by loading |
| 38 // a dll and then checking if we can find it within the process report. |
| 39 // Pick msvidc32.dll as it is present from WinXP to Win8 and yet rarely used. |
| 40 // msvidc32.dll exists in both 32 and 64 bit versions. |
| 41 base::FilePath msvdc32_dll(L"msvidc32.dll"); |
| 42 |
| 43 safe_browsing::ClientIncidentReport_EnvironmentData_Process process_report; |
| 44 safe_browsing::CollectDlls(&process_report); |
| 45 |
| 46 ASSERT_FALSE(ProcessReportContainsDll(process_report, msvdc32_dll)); |
| 47 |
| 48 // Redo the same verification after loading a new dll. |
| 49 base::ScopedNativeLibrary library(msvdc32_dll); |
| 50 |
| 51 process_report.clear_dll(); |
| 52 safe_browsing::CollectDlls(&process_report); |
| 53 |
| 54 ASSERT_TRUE(ProcessReportContainsDll(process_report, msvdc32_dll)); |
| 55 } |
| 56 |
| 57 TEST(SafeBrowsingEnvironmentDataCollectionWinTest, RecordLspFeature) { |
| 58 net::EnsureWinsockInit(); |
| 59 |
| 60 // Populate our incident report with loaded modules. |
| 61 safe_browsing::ClientIncidentReport_EnvironmentData_Process process_report; |
| 62 safe_browsing::CollectDlls(&process_report); |
| 63 |
| 64 // We'll test RecordLspFeatures against a real dll registered as a LSP. All |
| 65 // dll paths are expected to be lowercase in the process report. |
| 66 std::string lsp_path = "c:\\windows\\system32\\mswsock.dll"; |
| 67 int base_address = 0x77770000; |
| 68 int length = 0x180000; |
| 69 |
| 70 // Look for dlls that are registered as a LSP. |
| 71 safe_browsing::RecordLspFeature(&process_report); |
| 72 |
| 73 // Look through dll entries and check that none contains the LSP feature. |
| 74 bool lsp_feature_found = false; |
| 75 for (int i = 0; i < process_report.dll_size(); ++i) { |
| 76 if (process_report.dll(i).path() == lsp_path) { |
| 77 // Look for ClientIncidentReport_EnvironmentData_Process_DLL_Feature_LSP |
| 78 // through the features of each dll. |
| 79 for (int j = 0; j < process_report.dll(i).feature_size(); ++j) { |
| 80 if (process_report.dll(i).feature(j) == |
| 81 safe_browsing::ClientIncidentReport_EnvironmentData_Process_Dll:: |
| 82 LSP) |
| 83 lsp_feature_found = true; |
| 84 } |
| 85 } |
| 86 } |
| 87 |
| 88 ASSERT_FALSE(lsp_feature_found); |
| 89 |
| 90 // Manually add an entry to the process report that will get marked as a LSP. |
| 91 safe_browsing::ClientIncidentReport_EnvironmentData_Process_Dll* dll = |
| 92 process_report.add_dll(); |
| 93 dll->set_path(lsp_path); |
| 94 dll->set_base_address(base_address); |
| 95 dll->set_length(length); |
| 96 |
| 97 // Look for dlls that are registered as a LSP. |
| 98 safe_browsing::RecordLspFeature(&process_report); |
| 99 |
| 100 // Look through dll entries and check if the one we added contains the LSP |
| 101 // feature. |
| 102 lsp_feature_found = false; |
| 103 for (int i = 0; i < process_report.dll_size(); ++i) { |
| 104 if (process_report.dll(i).path() == lsp_path) { |
| 105 // Look for ClientIncidentReport_EnvironmentData_Process_DLL_Feature_LSP |
| 106 // through the features of each dll. |
| 107 for (int j = 0; j < process_report.dll(i).feature_size(); ++j) { |
| 108 if (process_report.dll(i).feature(j) == |
| 109 safe_browsing::ClientIncidentReport_EnvironmentData_Process_Dll:: |
| 110 LSP) |
| 111 lsp_feature_found = true; |
| 112 } |
| 113 } |
| 114 } |
| 115 |
| 116 ASSERT_TRUE(lsp_feature_found); |
| 117 } |
OLD | NEW |