Chromium Code Reviews| 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 |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 14 #include "base/files/scoped_file.h" | 14 #include "base/files/scoped_file.h" |
| 15 #include "base/files/scoped_temp_dir.h" | 15 #include "base/files/scoped_temp_dir.h" |
| 16 #include "base/json/json_reader.h" | 16 #include "base/json/json_reader.h" |
| 17 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
| 18 #include "base/run_loop.h" | |
| 19 #include "base/test/test_simple_task_runner.h" | |
| 20 #include "base/threading/thread.h" | |
| 18 #include "base/values.h" | 21 #include "base/values.h" |
| 19 #include "build/build_config.h" | 22 #include "build/build_config.h" |
| 20 #include "components/net_log/chrome_net_log.h" | 23 #include "components/net_log/chrome_net_log.h" |
| 24 #include "net/http/http_network_session.h" | |
| 21 #include "net/log/net_log_capture_mode.h" | 25 #include "net/log/net_log_capture_mode.h" |
| 22 #include "net/log/net_log_event_type.h" | 26 #include "net/log/net_log_event_type.h" |
| 23 #include "net/log/write_to_file_net_log_observer.h" | 27 #include "net/log/write_to_file_net_log_observer.h" |
| 28 #include "net/url_request/url_request_context_getter.h" | |
| 29 #include "net/url_request/url_request_test_util.h" | |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 31 |
| 26 namespace { | 32 namespace { |
| 27 | 33 |
| 28 const char kChannelString[] = "SomeChannel"; | 34 const char kChannelString[] = "SomeChannel"; |
| 29 | 35 |
| 36 // Keep this in sync with kLogRelativePath defined in net_log_file_writer.cc. | |
| 37 base::FilePath::CharType kLogRelativePath[] = | |
| 38 FILE_PATH_LITERAL("net-export/chrome-net-export-log.json"); | |
| 39 | |
| 40 const char kCaptureModeDefaultString[] = "STRIP_PRIVATE_DATA"; | |
| 41 const char kCaptureModeIncludeCookiesAndCredentialsString[] = "NORMAL"; | |
| 42 const char kCaptureModeIncludeSocketBytesString[] = "LOG_BYTES"; | |
| 43 | |
| 44 const char kStateUninitializedString[] = "UNINITIALIZED"; | |
| 45 const char kStateInitializingString[] = "INITIALIZING"; | |
| 46 const char kStateNotLoggingString[] = "NOT_LOGGING"; | |
| 47 const char kStateLoggingString[] = "LOGGING"; | |
| 48 const char kStateStoppingLogString[] = "STOPPING_LOG"; | |
| 49 | |
| 30 } // namespace | 50 } // namespace |
| 31 | 51 |
| 32 namespace net_log { | 52 namespace net_log { |
| 33 | 53 |
| 34 class TestNetLogFileWriter : public NetLogFileWriter { | 54 void VerifyFileExistsAndNotEmpty(const base::FilePath& path) { |
| 55 DCHECK(!path.empty()); | |
| 56 | |
| 57 EXPECT_TRUE(base::PathExists(path)); | |
| 58 | |
| 59 int64_t file_size; | |
| 60 // base::GetFileSize returns proper file size on open handles. | |
| 61 ASSERT_TRUE(base::GetFileSize(path, &file_size)); | |
| 62 EXPECT_GT(file_size, 0); | |
| 63 } | |
| 64 | |
| 65 void VerifyFileIsValidJSON(const base::FilePath& path) { | |
| 66 DCHECK(!path.empty()); | |
| 67 | |
| 68 VerifyFileExistsAndNotEmpty(path); | |
| 69 | |
| 70 std::string file_string; | |
| 71 ASSERT_TRUE(ReadFileToString(path, &file_string)); | |
| 72 std::unique_ptr<base::Value> json = base::JSONReader::Read(file_string); | |
| 73 EXPECT_TRUE(json); | |
| 74 } | |
| 75 | |
| 76 // Helper function to run |task_runner_a|, |task_runner_b|, and the main message | |
| 77 // loop until all are idle. Assume that either task runner can only post replies | |
| 78 // to the main thread (i.e. they cannot post tasks directly to each other). | |
| 79 void RunTaskRunnersUntilIdle( | |
|
eroman
2017/01/24 20:01:36
I think a better approach is to use separate threa
wangyix1
2017/01/25 22:48:27
Done.
Many tests have been modified to become whi
| |
| 80 scoped_refptr<base::TestSimpleTaskRunner> task_runner_a, | |
| 81 scoped_refptr<base::TestSimpleTaskRunner> task_runner_b) { | |
| 82 // First, run the main message loop to handle any async tasks posted to the | |
| 83 // main thread. | |
| 84 base::RunLoop().RunUntilIdle(); | |
| 85 | |
| 86 // Repeatedly try to run both task runners until they're simultaneously idle. | |
| 87 // After running the pending tasks on a task runner, run the main message loop | |
| 88 // until idle since those tasks could've posted replies to the main thread. | |
| 89 // Now, the replies on the main thread could possibly post tasks to either | |
| 90 // task runner. Therefore, only stop if the two task runners are | |
| 91 // simultaneously idle. | |
| 92 bool keepRunning; | |
| 93 do { | |
| 94 keepRunning = false; | |
| 95 | |
| 96 if (task_runner_a->HasPendingTask()) { | |
| 97 task_runner_a->RunUntilIdle(); | |
| 98 base::RunLoop().RunUntilIdle(); | |
| 99 keepRunning = true; | |
| 100 continue; | |
| 101 } | |
| 102 | |
| 103 if (task_runner_b->HasPendingTask()) { | |
| 104 task_runner_b->RunUntilIdle(); | |
| 105 base::RunLoop().RunUntilIdle(); | |
| 106 keepRunning = true; | |
| 107 } | |
| 108 } while (keepRunning); | |
| 109 } | |
| 110 | |
| 111 // A class similar to TestCompletionCallbackTemplate, but specialized for | |
| 112 // StateCallbacks. One difference is that WaitUntilIdle() is used here instead | |
| 113 // of WaitForResult(). WaitUntilIdle() will run the main thread and two | |
| 114 // TestSimpleTaskRunners until they're all idle before storing the callback | |
| 115 // result. | |
| 116 class TestStateCallback { | |
| 35 public: | 117 public: |
| 36 explicit TestNetLogFileWriter(ChromeNetLog* chrome_net_log) | 118 TestStateCallback() |
| 37 : NetLogFileWriter( | 119 : have_result_(false), |
| 38 chrome_net_log, | 120 callback_(base::Bind(&TestStateCallback::SetResult, |
| 39 base::CommandLine::ForCurrentProcess()->GetCommandLineString(), | 121 base::Unretained(this))) {} |
| 40 kChannelString), | 122 |
| 41 lie_about_net_export_log_directory_(false) { | 123 const base::Callback<void(std::unique_ptr<base::DictionaryValue>)>& |
| 42 EXPECT_TRUE(net_log_temp_dir_.CreateUniqueTempDir()); | 124 Callback() { |
| 43 } | 125 return callback_; |
| 44 | 126 } |
| 45 ~TestNetLogFileWriter() override { EXPECT_TRUE(net_log_temp_dir_.Delete()); } | 127 |
| 46 | 128 std::unique_ptr<base::DictionaryValue> WaitUntilIdle( |
| 47 // NetLogFileWriter implementation: | 129 scoped_refptr<base::TestSimpleTaskRunner> task_runner_a, |
| 48 bool GetNetExportLogBaseDirectory(base::FilePath* path) const override { | 130 scoped_refptr<base::TestSimpleTaskRunner> task_runner_b) { |
| 49 if (lie_about_net_export_log_directory_) | 131 RunTaskRunnersUntilIdle(task_runner_a, task_runner_b); |
| 50 return false; | 132 DCHECK(have_result_); |
| 51 *path = net_log_temp_dir_.GetPath(); | 133 have_result_ = false; |
| 52 return true; | 134 return std::move(result_); |
| 53 } | |
| 54 | |
| 55 void set_lie_about_net_export_log_directory( | |
| 56 bool lie_about_net_export_log_directory) { | |
| 57 lie_about_net_export_log_directory_ = lie_about_net_export_log_directory; | |
| 58 } | 135 } |
| 59 | 136 |
| 60 private: | 137 private: |
| 61 bool lie_about_net_export_log_directory_; | 138 void SetResult(std::unique_ptr<base::DictionaryValue> result) { |
| 62 | 139 result_ = std::move(result); |
| 63 base::ScopedTempDir net_log_temp_dir_; | 140 have_result_ = true; |
| 141 } | |
| 142 | |
| 143 bool have_result_; | |
| 144 std::unique_ptr<base::DictionaryValue> result_; | |
| 145 base::Callback<void(std::unique_ptr<base::DictionaryValue>)> callback_; | |
| 146 }; | |
| 147 | |
| 148 // A class similar to TestCompletionCallbackTemplate, but specialized for | |
| 149 // FilePathCallbacks. One difference is that WaitUntilIdle() is used here | |
| 150 // instead of WaitForResult(). WaitUntilIdle() will run the main thread and two | |
| 151 // TestSimpleTaskRunners until they're all idle before storing the callback | |
| 152 // result. | |
| 153 class TestFilePathCallback { | |
| 154 public: | |
| 155 TestFilePathCallback() | |
| 156 : have_result_(false), | |
| 157 callback_(base::Bind(&TestFilePathCallback::SetResult, | |
| 158 base::Unretained(this))) {} | |
| 159 | |
| 160 const base::Callback<void(const base::FilePath&)>& Callback() { | |
| 161 return callback_; | |
| 162 } | |
| 163 | |
| 164 const base::FilePath& WaitUntilIdle( | |
| 165 scoped_refptr<base::TestSimpleTaskRunner> task_runner_a, | |
| 166 scoped_refptr<base::TestSimpleTaskRunner> task_runner_b) { | |
| 167 RunTaskRunnersUntilIdle(task_runner_a, task_runner_b); | |
| 168 DCHECK(have_result_); | |
| 169 have_result_ = false; | |
| 170 return result_; | |
| 171 } | |
| 172 | |
| 173 private: | |
| 174 void SetResult(const base::FilePath& result) { | |
| 175 result_ = result; | |
| 176 have_result_ = true; | |
| 177 } | |
| 178 | |
| 179 bool have_result_; | |
| 180 base::FilePath result_; | |
| 181 base::Callback<void(const base::FilePath&)> callback_; | |
| 64 }; | 182 }; |
| 65 | 183 |
| 66 class NetLogFileWriterTest : public ::testing::Test { | 184 class NetLogFileWriterTest : public ::testing::Test { |
| 67 public: | 185 public: |
| 68 NetLogFileWriterTest() | 186 NetLogFileWriterTest() |
| 69 : net_log_(new ChromeNetLog( | 187 : net_log_(base::FilePath(), |
| 70 base::FilePath(), | 188 net::NetLogCaptureMode::Default(), |
| 71 net::NetLogCaptureMode::Default(), | 189 base::CommandLine::ForCurrentProcess()->GetCommandLineString(), |
| 190 kChannelString), | |
| 191 net_log_file_writer_( | |
| 192 &net_log_, | |
| 72 base::CommandLine::ForCurrentProcess()->GetCommandLineString(), | 193 base::CommandLine::ForCurrentProcess()->GetCommandLineString(), |
| 73 kChannelString)), | 194 kChannelString), |
| 74 net_log_file_writer_(new TestNetLogFileWriter(net_log_.get())) {} | 195 file_task_runner_(new base::TestSimpleTaskRunner()), |
| 75 | 196 net_task_runner_(new base::TestSimpleTaskRunner()) {} |
| 76 std::string GetStateString() const { | 197 |
| 77 std::unique_ptr<base::DictionaryValue> dict( | 198 // ::testing::Test implementation |
| 78 net_log_file_writer_->GetState()); | 199 void SetUp() override { |
| 79 std::string state; | 200 ASSERT_TRUE(log_temp_dir_.CreateUniqueTempDir()); |
| 80 EXPECT_TRUE(dict->GetString("state", &state)); | 201 |
| 81 return state; | 202 // Override |net_log_file_writer_|'s default-log-base-directory-getter to |
| 82 } | 203 // a getter that returns the temp dir created for the test. |
| 83 | 204 net_log_file_writer_.SetDefaultLogBaseDirectoryGetterForTest(base::Bind( |
| 84 std::string GetLogTypeString() const { | 205 &NetLogFileWriterTest::GetLogTempDirPath, log_temp_dir_.GetPath())); |
| 85 std::unique_ptr<base::DictionaryValue> dict( | 206 |
| 86 net_log_file_writer_->GetState()); | 207 default_log_path_ = log_temp_dir_.GetPath().Append(kLogRelativePath); |
| 87 std::string log_type; | 208 |
| 88 EXPECT_TRUE(dict->GetString("logType", &log_type)); | 209 net_log_file_writer_.SetTaskRunners(file_task_runner_, net_task_runner_); |
| 89 return log_type; | 210 } |
| 90 } | 211 |
| 91 | 212 void TearDown() override { |
| 92 // Make sure the export file has been created and is non-empty, as net | 213 DCHECK(!(file_task_runner_->HasPendingTask())); |
| 93 // constants will always be written to it on creation. | 214 DCHECK(!(net_task_runner_->HasPendingTask())); |
| 94 void VerifyNetExportLogExists() { | 215 ASSERT_TRUE(log_temp_dir_.Delete()); |
| 95 net_export_log_ = net_log_file_writer_->log_path_; | 216 } |
| 96 ASSERT_TRUE(base::PathExists(net_export_log_)); | 217 |
| 97 | 218 // A getter used to override NetLogFileWriter's usual getter that's used to |
| 98 int64_t file_size; | 219 // retrieve the default base directory for the log file. |
| 99 // base::GetFileSize returns proper file size on open handles. | 220 static bool GetLogTempDirPath(const base::FilePath& path_to_return, |
| 100 ASSERT_TRUE(base::GetFileSize(net_export_log_, &file_size)); | 221 base::FilePath* path) { |
| 101 EXPECT_GT(file_size, 0); | 222 *path = path_to_return; |
| 102 } | 223 return true; |
| 103 | 224 } |
| 104 // Make sure the export file has been created and a valid JSON file. This | 225 |
| 105 // should always be the case once logging has been stopped. | 226 std::unique_ptr<base::DictionaryValue> FileWriterGetState() { |
| 106 void VerifyNetExportLogComplete() { | 227 TestStateCallback test_callback; |
| 107 VerifyNetExportLogExists(); | 228 net_log_file_writer_.GetState(test_callback.Callback()); |
| 108 | 229 return test_callback.WaitUntilIdle(file_task_runner_, net_task_runner_); |
| 109 std::string log; | 230 } |
| 110 ASSERT_TRUE(ReadFileToString(net_export_log_, &log)); | 231 |
| 111 base::JSONReader reader; | 232 std::unique_ptr<base::DictionaryValue> |
| 112 std::unique_ptr<base::Value> json = base::JSONReader::Read(log); | 233 FileWriterGetStateWithoutRunningTaskRunners() { |
| 113 EXPECT_TRUE(json); | 234 // By passing two new temporary TestSimpleTaskRunners to WaitUntilIdle() |
| 114 } | 235 // instead of |file_task_runner_| and |net_task_runner_|, it allows |
| 115 | 236 // GetState() to complete without allowing any tasks on |file_task_runner_| |
| 116 // Verify state and GetFilePath return correct values if EnsureInit() fails. | 237 // or |net_task_runner_| to run. |
| 117 void VerifyFilePathAndStateAfterEnsureInitFailure() { | 238 // This only works if |net_log_file_writer_| is already initialized. |
| 118 EXPECT_EQ("UNINITIALIZED", GetStateString()); | 239 // Otherwise, GetState() would trigger initialization, which requires |
| 119 EXPECT_EQ(NetLogFileWriter::STATE_UNINITIALIZED, | 240 // running tasks on |file_task_runner_|. |
| 120 net_log_file_writer_->state()); | 241 TestStateCallback test_callback; |
| 121 | 242 net_log_file_writer_.GetState(test_callback.Callback()); |
| 122 base::FilePath net_export_file_path; | 243 return test_callback.WaitUntilIdle(new base::TestSimpleTaskRunner(), |
| 123 EXPECT_FALSE(net_log_file_writer_->GetFilePath(&net_export_file_path)); | 244 new base::TestSimpleTaskRunner()); |
| 124 } | 245 } |
| 125 | 246 |
| 126 // When we lie in NetExportLogExists, make sure state and GetFilePath return | 247 base::FilePath FileWriterGetFilePathToCompletedLog() { |
| 127 // correct values. | 248 TestFilePathCallback test_callback; |
| 128 void VerifyFilePathAndStateAfterEnsureInit() { | 249 net_log_file_writer_.GetFilePathToCompletedLog(test_callback.Callback()); |
| 129 EXPECT_EQ("NOT_LOGGING", GetStateString()); | 250 return test_callback.WaitUntilIdle(file_task_runner_, net_task_runner_); |
| 130 EXPECT_EQ(NetLogFileWriter::STATE_NOT_LOGGING, | 251 } |
| 131 net_log_file_writer_->state()); | 252 |
| 132 EXPECT_EQ("NONE", GetLogTypeString()); | 253 // Checks the fields of the state returned by NetLogFileWriter's callbacks. |
| 133 EXPECT_EQ(NetLogFileWriter::LOG_TYPE_NONE, | 254 // |expected_log_capture_mode_string| is only checked if |
| 134 net_log_file_writer_->log_type()); | 255 // |expected_log_capture_mode_known| is true. |
| 135 | 256 void VerifyState(std::unique_ptr<base::DictionaryValue> state, |
| 136 base::FilePath net_export_file_path; | 257 const std::string& expected_state_string, |
| 137 EXPECT_FALSE(net_log_file_writer_->GetFilePath(&net_export_file_path)); | 258 bool expected_log_exists, |
| 138 EXPECT_FALSE(net_log_file_writer_->NetExportLogExists()); | 259 bool expected_log_capture_mode_known, |
| 139 } | 260 const std::string& expected_log_capture_mode_string) { |
| 140 | 261 std::string state_string; |
| 141 // The following methods make sure the export file has been successfully | 262 ASSERT_TRUE(state->GetString("state", &state_string)); |
| 142 // initialized by a DO_START command of the given type. | 263 EXPECT_EQ(state_string, expected_state_string); |
| 143 | 264 |
| 144 void VerifyFileAndStateAfterDoStart() { | 265 bool log_exists; |
| 145 VerifyFileAndStateAfterStart( | 266 ASSERT_TRUE(state->GetBoolean("logExists", &log_exists)); |
| 146 NetLogFileWriter::LOG_TYPE_NORMAL, "NORMAL", | 267 EXPECT_EQ(log_exists, expected_log_exists); |
| 147 net::NetLogCaptureMode::IncludeCookiesAndCredentials()); | 268 |
| 148 } | 269 bool log_capture_mode_known; |
| 149 | 270 ASSERT_TRUE( |
| 150 void VerifyFileAndStateAfterDoStartStripPrivateData() { | 271 state->GetBoolean("logCaptureModeKnown", &log_capture_mode_known)); |
| 151 VerifyFileAndStateAfterStart(NetLogFileWriter::LOG_TYPE_STRIP_PRIVATE_DATA, | 272 EXPECT_EQ(log_capture_mode_known, expected_log_capture_mode_known); |
| 152 "STRIP_PRIVATE_DATA", | 273 |
| 153 net::NetLogCaptureMode::Default()); | 274 if (expected_log_capture_mode_known) { |
| 154 } | 275 std::string log_capture_mode_string; |
| 155 | 276 ASSERT_TRUE(state->GetString("captureMode", &log_capture_mode_string)); |
| 156 void VerifyFileAndStateAfterDoStartLogBytes() { | 277 EXPECT_EQ(log_capture_mode_string, expected_log_capture_mode_string); |
| 157 VerifyFileAndStateAfterStart(NetLogFileWriter::LOG_TYPE_LOG_BYTES, | 278 } |
| 158 "LOG_BYTES", | 279 } |
| 159 net::NetLogCaptureMode::IncludeSocketBytes()); | 280 |
| 160 } | 281 // If |custom_log_path| is empty path, |net_log_file_writer_| will use its |
| 161 | 282 // default log path. |
| 162 // Make sure the export file has been successfully initialized after DO_STOP | 283 void StartThenVerifyFileAndState( |
| 163 // command following a DO_START command of the given type. | 284 const base::FilePath& custom_log_path, |
| 164 | 285 net::NetLogCaptureMode capture_mode, |
| 165 void VerifyFileAndStateAfterDoStop() { | 286 const std::string& expected_capture_mode_string) { |
| 166 VerifyFileAndStateAfterDoStop(NetLogFileWriter::LOG_TYPE_NORMAL, "NORMAL"); | 287 TestStateCallback test_callback; |
| 167 } | 288 net_log_file_writer_.StartNetLog(custom_log_path, capture_mode, |
| 168 | 289 test_callback.Callback()); |
| 169 void VerifyFileAndStateAfterDoStopWithStripPrivateData() { | 290 VerifyState( |
| 170 VerifyFileAndStateAfterDoStop(NetLogFileWriter::LOG_TYPE_STRIP_PRIVATE_DATA, | 291 test_callback.WaitUntilIdle(file_task_runner_, net_task_runner_), |
| 171 "STRIP_PRIVATE_DATA"); | 292 kStateLoggingString, true, true, expected_capture_mode_string); |
| 172 } | 293 |
| 173 | 294 // Make sure NetLogFileWriter::GetFilePath() returns empty path when |
| 174 void VerifyFileAndStateAfterDoStopWithLogBytes() { | 295 // logging. |
| 175 VerifyFileAndStateAfterDoStop(NetLogFileWriter::LOG_TYPE_LOG_BYTES, | 296 EXPECT_EQ(FileWriterGetFilePathToCompletedLog(), base::FilePath()); |
| 176 "LOG_BYTES"); | 297 |
| 177 } | 298 // The log file should exist and be non-empty since StartnetLog() always |
|
eroman
2017/01/24 20:01:36
StartNetLog
wangyix1
2017/01/25 22:48:27
Comment no longer exists.
| |
| 178 | 299 // writes constants to the log file. |
| 179 std::unique_ptr<ChromeNetLog> net_log_; | 300 DCHECK(!file_task_runner_->HasPendingTask()); |
| 301 VerifyFileExistsAndNotEmpty(custom_log_path.empty() ? default_log_path_ | |
| 302 : custom_log_path); | |
| 303 } | |
| 304 | |
| 305 // If |custom_log_path| is empty path, it's assumed the log file with be at | |
| 306 // |default_path_|. | |
| 307 void StopThenVerifyFileAndState( | |
| 308 const base::FilePath& custom_log_path, | |
| 309 std::unique_ptr<base::DictionaryValue> polled_data, | |
| 310 scoped_refptr<net::URLRequestContextGetter> context_getter, | |
| 311 const std::string& expected_capture_mode_string) { | |
| 312 TestStateCallback test_callback; | |
| 313 net_log_file_writer_.StopNetLog(std::move(polled_data), context_getter, | |
| 314 test_callback.Callback()); | |
| 315 VerifyState( | |
| 316 test_callback.WaitUntilIdle(file_task_runner_, net_task_runner_), | |
| 317 kStateNotLoggingString, true, true, expected_capture_mode_string); | |
| 318 | |
| 319 const base::FilePath& log_path = | |
| 320 custom_log_path.empty() ? default_log_path_ : custom_log_path; | |
| 321 EXPECT_EQ(FileWriterGetFilePathToCompletedLog(), log_path); | |
| 322 | |
| 323 DCHECK(!file_task_runner_->HasPendingTask()); | |
| 324 VerifyFileIsValidJSON(log_path); | |
| 325 } | |
| 326 | |
| 327 protected: | |
| 328 ChromeNetLog net_log_; | |
| 329 | |
| 180 // |net_log_file_writer_| is initialized after |net_log_| so that it can stop | 330 // |net_log_file_writer_| is initialized after |net_log_| so that it can stop |
| 181 // obvserving on destruction. | 331 // obvserving on destruction. |
| 182 std::unique_ptr<TestNetLogFileWriter> net_log_file_writer_; | 332 NetLogFileWriter net_log_file_writer_; |
| 183 base::FilePath net_export_log_; | 333 |
| 334 base::ScopedTempDir log_temp_dir_; | |
| 335 | |
| 336 // The default log path that |net_log_file_writer_| will use is cached here. | |
| 337 base::FilePath default_log_path_; | |
| 338 | |
| 339 scoped_refptr<base::TestSimpleTaskRunner> file_task_runner_; | |
| 340 scoped_refptr<base::TestSimpleTaskRunner> net_task_runner_; | |
| 184 | 341 |
| 185 private: | 342 private: |
| 186 // Checks state after one of the DO_START* commands. | 343 // Allows tasks to be posted to the main thread. |
| 187 void VerifyFileAndStateAfterStart( | |
| 188 NetLogFileWriter::LogType expected_log_type, | |
| 189 const std::string& expected_log_type_string, | |
| 190 net::NetLogCaptureMode expected_capture_mode) { | |
| 191 EXPECT_EQ(NetLogFileWriter::STATE_LOGGING, net_log_file_writer_->state()); | |
| 192 EXPECT_EQ("LOGGING", GetStateString()); | |
| 193 EXPECT_EQ(expected_log_type, net_log_file_writer_->log_type()); | |
| 194 EXPECT_EQ(expected_log_type_string, GetLogTypeString()); | |
| 195 EXPECT_EQ(expected_capture_mode, | |
| 196 net_log_file_writer_->write_to_file_observer_->capture_mode()); | |
| 197 | |
| 198 // Check GetFilePath returns false when still writing to the file. | |
| 199 base::FilePath net_export_file_path; | |
| 200 EXPECT_FALSE(net_log_file_writer_->GetFilePath(&net_export_file_path)); | |
| 201 | |
| 202 VerifyNetExportLogExists(); | |
| 203 } | |
| 204 | |
| 205 void VerifyFileAndStateAfterDoStop( | |
| 206 NetLogFileWriter::LogType expected_log_type, | |
| 207 const std::string& expected_log_type_string) { | |
| 208 EXPECT_EQ(NetLogFileWriter::STATE_NOT_LOGGING, | |
| 209 net_log_file_writer_->state()); | |
| 210 EXPECT_EQ("NOT_LOGGING", GetStateString()); | |
| 211 EXPECT_EQ(expected_log_type, net_log_file_writer_->log_type()); | |
| 212 EXPECT_EQ(expected_log_type_string, GetLogTypeString()); | |
| 213 | |
| 214 base::FilePath net_export_file_path; | |
| 215 EXPECT_TRUE(net_log_file_writer_->GetFilePath(&net_export_file_path)); | |
| 216 EXPECT_EQ(net_export_log_, net_export_file_path); | |
| 217 | |
| 218 VerifyNetExportLogComplete(); | |
| 219 } | |
| 220 | |
| 221 base::MessageLoop message_loop_; | 344 base::MessageLoop message_loop_; |
| 222 }; | 345 }; |
| 223 | 346 |
| 224 TEST_F(NetLogFileWriterTest, EnsureInitFailure) { | 347 TEST_F(NetLogFileWriterTest, InitFail) { |
| 225 net_log_file_writer_->set_lie_about_net_export_log_directory(true); | 348 // Override net_log_file_writer_'s default log base directory getter to always |
| 226 | 349 // fail. |
| 227 EXPECT_FALSE(net_log_file_writer_->EnsureInit()); | 350 net_log_file_writer_.SetDefaultLogBaseDirectoryGetterForTest( |
| 228 VerifyFilePathAndStateAfterEnsureInitFailure(); | 351 base::Bind([](base::FilePath* path) -> bool { return false; })); |
| 229 | 352 |
| 230 net_log_file_writer_->ProcessCommand(NetLogFileWriter::DO_START); | 353 // GetState() will cause |net_log_file_writer_| to initialize. In this case, |
| 231 VerifyFilePathAndStateAfterEnsureInitFailure(); | 354 // initialization will fail since |net_log_file_writer_| will fail to retrive |
|
eroman
2017/01/24 20:01:36
retrieve
wangyix1
2017/01/25 22:48:27
Done.
| |
| 232 } | 355 // the default log base directory. |
| 233 | 356 VerifyState(FileWriterGetState(), kStateUninitializedString, false, false, |
| 234 TEST_F(NetLogFileWriterTest, EnsureInitAllowStart) { | 357 ""); |
| 235 EXPECT_TRUE(net_log_file_writer_->EnsureInit()); | 358 |
| 236 VerifyFilePathAndStateAfterEnsureInit(); | 359 // NetLogFileWriter::GetFilePath() should return empty path if uninitialized. |
| 237 | 360 EXPECT_TRUE(FileWriterGetFilePathToCompletedLog().empty()); |
| 238 // Calling EnsureInit() second time should be a no-op. | 361 } |
| 239 EXPECT_TRUE(net_log_file_writer_->EnsureInit()); | 362 |
| 240 VerifyFilePathAndStateAfterEnsureInit(); | 363 TEST_F(NetLogFileWriterTest, InitWithoutExistingLog) { |
| 241 | 364 // GetState() will cause |net_log_file_writer_| to initialize. |
| 242 // GetFilePath() should failed when there's no file. | 365 VerifyState(FileWriterGetState(), kStateNotLoggingString, false, false, ""); |
| 243 base::FilePath net_export_file_path; | 366 |
| 244 EXPECT_FALSE(net_log_file_writer_->GetFilePath(&net_export_file_path)); | 367 // NetLogFileWriter::GetFilePathToCompletedLog() should return empty path when |
| 245 } | 368 // no log file exists. |
| 246 | 369 EXPECT_TRUE(FileWriterGetFilePathToCompletedLog().empty()); |
| 247 TEST_F(NetLogFileWriterTest, EnsureInitAllowStartOrSend) { | 370 } |
| 248 net_log_file_writer_->SetUpDefaultNetExportLogPath(); | 371 |
| 249 net_export_log_ = net_log_file_writer_->log_path_; | 372 TEST_F(NetLogFileWriterTest, InitWithExistingLog) { |
| 250 | 373 // Create and close an empty log file to simulate existence of a previous log |
| 251 // Create and close an empty log file, to simulate an old log file already | 374 // file. |
| 252 // existing. | 375 ASSERT_TRUE( |
| 253 base::ScopedFILE created_file(base::OpenFile(net_export_log_, "w")); | 376 base::CreateDirectoryAndGetError(default_log_path_.DirName(), nullptr)); |
| 254 ASSERT_TRUE(created_file.get()); | 377 base::ScopedFILE empty_file(base::OpenFile(default_log_path_, "w")); |
| 255 created_file.reset(); | 378 ASSERT_TRUE(empty_file.get()); |
| 256 | 379 empty_file.reset(); |
| 257 EXPECT_TRUE(net_log_file_writer_->EnsureInit()); | 380 |
| 258 | 381 // GetState() will cause |net_log_file_writer_| to initialize. |
| 259 EXPECT_EQ("NOT_LOGGING", GetStateString()); | 382 VerifyState(FileWriterGetState(), kStateNotLoggingString, true, false, ""); |
| 260 EXPECT_EQ(NetLogFileWriter::STATE_NOT_LOGGING, net_log_file_writer_->state()); | 383 |
| 261 EXPECT_EQ("UNKNOWN", GetLogTypeString()); | 384 EXPECT_EQ(FileWriterGetFilePathToCompletedLog(), default_log_path_); |
| 262 EXPECT_EQ(NetLogFileWriter::LOG_TYPE_UNKNOWN, | 385 } |
| 263 net_log_file_writer_->log_type()); | 386 |
| 264 EXPECT_EQ(net_export_log_, net_log_file_writer_->log_path_); | 387 TEST_F(NetLogFileWriterTest, StartAndStopWithAllCaptureModes) { |
| 265 EXPECT_TRUE(base::PathExists(net_export_log_)); | 388 const net::NetLogCaptureMode captureModes[3] = { |
|
eroman
2017/01/24 20:01:36
either kCaptureModes, or capture_modes
wangyix1
2017/01/25 22:48:27
Done.
| |
| 266 | 389 net::NetLogCaptureMode::Default(), |
| 267 base::FilePath net_export_file_path; | 390 net::NetLogCaptureMode::IncludeCookiesAndCredentials(), |
| 268 EXPECT_TRUE(net_log_file_writer_->GetFilePath(&net_export_file_path)); | 391 net::NetLogCaptureMode::IncludeSocketBytes()}; |
| 269 EXPECT_TRUE(base::PathExists(net_export_file_path)); | 392 |
| 270 EXPECT_EQ(net_export_log_, net_export_file_path); | 393 const std::string captureModeStrings[3] = { |
|
eroman
2017/01/24 20:01:36
same here
wangyix1
2017/01/25 22:48:27
Done.
| |
| 271 } | 394 kCaptureModeDefaultString, kCaptureModeIncludeCookiesAndCredentialsString, |
| 272 | 395 kCaptureModeIncludeSocketBytesString}; |
| 273 TEST_F(NetLogFileWriterTest, ProcessCommandDoStartAndStop) { | 396 |
| 274 net_log_file_writer_->ProcessCommand(NetLogFileWriter::DO_START); | 397 // For each capture mode, start and stop |net_log_file_writer_| in that mode. |
| 275 VerifyFileAndStateAfterDoStart(); | 398 for (int i = 0; i < 3; ++i) { |
|
eroman
2017/01/24 20:01:36
[optional] Could use a parameterized test via TEST
wangyix1
2017/01/25 22:48:27
I'm not sure there's a clean way to do this. Each
eroman
2017/01/27 02:23:23
Had we gone this route, I would envision having th
| |
| 276 | 399 StartThenVerifyFileAndState(base::FilePath(), captureModes[i], |
| 277 // Calling a second time should be a no-op. | 400 captureModeStrings[i]); |
| 278 net_log_file_writer_->ProcessCommand(NetLogFileWriter::DO_START); | 401 |
| 279 VerifyFileAndStateAfterDoStart(); | 402 // Starting a second time should be a no-op. |
| 280 | 403 StartThenVerifyFileAndState(base::FilePath(), captureModes[i], |
| 281 // starting with other log levels should also be no-ops. | 404 captureModeStrings[i]); |
| 282 net_log_file_writer_->ProcessCommand( | 405 |
| 283 NetLogFileWriter::DO_START_STRIP_PRIVATE_DATA); | 406 // Starting with other capture modes should also be no-ops. This should also |
| 284 VerifyFileAndStateAfterDoStart(); | 407 // not affect the capture mode reported by |net_log_file_writer_|. |
| 285 net_log_file_writer_->ProcessCommand(NetLogFileWriter::DO_START_LOG_BYTES); | 408 StartThenVerifyFileAndState(base::FilePath(), captureModes[(i + 1) % 3], |
| 286 VerifyFileAndStateAfterDoStart(); | 409 captureModeStrings[i]); |
| 287 | 410 |
| 288 net_log_file_writer_->ProcessCommand(NetLogFileWriter::DO_STOP); | 411 StartThenVerifyFileAndState(base::FilePath(), captureModes[(i + 2) % 3], |
| 289 VerifyFileAndStateAfterDoStop(); | 412 captureModeStrings[i]); |
| 290 | 413 |
| 291 // Calling DO_STOP second time should be a no-op. | 414 StopThenVerifyFileAndState(base::FilePath(), nullptr, nullptr, |
| 292 net_log_file_writer_->ProcessCommand(NetLogFileWriter::DO_STOP); | 415 captureModeStrings[i]); |
| 293 VerifyFileAndStateAfterDoStop(); | 416 |
| 294 } | 417 // Stopping a second time should be a no-op. |
| 295 | 418 StopThenVerifyFileAndState(base::FilePath(), nullptr, nullptr, |
| 296 TEST_F(NetLogFileWriterTest, | 419 captureModeStrings[i]); |
| 297 ProcessCommandDoStartAndStopWithPrivateDataStripping) { | 420 } |
| 298 net_log_file_writer_->ProcessCommand( | 421 } |
| 299 NetLogFileWriter::DO_START_STRIP_PRIVATE_DATA); | 422 |
| 300 VerifyFileAndStateAfterDoStartStripPrivateData(); | 423 // Verify the file sizes after two consecutive starts/stops are the same (even |
| 301 | 424 // if some junk data is added in between). |
| 302 // Calling a second time should be a no-op. | 425 TEST_F(NetLogFileWriterTest, StartClearsFile) { |
| 303 net_log_file_writer_->ProcessCommand( | 426 StartThenVerifyFileAndState(base::FilePath(), |
| 304 NetLogFileWriter::DO_START_STRIP_PRIVATE_DATA); | 427 net::NetLogCaptureMode::Default(), |
| 305 VerifyFileAndStateAfterDoStartStripPrivateData(); | 428 kCaptureModeDefaultString); |
| 306 | |
| 307 net_log_file_writer_->ProcessCommand(NetLogFileWriter::DO_STOP); | |
| 308 VerifyFileAndStateAfterDoStopWithStripPrivateData(); | |
| 309 | |
| 310 // Calling DO_STOP second time should be a no-op. | |
| 311 net_log_file_writer_->ProcessCommand(NetLogFileWriter::DO_STOP); | |
| 312 VerifyFileAndStateAfterDoStopWithStripPrivateData(); | |
| 313 } | |
| 314 | |
| 315 TEST_F(NetLogFileWriterTest, ProcessCommandDoStartAndStopWithByteLogging) { | |
| 316 net_log_file_writer_->ProcessCommand(NetLogFileWriter::DO_START_LOG_BYTES); | |
| 317 VerifyFileAndStateAfterDoStartLogBytes(); | |
| 318 | |
| 319 // Calling a second time should be a no-op. | |
| 320 net_log_file_writer_->ProcessCommand(NetLogFileWriter::DO_START_LOG_BYTES); | |
| 321 VerifyFileAndStateAfterDoStartLogBytes(); | |
| 322 | |
| 323 net_log_file_writer_->ProcessCommand(NetLogFileWriter::DO_STOP); | |
| 324 VerifyFileAndStateAfterDoStopWithLogBytes(); | |
| 325 | |
| 326 // Calling DO_STOP second time should be a no-op. | |
| 327 net_log_file_writer_->ProcessCommand(NetLogFileWriter::DO_STOP); | |
| 328 VerifyFileAndStateAfterDoStopWithLogBytes(); | |
| 329 } | |
| 330 | |
| 331 TEST_F(NetLogFileWriterTest, DoStartClearsFile) { | |
| 332 // Verify file sizes after two consecutive starts/stops are the same (even if | |
| 333 // we add some junk data in between). | |
| 334 net_log_file_writer_->ProcessCommand(NetLogFileWriter::DO_START); | |
| 335 VerifyFileAndStateAfterDoStart(); | |
| 336 | 429 |
| 337 int64_t start_file_size; | 430 int64_t start_file_size; |
| 338 EXPECT_TRUE(base::GetFileSize(net_export_log_, &start_file_size)); | 431 EXPECT_TRUE(base::GetFileSize(default_log_path_, &start_file_size)); |
| 339 | 432 |
| 340 net_log_file_writer_->ProcessCommand(NetLogFileWriter::DO_STOP); | 433 StopThenVerifyFileAndState(base::FilePath(), nullptr, nullptr, |
| 341 VerifyFileAndStateAfterDoStop(); | 434 kCaptureModeDefaultString); |
| 342 | 435 |
| 343 int64_t stop_file_size; | 436 int64_t stop_file_size; |
| 344 EXPECT_TRUE(base::GetFileSize(net_export_log_, &stop_file_size)); | 437 EXPECT_TRUE(base::GetFileSize(default_log_path_, &stop_file_size)); |
| 345 EXPECT_GE(stop_file_size, start_file_size); | 438 EXPECT_GE(stop_file_size, start_file_size); |
| 346 | 439 |
| 347 // Add some junk at the end of the file. | 440 // Add some junk at the end of the file. |
| 348 std::string junk_data("Hello"); | 441 std::string junk_data("Hello"); |
| 442 EXPECT_TRUE(base::AppendToFile(default_log_path_, junk_data.c_str(), | |
| 443 junk_data.size())); | |
| 444 | |
| 445 int64_t junk_file_size; | |
| 446 EXPECT_TRUE(base::GetFileSize(default_log_path_, &junk_file_size)); | |
| 447 EXPECT_GT(junk_file_size, stop_file_size); | |
| 448 | |
| 449 // Start and stop again and make sure the file is back to the size it was | |
| 450 // before adding the junk data. | |
| 451 StartThenVerifyFileAndState(base::FilePath(), | |
| 452 net::NetLogCaptureMode::Default(), | |
| 453 kCaptureModeDefaultString); | |
| 454 | |
| 455 int64_t new_start_file_size; | |
| 456 EXPECT_TRUE(base::GetFileSize(default_log_path_, &new_start_file_size)); | |
| 457 EXPECT_EQ(new_start_file_size, start_file_size); | |
| 458 | |
| 459 StopThenVerifyFileAndState(base::FilePath(), nullptr, nullptr, | |
| 460 kCaptureModeDefaultString); | |
| 461 | |
| 462 int64_t new_stop_file_size; | |
| 463 EXPECT_TRUE(base::GetFileSize(default_log_path_, &new_stop_file_size)); | |
| 464 | |
| 465 EXPECT_EQ(new_stop_file_size, stop_file_size); | |
| 466 } | |
| 467 | |
| 468 // Adds an event to the log file, then checks that the file is larger than | |
| 469 // the file created without that event. | |
| 470 TEST_F(NetLogFileWriterTest, AddEvent) { | |
| 471 StartThenVerifyFileAndState(base::FilePath(), | |
| 472 net::NetLogCaptureMode::Default(), | |
| 473 kCaptureModeDefaultString); | |
| 474 | |
| 475 StopThenVerifyFileAndState(base::FilePath(), nullptr, nullptr, | |
| 476 kCaptureModeDefaultString); | |
| 477 | |
| 478 // Get file size without the event. | |
| 479 int64_t stop_file_size; | |
| 480 EXPECT_TRUE(base::GetFileSize(default_log_path_, &stop_file_size)); | |
| 481 | |
| 482 StartThenVerifyFileAndState(base::FilePath(), | |
| 483 net::NetLogCaptureMode::Default(), | |
| 484 kCaptureModeDefaultString); | |
| 485 | |
| 486 net_log_.AddGlobalEntry(net::NetLogEventType::CANCELLED); | |
| 487 | |
| 488 StopThenVerifyFileAndState(base::FilePath(), nullptr, nullptr, | |
| 489 kCaptureModeDefaultString); | |
| 490 | |
| 491 int64_t new_stop_file_size; | |
| 492 EXPECT_TRUE(base::GetFileSize(default_log_path_, &new_stop_file_size)); | |
| 493 | |
| 494 EXPECT_GE(new_stop_file_size, stop_file_size); | |
| 495 } | |
| 496 | |
| 497 // Using a custom path to make sure logging can still occur when | |
| 498 // the path has changed. | |
| 499 TEST_F(NetLogFileWriterTest, AddEventCustomPath) { | |
| 500 base::FilePath::CharType kCustomRelativePath[] = | |
| 501 FILE_PATH_LITERAL("custom/custom/chrome-net-export-log.json"); | |
| 502 base::FilePath custom_log_path = | |
| 503 log_temp_dir_.GetPath().Append(kCustomRelativePath); | |
| 349 EXPECT_TRUE( | 504 EXPECT_TRUE( |
| 350 base::AppendToFile(net_export_log_, junk_data.c_str(), junk_data.size())); | 505 base::CreateDirectoryAndGetError(custom_log_path.DirName(), nullptr)); |
| 351 | 506 |
| 352 int64_t junk_file_size; | 507 StartThenVerifyFileAndState(custom_log_path, |
| 353 EXPECT_TRUE(base::GetFileSize(net_export_log_, &junk_file_size)); | 508 net::NetLogCaptureMode::Default(), |
| 354 EXPECT_GT(junk_file_size, stop_file_size); | 509 kCaptureModeDefaultString); |
| 355 | 510 |
| 356 // Execute DO_START/DO_STOP commands and make sure the file is back to the | 511 StopThenVerifyFileAndState(custom_log_path, nullptr, nullptr, |
| 357 // size before addition of junk data. | 512 kCaptureModeDefaultString); |
| 358 net_log_file_writer_->ProcessCommand(NetLogFileWriter::DO_START); | 513 |
| 359 VerifyFileAndStateAfterDoStart(); | 514 // Get file size without the event. |
| 360 | 515 int64_t stop_file_size; |
| 361 int64_t new_start_file_size; | 516 EXPECT_TRUE(base::GetFileSize(custom_log_path, &stop_file_size)); |
| 362 EXPECT_TRUE(base::GetFileSize(net_export_log_, &new_start_file_size)); | 517 |
| 363 EXPECT_EQ(new_start_file_size, start_file_size); | 518 StartThenVerifyFileAndState(custom_log_path, |
| 364 | 519 net::NetLogCaptureMode::Default(), |
| 365 net_log_file_writer_->ProcessCommand(NetLogFileWriter::DO_STOP); | 520 kCaptureModeDefaultString); |
| 366 VerifyFileAndStateAfterDoStop(); | 521 |
| 522 net_log_.AddGlobalEntry(net::NetLogEventType::CANCELLED); | |
| 523 | |
| 524 StopThenVerifyFileAndState(custom_log_path, nullptr, nullptr, | |
| 525 kCaptureModeDefaultString); | |
| 367 | 526 |
| 368 int64_t new_stop_file_size; | 527 int64_t new_stop_file_size; |
| 369 EXPECT_TRUE(base::GetFileSize(net_export_log_, &new_stop_file_size)); | 528 EXPECT_TRUE(base::GetFileSize(custom_log_path, &new_stop_file_size)); |
| 370 EXPECT_EQ(new_stop_file_size, stop_file_size); | |
| 371 } | |
| 372 | |
| 373 TEST_F(NetLogFileWriterTest, CheckAddEvent) { | |
| 374 // Add an event to |net_log_| and then test to make sure that, after we stop | |
| 375 // logging, the file is larger than the file created without that event. | |
| 376 net_log_file_writer_->ProcessCommand(NetLogFileWriter::DO_START); | |
| 377 VerifyFileAndStateAfterDoStart(); | |
| 378 | |
| 379 // Get file size without the event. | |
| 380 net_log_file_writer_->ProcessCommand(NetLogFileWriter::DO_STOP); | |
| 381 VerifyFileAndStateAfterDoStop(); | |
| 382 | |
| 383 int64_t stop_file_size; | |
| 384 EXPECT_TRUE(base::GetFileSize(net_export_log_, &stop_file_size)); | |
| 385 | |
| 386 // Perform DO_START and add an Event and then DO_STOP and then compare | |
| 387 // file sizes. | |
| 388 net_log_file_writer_->ProcessCommand(NetLogFileWriter::DO_START); | |
| 389 VerifyFileAndStateAfterDoStart(); | |
| 390 | |
| 391 // Log an event. | |
| 392 net_log_->AddGlobalEntry(net::NetLogEventType::CANCELLED); | |
| 393 | |
| 394 net_log_file_writer_->ProcessCommand(NetLogFileWriter::DO_STOP); | |
| 395 VerifyFileAndStateAfterDoStop(); | |
| 396 | |
| 397 int64_t new_stop_file_size; | |
| 398 EXPECT_TRUE(base::GetFileSize(net_export_log_, &new_stop_file_size)); | |
| 399 EXPECT_GE(new_stop_file_size, stop_file_size); | 529 EXPECT_GE(new_stop_file_size, stop_file_size); |
| 400 } | 530 } |
| 401 | 531 |
| 402 TEST_F(NetLogFileWriterTest, CheckAddEventWithCustomPath) { | 532 TEST_F(NetLogFileWriterTest, StartAndStopWithPolledDataAndContextGetter) { |
| 403 // Using a custom path to make sure logging can still occur when | 533 // Create dummy polled data |
| 404 // the path has changed. | 534 const char kDummyPolledDataPath[] = "dummy_path"; |
| 405 base::FilePath path; | 535 const char kDummyPolledDataString[] = "dummy_info"; |
| 406 net_log_file_writer_->GetNetExportLogBaseDirectory(&path); | 536 std::unique_ptr<base::DictionaryValue> dummy_polled_data = |
| 407 | 537 base::MakeUnique<base::DictionaryValue>(); |
| 408 base::FilePath::CharType kCustomPath[] = | 538 dummy_polled_data->SetString(kDummyPolledDataPath, kDummyPolledDataString); |
| 409 FILE_PATH_LITERAL("custom/custom/chrome-net-export-log.json"); | 539 |
| 410 base::FilePath custom_path = path.Append(kCustomPath); | 540 // Create test context getter |
| 411 | 541 std::unique_ptr<net::TestURLRequestContext> context( |
| 412 EXPECT_TRUE(base::CreateDirectoryAndGetError(custom_path.DirName(), nullptr)); | 542 new net::TestURLRequestContext(true)); |
| 413 | 543 context->set_net_log(&net_log_); |
| 414 net_log_file_writer_->SetUpNetExportLogPath(custom_path); | 544 const int kDummyParam = 75; |
| 415 net_export_log_ = net_log_file_writer_->log_path_; | 545 std::unique_ptr<net::HttpNetworkSession::Params> params( |
| 416 EXPECT_EQ(custom_path, net_export_log_); | 546 new net::HttpNetworkSession::Params); |
| 417 | 547 params->quic_idle_connection_timeout_seconds = kDummyParam; |
| 418 // Add an event to |net_log_| and then test to make sure that, after we stop | 548 context->set_http_network_session_params(std::move(params)); |
| 419 // logging, the file is larger than the file created without that event. | 549 context->Init(); |
| 420 net_log_file_writer_->ProcessCommand(NetLogFileWriter::DO_START); | 550 scoped_refptr<net::TestURLRequestContextGetter> context_getter( |
| 421 VerifyFileAndStateAfterDoStart(); | 551 new net::TestURLRequestContextGetter(net_task_runner_, |
| 422 | 552 std::move(context))); |
| 423 // Get file size without the event. | 553 |
| 424 net_log_file_writer_->ProcessCommand(NetLogFileWriter::DO_STOP); | 554 StartThenVerifyFileAndState(base::FilePath(), |
| 425 VerifyFileAndStateAfterDoStop(); | 555 net::NetLogCaptureMode::Default(), |
| 426 | 556 kCaptureModeDefaultString); |
| 427 int64_t stop_file_size; | 557 |
| 428 EXPECT_TRUE(base::GetFileSize(net_export_log_, &stop_file_size)); | 558 StopThenVerifyFileAndState(base::FilePath(), std::move(dummy_polled_data), |
| 429 | 559 context_getter, kCaptureModeDefaultString); |
| 430 // Perform DO_START and add an Event and then DO_STOP and then compare | 560 |
| 431 // file sizes. | 561 // Read polledData from log file. |
| 432 net_log_file_writer_->ProcessCommand(NetLogFileWriter::DO_START); | 562 std::string log_file_string; |
| 433 VerifyFileAndStateAfterDoStart(); | 563 ASSERT_TRUE(ReadFileToString(default_log_path_, &log_file_string)); |
| 434 | 564 std::unique_ptr<base::DictionaryValue> dict = |
| 435 // Log an event. | 565 base::DictionaryValue::From(base::JSONReader::Read(log_file_string)); |
| 436 net_log_->AddGlobalEntry(net::NetLogEventType::CANCELLED); | 566 ASSERT_TRUE(dict); |
| 437 | 567 base::DictionaryValue* polled_data; |
| 438 net_log_file_writer_->ProcessCommand(NetLogFileWriter::DO_STOP); | 568 ASSERT_TRUE(dict->GetDictionary("polledData", &polled_data)); |
| 439 VerifyFileAndStateAfterDoStop(); | 569 |
| 440 | 570 // Check that it contains the field from the polled data that was passed in. |
| 441 int64_t new_stop_file_size; | 571 std::string dummy_string; |
| 442 EXPECT_TRUE(base::GetFileSize(net_export_log_, &new_stop_file_size)); | 572 ASSERT_TRUE(polled_data->GetString(kDummyPolledDataPath, &dummy_string)); |
| 443 EXPECT_GE(new_stop_file_size, stop_file_size); | 573 EXPECT_EQ(dummy_string, kDummyPolledDataString); |
| 574 | |
| 575 // Check that it also contains the field from the URLRequestContext that was | |
| 576 // passed in. | |
| 577 base::DictionaryValue* quic_info; | |
| 578 ASSERT_TRUE(polled_data->GetDictionary("quicInfo", &quic_info)); | |
| 579 base::Value* timeout_value = nullptr; | |
| 580 int timeout; | |
| 581 ASSERT_TRUE( | |
| 582 quic_info->Get("idle_connection_timeout_seconds", &timeout_value)); | |
| 583 ASSERT_TRUE(timeout_value->GetAsInteger(&timeout)); | |
| 584 EXPECT_EQ(timeout, kDummyParam); | |
| 585 } | |
| 586 | |
| 587 TEST_F(NetLogFileWriterTest, ReceiveCommandsWhileInitializing) { | |
| 588 // Trigger initialization, but don't run any of the posted initialization | |
| 589 // tasks. | |
| 590 TestStateCallback init_callback; | |
| 591 net_log_file_writer_.GetState(init_callback.Callback()); | |
| 592 | |
| 593 // Use GetState() to retrive the state without running any of the tasks posted | |
|
eroman
2017/01/24 20:01:36
retrieve throughtout
wangyix1
2017/01/25 22:48:27
Comment no longer exists.
| |
| 594 // to |file_task_runner_| or |net_task_runner_|. | |
| 595 VerifyState(FileWriterGetStateWithoutRunningTaskRunners(), | |
| 596 kStateInitializingString, false, false, ""); | |
| 597 | |
| 598 // Tell |net_log_file_writer_| to start logging while it's initializing. This | |
| 599 // should be a no-op. | |
| 600 TestStateCallback start_during_init_callback; | |
| 601 net_log_file_writer_.StartNetLog(base::FilePath(), | |
| 602 net::NetLogCaptureMode::Default(), | |
| 603 start_during_init_callback.Callback()); | |
| 604 | |
| 605 // Use GetState() to retrive the state without running any of the tasks posted | |
| 606 // to |file_task_runner_| or |net_task_runner_|. | |
| 607 VerifyState(FileWriterGetStateWithoutRunningTaskRunners(), | |
| 608 kStateInitializingString, false, false, ""); | |
| 609 | |
| 610 // Now, run the pending tasks on |file_task_runner_| and |net_task_runner_|. | |
| 611 // This allows |net_log_file_writer_| to fully initialize. | |
| 612 | |
| 613 // The state returned by the ignored StartNetLog() call should be the state | |
| 614 // after the initialization triggered by the first GetState() finishes. | |
| 615 VerifyState(start_during_init_callback.WaitUntilIdle(file_task_runner_, | |
| 616 net_task_runner_), | |
| 617 kStateNotLoggingString, false, false, ""); | |
| 618 | |
| 619 // Make sure the state returned by the first GetState() makes sense. | |
| 620 VerifyState(init_callback.WaitUntilIdle(file_task_runner_, net_task_runner_), | |
| 621 kStateNotLoggingString, false, false, ""); | |
| 622 | |
| 623 // Get the state with anoter GetState() call after everything's done to | |
| 624 // confirm |net_log_file_writer_| is not logging. | |
| 625 VerifyState(FileWriterGetState(), kStateNotLoggingString, false, false, ""); | |
| 626 } | |
| 627 | |
| 628 TEST_F(NetLogFileWriterTest, ReceiveCommandsWhileStopLogging) { | |
| 629 // Tell |net_log_file_writer_| to start logging and let it complete the | |
| 630 // command. | |
| 631 StartThenVerifyFileAndState(base::FilePath(), | |
| 632 net::NetLogCaptureMode::IncludeSocketBytes(), | |
| 633 kCaptureModeIncludeSocketBytesString); | |
| 634 | |
| 635 // Tell |net_log_file_writer_| to stop logging, but don't run any of the | |
| 636 // tasks posted by StopNetLog(). | |
| 637 TestStateCallback stop_callback; | |
| 638 net_log_file_writer_.StopNetLog(nullptr, nullptr, stop_callback.Callback()); | |
| 639 | |
| 640 // Use GetState() to retrive the state without running any of the tasks posted | |
|
eroman
2017/01/24 20:01:36
retrieve
wangyix1
2017/01/25 22:48:27
Comment no longer exists.
| |
| 641 // to |file_task_runner_| or |net_task_runner_|. | |
| 642 VerifyState(FileWriterGetStateWithoutRunningTaskRunners(), | |
| 643 kStateStoppingLogString, true, true, | |
| 644 kCaptureModeIncludeSocketBytesString); | |
| 645 | |
| 646 // Tell |net_log_file_writer_| to start logging while it's stopping logging. | |
| 647 // This should be a no-op. | |
| 648 TestStateCallback start_during_stop_callback; | |
| 649 net_log_file_writer_.StartNetLog(base::FilePath(), | |
| 650 net::NetLogCaptureMode::IncludeSocketBytes(), | |
| 651 start_during_stop_callback.Callback()); | |
| 652 | |
| 653 // Use GetState() to retrive the state without running any of the tasks posted | |
|
eroman
2017/01/24 20:01:36
same typo
wangyix1
2017/01/25 22:48:27
Comment no longer exists.
| |
| 654 // to |file_task_runner_| or |net_task_runner_|. | |
| 655 VerifyState(FileWriterGetStateWithoutRunningTaskRunners(), | |
| 656 kStateStoppingLogString, true, true, | |
| 657 kCaptureModeIncludeSocketBytesString); | |
| 658 | |
| 659 // Now, run the pending tasks on |file_task_runner_| and |net_task_runner_|. | |
| 660 // This allows |net_log_file_writer_| to fully stop logging. | |
| 661 | |
| 662 // The state returned by the callback of the ignored StartNetLog() call should | |
| 663 // still makes sense. | |
| 664 std::string start_during_stop_state_string; | |
| 665 ASSERT_TRUE(start_during_stop_callback | |
| 666 .WaitUntilIdle(file_task_runner_, net_task_runner_) | |
| 667 ->GetString("state", &start_during_stop_state_string)); | |
| 668 if (start_during_stop_state_string != kStateStoppingLogString && | |
| 669 start_during_stop_state_string != kStateNotLoggingString) { | |
| 670 ADD_FAILURE() << "State returned by ignored StartNetLog() should indicate " | |
| 671 "either stopping-log or not-logging"; | |
| 672 } | |
| 673 | |
| 674 // Make sure the state returned by the StopNetLog() makes sense. | |
| 675 VerifyState(stop_callback.WaitUntilIdle(file_task_runner_, net_task_runner_), | |
| 676 kStateNotLoggingString, true, true, | |
| 677 kCaptureModeIncludeSocketBytesString); | |
| 678 | |
| 679 // Get the state with another GetState() call after everything's done to | |
| 680 // confirm |net_log_file_writer_| is not logging. | |
| 681 VerifyState(FileWriterGetState(), kStateNotLoggingString, true, true, | |
| 682 kCaptureModeIncludeSocketBytesString); | |
| 444 } | 683 } |
| 445 | 684 |
| 446 } // namespace net_log | 685 } // namespace net_log |
| OLD | NEW |