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