summaryrefslogtreecommitdiffstats
path: root/tests/test-logging.c
blob: ac8deedc9a9830cd7615a2161a18dffaa42456e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * logging unit-tests
 *
 * Copyright (C) 2016 Linaro Ltd.
 *
 *  Author: Alex Bennée <alex.bennee@linaro.org>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "qemu/osdep.h"
#include <glib.h>

#include "qemu-common.h"
#include "include/qemu/log.h"

static void test_parse_range(void)
{
    qemu_set_dfilter_ranges("0x1000+0x100");

    g_assert_false(qemu_log_in_addr_range(0xfff));
    g_assert(qemu_log_in_addr_range(0x1000));
    g_assert(qemu_log_in_addr_range(0x1001));
    g_assert(qemu_log_in_addr_range(0x10ff));
    g_assert_false(qemu_log_in_addr_range(0x1100));

    qemu_set_dfilter_ranges("0x1000-0x100");

    g_assert_false(qemu_log_in_addr_range(0x1001));
    g_assert(qemu_log_in_addr_range(0x1000));
    g_assert(qemu_log_in_addr_range(0x0f01));
    g_assert_false(qemu_log_in_addr_range(0x0f00));

    qemu_set_dfilter_ranges("0x1000..0x1100");

    g_assert_false(qemu_log_in_addr_range(0xfff));
    g_assert(qemu_log_in_addr_range(0x1000));
    g_assert(qemu_log_in_addr_range(0x1100));
    g_assert_false(qemu_log_in_addr_range(0x1101));

    qemu_set_dfilter_ranges("0x1000..0x1000");

    g_assert_false(qemu_log_in_addr_range(0xfff));
    g_assert(qemu_log_in_addr_range(0x1000));
    g_assert_false(qemu_log_in_addr_range(0x1001));

    qemu_set_dfilter_ranges("0x1000+0x100,0x2100-0x100,0x3000..0x3100");
    g_assert(qemu_log_in_addr_range(0x1050));
    g_assert(qemu_log_in_addr_range(0x2050));
    g_assert(qemu_log_in_addr_range(0x3050));
}

#ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
static void test_parse_invalid_range_subprocess(void)
{
    qemu_set_dfilter_ranges("0x1000+onehundred");
}
static void test_parse_invalid_range(void)
{
    g_test_trap_subprocess("/logging/parse_invalid_range/subprocess", 0, 0);
    g_test_trap_assert_failed();
    g_test_trap_assert_stdout("");
    g_test_trap_assert_stderr("*Failed to parse range in: 0x1000+onehundred\n");
}
static void test_parse_zero_range_subprocess(void)
{
    qemu_set_dfilter_ranges("0x1000+0");
}
static void test_parse_zero_range(void)
{
    g_test_trap_subprocess("/logging/parse_zero_range/subprocess", 0, 0);
    g_test_trap_assert_failed();
    g_test_trap_assert_stdout("");
    g_test_trap_assert_stderr("*Failed to parse range in: 0x1000+0\n");
}

/* As the only real failure from a bad log filename path spec is
 * reporting to the user we have to use the g_test_trap_subprocess
 * mechanism and check no errors reported on stderr.
 */
static void test_parse_path_subprocess(void)
{
    /* All these should work without issue */
    qemu_set_log_filename("/tmp/qemu.log");
    qemu_set_log_filename("/tmp/qemu-%d.log");
    qemu_set_log_filename("/tmp/qemu.log.%d");
}
static void test_parse_path(void)
{
    g_test_trap_subprocess ("/logging/parse_path/subprocess", 0, 0);
    g_test_trap_assert_passed();
    g_test_trap_assert_stdout("");
    g_test_trap_assert_stderr("");
}
static void test_parse_invalid_path_subprocess(void)
{
    qemu_set_log_filename("/tmp/qemu-%d%d.log");
}
static void test_parse_invalid_path(void)
{
    g_test_trap_subprocess ("/logging/parse_invalid_path/subprocess", 0, 0);
    g_test_trap_assert_passed();
    g_test_trap_assert_stdout("");
    g_test_trap_assert_stderr("Bad logfile format: /tmp/qemu-%d%d.log\n");
}
#endif /* CONFIG_HAS_GLIB_SUBPROCESS_TESTS */

int main(int argc, char **argv)
{
    g_test_init(&argc, &argv, NULL);

    g_test_add_func("/logging/parse_range", test_parse_range);
#ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
    g_test_add_func("/logging/parse_invalid_range/subprocess", test_parse_invalid_range_subprocess);
    g_test_add_func("/logging/parse_invalid_range", test_parse_invalid_range);
    g_test_add_func("/logging/parse_zero_range/subprocess", test_parse_zero_range_subprocess);
    g_test_add_func("/logging/parse_zero_range", test_parse_zero_range);
    g_test_add_func("/logging/parse_path", test_parse_path);
    g_test_add_func("/logging/parse_path/subprocess", test_parse_path_subprocess);
    g_test_add_func("/logging/parse_invalid_path", test_parse_invalid_path);
    g_test_add_func("/logging/parse_invalid_path/subprocess", test_parse_invalid_path_subprocess);
#endif

    return g_test_run();
}