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 | |
| 5 | #include "shill/scope_logger.h" |
| 6 | |
| 7 | #include <gmock/gmock.h> |
| 8 | #include <gtest/gtest.h> |
| 9 | |
| 10 | namespace shill { |
| 11 | |
| 12 | class ScopeLoggerTest : public testing::Test { |
| 13 | protected: |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 14 | ScopeLoggerTest() {} |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 15 | |
| 16 | void TearDown() { |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 17 | logger_.set_verbose_level(0); |
| 18 | logger_.DisableAllScopes(); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 19 | } |
| 20 | |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 21 | ScopeLogger logger_; |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 22 | }; |
| 23 | |
| 24 | TEST_F(ScopeLoggerTest, DefaultConstruction) { |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 25 | for (int scope = 0; scope < ScopeLogger::kNumScopes; ++scope) { |
| 26 | for (int verbose_level = 0; verbose_level < 5; ++verbose_level) { |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 27 | EXPECT_FALSE(logger_.IsLogEnabled( |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 28 | static_cast<ScopeLogger::Scope>(scope), verbose_level)); |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | TEST_F(ScopeLoggerTest, GetAllScopeNames) { |
Ben Chan | 1c72260 | 2012-04-17 17:37:35 -0700 | [diff] [blame] | 34 | EXPECT_EQ("cellular+" |
| 35 | "connection+" |
| 36 | "crypto+" |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 37 | "daemon+" |
Ben Chan | 1c72260 | 2012-04-17 17:37:35 -0700 | [diff] [blame] | 38 | "dbus+" |
| 39 | "device+" |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 40 | "dhcp+" |
| 41 | "dns+" |
Ben Chan | 1c72260 | 2012-04-17 17:37:35 -0700 | [diff] [blame] | 42 | "ethernet+" |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 43 | "http+" |
| 44 | "httpproxy+" |
Ben Chan | 1c72260 | 2012-04-17 17:37:35 -0700 | [diff] [blame] | 45 | "inet+" |
| 46 | "manager+" |
| 47 | "metrics+" |
| 48 | "modem+" |
| 49 | "portal+" |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 50 | "power+" |
Ben Chan | 1c72260 | 2012-04-17 17:37:35 -0700 | [diff] [blame] | 51 | "profile+" |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 52 | "property+" |
| 53 | "resolver+" |
| 54 | "route+" |
Ben Chan | 1c72260 | 2012-04-17 17:37:35 -0700 | [diff] [blame] | 55 | "rtnl+" |
| 56 | "service+" |
| 57 | "storage+" |
| 58 | "task+" |
| 59 | "vpn+" |
Darin Petkov | 096b347 | 2012-05-15 10:26:22 +0200 | [diff] [blame] | 60 | "wifi+" |
| 61 | "wimax", |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 62 | logger_.GetAllScopeNames()); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | TEST_F(ScopeLoggerTest, GetEnabledScopeNames) { |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 66 | EXPECT_EQ("", logger_.GetEnabledScopeNames()); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 67 | |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 68 | logger_.SetScopeEnabled(ScopeLogger::kWiFi, true); |
| 69 | EXPECT_EQ("wifi", logger_.GetEnabledScopeNames()); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 70 | |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 71 | logger_.SetScopeEnabled(ScopeLogger::kService, true); |
| 72 | EXPECT_EQ("service+wifi", logger_.GetEnabledScopeNames()); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 73 | |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 74 | logger_.SetScopeEnabled(ScopeLogger::kVPN, true); |
| 75 | EXPECT_EQ("service+vpn+wifi", logger_.GetEnabledScopeNames()); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 76 | |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 77 | logger_.SetScopeEnabled(ScopeLogger::kWiFi, false); |
| 78 | EXPECT_EQ("service+vpn", logger_.GetEnabledScopeNames()); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | TEST_F(ScopeLoggerTest, EnableScopesByName) { |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 82 | logger_.EnableScopesByName(""); |
| 83 | EXPECT_EQ("", 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_.EnableScopesByName("+wifi"); |
| 86 | EXPECT_EQ("wifi", logger_.GetEnabledScopeNames()); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 87 | |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 88 | logger_.EnableScopesByName("+service"); |
| 89 | EXPECT_EQ("service+wifi", logger_.GetEnabledScopeNames()); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 90 | |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 91 | logger_.EnableScopesByName("+vpn+wifi"); |
| 92 | EXPECT_EQ("service+vpn+wifi", logger_.GetEnabledScopeNames()); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 93 | |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 94 | logger_.EnableScopesByName("-wifi"); |
| 95 | EXPECT_EQ("service+vpn", logger_.GetEnabledScopeNames()); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 96 | |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 97 | logger_.EnableScopesByName("-vpn-service+wifi"); |
| 98 | EXPECT_EQ("wifi", logger_.GetEnabledScopeNames()); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 99 | |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 100 | logger_.EnableScopesByName("+-wifi-"); |
| 101 | EXPECT_EQ("", logger_.GetEnabledScopeNames()); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 102 | |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 103 | logger_.EnableScopesByName("-vpn+vpn+wifi-wifi"); |
| 104 | EXPECT_EQ("vpn", logger_.GetEnabledScopeNames()); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 105 | |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 106 | logger_.EnableScopesByName("wifi"); |
| 107 | EXPECT_EQ("wifi", logger_.GetEnabledScopeNames()); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 108 | |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 109 | logger_.EnableScopesByName(""); |
| 110 | EXPECT_EQ("", logger_.GetEnabledScopeNames()); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | TEST_F(ScopeLoggerTest, EnableScopesByNameWithUnknownScopeName) { |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 114 | logger_.EnableScopesByName("foo"); |
| 115 | EXPECT_EQ("", 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("wifi+foo+vpn"); |
| 118 | EXPECT_EQ("vpn+wifi", logger_.GetEnabledScopeNames()); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | TEST_F(ScopeLoggerTest, SetScopeEnabled) { |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 122 | EXPECT_FALSE(logger_.IsLogEnabled(ScopeLogger::kService, 0)); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 123 | |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 124 | logger_.SetScopeEnabled(ScopeLogger::kService, true); |
| 125 | EXPECT_TRUE(logger_.IsLogEnabled(ScopeLogger::kService, 0)); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 126 | |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 127 | logger_.SetScopeEnabled(ScopeLogger::kService, false); |
| 128 | EXPECT_FALSE(logger_.IsLogEnabled(ScopeLogger::kService, 0)); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | TEST_F(ScopeLoggerTest, SetVerboseLevel) { |
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)); |
| 134 | EXPECT_FALSE(logger_.IsLogEnabled(ScopeLogger::kService, 1)); |
| 135 | EXPECT_FALSE(logger_.IsLogEnabled(ScopeLogger::kService, 2)); |
Ben Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 136 | |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 137 | 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 Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 141 | |
mukesh agrawal | e638be3 | 2012-05-31 15:13:24 -0700 | [diff] [blame] | 142 | 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 Chan | bc49ac7 | 2012-04-10 19:59:45 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | } // namespace shill |