1 : // Copyright 2015 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/refinery/analyzers/analysis_runner.h"
16 :
17 : #include "gmock/gmock.h"
18 : #include "gtest/gtest.h"
19 : #include "syzygy/minidump/minidump.h"
20 : #include "syzygy/refinery/analyzers/analyzer.h"
21 : #include "syzygy/refinery/analyzers/analyzer_util.h"
22 : #include "syzygy/refinery/process_state/process_state.h"
23 :
24 m : namespace refinery {
25 :
26 m : namespace {
27 :
28 m : using testing::_;
29 m : using testing::Return;
30 :
31 m : static const char kMockAnalyzerName[] = "MockAnalyzer";
32 :
33 m : class MockAnalyzer : public Analyzer {
34 m : public:
35 m : const char* name() const override { return kMockAnalyzerName; }
36 :
37 m : MOCK_METHOD2(Analyze,
38 m : AnalysisResult(const minidump::Minidump& minidump,
39 m : const ProcessAnalysis& process_analysis));
40 m : };
41 :
42 : // Creates a mock analyzer that has an expectation it will be called once.
43 m : MockAnalyzer* CreateMockAnalyzer(Analyzer::AnalysisResult result) {
44 m : MockAnalyzer* analyzer = new MockAnalyzer();
45 m : EXPECT_CALL(*analyzer, Analyze(_, _)).Times(1).WillOnce(Return(result));
46 m : return analyzer;
47 m : }
48 :
49 m : } // namespace
50 :
51 m : TEST(AnalysisRunnerTest, BasicSuccessTest) {
52 : // A runner with 2 analyzers that should run and succeed.
53 m : AnalysisRunner runner;
54 m : for (size_t i = 0; i < 2; ++i) {
55 m : std::unique_ptr<Analyzer> analyzer(
56 m : CreateMockAnalyzer(Analyzer::ANALYSIS_COMPLETE));
57 m : runner.AddAnalyzer(std::move(analyzer));
58 m : }
59 :
60 m : ProcessState process_state;
61 m : SimpleProcessAnalysis analysis(&process_state);
62 m : minidump::FileMinidump minidump;
63 :
64 : // Analyze.
65 m : ASSERT_EQ(Analyzer::ANALYSIS_COMPLETE, runner.Analyze(minidump, analysis));
66 m : }
67 :
68 m : TEST(AnalysisRunnerTest, BasicErrorTest) {
69 : // A runner with 1 analyzer that should run and return an error.
70 m : AnalysisRunner runner;
71 m : std::unique_ptr<Analyzer> analyzer(
72 m : CreateMockAnalyzer(Analyzer::ANALYSIS_ERROR));
73 m : runner.AddAnalyzer(std::move(analyzer));
74 :
75 m : ProcessState process_state;
76 m : SimpleProcessAnalysis analysis(&process_state);
77 m : minidump::FileMinidump minidump;
78 : // Analyze.
79 m : ASSERT_EQ(Analyzer::ANALYSIS_ERROR, runner.Analyze(minidump, analysis));
80 m : }
81 :
82 m : } // namespace refinery
|