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