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/pe/image_filter.h"
16 :
17 : #include "gtest/gtest.h"
18 : #include "syzygy/core/unittest_util.h"
19 : #include "syzygy/pe/unittest_util.h"
20 :
21 : namespace pe {
22 :
23 : namespace {
24 :
25 : class ImageFilterTest : public testing::PELibUnitTest {
26 : public:
27 E : ImageFilterTest() : dummy_path(L"C:\\this\\path\\does-not-exist.exe") {
28 E : }
29 :
30 E : virtual void SetUp() override {
31 E : test_dll_path = testing::GetExeRelativePath(testing::kTestDllName);
32 E : }
33 :
34 E : void InitPeFileAndSignature() {
35 E : EXPECT_TRUE(pe_file.Init(test_dll_path));
36 E : pe_file.GetSignature(&pe_signature);
37 E : }
38 :
39 : const base::FilePath dummy_path;
40 : base::FilePath test_dll_path;
41 :
42 : PEFile pe_file;
43 : PEFile::Signature pe_signature;
44 : };
45 :
46 : } // namespace
47 :
48 E : TEST_F(ImageFilterTest, Init) {
49 E : ImageFilter f1;
50 E : EXPECT_FALSE(f1.Init(dummy_path));
51 E : EXPECT_TRUE(f1.Init(test_dll_path));
52 :
53 E : InitPeFileAndSignature();
54 :
55 E : ImageFilter f2;
56 E : f2.Init(pe_file);
57 E : EXPECT_EQ(f1.signature, f2.signature);
58 E : EXPECT_EQ(f1.filter, f2.filter);
59 :
60 E : ImageFilter f3;
61 E : f3.Init(pe_signature);
62 E : EXPECT_EQ(f1.signature, f3.signature);
63 E : EXPECT_EQ(f1.filter, f3.filter);
64 E : }
65 :
66 E : TEST_F(ImageFilterTest, IsForModule) {
67 E : ImageFilter f1;
68 E : EXPECT_TRUE(f1.Init(test_dll_path));
69 :
70 E : ImageFilter f2;
71 E : f2.signature.base_address.set_value(0x01000000);
72 E : f2.signature.module_checksum = 0x12345678;
73 E : f2.signature.module_size = 8374832;
74 E : f2.signature.module_time_date_stamp = 0xBAADF00D;
75 E : f2.signature.path = dummy_path.value();
76 :
77 E : EXPECT_FALSE(f1.IsForModule(dummy_path));
78 E : EXPECT_TRUE(f1.IsForModule(test_dll_path));
79 E : EXPECT_FALSE(f2.IsForModule(test_dll_path));
80 :
81 E : InitPeFileAndSignature();
82 :
83 E : EXPECT_TRUE(f1.IsForModule(pe_file));
84 E : EXPECT_FALSE(f2.IsForModule(pe_file));
85 :
86 E : EXPECT_TRUE(f1.IsForModule(pe_signature));
87 E : EXPECT_FALSE(f2.IsForModule(pe_signature));
88 E : }
89 :
90 E : TEST_F(ImageFilterTest, SaveToAndLoadFromJSON) {
91 E : ImageFilter f1;
92 E : EXPECT_TRUE(f1.Init(test_dll_path));
93 :
94 : // Mark some ranges so that the filter isn't empty.
95 : f1.filter.Mark(ImageFilter::Range(
96 E : ImageFilter::RelativeAddress(0), 1024));
97 : f1.filter.Mark(ImageFilter::Range(
98 E : ImageFilter::RelativeAddress(4096), 4096));
99 : f1.filter.Mark(ImageFilter::Range(
100 E : ImageFilter::RelativeAddress(10240), 256));
101 :
102 E : base::FilePath temp_dir;
103 E : CreateTemporaryDir(&temp_dir);
104 E : base::FilePath pretty_json_path = temp_dir.Append(L"test_dll_pretty.json");
105 E : base::FilePath ugly_json_path = temp_dir.Append(L"test_dll_ugly.json");
106 :
107 E : EXPECT_TRUE(f1.SaveToJSON(true, pretty_json_path));
108 E : EXPECT_TRUE(f1.SaveToJSON(false, ugly_json_path));
109 :
110 E : std::string pretty_json, ugly_json;
111 E : base::ReadFileToString(pretty_json_path, &pretty_json);
112 E : base::ReadFileToString(ugly_json_path, &ugly_json);
113 E : EXPECT_LT(ugly_json.size(), pretty_json.size());
114 :
115 E : ImageFilter f2;
116 E : EXPECT_FALSE(f2.LoadFromJSON(dummy_path));
117 E : EXPECT_TRUE(f2.LoadFromJSON(pretty_json_path));
118 E : EXPECT_EQ(f1.signature, f2.signature);
119 E : EXPECT_EQ(f1.filter, f2.filter);
120 :
121 E : ImageFilter f3;
122 E : EXPECT_TRUE(f3.LoadFromJSON(ugly_json_path));
123 E : EXPECT_EQ(f1.signature, f3.signature);
124 E : EXPECT_EQ(f1.filter, f3.filter);
125 E : }
126 :
127 : } // namespace pe
|