xref: /qemu/tests/unit/test-logging.c (revision bd6fee9f1263dc5ba487c7ac57d33a727af63c00)
13514552eSAlex Bennée /*
23514552eSAlex Bennée  * logging unit-tests
33514552eSAlex Bennée  *
43514552eSAlex Bennée  * Copyright (C) 2016 Linaro Ltd.
53514552eSAlex Bennée  *
63514552eSAlex Bennée  *  Author: Alex Bennée <alex.bennee@linaro.org>
73514552eSAlex Bennée  *
83514552eSAlex Bennée  * Permission is hereby granted, free of charge, to any person obtaining a copy
93514552eSAlex Bennée  * of this software and associated documentation files (the "Software"), to deal
103514552eSAlex Bennée  * in the Software without restriction, including without limitation the rights
113514552eSAlex Bennée  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
123514552eSAlex Bennée  * copies of the Software, and to permit persons to whom the Software is
133514552eSAlex Bennée  * furnished to do so, subject to the following conditions:
143514552eSAlex Bennée  *
153514552eSAlex Bennée  * The above copyright notice and this permission notice shall be included in
163514552eSAlex Bennée  * all copies or substantial portions of the Software.
173514552eSAlex Bennée  *
183514552eSAlex Bennée  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
193514552eSAlex Bennée  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
203514552eSAlex Bennée  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
213514552eSAlex Bennée  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
223514552eSAlex Bennée  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
233514552eSAlex Bennée  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
243514552eSAlex Bennée  * THE SOFTWARE.
253514552eSAlex Bennée  */
263514552eSAlex Bennée 
273514552eSAlex Bennée #include "qemu/osdep.h"
283514552eSAlex Bennée 
293514552eSAlex Bennée #include "qemu-common.h"
30*bd6fee9fSMarkus Armbruster #include "qapi/error.h"
31*bd6fee9fSMarkus Armbruster #include "qemu/log.h"
323514552eSAlex Bennée 
333514552eSAlex Bennée static void test_parse_range(void)
343514552eSAlex Bennée {
35*bd6fee9fSMarkus Armbruster     Error *err = NULL;
36*bd6fee9fSMarkus Armbruster 
37*bd6fee9fSMarkus Armbruster     qemu_set_dfilter_ranges("0x1000+0x100", &error_abort);
383514552eSAlex Bennée 
393514552eSAlex Bennée     g_assert_false(qemu_log_in_addr_range(0xfff));
403514552eSAlex Bennée     g_assert(qemu_log_in_addr_range(0x1000));
413514552eSAlex Bennée     g_assert(qemu_log_in_addr_range(0x1001));
423514552eSAlex Bennée     g_assert(qemu_log_in_addr_range(0x10ff));
433514552eSAlex Bennée     g_assert_false(qemu_log_in_addr_range(0x1100));
443514552eSAlex Bennée 
45*bd6fee9fSMarkus Armbruster     qemu_set_dfilter_ranges("0x1000-0x100", &error_abort);
463514552eSAlex Bennée 
473514552eSAlex Bennée     g_assert_false(qemu_log_in_addr_range(0x1001));
483514552eSAlex Bennée     g_assert(qemu_log_in_addr_range(0x1000));
493514552eSAlex Bennée     g_assert(qemu_log_in_addr_range(0x0f01));
503514552eSAlex Bennée     g_assert_false(qemu_log_in_addr_range(0x0f00));
513514552eSAlex Bennée 
52*bd6fee9fSMarkus Armbruster     qemu_set_dfilter_ranges("0x1000..0x1100", &error_abort);
533514552eSAlex Bennée 
543514552eSAlex Bennée     g_assert_false(qemu_log_in_addr_range(0xfff));
553514552eSAlex Bennée     g_assert(qemu_log_in_addr_range(0x1000));
563514552eSAlex Bennée     g_assert(qemu_log_in_addr_range(0x1100));
573514552eSAlex Bennée     g_assert_false(qemu_log_in_addr_range(0x1101));
583514552eSAlex Bennée 
59*bd6fee9fSMarkus Armbruster     qemu_set_dfilter_ranges("0x1000..0x1000", &error_abort);
603514552eSAlex Bennée 
613514552eSAlex Bennée     g_assert_false(qemu_log_in_addr_range(0xfff));
623514552eSAlex Bennée     g_assert(qemu_log_in_addr_range(0x1000));
633514552eSAlex Bennée     g_assert_false(qemu_log_in_addr_range(0x1001));
643514552eSAlex Bennée 
65*bd6fee9fSMarkus Armbruster     qemu_set_dfilter_ranges("0x1000+0x100,0x2100-0x100,0x3000..0x3100",
66*bd6fee9fSMarkus Armbruster                             &error_abort);
673514552eSAlex Bennée     g_assert(qemu_log_in_addr_range(0x1050));
683514552eSAlex Bennée     g_assert(qemu_log_in_addr_range(0x2050));
693514552eSAlex Bennée     g_assert(qemu_log_in_addr_range(0x3050));
70*bd6fee9fSMarkus Armbruster 
71*bd6fee9fSMarkus Armbruster     qemu_set_dfilter_ranges("0x1000+onehundred", &err);
72*bd6fee9fSMarkus Armbruster     error_free_or_abort(&err);
73*bd6fee9fSMarkus Armbruster 
74*bd6fee9fSMarkus Armbruster     qemu_set_dfilter_ranges("0x1000+0", &err);
75*bd6fee9fSMarkus Armbruster     error_free_or_abort(&err);
763514552eSAlex Bennée }
773514552eSAlex Bennée 
783514552eSAlex Bennée #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
79f6880b7fSAlex Bennée /* As the only real failure from a bad log filename path spec is
80f6880b7fSAlex Bennée  * reporting to the user we have to use the g_test_trap_subprocess
81f6880b7fSAlex Bennée  * mechanism and check no errors reported on stderr.
82f6880b7fSAlex Bennée  */
83f6880b7fSAlex Bennée static void test_parse_path_subprocess(void)
84f6880b7fSAlex Bennée {
85f6880b7fSAlex Bennée     /* All these should work without issue */
86f6880b7fSAlex Bennée     qemu_set_log_filename("/tmp/qemu.log");
87f6880b7fSAlex Bennée     qemu_set_log_filename("/tmp/qemu-%d.log");
88f6880b7fSAlex Bennée     qemu_set_log_filename("/tmp/qemu.log.%d");
89f6880b7fSAlex Bennée }
90f6880b7fSAlex Bennée static void test_parse_path(void)
91f6880b7fSAlex Bennée {
92f6880b7fSAlex Bennée     g_test_trap_subprocess ("/logging/parse_path/subprocess", 0, 0);
93f6880b7fSAlex Bennée     g_test_trap_assert_passed();
94f6880b7fSAlex Bennée     g_test_trap_assert_stdout("");
95f6880b7fSAlex Bennée     g_test_trap_assert_stderr("");
96f6880b7fSAlex Bennée }
97f6880b7fSAlex Bennée static void test_parse_invalid_path_subprocess(void)
98f6880b7fSAlex Bennée {
99f6880b7fSAlex Bennée     qemu_set_log_filename("/tmp/qemu-%d%d.log");
100f6880b7fSAlex Bennée }
101f6880b7fSAlex Bennée static void test_parse_invalid_path(void)
102f6880b7fSAlex Bennée {
103f6880b7fSAlex Bennée     g_test_trap_subprocess ("/logging/parse_invalid_path/subprocess", 0, 0);
104f6880b7fSAlex Bennée     g_test_trap_assert_passed();
105f6880b7fSAlex Bennée     g_test_trap_assert_stdout("");
106f6880b7fSAlex Bennée     g_test_trap_assert_stderr("Bad logfile format: /tmp/qemu-%d%d.log\n");
107f6880b7fSAlex Bennée }
108f6880b7fSAlex Bennée #endif /* CONFIG_HAS_GLIB_SUBPROCESS_TESTS */
1093514552eSAlex Bennée 
1103514552eSAlex Bennée int main(int argc, char **argv)
1113514552eSAlex Bennée {
1123514552eSAlex Bennée     g_test_init(&argc, &argv, NULL);
1133514552eSAlex Bennée 
1143514552eSAlex Bennée     g_test_add_func("/logging/parse_range", test_parse_range);
1153514552eSAlex Bennée #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
116f6880b7fSAlex Bennée     g_test_add_func("/logging/parse_path", test_parse_path);
117f6880b7fSAlex Bennée     g_test_add_func("/logging/parse_path/subprocess", test_parse_path_subprocess);
118f6880b7fSAlex Bennée     g_test_add_func("/logging/parse_invalid_path", test_parse_invalid_path);
119f6880b7fSAlex Bennée     g_test_add_func("/logging/parse_invalid_path/subprocess", test_parse_invalid_path_subprocess);
1203514552eSAlex Bennée #endif
1213514552eSAlex Bennée 
1223514552eSAlex Bennée     return g_test_run();
1233514552eSAlex Bennée }
124