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