]> eyrie.org Git - kerberos/krb5-strength.git/blob - tests/util/messages-t.c
New upstream version 3.2
[kerberos/krb5-strength.git] / tests / util / messages-t.c
1 /*
2  * Test suite for error handling routines.
3  *
4  * The canonical version of this file is maintained in the rra-c-util package,
5  * which can be found at <https://www.eyrie.org/~eagle/software/rra-c-util/>.
6  *
7  * Written by Russ Allbery <eagle@eyrie.org>
8  * Copyright 2002, 2004-2005, 2015, 2017, 2020 Russ Allbery <eagle@eyrie.org>
9  * Copyright 2009-2012
10  *     The Board of Trustees of the Leland Stanford Junior University
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a
13  * copy of this software and associated documentation files (the "Software"),
14  * to deal in the Software without restriction, including without limitation
15  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16  * and/or sell copies of the Software, and to permit persons to whom the
17  * Software is furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included in
20  * all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
25  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28  * DEALINGS IN THE SOFTWARE.
29  *
30  * SPDX-License-Identifier: MIT
31  */
32
33 #include <config.h>
34 #include <portable/system.h>
35
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <sys/stat.h>
39 #include <sys/wait.h>
40
41 #include <tests/tap/basic.h>
42 #include <tests/tap/process.h>
43 #include <util/macros.h>
44 #include <util/messages.h>
45 #include <util/xmalloc.h>
46
47
48 /*
49  * Test functions.
50  */
51 static void
52 test1(void *data UNUSED)
53 {
54     warn("warning");
55 }
56
57 __attribute__((__noreturn__)) static void
58 test2(void *data UNUSED)
59 {
60     die("fatal");
61 }
62
63 static void
64 test3(void *data UNUSED)
65 {
66     errno = EPERM;
67     syswarn("permissions");
68 }
69
70 __attribute__((__noreturn__)) static void
71 test4(void *data UNUSED)
72 {
73     errno = EACCES;
74     sysdie("fatal access");
75 }
76
77 static void
78 test5(void *data UNUSED)
79 {
80     message_program_name = "test5";
81     warn("warning");
82 }
83
84 __attribute__((__noreturn__)) static void
85 test6(void *data UNUSED)
86 {
87     message_program_name = "test6";
88     die("fatal");
89 }
90
91 static void
92 test7(void *data UNUSED)
93 {
94     message_program_name = "test7";
95     errno = EPERM;
96     syswarn("perms %d", 7);
97 }
98
99 __attribute__((__noreturn__)) static void
100 test8(void *data UNUSED)
101 {
102     message_program_name = "test8";
103     errno = EACCES;
104     sysdie("%st%s", "fa", "al");
105 }
106
107 static int
108 return10(void)
109 {
110     return 10;
111 }
112
113 __attribute__((__noreturn__)) static void
114 test9(void *data UNUSED)
115 {
116     message_fatal_cleanup = return10;
117     die("fatal");
118 }
119
120 __attribute__((__noreturn__)) static void
121 test10(void *data UNUSED)
122 {
123     message_program_name = 0;
124     message_fatal_cleanup = return10;
125     errno = EPERM;
126     sysdie("fatal perm");
127 }
128
129 __attribute__((__noreturn__)) static void
130 test11(void *data UNUSED)
131 {
132     message_program_name = "test11";
133     message_fatal_cleanup = return10;
134     errno = EPERM;
135     fputs("1st ", stdout);
136     sysdie("fatal");
137 }
138
139 __attribute__((__format__(printf, 2, 0))) static void
140 log_msg(size_t len, const char *format, va_list args, int error)
141 {
142     fprintf(stderr, "%lu %d ", (unsigned long) len, error);
143     vfprintf(stderr, format, args);
144     fprintf(stderr, "\n");
145 }
146
147 static void
148 test12(void *data UNUSED)
149 {
150     message_handlers_warn(1, log_msg);
151     warn("warning");
152 }
153
154 __attribute__((__noreturn__)) static void
155 test13(void *data UNUSED)
156 {
157     message_handlers_die(1, log_msg);
158     die("fatal");
159 }
160
161 static void
162 test14(void *data UNUSED)
163 {
164     message_handlers_warn(2, log_msg, log_msg);
165     errno = EPERM;
166     syswarn("warning");
167 }
168
169 __attribute__((__noreturn__)) static void
170 test15(void *data UNUSED)
171 {
172     message_handlers_die(2, log_msg, log_msg);
173     message_fatal_cleanup = return10;
174     errno = EPERM;
175     sysdie("fatal");
176 }
177
178 static void
179 test16(void *data UNUSED)
180 {
181     message_handlers_warn(2, message_log_stderr, log_msg);
182     message_program_name = "test16";
183     errno = EPERM;
184     syswarn("warning");
185 }
186
187 static void
188 test17(void *data UNUSED)
189 {
190     notice("notice");
191 }
192
193 static void
194 test18(void *data UNUSED)
195 {
196     message_program_name = "test18";
197     notice("notice");
198 }
199
200 static void
201 test19(void *data UNUSED)
202 {
203     debug("debug");
204 }
205
206 static void
207 test20(void *data UNUSED)
208 {
209     message_handlers_notice(1, log_msg);
210     notice("foo");
211 }
212
213 static void
214 test21(void *data UNUSED)
215 {
216     message_handlers_debug(1, message_log_stdout);
217     message_program_name = "test23";
218     debug("baz");
219 }
220
221 __attribute__((__noreturn__)) static void
222 test22(void *data UNUSED)
223 {
224     message_handlers_die(0);
225     die("hi mom!");
226 }
227
228 static void
229 test23(void *data UNUSED)
230 {
231     message_handlers_warn(0);
232     warn("this is a test");
233 }
234
235 static void
236 test24(void *data UNUSED)
237 {
238     notice("first");
239     message_handlers_notice(0);
240     notice("second");
241     message_handlers_notice(1, message_log_stdout);
242     notice("third");
243 }
244
245
246 /*
247  * Given the intended status, intended message sans the appended strerror
248  * output, errno, and the function to run, check the output.
249  */
250 static void
251 test_strerror(int status, const char *output, int error,
252               test_function_type function)
253 {
254     char *full_output, *name;
255
256     xasprintf(&full_output, "%s: %s\n", output, strerror(error));
257     xasprintf(&name, "strerror %lu", testnum / 3 + 1);
258     is_function_output(function, NULL, status, full_output, "%s", name);
259     free(full_output);
260     free(name);
261 }
262
263
264 /*
265  * Run the tests.
266  */
267 int
268 main(void)
269 {
270     char buff[32];
271     char *output;
272
273     plan(24 * 3);
274
275     is_function_output(test1, NULL, 0, "warning\n", "test1");
276     is_function_output(test2, NULL, 1, "fatal\n", "test2");
277     test_strerror(0, "permissions", EPERM, test3);
278     test_strerror(1, "fatal access", EACCES, test4);
279     is_function_output(test5, NULL, 0, "test5: warning\n", "test5");
280     is_function_output(test6, NULL, 1, "test6: fatal\n", "test6");
281     test_strerror(0, "test7: perms 7", EPERM, test7);
282     test_strerror(1, "test8: fatal", EACCES, test8);
283     is_function_output(test9, NULL, 10, "fatal\n", "test9");
284     test_strerror(10, "fatal perm", EPERM, test10);
285     test_strerror(10, "1st test11: fatal", EPERM, test11);
286     is_function_output(test12, NULL, 0, "7 0 warning\n", "test12");
287     is_function_output(test13, NULL, 1, "5 0 fatal\n", "test13");
288
289     sprintf(buff, "%d", EPERM);
290
291     xasprintf(&output, "7 %d warning\n7 %d warning\n", EPERM, EPERM);
292     is_function_output(test14, NULL, 0, output, "test14");
293     free(output);
294     xasprintf(&output, "5 %d fatal\n5 %d fatal\n", EPERM, EPERM);
295     is_function_output(test15, NULL, 10, output, "test15");
296     free(output);
297     xasprintf(&output, "test16: warning: %s\n7 %d warning\n", strerror(EPERM),
298               EPERM);
299     is_function_output(test16, NULL, 0, output, "test16");
300     free(output);
301
302     is_function_output(test17, NULL, 0, "notice\n", "test17");
303     is_function_output(test18, NULL, 0, "test18: notice\n", "test18");
304     is_function_output(test19, NULL, 0, "", "test19");
305     is_function_output(test20, NULL, 0, "3 0 foo\n", "test20");
306     is_function_output(test21, NULL, 0, "test23: baz\n", "test21");
307
308     /* Make sure that it's possible to turn off a message type entirely. */
309     is_function_output(test22, NULL, 1, "", "test22");
310     is_function_output(test23, NULL, 0, "", "test23");
311     is_function_output(test24, NULL, 0, "first\nthird\n", "test24");
312
313     return 0;
314 }