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/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/logging.h"
14 #include "base/process/process_handle.h"
15 #include "base/strings/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"
27 DEFINE_GUID(kGuidNull
,
28 0x0000000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0);
30 const ULONG kTestProviderFlags
= 0xCAFEBABE;
32 class TestingProvider
: public EtwTraceProvider
{
34 explicit TestingProvider(const GUID
& provider_name
)
35 : EtwTraceProvider(provider_name
) {
36 callback_event_
.Set(::CreateEvent(NULL
, TRUE
, FALSE
, NULL
));
39 void WaitForCallback() {
40 ::WaitForSingleObject(callback_event_
.Get(), INFINITE
);
41 ::ResetEvent(callback_event_
.Get());
45 void OnEventsEnabled() override
{ ::SetEvent(callback_event_
.Get()); }
46 void PostEventsDisabled() override
{ ::SetEvent(callback_event_
.Get()); }
48 ScopedHandle callback_event_
;
50 DISALLOW_COPY_AND_ASSIGN(TestingProvider
);
55 TEST(EtwTracePropertiesTest
, Initialization
) {
56 EtwTraceProperties prop
;
58 EVENT_TRACE_PROPERTIES
* p
= prop
.get();
59 EXPECT_NE(0u, p
->Wnode
.BufferSize
);
60 EXPECT_EQ(0u, p
->Wnode
.ProviderId
);
61 EXPECT_EQ(0u, p
->Wnode
.HistoricalContext
);
63 EXPECT_TRUE(kGuidNull
== p
->Wnode
.Guid
);
64 EXPECT_EQ(0, p
->Wnode
.ClientContext
);
65 EXPECT_EQ(WNODE_FLAG_TRACED_GUID
, p
->Wnode
.Flags
);
67 EXPECT_EQ(0, p
->BufferSize
);
68 EXPECT_EQ(0, p
->MinimumBuffers
);
69 EXPECT_EQ(0, p
->MaximumBuffers
);
70 EXPECT_EQ(0, p
->MaximumFileSize
);
71 EXPECT_EQ(0, p
->LogFileMode
);
72 EXPECT_EQ(0, p
->FlushTimer
);
73 EXPECT_EQ(0, p
->EnableFlags
);
74 EXPECT_EQ(0, p
->AgeLimit
);
76 EXPECT_EQ(0, p
->NumberOfBuffers
);
77 EXPECT_EQ(0, p
->FreeBuffers
);
78 EXPECT_EQ(0, p
->EventsLost
);
79 EXPECT_EQ(0, p
->BuffersWritten
);
80 EXPECT_EQ(0, p
->LogBuffersLost
);
81 EXPECT_EQ(0, p
->RealTimeBuffersLost
);
82 EXPECT_EQ(0, p
->LoggerThreadId
);
83 EXPECT_NE(0u, p
->LogFileNameOffset
);
84 EXPECT_NE(0u, p
->LoggerNameOffset
);
87 TEST(EtwTracePropertiesTest
, Strings
) {
88 EtwTraceProperties prop
;
90 ASSERT_STREQ(L
"", prop
.GetLoggerFileName());
91 ASSERT_STREQ(L
"", prop
.GetLoggerName());
93 std::wstring
name(1023, L
'A');
94 ASSERT_HRESULT_SUCCEEDED(prop
.SetLoggerFileName(name
.c_str()));
95 ASSERT_HRESULT_SUCCEEDED(prop
.SetLoggerName(name
.c_str()));
96 ASSERT_STREQ(name
.c_str(), prop
.GetLoggerFileName());
97 ASSERT_STREQ(name
.c_str(), prop
.GetLoggerName());
99 std::wstring
name2(1024, L
'A');
100 ASSERT_HRESULT_FAILED(prop
.SetLoggerFileName(name2
.c_str()));
101 ASSERT_HRESULT_FAILED(prop
.SetLoggerName(name2
.c_str()));
106 class EtwTraceControllerTest
: public testing::Test
{
108 EtwTraceControllerTest()
109 : session_name_(StringPrintf(L
"TestSession-%d", GetCurrentProcId())) {
112 void SetUp() override
{
113 EtwTraceProperties ignore
;
114 EtwTraceController::Stop(session_name_
.c_str(), &ignore
);
116 // Allocate a new provider name GUID for each test.
117 ASSERT_HRESULT_SUCCEEDED(::CoCreateGuid(&test_provider_
));
120 void TearDown() override
{
121 EtwTraceProperties prop
;
122 EtwTraceController::Stop(session_name_
.c_str(), &prop
);
127 std::wstring session_name_
;
132 TEST_F(EtwTraceControllerTest
, Initialize
) {
133 EtwTraceController controller
;
135 EXPECT_EQ(NULL
, controller
.session());
136 EXPECT_STREQ(L
"", controller
.session_name());
140 TEST_F(EtwTraceControllerTest
, StartRealTimeSession
) {
141 EtwTraceController controller
;
143 HRESULT hr
= controller
.StartRealtimeSession(session_name_
.c_str(),
145 if (hr
== E_ACCESSDENIED
) {
146 VLOG(1) << "You must be an administrator to run this test on Vista";
150 EXPECT_TRUE(NULL
!= controller
.session());
151 EXPECT_STREQ(session_name_
.c_str(), controller
.session_name());
153 EXPECT_HRESULT_SUCCEEDED(controller
.Stop(NULL
));
154 EXPECT_EQ(NULL
, controller
.session());
155 EXPECT_STREQ(L
"", controller
.session_name());
158 TEST_F(EtwTraceControllerTest
, StartFileSession
) {
159 ScopedTempDir temp_dir
;
160 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
162 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir
.path(), &temp
));
164 EtwTraceController controller
;
165 HRESULT hr
= controller
.StartFileSession(session_name_
.c_str(),
166 temp
.value().c_str());
167 if (hr
== E_ACCESSDENIED
) {
168 VLOG(1) << "You must be an administrator to run this test on Vista";
169 base::DeleteFile(temp
, false);
173 EXPECT_TRUE(NULL
!= controller
.session());
174 EXPECT_STREQ(session_name_
.c_str(), controller
.session_name());
176 EXPECT_HRESULT_SUCCEEDED(controller
.Stop(NULL
));
177 EXPECT_EQ(NULL
, controller
.session());
178 EXPECT_STREQ(L
"", controller
.session_name());
179 base::DeleteFile(temp
, false);
182 TEST_F(EtwTraceControllerTest
, EnableDisable
) {
183 TestingProvider
provider(test_provider_
);
185 EXPECT_EQ(ERROR_SUCCESS
, provider
.Register());
186 EXPECT_EQ(NULL
, provider
.session_handle());
188 EtwTraceController controller
;
189 HRESULT hr
= controller
.StartRealtimeSession(session_name_
.c_str(),
191 if (hr
== E_ACCESSDENIED
) {
192 VLOG(1) << "You must be an administrator to run this test on Vista";
196 EXPECT_HRESULT_SUCCEEDED(controller
.EnableProvider(test_provider_
,
197 TRACE_LEVEL_VERBOSE
, kTestProviderFlags
));
199 provider
.WaitForCallback();
201 EXPECT_EQ(TRACE_LEVEL_VERBOSE
, provider
.enable_level());
202 EXPECT_EQ(kTestProviderFlags
, provider
.enable_flags());
204 EXPECT_HRESULT_SUCCEEDED(controller
.DisableProvider(test_provider_
));
206 provider
.WaitForCallback();
208 EXPECT_EQ(0, provider
.enable_level());
209 EXPECT_EQ(0, provider
.enable_flags());
211 EXPECT_EQ(ERROR_SUCCESS
, provider
.Unregister());
213 // Enable the provider again, before registering.
214 EXPECT_HRESULT_SUCCEEDED(controller
.EnableProvider(test_provider_
,
215 TRACE_LEVEL_VERBOSE
, kTestProviderFlags
));
217 // Register the provider again, the settings above
218 // should take immediate effect.
219 EXPECT_EQ(ERROR_SUCCESS
, provider
.Register());
221 EXPECT_EQ(TRACE_LEVEL_VERBOSE
, provider
.enable_level());
222 EXPECT_EQ(kTestProviderFlags
, provider
.enable_flags());
224 // Consume the callback event of the previous controller.EnableProvider().
225 provider
.WaitForCallback();
227 EXPECT_HRESULT_SUCCEEDED(controller
.Stop(NULL
));
229 provider
.WaitForCallback();
231 // Session should have wound down.
232 EXPECT_EQ(0, provider
.enable_level());
233 EXPECT_EQ(0, provider
.enable_flags());