summaryrefslogtreecommitdiffstats
path: root/base/trace_event_win_unittest.cc
blob: 79a48e30005ab4acc3012858adda5ed50c7ea9e6 (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/trace_event.h"

#include <strstream>

#include "base/at_exit.h"
#include "base/basictypes.h"
#include "base/file_util.h"
#include "base/event_trace_consumer_win.h"
#include "base/event_trace_controller_win.h"
#include "base/win/windows_version.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include <initguid.h>  // NOLINT - must be last include.

namespace {
using testing::_;
using testing::AnyNumber;
using testing::InSequence;
using testing::Ge;
using testing::Le;
using testing::NotNull;

// Data for unittests traces.
const char kEmpty[] = "";
const char kName[] = "unittest.trace_name";
const char kExtra[] = "UnittestDummyExtraString";
const void* kId = kName;

const wchar_t kTestSessionName[] = L"TraceEvent unittest session";

MATCHER_P(BufferStartsWith, str, "Buffer starts with") {
  return memcmp(arg, str.c_str(), str.length()) == 0;
}

// Duplicated from <evntrace.h> to fix link problems.
DEFINE_GUID( /* 68fdd900-4a3e-11d1-84f4-0000f80464e3 */
    kEventTraceGuid,
    0x68fdd900,
    0x4a3e,
    0x11d1,
    0x84, 0xf4, 0x00, 0x00, 0xf8, 0x04, 0x64, 0xe3);

class TestEventConsumer: public EtwTraceConsumerBase<TestEventConsumer> {
 public:
  TestEventConsumer() {
    EXPECT_TRUE(current_ == NULL);
    current_ = this;
  }

  ~TestEventConsumer() {
    EXPECT_TRUE(current_ == this);
    current_ = NULL;
  }

  MOCK_METHOD4(Event, void(REFGUID event_class,
                      EtwEventType event_type,
                      size_t buf_len,
                      const void* buf));

  static void ProcessEvent(EVENT_TRACE* event) {
    ASSERT_TRUE(current_ != NULL);
    current_->Event(event->Header.Guid,
                    event->Header.Class.Type,
                    event->MofLength,
                    event->MofData);
  }

 private:
  static TestEventConsumer* current_;
};

TestEventConsumer* TestEventConsumer::current_ = NULL;

class TraceEventTest: public testing::Test {
 public:
  TraceEventTest() {
  }

  void SetUp() {
    bool is_xp = base::win::GetVersion() < base::win::VERSION_VISTA;

    if (is_xp) {
      // Tear down any dangling session from an earlier failing test.
      EtwTraceProperties ignore;
      EtwTraceController::Stop(kTestSessionName, &ignore);
    }

    // Resurrect and initialize the TraceLog singleton instance.
    // On Vista and better, we need the provider registered before we
    // start the private, in-proc session, but on XP we need the global
    // session created and the provider enabled before we register our
    // provider.
    base::TraceLog* tracelog = NULL;
    if (!is_xp) {
      base::TraceLog::Resurrect();
      tracelog = base::TraceLog::Get();
      ASSERT_TRUE(tracelog != NULL);
      ASSERT_FALSE(tracelog->IsTracing());
    }

    // Create the log file.
    ASSERT_TRUE(file_util::CreateTemporaryFile(&log_file_));

    // Create a private log session on the file.
    EtwTraceProperties prop;
    ASSERT_HRESULT_SUCCEEDED(prop.SetLoggerFileName(log_file_.value().c_str()));
    EVENT_TRACE_PROPERTIES& p = *prop.get();
    p.Wnode.ClientContext = 1;  // QPC timer accuracy.
    p.LogFileMode = EVENT_TRACE_FILE_MODE_SEQUENTIAL;   // Sequential log.

    // On Vista and later, we create a private in-process log session, because
    // otherwise we'd need administrator privileges. Unfortunately we can't
    // do the same on XP and better, because the semantics of a private
    // logger session are different, and the IN_PROC flag is not supported.
    if (!is_xp) {
      p.LogFileMode |= EVENT_TRACE_PRIVATE_IN_PROC |  // In-proc for non-admin.
          EVENT_TRACE_PRIVATE_LOGGER_MODE;  // Process-private log.
    }

    p.MaximumFileSize = 100;  // 100M file size.
    p.FlushTimer = 1;  // 1 second flush lag.
    ASSERT_HRESULT_SUCCEEDED(controller_.Start(kTestSessionName, &prop));

    // Enable the TraceLog provider GUID.
    ASSERT_HRESULT_SUCCEEDED(
        controller_.EnableProvider(base::kChromeTraceProviderName,
                                   TRACE_LEVEL_INFORMATION,
                                   0));

    if (is_xp) {
      base::TraceLog::Resurrect();
      tracelog = base::TraceLog::Get();
    }
    ASSERT_TRUE(tracelog != NULL);
    EXPECT_TRUE(tracelog->IsTracing());
  }

  void TearDown() {
    EtwTraceProperties prop;
    if (controller_.session() != 0)
      EXPECT_HRESULT_SUCCEEDED(controller_.Stop(&prop));

    if (!log_file_.value().empty())
      file_util::Delete(log_file_, false);
  }

  void ExpectEvent(REFGUID guid,
                   EtwEventType type,
                   const char* name,
                   size_t name_len,
                   const void* id,
                   const char* extra,
                   size_t extra_len) {
    // Build the trace event buffer we expect will result from this.
    std::stringbuf str;
    str.sputn(name, name_len + 1);
    str.sputn(reinterpret_cast<const char*>(&id), sizeof(id));
    str.sputn(extra, extra_len + 1);

    // And set up the expectation for the event callback.
    EXPECT_CALL(consumer_, Event(guid,
                                 type,
                                 testing::Ge(str.str().length()),
                                 BufferStartsWith(str.str())));
  }

  void ExpectPlayLog() {
    // Ignore EventTraceGuid events.
    EXPECT_CALL(consumer_, Event(kEventTraceGuid, _, _, _))
        .Times(AnyNumber());
  }

  void PlayLog() {
    EtwTraceProperties prop;
    EXPECT_HRESULT_SUCCEEDED(controller_.Flush(&prop));
    EXPECT_HRESULT_SUCCEEDED(controller_.Stop(&prop));
    ASSERT_HRESULT_SUCCEEDED(
        consumer_.OpenFileSession(log_file_.value().c_str()));

    ASSERT_HRESULT_SUCCEEDED(consumer_.Consume());
  }

 private:
  // We want our singleton torn down after each test.
  base::ShadowingAtExitManager at_exit_manager_;
  EtwTraceController controller_;
  FilePath log_file_;
  TestEventConsumer consumer_;
};

}  // namespace


TEST_F(TraceEventTest, TraceLog) {
  ExpectPlayLog();

  // The events should arrive in the same sequence as the expects.
  InSequence in_sequence;

  // Full argument version, passing lengths explicitly.
  base::TraceLog::Trace(kName,
                        strlen(kName),
                        base::TraceLog::EVENT_BEGIN,
                        kId,
                        kExtra,
                        strlen(kExtra));

  ExpectEvent(base::kTraceEventClass32,
              base::kTraceEventTypeBegin,
              kName, strlen(kName),
              kId,
              kExtra, strlen(kExtra));

  // Const char* version.
  base::TraceLog::Trace(static_cast<const char*>(kName),
                        base::TraceLog::EVENT_END,
                        kId,
                        static_cast<const char*>(kExtra));

  ExpectEvent(base::kTraceEventClass32,
              base::kTraceEventTypeEnd,
              kName, strlen(kName),
              kId,
              kExtra, strlen(kExtra));

  // std::string extra version.
  base::TraceLog::Trace(static_cast<const char*>(kName),
                        base::TraceLog::EVENT_INSTANT,
                        kId,
                        std::string(kExtra));

  ExpectEvent(base::kTraceEventClass32,
              base::kTraceEventTypeInstant,
              kName, strlen(kName),
              kId,
              kExtra, strlen(kExtra));


  // Test for sanity on NULL inputs.
  base::TraceLog::Trace(NULL,
                        0,
                        base::TraceLog::EVENT_BEGIN,
                        kId,
                        NULL,
                        0);

  ExpectEvent(base::kTraceEventClass32,
              base::kTraceEventTypeBegin,
              kEmpty, 0,
              kId,
              kEmpty, 0);

  base::TraceLog::Trace(NULL,
                        -1,
                        base::TraceLog::EVENT_END,
                        kId,
                        NULL,
                        -1);

  ExpectEvent(base::kTraceEventClass32,
              base::kTraceEventTypeEnd,
              kEmpty, 0,
              kId,
              kEmpty, 0);

  PlayLog();
}

TEST_F(TraceEventTest, Macros) {
  ExpectPlayLog();

  // The events should arrive in the same sequence as the expects.
  InSequence in_sequence;

  TRACE_EVENT_BEGIN(kName, kId, kExtra);
  ExpectEvent(base::kTraceEventClass32,
              base::kTraceEventTypeBegin,
              kName, strlen(kName),
              kId,
              kExtra, strlen(kExtra));

  TRACE_EVENT_END(kName, kId, kExtra);
  ExpectEvent(base::kTraceEventClass32,
              base::kTraceEventTypeEnd,
              kName, strlen(kName),
              kId,
              kExtra, strlen(kExtra));

  TRACE_EVENT_INSTANT(kName, kId, kExtra);
  ExpectEvent(base::kTraceEventClass32,
              base::kTraceEventTypeInstant,
              kName, strlen(kName),
              kId,
              kExtra, strlen(kExtra));

  PlayLog();
}