1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145

base / trace_event / trace_logging_minimal_win_unittest.cc [blame]

// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/trace_event/trace_logging_minimal_win.h"

#include <gtest/gtest.h>

#include "base/functional/callback_helpers.h"

// Test fixture for TlmProvider
class TlmProviderTest : public ::testing::Test {
 protected:
  TlmProviderTest() = default;

  void MockOSEventRegister() {
    TlmProvider::StaticEnableCallback(nullptr, 1, 1, 0, 0, nullptr, &provider_);
  }

  TlmProvider provider_;
};

// Test default constructor
TEST_F(TlmProviderTest, DefaultConstructor) {
  TlmProvider default_provider;
  EXPECT_FALSE(default_provider.IsEnabled());
}

// Test parameterized constructor
TEST_F(TlmProviderTest, ParameterizedConstructor) {
  EXPECT_FALSE(provider_.IsEnabled());
}

// Test Register method
TEST_F(TlmProviderTest, Register) {
  ULONG result = provider_.Register("TestProvider", GUID(), base::DoNothing());
  EXPECT_EQ(result, 0UL);
}

// Test Unregister method
TEST_F(TlmProviderTest, Unregister) {
  provider_.Register("TestProvider", GUID(), base::DoNothing());
  provider_.Unregister();
  EXPECT_FALSE(provider_.IsEnabled());
}

// Test IsEnabled method
TEST_F(TlmProviderTest, IsEnabled) {
  provider_.Register("TestProvider", GUID(), base::DoNothing());
  EXPECT_FALSE(provider_.IsEnabled());
  MockOSEventRegister();
  EXPECT_TRUE(provider_.IsEnabled());
}

// Test WriteEvent method
TEST_F(TlmProviderTest, WriteEvent) {
  provider_.Register("TestProvider", GUID(), base::DoNothing());
  MockOSEventRegister();

  EXPECT_TRUE(provider_.IsEnabled());
  EVENT_DESCRIPTOR event_descriptor = {};
  ULONG result = provider_.WriteEvent("TestEvent", event_descriptor);
  EXPECT_EQ(result, 0UL);
}

// Test WriteEvent with TlmMbcsStringField
TEST_F(TlmProviderTest, WriteEventWithMbcsStringField) {
  provider_.Register("TestProvider", GUID(), base::DoNothing());
  MockOSEventRegister();

  EXPECT_TRUE(provider_.IsEnabled());
  EVENT_DESCRIPTOR event_descriptor = {};
  TlmMbcsStringField field("TestField", "TestValue");
  ULONG result = provider_.WriteEvent("TestEvent", event_descriptor, field);
  EXPECT_EQ(result, 0UL);
}

// Test WriteEvent with TlmUtf8StringField
TEST_F(TlmProviderTest, WriteEventWithUtf8StringField) {
  provider_.Register("TestProvider", GUID(), base::DoNothing());
  MockOSEventRegister();

  EXPECT_TRUE(provider_.IsEnabled());
  EVENT_DESCRIPTOR event_descriptor = {};
  TlmUtf8StringField field("TestField", "TestValue");
  ULONG result = provider_.WriteEvent("TestEvent", event_descriptor, field);
  EXPECT_EQ(result, 0UL);
}

// Test WriteEvent with TlmInt64Field
TEST_F(TlmProviderTest, WriteEventWithInt64Field) {
  provider_.Register("TestProvider", GUID(), base::DoNothing());
  MockOSEventRegister();

  EXPECT_TRUE(provider_.IsEnabled());
  EVENT_DESCRIPTOR event_descriptor = {};
  TlmInt64Field field("TestField", 1234567890);
  ULONG result = provider_.WriteEvent("TestEvent", event_descriptor, field);
  EXPECT_EQ(result, 0UL);
}

// Test WriteEvent with TlmUInt64Field
TEST_F(TlmProviderTest, WriteEventWithUInt64Field) {
  provider_.Register("TestProvider", GUID(), base::DoNothing());
  MockOSEventRegister();

  EXPECT_TRUE(provider_.IsEnabled());
  EVENT_DESCRIPTOR event_descriptor = {};
  TlmUInt64Field field("TestField", 1234567890);
  ULONG result = provider_.WriteEvent("TestEvent", event_descriptor, field);
  EXPECT_EQ(result, 0UL);
}

// Test fixture for TlmInt64Field
class TlmStringView final : public TlmFieldBase {
 public:
  TlmStringView(std::string_view name) : TlmFieldBase(name) {}
};

class TlmChar final : public TlmFieldBase {
 public:
  TlmChar(const char* name) : TlmFieldBase(name) {}
};

class TlmFieldBaseTest : public ::testing::Test {};

// Test the constructor and Value() method
TEST_F(TlmFieldBaseTest, CharConstructorAndValue) {
  char name[] = {'T', 'e', 's', 't', 'F', 'i', 'e', 'l', 'd', 0};
  unsigned long len = strlen(name);
  TlmChar field(name);

  // Mock doing stuff in the mem around the null terminated string
  name[9] = 0xAB;

  // We should be able to use the string view to initialize
  // Name().data(), this shouldn't cause an error.
  TlmStringView field2(field.Name());

  EXPECT_EQ(field.Name().data(), name);
  EXPECT_EQ(field.Name().size(), len);

  EXPECT_EQ(field2.Name().data(), name);
  EXPECT_EQ(field2.Name().size(), len);
}