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"
25 class WriteToFileNetLogObserverTest
: public testing::Test
{
27 void SetUp() override
{
28 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
29 log_path_
= temp_dir_
.path().AppendASCII("NetLogFile");
33 base::ScopedTempDir temp_dir_
;
34 base::FilePath log_path_
;
38 TEST_F(WriteToFileNetLogObserverTest
, GeneratesValidJSONForNoEvents
) {
39 // Create and destroy a logger.
40 base::ScopedFILE
file(base::OpenFile(log_path_
, "w"));
42 scoped_ptr
<WriteToFileNetLogObserver
> logger(new WriteToFileNetLogObserver());
43 logger
->StartObserving(&net_log_
, file
.Pass(), nullptr, nullptr);
44 logger
->StopObserving(nullptr);
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"));
67 WriteToFileNetLogObserver logger
;
68 logger
.StartObserving(&net_log_
, file
.Pass(), nullptr, nullptr);
69 EXPECT_EQ(NetLogCaptureMode::Default(), logger
.capture_mode());
70 logger
.StopObserving(nullptr);
72 file
.reset(base::OpenFile(log_path_
, "w"));
74 logger
.set_capture_mode(NetLogCaptureMode::IncludeCookiesAndCredentials());
75 logger
.StartObserving(&net_log_
, file
.Pass(), nullptr, nullptr);
76 EXPECT_EQ(NetLogCaptureMode::IncludeCookiesAndCredentials(),
77 logger
.capture_mode());
78 logger
.StopObserving(nullptr);
81 TEST_F(WriteToFileNetLogObserverTest
, GeneratesValidJSONWithOneEvent
) {
82 base::ScopedFILE
file(base::OpenFile(log_path_
, "w"));
84 scoped_ptr
<WriteToFileNetLogObserver
> logger(new WriteToFileNetLogObserver());
85 logger
->StartObserving(&net_log_
, file
.Pass(), nullptr, nullptr);
87 const int kDummyId
= 1;
88 NetLog::Source
source(NetLog::SOURCE_HTTP2_SESSION
, kDummyId
);
89 NetLog::EntryData
entry_data(NetLog::TYPE_PROXY_SERVICE
, source
,
90 NetLog::PHASE_BEGIN
, base::TimeTicks::Now(),
92 NetLog::Entry
entry(&entry_data
, NetLogCaptureMode::IncludeSocketBytes());
93 logger
->OnAddEntry(entry
);
94 logger
->StopObserving(nullptr);
98 ASSERT_TRUE(base::ReadFileToString(log_path_
, &input
));
100 base::JSONReader reader
;
101 scoped_ptr
<base::Value
> root(reader
.ReadToValue(input
));
102 ASSERT_TRUE(root
) << reader
.GetErrorMessage();
104 base::DictionaryValue
* dict
;
105 ASSERT_TRUE(root
->GetAsDictionary(&dict
));
106 base::ListValue
* events
;
107 ASSERT_TRUE(dict
->GetList("events", &events
));
108 ASSERT_EQ(1u, events
->GetSize());
111 TEST_F(WriteToFileNetLogObserverTest
, GeneratesValidJSONWithMultipleEvents
) {
112 base::ScopedFILE
file(base::OpenFile(log_path_
, "w"));
114 scoped_ptr
<WriteToFileNetLogObserver
> logger(new WriteToFileNetLogObserver());
115 logger
->StartObserving(&net_log_
, file
.Pass(), nullptr, nullptr);
117 const int kDummyId
= 1;
118 NetLog::Source
source(NetLog::SOURCE_HTTP2_SESSION
, kDummyId
);
119 NetLog::EntryData
entry_data(NetLog::TYPE_PROXY_SERVICE
, source
,
120 NetLog::PHASE_BEGIN
, base::TimeTicks::Now(),
122 NetLog::Entry
entry(&entry_data
, NetLogCaptureMode::IncludeSocketBytes());
124 // Add the entry multiple times.
125 logger
->OnAddEntry(entry
);
126 logger
->OnAddEntry(entry
);
127 logger
->StopObserving(nullptr);
131 ASSERT_TRUE(base::ReadFileToString(log_path_
, &input
));
133 base::JSONReader reader
;
134 scoped_ptr
<base::Value
> root(reader
.ReadToValue(input
));
135 ASSERT_TRUE(root
) << reader
.GetErrorMessage();
137 base::DictionaryValue
* dict
;
138 ASSERT_TRUE(root
->GetAsDictionary(&dict
));
139 base::ListValue
* events
;
140 ASSERT_TRUE(dict
->GetList("events", &events
));
141 ASSERT_EQ(2u, events
->GetSize());
144 TEST_F(WriteToFileNetLogObserverTest
, CustomConstants
) {
145 const char kConstantString
[] = "awesome constant";
146 scoped_ptr
<base::Value
> constants(new base::StringValue(kConstantString
));
147 base::ScopedFILE
file(base::OpenFile(log_path_
, "w"));
149 scoped_ptr
<WriteToFileNetLogObserver
> logger(new WriteToFileNetLogObserver());
150 logger
->StartObserving(&net_log_
, file
.Pass(), constants
.get(), nullptr);
151 logger
->StopObserving(nullptr);
155 ASSERT_TRUE(base::ReadFileToString(log_path_
, &input
));
157 base::JSONReader reader
;
158 scoped_ptr
<base::Value
> root(reader
.ReadToValue(input
));
159 ASSERT_TRUE(root
) << reader
.GetErrorMessage();
161 base::DictionaryValue
* dict
;
162 ASSERT_TRUE(root
->GetAsDictionary(&dict
));
163 std::string constants_string
;
164 ASSERT_TRUE(dict
->GetString("constants", &constants_string
));
165 ASSERT_EQ(kConstantString
, constants_string
);
168 TEST_F(WriteToFileNetLogObserverTest
, GeneratesValidJSONWithContext
) {
169 // Create context, start a request.
170 TestURLRequestContext
context(true);
171 context
.set_net_log(&net_log_
);
174 // Create and destroy a logger.
175 base::ScopedFILE
file(base::OpenFile(log_path_
, "w"));
177 scoped_ptr
<WriteToFileNetLogObserver
> logger(new WriteToFileNetLogObserver());
178 logger
->StartObserving(&net_log_
, file
.Pass(), nullptr, &context
);
179 logger
->StopObserving(&context
);
183 ASSERT_TRUE(base::ReadFileToString(log_path_
, &input
));
185 base::JSONReader reader
;
186 scoped_ptr
<base::Value
> root(reader
.ReadToValue(input
));
187 ASSERT_TRUE(root
) << reader
.GetErrorMessage();
189 base::DictionaryValue
* dict
;
190 ASSERT_TRUE(root
->GetAsDictionary(&dict
));
191 base::ListValue
* events
;
192 ASSERT_TRUE(dict
->GetList("events", &events
));
193 ASSERT_EQ(0u, events
->GetSize());
195 // Make sure additional information is present, but don't validate it.
196 base::DictionaryValue
* tab_info
;
197 ASSERT_TRUE(dict
->GetDictionary("tabInfo", &tab_info
));
200 TEST_F(WriteToFileNetLogObserverTest
,
201 GeneratesValidJSONWithContextWithActiveRequest
) {
202 // Create context, start a request.
203 TestURLRequestContext
context(true);
204 context
.set_net_log(&net_log_
);
206 TestDelegate delegate
;
208 // URL doesn't matter. Requests can't fail synchronously.
209 scoped_ptr
<URLRequest
> request(
210 context
.CreateRequest(GURL("blah:blah"), IDLE
, &delegate
));
213 // Create and destroy a logger.
214 base::ScopedFILE
file(base::OpenFile(log_path_
, "w"));
216 scoped_ptr
<WriteToFileNetLogObserver
> logger(new WriteToFileNetLogObserver());
217 logger
->StartObserving(&net_log_
, file
.Pass(), nullptr, &context
);
218 logger
->StopObserving(&context
);
222 ASSERT_TRUE(base::ReadFileToString(log_path_
, &input
));
224 base::JSONReader reader
;
225 scoped_ptr
<base::Value
> root(reader
.ReadToValue(input
));
226 ASSERT_TRUE(root
) << reader
.GetErrorMessage();
228 base::DictionaryValue
* dict
;
229 ASSERT_TRUE(root
->GetAsDictionary(&dict
));
230 base::ListValue
* events
;
231 ASSERT_TRUE(dict
->GetList("events", &events
));
232 ASSERT_EQ(1u, events
->GetSize());
234 // Make sure additional information is present, but don't validate it.
235 base::DictionaryValue
* tab_info
;
236 ASSERT_TRUE(dict
->GetDictionary("tabInfo", &tab_info
));