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