etc/services - sync with NetBSD-8
[minix.git] / crypto / external / bsd / heimdal / dist / lib / krb5 / test_princ.c
blob580ea8c71248742e94e13c7e304cd217864d1a84
1 /* $NetBSD: test_princ.c,v 1.1.1.1 2011/04/13 18:15:38 elric Exp $ */
3 /*
4 * Copyright (c) 2003 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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"
36 #include <err.h>
39 * Check that a closed cc still keeps it data and that it's no longer
40 * there when it's destroyed.
43 static void
44 test_princ(krb5_context context)
46 const char *princ = "lha@SU.SE";
47 const char *princ_short = "lha";
48 const char *noquote;
49 krb5_error_code ret;
50 char *princ_unparsed;
51 char *princ_reformed = NULL;
52 const char *realm;
54 krb5_principal p, p2;
56 ret = krb5_parse_name(context, princ, &p);
57 if (ret)
58 krb5_err(context, 1, ret, "krb5_parse_name");
60 ret = krb5_unparse_name(context, p, &princ_unparsed);
61 if (ret)
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);
68 free(princ_unparsed);
70 ret = krb5_unparse_name_flags(context, p,
71 KRB5_PRINCIPAL_UNPARSE_NO_REALM,
72 &princ_unparsed);
73 if (ret)
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);
78 free(princ_unparsed);
80 realm = krb5_principal_get_realm(context, p);
82 if (asprintf(&princ_reformed, "%s@%s", princ_short, realm) < 0 || princ_reformed == NULL)
83 errx(1, "malloc");
85 ret = krb5_parse_name(context, princ_reformed, &p2);
86 free(princ_reformed);
87 if (ret)
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");
97 if (ret)
98 krb5_err(context, 1, ret, "krb5_parse_name");
100 ret = krb5_unparse_name_flags(context, p,
101 KRB5_PRINCIPAL_UNPARSE_SHORT,
102 &princ_unparsed);
103 if (ret)
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);
111 if (ret)
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);
119 if (ret)
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");
127 if (ret)
128 krb5_err(context, 1, ret, "krb5_parse_name");
130 ret = krb5_parse_name(context, princ_short, &p2);
131 if (ret)
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);
141 if (ret)
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);
151 if (ret)
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);
158 if (ret)
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,
169 &princ_unparsed);
170 if (ret)
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);
178 if (ret)
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,
187 &p2);
188 if (!ret)
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,
194 &p2);
195 if (ret)
196 krb5_err(context, 1, ret, "krb5_parse_name");
198 ret = krb5_unparse_name_flags(context, p2,
199 KRB5_PRINCIPAL_UNPARSE_NO_REALM,
200 &princ_unparsed);
201 krb5_free_principal(context, p2);
202 if (ret)
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,
211 &p2);
212 if (!ret)
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,
218 &p2);
219 if (ret)
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,
227 &princ_unparsed);
228 krb5_free_principal(context, p2);
229 if (ret)
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);
238 /* test quoting */
240 princ = "test\\ principal@SU.SE";
241 noquote = "test principal@SU.SE";
243 ret = krb5_parse_name_flags(context, princ, 0, &p);
244 if (ret)
245 krb5_err(context, 1, ret, "krb5_parse_name");
247 ret = krb5_unparse_name_flags(context, p, 0, &princ_unparsed);
248 if (ret)
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,
256 &princ_unparsed);
257 if (ret)
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);
267 static void
268 test_enterprise(krb5_context context)
270 krb5_error_code ret;
271 char *unparsed;
272 krb5_principal p;
274 ret = krb5_set_default_realm(context, "SAMBA.ORG");
275 if (ret)
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);
280 if (ret)
281 krb5_err(context, 1, ret, "krb5_parse_name_flags");
283 ret = krb5_unparse_name(context, p, &unparsed);
284 if (ret)
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");
291 free(unparsed);
297 ret = krb5_parse_name_flags(context, "lha\\@su.se@WIN.SU.SE",
298 KRB5_PRINCIPAL_PARSE_ENTERPRISE, &p);
299 if (ret)
300 krb5_err(context, 1, ret, "krb5_parse_name_flags");
302 ret = krb5_unparse_name(context, p, &unparsed);
303 if (ret)
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);
309 free(unparsed);
315 ret = krb5_parse_name_flags(context, "lha\\@su.se@WIN.SU.SE", 0, &p);
316 if (ret)
317 krb5_err(context, 1, ret, "krb5_parse_name_flags");
319 ret = krb5_unparse_name(context, p, &unparsed);
320 if (ret)
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");
326 free(unparsed);
332 ret = krb5_parse_name_flags(context, "lha@su.se",
333 KRB5_PRINCIPAL_PARSE_ENTERPRISE, &p);
334 if (ret)
335 krb5_err(context, 1, ret, "krb5_parse_name_flags");
337 ret = krb5_unparse_name(context, p, &unparsed);
338 if (ret)
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);
344 free(unparsed);
349 main(int argc, char **argv)
351 krb5_context context;
352 krb5_error_code ret;
354 setprogname(argv[0]);
356 ret = krb5_init_context(&context);
357 if (ret)
358 errx (1, "krb5_init_context failed: %d", ret);
360 test_princ(context);
362 test_enterprise(context);
364 krb5_free_context(context);
366 return 0;