Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / net / base / net_log_logger_unittest.cc
blob256ae5917402fff84079ecf68081014f8b1d71ba
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"
16 namespace net {
18 namespace {
20 class NetLogLoggerTest : public testing::Test {
21 public:
22 void SetUp() override {
23 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
24 log_path_ = temp_dir_.path().AppendASCII("NetLogFile");
27 protected:
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");
35 ASSERT_TRUE(file);
36 scoped_ptr<base::Value> constants(GetNetConstants());
37 scoped_ptr<NetLogLogger> logger(new NetLogLogger(file, *constants));
38 logger.reset();
40 std::string input;
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");
57 ASSERT_TRUE(file);
58 scoped_ptr<base::Value> constants(GetNetConstants());
59 NetLogLogger logger(file, *constants);
61 NetLog net_log;
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");
76 ASSERT_TRUE(file);
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,
83 source,
84 NetLog::PHASE_BEGIN,
85 base::TimeTicks::Now(),
86 NULL);
87 NetLog::Entry entry(&entry_data, NetLog::LOG_ALL);
88 logger->OnAddEntry(entry);
89 logger.reset();
91 std::string input;
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");
107 ASSERT_TRUE(file);
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,
114 source,
115 NetLog::PHASE_BEGIN,
116 base::TimeTicks::Now(),
117 NULL);
118 NetLog::Entry entry(&entry_data, NetLog::LOG_ALL);
120 // Add the entry multiple times.
121 logger->OnAddEntry(entry);
122 logger->OnAddEntry(entry);
123 logger.reset();
125 std::string input;
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());
139 } // namespace
141 } // namespace net