1 // Copyright 2010 The Chromium Authors
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 "base/vlog.h"
6
7 #include "base/time/time.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9
10 namespace logging {
11
12 namespace {
13
TEST(VlogTest,NoVmodule)14 TEST(VlogTest, NoVmodule) {
15 int min_log_level = 0;
16 EXPECT_EQ(0,
17 VlogInfo(std::string(), std::string(), &min_log_level)
18 .GetVlogLevel("test1"));
19 EXPECT_EQ(0,
20 VlogInfo("0", std::string(), &min_log_level).GetVlogLevel("test2"));
21 EXPECT_EQ(
22 0, VlogInfo("blah", std::string(), &min_log_level).GetVlogLevel("test3"));
23 EXPECT_EQ(
24 0,
25 VlogInfo("0blah1", std::string(), &min_log_level).GetVlogLevel("test4"));
26 EXPECT_EQ(1,
27 VlogInfo("1", std::string(), &min_log_level).GetVlogLevel("test5"));
28 EXPECT_EQ(5,
29 VlogInfo("5", std::string(), &min_log_level).GetVlogLevel("test6"));
30 }
31
TEST(VlogTest,MatchVlogPattern)32 TEST(VlogTest, MatchVlogPattern) {
33 // Degenerate cases.
34 EXPECT_TRUE(MatchVlogPattern("", ""));
35 EXPECT_TRUE(MatchVlogPattern("", "****"));
36 EXPECT_FALSE(MatchVlogPattern("", "x"));
37 EXPECT_FALSE(MatchVlogPattern("x", ""));
38
39 // Basic.
40 EXPECT_TRUE(MatchVlogPattern("blah", "blah"));
41
42 // ? should match exactly one character.
43 EXPECT_TRUE(MatchVlogPattern("blah", "bl?h"));
44 EXPECT_FALSE(MatchVlogPattern("blh", "bl?h"));
45 EXPECT_FALSE(MatchVlogPattern("blaah", "bl?h"));
46 EXPECT_TRUE(MatchVlogPattern("blah", "?lah"));
47 EXPECT_FALSE(MatchVlogPattern("lah", "?lah"));
48 EXPECT_FALSE(MatchVlogPattern("bblah", "?lah"));
49
50 // * can match any number (even 0) of characters.
51 EXPECT_TRUE(MatchVlogPattern("blah", "bl*h"));
52 EXPECT_TRUE(MatchVlogPattern("blabcdefh", "bl*h"));
53 EXPECT_TRUE(MatchVlogPattern("blh", "bl*h"));
54 EXPECT_TRUE(MatchVlogPattern("blah", "*blah"));
55 EXPECT_TRUE(MatchVlogPattern("ohblah", "*blah"));
56 EXPECT_TRUE(MatchVlogPattern("blah", "blah*"));
57 EXPECT_TRUE(MatchVlogPattern("blahhhh", "blah*"));
58 EXPECT_TRUE(MatchVlogPattern("blahhhh", "blah*"));
59 EXPECT_TRUE(MatchVlogPattern("blah", "*blah*"));
60 EXPECT_TRUE(MatchVlogPattern("blahhhh", "*blah*"));
61 EXPECT_TRUE(MatchVlogPattern("bbbblahhhh", "*blah*"));
62
63 // Multiple *s should work fine.
64 EXPECT_TRUE(MatchVlogPattern("ballaah", "b*la*h"));
65 EXPECT_TRUE(MatchVlogPattern("blah", "b*la*h"));
66 EXPECT_TRUE(MatchVlogPattern("bbbblah", "b*la*h"));
67 EXPECT_TRUE(MatchVlogPattern("blaaah", "b*la*h"));
68
69 // There should be no escaping going on.
70 EXPECT_TRUE(MatchVlogPattern("bl\\ah", "bl\\?h"));
71 EXPECT_FALSE(MatchVlogPattern("bl?h", "bl\\?h"));
72 EXPECT_TRUE(MatchVlogPattern("bl\\aaaah", "bl\\*h"));
73 EXPECT_FALSE(MatchVlogPattern("bl*h", "bl\\*h"));
74
75 // Any slash matches any slash.
76 EXPECT_TRUE(MatchVlogPattern("/b\\lah", "/b\\lah"));
77 EXPECT_TRUE(MatchVlogPattern("\\b/lah", "/b\\lah"));
78 }
79
TEST(VlogTest,VmoduleBasic)80 TEST(VlogTest, VmoduleBasic) {
81 const char kVSwitch[] = "-1";
82 const char kVModuleSwitch[] =
83 "foo=,bar=0,baz=blah,,qux=0blah1,quux=1,corge.ext=5";
84 int min_log_level = 0;
85 VlogInfo vlog_info(kVSwitch, kVModuleSwitch, &min_log_level);
86 EXPECT_EQ(-1, vlog_info.GetVlogLevel("/path/to/grault.cc"));
87 EXPECT_EQ(0, vlog_info.GetVlogLevel("/path/to/foo.cc"));
88 EXPECT_EQ(0, vlog_info.GetVlogLevel("D:\\Path\\To\\bar-inl.mm"));
89 EXPECT_EQ(-1, vlog_info.GetVlogLevel("D:\\path\\to what/bar_unittest.m"));
90 EXPECT_EQ(0, vlog_info.GetVlogLevel("baz.h"));
91 EXPECT_EQ(0, vlog_info.GetVlogLevel("/another/path/to/qux.h"));
92 EXPECT_EQ(1, vlog_info.GetVlogLevel("/path/to/quux"));
93 EXPECT_EQ(5, vlog_info.GetVlogLevel("c:\\path/to/corge.ext.h"));
94 }
95
TEST(VlogTest,VmoduleDirs)96 TEST(VlogTest, VmoduleDirs) {
97 const char kVModuleSwitch[] =
98 "foo/bar.cc=1,baz\\*\\qux.cc=2,*quux/*=3,*/*-inl.h=4";
99 int min_log_level = 0;
100 VlogInfo vlog_info(std::string(), kVModuleSwitch, &min_log_level);
101 EXPECT_EQ(0, vlog_info.GetVlogLevel("/foo/bar.cc"));
102 EXPECT_EQ(0, vlog_info.GetVlogLevel("bar.cc"));
103 EXPECT_EQ(1, vlog_info.GetVlogLevel("foo/bar.cc"));
104
105 EXPECT_EQ(0, vlog_info.GetVlogLevel("baz/grault/qux.h"));
106 EXPECT_EQ(0, vlog_info.GetVlogLevel("/baz/grault/qux.cc"));
107 EXPECT_EQ(2, vlog_info.GetVlogLevel("baz/grault/qux.cc"));
108 EXPECT_EQ(2, vlog_info.GetVlogLevel("baz/grault/blah/qux.cc"));
109 EXPECT_EQ(2, vlog_info.GetVlogLevel("baz\\grault\\qux.cc"));
110 EXPECT_EQ(2, vlog_info.GetVlogLevel("baz\\grault//blah\\qux.cc"));
111
112 EXPECT_EQ(0, vlog_info.GetVlogLevel("/foo/bar/baz/quux.cc"));
113 EXPECT_EQ(3, vlog_info.GetVlogLevel("/foo/bar/baz/quux/grault.cc"));
114 EXPECT_EQ(3, vlog_info.GetVlogLevel("/foo\\bar/baz\\quux/grault.cc"));
115
116 EXPECT_EQ(0, vlog_info.GetVlogLevel("foo/bar/test-inl.cc"));
117 EXPECT_EQ(4, vlog_info.GetVlogLevel("foo/bar/test-inl.h"));
118 EXPECT_EQ(4, vlog_info.GetVlogLevel("foo/bar/baz/blah-inl.h"));
119 }
120
TEST(VlogTest,VmoduleDuplicateName)121 TEST(VlogTest, VmoduleDuplicateName) {
122 // When filename rules are duplicated, the first one is effective.
123 const char kVModuleSwitch[] = "foo=2,foo=1";
124 int min_log_level = 0;
125 VlogInfo vlog_info(std::string(), kVModuleSwitch, &min_log_level);
126 EXPECT_EQ(2, vlog_info.GetVlogLevel("foo.cc"));
127 }
128
TEST(VlogTest,VmoduleDuplicatePattern)129 TEST(VlogTest, VmoduleDuplicatePattern) {
130 // When pattern rules are duplicated, the first one is effective.
131 const char kVModuleSwitch[] = "foo*=3,foo*=4";
132 int min_log_level = 0;
133 VlogInfo vlog_info(std::string(), kVModuleSwitch, &min_log_level);
134 EXPECT_EQ(3, vlog_info.GetVlogLevel("foobar.cc"));
135 }
136
TEST(VlogTest,VmoduleOrderFirstMatchIsName)137 TEST(VlogTest, VmoduleOrderFirstMatchIsName) {
138 // When rules overlap, the first matching rule is effective.
139 // This is a filename before pattern case.
140 const char kVModuleSwitch[] = "foo=2,bar/*=1";
141 int min_log_level = 0;
142 VlogInfo vlog_info(std::string(), kVModuleSwitch, &min_log_level);
143 EXPECT_EQ(1, vlog_info.GetVlogLevel("bar/a.cc"));
144 EXPECT_EQ(2, vlog_info.GetVlogLevel("bar/foo.cc"));
145 }
146
TEST(VlogTest,VmoduleOrderFirstMatchIsPattern)147 TEST(VlogTest, VmoduleOrderFirstMatchIsPattern) {
148 // When rules overlap, the first matching rule is effective.
149 // This is a pattern before filename case.
150 const char kVModuleSwitch[] = "bar/*=1,foo=2";
151 int min_log_level = 0;
152 VlogInfo vlog_info(std::string(), kVModuleSwitch, &min_log_level);
153 EXPECT_EQ(1, vlog_info.GetVlogLevel("bar/foo.cc"));
154 EXPECT_EQ(1, vlog_info.GetVlogLevel("bar/a.cc"));
155 EXPECT_EQ(2, vlog_info.GetVlogLevel("foo.cc"));
156 }
157
TEST(VlogTest,VmoduleOrderSample)158 TEST(VlogTest, VmoduleOrderSample) {
159 // logging.h --vmodule example, with some overlapping cases checked.
160 const char kVModuleSwitch[] =
161 "profile=2,icon_loader=1,browser_*=3,*/chromeos/*=4";
162 int min_log_level = 0;
163 VlogInfo vlog_info(std::string(), kVModuleSwitch, &min_log_level);
164 EXPECT_EQ(4, vlog_info.GetVlogLevel("foo/chromeos/bar.cc"));
165 EXPECT_EQ(3, vlog_info.GetVlogLevel("browser_foo.cc"));
166 EXPECT_EQ(3, vlog_info.GetVlogLevel("foo/chromeos/browser_bar.cc"));
167 EXPECT_EQ(1, vlog_info.GetVlogLevel("icon_loader.cc"));
168 EXPECT_EQ(2, vlog_info.GetVlogLevel("foo/profile.cc"));
169 EXPECT_EQ(2, vlog_info.GetVlogLevel("foo/chromeos/profile.cc"));
170 EXPECT_EQ(0, vlog_info.GetVlogLevel("foo.cc"));
171 }
172
TEST(VlogTest,WithSwitches)173 TEST(VlogTest, WithSwitches) {
174 // Set up simple VlogInfo with just "foo".
175 int min_log_level = 0;
176 VlogInfo vlog_info("", "foo=1", &min_log_level);
177 EXPECT_EQ(1, vlog_info.GetVlogLevel("foo.cc"));
178 EXPECT_EQ(0, vlog_info.GetVlogLevel("bar.cc"));
179
180 // Now create another one adding "bar".
181 std::unique_ptr<VlogInfo> vlog_info_with_switches(
182 vlog_info.WithSwitches("bar=2"));
183 EXPECT_EQ(1, vlog_info_with_switches->GetVlogLevel("foo.cc"));
184 EXPECT_EQ(2, vlog_info_with_switches->GetVlogLevel("bar.cc"));
185 }
186 } // namespace
187
188 } // namespace logging
189