1 : // Copyright 2013 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #include "syzygy/genfilter/genfilter_app.h"
16 :
17 : #include "base/strings/stringprintf.h"
18 : #include "gtest/gtest.h"
19 : #include "syzygy/core/unittest_util.h"
20 : #include "syzygy/pe/image_filter.h"
21 : #include "syzygy/pe/unittest_util.h"
22 :
23 : namespace genfilter {
24 :
25 : namespace {
26 :
27 : class TestGenFilterApp : public GenFilterApp {
28 : public:
29 : // Expose for testing.
30 : using GenFilterApp::action_;
31 : using GenFilterApp::input_image_;
32 : using GenFilterApp::input_pdb_;
33 : using GenFilterApp::output_file_;
34 : using GenFilterApp::inputs_;
35 : using GenFilterApp::pretty_print_;
36 : using GenFilterApp::overwrite_;
37 : };
38 :
39 : class GenFilterAppTest : public testing::PELibUnitTest {
40 : public:
41 : typedef testing::PELibUnitTest Super;
42 : typedef application::Application<TestGenFilterApp> TestApplication;
43 :
44 : GenFilterAppTest()
45 : : cmd_line_(base::FilePath(L"genfilter.exe")),
46 E : impl_(app_.implementation()) {
47 E : }
48 :
49 E : virtual void SetUp() override {
50 E : Super::SetUp();
51 :
52 : // Setup the IO streams.
53 E : ASSERT_NO_FATAL_FAILURE(CreateTemporaryDir(&temp_dir_));
54 E : stdin_path_ = temp_dir_.Append(L"NUL");
55 E : stdout_path_ = temp_dir_.Append(L"stdout.txt");
56 E : stderr_path_ = temp_dir_.Append(L"stderr.txt");
57 E : ASSERT_NO_FATAL_FAILURE(InitStreams(
58 : stdin_path_, stdout_path_, stderr_path_));
59 :
60 : // Point the application at the test's command-line and IO streams.
61 E : app_.set_command_line(&cmd_line_);
62 E : app_.set_in(in());
63 E : app_.set_out(out());
64 E : app_.set_err(err());
65 :
66 E : test_dll_ = testing::GetExeRelativePath(testing::kTestDllName);
67 E : test_dll_pdb_ = testing::GetExeRelativePath(testing::kTestDllPdbName);
68 E : output_file_ = temp_dir_.Append(L"output.json");
69 E : }
70 :
71 : // Creates an empty file at the given path.
72 E : void MakeFile(const base::FilePath& path) {
73 E : base::ScopedFILE file(base::OpenFile(path, "wb"));
74 E : ASSERT_TRUE(file.get() != NULL);
75 E : }
76 :
77 : // Generates a file with the given name in the temp directory, returning the
78 : // path to it.
79 E : void MakeFile(const wchar_t* filename, base::FilePath* path) {
80 E : DCHECK(filename != NULL);
81 E : DCHECK(path != NULL);
82 E : *path = temp_dir_.Append(filename);
83 E : ASSERT_NO_FATAL_FAILURE(MakeFile(*path));
84 : return;
85 E : }
86 :
87 : // Builds a series of 2 filters, to test out the various set operation
88 : // actions. Populates filters_ and filter_paths_.
89 E : void BuildFilters() {
90 E : filters_.resize(2);
91 E : filters_[0].Init(test_dll_);
92 E : filters_[1].Init(test_dll_);
93 :
94 : // Create two filters with overlapping ranges so that we can test all of
95 : // the set operations.
96 : filters_[0].filter.Mark(pe::ImageFilter::RelativeAddressFilter::Range(
97 E : core::RelativeAddress(0), 1024));
98 : filters_[1].filter.Mark(pe::ImageFilter::RelativeAddressFilter::Range(
99 E : core::RelativeAddress(512), 1024));
100 :
101 E : filter_paths_.resize(filters_.size());
102 E : for (size_t i = 0; i < filters_.size(); ++i) {
103 : filter_paths_[i] = temp_dir_.Append(
104 E : base::StringPrintf(L"filter-%d.json", i));
105 E : ASSERT_TRUE(filters_[i].SaveToJSON(true, filter_paths_[i]));
106 E : }
107 :
108 E : pe::ImageFilter f(filters_[0]);
109 E : f.signature.module_time_date_stamp ^= 0xBAADF00D;
110 E : mismatched_filter_path_ = temp_dir_.Append(L"mismatched-filter.json");
111 E : ASSERT_TRUE(f.SaveToJSON(false, mismatched_filter_path_));
112 E : }
113 :
114 : protected:
115 : // The command line to be given to the application under test.
116 : base::CommandLine cmd_line_;
117 :
118 : // The application object under test.
119 : TestApplication app_;
120 :
121 : // A reference to the underlying application implementation for convenience.
122 : TestGenFilterApp& impl_;
123 :
124 : // A temporary folder where all IO will be stored.
125 : base::FilePath temp_dir_;
126 :
127 : // @name File paths used for the standard IO streams.
128 : // @{
129 : base::FilePath stdin_path_;
130 : base::FilePath stdout_path_;
131 : base::FilePath stderr_path_;
132 : // @}
133 :
134 : // A handful of paths.
135 : base::FilePath test_dll_;
136 : base::FilePath test_dll_pdb_;
137 : base::FilePath output_file_;
138 :
139 : // Some generated filters.
140 : std::vector<pe::ImageFilter> filters_;
141 : std::vector<base::FilePath> filter_paths_;
142 : base::FilePath mismatched_filter_path_;
143 : };
144 :
145 : } // namespace
146 :
147 E : TEST_F(GenFilterAppTest, ParseCommandLineFailsWithNoAction) {
148 E : cmd_line_.AppendArgPath(base::FilePath(L"foo.json"));
149 E : ASSERT_FALSE(impl_.ParseCommandLine(&cmd_line_));
150 E : }
151 :
152 E : TEST_F(GenFilterAppTest, ParseCommandLineFailsWithNoInputFiles) {
153 E : cmd_line_.AppendSwitchASCII("action", "invert");
154 E : ASSERT_FALSE(impl_.ParseCommandLine(&cmd_line_));
155 E : }
156 :
157 E : TEST_F(GenFilterAppTest, ParseCommandLineExplicitInputFiles) {
158 E : std::vector<base::FilePath> temp_files;
159 E : cmd_line_.AppendSwitchASCII("action", "union");
160 E : for (size_t i = 0; i < 10; ++i) {
161 E : base::FilePath temp_file;
162 E : ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_, &temp_file));
163 E : cmd_line_.AppendArgPath(temp_file);
164 E : temp_files.push_back(temp_file);
165 E : }
166 :
167 E : ASSERT_TRUE(impl_.ParseCommandLine(&cmd_line_));
168 E : ASSERT_EQ(temp_files, impl_.inputs_);
169 E : }
170 :
171 E : TEST_F(GenFilterAppTest, ParseCommandLineInputFilesGlob) {
172 E : std::vector<base::FilePath> temp_files;
173 E : cmd_line_.AppendSwitchASCII("action", "union");
174 E : for (size_t i = 0; i < 10; ++i) {
175 : base::FilePath path =
176 E : temp_dir_.Append(base::StringPrintf(L"filter-%d.json", i));
177 E : base::ScopedFILE file(base::OpenFile(path, "wb"));
178 E : temp_files.push_back(path);
179 E : }
180 E : cmd_line_.AppendArgPath(temp_dir_.Append(L"*.json"));
181 :
182 E : ASSERT_TRUE(impl_.ParseCommandLine(&cmd_line_));
183 E : ASSERT_EQ(temp_files, impl_.inputs_);
184 E : }
185 :
186 E : TEST_F(GenFilterAppTest, ParseCommandLineMinimal) {
187 E : base::FilePath foo_json;
188 E : ASSERT_NO_FATAL_FAILURE(MakeFile(L"foo.json", &foo_json));
189 :
190 E : cmd_line_.AppendArgPath(foo_json);
191 E : cmd_line_.AppendSwitchASCII("action", "invert");
192 E : ASSERT_TRUE(impl_.ParseCommandLine(&cmd_line_));
193 E : EXPECT_EQ(GenFilterApp::kInvert, impl_.action_);
194 E : EXPECT_TRUE(impl_.input_image_.empty());
195 E : EXPECT_TRUE(impl_.input_pdb_.empty());
196 E : EXPECT_TRUE(impl_.output_file_.empty());
197 E : EXPECT_EQ(1u, impl_.inputs_.size());
198 E : EXPECT_FALSE(impl_.overwrite_);
199 E : EXPECT_FALSE(impl_.pretty_print_);
200 E : }
201 :
202 E : TEST_F(GenFilterAppTest, ParseCommandLineFull) {
203 E : base::FilePath foo1_txt, foo2_txt;
204 E : ASSERT_NO_FATAL_FAILURE(MakeFile(L"foo1.txt", &foo1_txt));
205 E : ASSERT_NO_FATAL_FAILURE(MakeFile(L"foo2.txt", &foo2_txt));
206 :
207 E : cmd_line_.AppendArgPath(foo1_txt);
208 E : cmd_line_.AppendArgPath(foo2_txt);
209 E : cmd_line_.AppendSwitchASCII("action", "compile");
210 E : cmd_line_.AppendSwitchPath("input-image", test_dll_);
211 E : cmd_line_.AppendSwitchPath("input-pdb", test_dll_pdb_);
212 E : cmd_line_.AppendSwitchPath("output-file", output_file_);
213 E : cmd_line_.AppendSwitch("overwrite");
214 E : cmd_line_.AppendSwitch("pretty-print");
215 E : ASSERT_TRUE(impl_.ParseCommandLine(&cmd_line_));
216 E : EXPECT_EQ(GenFilterApp::kCompile, impl_.action_);
217 E : EXPECT_EQ(test_dll_, impl_.input_image_);
218 E : EXPECT_EQ(test_dll_pdb_, impl_.input_pdb_);
219 E : EXPECT_EQ(output_file_, impl_.output_file_);
220 E : EXPECT_EQ(2u, impl_.inputs_.size());
221 E : EXPECT_TRUE(impl_.overwrite_);
222 E : EXPECT_TRUE(impl_.pretty_print_);
223 E : }
224 :
225 E : TEST_F(GenFilterAppTest, ParseCommandLineInvertFailsWithMultipleInputs) {
226 E : base::FilePath foo1_json, foo2_json;
227 E : ASSERT_NO_FATAL_FAILURE(MakeFile(L"foo1.json", &foo1_json));
228 E : ASSERT_NO_FATAL_FAILURE(MakeFile(L"foo2.json", &foo2_json));
229 :
230 E : cmd_line_.AppendSwitchASCII("action", "invert");
231 E : cmd_line_.AppendArgPath(foo1_json);
232 E : cmd_line_.AppendArgPath(foo2_json);
233 E : ASSERT_FALSE(impl_.ParseCommandLine(&cmd_line_));
234 E : }
235 :
236 E : TEST_F(GenFilterAppTest, ParseCommandLineIntersectFailsWithSingleInput) {
237 E : base::FilePath foo_json;
238 E : ASSERT_NO_FATAL_FAILURE(MakeFile(L"foo.json", &foo_json));
239 :
240 E : cmd_line_.AppendSwitchASCII("action", "intersect");
241 E : cmd_line_.AppendArgPath(foo_json);
242 E : ASSERT_FALSE(impl_.ParseCommandLine(&cmd_line_));
243 E : }
244 :
245 E : TEST_F(GenFilterAppTest, ParseCommandLineUnionFailsWithSingleInput) {
246 E : base::FilePath foo_json;
247 E : ASSERT_NO_FATAL_FAILURE(MakeFile(L"foo.json", &foo_json));
248 :
249 E : cmd_line_.AppendSwitchASCII("action", "union");
250 E : cmd_line_.AppendArgPath(foo_json);
251 E : ASSERT_FALSE(impl_.ParseCommandLine(&cmd_line_));
252 E : }
253 :
254 E : TEST_F(GenFilterAppTest, ParseCommandLineSubtractFailsWithSingleInput) {
255 E : base::FilePath foo_json;
256 E : ASSERT_NO_FATAL_FAILURE(MakeFile(L"foo.json", &foo_json));
257 :
258 E : cmd_line_.AppendSwitchASCII("action", "subtract");
259 E : cmd_line_.AppendArgPath(foo_json);
260 E : ASSERT_FALSE(impl_.ParseCommandLine(&cmd_line_));
261 E : }
262 :
263 E : TEST_F(GenFilterAppTest, InvertDoesNotOverwriteExistingOutput) {
264 E : ASSERT_NO_FATAL_FAILURE(BuildFilters());
265 E : ASSERT_NO_FATAL_FAILURE(MakeFile(output_file_));
266 :
267 E : cmd_line_.AppendSwitchASCII("action", "invert");
268 E : cmd_line_.AppendArgPath(filter_paths_[0]);
269 E : cmd_line_.AppendSwitchPath("output-file", output_file_);
270 :
271 E : base::CopyFile(filter_paths_[0], output_file_);
272 :
273 E : ASSERT_TRUE(impl_.ParseCommandLine(&cmd_line_));
274 E : ASSERT_EQ(1, impl_.Run());
275 E : }
276 :
277 E : TEST_F(GenFilterAppTest, InvertOverwriteExistingOutputWorks) {
278 E : ASSERT_NO_FATAL_FAILURE(BuildFilters());
279 E : ASSERT_NO_FATAL_FAILURE(MakeFile(output_file_));
280 :
281 E : cmd_line_.AppendSwitchASCII("action", "invert");
282 E : cmd_line_.AppendArgPath(filter_paths_[0]);
283 E : cmd_line_.AppendSwitchPath("output-file", output_file_);
284 E : cmd_line_.AppendSwitch("overwrite");
285 :
286 E : base::CopyFile(filter_paths_[0], output_file_);
287 :
288 E : ASSERT_TRUE(impl_.ParseCommandLine(&cmd_line_));
289 E : ASSERT_EQ(0, impl_.Run());
290 E : }
291 :
292 E : TEST_F(GenFilterAppTest, InvertSucceeds) {
293 E : ASSERT_NO_FATAL_FAILURE(BuildFilters());
294 :
295 E : cmd_line_.AppendSwitchASCII("action", "invert");
296 E : cmd_line_.AppendArgPath(filter_paths_[0]);
297 E : cmd_line_.AppendSwitchPath("output-file", output_file_);
298 :
299 E : ASSERT_TRUE(impl_.ParseCommandLine(&cmd_line_));
300 E : ASSERT_EQ(0, impl_.Run());
301 :
302 E : ASSERT_TRUE(base::PathExists(output_file_));
303 E : pe::ImageFilter f;
304 E : ASSERT_TRUE(f.LoadFromJSON(output_file_));
305 :
306 E : filters_[0].filter.Invert(&filters_[0].filter);
307 E : EXPECT_EQ(filters_[0].filter, f.filter);
308 E : }
309 :
310 E : TEST_F(GenFilterAppTest, IntersectSucceeds) {
311 E : ASSERT_NO_FATAL_FAILURE(BuildFilters());
312 :
313 E : cmd_line_.AppendSwitchASCII("action", "intersect");
314 E : cmd_line_.AppendArgPath(filter_paths_[0]);
315 E : cmd_line_.AppendArgPath(filter_paths_[1]);
316 E : cmd_line_.AppendSwitchPath("output-file", output_file_);
317 :
318 E : ASSERT_TRUE(impl_.ParseCommandLine(&cmd_line_));
319 E : ASSERT_EQ(0, impl_.Run());
320 :
321 E : ASSERT_TRUE(base::PathExists(output_file_));
322 E : pe::ImageFilter f;
323 E : ASSERT_TRUE(f.LoadFromJSON(output_file_));
324 :
325 E : filters_[0].filter.Intersect(filters_[1].filter, &filters_[0].filter);
326 E : EXPECT_EQ(filters_[0].filter, f.filter);
327 E : }
328 :
329 E : TEST_F(GenFilterAppTest, SubtractSucceeds) {
330 E : ASSERT_NO_FATAL_FAILURE(BuildFilters());
331 :
332 E : cmd_line_.AppendSwitchASCII("action", "subtract");
333 E : cmd_line_.AppendArgPath(filter_paths_[0]);
334 E : cmd_line_.AppendArgPath(filter_paths_[1]);
335 E : cmd_line_.AppendSwitchPath("output-file", output_file_);
336 :
337 E : ASSERT_TRUE(impl_.ParseCommandLine(&cmd_line_));
338 E : ASSERT_EQ(0, impl_.Run());
339 :
340 E : ASSERT_TRUE(base::PathExists(output_file_));
341 E : pe::ImageFilter f;
342 E : ASSERT_TRUE(f.LoadFromJSON(output_file_));
343 :
344 E : filters_[0].filter.Subtract(filters_[1].filter, &filters_[0].filter);
345 E : EXPECT_EQ(filters_[0].filter, f.filter);
346 E : }
347 :
348 E : TEST_F(GenFilterAppTest, UnionSucceeds) {
349 E : ASSERT_NO_FATAL_FAILURE(BuildFilters());
350 :
351 E : cmd_line_.AppendSwitchASCII("action", "union");
352 E : cmd_line_.AppendArgPath(filter_paths_[0]);
353 E : cmd_line_.AppendArgPath(filter_paths_[1]);
354 E : cmd_line_.AppendSwitchPath("output-file", output_file_);
355 :
356 E : ASSERT_TRUE(impl_.ParseCommandLine(&cmd_line_));
357 E : ASSERT_EQ(0, impl_.Run());
358 :
359 E : ASSERT_TRUE(base::PathExists(output_file_));
360 E : pe::ImageFilter f;
361 E : ASSERT_TRUE(f.LoadFromJSON(output_file_));
362 :
363 E : filters_[0].filter.Union(filters_[1].filter, &filters_[0].filter);
364 E : EXPECT_EQ(filters_[0].filter, f.filter);
365 E : }
366 :
367 E : TEST_F(GenFilterAppTest, IntersectFailsMismatchedFilters) {
368 E : ASSERT_NO_FATAL_FAILURE(BuildFilters());
369 :
370 E : cmd_line_.AppendSwitchASCII("action", "intersect");
371 E : cmd_line_.AppendArgPath(filter_paths_[0]);
372 E : cmd_line_.AppendArgPath(mismatched_filter_path_);
373 E : cmd_line_.AppendSwitchPath("output-file", output_file_);
374 :
375 E : ASSERT_TRUE(impl_.ParseCommandLine(&cmd_line_));
376 E : ASSERT_NE(0, impl_.Run());
377 E : }
378 :
379 E : TEST_F(GenFilterAppTest, SubtractFailsMismatchedFilters) {
380 E : ASSERT_NO_FATAL_FAILURE(BuildFilters());
381 :
382 E : cmd_line_.AppendSwitchASCII("action", "subtract");
383 E : cmd_line_.AppendArgPath(filter_paths_[0]);
384 E : cmd_line_.AppendArgPath(mismatched_filter_path_);
385 E : cmd_line_.AppendSwitchPath("output-file", output_file_);
386 :
387 E : ASSERT_TRUE(impl_.ParseCommandLine(&cmd_line_));
388 E : ASSERT_NE(0, impl_.Run());
389 E : }
390 :
391 E : TEST_F(GenFilterAppTest, UnionFailsMismatchedFilters) {
392 E : ASSERT_NO_FATAL_FAILURE(BuildFilters());
393 :
394 E : cmd_line_.AppendSwitchASCII("action", "union");
395 E : cmd_line_.AppendArgPath(filter_paths_[0]);
396 E : cmd_line_.AppendArgPath(mismatched_filter_path_);
397 E : cmd_line_.AppendSwitchPath("output-file", output_file_);
398 :
399 E : ASSERT_TRUE(impl_.ParseCommandLine(&cmd_line_));
400 E : ASSERT_NE(0, impl_.Run());
401 E : }
402 :
403 E : TEST_F(GenFilterAppTest, CompileFailsInvalidInput) {
404 E : base::FilePath filter_txt = temp_dir_.Append(L"badfilter.txt");
405 : {
406 E : base::ScopedFILE file(base::OpenFile(filter_txt, "wb"));
407 E : ::fprintf(file.get(), "This is a badly formatted filter file.");
408 E : }
409 :
410 E : cmd_line_.AppendSwitchASCII("action", "compile");
411 E : cmd_line_.AppendArgPath(filter_txt);
412 E : cmd_line_.AppendSwitchPath("output-file", output_file_);
413 E : cmd_line_.AppendSwitchPath("input-image", test_dll_);
414 E : cmd_line_.AppendSwitchPath("input-pdb", test_dll_pdb_);
415 :
416 E : ASSERT_TRUE(impl_.ParseCommandLine(&cmd_line_));
417 E : ASSERT_NE(0, impl_.Run());
418 E : }
419 :
420 E : TEST_F(GenFilterAppTest, CompileSucceeds) {
421 E : base::FilePath filter_txt = temp_dir_.Append(L"goodfilter.txt");
422 : {
423 E : base::ScopedFILE file(base::OpenFile(filter_txt, "wb"));
424 E : ::fprintf(file.get(), "# A commend.\n");
425 E : ::fprintf(file.get(), "+function:DllMain\n");
426 E : }
427 :
428 E : cmd_line_.AppendSwitchASCII("action", "compile");
429 E : cmd_line_.AppendArgPath(filter_txt);
430 E : cmd_line_.AppendSwitchPath("output-file", output_file_);
431 E : cmd_line_.AppendSwitchPath("input-image", test_dll_);
432 E : cmd_line_.AppendSwitchPath("input-pdb", test_dll_pdb_);
433 :
434 E : ASSERT_TRUE(impl_.ParseCommandLine(&cmd_line_));
435 E : ASSERT_EQ(0, impl_.Run());
436 :
437 E : ASSERT_TRUE(base::PathExists(output_file_));
438 E : pe::ImageFilter f;
439 E : ASSERT_TRUE(f.LoadFromJSON(output_file_));
440 E : }
441 :
442 : } // namespace genfilter
|