DevTools: kill codeschool extension from whitelist
[chromium-blink-merge.git] / net / log / write_to_file_net_log_observer_unittest.cc
blob4cabfe8cc83eece142915f15b53a3ebbe822bbbe
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
3 // found in the LICENSE file.
5 #include "net/log/write_to_file_net_log_observer.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_file.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/json/json_reader.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/values.h"
14 #include "net/log/net_log.h"
15 #include "net/log/net_log_util.h"
16 #include "net/url_request/url_request.h"
17 #include "net/url_request/url_request_context.h"
18 #include "net/url_request/url_request_test_util.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 namespace net {
23 namespace {
25 class WriteToFileNetLogObserverTest : public testing::Test {
26 public:
27 void SetUp() override {
28 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
29 log_path_ = temp_dir_.path().AppendASCII("NetLogFile");
32 protected:
33 base::ScopedTempDir temp_dir_;
34 base::FilePath log_path_;
35 NetLog net_log_;
38 TEST_F(WriteToFileNetLogObserverTest, GeneratesValidJSONForNoEvents) {
39 // Create and destroy a logger.
40 base::ScopedFILE file(base::OpenFile(log_path_, "w"));
41 ASSERT_TRUE(file);
42 scoped_ptr<WriteToFileNetLogObserver> logger(new WriteToFileNetLogObserver());
43 logger->StartObserving(&net_log_, file.Pass(), nullptr, nullptr);
44 logger->StopObserving(nullptr);
45 logger.reset();
47 std::string input;
48 ASSERT_TRUE(base::ReadFileToString(log_path_, &input));
50 base::JSONReader reader;
51 scoped_ptr<base::Value> root(reader.ReadToValue(input));
52 ASSERT_TRUE(root) << reader.GetErrorMessage();
54 base::DictionaryValue* dict;
55 ASSERT_TRUE(root->GetAsDictionary(&dict));
56 base::ListValue* events;
57 ASSERT_TRUE(dict->GetList("events", &events));
58 ASSERT_EQ(0u, events->GetSize());
60 base::DictionaryValue* constants;
61 ASSERT_TRUE(dict->GetDictionary("constants", &constants));
64 TEST_F(WriteToFileNetLogObserverTest, CaptureMode) {
65 base::ScopedFILE file(base::OpenFile(log_path_, "w"));
66 ASSERT_TRUE(file);
67 WriteToFileNetLogObserver logger;
68 logger.StartObserving(&net_log_, file.Pass(), nullptr, nullptr);
69 EXPECT_EQ(NetLogCaptureMode::Default(), logger.capture_mode());
70 EXPECT_EQ(NetLogCaptureMode::Default(), net_log_.GetCaptureMode());
71 logger.StopObserving(nullptr);
73 file.reset(base::OpenFile(log_path_, "w"));
74 ASSERT_TRUE(file);
75 logger.set_capture_mode(NetLogCaptureMode::IncludeCookiesAndCredentials());
76 logger.StartObserving(&net_log_, file.Pass(), nullptr, nullptr);
77 EXPECT_EQ(NetLogCaptureMode::IncludeCookiesAndCredentials(),
78 logger.capture_mode());
79 EXPECT_EQ(NetLogCaptureMode::IncludeCookiesAndCredentials(),
80 net_log_.GetCaptureMode());
81 logger.StopObserving(nullptr);
84 TEST_F(WriteToFileNetLogObserverTest, GeneratesValidJSONWithOneEvent) {
85 base::ScopedFILE file(base::OpenFile(log_path_, "w"));
86 ASSERT_TRUE(file);
87 scoped_ptr<WriteToFileNetLogObserver> logger(new WriteToFileNetLogObserver());
88 logger->StartObserving(&net_log_, file.Pass(), nullptr, nullptr);
90 const int kDummyId = 1;
91 NetLog::Source source(NetLog::SOURCE_HTTP2_SESSION, kDummyId);
92 NetLog::EntryData entry_data(NetLog::TYPE_PROXY_SERVICE, source,
93 NetLog::PHASE_BEGIN, base::TimeTicks::Now(),
94 NULL);
95 NetLog::Entry entry(&entry_data, NetLogCaptureMode::IncludeSocketBytes());
96 logger->OnAddEntry(entry);
97 logger->StopObserving(nullptr);
98 logger.reset();
100 std::string input;
101 ASSERT_TRUE(base::ReadFileToString(log_path_, &input));
103 base::JSONReader reader;
104 scoped_ptr<base::Value> root(reader.ReadToValue(input));
105 ASSERT_TRUE(root) << reader.GetErrorMessage();
107 base::DictionaryValue* dict;
108 ASSERT_TRUE(root->GetAsDictionary(&dict));
109 base::ListValue* events;
110 ASSERT_TRUE(dict->GetList("events", &events));
111 ASSERT_EQ(1u, events->GetSize());
114 TEST_F(WriteToFileNetLogObserverTest, GeneratesValidJSONWithMultipleEvents) {
115 base::ScopedFILE file(base::OpenFile(log_path_, "w"));
116 ASSERT_TRUE(file);
117 scoped_ptr<WriteToFileNetLogObserver> logger(new WriteToFileNetLogObserver());
118 logger->StartObserving(&net_log_, file.Pass(), nullptr, nullptr);
120 const int kDummyId = 1;
121 NetLog::Source source(NetLog::SOURCE_HTTP2_SESSION, kDummyId);
122 NetLog::EntryData entry_data(NetLog::TYPE_PROXY_SERVICE, source,
123 NetLog::PHASE_BEGIN, base::TimeTicks::Now(),
124 NULL);
125 NetLog::Entry entry(&entry_data, NetLogCaptureMode::IncludeSocketBytes());
127 // Add the entry multiple times.
128 logger->OnAddEntry(entry);
129 logger->OnAddEntry(entry);
130 logger->StopObserving(nullptr);
131 logger.reset();
133 std::string input;
134 ASSERT_TRUE(base::ReadFileToString(log_path_, &input));
136 base::JSONReader reader;
137 scoped_ptr<base::Value> root(reader.ReadToValue(input));
138 ASSERT_TRUE(root) << reader.GetErrorMessage();
140 base::DictionaryValue* dict;
141 ASSERT_TRUE(root->GetAsDictionary(&dict));
142 base::ListValue* events;
143 ASSERT_TRUE(dict->GetList("events", &events));
144 ASSERT_EQ(2u, events->GetSize());
147 TEST_F(WriteToFileNetLogObserverTest, CustomConstants) {
148 const char kConstantString[] = "awesome constant";
149 scoped_ptr<base::Value> constants(new base::StringValue(kConstantString));
150 base::ScopedFILE file(base::OpenFile(log_path_, "w"));
151 ASSERT_TRUE(file);
152 scoped_ptr<WriteToFileNetLogObserver> logger(new WriteToFileNetLogObserver());
153 logger->StartObserving(&net_log_, file.Pass(), constants.get(), nullptr);
154 logger->StopObserving(nullptr);
155 logger.reset();
157 std::string input;
158 ASSERT_TRUE(base::ReadFileToString(log_path_, &input));
160 base::JSONReader reader;
161 scoped_ptr<base::Value> root(reader.ReadToValue(input));
162 ASSERT_TRUE(root) << reader.GetErrorMessage();
164 base::DictionaryValue* dict;
165 ASSERT_TRUE(root->GetAsDictionary(&dict));
166 std::string constants_string;
167 ASSERT_TRUE(dict->GetString("constants", &constants_string));
168 ASSERT_EQ(kConstantString, constants_string);
171 TEST_F(WriteToFileNetLogObserverTest, GeneratesValidJSONWithContext) {
172 // Create context, start a request.
173 TestURLRequestContext context(true);
174 context.set_net_log(&net_log_);
175 context.Init();
177 // Create and destroy a logger.
178 base::ScopedFILE file(base::OpenFile(log_path_, "w"));
179 ASSERT_TRUE(file);
180 scoped_ptr<WriteToFileNetLogObserver> logger(new WriteToFileNetLogObserver());
181 logger->StartObserving(&net_log_, file.Pass(), nullptr, &context);
182 logger->StopObserving(&context);
183 logger.reset();
185 std::string input;
186 ASSERT_TRUE(base::ReadFileToString(log_path_, &input));
188 base::JSONReader reader;
189 scoped_ptr<base::Value> root(reader.ReadToValue(input));
190 ASSERT_TRUE(root) << reader.GetErrorMessage();
192 base::DictionaryValue* dict;
193 ASSERT_TRUE(root->GetAsDictionary(&dict));
194 base::ListValue* events;
195 ASSERT_TRUE(dict->GetList("events", &events));
196 ASSERT_EQ(0u, events->GetSize());
198 // Make sure additional information is present, but don't validate it.
199 base::DictionaryValue* tab_info;
200 ASSERT_TRUE(dict->GetDictionary("tabInfo", &tab_info));
203 TEST_F(WriteToFileNetLogObserverTest,
204 GeneratesValidJSONWithContextWithActiveRequest) {
205 // Create context, start a request.
206 TestURLRequestContext context(true);
207 context.set_net_log(&net_log_);
208 context.Init();
209 TestDelegate delegate;
211 // URL doesn't matter. Requests can't fail synchronously.
212 scoped_ptr<URLRequest> request(
213 context.CreateRequest(GURL("blah:blah"), IDLE, &delegate));
214 request->Start();
216 // Create and destroy a logger.
217 base::ScopedFILE file(base::OpenFile(log_path_, "w"));
218 ASSERT_TRUE(file);
219 scoped_ptr<WriteToFileNetLogObserver> logger(new WriteToFileNetLogObserver());
220 logger->StartObserving(&net_log_, file.Pass(), nullptr, &context);
221 logger->StopObserving(&context);
222 logger.reset();
224 std::string input;
225 ASSERT_TRUE(base::ReadFileToString(log_path_, &input));
227 base::JSONReader reader;
228 scoped_ptr<base::Value> root(reader.ReadToValue(input));
229 ASSERT_TRUE(root) << reader.GetErrorMessage();
231 base::DictionaryValue* dict;
232 ASSERT_TRUE(root->GetAsDictionary(&dict));
233 base::ListValue* events;
234 ASSERT_TRUE(dict->GetList("events", &events));
235 ASSERT_EQ(1u, events->GetSize());
237 // Make sure additional information is present, but don't validate it.
238 base::DictionaryValue* tab_info;
239 ASSERT_TRUE(dict->GetDictionary("tabInfo", &tab_info));
242 } // namespace
244 } // namespace net