-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
EventTests.cpp
323 lines (264 loc) · 8.76 KB
/
EventTests.cpp
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#include <xplatform.h>
#include <cassert>
#include <Server.Contracts.h>
#include <windows_version_helpers.h>
// COM headers
#include <objbase.h>
#include <combaseapi.h>
#define COM_CLIENT
#include <Servers.h>
#define THROW_IF_FAILED(exp) { hr = exp; if (FAILED(hr)) { ::printf("FAILURE: 0x%08x = %s\n", hr, #exp); throw hr; } }
#define THROW_FAIL_IF_FALSE(exp) { if (!(exp)) { ::printf("FALSE: %s\n", #exp); throw E_FAIL; } }
#include <string>
namespace
{
class DispIDToStringMap
{
struct Pair
{
DISPID id;
WCHAR value[128];
};
Pair _pairs[8];
const Pair* _end;
public:
DispIDToStringMap()
: _pairs{}
, _end{ _pairs + ARRAY_SIZE(_pairs) }
{
for (auto curr = _pairs; curr != _end; ++curr)
curr->id = DISPID_UNKNOWN;
}
const WCHAR* Find(_In_ DISPID id)
{
for (auto curr = _pairs; curr != _end; ++curr)
{
if (curr->id == id)
return curr->value;
}
return nullptr;
}
void Insert(_In_ DISPID id, _In_z_ const WCHAR* value)
{
if (id == DISPID_UNKNOWN)
throw E_UNEXPECTED;
for (auto curr = _pairs; curr != _end; ++curr)
{
if (curr->id == DISPID_UNKNOWN)
{
curr->id = id;
size_t len = ::wcslen(value) + 1; // Include null
::memcpy(curr->value, value, len * sizeof(value[0]));
return;
}
}
throw E_UNEXPECTED;
}
void Erase(_In_ DISPID id)
{
for (auto curr = _pairs; curr != _end; ++curr)
{
if (curr->id == id)
{
curr->id = DISPID_UNKNOWN;
break;
}
}
}
};
class EventSink : public UnknownImpl, public TestingEvents
{
DispIDToStringMap _firedEvents;
public:
void ResetFiredState(_In_ DISPID id)
{
_firedEvents.Erase(id);
}
bool DidFire(_In_ DISPID id, _Out_ std::wstring& message)
{
auto value = _firedEvents.Find(id);
if (value == nullptr)
return false;
message = value;
return true;
}
public: // IDispatch
virtual HRESULT STDMETHODCALLTYPE GetTypeInfoCount(
/* [out] */ __RPC__out UINT* pctinfo)
{
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE GetTypeInfo(
/* [in] */ UINT iTInfo,
/* [in] */ LCID lcid,
/* [out] */ __RPC__deref_out_opt ITypeInfo** ppTInfo)
{
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE GetIDsOfNames(
/* [in] */ __RPC__in REFIID riid,
/* [size_is][in] */ __RPC__in_ecount_full(cNames) LPOLESTR* rgszNames,
/* [range][in] */ __RPC__in_range(0, 16384) UINT cNames,
/* [in] */ LCID lcid,
/* [size_is][out] */ __RPC__out_ecount_full(cNames) DISPID* rgDispId)
{
return E_NOTIMPL;
}
virtual /* [local] */ HRESULT STDMETHODCALLTYPE Invoke(
/* [annotation][in] */
_In_ DISPID dispIdMember,
/* [annotation][in] */
_In_ REFIID riid,
/* [annotation][in] */
_In_ LCID lcid,
/* [annotation][in] */
_In_ WORD wFlags,
/* [annotation][out][in] */
_In_ DISPPARAMS* pDispParams,
/* [annotation][out] */
_Out_opt_ VARIANT* pVarResult,
/* [annotation][out] */
_Out_opt_ EXCEPINFO* pExcepInfo,
/* [annotation][out] */
_Out_opt_ UINT* puArgErr)
{
//
// Note that arguments are received in reverse order for IDispatch::Invoke()
//
switch (dispIdMember)
{
case DISPATCHTESTINGEVENTS_DISPID_ONEVENT:
{
return OnFireEventHandler(dispIdMember, pDispParams);
}
}
return E_NOTIMPL;
}
private:
HRESULT OnFireEventHandler(_In_ DISPID dispId, _In_ DISPPARAMS* dispParams)
{
if (dispParams == nullptr)
return E_POINTER;
if (dispParams->cArgs != 1)
return E_INVALIDARG;
VARIANTARG* msgMaybe = dispParams->rgvarg;
if (msgMaybe->vt != VT_BSTR)
return E_INVALIDARG;
_firedEvents.Insert(dispId, msgMaybe->bstrVal);
return S_OK;
}
public: // IUnknown
STDMETHOD(QueryInterface)(
/* [in] */ REFIID riid,
/* [iid_is][out] */ _COM_Outptr_ void __RPC_FAR* __RPC_FAR* ppvObject)
{
return DoQueryInterface(riid, ppvObject,
static_cast<TestingEvents*>(this),
static_cast<IDispatch*>(this));
}
DEFINE_REF_COUNTING();
};
void VerifyAdviseUnadviseFromEvent()
{
HRESULT hr;
ComSmartPtr<IEventTesting> et;
THROW_IF_FAILED(::CoCreateInstance(CLSID_EventTesting, nullptr, CLSCTX_INPROC, IID_IEventTesting, (void**)&et));
ComSmartPtr<IConnectionPointContainer> cpc;
THROW_IF_FAILED(et->QueryInterface(&cpc));
ComSmartPtr<IConnectionPoint> cp;
THROW_IF_FAILED(cpc->FindConnectionPoint(IID_TestingEvents, &cp));
// Create event sink
ComSmartPtr<EventSink> es;
es.Attach(new EventSink());
DWORD cookie;
ComSmartPtr<IUnknown> uk;
THROW_IF_FAILED(es->QueryInterface(IID_IUnknown, (void**)&uk));
THROW_IF_FAILED(cp->Advise(uk, &cookie));
// Ensure state is valid.
es->ResetFiredState(DISPATCHTESTINGEVENTS_DISPID_ONEVENT);
THROW_IF_FAILED(et->FireEvent());
// Validate the event fired.
{
std::wstring eventName;
THROW_FAIL_IF_FALSE(es->DidFire(DISPATCHTESTINGEVENTS_DISPID_ONEVENT, eventName));
THROW_FAIL_IF_FALSE(eventName.compare(L"FireEvent") == 0);
}
THROW_IF_FAILED(cp->Unadvise(cookie));
// Reset state.
es->ResetFiredState(DISPATCHTESTINGEVENTS_DISPID_ONEVENT);
THROW_IF_FAILED(et->FireEvent());
// Validate the event was not fired.
{
std::wstring eventName;
THROW_FAIL_IF_FALSE(!es->DidFire(DISPATCHTESTINGEVENTS_DISPID_ONEVENT, eventName));
THROW_FAIL_IF_FALSE(eventName.empty());
}
}
void VerifyEnumConnectionPoints()
{
HRESULT hr;
ComSmartPtr<IEventTesting> et;
THROW_IF_FAILED(::CoCreateInstance(CLSID_EventTesting, nullptr, CLSCTX_INPROC, IID_IEventTesting, (void**)&et));
ComSmartPtr<IConnectionPointContainer> cpc;
THROW_IF_FAILED(et->QueryInterface(&cpc));
ComSmartPtr<IEnumConnectionPoints> ecp;
THROW_IF_FAILED(cpc->EnumConnectionPoints(&ecp));
bool foundEventInterface = false;
ULONG fetched;
LPCONNECTIONPOINT ptRaw = nullptr;
while ((hr = ecp->Next(1, &ptRaw, &fetched)) == S_OK)
{
THROW_FAIL_IF_FALSE(fetched == 1);
THROW_FAIL_IF_FALSE(ptRaw != nullptr);
ComSmartPtr<IConnectionPoint> pt;
pt.Attach(ptRaw);
ptRaw = nullptr;
IID iidMaybe;
THROW_IF_FAILED(pt->GetConnectionInterface(&iidMaybe));
foundEventInterface = (iidMaybe == IID_TestingEvents);
// There should only be one event interface
THROW_FAIL_IF_FALSE(foundEventInterface);
}
THROW_IF_FAILED(hr);
THROW_FAIL_IF_FALSE(foundEventInterface);
}
}
template<COINIT TM>
struct ComInit
{
const HRESULT Result;
ComInit()
: Result{ ::CoInitializeEx(nullptr, TM) }
{ }
~ComInit()
{
if (SUCCEEDED(Result))
::CoUninitialize();
}
};
using ComMTA = ComInit<COINIT_MULTITHREADED>;
int __cdecl main()
{
if (is_windows_nano() == S_OK)
{
::puts("RegFree COM is not supported on Windows Nano. Auto-passing this test.\n");
return 100;
}
ComMTA init;
if (FAILED(init.Result))
return -1;
try
{
CoreShimComActivation csact{ W("NETServer"), W("EventTesting") };
VerifyAdviseUnadviseFromEvent();
VerifyEnumConnectionPoints();
}
catch (HRESULT hr)
{
::printf("Test Failure: 0x%08x\n", hr);
return 101;
}
return 100;
}