chiark / gitweb /
Commit 2.4.5-5 as unpacked
[inn-innduct.git] / tests / lib / messages-t.c
1 /* $Id: messages-t.c 5638 2002-08-23 22:52:53Z rra $ */
2 /* Test suite for error handling routines. */
3
4 #include "config.h"
5 #include "clibrary.h"
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <sys/stat.h>
9 #include <sys/wait.h>
10
11 #include "inn/messages.h"
12 #include "libinn.h"
13
14 #define END     (char *) 0
15
16 /* Test function type. */
17 typedef void (*test_function_t)(void);
18
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. */
22 static int
23 run_test(test_function_t function, char *buf, size_t buflen)
24 {
25     int fds[2];
26     pid_t child;
27     ssize_t count, status;
28
29     /* Flush stdout before we start to avoid odd forking issues. */
30     fflush(stdout);
31
32     /* Set up the pipe and call the function, collecting its output. */
33     if (pipe(fds) == -1)
34         sysdie("can't create pipe");
35     child = fork();
36     if (child == (pid_t) -1) {
37         sysdie("can't fork");
38     } else if (child == 0) {
39         /* In child.  Set up our stdout and stderr. */
40         close(fds[0]);
41         if (dup2(fds[1], 1) == -1)
42             _exit(255);
43         if (dup2(fds[1], 2) == -1)
44             _exit(255);
45
46         /* Now, run the function and exit successfully if it returns. */
47         (*function)();
48         fflush(stdout);
49         _exit(0);
50     } else {
51         /* In the parent; close the extra file descriptor, read the output
52            if any, and then collect the exit status. */
53         close(fds[1]);
54         count = 0;
55         do {
56             status = read(fds[0], buf + count, buflen - count - 1);
57             if (status > 0)
58                 count += status;
59         } while (status > 0);
60         buf[count < 0 ? 0 : count] = '\0';
61         if (waitpid(child, &status, 0) == (pid_t) -1)
62             sysdie("waitpid failed");
63     }
64     return status;
65 }
66
67 /* Test functions. */
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";
74     warn("warning");
75 }
76 static void test6(void) {
77     message_program_name = "test6";
78     die("fatal");
79 }
80 static void test7(void) {
81     message_program_name = "test7";
82     errno = EPERM;
83     syswarn("perms %d", 7);
84 }
85 static void test8(void) {
86     message_program_name = "test8";
87     errno = EACCES;
88     sysdie("%st%s", "fa", "al");
89 }
90
91 static int return10(void) { return 10; }
92
93 static void test9(void) {
94     message_fatal_cleanup = return10;
95     die("fatal");
96 }
97 static void test10(void) {
98     message_program_name = 0;
99     message_fatal_cleanup = return10;
100     errno = EPERM;
101     sysdie("fatal perm");
102 }
103 static void test11(void) {
104     message_program_name = "test11";
105     message_fatal_cleanup = return10;
106     errno = EPERM;
107     fputs("1st ", stdout);
108     sysdie("fatal");
109 }
110
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");
115 }
116
117 static void test12(void) {
118     message_handlers_warn(1, log);
119     warn("warning");
120 }
121 static void test13(void) {
122     message_handlers_die(1, log);
123     die("fatal");
124 }
125 static void test14(void) {
126     message_handlers_warn(2, log, log);
127     errno = EPERM;
128     syswarn("warning");
129 }
130 static void test15(void) {
131     message_handlers_die(2, log, log);
132     message_fatal_cleanup = return10;
133     errno = EPERM;
134     sysdie("fatal");
135 }
136 static void test16(void) {
137     message_handlers_warn(2, message_log_stderr, log);
138     message_program_name = "test16";
139     errno = EPERM;
140     syswarn("warning");
141 }
142 static void test17(void) { notice("notice"); }
143 static void test18(void) {
144     message_program_name = "test18";
145     notice("notice");
146 }
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);
151     notice("foo");
152 }
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");
158 }
159 static void test23(void) {
160     message_handlers_debug(1, message_log_stdout);
161     message_program_name = "test23";
162     debug("baz");
163 }
164 static void test24(void) {
165     message_handlers_die(0);
166     die("hi mom!");
167 }
168 static void test25(void) {
169     message_handlers_warn(0);
170     warn("this is a test");
171 }
172 static void test26(void) {
173     notice("first");
174     message_handlers_notice(0);
175     notice("second");
176     message_handlers_notice(1, message_log_stdout);
177     notice("third");
178 }
179
180 /* Given the test number, intended exit status and message, and the function
181    to run, print ok or not ok. */
182 static void
183 test_error(int n, int status, const char *output, test_function_t function)
184 {
185     int real_status;
186     char buf[256];
187     int succeeded = 1;
188
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);
192         succeeded = 0;
193     }
194     if (strcmp(output, buf)) {
195         printf("  unexpected output: %s", buf);
196         printf("    expected output: %s", output);
197         succeeded = 0;
198     }
199     printf("%sok %d\n", succeeded ? "" : "not ", n);
200 }
201
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
204    ok. */
205 static void
206 test_strerror(int n, int status, const char *output, int error,
207               test_function_t function)
208 {
209     char *full_output;
210
211     full_output = concat(output, ": ", strerror(error), "\n", END);
212     test_error(n, status, full_output, function);
213     free(full_output);
214 }
215
216 /* Run the tests. */
217 int
218 main(void)
219 {
220     char buff[32];
221
222     puts("26");
223
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);
237
238     sprintf(buff, "%d", EPERM);
239
240     test_error(14, 0,
241                concat("7 ", buff, " warning\n7 ", buff, " warning\n", END),
242                test14);
243     test_error(15, 10,
244                concat("5 ", buff, " fatal\n5 ", buff, " fatal\n", END),
245                test15);
246     test_error(16, 0,
247                concat("test16: warning: ", strerror(EPERM), "\n7 ", buff,
248                       " warning\n", END),
249                test16);
250
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);
258
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);
263
264     return 0;
265 }