| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "net/base/net_log_logger.h" | 5 #include "net/base/net_log_logger.h" |
| 6 | 6 |
| 7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/files/scoped_file.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
| 12 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/values.h" | 13 #include "base/values.h" |
| 12 #include "net/base/net_log.h" | 14 #include "net/base/net_log.h" |
| 13 #include "net/base/net_log_util.h" | 15 #include "net/base/net_log_util.h" |
| 16 #include "net/url_request/url_request.h" |
| 17 #include "net/url_request/url_request_context.h" |
| 18 #include "net/url_request/url_request_test_util.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 20 |
| 16 namespace net { | 21 namespace net { |
| 17 | 22 |
| 18 namespace { | 23 namespace { |
| 19 | 24 |
| 20 class NetLogLoggerTest : public testing::Test { | 25 class NetLogLoggerTest : public testing::Test { |
| 21 public: | 26 public: |
| 22 void SetUp() override { | 27 void SetUp() override { |
| 23 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 28 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 24 log_path_ = temp_dir_.path().AppendASCII("NetLogFile"); | 29 log_path_ = temp_dir_.path().AppendASCII("NetLogFile"); |
| 25 } | 30 } |
| 26 | 31 |
| 27 protected: | 32 protected: |
| 28 base::ScopedTempDir temp_dir_; | 33 base::ScopedTempDir temp_dir_; |
| 29 base::FilePath log_path_; | 34 base::FilePath log_path_; |
| 35 NetLog net_log_; |
| 30 }; | 36 }; |
| 31 | 37 |
| 32 TEST_F(NetLogLoggerTest, GeneratesValidJSONForNoEvents) { | 38 TEST_F(NetLogLoggerTest, GeneratesValidJSONForNoEvents) { |
| 33 // Create and destroy a logger. | 39 // Create and destroy a logger. |
| 34 FILE* file = base::OpenFile(log_path_, "w"); | 40 base::ScopedFILE file(base::OpenFile(log_path_, "w")); |
| 35 ASSERT_TRUE(file); | 41 ASSERT_TRUE(file); |
| 36 scoped_ptr<base::Value> constants(GetNetConstants()); | 42 scoped_ptr<NetLogLogger> logger(new NetLogLogger()); |
| 37 scoped_ptr<NetLogLogger> logger(new NetLogLogger(file, *constants)); | 43 logger->StartObserving(&net_log_, file.Pass(), nullptr, nullptr); |
| 44 logger->StopObserving(nullptr); |
| 38 logger.reset(); | 45 logger.reset(); |
| 39 | 46 |
| 40 std::string input; | 47 std::string input; |
| 41 ASSERT_TRUE(base::ReadFileToString(log_path_, &input)); | 48 ASSERT_TRUE(base::ReadFileToString(log_path_, &input)); |
| 42 | 49 |
| 43 base::JSONReader reader; | 50 base::JSONReader reader; |
| 44 scoped_ptr<base::Value> root(reader.ReadToValue(input)); | 51 scoped_ptr<base::Value> root(reader.ReadToValue(input)); |
| 45 ASSERT_TRUE(root) << reader.GetErrorMessage(); | 52 ASSERT_TRUE(root) << reader.GetErrorMessage(); |
| 46 | 53 |
| 47 base::DictionaryValue* dict; | 54 base::DictionaryValue* dict; |
| 48 ASSERT_TRUE(root->GetAsDictionary(&dict)); | 55 ASSERT_TRUE(root->GetAsDictionary(&dict)); |
| 49 base::ListValue* events; | 56 base::ListValue* events; |
| 50 ASSERT_TRUE(dict->GetList("events", &events)); | 57 ASSERT_TRUE(dict->GetList("events", &events)); |
| 51 ASSERT_EQ(0u, events->GetSize()); | 58 ASSERT_EQ(0u, events->GetSize()); |
| 59 |
| 60 base::DictionaryValue* constants; |
| 61 ASSERT_TRUE(dict->GetDictionary("constants", &constants)); |
| 52 } | 62 } |
| 53 | 63 |
| 54 // Make sure the log level is LOG_STRIP_PRIVATE_DATA by default. | |
| 55 TEST_F(NetLogLoggerTest, LogLevel) { | 64 TEST_F(NetLogLoggerTest, LogLevel) { |
| 56 FILE* file = base::OpenFile(log_path_, "w"); | 65 base::ScopedFILE file(base::OpenFile(log_path_, "w")); |
| 57 ASSERT_TRUE(file); | 66 ASSERT_TRUE(file); |
| 58 scoped_ptr<base::Value> constants(GetNetConstants()); | 67 NetLogLogger logger; |
| 59 NetLogLogger logger(file, *constants); | 68 logger.StartObserving(&net_log_, file.Pass(), nullptr, nullptr); |
| 69 EXPECT_EQ(NetLog::LOG_STRIP_PRIVATE_DATA, logger.log_level()); |
| 70 EXPECT_EQ(NetLog::LOG_STRIP_PRIVATE_DATA, net_log_.GetLogLevel()); |
| 71 logger.StopObserving(nullptr); |
| 60 | 72 |
| 61 NetLog net_log; | 73 file.reset(base::OpenFile(log_path_, "w")); |
| 62 logger.StartObserving(&net_log); | 74 ASSERT_TRUE(file); |
| 63 EXPECT_EQ(NetLog::LOG_STRIP_PRIVATE_DATA, logger.log_level()); | |
| 64 EXPECT_EQ(NetLog::LOG_STRIP_PRIVATE_DATA, net_log.GetLogLevel()); | |
| 65 logger.StopObserving(); | |
| 66 | |
| 67 logger.set_log_level(NetLog::LOG_ALL_BUT_BYTES); | 75 logger.set_log_level(NetLog::LOG_ALL_BUT_BYTES); |
| 68 logger.StartObserving(&net_log); | 76 logger.StartObserving(&net_log_, file.Pass(), nullptr, nullptr); |
| 69 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES, logger.log_level()); | 77 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES, logger.log_level()); |
| 70 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES, net_log.GetLogLevel()); | 78 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES, net_log_.GetLogLevel()); |
| 71 logger.StopObserving(); | 79 logger.StopObserving(nullptr); |
| 72 } | 80 } |
| 73 | 81 |
| 74 TEST_F(NetLogLoggerTest, GeneratesValidJSONWithOneEvent) { | 82 TEST_F(NetLogLoggerTest, GeneratesValidJSONWithOneEvent) { |
| 75 FILE* file = base::OpenFile(log_path_, "w"); | 83 base::ScopedFILE file(base::OpenFile(log_path_, "w")); |
| 76 ASSERT_TRUE(file); | 84 ASSERT_TRUE(file); |
| 77 scoped_ptr<base::Value> constants(GetNetConstants()); | 85 scoped_ptr<NetLogLogger> logger(new NetLogLogger()); |
| 78 scoped_ptr<NetLogLogger> logger(new NetLogLogger(file, *constants)); | 86 logger->StartObserving(&net_log_, file.Pass(), nullptr, nullptr); |
| 79 | 87 |
| 80 const int kDummyId = 1; | 88 const int kDummyId = 1; |
| 81 NetLog::Source source(NetLog::SOURCE_HTTP2_SESSION, kDummyId); | 89 NetLog::Source source(NetLog::SOURCE_HTTP2_SESSION, kDummyId); |
| 82 NetLog::EntryData entry_data(NetLog::TYPE_PROXY_SERVICE, | 90 NetLog::EntryData entry_data(NetLog::TYPE_PROXY_SERVICE, |
| 83 source, | 91 source, |
| 84 NetLog::PHASE_BEGIN, | 92 NetLog::PHASE_BEGIN, |
| 85 base::TimeTicks::Now(), | 93 base::TimeTicks::Now(), |
| 86 NULL); | 94 NULL); |
| 87 NetLog::Entry entry(&entry_data, NetLog::LOG_ALL); | 95 NetLog::Entry entry(&entry_data, NetLog::LOG_ALL); |
| 88 logger->OnAddEntry(entry); | 96 logger->OnAddEntry(entry); |
| 97 logger->StopObserving(nullptr); |
| 89 logger.reset(); | 98 logger.reset(); |
| 90 | 99 |
| 91 std::string input; | 100 std::string input; |
| 92 ASSERT_TRUE(base::ReadFileToString(log_path_, &input)); | 101 ASSERT_TRUE(base::ReadFileToString(log_path_, &input)); |
| 93 | 102 |
| 94 base::JSONReader reader; | 103 base::JSONReader reader; |
| 95 scoped_ptr<base::Value> root(reader.ReadToValue(input)); | 104 scoped_ptr<base::Value> root(reader.ReadToValue(input)); |
| 96 ASSERT_TRUE(root) << reader.GetErrorMessage(); | 105 ASSERT_TRUE(root) << reader.GetErrorMessage(); |
| 97 | 106 |
| 98 base::DictionaryValue* dict; | 107 base::DictionaryValue* dict; |
| 99 ASSERT_TRUE(root->GetAsDictionary(&dict)); | 108 ASSERT_TRUE(root->GetAsDictionary(&dict)); |
| 100 base::ListValue* events; | 109 base::ListValue* events; |
| 101 ASSERT_TRUE(dict->GetList("events", &events)); | 110 ASSERT_TRUE(dict->GetList("events", &events)); |
| 102 ASSERT_EQ(1u, events->GetSize()); | 111 ASSERT_EQ(1u, events->GetSize()); |
| 103 } | 112 } |
| 104 | 113 |
| 105 TEST_F(NetLogLoggerTest, GeneratesValidJSONWithMultipleEvents) { | 114 TEST_F(NetLogLoggerTest, GeneratesValidJSONWithMultipleEvents) { |
| 106 FILE* file = base::OpenFile(log_path_, "w"); | 115 base::ScopedFILE file(base::OpenFile(log_path_, "w")); |
| 107 ASSERT_TRUE(file); | 116 ASSERT_TRUE(file); |
| 108 scoped_ptr<base::Value> constants(GetNetConstants()); | 117 scoped_ptr<NetLogLogger> logger(new NetLogLogger()); |
| 109 scoped_ptr<NetLogLogger> logger(new NetLogLogger(file, *constants)); | 118 logger->StartObserving(&net_log_, file.Pass(), nullptr, nullptr); |
| 110 | 119 |
| 111 const int kDummyId = 1; | 120 const int kDummyId = 1; |
| 112 NetLog::Source source(NetLog::SOURCE_HTTP2_SESSION, kDummyId); | 121 NetLog::Source source(NetLog::SOURCE_HTTP2_SESSION, kDummyId); |
| 113 NetLog::EntryData entry_data(NetLog::TYPE_PROXY_SERVICE, | 122 NetLog::EntryData entry_data(NetLog::TYPE_PROXY_SERVICE, |
| 114 source, | 123 source, |
| 115 NetLog::PHASE_BEGIN, | 124 NetLog::PHASE_BEGIN, |
| 116 base::TimeTicks::Now(), | 125 base::TimeTicks::Now(), |
| 117 NULL); | 126 NULL); |
| 118 NetLog::Entry entry(&entry_data, NetLog::LOG_ALL); | 127 NetLog::Entry entry(&entry_data, NetLog::LOG_ALL); |
| 119 | 128 |
| 120 // Add the entry multiple times. | 129 // Add the entry multiple times. |
| 121 logger->OnAddEntry(entry); | 130 logger->OnAddEntry(entry); |
| 122 logger->OnAddEntry(entry); | 131 logger->OnAddEntry(entry); |
| 132 logger->StopObserving(nullptr); |
| 123 logger.reset(); | 133 logger.reset(); |
| 124 | 134 |
| 125 std::string input; | 135 std::string input; |
| 126 ASSERT_TRUE(base::ReadFileToString(log_path_, &input)); | 136 ASSERT_TRUE(base::ReadFileToString(log_path_, &input)); |
| 127 | 137 |
| 128 base::JSONReader reader; | 138 base::JSONReader reader; |
| 129 scoped_ptr<base::Value> root(reader.ReadToValue(input)); | 139 scoped_ptr<base::Value> root(reader.ReadToValue(input)); |
| 130 ASSERT_TRUE(root) << reader.GetErrorMessage(); | 140 ASSERT_TRUE(root) << reader.GetErrorMessage(); |
| 131 | 141 |
| 132 base::DictionaryValue* dict; | 142 base::DictionaryValue* dict; |
| 133 ASSERT_TRUE(root->GetAsDictionary(&dict)); | 143 ASSERT_TRUE(root->GetAsDictionary(&dict)); |
| 134 base::ListValue* events; | 144 base::ListValue* events; |
| 135 ASSERT_TRUE(dict->GetList("events", &events)); | 145 ASSERT_TRUE(dict->GetList("events", &events)); |
| 136 ASSERT_EQ(2u, events->GetSize()); | 146 ASSERT_EQ(2u, events->GetSize()); |
| 137 } | 147 } |
| 138 | 148 |
| 149 TEST_F(NetLogLoggerTest, CustomConstants) { |
| 150 const char kConstantString[] = "awesome constant"; |
| 151 scoped_ptr<base::Value> constants(new base::StringValue(kConstantString)); |
| 152 base::ScopedFILE file(base::OpenFile(log_path_, "w")); |
| 153 ASSERT_TRUE(file); |
| 154 scoped_ptr<NetLogLogger> logger(new NetLogLogger()); |
| 155 logger->StartObserving(&net_log_, file.Pass(), constants.get(), nullptr); |
| 156 logger->StopObserving(nullptr); |
| 157 logger.reset(); |
| 158 |
| 159 std::string input; |
| 160 ASSERT_TRUE(base::ReadFileToString(log_path_, &input)); |
| 161 |
| 162 base::JSONReader reader; |
| 163 scoped_ptr<base::Value> root(reader.ReadToValue(input)); |
| 164 ASSERT_TRUE(root) << reader.GetErrorMessage(); |
| 165 |
| 166 base::DictionaryValue* dict; |
| 167 ASSERT_TRUE(root->GetAsDictionary(&dict)); |
| 168 std::string constants_string; |
| 169 ASSERT_TRUE(dict->GetString("constants", &constants_string)); |
| 170 ASSERT_EQ(kConstantString, constants_string); |
| 171 } |
| 172 |
| 173 TEST_F(NetLogLoggerTest, GeneratesValidJSONWithContext) { |
| 174 // Create context, start a request. |
| 175 TestURLRequestContext context(true); |
| 176 context.set_net_log(&net_log_); |
| 177 context.Init(); |
| 178 |
| 179 // Create and destroy a logger. |
| 180 base::ScopedFILE file(base::OpenFile(log_path_, "w")); |
| 181 ASSERT_TRUE(file); |
| 182 scoped_ptr<NetLogLogger> logger(new NetLogLogger()); |
| 183 logger->StartObserving(&net_log_, file.Pass(), nullptr, &context); |
| 184 logger->StopObserving(&context); |
| 185 logger.reset(); |
| 186 |
| 187 std::string input; |
| 188 ASSERT_TRUE(base::ReadFileToString(log_path_, &input)); |
| 189 |
| 190 base::JSONReader reader; |
| 191 scoped_ptr<base::Value> root(reader.ReadToValue(input)); |
| 192 ASSERT_TRUE(root) << reader.GetErrorMessage(); |
| 193 |
| 194 base::DictionaryValue* dict; |
| 195 ASSERT_TRUE(root->GetAsDictionary(&dict)); |
| 196 base::ListValue* events; |
| 197 ASSERT_TRUE(dict->GetList("events", &events)); |
| 198 ASSERT_EQ(0u, events->GetSize()); |
| 199 |
| 200 // Make sure additional information is present, but don't validate it. |
| 201 base::DictionaryValue* tab_info; |
| 202 ASSERT_TRUE(dict->GetDictionary("tabInfo", &tab_info)); |
| 203 } |
| 204 |
| 205 TEST_F(NetLogLoggerTest, GeneratesValidJSONWithContextWithActiveRequest) { |
| 206 // Create context, start a request. |
| 207 TestURLRequestContext context(true); |
| 208 context.set_net_log(&net_log_); |
| 209 context.Init(); |
| 210 TestDelegate delegate; |
| 211 |
| 212 // URL doesn't matter. Requests can't fail synchronously. |
| 213 scoped_ptr<URLRequest> request( |
| 214 context.CreateRequest(GURL("blah:blah"), IDLE, &delegate, nullptr)); |
| 215 request->Start(); |
| 216 |
| 217 // Create and destroy a logger. |
| 218 base::ScopedFILE file(base::OpenFile(log_path_, "w")); |
| 219 ASSERT_TRUE(file); |
| 220 scoped_ptr<NetLogLogger> logger(new NetLogLogger()); |
| 221 logger->StartObserving(&net_log_, file.Pass(), nullptr, &context); |
| 222 logger->StopObserving(&context); |
| 223 logger.reset(); |
| 224 |
| 225 std::string input; |
| 226 ASSERT_TRUE(base::ReadFileToString(log_path_, &input)); |
| 227 |
| 228 base::JSONReader reader; |
| 229 scoped_ptr<base::Value> root(reader.ReadToValue(input)); |
| 230 ASSERT_TRUE(root) << reader.GetErrorMessage(); |
| 231 |
| 232 base::DictionaryValue* dict; |
| 233 ASSERT_TRUE(root->GetAsDictionary(&dict)); |
| 234 base::ListValue* events; |
| 235 ASSERT_TRUE(dict->GetList("events", &events)); |
| 236 ASSERT_EQ(1u, events->GetSize()); |
| 237 |
| 238 // Make sure additional information is present, but don't validate it. |
| 239 base::DictionaryValue* tab_info; |
| 240 ASSERT_TRUE(dict->GetDictionary("tabInfo", &tab_info)); |
| 241 } |
| 242 |
| 139 } // namespace | 243 } // namespace |
| 140 | 244 |
| 141 } // namespace net | 245 } // namespace net |
| OLD | NEW |