1 /* $NetBSD: test_princ.c,v 1.1.1.1 2011/04/13 18:15:38 elric Exp $ */
4 * Copyright (c) 2003 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of KTH nor the names of its contributors may be
20 * used to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
24 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
35 #include "krb5_locl.h"
39 * Check that a closed cc still keeps it data and that it's no longer
40 * there when it's destroyed.
44 test_princ(krb5_context context
)
46 const char *princ
= "lha@SU.SE";
47 const char *princ_short
= "lha";
51 char *princ_reformed
= NULL
;
56 ret
= krb5_parse_name(context
, princ
, &p
);
58 krb5_err(context
, 1, ret
, "krb5_parse_name");
60 ret
= krb5_unparse_name(context
, p
, &princ_unparsed
);
62 krb5_err(context
, 1, ret
, "krb5_parse_name");
64 if (strcmp(princ
, princ_unparsed
)) {
65 krb5_errx(context
, 1, "%s != %s", princ
, princ_unparsed
);
70 ret
= krb5_unparse_name_flags(context
, p
,
71 KRB5_PRINCIPAL_UNPARSE_NO_REALM
,
74 krb5_err(context
, 1, ret
, "krb5_parse_name");
76 if (strcmp(princ_short
, princ_unparsed
))
77 krb5_errx(context
, 1, "%s != %s", princ_short
, princ_unparsed
);
80 realm
= krb5_principal_get_realm(context
, p
);
82 if (asprintf(&princ_reformed
, "%s@%s", princ_short
, realm
) < 0 || princ_reformed
== NULL
)
85 ret
= krb5_parse_name(context
, princ_reformed
, &p2
);
88 krb5_err(context
, 1, ret
, "krb5_parse_name");
90 if (!krb5_principal_compare(context
, p
, p2
)) {
91 krb5_errx(context
, 1, "p != p2");
94 krb5_free_principal(context
, p2
);
96 ret
= krb5_set_default_realm(context
, "SU.SE");
98 krb5_err(context
, 1, ret
, "krb5_parse_name");
100 ret
= krb5_unparse_name_flags(context
, p
,
101 KRB5_PRINCIPAL_UNPARSE_SHORT
,
104 krb5_err(context
, 1, ret
, "krb5_parse_name");
106 if (strcmp(princ_short
, princ_unparsed
))
107 krb5_errx(context
, 1, "'%s' != '%s'", princ_short
, princ_unparsed
);
108 free(princ_unparsed
);
110 ret
= krb5_parse_name(context
, princ_short
, &p2
);
112 krb5_err(context
, 1, ret
, "krb5_parse_name");
114 if (!krb5_principal_compare(context
, p
, p2
))
115 krb5_errx(context
, 1, "p != p2");
116 krb5_free_principal(context
, p2
);
118 ret
= krb5_unparse_name(context
, p
, &princ_unparsed
);
120 krb5_err(context
, 1, ret
, "krb5_parse_name");
122 if (strcmp(princ
, princ_unparsed
))
123 krb5_errx(context
, 1, "'%s' != '%s'", princ
, princ_unparsed
);
124 free(princ_unparsed
);
126 ret
= krb5_set_default_realm(context
, "SAMBA.ORG");
128 krb5_err(context
, 1, ret
, "krb5_parse_name");
130 ret
= krb5_parse_name(context
, princ_short
, &p2
);
132 krb5_err(context
, 1, ret
, "krb5_parse_name");
134 if (krb5_principal_compare(context
, p
, p2
))
135 krb5_errx(context
, 1, "p == p2");
137 if (!krb5_principal_compare_any_realm(context
, p
, p2
))
138 krb5_errx(context
, 1, "(ignoring realms) p != p2");
140 ret
= krb5_unparse_name(context
, p2
, &princ_unparsed
);
142 krb5_err(context
, 1, ret
, "krb5_parse_name");
144 if (strcmp(princ
, princ_unparsed
) == 0)
145 krb5_errx(context
, 1, "%s == %s", princ
, princ_unparsed
);
146 free(princ_unparsed
);
148 krb5_free_principal(context
, p2
);
150 ret
= krb5_parse_name(context
, princ
, &p2
);
152 krb5_err(context
, 1, ret
, "krb5_parse_name");
154 if (!krb5_principal_compare(context
, p
, p2
))
155 krb5_errx(context
, 1, "p != p2");
157 ret
= krb5_unparse_name(context
, p2
, &princ_unparsed
);
159 krb5_err(context
, 1, ret
, "krb5_parse_name");
161 if (strcmp(princ
, princ_unparsed
))
162 krb5_errx(context
, 1, "'%s' != '%s'", princ
, princ_unparsed
);
163 free(princ_unparsed
);
165 krb5_free_principal(context
, p2
);
167 ret
= krb5_unparse_name_flags(context
, p
,
168 KRB5_PRINCIPAL_UNPARSE_SHORT
,
171 krb5_err(context
, 1, ret
, "krb5_unparse_name_short");
173 if (strcmp(princ
, princ_unparsed
) != 0)
174 krb5_errx(context
, 1, "'%s' != '%s'", princ
, princ_unparsed
);
175 free(princ_unparsed
);
177 ret
= krb5_unparse_name(context
, p
, &princ_unparsed
);
179 krb5_err(context
, 1, ret
, "krb5_unparse_name_short");
181 if (strcmp(princ
, princ_unparsed
))
182 krb5_errx(context
, 1, "'%s' != '%s'", princ
, princ_unparsed
);
183 free(princ_unparsed
);
185 ret
= krb5_parse_name_flags(context
, princ
,
186 KRB5_PRINCIPAL_PARSE_NO_REALM
,
189 krb5_err(context
, 1, ret
, "Should have failed to parse %s a "
190 "short name", princ
);
192 ret
= krb5_parse_name_flags(context
, princ_short
,
193 KRB5_PRINCIPAL_PARSE_NO_REALM
,
196 krb5_err(context
, 1, ret
, "krb5_parse_name");
198 ret
= krb5_unparse_name_flags(context
, p2
,
199 KRB5_PRINCIPAL_UNPARSE_NO_REALM
,
201 krb5_free_principal(context
, p2
);
203 krb5_err(context
, 1, ret
, "krb5_unparse_name_norealm");
205 if (strcmp(princ_short
, princ_unparsed
))
206 krb5_errx(context
, 1, "'%s' != '%s'", princ_short
, princ_unparsed
);
207 free(princ_unparsed
);
209 ret
= krb5_parse_name_flags(context
, princ_short
,
210 KRB5_PRINCIPAL_PARSE_REQUIRE_REALM
,
213 krb5_err(context
, 1, ret
, "Should have failed to parse %s "
214 "because it lacked a realm", princ_short
);
216 ret
= krb5_parse_name_flags(context
, princ
,
217 KRB5_PRINCIPAL_PARSE_REQUIRE_REALM
,
220 krb5_err(context
, 1, ret
, "krb5_parse_name");
222 if (!krb5_principal_compare(context
, p
, p2
))
223 krb5_errx(context
, 1, "p != p2");
225 ret
= krb5_unparse_name_flags(context
, p2
,
226 KRB5_PRINCIPAL_UNPARSE_NO_REALM
,
228 krb5_free_principal(context
, p2
);
230 krb5_err(context
, 1, ret
, "krb5_unparse_name_norealm");
232 if (strcmp(princ_short
, princ_unparsed
))
233 krb5_errx(context
, 1, "'%s' != '%s'", princ_short
, princ_unparsed
);
234 free(princ_unparsed
);
236 krb5_free_principal(context
, p
);
240 princ
= "test\\ principal@SU.SE";
241 noquote
= "test principal@SU.SE";
243 ret
= krb5_parse_name_flags(context
, princ
, 0, &p
);
245 krb5_err(context
, 1, ret
, "krb5_parse_name");
247 ret
= krb5_unparse_name_flags(context
, p
, 0, &princ_unparsed
);
249 krb5_err(context
, 1, ret
, "krb5_unparse_name_flags");
251 if (strcmp(princ
, princ_unparsed
))
252 krb5_errx(context
, 1, "q '%s' != '%s'", princ
, princ_unparsed
);
253 free(princ_unparsed
);
255 ret
= krb5_unparse_name_flags(context
, p
, KRB5_PRINCIPAL_UNPARSE_DISPLAY
,
258 krb5_err(context
, 1, ret
, "krb5_unparse_name_flags");
260 if (strcmp(noquote
, princ_unparsed
))
261 krb5_errx(context
, 1, "nq '%s' != '%s'", noquote
, princ_unparsed
);
262 free(princ_unparsed
);
264 krb5_free_principal(context
, p
);
268 test_enterprise(krb5_context context
)
274 ret
= krb5_set_default_realm(context
, "SAMBA.ORG");
276 krb5_err(context
, 1, ret
, "krb5_parse_name");
278 ret
= krb5_parse_name_flags(context
, "lha@su.se@WIN.SU.SE",
279 KRB5_PRINCIPAL_PARSE_ENTERPRISE
, &p
);
281 krb5_err(context
, 1, ret
, "krb5_parse_name_flags");
283 ret
= krb5_unparse_name(context
, p
, &unparsed
);
285 krb5_err(context
, 1, ret
, "krb5_unparse_name");
287 krb5_free_principal(context
, p
);
289 if (strcmp(unparsed
, "lha\\@su.se@WIN.SU.SE") != 0)
290 krb5_errx(context
, 1, "enterprise name failed 1");
297 ret
= krb5_parse_name_flags(context
, "lha\\@su.se@WIN.SU.SE",
298 KRB5_PRINCIPAL_PARSE_ENTERPRISE
, &p
);
300 krb5_err(context
, 1, ret
, "krb5_parse_name_flags");
302 ret
= krb5_unparse_name(context
, p
, &unparsed
);
304 krb5_err(context
, 1, ret
, "krb5_unparse_name");
306 krb5_free_principal(context
, p
);
307 if (strcmp(unparsed
, "lha\\@su.se\\@WIN.SU.SE@SAMBA.ORG") != 0)
308 krb5_errx(context
, 1, "enterprise name failed 2: %s", unparsed
);
315 ret
= krb5_parse_name_flags(context
, "lha\\@su.se@WIN.SU.SE", 0, &p
);
317 krb5_err(context
, 1, ret
, "krb5_parse_name_flags");
319 ret
= krb5_unparse_name(context
, p
, &unparsed
);
321 krb5_err(context
, 1, ret
, "krb5_unparse_name");
323 krb5_free_principal(context
, p
);
324 if (strcmp(unparsed
, "lha\\@su.se@WIN.SU.SE") != 0)
325 krb5_errx(context
, 1, "enterprise name failed 3");
332 ret
= krb5_parse_name_flags(context
, "lha@su.se",
333 KRB5_PRINCIPAL_PARSE_ENTERPRISE
, &p
);
335 krb5_err(context
, 1, ret
, "krb5_parse_name_flags");
337 ret
= krb5_unparse_name(context
, p
, &unparsed
);
339 krb5_err(context
, 1, ret
, "krb5_unparse_name");
341 krb5_free_principal(context
, p
);
342 if (strcmp(unparsed
, "lha\\@su.se@SAMBA.ORG") != 0)
343 krb5_errx(context
, 1, "enterprise name failed 2: %s", unparsed
);
349 main(int argc
, char **argv
)
351 krb5_context context
;
354 setprogname(argv
[0]);
356 ret
= krb5_init_context(&context
);
358 errx (1, "krb5_init_context failed: %d", ret
);
362 test_enterprise(context
);
364 krb5_free_context(context
);