1/*
2 * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24#include "precompiled.hpp"
25#include "jvm.h"
26#include "logging/logLevel.hpp"
27#include "logging/logSelectionList.hpp"
28#include "logging/logTagSet.hpp"
29#include "utilities/globalDefinitions.hpp"
30#include "logTestUtils.inline.hpp"
31#include "unittest.hpp"
32
33TEST(LogSelectionList, combination_limit) {
34 size_t max_combinations = LogSelectionList::MaxSelections;
35 EXPECT_GT(max_combinations, LogTagSet::ntagsets())
36 << "Combination limit not sufficient for configuring all available tag sets";
37}
38
39TEST(LogSelectionList, parse) {
40 char buf[256];
41 const char* valid_expression[] = {
42 "logging=off,all", "gc,logging", "logging+gc", "logging+gc,gc", "gc=trace,logging=info",
43 "logging+gc=trace,gc+logging=warning,logging", "gc,all=info"
44 };
45
46 // Verify valid expressions parse without problems
47 for (size_t i = 0; i < ARRAY_SIZE(valid_expression); i++) {
48 LogSelectionList expr;
49 EXPECT_TRUE(expr.parse(valid_expression[i])) << "Valid expression '" << valid_expression[i] << "' did not parse";
50 }
51
52 // Verify invalid expressions do not parse
53 for (size_t i = 0; i < ARRAY_SIZE(valid_expression); i++) {
54 for (size_t j = 0; j < ARRAY_SIZE(invalid_selection_substr); j++) {
55 // Prefix with invalid substr
56 LogSelectionList expr;
57 jio_snprintf(buf, sizeof(buf), "%s%s", invalid_selection_substr[j], valid_expression[i]);
58 EXPECT_FALSE(expr.parse(buf)) << "'" << buf << "'" << " considered legal";
59
60 // Suffix with invalid substr
61 LogSelectionList expr1;
62 jio_snprintf(buf, sizeof(buf), "%s%s", valid_expression[i], invalid_selection_substr[j]);
63 EXPECT_FALSE(expr1.parse(buf)) << "'" << buf << "'" << " considered legal";
64
65 // Use only the invalid substr
66 LogSelectionList expr2;
67 EXPECT_FALSE(expr2.parse(invalid_selection_substr[j])) << "'" << invalid_selection_substr[j] << "'" << " considered legal";
68 }
69
70 // Suffix/prefix with some unique invalid prefixes/suffixes
71 LogSelectionList expr;
72 jio_snprintf(buf, sizeof(buf), "*%s", valid_expression[i]);
73 EXPECT_FALSE(expr.parse(buf)) << "'" << buf << "'" << " considered legal";
74
75 LogSelectionList expr1;
76 jio_snprintf(buf, sizeof(buf), "logging*%s", valid_expression[i]);
77 EXPECT_FALSE(expr1.parse(buf)) << "'" << buf << "'" << " considered legal";
78 }
79}
80
81// Test the level_for() function for an empty expression
82TEST(LogSelectionList, level_for_empty) {
83 LogSelectionList emptyexpr;
84 ASSERT_TRUE(emptyexpr.parse(""));
85 // All tagsets should be unspecified since the expression doesn't involve any tagset
86 for (LogTagSet* ts = LogTagSet::first(); ts != NULL; ts = ts->next()) {
87 EXPECT_EQ(LogLevel::Unspecified, emptyexpr.level_for(*ts));
88 }
89}
90
91// Test level_for() with an expression that has overlap (last subexpression should be used)
92TEST(LogSelectionList, level_for_overlap) {
93 LogSelectionList overlapexpr;
94 // The all=warning will be overridden with gc=info and/or logging+safepoint*=trace
95 ASSERT_TRUE(overlapexpr.parse("all=warning,gc=info,logging+safepoint*=trace"));
96 for (LogTagSet* ts = LogTagSet::first(); ts != NULL; ts = ts->next()) {
97 if (ts->contains(PREFIX_LOG_TAG(gc)) && ts->ntags() == 1) {
98 EXPECT_EQ(LogLevel::Info, overlapexpr.level_for(*ts));
99 } else if (ts->contains(PREFIX_LOG_TAG(logging)) && ts->contains(PREFIX_LOG_TAG(safepoint))) {
100 EXPECT_EQ(LogLevel::Trace, overlapexpr.level_for(*ts));
101 } else {
102 EXPECT_EQ(LogLevel::Warning, overlapexpr.level_for(*ts));
103 }
104 }
105 EXPECT_EQ(LogLevel::Warning, overlapexpr.level_for(LogTagSetMapping<LOG_TAGS(class)>::tagset()));
106 EXPECT_EQ(LogLevel::Info, overlapexpr.level_for(LogTagSetMapping<LOG_TAGS(gc)>::tagset()));
107 EXPECT_EQ(LogLevel::Trace, overlapexpr.level_for(LogTagSetMapping<LOG_TAGS(logging, safepoint)>::tagset()));
108 EXPECT_EQ(LogLevel::Trace,
109 overlapexpr.level_for(LogTagSetMapping<LOG_TAGS(logging, gc, class, safepoint, heap)>::tagset()));
110}
111
112// Test level_for() with an expression containing two independent subexpressions
113TEST(LogSelectionList, level_for_disjoint) {
114 LogSelectionList reducedexpr;
115 ASSERT_TRUE(reducedexpr.parse("gc+logging=trace,class*=error"));
116 EXPECT_EQ(LogLevel::Error, reducedexpr.level_for(LogTagSetMapping<LOG_TAGS(class)>::tagset()));
117 EXPECT_EQ(LogLevel::Error, reducedexpr.level_for(LogTagSetMapping<LOG_TAGS(safepoint, class)>::tagset()));
118 EXPECT_EQ(LogLevel::NotMentioned, reducedexpr.level_for(LogTagSetMapping<LOG_TAGS(safepoint)>::tagset()));
119 EXPECT_EQ(LogLevel::NotMentioned, reducedexpr.level_for(LogTagSetMapping<LOG_TAGS(logging)>::tagset()));
120 EXPECT_EQ(LogLevel::NotMentioned, reducedexpr.level_for(LogTagSetMapping<LOG_TAGS(gc)>::tagset()));
121 EXPECT_EQ(LogLevel::Trace, reducedexpr.level_for(LogTagSetMapping<LOG_TAGS(logging, gc)>::tagset()));
122}
123
124// Test level_for() with an expression that is completely overridden in the last part of the expression
125TEST(LogSelectionList, level_for_override) {
126 LogSelectionList overrideexpr;
127 // No matter what, everything should be set to error level because of the last part
128 ASSERT_TRUE(overrideexpr.parse("logging,gc*=trace,all=error"));
129 EXPECT_EQ(LogLevel::Error, overrideexpr.level_for(LogTagSetMapping<LOG_TAGS(class)>::tagset()));
130 EXPECT_EQ(LogLevel::Error, overrideexpr.level_for(LogTagSetMapping<LOG_TAGS(logging)>::tagset()));
131 EXPECT_EQ(LogLevel::Error, overrideexpr.level_for(LogTagSetMapping<LOG_TAGS(gc)>::tagset()));
132 EXPECT_EQ(LogLevel::Error, overrideexpr.level_for(LogTagSetMapping<LOG_TAGS(logging, gc)>::tagset()));
133}
134
135// Test level_for() with a mixed expression with a bit of everything
136TEST(LogSelectionList, level_for_mixed) {
137 LogSelectionList mixedexpr;
138 ASSERT_TRUE(mixedexpr.parse("all=warning,gc*=debug,gc=trace,safepoint*=off"));
139 EXPECT_EQ(LogLevel::Warning, mixedexpr.level_for(LogTagSetMapping<LOG_TAGS(logging)>::tagset()));
140 EXPECT_EQ(LogLevel::Warning, mixedexpr.level_for(LogTagSetMapping<LOG_TAGS(logging, class)>::tagset()));
141 EXPECT_EQ(LogLevel::Debug, mixedexpr.level_for(LogTagSetMapping<LOG_TAGS(gc, class)>::tagset()));
142 EXPECT_EQ(LogLevel::Off, mixedexpr.level_for(LogTagSetMapping<LOG_TAGS(gc, safepoint, logging)>::tagset()));
143 EXPECT_EQ(LogLevel::Off, mixedexpr.level_for(LogTagSetMapping<LOG_TAGS(safepoint)>::tagset()));
144 EXPECT_EQ(LogLevel::Debug, mixedexpr.level_for(LogTagSetMapping<LOG_TAGS(logging, gc)>::tagset()));
145 EXPECT_EQ(LogLevel::Trace, mixedexpr.level_for(LogTagSetMapping<LOG_TAGS(gc)>::tagset()));
146}
147