OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/log/net_log.h" | 5 #include "net/log/net_log.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/scoped_vector.h" | 8 #include "base/memory/scoped_vector.h" |
9 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
10 #include "base/threading/simple_thread.h" | 10 #include "base/threading/simple_thread.h" |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 NetLogCaptureMode kModes[] = { | 54 NetLogCaptureMode kModes[] = { |
55 NetLogCaptureMode::Default(), | 55 NetLogCaptureMode::Default(), |
56 NetLogCaptureMode::IncludeCookiesAndCredentials(), | 56 NetLogCaptureMode::IncludeCookiesAndCredentials(), |
57 NetLogCaptureMode::IncludeSocketBytes(), | 57 NetLogCaptureMode::IncludeSocketBytes(), |
58 }; | 58 }; |
59 | 59 |
60 TestNetLog net_log; | 60 TestNetLog net_log; |
61 | 61 |
62 for (NetLogCaptureMode mode : kModes) { | 62 for (NetLogCaptureMode mode : kModes) { |
63 net_log.SetCaptureMode(mode); | 63 net_log.SetCaptureMode(mode); |
64 EXPECT_EQ(mode, net_log.GetCaptureMode()); | 64 EXPECT_EQ(mode, net_log.GetObserver()->capture_mode()); |
65 | 65 |
66 net_log.AddGlobalEntry(NetLog::TYPE_SOCKET_ALIVE, | 66 net_log.AddGlobalEntry(NetLog::TYPE_SOCKET_ALIVE, |
67 base::Bind(NetCaptureModeCallback)); | 67 base::Bind(NetCaptureModeCallback)); |
68 | 68 |
69 TestNetLogEntry::List entries; | 69 TestNetLogEntry::List entries; |
70 net_log.GetEntries(&entries); | 70 net_log.GetEntries(&entries); |
71 | 71 |
72 ASSERT_EQ(1u, entries.size()); | 72 ASSERT_EQ(1u, entries.size()); |
73 EXPECT_EQ(NetLog::TYPE_SOCKET_ALIVE, entries[0].type); | 73 EXPECT_EQ(NetLog::TYPE_SOCKET_ALIVE, entries[0].type); |
74 EXPECT_EQ(NetLog::SOURCE_NONE, entries[0].source.type); | 74 EXPECT_EQ(NetLog::SOURCE_NONE, entries[0].source.type); |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 } | 254 } |
255 | 255 |
256 // Test adding and removing a single observer. | 256 // Test adding and removing a single observer. |
257 TEST(NetLogTest, NetLogAddRemoveObserver) { | 257 TEST(NetLogTest, NetLogAddRemoveObserver) { |
258 NetLog net_log; | 258 NetLog net_log; |
259 CountingObserver observer; | 259 CountingObserver observer; |
260 | 260 |
261 AddEvent(&net_log); | 261 AddEvent(&net_log); |
262 EXPECT_EQ(0, observer.count()); | 262 EXPECT_EQ(0, observer.count()); |
263 EXPECT_EQ(NULL, observer.net_log()); | 263 EXPECT_EQ(NULL, observer.net_log()); |
264 EXPECT_FALSE(net_log.GetCaptureMode().enabled()); | 264 EXPECT_FALSE(net_log.IsCapturing()); |
265 | 265 |
266 // Add the observer and add an event. | 266 // Add the observer and add an event. |
267 net_log.DeprecatedAddObserver( | 267 net_log.DeprecatedAddObserver( |
268 &observer, NetLogCaptureMode::IncludeCookiesAndCredentials()); | 268 &observer, NetLogCaptureMode::IncludeCookiesAndCredentials()); |
| 269 EXPECT_TRUE(net_log.IsCapturing()); |
269 EXPECT_EQ(&net_log, observer.net_log()); | 270 EXPECT_EQ(&net_log, observer.net_log()); |
270 EXPECT_EQ(NetLogCaptureMode::IncludeCookiesAndCredentials(), | 271 EXPECT_EQ(NetLogCaptureMode::IncludeCookiesAndCredentials(), |
271 observer.capture_mode()); | 272 observer.capture_mode()); |
272 EXPECT_EQ(NetLogCaptureMode::IncludeCookiesAndCredentials(), | 273 EXPECT_TRUE(net_log.IsCapturing()); |
273 net_log.GetCaptureMode()); | |
274 | 274 |
275 AddEvent(&net_log); | 275 AddEvent(&net_log); |
276 EXPECT_EQ(1, observer.count()); | 276 EXPECT_EQ(1, observer.count()); |
277 | 277 |
278 // Change the observer's logging level and add an event. | 278 // Change the observer's logging level and add an event. |
279 net_log.SetObserverCaptureMode(&observer, | 279 net_log.SetObserverCaptureMode(&observer, |
280 NetLogCaptureMode::IncludeSocketBytes()); | 280 NetLogCaptureMode::IncludeSocketBytes()); |
281 EXPECT_EQ(&net_log, observer.net_log()); | 281 EXPECT_EQ(&net_log, observer.net_log()); |
282 EXPECT_EQ(NetLogCaptureMode::IncludeSocketBytes(), observer.capture_mode()); | 282 EXPECT_EQ(NetLogCaptureMode::IncludeSocketBytes(), observer.capture_mode()); |
283 EXPECT_EQ(NetLogCaptureMode::IncludeSocketBytes(), net_log.GetCaptureMode()); | 283 EXPECT_TRUE(net_log.IsCapturing()); |
284 | 284 |
285 AddEvent(&net_log); | 285 AddEvent(&net_log); |
286 EXPECT_EQ(2, observer.count()); | 286 EXPECT_EQ(2, observer.count()); |
287 | 287 |
288 // Remove observer and add an event. | 288 // Remove observer and add an event. |
289 net_log.DeprecatedRemoveObserver(&observer); | 289 net_log.DeprecatedRemoveObserver(&observer); |
290 EXPECT_EQ(NULL, observer.net_log()); | 290 EXPECT_EQ(NULL, observer.net_log()); |
291 EXPECT_FALSE(net_log.GetCaptureMode().enabled()); | 291 EXPECT_FALSE(net_log.IsCapturing()); |
292 | 292 |
293 AddEvent(&net_log); | 293 AddEvent(&net_log); |
294 EXPECT_EQ(2, observer.count()); | 294 EXPECT_EQ(2, observer.count()); |
295 | 295 |
296 // Add the observer a final time, and add an event. | 296 // Add the observer a final time, and add an event. |
297 net_log.DeprecatedAddObserver(&observer, | 297 net_log.DeprecatedAddObserver(&observer, |
298 NetLogCaptureMode::IncludeSocketBytes()); | 298 NetLogCaptureMode::IncludeSocketBytes()); |
299 EXPECT_EQ(&net_log, observer.net_log()); | 299 EXPECT_EQ(&net_log, observer.net_log()); |
300 EXPECT_EQ(NetLogCaptureMode::IncludeSocketBytes(), observer.capture_mode()); | 300 EXPECT_EQ(NetLogCaptureMode::IncludeSocketBytes(), observer.capture_mode()); |
301 EXPECT_EQ(NetLogCaptureMode::IncludeSocketBytes(), net_log.GetCaptureMode()); | 301 EXPECT_TRUE(net_log.IsCapturing()); |
302 | 302 |
303 AddEvent(&net_log); | 303 AddEvent(&net_log); |
304 EXPECT_EQ(3, observer.count()); | 304 EXPECT_EQ(3, observer.count()); |
305 } | 305 } |
306 | 306 |
307 // Test adding and removing two observers at different log levels. | 307 // Test adding and removing two observers at different log levels. |
308 TEST(NetLogTest, NetLogTwoObservers) { | 308 TEST(NetLogTest, NetLogTwoObservers) { |
309 NetLog net_log; | 309 NetLog net_log; |
310 LoggingObserver observer[2]; | 310 LoggingObserver observer[2]; |
311 | 311 |
312 // Add first observer. | 312 // Add first observer. |
313 net_log.DeprecatedAddObserver( | 313 net_log.DeprecatedAddObserver( |
314 &observer[0], NetLogCaptureMode::IncludeCookiesAndCredentials()); | 314 &observer[0], NetLogCaptureMode::IncludeCookiesAndCredentials()); |
315 EXPECT_EQ(&net_log, observer[0].net_log()); | 315 EXPECT_EQ(&net_log, observer[0].net_log()); |
316 EXPECT_EQ(NULL, observer[1].net_log()); | 316 EXPECT_EQ(NULL, observer[1].net_log()); |
317 EXPECT_EQ(NetLogCaptureMode::IncludeCookiesAndCredentials(), | 317 EXPECT_EQ(NetLogCaptureMode::IncludeCookiesAndCredentials(), |
318 observer[0].capture_mode()); | 318 observer[0].capture_mode()); |
319 EXPECT_EQ(NetLogCaptureMode::IncludeCookiesAndCredentials(), | 319 EXPECT_TRUE(net_log.IsCapturing()); |
320 net_log.GetCaptureMode()); | |
321 | 320 |
322 // Add second observer observer. | 321 // Add second observer observer. |
323 net_log.DeprecatedAddObserver(&observer[1], | 322 net_log.DeprecatedAddObserver(&observer[1], |
324 NetLogCaptureMode::IncludeSocketBytes()); | 323 NetLogCaptureMode::IncludeSocketBytes()); |
325 EXPECT_EQ(&net_log, observer[0].net_log()); | 324 EXPECT_EQ(&net_log, observer[0].net_log()); |
326 EXPECT_EQ(&net_log, observer[1].net_log()); | 325 EXPECT_EQ(&net_log, observer[1].net_log()); |
327 EXPECT_EQ(NetLogCaptureMode::IncludeCookiesAndCredentials(), | 326 EXPECT_EQ(NetLogCaptureMode::IncludeCookiesAndCredentials(), |
328 observer[0].capture_mode()); | 327 observer[0].capture_mode()); |
329 EXPECT_EQ(NetLogCaptureMode::IncludeSocketBytes(), | 328 EXPECT_EQ(NetLogCaptureMode::IncludeSocketBytes(), |
330 observer[1].capture_mode()); | 329 observer[1].capture_mode()); |
331 EXPECT_EQ(NetLogCaptureMode::IncludeSocketBytes(), net_log.GetCaptureMode()); | 330 EXPECT_TRUE(net_log.IsCapturing()); |
332 | 331 |
333 // Add event and make sure both observers receive it at their respective log | 332 // Add event and make sure both observers receive it at their respective log |
334 // levels. | 333 // levels. |
335 int param; | 334 int param; |
336 AddEvent(&net_log); | 335 AddEvent(&net_log); |
337 ASSERT_EQ(1U, observer[0].GetNumValues()); | 336 ASSERT_EQ(1U, observer[0].GetNumValues()); |
338 ASSERT_TRUE(observer[0].GetValue(0)->GetInteger("params", ¶m)); | 337 ASSERT_TRUE(observer[0].GetValue(0)->GetInteger("params", ¶m)); |
339 EXPECT_EQ(observer[0].capture_mode().ToInternalValueForTesting(), param); | 338 EXPECT_EQ(observer[0].capture_mode().ToInternalValueForTesting(), param); |
340 ASSERT_EQ(1U, observer[1].GetNumValues()); | 339 ASSERT_EQ(1U, observer[1].GetNumValues()); |
341 ASSERT_TRUE(observer[1].GetValue(0)->GetInteger("params", ¶m)); | 340 ASSERT_TRUE(observer[1].GetValue(0)->GetInteger("params", ¶m)); |
342 EXPECT_EQ(observer[1].capture_mode().ToInternalValueForTesting(), param); | 341 EXPECT_EQ(observer[1].capture_mode().ToInternalValueForTesting(), param); |
343 | 342 |
344 // Remove second observer. | 343 // Remove second observer. |
345 net_log.DeprecatedRemoveObserver(&observer[1]); | 344 net_log.DeprecatedRemoveObserver(&observer[1]); |
346 EXPECT_EQ(&net_log, observer[0].net_log()); | 345 EXPECT_EQ(&net_log, observer[0].net_log()); |
347 EXPECT_EQ(NULL, observer[1].net_log()); | 346 EXPECT_EQ(NULL, observer[1].net_log()); |
348 EXPECT_EQ(NetLogCaptureMode::IncludeCookiesAndCredentials(), | 347 EXPECT_EQ(NetLogCaptureMode::IncludeCookiesAndCredentials(), |
349 observer[0].capture_mode()); | 348 observer[0].capture_mode()); |
350 EXPECT_EQ(NetLogCaptureMode::IncludeCookiesAndCredentials(), | 349 EXPECT_TRUE(net_log.IsCapturing()); |
351 net_log.GetCaptureMode()); | |
352 | 350 |
353 // Add event and make sure only second observer gets it. | 351 // Add event and make sure only second observer gets it. |
354 AddEvent(&net_log); | 352 AddEvent(&net_log); |
355 EXPECT_EQ(2U, observer[0].GetNumValues()); | 353 EXPECT_EQ(2U, observer[0].GetNumValues()); |
356 EXPECT_EQ(1U, observer[1].GetNumValues()); | 354 EXPECT_EQ(1U, observer[1].GetNumValues()); |
357 | 355 |
358 // Remove first observer. | 356 // Remove first observer. |
359 net_log.DeprecatedRemoveObserver(&observer[0]); | 357 net_log.DeprecatedRemoveObserver(&observer[0]); |
360 EXPECT_EQ(NULL, observer[0].net_log()); | 358 EXPECT_EQ(NULL, observer[0].net_log()); |
361 EXPECT_EQ(NULL, observer[1].net_log()); | 359 EXPECT_EQ(NULL, observer[1].net_log()); |
362 EXPECT_FALSE(net_log.GetCaptureMode().enabled()); | 360 EXPECT_FALSE(net_log.IsCapturing()); |
363 | 361 |
364 // Add event and make sure neither observer gets it. | 362 // Add event and make sure neither observer gets it. |
365 AddEvent(&net_log); | 363 AddEvent(&net_log); |
366 EXPECT_EQ(2U, observer[0].GetNumValues()); | 364 EXPECT_EQ(2U, observer[0].GetNumValues()); |
367 EXPECT_EQ(1U, observer[1].GetNumValues()); | 365 EXPECT_EQ(1U, observer[1].GetNumValues()); |
368 } | 366 } |
369 | 367 |
370 // Makes sure that adding and removing observers simultaneously on different | 368 // Makes sure that adding and removing observers simultaneously on different |
371 // threads works. | 369 // threads works. |
372 TEST(NetLogTest, NetLogAddRemoveObserverThreads) { | 370 TEST(NetLogTest, NetLogAddRemoveObserverThreads) { |
373 NetLog net_log; | 371 NetLog net_log; |
374 | 372 |
375 // Run a bunch of threads to completion, each of which will repeatedly add | 373 // Run a bunch of threads to completion, each of which will repeatedly add |
376 // and remove an observer, and set its logging level. | 374 // and remove an observer, and set its logging level. |
377 RunTestThreads<AddRemoveObserverTestThread>(&net_log); | 375 RunTestThreads<AddRemoveObserverTestThread>(&net_log); |
378 } | 376 } |
379 | 377 |
380 } // namespace | 378 } // namespace |
381 | 379 |
382 } // namespace net | 380 } // namespace net |
OLD | NEW |