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_dll_path_;
31 : using BasicBlockEntryInstrumenter::input_pdb_path_;
32 : using BasicBlockEntryInstrumenter::output_dll_path_;
33 : using BasicBlockEntryInstrumenter::output_pdb_path_;
34 : using BasicBlockEntryInstrumenter::allow_overwrite_;
35 : using BasicBlockEntryInstrumenter::new_decomposer_;
36 : using BasicBlockEntryInstrumenter::no_augment_pdb_;
37 : using BasicBlockEntryInstrumenter::no_parse_debug_info_;
38 : using BasicBlockEntryInstrumenter::no_strip_strings_;
39 : using BasicBlockEntryInstrumenter::inline_fast_path_;
40 : using BasicBlockEntryInstrumenter::debug_friendly_;
41 : using BasicBlockEntryInstrumenter::kAgentDllBasicBlockEntry;
42 : };
43 :
44 : class BasicBlockEntryInstrumenterTest : public testing::PELibUnitTest {
45 : public:
46 : typedef testing::PELibUnitTest Super;
47 :
48 E : BasicBlockEntryInstrumenterTest()
49 : : cmd_line_(base::FilePath(L"instrument.exe")) {
50 E : }
51 :
52 E : virtual void SetUp() OVERRIDE {
53 E : testing::Test::SetUp();
54 :
55 : // Several of the tests generate progress and (deliberate) error messages
56 : // that would otherwise clutter the unittest output.
57 E : logging::SetMinLogLevel(logging::LOG_FATAL);
58 :
59 : // Setup the IO streams.
60 E : CreateTemporaryDir(&temp_dir_);
61 E : stdin_path_ = temp_dir_.Append(L"NUL");
62 E : stdout_path_ = temp_dir_.Append(L"stdout.txt");
63 E : stderr_path_ = temp_dir_.Append(L"stderr.txt");
64 E : InitStreams(stdin_path_, stdout_path_, stderr_path_);
65 :
66 : // Initialize the (potential) input and output path values.
67 E : abs_input_dll_path_ = testing::GetExeRelativePath(testing::kTestDllName);
68 E : input_dll_path_ = testing::GetRelativePath(abs_input_dll_path_);
69 E : abs_input_pdb_path_ = testing::GetExeRelativePath(testing::kTestDllPdbName);
70 E : input_pdb_path_ = testing::GetRelativePath(abs_input_pdb_path_);
71 E : output_dll_path_ = temp_dir_.Append(input_dll_path_.BaseName());
72 E : output_pdb_path_ = temp_dir_.Append(input_pdb_path_.BaseName());
73 E : }
74 :
75 E : void SetUpValidCommandLine() {
76 E : cmd_line_.AppendSwitchPath("input-dll", input_dll_path_);
77 E : cmd_line_.AppendSwitchPath("output-dll", output_dll_path_);
78 E : }
79 :
80 : protected:
81 : base::FilePath temp_dir_;
82 :
83 : // @name The redirected streams paths.
84 : // @{
85 : base::FilePath stdin_path_;
86 : base::FilePath stdout_path_;
87 : base::FilePath stderr_path_;
88 : // @}
89 :
90 : // @name Command-line and parameters.
91 : // @{
92 : CommandLine cmd_line_;
93 : base::FilePath input_dll_path_;
94 : base::FilePath input_pdb_path_;
95 : base::FilePath output_dll_path_;
96 : base::FilePath output_pdb_path_;
97 : // @}
98 :
99 : // @name Expected final values of input parameters.
100 : // @{
101 : base::FilePath abs_input_dll_path_;
102 : base::FilePath abs_input_pdb_path_;
103 : // @}
104 :
105 : // The fake instrumenter we delegate to.
106 : TestBasicBlockEntryInstrumenter instrumenter_;
107 : };
108 :
109 : } // namespace
110 :
111 E : TEST_F(BasicBlockEntryInstrumenterTest, ParseMinimalBasicBlockEntry) {
112 E : SetUpValidCommandLine();
113 :
114 E : EXPECT_TRUE(instrumenter_.ParseCommandLine(&cmd_line_));
115 :
116 E : EXPECT_EQ(abs_input_dll_path_, instrumenter_.input_dll_path_);
117 E : EXPECT_EQ(output_dll_path_, instrumenter_.output_dll_path_);
118 : EXPECT_EQ(
119 : std::string(TestBasicBlockEntryInstrumenter::kAgentDllBasicBlockEntry),
120 E : instrumenter_.agent_dll_);
121 E : EXPECT_FALSE(instrumenter_.allow_overwrite_);
122 E : EXPECT_FALSE(instrumenter_.new_decomposer_);
123 E : EXPECT_FALSE(instrumenter_.no_augment_pdb_);
124 E : EXPECT_FALSE(instrumenter_.no_parse_debug_info_);
125 E : EXPECT_FALSE(instrumenter_.no_strip_strings_);
126 E : EXPECT_FALSE(instrumenter_.debug_friendly_);
127 E : EXPECT_FALSE(instrumenter_.inline_fast_path_);
128 E : }
129 :
130 E : TEST_F(BasicBlockEntryInstrumenterTest, ParseFullBasicBlockEntry) {
131 E : SetUpValidCommandLine();
132 E : cmd_line_.AppendSwitchASCII("agent", "foo.dll");
133 E : cmd_line_.AppendSwitch("debug-friendly");
134 E : cmd_line_.AppendSwitchPath("input-pdb", input_pdb_path_);
135 E : cmd_line_.AppendSwitch("new-decomposer");
136 E : cmd_line_.AppendSwitch("no-augment-pdb");
137 E : cmd_line_.AppendSwitch("no-parse-debug-info");
138 E : cmd_line_.AppendSwitch("no-strip-strings");
139 E : cmd_line_.AppendSwitch("inline-fast-path");
140 E : cmd_line_.AppendSwitchPath("output-pdb", output_pdb_path_);
141 E : cmd_line_.AppendSwitch("overwrite");
142 :
143 E : EXPECT_TRUE(instrumenter_.ParseCommandLine(&cmd_line_));
144 :
145 E : EXPECT_EQ(abs_input_dll_path_, instrumenter_.input_dll_path_);
146 E : EXPECT_EQ(output_dll_path_, instrumenter_.output_dll_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_.new_decomposer_);
152 E : EXPECT_TRUE(instrumenter_.inline_fast_path_);
153 E : EXPECT_TRUE(instrumenter_.no_augment_pdb_);
154 E : EXPECT_TRUE(instrumenter_.no_parse_debug_info_);
155 E : EXPECT_TRUE(instrumenter_.no_strip_strings_);
156 E : EXPECT_TRUE(instrumenter_.debug_friendly_);
157 E : }
158 :
159 E : TEST_F(BasicBlockEntryInstrumenterTest, InstrumentImpl) {
160 E : SetUpValidCommandLine();
161 :
162 E : EXPECT_TRUE(instrumenter_.ParseCommandLine(&cmd_line_));
163 E : EXPECT_TRUE(instrumenter_.Instrument());
164 E : }
165 :
166 : } // namespace instrumenters
167 : } // namespace instrument
|