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