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