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