Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / net / log / net_log_logger_unittest.cc
blob8a0d64a2866d963ab282e7729498c5129006cfb3
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/net_log_logger.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 NetLogLoggerTest : 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(NetLogLoggerTest, GeneratesValidJSONForNoEvents) {
39 // Create and destroy a logger.
40 base::ScopedFILE file(base::OpenFile(log_path_, "w"));
41 ASSERT_TRUE(file);
42 scoped_ptr<NetLogLogger> logger(new NetLogLogger());
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(NetLogLoggerTest, LogLevel) {
65 base::ScopedFILE file(base::OpenFile(log_path_, "w"));
66 ASSERT_TRUE(file);
67 NetLogLogger logger;
68 logger.StartObserving(&net_log_, file.Pass(), nullptr, nullptr);
69 EXPECT_EQ(NetLog::LOG_STRIP_PRIVATE_DATA, logger.log_level());
70 EXPECT_EQ(NetLog::LOG_STRIP_PRIVATE_DATA, net_log_.GetLogLevel());
71 logger.StopObserving(nullptr);
73 file.reset(base::OpenFile(log_path_, "w"));
74 ASSERT_TRUE(file);
75 logger.set_log_level(NetLog::LOG_ALL_BUT_BYTES);
76 logger.StartObserving(&net_log_, file.Pass(), nullptr, nullptr);
77 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES, logger.log_level());
78 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES, net_log_.GetLogLevel());
79 logger.StopObserving(nullptr);
82 TEST_F(NetLogLoggerTest, GeneratesValidJSONWithOneEvent) {
83 base::ScopedFILE file(base::OpenFile(log_path_, "w"));
84 ASSERT_TRUE(file);
85 scoped_ptr<NetLogLogger> logger(new NetLogLogger());
86 logger->StartObserving(&net_log_, file.Pass(), nullptr, nullptr);
88 const int kDummyId = 1;
89 NetLog::Source source(NetLog::SOURCE_HTTP2_SESSION, kDummyId);
90 NetLog::EntryData entry_data(NetLog::TYPE_PROXY_SERVICE, source,
91 NetLog::PHASE_BEGIN, base::TimeTicks::Now(),
92 NULL);
93 NetLog::Entry entry(&entry_data, NetLog::LOG_ALL);
94 logger->OnAddEntry(entry);
95 logger->StopObserving(nullptr);
96 logger.reset();
98 std::string input;
99 ASSERT_TRUE(base::ReadFileToString(log_path_, &input));
101 base::JSONReader reader;
102 scoped_ptr<base::Value> root(reader.ReadToValue(input));
103 ASSERT_TRUE(root) << reader.GetErrorMessage();
105 base::DictionaryValue* dict;
106 ASSERT_TRUE(root->GetAsDictionary(&dict));
107 base::ListValue* events;
108 ASSERT_TRUE(dict->GetList("events", &events));
109 ASSERT_EQ(1u, events->GetSize());
112 TEST_F(NetLogLoggerTest, GeneratesValidJSONWithMultipleEvents) {
113 base::ScopedFILE file(base::OpenFile(log_path_, "w"));
114 ASSERT_TRUE(file);
115 scoped_ptr<NetLogLogger> logger(new NetLogLogger());
116 logger->StartObserving(&net_log_, file.Pass(), nullptr, nullptr);
118 const int kDummyId = 1;
119 NetLog::Source source(NetLog::SOURCE_HTTP2_SESSION, kDummyId);
120 NetLog::EntryData entry_data(NetLog::TYPE_PROXY_SERVICE, source,
121 NetLog::PHASE_BEGIN, base::TimeTicks::Now(),
122 NULL);
123 NetLog::Entry entry(&entry_data, NetLog::LOG_ALL);
125 // Add the entry multiple times.
126 logger->OnAddEntry(entry);
127 logger->OnAddEntry(entry);
128 logger->StopObserving(nullptr);
129 logger.reset();
131 std::string input;
132 ASSERT_TRUE(base::ReadFileToString(log_path_, &input));
134 base::JSONReader reader;
135 scoped_ptr<base::Value> root(reader.ReadToValue(input));
136 ASSERT_TRUE(root) << reader.GetErrorMessage();
138 base::DictionaryValue* dict;
139 ASSERT_TRUE(root->GetAsDictionary(&dict));
140 base::ListValue* events;
141 ASSERT_TRUE(dict->GetList("events", &events));
142 ASSERT_EQ(2u, events->GetSize());
145 TEST_F(NetLogLoggerTest, CustomConstants) {
146 const char kConstantString[] = "awesome constant";
147 scoped_ptr<base::Value> constants(new base::StringValue(kConstantString));
148 base::ScopedFILE file(base::OpenFile(log_path_, "w"));
149 ASSERT_TRUE(file);
150 scoped_ptr<NetLogLogger> logger(new NetLogLogger());
151 logger->StartObserving(&net_log_, file.Pass(), constants.get(), nullptr);
152 logger->StopObserving(nullptr);
153 logger.reset();
155 std::string input;
156 ASSERT_TRUE(base::ReadFileToString(log_path_, &input));
158 base::JSONReader reader;
159 scoped_ptr<base::Value> root(reader.ReadToValue(input));
160 ASSERT_TRUE(root) << reader.GetErrorMessage();
162 base::DictionaryValue* dict;
163 ASSERT_TRUE(root->GetAsDictionary(&dict));
164 std::string constants_string;
165 ASSERT_TRUE(dict->GetString("constants", &constants_string));
166 ASSERT_EQ(kConstantString, constants_string);
169 TEST_F(NetLogLoggerTest, GeneratesValidJSONWithContext) {
170 // Create context, start a request.
171 TestURLRequestContext context(true);
172 context.set_net_log(&net_log_);
173 context.Init();
175 // Create and destroy a logger.
176 base::ScopedFILE file(base::OpenFile(log_path_, "w"));
177 ASSERT_TRUE(file);
178 scoped_ptr<NetLogLogger> logger(new NetLogLogger());
179 logger->StartObserving(&net_log_, file.Pass(), nullptr, &context);
180 logger->StopObserving(&context);
181 logger.reset();
183 std::string input;
184 ASSERT_TRUE(base::ReadFileToString(log_path_, &input));
186 base::JSONReader reader;
187 scoped_ptr<base::Value> root(reader.ReadToValue(input));
188 ASSERT_TRUE(root) << reader.GetErrorMessage();
190 base::DictionaryValue* dict;
191 ASSERT_TRUE(root->GetAsDictionary(&dict));
192 base::ListValue* events;
193 ASSERT_TRUE(dict->GetList("events", &events));
194 ASSERT_EQ(0u, events->GetSize());
196 // Make sure additional information is present, but don't validate it.
197 base::DictionaryValue* tab_info;
198 ASSERT_TRUE(dict->GetDictionary("tabInfo", &tab_info));
201 TEST_F(NetLogLoggerTest, GeneratesValidJSONWithContextWithActiveRequest) {
202 // Create context, start a request.
203 TestURLRequestContext context(true);
204 context.set_net_log(&net_log_);
205 context.Init();
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));
211 request->Start();
213 // Create and destroy a logger.
214 base::ScopedFILE file(base::OpenFile(log_path_, "w"));
215 ASSERT_TRUE(file);
216 scoped_ptr<NetLogLogger> logger(new NetLogLogger());
217 logger->StartObserving(&net_log_, file.Pass(), nullptr, &context);
218 logger->StopObserving(&context);
219 logger.reset();
221 std::string input;
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));
239 } // namespace
241 } // namespace net