Make GetSecurityLevel available from the java ToolbarModel.
[chromium-blink-merge.git] / net / base / net_log_logger_unittest.cc
blob2f63b8d75a5f7caf8307e35e5bd0e27bc9e0a0ee
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"
15 namespace net {
17 namespace {
19 class NetLogLoggerTest : public testing::Test {
20 public:
21 virtual void SetUp() {
22 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
23 log_path_ = temp_dir_.path().AppendASCII("NetLogFile");
26 protected:
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");
34 ASSERT_TRUE(file);
35 scoped_ptr<base::Value> constants(NetLogLogger::GetConstants());
36 scoped_ptr<NetLogLogger> logger(new NetLogLogger(file, *constants));
37 logger.reset();
39 std::string input;
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");
56 ASSERT_TRUE(file);
57 scoped_ptr<base::Value> constants(NetLogLogger::GetConstants());
58 NetLogLogger logger(file, *constants);
60 NetLog net_log;
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");
75 ASSERT_TRUE(file);
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,
82 source,
83 NetLog::PHASE_BEGIN,
84 base::TimeTicks::Now(),
85 NULL);
86 NetLog::Entry entry(&entry_data, NetLog::LOG_ALL);
87 logger->OnAddEntry(entry);
88 logger.reset();
90 std::string input;
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");
106 ASSERT_TRUE(file);
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,
113 source,
114 NetLog::PHASE_BEGIN,
115 base::TimeTicks::Now(),
116 NULL);
117 NetLog::Entry entry(&entry_data, NetLog::LOG_ALL);
119 // Add the entry multiple times.
120 logger->OnAddEntry(entry);
121 logger->OnAddEntry(entry);
122 logger.reset();
124 std::string input;
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());
138 } // namespace
140 } // namespace net