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/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 common::Application<TestGenFilterApp> TestApplication;
43 :
44 : GenFilterAppTest()
45 : : cmd_line_(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 FilePath& path) {
73 E : file_util::ScopedFILE file(file_util::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, 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 : 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 : FilePath temp_dir_;
126 :
127 : // @name File paths used for the standard IO streams.
128 : // @{
129 : FilePath stdin_path_;
130 : FilePath stdout_path_;
131 : FilePath stderr_path_;
132 : // @}
133 :
134 : // A handful of paths.
135 : FilePath test_dll_;
136 : FilePath test_dll_pdb_;
137 : FilePath output_file_;
138 :
139 : // Some generated filters.
140 : std::vector<pe::ImageFilter> filters_;
141 : std::vector<FilePath> filter_paths_;
142 : FilePath mismatched_filter_path_;
143 : };
144 :
145 : } // namespace
146 :
147 E : TEST_F(GenFilterAppTest, ParseCommandLineFailsWithNoAction) {
148 E : cmd_line_.AppendArgPath(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<FilePath> temp_files;
159 E : cmd_line_.AppendSwitchASCII("action", "union");
160 E : for (size_t i = 0; i < 10; ++i) {
161 E : FilePath temp_file;
162 E : ASSERT_TRUE(file_util::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<FilePath> temp_files;
173 E : cmd_line_.AppendSwitchASCII("action", "union");
174 E : for (size_t i = 0; i < 10; ++i) {
175 E : FilePath path = temp_dir_.Append(base::StringPrintf(L"filter-%d.json", i));
176 E : file_util::ScopedFILE file(file_util::OpenFile(path, "wb"));
177 E : temp_files.push_back(path);
178 E : }
179 E : cmd_line_.AppendArgPath(temp_dir_.Append(L"*.json"));
180 :
181 E : ASSERT_TRUE(impl_.ParseCommandLine(&cmd_line_));
182 E : ASSERT_EQ(temp_files, impl_.inputs_);
183 E : }
184 :
185 E : TEST_F(GenFilterAppTest, ParseCommandLineMinimal) {
186 E : FilePath foo_json;
187 E : ASSERT_NO_FATAL_FAILURE(MakeFile(L"foo.json", &foo_json));
188 :
189 E : cmd_line_.AppendArgPath(foo_json);
190 E : cmd_line_.AppendSwitchASCII("action", "invert");
191 E : ASSERT_TRUE(impl_.ParseCommandLine(&cmd_line_));
192 E : EXPECT_EQ(GenFilterApp::kInvert, impl_.action_);
193 E : EXPECT_TRUE(impl_.input_image_.empty());
194 E : EXPECT_TRUE(impl_.input_pdb_.empty());
195 E : EXPECT_TRUE(impl_.output_file_.empty());
196 E : EXPECT_EQ(1u, impl_.inputs_.size());
197 E : EXPECT_FALSE(impl_.overwrite_);
198 E : EXPECT_FALSE(impl_.pretty_print_);
199 E : }
200 :
201 E : TEST_F(GenFilterAppTest, ParseCommandLineFull) {
202 E : FilePath foo1_txt, foo2_txt;
203 E : ASSERT_NO_FATAL_FAILURE(MakeFile(L"foo1.txt", &foo1_txt));
204 E : ASSERT_NO_FATAL_FAILURE(MakeFile(L"foo2.txt", &foo2_txt));
205 :
206 E : cmd_line_.AppendArgPath(foo1_txt);
207 E : cmd_line_.AppendArgPath(foo2_txt);
208 E : cmd_line_.AppendSwitchASCII("action", "compile");
209 E : cmd_line_.AppendSwitchPath("input-image", test_dll_);
210 E : cmd_line_.AppendSwitchPath("input-pdb", test_dll_pdb_);
211 E : cmd_line_.AppendSwitchPath("output-file", output_file_);
212 E : cmd_line_.AppendSwitch("overwrite");
213 E : cmd_line_.AppendSwitch("pretty-print");
214 E : ASSERT_TRUE(impl_.ParseCommandLine(&cmd_line_));
215 E : EXPECT_EQ(GenFilterApp::kCompile, impl_.action_);
216 E : EXPECT_EQ(test_dll_, impl_.input_image_);
217 E : EXPECT_EQ(test_dll_pdb_, impl_.input_pdb_);
218 E : EXPECT_EQ(output_file_, impl_.output_file_);
219 E : EXPECT_EQ(2u, impl_.inputs_.size());
220 E : EXPECT_TRUE(impl_.overwrite_);
221 E : EXPECT_TRUE(impl_.pretty_print_);
222 E : }
223 :
224 E : TEST_F(GenFilterAppTest, ParseCommandLineInvertFailsWithMultipleInputs) {
225 E : FilePath foo1_json, foo2_json;
226 E : ASSERT_NO_FATAL_FAILURE(MakeFile(L"foo1.json", &foo1_json));
227 E : ASSERT_NO_FATAL_FAILURE(MakeFile(L"foo2.json", &foo2_json));
228 :
229 E : cmd_line_.AppendSwitchASCII("action", "invert");
230 E : cmd_line_.AppendArgPath(foo1_json);
231 E : cmd_line_.AppendArgPath(foo2_json);
232 E : ASSERT_FALSE(impl_.ParseCommandLine(&cmd_line_));
233 E : }
234 :
235 E : TEST_F(GenFilterAppTest, ParseCommandLineIntersectFailsWithSingleInput) {
236 E : FilePath foo_json;
237 E : ASSERT_NO_FATAL_FAILURE(MakeFile(L"foo.json", &foo_json));
238 :
239 E : cmd_line_.AppendSwitchASCII("action", "intersect");
240 E : cmd_line_.AppendArgPath(foo_json);
241 E : ASSERT_FALSE(impl_.ParseCommandLine(&cmd_line_));
242 E : }
243 :
244 E : TEST_F(GenFilterAppTest, ParseCommandLineUnionFailsWithSingleInput) {
245 E : FilePath foo_json;
246 E : ASSERT_NO_FATAL_FAILURE(MakeFile(L"foo.json", &foo_json));
247 :
248 E : cmd_line_.AppendSwitchASCII("action", "union");
249 E : cmd_line_.AppendArgPath(foo_json);
250 E : ASSERT_FALSE(impl_.ParseCommandLine(&cmd_line_));
251 E : }
252 :
253 E : TEST_F(GenFilterAppTest, ParseCommandLineSubtractFailsWithSingleInput) {
254 E : FilePath foo_json;
255 E : ASSERT_NO_FATAL_FAILURE(MakeFile(L"foo.json", &foo_json));
256 :
257 E : cmd_line_.AppendSwitchASCII("action", "subtract");
258 E : cmd_line_.AppendArgPath(foo_json);
259 E : ASSERT_FALSE(impl_.ParseCommandLine(&cmd_line_));
260 E : }
261 :
262 E : TEST_F(GenFilterAppTest, InvertDoesNotOverwriteExistingOutput) {
263 E : ASSERT_NO_FATAL_FAILURE(BuildFilters());
264 E : ASSERT_NO_FATAL_FAILURE(MakeFile(output_file_));
265 :
266 E : cmd_line_.AppendSwitchASCII("action", "invert");
267 E : cmd_line_.AppendArgPath(filter_paths_[0]);
268 E : cmd_line_.AppendSwitchPath("output-file", output_file_);
269 :
270 E : file_util::CopyFileW(filter_paths_[0], output_file_);
271 :
272 E : ASSERT_TRUE(impl_.ParseCommandLine(&cmd_line_));
273 E : ASSERT_EQ(1, impl_.Run());
274 E : }
275 :
276 E : TEST_F(GenFilterAppTest, InvertOverwriteExistingOutputWorks) {
277 E : ASSERT_NO_FATAL_FAILURE(BuildFilters());
278 E : ASSERT_NO_FATAL_FAILURE(MakeFile(output_file_));
279 :
280 E : cmd_line_.AppendSwitchASCII("action", "invert");
281 E : cmd_line_.AppendArgPath(filter_paths_[0]);
282 E : cmd_line_.AppendSwitchPath("output-file", output_file_);
283 E : cmd_line_.AppendSwitch("overwrite");
284 :
285 E : file_util::CopyFileW(filter_paths_[0], output_file_);
286 :
287 E : ASSERT_TRUE(impl_.ParseCommandLine(&cmd_line_));
288 E : ASSERT_EQ(0, impl_.Run());
289 E : }
290 :
291 E : TEST_F(GenFilterAppTest, InvertSucceeds) {
292 E : ASSERT_NO_FATAL_FAILURE(BuildFilters());
293 :
294 E : cmd_line_.AppendSwitchASCII("action", "invert");
295 E : cmd_line_.AppendArgPath(filter_paths_[0]);
296 E : cmd_line_.AppendSwitchPath("output-file", output_file_);
297 :
298 E : ASSERT_TRUE(impl_.ParseCommandLine(&cmd_line_));
299 E : ASSERT_EQ(0, impl_.Run());
300 :
301 E : ASSERT_TRUE(file_util::PathExists(output_file_));
302 E : pe::ImageFilter f;
303 E : ASSERT_TRUE(f.LoadFromJSON(output_file_));
304 :
305 E : filters_[0].filter.Invert(&filters_[0].filter);
306 E : EXPECT_EQ(filters_[0].filter, f.filter);
307 E : }
308 :
309 E : TEST_F(GenFilterAppTest, IntersectSucceeds) {
310 E : ASSERT_NO_FATAL_FAILURE(BuildFilters());
311 :
312 E : cmd_line_.AppendSwitchASCII("action", "intersect");
313 E : cmd_line_.AppendArgPath(filter_paths_[0]);
314 E : cmd_line_.AppendArgPath(filter_paths_[1]);
315 E : cmd_line_.AppendSwitchPath("output-file", output_file_);
316 :
317 E : ASSERT_TRUE(impl_.ParseCommandLine(&cmd_line_));
318 E : ASSERT_EQ(0, impl_.Run());
319 :
320 E : ASSERT_TRUE(file_util::PathExists(output_file_));
321 E : pe::ImageFilter f;
322 E : ASSERT_TRUE(f.LoadFromJSON(output_file_));
323 :
324 E : filters_[0].filter.Intersect(filters_[1].filter, &filters_[0].filter);
325 E : EXPECT_EQ(filters_[0].filter, f.filter);
326 E : }
327 :
328 E : TEST_F(GenFilterAppTest, SubtractSucceeds) {
329 E : ASSERT_NO_FATAL_FAILURE(BuildFilters());
330 :
331 E : cmd_line_.AppendSwitchASCII("action", "subtract");
332 E : cmd_line_.AppendArgPath(filter_paths_[0]);
333 E : cmd_line_.AppendArgPath(filter_paths_[1]);
334 E : cmd_line_.AppendSwitchPath("output-file", output_file_);
335 :
336 E : ASSERT_TRUE(impl_.ParseCommandLine(&cmd_line_));
337 E : ASSERT_EQ(0, impl_.Run());
338 :
339 E : ASSERT_TRUE(file_util::PathExists(output_file_));
340 E : pe::ImageFilter f;
341 E : ASSERT_TRUE(f.LoadFromJSON(output_file_));
342 :
343 E : filters_[0].filter.Subtract(filters_[1].filter, &filters_[0].filter);
344 E : EXPECT_EQ(filters_[0].filter, f.filter);
345 E : }
346 :
347 E : TEST_F(GenFilterAppTest, UnionSucceeds) {
348 E : ASSERT_NO_FATAL_FAILURE(BuildFilters());
349 :
350 E : cmd_line_.AppendSwitchASCII("action", "union");
351 E : cmd_line_.AppendArgPath(filter_paths_[0]);
352 E : cmd_line_.AppendArgPath(filter_paths_[1]);
353 E : cmd_line_.AppendSwitchPath("output-file", output_file_);
354 :
355 E : ASSERT_TRUE(impl_.ParseCommandLine(&cmd_line_));
356 E : ASSERT_EQ(0, impl_.Run());
357 :
358 E : ASSERT_TRUE(file_util::PathExists(output_file_));
359 E : pe::ImageFilter f;
360 E : ASSERT_TRUE(f.LoadFromJSON(output_file_));
361 :
362 E : filters_[0].filter.Union(filters_[1].filter, &filters_[0].filter);
363 E : EXPECT_EQ(filters_[0].filter, f.filter);
364 E : }
365 :
366 E : TEST_F(GenFilterAppTest, IntersectFailsMismatchedFilters) {
367 E : ASSERT_NO_FATAL_FAILURE(BuildFilters());
368 :
369 E : cmd_line_.AppendSwitchASCII("action", "intersect");
370 E : cmd_line_.AppendArgPath(filter_paths_[0]);
371 E : cmd_line_.AppendArgPath(mismatched_filter_path_);
372 E : cmd_line_.AppendSwitchPath("output-file", output_file_);
373 :
374 E : ASSERT_TRUE(impl_.ParseCommandLine(&cmd_line_));
375 E : ASSERT_NE(0, impl_.Run());
376 E : }
377 :
378 E : TEST_F(GenFilterAppTest, SubtractFailsMismatchedFilters) {
379 E : ASSERT_NO_FATAL_FAILURE(BuildFilters());
380 :
381 E : cmd_line_.AppendSwitchASCII("action", "subtract");
382 E : cmd_line_.AppendArgPath(filter_paths_[0]);
383 E : cmd_line_.AppendArgPath(mismatched_filter_path_);
384 E : cmd_line_.AppendSwitchPath("output-file", output_file_);
385 :
386 E : ASSERT_TRUE(impl_.ParseCommandLine(&cmd_line_));
387 E : ASSERT_NE(0, impl_.Run());
388 E : }
389 :
390 E : TEST_F(GenFilterAppTest, UnionFailsMismatchedFilters) {
391 E : ASSERT_NO_FATAL_FAILURE(BuildFilters());
392 :
393 E : cmd_line_.AppendSwitchASCII("action", "union");
394 E : cmd_line_.AppendArgPath(filter_paths_[0]);
395 E : cmd_line_.AppendArgPath(mismatched_filter_path_);
396 E : cmd_line_.AppendSwitchPath("output-file", output_file_);
397 :
398 E : ASSERT_TRUE(impl_.ParseCommandLine(&cmd_line_));
399 E : ASSERT_NE(0, impl_.Run());
400 E : }
401 :
402 E : TEST_F(GenFilterAppTest, CompileFailsInvalidInput) {
403 E : FilePath filter_txt = temp_dir_.Append(L"badfilter.txt");
404 : {
405 E : file_util::ScopedFILE file(file_util::OpenFile(filter_txt, "wb"));
406 E : ::fprintf(file.get(), "This is a badly formatted filter file.");
407 E : }
408 :
409 E : cmd_line_.AppendSwitchASCII("action", "compile");
410 E : cmd_line_.AppendArgPath(filter_txt);
411 E : cmd_line_.AppendSwitchPath("output-file", output_file_);
412 E : cmd_line_.AppendSwitchPath("input-image", test_dll_);
413 E : cmd_line_.AppendSwitchPath("input-pdb", test_dll_pdb_);
414 :
415 E : ASSERT_TRUE(impl_.ParseCommandLine(&cmd_line_));
416 E : ASSERT_NE(0, impl_.Run());
417 E : }
418 :
419 E : TEST_F(GenFilterAppTest, CompileSucceeds) {
420 E : FilePath filter_txt = temp_dir_.Append(L"goodfilter.txt");
421 : {
422 E : file_util::ScopedFILE file(file_util::OpenFile(filter_txt, "wb"));
423 E : ::fprintf(file.get(), "# A commend.\n");
424 E : ::fprintf(file.get(), "+function:DllMain\n");
425 E : }
426 :
427 E : cmd_line_.AppendSwitchASCII("action", "compile");
428 E : cmd_line_.AppendArgPath(filter_txt);
429 E : cmd_line_.AppendSwitchPath("output-file", output_file_);
430 E : cmd_line_.AppendSwitchPath("input-image", test_dll_);
431 E : cmd_line_.AppendSwitchPath("input-pdb", test_dll_pdb_);
432 :
433 E : ASSERT_TRUE(impl_.ParseCommandLine(&cmd_line_));
434 E : ASSERT_EQ(0, impl_.Run());
435 :
436 E : ASSERT_TRUE(file_util::PathExists(output_file_));
437 E : pe::ImageFilter f;
438 E : ASSERT_TRUE(f.LoadFromJSON(output_file_));
439 E : }
440 :
441 : } // namespace genfilter
|