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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
base / win / event_trace_consumer_unittest.cc [blame]
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Unit tests for event trace consumer base class.
#include "base/win/event_trace_consumer.h"
#include <objbase.h>
#include <initguid.h>
#include <iterator>
#include <list>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/logging.h"
#include "base/process/process_handle.h"
#include "base/strings/string_number_conversions_win.h"
#include "base/strings/string_util.h"
#include "base/win/event_trace_controller.h"
#include "base/win/event_trace_provider.h"
#include "base/win/scoped_handle.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace win {
namespace {
using EventQueue = std::list<EVENT_TRACE>;
class TestConsumer : public EtwTraceConsumerBase<TestConsumer> {
public:
TestConsumer() {
sank_event_.Set(::CreateEvent(nullptr, TRUE, FALSE, nullptr));
ClearQueue();
}
TestConsumer(const TestConsumer&) = delete;
TestConsumer& operator=(const TestConsumer&) = delete;
~TestConsumer() {
ClearQueue();
sank_event_.Close();
}
void ClearQueue() {
for (EventQueue::const_iterator it(events_.begin()), end(events_.end());
it != end; ++it) {
delete[] reinterpret_cast<char*>(it->MofData);
}
events_.clear();
}
static void EnqueueEvent(EVENT_TRACE* event) {
events_.push_back(*event);
EVENT_TRACE& back = events_.back();
if (event->MofData != nullptr && event->MofLength != 0) {
back.MofData = new char[event->MofLength];
memcpy(back.MofData, event->MofData, event->MofLength);
}
}
static void ProcessEvent(EVENT_TRACE* event) {
EnqueueEvent(event);
::SetEvent(sank_event_.get());
}
static ScopedHandle sank_event_;
static EventQueue events_;
};
ScopedHandle TestConsumer::sank_event_;
EventQueue TestConsumer::events_;
class EtwTraceConsumerBaseTest : public testing::Test {
public:
EtwTraceConsumerBaseTest()
: session_name_(L"TestSession-" + NumberToWString(GetCurrentProcId())) {}
void SetUp() override {
// Cleanup any potentially dangling sessions.
EtwTraceProperties ignore;
EtwTraceController::Stop(session_name_.c_str(), &ignore);
// Allocate a new GUID for each provider test.
ASSERT_HRESULT_SUCCEEDED(::CoCreateGuid(&test_provider_));
}
void TearDown() override {
// Cleanup any potentially dangling sessions.
EtwTraceProperties ignore;
EtwTraceController::Stop(session_name_.c_str(), &ignore);
}
protected:
GUID test_provider_;
std::wstring session_name_;
};
} // namespace
TEST_F(EtwTraceConsumerBaseTest, Initialize) {
TestConsumer consumer_;
}
TEST_F(EtwTraceConsumerBaseTest, OpenRealtimeSucceedsWhenNoSession) {
TestConsumer consumer_;
ASSERT_HRESULT_SUCCEEDED(
consumer_.OpenRealtimeSession(session_name_.c_str()));
}
TEST_F(EtwTraceConsumerBaseTest, ConsumerImmediateFailureWhenNoSession) {
TestConsumer consumer_;
ASSERT_HRESULT_SUCCEEDED(
consumer_.OpenRealtimeSession(session_name_.c_str()));
ASSERT_HRESULT_FAILED(consumer_.Consume());
}
namespace {
class EtwTraceConsumerRealtimeTest : public EtwTraceConsumerBaseTest {
public:
void SetUp() override {
EtwTraceConsumerBaseTest::SetUp();
ASSERT_HRESULT_SUCCEEDED(
consumer_.OpenRealtimeSession(session_name_.c_str()));
}
void TearDown() override {
consumer_.Close();
EtwTraceConsumerBaseTest::TearDown();
}
DWORD ConsumerThread() {
::SetEvent(consumer_ready_.get());
return consumer_.Consume();
}
static DWORD WINAPI ConsumerThreadMainProc(void* arg) {
return reinterpret_cast<EtwTraceConsumerRealtimeTest*>(arg)
->ConsumerThread();
}
HRESULT StartConsumerThread() {
consumer_ready_.Set(::CreateEvent(nullptr, TRUE, FALSE, nullptr));
EXPECT_TRUE(consumer_ready_.is_valid());
consumer_thread_.Set(
::CreateThread(nullptr, 0, ConsumerThreadMainProc, this, 0, nullptr));
if (consumer_thread_.get() == nullptr)
return HRESULT_FROM_WIN32(::GetLastError());
HANDLE events[] = {consumer_ready_.get(), consumer_thread_.get()};
DWORD result =
::WaitForMultipleObjects(std::size(events), events, FALSE, INFINITE);
switch (result) {
case WAIT_OBJECT_0:
// The event was set, the consumer_ is ready.
return S_OK;
case WAIT_OBJECT_0 + 1: {
// The thread finished. This may race with the event, so check
// explicitly for the event here, before concluding there's trouble.
if (::WaitForSingleObject(consumer_ready_.get(), 0) == WAIT_OBJECT_0)
return S_OK;
DWORD exit_code = 0;
if (::GetExitCodeThread(consumer_thread_.get(), &exit_code))
return exit_code;
return HRESULT_FROM_WIN32(::GetLastError());
}
default:
return E_UNEXPECTED;
}
}
// Waits for consumer_ thread to exit, and returns its exit code.
HRESULT JoinConsumerThread() {
if (::WaitForSingleObject(consumer_thread_.get(), INFINITE) !=
WAIT_OBJECT_0) {
return HRESULT_FROM_WIN32(::GetLastError());
}
DWORD exit_code = 0;
if (::GetExitCodeThread(consumer_thread_.get(), &exit_code))
return exit_code;
return HRESULT_FROM_WIN32(::GetLastError());
}
TestConsumer consumer_;
ScopedHandle consumer_ready_;
ScopedHandle consumer_thread_;
};
} // namespace
TEST_F(EtwTraceConsumerRealtimeTest, ConsumerReturnsWhenSessionClosed) {
EtwTraceController controller;
if (controller.StartRealtimeSession(session_name_.c_str(), 1024) ==
E_ACCESSDENIED) {
VLOG(1) << "You must be an administrator to run this test on Vista";
return;
}
// Start the consumer_.
ASSERT_HRESULT_SUCCEEDED(StartConsumerThread());
// Wait around for the consumer_ thread a bit. This is inherently racy because
// the consumer thread says that it is ready and then calls Consume() which
// calls ::ProcessTrace. We need to call WaitForSingleObject after the call to
// ::ProcessTrace but there is no way to know when that call has been made.
// With a timeout of 50 ms this test was failing frequently when the system
// was under load. It is hoped that 500 ms will be enough.
ASSERT_EQ(static_cast<DWORD>(WAIT_TIMEOUT),
::WaitForSingleObject(consumer_thread_.get(), 500));
ASSERT_HRESULT_SUCCEEDED(controller.Stop(nullptr));
// The consumer_ returns success on session stop.
ASSERT_HRESULT_SUCCEEDED(JoinConsumerThread());
}
namespace {
// clang-format off
// {57E47923-A549-476f-86CA-503D57F59E62}
DEFINE_GUID(
kTestEventType,
0x57e47923, 0xa549, 0x476f, 0x86, 0xca, 0x50, 0x3d, 0x57, 0xf5, 0x9e, 0x62);
// clang-format on
} // namespace
TEST_F(EtwTraceConsumerRealtimeTest, ConsumeEvent) {
EtwTraceController controller;
if (controller.StartRealtimeSession(session_name_.c_str(), 1024) ==
E_ACCESSDENIED) {
VLOG(1) << "You must be an administrator to run this test on Vista";
return;
}
ASSERT_HRESULT_SUCCEEDED(controller.EnableProvider(
test_provider_, TRACE_LEVEL_VERBOSE, 0xFFFFFFFF));
EtwTraceProvider provider(test_provider_);
ASSERT_EQ(static_cast<DWORD>(ERROR_SUCCESS), provider.Register());
// Start the consumer_.
ASSERT_HRESULT_SUCCEEDED(StartConsumerThread());
ASSERT_EQ(0u, TestConsumer::events_.size());
EtwMofEvent<1> event(kTestEventType, 1, TRACE_LEVEL_ERROR);
EXPECT_EQ(static_cast<DWORD>(ERROR_SUCCESS), provider.Log(&event.header));
EXPECT_EQ(WAIT_OBJECT_0,
::WaitForSingleObject(TestConsumer::sank_event_.get(), INFINITE));
ASSERT_HRESULT_SUCCEEDED(controller.Stop(nullptr));
ASSERT_HRESULT_SUCCEEDED(JoinConsumerThread());
ASSERT_NE(0u, TestConsumer::events_.size());
}
namespace {
// We run events through a file session to assert that
// the content comes through.
class EtwTraceConsumerDataTest : public EtwTraceConsumerBaseTest {
public:
EtwTraceConsumerDataTest() = default;
void SetUp() override {
EtwTraceConsumerBaseTest::SetUp();
EtwTraceProperties prop;
EtwTraceController::Stop(session_name_.c_str(), &prop);
// Create a temp dir for this test.
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
// Construct a temp file name in our dir.
temp_file_ = temp_dir_.GetPath().Append(FILE_PATH_LITERAL("test.etl"));
}
void TearDown() override {
EXPECT_TRUE(DeleteFile(temp_file_));
EtwTraceConsumerBaseTest::TearDown();
}
HRESULT LogEventToTempSession(PEVENT_TRACE_HEADER header) {
EtwTraceController controller;
// Set up a file session.
HRESULT hr = controller.StartFileSession(session_name_.c_str(),
temp_file_.value().c_str());
if (FAILED(hr))
return hr;
// Enable our provider.
EXPECT_HRESULT_SUCCEEDED(controller.EnableProvider(
test_provider_, TRACE_LEVEL_VERBOSE, 0xFFFFFFFF));
EtwTraceProvider provider(test_provider_);
// Then register our provider, means we get a session handle immediately.
EXPECT_EQ(static_cast<DWORD>(ERROR_SUCCESS), provider.Register());
// Trace the event, it goes to the temp file.
EXPECT_EQ(static_cast<DWORD>(ERROR_SUCCESS), provider.Log(header));
EXPECT_HRESULT_SUCCEEDED(controller.DisableProvider(test_provider_));
EXPECT_HRESULT_SUCCEEDED(provider.Unregister());
EXPECT_HRESULT_SUCCEEDED(controller.Flush(nullptr));
EXPECT_HRESULT_SUCCEEDED(controller.Stop(nullptr));
return S_OK;
}
HRESULT ConsumeEventFromTempSession() {
// Now consume the event(s).
TestConsumer consumer_;
HRESULT hr = consumer_.OpenFileSession(temp_file_.value().c_str());
if (SUCCEEDED(hr))
hr = consumer_.Consume();
consumer_.Close();
// And nab the result.
events_.swap(TestConsumer::events_);
return hr;
}
HRESULT RoundTripEvent(PEVENT_TRACE_HEADER header, PEVENT_TRACE* trace) {
DeleteFile(temp_file_);
HRESULT hr = LogEventToTempSession(header);
if (SUCCEEDED(hr))
hr = ConsumeEventFromTempSession();
if (FAILED(hr))
return hr;
// We should now have the event in the queue.
if (events_.empty())
return E_FAIL;
*trace = &events_.back();
return S_OK;
}
EventQueue events_;
ScopedTempDir temp_dir_;
FilePath temp_file_;
};
} // namespace
TEST_F(EtwTraceConsumerDataTest, RoundTrip) {
EtwMofEvent<1> event(kTestEventType, 1, TRACE_LEVEL_ERROR);
static const char kData[] = "This is but test data";
event.fields[0].DataPtr = reinterpret_cast<ULONG64>(kData);
event.fields[0].Length = sizeof(kData);
PEVENT_TRACE trace = nullptr;
HRESULT hr = RoundTripEvent(&event.header, &trace);
if (hr == E_ACCESSDENIED) {
VLOG(1) << "You must be an administrator to run this test on Vista";
return;
}
ASSERT_HRESULT_SUCCEEDED(hr) << "RoundTripEvent failed";
ASSERT_TRUE(trace != nullptr);
ASSERT_EQ(sizeof(kData), trace->MofLength);
ASSERT_STREQ(kData, reinterpret_cast<const char*>(trace->MofData));
}
} // namespace win
} // namespace base