OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/base/net_log_logger.h" | 5 #include "net/base/net_log_logger.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "net/base/net_log.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
13 | 14 |
14 namespace net { | 15 namespace net { |
15 | 16 |
17 namespace { | |
davidben
2014/09/30 17:33:50
I've always been unclear on whether tests should b
| |
18 | |
16 class NetLogLoggerTest : public testing::Test { | 19 class NetLogLoggerTest : public testing::Test { |
17 public: | 20 public: |
18 virtual void SetUp() { | 21 virtual void SetUp() { |
19 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 22 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
20 log_path_ = temp_dir_.path().AppendASCII("NetLogFile"); | 23 log_path_ = temp_dir_.path().AppendASCII("NetLogFile"); |
21 } | 24 } |
22 | 25 |
23 protected: | 26 protected: |
24 base::ScopedTempDir temp_dir_; | 27 base::ScopedTempDir temp_dir_; |
25 base::FilePath log_path_; | 28 base::FilePath log_path_; |
26 }; | 29 }; |
27 | 30 |
28 TEST_F(NetLogLoggerTest, GeneratesValidJSONForNoEvents) { | 31 TEST_F(NetLogLoggerTest, GeneratesValidJSONForNoEvents) { |
29 { | 32 // Create and destroy a logger. |
30 // Create and destroy a logger. | 33 FILE* file = base::OpenFile(log_path_, "w"); |
31 FILE* file = base::OpenFile(log_path_, "w"); | 34 ASSERT_TRUE(file); |
32 ASSERT_TRUE(file); | 35 scoped_ptr<base::Value> constants(NetLogLogger::GetConstants()); |
33 scoped_ptr<base::Value> constants(NetLogLogger::GetConstants()); | 36 scoped_ptr<NetLogLogger> logger(new NetLogLogger(file, *constants)); |
34 NetLogLogger logger(file, *constants); | 37 logger.reset(); |
35 } | |
36 | 38 |
37 std::string input; | 39 std::string input; |
38 ASSERT_TRUE(base::ReadFileToString(log_path_, &input)); | 40 ASSERT_TRUE(base::ReadFileToString(log_path_, &input)); |
39 | 41 |
40 base::JSONReader reader; | 42 base::JSONReader reader; |
41 scoped_ptr<base::Value> root(reader.ReadToValue(input)); | 43 scoped_ptr<base::Value> root(reader.ReadToValue(input)); |
42 ASSERT_TRUE(root) << reader.GetErrorMessage(); | 44 ASSERT_TRUE(root) << reader.GetErrorMessage(); |
43 | 45 |
44 base::DictionaryValue* dict; | 46 base::DictionaryValue* dict; |
45 ASSERT_TRUE(root->GetAsDictionary(&dict)); | 47 ASSERT_TRUE(root->GetAsDictionary(&dict)); |
46 base::ListValue* events; | 48 base::ListValue* events; |
47 ASSERT_TRUE(dict->GetList("events", &events)); | 49 ASSERT_TRUE(dict->GetList("events", &events)); |
48 ASSERT_EQ(0u, events->GetSize()); | 50 ASSERT_EQ(0u, events->GetSize()); |
49 } | 51 } |
50 | 52 |
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); | |
59 | |
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(); | |
65 | |
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(); | |
71 } | |
72 | |
51 TEST_F(NetLogLoggerTest, GeneratesValidJSONWithOneEvent) { | 73 TEST_F(NetLogLoggerTest, GeneratesValidJSONWithOneEvent) { |
52 { | 74 FILE* file = base::OpenFile(log_path_, "w"); |
53 FILE* file = base::OpenFile(log_path_, "w"); | 75 ASSERT_TRUE(file); |
54 ASSERT_TRUE(file); | 76 scoped_ptr<base::Value> constants(NetLogLogger::GetConstants()); |
55 scoped_ptr<base::Value> constants(NetLogLogger::GetConstants()); | 77 scoped_ptr<NetLogLogger> logger(new NetLogLogger(file, *constants)); |
56 NetLogLogger logger(file, *constants); | |
57 | 78 |
58 const int kDummyId = 1; | 79 const int kDummyId = 1; |
59 NetLog::Source source(NetLog::SOURCE_SPDY_SESSION, kDummyId); | 80 NetLog::Source source(NetLog::SOURCE_SPDY_SESSION, kDummyId); |
60 NetLog::EntryData entry_data(NetLog::TYPE_PROXY_SERVICE, | 81 NetLog::EntryData entry_data(NetLog::TYPE_PROXY_SERVICE, |
61 source, | 82 source, |
62 NetLog::PHASE_BEGIN, | 83 NetLog::PHASE_BEGIN, |
63 base::TimeTicks::Now(), | 84 base::TimeTicks::Now(), |
64 NULL); | 85 NULL); |
65 NetLog::Entry entry(&entry_data, NetLog::LOG_ALL); | 86 NetLog::Entry entry(&entry_data, NetLog::LOG_ALL); |
66 logger.OnAddEntry(entry); | 87 logger->OnAddEntry(entry); |
67 } | 88 logger.reset(); |
68 | 89 |
69 std::string input; | 90 std::string input; |
70 ASSERT_TRUE(base::ReadFileToString(log_path_, &input)); | 91 ASSERT_TRUE(base::ReadFileToString(log_path_, &input)); |
71 | 92 |
72 base::JSONReader reader; | 93 base::JSONReader reader; |
73 scoped_ptr<base::Value> root(reader.ReadToValue(input)); | 94 scoped_ptr<base::Value> root(reader.ReadToValue(input)); |
74 ASSERT_TRUE(root) << reader.GetErrorMessage(); | 95 ASSERT_TRUE(root) << reader.GetErrorMessage(); |
75 | 96 |
76 base::DictionaryValue* dict; | 97 base::DictionaryValue* dict; |
77 ASSERT_TRUE(root->GetAsDictionary(&dict)); | 98 ASSERT_TRUE(root->GetAsDictionary(&dict)); |
78 base::ListValue* events; | 99 base::ListValue* events; |
79 ASSERT_TRUE(dict->GetList("events", &events)); | 100 ASSERT_TRUE(dict->GetList("events", &events)); |
80 ASSERT_EQ(1u, events->GetSize()); | 101 ASSERT_EQ(1u, events->GetSize()); |
81 } | 102 } |
82 | 103 |
83 TEST_F(NetLogLoggerTest, GeneratesValidJSONWithMultipleEvents) { | 104 TEST_F(NetLogLoggerTest, GeneratesValidJSONWithMultipleEvents) { |
84 { | 105 FILE* file = base::OpenFile(log_path_, "w"); |
85 FILE* file = base::OpenFile(log_path_, "w"); | 106 ASSERT_TRUE(file); |
86 ASSERT_TRUE(file); | 107 scoped_ptr<base::Value> constants(NetLogLogger::GetConstants()); |
87 scoped_ptr<base::Value> constants(NetLogLogger::GetConstants()); | 108 scoped_ptr<NetLogLogger> logger(new NetLogLogger(file, *constants)); |
88 NetLogLogger logger(file, *constants); | |
89 | 109 |
90 const int kDummyId = 1; | 110 const int kDummyId = 1; |
91 NetLog::Source source(NetLog::SOURCE_SPDY_SESSION, kDummyId); | 111 NetLog::Source source(NetLog::SOURCE_SPDY_SESSION, kDummyId); |
92 NetLog::EntryData entry_data(NetLog::TYPE_PROXY_SERVICE, | 112 NetLog::EntryData entry_data(NetLog::TYPE_PROXY_SERVICE, |
93 source, | 113 source, |
94 NetLog::PHASE_BEGIN, | 114 NetLog::PHASE_BEGIN, |
95 base::TimeTicks::Now(), | 115 base::TimeTicks::Now(), |
96 NULL); | 116 NULL); |
97 NetLog::Entry entry(&entry_data, NetLog::LOG_ALL); | 117 NetLog::Entry entry(&entry_data, NetLog::LOG_ALL); |
98 | 118 |
99 // Add the entry multiple times. | 119 // Add the entry multiple times. |
100 logger.OnAddEntry(entry); | 120 logger->OnAddEntry(entry); |
101 logger.OnAddEntry(entry); | 121 logger->OnAddEntry(entry); |
102 } | 122 logger.reset(); |
103 | 123 |
104 std::string input; | 124 std::string input; |
105 ASSERT_TRUE(base::ReadFileToString(log_path_, &input)); | 125 ASSERT_TRUE(base::ReadFileToString(log_path_, &input)); |
106 | 126 |
107 base::JSONReader reader; | 127 base::JSONReader reader; |
108 scoped_ptr<base::Value> root(reader.ReadToValue(input)); | 128 scoped_ptr<base::Value> root(reader.ReadToValue(input)); |
109 ASSERT_TRUE(root) << reader.GetErrorMessage(); | 129 ASSERT_TRUE(root) << reader.GetErrorMessage(); |
110 | 130 |
111 base::DictionaryValue* dict; | 131 base::DictionaryValue* dict; |
112 ASSERT_TRUE(root->GetAsDictionary(&dict)); | 132 ASSERT_TRUE(root->GetAsDictionary(&dict)); |
113 base::ListValue* events; | 133 base::ListValue* events; |
114 ASSERT_TRUE(dict->GetList("events", &events)); | 134 ASSERT_TRUE(dict->GetList("events", &events)); |
115 ASSERT_EQ(2u, events->GetSize()); | 135 ASSERT_EQ(2u, events->GetSize()); |
116 } | 136 } |
117 | 137 |
138 } // namespace | |
139 | |
118 } // namespace net | 140 } // namespace net |
OLD | NEW |