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_image_to_text_app.h" // NOLINT
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 TestDecomposeImageToTextApp : public DecomposeImageToTextApp {
35 : public:
36 : // Methods
37 : using DecomposeImageToTextApp::PrintUsage;
38 :
39 : // Member variables
40 : using DecomposeImageToTextApp::image_path_;
41 : using DecomposeImageToTextApp::dump_basic_blocks_;
42 : using DecomposeImageToTextApp::num_refs_;
43 : };
44 :
45 : class DecomposeImageToTextAppTest : public testing::PELibUnitTest {
46 : public:
47 : typedef testing::PELibUnitTest Super;
48 : typedef Application<TestDecomposeImageToTextApp> TestApplication;
49 :
50 : DecomposeImageToTextAppTest()
51 : : cmd_line_(base::FilePath(L"decompose_image_to_text.exe")),
52 E : impl_(app_.implementation()) {
53 E : }
54 :
55 E : void SetUp() {
56 E : Super::SetUp();
57 :
58 : // Setup the IO streams.
59 E : CreateTemporaryDir(&temp_dir_);
60 E : stdin_path_ = temp_dir_.Append(L"NUL");
61 E : stdout_path_ = temp_dir_.Append(L"stdout.txt");
62 E : stderr_path_ = temp_dir_.Append(L"stderr.txt");
63 E : InitStreams(stdin_path_, stdout_path_, stderr_path_);
64 :
65 : // Initialize the input and output path values.
66 E : image_path_ = testing::GetExeTestDataRelativePath(testing::kTestDllName);
67 :
68 : // Point the application at the test's command-line and IO streams.
69 E : app_.set_command_line(&cmd_line_);
70 E : app_.set_in(in());
71 E : app_.set_out(out());
72 E : app_.set_err(err());
73 E : }
74 :
75 : protected:
76 : // The command line to be given to the application under test.
77 : base::CommandLine cmd_line_;
78 :
79 : // The application object under test.
80 : TestApplication app_;
81 :
82 : // A reference to the underlying application implementation for convenience.
83 : TestDecomposeImageToTextApp& impl_;
84 :
85 : // A temporary folder where all IO will be stored.
86 : base::FilePath temp_dir_;
87 :
88 : // @name File paths used for the standard IO streams.
89 : // @{
90 : base::FilePath stdin_path_;
91 : base::FilePath stdout_path_;
92 : base::FilePath stderr_path_;
93 : // @}
94 :
95 : // @name Paths given as command-line parameters
96 : // @{
97 : base::FilePath image_path_;
98 : // @{
99 : };
100 :
101 : } // namespace
102 :
103 E : TEST_F(DecomposeImageToTextAppTest, EmptyCommandLineFails) {
104 E : ASSERT_FALSE(impl_.ParseCommandLine(&cmd_line_));
105 E : }
106 :
107 E : TEST_F(DecomposeImageToTextAppTest, GetHelp) {
108 E : cmd_line_.AppendSwitch("help");
109 E : ASSERT_FALSE(impl_.ParseCommandLine(&cmd_line_));
110 E : }
111 :
112 E : TEST_F(DecomposeImageToTextAppTest, ParseCommandLine) {
113 E : ASSERT_TRUE(impl_.image_path_.empty());
114 E : ASSERT_FALSE(impl_.dump_basic_blocks_);
115 :
116 E : cmd_line_.AppendSwitchPath("image", image_path_);
117 E : cmd_line_.AppendSwitch("basic-blocks");
118 :
119 E : ASSERT_TRUE(impl_.ParseCommandLine(&cmd_line_));
120 E : ASSERT_EQ(image_path_, impl_.image_path_);
121 E : ASSERT_TRUE(impl_.dump_basic_blocks_);
122 E : }
123 :
124 E : TEST_F(DecomposeImageToTextAppTest, RunOnTestDll) {
125 E : ScopedLogLevelSaver log_level_saver;
126 E : logging::SetMinLogLevel(logging::LOG_FATAL);
127 :
128 E : cmd_line_.AppendSwitchPath("image", image_path_);
129 E : cmd_line_.AppendSwitch("basic-blocks");
130 :
131 E : ASSERT_EQ(0, app_.Run());
132 E : }
133 :
134 : } // namespace pe
|