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 ASSERT_TRUE(binary.has_image_headers()); | |
grt (UTC plus 2)
2015/03/20 20:51:22
oh yeah. this needs #if defined(OS_WIN) around it.
| |
101 ASSERT_TRUE(binary.image_headers().has_pe_headers()); | |
102 EXPECT_TRUE(binary.image_headers().pe_headers().has_dos_header()); | |
103 EXPECT_TRUE(binary.image_headers().pe_headers().has_file_header()); | |
104 EXPECT_TRUE(binary.image_headers().pe_headers().has_optional_headers32()); | |
105 EXPECT_FALSE(binary.image_headers().pe_headers().has_optional_headers64()); | |
106 } | |
107 | |
108 static const uint8_t kUnsignedDigest[]; | |
109 static const uint8_t kSignedDigest[]; | |
110 static const BinaryData kUnsignedExe; | |
111 static const BinaryData kSignedExe; | |
112 | |
113 base::FilePath dir_test_data_; | |
114 content::TestBrowserThreadBundle browser_thread_bundle_; | |
115 content::InProcessUtilityThreadHelper utility_thread_helper_; | |
116 }; | |
117 | |
118 // static | |
119 const uint8_t SandboxedZipAnalyzerTest::kUnsignedDigest[] = { | |
120 0x1e, 0x95, 0x4d, 0x9c, 0xe0, 0x38, 0x9e, 0x2b, 0xa7, 0x44, 0x72, 0x16, | |
121 0xf2, 0x17, 0x61, 0xf9, 0x8d, 0x1e, 0x65, 0x40, 0xc2, 0xab, 0xec, 0xdb, | |
122 0xec, 0xff, 0x57, 0x0e, 0x36, 0xc4, 0x93, 0xdb | |
123 }; | |
124 const uint8_t SandboxedZipAnalyzerTest::kSignedDigest[] = { | |
125 0xe1, 0x1f, 0xfa, 0x0c, 0x9f, 0x25, 0x23, 0x44, 0x53, 0xa9, 0xed, 0xd1, | |
126 0xcb, 0x25, 0x1d, 0x46, 0x10, 0x7f, 0x34, 0xb5, 0x36, 0xad, 0x74, 0x64, | |
127 0x2a, 0x85, 0x84, 0xac, 0xa8, 0xc1, 0xa8, 0xce | |
128 }; | |
129 const SandboxedZipAnalyzerTest::BinaryData | |
130 SandboxedZipAnalyzerTest::kUnsignedExe = { | |
131 "unsigned.exe", | |
132 ClientDownloadRequest_DownloadType_WIN_EXECUTABLE, | |
133 &kUnsignedDigest[0], | |
134 36864, | |
135 }; | |
136 const SandboxedZipAnalyzerTest::BinaryData | |
137 SandboxedZipAnalyzerTest::kSignedExe = { | |
138 "signed.exe", | |
139 ClientDownloadRequest_DownloadType_WIN_EXECUTABLE, | |
140 &kSignedDigest[0], | |
141 37768, | |
142 }; | |
143 | |
144 TEST_F(SandboxedZipAnalyzerTest, NoBinaries) { | |
145 zip_analyzer::Results results; | |
146 RunAnalyzer(dir_test_data_.AppendASCII("zipfile_no_binaries.zip"), &results); | |
147 ASSERT_TRUE(results.success); | |
148 EXPECT_FALSE(results.has_executable); | |
149 EXPECT_FALSE(results.has_archive); | |
150 EXPECT_EQ(0, results.archived_binary.size()); | |
151 } | |
152 | |
153 TEST_F(SandboxedZipAnalyzerTest, OneUnsignedBinary) { | |
154 zip_analyzer::Results results; | |
155 RunAnalyzer(dir_test_data_.AppendASCII("zipfile_one_unsigned_binary.zip"), | |
156 &results); | |
157 ASSERT_TRUE(results.success); | |
158 EXPECT_TRUE(results.has_executable); | |
159 EXPECT_FALSE(results.has_archive); | |
160 ASSERT_EQ(1, results.archived_binary.size()); | |
161 ExpectBinary(kUnsignedExe, results.archived_binary.Get(0)); | |
162 } | |
163 | |
164 TEST_F(SandboxedZipAnalyzerTest, TwoBinariesOneSigned) { | |
165 zip_analyzer::Results results; | |
166 RunAnalyzer(dir_test_data_.AppendASCII("zipfile_two_binaries_one_signed.zip"), | |
167 &results); | |
168 ASSERT_TRUE(results.success); | |
169 EXPECT_TRUE(results.has_executable); | |
170 EXPECT_FALSE(results.has_archive); | |
171 ASSERT_EQ(2, results.archived_binary.size()); | |
172 ExpectBinary(kUnsignedExe, results.archived_binary.Get(0)); | |
173 ExpectBinary(kSignedExe, results.archived_binary.Get(1)); | |
174 } | |
175 | |
176 } // namespace safe_browsing | |
OLD | NEW |