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 {
40 E : Super::Test::SetUp();
41 E : }
42 :
43 E : void InitParser(trace::parser::ParseEventHandlerImpl* handler) {
44 E : ASSERT_TRUE(handler != NULL);
45 :
46 E : ASSERT_TRUE(parser_.Init(handler));
47 :
48 : base::FilePath trace_file =
49 E : testing::GetExeTestDataRelativePath(testing::kCoverageTraceFiles[0]);
50 :
51 E : ASSERT_TRUE(parser_.OpenTraceFile(trace_file));
52 E : }
53 :
54 : void GrindAndOutputSucceeds(
55 E : CoverageGrinder::OutputFormat expected_output_format) {
56 E : TestCoverageGrinder grinder;
57 E : grinder.ParseCommandLine(&cmd_line_);
58 E : EXPECT_EQ(expected_output_format, grinder.output_format());
59 :
60 E : ASSERT_NO_FATAL_FAILURE(InitParser(&grinder));
61 E : grinder.SetParser(&parser_);
62 E : ASSERT_TRUE(parser_.Consume());
63 :
64 E : EXPECT_TRUE(grinder.Grind());
65 :
66 E : testing::ScopedTempFile output_path;
67 E : base::ScopedFILE output_file(base::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(base::GetFileSize(output_path.path(), &cache_grind_file_size));
75 E : EXPECT_LT(0u, cache_grind_file_size);
76 E : }
77 :
78 : // Ensures that COM is initialized for tests in this fixture.
79 : base::win::ScopedCOMInitializer com_initializer_;
80 :
81 : CommandLine cmd_line_;
82 : trace::parser::Parser parser_;
83 : };
84 :
85 : } // namespace
86 :
87 E : TEST_F(CoverageGrinderTest, ParseEmptyCommandLineSucceeds) {
88 E : TestCoverageGrinder grinder;
89 E : EXPECT_TRUE(grinder.ParseCommandLine(&cmd_line_));
90 E : EXPECT_EQ(CoverageGrinder::kLcovFormat, grinder.output_format());
91 E : }
92 :
93 E : TEST_F(CoverageGrinderTest, ParseInvalidOutputFormatFails) {
94 E : TestCoverageGrinder grinder;
95 E : cmd_line_.AppendSwitchASCII("output-format", "foobar");
96 E : EXPECT_FALSE(grinder.ParseCommandLine(&cmd_line_));
97 E : }
98 :
99 E : TEST_F(CoverageGrinderTest, SetParserSucceeds) {
100 E : TestCoverageGrinder grinder;
101 :
102 E : grinder.ParseCommandLine(&cmd_line_);
103 :
104 E : ASSERT_NO_FATAL_FAILURE(InitParser(&grinder));
105 :
106 E : grinder.SetParser(&parser_);
107 E : EXPECT_EQ(&parser_, grinder.parser_);
108 E : }
109 :
110 E : TEST_F(CoverageGrinderTest, GrindFailsOnNoCoverageEvents) {
111 E : TestCoverageGrinder grinder;
112 :
113 E : grinder.ParseCommandLine(&cmd_line_);
114 :
115 E : ASSERT_NO_FATAL_FAILURE(InitParser(&grinder));
116 E : grinder.SetParser(&parser_);
117 :
118 E : EXPECT_FALSE(grinder.Grind());
119 E : }
120 :
121 E : TEST_F(CoverageGrinderTest, GrindAndOutputLcovDataSucceeds) {
122 E : cmd_line_.AppendSwitchASCII("output-format", "lcov");
123 E : ASSERT_NO_FATAL_FAILURE(GrindAndOutputSucceeds(CoverageGrinder::kLcovFormat));
124 : // TODO(chrisha): Validate the output is a valid LCOV file.
125 E : }
126 :
127 E : TEST_F(CoverageGrinderTest, GrindAndOutputCacheGrindDataSucceeds) {
128 E : cmd_line_.AppendSwitchASCII("output-format", "cachegrind");
129 : ASSERT_NO_FATAL_FAILURE(GrindAndOutputSucceeds(
130 E : CoverageGrinder::kCacheGrindFormat));
131 : // TODO(chrisha): Validate the output is a valid CacheGrind file.
132 E : }
133 :
134 : } // namespace grinders
135 : } // namespace grinder
|