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 "testing/gtest/include/gtest/gtest.h"
19 class NetLogLoggerTest
: public testing::Test
{
21 virtual void SetUp() {
22 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
23 log_path_
= temp_dir_
.path().AppendASCII("NetLogFile");
27 base::ScopedTempDir temp_dir_
;
28 base::FilePath log_path_
;
31 TEST_F(NetLogLoggerTest
, GeneratesValidJSONForNoEvents
) {
32 // Create and destroy a logger.
33 FILE* file
= base::OpenFile(log_path_
, "w");
35 scoped_ptr
<base::Value
> constants(NetLogLogger::GetConstants());
36 scoped_ptr
<NetLogLogger
> logger(new NetLogLogger(file
, *constants
));
40 ASSERT_TRUE(base::ReadFileToString(log_path_
, &input
));
42 base::JSONReader reader
;
43 scoped_ptr
<base::Value
> root(reader
.ReadToValue(input
));
44 ASSERT_TRUE(root
) << reader
.GetErrorMessage();
46 base::DictionaryValue
* dict
;
47 ASSERT_TRUE(root
->GetAsDictionary(&dict
));
48 base::ListValue
* events
;
49 ASSERT_TRUE(dict
->GetList("events", &events
));
50 ASSERT_EQ(0u, events
->GetSize());
53 // Make sure the log level is LOG_STRIP_PRIVATE_DATA by default.
54 TEST_F(NetLogLoggerTest
, LogLevel
) {
55 FILE* file
= base::OpenFile(log_path_
, "w");
57 scoped_ptr
<base::Value
> constants(NetLogLogger::GetConstants());
58 NetLogLogger
logger(file
, *constants
);
61 logger
.StartObserving(&net_log
);
62 EXPECT_EQ(NetLog::LOG_STRIP_PRIVATE_DATA
, logger
.log_level());
63 EXPECT_EQ(NetLog::LOG_STRIP_PRIVATE_DATA
, net_log
.GetLogLevel());
64 logger
.StopObserving();
66 logger
.set_log_level(NetLog::LOG_ALL_BUT_BYTES
);
67 logger
.StartObserving(&net_log
);
68 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES
, logger
.log_level());
69 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES
, net_log
.GetLogLevel());
70 logger
.StopObserving();
73 TEST_F(NetLogLoggerTest
, GeneratesValidJSONWithOneEvent
) {
74 FILE* file
= base::OpenFile(log_path_
, "w");
76 scoped_ptr
<base::Value
> constants(NetLogLogger::GetConstants());
77 scoped_ptr
<NetLogLogger
> logger(new NetLogLogger(file
, *constants
));
79 const int kDummyId
= 1;
80 NetLog::Source
source(NetLog::SOURCE_SPDY_SESSION
, kDummyId
);
81 NetLog::EntryData
entry_data(NetLog::TYPE_PROXY_SERVICE
,
84 base::TimeTicks::Now(),
86 NetLog::Entry
entry(&entry_data
, NetLog::LOG_ALL
);
87 logger
->OnAddEntry(entry
);
91 ASSERT_TRUE(base::ReadFileToString(log_path_
, &input
));
93 base::JSONReader reader
;
94 scoped_ptr
<base::Value
> root(reader
.ReadToValue(input
));
95 ASSERT_TRUE(root
) << reader
.GetErrorMessage();
97 base::DictionaryValue
* dict
;
98 ASSERT_TRUE(root
->GetAsDictionary(&dict
));
99 base::ListValue
* events
;
100 ASSERT_TRUE(dict
->GetList("events", &events
));
101 ASSERT_EQ(1u, events
->GetSize());
104 TEST_F(NetLogLoggerTest
, GeneratesValidJSONWithMultipleEvents
) {
105 FILE* file
= base::OpenFile(log_path_
, "w");
107 scoped_ptr
<base::Value
> constants(NetLogLogger::GetConstants());
108 scoped_ptr
<NetLogLogger
> logger(new NetLogLogger(file
, *constants
));
110 const int kDummyId
= 1;
111 NetLog::Source
source(NetLog::SOURCE_SPDY_SESSION
, kDummyId
);
112 NetLog::EntryData
entry_data(NetLog::TYPE_PROXY_SERVICE
,
115 base::TimeTicks::Now(),
117 NetLog::Entry
entry(&entry_data
, NetLog::LOG_ALL
);
119 // Add the entry multiple times.
120 logger
->OnAddEntry(entry
);
121 logger
->OnAddEntry(entry
);
125 ASSERT_TRUE(base::ReadFileToString(log_path_
, &input
));
127 base::JSONReader reader
;
128 scoped_ptr
<base::Value
> root(reader
.ReadToValue(input
));
129 ASSERT_TRUE(root
) << reader
.GetErrorMessage();
131 base::DictionaryValue
* dict
;
132 ASSERT_TRUE(root
->GetAsDictionary(&dict
));
133 base::ListValue
* events
;
134 ASSERT_TRUE(dict
->GetList("events", &events
));
135 ASSERT_EQ(2u, events
->GetSize());