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/coverage_instrumenter.h"
16 :
17 : #include "base/command_line.h"
18 : #include "gtest/gtest.h"
19 : #include "syzygy/core/unittest_util.h"
20 : #include "syzygy/pe/unittest_util.h"
21 :
22 : namespace instrument {
23 : namespace instrumenters {
24 :
25 : namespace {
26 :
27 : class TestCoverageInstrumenter : public CoverageInstrumenter {
28 : public:
29 : using CoverageInstrumenter::agent_dll_;
30 : using CoverageInstrumenter::input_dll_path_;
31 : using CoverageInstrumenter::input_pdb_path_;
32 : using CoverageInstrumenter::output_dll_path_;
33 : using CoverageInstrumenter::output_pdb_path_;
34 : using CoverageInstrumenter::allow_overwrite_;
35 : using CoverageInstrumenter::new_decomposer_;
36 : using CoverageInstrumenter::no_augment_pdb_;
37 : using CoverageInstrumenter::no_parse_debug_info_;
38 : using CoverageInstrumenter::no_strip_strings_;
39 : using CoverageInstrumenter::debug_friendly_;
40 : using CoverageInstrumenter::kAgentDllCoverage;
41 : };
42 :
43 : class CoverageInstrumenterTest : public testing::PELibUnitTest {
44 : public:
45 : typedef testing::PELibUnitTest Super;
46 :
47 E : CoverageInstrumenterTest()
48 : : cmd_line_(base::FilePath(L"instrument.exe")) {
49 E : }
50 :
51 E : virtual void SetUp() OVERRIDE {
52 E : testing::Test::SetUp();
53 :
54 : // Several of the tests generate progress and (deliberate) error messages
55 : // that would otherwise clutter the unittest output.
56 E : logging::SetMinLogLevel(logging::LOG_FATAL);
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 (potential) input and output path values.
66 E : abs_input_dll_path_ = testing::GetExeRelativePath(testing::kTestDllName);
67 E : input_dll_path_ = testing::GetRelativePath(abs_input_dll_path_);
68 E : abs_input_pdb_path_ = testing::GetExeRelativePath(testing::kTestDllPdbName);
69 E : input_pdb_path_ = testing::GetRelativePath(abs_input_pdb_path_);
70 E : output_dll_path_ = temp_dir_.Append(input_dll_path_.BaseName());
71 E : output_pdb_path_ = temp_dir_.Append(input_pdb_path_.BaseName());
72 E : }
73 :
74 E : void SetUpValidCommandLine() {
75 E : cmd_line_.AppendSwitchPath("input-dll", input_dll_path_);
76 E : cmd_line_.AppendSwitchPath("output-dll", output_dll_path_);
77 E : }
78 :
79 : protected:
80 : base::FilePath temp_dir_;
81 :
82 : // @name The redirected streams paths.
83 : // @{
84 : base::FilePath stdin_path_;
85 : base::FilePath stdout_path_;
86 : base::FilePath stderr_path_;
87 : // @}
88 :
89 : // @name Command-line and parameters.
90 : // @{
91 : CommandLine cmd_line_;
92 : base::FilePath input_dll_path_;
93 : base::FilePath input_pdb_path_;
94 : base::FilePath output_dll_path_;
95 : base::FilePath output_pdb_path_;
96 : // @}
97 :
98 : // @name Expected final values of input parameters.
99 : // @{
100 : base::FilePath abs_input_dll_path_;
101 : base::FilePath abs_input_pdb_path_;
102 : // @}
103 :
104 : // The fake instrumenter we delegate to.
105 : TestCoverageInstrumenter instrumenter_;
106 : };
107 :
108 : } // namespace
109 :
110 E : TEST_F(CoverageInstrumenterTest, ParseMinimalCoverage) {
111 E : SetUpValidCommandLine();
112 :
113 E : EXPECT_TRUE(instrumenter_.ParseCommandLine(&cmd_line_));
114 :
115 E : EXPECT_EQ(abs_input_dll_path_, instrumenter_.input_dll_path_);
116 E : EXPECT_EQ(output_dll_path_, instrumenter_.output_dll_path_);
117 : EXPECT_EQ(std::string(TestCoverageInstrumenter::kAgentDllCoverage),
118 E : instrumenter_.agent_dll_);
119 E : EXPECT_FALSE(instrumenter_.allow_overwrite_);
120 E : EXPECT_FALSE(instrumenter_.new_decomposer_);
121 E : EXPECT_FALSE(instrumenter_.no_augment_pdb_);
122 E : EXPECT_FALSE(instrumenter_.no_parse_debug_info_);
123 E : EXPECT_FALSE(instrumenter_.no_strip_strings_);
124 E : EXPECT_FALSE(instrumenter_.debug_friendly_);
125 E : }
126 :
127 E : TEST_F(CoverageInstrumenterTest, ParseFullCoverage) {
128 E : SetUpValidCommandLine();
129 E : cmd_line_.AppendSwitchASCII("agent", "foo.dll");
130 E : cmd_line_.AppendSwitch("debug-friendly");
131 E : cmd_line_.AppendSwitchPath("input-pdb", input_pdb_path_);
132 E : cmd_line_.AppendSwitch("new-decomposer");
133 E : cmd_line_.AppendSwitch("no-augment-pdb");
134 E : cmd_line_.AppendSwitch("no-parse-debug-info");
135 E : cmd_line_.AppendSwitch("no-strip-strings");
136 E : cmd_line_.AppendSwitchPath("output-pdb", output_pdb_path_);
137 E : cmd_line_.AppendSwitch("overwrite");
138 :
139 E : EXPECT_TRUE(instrumenter_.ParseCommandLine(&cmd_line_));
140 :
141 E : EXPECT_EQ(abs_input_dll_path_, instrumenter_.input_dll_path_);
142 E : EXPECT_EQ(output_dll_path_, instrumenter_.output_dll_path_);
143 E : EXPECT_EQ(abs_input_pdb_path_, instrumenter_.input_pdb_path_);
144 E : EXPECT_EQ(output_pdb_path_, instrumenter_.output_pdb_path_);
145 E : EXPECT_EQ(std::string("foo.dll"), instrumenter_.agent_dll_);
146 E : EXPECT_TRUE(instrumenter_.allow_overwrite_);
147 E : EXPECT_TRUE(instrumenter_.new_decomposer_);
148 E : EXPECT_TRUE(instrumenter_.no_augment_pdb_);
149 E : EXPECT_TRUE(instrumenter_.no_parse_debug_info_);
150 E : EXPECT_TRUE(instrumenter_.no_strip_strings_);
151 E : EXPECT_TRUE(instrumenter_.debug_friendly_);
152 E : }
153 :
154 E : TEST_F(CoverageInstrumenterTest, InstrumentImpl) {
155 E : SetUpValidCommandLine();
156 :
157 E : EXPECT_TRUE(instrumenter_.ParseCommandLine(&cmd_line_));
158 E : EXPECT_TRUE(instrumenter_.Instrument());
159 E : }
160 : } // namespace instrumenters
161 : } // namespace instrument
|