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 "glib-compat.h"
12 #include <locale.h>
13
14 #include "qemu/error-report.h"
15 #include "qapi/error.h"
16
17 static void
test_error_report_simple(void)18 test_error_report_simple(void)
19 {
20 if (g_test_subprocess()) {
21 error_report("%s", "test error");
22 warn_report("%s", "test warn");
23 info_report("%s", "test info");
24 return;
25 }
26
27 g_test_trap_subprocess(NULL, 0, 0);
28 g_test_trap_assert_passed();
29 g_test_trap_assert_stderr("\
30 test-error-report: test error*\
31 test-error-report: warning: test warn*\
32 test-error-report: info: test info*\
33 ");
34 }
35
36 static void
test_error_report_loc(void)37 test_error_report_loc(void)
38 {
39 if (g_test_subprocess()) {
40 loc_set_file("some-file.c", 7717);
41 error_report("%s", "test error1");
42 loc_set_none();
43 error_report("%s", "test error2");
44 return;
45 }
46
47 g_test_trap_subprocess(NULL, 0, 0);
48 g_test_trap_assert_passed();
49 g_test_trap_assert_stderr("\
50 test-error-report:some-file.c:7717: test error1*\
51 test-error-report: test error2*\
52 ");
53 }
54
55 static void
test_error_report_glog(void)56 test_error_report_glog(void)
57 {
58 if (g_test_subprocess()) {
59 g_message("gmessage");
60 return;
61 }
62
63 g_test_trap_subprocess(NULL, 0, 0);
64 g_test_trap_assert_passed();
65 g_test_trap_assert_stderr("test-error-report: info: gmessage*");
66 }
67
68 static void
test_error_report_once(void)69 test_error_report_once(void)
70 {
71 int i;
72
73 if (g_test_subprocess()) {
74 for (i = 0; i < 3; i++) {
75 warn_report_once("warn");
76 error_report_once("err");
77 }
78 return;
79 }
80
81 g_test_trap_subprocess(NULL, 0, 0);
82 g_test_trap_assert_passed();
83 g_test_trap_assert_stderr("\
84 test-error-report: warning: warn*\
85 test-error-report: err*\
86 ");
87 }
88
89 static void
test_error_report_timestamp(void)90 test_error_report_timestamp(void)
91 {
92 if (g_test_subprocess()) {
93 message_with_timestamp = true;
94 warn_report("warn");
95 error_report("err");
96 return;
97 }
98
99 g_test_trap_subprocess(NULL, 0, 0);
100 g_test_trap_assert_passed();
101 g_test_trap_assert_stderr("\
102 *-*-*:*:* test-error-report: warning: warn*\
103 *-*-*:*:* test-error-report: err*\
104 ");
105 }
106
107 static void
test_error_warn(void)108 test_error_warn(void)
109 {
110 if (g_test_subprocess()) {
111 error_setg(&error_warn, "Testing &error_warn");
112 return;
113 }
114
115 g_test_trap_subprocess(NULL, 0, 0);
116 g_test_trap_assert_passed();
117 g_test_trap_assert_stderr("\
118 test-error-report: warning: Testing &error_warn*\
119 ");
120 }
121
122
123 int
main(int argc,char * argv[])124 main(int argc, char *argv[])
125 {
126 setlocale(LC_ALL, "");
127
128 g_test_init(&argc, &argv, NULL);
129 error_init("test-error-report");
130
131 g_test_add_func("/error-report/simple", test_error_report_simple);
132 g_test_add_func("/error-report/loc", test_error_report_loc);
133 g_test_add_func("/error-report/glog", test_error_report_glog);
134 g_test_add_func("/error-report/once", test_error_report_once);
135 g_test_add_func("/error-report/timestamp", test_error_report_timestamp);
136 g_test_add_func("/error-report/warn", test_error_warn);
137
138 return g_test_run();
139 }
140