qemu/tests/test-logging.c
<<
>>
Prefs
   1/*
   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"
  28#include <glib.h>
  29
  30#include "qemu-common.h"
  31#include "include/qemu/log.h"
  32
  33static void test_parse_range(void)
  34{
  35    qemu_set_dfilter_ranges("0x1000+0x100");
  36
  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));
  42
  43    qemu_set_dfilter_ranges("0x1000-0x100");
  44
  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));
  49
  50    qemu_set_dfilter_ranges("0x1000..0x1100");
  51
  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));
  56
  57    qemu_set_dfilter_ranges("0x1000..0x1000");
  58
  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));
  62
  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));
  67}
  68
  69#ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
  70static void test_parse_invalid_range_subprocess(void)
  71{
  72    qemu_set_dfilter_ranges("0x1000+onehundred");
  73}
  74static void test_parse_invalid_range(void)
  75{
  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");
  80}
  81static void test_parse_zero_range_subprocess(void)
  82{
  83    qemu_set_dfilter_ranges("0x1000+0");
  84}
  85static void test_parse_zero_range(void)
  86{
  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");
  91}
  92
  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.
  96 */
  97static void test_parse_path_subprocess(void)
  98{
  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");
 103}
 104static void test_parse_path(void)
 105{
 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("");
 110}
 111static void test_parse_invalid_path_subprocess(void)
 112{
 113    qemu_set_log_filename("/tmp/qemu-%d%d.log");
 114}
 115static void test_parse_invalid_path(void)
 116{
 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");
 121}
 122#endif /* CONFIG_HAS_GLIB_SUBPROCESS_TESTS */
 123
 124int main(int argc, char **argv)
 125{
 126    g_test_init(&argc, &argv, NULL);
 127
 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
 139
 140    return g_test_run();
 141}
 142