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/bard/events/heap_set_information_event.h"
16 :
17 : #include "gmock/gmock.h"
18 : #include "gtest/gtest.h"
19 : #include "syzygy/bard/unittest_util.h"
20 : #include "syzygy/bard/backdrops/heap_backdrop.h"
21 :
22 : namespace bard {
23 : namespace events {
24 :
25 : namespace {
26 :
27 : using bard::backdrops::HeapBackdrop;
28 : using bard::events::HeapSetInformationEvent;
29 : using testing::Return;
30 :
31 : const HANDLE kLiveHeap = reinterpret_cast<HANDLE>(0x4197FC83);
32 : const HANDLE kTraceHeap = reinterpret_cast<HANDLE>(0xAB12CD34);
33 : const LPVOID kLiveAlloc = reinterpret_cast<LPVOID>(0x4820BC7A);
34 : const LPVOID kTraceAlloc = reinterpret_cast<LPVOID>(0xF1D97AE4);
35 : const HEAP_INFORMATION_CLASS kInfoClass =
36 : static_cast<HEAP_INFORMATION_CLASS>(0);
37 : const PVOID kInfo = reinterpret_cast<PVOID>(0x12345678);
38 : const SIZE_T kInfoLength = 100;
39 :
40 : class HeapSetInformationEventTest : public testing::Test {
41 : public:
42 E : MOCK_METHOD4(FakeCall, BOOL(HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T));
43 :
44 E : void SetUp() override {
45 E : backdrop_.heap_map().AddMapping(kTraceHeap, kLiveHeap);
46 E : backdrop_.alloc_map().AddMapping(kTraceAlloc, kLiveAlloc);
47 :
48 : backdrop_.set_heap_set_information(base::Bind(
49 E : &HeapSetInformationEventTest::FakeCall, base::Unretained(this)));
50 E : }
51 :
52 : protected:
53 : HeapBackdrop backdrop_;
54 : };
55 :
56 : } // namespace
57 :
58 E : TEST_F(HeapSetInformationEventTest, TestSuccessCall) {
59 : HeapSetInformationEvent heap_set_information_event(
60 E : kTraceHeap, kInfoClass, kInfo, kInfoLength, true);
61 : EXPECT_CALL(*this, FakeCall(kLiveHeap, kInfoClass, kInfo, kInfoLength))
62 E : .WillOnce(Return(true));
63 :
64 : EXPECT_TRUE(
65 E : heap_set_information_event.Play(reinterpret_cast<void*>(&backdrop_)));
66 E : }
67 :
68 E : TEST_F(HeapSetInformationEventTest, TestFailCall) {
69 : HeapSetInformationEvent heap_set_information_event(
70 E : kTraceHeap, kInfoClass, kInfo, kInfoLength, false);
71 : EXPECT_CALL(*this, FakeCall(kLiveHeap, kInfoClass, kInfo, kInfoLength))
72 E : .WillOnce(Return(false));
73 :
74 : EXPECT_TRUE(
75 E : heap_set_information_event.Play(reinterpret_cast<void*>(&backdrop_)));
76 E : }
77 :
78 E : TEST_F(HeapSetInformationEventTest, TestInconsistentReturn) {
79 : HeapSetInformationEvent heap_set_information_event(
80 E : kTraceHeap, kInfoClass, kInfo, kInfoLength, false);
81 : EXPECT_CALL(*this, FakeCall(kLiveHeap, kInfoClass, kInfo, kInfoLength))
82 E : .WillOnce(Return(true));
83 :
84 : EXPECT_FALSE(
85 E : heap_set_information_event.Play(reinterpret_cast<void*>(&backdrop_)));
86 E : }
87 :
88 E : TEST_F(HeapSetInformationEventTest, Equals) {
89 E : HeapSetInformationEvent e1(kTraceHeap, kInfoClass, kInfo, kInfoLength, true);
90 E : HeapSetInformationEvent e2(kTraceHeap, kInfoClass, kInfo, kInfoLength, true);
91 E : HeapSetInformationEvent e3(kTraceHeap, kInfoClass, kInfo, kInfoLength, false);
92 E : EXPECT_TRUE(e1.Equals(&e1));
93 E : EXPECT_TRUE(e1.Equals(&e2));
94 E : EXPECT_FALSE(e1.Equals(&e3));
95 E : EXPECT_FALSE(e2.Equals(&e3));
96 E : }
97 :
98 E : TEST_F(HeapSetInformationEventTest, TestSerialization) {
99 : HeapSetInformationEvent heap_set_information_event(
100 E : kTraceHeap, kInfoClass, kInfo, kInfoLength, true);
101 E : testing::TestEventSerialization(heap_set_information_event);
102 E : }
103 :
104 : } // namespace events
105 : } // namespace bard
|