Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(22)

Side by Side Diff: net/log/write_to_file_net_log_observer_unittest.cc

Issue 1084533002: Rename NetLogLogger and CapturingNetLog (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/net_log_logger.h" 5 #include "net/log/write_to_file_net_log_observer.h"
6 6
7 #include "base/files/file_path.h" 7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h" 8 #include "base/files/file_util.h"
9 #include "base/files/scoped_file.h" 9 #include "base/files/scoped_file.h"
10 #include "base/files/scoped_temp_dir.h" 10 #include "base/files/scoped_temp_dir.h"
11 #include "base/json/json_reader.h" 11 #include "base/json/json_reader.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/values.h" 13 #include "base/values.h"
14 #include "net/log/net_log.h" 14 #include "net/log/net_log.h"
15 #include "net/log/net_log_util.h" 15 #include "net/log/net_log_util.h"
16 #include "net/url_request/url_request.h" 16 #include "net/url_request/url_request.h"
17 #include "net/url_request/url_request_context.h" 17 #include "net/url_request/url_request_context.h"
18 #include "net/url_request/url_request_test_util.h" 18 #include "net/url_request/url_request_test_util.h"
19 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
20 20
21 namespace net { 21 namespace net {
22 22
23 namespace { 23 namespace {
24 24
25 class NetLogLoggerTest : public testing::Test { 25 class NetLogLoggerTest : public testing::Test {
eroman 2015/04/14 20:06:12 This should be renamed to: WriteToFileNetLogO
26 public: 26 public:
27 void SetUp() override { 27 void SetUp() override {
28 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 28 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
29 log_path_ = temp_dir_.path().AppendASCII("NetLogFile"); 29 log_path_ = temp_dir_.path().AppendASCII("NetLogFile");
30 } 30 }
31 31
32 protected: 32 protected:
33 base::ScopedTempDir temp_dir_; 33 base::ScopedTempDir temp_dir_;
34 base::FilePath log_path_; 34 base::FilePath log_path_;
35 NetLog net_log_; 35 NetLog net_log_;
36 }; 36 };
37 37
38 TEST_F(NetLogLoggerTest, GeneratesValidJSONForNoEvents) { 38 TEST_F(NetLogLoggerTest, GeneratesValidJSONForNoEvents) {
39 // Create and destroy a logger. 39 // Create and destroy a logger.
40 base::ScopedFILE file(base::OpenFile(log_path_, "w")); 40 base::ScopedFILE file(base::OpenFile(log_path_, "w"));
41 ASSERT_TRUE(file); 41 ASSERT_TRUE(file);
42 scoped_ptr<NetLogLogger> logger(new NetLogLogger()); 42 scoped_ptr<WriteToFileNetLogObserver> logger(new WriteToFileNetLogObserver());
43 logger->StartObserving(&net_log_, file.Pass(), nullptr, nullptr); 43 logger->StartObserving(&net_log_, file.Pass(), nullptr, nullptr);
44 logger->StopObserving(nullptr); 44 logger->StopObserving(nullptr);
45 logger.reset(); 45 logger.reset();
46 46
47 std::string input; 47 std::string input;
48 ASSERT_TRUE(base::ReadFileToString(log_path_, &input)); 48 ASSERT_TRUE(base::ReadFileToString(log_path_, &input));
49 49
50 base::JSONReader reader; 50 base::JSONReader reader;
51 scoped_ptr<base::Value> root(reader.ReadToValue(input)); 51 scoped_ptr<base::Value> root(reader.ReadToValue(input));
52 ASSERT_TRUE(root) << reader.GetErrorMessage(); 52 ASSERT_TRUE(root) << reader.GetErrorMessage();
53 53
54 base::DictionaryValue* dict; 54 base::DictionaryValue* dict;
55 ASSERT_TRUE(root->GetAsDictionary(&dict)); 55 ASSERT_TRUE(root->GetAsDictionary(&dict));
56 base::ListValue* events; 56 base::ListValue* events;
57 ASSERT_TRUE(dict->GetList("events", &events)); 57 ASSERT_TRUE(dict->GetList("events", &events));
58 ASSERT_EQ(0u, events->GetSize()); 58 ASSERT_EQ(0u, events->GetSize());
59 59
60 base::DictionaryValue* constants; 60 base::DictionaryValue* constants;
61 ASSERT_TRUE(dict->GetDictionary("constants", &constants)); 61 ASSERT_TRUE(dict->GetDictionary("constants", &constants));
62 } 62 }
63 63
64 TEST_F(NetLogLoggerTest, LogLevel) { 64 TEST_F(NetLogLoggerTest, LogLevel) {
65 base::ScopedFILE file(base::OpenFile(log_path_, "w")); 65 base::ScopedFILE file(base::OpenFile(log_path_, "w"));
66 ASSERT_TRUE(file); 66 ASSERT_TRUE(file);
67 NetLogLogger logger; 67 WriteToFileNetLogObserver logger;
68 logger.StartObserving(&net_log_, file.Pass(), nullptr, nullptr); 68 logger.StartObserving(&net_log_, file.Pass(), nullptr, nullptr);
69 EXPECT_EQ(NetLog::LOG_STRIP_PRIVATE_DATA, logger.log_level()); 69 EXPECT_EQ(NetLog::LOG_STRIP_PRIVATE_DATA, logger.log_level());
70 EXPECT_EQ(NetLog::LOG_STRIP_PRIVATE_DATA, net_log_.GetLogLevel()); 70 EXPECT_EQ(NetLog::LOG_STRIP_PRIVATE_DATA, net_log_.GetLogLevel());
71 logger.StopObserving(nullptr); 71 logger.StopObserving(nullptr);
72 72
73 file.reset(base::OpenFile(log_path_, "w")); 73 file.reset(base::OpenFile(log_path_, "w"));
74 ASSERT_TRUE(file); 74 ASSERT_TRUE(file);
75 logger.set_log_level(NetLog::LOG_ALL_BUT_BYTES); 75 logger.set_log_level(NetLog::LOG_ALL_BUT_BYTES);
76 logger.StartObserving(&net_log_, file.Pass(), nullptr, nullptr); 76 logger.StartObserving(&net_log_, file.Pass(), nullptr, nullptr);
77 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES, logger.log_level()); 77 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES, logger.log_level());
78 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES, net_log_.GetLogLevel()); 78 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES, net_log_.GetLogLevel());
79 logger.StopObserving(nullptr); 79 logger.StopObserving(nullptr);
80 } 80 }
81 81
82 TEST_F(NetLogLoggerTest, GeneratesValidJSONWithOneEvent) { 82 TEST_F(NetLogLoggerTest, GeneratesValidJSONWithOneEvent) {
83 base::ScopedFILE file(base::OpenFile(log_path_, "w")); 83 base::ScopedFILE file(base::OpenFile(log_path_, "w"));
84 ASSERT_TRUE(file); 84 ASSERT_TRUE(file);
85 scoped_ptr<NetLogLogger> logger(new NetLogLogger()); 85 scoped_ptr<WriteToFileNetLogObserver> logger(new WriteToFileNetLogObserver());
86 logger->StartObserving(&net_log_, file.Pass(), nullptr, nullptr); 86 logger->StartObserving(&net_log_, file.Pass(), nullptr, nullptr);
87 87
88 const int kDummyId = 1; 88 const int kDummyId = 1;
89 NetLog::Source source(NetLog::SOURCE_HTTP2_SESSION, kDummyId); 89 NetLog::Source source(NetLog::SOURCE_HTTP2_SESSION, kDummyId);
90 NetLog::EntryData entry_data(NetLog::TYPE_PROXY_SERVICE, source, 90 NetLog::EntryData entry_data(NetLog::TYPE_PROXY_SERVICE, source,
91 NetLog::PHASE_BEGIN, base::TimeTicks::Now(), 91 NetLog::PHASE_BEGIN, base::TimeTicks::Now(),
92 NULL); 92 NULL);
93 NetLog::Entry entry(&entry_data, NetLog::LOG_ALL); 93 NetLog::Entry entry(&entry_data, NetLog::LOG_ALL);
94 logger->OnAddEntry(entry); 94 logger->OnAddEntry(entry);
95 logger->StopObserving(nullptr); 95 logger->StopObserving(nullptr);
96 logger.reset(); 96 logger.reset();
97 97
98 std::string input; 98 std::string input;
99 ASSERT_TRUE(base::ReadFileToString(log_path_, &input)); 99 ASSERT_TRUE(base::ReadFileToString(log_path_, &input));
100 100
101 base::JSONReader reader; 101 base::JSONReader reader;
102 scoped_ptr<base::Value> root(reader.ReadToValue(input)); 102 scoped_ptr<base::Value> root(reader.ReadToValue(input));
103 ASSERT_TRUE(root) << reader.GetErrorMessage(); 103 ASSERT_TRUE(root) << reader.GetErrorMessage();
104 104
105 base::DictionaryValue* dict; 105 base::DictionaryValue* dict;
106 ASSERT_TRUE(root->GetAsDictionary(&dict)); 106 ASSERT_TRUE(root->GetAsDictionary(&dict));
107 base::ListValue* events; 107 base::ListValue* events;
108 ASSERT_TRUE(dict->GetList("events", &events)); 108 ASSERT_TRUE(dict->GetList("events", &events));
109 ASSERT_EQ(1u, events->GetSize()); 109 ASSERT_EQ(1u, events->GetSize());
110 } 110 }
111 111
112 TEST_F(NetLogLoggerTest, GeneratesValidJSONWithMultipleEvents) { 112 TEST_F(NetLogLoggerTest, GeneratesValidJSONWithMultipleEvents) {
113 base::ScopedFILE file(base::OpenFile(log_path_, "w")); 113 base::ScopedFILE file(base::OpenFile(log_path_, "w"));
114 ASSERT_TRUE(file); 114 ASSERT_TRUE(file);
115 scoped_ptr<NetLogLogger> logger(new NetLogLogger()); 115 scoped_ptr<WriteToFileNetLogObserver> logger(new WriteToFileNetLogObserver());
116 logger->StartObserving(&net_log_, file.Pass(), nullptr, nullptr); 116 logger->StartObserving(&net_log_, file.Pass(), nullptr, nullptr);
117 117
118 const int kDummyId = 1; 118 const int kDummyId = 1;
119 NetLog::Source source(NetLog::SOURCE_HTTP2_SESSION, kDummyId); 119 NetLog::Source source(NetLog::SOURCE_HTTP2_SESSION, kDummyId);
120 NetLog::EntryData entry_data(NetLog::TYPE_PROXY_SERVICE, source, 120 NetLog::EntryData entry_data(NetLog::TYPE_PROXY_SERVICE, source,
121 NetLog::PHASE_BEGIN, base::TimeTicks::Now(), 121 NetLog::PHASE_BEGIN, base::TimeTicks::Now(),
122 NULL); 122 NULL);
123 NetLog::Entry entry(&entry_data, NetLog::LOG_ALL); 123 NetLog::Entry entry(&entry_data, NetLog::LOG_ALL);
124 124
125 // Add the entry multiple times. 125 // Add the entry multiple times.
(...skipping 14 matching lines...) Expand all
140 base::ListValue* events; 140 base::ListValue* events;
141 ASSERT_TRUE(dict->GetList("events", &events)); 141 ASSERT_TRUE(dict->GetList("events", &events));
142 ASSERT_EQ(2u, events->GetSize()); 142 ASSERT_EQ(2u, events->GetSize());
143 } 143 }
144 144
145 TEST_F(NetLogLoggerTest, CustomConstants) { 145 TEST_F(NetLogLoggerTest, CustomConstants) {
146 const char kConstantString[] = "awesome constant"; 146 const char kConstantString[] = "awesome constant";
147 scoped_ptr<base::Value> constants(new base::StringValue(kConstantString)); 147 scoped_ptr<base::Value> constants(new base::StringValue(kConstantString));
148 base::ScopedFILE file(base::OpenFile(log_path_, "w")); 148 base::ScopedFILE file(base::OpenFile(log_path_, "w"));
149 ASSERT_TRUE(file); 149 ASSERT_TRUE(file);
150 scoped_ptr<NetLogLogger> logger(new NetLogLogger()); 150 scoped_ptr<WriteToFileNetLogObserver> logger(new WriteToFileNetLogObserver());
151 logger->StartObserving(&net_log_, file.Pass(), constants.get(), nullptr); 151 logger->StartObserving(&net_log_, file.Pass(), constants.get(), nullptr);
152 logger->StopObserving(nullptr); 152 logger->StopObserving(nullptr);
153 logger.reset(); 153 logger.reset();
154 154
155 std::string input; 155 std::string input;
156 ASSERT_TRUE(base::ReadFileToString(log_path_, &input)); 156 ASSERT_TRUE(base::ReadFileToString(log_path_, &input));
157 157
158 base::JSONReader reader; 158 base::JSONReader reader;
159 scoped_ptr<base::Value> root(reader.ReadToValue(input)); 159 scoped_ptr<base::Value> root(reader.ReadToValue(input));
160 ASSERT_TRUE(root) << reader.GetErrorMessage(); 160 ASSERT_TRUE(root) << reader.GetErrorMessage();
161 161
162 base::DictionaryValue* dict; 162 base::DictionaryValue* dict;
163 ASSERT_TRUE(root->GetAsDictionary(&dict)); 163 ASSERT_TRUE(root->GetAsDictionary(&dict));
164 std::string constants_string; 164 std::string constants_string;
165 ASSERT_TRUE(dict->GetString("constants", &constants_string)); 165 ASSERT_TRUE(dict->GetString("constants", &constants_string));
166 ASSERT_EQ(kConstantString, constants_string); 166 ASSERT_EQ(kConstantString, constants_string);
167 } 167 }
168 168
169 TEST_F(NetLogLoggerTest, GeneratesValidJSONWithContext) { 169 TEST_F(NetLogLoggerTest, GeneratesValidJSONWithContext) {
170 // Create context, start a request. 170 // Create context, start a request.
171 TestURLRequestContext context(true); 171 TestURLRequestContext context(true);
172 context.set_net_log(&net_log_); 172 context.set_net_log(&net_log_);
173 context.Init(); 173 context.Init();
174 174
175 // Create and destroy a logger. 175 // Create and destroy a logger.
176 base::ScopedFILE file(base::OpenFile(log_path_, "w")); 176 base::ScopedFILE file(base::OpenFile(log_path_, "w"));
177 ASSERT_TRUE(file); 177 ASSERT_TRUE(file);
178 scoped_ptr<NetLogLogger> logger(new NetLogLogger()); 178 scoped_ptr<WriteToFileNetLogObserver> logger(new WriteToFileNetLogObserver());
179 logger->StartObserving(&net_log_, file.Pass(), nullptr, &context); 179 logger->StartObserving(&net_log_, file.Pass(), nullptr, &context);
180 logger->StopObserving(&context); 180 logger->StopObserving(&context);
181 logger.reset(); 181 logger.reset();
182 182
183 std::string input; 183 std::string input;
184 ASSERT_TRUE(base::ReadFileToString(log_path_, &input)); 184 ASSERT_TRUE(base::ReadFileToString(log_path_, &input));
185 185
186 base::JSONReader reader; 186 base::JSONReader reader;
187 scoped_ptr<base::Value> root(reader.ReadToValue(input)); 187 scoped_ptr<base::Value> root(reader.ReadToValue(input));
188 ASSERT_TRUE(root) << reader.GetErrorMessage(); 188 ASSERT_TRUE(root) << reader.GetErrorMessage();
(...skipping 17 matching lines...) Expand all
206 TestDelegate delegate; 206 TestDelegate delegate;
207 207
208 // URL doesn't matter. Requests can't fail synchronously. 208 // URL doesn't matter. Requests can't fail synchronously.
209 scoped_ptr<URLRequest> request( 209 scoped_ptr<URLRequest> request(
210 context.CreateRequest(GURL("blah:blah"), IDLE, &delegate)); 210 context.CreateRequest(GURL("blah:blah"), IDLE, &delegate));
211 request->Start(); 211 request->Start();
212 212
213 // Create and destroy a logger. 213 // Create and destroy a logger.
214 base::ScopedFILE file(base::OpenFile(log_path_, "w")); 214 base::ScopedFILE file(base::OpenFile(log_path_, "w"));
215 ASSERT_TRUE(file); 215 ASSERT_TRUE(file);
216 scoped_ptr<NetLogLogger> logger(new NetLogLogger()); 216 scoped_ptr<WriteToFileNetLogObserver> logger(new WriteToFileNetLogObserver());
217 logger->StartObserving(&net_log_, file.Pass(), nullptr, &context); 217 logger->StartObserving(&net_log_, file.Pass(), nullptr, &context);
218 logger->StopObserving(&context); 218 logger->StopObserving(&context);
219 logger.reset(); 219 logger.reset();
220 220
221 std::string input; 221 std::string input;
222 ASSERT_TRUE(base::ReadFileToString(log_path_, &input)); 222 ASSERT_TRUE(base::ReadFileToString(log_path_, &input));
223 223
224 base::JSONReader reader; 224 base::JSONReader reader;
225 scoped_ptr<base::Value> root(reader.ReadToValue(input)); 225 scoped_ptr<base::Value> root(reader.ReadToValue(input));
226 ASSERT_TRUE(root) << reader.GetErrorMessage(); 226 ASSERT_TRUE(root) << reader.GetErrorMessage();
227 227
228 base::DictionaryValue* dict; 228 base::DictionaryValue* dict;
229 ASSERT_TRUE(root->GetAsDictionary(&dict)); 229 ASSERT_TRUE(root->GetAsDictionary(&dict));
230 base::ListValue* events; 230 base::ListValue* events;
231 ASSERT_TRUE(dict->GetList("events", &events)); 231 ASSERT_TRUE(dict->GetList("events", &events));
232 ASSERT_EQ(1u, events->GetSize()); 232 ASSERT_EQ(1u, events->GetSize());
233 233
234 // Make sure additional information is present, but don't validate it. 234 // Make sure additional information is present, but don't validate it.
235 base::DictionaryValue* tab_info; 235 base::DictionaryValue* tab_info;
236 ASSERT_TRUE(dict->GetDictionary("tabInfo", &tab_info)); 236 ASSERT_TRUE(dict->GetDictionary("tabInfo", &tab_info));
237 } 237 }
238 238
239 } // namespace 239 } // namespace
240 240
241 } // namespace net 241 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698