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