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/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 :
23 : namespace {
24 :
25 : class TestCoverageGrinder : public CoverageGrinder {
26 : public:
27 : using CoverageGrinder::parser_;
28 : };
29 :
30 : class CoverageGrinderTest : public testing::PELibUnitTest {
31 : public:
32 : typedef testing::PELibUnitTest Super;
33 :
34 E : CoverageGrinderTest() : cmd_line_(FilePath(L"coverage_grinder.exe")) {
35 E : }
36 :
37 E : virtual void SetUp() OVERRIDE {
38 E : Super::Test::SetUp();
39 E : }
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 : 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 : file_util::ScopedFILE output_file(
66 E : file_util::OpenFile(output_path.path(), "wb"));
67 E : ASSERT_TRUE(output_file.get() != NULL);
68 :
69 E : EXPECT_TRUE(grinder.OutputData(output_file.get()));
70 E : output_file.reset();
71 :
72 E : int64 cache_grind_file_size = 0;
73 E : ASSERT_TRUE(file_util::GetFileSize(output_path.path(),
74 : &cache_grind_file_size));
75 E : EXPECT_LT(0u, cache_grind_file_size);
76 E : }
77 :
78 : CommandLine cmd_line_;
79 : trace::parser::Parser parser_;
80 : };
81 :
82 : } // namespace
83 :
84 E : TEST_F(CoverageGrinderTest, ParseEmptyCommandLineSucceeds) {
85 E : TestCoverageGrinder grinder;
86 E : EXPECT_TRUE(grinder.ParseCommandLine(&cmd_line_));
87 E : EXPECT_EQ(CoverageGrinder::kLcovFormat, grinder.output_format());
88 E : }
89 :
90 E : TEST_F(CoverageGrinderTest, ParseInvalidOutputFormatFails) {
91 E : TestCoverageGrinder grinder;
92 E : cmd_line_.AppendSwitchASCII("output-format", "foobar");
93 E : EXPECT_FALSE(grinder.ParseCommandLine(&cmd_line_));
94 E : }
95 :
96 E : TEST_F(CoverageGrinderTest, SetParserSucceeds) {
97 E : TestCoverageGrinder grinder;
98 :
99 E : grinder.ParseCommandLine(&cmd_line_);
100 :
101 E : ASSERT_NO_FATAL_FAILURE(InitParser(&grinder));
102 :
103 E : grinder.SetParser(&parser_);
104 E : EXPECT_EQ(&parser_, grinder.parser_);
105 E : }
106 :
107 E : TEST_F(CoverageGrinderTest, GrindFailsOnNoCoverageEvents) {
108 E : TestCoverageGrinder grinder;
109 :
110 E : grinder.ParseCommandLine(&cmd_line_);
111 :
112 E : ASSERT_NO_FATAL_FAILURE(InitParser(&grinder));
113 E : grinder.SetParser(&parser_);
114 :
115 E : EXPECT_FALSE(grinder.Grind());
116 E : }
117 :
118 E : TEST_F(CoverageGrinderTest, GrindAndOutputLcovDataSucceeds) {
119 E : cmd_line_.AppendSwitchASCII("output-format", "lcov");
120 E : ASSERT_NO_FATAL_FAILURE(GrindAndOutputSucceeds(CoverageGrinder::kLcovFormat));
121 : // TODO(chrisha): Validate the output is a valid LCOV file.
122 E : }
123 :
124 E : TEST_F(CoverageGrinderTest, GrindAndOutputCacheGrindDataSucceeds) {
125 E : cmd_line_.AppendSwitchASCII("output-format", "cachegrind");
126 : ASSERT_NO_FATAL_FAILURE(GrindAndOutputSucceeds(
127 E : CoverageGrinder::kCacheGrindFormat));
128 : // TODO(chrisha): Validate the output is a valid CacheGrind file.
129 E : }
130 :
131 : } // namespace grinder
|