1 // Copyright (c) 2009 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.
4 //
5 // Unit tests for event trace provider.
6 #include "base/event_trace_provider_win.h"
7 #include <new>
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include <initguid.h> // NOLINT - has to be last
10
11 namespace {
12
13 // {7F0FD37F-FA3C-4cd6-9242-DF60967A2CB2}
14 DEFINE_GUID(kTestProvider,
15 0x7f0fd37f, 0xfa3c, 0x4cd6, 0x92, 0x42, 0xdf, 0x60, 0x96, 0x7a, 0x2c, 0xb2);
16
17 // {7F0FD37F-FA3C-4cd6-9242-DF60967A2CB2}
18 DEFINE_GUID(kTestEventClass,
19 0x7f0fd37f, 0xfa3c, 0x4cd6, 0x92, 0x42, 0xdf, 0x60, 0x96, 0x7a, 0x2c, 0xb2);
20
21 } // namespace
22
TEST(EtwTraceProviderTest,ToleratesPreCreateInvocations)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);
30
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());
35
36 EXPECT_FALSE(provider.ShouldLog(TRACE_LEVEL_FATAL, 0xfffffff));
37
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");
41
42 EtwMofEvent<1> dummy(kTestEventClass, 0, TRACE_LEVEL_FATAL);
43 DWORD data = 0;
44 dummy.SetField(0, sizeof(data), &data);
45 provider.Log(dummy.get());
46
47 // Placement-new the provider into our buffer.
48 new (buf) EtwTraceProvider(kTestProvider);
49
50 // Registration is now safe.
51 EXPECT_EQ(ERROR_SUCCESS, provider.Register());
52
53 // Destruct the instance, this should unregister it.
54 provider.EtwTraceProvider::~EtwTraceProvider();
55
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());
61
62 EXPECT_FALSE(provider.ShouldLog(TRACE_LEVEL_FATAL, 0xfffffff));
63
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());
68 }
69
TEST(EtwTraceProviderTest,Initialize)70 TEST(EtwTraceProviderTest, Initialize) {
71 EtwTraceProvider provider(kTestProvider);
72
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());
77 }
78
TEST(EtwTraceProviderTest,Register)79 TEST(EtwTraceProviderTest, Register) {
80 EtwTraceProvider provider(kTestProvider);
81
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());
86 }
87
TEST(EtwTraceProviderTest,RegisterWithNoNameFails)88 TEST(EtwTraceProviderTest, RegisterWithNoNameFails) {
89 EtwTraceProvider provider;
90
91 EXPECT_NE(ERROR_SUCCESS, provider.Register());
92 }
93
TEST(EtwTraceProviderTest,Enable)94 TEST(EtwTraceProviderTest, Enable) {
95 EtwTraceProvider provider(kTestProvider);
96
97 ASSERT_EQ(ERROR_SUCCESS, provider.Register());
98 EXPECT_NE(NULL, provider.registration_handle());
99
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());
104
105 ASSERT_EQ(ERROR_SUCCESS, provider.Unregister());
106 EXPECT_EQ(NULL, provider.registration_handle());
107 }
108