| 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 <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/files/scoped_file.h" | 12 #include "base/files/scoped_file.h" |
| 13 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
| 14 #include "base/json/json_reader.h" | 14 #include "base/json/json_reader.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_entry.h" |
| 17 #include "net/log/net_log_event_type.h" | 18 #include "net/log/net_log_event_type.h" |
| 19 #include "net/log/net_log_source.h" |
| 18 #include "net/log/net_log_source_type.h" | 20 #include "net/log/net_log_source_type.h" |
| 19 #include "net/log/net_log_util.h" | 21 #include "net/log/net_log_util.h" |
| 20 #include "net/url_request/url_request.h" | 22 #include "net/url_request/url_request.h" |
| 21 #include "net/url_request/url_request_context.h" | 23 #include "net/url_request/url_request_context.h" |
| 22 #include "net/url_request/url_request_test_util.h" | 24 #include "net/url_request/url_request_test_util.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 26 |
| 25 namespace net { | 27 namespace net { |
| 26 | 28 |
| 27 namespace { | 29 namespace { |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 } | 86 } |
| 85 | 87 |
| 86 TEST_F(WriteToFileNetLogObserverTest, GeneratesValidJSONWithOneEvent) { | 88 TEST_F(WriteToFileNetLogObserverTest, GeneratesValidJSONWithOneEvent) { |
| 87 base::ScopedFILE file(base::OpenFile(log_path_, "w")); | 89 base::ScopedFILE file(base::OpenFile(log_path_, "w")); |
| 88 ASSERT_TRUE(file); | 90 ASSERT_TRUE(file); |
| 89 std::unique_ptr<WriteToFileNetLogObserver> logger( | 91 std::unique_ptr<WriteToFileNetLogObserver> logger( |
| 90 new WriteToFileNetLogObserver()); | 92 new WriteToFileNetLogObserver()); |
| 91 logger->StartObserving(&net_log_, std::move(file), nullptr, nullptr); | 93 logger->StartObserving(&net_log_, std::move(file), nullptr, nullptr); |
| 92 | 94 |
| 93 const int kDummyId = 1; | 95 const int kDummyId = 1; |
| 94 NetLog::Source source(NetLogSourceType::HTTP2_SESSION, kDummyId); | 96 NetLogSource source(NetLogSourceType::HTTP2_SESSION, kDummyId); |
| 95 NetLog::EntryData entry_data(NetLogEventType::PROXY_SERVICE, source, | 97 NetLogEntryData entry_data(NetLogEventType::PROXY_SERVICE, source, |
| 96 NetLogEventPhase::BEGIN, base::TimeTicks::Now(), | 98 NetLogEventPhase::BEGIN, base::TimeTicks::Now(), |
| 97 NULL); | 99 NULL); |
| 98 NetLog::Entry entry(&entry_data, NetLogCaptureMode::IncludeSocketBytes()); | 100 NetLogEntry entry(&entry_data, NetLogCaptureMode::IncludeSocketBytes()); |
| 99 logger->OnAddEntry(entry); | 101 logger->OnAddEntry(entry); |
| 100 logger->StopObserving(nullptr); | 102 logger->StopObserving(nullptr); |
| 101 logger.reset(); | 103 logger.reset(); |
| 102 | 104 |
| 103 std::string input; | 105 std::string input; |
| 104 ASSERT_TRUE(base::ReadFileToString(log_path_, &input)); | 106 ASSERT_TRUE(base::ReadFileToString(log_path_, &input)); |
| 105 | 107 |
| 106 base::JSONReader reader; | 108 base::JSONReader reader; |
| 107 std::unique_ptr<base::Value> root(reader.ReadToValue(input)); | 109 std::unique_ptr<base::Value> root(reader.ReadToValue(input)); |
| 108 ASSERT_TRUE(root) << reader.GetErrorMessage(); | 110 ASSERT_TRUE(root) << reader.GetErrorMessage(); |
| 109 | 111 |
| 110 base::DictionaryValue* dict; | 112 base::DictionaryValue* dict; |
| 111 ASSERT_TRUE(root->GetAsDictionary(&dict)); | 113 ASSERT_TRUE(root->GetAsDictionary(&dict)); |
| 112 base::ListValue* events; | 114 base::ListValue* events; |
| 113 ASSERT_TRUE(dict->GetList("events", &events)); | 115 ASSERT_TRUE(dict->GetList("events", &events)); |
| 114 ASSERT_EQ(1u, events->GetSize()); | 116 ASSERT_EQ(1u, events->GetSize()); |
| 115 } | 117 } |
| 116 | 118 |
| 117 TEST_F(WriteToFileNetLogObserverTest, GeneratesValidJSONWithMultipleEvents) { | 119 TEST_F(WriteToFileNetLogObserverTest, GeneratesValidJSONWithMultipleEvents) { |
| 118 base::ScopedFILE file(base::OpenFile(log_path_, "w")); | 120 base::ScopedFILE file(base::OpenFile(log_path_, "w")); |
| 119 ASSERT_TRUE(file); | 121 ASSERT_TRUE(file); |
| 120 std::unique_ptr<WriteToFileNetLogObserver> logger( | 122 std::unique_ptr<WriteToFileNetLogObserver> logger( |
| 121 new WriteToFileNetLogObserver()); | 123 new WriteToFileNetLogObserver()); |
| 122 logger->StartObserving(&net_log_, std::move(file), nullptr, nullptr); | 124 logger->StartObserving(&net_log_, std::move(file), nullptr, nullptr); |
| 123 | 125 |
| 124 const int kDummyId = 1; | 126 const int kDummyId = 1; |
| 125 NetLog::Source source(NetLogSourceType::HTTP2_SESSION, kDummyId); | 127 NetLogSource source(NetLogSourceType::HTTP2_SESSION, kDummyId); |
| 126 NetLog::EntryData entry_data(NetLogEventType::PROXY_SERVICE, source, | 128 NetLogEntryData entry_data(NetLogEventType::PROXY_SERVICE, source, |
| 127 NetLogEventPhase::BEGIN, base::TimeTicks::Now(), | 129 NetLogEventPhase::BEGIN, base::TimeTicks::Now(), |
| 128 NULL); | 130 NULL); |
| 129 NetLog::Entry entry(&entry_data, NetLogCaptureMode::IncludeSocketBytes()); | 131 NetLogEntry entry(&entry_data, NetLogCaptureMode::IncludeSocketBytes()); |
| 130 | 132 |
| 131 // Add the entry multiple times. | 133 // Add the entry multiple times. |
| 132 logger->OnAddEntry(entry); | 134 logger->OnAddEntry(entry); |
| 133 logger->OnAddEntry(entry); | 135 logger->OnAddEntry(entry); |
| 134 logger->StopObserving(nullptr); | 136 logger->StopObserving(nullptr); |
| 135 logger.reset(); | 137 logger.reset(); |
| 136 | 138 |
| 137 std::string input; | 139 std::string input; |
| 138 ASSERT_TRUE(base::ReadFileToString(log_path_, &input)); | 140 ASSERT_TRUE(base::ReadFileToString(log_path_, &input)); |
| 139 | 141 |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 ASSERT_EQ(1u, events->GetSize()); | 245 ASSERT_EQ(1u, events->GetSize()); |
| 244 | 246 |
| 245 // Make sure additional information is present, but don't validate it. | 247 // Make sure additional information is present, but don't validate it. |
| 246 base::DictionaryValue* tab_info; | 248 base::DictionaryValue* tab_info; |
| 247 ASSERT_TRUE(dict->GetDictionary("tabInfo", &tab_info)); | 249 ASSERT_TRUE(dict->GetDictionary("tabInfo", &tab_info)); |
| 248 } | 250 } |
| 249 | 251 |
| 250 } // namespace | 252 } // namespace |
| 251 | 253 |
| 252 } // namespace net | 254 } // namespace net |
| OLD | NEW |