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/grinder/grinder_app.h"
16 :
17 : #include "base/files/scoped_temp_dir.h"
18 : #include "gtest/gtest.h"
19 : #include "syzygy/common/application.h"
20 : #include "syzygy/core/unittest_util.h"
21 : #include "syzygy/pe/unittest_util.h"
22 : #include "syzygy/sampler/unittest_util.h"
23 :
24 : namespace grinder {
25 :
26 : namespace {
27 :
28 : class TestGrinderApp : public GrinderApp {
29 : public:
30 : // Expose for testing.
31 : using GrinderApp::trace_files_;
32 : using GrinderApp::output_file_;
33 : };
34 :
35 : class GrinderAppTest : public testing::PELibUnitTest {
36 : public:
37 : typedef testing::PELibUnitTest Super;
38 : typedef common::Application<TestGrinderApp> TestApplication;
39 :
40 : GrinderAppTest()
41 : : cmd_line_(base::FilePath(L"grinder.exe")),
42 E : impl_(app_.implementation()) {
43 E : }
44 :
45 E : virtual void SetUp() OVERRIDE {
46 E : Super::SetUp();
47 :
48 : // Setup the IO streams.
49 E : ASSERT_NO_FATAL_FAILURE(CreateTemporaryDir(&temp_dir_));
50 E : stdin_path_ = temp_dir_.Append(L"NUL");
51 E : stdout_path_ = temp_dir_.Append(L"stdout.txt");
52 E : stderr_path_ = temp_dir_.Append(L"stderr.txt");
53 E : ASSERT_NO_FATAL_FAILURE(InitStreams(
54 : stdin_path_, stdout_path_, stderr_path_));
55 :
56 : // Point the application at the test's command-line and IO streams.
57 E : app_.set_command_line(&cmd_line_);
58 E : app_.set_in(in());
59 E : app_.set_out(out());
60 E : app_.set_err(err());
61 E : }
62 :
63 : protected:
64 : // The command line to be given to the application under test.
65 : CommandLine cmd_line_;
66 :
67 : // The application object under test.
68 : TestApplication app_;
69 :
70 : // A reference to the underlying application implementation for convenience.
71 : TestGrinderApp& impl_;
72 :
73 : // A temporary folder where all IO will be stored.
74 : base::FilePath temp_dir_;
75 :
76 : // @name File paths used for the standard IO streams.
77 : // @{
78 : base::FilePath stdin_path_;
79 : base::FilePath stdout_path_;
80 : base::FilePath stderr_path_;
81 : // @}
82 : };
83 :
84 : } // namespace
85 :
86 E : TEST_F(GrinderAppTest, ParseCommandLineFailsWithNoMode) {
87 E : cmd_line_.AppendArgPath(base::FilePath(L"foo.dat"));
88 E : ASSERT_FALSE(impl_.ParseCommandLine(&cmd_line_));
89 E : }
90 :
91 E : TEST_F(GrinderAppTest, ParseCommandLineFailsWithNoFiles) {
92 E : cmd_line_.AppendSwitchASCII("mode", "profile");
93 E : ASSERT_FALSE(impl_.ParseCommandLine(&cmd_line_));
94 E : }
95 :
96 E : TEST_F(GrinderAppTest, ParseCommandLineTraceFiles) {
97 E : std::vector<base::FilePath> temp_files;
98 E : cmd_line_.AppendSwitchASCII("mode", "profile");
99 E : for (size_t i = 0; i < 10; ++i) {
100 E : base::FilePath temp_file;
101 E : ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_, &temp_file));
102 E : cmd_line_.AppendArgPath(temp_file);
103 E : temp_files.push_back(temp_file);
104 E : }
105 :
106 E : ASSERT_TRUE(impl_.ParseCommandLine(&cmd_line_));
107 E : ASSERT_EQ(temp_files, impl_.trace_files_);
108 E : }
109 :
110 E : TEST_F(GrinderAppTest, ParseCommandLineOutputFile) {
111 E : ASSERT_TRUE(impl_.output_file_.empty());
112 E : cmd_line_.AppendSwitchASCII("mode", "profile");
113 E : cmd_line_.AppendSwitchPath("output-file", base::FilePath(L"output.txt"));
114 : cmd_line_.AppendArgPath(testing::GetExeTestDataRelativePath(
115 E : testing::kProfileTraceFiles[0]));
116 :
117 E : ASSERT_TRUE(impl_.ParseCommandLine(&cmd_line_));
118 E : ASSERT_EQ(L"output.txt", impl_.output_file_.value());
119 E : }
120 :
121 E : TEST_F(GrinderAppTest, BasicBlockEntryEndToEnd) {
122 E : cmd_line_.AppendSwitchASCII("mode", "bbentry");
123 : cmd_line_.AppendArgPath(testing::GetExeTestDataRelativePath(
124 E : testing::kBBEntryTraceFiles[0]));
125 :
126 E : base::FilePath output_file;
127 E : ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_, &output_file));
128 E : ASSERT_TRUE(file_util::Delete(output_file, false));
129 E : cmd_line_.AppendSwitchPath("output-file", output_file);
130 :
131 E : ASSERT_TRUE(!file_util::PathExists(output_file));
132 :
133 E : EXPECT_EQ(0, app_.Run());
134 :
135 : // Verify that the output file was created.
136 E : EXPECT_TRUE(file_util::PathExists(output_file));
137 E : }
138 :
139 E : TEST_F(GrinderAppTest, ProfileEndToEnd) {
140 E : cmd_line_.AppendSwitchASCII("mode", "profile");
141 : cmd_line_.AppendArgPath(testing::GetExeTestDataRelativePath(
142 E : testing::kProfileTraceFiles[0]));
143 :
144 E : base::FilePath output_file;
145 E : ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_, &output_file));
146 E : ASSERT_TRUE(file_util::Delete(output_file, false));
147 E : cmd_line_.AppendSwitchPath("output-file", output_file);
148 :
149 E : ASSERT_TRUE(!file_util::PathExists(output_file));
150 :
151 E : EXPECT_EQ(0, app_.Run());
152 :
153 : // Verify that the output file was created.
154 E : EXPECT_TRUE(file_util::PathExists(output_file));
155 E : }
156 :
157 E : TEST_F(GrinderAppTest, CoverageEndToEnd) {
158 E : cmd_line_.AppendSwitchASCII("mode", "coverage");
159 : cmd_line_.AppendArgPath(testing::GetExeTestDataRelativePath(
160 E : testing::kCoverageTraceFiles[0]));
161 :
162 E : base::FilePath output_file;
163 E : ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_, &output_file));
164 E : ASSERT_TRUE(file_util::Delete(output_file, false));
165 E : cmd_line_.AppendSwitchPath("output-file", output_file);
166 E : cmd_line_.AppendSwitchASCII("output-format", "lcov");
167 :
168 E : ASSERT_TRUE(!file_util::PathExists(output_file));
169 :
170 E : EXPECT_EQ(0, app_.Run());
171 :
172 : // Verify that the output file was created.
173 E : EXPECT_TRUE(file_util::PathExists(output_file));
174 E : }
175 :
176 E : TEST_F(GrinderAppTest, SampleEndToEnd) {
177 E : base::FilePath trace_file = temp_dir_.Append(L"sampler.bin");
178 E : ASSERT_NO_FATAL_FAILURE(testing::WriteDummySamplerTraceFile(trace_file));
179 E : ASSERT_TRUE(file_util::PathExists(trace_file));
180 :
181 E : cmd_line_.AppendSwitchASCII("mode", "sample");
182 : cmd_line_.AppendSwitchPath(
183 E : "image", testing::GetOutputRelativePath(testing::kTestDllName));
184 E : cmd_line_.AppendArgPath(trace_file);
185 :
186 E : base::FilePath output_file;
187 E : ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_, &output_file));
188 E : ASSERT_TRUE(file_util::Delete(output_file, false));
189 E : cmd_line_.AppendSwitchPath("output-file", output_file);
190 :
191 E : ASSERT_TRUE(!file_util::PathExists(output_file));
192 :
193 E : EXPECT_EQ(0, app_.Run());
194 :
195 : // Verify that the output file was created.
196 E : EXPECT_TRUE(file_util::PathExists(output_file));
197 E : }
198 :
199 : } // namespace grinder
|