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_create_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::HeapCreateEvent;
29 : using testing::Return;
30 :
31 : const HANDLE kLiveHeap = reinterpret_cast<HANDLE>(0x4197FC83);
32 : const HANDLE kTraceHeap = reinterpret_cast<HANDLE>(0xAB12CD34);
33 : const DWORD kOptions = 0;
34 : const SIZE_T kInitialSize = 1;
35 : const SIZE_T kMaximumSize = 1000;
36 :
37 : class HeapCreateEventTest : public testing::Test {
38 : public:
39 E : HeapCreateEventTest()
40 : : heap_create_event_(kOptions, kInitialSize, kMaximumSize, kTraceHeap) {}
41 :
42 E : MOCK_METHOD3(FakeCall, HANDLE(DWORD, SIZE_T, SIZE_T));
43 :
44 E : void SetUp() override {
45 : backdrop_.set_heap_create(
46 E : base::Bind(&HeapCreateEventTest::FakeCall, base::Unretained(this)));
47 E : }
48 :
49 : protected:
50 : HeapCreateEvent heap_create_event_;
51 : HeapBackdrop backdrop_;
52 : };
53 :
54 : } // namespace
55 :
56 E : TEST_F(HeapCreateEventTest, TestSuccessCall) {
57 : EXPECT_CALL(*this, FakeCall(kOptions, kInitialSize, kMaximumSize))
58 E : .WillOnce(Return(kLiveHeap));
59 :
60 E : EXPECT_TRUE(heap_create_event_.Play(reinterpret_cast<void*>(&backdrop_)));
61 :
62 : testing::CheckTraceLiveMapContains(backdrop_.heap_map(),
63 : kTraceHeap,
64 E : kLiveHeap);
65 E : }
66 :
67 E : TEST_F(HeapCreateEventTest, TestFailCall) {
68 : EXPECT_CALL(*this, FakeCall(kOptions, kInitialSize, kMaximumSize))
69 E : .WillOnce(Return(nullptr));
70 :
71 E : EXPECT_FALSE(heap_create_event_.Play(reinterpret_cast<void*>(&backdrop_)));
72 :
73 : testing::CheckTraceLiveMapNotContain(backdrop_.heap_map(),
74 : kTraceHeap,
75 E : kLiveHeap);
76 E : }
77 :
78 E : TEST_F(HeapCreateEventTest, Equals) {
79 E : HeapCreateEvent e1(kOptions, kInitialSize, kMaximumSize, kTraceHeap);
80 E : HeapCreateEvent e2(kOptions, kInitialSize, kMaximumSize, kTraceHeap);
81 E : HeapCreateEvent e3(kOptions, kInitialSize + 1, kMaximumSize + 1, kTraceHeap);
82 E : EXPECT_TRUE(e1.Equals(&e1));
83 E : EXPECT_TRUE(e1.Equals(&e2));
84 E : EXPECT_FALSE(e1.Equals(&e3));
85 E : EXPECT_FALSE(e2.Equals(&e3));
86 E : }
87 :
88 E : TEST_F(HeapCreateEventTest, TestSerialization) {
89 E : testing::TestEventSerialization(heap_create_event_);
90 E : }
91 :
92 : } // namespace events
93 : } // namespace bard
|