1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
/*
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#include "precompiled.hpp"
#include "jvm.h"
#include "logging/logDecorators.hpp"
#include "runtime/os.hpp"
#include "unittest.hpp"
static LogDecorators::Decorator decorator_array[] = {
#define DECORATOR(name, abbr) LogDecorators::name##_decorator,
DECORATOR_LIST
#undef DECORATOR
};
static const char* decorator_name_array[] = {
#define DECORATOR(name, abbr) #name,
DECORATOR_LIST
#undef DECORATOR
};
static const char* decorator_abbr_array[] = {
#define DECORATOR(name, abbr) #abbr,
DECORATOR_LIST
#undef DECORATOR
};
// Assert that the given decorators object has the default decorators (uptime, level, tags)
// If exclusive = true, also assert that no other decorators are selected
static void assert_default_decorators(LogDecorators* decorators, bool exclusive = true) {
for (int i = 0; i < LogDecorators::Count; i++) {
LogDecorators::Decorator decorator = decorator_array[i];
if (decorator == LogDecorators::uptime_decorator ||
decorator == LogDecorators::level_decorator ||
decorator == LogDecorators::tags_decorator) {
EXPECT_TRUE(decorators->is_decorator(decorator));
} else if (exclusive) {
EXPECT_FALSE(decorators->is_decorator(decorator));
}
}
}
TEST(LogDecorators, defaults) {
LogDecorators decorators;
assert_default_decorators(&decorators);
}
// Test converting between name and decorator (string and enum)
TEST(LogDecorators, from_and_to_name) {
EXPECT_EQ(LogDecorators::Invalid, LogDecorators::from_string("unknown"));
EXPECT_EQ(LogDecorators::Invalid, LogDecorators::from_string(""));
for (int i = 0; i < LogDecorators::Count; i++) {
LogDecorators::Decorator decorator = decorator_array[i];
const char* name = LogDecorators::name(decorator);
EXPECT_STREQ(decorator_name_array[i], name);
LogDecorators::Decorator decorator2 = LogDecorators::from_string(name);
EXPECT_EQ(decorator, decorator2);
// Test case insensitivity
char* name_cpy = os::strdup(name, mtTest);
name_cpy[0] = toupper(name_cpy[0]);
decorator2 = LogDecorators::from_string(name_cpy);
os::free(name_cpy);
EXPECT_EQ(decorator, decorator2);
}
}
// Test decorator abbreviations
TEST(LogDecorators, from_and_to_abbr) {
for (int i = 0; i < LogDecorators::Count; i++) {
LogDecorators::Decorator decorator = decorator_array[i];
const char* abbr = LogDecorators::abbreviation(decorator);
EXPECT_STREQ(decorator_abbr_array[i], abbr);
LogDecorators::Decorator decorator2 = LogDecorators::from_string(abbr);
ASSERT_EQ(decorator, decorator2);
// Test case insensitivity
char* abbr_cpy = os::strdup(abbr);
abbr_cpy[0] = toupper(abbr_cpy[0]);
decorator2 = LogDecorators::from_string(abbr_cpy);
os::free(abbr_cpy);
EXPECT_EQ(decorator, decorator2);
}
}
TEST(LogDecorators, parse_default) {
LogDecorators decorators;
decorators.parse(""); // Empty string means we should use the default decorators
assert_default_decorators(&decorators);
}
// Test that "none" gives no decorators at all
TEST(LogDecorators, parse_none) {
LogDecorators decorators;
decorators.parse("none");
for (int i = 0; i < LogDecorators::Count; i++) {
EXPECT_FALSE(decorators.is_decorator(decorator_array[i]));
}
}
// Test a few invalid decorator selections
TEST(LogDecorators, parse_invalid) {
LogDecorators decorators;
EXPECT_FALSE(decorators.parse("invalid"));
EXPECT_FALSE(decorators.parse(",invalid"));
EXPECT_FALSE(decorators.parse(",invalid,"));
assert_default_decorators(&decorators);
}
// Assert that the given decorator has all decorators between first and last
static void assert_decorations_between(const LogDecorators* decorator, size_t first, size_t last) {
for (size_t i = 0; i < ARRAY_SIZE(decorator_array); i++) {
if (i >= first && i <= last) {
EXPECT_TRUE(decorator->is_decorator(decorator_array[i]));
} else {
EXPECT_FALSE(decorator->is_decorator(decorator_array[i]));
}
}
}
TEST(LogDecorators, parse) {
LogDecorators decorators;
// Verify a bunch of different decorator selections
char decstr[1 * K];
decstr[0] = '\0';
size_t written = 0;
for (size_t i = 0; i < ARRAY_SIZE(decorator_array); i++) {
for (size_t j = i; j < ARRAY_SIZE(decorator_array); j++) {
for (size_t k = i; k <= j; k++) {
ASSERT_LT(written, sizeof(decstr)) << "decstr overflow";
int ret = jio_snprintf(decstr + written, sizeof(decstr) - written, "%s%s",
written == 0 ? "" : ",",
((k + j) % 2 == 0) ? decorator_name_array[k] : decorator_abbr_array[k]);
ASSERT_NE(-1, ret);
written += ret;
}
EXPECT_TRUE(decorators.parse(decstr)) << "Valid decorator selection did not parse: " << decstr;
assert_decorations_between(&decorators, i, j);
written = 0;
decstr[0] = '\0';
}
}
}
TEST(LogDecorators, combine_with) {
LogDecorators dec1;
LogDecorators dec2;
// Select first and third decorator for dec1
char input[64];
os::snprintf_checked(input, sizeof(input), "%s,%s", decorator_name_array[0], decorator_name_array[3]);
dec1.parse(input);
EXPECT_TRUE(dec1.is_decorator(decorator_array[0]));
EXPECT_TRUE(dec1.is_decorator(decorator_array[3]));
// Select the default decorators for dec2
EXPECT_FALSE(dec2.is_decorator(decorator_array[0]));
EXPECT_FALSE(dec2.is_decorator(decorator_array[3]));
assert_default_decorators(&dec2);
// Combine and verify that the combination includes first, third and default decorators
dec2.combine_with(dec1);
EXPECT_TRUE(dec2.is_decorator(decorator_array[0]));
EXPECT_TRUE(dec2.is_decorator(decorator_array[3]));
assert_default_decorators(&dec2, false);
}
TEST(LogDecorators, clear) {
// Start with default decorators and then clear it
LogDecorators dec;
EXPECT_FALSE(dec.is_empty());
dec.clear();
EXPECT_TRUE(dec.is_empty());
for (size_t i = 0; i < LogDecorators::Count; i++) {
EXPECT_FALSE(dec.is_decorator(decorator_array[i]));
}
}
// Test the decorator constant None
TEST(LogDecorators, none) {
LogDecorators dec = LogDecorators::None;
for (size_t i = 0; i < LogDecorators::Count; i++) {
EXPECT_FALSE(dec.is_decorator(decorator_array[i]));
}
}
TEST(LogDecorators, all) {
LogDecorators dec = LogDecorators::All;
for (size_t i = 0; i < LogDecorators::Count; i++) {
EXPECT_TRUE(dec.is_decorator(decorator_array[i]));
}
}
TEST(LogDecorators, is_empty) {
LogDecorators def, none = LogDecorators::None;
EXPECT_FALSE(def.is_empty());
EXPECT_TRUE(none.is_empty());
}
|