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/event.h"
16 :
17 : #include "gmock/gmock.h"
18 : #include "gtest/gtest.h"
19 : #include "syzygy/bard/events/heap_alloc_event.h"
20 : #include "syzygy/bard/events/heap_create_event.h"
21 : #include "syzygy/bard/events/heap_destroy_event.h"
22 : #include "syzygy/bard/events/heap_free_event.h"
23 : #include "syzygy/bard/events/heap_realloc_event.h"
24 : #include "syzygy/bard/events/heap_set_information_event.h"
25 : #include "syzygy/bard/events/heap_size_event.h"
26 :
27 : namespace bard {
28 :
29 : // A template class for generating a test instance of an event. Specializations
30 : // for each event type are below.
31 : template <typename EventType>
32 : std::unique_ptr<EventInterface> CreateTestEvent();
33 :
34 : template <>
35 E : std::unique_ptr<EventInterface> CreateTestEvent<events::HeapAllocEvent>() {
36 E : return std::unique_ptr<EventInterface>(
37 : new events::HeapAllocEvent(0, reinterpret_cast<HANDLE>(0x1000), 0x1, 47,
38 : reinterpret_cast<LPVOID>(0x2000)));
39 E : }
40 :
41 : template <>
42 E : std::unique_ptr<EventInterface> CreateTestEvent<events::HeapCreateEvent>() {
43 E : return std::unique_ptr<EventInterface>(new events::HeapCreateEvent(
44 : 0, 0, 100, 200, reinterpret_cast<HANDLE>(0x1000)));
45 E : }
46 :
47 : template <>
48 E : std::unique_ptr<EventInterface> CreateTestEvent<events::HeapDestroyEvent>() {
49 E : return std::unique_ptr<EventInterface>(
50 : new events::HeapDestroyEvent(0, reinterpret_cast<HANDLE>(0x1000), true));
51 E : }
52 :
53 : template <>
54 E : std::unique_ptr<EventInterface> CreateTestEvent<events::HeapFreeEvent>() {
55 E : return std::unique_ptr<EventInterface>(
56 : new events::HeapFreeEvent(0, reinterpret_cast<HANDLE>(0x1000), 0,
57 : reinterpret_cast<LPVOID>(0x2000), true));
58 E : }
59 :
60 : template <>
61 E : std::unique_ptr<EventInterface> CreateTestEvent<events::HeapReAllocEvent>() {
62 E : return std::unique_ptr<EventInterface>(new events::HeapReAllocEvent(
63 : 0, reinterpret_cast<HANDLE>(0x1000), 0, reinterpret_cast<LPVOID>(0x2000),
64 : 123, reinterpret_cast<LPVOID>(0x3000)));
65 E : }
66 :
67 : template <>
68 : std::unique_ptr<EventInterface>
69 E : CreateTestEvent<events::HeapSetInformationEvent>() {
70 E : return std::unique_ptr<EventInterface>(new events::HeapSetInformationEvent(
71 : 0, reinterpret_cast<HANDLE>(0x1000),
72 : static_cast<HEAP_INFORMATION_CLASS>(0), reinterpret_cast<PVOID>(0x2000),
73 : 100, false));
74 E : }
75 :
76 : template <>
77 E : std::unique_ptr<EventInterface> CreateTestEvent<events::HeapSizeEvent>() {
78 E : return std::unique_ptr<EventInterface>(
79 : new events::HeapSizeEvent(0, reinterpret_cast<HANDLE>(0x1000), 0,
80 : reinterpret_cast<LPCVOID>(0x2000), 233));
81 E : }
82 :
83 : // Test helper for testing the EventInterface abstract serialization mechanism.
84 : template <typename EventType>
85 E : void EventSerializationTest() {
86 E : core::ByteVector bytes;
87 :
88 E : core::ScopedOutStreamPtr out_stream;
89 E : out_stream.reset(core::CreateByteOutStream(std::back_inserter(bytes)));
90 E : core::NativeBinaryOutArchive out_archive(out_stream.get());
91 E : std::unique_ptr<EventInterface> e0 = CreateTestEvent<EventType>();
92 E : EXPECT_TRUE(EventInterface::Save(e0.get(), &out_archive));
93 E : EXPECT_TRUE(out_archive.Flush());
94 :
95 E : core::ScopedInStreamPtr in_stream;
96 E : in_stream.reset(core::CreateByteInStream(bytes.begin(), bytes.end()));
97 E : core::NativeBinaryInArchive in_archive(in_stream.get());
98 : std::unique_ptr<EventInterface> e1 =
99 E : std::move(EventInterface::Load(&in_archive));
100 E : EXPECT_NE(static_cast<EventInterface*>(nullptr), e1.get());
101 :
102 E : EXPECT_TRUE(e0->Equals(e1.get()));
103 E : }
104 :
105 : // Test abstract serialization of each event type.
106 E : TEST(EventInterfaceTest, AbstractSerialization) {
107 E : EXPECT_NO_FATAL_FAILURE(EventSerializationTest<events::HeapAllocEvent>());
108 E : EXPECT_NO_FATAL_FAILURE(EventSerializationTest<events::HeapCreateEvent>());
109 E : EXPECT_NO_FATAL_FAILURE(EventSerializationTest<events::HeapDestroyEvent>());
110 E : EXPECT_NO_FATAL_FAILURE(EventSerializationTest<events::HeapFreeEvent>());
111 E : EXPECT_NO_FATAL_FAILURE(EventSerializationTest<events::HeapReAllocEvent>());
112 E : EXPECT_NO_FATAL_FAILURE(
113 E : EventSerializationTest<events::HeapSetInformationEvent>());
114 E : EXPECT_NO_FATAL_FAILURE(EventSerializationTest<events::HeapSizeEvent>());
115 :
116 : static_assert(static_cast<int>(EventInterface::kHeapSizeEvent + 1) ==
117 : static_cast<int>(EventInterface::kMaxEventType),
118 : "all event types must be tested");
119 E : }
120 :
121 : } // namespace bard
|