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/bbentry_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 TestBasicBlockEntryInstrumenter : public BasicBlockEntryInstrumenter {
28 : public:
29 : using BasicBlockEntryInstrumenter::agent_dll_;
30 : using BasicBlockEntryInstrumenter::input_image_path_;
31 : using BasicBlockEntryInstrumenter::input_pdb_path_;
32 : using BasicBlockEntryInstrumenter::output_image_path_;
33 : using BasicBlockEntryInstrumenter::output_pdb_path_;
34 : using BasicBlockEntryInstrumenter::allow_overwrite_;
35 : using BasicBlockEntryInstrumenter::no_augment_pdb_;
36 : using BasicBlockEntryInstrumenter::no_strip_strings_;
37 : using BasicBlockEntryInstrumenter::inline_fast_path_;
38 : using BasicBlockEntryInstrumenter::debug_friendly_;
39 : using BasicBlockEntryInstrumenter::kAgentDllBasicBlockEntry;
40 : using BasicBlockEntryInstrumenter::InstrumentPrepare;
41 : using BasicBlockEntryInstrumenter::InstrumentImpl;
42 : using InstrumenterWithAgent::CreateRelinker;
43 : };
44 :
45 : class BasicBlockEntryInstrumenterTest : public testing::PELibUnitTest {
46 : public:
47 : typedef testing::PELibUnitTest Super;
48 :
49 E : BasicBlockEntryInstrumenterTest()
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 : TestBasicBlockEntryInstrumenter instrumenter_;
108 : };
109 :
110 : } // namespace
111 :
112 E : TEST_F(BasicBlockEntryInstrumenterTest, ParseMinimalBasicBlockEntry) {
113 E : SetUpValidCommandLine();
114 :
115 E : EXPECT_TRUE(instrumenter_.ParseCommandLine(&cmd_line_));
116 :
117 E : EXPECT_EQ(abs_input_image_path_, instrumenter_.input_image_path_);
118 E : EXPECT_EQ(output_image_path_, instrumenter_.output_image_path_);
119 : EXPECT_EQ(
120 : std::string(TestBasicBlockEntryInstrumenter::kAgentDllBasicBlockEntry),
121 E : instrumenter_.agent_dll_);
122 E : EXPECT_FALSE(instrumenter_.allow_overwrite_);
123 E : EXPECT_FALSE(instrumenter_.no_augment_pdb_);
124 E : EXPECT_FALSE(instrumenter_.no_strip_strings_);
125 E : EXPECT_FALSE(instrumenter_.debug_friendly_);
126 E : EXPECT_FALSE(instrumenter_.inline_fast_path_);
127 E : }
128 :
129 E : TEST_F(BasicBlockEntryInstrumenterTest, ParseFullBasicBlockEntry) {
130 E : SetUpValidCommandLine();
131 E : cmd_line_.AppendSwitchASCII("agent", "foo.dll");
132 E : cmd_line_.AppendSwitch("debug-friendly");
133 E : cmd_line_.AppendSwitchPath("input-pdb", input_pdb_path_);
134 E : cmd_line_.AppendSwitch("no-augment-pdb");
135 E : cmd_line_.AppendSwitch("no-strip-strings");
136 E : cmd_line_.AppendSwitch("inline-fast-path");
137 E : cmd_line_.AppendSwitchPath("output-pdb", output_pdb_path_);
138 E : cmd_line_.AppendSwitch("overwrite");
139 :
140 E : EXPECT_TRUE(instrumenter_.ParseCommandLine(&cmd_line_));
141 :
142 E : EXPECT_EQ(abs_input_image_path_, instrumenter_.input_image_path_);
143 E : EXPECT_EQ(output_image_path_, instrumenter_.output_image_path_);
144 E : EXPECT_EQ(abs_input_pdb_path_, instrumenter_.input_pdb_path_);
145 E : EXPECT_EQ(output_pdb_path_, instrumenter_.output_pdb_path_);
146 E : EXPECT_EQ(std::string("foo.dll"), instrumenter_.agent_dll_);
147 E : EXPECT_TRUE(instrumenter_.allow_overwrite_);
148 E : EXPECT_TRUE(instrumenter_.inline_fast_path_);
149 E : EXPECT_TRUE(instrumenter_.no_augment_pdb_);
150 E : EXPECT_TRUE(instrumenter_.no_strip_strings_);
151 E : EXPECT_TRUE(instrumenter_.debug_friendly_);
152 E : }
153 :
154 E : TEST_F(BasicBlockEntryInstrumenterTest, InstrumentImpl) {
155 E : SetUpValidCommandLine();
156 :
157 E : EXPECT_TRUE(instrumenter_.ParseCommandLine(&cmd_line_));
158 E : EXPECT_TRUE(instrumenter_.InstrumentPrepare());
159 E : EXPECT_TRUE(instrumenter_.CreateRelinker());
160 E : EXPECT_TRUE(instrumenter_.InstrumentImpl());
161 E : }
162 :
163 : } // namespace instrumenters
164 : } // namespace instrument
|