| 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 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 }; | 250 }; |
| 251 | 251 |
| 252 class NetLogFileWriterTest : public ::testing::Test { | 252 class NetLogFileWriterTest : public ::testing::Test { |
| 253 public: | 253 public: |
| 254 using URLRequestContextGetterList = | 254 using URLRequestContextGetterList = |
| 255 std::vector<scoped_refptr<net::URLRequestContextGetter>>; | 255 std::vector<scoped_refptr<net::URLRequestContextGetter>>; |
| 256 | 256 |
| 257 NetLogFileWriterTest() | 257 NetLogFileWriterTest() |
| 258 : net_log_(base::FilePath(), | 258 : net_log_(base::FilePath(), |
| 259 net::NetLogCaptureMode::Default(), | 259 net::NetLogCaptureMode::Default(), |
| 260 base::CommandLine::ForCurrentProcess()->GetCommandLineString(), | 260 base::CommandLine::StringType(), |
| 261 kChannelString), | 261 kChannelString), |
| 262 net_log_file_writer_( | 262 net_log_file_writer_(&net_log_), |
| 263 &net_log_, | |
| 264 base::CommandLine::ForCurrentProcess()->GetCommandLineString(), | |
| 265 kChannelString), | |
| 266 file_thread_("NetLogFileWriter file thread"), | 263 file_thread_("NetLogFileWriter file thread"), |
| 267 net_thread_("NetLogFileWriter net thread") {} | 264 net_thread_("NetLogFileWriter net thread") {} |
| 268 | 265 |
| 269 // ::testing::Test implementation | 266 // ::testing::Test implementation |
| 270 void SetUp() override { | 267 void SetUp() override { |
| 271 ASSERT_TRUE(log_temp_dir_.CreateUniqueTempDir()); | 268 ASSERT_TRUE(log_temp_dir_.CreateUniqueTempDir()); |
| 272 | 269 |
| 273 // Override |net_log_file_writer_|'s default-log-base-directory-getter to | 270 // Override |net_log_file_writer_|'s default-log-base-directory-getter to |
| 274 // a getter that returns the temp dir created for the test. | 271 // a getter that returns the temp dir created for the test. |
| 275 net_log_file_writer_.SetDefaultLogBaseDirectoryGetterForTest( | 272 net_log_file_writer_.SetDefaultLogBaseDirectoryGetterForTest( |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 } | 327 } |
| 331 | 328 |
| 332 // If |custom_log_path| is empty path, |net_log_file_writer_| will use its | 329 // If |custom_log_path| is empty path, |net_log_file_writer_| will use its |
| 333 // default log path, which is cached in |default_log_path_|. | 330 // default log path, which is cached in |default_log_path_|. |
| 334 WARN_UNUSED_RESULT::testing::AssertionResult StartThenVerifyNewState( | 331 WARN_UNUSED_RESULT::testing::AssertionResult StartThenVerifyNewState( |
| 335 const base::FilePath& custom_log_path, | 332 const base::FilePath& custom_log_path, |
| 336 net::NetLogCaptureMode capture_mode, | 333 net::NetLogCaptureMode capture_mode, |
| 337 const std::string& expected_capture_mode_string, | 334 const std::string& expected_capture_mode_string, |
| 338 const URLRequestContextGetterList& context_getters) { | 335 const URLRequestContextGetterList& context_getters) { |
| 339 net_log_file_writer_.StartNetLog(custom_log_path, capture_mode, | 336 net_log_file_writer_.StartNetLog(custom_log_path, capture_mode, |
| 340 context_getters); | 337 base::CommandLine::StringType(), |
| 338 kChannelString, context_getters); |
| 341 std::unique_ptr<base::DictionaryValue> state = | 339 std::unique_ptr<base::DictionaryValue> state = |
| 342 test_state_observer_.WaitForNewState(); | 340 test_state_observer_.WaitForNewState(); |
| 343 ::testing::AssertionResult result = | 341 ::testing::AssertionResult result = |
| 344 VerifyState(std::move(state), kStateStartingLogString); | 342 VerifyState(std::move(state), kStateStartingLogString); |
| 345 if (!result) { | 343 if (!result) { |
| 346 return ::testing::AssertionFailure() | 344 return ::testing::AssertionFailure() |
| 347 << "First state after StartNetLog() does not match expected:" | 345 << "First state after StartNetLog() does not match expected:" |
| 348 << std::endl | 346 << std::endl |
| 349 << result.message(); | 347 << result.message(); |
| 350 } | 348 } |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 496 // For each capture mode, start and stop |net_log_file_writer_| in that mode. | 494 // For each capture mode, start and stop |net_log_file_writer_| in that mode. |
| 497 for (int i = 0; i < 3; ++i) { | 495 for (int i = 0; i < 3; ++i) { |
| 498 // StartNetLog(), should result in state change. | 496 // StartNetLog(), should result in state change. |
| 499 ASSERT_TRUE(StartThenVerifyNewState(base::FilePath(), capture_modes[i], | 497 ASSERT_TRUE(StartThenVerifyNewState(base::FilePath(), capture_modes[i], |
| 500 capture_mode_strings[i], | 498 capture_mode_strings[i], |
| 501 URLRequestContextGetterList())); | 499 URLRequestContextGetterList())); |
| 502 | 500 |
| 503 // Calling StartNetLog() again should be a no-op. Try doing StartNetLog() | 501 // Calling StartNetLog() again should be a no-op. Try doing StartNetLog() |
| 504 // with various capture modes; they should all be ignored and result in no | 502 // with various capture modes; they should all be ignored and result in no |
| 505 // state change. | 503 // state change. |
| 506 net_log_file_writer_.StartNetLog(base::FilePath(), capture_modes[i], | 504 net_log_file_writer_.StartNetLog( |
| 507 URLRequestContextGetterList()); | 505 base::FilePath(), capture_modes[i], base::CommandLine::StringType(), |
| 508 net_log_file_writer_.StartNetLog(base::FilePath(), | 506 kChannelString, URLRequestContextGetterList()); |
| 509 capture_modes[(i + 1) % 3], | 507 net_log_file_writer_.StartNetLog( |
| 510 URLRequestContextGetterList()); | 508 base::FilePath(), capture_modes[(i + 1) % 3], |
| 511 net_log_file_writer_.StartNetLog(base::FilePath(), | 509 base::CommandLine::StringType(), kChannelString, |
| 512 capture_modes[(i + 2) % 3], | 510 URLRequestContextGetterList()); |
| 513 URLRequestContextGetterList()); | 511 net_log_file_writer_.StartNetLog( |
| 512 base::FilePath(), capture_modes[(i + 2) % 3], |
| 513 base::CommandLine::StringType(), kChannelString, |
| 514 URLRequestContextGetterList()); |
| 514 | 515 |
| 515 // StopNetLog(), should result in state change. The capture mode should | 516 // StopNetLog(), should result in state change. The capture mode should |
| 516 // match that of the first StartNetLog() call (called by | 517 // match that of the first StartNetLog() call (called by |
| 517 // StartThenVerifyNewState()). | 518 // StartThenVerifyNewState()). |
| 518 ASSERT_TRUE(StopThenVerifyNewStateAndFile( | 519 ASSERT_TRUE(StopThenVerifyNewStateAndFile( |
| 519 base::FilePath(), nullptr, nullptr, capture_mode_strings[i])); | 520 base::FilePath(), nullptr, nullptr, capture_mode_strings[i])); |
| 520 | 521 |
| 521 // Stopping a second time should be a no-op. | 522 // Stopping a second time should be a no-op. |
| 522 net_log_file_writer_.StopNetLog(nullptr, nullptr); | 523 net_log_file_writer_.StopNetLog(nullptr, nullptr); |
| 523 } | 524 } |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 741 TEST_F(NetLogFileWriterTest, ReceiveStartWhileInitializing) { | 742 TEST_F(NetLogFileWriterTest, ReceiveStartWhileInitializing) { |
| 742 // Trigger initialization of |net_log_file_writer_|. | 743 // Trigger initialization of |net_log_file_writer_|. |
| 743 net_log_file_writer_.Initialize(file_thread_.task_runner(), | 744 net_log_file_writer_.Initialize(file_thread_.task_runner(), |
| 744 net_thread_.task_runner()); | 745 net_thread_.task_runner()); |
| 745 | 746 |
| 746 // Before running the main message loop, tell |net_log_file_writer_| to start | 747 // Before running the main message loop, tell |net_log_file_writer_| to start |
| 747 // logging. Not running the main message loop prevents the initialization | 748 // logging. Not running the main message loop prevents the initialization |
| 748 // process from completing, so this ensures that StartNetLog() is received | 749 // process from completing, so this ensures that StartNetLog() is received |
| 749 // before |net_log_file_writer_| finishes initialization, which means this | 750 // before |net_log_file_writer_| finishes initialization, which means this |
| 750 // should be a no-op. | 751 // should be a no-op. |
| 751 net_log_file_writer_.StartNetLog(base::FilePath(), | 752 net_log_file_writer_.StartNetLog( |
| 752 net::NetLogCaptureMode::Default(), | 753 base::FilePath(), net::NetLogCaptureMode::Default(), |
| 753 URLRequestContextGetterList()); | 754 base::CommandLine::StringType(), kChannelString, |
| 755 URLRequestContextGetterList()); |
| 754 | 756 |
| 755 // Now run the main message loop. Make sure StartNetLog() was ignored by | 757 // Now run the main message loop. Make sure StartNetLog() was ignored by |
| 756 // checking that the next two states are "initializing" followed by | 758 // checking that the next two states are "initializing" followed by |
| 757 // "not-logging". | 759 // "not-logging". |
| 758 std::unique_ptr<base::DictionaryValue> state = | 760 std::unique_ptr<base::DictionaryValue> state = |
| 759 test_state_observer_.WaitForNewState(); | 761 test_state_observer_.WaitForNewState(); |
| 760 ASSERT_TRUE(VerifyState(std::move(state), kStateInitializingString)); | 762 ASSERT_TRUE(VerifyState(std::move(state), kStateInitializingString)); |
| 761 state = test_state_observer_.WaitForNewState(); | 763 state = test_state_observer_.WaitForNewState(); |
| 762 ASSERT_TRUE( | 764 ASSERT_TRUE( |
| 763 VerifyState(std::move(state), kStateNotLoggingString, false, false, "")); | 765 VerifyState(std::move(state), kStateNotLoggingString, false, false, "")); |
| 764 } | 766 } |
| 765 | 767 |
| 766 TEST_F(NetLogFileWriterTest, ReceiveStartWhileStoppingLog) { | 768 TEST_F(NetLogFileWriterTest, ReceiveStartWhileStoppingLog) { |
| 767 ASSERT_TRUE(InitializeThenVerifyNewState(true, false)); | 769 ASSERT_TRUE(InitializeThenVerifyNewState(true, false)); |
| 768 | 770 |
| 769 // Call StartNetLog() on |net_log_file_writer_| and wait for the state change. | 771 // Call StartNetLog() on |net_log_file_writer_| and wait for the state change. |
| 770 ASSERT_TRUE(StartThenVerifyNewState( | 772 ASSERT_TRUE(StartThenVerifyNewState( |
| 771 base::FilePath(), net::NetLogCaptureMode::IncludeSocketBytes(), | 773 base::FilePath(), net::NetLogCaptureMode::IncludeSocketBytes(), |
| 772 kCaptureModeIncludeSocketBytesString, URLRequestContextGetterList())); | 774 kCaptureModeIncludeSocketBytesString, URLRequestContextGetterList())); |
| 773 | 775 |
| 774 // Tell |net_log_file_writer_| to stop logging. | 776 // Tell |net_log_file_writer_| to stop logging. |
| 775 net_log_file_writer_.StopNetLog(nullptr, nullptr); | 777 net_log_file_writer_.StopNetLog(nullptr, nullptr); |
| 776 | 778 |
| 777 // Before running the main message loop, tell |net_log_file_writer_| to start | 779 // Before running the main message loop, tell |net_log_file_writer_| to start |
| 778 // logging. Not running the main message loop prevents the stopping process | 780 // logging. Not running the main message loop prevents the stopping process |
| 779 // from completing, so this ensures StartNetLog() is received before | 781 // from completing, so this ensures StartNetLog() is received before |
| 780 // |net_log_file_writer_| finishes stopping, which means this should be a | 782 // |net_log_file_writer_| finishes stopping, which means this should be a |
| 781 // no-op. | 783 // no-op. |
| 782 net_log_file_writer_.StartNetLog(base::FilePath(), | 784 net_log_file_writer_.StartNetLog( |
| 783 net::NetLogCaptureMode::Default(), | 785 base::FilePath(), net::NetLogCaptureMode::Default(), |
| 784 URLRequestContextGetterList()); | 786 base::CommandLine::StringType(), kChannelString, |
| 787 URLRequestContextGetterList()); |
| 785 | 788 |
| 786 // Now run the main message loop. Make sure the last StartNetLog() was | 789 // Now run the main message loop. Make sure the last StartNetLog() was |
| 787 // ignored by checking that the next two states are "stopping-log" followed by | 790 // ignored by checking that the next two states are "stopping-log" followed by |
| 788 // "not-logging". Also make sure the capture mode matches that of the first | 791 // "not-logging". Also make sure the capture mode matches that of the first |
| 789 // StartNetLog() call (called by StartThenVerifyState()). | 792 // StartNetLog() call (called by StartThenVerifyState()). |
| 790 std::unique_ptr<base::DictionaryValue> state = | 793 std::unique_ptr<base::DictionaryValue> state = |
| 791 test_state_observer_.WaitForNewState(); | 794 test_state_observer_.WaitForNewState(); |
| 792 ASSERT_TRUE(VerifyState(std::move(state), kStateStoppingLogString)); | 795 ASSERT_TRUE(VerifyState(std::move(state), kStateStoppingLogString)); |
| 793 state = test_state_observer_.WaitForNewState(); | 796 state = test_state_observer_.WaitForNewState(); |
| 794 ASSERT_TRUE(VerifyState(std::move(state), kStateNotLoggingString, true, true, | 797 ASSERT_TRUE(VerifyState(std::move(state), kStateNotLoggingString, true, true, |
| 795 kCaptureModeIncludeSocketBytesString)); | 798 kCaptureModeIncludeSocketBytesString)); |
| 796 } | 799 } |
| 797 | 800 |
| 798 } // namespace net_log | 801 } // namespace net_log |
| OLD | NEW |