Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS 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 | |
Paul Stewart | 5581d07 | 2012-12-17 17:30:20 -0800 | [diff] [blame] | 5 | #include <base/bind.h> |
| 6 | #include <base/memory/weak_ptr.h> |
| 7 | |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 8 | #include "shill/logging.h" |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 9 | #include "shill/scope_logger.h" |
| 10 | |
| 11 | #include <gmock/gmock.h> |
| 12 | #include <gtest/gtest.h> |
| 13 | |
Paul Stewart | 5581d07 | 2012-12-17 17:30:20 -0800 | [diff] [blame] | 14 | using ::testing::_; |
| 15 | |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 16 | namespace shill { |
| 17 | |
| 18 | class ScopeLoggerTest : public testing::Test { |
| 19 | protected: |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 20 | ScopeLoggerTest() {} |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 21 | |
| 22 | void TearDown() { |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 23 | logger_.set_verbose_level(0); |
| 24 | logger_.DisableAllScopes(); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 25 | } |
| 26 | |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 27 | ScopeLogger logger_; |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | TEST_F(ScopeLoggerTest, DefaultConstruction) { |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 31 | for (int scope = 0; scope < ScopeLogger::kNumScopes; ++scope) { |
| 32 | for (int verbose_level = 0; verbose_level < 5; ++verbose_level) { |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 33 | EXPECT_FALSE(logger_.IsLogEnabled( |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 34 | static_cast<ScopeLogger::Scope>(scope), verbose_level)); |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | TEST_F(ScopeLoggerTest, GetAllScopeNames) { |
Ben Chan | 1c72260 | 2012-04-17 17:37:35 -0700 | [diff] [blame] | 40 | EXPECT_EQ("cellular+" |
| 41 | "connection+" |
| 42 | "crypto+" |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 43 | "daemon+" |
Ben Chan | 1c72260 | 2012-04-17 17:37:35 -0700 | [diff] [blame] | 44 | "dbus+" |
| 45 | "device+" |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 46 | "dhcp+" |
| 47 | "dns+" |
Ben Chan | 1c72260 | 2012-04-17 17:37:35 -0700 | [diff] [blame] | 48 | "ethernet+" |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 49 | "http+" |
| 50 | "httpproxy+" |
Ben Chan | 1c72260 | 2012-04-17 17:37:35 -0700 | [diff] [blame] | 51 | "inet+" |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 52 | "link+" |
Ben Chan | 1c72260 | 2012-04-17 17:37:35 -0700 | [diff] [blame] | 53 | "manager+" |
| 54 | "metrics+" |
| 55 | "modem+" |
| 56 | "portal+" |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 57 | "power+" |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 58 | "ppp+" |
Ben Chan | 1c72260 | 2012-04-17 17:37:35 -0700 | [diff] [blame] | 59 | "profile+" |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 60 | "property+" |
| 61 | "resolver+" |
| 62 | "route+" |
Ben Chan | 1c72260 | 2012-04-17 17:37:35 -0700 | [diff] [blame] | 63 | "rtnl+" |
| 64 | "service+" |
| 65 | "storage+" |
| 66 | "task+" |
| 67 | "vpn+" |
Darin Petkov | 096b347 | 2012-05-15 10:26:22 +0200 | [diff] [blame] | 68 | "wifi+" |
| 69 | "wimax", |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 70 | logger_.GetAllScopeNames()); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | TEST_F(ScopeLoggerTest, GetEnabledScopeNames) { |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 74 | EXPECT_EQ("", logger_.GetEnabledScopeNames()); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 75 | |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 76 | logger_.SetScopeEnabled(ScopeLogger::kWiFi, true); |
| 77 | EXPECT_EQ("wifi", logger_.GetEnabledScopeNames()); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 78 | |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 79 | logger_.SetScopeEnabled(ScopeLogger::kService, true); |
| 80 | EXPECT_EQ("service+wifi", logger_.GetEnabledScopeNames()); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 81 | |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 82 | logger_.SetScopeEnabled(ScopeLogger::kVPN, true); |
| 83 | EXPECT_EQ("service+vpn+wifi", logger_.GetEnabledScopeNames()); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 84 | |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 85 | logger_.SetScopeEnabled(ScopeLogger::kWiFi, false); |
| 86 | EXPECT_EQ("service+vpn", logger_.GetEnabledScopeNames()); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | TEST_F(ScopeLoggerTest, EnableScopesByName) { |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 90 | logger_.EnableScopesByName(""); |
| 91 | EXPECT_EQ("", logger_.GetEnabledScopeNames()); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 92 | |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 93 | logger_.EnableScopesByName("+wifi"); |
| 94 | EXPECT_EQ("wifi", logger_.GetEnabledScopeNames()); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 95 | |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 96 | logger_.EnableScopesByName("+service"); |
| 97 | EXPECT_EQ("service+wifi", logger_.GetEnabledScopeNames()); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 98 | |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 99 | logger_.EnableScopesByName("+vpn+wifi"); |
| 100 | EXPECT_EQ("service+vpn+wifi", logger_.GetEnabledScopeNames()); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 101 | |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 102 | logger_.EnableScopesByName("-wifi"); |
| 103 | EXPECT_EQ("service+vpn", logger_.GetEnabledScopeNames()); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 104 | |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 105 | logger_.EnableScopesByName("-vpn-service+wifi"); |
| 106 | EXPECT_EQ("wifi", logger_.GetEnabledScopeNames()); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 107 | |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 108 | logger_.EnableScopesByName("+-wifi-"); |
| 109 | EXPECT_EQ("", logger_.GetEnabledScopeNames()); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 110 | |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 111 | logger_.EnableScopesByName("-vpn+vpn+wifi-wifi"); |
| 112 | EXPECT_EQ("vpn", logger_.GetEnabledScopeNames()); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 113 | |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 114 | logger_.EnableScopesByName("wifi"); |
| 115 | EXPECT_EQ("wifi", logger_.GetEnabledScopeNames()); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 116 | |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 117 | logger_.EnableScopesByName(""); |
| 118 | EXPECT_EQ("", logger_.GetEnabledScopeNames()); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | TEST_F(ScopeLoggerTest, EnableScopesByNameWithUnknownScopeName) { |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 122 | logger_.EnableScopesByName("foo"); |
| 123 | EXPECT_EQ("", logger_.GetEnabledScopeNames()); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 124 | |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 125 | logger_.EnableScopesByName("wifi+foo+vpn"); |
| 126 | EXPECT_EQ("vpn+wifi", logger_.GetEnabledScopeNames()); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | TEST_F(ScopeLoggerTest, SetScopeEnabled) { |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 130 | EXPECT_FALSE(logger_.IsLogEnabled(ScopeLogger::kService, 0)); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 131 | |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 132 | logger_.SetScopeEnabled(ScopeLogger::kService, true); |
| 133 | EXPECT_TRUE(logger_.IsLogEnabled(ScopeLogger::kService, 0)); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 134 | |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 135 | logger_.SetScopeEnabled(ScopeLogger::kService, false); |
| 136 | EXPECT_FALSE(logger_.IsLogEnabled(ScopeLogger::kService, 0)); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | TEST_F(ScopeLoggerTest, SetVerboseLevel) { |
Christopher Wiley | d34a47c | 2012-08-01 15:42:45 -0700 | [diff] [blame] | 140 | ScopeLogger *logger = ScopeLogger::GetInstance(); |
| 141 | logger->SetScopeEnabled(ScopeLogger::kService, true); |
| 142 | EXPECT_TRUE(logger->IsLogEnabled(ScopeLogger::kService, 0)); |
| 143 | EXPECT_FALSE(logger->IsLogEnabled(ScopeLogger::kService, 1)); |
| 144 | EXPECT_FALSE(logger->IsLogEnabled(ScopeLogger::kService, 2)); |
| 145 | EXPECT_TRUE(SLOG_IS_ON(Service, 0)); |
| 146 | EXPECT_FALSE(SLOG_IS_ON(Service, 1)); |
| 147 | EXPECT_FALSE(SLOG_IS_ON(Service, 2)); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 148 | |
Christopher Wiley | d34a47c | 2012-08-01 15:42:45 -0700 | [diff] [blame] | 149 | logger->set_verbose_level(1); |
| 150 | EXPECT_TRUE(logger->IsLogEnabled(ScopeLogger::kService, 0)); |
| 151 | EXPECT_TRUE(logger->IsLogEnabled(ScopeLogger::kService, 1)); |
| 152 | EXPECT_FALSE(logger->IsLogEnabled(ScopeLogger::kService, 2)); |
| 153 | EXPECT_TRUE(SLOG_IS_ON(Service, 0)); |
| 154 | EXPECT_TRUE(SLOG_IS_ON(Service, 1)); |
| 155 | EXPECT_FALSE(SLOG_IS_ON(Service, 2)); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 156 | |
Christopher Wiley | d34a47c | 2012-08-01 15:42:45 -0700 | [diff] [blame] | 157 | logger->set_verbose_level(2); |
| 158 | EXPECT_TRUE(logger->IsLogEnabled(ScopeLogger::kService, 0)); |
| 159 | EXPECT_TRUE(logger->IsLogEnabled(ScopeLogger::kService, 1)); |
| 160 | EXPECT_TRUE(logger->IsLogEnabled(ScopeLogger::kService, 2)); |
| 161 | EXPECT_TRUE(SLOG_IS_ON(Service, 0)); |
| 162 | EXPECT_TRUE(SLOG_IS_ON(Service, 1)); |
| 163 | EXPECT_TRUE(SLOG_IS_ON(Service, 2)); |
| 164 | |
| 165 | logger->set_verbose_level(0); |
| 166 | logger->SetScopeEnabled(ScopeLogger::kService, false); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Paul Stewart | 5581d07 | 2012-12-17 17:30:20 -0800 | [diff] [blame] | 169 | class ScopeChangeTarget { |
| 170 | public: |
| 171 | ScopeChangeTarget() : weak_ptr_factory_(this) {} |
| 172 | virtual ~ScopeChangeTarget() {} |
| 173 | MOCK_METHOD1(Callback, void(bool enabled)); |
| 174 | ScopeLogger::ScopeEnableChangedCallback GetCallback() { |
| 175 | return base::Bind( |
| 176 | &ScopeChangeTarget::Callback, weak_ptr_factory_.GetWeakPtr()); |
| 177 | } |
| 178 | |
| 179 | private: |
| 180 | base::WeakPtrFactory<ScopeChangeTarget> weak_ptr_factory_; |
| 181 | }; |
| 182 | |
| 183 | TEST_F(ScopeLoggerTest, LogScopeCallback) { |
| 184 | ScopeChangeTarget target0; |
| 185 | logger_.RegisterScopeEnableChangedCallback( |
| 186 | ScopeLogger::kWiFi, target0.GetCallback()); |
| 187 | EXPECT_CALL(target0, Callback(_)).Times(0); |
| 188 | // Call for a scope other than registered-for. |
| 189 | logger_.EnableScopesByName("+vpn"); |
| 190 | // Change to the same value as default. |
| 191 | logger_.EnableScopesByName("-wifi"); |
| 192 | testing::Mock::VerifyAndClearExpectations(&target0); |
| 193 | |
| 194 | EXPECT_CALL(target0, Callback(true)).Times(1); |
| 195 | logger_.EnableScopesByName("+wifi"); |
| 196 | testing::Mock::VerifyAndClearExpectations(&target0); |
| 197 | |
| 198 | EXPECT_CALL(target0, Callback(false)).Times(1); |
| 199 | logger_.EnableScopesByName(""); |
| 200 | testing::Mock::VerifyAndClearExpectations(&target0); |
| 201 | |
| 202 | // Change to the same value as last set. |
| 203 | EXPECT_CALL(target0, Callback(_)).Times(0); |
| 204 | logger_.EnableScopesByName("-wifi"); |
| 205 | testing::Mock::VerifyAndClearExpectations(&target0); |
| 206 | |
| 207 | ScopeChangeTarget target1; |
| 208 | logger_.RegisterScopeEnableChangedCallback( |
| 209 | ScopeLogger::kWiFi, target1.GetCallback()); |
| 210 | EXPECT_CALL(target0, Callback(true)).Times(1); |
| 211 | EXPECT_CALL(target1, Callback(true)).Times(1); |
| 212 | logger_.EnableScopesByName("+wifi"); |
| 213 | } |
| 214 | |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 215 | } // namespace shill |