| 1 | /* | 
|---|
| 2 | * Copyright (c) 2016, 2019, 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 "logging/log.hpp" | 
|---|
| 25 | #include "logging/logConfiguration.hpp" | 
|---|
| 26 | #include "logging/logStream.hpp" | 
|---|
| 27 | #include "memory/resourceArea.hpp" | 
|---|
| 28 | #include "runtime/os.hpp" | 
|---|
| 29 | #include "unittest.hpp" | 
|---|
| 30 |  | 
|---|
| 31 | #define LOG_TEST_STRING_LITERAL "a (hopefully) unique log message for testing" | 
|---|
| 32 |  | 
|---|
| 33 | static const char* invalid_selection_substr[] = { | 
|---|
| 34 | "=", "+", " ", "+=", "+=*", "*+", " +", "**", "++", ".", ",", ",," ",+", | 
|---|
| 35 | " *", "all+", "all*", "+all", "+all=Warning", "==Info", "=InfoWarning", | 
|---|
| 36 | "BadTag+", "logging++", "logging*+", ",=", "gc+gc+" | 
|---|
| 37 | }; | 
|---|
| 38 |  | 
|---|
| 39 | static inline bool string_contains_substring(const char* haystack, const char* needle) { | 
|---|
| 40 | return strstr(haystack, needle) != NULL; | 
|---|
| 41 | } | 
|---|
| 42 |  | 
|---|
| 43 | static inline bool file_exists(const char* filename) { | 
|---|
| 44 | struct stat st; | 
|---|
| 45 | return os::stat(filename, &st) == 0; | 
|---|
| 46 | } | 
|---|
| 47 |  | 
|---|
| 48 | static inline void delete_file(const char* filename) { | 
|---|
| 49 | if (!file_exists(filename)) { | 
|---|
| 50 | return; | 
|---|
| 51 | } | 
|---|
| 52 | int ret = remove(filename); | 
|---|
| 53 | EXPECT_TRUE(ret == 0 || errno == ENOENT) << "failed to remove file '"<< filename << "': " | 
|---|
| 54 | << os::strerror(errno) << " ("<< errno << ")"; | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | static inline void create_directory(const char* name) { | 
|---|
| 58 | assert(!file_exists(name), "can't create directory: %s already exists", name); | 
|---|
| 59 | bool failed; | 
|---|
| 60 | #ifdef _WINDOWS | 
|---|
| 61 | failed = !CreateDirectory(name, NULL); | 
|---|
| 62 | #else | 
|---|
| 63 | failed = mkdir(name, 0777); | 
|---|
| 64 | #endif | 
|---|
| 65 | assert(!failed, "failed to create directory %s", name); | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | static inline void delete_empty_directory(const char* name) { | 
|---|
| 69 | #ifdef _WINDOWS | 
|---|
| 70 | if (!file_exists(name)) { | 
|---|
| 71 | return; | 
|---|
| 72 | } | 
|---|
| 73 | bool failed; | 
|---|
| 74 | failed = !RemoveDirectory(name); | 
|---|
| 75 | EXPECT_FALSE(failed) << "failed to remove directory '"<< name | 
|---|
| 76 | << "': LastError = "<< GetLastError(); | 
|---|
| 77 | #else | 
|---|
| 78 | delete_file(name); | 
|---|
| 79 | #endif | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | static inline void init_log_file(const char* filename, const char* options = "") { | 
|---|
| 83 | LogStreamHandle(Error, logging) stream; | 
|---|
| 84 | bool success = LogConfiguration::parse_log_arguments(filename, "logging=trace", "", options, &stream); | 
|---|
| 85 | guarantee(success, "Failed to initialize log file '%s' with options '%s'", filename, options); | 
|---|
| 86 | log_debug(logging)( "%s", LOG_TEST_STRING_LITERAL); | 
|---|
| 87 | success = LogConfiguration::parse_log_arguments(filename, "all=off", "", "", &stream); | 
|---|
| 88 | guarantee(success, "Failed to disable logging to file '%s'", filename); | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | static const char* tmp_dir = os::get_temp_directory(); | 
|---|
| 92 | static const char* file_sep = os::file_separator(); | 
|---|
| 93 |  | 
|---|
| 94 | // Prepend filename with the temp directory and pid and return the result as a | 
|---|
| 95 | // resource allocated string. | 
|---|
| 96 | static inline char* prepend_temp_dir(const char* filename) { | 
|---|
| 97 | size_t temp_file_len = strlen(tmp_dir) + strlen(file_sep) + strlen(filename) + 28; | 
|---|
| 98 | char* temp_file = NEW_RESOURCE_ARRAY(char, temp_file_len); | 
|---|
| 99 | int ret = jio_snprintf(temp_file, temp_file_len, "%s%spid%d.%s", | 
|---|
| 100 | tmp_dir, file_sep, | 
|---|
| 101 | os::current_process_id(), filename); | 
|---|
| 102 | return temp_file; | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | // Prepend filename with specified prefix and the temp directory and return the | 
|---|
| 106 | // result as a malloc allocated string.  This is used by test_logFileOutput.cpp. | 
|---|
| 107 | static inline char* prepend_prefix_temp_dir(const char* prefix, const char* filename) { | 
|---|
| 108 | size_t temp_file_len = strlen(prefix) + strlen(tmp_dir) + strlen(file_sep) + strlen(filename) + 1; | 
|---|
| 109 | char* temp_file = (char*)os::malloc(temp_file_len, mtLogging); | 
|---|
| 110 | int ret = jio_snprintf(temp_file, temp_file_len, "%s%s%s%s", | 
|---|
| 111 | prefix, tmp_dir, file_sep, filename); | 
|---|
| 112 | return temp_file; | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 | // Read a complete line from fp and return it as a resource allocated string. | 
|---|
| 116 | // Returns NULL on EOF. | 
|---|
| 117 | static inline char* read_line(FILE* fp) { | 
|---|
| 118 | assert(fp != NULL, "invalid fp"); | 
|---|
| 119 | int buflen = 512; | 
|---|
| 120 | char* buf = NEW_RESOURCE_ARRAY(char, buflen); | 
|---|
| 121 | long pos = ftell(fp); | 
|---|
| 122 | if (pos < 0) return NULL; | 
|---|
| 123 |  | 
|---|
| 124 | char* ret = fgets(buf, buflen, fp); | 
|---|
| 125 | while (ret != NULL && buf[strlen(buf) - 1] != '\n' && !feof(fp)) { | 
|---|
| 126 | // retry with a larger buffer | 
|---|
| 127 | buf = REALLOC_RESOURCE_ARRAY(char, buf, buflen, buflen * 2); | 
|---|
| 128 | buflen *= 2; | 
|---|
| 129 | // rewind to beginning of line | 
|---|
| 130 | fseek(fp, pos, SEEK_SET); | 
|---|
| 131 | // retry read with new buffer | 
|---|
| 132 | ret = fgets(buf, buflen, fp); | 
|---|
| 133 | } | 
|---|
| 134 | return ret; | 
|---|
| 135 | } | 
|---|
| 136 |  | 
|---|
| 137 | static bool file_contains_substrings_in_order(const char* filename, const char* substrs[]) { | 
|---|
| 138 | FILE* fp = fopen(filename, "r"); | 
|---|
| 139 | assert(fp != NULL, "error opening file %s: %s", filename, strerror(errno)); | 
|---|
| 140 |  | 
|---|
| 141 | size_t idx = 0; | 
|---|
| 142 | while (substrs[idx] != NULL) { | 
|---|
| 143 | ResourceMark rm; | 
|---|
| 144 | char* line = read_line(fp); | 
|---|
| 145 | if (line == NULL) { | 
|---|
| 146 | break; | 
|---|
| 147 | } | 
|---|
| 148 | for (char* match = strstr(line, substrs[idx]); match != NULL;) { | 
|---|
| 149 | size_t match_len = strlen(substrs[idx]); | 
|---|
| 150 | idx++; | 
|---|
| 151 | if (substrs[idx] == NULL) { | 
|---|
| 152 | break; | 
|---|
| 153 | } | 
|---|
| 154 | match = strstr(match + match_len, substrs[idx]); | 
|---|
| 155 | } | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | fclose(fp); | 
|---|
| 159 | return substrs[idx] == NULL; | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 | static inline bool file_contains_substring(const char* filename, const char* substr) { | 
|---|
| 163 | const char* strs[] = {substr, NULL}; | 
|---|
| 164 | return file_contains_substrings_in_order(filename, strs); | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|