]>
Commit | Line | Data |
---|---|---|
3514552e AB |
1 | /* |
2 | * logging unit-tests | |
3 | * | |
4 | * Copyright (C) 2016 Linaro Ltd. | |
5 | * | |
6 | * Author: Alex Bennée <[email protected]> | |
7 | * | |
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
9 | * of this software and associated documentation files (the "Software"), to deal | |
10 | * in the Software without restriction, including without limitation the rights | |
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
12 | * copies of the Software, and to permit persons to whom the Software is | |
13 | * furnished to do so, subject to the following conditions: | |
14 | * | |
15 | * The above copyright notice and this permission notice shall be included in | |
16 | * all copies or substantial portions of the Software. | |
17 | * | |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
21 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
24 | * THE SOFTWARE. | |
25 | */ | |
26 | ||
27 | #include "qemu/osdep.h" | |
5f9f818e | 28 | #include <glib/gstdio.h> |
3514552e AB |
29 | |
30 | #include "qemu-common.h" | |
bd6fee9f MA |
31 | #include "qapi/error.h" |
32 | #include "qemu/log.h" | |
3514552e AB |
33 | |
34 | static void test_parse_range(void) | |
35 | { | |
bd6fee9f MA |
36 | Error *err = NULL; |
37 | ||
38 | qemu_set_dfilter_ranges("0x1000+0x100", &error_abort); | |
3514552e AB |
39 | |
40 | g_assert_false(qemu_log_in_addr_range(0xfff)); | |
41 | g_assert(qemu_log_in_addr_range(0x1000)); | |
42 | g_assert(qemu_log_in_addr_range(0x1001)); | |
43 | g_assert(qemu_log_in_addr_range(0x10ff)); | |
44 | g_assert_false(qemu_log_in_addr_range(0x1100)); | |
45 | ||
bd6fee9f | 46 | qemu_set_dfilter_ranges("0x1000-0x100", &error_abort); |
3514552e AB |
47 | |
48 | g_assert_false(qemu_log_in_addr_range(0x1001)); | |
49 | g_assert(qemu_log_in_addr_range(0x1000)); | |
50 | g_assert(qemu_log_in_addr_range(0x0f01)); | |
51 | g_assert_false(qemu_log_in_addr_range(0x0f00)); | |
52 | ||
bd6fee9f | 53 | qemu_set_dfilter_ranges("0x1000..0x1100", &error_abort); |
3514552e AB |
54 | |
55 | g_assert_false(qemu_log_in_addr_range(0xfff)); | |
56 | g_assert(qemu_log_in_addr_range(0x1000)); | |
57 | g_assert(qemu_log_in_addr_range(0x1100)); | |
58 | g_assert_false(qemu_log_in_addr_range(0x1101)); | |
59 | ||
bd6fee9f | 60 | qemu_set_dfilter_ranges("0x1000..0x1000", &error_abort); |
3514552e AB |
61 | |
62 | g_assert_false(qemu_log_in_addr_range(0xfff)); | |
63 | g_assert(qemu_log_in_addr_range(0x1000)); | |
64 | g_assert_false(qemu_log_in_addr_range(0x1001)); | |
65 | ||
bd6fee9f MA |
66 | qemu_set_dfilter_ranges("0x1000+0x100,0x2100-0x100,0x3000..0x3100", |
67 | &error_abort); | |
3514552e AB |
68 | g_assert(qemu_log_in_addr_range(0x1050)); |
69 | g_assert(qemu_log_in_addr_range(0x2050)); | |
70 | g_assert(qemu_log_in_addr_range(0x3050)); | |
3514552e | 71 | |
58e19e6e MA |
72 | qemu_set_dfilter_ranges("0xffffffffffffffff-1", &error_abort); |
73 | g_assert(qemu_log_in_addr_range(UINT64_MAX)); | |
74 | g_assert_false(qemu_log_in_addr_range(UINT64_MAX - 1)); | |
75 | ||
76 | qemu_set_dfilter_ranges("0..0xffffffffffffffff", &err); | |
58eeb83c MA |
77 | g_assert(qemu_log_in_addr_range(0)); |
78 | g_assert(qemu_log_in_addr_range(UINT64_MAX)); | |
79 | ||
58e19e6e MA |
80 | qemu_set_dfilter_ranges("2..1", &err); |
81 | error_free_or_abort(&err); | |
82 | ||
bd6fee9f MA |
83 | qemu_set_dfilter_ranges("0x1000+onehundred", &err); |
84 | error_free_or_abort(&err); | |
85 | ||
86 | qemu_set_dfilter_ranges("0x1000+0", &err); | |
87 | error_free_or_abort(&err); | |
3514552e | 88 | } |
f6880b7f | 89 | |
5f9f818e | 90 | static void set_log_path_tmp(char const *dir, char const *tpl, Error **errp) |
f6880b7f | 91 | { |
5f9f818e SS |
92 | gchar *file_path = g_build_filename(dir, tpl, NULL); |
93 | ||
94 | qemu_set_log_filename(file_path, errp); | |
95 | g_free(file_path); | |
96 | } | |
97 | ||
98 | static void test_parse_path(gconstpointer data) | |
99 | { | |
100 | gchar const *tmp_path = data; | |
daa76aa4 MA |
101 | Error *err = NULL; |
102 | ||
5f9f818e SS |
103 | set_log_path_tmp(tmp_path, "qemu.log", &error_abort); |
104 | set_log_path_tmp(tmp_path, "qemu-%d.log", &error_abort); | |
105 | set_log_path_tmp(tmp_path, "qemu.log.%d", &error_abort); | |
daa76aa4 | 106 | |
5f9f818e | 107 | set_log_path_tmp(tmp_path, "qemu-%d%d.log", &err); |
daa76aa4 | 108 | error_free_or_abort(&err); |
f6880b7f | 109 | } |
3514552e | 110 | |
fb47fc69 RF |
111 | static void test_logfile_write(gconstpointer data) |
112 | { | |
113 | QemuLogFile *logfile; | |
114 | QemuLogFile *logfile2; | |
115 | gchar const *dir = data; | |
116 | Error *err = NULL; | |
24f7eeed MR |
117 | g_autofree gchar *file_path = NULL; |
118 | g_autofree gchar *file_path1 = NULL; | |
fb47fc69 RF |
119 | FILE *orig_fd; |
120 | ||
121 | /* | |
122 | * Before starting test, set log flags, to ensure the file gets | |
123 | * opened below with the call to qemu_set_log_filename(). | |
124 | * In cases where a logging backend other than log is used, | |
125 | * this is needed. | |
126 | */ | |
127 | qemu_set_log(CPU_LOG_TB_OUT_ASM); | |
128 | file_path = g_build_filename(dir, "qemu_test_log_write0.log", NULL); | |
129 | file_path1 = g_build_filename(dir, "qemu_test_log_write1.log", NULL); | |
130 | ||
131 | /* | |
132 | * Test that even if an open file handle is changed, | |
133 | * our handle remains valid due to RCU. | |
134 | */ | |
135 | qemu_set_log_filename(file_path, &err); | |
136 | g_assert(!err); | |
137 | rcu_read_lock(); | |
138 | logfile = atomic_rcu_read(&qemu_logfile); | |
139 | orig_fd = logfile->fd; | |
140 | g_assert(logfile && logfile->fd); | |
141 | fprintf(logfile->fd, "%s 1st write to file\n", __func__); | |
142 | fflush(logfile->fd); | |
143 | ||
144 | /* Change the logfile and ensure that the handle is still valid. */ | |
145 | qemu_set_log_filename(file_path1, &err); | |
146 | g_assert(!err); | |
147 | logfile2 = atomic_rcu_read(&qemu_logfile); | |
148 | g_assert(logfile->fd == orig_fd); | |
149 | g_assert(logfile2->fd != logfile->fd); | |
150 | fprintf(logfile->fd, "%s 2nd write to file\n", __func__); | |
151 | fflush(logfile->fd); | |
152 | rcu_read_unlock(); | |
153 | } | |
154 | ||
155 | static void test_logfile_lock(gconstpointer data) | |
156 | { | |
157 | FILE *logfile; | |
158 | gchar const *dir = data; | |
159 | Error *err = NULL; | |
24f7eeed | 160 | g_autofree gchar *file_path = NULL; |
fb47fc69 RF |
161 | |
162 | file_path = g_build_filename(dir, "qemu_test_logfile_lock0.log", NULL); | |
163 | ||
164 | /* | |
165 | * Test the use of the logfile lock, such | |
166 | * that even if an open file handle is closed, | |
167 | * our handle remains valid for use due to RCU. | |
168 | */ | |
169 | qemu_set_log_filename(file_path, &err); | |
170 | logfile = qemu_log_lock(); | |
171 | g_assert(logfile); | |
172 | fprintf(logfile, "%s 1st write to file\n", __func__); | |
173 | fflush(logfile); | |
174 | ||
175 | /* | |
176 | * Initiate a close file and make sure our handle remains | |
177 | * valid since we still have the logfile lock. | |
178 | */ | |
179 | qemu_log_close(); | |
180 | fprintf(logfile, "%s 2nd write to file\n", __func__); | |
181 | fflush(logfile); | |
182 | qemu_log_unlock(logfile); | |
183 | ||
184 | g_assert(!err); | |
185 | } | |
186 | ||
5f9f818e SS |
187 | /* Remove a directory and all its entries (non-recursive). */ |
188 | static void rmdir_full(gchar const *root) | |
189 | { | |
190 | GDir *root_gdir = g_dir_open(root, 0, NULL); | |
191 | gchar const *entry_name; | |
192 | ||
193 | g_assert_nonnull(root_gdir); | |
194 | while ((entry_name = g_dir_read_name(root_gdir)) != NULL) { | |
195 | gchar *entry_path = g_build_filename(root, entry_name, NULL); | |
196 | g_assert(g_remove(entry_path) == 0); | |
197 | g_free(entry_path); | |
198 | } | |
199 | g_dir_close(root_gdir); | |
200 | g_assert(g_rmdir(root) == 0); | |
201 | } | |
202 | ||
3514552e AB |
203 | int main(int argc, char **argv) |
204 | { | |
5f9f818e SS |
205 | gchar *tmp_path = g_dir_make_tmp("qemu-test-logging.XXXXXX", NULL); |
206 | int rc; | |
207 | ||
3514552e | 208 | g_test_init(&argc, &argv, NULL); |
5f9f818e | 209 | g_assert_nonnull(tmp_path); |
3514552e AB |
210 | |
211 | g_test_add_func("/logging/parse_range", test_parse_range); | |
5f9f818e | 212 | g_test_add_data_func("/logging/parse_path", tmp_path, test_parse_path); |
fb47fc69 RF |
213 | g_test_add_data_func("/logging/logfile_write_path", |
214 | tmp_path, test_logfile_write); | |
215 | g_test_add_data_func("/logging/logfile_lock_path", | |
216 | tmp_path, test_logfile_lock); | |
5f9f818e SS |
217 | |
218 | rc = g_test_run(); | |
3514552e | 219 | |
5f9f818e SS |
220 | rmdir_full(tmp_path); |
221 | g_free(tmp_path); | |
222 | return rc; | |
3514552e | 223 | } |