1 | /* |
---|
2 | * Utility functions to test message handling. |
---|
3 | * |
---|
4 | * These functions set up a message handler to trap warn and notice output |
---|
5 | * into a buffer that can be inspected later, allowing testing of error |
---|
6 | * handling. |
---|
7 | * |
---|
8 | * Copyright 2006, 2007, 2009 |
---|
9 | * Board of Trustees, Leland Stanford Jr. University |
---|
10 | * Copyright (c) 2004, 2005, 2006 |
---|
11 | * by Internet Systems Consortium, Inc. ("ISC") |
---|
12 | * Copyright (c) 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, |
---|
13 | * 2002, 2003 by The Internet Software Consortium and Rich Salz |
---|
14 | * |
---|
15 | * See LICENSE for licensing terms. |
---|
16 | */ |
---|
17 | |
---|
18 | #include <config.h> |
---|
19 | #include <portable/system.h> |
---|
20 | |
---|
21 | #include <tests/tap/messages.h> |
---|
22 | #include <util/util.h> |
---|
23 | |
---|
24 | /* A global buffer into which message_log_buffer stores error messages. */ |
---|
25 | char *errors = NULL; |
---|
26 | |
---|
27 | |
---|
28 | /* |
---|
29 | * An error handler that appends all errors to the errors global. Used by |
---|
30 | * error_capture. |
---|
31 | */ |
---|
32 | static void |
---|
33 | message_log_buffer(int len, const char *fmt, va_list args, int error UNUSED) |
---|
34 | { |
---|
35 | char *message; |
---|
36 | |
---|
37 | message = xmalloc(len + 1); |
---|
38 | vsnprintf(message, len + 1, fmt, args); |
---|
39 | if (errors == NULL) { |
---|
40 | errors = concat(message, "\n", (char *) 0); |
---|
41 | } else { |
---|
42 | char *new_errors; |
---|
43 | |
---|
44 | new_errors = concat(errors, message, "\n", (char *) 0); |
---|
45 | free(errors); |
---|
46 | errors = new_errors; |
---|
47 | } |
---|
48 | free(message); |
---|
49 | } |
---|
50 | |
---|
51 | |
---|
52 | /* |
---|
53 | * Turn on the capturing of errors. Errors will be stored in the global |
---|
54 | * errors variable where they can be checked by the test suite. Capturing is |
---|
55 | * turned off with errors_uncapture. |
---|
56 | */ |
---|
57 | void |
---|
58 | errors_capture(void) |
---|
59 | { |
---|
60 | if (errors != NULL) { |
---|
61 | free(errors); |
---|
62 | errors = NULL; |
---|
63 | } |
---|
64 | message_handlers_warn(1, message_log_buffer); |
---|
65 | message_handlers_notice(1, message_log_buffer); |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | /* |
---|
70 | * Turn off the capturing of errors again. |
---|
71 | */ |
---|
72 | void |
---|
73 | errors_uncapture(void) |
---|
74 | { |
---|
75 | message_handlers_warn(1, message_log_stderr); |
---|
76 | message_handlers_notice(1, message_log_stdout); |
---|
77 | } |
---|