]> eyrie.org Git - kerberos/krb5-strength.git/blob - tests/util/messages-t.c
Change CrackLib tests for system CrackLib
[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 test1(void *data UNUSED) { warn("warning"); }
50 static void test2(void *data UNUSED) { die("fatal"); }
51 static void test3(void *data UNUSED) { errno = EPERM; syswarn("permissions"); }
52 static void test4(void *data UNUSED) {
53     errno = EACCES;
54     sysdie("fatal access");
55 }
56 static void test5(void *data UNUSED) {
57     message_program_name = "test5";
58     warn("warning");
59 }
60 static void test6(void *data UNUSED) {
61     message_program_name = "test6";
62     die("fatal");
63 }
64 static void test7(void *data UNUSED) {
65     message_program_name = "test7";
66     errno = EPERM;
67     syswarn("perms %d", 7);
68 }
69 static void test8(void *data UNUSED) {
70     message_program_name = "test8";
71     errno = EACCES;
72     sysdie("%st%s", "fa", "al");
73 }
74
75 static int return10(void) { return 10; }
76
77 static void test9(void *data UNUSED) {
78     message_fatal_cleanup = return10;
79     die("fatal");
80 }
81 static void test10(void *data UNUSED) {
82     message_program_name = 0;
83     message_fatal_cleanup = return10;
84     errno = EPERM;
85     sysdie("fatal perm");
86 }
87 static void test11(void *data UNUSED) {
88     message_program_name = "test11";
89     message_fatal_cleanup = return10;
90     errno = EPERM;
91     fputs("1st ", stdout);
92     sysdie("fatal");
93 }
94
95 static void __attribute__((__format__(printf, 2, 0)))
96 log_msg(size_t len, const char *format, va_list args, int error) {
97     fprintf(stderr, "%lu %d ", (unsigned long) len, error);
98     vfprintf(stderr, format, args);
99     fprintf(stderr, "\n");
100 }
101
102 static void test12(void *data UNUSED) {
103     message_handlers_warn(1, log_msg);
104     warn("warning");
105 }
106 static void test13(void *data UNUSED) {
107     message_handlers_die(1, log_msg);
108     die("fatal");
109 }
110 static void test14(void *data UNUSED) {
111     message_handlers_warn(2, log_msg, log_msg);
112     errno = EPERM;
113     syswarn("warning");
114 }
115 static void test15(void *data UNUSED) {
116     message_handlers_die(2, log_msg, log_msg);
117     message_fatal_cleanup = return10;
118     errno = EPERM;
119     sysdie("fatal");
120 }
121 static void test16(void *data UNUSED) {
122     message_handlers_warn(2, message_log_stderr, log_msg);
123     message_program_name = "test16";
124     errno = EPERM;
125     syswarn("warning");
126 }
127 static void test17(void *data UNUSED) { notice("notice"); }
128 static void test18(void *data UNUSED) {
129     message_program_name = "test18";
130     notice("notice");
131 }
132 static void test19(void *data UNUSED) { debug("debug"); }
133 static void test20(void *data UNUSED) {
134     message_handlers_notice(1, log_msg);
135     notice("foo");
136 }
137 static void test21(void *data UNUSED) {
138     message_handlers_debug(1, message_log_stdout);
139     message_program_name = "test23";
140     debug("baz");
141 }
142 static void test22(void *data UNUSED) {
143     message_handlers_die(0);
144     die("hi mom!");
145 }
146 static void test23(void *data UNUSED) {
147     message_handlers_warn(0);
148     warn("this is a test");
149 }
150 static void test24(void *data UNUSED) {
151     notice("first");
152     message_handlers_notice(0);
153     notice("second");
154     message_handlers_notice(1, message_log_stdout);
155     notice("third");
156 }
157
158
159 /*
160  * Given the intended status, intended message sans the appended strerror
161  * output, errno, and the function to run, check the output.
162  */
163 static void
164 test_strerror(int status, const char *output, int error,
165               test_function_type function)
166 {
167     char *full_output, *name;
168
169     xasprintf(&full_output, "%s: %s\n", output, strerror(error));
170     xasprintf(&name, "strerror %lu", testnum / 3 + 1);
171     is_function_output(function, NULL, status, full_output, "%s", name);
172     free(full_output);
173     free(name);
174 }
175
176
177 /*
178  * Run the tests.
179  */
180 int
181 main(void)
182 {
183     char buff[32];
184     char *output;
185
186     plan(24 * 3);
187
188     is_function_output(test1, NULL, 0, "warning\n", "test1");
189     is_function_output(test2, NULL, 1, "fatal\n", "test2");
190     test_strerror(0, "permissions", EPERM, test3);
191     test_strerror(1, "fatal access", EACCES, test4);
192     is_function_output(test5, NULL, 0, "test5: warning\n", "test5");
193     is_function_output(test6, NULL, 1, "test6: fatal\n", "test6");
194     test_strerror(0, "test7: perms 7", EPERM, test7);
195     test_strerror(1, "test8: fatal", EACCES, test8);
196     is_function_output(test9, NULL, 10, "fatal\n", "test9");
197     test_strerror(10, "fatal perm", EPERM, test10);
198     test_strerror(10, "1st test11: fatal", EPERM, test11);
199     is_function_output(test12, NULL, 0, "7 0 warning\n", "test12");
200     is_function_output(test13, NULL, 1, "5 0 fatal\n", "test13");
201
202     sprintf(buff, "%d", EPERM);
203
204     xasprintf(&output, "7 %d warning\n7 %d warning\n", EPERM, EPERM);
205     is_function_output(test14, NULL, 0, output, "test14");
206     free(output);
207     xasprintf(&output, "5 %d fatal\n5 %d fatal\n", EPERM, EPERM);
208     is_function_output(test15, NULL, 10, output, "test15");
209     free(output);
210     xasprintf(&output, "test16: warning: %s\n7 %d warning\n", strerror(EPERM),
211               EPERM);
212     is_function_output(test16, NULL, 0, output, "test16");
213     free(output);
214
215     is_function_output(test17, NULL, 0, "notice\n", "test17");
216     is_function_output(test18, NULL, 0, "test18: notice\n", "test18");
217     is_function_output(test19, NULL, 0, "", "test19");
218     is_function_output(test20, NULL, 0, "3 0 foo\n", "test20");
219     is_function_output(test21, NULL, 0, "test23: baz\n", "test21");
220
221     /* Make sure that it's possible to turn off a message type entirely. */ 
222     is_function_output(test22, NULL, 1, "", "test22");
223     is_function_output(test23, NULL, 0, "", "test23");
224     is_function_output(test24, NULL, 0, "first\nthird\n", "test24");
225
226     return 0;
227 }