1 /* $Id: messages-t.c 5638 2002-08-23 22:52:53Z rra $ */
2 /* Test suite for error handling routines. */
11 #include "inn/messages.h"
14 #define END (char *) 0
16 /* Test function type. */
17 typedef void (*test_function_t)(void);
19 /* Fork and execute the provided function, connecting stdout and stderr to a
20 pipe. Captures the output into the provided buffer and returns the exit
21 status as a waitpid status value. */
23 run_test(test_function_t function, char *buf, size_t buflen)
27 ssize_t count, status;
29 /* Flush stdout before we start to avoid odd forking issues. */
32 /* Set up the pipe and call the function, collecting its output. */
34 sysdie("can't create pipe");
36 if (child == (pid_t) -1) {
38 } else if (child == 0) {
39 /* In child. Set up our stdout and stderr. */
41 if (dup2(fds[1], 1) == -1)
43 if (dup2(fds[1], 2) == -1)
46 /* Now, run the function and exit successfully if it returns. */
51 /* In the parent; close the extra file descriptor, read the output
52 if any, and then collect the exit status. */
56 status = read(fds[0], buf + count, buflen - count - 1);
60 buf[count < 0 ? 0 : count] = '\0';
61 if (waitpid(child, &status, 0) == (pid_t) -1)
62 sysdie("waitpid failed");
68 static void test1(void) { warn("warning"); }
69 static void test2(void) { die("fatal"); }
70 static void test3(void) { errno = EPERM; syswarn("permissions"); }
71 static void test4(void) { errno = EACCES; sysdie("fatal access"); }
72 static void test5(void) {
73 message_program_name = "test5";
76 static void test6(void) {
77 message_program_name = "test6";
80 static void test7(void) {
81 message_program_name = "test7";
83 syswarn("perms %d", 7);
85 static void test8(void) {
86 message_program_name = "test8";
88 sysdie("%st%s", "fa", "al");
91 static int return10(void) { return 10; }
93 static void test9(void) {
94 message_fatal_cleanup = return10;
97 static void test10(void) {
98 message_program_name = 0;
99 message_fatal_cleanup = return10;
101 sysdie("fatal perm");
103 static void test11(void) {
104 message_program_name = "test11";
105 message_fatal_cleanup = return10;
107 fputs("1st ", stdout);
111 static void log(int len, const char *format, va_list args, int error) {
112 fprintf(stderr, "%d %d ", len, error);
113 vfprintf(stderr, format, args);
114 fprintf(stderr, "\n");
117 static void test12(void) {
118 message_handlers_warn(1, log);
121 static void test13(void) {
122 message_handlers_die(1, log);
125 static void test14(void) {
126 message_handlers_warn(2, log, log);
130 static void test15(void) {
131 message_handlers_die(2, log, log);
132 message_fatal_cleanup = return10;
136 static void test16(void) {
137 message_handlers_warn(2, message_log_stderr, log);
138 message_program_name = "test16";
142 static void test17(void) { notice("notice"); }
143 static void test18(void) {
144 message_program_name = "test18";
147 static void test19(void) { trace(TRACE_PROGRAM, "tracing"); }
148 static void test20(void) { debug("debug"); }
149 static void test21(void) {
150 message_handlers_notice(1, log);
153 static void test22(void) {
154 message_handlers_trace(1, log);
155 message_trace_enable(TRACE_PROGRAM, true);
156 trace(TRACE_PROGRAM, "foo");
157 trace(TRACE_NETWORK, "bar");
159 static void test23(void) {
160 message_handlers_debug(1, message_log_stdout);
161 message_program_name = "test23";
164 static void test24(void) {
165 message_handlers_die(0);
168 static void test25(void) {
169 message_handlers_warn(0);
170 warn("this is a test");
172 static void test26(void) {
174 message_handlers_notice(0);
176 message_handlers_notice(1, message_log_stdout);
180 /* Given the test number, intended exit status and message, and the function
181 to run, print ok or not ok. */
183 test_error(int n, int status, const char *output, test_function_t function)
189 real_status = run_test(function, buf, sizeof(buf));
190 if (!WIFEXITED(real_status) || status != WEXITSTATUS(real_status)) {
191 printf(" unexpected exit status %d\n", real_status);
194 if (strcmp(output, buf)) {
195 printf(" unexpected output: %s", buf);
196 printf(" expected output: %s", output);
199 printf("%sok %d\n", succeeded ? "" : "not ", n);
202 /* Given the test number, intended status, intended message sans the
203 appended strerror output, errno, and the function to run, print ok or not
206 test_strerror(int n, int status, const char *output, int error,
207 test_function_t function)
211 full_output = concat(output, ": ", strerror(error), "\n", END);
212 test_error(n, status, full_output, function);
224 test_error(1, 0, "warning\n", test1);
225 test_error(2, 1, "fatal\n", test2);
226 test_strerror(3, 0, "permissions", EPERM, test3);
227 test_strerror(4, 1, "fatal access", EACCES, test4);
228 test_error(5, 0, "test5: warning\n", test5);
229 test_error(6, 1, "test6: fatal\n", test6);
230 test_strerror(7, 0, "test7: perms 7", EPERM, test7);
231 test_strerror(8, 1, "test8: fatal", EACCES, test8);
232 test_error(9, 10, "fatal\n", test9);
233 test_strerror(10, 10, "fatal perm", EPERM, test10);
234 test_strerror(11, 10, "1st test11: fatal", EPERM, test11);
235 test_error(12, 0, "7 0 warning\n", test12);
236 test_error(13, 1, "5 0 fatal\n", test13);
238 sprintf(buff, "%d", EPERM);
241 concat("7 ", buff, " warning\n7 ", buff, " warning\n", END),
244 concat("5 ", buff, " fatal\n5 ", buff, " fatal\n", END),
247 concat("test16: warning: ", strerror(EPERM), "\n7 ", buff,
251 test_error(17, 0, "notice\n", test17);
252 test_error(18, 0, "test18: notice\n", test18);
253 test_error(19, 0, "", test19);
254 test_error(20, 0, "", test20);
255 test_error(21, 0, "3 0 foo\n", test21);
256 test_error(22, 0, "3 0 foo\n", test22);
257 test_error(23, 0, "test23: baz\n", test23);
259 /* Make sure that it's possible to turn off a message type entirely. */
260 test_error(24, 1, "", test24);
261 test_error(25, 0, "", test25);
262 test_error(26, 0, "first\nthird\n", test26);