| 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_temp_file.h" | 5 #include "components/net_log/net_log_temp_file.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <memory> |
| 10 |
| 9 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 10 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 11 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 12 #include "base/files/scoped_file.h" | 14 #include "base/files/scoped_file.h" |
| 13 #include "base/files/scoped_temp_dir.h" | 15 #include "base/files/scoped_temp_dir.h" |
| 14 #include "base/json/json_reader.h" | 16 #include "base/json/json_reader.h" |
| 15 #include "base/memory/scoped_ptr.h" | |
| 16 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
| 17 #include "base/values.h" | 18 #include "base/values.h" |
| 18 #include "build/build_config.h" | 19 #include "build/build_config.h" |
| 19 #include "components/net_log/chrome_net_log.h" | 20 #include "components/net_log/chrome_net_log.h" |
| 20 #include "net/log/net_log_capture_mode.h" | 21 #include "net/log/net_log_capture_mode.h" |
| 21 #include "net/log/write_to_file_net_log_observer.h" | 22 #include "net/log/write_to_file_net_log_observer.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 base::FilePath(), | 69 base::FilePath(), |
| 69 net::NetLogCaptureMode::Default(), | 70 net::NetLogCaptureMode::Default(), |
| 70 base::CommandLine::ForCurrentProcess()->GetCommandLineString(), | 71 base::CommandLine::ForCurrentProcess()->GetCommandLineString(), |
| 71 kChannelString)), | 72 kChannelString)), |
| 72 net_log_temp_file_(new TestNetLogTempFile(net_log_.get())) { | 73 net_log_temp_file_(new TestNetLogTempFile(net_log_.get())) { |
| 73 EXPECT_TRUE(net_log_temp_file_->SetUpNetExportLogPath()); | 74 EXPECT_TRUE(net_log_temp_file_->SetUpNetExportLogPath()); |
| 74 net_export_log_ = net_log_temp_file_->log_path_; | 75 net_export_log_ = net_log_temp_file_->log_path_; |
| 75 } | 76 } |
| 76 | 77 |
| 77 std::string GetStateString() const { | 78 std::string GetStateString() const { |
| 78 scoped_ptr<base::DictionaryValue> dict(net_log_temp_file_->GetState()); | 79 std::unique_ptr<base::DictionaryValue> dict(net_log_temp_file_->GetState()); |
| 79 std::string state; | 80 std::string state; |
| 80 EXPECT_TRUE(dict->GetString("state", &state)); | 81 EXPECT_TRUE(dict->GetString("state", &state)); |
| 81 return state; | 82 return state; |
| 82 } | 83 } |
| 83 | 84 |
| 84 std::string GetLogTypeString() const { | 85 std::string GetLogTypeString() const { |
| 85 scoped_ptr<base::DictionaryValue> dict(net_log_temp_file_->GetState()); | 86 std::unique_ptr<base::DictionaryValue> dict(net_log_temp_file_->GetState()); |
| 86 std::string log_type; | 87 std::string log_type; |
| 87 EXPECT_TRUE(dict->GetString("logType", &log_type)); | 88 EXPECT_TRUE(dict->GetString("logType", &log_type)); |
| 88 return log_type; | 89 return log_type; |
| 89 } | 90 } |
| 90 | 91 |
| 91 // Make sure the export file has been created and is non-empty, as net | 92 // Make sure the export file has been created and is non-empty, as net |
| 92 // constants will always be written to it on creation. | 93 // constants will always be written to it on creation. |
| 93 void VerifyNetExportLogExists() const { | 94 void VerifyNetExportLogExists() const { |
| 94 EXPECT_EQ(net_export_log_, net_log_temp_file_->log_path_); | 95 EXPECT_EQ(net_export_log_, net_log_temp_file_->log_path_); |
| 95 ASSERT_TRUE(base::PathExists(net_export_log_)); | 96 ASSERT_TRUE(base::PathExists(net_export_log_)); |
| 96 | 97 |
| 97 int64_t file_size; | 98 int64_t file_size; |
| 98 // base::GetFileSize returns proper file size on open handles. | 99 // base::GetFileSize returns proper file size on open handles. |
| 99 ASSERT_TRUE(base::GetFileSize(net_export_log_, &file_size)); | 100 ASSERT_TRUE(base::GetFileSize(net_export_log_, &file_size)); |
| 100 EXPECT_GT(file_size, 0); | 101 EXPECT_GT(file_size, 0); |
| 101 } | 102 } |
| 102 | 103 |
| 103 // Make sure the export file has been created and a valid JSON file. This | 104 // Make sure the export file has been created and a valid JSON file. This |
| 104 // should always be the case once logging has been stopped. | 105 // should always be the case once logging has been stopped. |
| 105 void VerifyNetExportLogComplete() const { | 106 void VerifyNetExportLogComplete() const { |
| 106 VerifyNetExportLogExists(); | 107 VerifyNetExportLogExists(); |
| 107 | 108 |
| 108 std::string log; | 109 std::string log; |
| 109 ASSERT_TRUE(ReadFileToString(net_export_log_, &log)); | 110 ASSERT_TRUE(ReadFileToString(net_export_log_, &log)); |
| 110 base::JSONReader reader; | 111 base::JSONReader reader; |
| 111 scoped_ptr<base::Value> json = base::JSONReader::Read(log); | 112 std::unique_ptr<base::Value> json = base::JSONReader::Read(log); |
| 112 EXPECT_TRUE(json); | 113 EXPECT_TRUE(json); |
| 113 } | 114 } |
| 114 | 115 |
| 115 // Verify state and GetFilePath return correct values if EnsureInit() fails. | 116 // Verify state and GetFilePath return correct values if EnsureInit() fails. |
| 116 void VerifyFilePathAndStateAfterEnsureInitFailure() { | 117 void VerifyFilePathAndStateAfterEnsureInitFailure() { |
| 117 EXPECT_EQ("UNINITIALIZED", GetStateString()); | 118 EXPECT_EQ("UNINITIALIZED", GetStateString()); |
| 118 EXPECT_EQ(NetLogTempFile::STATE_UNINITIALIZED, net_log_temp_file_->state()); | 119 EXPECT_EQ(NetLogTempFile::STATE_UNINITIALIZED, net_log_temp_file_->state()); |
| 119 | 120 |
| 120 base::FilePath net_export_file_path; | 121 base::FilePath net_export_file_path; |
| 121 EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path)); | 122 EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path)); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 void VerifyFileAndStateAfterDoStopWithStripPrivateData() const { | 166 void VerifyFileAndStateAfterDoStopWithStripPrivateData() const { |
| 166 VerifyFileAndStateAfterDoStop(NetLogTempFile::LOG_TYPE_STRIP_PRIVATE_DATA, | 167 VerifyFileAndStateAfterDoStop(NetLogTempFile::LOG_TYPE_STRIP_PRIVATE_DATA, |
| 167 "STRIP_PRIVATE_DATA"); | 168 "STRIP_PRIVATE_DATA"); |
| 168 } | 169 } |
| 169 | 170 |
| 170 void VerifyFileAndStateAfterDoStopWithLogBytes() const { | 171 void VerifyFileAndStateAfterDoStopWithLogBytes() const { |
| 171 VerifyFileAndStateAfterDoStop(NetLogTempFile::LOG_TYPE_LOG_BYTES, | 172 VerifyFileAndStateAfterDoStop(NetLogTempFile::LOG_TYPE_LOG_BYTES, |
| 172 "LOG_BYTES"); | 173 "LOG_BYTES"); |
| 173 } | 174 } |
| 174 | 175 |
| 175 scoped_ptr<ChromeNetLog> net_log_; | 176 std::unique_ptr<ChromeNetLog> net_log_; |
| 176 // |net_log_temp_file_| is initialized after |net_log_| so that it can stop | 177 // |net_log_temp_file_| is initialized after |net_log_| so that it can stop |
| 177 // obvserving on destruction. | 178 // obvserving on destruction. |
| 178 scoped_ptr<TestNetLogTempFile> net_log_temp_file_; | 179 std::unique_ptr<TestNetLogTempFile> net_log_temp_file_; |
| 179 base::FilePath net_export_log_; | 180 base::FilePath net_export_log_; |
| 180 | 181 |
| 181 private: | 182 private: |
| 182 // Checks state after one of the DO_START* commands. | 183 // Checks state after one of the DO_START* commands. |
| 183 void VerifyFileAndStateAfterStart( | 184 void VerifyFileAndStateAfterStart( |
| 184 NetLogTempFile::LogType expected_log_type, | 185 NetLogTempFile::LogType expected_log_type, |
| 185 const std::string& expected_log_type_string, | 186 const std::string& expected_log_type_string, |
| 186 net::NetLogCaptureMode expected_capture_mode) const { | 187 net::NetLogCaptureMode expected_capture_mode) const { |
| 187 EXPECT_EQ(NetLogTempFile::STATE_LOGGING, net_log_temp_file_->state()); | 188 EXPECT_EQ(NetLogTempFile::STATE_LOGGING, net_log_temp_file_->state()); |
| 188 EXPECT_EQ("LOGGING", GetStateString()); | 189 EXPECT_EQ("LOGGING", GetStateString()); |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 | 385 |
| 385 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); | 386 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); |
| 386 VerifyFileAndStateAfterDoStop(); | 387 VerifyFileAndStateAfterDoStop(); |
| 387 | 388 |
| 388 int64_t new_stop_file_size; | 389 int64_t new_stop_file_size; |
| 389 EXPECT_TRUE(base::GetFileSize(net_export_log_, &new_stop_file_size)); | 390 EXPECT_TRUE(base::GetFileSize(net_export_log_, &new_stop_file_size)); |
| 390 EXPECT_GE(new_stop_file_size, stop_file_size); | 391 EXPECT_GE(new_stop_file_size, stop_file_size); |
| 391 } | 392 } |
| 392 | 393 |
| 393 } // namespace net_log | 394 } // namespace net_log |
| OLD | NEW |