1 // Copyright (c) 2010 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 provider.
6 #include "base/win/event_trace_provider.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include <initguid.h> // NOLINT - has to be last
13 using base::win::EtwTraceProvider
;
14 using base::win::EtwMofEvent
;
16 // {7F0FD37F-FA3C-4cd6-9242-DF60967A2CB2}
17 DEFINE_GUID(kTestProvider
,
18 0x7f0fd37f, 0xfa3c, 0x4cd6, 0x92, 0x42, 0xdf, 0x60, 0x96, 0x7a, 0x2c, 0xb2);
20 // {7F0FD37F-FA3C-4cd6-9242-DF60967A2CB2}
21 DEFINE_GUID(kTestEventClass
,
22 0x7f0fd37f, 0xfa3c, 0x4cd6, 0x92, 0x42, 0xdf, 0x60, 0x96, 0x7a, 0x2c, 0xb2);
26 TEST(EtwTraceProviderTest
, ToleratesPreCreateInvocations
) {
27 // Because the trace provider is used in logging, it's important that
28 // it be possible to use static provider instances without regard to
29 // whether they've been constructed or destructed.
30 // The interface of the class is designed to tolerate this usage.
31 char buf
[sizeof(EtwTraceProvider
)] = {0};
32 EtwTraceProvider
& provider
= reinterpret_cast<EtwTraceProvider
&>(buf
);
34 EXPECT_EQ(NULL
, provider
.registration_handle());
35 EXPECT_EQ(NULL
, provider
.session_handle());
36 EXPECT_EQ(0, provider
.enable_flags());
37 EXPECT_EQ(0, provider
.enable_level());
39 EXPECT_FALSE(provider
.ShouldLog(TRACE_LEVEL_FATAL
, 0xfffffff));
41 // We expect these not to crash.
42 provider
.Log(kTestEventClass
, 0, TRACE_LEVEL_FATAL
, "foo");
43 provider
.Log(kTestEventClass
, 0, TRACE_LEVEL_FATAL
, L
"foo");
45 EtwMofEvent
<1> dummy(kTestEventClass
, 0, TRACE_LEVEL_FATAL
);
47 dummy
.SetField(0, sizeof(data
), &data
);
48 provider
.Log(dummy
.get());
50 // Placement-new the provider into our buffer.
51 new (buf
) EtwTraceProvider(kTestProvider
);
53 // Registration is now safe.
54 EXPECT_EQ(ERROR_SUCCESS
, provider
.Register());
56 // Destruct the instance, this should unregister it.
57 provider
.EtwTraceProvider::~EtwTraceProvider();
59 // And post-destruction, all of the above should still be safe.
60 EXPECT_EQ(NULL
, provider
.registration_handle());
61 EXPECT_EQ(NULL
, provider
.session_handle());
62 EXPECT_EQ(0, provider
.enable_flags());
63 EXPECT_EQ(0, provider
.enable_level());
65 EXPECT_FALSE(provider
.ShouldLog(TRACE_LEVEL_FATAL
, 0xfffffff));
67 // We expect these not to crash.
68 provider
.Log(kTestEventClass
, 0, TRACE_LEVEL_FATAL
, "foo");
69 provider
.Log(kTestEventClass
, 0, TRACE_LEVEL_FATAL
, L
"foo");
70 provider
.Log(dummy
.get());
73 TEST(EtwTraceProviderTest
, Initialize
) {
74 EtwTraceProvider
provider(kTestProvider
);
76 EXPECT_EQ(NULL
, provider
.registration_handle());
77 EXPECT_EQ(NULL
, provider
.session_handle());
78 EXPECT_EQ(0, provider
.enable_flags());
79 EXPECT_EQ(0, provider
.enable_level());
82 TEST(EtwTraceProviderTest
, Register
) {
83 EtwTraceProvider
provider(kTestProvider
);
85 ASSERT_EQ(ERROR_SUCCESS
, provider
.Register());
86 EXPECT_NE(NULL
, provider
.registration_handle());
87 ASSERT_EQ(ERROR_SUCCESS
, provider
.Unregister());
88 EXPECT_EQ(NULL
, provider
.registration_handle());
91 TEST(EtwTraceProviderTest
, RegisterWithNoNameFails
) {
92 EtwTraceProvider provider
;
94 EXPECT_TRUE(provider
.Register() != ERROR_SUCCESS
);
97 TEST(EtwTraceProviderTest
, Enable
) {
98 EtwTraceProvider
provider(kTestProvider
);
100 ASSERT_EQ(ERROR_SUCCESS
, provider
.Register());
101 EXPECT_NE(NULL
, provider
.registration_handle());
103 // No session so far.
104 EXPECT_EQ(NULL
, provider
.session_handle());
105 EXPECT_EQ(0, provider
.enable_flags());
106 EXPECT_EQ(0, provider
.enable_level());
108 ASSERT_EQ(ERROR_SUCCESS
, provider
.Unregister());
109 EXPECT_EQ(NULL
, provider
.registration_handle());