event_trace_provider_unittest.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. namespace {
  11. using base::win::EtwMofEvent;
  12. using base::win::EtwTraceProvider;
  13. // clang-format off
  14. // {7F0FD37F-FA3C-4cd6-9242-DF60967A2CB2}
  15. DEFINE_GUID(kTestProvider,
  16. 0x7f0fd37f, 0xfa3c, 0x4cd6, 0x92, 0x42, 0xdf, 0x60, 0x96, 0x7a, 0x2c, 0xb2);
  17. // clang-format on
  18. // clang-format off
  19. // {7F0FD37F-FA3C-4cd6-9242-DF60967A2CB2}
  20. DEFINE_GUID(kTestEventClass,
  21. 0x7f0fd37f, 0xfa3c, 0x4cd6, 0x92, 0x42, 0xdf, 0x60, 0x96, 0x7a, 0x2c, 0xb2);
  22. // clang-format on
  23. } // namespace
  24. TEST(EtwTraceProviderTest, ToleratesPreCreateInvocations) {
  25. // Because the trace provider is used in logging, it's important that
  26. // it be possible to use static provider instances without regard to
  27. // whether they've been constructed or destructed.
  28. // The interface of the class is designed to tolerate this usage.
  29. char buf[sizeof(EtwTraceProvider)] = {0};
  30. EtwTraceProvider& provider = reinterpret_cast<EtwTraceProvider&>(buf);
  31. EXPECT_EQ(0u, provider.registration_handle());
  32. EXPECT_EQ(0u, provider.session_handle());
  33. EXPECT_EQ(0u, provider.enable_flags());
  34. EXPECT_EQ(0u, provider.enable_level());
  35. EXPECT_FALSE(provider.ShouldLog(TRACE_LEVEL_FATAL, 0xfffffff));
  36. // We expect these not to crash.
  37. provider.Log(kTestEventClass, 0, TRACE_LEVEL_FATAL, "foo");
  38. provider.Log(kTestEventClass, 0, TRACE_LEVEL_FATAL, L"foo");
  39. EtwMofEvent<1> dummy(kTestEventClass, 0, TRACE_LEVEL_FATAL);
  40. DWORD data = 0;
  41. dummy.SetField(0, sizeof(data), &data);
  42. provider.Log(dummy.get());
  43. // Placement-new the provider into our buffer.
  44. new (buf) EtwTraceProvider(kTestProvider);
  45. // Registration is now safe.
  46. EXPECT_EQ(static_cast<ULONG>(ERROR_SUCCESS), provider.Register());
  47. // Destruct the instance, this should unregister it.
  48. provider.EtwTraceProvider::~EtwTraceProvider();
  49. // And post-destruction, all of the above should still be safe.
  50. EXPECT_EQ(0u, provider.registration_handle());
  51. EXPECT_EQ(0u, provider.session_handle());
  52. EXPECT_EQ(0u, provider.enable_flags());
  53. EXPECT_EQ(0u, provider.enable_level());
  54. EXPECT_FALSE(provider.ShouldLog(TRACE_LEVEL_FATAL, 0xfffffff));
  55. // We expect these not to crash.
  56. provider.Log(kTestEventClass, 0, TRACE_LEVEL_FATAL, "foo");
  57. provider.Log(kTestEventClass, 0, TRACE_LEVEL_FATAL, L"foo");
  58. provider.Log(dummy.get());
  59. }
  60. TEST(EtwTraceProviderTest, Initialize) {
  61. EtwTraceProvider provider(kTestProvider);
  62. EXPECT_EQ(0u, provider.registration_handle());
  63. EXPECT_EQ(0u, provider.session_handle());
  64. EXPECT_EQ(0u, provider.enable_flags());
  65. EXPECT_EQ(0u, provider.enable_level());
  66. }
  67. TEST(EtwTraceProviderTest, Register) {
  68. EtwTraceProvider provider(kTestProvider);
  69. ASSERT_EQ(static_cast<ULONG>(ERROR_SUCCESS), provider.Register());
  70. EXPECT_NE(0u, provider.registration_handle());
  71. ASSERT_EQ(static_cast<ULONG>(ERROR_SUCCESS), provider.Unregister());
  72. EXPECT_EQ(0u, provider.registration_handle());
  73. }
  74. TEST(EtwTraceProviderTest, RegisterWithNoNameFails) {
  75. EtwTraceProvider provider;
  76. EXPECT_TRUE(provider.Register() != ERROR_SUCCESS);
  77. }
  78. TEST(EtwTraceProviderTest, Enable) {
  79. EtwTraceProvider provider(kTestProvider);
  80. ASSERT_EQ(static_cast<ULONG>(ERROR_SUCCESS), provider.Register());
  81. EXPECT_NE(0u, provider.registration_handle());
  82. // No session so far.
  83. EXPECT_EQ(0u, provider.session_handle());
  84. EXPECT_EQ(0u, provider.enable_flags());
  85. EXPECT_EQ(0u, provider.enable_level());
  86. ASSERT_EQ(static_cast<ULONG>(ERROR_SUCCESS), provider.Unregister());
  87. EXPECT_EQ(0u, provider.registration_handle());
  88. }