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