1 : // Copyright 2012 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/pe/decompose_app.h"
16 :
17 : #include "gmock/gmock.h"
18 : #include "gtest/gtest.h"
19 : #include "syzygy/block_graph/unittest_util.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 pe {
26 :
27 : using block_graph::BlockGraph;
28 : using application::Application;
29 : using core::RelativeAddress;
30 : using ::testing::ScopedLogLevelSaver;
31 :
32 : namespace {
33 :
34 : class TestDecomposeApp : public DecomposeApp {
35 : public:
36 : // Member variables.
37 : using DecomposeApp::image_path_;
38 : using DecomposeApp::output_path_;
39 : using DecomposeApp::benchmark_load_;
40 : using DecomposeApp::strip_strings_;
41 : };
42 :
43 : class DecomposeAppTest : public testing::PELibUnitTest {
44 : public:
45 : typedef testing::PELibUnitTest Super;
46 : typedef Application<TestDecomposeApp> TestApplication;
47 :
48 : DecomposeAppTest()
49 : : cmd_line_(base::FilePath(L"decompose.exe")),
50 E : impl_(app_.implementation()) {
51 E : }
52 :
53 E : void SetUp() {
54 E : Super::SetUp();
55 :
56 : // Setup the IO streams.
57 E : CreateTemporaryDir(&temp_dir_);
58 E : stdin_path_ = temp_dir_.Append(L"NUL");
59 E : stdout_path_ = temp_dir_.Append(L"stdout.txt");
60 E : stderr_path_ = temp_dir_.Append(L"stderr.txt");
61 E : InitStreams(stdin_path_, stdout_path_, stderr_path_);
62 :
63 : // Initialize the input and output path values.
64 E : image_path_ = testing::GetExeTestDataRelativePath(testing::kTestDllName);
65 E : output_path_ = temp_dir_.Append(L"output.bg");
66 :
67 : // Point the application at the test's command-line and IO streams.
68 E : app_.set_command_line(&cmd_line_);
69 E : app_.set_in(in());
70 E : app_.set_out(out());
71 E : app_.set_err(err());
72 E : }
73 :
74 : protected:
75 : // The command line to be given to the application under test.
76 : base::CommandLine cmd_line_;
77 :
78 : // The application object under test.
79 : TestApplication app_;
80 :
81 : // A reference to the underlying application implementation for convenience.
82 : TestDecomposeApp& impl_;
83 :
84 : // A temporary folder where all IO will be stored.
85 : base::FilePath temp_dir_;
86 :
87 : // @name File paths used for the standard IO streams.
88 : // @{
89 : base::FilePath stdin_path_;
90 : base::FilePath stdout_path_;
91 : base::FilePath stderr_path_;
92 : // @}
93 :
94 : // @name Paths given as command-line parameters
95 : // @{
96 : base::FilePath image_path_;
97 : base::FilePath output_path_;
98 : // @{
99 : };
100 :
101 : } // namespace
102 :
103 E : TEST_F(DecomposeAppTest, EmptyCommandLineFails) {
104 E : ASSERT_FALSE(impl_.ParseCommandLine(&cmd_line_));
105 E : }
106 :
107 E : TEST_F(DecomposeAppTest, GetHelp) {
108 E : cmd_line_.AppendSwitch("help");
109 E : ASSERT_FALSE(impl_.ParseCommandLine(&cmd_line_));
110 E : }
111 :
112 E : TEST_F(DecomposeAppTest, ParseCommandLineMinimal) {
113 E : ASSERT_TRUE(impl_.image_path_.empty());
114 E : ASSERT_TRUE(impl_.output_path_.empty());
115 E : ASSERT_FALSE(impl_.benchmark_load_);
116 :
117 E : cmd_line_.AppendSwitchPath("image", image_path_);
118 :
119 E : ASSERT_TRUE(impl_.ParseCommandLine(&cmd_line_));
120 E : ASSERT_EQ(image_path_, impl_.image_path_);
121 E : ASSERT_EQ(image_path_.value() + L".bg", impl_.output_path_.value());
122 E : ASSERT_FALSE(impl_.benchmark_load_);
123 E : ASSERT_FALSE(impl_.strip_strings_);
124 E : }
125 :
126 E : TEST_F(DecomposeAppTest, ParseCommandLineFull) {
127 E : ASSERT_TRUE(impl_.image_path_.empty());
128 E : ASSERT_TRUE(impl_.output_path_.empty());
129 E : ASSERT_FALSE(impl_.benchmark_load_);
130 :
131 E : cmd_line_.AppendSwitchPath("image", image_path_);
132 E : cmd_line_.AppendSwitchPath("output", output_path_);
133 E : cmd_line_.AppendSwitch("benchmark-load");
134 E : cmd_line_.AppendSwitch("strip-strings");
135 :
136 E : ASSERT_TRUE(impl_.ParseCommandLine(&cmd_line_));
137 E : ASSERT_EQ(image_path_, impl_.image_path_);
138 E : ASSERT_EQ(output_path_, impl_.output_path_);
139 E : ASSERT_TRUE(impl_.benchmark_load_);
140 E : ASSERT_TRUE(impl_.strip_strings_);
141 E : }
142 :
143 E : TEST_F(DecomposeAppTest, RunOnTestDll) {
144 E : ScopedLogLevelSaver log_level_saver;
145 E : logging::SetMinLogLevel(logging::LOG_FATAL);
146 :
147 E : cmd_line_.AppendSwitchPath("image", image_path_);
148 E : cmd_line_.AppendSwitchPath("output", output_path_);
149 E : cmd_line_.AppendSwitch("benchmark-load");
150 :
151 E : ASSERT_EQ(0, app_.Run());
152 E : }
153 :
154 E : TEST_F(DecomposeAppTest, RunOnTestDllBlockGraphOnlyNoStrings) {
155 E : ScopedLogLevelSaver log_level_saver;
156 E : logging::SetMinLogLevel(logging::LOG_FATAL);
157 :
158 E : cmd_line_.AppendSwitchPath("image", image_path_);
159 E : cmd_line_.AppendSwitchPath("output", output_path_);
160 E : cmd_line_.AppendSwitch("benchmark-load");
161 E : cmd_line_.AppendSwitch("strip-strings");
162 E : cmd_line_.AppendSwitch("graph-only");
163 :
164 E : ASSERT_EQ(0, app_.Run());
165 E : }
166 :
167 : } // namespace pe
|