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 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 params->quic_idle_connection_timeout_seconds = | 174 params->quic_idle_connection_timeout_seconds = |
174 quic_idle_connection_timeout_seconds; | 175 quic_idle_connection_timeout_seconds; |
175 | 176 |
176 context->set_http_network_session_params(std::move(params)); | 177 context->set_http_network_session_params(std::move(params)); |
177 context->Init(); | 178 context->Init(); |
178 | 179 |
179 *context_getter = new net::TestURLRequestContextGetter( | 180 *context_getter = new net::TestURLRequestContextGetter( |
180 base::ThreadTaskRunnerHandle::Get(), std::move(context)); | 181 base::ThreadTaskRunnerHandle::Get(), std::move(context)); |
181 } | 182 } |
182 | 183 |
| 184 void SetUpTestContextGetterWithRequest( |
| 185 net::NetLog* net_log, |
| 186 const GURL& url, |
| 187 net::URLRequest::Delegate* delegate, |
| 188 scoped_refptr<net::TestURLRequestContextGetter>* context_getter, |
| 189 std::unique_ptr<net::URLRequest>* request) { |
| 190 auto context = base::MakeUnique<net::TestURLRequestContext>(true); |
| 191 context->set_net_log(net_log); |
| 192 context->Init(); |
| 193 |
| 194 *request = context->CreateRequest(url, net::IDLE, delegate); |
| 195 (*request)->Start(); |
| 196 |
| 197 *context_getter = new net::TestURLRequestContextGetter( |
| 198 base::ThreadTaskRunnerHandle::Get(), std::move(context)); |
| 199 } |
| 200 |
183 // An implementation of NetLogFileWriter::StateObserver that allows waiting | 201 // An implementation of NetLogFileWriter::StateObserver that allows waiting |
184 // until it's notified of a new state. | 202 // until it's notified of a new state. |
185 class TestStateObserver : public NetLogFileWriter::StateObserver { | 203 class TestStateObserver : public NetLogFileWriter::StateObserver { |
186 public: | 204 public: |
187 // NetLogFileWriter::StateObserver implementation | 205 // NetLogFileWriter::StateObserver implementation |
188 void OnNewState(const base::DictionaryValue& state) override { | 206 void OnNewState(const base::DictionaryValue& state) override { |
189 test_closure_.closure().Run(); | 207 test_closure_.closure().Run(); |
190 result_state_ = state.CreateDeepCopy(); | 208 result_state_ = state.CreateDeepCopy(); |
191 } | 209 } |
192 | 210 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 test_closure_.closure().Run(); | 242 test_closure_.closure().Run(); |
225 } | 243 } |
226 | 244 |
227 net::TestClosure test_closure_; | 245 net::TestClosure test_closure_; |
228 base::FilePath result_; | 246 base::FilePath result_; |
229 base::Callback<void(const base::FilePath&)> callback_; | 247 base::Callback<void(const base::FilePath&)> callback_; |
230 }; | 248 }; |
231 | 249 |
232 class NetLogFileWriterTest : public ::testing::Test { | 250 class NetLogFileWriterTest : public ::testing::Test { |
233 public: | 251 public: |
| 252 using URLRequestContextGetterList = |
| 253 std::vector<scoped_refptr<net::URLRequestContextGetter>>; |
| 254 |
234 NetLogFileWriterTest() | 255 NetLogFileWriterTest() |
235 : net_log_(base::FilePath(), | 256 : net_log_(base::FilePath(), |
236 net::NetLogCaptureMode::Default(), | 257 net::NetLogCaptureMode::Default(), |
237 base::CommandLine::ForCurrentProcess()->GetCommandLineString(), | 258 base::CommandLine::ForCurrentProcess()->GetCommandLineString(), |
238 kChannelString), | 259 kChannelString), |
239 net_log_file_writer_( | 260 net_log_file_writer_( |
240 &net_log_, | 261 &net_log_, |
241 base::CommandLine::ForCurrentProcess()->GetCommandLineString(), | 262 base::CommandLine::ForCurrentProcess()->GetCommandLineString(), |
242 kChannelString), | 263 kChannelString), |
243 file_thread_("NetLogFileWriter file thread"), | 264 file_thread_("NetLogFileWriter file thread"), |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 << "Second state after Initialize() does not match expected:" | 322 << "Second state after Initialize() does not match expected:" |
302 << std::endl | 323 << std::endl |
303 << result.message(); | 324 << result.message(); |
304 } | 325 } |
305 | 326 |
306 return ::testing::AssertionSuccess(); | 327 return ::testing::AssertionSuccess(); |
307 } | 328 } |
308 | 329 |
309 // If |custom_log_path| is empty path, |net_log_file_writer_| will use its | 330 // 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_|. | 331 // default log path, which is cached in |default_log_path_|. |
311 WARN_UNUSED_RESULT ::testing::AssertionResult StartThenVerifyNewState( | 332 WARN_UNUSED_RESULT::testing::AssertionResult StartThenVerifyNewState( |
312 const base::FilePath& custom_log_path, | 333 const base::FilePath& custom_log_path, |
313 net::NetLogCaptureMode capture_mode, | 334 net::NetLogCaptureMode capture_mode, |
314 const std::string& expected_capture_mode_string) { | 335 const std::string& expected_capture_mode_string, |
315 net_log_file_writer_.StartNetLog(custom_log_path, capture_mode); | 336 const URLRequestContextGetterList& context_getters) { |
| 337 net_log_file_writer_.StartNetLog(custom_log_path, capture_mode, |
| 338 context_getters); |
316 std::unique_ptr<base::DictionaryValue> state = | 339 std::unique_ptr<base::DictionaryValue> state = |
317 test_state_observer_.WaitForNewState(); | 340 test_state_observer_.WaitForNewState(); |
318 ::testing::AssertionResult result = | 341 ::testing::AssertionResult result = |
319 VerifyState(std::move(state), kStateLoggingString, true, true, | 342 VerifyState(std::move(state), kStateStartingLogString); |
320 expected_capture_mode_string); | |
321 if (!result) { | 343 if (!result) { |
322 return ::testing::AssertionFailure() | 344 return ::testing::AssertionFailure() |
323 << "First state after StartNetLog() does not match expected:" | 345 << "First state after StartNetLog() does not match expected:" |
324 << std::endl | 346 << std::endl |
325 << result.message(); | 347 << result.message(); |
326 } | 348 } |
327 | 349 |
| 350 state = test_state_observer_.WaitForNewState(); |
| 351 result = VerifyState(std::move(state), kStateLoggingString, true, true, |
| 352 expected_capture_mode_string); |
| 353 if (!result) { |
| 354 return ::testing::AssertionFailure() |
| 355 << "Second state after StartNetLog() does not match expected:" |
| 356 << std::endl |
| 357 << result.message(); |
| 358 } |
| 359 |
328 // Make sure GetFilePath() returns empty path when logging. | 360 // Make sure GetFilePath() returns empty path when logging. |
329 base::FilePath actual_log_path = FileWriterGetFilePathToCompletedLog(); | 361 base::FilePath actual_log_path = FileWriterGetFilePathToCompletedLog(); |
330 if (!actual_log_path.empty()) { | 362 if (!actual_log_path.empty()) { |
331 return ::testing::AssertionFailure() | 363 return ::testing::AssertionFailure() |
332 << "GetFilePath() should return empty path after logging starts." | 364 << "GetFilePath() should return empty path after logging starts." |
333 << " Actual: " << ::testing::PrintToString(actual_log_path); | 365 << " Actual: " << ::testing::PrintToString(actual_log_path); |
334 } | 366 } |
335 | 367 |
336 return ::testing::AssertionSuccess(); | 368 return ::testing::AssertionSuccess(); |
337 } | 369 } |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
456 const std::string capture_mode_strings[3] = { | 488 const std::string capture_mode_strings[3] = { |
457 kCaptureModeDefaultString, kCaptureModeIncludeCookiesAndCredentialsString, | 489 kCaptureModeDefaultString, kCaptureModeIncludeCookiesAndCredentialsString, |
458 kCaptureModeIncludeSocketBytesString}; | 490 kCaptureModeIncludeSocketBytesString}; |
459 | 491 |
460 ASSERT_TRUE(InitializeThenVerifyNewState(true, false)); | 492 ASSERT_TRUE(InitializeThenVerifyNewState(true, false)); |
461 | 493 |
462 // 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. |
463 for (int i = 0; i < 3; ++i) { | 495 for (int i = 0; i < 3; ++i) { |
464 // StartNetLog(), should result in state change. | 496 // StartNetLog(), should result in state change. |
465 ASSERT_TRUE(StartThenVerifyNewState(base::FilePath(), capture_modes[i], | 497 ASSERT_TRUE(StartThenVerifyNewState(base::FilePath(), capture_modes[i], |
466 capture_mode_strings[i])); | 498 capture_mode_strings[i], |
| 499 URLRequestContextGetterList())); |
467 | 500 |
468 // Calling StartNetLog() again should be a no-op. Try doing StartNetLog() | 501 // 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 | 502 // with various capture modes; they should all be ignored and result in no |
470 // state change. | 503 // state change. |
471 net_log_file_writer_.StartNetLog(base::FilePath(), capture_modes[i]); | 504 net_log_file_writer_.StartNetLog(base::FilePath(), capture_modes[i], |
| 505 URLRequestContextGetterList()); |
472 net_log_file_writer_.StartNetLog(base::FilePath(), | 506 net_log_file_writer_.StartNetLog(base::FilePath(), |
473 capture_modes[(i + 1) % 3]); | 507 capture_modes[(i + 1) % 3], |
| 508 URLRequestContextGetterList()); |
474 net_log_file_writer_.StartNetLog(base::FilePath(), | 509 net_log_file_writer_.StartNetLog(base::FilePath(), |
475 capture_modes[(i + 2) % 3]); | 510 capture_modes[(i + 2) % 3], |
| 511 URLRequestContextGetterList()); |
476 | 512 |
477 // StopNetLog(), should result in state change. The capture mode should | 513 // StopNetLog(), should result in state change. The capture mode should |
478 // match that of the first StartNetLog() call (called by | 514 // match that of the first StartNetLog() call (called by |
479 // StartThenVerifyNewState()). | 515 // StartThenVerifyNewState()). |
480 ASSERT_TRUE(StopThenVerifyNewStateAndFile( | 516 ASSERT_TRUE(StopThenVerifyNewStateAndFile( |
481 base::FilePath(), nullptr, nullptr, capture_mode_strings[i])); | 517 base::FilePath(), nullptr, nullptr, capture_mode_strings[i])); |
482 | 518 |
483 // Stopping a second time should be a no-op. | 519 // Stopping a second time should be a no-op. |
484 net_log_file_writer_.StopNetLog(nullptr, nullptr); | 520 net_log_file_writer_.StopNetLog(nullptr, nullptr); |
485 } | 521 } |
486 | 522 |
487 // Start and stop one more time just to make sure the last StopNetLog() call | 523 // Start and stop one more time just to make sure the last StopNetLog() call |
488 // was properly ignored and left |net_log_file_writer_| in a valid state. | 524 // was properly ignored and left |net_log_file_writer_| in a valid state. |
489 ASSERT_TRUE(StartThenVerifyNewState(base::FilePath(), capture_modes[0], | 525 ASSERT_TRUE(StartThenVerifyNewState(base::FilePath(), capture_modes[0], |
490 capture_mode_strings[0])); | 526 capture_mode_strings[0], |
| 527 URLRequestContextGetterList())); |
491 | 528 |
492 ASSERT_TRUE(StopThenVerifyNewStateAndFile(base::FilePath(), nullptr, nullptr, | 529 ASSERT_TRUE(StopThenVerifyNewStateAndFile(base::FilePath(), nullptr, nullptr, |
493 capture_mode_strings[0])); | 530 capture_mode_strings[0])); |
494 } | 531 } |
495 | 532 |
496 // Verify the file sizes after two consecutive starts/stops are the same (even | 533 // Verify the file sizes after two consecutive starts/stops are the same (even |
497 // if some junk data is added in between). | 534 // if some junk data is added in between). |
498 TEST_F(NetLogFileWriterTest, StartClearsFile) { | 535 TEST_F(NetLogFileWriterTest, StartClearsFile) { |
499 ASSERT_TRUE(InitializeThenVerifyNewState(true, false)); | 536 ASSERT_TRUE(InitializeThenVerifyNewState(true, false)); |
500 | 537 |
501 ASSERT_TRUE(StartThenVerifyNewState(base::FilePath(), | 538 ASSERT_TRUE(StartThenVerifyNewState( |
502 net::NetLogCaptureMode::Default(), | 539 base::FilePath(), net::NetLogCaptureMode::Default(), |
503 kCaptureModeDefaultString)); | 540 kCaptureModeDefaultString, URLRequestContextGetterList())); |
504 | 541 |
505 ASSERT_TRUE(StopThenVerifyNewStateAndFile(base::FilePath(), nullptr, nullptr, | 542 ASSERT_TRUE(StopThenVerifyNewStateAndFile(base::FilePath(), nullptr, nullptr, |
506 kCaptureModeDefaultString)); | 543 kCaptureModeDefaultString)); |
507 | 544 |
508 int64_t stop_file_size; | 545 int64_t stop_file_size; |
509 EXPECT_TRUE(base::GetFileSize(default_log_path_, &stop_file_size)); | 546 EXPECT_TRUE(base::GetFileSize(default_log_path_, &stop_file_size)); |
510 | 547 |
511 // Add some junk at the end of the file. | 548 // Add some junk at the end of the file. |
512 std::string junk_data("Hello"); | 549 std::string junk_data("Hello"); |
513 EXPECT_TRUE(base::AppendToFile(default_log_path_, junk_data.c_str(), | 550 EXPECT_TRUE(base::AppendToFile(default_log_path_, junk_data.c_str(), |
514 junk_data.size())); | 551 junk_data.size())); |
515 | 552 |
516 int64_t junk_file_size; | 553 int64_t junk_file_size; |
517 EXPECT_TRUE(base::GetFileSize(default_log_path_, &junk_file_size)); | 554 EXPECT_TRUE(base::GetFileSize(default_log_path_, &junk_file_size)); |
518 EXPECT_GT(junk_file_size, stop_file_size); | 555 EXPECT_GT(junk_file_size, stop_file_size); |
519 | 556 |
520 // Start and stop again and make sure the file is back to the size it was | 557 // Start and stop again and make sure the file is back to the size it was |
521 // before adding the junk data. | 558 // before adding the junk data. |
522 ASSERT_TRUE(StartThenVerifyNewState(base::FilePath(), | 559 ASSERT_TRUE(StartThenVerifyNewState( |
523 net::NetLogCaptureMode::Default(), | 560 base::FilePath(), net::NetLogCaptureMode::Default(), |
524 kCaptureModeDefaultString)); | 561 kCaptureModeDefaultString, URLRequestContextGetterList())); |
525 | 562 |
526 ASSERT_TRUE(StopThenVerifyNewStateAndFile(base::FilePath(), nullptr, nullptr, | 563 ASSERT_TRUE(StopThenVerifyNewStateAndFile(base::FilePath(), nullptr, nullptr, |
527 kCaptureModeDefaultString)); | 564 kCaptureModeDefaultString)); |
528 | 565 |
529 int64_t new_stop_file_size; | 566 int64_t new_stop_file_size; |
530 EXPECT_TRUE(base::GetFileSize(default_log_path_, &new_stop_file_size)); | 567 EXPECT_TRUE(base::GetFileSize(default_log_path_, &new_stop_file_size)); |
531 | 568 |
532 EXPECT_EQ(stop_file_size, new_stop_file_size); | 569 EXPECT_EQ(stop_file_size, new_stop_file_size); |
533 } | 570 } |
534 | 571 |
535 // Adds an event to the log file, then checks that the file is larger than | 572 // Adds an event to the log file, then checks that the file is larger than |
536 // the file created without that event. | 573 // the file created without that event. |
537 TEST_F(NetLogFileWriterTest, AddEvent) { | 574 TEST_F(NetLogFileWriterTest, AddEvent) { |
538 ASSERT_TRUE(InitializeThenVerifyNewState(true, false)); | 575 ASSERT_TRUE(InitializeThenVerifyNewState(true, false)); |
539 | 576 |
540 ASSERT_TRUE(StartThenVerifyNewState(base::FilePath(), | 577 ASSERT_TRUE(StartThenVerifyNewState( |
541 net::NetLogCaptureMode::Default(), | 578 base::FilePath(), net::NetLogCaptureMode::Default(), |
542 kCaptureModeDefaultString)); | 579 kCaptureModeDefaultString, URLRequestContextGetterList())); |
543 | 580 |
544 ASSERT_TRUE(StopThenVerifyNewStateAndFile(base::FilePath(), nullptr, nullptr, | 581 ASSERT_TRUE(StopThenVerifyNewStateAndFile(base::FilePath(), nullptr, nullptr, |
545 kCaptureModeDefaultString)); | 582 kCaptureModeDefaultString)); |
546 | 583 |
547 // Get file size without the event. | 584 // Get file size without the event. |
548 int64_t stop_file_size; | 585 int64_t stop_file_size; |
549 EXPECT_TRUE(base::GetFileSize(default_log_path_, &stop_file_size)); | 586 EXPECT_TRUE(base::GetFileSize(default_log_path_, &stop_file_size)); |
550 | 587 |
551 ASSERT_TRUE(StartThenVerifyNewState(base::FilePath(), | 588 ASSERT_TRUE(StartThenVerifyNewState( |
552 net::NetLogCaptureMode::Default(), | 589 base::FilePath(), net::NetLogCaptureMode::Default(), |
553 kCaptureModeDefaultString)); | 590 kCaptureModeDefaultString, URLRequestContextGetterList())); |
554 | 591 |
555 net_log_.AddGlobalEntry(net::NetLogEventType::CANCELLED); | 592 net_log_.AddGlobalEntry(net::NetLogEventType::CANCELLED); |
556 | 593 |
557 ASSERT_TRUE(StopThenVerifyNewStateAndFile(base::FilePath(), nullptr, nullptr, | 594 ASSERT_TRUE(StopThenVerifyNewStateAndFile(base::FilePath(), nullptr, nullptr, |
558 kCaptureModeDefaultString)); | 595 kCaptureModeDefaultString)); |
559 | 596 |
560 // Get file size after adding the event and make sure it's larger than before. | 597 // Get file size after adding the event and make sure it's larger than before. |
561 int64_t new_stop_file_size; | 598 int64_t new_stop_file_size; |
562 EXPECT_TRUE(base::GetFileSize(default_log_path_, &new_stop_file_size)); | 599 EXPECT_TRUE(base::GetFileSize(default_log_path_, &new_stop_file_size)); |
563 EXPECT_GE(new_stop_file_size, stop_file_size); | 600 EXPECT_GE(new_stop_file_size, stop_file_size); |
564 } | 601 } |
565 | 602 |
566 // Using a custom path to make sure logging can still occur when the path has | 603 // Using a custom path to make sure logging can still occur when the path has |
567 // changed. | 604 // changed. |
568 TEST_F(NetLogFileWriterTest, AddEventCustomPath) { | 605 TEST_F(NetLogFileWriterTest, AddEventCustomPath) { |
569 ASSERT_TRUE(InitializeThenVerifyNewState(true, false)); | 606 ASSERT_TRUE(InitializeThenVerifyNewState(true, false)); |
570 | 607 |
571 base::FilePath::CharType kCustomRelativePath[] = | 608 base::FilePath::CharType kCustomRelativePath[] = |
572 FILE_PATH_LITERAL("custom/custom/chrome-net-export-log.json"); | 609 FILE_PATH_LITERAL("custom/custom/chrome-net-export-log.json"); |
573 base::FilePath custom_log_path = | 610 base::FilePath custom_log_path = |
574 log_temp_dir_.GetPath().Append(kCustomRelativePath); | 611 log_temp_dir_.GetPath().Append(kCustomRelativePath); |
575 EXPECT_TRUE( | 612 EXPECT_TRUE( |
576 base::CreateDirectoryAndGetError(custom_log_path.DirName(), nullptr)); | 613 base::CreateDirectoryAndGetError(custom_log_path.DirName(), nullptr)); |
577 | 614 |
578 ASSERT_TRUE(StartThenVerifyNewState(custom_log_path, | 615 ASSERT_TRUE(StartThenVerifyNewState( |
579 net::NetLogCaptureMode::Default(), | 616 custom_log_path, net::NetLogCaptureMode::Default(), |
580 kCaptureModeDefaultString)); | 617 kCaptureModeDefaultString, URLRequestContextGetterList())); |
581 | 618 |
582 ASSERT_TRUE(StopThenVerifyNewStateAndFile(custom_log_path, nullptr, nullptr, | 619 ASSERT_TRUE(StopThenVerifyNewStateAndFile(custom_log_path, nullptr, nullptr, |
583 kCaptureModeDefaultString)); | 620 kCaptureModeDefaultString)); |
584 | 621 |
585 // Get file size without the event. | 622 // Get file size without the event. |
586 int64_t stop_file_size; | 623 int64_t stop_file_size; |
587 EXPECT_TRUE(base::GetFileSize(custom_log_path, &stop_file_size)); | 624 EXPECT_TRUE(base::GetFileSize(custom_log_path, &stop_file_size)); |
588 | 625 |
589 ASSERT_TRUE(StartThenVerifyNewState(custom_log_path, | 626 ASSERT_TRUE(StartThenVerifyNewState( |
590 net::NetLogCaptureMode::Default(), | 627 custom_log_path, net::NetLogCaptureMode::Default(), |
591 kCaptureModeDefaultString)); | 628 kCaptureModeDefaultString, URLRequestContextGetterList())); |
592 | 629 |
593 net_log_.AddGlobalEntry(net::NetLogEventType::CANCELLED); | 630 net_log_.AddGlobalEntry(net::NetLogEventType::CANCELLED); |
594 | 631 |
595 ASSERT_TRUE(StopThenVerifyNewStateAndFile(custom_log_path, nullptr, nullptr, | 632 ASSERT_TRUE(StopThenVerifyNewStateAndFile(custom_log_path, nullptr, nullptr, |
596 kCaptureModeDefaultString)); | 633 kCaptureModeDefaultString)); |
597 | 634 |
598 // Get file size after adding the event and make sure it's larger than before. | 635 // Get file size after adding the event and make sure it's larger than before. |
599 int64_t new_stop_file_size; | 636 int64_t new_stop_file_size; |
600 EXPECT_TRUE(base::GetFileSize(custom_log_path, &new_stop_file_size)); | 637 EXPECT_TRUE(base::GetFileSize(custom_log_path, &new_stop_file_size)); |
601 EXPECT_GE(new_stop_file_size, stop_file_size); | 638 EXPECT_GE(new_stop_file_size, stop_file_size); |
(...skipping 12 matching lines...) Expand all Loading... |
614 // Create test context getter on |net_thread_| and wait for it to finish. | 651 // Create test context getter on |net_thread_| and wait for it to finish. |
615 scoped_refptr<net::TestURLRequestContextGetter> context_getter; | 652 scoped_refptr<net::TestURLRequestContextGetter> context_getter; |
616 const int kDummyQuicParam = 75; | 653 const int kDummyQuicParam = 75; |
617 net::TestClosure init_done; | 654 net::TestClosure init_done; |
618 net_thread_.task_runner()->PostTaskAndReply( | 655 net_thread_.task_runner()->PostTaskAndReply( |
619 FROM_HERE, base::Bind(&SetUpTestContextGetterWithQuicTimeoutInfo, | 656 FROM_HERE, base::Bind(&SetUpTestContextGetterWithQuicTimeoutInfo, |
620 &net_log_, kDummyQuicParam, &context_getter), | 657 &net_log_, kDummyQuicParam, &context_getter), |
621 init_done.closure()); | 658 init_done.closure()); |
622 init_done.WaitForResult(); | 659 init_done.WaitForResult(); |
623 | 660 |
624 ASSERT_TRUE(StartThenVerifyNewState(base::FilePath(), | 661 ASSERT_TRUE(StartThenVerifyNewState( |
625 net::NetLogCaptureMode::Default(), | 662 base::FilePath(), net::NetLogCaptureMode::Default(), |
626 kCaptureModeDefaultString)); | 663 kCaptureModeDefaultString, URLRequestContextGetterList())); |
627 | 664 |
628 ASSERT_TRUE(StopThenVerifyNewStateAndFile( | 665 ASSERT_TRUE(StopThenVerifyNewStateAndFile( |
629 base::FilePath(), std::move(dummy_polled_data), context_getter, | 666 base::FilePath(), std::move(dummy_polled_data), context_getter, |
630 kCaptureModeDefaultString)); | 667 kCaptureModeDefaultString)); |
631 | 668 |
632 // Read polledData from log file. | 669 // Read polledData from log file. |
633 std::unique_ptr<base::DictionaryValue> root; | 670 std::unique_ptr<base::DictionaryValue> root; |
634 ASSERT_TRUE(ReadCompleteLogFile(default_log_path_, &root)); | 671 ASSERT_TRUE(ReadCompleteLogFile(default_log_path_, &root)); |
635 base::DictionaryValue* polled_data; | 672 base::DictionaryValue* polled_data; |
636 ASSERT_TRUE(root->GetDictionary("polledData", &polled_data)); | 673 ASSERT_TRUE(root->GetDictionary("polledData", &polled_data)); |
637 | 674 |
638 // Check that it contains the field from the polled data that was passed in. | 675 // Check that it contains the field from the polled data that was passed in. |
639 std::string dummy_string; | 676 std::string dummy_string; |
640 ASSERT_TRUE(polled_data->GetString(kDummyPolledDataPath, &dummy_string)); | 677 ASSERT_TRUE(polled_data->GetString(kDummyPolledDataPath, &dummy_string)); |
641 EXPECT_EQ(kDummyPolledDataString, dummy_string); | 678 EXPECT_EQ(kDummyPolledDataString, dummy_string); |
642 | 679 |
643 // Check that it also contains the field from the URLRequestContext that was | 680 // Check that it also contains the field from the URLRequestContext that was |
644 // passed in. | 681 // passed in. |
645 base::DictionaryValue* quic_info; | 682 base::DictionaryValue* quic_info; |
646 ASSERT_TRUE(polled_data->GetDictionary("quicInfo", &quic_info)); | 683 ASSERT_TRUE(polled_data->GetDictionary("quicInfo", &quic_info)); |
647 base::Value* timeout_value = nullptr; | 684 base::Value* timeout_value = nullptr; |
648 int timeout; | 685 int timeout; |
649 ASSERT_TRUE( | 686 ASSERT_TRUE( |
650 quic_info->Get("idle_connection_timeout_seconds", &timeout_value)); | 687 quic_info->Get("idle_connection_timeout_seconds", &timeout_value)); |
651 ASSERT_TRUE(timeout_value->GetAsInteger(&timeout)); | 688 ASSERT_TRUE(timeout_value->GetAsInteger(&timeout)); |
652 EXPECT_EQ(kDummyQuicParam, timeout); | 689 EXPECT_EQ(kDummyQuicParam, timeout); |
653 } | 690 } |
654 | 691 |
| 692 TEST_F(NetLogFileWriterTest, StartWithContextGetters) { |
| 693 ASSERT_TRUE(InitializeThenVerifyNewState(true, false)); |
| 694 |
| 695 // Create test context getter and request on |net_thread_| and wait for it to |
| 696 // finish. |
| 697 const std::string kDummyUrl = "blah:blah"; |
| 698 scoped_refptr<net::TestURLRequestContextGetter> context_getter; |
| 699 std::unique_ptr<net::URLRequest> request; |
| 700 net::TestDelegate delegate; |
| 701 delegate.set_quit_on_complete(false); |
| 702 |
| 703 net::TestClosure init_done; |
| 704 net_thread_.task_runner()->PostTaskAndReply( |
| 705 FROM_HERE, |
| 706 base::Bind(&SetUpTestContextGetterWithRequest, &net_log_, GURL(kDummyUrl), |
| 707 &delegate, &context_getter, &request), |
| 708 init_done.closure()); |
| 709 init_done.WaitForResult(); |
| 710 |
| 711 ASSERT_TRUE(StartThenVerifyNewState( |
| 712 base::FilePath(), net::NetLogCaptureMode::Default(), |
| 713 kCaptureModeDefaultString, {context_getter})); |
| 714 |
| 715 ASSERT_TRUE(StopThenVerifyNewStateAndFile(base::FilePath(), nullptr, nullptr, |
| 716 kCaptureModeDefaultString)); |
| 717 |
| 718 // Read events from log file. |
| 719 std::unique_ptr<base::DictionaryValue> root; |
| 720 ASSERT_TRUE(ReadCompleteLogFile(default_log_path_, &root)); |
| 721 base::ListValue* events; |
| 722 ASSERT_TRUE(root->GetList("events", &events)); |
| 723 |
| 724 // Check there is at least one event as a result of the ongoing request. |
| 725 EXPECT_GE(events->GetSize(), 1u); |
| 726 |
| 727 // Check the URL in the params of the first event. |
| 728 base::DictionaryValue* event; |
| 729 EXPECT_TRUE(events->GetDictionary(0, &event)); |
| 730 base::DictionaryValue* event_params; |
| 731 EXPECT_TRUE(event->GetDictionary("params", &event_params)); |
| 732 std::string event_url; |
| 733 EXPECT_TRUE(event_params->GetString("url", &event_url)); |
| 734 EXPECT_EQ(kDummyUrl, event_url); |
| 735 |
| 736 net_thread_.task_runner()->DeleteSoon(FROM_HERE, request.release()); |
| 737 } |
| 738 |
655 TEST_F(NetLogFileWriterTest, ReceiveStartWhileInitializing) { | 739 TEST_F(NetLogFileWriterTest, ReceiveStartWhileInitializing) { |
656 // Trigger initialization of |net_log_file_writer_|. | 740 // Trigger initialization of |net_log_file_writer_|. |
657 net_log_file_writer_.Initialize(file_thread_.task_runner(), | 741 net_log_file_writer_.Initialize(file_thread_.task_runner(), |
658 net_thread_.task_runner()); | 742 net_thread_.task_runner()); |
659 | 743 |
660 // Before running the main message loop, tell |net_log_file_writer_| to start | 744 // Before running the main message loop, tell |net_log_file_writer_| to start |
661 // logging. Not running the main message loop prevents the initialization | 745 // logging. Not running the main message loop prevents the initialization |
662 // process from completing, so this ensures that StartNetLog() is received | 746 // process from completing, so this ensures that StartNetLog() is received |
663 // before |net_log_file_writer_| finishes initialization, which means this | 747 // before |net_log_file_writer_| finishes initialization, which means this |
664 // should be a no-op. | 748 // should be a no-op. |
665 net_log_file_writer_.StartNetLog(base::FilePath(), | 749 net_log_file_writer_.StartNetLog(base::FilePath(), |
666 net::NetLogCaptureMode::Default()); | 750 net::NetLogCaptureMode::Default(), |
| 751 URLRequestContextGetterList()); |
667 | 752 |
668 // Now run the main message loop. Make sure StartNetLog() was ignored by | 753 // Now run the main message loop. Make sure StartNetLog() was ignored by |
669 // checking that the next two states are "initializing" followed by | 754 // checking that the next two states are "initializing" followed by |
670 // "not-logging". | 755 // "not-logging". |
671 std::unique_ptr<base::DictionaryValue> state = | 756 std::unique_ptr<base::DictionaryValue> state = |
672 test_state_observer_.WaitForNewState(); | 757 test_state_observer_.WaitForNewState(); |
673 ASSERT_TRUE(VerifyState(std::move(state), kStateInitializingString)); | 758 ASSERT_TRUE(VerifyState(std::move(state), kStateInitializingString)); |
674 state = test_state_observer_.WaitForNewState(); | 759 state = test_state_observer_.WaitForNewState(); |
675 ASSERT_TRUE( | 760 ASSERT_TRUE( |
676 VerifyState(std::move(state), kStateNotLoggingString, false, false, "")); | 761 VerifyState(std::move(state), kStateNotLoggingString, false, false, "")); |
677 } | 762 } |
678 | 763 |
679 TEST_F(NetLogFileWriterTest, ReceiveStartWhileStoppingLog) { | 764 TEST_F(NetLogFileWriterTest, ReceiveStartWhileStoppingLog) { |
680 ASSERT_TRUE(InitializeThenVerifyNewState(true, false)); | 765 ASSERT_TRUE(InitializeThenVerifyNewState(true, false)); |
681 | 766 |
682 // Call StartNetLog() on |net_log_file_writer_| and wait for the state change. | 767 // Call StartNetLog() on |net_log_file_writer_| and wait for the state change. |
683 ASSERT_TRUE(StartThenVerifyNewState( | 768 ASSERT_TRUE(StartThenVerifyNewState( |
684 base::FilePath(), net::NetLogCaptureMode::IncludeSocketBytes(), | 769 base::FilePath(), net::NetLogCaptureMode::IncludeSocketBytes(), |
685 kCaptureModeIncludeSocketBytesString)); | 770 kCaptureModeIncludeSocketBytesString, URLRequestContextGetterList())); |
686 | 771 |
687 // Tell |net_log_file_writer_| to stop logging. | 772 // Tell |net_log_file_writer_| to stop logging. |
688 net_log_file_writer_.StopNetLog(nullptr, nullptr); | 773 net_log_file_writer_.StopNetLog(nullptr, nullptr); |
689 | 774 |
690 // Before running the main message loop, tell |net_log_file_writer_| to start | 775 // 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 | 776 // logging. Not running the main message loop prevents the stopping process |
692 // from completing, so this ensures StartNetLog() is received before | 777 // from completing, so this ensures StartNetLog() is received before |
693 // |net_log_file_writer_| finishes stopping, which means this should be a | 778 // |net_log_file_writer_| finishes stopping, which means this should be a |
694 // no-op. | 779 // no-op. |
695 net_log_file_writer_.StartNetLog(base::FilePath(), | 780 net_log_file_writer_.StartNetLog(base::FilePath(), |
696 net::NetLogCaptureMode::Default()); | 781 net::NetLogCaptureMode::Default(), |
| 782 URLRequestContextGetterList()); |
697 | 783 |
698 // Now run the main message loop. Make sure the last StartNetLog() was | 784 // 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 | 785 // 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 | 786 // "not-logging". Also make sure the capture mode matches that of the first |
701 // StartNetLog() call (called by StartThenVerifyState()). | 787 // StartNetLog() call (called by StartThenVerifyState()). |
702 std::unique_ptr<base::DictionaryValue> state = | 788 std::unique_ptr<base::DictionaryValue> state = |
703 test_state_observer_.WaitForNewState(); | 789 test_state_observer_.WaitForNewState(); |
704 ASSERT_TRUE(VerifyState(std::move(state), kStateStoppingLogString)); | 790 ASSERT_TRUE(VerifyState(std::move(state), kStateStoppingLogString)); |
705 state = test_state_observer_.WaitForNewState(); | 791 state = test_state_observer_.WaitForNewState(); |
706 ASSERT_TRUE(VerifyState(std::move(state), kStateNotLoggingString, true, true, | 792 ASSERT_TRUE(VerifyState(std::move(state), kStateNotLoggingString, true, true, |
707 kCaptureModeIncludeSocketBytesString)); | 793 kCaptureModeIncludeSocketBytesString)); |
708 } | 794 } |
709 | 795 |
710 } // namespace net_log | 796 } // namespace net_log |
OLD | NEW |