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/base/net_log_logger.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/json/json_reader.h"
11 #include "base/values.h"
12 #include "net/base/net_log.h"
13 #include "net/base/net_log_util.h"
14 #include "testing/gtest/include/gtest/gtest.h"
20 class NetLogLoggerTest
: public testing::Test
{
22 void SetUp() override
{
23 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
24 log_path_
= temp_dir_
.path().AppendASCII("NetLogFile");
28 base::ScopedTempDir temp_dir_
;
29 base::FilePath log_path_
;
32 TEST_F(NetLogLoggerTest
, GeneratesValidJSONForNoEvents
) {
33 // Create and destroy a logger.
34 FILE* file
= base::OpenFile(log_path_
, "w");
36 scoped_ptr
<base::Value
> constants(GetNetConstants());
37 scoped_ptr
<NetLogLogger
> logger(new NetLogLogger(file
, *constants
));
41 ASSERT_TRUE(base::ReadFileToString(log_path_
, &input
));
43 base::JSONReader reader
;
44 scoped_ptr
<base::Value
> root(reader
.ReadToValue(input
));
45 ASSERT_TRUE(root
) << reader
.GetErrorMessage();
47 base::DictionaryValue
* dict
;
48 ASSERT_TRUE(root
->GetAsDictionary(&dict
));
49 base::ListValue
* events
;
50 ASSERT_TRUE(dict
->GetList("events", &events
));
51 ASSERT_EQ(0u, events
->GetSize());
54 // Make sure the log level is LOG_STRIP_PRIVATE_DATA by default.
55 TEST_F(NetLogLoggerTest
, LogLevel
) {
56 FILE* file
= base::OpenFile(log_path_
, "w");
58 scoped_ptr
<base::Value
> constants(GetNetConstants());
59 NetLogLogger
logger(file
, *constants
);
62 logger
.StartObserving(&net_log
);
63 EXPECT_EQ(NetLog::LOG_STRIP_PRIVATE_DATA
, logger
.log_level());
64 EXPECT_EQ(NetLog::LOG_STRIP_PRIVATE_DATA
, net_log
.GetLogLevel());
65 logger
.StopObserving();
67 logger
.set_log_level(NetLog::LOG_ALL_BUT_BYTES
);
68 logger
.StartObserving(&net_log
);
69 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES
, logger
.log_level());
70 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES
, net_log
.GetLogLevel());
71 logger
.StopObserving();
74 TEST_F(NetLogLoggerTest
, GeneratesValidJSONWithOneEvent
) {
75 FILE* file
= base::OpenFile(log_path_
, "w");
77 scoped_ptr
<base::Value
> constants(GetNetConstants());
78 scoped_ptr
<NetLogLogger
> logger(new NetLogLogger(file
, *constants
));
80 const int kDummyId
= 1;
81 NetLog::Source
source(NetLog::SOURCE_SPDY_SESSION
, kDummyId
);
82 NetLog::EntryData
entry_data(NetLog::TYPE_PROXY_SERVICE
,
85 base::TimeTicks::Now(),
87 NetLog::Entry
entry(&entry_data
, NetLog::LOG_ALL
);
88 logger
->OnAddEntry(entry
);
92 ASSERT_TRUE(base::ReadFileToString(log_path_
, &input
));
94 base::JSONReader reader
;
95 scoped_ptr
<base::Value
> root(reader
.ReadToValue(input
));
96 ASSERT_TRUE(root
) << reader
.GetErrorMessage();
98 base::DictionaryValue
* dict
;
99 ASSERT_TRUE(root
->GetAsDictionary(&dict
));
100 base::ListValue
* events
;
101 ASSERT_TRUE(dict
->GetList("events", &events
));
102 ASSERT_EQ(1u, events
->GetSize());
105 TEST_F(NetLogLoggerTest
, GeneratesValidJSONWithMultipleEvents
) {
106 FILE* file
= base::OpenFile(log_path_
, "w");
108 scoped_ptr
<base::Value
> constants(GetNetConstants());
109 scoped_ptr
<NetLogLogger
> logger(new NetLogLogger(file
, *constants
));
111 const int kDummyId
= 1;
112 NetLog::Source
source(NetLog::SOURCE_SPDY_SESSION
, kDummyId
);
113 NetLog::EntryData
entry_data(NetLog::TYPE_PROXY_SERVICE
,
116 base::TimeTicks::Now(),
118 NetLog::Entry
entry(&entry_data
, NetLog::LOG_ALL
);
120 // Add the entry multiple times.
121 logger
->OnAddEntry(entry
);
122 logger
->OnAddEntry(entry
);
126 ASSERT_TRUE(base::ReadFileToString(log_path_
, &input
));
128 base::JSONReader reader
;
129 scoped_ptr
<base::Value
> root(reader
.ReadToValue(input
));
130 ASSERT_TRUE(root
) << reader
.GetErrorMessage();
132 base::DictionaryValue
* dict
;
133 ASSERT_TRUE(root
->GetAsDictionary(&dict
));
134 base::ListValue
* events
;
135 ASSERT_TRUE(dict
->GetList("events", &events
));
136 ASSERT_EQ(2u, events
->GetSize());