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