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/pehacker/operations/add_imports_operation.h"
16 :
17 : #include "gmock/gmock.h"
18 : #include "gtest/gtest.h"
19 : #include "syzygy/pe/pe_transform_policy.h"
20 : #include "syzygy/pehacker/unittest_util.h"
21 :
22 : namespace pehacker {
23 : namespace operations {
24 :
25 : namespace {
26 :
27 : using block_graph::BlockGraph;
28 : using testing::Return;
29 :
30 : const char kSimpleConfig[] =
31 : "{\n"
32 : " \"type\": \"add_imports\",\n"
33 : " \"modules\": [\n"
34 : " {\n"
35 : " \"module_name\": \"foo.dll\","
36 : " \"imports\": [\n"
37 : " { \"function_name\": \"bar\" },\n"
38 : " ]\n"
39 : " },\n"
40 : " ],\n"
41 : "}";
42 :
43 : class TestAddImportsOperation : public AddImportsOperation {
44 : public:
45 E : TestAddImportsOperation() { }
46 E : virtual ~TestAddImportsOperation() { }
47 :
48 : MOCK_METHOD4(ApplyTransform, bool(block_graph::BlockGraphTransformInterface*,
49 : const TransformPolicyInterface*,
50 : BlockGraph*,
51 E : BlockGraph::Block*));
52 :
53 : typedef AddImportsOperation::ImportedModuleMap ImportedModuleMap;
54 :
55 : using AddImportsOperation::add_imports_tx_;
56 : using AddImportsOperation::imported_modules_;
57 : using AddImportsOperation::imported_module_map_;
58 : };
59 :
60 : typedef testing::OperationTest AddImportsOperationTest;
61 :
62 : } // namespace
63 :
64 E : TEST_F(AddImportsOperationTest, Name) {
65 E : TestAddImportsOperation op;
66 E : EXPECT_STREQ(TestAddImportsOperation::kName, op.name());
67 E : }
68 :
69 E : TEST_F(AddImportsOperationTest, Init) {
70 E : TestAddImportsOperation op;
71 E : ASSERT_NO_FATAL_FAILURE(InitConfig(kSimpleConfig));
72 E : pe::PETransformPolicy policy;
73 E : EXPECT_TRUE(op.Init(&policy, config_.get()));
74 :
75 E : EXPECT_EQ(1u, op.imported_modules_.size());
76 E : EXPECT_EQ(1u, op.imported_module_map_.size());
77 :
78 : // Ensure the transform is appropriately configured.
79 : TestAddImportsOperation::ImportedModuleMap::iterator mod_it =
80 E : op.imported_module_map_.begin();
81 E : EXPECT_EQ("foo.dll", mod_it->first);
82 E : EXPECT_EQ(op.imported_modules_[0], mod_it->second);
83 E : EXPECT_EQ("foo.dll", mod_it->second->name());
84 E : EXPECT_EQ(1u, mod_it->second->size());
85 E : EXPECT_EQ("bar", mod_it->second->GetSymbolName(0));
86 : EXPECT_EQ(pe::transforms::ImportedModule::kAlwaysImport,
87 E : mod_it->second->GetSymbolMode(0));
88 E : }
89 :
90 E : TEST_F(AddImportsOperationTest, RunFails) {
91 E : TestAddImportsOperation op;
92 E : ASSERT_NO_FATAL_FAILURE(InitConfig(kSimpleConfig));
93 E : pe::PETransformPolicy policy;
94 E : ASSERT_TRUE(op.Init(&policy, config_.get()));
95 :
96 E : BlockGraph bg;
97 E : BlockGraph::Block* header = bg.AddBlock(BlockGraph::DATA_BLOCK, 1, "header");
98 :
99 : EXPECT_CALL(op, ApplyTransform(&op.add_imports_tx_,
100 : &policy,
101 : &bg,
102 E : header)).WillOnce(Return(false));
103 :
104 E : EXPECT_FALSE(op.Apply(&policy, &bg, header));
105 E : }
106 :
107 E : TEST_F(AddImportsOperationTest, RunSucceeds) {
108 E : TestAddImportsOperation op;
109 E : ASSERT_NO_FATAL_FAILURE(InitConfig(kSimpleConfig));
110 E : pe::PETransformPolicy policy;
111 E : ASSERT_TRUE(op.Init(&policy, config_.get()));
112 :
113 E : BlockGraph bg;
114 E : BlockGraph::Block* header = bg.AddBlock(BlockGraph::DATA_BLOCK, 1, "header");
115 :
116 : EXPECT_CALL(op, ApplyTransform(&op.add_imports_tx_,
117 : &policy,
118 : &bg,
119 E : header)).WillOnce(Return(true));
120 :
121 E : EXPECT_TRUE(op.Apply(&policy, &bg, header));
122 E : }
123 :
124 : } // namespace operations
125 : } // namespace pehacker
|