| 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 "chrome/browser/net/net_log_temp_file.h" | 5 #include "components/net_log/net_log_temp_file.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/command_line.h" |
| 8 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 9 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 10 #include "base/files/scoped_file.h" | 11 #include "base/files/scoped_file.h" |
| 11 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/json/json_reader.h" | 13 #include "base/json/json_reader.h" |
| 13 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 15 #include "base/values.h" | 16 #include "base/values.h" |
| 16 #include "build/build_config.h" | 17 #include "build/build_config.h" |
| 17 #include "chrome/browser/net/chrome_net_log.h" | 18 #include "components/net_log/chrome_net_log.h" |
| 18 #include "content/public/test/test_browser_thread.h" | 19 #include "net/log/net_log_capture_mode.h" |
| 19 #include "net/log/write_to_file_net_log_observer.h" | 20 #include "net/log/write_to_file_net_log_observer.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 22 |
| 22 using content::BrowserThread; | 23 namespace { |
| 24 |
| 25 const char kChannelString[] = "SomeChannel"; |
| 26 |
| 27 } // namespace |
| 28 |
| 29 namespace net_log { |
| 23 | 30 |
| 24 class TestNetLogTempFile : public NetLogTempFile { | 31 class TestNetLogTempFile : public NetLogTempFile { |
| 25 public: | 32 public: |
| 26 explicit TestNetLogTempFile(ChromeNetLog* chrome_net_log) | 33 explicit TestNetLogTempFile(ChromeNetLog* chrome_net_log) |
| 27 : NetLogTempFile(chrome_net_log), | 34 : NetLogTempFile( |
| 35 chrome_net_log, |
| 36 base::CommandLine::ForCurrentProcess()->GetCommandLineString(), |
| 37 kChannelString), |
| 28 lie_about_net_export_log_directory_(false) { | 38 lie_about_net_export_log_directory_(false) { |
| 29 EXPECT_TRUE(net_log_temp_dir_.CreateUniqueTempDir()); | 39 EXPECT_TRUE(net_log_temp_dir_.CreateUniqueTempDir()); |
| 30 } | 40 } |
| 31 | 41 |
| 32 ~TestNetLogTempFile() override { | 42 ~TestNetLogTempFile() override { EXPECT_TRUE(net_log_temp_dir_.Delete()); } |
| 33 EXPECT_TRUE(net_log_temp_dir_.Delete()); | |
| 34 } | |
| 35 | 43 |
| 36 // NetLogTempFile implementation: | 44 // NetLogTempFile implementation: |
| 37 bool GetNetExportLogBaseDirectory(base::FilePath* path) const override { | 45 bool GetNetExportLogBaseDirectory(base::FilePath* path) const override { |
| 38 if (lie_about_net_export_log_directory_) | 46 if (lie_about_net_export_log_directory_) |
| 39 return false; | 47 return false; |
| 40 *path = net_log_temp_dir_.path(); | 48 *path = net_log_temp_dir_.path(); |
| 41 return true; | 49 return true; |
| 42 } | 50 } |
| 43 | 51 |
| 44 void set_lie_about_net_export_log_directory( | 52 void set_lie_about_net_export_log_directory( |
| 45 bool lie_about_net_export_log_directory) { | 53 bool lie_about_net_export_log_directory) { |
| 46 lie_about_net_export_log_directory_ = lie_about_net_export_log_directory; | 54 lie_about_net_export_log_directory_ = lie_about_net_export_log_directory; |
| 47 } | 55 } |
| 48 | 56 |
| 49 private: | 57 private: |
| 50 bool lie_about_net_export_log_directory_; | 58 bool lie_about_net_export_log_directory_; |
| 51 | 59 |
| 52 base::ScopedTempDir net_log_temp_dir_; | 60 base::ScopedTempDir net_log_temp_dir_; |
| 53 }; | 61 }; |
| 54 | 62 |
| 55 class NetLogTempFileTest : public ::testing::Test { | 63 class NetLogTempFileTest : public ::testing::Test { |
| 56 public: | 64 public: |
| 57 NetLogTempFileTest() | 65 NetLogTempFileTest() |
| 58 : net_log_(new ChromeNetLog), | 66 : net_log_(new ChromeNetLog( |
| 59 net_log_temp_file_(new TestNetLogTempFile(net_log_.get())), | 67 base::FilePath(), |
| 60 file_user_blocking_thread_(BrowserThread::FILE_USER_BLOCKING, | 68 net::NetLogCaptureMode::Default(), |
| 61 &message_loop_) { | 69 base::CommandLine::ForCurrentProcess()->GetCommandLineString(), |
| 70 kChannelString)), |
| 71 net_log_temp_file_(new TestNetLogTempFile(net_log_.get())) { |
| 62 EXPECT_TRUE(net_log_temp_file_->SetUpNetExportLogPath()); | 72 EXPECT_TRUE(net_log_temp_file_->SetUpNetExportLogPath()); |
| 63 net_export_log_ = net_log_temp_file_->log_path_; | 73 net_export_log_ = net_log_temp_file_->log_path_; |
| 64 } | 74 } |
| 65 | 75 |
| 66 std::string GetStateString() const { | 76 std::string GetStateString() const { |
| 67 scoped_ptr<base::DictionaryValue> dict(net_log_temp_file_->GetState()); | 77 scoped_ptr<base::DictionaryValue> dict(net_log_temp_file_->GetState()); |
| 68 std::string state; | 78 std::string state; |
| 69 EXPECT_TRUE(dict->GetString("state", &state)); | 79 EXPECT_TRUE(dict->GetString("state", &state)); |
| 70 return state; | 80 return state; |
| 71 } | 81 } |
| (...skipping 25 matching lines...) Expand all Loading... |
| 97 std::string log; | 107 std::string log; |
| 98 ASSERT_TRUE(ReadFileToString(net_export_log_, &log)); | 108 ASSERT_TRUE(ReadFileToString(net_export_log_, &log)); |
| 99 base::JSONReader reader; | 109 base::JSONReader reader; |
| 100 scoped_ptr<base::Value> json = base::JSONReader::Read(log); | 110 scoped_ptr<base::Value> json = base::JSONReader::Read(log); |
| 101 EXPECT_TRUE(json); | 111 EXPECT_TRUE(json); |
| 102 } | 112 } |
| 103 | 113 |
| 104 // Verify state and GetFilePath return correct values if EnsureInit() fails. | 114 // Verify state and GetFilePath return correct values if EnsureInit() fails. |
| 105 void VerifyFilePathAndStateAfterEnsureInitFailure() { | 115 void VerifyFilePathAndStateAfterEnsureInitFailure() { |
| 106 EXPECT_EQ("UNINITIALIZED", GetStateString()); | 116 EXPECT_EQ("UNINITIALIZED", GetStateString()); |
| 107 EXPECT_EQ(NetLogTempFile::STATE_UNINITIALIZED, | 117 EXPECT_EQ(NetLogTempFile::STATE_UNINITIALIZED, net_log_temp_file_->state()); |
| 108 net_log_temp_file_->state()); | |
| 109 | 118 |
| 110 base::FilePath net_export_file_path; | 119 base::FilePath net_export_file_path; |
| 111 EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path)); | 120 EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path)); |
| 112 } | 121 } |
| 113 | 122 |
| 114 // When we lie in NetExportLogExists, make sure state and GetFilePath return | 123 // When we lie in NetExportLogExists, make sure state and GetFilePath return |
| 115 // correct values. | 124 // correct values. |
| 116 void VerifyFilePathAndStateAfterEnsureInit() { | 125 void VerifyFilePathAndStateAfterEnsureInit() { |
| 117 EXPECT_EQ("NOT_LOGGING", GetStateString()); | 126 EXPECT_EQ("NOT_LOGGING", GetStateString()); |
| 118 EXPECT_EQ(NetLogTempFile::STATE_NOT_LOGGING, net_log_temp_file_->state()); | 127 EXPECT_EQ(NetLogTempFile::STATE_NOT_LOGGING, net_log_temp_file_->state()); |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 EXPECT_EQ(expected_log_type_string, GetLogTypeString()); | 206 EXPECT_EQ(expected_log_type_string, GetLogTypeString()); |
| 198 | 207 |
| 199 base::FilePath net_export_file_path; | 208 base::FilePath net_export_file_path; |
| 200 EXPECT_TRUE(net_log_temp_file_->GetFilePath(&net_export_file_path)); | 209 EXPECT_TRUE(net_log_temp_file_->GetFilePath(&net_export_file_path)); |
| 201 EXPECT_EQ(net_export_log_, net_export_file_path); | 210 EXPECT_EQ(net_export_log_, net_export_file_path); |
| 202 | 211 |
| 203 VerifyNetExportLogComplete(); | 212 VerifyNetExportLogComplete(); |
| 204 } | 213 } |
| 205 | 214 |
| 206 base::MessageLoop message_loop_; | 215 base::MessageLoop message_loop_; |
| 207 content::TestBrowserThread file_user_blocking_thread_; | |
| 208 }; | 216 }; |
| 209 | 217 |
| 210 TEST_F(NetLogTempFileTest, EnsureInitFailure) { | 218 TEST_F(NetLogTempFileTest, EnsureInitFailure) { |
| 211 net_log_temp_file_->set_lie_about_net_export_log_directory(true); | 219 net_log_temp_file_->set_lie_about_net_export_log_directory(true); |
| 212 | 220 |
| 213 EXPECT_FALSE(net_log_temp_file_->EnsureInit()); | 221 EXPECT_FALSE(net_log_temp_file_->EnsureInit()); |
| 214 VerifyFilePathAndStateAfterEnsureInitFailure(); | 222 VerifyFilePathAndStateAfterEnsureInitFailure(); |
| 215 | 223 |
| 216 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START); | 224 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START); |
| 217 VerifyFilePathAndStateAfterEnsureInitFailure(); | 225 VerifyFilePathAndStateAfterEnsureInitFailure(); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 VerifyFileAndStateAfterDoStart(); | 265 VerifyFileAndStateAfterDoStart(); |
| 258 | 266 |
| 259 // Calling a second time should be a no-op. | 267 // Calling a second time should be a no-op. |
| 260 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START); | 268 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START); |
| 261 VerifyFileAndStateAfterDoStart(); | 269 VerifyFileAndStateAfterDoStart(); |
| 262 | 270 |
| 263 // starting with other log levels should also be no-ops. | 271 // starting with other log levels should also be no-ops. |
| 264 net_log_temp_file_->ProcessCommand( | 272 net_log_temp_file_->ProcessCommand( |
| 265 NetLogTempFile::DO_START_STRIP_PRIVATE_DATA); | 273 NetLogTempFile::DO_START_STRIP_PRIVATE_DATA); |
| 266 VerifyFileAndStateAfterDoStart(); | 274 VerifyFileAndStateAfterDoStart(); |
| 267 net_log_temp_file_->ProcessCommand( | 275 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START_LOG_BYTES); |
| 268 NetLogTempFile::DO_START_LOG_BYTES); | |
| 269 VerifyFileAndStateAfterDoStart(); | 276 VerifyFileAndStateAfterDoStart(); |
| 270 | 277 |
| 271 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); | 278 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); |
| 272 VerifyFileAndStateAfterDoStop(); | 279 VerifyFileAndStateAfterDoStop(); |
| 273 | 280 |
| 274 // Calling DO_STOP second time should be a no-op. | 281 // Calling DO_STOP second time should be a no-op. |
| 275 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); | 282 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); |
| 276 VerifyFileAndStateAfterDoStop(); | 283 VerifyFileAndStateAfterDoStop(); |
| 277 } | 284 } |
| 278 | 285 |
| 279 TEST_F(NetLogTempFileTest, | 286 TEST_F(NetLogTempFileTest, |
| 280 ProcessCommandDoStartAndStopWithPrivateDataStripping) { | 287 ProcessCommandDoStartAndStopWithPrivateDataStripping) { |
| 281 net_log_temp_file_->ProcessCommand( | 288 net_log_temp_file_->ProcessCommand( |
| 282 NetLogTempFile::DO_START_STRIP_PRIVATE_DATA); | 289 NetLogTempFile::DO_START_STRIP_PRIVATE_DATA); |
| 283 VerifyFileAndStateAfterDoStartStripPrivateData(); | 290 VerifyFileAndStateAfterDoStartStripPrivateData(); |
| 284 | 291 |
| 285 // Calling a second time should be a no-op. | 292 // Calling a second time should be a no-op. |
| 286 net_log_temp_file_->ProcessCommand( | 293 net_log_temp_file_->ProcessCommand( |
| 287 NetLogTempFile::DO_START_STRIP_PRIVATE_DATA); | 294 NetLogTempFile::DO_START_STRIP_PRIVATE_DATA); |
| 288 VerifyFileAndStateAfterDoStartStripPrivateData(); | 295 VerifyFileAndStateAfterDoStartStripPrivateData(); |
| 289 | 296 |
| 290 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); | 297 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); |
| 291 VerifyFileAndStateAfterDoStopWithStripPrivateData(); | 298 VerifyFileAndStateAfterDoStopWithStripPrivateData(); |
| 292 | 299 |
| 293 // Calling DO_STOP second time should be a no-op. | 300 // Calling DO_STOP second time should be a no-op. |
| 294 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); | 301 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); |
| 295 VerifyFileAndStateAfterDoStopWithStripPrivateData(); | 302 VerifyFileAndStateAfterDoStopWithStripPrivateData(); |
| 296 } | 303 } |
| 297 | 304 |
| 298 TEST_F(NetLogTempFileTest, | 305 TEST_F(NetLogTempFileTest, ProcessCommandDoStartAndStopWithByteLogging) { |
| 299 ProcessCommandDoStartAndStopWithByteLogging) { | |
| 300 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START_LOG_BYTES); | 306 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START_LOG_BYTES); |
| 301 VerifyFileAndStateAfterDoStartLogBytes(); | 307 VerifyFileAndStateAfterDoStartLogBytes(); |
| 302 | 308 |
| 303 // Calling a second time should be a no-op. | 309 // Calling a second time should be a no-op. |
| 304 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START_LOG_BYTES); | 310 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START_LOG_BYTES); |
| 305 VerifyFileAndStateAfterDoStartLogBytes(); | 311 VerifyFileAndStateAfterDoStartLogBytes(); |
| 306 | 312 |
| 307 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); | 313 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); |
| 308 VerifyFileAndStateAfterDoStopWithLogBytes(); | 314 VerifyFileAndStateAfterDoStopWithLogBytes(); |
| 309 | 315 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 323 | 329 |
| 324 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); | 330 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); |
| 325 VerifyFileAndStateAfterDoStop(); | 331 VerifyFileAndStateAfterDoStop(); |
| 326 | 332 |
| 327 int64 stop_file_size; | 333 int64 stop_file_size; |
| 328 EXPECT_TRUE(base::GetFileSize(net_export_log_, &stop_file_size)); | 334 EXPECT_TRUE(base::GetFileSize(net_export_log_, &stop_file_size)); |
| 329 EXPECT_GE(stop_file_size, start_file_size); | 335 EXPECT_GE(stop_file_size, start_file_size); |
| 330 | 336 |
| 331 // Add some junk at the end of the file. | 337 // Add some junk at the end of the file. |
| 332 std::string junk_data("Hello"); | 338 std::string junk_data("Hello"); |
| 333 EXPECT_TRUE(base::AppendToFile(net_export_log_, junk_data.c_str(), | 339 EXPECT_TRUE( |
| 334 junk_data.size())); | 340 base::AppendToFile(net_export_log_, junk_data.c_str(), junk_data.size())); |
| 335 | 341 |
| 336 int64 junk_file_size; | 342 int64 junk_file_size; |
| 337 EXPECT_TRUE(base::GetFileSize(net_export_log_, &junk_file_size)); | 343 EXPECT_TRUE(base::GetFileSize(net_export_log_, &junk_file_size)); |
| 338 EXPECT_GT(junk_file_size, stop_file_size); | 344 EXPECT_GT(junk_file_size, stop_file_size); |
| 339 | 345 |
| 340 // Execute DO_START/DO_STOP commands and make sure the file is back to the | 346 // Execute DO_START/DO_STOP commands and make sure the file is back to the |
| 341 // size before addition of junk data. | 347 // size before addition of junk data. |
| 342 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START); | 348 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START); |
| 343 VerifyFileAndStateAfterDoStart(); | 349 VerifyFileAndStateAfterDoStart(); |
| 344 | 350 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 375 // Log an event. | 381 // Log an event. |
| 376 net_log_->AddGlobalEntry(net::NetLog::TYPE_CANCELLED); | 382 net_log_->AddGlobalEntry(net::NetLog::TYPE_CANCELLED); |
| 377 | 383 |
| 378 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); | 384 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); |
| 379 VerifyFileAndStateAfterDoStop(); | 385 VerifyFileAndStateAfterDoStop(); |
| 380 | 386 |
| 381 int64 new_stop_file_size; | 387 int64 new_stop_file_size; |
| 382 EXPECT_TRUE(base::GetFileSize(net_export_log_, &new_stop_file_size)); | 388 EXPECT_TRUE(base::GetFileSize(net_export_log_, &new_stop_file_size)); |
| 383 EXPECT_GE(new_stop_file_size, stop_file_size); | 389 EXPECT_GE(new_stop_file_size, stop_file_size); |
| 384 } | 390 } |
| 391 |
| 392 } // namespace net_log |
| OLD | NEW |