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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
|
/*
Copyright (C) 2015 Red Hat, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#define G_LOG_DOMAIN "Spice"
#include <glib.h>
#include <stdlib.h>
#include "common/log.h"
#define OTHER_LOG_DOMAIN "Other"
#define LOG_OTHER_HELPER(suffix, level) \
static void G_PASTE(other_, suffix)(const gchar *format, ...) \
{ \
va_list args; \
\
va_start (args, format); \
g_logv(OTHER_LOG_DOMAIN, G_PASTE(G_LOG_LEVEL_, level), format, args); \
va_end (args); \
}
/* Set of helpers to log in a different log domain than "Spice" */
LOG_OTHER_HELPER(debug, DEBUG)
LOG_OTHER_HELPER(info, INFO)
LOG_OTHER_HELPER(message, MESSAGE)
LOG_OTHER_HELPER(warning, WARNING)
LOG_OTHER_HELPER(critical, CRITICAL)
#if GLIB_CHECK_VERSION(2,38,0)
/* Checks that spice_warning() aborts after changing SPICE_ABORT_LEVEL */
static void test_spice_abort_level(void)
{
if (g_test_subprocess()) {
spice_warning("spice_warning");
return;
}
/* 2 = SPICE_LOG_LEVEL_WARNING */
g_setenv("SPICE_ABORT_LEVEL", "2", TRUE);
g_test_trap_subprocess(NULL, 0, 0);
g_unsetenv("SPICE_ABORT_LEVEL");
g_test_trap_assert_failed();
g_test_trap_assert_stderr("*SPICE_ABORT_LEVEL*deprecated*");
g_test_trap_assert_stderr("*spice_warning*");
}
/* Checks that g_warning() aborts after changing SPICE_ABORT_LEVEL */
static void test_spice_abort_level_g_warning(void)
{
if (g_test_subprocess()) {
g_warning("g_warning");
return;
}
g_setenv("SPICE_ABORT_LEVEL", "2", TRUE);
g_test_trap_subprocess(NULL, 0, 0);
g_unsetenv("SPICE_ABORT_LEVEL");
g_test_trap_assert_failed();
g_test_trap_assert_stderr("*SPICE_ABORT_LEVEL*deprecated*");
g_test_trap_assert_stderr("*g_warning*");
}
/* Checks that spice_warning() aborts after setting G_DEBUG=fatal-warnings */
static void test_spice_fatal_warning(void)
{
g_setenv("G_DEBUG", "fatal-warnings", TRUE);
if (g_test_subprocess()) {
spice_warning("spice_warning");
return;
}
g_test_trap_subprocess(NULL, 0, 0);
g_test_trap_assert_failed();
g_test_trap_assert_stderr("*spice_warning*");
g_unsetenv("G_DEBUG");
}
/* Checks that spice_critical() aborts by default if SPICE_DISABLE_ABORT is not
* defined at compile-time */
static void test_spice_fatal_critical(void)
{
if (g_test_subprocess()) {
spice_critical("spice_critical");
return;
}
g_test_trap_subprocess(NULL, 0, 0);
#ifdef SPICE_DISABLE_ABORT
g_test_trap_assert_passed();
#else
g_test_trap_assert_failed();
#endif
g_test_trap_assert_stderr("*spice_critical*");
}
/* Checks that g_critical() does not abort by default */
static void test_spice_non_fatal_g_critical(void)
{
if (g_test_subprocess()) {
g_critical("g_critical");
return;
}
g_test_trap_subprocess(NULL, 0, 0);
g_test_trap_assert_passed();
g_test_trap_assert_stderr("*g_critical*");
}
/* Checks that g_critical() aborts after setting G_DEBUG=fatal-criticals */
static void test_spice_fatal_g_critical(void)
{
g_setenv("G_DEBUG", "fatal-criticals", TRUE);
if (g_test_subprocess()) {
g_critical("g_critical");
return;
}
g_test_trap_subprocess(NULL, 0, 0);
g_test_trap_assert_failed();
g_test_trap_assert_stderr("*g_critical*");
g_unsetenv("G_DEBUG");
}
/* Checks that spice_return_if_fail() aborts by default unless
* SPICE_DISABLE_ABORT was defined at compile time*/
static void test_spice_fatal_return_if_fail(void)
{
if (g_test_subprocess()) {
spice_return_if_fail(FALSE);
return;
}
g_test_trap_subprocess(NULL, 0, 0);
#ifdef SPICE_DISABLE_ABORT
g_test_trap_assert_passed();
#else
g_test_trap_assert_failed();
#endif
g_test_trap_assert_stderr("*test_spice_fatal_return_if_fail*");
}
/* Checks that g_return_if_fail() does not abort by default */
static void test_spice_non_fatal_g_return_if_fail(void)
{
if (g_test_subprocess()) {
g_return_if_fail(FALSE);
return;
}
g_test_trap_subprocess(NULL, 0, 0);
g_test_trap_assert_passed();
}
/* Checks that spice_*, g_* and other_* (different log domain as g_*) all
* go through g_log() with the correct domain/log level. This then checks
* that only logs with level 'message' or higher are shown by default.
*/
static void test_log_levels(void)
{
if (g_test_subprocess()) {
g_test_expect_message(G_LOG_DOMAIN,
G_LOG_LEVEL_WARNING,
"*spice_warning");
spice_warning("spice_warning");
g_test_expect_message(G_LOG_DOMAIN,
G_LOG_LEVEL_INFO,
"*spice_info");
spice_info("spice_info");
g_test_expect_message(G_LOG_DOMAIN,
G_LOG_LEVEL_DEBUG,
"*spice_debug");
spice_debug("spice_debug");
g_test_expect_message(G_LOG_DOMAIN,
G_LOG_LEVEL_CRITICAL,
"*g_critical");
g_critical("g_critical");
g_test_expect_message(G_LOG_DOMAIN,
G_LOG_LEVEL_WARNING,
"*g_warning");
g_warning("g_warning");
g_test_expect_message(G_LOG_DOMAIN,
G_LOG_LEVEL_MESSAGE,
"*g_message");
g_message("g_message");
g_test_expect_message(G_LOG_DOMAIN,
G_LOG_LEVEL_INFO,
"*g_info");
g_info("g_info");
g_test_expect_message(G_LOG_DOMAIN,
G_LOG_LEVEL_DEBUG,
"*g_debug");
g_debug("g_debug");
g_test_expect_message(OTHER_LOG_DOMAIN,
G_LOG_LEVEL_CRITICAL,
"*other_critical");
other_critical("other_critical");
g_test_expect_message(OTHER_LOG_DOMAIN,
G_LOG_LEVEL_WARNING,
"*other_warning");
other_warning("other_warning");
g_test_expect_message(OTHER_LOG_DOMAIN,
G_LOG_LEVEL_MESSAGE,
"*other_message");
other_message("other_message");
g_test_expect_message(OTHER_LOG_DOMAIN,
G_LOG_LEVEL_INFO,
"*other_info");
other_info("other_info");
g_test_expect_message(OTHER_LOG_DOMAIN,
G_LOG_LEVEL_DEBUG,
"*other_debug");
other_debug("other_debug");
g_test_assert_expected_messages();
/* g_test_expected_message only checks whether the appropriate messages got up to g_log()
* The following calls will be caught by the parent process to check what was (not) printed
* to stdout/stderr
*/
spice_warning("spice_warning");
spice_info("spice_info");
spice_debug("spice_debug");
g_critical("g_critical");
g_warning("g_warning");
g_message("g_message");
g_info("g_info");
g_debug("g_debug");
other_critical("other_critical");
other_warning("other_warning");
other_message("other_message");
other_info("other_info");
other_debug("other_debug");
return;
}
g_test_trap_subprocess(NULL, 0, 0);
g_test_trap_assert_passed();
g_test_trap_assert_stderr("*spice_warning\n*g_critical\n*g_warning\n*g_message\n*other_critical\n*other_warning\n*other_message\n");
g_test_trap_assert_stdout_unmatched("*spice_info*");
g_test_trap_assert_stdout_unmatched("*spice_debug*");
g_test_trap_assert_stdout_unmatched("*g_info*");
g_test_trap_assert_stdout_unmatched("*g_debug*");
g_test_trap_assert_stdout_unmatched("*other_info*");
g_test_trap_assert_stdout_unmatched("*other_debug*");
}
/* Checks that SPICE_DEBUG_LEVEL impacts spice_debug(), g_debug() but not other_debug() */
static void test_spice_debug_level(void)
{
if (g_test_subprocess()) {
/* g_test_expected_message only checks whether the appropriate messages got up to g_log()
* The following calls will be caught by the parent process to check what was (not) printed
* to stdout/stderr
*/
spice_info("spice_info");
g_debug("g_debug");
spice_debug("spice_debug");
other_debug("other_debug");
return;
}
g_unsetenv("G_MESSAGES_DEBUG");
g_setenv("SPICE_DEBUG_LEVEL", "5", TRUE);
g_test_trap_subprocess(NULL, 0, 0);
g_unsetenv("SPICE_DEBUG_LEVEL");
g_test_trap_assert_passed();
g_test_trap_assert_stderr("*SPICE_DEBUG_LEVEL*deprecated*");
g_test_trap_assert_stdout("*spice_info\n*g_debug\n*spice_debug\n");
g_test_trap_assert_stdout_unmatched("*other_debug*");
}
/* Checks that raising SPICE_DEBUG_LEVEL allows to only show spice_warning() and spice_critical()
* messages, as well as g_warning() and g_critical(), but does not impact other_message()
*/
static void test_spice_debug_level_warning(void)
{
if (g_test_subprocess()) {
spice_info("spice_info");
spice_debug("spice_debug");
spice_warning("spice_warning");
spice_critical("spice_critical");
g_debug("g_debug");
g_info("g_info");
g_message("g_message");
g_warning("g_warning");
g_critical("g_critical");
other_debug("other_debug");
other_info("other_info");
other_message("other_message");
other_warning("other_warning");
other_critical("other_critical");
return;
}
g_setenv("SPICE_ABORT_LEVEL", "0", TRUE);
g_setenv("SPICE_DEBUG_LEVEL", "1", TRUE);
g_test_trap_subprocess(NULL, 0, 0);
g_unsetenv("SPICE_ABORT_LEVEL");
g_unsetenv("SPICE_DEBUG_LEVEL");
g_test_trap_assert_passed();
g_test_trap_assert_stderr("*SPICE_DEBUG_LEVEL*deprecated*");
g_test_trap_assert_stderr("*SPICE_ABORT_LEVEL*deprecated*");
g_test_trap_assert_stderr("*spice_critical\n*g_critical\n*other_message\n*other_warning\n*other_critical\n");
g_test_trap_assert_stdout_unmatched("*spice_info*");
g_test_trap_assert_stdout_unmatched("*spice_debug*");
g_test_trap_assert_stderr_unmatched("*spice_warning*");
g_test_trap_assert_stdout_unmatched("*g_debug*");
g_test_trap_assert_stdout_unmatched("*g_info*");
g_test_trap_assert_stderr_unmatched("*g_message*");
g_test_trap_assert_stderr_unmatched("*g_warning*");
g_test_trap_assert_stdout_unmatched("*other_info*");
}
/* Checks that setting G_MESSAGES_DEBUG to 'Spice' impacts spice_debug() and
* g_debug() but not other_debug() */
static void test_spice_g_messages_debug(void)
{
if (g_test_subprocess()) {
g_setenv("G_MESSAGES_DEBUG", "Spice", TRUE);
spice_debug("spice_debug");
spice_info("spice_info");
g_debug("g_debug");
g_info("g_info");
g_message("g_message");
other_debug("other_debug");
other_info("other_info");
other_message("other_message");
return;
}
g_test_trap_subprocess(NULL, 0, 0);
g_test_trap_assert_passed();
g_test_trap_assert_stdout("*spice_debug\n*spice_info\n*g_debug\n*g_info\n");
g_test_trap_assert_stderr("*g_message\n*other_message\n");
g_test_trap_assert_stdout_unmatched("*other_debug*");
g_test_trap_assert_stdout_unmatched("*other_info*");
}
/* Checks that setting G_MESSAGES_DEBUG to 'all' impacts spice_debug(),
* g_debug() and other_debug() */
static void test_spice_g_messages_debug_all(void)
{
if (g_test_subprocess()) {
g_setenv("G_MESSAGES_DEBUG", "all", TRUE);
spice_debug("spice_debug");
spice_info("spice_info");
g_debug("g_debug");
g_info("g_info");
g_message("g_message");
other_debug("other_debug");
other_info("other_info");
other_message("other_message");
return;
}
g_test_trap_subprocess(NULL, 0, 0);
g_test_trap_assert_passed();
g_test_trap_assert_stdout("*spice_debug\n*spice_info\n*g_debug\n*g_info\n*other_debug\n*other_info\n");
g_test_trap_assert_stderr("*g_message\n*other_message\n");
}
#endif /* GLIB_CHECK_VERSION(2,38,0) */
static void handle_sigabrt(int sig G_GNUC_UNUSED)
{
_Exit(1);
}
int main(int argc, char **argv)
{
GLogLevelFlags fatal_mask;
/* prevents core generations as this could cause some issues/timeout
* depending on system configuration */
signal(SIGABRT, handle_sigabrt);
fatal_mask = (GLogLevelFlags)g_log_set_always_fatal((GLogLevelFlags) G_LOG_FATAL_MASK);
g_test_init(&argc, &argv, NULL);
/* Reset fatal mask set by g_test_init() as we don't want
* warnings/criticals to be fatal by default since this is what some of the
* test cases are going to test */
g_log_set_always_fatal(fatal_mask & G_LOG_LEVEL_MASK);
#if GLIB_CHECK_VERSION(2,38,0)
g_test_add_func("/spice-common/spice-abort-level", test_spice_abort_level);
g_test_add_func("/spice-common/spice-abort-level-gwarning", test_spice_abort_level_g_warning);
g_test_add_func("/spice-common/spice-debug-level", test_spice_debug_level);
g_test_add_func("/spice-common/spice-debug-level-warning", test_spice_debug_level_warning);
g_test_add_func("/spice-common/spice-g-messages-debug", test_spice_g_messages_debug);
g_test_add_func("/spice-common/spice-g-messages-debug-all", test_spice_g_messages_debug_all);
g_test_add_func("/spice-common/spice-log-levels", test_log_levels);
g_test_add_func("/spice-common/spice-fatal-critical", test_spice_fatal_critical);
g_test_add_func("/spice-common/spice-non-fatal-gcritical", test_spice_non_fatal_g_critical);
g_test_add_func("/spice-common/spice-fatal-gcritical", test_spice_fatal_g_critical);
g_test_add_func("/spice-common/spice-fatal-return-if-fail", test_spice_fatal_return_if_fail);
g_test_add_func("/spice-common/spice-non-fatal-greturn-if-fail", test_spice_non_fatal_g_return_if_fail);
g_test_add_func("/spice-common/spice-fatal-warning", test_spice_fatal_warning);
#endif /* GLIB_CHECK_VERSION(2,38,0) */
return g_test_run();
}
|