OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "net/base/trace_net_log_observer.h" |
| 6 |
| 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/debug/trace_event.h" |
| 11 #include "base/debug/trace_event_impl.h" |
| 12 #include "base/json/json_reader.h" |
| 13 #include "base/logging.h" |
| 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/ref_counted_memory.h" |
| 16 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/run_loop.h" |
| 18 #include "base/strings/stringprintf.h" |
| 19 #include "base/values.h" |
| 20 #include "net/base/capturing_net_log.h" |
| 21 #include "net/base/net_log.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" |
| 23 |
| 24 using base::debug::TraceLog; |
| 25 |
| 26 namespace net { |
| 27 |
| 28 namespace { |
| 29 |
| 30 struct TraceEntryInfo { |
| 31 std::string category; |
| 32 std::string id; |
| 33 std::string phase; |
| 34 std::string name; |
| 35 std::string source_type; |
| 36 }; |
| 37 |
| 38 TraceEntryInfo GetTraceEntryInfoFromValue(const base::DictionaryValue& value) { |
| 39 TraceEntryInfo info; |
| 40 EXPECT_TRUE(value.GetString("cat", &info.category)); |
| 41 EXPECT_TRUE(value.GetString("id", &info.id)); |
| 42 EXPECT_TRUE(value.GetString("ph", &info.phase)); |
| 43 EXPECT_TRUE(value.GetString("name", &info.name)); |
| 44 EXPECT_TRUE(value.GetString("args.source_type", &info.source_type)); |
| 45 |
| 46 return info; |
| 47 } |
| 48 |
| 49 class TraceNetLogObserverTest : public testing::Test { |
| 50 public: |
| 51 TraceNetLogObserverTest() { |
| 52 TraceLog::DeleteForTesting(); |
| 53 TraceLog* tracelog = TraceLog::GetInstance(); |
| 54 DCHECK(tracelog); |
| 55 DCHECK(!tracelog->IsEnabled()); |
| 56 trace_buffer_.SetOutputCallback(json_output_.GetCallback()); |
| 57 trace_net_log_observer_.reset(new TraceNetLogObserver()); |
| 58 trace_events_.reset(new base::ListValue()); |
| 59 } |
| 60 |
| 61 virtual ~TraceNetLogObserverTest() { |
| 62 DCHECK(!TraceLog::GetInstance()->IsEnabled()); |
| 63 TraceLog::DeleteForTesting(); |
| 64 } |
| 65 |
| 66 void OnTraceDataCollected( |
| 67 base::RunLoop* run_loop, |
| 68 const scoped_refptr<base::RefCountedString>& events_str, |
| 69 bool has_more_events) { |
| 70 DCHECK(trace_events_->empty()); |
| 71 trace_buffer_.Start(); |
| 72 trace_buffer_.AddFragment(events_str->data()); |
| 73 trace_buffer_.Finish(); |
| 74 |
| 75 scoped_ptr<base::Value> trace_value; |
| 76 trace_value.reset(base::JSONReader::Read( |
| 77 json_output_.json_output, |
| 78 base::JSON_PARSE_RFC | base::JSON_DETACHABLE_CHILDREN)); |
| 79 |
| 80 ASSERT_TRUE(trace_value) << json_output_.json_output; |
| 81 base::ListValue* trace_events = NULL; |
| 82 ASSERT_TRUE(trace_value->GetAsList(&trace_events)); |
| 83 |
| 84 trace_events_ = FilterNetLogTraceEvents(*trace_events); |
| 85 |
| 86 if (!has_more_events) |
| 87 run_loop->Quit(); |
| 88 } |
| 89 |
| 90 static void EnableTraceLog() { |
| 91 TraceLog::GetInstance()->SetEnabled(base::debug::CategoryFilter("netlog"), |
| 92 TraceLog::RECORDING_MODE, |
| 93 base::debug::TraceOptions()); |
| 94 } |
| 95 |
| 96 void EndTraceAndFlush() { |
| 97 base::RunLoop run_loop; |
| 98 TraceLog::GetInstance()->SetDisabled(); |
| 99 TraceLog::GetInstance()->Flush( |
| 100 base::Bind(&TraceNetLogObserverTest::OnTraceDataCollected, |
| 101 base::Unretained(this), |
| 102 base::Unretained(&run_loop))); |
| 103 run_loop.Run(); |
| 104 } |
| 105 |
| 106 void set_trace_net_log_observer(TraceNetLogObserver* trace_net_log_observer) { |
| 107 trace_net_log_observer_.reset(trace_net_log_observer); |
| 108 } |
| 109 |
| 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"; |
| 117 continue; |
| 118 } |
| 119 std::string category; |
| 120 if (!dict->GetString("cat", &category)) { |
| 121 ADD_FAILURE() |
| 122 << "Unexpected item without a category field in trace_events"; |
| 123 continue; |
| 124 } |
| 125 if (category != "netlog") |
| 126 continue; |
| 127 filtered_trace_events->Append(dict->DeepCopy()); |
| 128 } |
| 129 return filtered_trace_events.Pass(); |
| 130 } |
| 131 |
| 132 base::ListValue* trace_events() const { |
| 133 return trace_events_.get(); |
| 134 } |
| 135 |
| 136 CapturingNetLog* net_log() { |
| 137 return &net_log_; |
| 138 } |
| 139 |
| 140 TraceNetLogObserver* trace_net_log_observer() const { |
| 141 return trace_net_log_observer_.get(); |
| 142 } |
| 143 |
| 144 private: |
| 145 scoped_ptr<base::ListValue> trace_events_; |
| 146 base::debug::TraceResultBuffer trace_buffer_; |
| 147 base::debug::TraceResultBuffer::SimpleOutput json_output_; |
| 148 CapturingNetLog net_log_; |
| 149 scoped_ptr<TraceNetLogObserver> trace_net_log_observer_; |
| 150 }; |
| 151 |
| 152 TEST_F(TraceNetLogObserverTest, TracingNotEnabled) { |
| 153 trace_net_log_observer()->WatchForTraceStart(net_log()); |
| 154 net_log()->AddGlobalEntry(NetLog::TYPE_REQUEST_ALIVE); |
| 155 |
| 156 EndTraceAndFlush(); |
| 157 trace_net_log_observer()->StopWatchForTraceStart(); |
| 158 |
| 159 EXPECT_EQ(0u, trace_events()->GetSize()); |
| 160 } |
| 161 |
| 162 TEST_F(TraceNetLogObserverTest, TraceEventCaptured) { |
| 163 CapturingNetLog::CapturedEntryList entries; |
| 164 net_log()->GetEntries(&entries); |
| 165 EXPECT_TRUE(entries.empty()); |
| 166 |
| 167 trace_net_log_observer()->WatchForTraceStart(net_log()); |
| 168 EnableTraceLog(); |
| 169 BoundNetLog bound_net_log = |
| 170 BoundNetLog::Make(net_log(), net::NetLog::SOURCE_NONE); |
| 171 net_log()->AddGlobalEntry(NetLog::TYPE_CANCELLED); |
| 172 bound_net_log.BeginEvent(NetLog::TYPE_URL_REQUEST_START_JOB); |
| 173 bound_net_log.EndEvent(NetLog::TYPE_REQUEST_ALIVE); |
| 174 |
| 175 net_log()->GetEntries(&entries); |
| 176 EXPECT_EQ(3u, entries.size()); |
| 177 EndTraceAndFlush(); |
| 178 trace_net_log_observer()->StopWatchForTraceStart(); |
| 179 EXPECT_EQ(3u, trace_events()->GetSize()); |
| 180 const base::DictionaryValue* item1 = NULL; |
| 181 ASSERT_TRUE(trace_events()->GetDictionary(0, &item1)); |
| 182 const base::DictionaryValue* item2 = NULL; |
| 183 ASSERT_TRUE(trace_events()->GetDictionary(1, &item2)); |
| 184 const base::DictionaryValue* item3 = NULL; |
| 185 ASSERT_TRUE(trace_events()->GetDictionary(2, &item3)); |
| 186 |
| 187 TraceEntryInfo actual_item1 = GetTraceEntryInfoFromValue(*item1); |
| 188 TraceEntryInfo actual_item2 = GetTraceEntryInfoFromValue(*item2); |
| 189 TraceEntryInfo actual_item3 = GetTraceEntryInfoFromValue(*item3); |
| 190 EXPECT_EQ("netlog", actual_item1.category); |
| 191 EXPECT_EQ(base::StringPrintf("0x%d", entries[0].source.id), actual_item1.id); |
| 192 EXPECT_EQ(std::string(1, TRACE_EVENT_PHASE_NESTABLE_ASYNC_INSTANT), |
| 193 actual_item1.phase); |
| 194 EXPECT_EQ(NetLog::EventTypeToString(NetLog::TYPE_CANCELLED), |
| 195 actual_item1.name); |
| 196 EXPECT_EQ(NetLog::SourceTypeToString(entries[0].source.type), |
| 197 actual_item1.source_type); |
| 198 |
| 199 EXPECT_EQ("netlog", actual_item2.category); |
| 200 EXPECT_EQ(base::StringPrintf("0x%d", entries[1].source.id), actual_item2.id); |
| 201 EXPECT_EQ(std::string(1, TRACE_EVENT_PHASE_NESTABLE_ASYNC_BEGIN), |
| 202 actual_item2.phase); |
| 203 EXPECT_EQ(NetLog::EventTypeToString(NetLog::TYPE_URL_REQUEST_START_JOB), |
| 204 actual_item2.name); |
| 205 EXPECT_EQ(NetLog::SourceTypeToString(entries[1].source.type), |
| 206 actual_item2.source_type); |
| 207 |
| 208 EXPECT_EQ("netlog", actual_item3.category); |
| 209 EXPECT_EQ(base::StringPrintf("0x%d", entries[2].source.id), actual_item3.id); |
| 210 EXPECT_EQ(std::string(1, TRACE_EVENT_PHASE_NESTABLE_ASYNC_END), |
| 211 actual_item3.phase); |
| 212 EXPECT_EQ(NetLog::EventTypeToString(NetLog::TYPE_REQUEST_ALIVE), |
| 213 actual_item3.name); |
| 214 EXPECT_EQ(NetLog::SourceTypeToString(entries[2].source.type), |
| 215 actual_item3.source_type); |
| 216 } |
| 217 |
| 218 TEST_F(TraceNetLogObserverTest, EnableAndDisableTracing) { |
| 219 trace_net_log_observer()->WatchForTraceStart(net_log()); |
| 220 EnableTraceLog(); |
| 221 net_log()->AddGlobalEntry(NetLog::TYPE_CANCELLED); |
| 222 TraceLog::GetInstance()->SetDisabled(); |
| 223 net_log()->AddGlobalEntry(NetLog::TYPE_REQUEST_ALIVE); |
| 224 EnableTraceLog(); |
| 225 net_log()->AddGlobalEntry(NetLog::TYPE_URL_REQUEST_START_JOB); |
| 226 |
| 227 EndTraceAndFlush(); |
| 228 trace_net_log_observer()->StopWatchForTraceStart(); |
| 229 |
| 230 CapturingNetLog::CapturedEntryList entries; |
| 231 net_log()->GetEntries(&entries); |
| 232 EXPECT_EQ(3u, entries.size()); |
| 233 EXPECT_EQ(2u, trace_events()->GetSize()); |
| 234 const base::DictionaryValue* item1 = NULL; |
| 235 ASSERT_TRUE(trace_events()->GetDictionary(0, &item1)); |
| 236 const base::DictionaryValue* item2 = NULL; |
| 237 ASSERT_TRUE(trace_events()->GetDictionary(1, &item2)); |
| 238 |
| 239 TraceEntryInfo actual_item1 = GetTraceEntryInfoFromValue(*item1); |
| 240 TraceEntryInfo actual_item2 = GetTraceEntryInfoFromValue(*item2); |
| 241 EXPECT_EQ("netlog", actual_item1.category); |
| 242 EXPECT_EQ(base::StringPrintf("0x%d", entries[0].source.id), actual_item1.id); |
| 243 EXPECT_EQ(std::string(1, TRACE_EVENT_PHASE_NESTABLE_ASYNC_INSTANT), |
| 244 actual_item1.phase); |
| 245 EXPECT_EQ(NetLog::EventTypeToString(NetLog::TYPE_CANCELLED), |
| 246 actual_item1.name); |
| 247 EXPECT_EQ(NetLog::SourceTypeToString(entries[0].source.type), |
| 248 actual_item1.source_type); |
| 249 |
| 250 EXPECT_EQ("netlog", actual_item2.category); |
| 251 EXPECT_EQ(base::StringPrintf("0x%d", entries[2].source.id), actual_item2.id); |
| 252 EXPECT_EQ(std::string(1, TRACE_EVENT_PHASE_NESTABLE_ASYNC_INSTANT), |
| 253 actual_item2.phase); |
| 254 EXPECT_EQ(NetLog::EventTypeToString(NetLog::TYPE_URL_REQUEST_START_JOB), |
| 255 actual_item2.name); |
| 256 EXPECT_EQ(NetLog::SourceTypeToString(entries[2].source.type), |
| 257 actual_item2.source_type); |
| 258 } |
| 259 |
| 260 TEST_F(TraceNetLogObserverTest, DestroyObserverWhileTracing) { |
| 261 trace_net_log_observer()->WatchForTraceStart(net_log()); |
| 262 EnableTraceLog(); |
| 263 net_log()->AddGlobalEntry(NetLog::TYPE_CANCELLED); |
| 264 trace_net_log_observer()->StopWatchForTraceStart(); |
| 265 set_trace_net_log_observer(NULL); |
| 266 net_log()->AddGlobalEntry(NetLog::TYPE_REQUEST_ALIVE); |
| 267 |
| 268 EndTraceAndFlush(); |
| 269 |
| 270 CapturingNetLog::CapturedEntryList entries; |
| 271 net_log()->GetEntries(&entries); |
| 272 EXPECT_EQ(2u, entries.size()); |
| 273 EXPECT_EQ(1u, trace_events()->GetSize()); |
| 274 |
| 275 const base::DictionaryValue* item1 = NULL; |
| 276 ASSERT_TRUE(trace_events()->GetDictionary(0, &item1)); |
| 277 |
| 278 TraceEntryInfo actual_item1 = GetTraceEntryInfoFromValue(*item1); |
| 279 EXPECT_EQ("netlog", actual_item1.category); |
| 280 EXPECT_EQ(base::StringPrintf("0x%d", entries[0].source.id), actual_item1.id); |
| 281 EXPECT_EQ(std::string(1, TRACE_EVENT_PHASE_NESTABLE_ASYNC_INSTANT), |
| 282 actual_item1.phase); |
| 283 EXPECT_EQ(NetLog::EventTypeToString(NetLog::TYPE_CANCELLED), |
| 284 actual_item1.name); |
| 285 EXPECT_EQ(NetLog::SourceTypeToString(entries[0].source.type), |
| 286 actual_item1.source_type); |
| 287 } |
| 288 |
| 289 TEST_F(TraceNetLogObserverTest, DestroyObserverWhileNotTracing) { |
| 290 trace_net_log_observer()->WatchForTraceStart(net_log()); |
| 291 net_log()->AddGlobalEntry(NetLog::TYPE_CANCELLED); |
| 292 trace_net_log_observer()->StopWatchForTraceStart(); |
| 293 set_trace_net_log_observer(NULL); |
| 294 net_log()->AddGlobalEntry(NetLog::TYPE_REQUEST_ALIVE); |
| 295 net_log()->AddGlobalEntry(NetLog::TYPE_URL_REQUEST_START_JOB); |
| 296 |
| 297 EndTraceAndFlush(); |
| 298 |
| 299 CapturingNetLog::CapturedEntryList entries; |
| 300 net_log()->GetEntries(&entries); |
| 301 EXPECT_EQ(3u, entries.size()); |
| 302 EXPECT_EQ(0u, trace_events()->GetSize()); |
| 303 } |
| 304 |
| 305 TEST_F(TraceNetLogObserverTest, CreateObserverAfterTracingStarts) { |
| 306 set_trace_net_log_observer(NULL); |
| 307 EnableTraceLog(); |
| 308 set_trace_net_log_observer(new TraceNetLogObserver()); |
| 309 trace_net_log_observer()->WatchForTraceStart(net_log()); |
| 310 net_log()->AddGlobalEntry(NetLog::TYPE_CANCELLED); |
| 311 trace_net_log_observer()->StopWatchForTraceStart(); |
| 312 net_log()->AddGlobalEntry(NetLog::TYPE_REQUEST_ALIVE); |
| 313 net_log()->AddGlobalEntry(NetLog::TYPE_URL_REQUEST_START_JOB); |
| 314 |
| 315 EndTraceAndFlush(); |
| 316 |
| 317 CapturingNetLog::CapturedEntryList entries; |
| 318 net_log()->GetEntries(&entries); |
| 319 EXPECT_EQ(3u, entries.size()); |
| 320 EXPECT_EQ(0u, trace_events()->GetSize()); |
| 321 } |
| 322 |
| 323 TEST_F(TraceNetLogObserverTest, EventsWithAndWithoutParameters) { |
| 324 trace_net_log_observer()->WatchForTraceStart(net_log()); |
| 325 EnableTraceLog(); |
| 326 NetLog::ParametersCallback net_log_callback; |
| 327 std::string param = "bar"; |
| 328 net_log_callback = NetLog::StringCallback("foo", ¶m); |
| 329 |
| 330 net_log()->AddGlobalEntry(NetLog::TYPE_CANCELLED, net_log_callback); |
| 331 net_log()->AddGlobalEntry(NetLog::TYPE_REQUEST_ALIVE); |
| 332 |
| 333 EndTraceAndFlush(); |
| 334 trace_net_log_observer()->StopWatchForTraceStart(); |
| 335 |
| 336 CapturingNetLog::CapturedEntryList entries; |
| 337 net_log()->GetEntries(&entries); |
| 338 EXPECT_EQ(2u, entries.size()); |
| 339 EXPECT_EQ(2u, trace_events()->GetSize()); |
| 340 const base::DictionaryValue* item1 = NULL; |
| 341 ASSERT_TRUE(trace_events()->GetDictionary(0, &item1)); |
| 342 const base::DictionaryValue* item2 = NULL; |
| 343 ASSERT_TRUE(trace_events()->GetDictionary(1, &item2)); |
| 344 |
| 345 TraceEntryInfo actual_item1 = GetTraceEntryInfoFromValue(*item1); |
| 346 TraceEntryInfo actual_item2 = GetTraceEntryInfoFromValue(*item2); |
| 347 EXPECT_EQ("netlog", actual_item1.category); |
| 348 EXPECT_EQ(base::StringPrintf("0x%d", entries[0].source.id), actual_item1.id); |
| 349 EXPECT_EQ(std::string(1, TRACE_EVENT_PHASE_NESTABLE_ASYNC_INSTANT), |
| 350 actual_item1.phase); |
| 351 EXPECT_EQ(NetLog::EventTypeToString(NetLog::TYPE_CANCELLED), |
| 352 actual_item1.name); |
| 353 EXPECT_EQ(NetLog::SourceTypeToString(entries[0].source.type), |
| 354 actual_item1.source_type); |
| 355 |
| 356 EXPECT_EQ("netlog", actual_item2.category); |
| 357 EXPECT_EQ(base::StringPrintf("0x%d", entries[1].source.id), actual_item2.id); |
| 358 EXPECT_EQ(std::string(1, TRACE_EVENT_PHASE_NESTABLE_ASYNC_INSTANT), |
| 359 actual_item2.phase); |
| 360 EXPECT_EQ(NetLog::EventTypeToString(NetLog::TYPE_REQUEST_ALIVE), |
| 361 actual_item2.name); |
| 362 EXPECT_EQ(NetLog::SourceTypeToString(entries[1].source.type), |
| 363 actual_item2.source_type); |
| 364 |
| 365 std::string item1_params; |
| 366 std::string item2_params; |
| 367 EXPECT_TRUE(item1->GetString("args.params.foo", &item1_params)); |
| 368 EXPECT_EQ("bar", item1_params); |
| 369 |
| 370 EXPECT_TRUE(item2->GetString("args.params", &item2_params)); |
| 371 EXPECT_TRUE(item2_params.empty()); |
| 372 } |
| 373 |
| 374 } // namespace |
| 375 |
| 376 } // namespace net |
OLD | NEW |