forked from microsoft/SysmonCommon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.h
121 lines (103 loc) · 3.21 KB
/
test.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Common header and definitions for testing.
#pragma once
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#if !defined _WIN64 && !defined _WIN32
#include "linuxTypes.h"
#endif
#include "utils.h"
#if defined _WIN64 || defined _WIN32
#include "..\..\sysmonCommon\rules.h"
#include "..\..\sysmonCommon\service.h"
#include "..\..\sysmonCommon\xml.h"
#include "..\..\exe\environmentvar.h"
#include "..\..\sysmonCommon\eventsCommon.h"
#else
#include "rules.h"
#include "xml.h"
#include "eventsCommon.h"
#endif
#define mockSessionId 1111
#define mockSecondSessionId 2222
#if defined _WIN64 || defined _WIN32
extern EnvironmentVariableCache *envCache;
// Common mock classes
class MockEnvironmentVariableCache : public EnvironmentVariableCache
{
public:
MockEnvironmentVariableCache() {
replaced = nullptr;
// Default handlers relay to the parent class.
ON_CALL(*this, AddValueToCache).WillByDefault([this](
_In_ ULONG sessionId,
_In_ LPWSTR variableString,
_In_ LPWSTR expandedString
) {
EnvironmentVariableCache::AddValueToCache(
sessionId,
variableString,
expandedString
);
});
ON_CALL(*this, GetUserToken).WillByDefault([this](
_In_ ULONG sessionId,
_Out_ HANDLE* token
) {
return EnvironmentVariableCache::GetUserToken(
sessionId,
token);
});
}
// Constructor to temporarily replace a global cache.
MockEnvironmentVariableCache(EnvironmentVariableCache **current) : MockEnvironmentVariableCache() {
if( *current != nullptr ) {
delete *current;
}
replaced = current;
*current = this;
}
~MockEnvironmentVariableCache() {
// Clean any pointer that was replaced.
if( replaced != nullptr ) {
*replaced = new EnvironmentVariableCache();
}
}
MOCK_METHOD(void, AddValueToCache, (
_In_ ULONG sessionId,
_In_ LPWSTR variableString,
_In_ LPWSTR expandedString), (override));
MOCK_METHOD(BOOL, GetUserToken, (
_In_ ULONG sessionId,
_Out_ HANDLE* token), (override));
// MockGetUserToken can be used to replace GetUserToken to return the hToken for the current process.
static BOOL MockGetUserToken(
_In_ ULONG sessionId,
_Out_ HANDLE* token) {
return OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, token);
}
private:
EnvironmentVariableCache **replaced;
};
#endif
// Internal functions that are called by the unittest framework but not defined in Sysmon headers.
// from events.cpp
RuleDefaultType
ProcessEventRulesDry(
PLARGE_INTEGER EventTime,
PSYSMON_EVENT_TYPE_FMT EventType,
PSYSMON_DATA_DESCRIPTOR EventBuffer,
PSYSMON_EVENT_HEADER EventData,
PWCHAR *RuleName);
VOID EventSetFieldS(
_In_ PSYSMON_DATA_DESCRIPTOR DataDescriptor,
_In_ ULONG FieldIndex,
_In_ const TCHAR *String,
_In_ BOOLEAN Allocated);
PVOID ExtGetPtr(
_In_ PULONG extensionsSizes,
_In_ PVOID extensions,
_In_ ULONG index,
_Out_ PULONG retSize
);
#define ExtGetPtrX( _v, _i, _s ) \
ExtGetPtr( (_v)->m_Extensions, (_v) + 1, _i, _s )