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 "components/net_log/net_log_file_writer.h" | 5 #include "components/net_log/net_log_file_writer.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
(...skipping 28 matching lines...) Expand all Loading... |
39 base::FilePath::CharType kLogRelativePath[] = | 39 base::FilePath::CharType kLogRelativePath[] = |
40 FILE_PATH_LITERAL("net-export/chrome-net-export-log.json"); | 40 FILE_PATH_LITERAL("net-export/chrome-net-export-log.json"); |
41 | 41 |
42 const char kCaptureModeDefaultString[] = "STRIP_PRIVATE_DATA"; | 42 const char kCaptureModeDefaultString[] = "STRIP_PRIVATE_DATA"; |
43 const char kCaptureModeIncludeCookiesAndCredentialsString[] = "NORMAL"; | 43 const char kCaptureModeIncludeCookiesAndCredentialsString[] = "NORMAL"; |
44 const char kCaptureModeIncludeSocketBytesString[] = "LOG_BYTES"; | 44 const char kCaptureModeIncludeSocketBytesString[] = "LOG_BYTES"; |
45 | 45 |
46 const char kStateUninitializedString[] = "UNINITIALIZED"; | 46 const char kStateUninitializedString[] = "UNINITIALIZED"; |
47 const char kStateInitializingString[] = "INITIALIZING"; | 47 const char kStateInitializingString[] = "INITIALIZING"; |
48 const char kStateNotLoggingString[] = "NOT_LOGGING"; | 48 const char kStateNotLoggingString[] = "NOT_LOGGING"; |
| 49 const char kStateStartingLogString[] = "STARTING_LOG"; |
49 const char kStateLoggingString[] = "LOGGING"; | 50 const char kStateLoggingString[] = "LOGGING"; |
50 const char kStateStoppingLogString[] = "STOPPING_LOG"; | 51 const char kStateStoppingLogString[] = "STOPPING_LOG"; |
51 } // namespace | 52 } // namespace |
52 | 53 |
53 namespace net_log { | 54 namespace net_log { |
54 | 55 |
55 // Sets |path| to |path_to_return| and always returns true. This function is | 56 // Sets |path| to |path_to_return| and always returns true. This function is |
56 // used to override NetLogFileWriter's usual getter for the default log base | 57 // used to override NetLogFileWriter's usual getter for the default log base |
57 // directory. | 58 // directory. |
58 bool SetPathToGivenAndReturnTrue(const base::FilePath& path_to_return, | 59 bool SetPathToGivenAndReturnTrue(const base::FilePath& path_to_return, |
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 | 306 |
306 return ::testing::AssertionSuccess(); | 307 return ::testing::AssertionSuccess(); |
307 } | 308 } |
308 | 309 |
309 // If |custom_log_path| is empty path, |net_log_file_writer_| will use its | 310 // If |custom_log_path| is empty path, |net_log_file_writer_| will use its |
310 // default log path, which is cached in |default_log_path_|. | 311 // default log path, which is cached in |default_log_path_|. |
311 WARN_UNUSED_RESULT ::testing::AssertionResult StartThenVerifyNewState( | 312 WARN_UNUSED_RESULT ::testing::AssertionResult StartThenVerifyNewState( |
312 const base::FilePath& custom_log_path, | 313 const base::FilePath& custom_log_path, |
313 net::NetLogCaptureMode capture_mode, | 314 net::NetLogCaptureMode capture_mode, |
314 const std::string& expected_capture_mode_string) { | 315 const std::string& expected_capture_mode_string) { |
315 net_log_file_writer_.StartNetLog(custom_log_path, capture_mode); | 316 net_log_file_writer_.StartNetLog(custom_log_path, capture_mode, {}); |
316 std::unique_ptr<base::DictionaryValue> state = | 317 std::unique_ptr<base::DictionaryValue> state = |
317 test_state_observer_.WaitForNewState(); | 318 test_state_observer_.WaitForNewState(); |
318 ::testing::AssertionResult result = | 319 ::testing::AssertionResult result = |
319 VerifyState(std::move(state), kStateLoggingString, true, true, | 320 VerifyState(std::move(state), kStateStartingLogString); |
320 expected_capture_mode_string); | |
321 if (!result) { | 321 if (!result) { |
322 return ::testing::AssertionFailure() | 322 return ::testing::AssertionFailure() |
323 << "First state after StartNetLog() does not match expected:" | 323 << "First state after StartNetLog() does not match expected:" |
324 << std::endl | 324 << std::endl |
325 << result.message(); | 325 << result.message(); |
326 } | 326 } |
327 | 327 |
| 328 state = test_state_observer_.WaitForNewState(); |
| 329 result = VerifyState(std::move(state), kStateLoggingString, true, true, |
| 330 expected_capture_mode_string); |
| 331 if (!result) { |
| 332 return ::testing::AssertionFailure() |
| 333 << "Second state after StartNetLog() does not match expected:" |
| 334 << std::endl |
| 335 << result.message(); |
| 336 } |
| 337 |
328 // Make sure GetFilePath() returns empty path when logging. | 338 // Make sure GetFilePath() returns empty path when logging. |
329 base::FilePath actual_log_path = FileWriterGetFilePathToCompletedLog(); | 339 base::FilePath actual_log_path = FileWriterGetFilePathToCompletedLog(); |
330 if (!actual_log_path.empty()) { | 340 if (!actual_log_path.empty()) { |
331 return ::testing::AssertionFailure() | 341 return ::testing::AssertionFailure() |
332 << "GetFilePath() should return empty path after logging starts." | 342 << "GetFilePath() should return empty path after logging starts." |
333 << " Actual: " << ::testing::PrintToString(actual_log_path); | 343 << " Actual: " << ::testing::PrintToString(actual_log_path); |
334 } | 344 } |
335 | 345 |
336 return ::testing::AssertionSuccess(); | 346 return ::testing::AssertionSuccess(); |
337 } | 347 } |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
461 | 471 |
462 // For each capture mode, start and stop |net_log_file_writer_| in that mode. | 472 // For each capture mode, start and stop |net_log_file_writer_| in that mode. |
463 for (int i = 0; i < 3; ++i) { | 473 for (int i = 0; i < 3; ++i) { |
464 // StartNetLog(), should result in state change. | 474 // StartNetLog(), should result in state change. |
465 ASSERT_TRUE(StartThenVerifyNewState(base::FilePath(), capture_modes[i], | 475 ASSERT_TRUE(StartThenVerifyNewState(base::FilePath(), capture_modes[i], |
466 capture_mode_strings[i])); | 476 capture_mode_strings[i])); |
467 | 477 |
468 // Calling StartNetLog() again should be a no-op. Try doing StartNetLog() | 478 // Calling StartNetLog() again should be a no-op. Try doing StartNetLog() |
469 // with various capture modes; they should all be ignored and result in no | 479 // with various capture modes; they should all be ignored and result in no |
470 // state change. | 480 // state change. |
471 net_log_file_writer_.StartNetLog(base::FilePath(), capture_modes[i]); | 481 net_log_file_writer_.StartNetLog(base::FilePath(), capture_modes[i], {}); |
472 net_log_file_writer_.StartNetLog(base::FilePath(), | 482 net_log_file_writer_.StartNetLog(base::FilePath(), |
473 capture_modes[(i + 1) % 3]); | 483 capture_modes[(i + 1) % 3], {}); |
474 net_log_file_writer_.StartNetLog(base::FilePath(), | 484 net_log_file_writer_.StartNetLog(base::FilePath(), |
475 capture_modes[(i + 2) % 3]); | 485 capture_modes[(i + 2) % 3], {}); |
476 | 486 |
477 // StopNetLog(), should result in state change. The capture mode should | 487 // StopNetLog(), should result in state change. The capture mode should |
478 // match that of the first StartNetLog() call (called by | 488 // match that of the first StartNetLog() call (called by |
479 // StartThenVerifyNewState()). | 489 // StartThenVerifyNewState()). |
480 ASSERT_TRUE(StopThenVerifyNewStateAndFile( | 490 ASSERT_TRUE(StopThenVerifyNewStateAndFile( |
481 base::FilePath(), nullptr, nullptr, capture_mode_strings[i])); | 491 base::FilePath(), nullptr, nullptr, capture_mode_strings[i])); |
482 | 492 |
483 // Stopping a second time should be a no-op. | 493 // Stopping a second time should be a no-op. |
484 net_log_file_writer_.StopNetLog(nullptr, nullptr); | 494 net_log_file_writer_.StopNetLog(nullptr, nullptr); |
485 } | 495 } |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
656 // Trigger initialization of |net_log_file_writer_|. | 666 // Trigger initialization of |net_log_file_writer_|. |
657 net_log_file_writer_.Initialize(file_thread_.task_runner(), | 667 net_log_file_writer_.Initialize(file_thread_.task_runner(), |
658 net_thread_.task_runner()); | 668 net_thread_.task_runner()); |
659 | 669 |
660 // Before running the main message loop, tell |net_log_file_writer_| to start | 670 // Before running the main message loop, tell |net_log_file_writer_| to start |
661 // logging. Not running the main message loop prevents the initialization | 671 // logging. Not running the main message loop prevents the initialization |
662 // process from completing, so this ensures that StartNetLog() is received | 672 // process from completing, so this ensures that StartNetLog() is received |
663 // before |net_log_file_writer_| finishes initialization, which means this | 673 // before |net_log_file_writer_| finishes initialization, which means this |
664 // should be a no-op. | 674 // should be a no-op. |
665 net_log_file_writer_.StartNetLog(base::FilePath(), | 675 net_log_file_writer_.StartNetLog(base::FilePath(), |
666 net::NetLogCaptureMode::Default()); | 676 net::NetLogCaptureMode::Default(), {}); |
667 | 677 |
668 // Now run the main message loop. Make sure StartNetLog() was ignored by | 678 // Now run the main message loop. Make sure StartNetLog() was ignored by |
669 // checking that the next two states are "initializing" followed by | 679 // checking that the next two states are "initializing" followed by |
670 // "not-logging". | 680 // "not-logging". |
671 std::unique_ptr<base::DictionaryValue> state = | 681 std::unique_ptr<base::DictionaryValue> state = |
672 test_state_observer_.WaitForNewState(); | 682 test_state_observer_.WaitForNewState(); |
673 ASSERT_TRUE(VerifyState(std::move(state), kStateInitializingString)); | 683 ASSERT_TRUE(VerifyState(std::move(state), kStateInitializingString)); |
674 state = test_state_observer_.WaitForNewState(); | 684 state = test_state_observer_.WaitForNewState(); |
675 ASSERT_TRUE( | 685 ASSERT_TRUE( |
676 VerifyState(std::move(state), kStateNotLoggingString, false, false, "")); | 686 VerifyState(std::move(state), kStateNotLoggingString, false, false, "")); |
677 } | 687 } |
678 | 688 |
679 TEST_F(NetLogFileWriterTest, ReceiveStartWhileStoppingLog) { | 689 TEST_F(NetLogFileWriterTest, ReceiveStartWhileStoppingLog) { |
680 ASSERT_TRUE(InitializeThenVerifyNewState(true, false)); | 690 ASSERT_TRUE(InitializeThenVerifyNewState(true, false)); |
681 | 691 |
682 // Call StartNetLog() on |net_log_file_writer_| and wait for the state change. | 692 // Call StartNetLog() on |net_log_file_writer_| and wait for the state change. |
683 ASSERT_TRUE(StartThenVerifyNewState( | 693 ASSERT_TRUE(StartThenVerifyNewState( |
684 base::FilePath(), net::NetLogCaptureMode::IncludeSocketBytes(), | 694 base::FilePath(), net::NetLogCaptureMode::IncludeSocketBytes(), |
685 kCaptureModeIncludeSocketBytesString)); | 695 kCaptureModeIncludeSocketBytesString)); |
686 | 696 |
687 // Tell |net_log_file_writer_| to stop logging. | 697 // Tell |net_log_file_writer_| to stop logging. |
688 net_log_file_writer_.StopNetLog(nullptr, nullptr); | 698 net_log_file_writer_.StopNetLog(nullptr, nullptr); |
689 | 699 |
690 // Before running the main message loop, tell |net_log_file_writer_| to start | 700 // Before running the main message loop, tell |net_log_file_writer_| to start |
691 // logging. Not running the main message loop prevents the stopping process | 701 // logging. Not running the main message loop prevents the stopping process |
692 // from completing, so this ensures StartNetLog() is received before | 702 // from completing, so this ensures StartNetLog() is received before |
693 // |net_log_file_writer_| finishes stopping, which means this should be a | 703 // |net_log_file_writer_| finishes stopping, which means this should be a |
694 // no-op. | 704 // no-op. |
695 net_log_file_writer_.StartNetLog(base::FilePath(), | 705 net_log_file_writer_.StartNetLog(base::FilePath(), |
696 net::NetLogCaptureMode::Default()); | 706 net::NetLogCaptureMode::Default(), {}); |
697 | 707 |
698 // Now run the main message loop. Make sure the last StartNetLog() was | 708 // Now run the main message loop. Make sure the last StartNetLog() was |
699 // ignored by checking that the next two states are "stopping-log" followed by | 709 // ignored by checking that the next two states are "stopping-log" followed by |
700 // "not-logging". Also make sure the capture mode matches that of the first | 710 // "not-logging". Also make sure the capture mode matches that of the first |
701 // StartNetLog() call (called by StartThenVerifyState()). | 711 // StartNetLog() call (called by StartThenVerifyState()). |
702 std::unique_ptr<base::DictionaryValue> state = | 712 std::unique_ptr<base::DictionaryValue> state = |
703 test_state_observer_.WaitForNewState(); | 713 test_state_observer_.WaitForNewState(); |
704 ASSERT_TRUE(VerifyState(std::move(state), kStateStoppingLogString)); | 714 ASSERT_TRUE(VerifyState(std::move(state), kStateStoppingLogString)); |
705 state = test_state_observer_.WaitForNewState(); | 715 state = test_state_observer_.WaitForNewState(); |
706 ASSERT_TRUE(VerifyState(std::move(state), kStateNotLoggingString, true, true, | 716 ASSERT_TRUE(VerifyState(std::move(state), kStateNotLoggingString, true, true, |
707 kCaptureModeIncludeSocketBytesString)); | 717 kCaptureModeIncludeSocketBytesString)); |
708 } | 718 } |
709 | 719 |
710 } // namespace net_log | 720 } // namespace net_log |
OLD | NEW |