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