1 // Copyright 2010 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 // Unit tests for event trace provider.
6 #include "base/win/event_trace_provider.h"
7
8 #include <initguid.h>
9
10 #include <new>
11
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace {
15
16 using base::win::EtwMofEvent;
17 using base::win::EtwTraceProvider;
18
19 // clang-format off
20 // {7F0FD37F-FA3C-4cd6-9242-DF60967A2CB2}
21 DEFINE_GUID(kTestProvider,
22 0x7f0fd37f, 0xfa3c, 0x4cd6, 0x92, 0x42, 0xdf, 0x60, 0x96, 0x7a, 0x2c, 0xb2);
23 // clang-format on
24
25 // clang-format off
26 // {7F0FD37F-FA3C-4cd6-9242-DF60967A2CB2}
27 DEFINE_GUID(kTestEventClass,
28 0x7f0fd37f, 0xfa3c, 0x4cd6, 0x92, 0x42, 0xdf, 0x60, 0x96, 0x7a, 0x2c, 0xb2);
29 // clang-format on
30
31 } // namespace
32
TEST(EtwTraceProviderTest,ToleratesPreCreateInvocations)33 TEST(EtwTraceProviderTest, ToleratesPreCreateInvocations) {
34 // Because the trace provider is used in logging, it's important that
35 // it be possible to use static provider instances without regard to
36 // whether they've been constructed or destructed.
37 // The interface of the class is designed to tolerate this usage.
38 char buf[sizeof(EtwTraceProvider)] = {0};
39 EtwTraceProvider& provider = reinterpret_cast<EtwTraceProvider&>(buf);
40
41 EXPECT_EQ(0u, provider.registration_handle());
42 EXPECT_EQ(0u, provider.session_handle());
43 EXPECT_EQ(0u, provider.enable_flags());
44 EXPECT_EQ(0u, provider.enable_level());
45
46 EXPECT_FALSE(provider.ShouldLog(TRACE_LEVEL_FATAL, 0xfffffff));
47
48 // We expect these not to crash.
49 provider.Log(kTestEventClass, 0, TRACE_LEVEL_FATAL, "foo");
50 provider.Log(kTestEventClass, 0, TRACE_LEVEL_FATAL, L"foo");
51
52 EtwMofEvent<1> dummy(kTestEventClass, 0, TRACE_LEVEL_FATAL);
53 DWORD data = 0;
54 dummy.SetField(0, sizeof(data), &data);
55 provider.Log(dummy.get());
56
57 // Placement-new the provider into our buffer.
58 new (buf) EtwTraceProvider(kTestProvider);
59
60 // Registration is now safe.
61 EXPECT_EQ(static_cast<ULONG>(ERROR_SUCCESS), provider.Register());
62
63 // Destruct the instance, this should unregister it.
64 provider.EtwTraceProvider::~EtwTraceProvider();
65
66 // And post-destruction, all of the above should still be safe.
67 EXPECT_EQ(0u, provider.registration_handle());
68 EXPECT_EQ(0u, provider.session_handle());
69 EXPECT_EQ(0u, provider.enable_flags());
70 EXPECT_EQ(0u, provider.enable_level());
71
72 EXPECT_FALSE(provider.ShouldLog(TRACE_LEVEL_FATAL, 0xfffffff));
73
74 // We expect these not to crash.
75 provider.Log(kTestEventClass, 0, TRACE_LEVEL_FATAL, "foo");
76 provider.Log(kTestEventClass, 0, TRACE_LEVEL_FATAL, L"foo");
77 provider.Log(dummy.get());
78 }
79
TEST(EtwTraceProviderTest,Initialize)80 TEST(EtwTraceProviderTest, Initialize) {
81 EtwTraceProvider provider(kTestProvider);
82
83 EXPECT_EQ(0u, provider.registration_handle());
84 EXPECT_EQ(0u, provider.session_handle());
85 EXPECT_EQ(0u, provider.enable_flags());
86 EXPECT_EQ(0u, provider.enable_level());
87 }
88
TEST(EtwTraceProviderTest,Register)89 TEST(EtwTraceProviderTest, Register) {
90 EtwTraceProvider provider(kTestProvider);
91
92 ASSERT_EQ(static_cast<ULONG>(ERROR_SUCCESS), provider.Register());
93 EXPECT_NE(0u, provider.registration_handle());
94 ASSERT_EQ(static_cast<ULONG>(ERROR_SUCCESS), provider.Unregister());
95 EXPECT_EQ(0u, provider.registration_handle());
96 }
97
TEST(EtwTraceProviderTest,RegisterWithNoNameFails)98 TEST(EtwTraceProviderTest, RegisterWithNoNameFails) {
99 EtwTraceProvider provider;
100
101 EXPECT_TRUE(provider.Register() != ERROR_SUCCESS);
102 }
103
TEST(EtwTraceProviderTest,Enable)104 TEST(EtwTraceProviderTest, Enable) {
105 EtwTraceProvider provider(kTestProvider);
106
107 ASSERT_EQ(static_cast<ULONG>(ERROR_SUCCESS), provider.Register());
108 EXPECT_NE(0u, provider.registration_handle());
109
110 // No session so far.
111 EXPECT_EQ(0u, provider.session_handle());
112 EXPECT_EQ(0u, provider.enable_flags());
113 EXPECT_EQ(0u, provider.enable_level());
114
115 ASSERT_EQ(static_cast<ULONG>(ERROR_SUCCESS), provider.Unregister());
116 EXPECT_EQ(0u, provider.registration_handle());
117 }
118