1 // Copyright (c) 2010 The Chromium 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 "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
121 } // namespace
122
123 } // namespace logging
124