blob: c145dfea9bccc392a6d45a02d9fc08e8a0ecf264 [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
5#include "shill/scope_logger.h"
6
7#include <gmock/gmock.h>
8#include <gtest/gtest.h>
9
10namespace shill {
11
12class ScopeLoggerTest : public testing::Test {
13 protected:
mukesh agrawale638be32012-05-31 15:13:24 -070014 ScopeLoggerTest() {}
Ben Chanbc49ac72012-04-10 19:59:45 -070015
16 void TearDown() {
mukesh agrawale638be32012-05-31 15:13:24 -070017 logger_.set_verbose_level(0);
18 logger_.DisableAllScopes();
Ben Chanbc49ac72012-04-10 19:59:45 -070019 }
20
mukesh agrawale638be32012-05-31 15:13:24 -070021 ScopeLogger logger_;
Ben Chanbc49ac72012-04-10 19:59:45 -070022};
23
24TEST_F(ScopeLoggerTest, DefaultConstruction) {
Ben Chanbc49ac72012-04-10 19:59:45 -070025 for (int scope = 0; scope < ScopeLogger::kNumScopes; ++scope) {
26 for (int verbose_level = 0; verbose_level < 5; ++verbose_level) {
mukesh agrawale638be32012-05-31 15:13:24 -070027 EXPECT_FALSE(logger_.IsLogEnabled(
Ben Chanbc49ac72012-04-10 19:59:45 -070028 static_cast<ScopeLogger::Scope>(scope), verbose_level));
29 }
30 }
31}
32
33TEST_F(ScopeLoggerTest, GetAllScopeNames) {
Ben Chan1c722602012-04-17 17:37:35 -070034 EXPECT_EQ("cellular+"
35 "connection+"
36 "crypto+"
Ben Chanfad4a0b2012-04-18 15:49:59 -070037 "daemon+"
Ben Chan1c722602012-04-17 17:37:35 -070038 "dbus+"
39 "device+"
Ben Chanfad4a0b2012-04-18 15:49:59 -070040 "dhcp+"
41 "dns+"
Ben Chan1c722602012-04-17 17:37:35 -070042 "ethernet+"
Ben Chanfad4a0b2012-04-18 15:49:59 -070043 "http+"
44 "httpproxy+"
Ben Chan1c722602012-04-17 17:37:35 -070045 "inet+"
46 "manager+"
47 "metrics+"
48 "modem+"
49 "portal+"
Ben Chanfad4a0b2012-04-18 15:49:59 -070050 "power+"
Ben Chan1c722602012-04-17 17:37:35 -070051 "profile+"
Ben Chanfad4a0b2012-04-18 15:49:59 -070052 "property+"
53 "resolver+"
54 "route+"
Ben Chan1c722602012-04-17 17:37:35 -070055 "rtnl+"
56 "service+"
57 "storage+"
58 "task+"
59 "vpn+"
Darin Petkov096b3472012-05-15 10:26:22 +020060 "wifi+"
61 "wimax",
mukesh agrawale638be32012-05-31 15:13:24 -070062 logger_.GetAllScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -070063}
64
65TEST_F(ScopeLoggerTest, GetEnabledScopeNames) {
mukesh agrawale638be32012-05-31 15:13:24 -070066 EXPECT_EQ("", logger_.GetEnabledScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -070067
mukesh agrawale638be32012-05-31 15:13:24 -070068 logger_.SetScopeEnabled(ScopeLogger::kWiFi, true);
69 EXPECT_EQ("wifi", logger_.GetEnabledScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -070070
mukesh agrawale638be32012-05-31 15:13:24 -070071 logger_.SetScopeEnabled(ScopeLogger::kService, true);
72 EXPECT_EQ("service+wifi", logger_.GetEnabledScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -070073
mukesh agrawale638be32012-05-31 15:13:24 -070074 logger_.SetScopeEnabled(ScopeLogger::kVPN, true);
75 EXPECT_EQ("service+vpn+wifi", logger_.GetEnabledScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -070076
mukesh agrawale638be32012-05-31 15:13:24 -070077 logger_.SetScopeEnabled(ScopeLogger::kWiFi, false);
78 EXPECT_EQ("service+vpn", logger_.GetEnabledScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -070079}
80
81TEST_F(ScopeLoggerTest, EnableScopesByName) {
mukesh agrawale638be32012-05-31 15:13:24 -070082 logger_.EnableScopesByName("");
83 EXPECT_EQ("", logger_.GetEnabledScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -070084
mukesh agrawale638be32012-05-31 15:13:24 -070085 logger_.EnableScopesByName("+wifi");
86 EXPECT_EQ("wifi", logger_.GetEnabledScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -070087
mukesh agrawale638be32012-05-31 15:13:24 -070088 logger_.EnableScopesByName("+service");
89 EXPECT_EQ("service+wifi", logger_.GetEnabledScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -070090
mukesh agrawale638be32012-05-31 15:13:24 -070091 logger_.EnableScopesByName("+vpn+wifi");
92 EXPECT_EQ("service+vpn+wifi", logger_.GetEnabledScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -070093
mukesh agrawale638be32012-05-31 15:13:24 -070094 logger_.EnableScopesByName("-wifi");
95 EXPECT_EQ("service+vpn", logger_.GetEnabledScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -070096
mukesh agrawale638be32012-05-31 15:13:24 -070097 logger_.EnableScopesByName("-vpn-service+wifi");
98 EXPECT_EQ("wifi", logger_.GetEnabledScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -070099
mukesh agrawale638be32012-05-31 15:13:24 -0700100 logger_.EnableScopesByName("+-wifi-");
101 EXPECT_EQ("", logger_.GetEnabledScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -0700102
mukesh agrawale638be32012-05-31 15:13:24 -0700103 logger_.EnableScopesByName("-vpn+vpn+wifi-wifi");
104 EXPECT_EQ("vpn", logger_.GetEnabledScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -0700105
mukesh agrawale638be32012-05-31 15:13:24 -0700106 logger_.EnableScopesByName("wifi");
107 EXPECT_EQ("wifi", logger_.GetEnabledScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -0700108
mukesh agrawale638be32012-05-31 15:13:24 -0700109 logger_.EnableScopesByName("");
110 EXPECT_EQ("", logger_.GetEnabledScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -0700111}
112
113TEST_F(ScopeLoggerTest, EnableScopesByNameWithUnknownScopeName) {
mukesh agrawale638be32012-05-31 15:13:24 -0700114 logger_.EnableScopesByName("foo");
115 EXPECT_EQ("", logger_.GetEnabledScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -0700116
mukesh agrawale638be32012-05-31 15:13:24 -0700117 logger_.EnableScopesByName("wifi+foo+vpn");
118 EXPECT_EQ("vpn+wifi", logger_.GetEnabledScopeNames());
Ben Chanbc49ac72012-04-10 19:59:45 -0700119}
120
121TEST_F(ScopeLoggerTest, SetScopeEnabled) {
mukesh agrawale638be32012-05-31 15:13:24 -0700122 EXPECT_FALSE(logger_.IsLogEnabled(ScopeLogger::kService, 0));
Ben Chanbc49ac72012-04-10 19:59:45 -0700123
mukesh agrawale638be32012-05-31 15:13:24 -0700124 logger_.SetScopeEnabled(ScopeLogger::kService, true);
125 EXPECT_TRUE(logger_.IsLogEnabled(ScopeLogger::kService, 0));
Ben Chanbc49ac72012-04-10 19:59:45 -0700126
mukesh agrawale638be32012-05-31 15:13:24 -0700127 logger_.SetScopeEnabled(ScopeLogger::kService, false);
128 EXPECT_FALSE(logger_.IsLogEnabled(ScopeLogger::kService, 0));
Ben Chanbc49ac72012-04-10 19:59:45 -0700129}
130
131TEST_F(ScopeLoggerTest, SetVerboseLevel) {
mukesh agrawale638be32012-05-31 15:13:24 -0700132 logger_.SetScopeEnabled(ScopeLogger::kService, true);
133 EXPECT_TRUE(logger_.IsLogEnabled(ScopeLogger::kService, 0));
134 EXPECT_FALSE(logger_.IsLogEnabled(ScopeLogger::kService, 1));
135 EXPECT_FALSE(logger_.IsLogEnabled(ScopeLogger::kService, 2));
Ben Chanbc49ac72012-04-10 19:59:45 -0700136
mukesh agrawale638be32012-05-31 15:13:24 -0700137 logger_.set_verbose_level(1);
138 EXPECT_TRUE(logger_.IsLogEnabled(ScopeLogger::kService, 0));
139 EXPECT_TRUE(logger_.IsLogEnabled(ScopeLogger::kService, 1));
140 EXPECT_FALSE(logger_.IsLogEnabled(ScopeLogger::kService, 2));
Ben Chanbc49ac72012-04-10 19:59:45 -0700141
mukesh agrawale638be32012-05-31 15:13:24 -0700142 logger_.set_verbose_level(2);
143 EXPECT_TRUE(logger_.IsLogEnabled(ScopeLogger::kService, 0));
144 EXPECT_TRUE(logger_.IsLogEnabled(ScopeLogger::kService, 1));
145 EXPECT_TRUE(logger_.IsLogEnabled(ScopeLogger::kService, 2));
Ben Chanbc49ac72012-04-10 19:59:45 -0700146}
147
148} // namespace shill