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
3 // found in the LICENSE file.
5 // Unit tests for event trace controller.
10 #include "base/file_path.h"
11 #include "base/file_util.h"
12 #include "base/logging.h"
13 #include "base/process.h"
14 #include "base/scoped_temp_dir.h"
15 #include "base/stringprintf.h"
16 #include "base/sys_info.h"
17 #include "base/win/event_trace_controller.h"
18 #include "base/win/event_trace_provider.h"
19 #include "base/win/scoped_handle.h"
20 #include "testing/gtest/include/gtest/gtest.h"
24 using base::win::EtwTraceController
;
25 using base::win::EtwTraceProvider
;
26 using base::win::EtwTraceProperties
;
28 DEFINE_GUID(kGuidNull
,
29 0x0000000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0);
31 const ULONG kTestProviderFlags
= 0xCAFEBABE;
33 class TestingProvider
: public EtwTraceProvider
{
35 explicit TestingProvider(const GUID
& provider_name
)
36 : EtwTraceProvider(provider_name
) {
37 callback_event_
.Set(::CreateEvent(NULL
, TRUE
, FALSE
, NULL
));
40 void WaitForCallback() {
41 ::WaitForSingleObject(callback_event_
.Get(), INFINITE
);
42 ::ResetEvent(callback_event_
.Get());
46 virtual void OnEventsEnabled() {
47 ::SetEvent(callback_event_
.Get());
49 virtual void PostEventsDisabled() {
50 ::SetEvent(callback_event_
.Get());
53 base::win::ScopedHandle callback_event_
;
55 DISALLOW_COPY_AND_ASSIGN(TestingProvider
);
60 TEST(EtwTracePropertiesTest
, Initialization
) {
61 EtwTraceProperties prop
;
63 EVENT_TRACE_PROPERTIES
* p
= prop
.get();
64 EXPECT_NE(0u, p
->Wnode
.BufferSize
);
65 EXPECT_EQ(0u, p
->Wnode
.ProviderId
);
66 EXPECT_EQ(0u, p
->Wnode
.HistoricalContext
);
68 EXPECT_TRUE(kGuidNull
== p
->Wnode
.Guid
);
69 EXPECT_EQ(0, p
->Wnode
.ClientContext
);
70 EXPECT_EQ(WNODE_FLAG_TRACED_GUID
, p
->Wnode
.Flags
);
72 EXPECT_EQ(0, p
->BufferSize
);
73 EXPECT_EQ(0, p
->MinimumBuffers
);
74 EXPECT_EQ(0, p
->MaximumBuffers
);
75 EXPECT_EQ(0, p
->MaximumFileSize
);
76 EXPECT_EQ(0, p
->LogFileMode
);
77 EXPECT_EQ(0, p
->FlushTimer
);
78 EXPECT_EQ(0, p
->EnableFlags
);
79 EXPECT_EQ(0, p
->AgeLimit
);
81 EXPECT_EQ(0, p
->NumberOfBuffers
);
82 EXPECT_EQ(0, p
->FreeBuffers
);
83 EXPECT_EQ(0, p
->EventsLost
);
84 EXPECT_EQ(0, p
->BuffersWritten
);
85 EXPECT_EQ(0, p
->LogBuffersLost
);
86 EXPECT_EQ(0, p
->RealTimeBuffersLost
);
87 EXPECT_EQ(0, p
->LoggerThreadId
);
88 EXPECT_NE(0u, p
->LogFileNameOffset
);
89 EXPECT_NE(0u, p
->LoggerNameOffset
);
92 TEST(EtwTracePropertiesTest
, Strings
) {
93 EtwTraceProperties prop
;
95 ASSERT_STREQ(L
"", prop
.GetLoggerFileName());
96 ASSERT_STREQ(L
"", prop
.GetLoggerName());
98 std::wstring
name(1023, L
'A');
99 ASSERT_HRESULT_SUCCEEDED(prop
.SetLoggerFileName(name
.c_str()));
100 ASSERT_HRESULT_SUCCEEDED(prop
.SetLoggerName(name
.c_str()));
101 ASSERT_STREQ(name
.c_str(), prop
.GetLoggerFileName());
102 ASSERT_STREQ(name
.c_str(), prop
.GetLoggerName());
104 std::wstring
name2(1024, L
'A');
105 ASSERT_HRESULT_FAILED(prop
.SetLoggerFileName(name2
.c_str()));
106 ASSERT_HRESULT_FAILED(prop
.SetLoggerName(name2
.c_str()));
111 class EtwTraceControllerTest
: public testing::Test
{
113 EtwTraceControllerTest() : session_name_(
114 base::StringPrintf(L
"TestSession-%d", base::Process::Current().pid())) {
117 virtual void SetUp() {
118 EtwTraceProperties ignore
;
119 EtwTraceController::Stop(session_name_
.c_str(), &ignore
);
121 // Allocate a new provider name GUID for each test.
122 ASSERT_HRESULT_SUCCEEDED(::CoCreateGuid(&test_provider_
));
125 virtual void TearDown() {
126 EtwTraceProperties prop
;
127 EtwTraceController::Stop(session_name_
.c_str(), &prop
);
132 std::wstring session_name_
;
137 TEST_F(EtwTraceControllerTest
, Initialize
) {
138 EtwTraceController controller
;
140 EXPECT_EQ(NULL
, controller
.session());
141 EXPECT_STREQ(L
"", controller
.session_name());
145 TEST_F(EtwTraceControllerTest
, StartRealTimeSession
) {
146 EtwTraceController controller
;
148 HRESULT hr
= controller
.StartRealtimeSession(session_name_
.c_str(),
150 if (hr
== E_ACCESSDENIED
) {
151 VLOG(1) << "You must be an administrator to run this test on Vista";
155 EXPECT_TRUE(NULL
!= controller
.session());
156 EXPECT_STREQ(session_name_
.c_str(), controller
.session_name());
158 EXPECT_HRESULT_SUCCEEDED(controller
.Stop(NULL
));
159 EXPECT_EQ(NULL
, controller
.session());
160 EXPECT_STREQ(L
"", controller
.session_name());
163 TEST_F(EtwTraceControllerTest
, StartFileSession
) {
164 ScopedTempDir temp_dir
;
165 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
167 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir
.path(), &temp
));
169 EtwTraceController controller
;
170 HRESULT hr
= controller
.StartFileSession(session_name_
.c_str(),
171 temp
.value().c_str());
172 if (hr
== E_ACCESSDENIED
) {
173 VLOG(1) << "You must be an administrator to run this test on Vista";
174 file_util::Delete(temp
, false);
178 EXPECT_TRUE(NULL
!= controller
.session());
179 EXPECT_STREQ(session_name_
.c_str(), controller
.session_name());
181 EXPECT_HRESULT_SUCCEEDED(controller
.Stop(NULL
));
182 EXPECT_EQ(NULL
, controller
.session());
183 EXPECT_STREQ(L
"", controller
.session_name());
184 file_util::Delete(temp
, false);
187 TEST_F(EtwTraceControllerTest
, EnableDisable
) {
188 TestingProvider
provider(test_provider_
);
190 EXPECT_EQ(ERROR_SUCCESS
, provider
.Register());
191 EXPECT_EQ(NULL
, provider
.session_handle());
193 EtwTraceController controller
;
194 HRESULT hr
= controller
.StartRealtimeSession(session_name_
.c_str(),
196 if (hr
== E_ACCESSDENIED
) {
197 VLOG(1) << "You must be an administrator to run this test on Vista";
201 EXPECT_HRESULT_SUCCEEDED(controller
.EnableProvider(test_provider_
,
202 TRACE_LEVEL_VERBOSE
, kTestProviderFlags
));
204 provider
.WaitForCallback();
206 EXPECT_EQ(TRACE_LEVEL_VERBOSE
, provider
.enable_level());
207 EXPECT_EQ(kTestProviderFlags
, provider
.enable_flags());
209 EXPECT_HRESULT_SUCCEEDED(controller
.DisableProvider(test_provider_
));
211 provider
.WaitForCallback();
213 EXPECT_EQ(0, provider
.enable_level());
214 EXPECT_EQ(0, provider
.enable_flags());
216 EXPECT_EQ(ERROR_SUCCESS
, provider
.Unregister());
218 // Enable the provider again, before registering.
219 EXPECT_HRESULT_SUCCEEDED(controller
.EnableProvider(test_provider_
,
220 TRACE_LEVEL_VERBOSE
, kTestProviderFlags
));
222 // Register the provider again, the settings above
223 // should take immediate effect.
224 EXPECT_EQ(ERROR_SUCCESS
, provider
.Register());
226 EXPECT_EQ(TRACE_LEVEL_VERBOSE
, provider
.enable_level());
227 EXPECT_EQ(kTestProviderFlags
, provider
.enable_flags());
229 EXPECT_HRESULT_SUCCEEDED(controller
.Stop(NULL
));
231 provider
.WaitForCallback();
233 // Session should have wound down.
234 EXPECT_EQ(0, provider
.enable_level());
235 EXPECT_EQ(0, provider
.enable_flags());