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/basictypes.h"
8#include "base/logging.h"
9#include "base/third_party/dynamic_annotations/dynamic_annotations.h"
10#include "base/time/time.h"
11#include "testing/gtest/include/gtest/gtest.h"
12
13namespace logging {
14
15namespace {
16
17TEST(VlogTest, NoVmodule) {
18  int min_log_level = 0;
19  EXPECT_EQ(0,
20            VlogInfo(std::string(), std::string(), &min_log_level)
21                .GetVlogLevel("test1"));
22  EXPECT_EQ(0,
23            VlogInfo("0", std::string(), &min_log_level).GetVlogLevel("test2"));
24  EXPECT_EQ(
25      0, VlogInfo("blah", std::string(), &min_log_level).GetVlogLevel("test3"));
26  EXPECT_EQ(
27      0,
28      VlogInfo("0blah1", std::string(), &min_log_level).GetVlogLevel("test4"));
29  EXPECT_EQ(1,
30            VlogInfo("1", std::string(), &min_log_level).GetVlogLevel("test5"));
31  EXPECT_EQ(5,
32            VlogInfo("5", std::string(), &min_log_level).GetVlogLevel("test6"));
33}
34
35TEST(VlogTest, MatchVlogPattern) {
36  // Degenerate cases.
37  EXPECT_TRUE(MatchVlogPattern("", ""));
38  EXPECT_TRUE(MatchVlogPattern("", "****"));
39  EXPECT_FALSE(MatchVlogPattern("", "x"));
40  EXPECT_FALSE(MatchVlogPattern("x", ""));
41
42  // Basic.
43  EXPECT_TRUE(MatchVlogPattern("blah", "blah"));
44
45  // ? should match exactly one character.
46  EXPECT_TRUE(MatchVlogPattern("blah", "bl?h"));
47  EXPECT_FALSE(MatchVlogPattern("blh", "bl?h"));
48  EXPECT_FALSE(MatchVlogPattern("blaah", "bl?h"));
49  EXPECT_TRUE(MatchVlogPattern("blah", "?lah"));
50  EXPECT_FALSE(MatchVlogPattern("lah", "?lah"));
51  EXPECT_FALSE(MatchVlogPattern("bblah", "?lah"));
52
53  // * can match any number (even 0) of characters.
54  EXPECT_TRUE(MatchVlogPattern("blah", "bl*h"));
55  EXPECT_TRUE(MatchVlogPattern("blabcdefh", "bl*h"));
56  EXPECT_TRUE(MatchVlogPattern("blh", "bl*h"));
57  EXPECT_TRUE(MatchVlogPattern("blah", "*blah"));
58  EXPECT_TRUE(MatchVlogPattern("ohblah", "*blah"));
59  EXPECT_TRUE(MatchVlogPattern("blah", "blah*"));
60  EXPECT_TRUE(MatchVlogPattern("blahhhh", "blah*"));
61  EXPECT_TRUE(MatchVlogPattern("blahhhh", "blah*"));
62  EXPECT_TRUE(MatchVlogPattern("blah", "*blah*"));
63  EXPECT_TRUE(MatchVlogPattern("blahhhh", "*blah*"));
64  EXPECT_TRUE(MatchVlogPattern("bbbblahhhh", "*blah*"));
65
66  // Multiple *s should work fine.
67  EXPECT_TRUE(MatchVlogPattern("ballaah", "b*la*h"));
68  EXPECT_TRUE(MatchVlogPattern("blah", "b*la*h"));
69  EXPECT_TRUE(MatchVlogPattern("bbbblah", "b*la*h"));
70  EXPECT_TRUE(MatchVlogPattern("blaaah", "b*la*h"));
71
72  // There should be no escaping going on.
73  EXPECT_TRUE(MatchVlogPattern("bl\\ah", "bl\\?h"));
74  EXPECT_FALSE(MatchVlogPattern("bl?h", "bl\\?h"));
75  EXPECT_TRUE(MatchVlogPattern("bl\\aaaah", "bl\\*h"));
76  EXPECT_FALSE(MatchVlogPattern("bl*h", "bl\\*h"));
77
78  // Any slash matches any slash.
79  EXPECT_TRUE(MatchVlogPattern("/b\\lah", "/b\\lah"));
80  EXPECT_TRUE(MatchVlogPattern("\\b/lah", "/b\\lah"));
81}
82
83TEST(VlogTest, VmoduleBasic) {
84  const char kVSwitch[] = "-1";
85  const char kVModuleSwitch[] =
86      "foo=,bar=0,baz=blah,,qux=0blah1,quux=1,corge.ext=5";
87  int min_log_level = 0;
88  VlogInfo vlog_info(kVSwitch, kVModuleSwitch, &min_log_level);
89  EXPECT_EQ(-1, vlog_info.GetVlogLevel("/path/to/grault.cc"));
90  EXPECT_EQ(0, vlog_info.GetVlogLevel("/path/to/foo.cc"));
91  EXPECT_EQ(0, vlog_info.GetVlogLevel("D:\\Path\\To\\bar-inl.mm"));
92  EXPECT_EQ(-1, vlog_info.GetVlogLevel("D:\\path\\to what/bar_unittest.m"));
93  EXPECT_EQ(0, vlog_info.GetVlogLevel("baz.h"));
94  EXPECT_EQ(0, vlog_info.GetVlogLevel("/another/path/to/qux.h"));
95  EXPECT_EQ(1, vlog_info.GetVlogLevel("/path/to/quux"));
96  EXPECT_EQ(5, vlog_info.GetVlogLevel("c:\\path/to/corge.ext.h"));
97}
98
99TEST(VlogTest, VmoduleDirs) {
100  const char kVModuleSwitch[] =
101      "foo/bar.cc=1,baz\\*\\qux.cc=2,*quux/*=3,*/*-inl.h=4";
102  int min_log_level = 0;
103  VlogInfo vlog_info(std::string(), kVModuleSwitch, &min_log_level);
104  EXPECT_EQ(0, vlog_info.GetVlogLevel("/foo/bar.cc"));
105  EXPECT_EQ(0, vlog_info.GetVlogLevel("bar.cc"));
106  EXPECT_EQ(1, vlog_info.GetVlogLevel("foo/bar.cc"));
107
108  EXPECT_EQ(0, vlog_info.GetVlogLevel("baz/grault/qux.h"));
109  EXPECT_EQ(0, vlog_info.GetVlogLevel("/baz/grault/qux.cc"));
110  EXPECT_EQ(2, vlog_info.GetVlogLevel("baz/grault/qux.cc"));
111  EXPECT_EQ(2, vlog_info.GetVlogLevel("baz/grault/blah/qux.cc"));
112  EXPECT_EQ(2, vlog_info.GetVlogLevel("baz\\grault\\qux.cc"));
113  EXPECT_EQ(2, vlog_info.GetVlogLevel("baz\\grault//blah\\qux.cc"));
114
115  EXPECT_EQ(0, vlog_info.GetVlogLevel("/foo/bar/baz/quux.cc"));
116  EXPECT_EQ(3, vlog_info.GetVlogLevel("/foo/bar/baz/quux/grault.cc"));
117  EXPECT_EQ(3, vlog_info.GetVlogLevel("/foo\\bar/baz\\quux/grault.cc"));
118
119  EXPECT_EQ(0, vlog_info.GetVlogLevel("foo/bar/test-inl.cc"));
120  EXPECT_EQ(4, vlog_info.GetVlogLevel("foo/bar/test-inl.h"));
121  EXPECT_EQ(4, vlog_info.GetVlogLevel("foo/bar/baz/blah-inl.h"));
122}
123
124}  // namespace
125
126}  // namespace logging
127