2 * Copyright (c) 2002 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of KTH nor the names of its contributors may be
18 * used to endorse or promote products derived from this software without
19 * specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
33 #include "krb5_locl.h"
35 RCSID("$Id: parse-name-test.c,v 1.3.4.1 2004/03/22 19:27:36 joda Exp $");
37 enum { MAX_COMPONENTS = 3 };
39 static struct testcase {
40 const char *input_string;
41 const char *output_string;
44 char *comp_val[MAX_COMPONENTS];
47 {"", "@", "", 1, {""}, FALSE},
48 {"a", "a@", "", 1, {"a"}, FALSE},
49 {"\\n", "\\n@", "", 1, {"\n"}, FALSE},
50 {"\\ ", "\\ @", "", 1, {" "}, FALSE},
51 {"\\t", "\\t@", "", 1, {"\t"}, FALSE},
52 {"\\b", "\\b@", "", 1, {"\b"}, FALSE},
53 {"\\\\", "\\\\@", "", 1, {"\\"}, FALSE},
54 {"\\/", "\\/@", "", 1, {"/"}, FALSE},
55 {"\\@", "\\@@", "", 1, {"@"}, FALSE},
56 {"@", "@", "", 1, {""}, TRUE},
57 {"a/b", "a/b@", "", 2, {"a", "b"}, FALSE},
58 {"a/", "a/@", "", 2, {"a", ""}, FALSE},
59 {"a\\//\\/", "a\\//\\/@", "", 2, {"a/", "/"}, FALSE},
60 {"/a", "/a@", "", 2, {"", "a"}, FALSE},
61 {"\\@@\\@", "\\@@\\@", "@", 1, {"@"}, TRUE},
62 {"a/b/c", "a/b/c@", "", 3, {"a", "b", "c"}, FALSE},
63 {NULL, NULL, "", 0, { NULL }, FALSE}};
66 main(int argc, char **argv)
73 ret = krb5_init_context (&context);
75 errx (1, "krb5_init_context failed: %d", ret);
77 /* to enable realm-less principal name above */
79 krb5_set_default_realm(context, "");
81 for (t = tests; t->input_string; ++t) {
87 ret = krb5_parse_name(context, t->input_string, &princ);
89 krb5_err (context, 1, ret, "krb5_parse_name %s",
91 if (strcmp (t->realm, princ->realm) != 0) {
92 printf ("wrong realm (\"%s\" should be \"%s\")"
94 princ->realm, t->realm,
99 if (t->ncomponents != princ->name.name_string.len) {
100 printf ("wrong number of components (%u should be %u)"
102 princ->name.name_string.len, t->ncomponents,
106 for (i = 0; i < t->ncomponents; ++i) {
107 if (strcmp(t->comp_val[i],
108 princ->name.name_string.val[i]) != 0) {
109 printf ("bad component %d (\"%s\" should be \"%s\")"
112 princ->name.name_string.val[i],
119 for (j = 0; j < strlen(t->output_string); ++j) {
120 ret = krb5_unparse_name_fixed(context, princ,
123 printf ("unparse_name %s with length %d should have failed\n",
129 ret = krb5_unparse_name_fixed(context, princ,
130 name_buf, sizeof(name_buf));
132 krb5_err (context, 1, ret, "krb5_unparse_name_fixed");
134 if (strcmp (t->output_string, name_buf) != 0) {
135 printf ("failed comparing the re-parsed"
136 " (\"%s\" should be \"%s\")\n",
137 name_buf, t->output_string);
141 ret = krb5_unparse_name(context, princ, &s);
143 krb5_err (context, 1, ret, "krb5_unparse_name");
145 if (strcmp (t->output_string, s) != 0) {
146 printf ("failed comparing the re-parsed"
147 " (\"%s\" should be \"%s\"\n",
148 s, t->output_string);
154 for (j = 0; j < strlen(t->input_string); ++j) {
155 ret = krb5_unparse_name_fixed_short(context, princ,
158 printf ("unparse_name_short %s with length %d"
159 " should have failed\n",
165 ret = krb5_unparse_name_fixed_short(context, princ,
166 name_buf, sizeof(name_buf));
168 krb5_err (context, 1, ret, "krb5_unparse_name_fixed");
170 if (strcmp (t->input_string, name_buf) != 0) {
171 printf ("failed comparing the re-parsed"
172 " (\"%s\" should be \"%s\")\n",
173 name_buf, t->input_string);
177 ret = krb5_unparse_name_short(context, princ, &s);
179 krb5_err (context, 1, ret, "krb5_unparse_name_short");
181 if (strcmp (t->input_string, s) != 0) {
182 printf ("failed comparing the re-parsed"
183 " (\"%s\" should be \"%s\"\n",
189 krb5_free_principal (context, princ);