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/grinders/coverage_grinder.h"
16 :
17 : #include "base/win/scoped_com_initializer.h"
18 : #include "gtest/gtest.h"
19 : #include "syzygy/core/unittest_util.h"
20 : #include "syzygy/pe/unittest_util.h"
21 :
22 : namespace grinder {
23 : namespace grinders {
24 :
25 : namespace {
26 :
27 : class TestCoverageGrinder : public CoverageGrinder {
28 : public:
29 : using CoverageGrinder::parser_;
30 : };
31 :
32 : class CoverageGrinderTest : public testing::PELibUnitTest {
33 : public:
34 : typedef testing::PELibUnitTest Super;
35 :
36 E : CoverageGrinderTest() : cmd_line_(base::FilePath(L"coverage_grinder.exe")) {
37 E : }
38 :
39 E : virtual void SetUp() override { Super::Test::SetUp(); }
40 :
41 E : void InitParser(trace::parser::ParseEventHandlerImpl* handler) {
42 E : ASSERT_TRUE(handler != NULL);
43 :
44 E : ASSERT_TRUE(parser_.Init(handler));
45 :
46 : base::FilePath trace_file =
47 E : testing::GetExeTestDataRelativePath(testing::kCoverageTraceFiles[0]);
48 :
49 E : ASSERT_TRUE(parser_.OpenTraceFile(trace_file));
50 E : }
51 :
52 : void GrindAndOutputSucceeds(
53 E : CoverageGrinder::OutputFormat expected_output_format) {
54 E : TestCoverageGrinder grinder;
55 E : grinder.ParseCommandLine(&cmd_line_);
56 E : EXPECT_EQ(expected_output_format, grinder.output_format());
57 :
58 E : ASSERT_NO_FATAL_FAILURE(InitParser(&grinder));
59 E : grinder.SetParser(&parser_);
60 E : ASSERT_TRUE(parser_.Consume());
61 :
62 E : EXPECT_TRUE(grinder.Grind());
63 :
64 E : testing::ScopedTempFile output_path;
65 E : base::ScopedFILE output_file(base::OpenFile(output_path.path(), "wb"));
66 E : ASSERT_TRUE(output_file.get() != NULL);
67 :
68 E : EXPECT_TRUE(grinder.OutputData(output_file.get()));
69 E : output_file.reset();
70 :
71 E : int64 cache_grind_file_size = 0;
72 E : ASSERT_TRUE(base::GetFileSize(output_path.path(), &cache_grind_file_size));
73 E : EXPECT_LT(0u, cache_grind_file_size);
74 E : }
75 :
76 : // Ensures that COM is initialized for tests in this fixture.
77 : base::win::ScopedCOMInitializer com_initializer_;
78 :
79 : base::CommandLine cmd_line_;
80 : trace::parser::Parser parser_;
81 : };
82 :
83 : } // namespace
84 :
85 E : TEST_F(CoverageGrinderTest, ParseEmptyCommandLineSucceeds) {
86 E : TestCoverageGrinder grinder;
87 E : EXPECT_TRUE(grinder.ParseCommandLine(&cmd_line_));
88 E : EXPECT_EQ(CoverageGrinder::kLcovFormat, grinder.output_format());
89 E : }
90 :
91 E : TEST_F(CoverageGrinderTest, ParseInvalidOutputFormatFails) {
92 E : TestCoverageGrinder grinder;
93 E : cmd_line_.AppendSwitchASCII("output-format", "foobar");
94 E : EXPECT_FALSE(grinder.ParseCommandLine(&cmd_line_));
95 E : }
96 :
97 E : TEST_F(CoverageGrinderTest, SetParserSucceeds) {
98 E : TestCoverageGrinder grinder;
99 :
100 E : grinder.ParseCommandLine(&cmd_line_);
101 :
102 E : ASSERT_NO_FATAL_FAILURE(InitParser(&grinder));
103 :
104 E : grinder.SetParser(&parser_);
105 E : EXPECT_EQ(&parser_, grinder.parser_);
106 E : }
107 :
108 E : TEST_F(CoverageGrinderTest, GrindFailsOnNoCoverageEvents) {
109 E : TestCoverageGrinder grinder;
110 :
111 E : grinder.ParseCommandLine(&cmd_line_);
112 :
113 E : ASSERT_NO_FATAL_FAILURE(InitParser(&grinder));
114 E : grinder.SetParser(&parser_);
115 :
116 E : EXPECT_FALSE(grinder.Grind());
117 E : }
118 :
119 E : TEST_F(CoverageGrinderTest, GrindAndOutputLcovDataSucceeds) {
120 E : cmd_line_.AppendSwitchASCII("output-format", "lcov");
121 E : ASSERT_NO_FATAL_FAILURE(GrindAndOutputSucceeds(CoverageGrinder::kLcovFormat));
122 : // TODO(chrisha): Validate the output is a valid LCOV file.
123 E : }
124 :
125 E : TEST_F(CoverageGrinderTest, GrindAndOutputCacheGrindDataSucceeds) {
126 E : cmd_line_.AppendSwitchASCII("output-format", "cachegrind");
127 : ASSERT_NO_FATAL_FAILURE(GrindAndOutputSucceeds(
128 E : CoverageGrinder::kCacheGrindFormat));
129 : // TODO(chrisha): Validate the output is a valid CacheGrind file.
130 E : }
131 :
132 : } // namespace grinders
133 : } // namespace grinder
|