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