1 // 2 // Copyright (C) 2012 The Android Open Source Project 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 #include "shill/scope_logger.h" 18 19 #include <base/bind.h> 20 #include <base/memory/weak_ptr.h> 21 22 #include "shill/logging.h" 23 24 #include <gmock/gmock.h> 25 #include <gtest/gtest.h> 26 27 using ::testing::_; 28 29 namespace shill { 30 31 class ScopeLoggerTest : public testing::Test { 32 protected: 33 ScopeLoggerTest() {} 34 35 void TearDown() { 36 logger_.set_verbose_level(0); 37 logger_.DisableAllScopes(); 38 } 39 40 ScopeLogger logger_; 41 }; 42 43 TEST_F(ScopeLoggerTest, DefaultConstruction) { 44 for (int scope = 0; scope < ScopeLogger::kNumScopes; ++scope) { 45 for (int verbose_level = 0; verbose_level < 5; ++verbose_level) { 46 EXPECT_FALSE(logger_.IsLogEnabled( 47 static_cast<ScopeLogger::Scope>(scope), verbose_level)); 48 } 49 } 50 } 51 52 TEST_F(ScopeLoggerTest, GetAllScopeNames) { 53 EXPECT_EQ("binder+" 54 "cellular+" 55 "connection+" 56 "crypto+" 57 "daemon+" 58 "dbus+" 59 "device+" 60 "dhcp+" 61 "dns+" 62 "ethernet+" 63 "http+" 64 "httpproxy+" 65 "inet+" 66 "link+" 67 "manager+" 68 "metrics+" 69 "modem+" 70 "portal+" 71 "power+" 72 "ppp+" 73 "pppoe+" 74 "profile+" 75 "property+" 76 "resolver+" 77 "route+" 78 "rtnl+" 79 "service+" 80 "storage+" 81 "task+" 82 "vpn+" 83 "wifi+" 84 "wimax", 85 logger_.GetAllScopeNames()); 86 } 87 88 TEST_F(ScopeLoggerTest, GetEnabledScopeNames) { 89 EXPECT_EQ("", logger_.GetEnabledScopeNames()); 90 91 logger_.SetScopeEnabled(ScopeLogger::kWiFi, true); 92 EXPECT_EQ("wifi", logger_.GetEnabledScopeNames()); 93 94 logger_.SetScopeEnabled(ScopeLogger::kService, true); 95 EXPECT_EQ("service+wifi", logger_.GetEnabledScopeNames()); 96 97 logger_.SetScopeEnabled(ScopeLogger::kVPN, true); 98 EXPECT_EQ("service+vpn+wifi", logger_.GetEnabledScopeNames()); 99 100 logger_.SetScopeEnabled(ScopeLogger::kWiFi, false); 101 EXPECT_EQ("service+vpn", logger_.GetEnabledScopeNames()); 102 } 103 104 TEST_F(ScopeLoggerTest, EnableScopesByName) { 105 logger_.EnableScopesByName(""); 106 EXPECT_EQ("", logger_.GetEnabledScopeNames()); 107 108 logger_.EnableScopesByName("+wifi"); 109 EXPECT_EQ("wifi", logger_.GetEnabledScopeNames()); 110 111 logger_.EnableScopesByName("+service"); 112 EXPECT_EQ("service+wifi", logger_.GetEnabledScopeNames()); 113 114 logger_.EnableScopesByName("+vpn+wifi"); 115 EXPECT_EQ("service+vpn+wifi", logger_.GetEnabledScopeNames()); 116 117 logger_.EnableScopesByName("-wifi"); 118 EXPECT_EQ("service+vpn", logger_.GetEnabledScopeNames()); 119 120 logger_.EnableScopesByName("-vpn-service+wifi"); 121 EXPECT_EQ("wifi", logger_.GetEnabledScopeNames()); 122 123 logger_.EnableScopesByName("+-wifi-"); 124 EXPECT_EQ("", logger_.GetEnabledScopeNames()); 125 126 logger_.EnableScopesByName("-vpn+vpn+wifi-wifi"); 127 EXPECT_EQ("vpn", logger_.GetEnabledScopeNames()); 128 129 logger_.EnableScopesByName("wifi"); 130 EXPECT_EQ("wifi", logger_.GetEnabledScopeNames()); 131 132 logger_.EnableScopesByName(""); 133 EXPECT_EQ("", logger_.GetEnabledScopeNames()); 134 } 135 136 TEST_F(ScopeLoggerTest, EnableScopesByNameWithUnknownScopeName) { 137 logger_.EnableScopesByName("foo"); 138 EXPECT_EQ("", logger_.GetEnabledScopeNames()); 139 140 logger_.EnableScopesByName("wifi+foo+vpn"); 141 EXPECT_EQ("vpn+wifi", logger_.GetEnabledScopeNames()); 142 } 143 144 TEST_F(ScopeLoggerTest, SetScopeEnabled) { 145 EXPECT_FALSE(logger_.IsLogEnabled(ScopeLogger::kService, 0)); 146 147 logger_.SetScopeEnabled(ScopeLogger::kService, true); 148 EXPECT_TRUE(logger_.IsLogEnabled(ScopeLogger::kService, 0)); 149 150 logger_.SetScopeEnabled(ScopeLogger::kService, false); 151 EXPECT_FALSE(logger_.IsLogEnabled(ScopeLogger::kService, 0)); 152 } 153 154 TEST_F(ScopeLoggerTest, SetVerboseLevel) { 155 ScopeLogger* logger = ScopeLogger::GetInstance(); 156 logger->SetScopeEnabled(ScopeLogger::kService, true); 157 EXPECT_TRUE(logger->IsLogEnabled(ScopeLogger::kService, 0)); 158 EXPECT_FALSE(logger->IsLogEnabled(ScopeLogger::kService, 1)); 159 EXPECT_FALSE(logger->IsLogEnabled(ScopeLogger::kService, 2)); 160 EXPECT_TRUE(SLOG_IS_ON(Service, 0)); 161 EXPECT_FALSE(SLOG_IS_ON(Service, 1)); 162 EXPECT_FALSE(SLOG_IS_ON(Service, 2)); 163 164 logger->set_verbose_level(1); 165 EXPECT_TRUE(logger->IsLogEnabled(ScopeLogger::kService, 0)); 166 EXPECT_TRUE(logger->IsLogEnabled(ScopeLogger::kService, 1)); 167 EXPECT_FALSE(logger->IsLogEnabled(ScopeLogger::kService, 2)); 168 EXPECT_TRUE(SLOG_IS_ON(Service, 0)); 169 EXPECT_TRUE(SLOG_IS_ON(Service, 1)); 170 EXPECT_FALSE(SLOG_IS_ON(Service, 2)); 171 172 logger->set_verbose_level(2); 173 EXPECT_TRUE(logger->IsLogEnabled(ScopeLogger::kService, 0)); 174 EXPECT_TRUE(logger->IsLogEnabled(ScopeLogger::kService, 1)); 175 EXPECT_TRUE(logger->IsLogEnabled(ScopeLogger::kService, 2)); 176 EXPECT_TRUE(SLOG_IS_ON(Service, 0)); 177 EXPECT_TRUE(SLOG_IS_ON(Service, 1)); 178 EXPECT_TRUE(SLOG_IS_ON(Service, 2)); 179 180 logger->set_verbose_level(0); 181 logger->SetScopeEnabled(ScopeLogger::kService, false); 182 } 183 184 class ScopeChangeTarget { 185 public: 186 ScopeChangeTarget() : weak_ptr_factory_(this) {} 187 virtual ~ScopeChangeTarget() {} 188 MOCK_METHOD1(Callback, void(bool enabled)); 189 ScopeLogger::ScopeEnableChangedCallback GetCallback() { 190 return base::Bind( 191 &ScopeChangeTarget::Callback, weak_ptr_factory_.GetWeakPtr()); 192 } 193 194 private: 195 base::WeakPtrFactory<ScopeChangeTarget> weak_ptr_factory_; 196 }; 197 198 TEST_F(ScopeLoggerTest, LogScopeCallback) { 199 ScopeChangeTarget target0; 200 logger_.RegisterScopeEnableChangedCallback( 201 ScopeLogger::kWiFi, target0.GetCallback()); 202 EXPECT_CALL(target0, Callback(_)).Times(0); 203 // Call for a scope other than registered-for. 204 logger_.EnableScopesByName("+vpn"); 205 // Change to the same value as default. 206 logger_.EnableScopesByName("-wifi"); 207 testing::Mock::VerifyAndClearExpectations(&target0); 208 209 EXPECT_CALL(target0, Callback(true)).Times(1); 210 logger_.EnableScopesByName("+wifi"); 211 testing::Mock::VerifyAndClearExpectations(&target0); 212 213 EXPECT_CALL(target0, Callback(false)).Times(1); 214 logger_.EnableScopesByName(""); 215 testing::Mock::VerifyAndClearExpectations(&target0); 216 217 // Change to the same value as last set. 218 EXPECT_CALL(target0, Callback(_)).Times(0); 219 logger_.EnableScopesByName("-wifi"); 220 testing::Mock::VerifyAndClearExpectations(&target0); 221 222 ScopeChangeTarget target1; 223 logger_.RegisterScopeEnableChangedCallback( 224 ScopeLogger::kWiFi, target1.GetCallback()); 225 EXPECT_CALL(target0, Callback(true)).Times(1); 226 EXPECT_CALL(target1, Callback(true)).Times(1); 227 logger_.EnableScopesByName("+wifi"); 228 } 229 230 } // namespace shill 231