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