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 "runtime/os.hpp"
25 #include "unittest.hpp"
26
27 #define LOG_TEST_STRING_LITERAL "a (hopefully) unique log message for testing"
28
29 static inline bool string_contains_substring(const char* haystack, const char* needle) {
30 return strstr(haystack, needle) != NULL;
31 }
32
33 static inline bool file_exists(const char* filename) {
34 struct stat st;
35 return os::stat(filename, &st) == 0;
36 }
37
38 static inline void delete_file(const char* filename) {
39 if (!file_exists(filename)) {
40 return;
41 }
42 int ret = remove(filename);
43 EXPECT_TRUE(ret == 0 || errno == ENOENT) << "failed to remove file '" << filename << "': "
44 << os::strerror(errno) << " (" << errno << ")";
45 }
|
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 inline bool string_contains_substring(const char* haystack, const char* needle) {
34 return strstr(haystack, needle) != NULL;
35 }
36
37 static inline bool file_exists(const char* filename) {
38 struct stat st;
39 return os::stat(filename, &st) == 0;
40 }
41
42 static inline void delete_file(const char* filename) {
43 if (!file_exists(filename)) {
44 return;
45 }
46 int ret = remove(filename);
47 EXPECT_TRUE(ret == 0 || errno == ENOENT) << "failed to remove file '" << filename << "': "
48 << os::strerror(errno) << " (" << errno << ")";
49 }
50
51 static inline void create_directory(const char* name) {
52 assert(!file_exists(name), "can't create directory: %s already exists", name);
53 bool failed;
54 #ifdef _WINDOWS
55 failed = !CreateDirectory(name, NULL);
56 #else
57 failed = mkdir(name, 0777);
58 #endif
59 assert(!failed, "failed to create directory %s", name);
60 }
61
62 static inline void init_log_file(const char* filename, const char* options = "") {
63 LogStreamHandle(Error, logging) stream;
64 bool success = LogConfiguration::parse_log_arguments(filename, "logging=trace", "", options, &stream);
65 guarantee(success, "Failed to initialize log file '%s' with options '%s'", filename, options);
66 log_debug(logging)("%s", LOG_TEST_STRING_LITERAL);
67 success = LogConfiguration::parse_log_arguments(filename, "all=off", "", "", &stream);
68 guarantee(success, "Failed to disable logging to file '%s'", filename);
69 }
70
71 // Read a complete line from fp and return it as a resource allocated string.
72 // Returns NULL on EOF.
73 static inline char* read_line(FILE* fp) {
74 assert(fp != NULL, "invalid fp");
75 int buflen = 512;
76 char* buf = NEW_RESOURCE_ARRAY(char, buflen);
77 long pos = ftell(fp);
78
79 char* ret = fgets(buf, buflen, fp);
80 while (ret != NULL && buf[strlen(buf) - 1] != '\n' && !feof(fp)) {
81 // retry with a larger buffer
82 buf = REALLOC_RESOURCE_ARRAY(char, buf, buflen, buflen * 2);
83 buflen *= 2;
84 // rewind to beginning of line
85 fseek(fp, pos, SEEK_SET);
86 // retry read with new buffer
87 ret = fgets(buf, buflen, fp);
88 }
89 return ret;
90 }
91
92 static bool file_contains_substrings_in_order(const char* filename, const char* substrs[]) {
93 FILE* fp = fopen(filename, "r");
94 assert(fp != NULL, "error opening file %s: %s", filename, strerror(errno));
95
96 size_t idx = 0;
97 while (substrs[idx] != NULL) {
98 ResourceMark rm;
99 char* line = read_line(fp);
100 if (line == NULL) {
101 break;
102 }
103 for (char* match = strstr(line, substrs[idx]); match != NULL;) {
104 size_t match_len = strlen(substrs[idx]);
105 idx++;
106 if (substrs[idx] == NULL) {
107 break;
108 }
109 match = strstr(match + match_len, substrs[idx]);
110 }
111 }
112
113 fclose(fp);
114 return substrs[idx] == NULL;
115 }
116
117 static inline bool file_contains_substring(const char* filename, const char* substr) {
118 const char* strs[] = {substr, NULL};
119 return file_contains_substrings_in_order(filename, strs);
120 }
|