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 : // 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/common/syzygy_version.h"
22 : #include "syzygy/core/unittest_util.h"
23 : #include "syzygy/pe/unittest_util.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<FilePath> TraceFileList;
49 :
50 E : void InitTraceFileList() {
51 : const FilePath trace_files_initializer[] = {
52 E : GetExeTestDataRelativePath(L"rpc_traces/trace-1.bin"),
53 E : GetExeTestDataRelativePath(L"rpc_traces/trace-2.bin"),
54 E : GetExeTestDataRelativePath(L"rpc_traces/trace-3.bin"),
55 : GetExeTestDataRelativePath(L"rpc_traces/trace-4.bin")
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(kDllName);
64 E : instrumented_path_ = GetExeTestDataRelativePath(kRpcInstrumentedDllName);
65 :
66 : simulator_.reset(new Simulator(module_path_,
67 : instrumented_path_,
68 : trace_files_,
69 E : &simulation_event_handler_));
70 E : ASSERT_TRUE(simulator_.get() != NULL);
71 E : }
72 :
73 : protected:
74 : FilePath module_path_;
75 : FilePath instrumented_path_;
76 : TraceFileList trace_files_;
77 : testing::StrictMock<MockSimulationEventHandler> simulation_event_handler_;
78 :
79 : scoped_ptr<Simulator> simulator_;
80 : };
81 :
82 : } // namespace
83 :
84 E : TEST_F(SimulatorTest, SuccesfulRead) {
85 E : ASSERT_NO_FATAL_FAILURE(InitTraceFileList());
86 E : ASSERT_NO_FATAL_FAILURE(InitSimulator());
87 :
88 : // SerializeToJSON shouldn't be called by Simulator.
89 E : EXPECT_CALL(simulation_event_handler_, SerializeToJSON(_, _)).Times(0);
90 :
91 : // We know that since each of the test trace files contains a single process,
92 : // OnProcessStarted will be called exactly 4 times. Also, since they are
93 : // RPC-instrumented trace files we will know the value of the page size, so
94 : // it will be called with an argument greater than 0.
95 E : EXPECT_CALL(simulation_event_handler_, OnProcessStarted(_, Gt(0u))).Times(4);
96 :
97 : // We don't have that much information about OnFunctionEntry events, but at
98 : // least know they should happen.
99 : EXPECT_CALL(simulation_event_handler_,
100 E : OnFunctionEntry(_, _)).Times(AtLeast(1));
101 :
102 E : ASSERT_TRUE(simulator_->ParseTraceFiles());
103 E : }
104 :
105 : } //namespace simulate
|