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