1*d3273b5bSchristos /* $NetBSD: parse-name-test.c,v 1.2 2017/01/28 21:31:49 christos Exp $ */
2ca1c9b0cSelric
3ca1c9b0cSelric /*
4ca1c9b0cSelric * Copyright (c) 2002 Kungliga Tekniska Högskolan
5ca1c9b0cSelric * (Royal Institute of Technology, Stockholm, Sweden).
6ca1c9b0cSelric * All rights reserved.
7ca1c9b0cSelric *
8ca1c9b0cSelric * Redistribution and use in source and binary forms, with or without
9ca1c9b0cSelric * modification, are permitted provided that the following conditions
10ca1c9b0cSelric * are met:
11ca1c9b0cSelric *
12ca1c9b0cSelric * 1. Redistributions of source code must retain the above copyright
13ca1c9b0cSelric * notice, this list of conditions and the following disclaimer.
14ca1c9b0cSelric *
15ca1c9b0cSelric * 2. Redistributions in binary form must reproduce the above copyright
16ca1c9b0cSelric * notice, this list of conditions and the following disclaimer in the
17ca1c9b0cSelric * documentation and/or other materials provided with the distribution.
18ca1c9b0cSelric *
19ca1c9b0cSelric * 3. Neither the name of KTH nor the names of its contributors may be
20ca1c9b0cSelric * used to endorse or promote products derived from this software without
21ca1c9b0cSelric * specific prior written permission.
22ca1c9b0cSelric *
23ca1c9b0cSelric * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
24ca1c9b0cSelric * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ca1c9b0cSelric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26ca1c9b0cSelric * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
27ca1c9b0cSelric * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28ca1c9b0cSelric * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29ca1c9b0cSelric * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30ca1c9b0cSelric * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31ca1c9b0cSelric * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32ca1c9b0cSelric * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33ca1c9b0cSelric * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
34ca1c9b0cSelric
35ca1c9b0cSelric #include "krb5_locl.h"
36ca1c9b0cSelric #include <err.h>
37ca1c9b0cSelric
38ca1c9b0cSelric enum { MAX_COMPONENTS = 3 };
39ca1c9b0cSelric
40ca1c9b0cSelric static struct testcase {
41ca1c9b0cSelric const char *input_string;
42ca1c9b0cSelric const char *output_string;
43ca1c9b0cSelric krb5_realm realm;
44ca1c9b0cSelric unsigned ncomponents;
45ca1c9b0cSelric char *comp_val[MAX_COMPONENTS];
46ca1c9b0cSelric int realmp;
47ca1c9b0cSelric } tests[] = {
48ca1c9b0cSelric {"", "@", "", 1, {""}, FALSE},
49ca1c9b0cSelric {"a", "a@", "", 1, {"a"}, FALSE},
50ca1c9b0cSelric {"\\n", "\\n@", "", 1, {"\n"}, FALSE},
51ca1c9b0cSelric {"\\ ", "\\ @", "", 1, {" "}, FALSE},
52ca1c9b0cSelric {"\\t", "\\t@", "", 1, {"\t"}, FALSE},
53ca1c9b0cSelric {"\\b", "\\b@", "", 1, {"\b"}, FALSE},
54ca1c9b0cSelric {"\\\\", "\\\\@", "", 1, {"\\"}, FALSE},
55ca1c9b0cSelric {"\\/", "\\/@", "", 1, {"/"}, FALSE},
56ca1c9b0cSelric {"\\@", "\\@@", "", 1, {"@"}, FALSE},
57ca1c9b0cSelric {"@", "@", "", 1, {""}, TRUE},
58ca1c9b0cSelric {"a/b", "a/b@", "", 2, {"a", "b"}, FALSE},
59ca1c9b0cSelric {"a/", "a/@", "", 2, {"a", ""}, FALSE},
60ca1c9b0cSelric {"a\\//\\/", "a\\//\\/@", "", 2, {"a/", "/"}, FALSE},
61ca1c9b0cSelric {"/a", "/a@", "", 2, {"", "a"}, FALSE},
62ca1c9b0cSelric {"\\@@\\@", "\\@@\\@", "@", 1, {"@"}, TRUE},
63ca1c9b0cSelric {"a/b/c", "a/b/c@", "", 3, {"a", "b", "c"}, FALSE},
64ca1c9b0cSelric {NULL, NULL, "", 0, { NULL }, FALSE}};
65ca1c9b0cSelric
66ca1c9b0cSelric int
main(int argc,char ** argv)67ca1c9b0cSelric main(int argc, char **argv)
68ca1c9b0cSelric {
69ca1c9b0cSelric struct testcase *t;
70ca1c9b0cSelric krb5_context context;
71ca1c9b0cSelric krb5_error_code ret;
72ca1c9b0cSelric int val = 0;
73ca1c9b0cSelric
74ca1c9b0cSelric ret = krb5_init_context (&context);
75ca1c9b0cSelric if (ret)
76ca1c9b0cSelric errx (1, "krb5_init_context failed: %d", ret);
77ca1c9b0cSelric
78ca1c9b0cSelric /* to enable realm-less principal name above */
79ca1c9b0cSelric
80ca1c9b0cSelric krb5_set_default_realm(context, "");
81ca1c9b0cSelric
82ca1c9b0cSelric for (t = tests; t->input_string; ++t) {
83ca1c9b0cSelric krb5_principal princ;
84ca1c9b0cSelric int i, j;
85ca1c9b0cSelric char name_buf[1024];
86ca1c9b0cSelric char *s;
87ca1c9b0cSelric
88ca1c9b0cSelric ret = krb5_parse_name(context, t->input_string, &princ);
89ca1c9b0cSelric if (ret)
90ca1c9b0cSelric krb5_err (context, 1, ret, "krb5_parse_name %s",
91ca1c9b0cSelric t->input_string);
92ca1c9b0cSelric if (strcmp (t->realm, princ->realm) != 0) {
93ca1c9b0cSelric printf ("wrong realm (\"%s\" should be \"%s\")"
94ca1c9b0cSelric " for \"%s\"\n",
95ca1c9b0cSelric princ->realm, t->realm,
96ca1c9b0cSelric t->input_string);
97ca1c9b0cSelric val = 1;
98ca1c9b0cSelric }
99ca1c9b0cSelric
100ca1c9b0cSelric if (t->ncomponents != princ->name.name_string.len) {
101ca1c9b0cSelric printf ("wrong number of components (%u should be %u)"
102ca1c9b0cSelric " for \"%s\"\n",
103ca1c9b0cSelric princ->name.name_string.len, t->ncomponents,
104ca1c9b0cSelric t->input_string);
105ca1c9b0cSelric val = 1;
106ca1c9b0cSelric } else {
107ca1c9b0cSelric for (i = 0; i < t->ncomponents; ++i) {
108ca1c9b0cSelric if (strcmp(t->comp_val[i],
109ca1c9b0cSelric princ->name.name_string.val[i]) != 0) {
110ca1c9b0cSelric printf ("bad component %d (\"%s\" should be \"%s\")"
111ca1c9b0cSelric " for \"%s\"\n",
112ca1c9b0cSelric i,
113ca1c9b0cSelric princ->name.name_string.val[i],
114ca1c9b0cSelric t->comp_val[i],
115ca1c9b0cSelric t->input_string);
116ca1c9b0cSelric val = 1;
117ca1c9b0cSelric }
118ca1c9b0cSelric }
119ca1c9b0cSelric }
120ca1c9b0cSelric for (j = 0; j < strlen(t->output_string); ++j) {
121ca1c9b0cSelric ret = krb5_unparse_name_fixed(context, princ,
122ca1c9b0cSelric name_buf, j);
123ca1c9b0cSelric if (ret != ERANGE) {
124ca1c9b0cSelric printf ("unparse_name %s with length %d should have failed\n",
125ca1c9b0cSelric t->input_string, j);
126ca1c9b0cSelric val = 1;
127ca1c9b0cSelric break;
128ca1c9b0cSelric }
129ca1c9b0cSelric }
130ca1c9b0cSelric ret = krb5_unparse_name_fixed(context, princ,
131ca1c9b0cSelric name_buf, sizeof(name_buf));
132ca1c9b0cSelric if (ret)
133ca1c9b0cSelric krb5_err (context, 1, ret, "krb5_unparse_name_fixed");
134ca1c9b0cSelric
135ca1c9b0cSelric if (strcmp (t->output_string, name_buf) != 0) {
136ca1c9b0cSelric printf ("failed comparing the re-parsed"
137ca1c9b0cSelric " (\"%s\" should be \"%s\")\n",
138ca1c9b0cSelric name_buf, t->output_string);
139ca1c9b0cSelric val = 1;
140ca1c9b0cSelric }
141ca1c9b0cSelric
142ca1c9b0cSelric ret = krb5_unparse_name(context, princ, &s);
143ca1c9b0cSelric if (ret)
144ca1c9b0cSelric krb5_err (context, 1, ret, "krb5_unparse_name");
145ca1c9b0cSelric
146ca1c9b0cSelric if (strcmp (t->output_string, s) != 0) {
147ca1c9b0cSelric printf ("failed comparing the re-parsed"
148ca1c9b0cSelric " (\"%s\" should be \"%s\"\n",
149ca1c9b0cSelric s, t->output_string);
150ca1c9b0cSelric val = 1;
151ca1c9b0cSelric }
152ca1c9b0cSelric free(s);
153ca1c9b0cSelric
154ca1c9b0cSelric if (!t->realmp) {
155ca1c9b0cSelric for (j = 0; j < strlen(t->input_string); ++j) {
156ca1c9b0cSelric ret = krb5_unparse_name_fixed_short(context, princ,
157ca1c9b0cSelric name_buf, j);
158ca1c9b0cSelric if (ret != ERANGE) {
159ca1c9b0cSelric printf ("unparse_name_short %s with length %d"
160ca1c9b0cSelric " should have failed\n",
161ca1c9b0cSelric t->input_string, j);
162ca1c9b0cSelric val = 1;
163ca1c9b0cSelric break;
164ca1c9b0cSelric }
165ca1c9b0cSelric }
166ca1c9b0cSelric ret = krb5_unparse_name_fixed_short(context, princ,
167ca1c9b0cSelric name_buf, sizeof(name_buf));
168ca1c9b0cSelric if (ret)
169ca1c9b0cSelric krb5_err (context, 1, ret, "krb5_unparse_name_fixed");
170ca1c9b0cSelric
171ca1c9b0cSelric if (strcmp (t->input_string, name_buf) != 0) {
172ca1c9b0cSelric printf ("failed comparing the re-parsed"
173ca1c9b0cSelric " (\"%s\" should be \"%s\")\n",
174ca1c9b0cSelric name_buf, t->input_string);
175ca1c9b0cSelric val = 1;
176ca1c9b0cSelric }
177ca1c9b0cSelric
178ca1c9b0cSelric ret = krb5_unparse_name_short(context, princ, &s);
179ca1c9b0cSelric if (ret)
180ca1c9b0cSelric krb5_err (context, 1, ret, "krb5_unparse_name_short");
181ca1c9b0cSelric
182ca1c9b0cSelric if (strcmp (t->input_string, s) != 0) {
183ca1c9b0cSelric printf ("failed comparing the re-parsed"
184ca1c9b0cSelric " (\"%s\" should be \"%s\"\n",
185ca1c9b0cSelric s, t->input_string);
186ca1c9b0cSelric val = 1;
187ca1c9b0cSelric }
188ca1c9b0cSelric free(s);
189ca1c9b0cSelric }
190ca1c9b0cSelric krb5_free_principal (context, princ);
191ca1c9b0cSelric }
192ca1c9b0cSelric krb5_free_context(context);
193ca1c9b0cSelric return val;
194ca1c9b0cSelric }
195