15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2010 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Unit tests for event trace provider.
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/win/event_trace_provider.h"
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <new>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <initguid.h>  // NOLINT - has to be last
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using base::win::EtwTraceProvider;
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using base::win::EtwMofEvent;
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// {7F0FD37F-FA3C-4cd6-9242-DF60967A2CB2}
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)DEFINE_GUID(kTestProvider,
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  0x7f0fd37f, 0xfa3c, 0x4cd6, 0x92, 0x42, 0xdf, 0x60, 0x96, 0x7a, 0x2c, 0xb2);
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// {7F0FD37F-FA3C-4cd6-9242-DF60967A2CB2}
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)DEFINE_GUID(kTestEventClass,
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  0x7f0fd37f, 0xfa3c, 0x4cd6, 0x92, 0x42, 0xdf, 0x60, 0x96, 0x7a, 0x2c, 0xb2);
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(EtwTraceProviderTest, ToleratesPreCreateInvocations) {
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Because the trace provider is used in logging, it's important that
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // it be possible to use static provider instances without regard to
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // whether they've been constructed or destructed.
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The interface of the class is designed to tolerate this usage.
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  char buf[sizeof(EtwTraceProvider)] = {0};
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EtwTraceProvider& provider = reinterpret_cast<EtwTraceProvider&>(buf);
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(NULL, provider.registration_handle());
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(NULL, provider.session_handle());
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(0, provider.enable_flags());
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(0, provider.enable_level());
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(provider.ShouldLog(TRACE_LEVEL_FATAL, 0xfffffff));
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We expect these not to crash.
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  provider.Log(kTestEventClass, 0, TRACE_LEVEL_FATAL, "foo");
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  provider.Log(kTestEventClass, 0, TRACE_LEVEL_FATAL, L"foo");
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EtwMofEvent<1> dummy(kTestEventClass, 0, TRACE_LEVEL_FATAL);
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DWORD data = 0;
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  dummy.SetField(0, sizeof(data), &data);
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  provider.Log(dummy.get());
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Placement-new the provider into our buffer.
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  new (buf) EtwTraceProvider(kTestProvider);
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Registration is now safe.
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(ERROR_SUCCESS, provider.Register());
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Destruct the instance, this should unregister it.
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  provider.EtwTraceProvider::~EtwTraceProvider();
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // And post-destruction, all of the above should still be safe.
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(NULL, provider.registration_handle());
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(NULL, provider.session_handle());
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(0, provider.enable_flags());
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(0, provider.enable_level());
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(provider.ShouldLog(TRACE_LEVEL_FATAL, 0xfffffff));
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We expect these not to crash.
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  provider.Log(kTestEventClass, 0, TRACE_LEVEL_FATAL, "foo");
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  provider.Log(kTestEventClass, 0, TRACE_LEVEL_FATAL, L"foo");
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  provider.Log(dummy.get());
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(EtwTraceProviderTest, Initialize) {
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EtwTraceProvider provider(kTestProvider);
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(NULL, provider.registration_handle());
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(NULL, provider.session_handle());
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(0, provider.enable_flags());
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(0, provider.enable_level());
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(EtwTraceProviderTest, Register) {
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EtwTraceProvider provider(kTestProvider);
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_EQ(ERROR_SUCCESS, provider.Register());
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_NE(NULL, provider.registration_handle());
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_EQ(ERROR_SUCCESS, provider.Unregister());
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(NULL, provider.registration_handle());
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(EtwTraceProviderTest, RegisterWithNoNameFails) {
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EtwTraceProvider provider;
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(provider.Register() != ERROR_SUCCESS);
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(EtwTraceProviderTest, Enable) {
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EtwTraceProvider provider(kTestProvider);
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_EQ(ERROR_SUCCESS, provider.Register());
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_NE(NULL, provider.registration_handle());
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // No session so far.
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(NULL, provider.session_handle());
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(0, provider.enable_flags());
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(0, provider.enable_level());
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_EQ(ERROR_SUCCESS, provider.Unregister());
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(NULL, provider.registration_handle());
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
111