Merge remote-tracking branch 'remotes/lalrae/tags/mips-20160428' into staging
[qemu.git] / tests / test-logging.c
blobac8deedc9a9830cd7615a2161a18dffaa42456e7
1 /*
2 * logging unit-tests
4 * Copyright (C) 2016 Linaro Ltd.
6 * Author: Alex Bennée <alex.bennee@linaro.org>
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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.
27 #include "qemu/osdep.h"
28 #include <glib.h>
30 #include "qemu-common.h"
31 #include "include/qemu/log.h"
33 static void test_parse_range(void)
35 qemu_set_dfilter_ranges("0x1000+0x100");
37 g_assert_false(qemu_log_in_addr_range(0xfff));
38 g_assert(qemu_log_in_addr_range(0x1000));
39 g_assert(qemu_log_in_addr_range(0x1001));
40 g_assert(qemu_log_in_addr_range(0x10ff));
41 g_assert_false(qemu_log_in_addr_range(0x1100));
43 qemu_set_dfilter_ranges("0x1000-0x100");
45 g_assert_false(qemu_log_in_addr_range(0x1001));
46 g_assert(qemu_log_in_addr_range(0x1000));
47 g_assert(qemu_log_in_addr_range(0x0f01));
48 g_assert_false(qemu_log_in_addr_range(0x0f00));
50 qemu_set_dfilter_ranges("0x1000..0x1100");
52 g_assert_false(qemu_log_in_addr_range(0xfff));
53 g_assert(qemu_log_in_addr_range(0x1000));
54 g_assert(qemu_log_in_addr_range(0x1100));
55 g_assert_false(qemu_log_in_addr_range(0x1101));
57 qemu_set_dfilter_ranges("0x1000..0x1000");
59 g_assert_false(qemu_log_in_addr_range(0xfff));
60 g_assert(qemu_log_in_addr_range(0x1000));
61 g_assert_false(qemu_log_in_addr_range(0x1001));
63 qemu_set_dfilter_ranges("0x1000+0x100,0x2100-0x100,0x3000..0x3100");
64 g_assert(qemu_log_in_addr_range(0x1050));
65 g_assert(qemu_log_in_addr_range(0x2050));
66 g_assert(qemu_log_in_addr_range(0x3050));
69 #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
70 static void test_parse_invalid_range_subprocess(void)
72 qemu_set_dfilter_ranges("0x1000+onehundred");
74 static void test_parse_invalid_range(void)
76 g_test_trap_subprocess("/logging/parse_invalid_range/subprocess", 0, 0);
77 g_test_trap_assert_failed();
78 g_test_trap_assert_stdout("");
79 g_test_trap_assert_stderr("*Failed to parse range in: 0x1000+onehundred\n");
81 static void test_parse_zero_range_subprocess(void)
83 qemu_set_dfilter_ranges("0x1000+0");
85 static void test_parse_zero_range(void)
87 g_test_trap_subprocess("/logging/parse_zero_range/subprocess", 0, 0);
88 g_test_trap_assert_failed();
89 g_test_trap_assert_stdout("");
90 g_test_trap_assert_stderr("*Failed to parse range in: 0x1000+0\n");
93 /* As the only real failure from a bad log filename path spec is
94 * reporting to the user we have to use the g_test_trap_subprocess
95 * mechanism and check no errors reported on stderr.
97 static void test_parse_path_subprocess(void)
99 /* All these should work without issue */
100 qemu_set_log_filename("/tmp/qemu.log");
101 qemu_set_log_filename("/tmp/qemu-%d.log");
102 qemu_set_log_filename("/tmp/qemu.log.%d");
104 static void test_parse_path(void)
106 g_test_trap_subprocess ("/logging/parse_path/subprocess", 0, 0);
107 g_test_trap_assert_passed();
108 g_test_trap_assert_stdout("");
109 g_test_trap_assert_stderr("");
111 static void test_parse_invalid_path_subprocess(void)
113 qemu_set_log_filename("/tmp/qemu-%d%d.log");
115 static void test_parse_invalid_path(void)
117 g_test_trap_subprocess ("/logging/parse_invalid_path/subprocess", 0, 0);
118 g_test_trap_assert_passed();
119 g_test_trap_assert_stdout("");
120 g_test_trap_assert_stderr("Bad logfile format: /tmp/qemu-%d%d.log\n");
122 #endif /* CONFIG_HAS_GLIB_SUBPROCESS_TESTS */
124 int main(int argc, char **argv)
126 g_test_init(&argc, &argv, NULL);
128 g_test_add_func("/logging/parse_range", test_parse_range);
129 #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
130 g_test_add_func("/logging/parse_invalid_range/subprocess", test_parse_invalid_range_subprocess);
131 g_test_add_func("/logging/parse_invalid_range", test_parse_invalid_range);
132 g_test_add_func("/logging/parse_zero_range/subprocess", test_parse_zero_range_subprocess);
133 g_test_add_func("/logging/parse_zero_range", test_parse_zero_range);
134 g_test_add_func("/logging/parse_path", test_parse_path);
135 g_test_add_func("/logging/parse_path/subprocess", test_parse_path_subprocess);
136 g_test_add_func("/logging/parse_invalid_path", test_parse_invalid_path);
137 g_test_add_func("/logging/parse_invalid_path/subprocess", test_parse_invalid_path_subprocess);
138 #endif
140 return g_test_run();