]> eyrie.org Git - kerberos/krb5-strength.git/blob - tests/util/messages-krb5-t.c
New upstream version 3.2
[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 2020 Russ Allbery <eagle@eyrie.org>
9  * Copyright 2010-2011, 2013-2014
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 #ifdef HAVE_KRB5
35 #    include <portable/krb5.h>
36 #endif
37 #include <portable/system.h>
38
39 #include <tests/tap/basic.h>
40 #include <tests/tap/process.h>
41 #include <util/macros.h>
42 #ifdef HAVE_KRB5
43 #    include <util/messages-krb5.h>
44 #endif
45 #include <util/messages.h>
46 #include <util/xmalloc.h>
47
48
49 /* Skip the whole test if not built with Kerberos support. */
50 #ifndef HAVE_KRB5
51 int
52 main(void)
53 {
54     skip_all("not built with Kerberos support");
55     return 0;
56 }
57 #else
58
59 /*
60  * Test functions.
61  */
62 __attribute__((__noreturn__)) static void
63 test_warn(void *data UNUSED)
64 {
65     krb5_context ctx;
66     krb5_error_code code;
67     krb5_principal princ;
68
69     code = krb5_init_context(&ctx);
70     if (code < 0)
71         die_krb5(ctx, code, "cannot create context");
72     code = krb5_parse_name(ctx, "foo@bar@EXAMPLE.COM", &princ);
73     if (code < 0)
74         warn_krb5(ctx, code, "principal parse failed");
75     else
76         die("unexpected success parsing principal");
77     exit(0);
78 }
79
80 __attribute__((__noreturn__)) static void
81 test_die(void *data UNUSED)
82 {
83     krb5_context ctx;
84     krb5_error_code code;
85     krb5_principal princ;
86
87     code = krb5_init_context(&ctx);
88     if (code < 0)
89         die_krb5(ctx, code, "cannot create context");
90     code = krb5_parse_name(ctx, "foo@bar@EXAMPLE.COM", &princ);
91     if (code < 0)
92         die_krb5(ctx, code, "principal parse failed");
93     else
94         die("unexpected success parsing principal");
95     exit(0);
96 }
97
98
99 /*
100  * Run the tests.
101  */
102 int
103 main(void)
104 {
105     krb5_context ctx;
106     krb5_error_code code;
107     krb5_principal princ;
108     const char *message;
109     char *wanted;
110
111     plan(6 * 3);
112
113     /* First, we have to get what the correct error message is. */
114     code = krb5_init_context(&ctx);
115     if (code < 0)
116         bail("cannot create context");
117     code = krb5_parse_name(ctx, "foo@bar@EXAMPLE.COM", &princ);
118     message = krb5_get_error_message(ctx, code);
119
120     xasprintf(&wanted, "principal parse failed: %s\n", message);
121     is_function_output(test_warn, NULL, 0, wanted, "warn_krb5");
122     is_function_output(test_die, NULL, 1, wanted, "die_krb5");
123     free(wanted);
124
125     message_program_name = "msg-test";
126     xasprintf(&wanted, "msg-test: principal parse failed: %s\n", message);
127     is_function_output(test_warn, NULL, 0, wanted, "warn_krb5 with name");
128     is_function_output(test_die, NULL, 1, wanted, "die_krb5 with name");
129     free(wanted);
130
131     message_handlers_warn(0);
132     is_function_output(test_warn, NULL, 0, "", "warn_krb5 with no handlers");
133     message_handlers_die(0);
134     is_function_output(test_die, NULL, 1, "", "warn_krb5 with no handlers");
135
136     krb5_free_error_message(ctx, message);
137     krb5_free_context(ctx);
138     return 0;
139 }
140
141 #endif /* HAVE_KRB5 */