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. 4 // 5 // Unit tests for event trace provider. 6 #include "base/win/event_trace_provider.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 using base::win::EtwTraceProvider; 14 using base::win::EtwMofEvent; 15 16 // {7F0FD37F-FA3C-4cd6-9242-DF60967A2CB2} 17 DEFINE_GUID(kTestProvider, 18 0x7f0fd37f, 0xfa3c, 0x4cd6, 0x92, 0x42, 0xdf, 0x60, 0x96, 0x7a, 0x2c, 0xb2); 19 20 // {7F0FD37F-FA3C-4cd6-9242-DF60967A2CB2} 21 DEFINE_GUID(kTestEventClass, 22 0x7f0fd37f, 0xfa3c, 0x4cd6, 0x92, 0x42, 0xdf, 0x60, 0x96, 0x7a, 0x2c, 0xb2); 23 24 } // namespace 25 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); 33 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()); 38 39 EXPECT_FALSE(provider.ShouldLog(TRACE_LEVEL_FATAL, 0xfffffff)); 40 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"); 44 45 EtwMofEvent<1> dummy(kTestEventClass, 0, TRACE_LEVEL_FATAL); 46 DWORD data = 0; 47 dummy.SetField(0, sizeof(data), &data); 48 provider.Log(dummy.get()); 49 50 // Placement-new the provider into our buffer. 51 new (buf) EtwTraceProvider(kTestProvider); 52 53 // Registration is now safe. 54 EXPECT_EQ(ERROR_SUCCESS, provider.Register()); 55 56 // Destruct the instance, this should unregister it. 57 provider.EtwTraceProvider::~EtwTraceProvider(); 58 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()); 64 65 EXPECT_FALSE(provider.ShouldLog(TRACE_LEVEL_FATAL, 0xfffffff)); 66 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()); 71 } 72 73 TEST(EtwTraceProviderTest, Initialize) { 74 EtwTraceProvider provider(kTestProvider); 75 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()); 80 } 81 82 TEST(EtwTraceProviderTest, Register) { 83 EtwTraceProvider provider(kTestProvider); 84 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()); 89 } 90 91 TEST(EtwTraceProviderTest, RegisterWithNoNameFails) { 92 EtwTraceProvider provider; 93 94 EXPECT_TRUE(provider.Register() != ERROR_SUCCESS); 95 } 96 97 TEST(EtwTraceProviderTest, Enable) { 98 EtwTraceProvider provider(kTestProvider); 99 100 ASSERT_EQ(ERROR_SUCCESS, provider.Register()); 101 EXPECT_NE(NULL, provider.registration_handle()); 102 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()); 107 108 ASSERT_EQ(ERROR_SUCCESS, provider.Unregister()); 109 EXPECT_EQ(NULL, provider.registration_handle()); 110 } 111