1 : // Copyright 2012 Google Inc.
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 : static const wchar_t kCoverageTraceFile[] = L"coverage_traces/trace-1.bin";
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_(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 : FilePath trace_file =
49 E : testing::GetExeTestDataRelativePath(kCoverageTraceFile);
50 :
51 E : ASSERT_TRUE(parser_.OpenTraceFile(trace_file));
52 E : }
53 :
54 : protected:
55 : CommandLine cmd_line_;
56 : trace::parser::Parser parser_;
57 : };
58 :
59 : } // namespace
60 :
61 E : TEST_F(CoverageGrinderTest, ParseCommandLineSucceeds) {
62 E : TestCoverageGrinder grinder;
63 E : EXPECT_TRUE(grinder.ParseCommandLine(&cmd_line_));
64 E : }
65 :
66 E : TEST_F(CoverageGrinderTest, SetParserSucceeds) {
67 E : TestCoverageGrinder grinder;
68 :
69 E : grinder.ParseCommandLine(&cmd_line_);
70 :
71 E : ASSERT_NO_FATAL_FAILURE(InitParser(&grinder));
72 :
73 E : grinder.SetParser(&parser_);
74 E : EXPECT_EQ(&parser_, grinder.parser_);
75 E : }
76 :
77 E : TEST_F(CoverageGrinderTest, GrindFailsOnNoCoverageEvents) {
78 E : TestCoverageGrinder grinder;
79 :
80 E : grinder.ParseCommandLine(&cmd_line_);
81 :
82 E : ASSERT_NO_FATAL_FAILURE(InitParser(&grinder));
83 E : grinder.SetParser(&parser_);
84 :
85 E : EXPECT_FALSE(grinder.Grind());
86 E : }
87 :
88 E : TEST_F(CoverageGrinderTest, GrindAndOutputDataSucceeds) {
89 E : TestCoverageGrinder grinder;
90 E : grinder.ParseCommandLine(&cmd_line_);
91 :
92 E : ASSERT_NO_FATAL_FAILURE(InitParser(&grinder));
93 E : grinder.SetParser(&parser_);
94 E : ASSERT_TRUE(parser_.Consume());
95 :
96 E : EXPECT_TRUE(grinder.Grind());
97 :
98 E : FilePath temp_dir;
99 E : ASSERT_NO_FATAL_FAILURE(CreateTemporaryDir(&temp_dir));
100 :
101 E : FilePath output_path;
102 : file_util::ScopedFILE output_file(
103 E : file_util::CreateAndOpenTemporaryFileInDir(temp_dir, &output_path));
104 E : ASSERT_TRUE(output_file.get() != NULL);
105 :
106 E : EXPECT_TRUE(grinder.OutputData(output_file.get()));
107 E : output_file.reset();
108 :
109 E : int64 lcov_file_size = 0;
110 E : ASSERT_TRUE(file_util::GetFileSize(output_path, &lcov_file_size));
111 E : EXPECT_LT(0u, lcov_file_size);
112 E : }
113 :
114 : } // namespace grinder
|