OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/safe_browsing/sandboxed_zip_analyzer.h" |
| 6 |
| 7 #include <stdint.h> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" |
| 11 #include "base/files/file_path.h" |
| 12 #include "base/path_service.h" |
| 13 #include "base/run_loop.h" |
| 14 #include "chrome/common/chrome_paths.h" |
| 15 #include "chrome/common/safe_browsing/zip_analyzer_results.h" |
| 16 #include "content/public/test/test_browser_thread_bundle.h" |
| 17 #include "content/public/test/test_utils.h" |
| 18 #include "crypto/sha2.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 |
| 21 namespace safe_browsing { |
| 22 |
| 23 class SandboxedZipAnalyzerTest : public ::testing::Test { |
| 24 protected: |
| 25 // Constants for validating the data reported by the analyzer. |
| 26 struct BinaryData { |
| 27 const char* file_basename; |
| 28 ClientDownloadRequest_DownloadType download_type; |
| 29 const uint8_t* sha256_digest; |
| 30 int64_t length; |
| 31 }; |
| 32 |
| 33 // A helper that provides a SandboxedZipAnalyzer::ResultCallback that will |
| 34 // store a copy of an analyzer's results and then run a closure. |
| 35 class ResultsGetter { |
| 36 public: |
| 37 ResultsGetter(const base::Closure& quit_closure, |
| 38 zip_analyzer::Results* results) |
| 39 : quit_closure_(quit_closure), |
| 40 results_(results) { |
| 41 DCHECK(results); |
| 42 results->success = false; |
| 43 } |
| 44 |
| 45 SandboxedZipAnalyzer::ResultCallback GetCallback() { |
| 46 return base::Bind(&ResultsGetter::OnZipAnalyzerResults, |
| 47 base::Unretained(this)); |
| 48 } |
| 49 |
| 50 private: |
| 51 void OnZipAnalyzerResults(const zip_analyzer::Results& results) { |
| 52 *results_ = results; |
| 53 quit_closure_.Run(); |
| 54 } |
| 55 |
| 56 base::Closure quit_closure_; |
| 57 zip_analyzer::Results* results_; |
| 58 DISALLOW_COPY_AND_ASSIGN(ResultsGetter); |
| 59 }; |
| 60 |
| 61 SandboxedZipAnalyzerTest() |
| 62 : browser_thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {} |
| 63 |
| 64 void SetUp() override { |
| 65 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &dir_test_data_)); |
| 66 dir_test_data_ = dir_test_data_.AppendASCII("safe_browsing"); |
| 67 dir_test_data_ = dir_test_data_.AppendASCII("download_protection"); |
| 68 } |
| 69 |
| 70 // Runs a sandboxed zip analyzer on |file_path|, writing its results into |
| 71 // |results|. |
| 72 void RunAnalyzer(const base::FilePath& file_path, |
| 73 zip_analyzer::Results* results) { |
| 74 DCHECK(results); |
| 75 base::RunLoop run_loop; |
| 76 ResultsGetter results_getter(run_loop.QuitClosure(), results); |
| 77 scoped_refptr<SandboxedZipAnalyzer> analyzer( |
| 78 new SandboxedZipAnalyzer(file_path, results_getter.GetCallback())); |
| 79 analyzer->Start(); |
| 80 run_loop.Run(); |
| 81 } |
| 82 |
| 83 // Verifies expectations about a binary found by the analyzer. |
| 84 void ExpectBinary(const BinaryData& data, |
| 85 const ClientDownloadRequest_ArchivedBinary& binary) { |
| 86 ASSERT_TRUE(binary.has_file_basename()); |
| 87 EXPECT_EQ(data.file_basename, binary.file_basename()); |
| 88 ASSERT_TRUE(binary.has_download_type()); |
| 89 EXPECT_EQ(data.download_type, binary.download_type()); |
| 90 ASSERT_TRUE(binary.has_digests()); |
| 91 ASSERT_TRUE(binary.digests().has_sha256()); |
| 92 EXPECT_EQ(std::string(data.sha256_digest, |
| 93 data.sha256_digest + crypto::kSHA256Length), |
| 94 binary.digests().sha256()); |
| 95 EXPECT_FALSE(binary.digests().has_sha1()); |
| 96 EXPECT_FALSE(binary.digests().has_md5()); |
| 97 ASSERT_TRUE(binary.has_length()); |
| 98 EXPECT_EQ(data.length, binary.length()); |
| 99 EXPECT_FALSE(binary.has_signature()); |
| 100 #if defined(OS_WIN) // ExtractImageHeaders is only implemented for Win. |
| 101 ASSERT_TRUE(binary.has_image_headers()); |
| 102 ASSERT_TRUE(binary.image_headers().has_pe_headers()); |
| 103 EXPECT_TRUE(binary.image_headers().pe_headers().has_dos_header()); |
| 104 EXPECT_TRUE(binary.image_headers().pe_headers().has_file_header()); |
| 105 EXPECT_TRUE(binary.image_headers().pe_headers().has_optional_headers32()); |
| 106 EXPECT_FALSE(binary.image_headers().pe_headers().has_optional_headers64()); |
| 107 #else // OS_WIN |
| 108 ASSERT_FALSE(binary.has_image_headers()); |
| 109 #endif // !OS_WIN |
| 110 } |
| 111 |
| 112 static const uint8_t kUnsignedDigest[]; |
| 113 static const uint8_t kSignedDigest[]; |
| 114 static const BinaryData kUnsignedExe; |
| 115 static const BinaryData kSignedExe; |
| 116 |
| 117 base::FilePath dir_test_data_; |
| 118 content::TestBrowserThreadBundle browser_thread_bundle_; |
| 119 content::InProcessUtilityThreadHelper utility_thread_helper_; |
| 120 }; |
| 121 |
| 122 // static |
| 123 const uint8_t SandboxedZipAnalyzerTest::kUnsignedDigest[] = { |
| 124 0x1e, 0x95, 0x4d, 0x9c, 0xe0, 0x38, 0x9e, 0x2b, 0xa7, 0x44, 0x72, 0x16, |
| 125 0xf2, 0x17, 0x61, 0xf9, 0x8d, 0x1e, 0x65, 0x40, 0xc2, 0xab, 0xec, 0xdb, |
| 126 0xec, 0xff, 0x57, 0x0e, 0x36, 0xc4, 0x93, 0xdb |
| 127 }; |
| 128 const uint8_t SandboxedZipAnalyzerTest::kSignedDigest[] = { |
| 129 0xe1, 0x1f, 0xfa, 0x0c, 0x9f, 0x25, 0x23, 0x44, 0x53, 0xa9, 0xed, 0xd1, |
| 130 0xcb, 0x25, 0x1d, 0x46, 0x10, 0x7f, 0x34, 0xb5, 0x36, 0xad, 0x74, 0x64, |
| 131 0x2a, 0x85, 0x84, 0xac, 0xa8, 0xc1, 0xa8, 0xce |
| 132 }; |
| 133 const SandboxedZipAnalyzerTest::BinaryData |
| 134 SandboxedZipAnalyzerTest::kUnsignedExe = { |
| 135 "unsigned.exe", |
| 136 ClientDownloadRequest_DownloadType_WIN_EXECUTABLE, |
| 137 &kUnsignedDigest[0], |
| 138 36864, |
| 139 }; |
| 140 const SandboxedZipAnalyzerTest::BinaryData |
| 141 SandboxedZipAnalyzerTest::kSignedExe = { |
| 142 "signed.exe", |
| 143 ClientDownloadRequest_DownloadType_WIN_EXECUTABLE, |
| 144 &kSignedDigest[0], |
| 145 37768, |
| 146 }; |
| 147 |
| 148 TEST_F(SandboxedZipAnalyzerTest, NoBinaries) { |
| 149 zip_analyzer::Results results; |
| 150 RunAnalyzer(dir_test_data_.AppendASCII("zipfile_no_binaries.zip"), &results); |
| 151 ASSERT_TRUE(results.success); |
| 152 EXPECT_FALSE(results.has_executable); |
| 153 EXPECT_FALSE(results.has_archive); |
| 154 EXPECT_EQ(0, results.archived_binary.size()); |
| 155 } |
| 156 |
| 157 TEST_F(SandboxedZipAnalyzerTest, OneUnsignedBinary) { |
| 158 zip_analyzer::Results results; |
| 159 RunAnalyzer(dir_test_data_.AppendASCII("zipfile_one_unsigned_binary.zip"), |
| 160 &results); |
| 161 ASSERT_TRUE(results.success); |
| 162 EXPECT_TRUE(results.has_executable); |
| 163 EXPECT_FALSE(results.has_archive); |
| 164 ASSERT_EQ(1, results.archived_binary.size()); |
| 165 ExpectBinary(kUnsignedExe, results.archived_binary.Get(0)); |
| 166 } |
| 167 |
| 168 TEST_F(SandboxedZipAnalyzerTest, TwoBinariesOneSigned) { |
| 169 zip_analyzer::Results results; |
| 170 RunAnalyzer(dir_test_data_.AppendASCII("zipfile_two_binaries_one_signed.zip"), |
| 171 &results); |
| 172 ASSERT_TRUE(results.success); |
| 173 EXPECT_TRUE(results.has_executable); |
| 174 EXPECT_FALSE(results.has_archive); |
| 175 ASSERT_EQ(2, results.archived_binary.size()); |
| 176 ExpectBinary(kUnsignedExe, results.archived_binary.Get(0)); |
| 177 ExpectBinary(kSignedExe, results.archived_binary.Get(1)); |
| 178 } |
| 179 |
| 180 } // namespace safe_browsing |
OLD | NEW |