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