blob: 60ff2a5b3abdd37fee497f148391b4e66f9151d9 [file] [log] [blame]
Ben Chanbc49ac72012-04-10 19:59:45 -07001// 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 Stewart5581d072012-12-17 17:30:20 -08005#include <base/bind.h>
6#include <base/memory/weak_ptr.h>
7
Christopher Wileyb691efd2012-08-09 13:51:51 -07008#include "shill/logging.h"
Ben Chanbc49ac72012-04-10 19:59:45 -07009#include "shill/scope_logger.h"
10
11#include <gmock/gmock.h>
12#include <gtest/gtest.h>
13
Paul Stewart5581d072012-12-17 17:30:20 -080014using ::testing::_;
15
Ben Chanbc49ac72012-04-10 19:59:45 -070016namespace shill {
17
18class ScopeLoggerTest : public testing::Test {
19 protected:
mukesh agrawale638be32012-05-31 15:13:24 -070020 ScopeLoggerTest() {}
Ben Chanbc49ac72012-04-10 19:59:45 -070021
22 void TearDown() {
mukesh agrawale638be32012-05-31 15:13:24 -070023 logger_.set_verbose_level(0);
24 logger_.DisableAllScopes();
Ben Chanbc49ac72012-04-10 19:59:45 -070025 }
26
mukesh agrawale638be32012-05-31 15:13:24 -070027 ScopeLogger logger_;
Ben Chanbc49ac72012-04-10 19:59:45 -070028};
29
30TEST_F(ScopeLoggerTest, DefaultConstruction) {
Ben Chanbc49ac72012-04-10 19:59:45 -070031 for (int scope = 0; scope < ScopeLogger::kNumScopes; ++scope) {
32 for (int verbose_level = 0; verbose_level < 5; ++verbose_level) {
mukesh agrawale638be32012-05-31 15:13:24 -070033 EXPECT_FALSE(logger_.IsLogEnabled(
Ben Chanbc49ac72012-04-10 19:59:45 -070034 static_cast<ScopeLogger::Scope>(scope), verbose_level));
35 }
36 }
37}
38
39TEST_F(ScopeLoggerTest, GetAllScopeNames) {
Ben Chan1c722602012-04-17 17:37:35 -070040 EXPECT_EQ("cellular+"
41 "connection+"
42 "crypto+"
Ben Chanfad4a0b2012-04-18 15:49:59 -070043 "daemon+"
Ben Chan1c722602012-04-17 17:37:35 -070044 "dbus+"
45 "device+"
Ben Chanfad4a0b2012-04-18 15:49:59 -070046 "dhcp+"
47 "dns+"
Ben Chan1c722602012-04-17 17:37:35 -070048 "ethernet+"
Ben Chanfad4a0b2012-04-18 15:49:59 -070049 "http+"
50 "httpproxy+"
Ben Chan1c722602012-04-17 17:37:35 -070051 "inet+"
Paul Stewart6c72c972012-07-27 11:29:20 -070052 "link+"
Ben Chan1c722602012-04-17 17:37:35 -070053 "manager+"
54 "metrics+"
55 "modem+"
56 "portal+"
Ben Chanfad4a0b2012-04-18 15:49:59 -070057 "power+"
mukesh agrawal9da07772013-05-15 14:15:17 -070058 "ppp+"
Ben Chan1c722602012-04-17 17:37:35 -070059 "profile+"
Ben Chanfad4a0b2012-04-18 15:49:59 -070060 "property+"
61 "resolver+"
62 "route+"
Ben Chan1c722602012-04-17 17:37:35 -070063 "rtnl+"
64 "service+"
65 "storage+"
66 "task+"
67 "vpn+"
Darin Petkov096b3472012-05-15 10:26:22 +020068 "wifi+"
69 "wimax",
mukesh agrawale638be32012-05-31 15:13:24 -070070 logger_.GetAllScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -070071}
72
73TEST_F(ScopeLoggerTest, GetEnabledScopeNames) {
mukesh agrawale638be32012-05-31 15:13:24 -070074 EXPECT_EQ("", logger_.GetEnabledScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -070075
mukesh agrawale638be32012-05-31 15:13:24 -070076 logger_.SetScopeEnabled(ScopeLogger::kWiFi, true);
77 EXPECT_EQ("wifi", logger_.GetEnabledScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -070078
mukesh agrawale638be32012-05-31 15:13:24 -070079 logger_.SetScopeEnabled(ScopeLogger::kService, true);
80 EXPECT_EQ("service+wifi", logger_.GetEnabledScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -070081
mukesh agrawale638be32012-05-31 15:13:24 -070082 logger_.SetScopeEnabled(ScopeLogger::kVPN, true);
83 EXPECT_EQ("service+vpn+wifi", logger_.GetEnabledScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -070084
mukesh agrawale638be32012-05-31 15:13:24 -070085 logger_.SetScopeEnabled(ScopeLogger::kWiFi, false);
86 EXPECT_EQ("service+vpn", logger_.GetEnabledScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -070087}
88
89TEST_F(ScopeLoggerTest, EnableScopesByName) {
mukesh agrawale638be32012-05-31 15:13:24 -070090 logger_.EnableScopesByName("");
91 EXPECT_EQ("", logger_.GetEnabledScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -070092
mukesh agrawale638be32012-05-31 15:13:24 -070093 logger_.EnableScopesByName("+wifi");
94 EXPECT_EQ("wifi", logger_.GetEnabledScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -070095
mukesh agrawale638be32012-05-31 15:13:24 -070096 logger_.EnableScopesByName("+service");
97 EXPECT_EQ("service+wifi", logger_.GetEnabledScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -070098
mukesh agrawale638be32012-05-31 15:13:24 -070099 logger_.EnableScopesByName("+vpn+wifi");
100 EXPECT_EQ("service+vpn+wifi", logger_.GetEnabledScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -0700101
mukesh agrawale638be32012-05-31 15:13:24 -0700102 logger_.EnableScopesByName("-wifi");
103 EXPECT_EQ("service+vpn", logger_.GetEnabledScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -0700104
mukesh agrawale638be32012-05-31 15:13:24 -0700105 logger_.EnableScopesByName("-vpn-service+wifi");
106 EXPECT_EQ("wifi", logger_.GetEnabledScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -0700107
mukesh agrawale638be32012-05-31 15:13:24 -0700108 logger_.EnableScopesByName("+-wifi-");
109 EXPECT_EQ("", logger_.GetEnabledScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -0700110
mukesh agrawale638be32012-05-31 15:13:24 -0700111 logger_.EnableScopesByName("-vpn+vpn+wifi-wifi");
112 EXPECT_EQ("vpn", logger_.GetEnabledScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -0700113
mukesh agrawale638be32012-05-31 15:13:24 -0700114 logger_.EnableScopesByName("wifi");
115 EXPECT_EQ("wifi", logger_.GetEnabledScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -0700116
mukesh agrawale638be32012-05-31 15:13:24 -0700117 logger_.EnableScopesByName("");
118 EXPECT_EQ("", logger_.GetEnabledScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -0700119}
120
121TEST_F(ScopeLoggerTest, EnableScopesByNameWithUnknownScopeName) {
mukesh agrawale638be32012-05-31 15:13:24 -0700122 logger_.EnableScopesByName("foo");
123 EXPECT_EQ("", logger_.GetEnabledScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -0700124
mukesh agrawale638be32012-05-31 15:13:24 -0700125 logger_.EnableScopesByName("wifi+foo+vpn");
126 EXPECT_EQ("vpn+wifi", logger_.GetEnabledScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -0700127}
128
129TEST_F(ScopeLoggerTest, SetScopeEnabled) {
mukesh agrawale638be32012-05-31 15:13:24 -0700130 EXPECT_FALSE(logger_.IsLogEnabled(ScopeLogger::kService, 0));
Ben Chanbc49ac72012-04-10 19:59:45 -0700131
mukesh agrawale638be32012-05-31 15:13:24 -0700132 logger_.SetScopeEnabled(ScopeLogger::kService, true);
133 EXPECT_TRUE(logger_.IsLogEnabled(ScopeLogger::kService, 0));
Ben Chanbc49ac72012-04-10 19:59:45 -0700134
mukesh agrawale638be32012-05-31 15:13:24 -0700135 logger_.SetScopeEnabled(ScopeLogger::kService, false);
136 EXPECT_FALSE(logger_.IsLogEnabled(ScopeLogger::kService, 0));
Ben Chanbc49ac72012-04-10 19:59:45 -0700137}
138
139TEST_F(ScopeLoggerTest, SetVerboseLevel) {
Christopher Wileyd34a47c2012-08-01 15:42:45 -0700140 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 Chanbc49ac72012-04-10 19:59:45 -0700148
Christopher Wileyd34a47c2012-08-01 15:42:45 -0700149 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 Chanbc49ac72012-04-10 19:59:45 -0700156
Christopher Wileyd34a47c2012-08-01 15:42:45 -0700157 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 Chanbc49ac72012-04-10 19:59:45 -0700167}
168
Paul Stewart5581d072012-12-17 17:30:20 -0800169class 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
183TEST_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 Chanbc49ac72012-04-10 19:59:45 -0700215} // namespace shill