| 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 "net/base/net_log.h" |
| 13 #include "net/base/net_log_util.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 15 |
| 15 namespace net { | 16 namespace net { |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 class NetLogLoggerTest : public testing::Test { | 20 class NetLogLoggerTest : public testing::Test { |
| 20 public: | 21 public: |
| 21 virtual void SetUp() { | 22 virtual void SetUp() { |
| 22 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 23 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 23 log_path_ = temp_dir_.path().AppendASCII("NetLogFile"); | 24 log_path_ = temp_dir_.path().AppendASCII("NetLogFile"); |
| 24 } | 25 } |
| 25 | 26 |
| 26 protected: | 27 protected: |
| 27 base::ScopedTempDir temp_dir_; | 28 base::ScopedTempDir temp_dir_; |
| 28 base::FilePath log_path_; | 29 base::FilePath log_path_; |
| 29 }; | 30 }; |
| 30 | 31 |
| 31 TEST_F(NetLogLoggerTest, GeneratesValidJSONForNoEvents) { | 32 TEST_F(NetLogLoggerTest, GeneratesValidJSONForNoEvents) { |
| 32 // Create and destroy a logger. | 33 // Create and destroy a logger. |
| 33 FILE* file = base::OpenFile(log_path_, "w"); | 34 FILE* file = base::OpenFile(log_path_, "w"); |
| 34 ASSERT_TRUE(file); | 35 ASSERT_TRUE(file); |
| 35 scoped_ptr<base::Value> constants(NetLogLogger::GetConstants()); | 36 scoped_ptr<base::Value> constants(GetNetConstants()); |
| 36 scoped_ptr<NetLogLogger> logger(new NetLogLogger(file, *constants)); | 37 scoped_ptr<NetLogLogger> logger(new NetLogLogger(file, *constants)); |
| 37 logger.reset(); | 38 logger.reset(); |
| 38 | 39 |
| 39 std::string input; | 40 std::string input; |
| 40 ASSERT_TRUE(base::ReadFileToString(log_path_, &input)); | 41 ASSERT_TRUE(base::ReadFileToString(log_path_, &input)); |
| 41 | 42 |
| 42 base::JSONReader reader; | 43 base::JSONReader reader; |
| 43 scoped_ptr<base::Value> root(reader.ReadToValue(input)); | 44 scoped_ptr<base::Value> root(reader.ReadToValue(input)); |
| 44 ASSERT_TRUE(root) << reader.GetErrorMessage(); | 45 ASSERT_TRUE(root) << reader.GetErrorMessage(); |
| 45 | 46 |
| 46 base::DictionaryValue* dict; | 47 base::DictionaryValue* dict; |
| 47 ASSERT_TRUE(root->GetAsDictionary(&dict)); | 48 ASSERT_TRUE(root->GetAsDictionary(&dict)); |
| 48 base::ListValue* events; | 49 base::ListValue* events; |
| 49 ASSERT_TRUE(dict->GetList("events", &events)); | 50 ASSERT_TRUE(dict->GetList("events", &events)); |
| 50 ASSERT_EQ(0u, events->GetSize()); | 51 ASSERT_EQ(0u, events->GetSize()); |
| 51 } | 52 } |
| 52 | 53 |
| 53 // Make sure the log level is LOG_STRIP_PRIVATE_DATA by default. | 54 // Make sure the log level is LOG_STRIP_PRIVATE_DATA by default. |
| 54 TEST_F(NetLogLoggerTest, LogLevel) { | 55 TEST_F(NetLogLoggerTest, LogLevel) { |
| 55 FILE* file = base::OpenFile(log_path_, "w"); | 56 FILE* file = base::OpenFile(log_path_, "w"); |
| 56 ASSERT_TRUE(file); | 57 ASSERT_TRUE(file); |
| 57 scoped_ptr<base::Value> constants(NetLogLogger::GetConstants()); | 58 scoped_ptr<base::Value> constants(GetNetConstants()); |
| 58 NetLogLogger logger(file, *constants); | 59 NetLogLogger logger(file, *constants); |
| 59 | 60 |
| 60 NetLog net_log; | 61 NetLog net_log; |
| 61 logger.StartObserving(&net_log); | 62 logger.StartObserving(&net_log); |
| 62 EXPECT_EQ(NetLog::LOG_STRIP_PRIVATE_DATA, logger.log_level()); | 63 EXPECT_EQ(NetLog::LOG_STRIP_PRIVATE_DATA, logger.log_level()); |
| 63 EXPECT_EQ(NetLog::LOG_STRIP_PRIVATE_DATA, net_log.GetLogLevel()); | 64 EXPECT_EQ(NetLog::LOG_STRIP_PRIVATE_DATA, net_log.GetLogLevel()); |
| 64 logger.StopObserving(); | 65 logger.StopObserving(); |
| 65 | 66 |
| 66 logger.set_log_level(NetLog::LOG_ALL_BUT_BYTES); | 67 logger.set_log_level(NetLog::LOG_ALL_BUT_BYTES); |
| 67 logger.StartObserving(&net_log); | 68 logger.StartObserving(&net_log); |
| 68 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES, logger.log_level()); | 69 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES, logger.log_level()); |
| 69 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES, net_log.GetLogLevel()); | 70 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES, net_log.GetLogLevel()); |
| 70 logger.StopObserving(); | 71 logger.StopObserving(); |
| 71 } | 72 } |
| 72 | 73 |
| 73 TEST_F(NetLogLoggerTest, GeneratesValidJSONWithOneEvent) { | 74 TEST_F(NetLogLoggerTest, GeneratesValidJSONWithOneEvent) { |
| 74 FILE* file = base::OpenFile(log_path_, "w"); | 75 FILE* file = base::OpenFile(log_path_, "w"); |
| 75 ASSERT_TRUE(file); | 76 ASSERT_TRUE(file); |
| 76 scoped_ptr<base::Value> constants(NetLogLogger::GetConstants()); | 77 scoped_ptr<base::Value> constants(GetNetConstants()); |
| 77 scoped_ptr<NetLogLogger> logger(new NetLogLogger(file, *constants)); | 78 scoped_ptr<NetLogLogger> logger(new NetLogLogger(file, *constants)); |
| 78 | 79 |
| 79 const int kDummyId = 1; | 80 const int kDummyId = 1; |
| 80 NetLog::Source source(NetLog::SOURCE_SPDY_SESSION, kDummyId); | 81 NetLog::Source source(NetLog::SOURCE_SPDY_SESSION, kDummyId); |
| 81 NetLog::EntryData entry_data(NetLog::TYPE_PROXY_SERVICE, | 82 NetLog::EntryData entry_data(NetLog::TYPE_PROXY_SERVICE, |
| 82 source, | 83 source, |
| 83 NetLog::PHASE_BEGIN, | 84 NetLog::PHASE_BEGIN, |
| 84 base::TimeTicks::Now(), | 85 base::TimeTicks::Now(), |
| 85 NULL); | 86 NULL); |
| 86 NetLog::Entry entry(&entry_data, NetLog::LOG_ALL); | 87 NetLog::Entry entry(&entry_data, NetLog::LOG_ALL); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 97 base::DictionaryValue* dict; | 98 base::DictionaryValue* dict; |
| 98 ASSERT_TRUE(root->GetAsDictionary(&dict)); | 99 ASSERT_TRUE(root->GetAsDictionary(&dict)); |
| 99 base::ListValue* events; | 100 base::ListValue* events; |
| 100 ASSERT_TRUE(dict->GetList("events", &events)); | 101 ASSERT_TRUE(dict->GetList("events", &events)); |
| 101 ASSERT_EQ(1u, events->GetSize()); | 102 ASSERT_EQ(1u, events->GetSize()); |
| 102 } | 103 } |
| 103 | 104 |
| 104 TEST_F(NetLogLoggerTest, GeneratesValidJSONWithMultipleEvents) { | 105 TEST_F(NetLogLoggerTest, GeneratesValidJSONWithMultipleEvents) { |
| 105 FILE* file = base::OpenFile(log_path_, "w"); | 106 FILE* file = base::OpenFile(log_path_, "w"); |
| 106 ASSERT_TRUE(file); | 107 ASSERT_TRUE(file); |
| 107 scoped_ptr<base::Value> constants(NetLogLogger::GetConstants()); | 108 scoped_ptr<base::Value> constants(GetNetConstants()); |
| 108 scoped_ptr<NetLogLogger> logger(new NetLogLogger(file, *constants)); | 109 scoped_ptr<NetLogLogger> logger(new NetLogLogger(file, *constants)); |
| 109 | 110 |
| 110 const int kDummyId = 1; | 111 const int kDummyId = 1; |
| 111 NetLog::Source source(NetLog::SOURCE_SPDY_SESSION, kDummyId); | 112 NetLog::Source source(NetLog::SOURCE_SPDY_SESSION, kDummyId); |
| 112 NetLog::EntryData entry_data(NetLog::TYPE_PROXY_SERVICE, | 113 NetLog::EntryData entry_data(NetLog::TYPE_PROXY_SERVICE, |
| 113 source, | 114 source, |
| 114 NetLog::PHASE_BEGIN, | 115 NetLog::PHASE_BEGIN, |
| 115 base::TimeTicks::Now(), | 116 base::TimeTicks::Now(), |
| 116 NULL); | 117 NULL); |
| 117 NetLog::Entry entry(&entry_data, NetLog::LOG_ALL); | 118 NetLog::Entry entry(&entry_data, NetLog::LOG_ALL); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 131 base::DictionaryValue* dict; | 132 base::DictionaryValue* dict; |
| 132 ASSERT_TRUE(root->GetAsDictionary(&dict)); | 133 ASSERT_TRUE(root->GetAsDictionary(&dict)); |
| 133 base::ListValue* events; | 134 base::ListValue* events; |
| 134 ASSERT_TRUE(dict->GetList("events", &events)); | 135 ASSERT_TRUE(dict->GetList("events", &events)); |
| 135 ASSERT_EQ(2u, events->GetSize()); | 136 ASSERT_EQ(2u, events->GetSize()); |
| 136 } | 137 } |
| 137 | 138 |
| 138 } // namespace | 139 } // namespace |
| 139 | 140 |
| 140 } // namespace net | 141 } // namespace net |
| OLD | NEW |