]> eyrie.org Git - kerberos/krb5-strength.git/blob - tests/util/messages-krb5-t.c
Change CrackLib tests for system CrackLib
[kerberos/krb5-strength.git] / tests / util / messages-krb5-t.c
1 /*
2  * Test suite for Kerberos 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 2010, 2011, 2013, 2014
9  *     The Board of Trustees of the Leland Stanford Junior University
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included in
19  * all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  */
29
30 #include <config.h>
31 #ifdef HAVE_KRB5
32 # include <portable/krb5.h>
33 #endif
34 #include <portable/system.h>
35
36 #include <tests/tap/basic.h>
37 #include <tests/tap/process.h>
38 #include <util/macros.h>
39 #ifdef HAVE_KRB5
40 # include <util/messages-krb5.h>
41 #endif
42 #include <util/messages.h>
43 #include <util/xmalloc.h>
44
45
46 /* Skip the whole test if not built with Kerberos support. */
47 #ifndef HAVE_KRB5
48 int
49 main(void)
50 {
51     skip_all("not built with Kerberos support");
52     return 0;
53 }
54 #else
55
56 /*
57  * Test functions.
58  */
59 static void
60 test_warn(void *data UNUSED)
61 {
62     krb5_context ctx;
63     krb5_error_code code;
64     krb5_principal princ;
65
66     code = krb5_init_context(&ctx);
67     if (code < 0)
68         die_krb5(ctx, code, "cannot create context");
69     code = krb5_parse_name(ctx, "foo@bar@EXAMPLE.COM", &princ);
70     if (code < 0)
71         warn_krb5(ctx, code, "principal parse failed");
72     else
73         die("unexpected success parsing principal");
74     exit(0);
75 }
76
77 static void
78 test_die(void *data UNUSED)
79 {
80     krb5_context ctx;
81     krb5_error_code code;
82     krb5_principal princ;
83
84     code = krb5_init_context(&ctx);
85     if (code < 0)
86         die_krb5(ctx, code, "cannot create context");
87     code = krb5_parse_name(ctx, "foo@bar@EXAMPLE.COM", &princ);
88     if (code < 0)
89         die_krb5(ctx, code, "principal parse failed");
90     else
91         die("unexpected success parsing principal");
92     exit(0);
93 }
94
95
96 /*
97  * Run the tests.
98  */
99 int
100 main(void)
101 {
102     krb5_context ctx;
103     krb5_error_code code;
104     krb5_principal princ;
105     const char *message;
106     char *wanted;
107
108     plan(6 * 3);
109
110     /* First, we have to get what the correct error message is. */
111     code = krb5_init_context(&ctx);
112     if (code < 0)
113         bail("cannot create context");
114     code = krb5_parse_name(ctx, "foo@bar@EXAMPLE.COM", &princ);
115     message = krb5_get_error_message(ctx, code);
116
117     xasprintf(&wanted, "principal parse failed: %s\n", message);
118     is_function_output(test_warn, NULL, 0, wanted, "warn_krb5");
119     is_function_output(test_die, NULL, 1, wanted, "die_krb5");
120     free(wanted);
121
122     message_program_name = "msg-test";
123     xasprintf(&wanted, "msg-test: principal parse failed: %s\n", message);
124     is_function_output(test_warn, NULL, 0, wanted, "warn_krb5 with name");
125     is_function_output(test_die, NULL, 1, wanted, "die_krb5 with name");
126     free(wanted);
127
128     message_handlers_warn(0);
129     is_function_output(test_warn, NULL, 0, "", "warn_krb5 with no handlers");
130     message_handlers_die(0);
131     is_function_output(test_die, NULL, 1, "", "warn_krb5 with no handlers");
132
133     krb5_free_error_message(ctx, message);
134     krb5_free_context(ctx);
135     return 0;
136 }
137
138 #endif /* HAVE_KRB5 */