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/optimize/optimize_app.h"
16 :
17 : #include "base/strings/stringprintf.h"
18 : #include "gmock/gmock.h"
19 : #include "gtest/gtest.h"
20 : #include "syzygy/common/unittest_util.h"
21 : #include "syzygy/core/unittest_util.h"
22 : #include "syzygy/pe/pe_utils.h"
23 : #include "syzygy/pe/unittest_util.h"
24 :
25 : namespace optimize {
26 :
27 : using application::Application;
28 : using ::testing::ScopedLogLevelSaver;
29 :
30 : namespace {
31 :
32 : class TestOptimizeApp : public OptimizeApp {
33 : public:
34 : using OptimizeApp::input_image_path_;
35 : using OptimizeApp::input_pdb_path_;
36 : using OptimizeApp::output_image_path_;
37 : using OptimizeApp::output_pdb_path_;
38 : using OptimizeApp::branch_file_path_;
39 : using OptimizeApp::unreachable_graph_path_;
40 : using OptimizeApp::basic_block_reorder_;
41 : using OptimizeApp::block_alignment_;
42 : using OptimizeApp::fuzz_;
43 : using OptimizeApp::inlining_;
44 : using OptimizeApp::allow_inline_assembly_;
45 : using OptimizeApp::peephole_;
46 : using OptimizeApp::overwrite_;
47 : };
48 :
49 : typedef application::Application<TestOptimizeApp> TestApp;
50 :
51 : class OptimizeAppTest : public testing::PELibUnitTest {
52 : public:
53 : typedef testing::PELibUnitTest Super;
54 :
55 : OptimizeAppTest()
56 : : cmd_line_(base::FilePath(L"optimize.exe")),
57 E : test_impl_(test_app_.implementation()) {
58 E : }
59 :
60 E : void SetUp() {
61 E : Super::SetUp();
62 :
63 : // Several of the tests generate progress and (deliberate) error messages
64 : // that would otherwise clutter the unittest output.
65 E : logging::SetMinLogLevel(logging::LOG_FATAL);
66 :
67 : // Setup the IO streams.
68 E : CreateTemporaryDir(&temp_dir_);
69 E : stdin_path_ = temp_dir_.Append(L"NUL");
70 E : stdout_path_ = temp_dir_.Append(L"stdout.txt");
71 E : stderr_path_ = temp_dir_.Append(L"stderr.txt");
72 E : InitStreams(stdin_path_, stdout_path_, stderr_path_);
73 :
74 : // Initialize the (potential) input and output path values.
75 E : abs_input_image_path_ = testing::GetExeRelativePath(testing::kTestDllName);
76 E : input_image_path_ = testing::GetRelativePath(abs_input_image_path_);
77 E : abs_input_pdb_path_ = testing::GetExeRelativePath(testing::kTestDllPdbName);
78 E : input_pdb_path_ = testing::GetRelativePath(abs_input_pdb_path_);
79 E : output_image_path_ = temp_dir_.Append(input_image_path_.BaseName());
80 E : output_pdb_path_ = temp_dir_.Append(input_pdb_path_.BaseName());
81 E : branch_file_path_ = temp_dir_.Append(L"branch.json");
82 E : unreachable_graph_path_ = temp_dir_.Append(L"unreachable.callgrind");
83 :
84 : // Point the application at the test's command-line and IO streams.
85 E : test_app_.set_command_line(&cmd_line_);
86 E : test_app_.set_in(in());
87 E : test_app_.set_out(out());
88 E : test_app_.set_err(err());
89 E : }
90 :
91 : // Stashes the current log-level before each test instance and restores it
92 : // after each test completes.
93 : ScopedLogLevelSaver log_level_saver;
94 :
95 : // @name The application under test.
96 : // @{
97 : TestApp test_app_;
98 : TestApp::Implementation& test_impl_;
99 : base::FilePath temp_dir_;
100 : base::FilePath stdin_path_;
101 : base::FilePath stdout_path_;
102 : base::FilePath stderr_path_;
103 : // @}
104 :
105 : // @name Command-line and parameters.
106 : // @{
107 : base::CommandLine cmd_line_;
108 : base::FilePath input_image_path_;
109 : base::FilePath input_pdb_path_;
110 : base::FilePath output_image_path_;
111 : base::FilePath output_pdb_path_;
112 : base::FilePath branch_file_path_;
113 : base::FilePath unreachable_graph_path_;
114 : // @}
115 :
116 : // @name Expected final values of input parameters.
117 : // @{
118 : base::FilePath abs_input_image_path_;
119 : base::FilePath abs_input_pdb_path_;
120 : // @}
121 : };
122 :
123 : } // namespace
124 :
125 E : TEST_F(OptimizeAppTest, GetHelp) {
126 E : cmd_line_.AppendSwitch("help");
127 E : ASSERT_FALSE(test_impl_.ParseCommandLine(&cmd_line_));
128 E : }
129 :
130 E : TEST_F(OptimizeAppTest, EmptyCommandLineFails) {
131 E : ASSERT_FALSE(test_impl_.ParseCommandLine(&cmd_line_));
132 E : }
133 :
134 E : TEST_F(OptimizeAppTest, ParseWithNoInputFails) {
135 E : cmd_line_.AppendSwitchPath("output-image", output_image_path_);
136 :
137 E : ASSERT_FALSE(test_impl_.ParseCommandLine(&cmd_line_));
138 E : }
139 :
140 E : TEST_F(OptimizeAppTest, ParseWithNoOutputFails) {
141 E : cmd_line_.AppendSwitchPath("input-image", output_image_path_);
142 :
143 E : ASSERT_FALSE(test_impl_.ParseCommandLine(&cmd_line_));
144 E : }
145 :
146 E : TEST_F(OptimizeAppTest, ParseMinimalCommandLineWithInputAndOutput) {
147 E : cmd_line_.AppendSwitchPath("input-image", input_image_path_);
148 E : cmd_line_.AppendSwitchPath("output-image", output_image_path_);
149 :
150 E : EXPECT_TRUE(test_impl_.ParseCommandLine(&cmd_line_));
151 E : EXPECT_TRUE(test_impl_.SetUp());
152 E : }
153 :
154 E : TEST_F(OptimizeAppTest, ParseMinimalCommandLineWithBranchFile) {
155 E : cmd_line_.AppendSwitchPath("branch-file", branch_file_path_);
156 E : cmd_line_.AppendSwitchPath("input-image", input_image_path_);
157 E : cmd_line_.AppendSwitchPath("output-image", output_image_path_);
158 E : EXPECT_FALSE(test_impl_.overwrite_);
159 E : EXPECT_FALSE(test_impl_.inlining_);
160 E : EXPECT_FALSE(test_impl_.allow_inline_assembly_);
161 E : EXPECT_FALSE(test_impl_.block_alignment_);
162 E : EXPECT_FALSE(test_impl_.basic_block_reorder_);
163 E : EXPECT_FALSE(test_impl_.peephole_);
164 E : EXPECT_FALSE(test_impl_.fuzz_);
165 :
166 E : EXPECT_TRUE(test_impl_.ParseCommandLine(&cmd_line_));
167 E : EXPECT_TRUE(test_impl_.SetUp());
168 E : }
169 :
170 E : TEST_F(OptimizeAppTest, ParseFullCommandLineWithBranchFile) {
171 E : cmd_line_.AppendSwitchPath("input-image", input_image_path_);
172 E : cmd_line_.AppendSwitchPath("output-image", output_image_path_);
173 E : cmd_line_.AppendSwitchPath("output-pdb", output_pdb_path_);
174 E : cmd_line_.AppendSwitchPath("branch-file", branch_file_path_);
175 E : cmd_line_.AppendSwitch("overwrite");
176 :
177 E : EXPECT_TRUE(test_impl_.ParseCommandLine(&cmd_line_));
178 E : EXPECT_FALSE(test_impl_.input_image_path_.empty());
179 E : EXPECT_TRUE(test_impl_.input_pdb_path_.empty());
180 E : EXPECT_EQ(output_image_path_, test_impl_.output_image_path_);
181 E : EXPECT_EQ(output_pdb_path_, test_impl_.output_pdb_path_);
182 E : EXPECT_EQ(branch_file_path_, test_impl_.branch_file_path_);
183 E : EXPECT_TRUE(test_impl_.overwrite_);
184 :
185 E : EXPECT_TRUE(test_impl_.SetUp());
186 E : }
187 :
188 E : TEST_F(OptimizeAppTest, ParseFullCommandLineWithInputAndOutputPdb) {
189 E : cmd_line_.AppendSwitchPath("input-image", input_image_path_);
190 E : cmd_line_.AppendSwitchPath("input-pdb", input_pdb_path_);
191 E : cmd_line_.AppendSwitchPath("output-image", output_image_path_);
192 E : cmd_line_.AppendSwitchPath("output-pdb", output_pdb_path_);
193 E : cmd_line_.AppendSwitch("overwrite");
194 E : cmd_line_.AppendSwitch("inlining");
195 E : cmd_line_.AppendSwitch("allow-inline-assembly");
196 E : cmd_line_.AppendSwitch("block-alignment");
197 E : cmd_line_.AppendSwitch("basic-block-reorder");
198 E : cmd_line_.AppendSwitch("peephole");
199 E : cmd_line_.AppendSwitch("fuzz");
200 :
201 E : EXPECT_TRUE(test_impl_.ParseCommandLine(&cmd_line_));
202 E : EXPECT_EQ(abs_input_image_path_, test_impl_.input_image_path_);
203 E : EXPECT_EQ(abs_input_pdb_path_, test_impl_.input_pdb_path_);
204 E : EXPECT_EQ(output_image_path_, test_impl_.output_image_path_);
205 E : EXPECT_EQ(output_pdb_path_, test_impl_.output_pdb_path_);
206 E : EXPECT_TRUE(test_impl_.overwrite_);
207 E : EXPECT_TRUE(test_impl_.inlining_);
208 E : EXPECT_TRUE(test_impl_.allow_inline_assembly_);
209 E : EXPECT_TRUE(test_impl_.block_alignment_);
210 E : EXPECT_TRUE(test_impl_.basic_block_reorder_);
211 E : EXPECT_TRUE(test_impl_.peephole_);
212 E : EXPECT_TRUE(test_impl_.fuzz_);
213 :
214 E : EXPECT_TRUE(test_impl_.SetUp());
215 E : }
216 :
217 E : TEST_F(OptimizeAppTest, ParseAllCommandLineWithInputAndOutputPdb) {
218 E : cmd_line_.AppendSwitchPath("input-image", input_image_path_);
219 E : cmd_line_.AppendSwitchPath("input-pdb", input_pdb_path_);
220 E : cmd_line_.AppendSwitchPath("output-image", output_image_path_);
221 E : cmd_line_.AppendSwitchPath("output-pdb", output_pdb_path_);
222 E : cmd_line_.AppendSwitch("overwrite");
223 E : cmd_line_.AppendSwitch("all");
224 :
225 E : EXPECT_TRUE(test_impl_.ParseCommandLine(&cmd_line_));
226 E : EXPECT_EQ(abs_input_image_path_, test_impl_.input_image_path_);
227 E : EXPECT_EQ(abs_input_pdb_path_, test_impl_.input_pdb_path_);
228 E : EXPECT_EQ(output_image_path_, test_impl_.output_image_path_);
229 E : EXPECT_EQ(output_pdb_path_, test_impl_.output_pdb_path_);
230 E : EXPECT_TRUE(test_impl_.overwrite_);
231 E : EXPECT_TRUE(test_impl_.inlining_);
232 E : EXPECT_TRUE(test_impl_.block_alignment_);
233 E : EXPECT_TRUE(test_impl_.basic_block_reorder_);
234 E : EXPECT_TRUE(test_impl_.block_alignment_);
235 E : EXPECT_TRUE(test_impl_.peephole_);
236 E : EXPECT_FALSE(test_impl_.fuzz_);
237 :
238 E : EXPECT_TRUE(test_impl_.SetUp());
239 E : }
240 :
241 E : TEST_F(OptimizeAppTest, ParseCommandLineWithUnreachableGraph) {
242 E : cmd_line_.AppendSwitchPath("input-image", input_image_path_);
243 E : cmd_line_.AppendSwitchPath("output-image", output_image_path_);
244 E : cmd_line_.AppendSwitch("unreachable-block");
245 E : cmd_line_.AppendSwitchPath("dump-unreachable-graph", unreachable_graph_path_);
246 E : cmd_line_.AppendSwitch("overwrite");
247 :
248 E : EXPECT_TRUE(test_impl_.ParseCommandLine(&cmd_line_));
249 E : EXPECT_FALSE(test_impl_.input_image_path_.empty());
250 E : EXPECT_EQ(output_image_path_, test_impl_.output_image_path_);
251 E : EXPECT_EQ(unreachable_graph_path_, test_impl_.unreachable_graph_path_);
252 E : EXPECT_TRUE(test_impl_.overwrite_);
253 :
254 E : EXPECT_TRUE(test_impl_.SetUp());
255 E : }
256 :
257 E : TEST_F(OptimizeAppTest, RelinkDecompose) {
258 E : cmd_line_.AppendSwitchPath("input-image", input_image_path_);
259 E : cmd_line_.AppendSwitchPath("output-image", output_image_path_);
260 E : cmd_line_.AppendSwitch("overwrite");
261 :
262 E : ASSERT_EQ(0, test_app_.Run());
263 E : ASSERT_NO_FATAL_FAILURE(CheckTestDll(output_image_path_));
264 E : }
265 :
266 : } // namespace optimize
|