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