1 // Copyright 2014 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.
5 #include "net/log/trace_net_log_observer.h"
10 #include "base/json/json_reader.h"
11 #include "base/logging.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/ref_counted_memory.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/run_loop.h"
16 #include "base/strings/stringprintf.h"
17 #include "base/trace_event/trace_event.h"
18 #include "base/trace_event/trace_event_impl.h"
19 #include "base/values.h"
20 #include "net/log/capturing_net_log.h"
21 #include "net/log/net_log.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 using base::trace_event::TraceLog
;
30 // TraceLog category for NetLog events.
31 const char kNetLogTracingCategory
[] = "netlog";
33 struct TraceEntryInfo
{
38 std::string source_type
;
41 TraceEntryInfo
GetTraceEntryInfoFromValue(const base::DictionaryValue
& value
) {
43 EXPECT_TRUE(value
.GetString("cat", &info
.category
));
44 EXPECT_TRUE(value
.GetString("id", &info
.id
));
45 EXPECT_TRUE(value
.GetString("ph", &info
.phase
));
46 EXPECT_TRUE(value
.GetString("name", &info
.name
));
47 EXPECT_TRUE(value
.GetString("args.source_type", &info
.source_type
));
52 class TraceNetLogObserverTest
: public testing::Test
{
54 TraceNetLogObserverTest() {
55 TraceLog
* tracelog
= TraceLog::GetInstance();
57 DCHECK(!tracelog
->IsEnabled());
58 trace_buffer_
.SetOutputCallback(json_output_
.GetCallback());
59 trace_net_log_observer_
.reset(new TraceNetLogObserver());
60 trace_events_
.reset(new base::ListValue());
63 ~TraceNetLogObserverTest() override
{
64 DCHECK(!TraceLog::GetInstance()->IsEnabled());
67 void OnTraceDataCollected(
68 base::RunLoop
* run_loop
,
69 const scoped_refptr
<base::RefCountedString
>& events_str
,
70 bool has_more_events
) {
71 DCHECK(trace_events_
->empty());
72 trace_buffer_
.Start();
73 trace_buffer_
.AddFragment(events_str
->data());
74 trace_buffer_
.Finish();
76 scoped_ptr
<base::Value
> trace_value
;
77 trace_value
.reset(base::JSONReader::Read(
78 json_output_
.json_output
,
79 base::JSON_PARSE_RFC
| base::JSON_DETACHABLE_CHILDREN
));
81 ASSERT_TRUE(trace_value
) << json_output_
.json_output
;
82 base::ListValue
* trace_events
= NULL
;
83 ASSERT_TRUE(trace_value
->GetAsList(&trace_events
));
85 trace_events_
= FilterNetLogTraceEvents(*trace_events
);
91 static void EnableTraceLog() {
92 TraceLog::GetInstance()->SetEnabled(
93 base::trace_event::CategoryFilter(kNetLogTracingCategory
),
94 TraceLog::RECORDING_MODE
, base::trace_event::TraceOptions());
97 void EndTraceAndFlush() {
98 base::RunLoop run_loop
;
99 TraceLog::GetInstance()->SetDisabled();
100 TraceLog::GetInstance()->Flush(
101 base::Bind(&TraceNetLogObserverTest::OnTraceDataCollected
,
102 base::Unretained(this), base::Unretained(&run_loop
)));
106 void set_trace_net_log_observer(TraceNetLogObserver
* trace_net_log_observer
) {
107 trace_net_log_observer_
.reset(trace_net_log_observer
);
110 static scoped_ptr
<base::ListValue
> FilterNetLogTraceEvents(
111 const base::ListValue
& trace_events
) {
112 scoped_ptr
<base::ListValue
> filtered_trace_events(new base::ListValue());
113 for (size_t i
= 0; i
< trace_events
.GetSize(); i
++) {
114 const base::DictionaryValue
* dict
= NULL
;
115 if (!trace_events
.GetDictionary(i
, &dict
)) {
116 ADD_FAILURE() << "Unexpected non-dictionary event in trace_events";
119 std::string category
;
120 if (!dict
->GetString("cat", &category
)) {
122 << "Unexpected item without a category field in trace_events";
125 if (category
!= kNetLogTracingCategory
)
127 filtered_trace_events
->Append(dict
->DeepCopy());
129 return filtered_trace_events
.Pass();
132 base::ListValue
* trace_events() const { return trace_events_
.get(); }
134 CapturingNetLog
* net_log() { return &net_log_
; }
136 TraceNetLogObserver
* trace_net_log_observer() const {
137 return trace_net_log_observer_
.get();
141 scoped_ptr
<base::ListValue
> trace_events_
;
142 base::trace_event::TraceResultBuffer trace_buffer_
;
143 base::trace_event::TraceResultBuffer::SimpleOutput json_output_
;
144 CapturingNetLog net_log_
;
145 scoped_ptr
<TraceNetLogObserver
> trace_net_log_observer_
;
148 TEST_F(TraceNetLogObserverTest
, TracingNotEnabled
) {
149 trace_net_log_observer()->WatchForTraceStart(net_log());
150 net_log()->AddGlobalEntry(NetLog::TYPE_REQUEST_ALIVE
);
153 trace_net_log_observer()->StopWatchForTraceStart();
155 EXPECT_EQ(0u, trace_events()->GetSize());
158 TEST_F(TraceNetLogObserverTest
, TraceEventCaptured
) {
159 CapturingNetLog::CapturedEntryList entries
;
160 net_log()->GetEntries(&entries
);
161 EXPECT_TRUE(entries
.empty());
163 trace_net_log_observer()->WatchForTraceStart(net_log());
165 BoundNetLog bound_net_log
=
166 BoundNetLog::Make(net_log(), net::NetLog::SOURCE_NONE
);
167 net_log()->AddGlobalEntry(NetLog::TYPE_CANCELLED
);
168 bound_net_log
.BeginEvent(NetLog::TYPE_URL_REQUEST_START_JOB
);
169 bound_net_log
.EndEvent(NetLog::TYPE_REQUEST_ALIVE
);
171 net_log()->GetEntries(&entries
);
172 EXPECT_EQ(3u, entries
.size());
174 trace_net_log_observer()->StopWatchForTraceStart();
175 EXPECT_EQ(3u, trace_events()->GetSize());
176 const base::DictionaryValue
* item1
= NULL
;
177 ASSERT_TRUE(trace_events()->GetDictionary(0, &item1
));
178 const base::DictionaryValue
* item2
= NULL
;
179 ASSERT_TRUE(trace_events()->GetDictionary(1, &item2
));
180 const base::DictionaryValue
* item3
= NULL
;
181 ASSERT_TRUE(trace_events()->GetDictionary(2, &item3
));
183 TraceEntryInfo actual_item1
= GetTraceEntryInfoFromValue(*item1
);
184 TraceEntryInfo actual_item2
= GetTraceEntryInfoFromValue(*item2
);
185 TraceEntryInfo actual_item3
= GetTraceEntryInfoFromValue(*item3
);
186 EXPECT_EQ(kNetLogTracingCategory
, actual_item1
.category
);
187 EXPECT_EQ(base::StringPrintf("0x%d", entries
[0].source
.id
), actual_item1
.id
);
188 EXPECT_EQ(std::string(1, TRACE_EVENT_PHASE_NESTABLE_ASYNC_INSTANT
),
190 EXPECT_EQ(NetLog::EventTypeToString(NetLog::TYPE_CANCELLED
),
192 EXPECT_EQ(NetLog::SourceTypeToString(entries
[0].source
.type
),
193 actual_item1
.source_type
);
195 EXPECT_EQ(kNetLogTracingCategory
, actual_item2
.category
);
196 EXPECT_EQ(base::StringPrintf("0x%d", entries
[1].source
.id
), actual_item2
.id
);
197 EXPECT_EQ(std::string(1, TRACE_EVENT_PHASE_NESTABLE_ASYNC_BEGIN
),
199 EXPECT_EQ(NetLog::EventTypeToString(NetLog::TYPE_URL_REQUEST_START_JOB
),
201 EXPECT_EQ(NetLog::SourceTypeToString(entries
[1].source
.type
),
202 actual_item2
.source_type
);
204 EXPECT_EQ(kNetLogTracingCategory
, actual_item3
.category
);
205 EXPECT_EQ(base::StringPrintf("0x%d", entries
[2].source
.id
), actual_item3
.id
);
206 EXPECT_EQ(std::string(1, TRACE_EVENT_PHASE_NESTABLE_ASYNC_END
),
208 EXPECT_EQ(NetLog::EventTypeToString(NetLog::TYPE_REQUEST_ALIVE
),
210 EXPECT_EQ(NetLog::SourceTypeToString(entries
[2].source
.type
),
211 actual_item3
.source_type
);
214 TEST_F(TraceNetLogObserverTest
, EnableAndDisableTracing
) {
215 trace_net_log_observer()->WatchForTraceStart(net_log());
217 net_log()->AddGlobalEntry(NetLog::TYPE_CANCELLED
);
218 TraceLog::GetInstance()->SetDisabled();
219 net_log()->AddGlobalEntry(NetLog::TYPE_REQUEST_ALIVE
);
221 net_log()->AddGlobalEntry(NetLog::TYPE_URL_REQUEST_START_JOB
);
224 trace_net_log_observer()->StopWatchForTraceStart();
226 CapturingNetLog::CapturedEntryList entries
;
227 net_log()->GetEntries(&entries
);
228 EXPECT_EQ(3u, entries
.size());
229 EXPECT_EQ(2u, trace_events()->GetSize());
230 const base::DictionaryValue
* item1
= NULL
;
231 ASSERT_TRUE(trace_events()->GetDictionary(0, &item1
));
232 const base::DictionaryValue
* item2
= NULL
;
233 ASSERT_TRUE(trace_events()->GetDictionary(1, &item2
));
235 TraceEntryInfo actual_item1
= GetTraceEntryInfoFromValue(*item1
);
236 TraceEntryInfo actual_item2
= GetTraceEntryInfoFromValue(*item2
);
237 EXPECT_EQ(kNetLogTracingCategory
, actual_item1
.category
);
238 EXPECT_EQ(base::StringPrintf("0x%d", entries
[0].source
.id
), actual_item1
.id
);
239 EXPECT_EQ(std::string(1, TRACE_EVENT_PHASE_NESTABLE_ASYNC_INSTANT
),
241 EXPECT_EQ(NetLog::EventTypeToString(NetLog::TYPE_CANCELLED
),
243 EXPECT_EQ(NetLog::SourceTypeToString(entries
[0].source
.type
),
244 actual_item1
.source_type
);
246 EXPECT_EQ(kNetLogTracingCategory
, actual_item2
.category
);
247 EXPECT_EQ(base::StringPrintf("0x%d", entries
[2].source
.id
), actual_item2
.id
);
248 EXPECT_EQ(std::string(1, TRACE_EVENT_PHASE_NESTABLE_ASYNC_INSTANT
),
250 EXPECT_EQ(NetLog::EventTypeToString(NetLog::TYPE_URL_REQUEST_START_JOB
),
252 EXPECT_EQ(NetLog::SourceTypeToString(entries
[2].source
.type
),
253 actual_item2
.source_type
);
256 TEST_F(TraceNetLogObserverTest
, DestroyObserverWhileTracing
) {
257 trace_net_log_observer()->WatchForTraceStart(net_log());
259 net_log()->AddGlobalEntry(NetLog::TYPE_CANCELLED
);
260 trace_net_log_observer()->StopWatchForTraceStart();
261 set_trace_net_log_observer(NULL
);
262 net_log()->AddGlobalEntry(NetLog::TYPE_REQUEST_ALIVE
);
266 CapturingNetLog::CapturedEntryList entries
;
267 net_log()->GetEntries(&entries
);
268 EXPECT_EQ(2u, entries
.size());
269 EXPECT_EQ(1u, trace_events()->GetSize());
271 const base::DictionaryValue
* item1
= NULL
;
272 ASSERT_TRUE(trace_events()->GetDictionary(0, &item1
));
274 TraceEntryInfo actual_item1
= GetTraceEntryInfoFromValue(*item1
);
275 EXPECT_EQ(kNetLogTracingCategory
, actual_item1
.category
);
276 EXPECT_EQ(base::StringPrintf("0x%d", entries
[0].source
.id
), actual_item1
.id
);
277 EXPECT_EQ(std::string(1, TRACE_EVENT_PHASE_NESTABLE_ASYNC_INSTANT
),
279 EXPECT_EQ(NetLog::EventTypeToString(NetLog::TYPE_CANCELLED
),
281 EXPECT_EQ(NetLog::SourceTypeToString(entries
[0].source
.type
),
282 actual_item1
.source_type
);
285 TEST_F(TraceNetLogObserverTest
, DestroyObserverWhileNotTracing
) {
286 trace_net_log_observer()->WatchForTraceStart(net_log());
287 net_log()->AddGlobalEntry(NetLog::TYPE_CANCELLED
);
288 trace_net_log_observer()->StopWatchForTraceStart();
289 set_trace_net_log_observer(NULL
);
290 net_log()->AddGlobalEntry(NetLog::TYPE_REQUEST_ALIVE
);
291 net_log()->AddGlobalEntry(NetLog::TYPE_URL_REQUEST_START_JOB
);
295 CapturingNetLog::CapturedEntryList entries
;
296 net_log()->GetEntries(&entries
);
297 EXPECT_EQ(3u, entries
.size());
298 EXPECT_EQ(0u, trace_events()->GetSize());
301 TEST_F(TraceNetLogObserverTest
, CreateObserverAfterTracingStarts
) {
302 set_trace_net_log_observer(NULL
);
304 set_trace_net_log_observer(new TraceNetLogObserver());
305 trace_net_log_observer()->WatchForTraceStart(net_log());
306 net_log()->AddGlobalEntry(NetLog::TYPE_CANCELLED
);
307 trace_net_log_observer()->StopWatchForTraceStart();
308 net_log()->AddGlobalEntry(NetLog::TYPE_REQUEST_ALIVE
);
309 net_log()->AddGlobalEntry(NetLog::TYPE_URL_REQUEST_START_JOB
);
313 CapturingNetLog::CapturedEntryList entries
;
314 net_log()->GetEntries(&entries
);
315 EXPECT_EQ(3u, entries
.size());
316 EXPECT_EQ(0u, trace_events()->GetSize());
319 TEST_F(TraceNetLogObserverTest
, EventsWithAndWithoutParameters
) {
320 trace_net_log_observer()->WatchForTraceStart(net_log());
322 NetLog::ParametersCallback net_log_callback
;
323 std::string param
= "bar";
324 net_log_callback
= NetLog::StringCallback("foo", ¶m
);
326 net_log()->AddGlobalEntry(NetLog::TYPE_CANCELLED
, net_log_callback
);
327 net_log()->AddGlobalEntry(NetLog::TYPE_REQUEST_ALIVE
);
330 trace_net_log_observer()->StopWatchForTraceStart();
332 CapturingNetLog::CapturedEntryList entries
;
333 net_log()->GetEntries(&entries
);
334 EXPECT_EQ(2u, entries
.size());
335 EXPECT_EQ(2u, trace_events()->GetSize());
336 const base::DictionaryValue
* item1
= NULL
;
337 ASSERT_TRUE(trace_events()->GetDictionary(0, &item1
));
338 const base::DictionaryValue
* item2
= NULL
;
339 ASSERT_TRUE(trace_events()->GetDictionary(1, &item2
));
341 TraceEntryInfo actual_item1
= GetTraceEntryInfoFromValue(*item1
);
342 TraceEntryInfo actual_item2
= GetTraceEntryInfoFromValue(*item2
);
343 EXPECT_EQ(kNetLogTracingCategory
, actual_item1
.category
);
344 EXPECT_EQ(base::StringPrintf("0x%d", entries
[0].source
.id
), actual_item1
.id
);
345 EXPECT_EQ(std::string(1, TRACE_EVENT_PHASE_NESTABLE_ASYNC_INSTANT
),
347 EXPECT_EQ(NetLog::EventTypeToString(NetLog::TYPE_CANCELLED
),
349 EXPECT_EQ(NetLog::SourceTypeToString(entries
[0].source
.type
),
350 actual_item1
.source_type
);
352 EXPECT_EQ(kNetLogTracingCategory
, actual_item2
.category
);
353 EXPECT_EQ(base::StringPrintf("0x%d", entries
[1].source
.id
), actual_item2
.id
);
354 EXPECT_EQ(std::string(1, TRACE_EVENT_PHASE_NESTABLE_ASYNC_INSTANT
),
356 EXPECT_EQ(NetLog::EventTypeToString(NetLog::TYPE_REQUEST_ALIVE
),
358 EXPECT_EQ(NetLog::SourceTypeToString(entries
[1].source
.type
),
359 actual_item2
.source_type
);
361 std::string item1_params
;
362 std::string item2_params
;
363 EXPECT_TRUE(item1
->GetString("args.params.foo", &item1_params
));
364 EXPECT_EQ("bar", item1_params
);
366 EXPECT_TRUE(item2
->GetString("args.params", &item2_params
));
367 EXPECT_TRUE(item2_params
.empty());