blob: a12585f70afde0c21b695aea0c6b45f4071394a6 [file] [log] [blame]
Alex Bennée35145522016-03-15 14:30:20 +00001/*
2 * logging unit-tests
3 *
4 * Copyright (C) 2016 Linaro Ltd.
5 *
6 * Author: Alex Bennée <alex.bennee@linaro.org>
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
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.
25 */
26
27#include "qemu/osdep.h"
Sascha Silbe5f9f8182016-08-18 20:46:03 +020028#include <glib/gstdio.h>
Alex Bennée35145522016-03-15 14:30:20 +000029
30#include "qemu-common.h"
Markus Armbrusterbd6fee92016-06-15 19:27:15 +020031#include "qapi/error.h"
32#include "qemu/log.h"
Alex Bennée35145522016-03-15 14:30:20 +000033
34static void test_parse_range(void)
35{
Markus Armbrusterbd6fee92016-06-15 19:27:15 +020036 Error *err = NULL;
37
38 qemu_set_dfilter_ranges("0x1000+0x100", &error_abort);
Alex Bennée35145522016-03-15 14:30:20 +000039
40 g_assert_false(qemu_log_in_addr_range(0xfff));
41 g_assert(qemu_log_in_addr_range(0x1000));
42 g_assert(qemu_log_in_addr_range(0x1001));
43 g_assert(qemu_log_in_addr_range(0x10ff));
44 g_assert_false(qemu_log_in_addr_range(0x1100));
45
Markus Armbrusterbd6fee92016-06-15 19:27:15 +020046 qemu_set_dfilter_ranges("0x1000-0x100", &error_abort);
Alex Bennée35145522016-03-15 14:30:20 +000047
48 g_assert_false(qemu_log_in_addr_range(0x1001));
49 g_assert(qemu_log_in_addr_range(0x1000));
50 g_assert(qemu_log_in_addr_range(0x0f01));
51 g_assert_false(qemu_log_in_addr_range(0x0f00));
52
Markus Armbrusterbd6fee92016-06-15 19:27:15 +020053 qemu_set_dfilter_ranges("0x1000..0x1100", &error_abort);
Alex Bennée35145522016-03-15 14:30:20 +000054
55 g_assert_false(qemu_log_in_addr_range(0xfff));
56 g_assert(qemu_log_in_addr_range(0x1000));
57 g_assert(qemu_log_in_addr_range(0x1100));
58 g_assert_false(qemu_log_in_addr_range(0x1101));
59
Markus Armbrusterbd6fee92016-06-15 19:27:15 +020060 qemu_set_dfilter_ranges("0x1000..0x1000", &error_abort);
Alex Bennée35145522016-03-15 14:30:20 +000061
62 g_assert_false(qemu_log_in_addr_range(0xfff));
63 g_assert(qemu_log_in_addr_range(0x1000));
64 g_assert_false(qemu_log_in_addr_range(0x1001));
65
Markus Armbrusterbd6fee92016-06-15 19:27:15 +020066 qemu_set_dfilter_ranges("0x1000+0x100,0x2100-0x100,0x3000..0x3100",
67 &error_abort);
Alex Bennée35145522016-03-15 14:30:20 +000068 g_assert(qemu_log_in_addr_range(0x1050));
69 g_assert(qemu_log_in_addr_range(0x2050));
70 g_assert(qemu_log_in_addr_range(0x3050));
Markus Armbrusterbd6fee92016-06-15 19:27:15 +020071
Markus Armbruster58e19e62016-07-01 13:47:46 +020072 qemu_set_dfilter_ranges("0xffffffffffffffff-1", &error_abort);
73 g_assert(qemu_log_in_addr_range(UINT64_MAX));
74 g_assert_false(qemu_log_in_addr_range(UINT64_MAX - 1));
75
76 qemu_set_dfilter_ranges("0..0xffffffffffffffff", &err);
Markus Armbruster58eeb832016-07-01 13:47:49 +020077 g_assert(qemu_log_in_addr_range(0));
78 g_assert(qemu_log_in_addr_range(UINT64_MAX));
79
Markus Armbruster58e19e62016-07-01 13:47:46 +020080 qemu_set_dfilter_ranges("2..1", &err);
81 error_free_or_abort(&err);
82
Markus Armbrusterbd6fee92016-06-15 19:27:15 +020083 qemu_set_dfilter_ranges("0x1000+onehundred", &err);
84 error_free_or_abort(&err);
85
86 qemu_set_dfilter_ranges("0x1000+0", &err);
87 error_free_or_abort(&err);
Alex Bennée35145522016-03-15 14:30:20 +000088}
89
Sascha Silbe5f9f8182016-08-18 20:46:03 +020090static void set_log_path_tmp(char const *dir, char const *tpl, Error **errp)
Alex Bennéef6880b72016-03-15 14:30:23 +000091{
Sascha Silbe5f9f8182016-08-18 20:46:03 +020092 gchar *file_path = g_build_filename(dir, tpl, NULL);
93
94 qemu_set_log_filename(file_path, errp);
95 g_free(file_path);
96}
97
98static void test_parse_path(gconstpointer data)
99{
100 gchar const *tmp_path = data;
Markus Armbrusterdaa76aa2016-06-15 19:27:16 +0200101 Error *err = NULL;
102
Sascha Silbe5f9f8182016-08-18 20:46:03 +0200103 set_log_path_tmp(tmp_path, "qemu.log", &error_abort);
104 set_log_path_tmp(tmp_path, "qemu-%d.log", &error_abort);
105 set_log_path_tmp(tmp_path, "qemu.log.%d", &error_abort);
Markus Armbrusterdaa76aa2016-06-15 19:27:16 +0200106
Sascha Silbe5f9f8182016-08-18 20:46:03 +0200107 set_log_path_tmp(tmp_path, "qemu-%d%d.log", &err);
Markus Armbrusterdaa76aa2016-06-15 19:27:16 +0200108 error_free_or_abort(&err);
Alex Bennéef6880b72016-03-15 14:30:23 +0000109}
Alex Bennée35145522016-03-15 14:30:20 +0000110
Sascha Silbe5f9f8182016-08-18 20:46:03 +0200111/* Remove a directory and all its entries (non-recursive). */
112static void rmdir_full(gchar const *root)
113{
114 GDir *root_gdir = g_dir_open(root, 0, NULL);
115 gchar const *entry_name;
116
117 g_assert_nonnull(root_gdir);
118 while ((entry_name = g_dir_read_name(root_gdir)) != NULL) {
119 gchar *entry_path = g_build_filename(root, entry_name, NULL);
120 g_assert(g_remove(entry_path) == 0);
121 g_free(entry_path);
122 }
123 g_dir_close(root_gdir);
124 g_assert(g_rmdir(root) == 0);
125}
126
Alex Bennée35145522016-03-15 14:30:20 +0000127int main(int argc, char **argv)
128{
Sascha Silbe5f9f8182016-08-18 20:46:03 +0200129 gchar *tmp_path = g_dir_make_tmp("qemu-test-logging.XXXXXX", NULL);
130 int rc;
131
Alex Bennée35145522016-03-15 14:30:20 +0000132 g_test_init(&argc, &argv, NULL);
Sascha Silbe5f9f8182016-08-18 20:46:03 +0200133 g_assert_nonnull(tmp_path);
Alex Bennée35145522016-03-15 14:30:20 +0000134
135 g_test_add_func("/logging/parse_range", test_parse_range);
Sascha Silbe5f9f8182016-08-18 20:46:03 +0200136 g_test_add_data_func("/logging/parse_path", tmp_path, test_parse_path);
Alex Bennée35145522016-03-15 14:30:20 +0000137
Sascha Silbe5f9f8182016-08-18 20:46:03 +0200138 rc = g_test_run();
139
140 rmdir_full(tmp_path);
141 g_free(tmp_path);
142 return rc;
Alex Bennée35145522016-03-15 14:30:20 +0000143}