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 "gtest/gtest.h"
18 : #include "syzygy/core/unittest_util.h"
19 : #include "syzygy/pe/unittest_util.h"
20 :
21 : namespace grinder {
22 : namespace grinders {
23 :
24 : namespace {
25 :
26 : class TestCoverageGrinder : public CoverageGrinder {
27 : public:
28 : using CoverageGrinder::parser_;
29 : };
30 :
31 : class CoverageGrinderTest : public testing::PELibUnitTest {
32 : public:
33 : typedef testing::PELibUnitTest Super;
34 :
35 E : CoverageGrinderTest() : cmd_line_(base::FilePath(L"coverage_grinder.exe")) {
36 E : }
37 :
38 E : virtual void SetUp() OVERRIDE {
39 E : Super::Test::SetUp();
40 E : }
41 :
42 E : void InitParser(trace::parser::ParseEventHandlerImpl* handler) {
43 E : ASSERT_TRUE(handler != NULL);
44 :
45 E : ASSERT_TRUE(parser_.Init(handler));
46 :
47 : base::FilePath trace_file =
48 E : testing::GetExeTestDataRelativePath(testing::kCoverageTraceFiles[0]);
49 :
50 E : ASSERT_TRUE(parser_.OpenTraceFile(trace_file));
51 E : }
52 :
53 : void GrindAndOutputSucceeds(
54 E : CoverageGrinder::OutputFormat expected_output_format) {
55 E : TestCoverageGrinder grinder;
56 E : grinder.ParseCommandLine(&cmd_line_);
57 E : EXPECT_EQ(expected_output_format, grinder.output_format());
58 :
59 E : ASSERT_NO_FATAL_FAILURE(InitParser(&grinder));
60 E : grinder.SetParser(&parser_);
61 E : ASSERT_TRUE(parser_.Consume());
62 :
63 E : EXPECT_TRUE(grinder.Grind());
64 :
65 E : testing::ScopedTempFile output_path;
66 : file_util::ScopedFILE output_file(
67 E : file_util::OpenFile(output_path.path(), "wb"));
68 E : ASSERT_TRUE(output_file.get() != NULL);
69 :
70 E : EXPECT_TRUE(grinder.OutputData(output_file.get()));
71 E : output_file.reset();
72 :
73 E : int64 cache_grind_file_size = 0;
74 E : ASSERT_TRUE(file_util::GetFileSize(output_path.path(),
75 : &cache_grind_file_size));
76 E : EXPECT_LT(0u, cache_grind_file_size);
77 E : }
78 :
79 : 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
|