| 1 | /* |
| 2 | * Error reporting test |
| 3 | * |
| 4 | * Copyright (C) 2022 Red Hat Inc. |
| 5 | * |
| 6 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 7 | * See the COPYING file in the top-level directory. |
| 8 | */ |
| 9 | |
| 10 | #include "qemu/osdep.h" |
| 11 | #include <locale.h> |
| 12 | |
| 13 | #include "qemu/error-report.h" |
| 14 | #include "qapi/error.h" |
| 15 | |
| 16 | static void |
| 17 | test_error_report_simple(void) |
| 18 | { |
| 19 | if (g_test_subprocess()) { |
| 20 | error_report("%s", "test error"); |
| 21 | warn_report("%s", "test warn"); |
| 22 | info_report("%s", "test info"); |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | g_test_trap_subprocess(NULL, 0, 0); |
| 27 | g_test_trap_assert_passed(); |
| 28 | g_test_trap_assert_stderr("\ |
| 29 | test-error-report: test error*\ |
| 30 | test-error-report: warning: test warn*\ |
| 31 | test-error-report: info: test info*\ |
| 32 | "); |
| 33 | } |
| 34 | |
| 35 | static void |
| 36 | test_error_report_loc(void) |
| 37 | { |
| 38 | if (g_test_subprocess()) { |
| 39 | loc_set_file("some-file.c", 7717); |
| 40 | error_report("%s", "test error1"); |
| 41 | loc_set_none(); |
| 42 | error_report("%s", "test error2"); |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | g_test_trap_subprocess(NULL, 0, 0); |
| 47 | g_test_trap_assert_passed(); |
| 48 | g_test_trap_assert_stderr("\ |
| 49 | test-error-report:some-file.c:7717: test error1*\ |
| 50 | test-error-report: test error2*\ |
| 51 | "); |
| 52 | } |
| 53 | |
| 54 | static void |
| 55 | test_error_report_glog(void) |
| 56 | { |
| 57 | if (g_test_subprocess()) { |
| 58 | g_message("gmessage"); |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | g_test_trap_subprocess(NULL, 0, 0); |
| 63 | g_test_trap_assert_passed(); |
| 64 | g_test_trap_assert_stderr("test-error-report: info: gmessage*"); |
| 65 | } |
| 66 | |
| 67 | static void |
| 68 | test_error_report_once(void) |
| 69 | { |
| 70 | int i; |
| 71 | |
| 72 | if (g_test_subprocess()) { |
| 73 | for (i = 0; i < 3; i++) { |
| 74 | warn_report_once("warn"); |
| 75 | error_report_once("err"); |
| 76 | } |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | g_test_trap_subprocess(NULL, 0, 0); |
| 81 | g_test_trap_assert_passed(); |
| 82 | g_test_trap_assert_stderr("\ |
| 83 | test-error-report: warning: warn*\ |
| 84 | test-error-report: err*\ |
| 85 | "); |
| 86 | } |
| 87 | |
| 88 | static void |
| 89 | test_error_report_timestamp(void) |
| 90 | { |
| 91 | if (g_test_subprocess()) { |
| 92 | message_with_timestamp = true; |
| 93 | warn_report("warn"); |
| 94 | error_report("err"); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | g_test_trap_subprocess(NULL, 0, 0); |
| 99 | g_test_trap_assert_passed(); |
| 100 | g_test_trap_assert_stderr("\ |
| 101 | *-*-*:*:* test-error-report: warning: warn*\ |
| 102 | *-*-*:*:* test-error-report: err*\ |
| 103 | "); |
| 104 | } |
| 105 | |
| 106 | int |
| 107 | main(int argc, char *argv[]) |
| 108 | { |
| 109 | setlocale(LC_ALL, ""); |
| 110 | |
| 111 | g_test_init(&argc, &argv, NULL); |
| 112 | error_init("test-error-report"); |
| 113 | |
| 114 | g_test_add_func("/error-report/simple", test_error_report_simple); |
| 115 | g_test_add_func("/error-report/loc", test_error_report_loc); |
| 116 | g_test_add_func("/error-report/glog", test_error_report_glog); |
| 117 | g_test_add_func("/error-report/once", test_error_report_once); |
| 118 | g_test_add_func("/error-report/timestamp", test_error_report_timestamp); |
| 119 | |
| 120 | return g_test_run(); |
| 121 | } |