Path: blob/master/test/hotspot/gtest/logging/test_logSelection.cpp
41144 views
/*1* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223#include "precompiled.hpp"24#include "jvm.h"25#include "logging/logLevel.hpp"26#include "logging/logSelection.hpp"27#include "logging/logTagSet.hpp"28#include "utilities/globalDefinitions.hpp"29#include "logTestUtils.inline.hpp"30#include "unittest.hpp"3132// These tests can only run in debug VMs because they rely on the (debug-only) LogTag::_test33#ifdef ASSERT3435#define NON_EXISTING_TAG_SET "logging+test+start+exit+safepoint"3637// let google test know how to print LogSelection nicely for better error messages38void PrintTo(const LogSelection& sel, ::std::ostream* os) {39if (sel == LogSelection::Invalid) {40*os << "LogSelection::Invalid";41return;42}43char buf[256];44sel.describe(buf, sizeof(buf));45*os << buf;46}4748TEST(LogSelection, sanity) {49LogTagType tags[LogTag::MaxTags] = { PREFIX_LOG_TAG(logging), PREFIX_LOG_TAG(test), PREFIX_LOG_TAG(_NO_TAG) };50LogSelection selection(tags, false, LogLevel::Trace);5152EXPECT_EQ(2u, selection.ntags());53EXPECT_EQ(LogLevel::Trace, selection.level());5455// Verify that copying the selection also works as expected56LogSelection copy = selection;57EXPECT_EQ(2u, copy.ntags());58EXPECT_EQ(LogLevel::Trace, copy.level());5960tags[0] = PREFIX_LOG_TAG(gc);61tags[1] = PREFIX_LOG_TAG(_NO_TAG);62LogSelection copy2(tags, true, LogLevel::Off); // start with a completely different selection63copy2 = selection; // and test copy assignment64EXPECT_EQ(2u, copy2.ntags());65EXPECT_EQ(LogLevel::Trace, copy2.level());66}6768TEST(LogSelection, tag_sets_selected) {69LogTagType tags[LogTag::MaxTags] = { PREFIX_LOG_TAG(logging), PREFIX_LOG_TAG(test), PREFIX_LOG_TAG(_NO_TAG) };70LogSelection selection(tags, false, LogLevel::Trace);7172EXPECT_EQ(1u, selection.tag_sets_selected()) << "there should be a single (it's not a wildcard selection) "73"tag set selected by this (in gtest libjvm)";7475EXPECT_EQ(LogTagSet::ntagsets(), LogSelection::parse("all").tag_sets_selected()) << "all should select every tag set";76EXPECT_EQ(0u, LogSelection::parse(NON_EXISTING_TAG_SET).tag_sets_selected()) <<77"(assuming the tag set doesn't exist) the selection shouldn't select any tag sets";78}7980static const char* valid_expression[] = {81"all", "gc", "gc+logging", "logging+gc", "logging+gc*", "gc=trace",82"logging+gc=trace", "logging*", "logging*=info", "gc+logging*=error"83};8485TEST(LogSelection, parse) {86LogTagType tags[LogTag::MaxTags] = { PREFIX_LOG_TAG(logging), PREFIX_LOG_TAG(test), PREFIX_LOG_TAG(_NO_TAG) };87LogSelection selection(tags, true, LogLevel::Off);88LogSelection parsed = LogSelection::parse("logging+test*=off");89EXPECT_EQ(selection, parsed) << "parsed selection not equal to programmatically constructed";9091// Verify valid expressions parse without problems92for (size_t i = 0; i < ARRAY_SIZE(valid_expression); i++) {93EXPECT_NE(LogSelection::Invalid, LogSelection::parse(valid_expression[i])) <<94"Valid expression '" << valid_expression[i] << "' did not parse";95}9697// Test 'all' with each level98for (LogLevelType level = LogLevel::First; level <= LogLevel::Last; level = static_cast<LogLevelType>(level + 1)) {99char buf[64];100int ret = jio_snprintf(buf, sizeof(buf), "all=%s", LogLevel::name(level));101ASSERT_NE(-1, ret);102103LogSelection sel = LogSelection::parse(buf);104EXPECT_EQ(LogTagSet::ntagsets(), sel.tag_sets_selected()) << "'all' should select all tag sets";105EXPECT_EQ(level, sel.level());106}107108// Test with 5 tags109LogTagType expected_tags[] = { PREFIX_LOG_TAG(logging), PREFIX_LOG_TAG(test), PREFIX_LOG_TAG(start),110PREFIX_LOG_TAG(exit), PREFIX_LOG_TAG(safepoint) };111LogSelection expected(expected_tags, false, LogLevel::Debug);112LogSelection five_tag_selection = LogSelection::parse("logging+test+start+exit+safepoint=debug");113EXPECT_EQ(5u, five_tag_selection.ntags()) << "parsed wrong number of tags";114EXPECT_EQ(expected, five_tag_selection);115EXPECT_EQ(LogLevel::Debug, five_tag_selection.level());116117// Test implicit level118selection = LogSelection::parse("logging");119EXPECT_EQ(LogLevel::Unspecified, selection.level()) << "parsed implicit level incorrectly";120EXPECT_EQ(1u, selection.ntags());121}122123TEST(LogSelection, parse_invalid) {124125// Attempt to parse an expression with too many tags126EXPECT_EQ(LogSelection::Invalid, LogSelection::parse(NON_EXISTING_TAG_SET "+gc"));127128// Construct a bunch of invalid expressions and verify that they don't parse129for (size_t i = 0; i < ARRAY_SIZE(valid_expression); i++) {130char buf[256];131for (size_t j = 0; j < ARRAY_SIZE(invalid_selection_substr); j++) {132// Prefix with invalid substr133jio_snprintf(buf, sizeof(buf), "%s%s", invalid_selection_substr[j], valid_expression[i]);134EXPECT_EQ(LogSelection::Invalid, LogSelection::parse(buf)) << "'" << buf << "'" << " considered legal";135136// Suffix with invalid substr137jio_snprintf(buf, sizeof(buf), "%s%s", valid_expression[i], invalid_selection_substr[j]);138EXPECT_EQ(LogSelection::Invalid, LogSelection::parse(buf)) << "'" << buf << "'" << " considered legal";139140// Use only the invalid substr141EXPECT_EQ(LogSelection::Invalid, LogSelection::parse(invalid_selection_substr[j])) <<142"'" << invalid_selection_substr[j] << "'" << " considered legal";143}144145// Suffix/prefix with some unique invalid prefixes/suffixes146jio_snprintf(buf, sizeof(buf), "*%s", valid_expression[i]);147EXPECT_EQ(LogSelection::Invalid, LogSelection::parse(buf)) << "'" << buf << "'" << " considered legal";148149jio_snprintf(buf, sizeof(buf), "logging*%s", valid_expression[i]);150EXPECT_EQ(LogSelection::Invalid, LogSelection::parse(buf)) << "'" << buf << "'" << " considered legal";151}152}153154TEST(LogSelection, equals) {155LogTagType tags[LogTag::MaxTags] = { PREFIX_LOG_TAG(logging), PREFIX_LOG_TAG(test), PREFIX_LOG_TAG(_NO_TAG) };156LogSelection selection(tags, true, LogLevel::Info);157LogSelection copy(tags, true, LogLevel::Info);158EXPECT_EQ(selection, selection);159EXPECT_EQ(selection, copy);160161tags[0] = PREFIX_LOG_TAG(gc);162LogSelection other_tags(tags, true, LogLevel::Info);163EXPECT_NE(selection, other_tags);164165tags[0] = PREFIX_LOG_TAG(test);166tags[1] = PREFIX_LOG_TAG(logging);167LogSelection reversed(tags, true, LogLevel::Info);168EXPECT_NE(selection, reversed);169170LogSelection no_wildcard(tags, false, LogLevel::Info);171EXPECT_NE(selection, no_wildcard);172173LogSelection different_level(tags, true, LogLevel::Warning);174EXPECT_NE(selection, different_level);175176tags[2] = PREFIX_LOG_TAG(gc);177tags[3] = PREFIX_LOG_TAG(_NO_TAG);178LogSelection more_tags(tags, true, LogLevel::Info);179EXPECT_NE(selection, more_tags);180181tags[1] = PREFIX_LOG_TAG(_NO_TAG);182LogSelection fewer_tags(tags, true, LogLevel::Info);183EXPECT_NE(selection, fewer_tags);184}185186TEST(LogSelection, consists_of) {187LogTagType tags[LogTag::MaxTags] = {188PREFIX_LOG_TAG(logging), PREFIX_LOG_TAG(test), PREFIX_LOG_TAG(_NO_TAG)189};190LogSelection s(tags, false, LogLevel::Off);191EXPECT_TRUE(s.consists_of(tags));192193tags[2] = PREFIX_LOG_TAG(safepoint);194EXPECT_FALSE(s.consists_of(tags));195196s = LogSelection(tags, true, LogLevel::Info);197EXPECT_TRUE(s.consists_of(tags));198}199200TEST(LogSelection, describe_tags) {201char buf[256];202LogTagType tags[LogTag::MaxTags] = { PREFIX_LOG_TAG(logging), PREFIX_LOG_TAG(test), PREFIX_LOG_TAG(_NO_TAG) };203LogSelection selection(tags, true, LogLevel::Off);204selection.describe_tags(buf, sizeof(buf));205EXPECT_STREQ("logging+test*", buf);206}207208TEST(LogSelection, describe) {209char buf[256];210LogTagType tags[LogTag::MaxTags] = { PREFIX_LOG_TAG(logging), PREFIX_LOG_TAG(test), PREFIX_LOG_TAG(_NO_TAG) };211LogSelection selection(tags, true, LogLevel::Off);212selection.describe(buf, sizeof(buf));213EXPECT_STREQ("logging+test*=off", buf);214}215216#endif217218219