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/instrument/instrumenters/branch_instrumenter.h"
16 :
17 : #include "base/command_line.h"
18 : #include "base/compiler_specific.h"
19 : #include "gmock/gmock.h"
20 : #include "gtest/gtest.h"
21 : #include "syzygy/core/unittest_util.h"
22 : #include "syzygy/pe/unittest_util.h"
23 :
24 : namespace instrument {
25 : namespace instrumenters {
26 :
27 : namespace {
28 :
29 : class TestBranchInstrumenter : public BranchInstrumenter {
30 : public:
31 : using BranchInstrumenter::agent_dll_;
32 : using BranchInstrumenter::input_image_path_;
33 : using BranchInstrumenter::input_pdb_path_;
34 : using BranchInstrumenter::output_image_path_;
35 : using BranchInstrumenter::output_pdb_path_;
36 : using BranchInstrumenter::allow_overwrite_;
37 : using BranchInstrumenter::old_decomposer_;
38 : using BranchInstrumenter::no_augment_pdb_;
39 : using BranchInstrumenter::no_strip_strings_;
40 : using BranchInstrumenter::debug_friendly_;
41 : using BranchInstrumenter::buffering_;
42 : using BranchInstrumenter::fs_slot_;
43 : using BranchInstrumenter::kAgentDllBasicBlockEntry;
44 : using BranchInstrumenter::InstrumentImpl;
45 : using InstrumenterWithAgent::CreateRelinker;
46 :
47 E : TestBranchInstrumenter() {
48 : // Call the GetPERelinker function to initialize it.
49 E : pe::PERelinker* relinker = GetPERelinker();
50 E : EXPECT_TRUE(relinker != NULL);
51 E : }
52 : };
53 :
54 : class BranchInstrumenterTest : public testing::PELibUnitTest {
55 : public:
56 : typedef testing::PELibUnitTest Super;
57 :
58 E : BranchInstrumenterTest()
59 : : cmd_line_(base::FilePath(L"instrument.exe")) {
60 E : }
61 :
62 E : virtual void SetUp() OVERRIDE {
63 E : testing::Test::SetUp();
64 :
65 : // Several of the tests generate progress and (deliberate) error messages
66 : // that would otherwise clutter the unittest output.
67 E : logging::SetMinLogLevel(logging::LOG_FATAL);
68 :
69 : // Setup the IO streams.
70 E : CreateTemporaryDir(&temp_dir_);
71 E : stdin_path_ = temp_dir_.Append(L"NUL");
72 E : stdout_path_ = temp_dir_.Append(L"stdout.txt");
73 E : stderr_path_ = temp_dir_.Append(L"stderr.txt");
74 E : InitStreams(stdin_path_, stdout_path_, stderr_path_);
75 :
76 : // Initialize the (potential) input and output path values.
77 E : abs_input_image_path_ = testing::GetExeRelativePath(testing::kTestDllName);
78 E : input_image_path_ = testing::GetRelativePath(abs_input_image_path_);
79 E : abs_input_pdb_path_ = testing::GetExeRelativePath(testing::kTestDllPdbName);
80 E : input_pdb_path_ = testing::GetRelativePath(abs_input_pdb_path_);
81 E : output_image_path_ = temp_dir_.Append(input_image_path_.BaseName());
82 E : output_pdb_path_ = temp_dir_.Append(input_pdb_path_.BaseName());
83 E : }
84 :
85 E : void SetUpValidCommandLine() {
86 E : cmd_line_.AppendSwitchPath("input-image", input_image_path_);
87 E : cmd_line_.AppendSwitchPath("output-image", output_image_path_);
88 E : }
89 :
90 : protected:
91 : base::FilePath temp_dir_;
92 :
93 : // @name The redirected streams paths.
94 : // @{
95 : base::FilePath stdin_path_;
96 : base::FilePath stdout_path_;
97 : base::FilePath stderr_path_;
98 : // @}
99 :
100 : // @name Command-line and parameters.
101 : // @{
102 : CommandLine cmd_line_;
103 : base::FilePath input_image_path_;
104 : base::FilePath input_pdb_path_;
105 : base::FilePath output_image_path_;
106 : base::FilePath output_pdb_path_;
107 : base::FilePath test_dll_filter_path_;
108 : base::FilePath dummy_filter_path_;
109 : // @}
110 :
111 : // @name Expected final values of input parameters.
112 : // @{
113 : base::FilePath abs_input_image_path_;
114 : base::FilePath abs_input_pdb_path_;
115 : // @}
116 :
117 : // The fake instrumenter we delegate to.
118 : TestBranchInstrumenter instrumenter_;
119 : };
120 :
121 : } // namespace
122 :
123 E : TEST_F(BranchInstrumenterTest, ParseMinimalBranch) {
124 E : SetUpValidCommandLine();
125 :
126 E : EXPECT_TRUE(instrumenter_.ParseCommandLine(&cmd_line_));
127 :
128 E : EXPECT_EQ(abs_input_image_path_, instrumenter_.input_image_path_);
129 E : EXPECT_EQ(output_image_path_, instrumenter_.output_image_path_);
130 : EXPECT_EQ(std::string(TestBranchInstrumenter::kAgentDllBasicBlockEntry),
131 E : instrumenter_.agent_dll_);
132 E : EXPECT_FALSE(instrumenter_.allow_overwrite_);
133 E : EXPECT_FALSE(instrumenter_.old_decomposer_);
134 E : EXPECT_FALSE(instrumenter_.no_augment_pdb_);
135 E : EXPECT_FALSE(instrumenter_.no_strip_strings_);
136 E : EXPECT_FALSE(instrumenter_.debug_friendly_);
137 E : EXPECT_FALSE(instrumenter_.buffering_);
138 E : EXPECT_EQ(0U, instrumenter_.fs_slot_);
139 E : }
140 :
141 E : TEST_F(BranchInstrumenterTest, ParseFullBranch) {
142 E : SetUpValidCommandLine();
143 :
144 E : cmd_line_.AppendSwitchPath("filter", test_dll_filter_path_);
145 E : cmd_line_.AppendSwitchASCII("agent", "foo.dll");
146 E : cmd_line_.AppendSwitch("debug-friendly");
147 E : cmd_line_.AppendSwitchPath("input-pdb", input_pdb_path_);
148 E : cmd_line_.AppendSwitch("old-decomposer");
149 E : cmd_line_.AppendSwitch("no-augment-pdb");
150 E : cmd_line_.AppendSwitch("no-strip-strings");
151 E : cmd_line_.AppendSwitchPath("output-pdb", output_pdb_path_);
152 E : cmd_line_.AppendSwitch("overwrite");
153 E : cmd_line_.AppendSwitch("buffering");
154 E : cmd_line_.AppendSwitchASCII("fs-slot", "2");
155 :
156 E : EXPECT_TRUE(instrumenter_.ParseCommandLine(&cmd_line_));
157 :
158 E : EXPECT_EQ(abs_input_image_path_, instrumenter_.input_image_path_);
159 E : EXPECT_EQ(output_image_path_, instrumenter_.output_image_path_);
160 E : EXPECT_EQ(abs_input_pdb_path_, instrumenter_.input_pdb_path_);
161 E : EXPECT_EQ(output_pdb_path_, instrumenter_.output_pdb_path_);
162 E : EXPECT_EQ(std::string("foo.dll"), instrumenter_.agent_dll_);
163 E : EXPECT_TRUE(instrumenter_.allow_overwrite_);
164 E : EXPECT_TRUE(instrumenter_.old_decomposer_);
165 E : EXPECT_TRUE(instrumenter_.no_augment_pdb_);
166 E : EXPECT_TRUE(instrumenter_.no_strip_strings_);
167 E : EXPECT_TRUE(instrumenter_.debug_friendly_);
168 E : EXPECT_TRUE(instrumenter_.buffering_);
169 E : EXPECT_EQ(2U, instrumenter_.fs_slot_);
170 E : }
171 :
172 E : TEST_F(BranchInstrumenterTest, ParseHugeSlotFail) {
173 E : SetUpValidCommandLine();
174 E : cmd_line_.AppendSwitchASCII("fs-slot", "8");
175 E : EXPECT_FALSE(instrumenter_.ParseCommandLine(&cmd_line_));
176 E : }
177 :
178 E : TEST_F(BranchInstrumenterTest, ParseNegativeSlotFail) {
179 E : SetUpValidCommandLine();
180 E : cmd_line_.AppendSwitchASCII("fs-slot", "-1");
181 E : EXPECT_FALSE(instrumenter_.ParseCommandLine(&cmd_line_));
182 E : }
183 :
184 E : TEST_F(BranchInstrumenterTest, ParseZeroSlotFail) {
185 E : SetUpValidCommandLine();
186 E : cmd_line_.AppendSwitchASCII("fs-slot", "0");
187 E : EXPECT_FALSE(instrumenter_.ParseCommandLine(&cmd_line_));
188 E : }
189 :
190 E : TEST_F(BranchInstrumenterTest, ParseDummySlotFail) {
191 E : SetUpValidCommandLine();
192 E : cmd_line_.AppendSwitchASCII("fs-slot", "dummy");
193 E : EXPECT_FALSE(instrumenter_.ParseCommandLine(&cmd_line_));
194 E : }
195 :
196 E : TEST_F(BranchInstrumenterTest, InstrumentImpl) {
197 E : SetUpValidCommandLine();
198 :
199 E : EXPECT_TRUE(instrumenter_.ParseCommandLine(&cmd_line_));
200 E : EXPECT_TRUE(instrumenter_.CreateRelinker());
201 E : EXPECT_TRUE(instrumenter_.InstrumentImpl());
202 E : }
203 :
204 : } // namespace instrumenters
205 : } // namespace instrument
|