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