James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "net/base/net_log_logger.h" |
| 6 | |
| 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" |
James Robinson | 7f48021 | 2014-10-31 10:28:08 -0700 | [diff] [blame] | 13 | #include "net/base/net_log_util.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 14 | #include "testing/gtest/include/gtest/gtest.h" |
| 15 | |
| 16 | namespace net { |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | class NetLogLoggerTest : public testing::Test { |
| 21 | public: |
James Robinson | 53b7758 | 2014-10-28 17:00:48 -0700 | [diff] [blame] | 22 | void SetUp() override { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 23 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 24 | log_path_ = temp_dir_.path().AppendASCII("NetLogFile"); |
| 25 | } |
| 26 | |
| 27 | protected: |
| 28 | base::ScopedTempDir temp_dir_; |
| 29 | base::FilePath log_path_; |
| 30 | }; |
| 31 | |
| 32 | TEST_F(NetLogLoggerTest, GeneratesValidJSONForNoEvents) { |
| 33 | // Create and destroy a logger. |
| 34 | FILE* file = base::OpenFile(log_path_, "w"); |
| 35 | ASSERT_TRUE(file); |
James Robinson | 7f48021 | 2014-10-31 10:28:08 -0700 | [diff] [blame] | 36 | scoped_ptr<base::Value> constants(GetNetConstants()); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 37 | scoped_ptr<NetLogLogger> logger(new NetLogLogger(file, *constants)); |
| 38 | logger.reset(); |
| 39 | |
| 40 | std::string input; |
| 41 | ASSERT_TRUE(base::ReadFileToString(log_path_, &input)); |
| 42 | |
| 43 | base::JSONReader reader; |
| 44 | scoped_ptr<base::Value> root(reader.ReadToValue(input)); |
| 45 | ASSERT_TRUE(root) << reader.GetErrorMessage(); |
| 46 | |
| 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()); |
| 52 | } |
| 53 | |
| 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); |
James Robinson | 7f48021 | 2014-10-31 10:28:08 -0700 | [diff] [blame] | 58 | scoped_ptr<base::Value> constants(GetNetConstants()); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 59 | NetLogLogger logger(file, *constants); |
| 60 | |
| 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(); |
| 66 | |
| 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(); |
| 72 | } |
| 73 | |
| 74 | TEST_F(NetLogLoggerTest, GeneratesValidJSONWithOneEvent) { |
| 75 | FILE* file = base::OpenFile(log_path_, "w"); |
| 76 | ASSERT_TRUE(file); |
James Robinson | 7f48021 | 2014-10-31 10:28:08 -0700 | [diff] [blame] | 77 | scoped_ptr<base::Value> constants(GetNetConstants()); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 78 | scoped_ptr<NetLogLogger> logger(new NetLogLogger(file, *constants)); |
| 79 | |
| 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(); |
| 90 | |
| 91 | std::string input; |
| 92 | ASSERT_TRUE(base::ReadFileToString(log_path_, &input)); |
| 93 | |
| 94 | base::JSONReader reader; |
| 95 | scoped_ptr<base::Value> root(reader.ReadToValue(input)); |
| 96 | ASSERT_TRUE(root) << reader.GetErrorMessage(); |
| 97 | |
| 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()); |
| 103 | } |
| 104 | |
| 105 | TEST_F(NetLogLoggerTest, GeneratesValidJSONWithMultipleEvents) { |
| 106 | FILE* file = base::OpenFile(log_path_, "w"); |
| 107 | ASSERT_TRUE(file); |
James Robinson | 7f48021 | 2014-10-31 10:28:08 -0700 | [diff] [blame] | 108 | scoped_ptr<base::Value> constants(GetNetConstants()); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 109 | scoped_ptr<NetLogLogger> logger(new NetLogLogger(file, *constants)); |
| 110 | |
| 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); |
| 119 | |
| 120 | // Add the entry multiple times. |
| 121 | logger->OnAddEntry(entry); |
| 122 | logger->OnAddEntry(entry); |
| 123 | logger.reset(); |
| 124 | |
| 125 | std::string input; |
| 126 | ASSERT_TRUE(base::ReadFileToString(log_path_, &input)); |
| 127 | |
| 128 | base::JSONReader reader; |
| 129 | scoped_ptr<base::Value> root(reader.ReadToValue(input)); |
| 130 | ASSERT_TRUE(root) << reader.GetErrorMessage(); |
| 131 | |
| 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()); |
| 137 | } |
| 138 | |
| 139 | } // namespace |
| 140 | |
| 141 | } // namespace net |