| 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/binary_integrity_service.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/file_util.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/path_service.h" |
| 12 #include "base/test/scoped_path_override.h" |
| 13 #include "chrome/common/chrome_paths.h" |
| 14 #include "chrome/common/safe_browsing/csd.pb.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" |
| 17 |
| 18 namespace safe_browsing { |
| 19 |
| 20 namespace { |
| 21 |
| 22 |
| 23 const wchar_t kChromeDll[] = L"chrome.dll"; |
| 24 const wchar_t kChromeElfDll[] = L"chrome_elf.dll"; |
| 25 const wchar_t kChromeExe[] = L"chrome.exe"; |
| 26 const wchar_t kSignedBinaryDll[] = L"signed_binary.dll"; |
| 27 |
| 28 // Indicate if the AddIncidentCallback has been called. |
| 29 bool g_callback_called = false; |
| 30 |
| 31 // Helper function to erase the content of a binary to make sure the signature |
| 32 // verification will fail. |
| 33 bool EraseFileContent(const base::FilePath& file_path) { |
| 34 FILE* file = base::OpenFile(file_path, "w"); |
| 35 |
| 36 if (file == NULL) |
| 37 return false; |
| 38 |
| 39 bool success = base::TruncateFile(file); |
| 40 return base::CloseFile(file) && success; |
| 41 } |
| 42 |
| 43 // Mock the AddIncidentCallback so we can test that VerifyBinaryIntegrity |
| 44 // adds an incident callback when a signature verification fails. |
| 45 void MockAddIncidentCallback(scoped_ptr<ClientIncidentReport_IncidentData>) { |
| 46 g_callback_called = true; |
| 47 } |
| 48 |
| 49 } // namespace |
| 50 |
| 51 class BinaryIntegrityServiceWinTest : public ::testing::Test { |
| 52 protected: |
| 53 BinaryIntegrityServiceWinTest(); |
| 54 |
| 55 base::FilePath test_data_dir_; |
| 56 base::ScopedTempDir temp_dir_; |
| 57 scoped_ptr<base::ScopedPathOverride> exe_dir_override_; |
| 58 }; |
| 59 |
| 60 BinaryIntegrityServiceWinTest::BinaryIntegrityServiceWinTest() { |
| 61 temp_dir_.CreateUniqueTempDir(); |
| 62 base::CreateDirectory(temp_dir_.path().Append(GetChromeVersion())); |
| 63 |
| 64 // We retrieve DIR_TEST_DATA here because it is based on DIR_EXE and we are |
| 65 // about to override the path to the latter. |
| 66 if (!PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_)) |
| 67 NOTREACHED(); |
| 68 |
| 69 exe_dir_override_.reset( |
| 70 new base::ScopedPathOverride(base::DIR_EXE, temp_dir_.path())); |
| 71 } |
| 72 |
| 73 TEST_F(BinaryIntegrityServiceWinTest, GetCriticalBinariesPath) { |
| 74 // Expected paths. |
| 75 std::vector<base::FilePath> critical_binaries_path_expected; |
| 76 critical_binaries_path_expected.push_back( |
| 77 temp_dir_.path().Append(GetChromeVersion()).Append(kChromeDll)); |
| 78 critical_binaries_path_expected.push_back( |
| 79 temp_dir_.path().Append(GetChromeVersion()).Append(kChromeElfDll)); |
| 80 critical_binaries_path_expected.push_back( |
| 81 temp_dir_.path().Append(kChromeExe)); |
| 82 |
| 83 std::vector<base::FilePath> critical_binaries_path = |
| 84 GetCriticalBinariesPath(); |
| 85 |
| 86 ASSERT_THAT(critical_binaries_path, |
| 87 ::testing::ContainerEq(critical_binaries_path_expected)); |
| 88 } |
| 89 |
| 90 TEST_F(BinaryIntegrityServiceWinTest, VerifyBinaryIntegrity) { |
| 91 // Copy the signed dll to the temp exe directory. |
| 92 base::FilePath signed_binary_path(test_data_dir_); |
| 93 signed_binary_path = |
| 94 signed_binary_path.Append(L"safe_browsing").Append(kSignedBinaryDll); |
| 95 |
| 96 base::FilePath chrome_elf_path(temp_dir_.path()); |
| 97 chrome_elf_path = chrome_elf_path.Append( |
| 98 GetChromeVersion()).Append(kChromeElfDll); |
| 99 |
| 100 ASSERT_TRUE(base::CopyFile(signed_binary_path, chrome_elf_path)); |
| 101 |
| 102 AddIncidentCallback callback = base::Bind(&MockAddIncidentCallback); |
| 103 |
| 104 VerifyBinaryIntegrity(callback); |
| 105 ASSERT_FALSE(g_callback_called); |
| 106 |
| 107 ASSERT_TRUE(EraseFileContent(chrome_elf_path)); |
| 108 |
| 109 VerifyBinaryIntegrity(callback); |
| 110 ASSERT_TRUE(g_callback_called); |
| 111 } |
| 112 |
| 113 } // namespace safe_browsing |
| OLD | NEW |