2 * Test suite for Kerberos error handling routines.
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/>.
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
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:
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
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.
30 * SPDX-License-Identifier: MIT
35 # include <portable/krb5.h>
37 #include <portable/system.h>
39 #include <tests/tap/basic.h>
40 #include <tests/tap/process.h>
41 #include <util/macros.h>
43 # include <util/messages-krb5.h>
45 #include <util/messages.h>
46 #include <util/xmalloc.h>
49 /* Skip the whole test if not built with Kerberos support. */
54 skip_all("not built with Kerberos support");
62 __attribute__((__noreturn__)) static void
63 test_warn(void *data UNUSED)
69 code = krb5_init_context(&ctx);
71 die_krb5(ctx, code, "cannot create context");
72 code = krb5_parse_name(ctx, "foo@bar@EXAMPLE.COM", &princ);
74 warn_krb5(ctx, code, "principal parse failed");
76 die("unexpected success parsing principal");
80 __attribute__((__noreturn__)) static void
81 test_die(void *data UNUSED)
87 code = krb5_init_context(&ctx);
89 die_krb5(ctx, code, "cannot create context");
90 code = krb5_parse_name(ctx, "foo@bar@EXAMPLE.COM", &princ);
92 die_krb5(ctx, code, "principal parse failed");
94 die("unexpected success parsing principal");
106 krb5_error_code code;
107 krb5_principal princ;
113 /* First, we have to get what the correct error message is. */
114 code = krb5_init_context(&ctx);
116 bail("cannot create context");
117 code = krb5_parse_name(ctx, "foo@bar@EXAMPLE.COM", &princ);
118 message = krb5_get_error_message(ctx, code);
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");
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");
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");
136 krb5_free_error_message(ctx, message);
137 krb5_free_context(ctx);
141 #endif /* HAVE_KRB5 */