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/instrumenter_with_agent.h"
16 :
17 : #include "base/command_line.h"
18 : #include "base/files/file_path.h"
19 : #include "gmock/gmock.h"
20 : #include "gtest/gtest.h"
21 : #include "syzygy/core/unittest_util.h"
22 : #include "syzygy/pe/unittest_util.h"
23 :
24 : namespace instrument {
25 : namespace instrumenters {
26 :
27 : namespace {
28 :
29 : const char kTestAgentDllName[] = "test_agent_dll.dll";
30 :
31 : class TestInstrumenterWithAgent : public InstrumenterWithAgent {
32 : public:
33 : using InstrumenterWithAgent::input_image_path_;
34 : using InstrumenterWithAgent::output_image_path_;
35 :
36 E : TestInstrumenterWithAgent() {
37 : // |agent_dll_| is left empty.
38 E : }
39 :
40 i : MOCK_METHOD0(InstrumentPrepare, bool());
41 i : MOCK_METHOD0(InstrumentImpl, bool());
42 :
43 i : const char* InstrumentationMode() override { return "test"; }
44 : };
45 :
46 : class TestInstrumenterWithAgentHasDefaultDll :
47 : public TestInstrumenterWithAgent {
48 : public:
49 E : TestInstrumenterWithAgentHasDefaultDll() {
50 E : agent_dll_ = kTestAgentDllName;
51 E : }
52 : };
53 :
54 : class InstrumenterWithAgentTest : public testing::PELibUnitTest {
55 : public:
56 : typedef testing::PELibUnitTest Super;
57 :
58 E : InstrumenterWithAgentTest()
59 : : cmd_line_(base::FilePath(L"instrument.exe")) {
60 E : }
61 :
62 E : void SetUp() override {
63 E : testing::Test::SetUp();
64 :
65 : // Several of the tests generate progress and (deliberate) error messages
66 : // that would otherwise clutter the unittest output.
67 E : logging::SetMinLogLevel(logging::LOG_FATAL);
68 :
69 : // Setup the IO streams.
70 E : CreateTemporaryDir(&temp_dir_);
71 E : stdin_path_ = temp_dir_.Append(L"NUL");
72 E : stdout_path_ = temp_dir_.Append(L"stdout.txt");
73 E : stderr_path_ = temp_dir_.Append(L"stderr.txt");
74 E : InitStreams(stdin_path_, stdout_path_, stderr_path_);
75 :
76 : // Initialize the (potential) input and output path values.
77 : abs_input_pe_image_path_ = testing::GetExeRelativePath(
78 E : testing::kTestDllName);
79 E : input_pe_image_path_ = testing::GetRelativePath(abs_input_pe_image_path_);
80 E : abs_input_pdb_path_ = testing::GetExeRelativePath(testing::kTestDllPdbName);
81 E : input_pdb_path_ = testing::GetRelativePath(abs_input_pdb_path_);
82 E : output_pe_image_path_ = temp_dir_.Append(input_pe_image_path_.BaseName());
83 E : output_pdb_path_ = temp_dir_.Append(input_pdb_path_.BaseName());
84 E : }
85 :
86 : protected:
87 : base::FilePath temp_dir_;
88 :
89 : // @name The redirected streams paths.
90 : // @{
91 : base::FilePath stdin_path_;
92 : base::FilePath stdout_path_;
93 : base::FilePath stderr_path_;
94 : // @}
95 :
96 : // @name Command-line and parameters.
97 : // @{
98 : base::CommandLine cmd_line_;
99 : base::FilePath input_pe_image_path_;
100 : base::FilePath input_pdb_path_;
101 : base::FilePath output_pe_image_path_;
102 : base::FilePath output_pdb_path_;
103 : // @}
104 :
105 : // @name Expected final values of input parameters.
106 : // @{
107 : base::FilePath abs_input_pe_image_path_;
108 : base::FilePath abs_input_pdb_path_;
109 : // @}
110 : };
111 :
112 : } // namespace
113 :
114 E : TEST_F(InstrumenterWithAgentTest, HasDefault_agent_dll) {
115 E : TestInstrumenterWithAgentHasDefaultDll instrumenter;
116 E : EXPECT_EQ(kTestAgentDllName, instrumenter.agent_dll());
117 E : }
118 :
119 E : TEST_F(InstrumenterWithAgentTest, EmptyCommandLineFails) {
120 E : TestInstrumenterWithAgentHasDefaultDll instrumenter;
121 E : EXPECT_FALSE(instrumenter.ParseCommandLine(&cmd_line_));
122 E : }
123 :
124 E : TEST_F(InstrumenterWithAgentTest, ParseWithNoInputImageFails) {
125 E : cmd_line_.AppendSwitchPath("output-image", output_pe_image_path_);
126 :
127 E : TestInstrumenterWithAgentHasDefaultDll instrumenter;
128 E : EXPECT_FALSE(instrumenter.ParseCommandLine(&cmd_line_));
129 E : }
130 :
131 E : TEST_F(InstrumenterWithAgentTest, ParseWithNoOutputImageFails) {
132 E : cmd_line_.AppendSwitchPath("input-image", input_pe_image_path_);
133 :
134 E : TestInstrumenterWithAgentHasDefaultDll instrumenter;
135 E : EXPECT_FALSE(instrumenter.ParseCommandLine(&cmd_line_));
136 E : }
137 :
138 E : TEST_F(InstrumenterWithAgentTest, ParseWithNoAgentDllFails) {
139 E : cmd_line_.AppendSwitchPath("input-image", input_pe_image_path_);
140 E : cmd_line_.AppendSwitchPath("output-image", output_pe_image_path_);
141 :
142 E : TestInstrumenterWithAgent instrumenter; // No default DLL.
143 E : EXPECT_FALSE(instrumenter.ParseCommandLine(&cmd_line_));
144 E : }
145 :
146 E : TEST_F(InstrumenterWithAgentTest, ParseInputRequired) {
147 E : cmd_line_.AppendSwitchPath("input-image", input_pe_image_path_);
148 E : cmd_line_.AppendSwitchPath("output-image", output_pe_image_path_);
149 E : cmd_line_.AppendSwitchASCII("agent", kTestAgentDllName);
150 :
151 E : TestInstrumenterWithAgent instrumenter; // No default DLL.
152 E : EXPECT_TRUE(instrumenter.ParseCommandLine(&cmd_line_));
153 E : EXPECT_EQ(abs_input_pe_image_path_, instrumenter.input_image_path_);
154 E : EXPECT_EQ(output_pe_image_path_, instrumenter.output_image_path_);
155 E : EXPECT_EQ(kTestAgentDllName, instrumenter.agent_dll());
156 E : }
157 :
158 : } // namespace instrumenters
159 : } // namespace instrument
|