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