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 : // This class tests that Simulator calls the functions of its respective
16 : // SimulationEventHandler if called with the test DLL.
17 :
18 : #include "syzygy/simulate/simulator.h"
19 :
20 : #include "gmock/gmock.h"
21 : #include "syzygy/core/unittest_util.h"
22 : #include "syzygy/pe/unittest_util.h"
23 : #include "syzygy/version/syzygy_version.h"
24 :
25 : namespace simulate {
26 :
27 : namespace {
28 :
29 : using testing::_;
30 : using testing::AtLeast;
31 : using testing::GetExeTestDataRelativePath;
32 : using testing::Gt;
33 :
34 : class MockSimulationEventHandler : public SimulationEventHandler {
35 : public:
36 : MOCK_METHOD2(OnProcessStarted, void(base::Time time,
37 E : size_t default_page_size));
38 :
39 : MOCK_METHOD2(
40 : OnFunctionEntry,
41 E : void(base::Time time, const block_graph::BlockGraph::Block* block));
42 :
43 E : MOCK_METHOD2(SerializeToJSON, bool (FILE* output, bool pretty_print));
44 : };
45 :
46 : class SimulatorTest : public testing::PELibUnitTest {
47 : public:
48 : typedef std::vector<base::FilePath> TraceFileList;
49 :
50 E : void InitTraceFileList() {
51 : const base::FilePath trace_files_initializer[] = {
52 : GetExeTestDataRelativePath(testing::kCallTraceTraceFiles[0]),
53 : GetExeTestDataRelativePath(testing::kCallTraceTraceFiles[1]),
54 : GetExeTestDataRelativePath(testing::kCallTraceTraceFiles[2]),
55 : GetExeTestDataRelativePath(testing::kCallTraceTraceFiles[3]),
56 E : };
57 :
58 : trace_files_ = TraceFileList(trace_files_initializer,
59 E : trace_files_initializer + arraysize(trace_files_initializer));
60 E : }
61 :
62 E : void InitSimulator() {
63 E : module_path_ = GetExeTestDataRelativePath(testing::kTestDllName);
64 : instrumented_path_ = GetExeTestDataRelativePath(
65 E : testing::kCallTraceInstrumentedTestDllName);
66 :
67 : simulator_.reset(new Simulator(module_path_,
68 : instrumented_path_,
69 : trace_files_,
70 E : &simulation_event_handler_));
71 E : ASSERT_TRUE(simulator_.get() != NULL);
72 E : }
73 :
74 : protected:
75 : base::FilePath module_path_;
76 : base::FilePath instrumented_path_;
77 : TraceFileList trace_files_;
78 : testing::StrictMock<MockSimulationEventHandler> simulation_event_handler_;
79 :
80 : scoped_ptr<Simulator> simulator_;
81 : };
82 :
83 : } // namespace
84 :
85 E : TEST_F(SimulatorTest, SuccesfulRead) {
86 E : ASSERT_NO_FATAL_FAILURE(InitTraceFileList());
87 E : ASSERT_NO_FATAL_FAILURE(InitSimulator());
88 :
89 : // SerializeToJSON shouldn't be called by Simulator.
90 E : EXPECT_CALL(simulation_event_handler_, SerializeToJSON(_, _)).Times(0);
91 :
92 : // We know that since each of the test trace files contains a single process,
93 : // OnProcessStarted will be called exactly 4 times. Also, since they are
94 : // RPC-instrumented trace files we will know the value of the page size, so
95 : // it will be called with an argument greater than 0.
96 E : EXPECT_CALL(simulation_event_handler_, OnProcessStarted(_, Gt(0u))).Times(4);
97 :
98 : // We don't have that much information about OnFunctionEntry events, but at
99 : // least know they should happen.
100 : EXPECT_CALL(simulation_event_handler_,
101 E : OnFunctionEntry(_, _)).Times(AtLeast(1));
102 :
103 E : ASSERT_TRUE(simulator_->ParseTraceFiles());
104 E : }
105 :
106 : } // namespace simulate
|