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