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