xref: /qemu/tests/unit/test-logging.c (revision f6880b7f48ffd4aa038d7a964a3820b1c18d5307)
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 #include <glib.h>
293514552eSAlex Bennée 
303514552eSAlex Bennée #include "qemu-common.h"
313514552eSAlex Bennée #include "include/qemu/log.h"
323514552eSAlex Bennée 
333514552eSAlex Bennée static void test_parse_range(void)
343514552eSAlex Bennée {
353514552eSAlex Bennée     qemu_set_dfilter_ranges("0x1000+0x100");
363514552eSAlex Bennée 
373514552eSAlex Bennée     g_assert_false(qemu_log_in_addr_range(0xfff));
383514552eSAlex Bennée     g_assert(qemu_log_in_addr_range(0x1000));
393514552eSAlex Bennée     g_assert(qemu_log_in_addr_range(0x1001));
403514552eSAlex Bennée     g_assert(qemu_log_in_addr_range(0x10ff));
413514552eSAlex Bennée     g_assert_false(qemu_log_in_addr_range(0x1100));
423514552eSAlex Bennée 
433514552eSAlex Bennée     qemu_set_dfilter_ranges("0x1000-0x100");
443514552eSAlex Bennée 
453514552eSAlex Bennée     g_assert_false(qemu_log_in_addr_range(0x1001));
463514552eSAlex Bennée     g_assert(qemu_log_in_addr_range(0x1000));
473514552eSAlex Bennée     g_assert(qemu_log_in_addr_range(0x0f01));
483514552eSAlex Bennée     g_assert_false(qemu_log_in_addr_range(0x0f00));
493514552eSAlex Bennée 
503514552eSAlex Bennée     qemu_set_dfilter_ranges("0x1000..0x1100");
513514552eSAlex Bennée 
523514552eSAlex Bennée     g_assert_false(qemu_log_in_addr_range(0xfff));
533514552eSAlex Bennée     g_assert(qemu_log_in_addr_range(0x1000));
543514552eSAlex Bennée     g_assert(qemu_log_in_addr_range(0x1100));
553514552eSAlex Bennée     g_assert_false(qemu_log_in_addr_range(0x1101));
563514552eSAlex Bennée 
573514552eSAlex Bennée     qemu_set_dfilter_ranges("0x1000..0x1000");
583514552eSAlex Bennée 
593514552eSAlex Bennée     g_assert_false(qemu_log_in_addr_range(0xfff));
603514552eSAlex Bennée     g_assert(qemu_log_in_addr_range(0x1000));
613514552eSAlex Bennée     g_assert_false(qemu_log_in_addr_range(0x1001));
623514552eSAlex Bennée 
633514552eSAlex Bennée     qemu_set_dfilter_ranges("0x1000+0x100,0x2100-0x100,0x3000..0x3100");
643514552eSAlex Bennée     g_assert(qemu_log_in_addr_range(0x1050));
653514552eSAlex Bennée     g_assert(qemu_log_in_addr_range(0x2050));
663514552eSAlex Bennée     g_assert(qemu_log_in_addr_range(0x3050));
673514552eSAlex Bennée }
683514552eSAlex Bennée 
693514552eSAlex Bennée #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
703514552eSAlex Bennée static void test_parse_invalid_range_subprocess(void)
713514552eSAlex Bennée {
723514552eSAlex Bennée     qemu_set_dfilter_ranges("0x1000+onehundred");
733514552eSAlex Bennée }
743514552eSAlex Bennée static void test_parse_invalid_range(void)
753514552eSAlex Bennée {
763514552eSAlex Bennée     g_test_trap_subprocess("/logging/parse_invalid_range/subprocess", 0, 0);
773514552eSAlex Bennée     g_test_trap_assert_failed();
783514552eSAlex Bennée     g_test_trap_assert_stdout("");
793514552eSAlex Bennée     g_test_trap_assert_stderr("*Failed to parse range in: 0x1000+onehundred\n");
803514552eSAlex Bennée }
813514552eSAlex Bennée static void test_parse_zero_range_subprocess(void)
823514552eSAlex Bennée {
833514552eSAlex Bennée     qemu_set_dfilter_ranges("0x1000+0");
843514552eSAlex Bennée }
853514552eSAlex Bennée static void test_parse_zero_range(void)
863514552eSAlex Bennée {
873514552eSAlex Bennée     g_test_trap_subprocess("/logging/parse_zero_range/subprocess", 0, 0);
883514552eSAlex Bennée     g_test_trap_assert_failed();
893514552eSAlex Bennée     g_test_trap_assert_stdout("");
903514552eSAlex Bennée     g_test_trap_assert_stderr("*Failed to parse range in: 0x1000+0\n");
913514552eSAlex Bennée }
92*f6880b7fSAlex Bennée 
93*f6880b7fSAlex Bennée /* As the only real failure from a bad log filename path spec is
94*f6880b7fSAlex Bennée  * reporting to the user we have to use the g_test_trap_subprocess
95*f6880b7fSAlex Bennée  * mechanism and check no errors reported on stderr.
96*f6880b7fSAlex Bennée  */
97*f6880b7fSAlex Bennée static void test_parse_path_subprocess(void)
98*f6880b7fSAlex Bennée {
99*f6880b7fSAlex Bennée     /* All these should work without issue */
100*f6880b7fSAlex Bennée     qemu_set_log_filename("/tmp/qemu.log");
101*f6880b7fSAlex Bennée     qemu_set_log_filename("/tmp/qemu-%d.log");
102*f6880b7fSAlex Bennée     qemu_set_log_filename("/tmp/qemu.log.%d");
103*f6880b7fSAlex Bennée }
104*f6880b7fSAlex Bennée static void test_parse_path(void)
105*f6880b7fSAlex Bennée {
106*f6880b7fSAlex Bennée     g_test_trap_subprocess ("/logging/parse_path/subprocess", 0, 0);
107*f6880b7fSAlex Bennée     g_test_trap_assert_passed();
108*f6880b7fSAlex Bennée     g_test_trap_assert_stdout("");
109*f6880b7fSAlex Bennée     g_test_trap_assert_stderr("");
110*f6880b7fSAlex Bennée }
111*f6880b7fSAlex Bennée static void test_parse_invalid_path_subprocess(void)
112*f6880b7fSAlex Bennée {
113*f6880b7fSAlex Bennée     qemu_set_log_filename("/tmp/qemu-%d%d.log");
114*f6880b7fSAlex Bennée }
115*f6880b7fSAlex Bennée static void test_parse_invalid_path(void)
116*f6880b7fSAlex Bennée {
117*f6880b7fSAlex Bennée     g_test_trap_subprocess ("/logging/parse_invalid_path/subprocess", 0, 0);
118*f6880b7fSAlex Bennée     g_test_trap_assert_passed();
119*f6880b7fSAlex Bennée     g_test_trap_assert_stdout("");
120*f6880b7fSAlex Bennée     g_test_trap_assert_stderr("Bad logfile format: /tmp/qemu-%d%d.log\n");
121*f6880b7fSAlex Bennée }
122*f6880b7fSAlex Bennée #endif /* CONFIG_HAS_GLIB_SUBPROCESS_TESTS */
1233514552eSAlex Bennée 
1243514552eSAlex Bennée int main(int argc, char **argv)
1253514552eSAlex Bennée {
1263514552eSAlex Bennée     g_test_init(&argc, &argv, NULL);
1273514552eSAlex Bennée 
1283514552eSAlex Bennée     g_test_add_func("/logging/parse_range", test_parse_range);
1293514552eSAlex Bennée #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
1303514552eSAlex Bennée     g_test_add_func("/logging/parse_invalid_range/subprocess", test_parse_invalid_range_subprocess);
1313514552eSAlex Bennée     g_test_add_func("/logging/parse_invalid_range", test_parse_invalid_range);
1323514552eSAlex Bennée     g_test_add_func("/logging/parse_zero_range/subprocess", test_parse_zero_range_subprocess);
1333514552eSAlex Bennée     g_test_add_func("/logging/parse_zero_range", test_parse_zero_range);
134*f6880b7fSAlex Bennée     g_test_add_func("/logging/parse_path", test_parse_path);
135*f6880b7fSAlex Bennée     g_test_add_func("/logging/parse_path/subprocess", test_parse_path_subprocess);
136*f6880b7fSAlex Bennée     g_test_add_func("/logging/parse_invalid_path", test_parse_invalid_path);
137*f6880b7fSAlex Bennée     g_test_add_func("/logging/parse_invalid_path/subprocess", test_parse_invalid_path_subprocess);
1383514552eSAlex Bennée #endif
1393514552eSAlex Bennée 
1403514552eSAlex Bennée     return g_test_run();
1413514552eSAlex Bennée }
142