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_realloc_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::HeapReAllocEvent;
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 LPVOID kTraceReAlloc = reinterpret_cast<LPVOID>(0x12345678);
36 : const LPVOID kLiveReAlloc = reinterpret_cast<LPVOID>(0x87654321);
37 : const DWORD kFlags = 0;
38 : const SIZE_T kBytes = 100;
39 :
40 : class HeapReAllocEventTest : public testing::Test {
41 : public:
42 : HeapReAllocEventTest()
43 E : : heap_realloc_event_(0,
44 : kTraceHeap,
45 : kFlags,
46 : kTraceAlloc,
47 : kBytes,
48 E : kTraceReAlloc) {}
49 :
50 E : MOCK_METHOD4(FakeCall, LPVOID(HANDLE, DWORD, LPVOID, SIZE_T));
51 :
52 E : void SetUp() override {
53 E : backdrop_.heap_map().AddMapping(kTraceHeap, kLiveHeap);
54 E : backdrop_.alloc_map().AddMapping(kTraceAlloc, kLiveAlloc);
55 :
56 E : backdrop_.set_heap_realloc(
57 : base::Bind(&HeapReAllocEventTest::FakeCall, base::Unretained(this)));
58 E : }
59 :
60 : protected:
61 : HeapReAllocEvent heap_realloc_event_;
62 : HeapBackdrop backdrop_;
63 : };
64 :
65 : } // namespace
66 :
67 E : TEST_F(HeapReAllocEventTest, TestSuccessCall) {
68 : EXPECT_CALL(*this, FakeCall(kLiveHeap, kFlags, kLiveAlloc, kBytes))
69 E : .WillOnce(Return(kLiveReAlloc));
70 :
71 E : EXPECT_TRUE(heap_realloc_event_.Play(reinterpret_cast<void*>(&backdrop_)));
72 :
73 E : testing::CheckTraceLiveMapNotContain(backdrop_.alloc_map(),
74 : kTraceAlloc,
75 : kLiveAlloc);
76 E : testing::CheckTraceLiveMapContains(backdrop_.alloc_map(),
77 : kTraceReAlloc,
78 : kLiveReAlloc);
79 E : }
80 :
81 E : TEST_F(HeapReAllocEventTest, TestFailCall) {
82 : EXPECT_CALL(*this, FakeCall(kLiveHeap, kFlags, kLiveAlloc, kBytes))
83 E : .WillOnce(Return(nullptr));
84 :
85 E : EXPECT_FALSE(heap_realloc_event_.Play(reinterpret_cast<void*>(&backdrop_)));
86 :
87 E : testing::CheckTraceLiveMapContains(backdrop_.alloc_map(),
88 : kTraceAlloc,
89 : kLiveAlloc);
90 E : testing::CheckTraceLiveMapNotContain(backdrop_.alloc_map(),
91 : kTraceReAlloc,
92 : kLiveReAlloc);
93 E : }
94 :
95 E : TEST_F(HeapReAllocEventTest, Equals) {
96 E : HeapReAllocEvent e1(0, kTraceHeap, kFlags, kTraceAlloc, kBytes,
97 : kTraceReAlloc);
98 E : HeapReAllocEvent e2(0, kTraceHeap, kFlags, kTraceAlloc, kBytes,
99 : kTraceReAlloc);
100 E : HeapReAllocEvent e3(0, kTraceHeap, kFlags + 1, kTraceAlloc, kBytes,
101 : kTraceReAlloc);
102 E : EXPECT_TRUE(e1.Equals(&e1));
103 E : EXPECT_TRUE(e1.Equals(&e2));
104 E : EXPECT_FALSE(e1.Equals(&e3));
105 E : EXPECT_FALSE(e2.Equals(&e3));
106 E : }
107 :
108 E : TEST_F(HeapReAllocEventTest, TestSerialization) {
109 E : testing::TestEventSerialization(heap_realloc_event_);
110 E : }
111 :
112 : } // namespace events
113 : } // namespace bard
|