OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // Unit tests for event trace consumer base class. | 5 // Unit tests for event trace consumer base class. |
6 #include "base/win/event_trace_consumer.h" | 6 #include "base/win/event_trace_consumer.h" |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 | 9 |
10 #include <objbase.h> | 10 #include <objbase.h> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/file_path.h" | 13 #include "base/file_path.h" |
14 #include "base/file_util.h" | 14 #include "base/file_util.h" |
15 #include "base/files/scoped_temp_dir.h" | 15 #include "base/files/scoped_temp_dir.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/process.h" | 17 #include "base/process.h" |
18 #include "base/stringprintf.h" | 18 #include "base/stringprintf.h" |
19 #include "base/win/event_trace_controller.h" | 19 #include "base/win/event_trace_controller.h" |
20 #include "base/win/event_trace_provider.h" | 20 #include "base/win/event_trace_provider.h" |
21 #include "base/win/scoped_handle.h" | 21 #include "base/win/scoped_handle.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
23 | 23 |
24 #include <initguid.h> // NOLINT - has to be last | 24 #include <initguid.h> // NOLINT - has to be last |
25 | 25 |
| 26 namespace base { |
| 27 namespace win { |
| 28 |
26 namespace { | 29 namespace { |
27 | 30 |
28 using base::win::EtwMofEvent; | |
29 using base::win::EtwTraceController; | |
30 using base::win::EtwTraceConsumerBase; | |
31 using base::win::EtwTraceProperties; | |
32 using base::win::EtwTraceProvider; | |
33 | |
34 typedef std::list<EVENT_TRACE> EventQueue; | 31 typedef std::list<EVENT_TRACE> EventQueue; |
35 | 32 |
36 class TestConsumer: public EtwTraceConsumerBase<TestConsumer> { | 33 class TestConsumer: public EtwTraceConsumerBase<TestConsumer> { |
37 public: | 34 public: |
38 TestConsumer() { | 35 TestConsumer() { |
39 sank_event_.Set(::CreateEvent(NULL, TRUE, FALSE, NULL)); | 36 sank_event_.Set(::CreateEvent(NULL, TRUE, FALSE, NULL)); |
40 ClearQueue(); | 37 ClearQueue(); |
41 } | 38 } |
42 | 39 |
43 ~TestConsumer() { | 40 ~TestConsumer() { |
(...skipping 19 matching lines...) Expand all Loading... |
63 back.MofData = new char[event->MofLength]; | 60 back.MofData = new char[event->MofLength]; |
64 memcpy(back.MofData, event->MofData, event->MofLength); | 61 memcpy(back.MofData, event->MofData, event->MofLength); |
65 } | 62 } |
66 } | 63 } |
67 | 64 |
68 static void ProcessEvent(EVENT_TRACE* event) { | 65 static void ProcessEvent(EVENT_TRACE* event) { |
69 EnqueueEvent(event); | 66 EnqueueEvent(event); |
70 ::SetEvent(sank_event_.Get()); | 67 ::SetEvent(sank_event_.Get()); |
71 } | 68 } |
72 | 69 |
73 static base::win::ScopedHandle sank_event_; | 70 static ScopedHandle sank_event_; |
74 static EventQueue events_; | 71 static EventQueue events_; |
75 | 72 |
76 private: | 73 private: |
77 DISALLOW_COPY_AND_ASSIGN(TestConsumer); | 74 DISALLOW_COPY_AND_ASSIGN(TestConsumer); |
78 }; | 75 }; |
79 | 76 |
80 base::win::ScopedHandle TestConsumer::sank_event_; | 77 ScopedHandle TestConsumer::sank_event_; |
81 EventQueue TestConsumer::events_; | 78 EventQueue TestConsumer::events_; |
82 | 79 |
83 class EtwTraceConsumerBaseTest: public testing::Test { | 80 class EtwTraceConsumerBaseTest: public testing::Test { |
84 public: | 81 public: |
85 EtwTraceConsumerBaseTest() | 82 EtwTraceConsumerBaseTest() |
86 : session_name_(base::StringPrintf(L"TestSession-%d", | 83 : session_name_(StringPrintf(L"TestSession-%d", |
87 base::Process::Current().pid())) { | 84 Process::Current().pid())) { |
88 } | 85 } |
89 | 86 |
90 virtual void SetUp() { | 87 virtual void SetUp() { |
91 // Cleanup any potentially dangling sessions. | 88 // Cleanup any potentially dangling sessions. |
92 EtwTraceProperties ignore; | 89 EtwTraceProperties ignore; |
93 EtwTraceController::Stop(session_name_.c_str(), &ignore); | 90 EtwTraceController::Stop(session_name_.c_str(), &ignore); |
94 | 91 |
95 // Allocate a new GUID for each provider test. | 92 // Allocate a new GUID for each provider test. |
96 ASSERT_HRESULT_SUCCEEDED(::CoCreateGuid(&test_provider_)); | 93 ASSERT_HRESULT_SUCCEEDED(::CoCreateGuid(&test_provider_)); |
97 } | 94 } |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 return HRESULT_FROM_WIN32(::GetLastError()); | 196 return HRESULT_FROM_WIN32(::GetLastError()); |
200 | 197 |
201 DWORD exit_code = 0; | 198 DWORD exit_code = 0; |
202 if (::GetExitCodeThread(consumer_thread_, &exit_code)) | 199 if (::GetExitCodeThread(consumer_thread_, &exit_code)) |
203 return exit_code; | 200 return exit_code; |
204 | 201 |
205 return HRESULT_FROM_WIN32(::GetLastError()); | 202 return HRESULT_FROM_WIN32(::GetLastError()); |
206 } | 203 } |
207 | 204 |
208 TestConsumer consumer_; | 205 TestConsumer consumer_; |
209 base::win::ScopedHandle consumer_ready_; | 206 ScopedHandle consumer_ready_; |
210 base::win::ScopedHandle consumer_thread_; | 207 ScopedHandle consumer_thread_; |
211 }; | 208 }; |
212 | 209 |
213 } // namespace | 210 } // namespace |
214 | 211 |
215 TEST_F(EtwTraceConsumerRealtimeTest, ConsumerReturnsWhenSessionClosed) { | 212 TEST_F(EtwTraceConsumerRealtimeTest, ConsumerReturnsWhenSessionClosed) { |
216 EtwTraceController controller; | 213 EtwTraceController controller; |
217 | 214 |
218 HRESULT hr = controller.StartRealtimeSession(session_name_.c_str(), | 215 HRESULT hr = controller.StartRealtimeSession(session_name_.c_str(), |
219 100 * 1024); | 216 100 * 1024); |
220 if (hr == E_ACCESSDENIED) { | 217 if (hr == E_ACCESSDENIED) { |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 | 346 |
350 // We should now have the event in the queue. | 347 // We should now have the event in the queue. |
351 if (events_.empty()) | 348 if (events_.empty()) |
352 return E_FAIL; | 349 return E_FAIL; |
353 | 350 |
354 *trace = &events_.back(); | 351 *trace = &events_.back(); |
355 return S_OK; | 352 return S_OK; |
356 } | 353 } |
357 | 354 |
358 EventQueue events_; | 355 EventQueue events_; |
359 base::ScopedTempDir temp_dir_; | 356 ScopedTempDir temp_dir_; |
360 FilePath temp_file_; | 357 FilePath temp_file_; |
361 }; | 358 }; |
362 | 359 |
363 } // namespace | 360 } // namespace |
364 | 361 |
365 | 362 |
366 TEST_F(EtwTraceConsumerDataTest, RoundTrip) { | 363 TEST_F(EtwTraceConsumerDataTest, RoundTrip) { |
367 EtwMofEvent<1> event(kTestEventType, 1, TRACE_LEVEL_ERROR); | 364 EtwMofEvent<1> event(kTestEventType, 1, TRACE_LEVEL_ERROR); |
368 | 365 |
369 static const char kData[] = "This is but test data"; | 366 static const char kData[] = "This is but test data"; |
370 event.fields[0].DataPtr = reinterpret_cast<ULONG64>(kData); | 367 event.fields[0].DataPtr = reinterpret_cast<ULONG64>(kData); |
371 event.fields[0].Length = sizeof(kData); | 368 event.fields[0].Length = sizeof(kData); |
372 | 369 |
373 PEVENT_TRACE trace = NULL; | 370 PEVENT_TRACE trace = NULL; |
374 HRESULT hr = RoundTripEvent(&event.header, &trace); | 371 HRESULT hr = RoundTripEvent(&event.header, &trace); |
375 if (hr == E_ACCESSDENIED) { | 372 if (hr == E_ACCESSDENIED) { |
376 VLOG(1) << "You must be an administrator to run this test on Vista"; | 373 VLOG(1) << "You must be an administrator to run this test on Vista"; |
377 return; | 374 return; |
378 } | 375 } |
379 ASSERT_HRESULT_SUCCEEDED(hr) << "RoundTripEvent failed"; | 376 ASSERT_HRESULT_SUCCEEDED(hr) << "RoundTripEvent failed"; |
380 ASSERT_TRUE(NULL != trace); | 377 ASSERT_TRUE(NULL != trace); |
381 ASSERT_EQ(sizeof(kData), trace->MofLength); | 378 ASSERT_EQ(sizeof(kData), trace->MofLength); |
382 ASSERT_STREQ(kData, reinterpret_cast<const char*>(trace->MofData)); | 379 ASSERT_STREQ(kData, reinterpret_cast<const char*>(trace->MofData)); |
383 } | 380 } |
| 381 |
| 382 } // namespace win |
| 383 } // namespace base |
OLD | NEW |