]> eyrie.org Git - kerberos/krb5-strength.git/blob - tests/util/messages-t.c
Reformat with clang-format
[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 Russ Allbery <eagle@eyrie.org>
9  * Copyright 2009, 2010, 2011, 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
31 #include <config.h>
32 #include <portable/system.h>
33
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <sys/stat.h>
37 #include <sys/wait.h>
38
39 #include <tests/tap/basic.h>
40 #include <tests/tap/process.h>
41 #include <util/macros.h>
42 #include <util/messages.h>
43 #include <util/xmalloc.h>
44
45
46 /*
47  * Test functions.
48  */
49 static void
50 test1(void *data UNUSED)
51 {
52     warn("warning");
53 }
54 static void
55 test2(void *data UNUSED)
56 {
57     die("fatal");
58 }
59 static void
60 test3(void *data UNUSED)
61 {
62     errno = EPERM;
63     syswarn("permissions");
64 }
65 static void
66 test4(void *data UNUSED)
67 {
68     errno = EACCES;
69     sysdie("fatal access");
70 }
71 static void
72 test5(void *data UNUSED)
73 {
74     message_program_name = "test5";
75     warn("warning");
76 }
77 static void
78 test6(void *data UNUSED)
79 {
80     message_program_name = "test6";
81     die("fatal");
82 }
83 static void
84 test7(void *data UNUSED)
85 {
86     message_program_name = "test7";
87     errno = EPERM;
88     syswarn("perms %d", 7);
89 }
90 static void
91 test8(void *data UNUSED)
92 {
93     message_program_name = "test8";
94     errno = EACCES;
95     sysdie("%st%s", "fa", "al");
96 }
97
98 static int
99 return10(void)
100 {
101     return 10;
102 }
103
104 static void
105 test9(void *data UNUSED)
106 {
107     message_fatal_cleanup = return10;
108     die("fatal");
109 }
110 static void
111 test10(void *data UNUSED)
112 {
113     message_program_name = 0;
114     message_fatal_cleanup = return10;
115     errno = EPERM;
116     sysdie("fatal perm");
117 }
118 static void
119 test11(void *data UNUSED)
120 {
121     message_program_name = "test11";
122     message_fatal_cleanup = return10;
123     errno = EPERM;
124     fputs("1st ", stdout);
125     sysdie("fatal");
126 }
127
128 static void __attribute__((__format__(printf, 2, 0)))
129 log_msg(size_t len, const char *format, va_list args, int error)
130 {
131     fprintf(stderr, "%lu %d ", (unsigned long) len, error);
132     vfprintf(stderr, format, args);
133     fprintf(stderr, "\n");
134 }
135
136 static void
137 test12(void *data UNUSED)
138 {
139     message_handlers_warn(1, log_msg);
140     warn("warning");
141 }
142 static void
143 test13(void *data UNUSED)
144 {
145     message_handlers_die(1, log_msg);
146     die("fatal");
147 }
148 static void
149 test14(void *data UNUSED)
150 {
151     message_handlers_warn(2, log_msg, log_msg);
152     errno = EPERM;
153     syswarn("warning");
154 }
155 static void
156 test15(void *data UNUSED)
157 {
158     message_handlers_die(2, log_msg, log_msg);
159     message_fatal_cleanup = return10;
160     errno = EPERM;
161     sysdie("fatal");
162 }
163 static void
164 test16(void *data UNUSED)
165 {
166     message_handlers_warn(2, message_log_stderr, log_msg);
167     message_program_name = "test16";
168     errno = EPERM;
169     syswarn("warning");
170 }
171 static void
172 test17(void *data UNUSED)
173 {
174     notice("notice");
175 }
176 static void
177 test18(void *data UNUSED)
178 {
179     message_program_name = "test18";
180     notice("notice");
181 }
182 static void
183 test19(void *data UNUSED)
184 {
185     debug("debug");
186 }
187 static void
188 test20(void *data UNUSED)
189 {
190     message_handlers_notice(1, log_msg);
191     notice("foo");
192 }
193 static void
194 test21(void *data UNUSED)
195 {
196     message_handlers_debug(1, message_log_stdout);
197     message_program_name = "test23";
198     debug("baz");
199 }
200 static void
201 test22(void *data UNUSED)
202 {
203     message_handlers_die(0);
204     die("hi mom!");
205 }
206 static void
207 test23(void *data UNUSED)
208 {
209     message_handlers_warn(0);
210     warn("this is a test");
211 }
212 static void
213 test24(void *data UNUSED)
214 {
215     notice("first");
216     message_handlers_notice(0);
217     notice("second");
218     message_handlers_notice(1, message_log_stdout);
219     notice("third");
220 }
221
222
223 /*
224  * Given the intended status, intended message sans the appended strerror
225  * output, errno, and the function to run, check the output.
226  */
227 static void
228 test_strerror(int status, const char *output, int error,
229               test_function_type function)
230 {
231     char *full_output, *name;
232
233     xasprintf(&full_output, "%s: %s\n", output, strerror(error));
234     xasprintf(&name, "strerror %lu", testnum / 3 + 1);
235     is_function_output(function, NULL, status, full_output, "%s", name);
236     free(full_output);
237     free(name);
238 }
239
240
241 /*
242  * Run the tests.
243  */
244 int
245 main(void)
246 {
247     char buff[32];
248     char *output;
249
250     plan(24 * 3);
251
252     is_function_output(test1, NULL, 0, "warning\n", "test1");
253     is_function_output(test2, NULL, 1, "fatal\n", "test2");
254     test_strerror(0, "permissions", EPERM, test3);
255     test_strerror(1, "fatal access", EACCES, test4);
256     is_function_output(test5, NULL, 0, "test5: warning\n", "test5");
257     is_function_output(test6, NULL, 1, "test6: fatal\n", "test6");
258     test_strerror(0, "test7: perms 7", EPERM, test7);
259     test_strerror(1, "test8: fatal", EACCES, test8);
260     is_function_output(test9, NULL, 10, "fatal\n", "test9");
261     test_strerror(10, "fatal perm", EPERM, test10);
262     test_strerror(10, "1st test11: fatal", EPERM, test11);
263     is_function_output(test12, NULL, 0, "7 0 warning\n", "test12");
264     is_function_output(test13, NULL, 1, "5 0 fatal\n", "test13");
265
266     sprintf(buff, "%d", EPERM);
267
268     xasprintf(&output, "7 %d warning\n7 %d warning\n", EPERM, EPERM);
269     is_function_output(test14, NULL, 0, output, "test14");
270     free(output);
271     xasprintf(&output, "5 %d fatal\n5 %d fatal\n", EPERM, EPERM);
272     is_function_output(test15, NULL, 10, output, "test15");
273     free(output);
274     xasprintf(&output, "test16: warning: %s\n7 %d warning\n", strerror(EPERM),
275               EPERM);
276     is_function_output(test16, NULL, 0, output, "test16");
277     free(output);
278
279     is_function_output(test17, NULL, 0, "notice\n", "test17");
280     is_function_output(test18, NULL, 0, "test18: notice\n", "test18");
281     is_function_output(test19, NULL, 0, "", "test19");
282     is_function_output(test20, NULL, 0, "3 0 foo\n", "test20");
283     is_function_output(test21, NULL, 0, "test23: baz\n", "test21");
284
285     /* Make sure that it's possible to turn off a message type entirely. */
286     is_function_output(test22, NULL, 1, "", "test22");
287     is_function_output(test23, NULL, 0, "", "test23");
288     is_function_output(test24, NULL, 0, "first\nthird\n", "test24");
289
290     return 0;
291 }