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 "logging/logTag.hpp"
26 #include "utilities/ostream.hpp"
27 #include "unittest.hpp"
28 
TEST(LogTag,from_string)29 TEST(LogTag, from_string) {
30   // Verify for all tags defined in LOG_TAG_LIST
31 #define LOG_TAG(tag) \
32   EXPECT_EQ(PREFIX_LOG_TAG(tag), LogTag::from_string(#tag));
33   LOG_TAG_LIST
34 #undef LOG_TAG
35 
36   // Verify a couple of invalid strings parsing as invalid tags
37   const char* invalid_tag[] = {
38     "bad tag", ".^@", "**", "*", "gcc", "+gc", "gc+", "gc+safepoint",
39     "gc+safepoint=warning", "warning", "=info", "gcsafepointlogging",
40     "gc+safepointlogging", "gclogging", "+", " gc", "logging ", ","
41   };
42   for (size_t i = 0; i < sizeof(invalid_tag) / sizeof(*invalid_tag); i++) {
43     EXPECT_EQ(LogTag::__NO_TAG, LogTag::from_string(invalid_tag[i]))
44         << "'" << invalid_tag[i] << "' did not parse as an invalid tag";
45   }
46 }
47 
TEST(LogTag,fuzzy_match)48 TEST(LogTag, fuzzy_match) {
49   for (size_t i = 1; i < LogTag::Count; i++) {
50     LogTagType tag = static_cast<LogTagType>(i);
51     EXPECT_EQ(tag, LogTag::fuzzy_match(LogTag::name(tag)));
52   }
53 
54   EXPECT_EQ(LogTag::_logging, LogTag::fuzzy_match("loggin"));
55   EXPECT_EQ(LogTag::_logging, LogTag::fuzzy_match("loging"));
56 
57   EXPECT_EQ(LogTag::__NO_TAG, LogTag::fuzzy_match("unrecognizabletag"));
58 }
59 
TEST(LogTag,name)60 TEST(LogTag, name) {
61   // Verify for each tag from the macro
62 #define LOG_TAG(tag) \
63   EXPECT_STREQ(#tag, LogTag::name(PREFIX_LOG_TAG(tag)));
64   LOG_TAG_LIST
65 #undef LOG_TAG
66 }
67 
TEST(LogTag,list_tags)68 TEST(LogTag, list_tags) {
69   char buf[LogTag::Count * 16] = {0};
70   stringStream ss(buf, sizeof(buf));
71   LogTag::list_tags(&ss);
72 
73   bool listed_tags[LogTag::Count] = { false };
74 
75   char* last_tag = NULL;
76   for (char* tag = buf; *tag != '\0';) {
77     char* end = strpbrk(tag, ",\n");
78     ASSERT_TRUE(end != NULL) <<  "line should end with newline";
79     *end = '\0';
80     if (*tag == ' ') {
81       tag++;
82     }
83 
84     EXPECT_TRUE(last_tag == NULL || strcmp(last_tag, tag) < 0) << tag << " should be listed before " << last_tag;
85     listed_tags[LogTag::from_string(tag)] = true;
86 
87     last_tag = tag;
88     tag = end + 1;
89   }
90 
91   for (size_t i = 1; i < LogTag::Count; i++) {
92     EXPECT_TRUE(listed_tags[i]) << "tag '" << LogTag::name(static_cast<LogTagType>(i)) << "' not listed!";
93   }
94 }
95