hdb: read_master_key use free_master_key on error
[heimdal.git] / lib / krb5 / test_princ.c
blob98e61e3d8bf81ddf53a58f7c014671541df19de4
1 /*
2 * Copyright (c) 2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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"
34 #include <err.h>
37 * Check that a closed cc still keeps it data and that it's no longer
38 * there when it's destroyed.
41 static void
42 test_princ(krb5_context context)
44 const char *princ = "lha@SU.SE";
45 const char *princ_short = "lha";
46 const char *noquote;
47 krb5_error_code ret;
48 char *princ_unparsed;
49 char *princ_reformed = NULL;
50 const char *realm;
52 krb5_principal p, p2;
54 ret = krb5_parse_name(context, princ, &p);
55 if (ret)
56 krb5_err(context, 1, ret, "krb5_parse_name");
58 ret = krb5_unparse_name(context, p, &princ_unparsed);
59 if (ret)
60 krb5_err(context, 1, ret, "krb5_parse_name");
62 if (strcmp(princ, princ_unparsed)) {
63 krb5_errx(context, 1, "%s != %s", princ, princ_unparsed);
66 free(princ_unparsed);
68 ret = krb5_unparse_name_flags(context, p,
69 KRB5_PRINCIPAL_UNPARSE_NO_REALM,
70 &princ_unparsed);
71 if (ret)
72 krb5_err(context, 1, ret, "krb5_parse_name");
74 if (strcmp(princ_short, princ_unparsed))
75 krb5_errx(context, 1, "%s != %s", princ_short, princ_unparsed);
76 free(princ_unparsed);
78 realm = krb5_principal_get_realm(context, p);
80 if (asprintf(&princ_reformed, "%s@%s", princ_short, realm) < 0 || princ_reformed == NULL)
81 errx(1, "malloc");
83 ret = krb5_parse_name(context, princ_reformed, &p2);
84 free(princ_reformed);
85 if (ret)
86 krb5_err(context, 1, ret, "krb5_parse_name");
88 if (!krb5_principal_compare(context, p, p2)) {
89 krb5_errx(context, 1, "p != p2");
92 krb5_free_principal(context, p2);
94 ret = krb5_set_default_realm(context, "SU.SE");
95 if (ret)
96 krb5_err(context, 1, ret, "krb5_parse_name");
98 ret = krb5_unparse_name_flags(context, p,
99 KRB5_PRINCIPAL_UNPARSE_SHORT,
100 &princ_unparsed);
101 if (ret)
102 krb5_err(context, 1, ret, "krb5_parse_name");
104 if (strcmp(princ_short, princ_unparsed))
105 krb5_errx(context, 1, "'%s' != '%s'", princ_short, princ_unparsed);
106 free(princ_unparsed);
108 ret = krb5_parse_name(context, princ_short, &p2);
109 if (ret)
110 krb5_err(context, 1, ret, "krb5_parse_name");
112 if (!krb5_principal_compare(context, p, p2))
113 krb5_errx(context, 1, "p != p2");
114 krb5_free_principal(context, p2);
116 ret = krb5_unparse_name(context, p, &princ_unparsed);
117 if (ret)
118 krb5_err(context, 1, ret, "krb5_parse_name");
120 if (strcmp(princ, princ_unparsed))
121 krb5_errx(context, 1, "'%s' != '%s'", princ, princ_unparsed);
122 free(princ_unparsed);
124 ret = krb5_set_default_realm(context, "SAMBA.ORG");
125 if (ret)
126 krb5_err(context, 1, ret, "krb5_parse_name");
128 ret = krb5_parse_name(context, princ_short, &p2);
129 if (ret)
130 krb5_err(context, 1, ret, "krb5_parse_name");
132 if (krb5_principal_compare(context, p, p2))
133 krb5_errx(context, 1, "p == p2");
135 if (!krb5_principal_compare_any_realm(context, p, p2))
136 krb5_errx(context, 1, "(ignoring realms) p != p2");
138 ret = krb5_unparse_name(context, p2, &princ_unparsed);
139 if (ret)
140 krb5_err(context, 1, ret, "krb5_parse_name");
142 if (strcmp(princ, princ_unparsed) == 0)
143 krb5_errx(context, 1, "%s == %s", princ, princ_unparsed);
144 free(princ_unparsed);
146 krb5_free_principal(context, p2);
148 ret = krb5_parse_name(context, princ, &p2);
149 if (ret)
150 krb5_err(context, 1, ret, "krb5_parse_name");
152 if (!krb5_principal_compare(context, p, p2))
153 krb5_errx(context, 1, "p != p2");
155 ret = krb5_unparse_name(context, p2, &princ_unparsed);
156 if (ret)
157 krb5_err(context, 1, ret, "krb5_parse_name");
159 if (strcmp(princ, princ_unparsed))
160 krb5_errx(context, 1, "'%s' != '%s'", princ, princ_unparsed);
161 free(princ_unparsed);
163 krb5_free_principal(context, p2);
165 ret = krb5_unparse_name_flags(context, p,
166 KRB5_PRINCIPAL_UNPARSE_SHORT,
167 &princ_unparsed);
168 if (ret)
169 krb5_err(context, 1, ret, "krb5_unparse_name_short");
171 if (strcmp(princ, princ_unparsed) != 0)
172 krb5_errx(context, 1, "'%s' != '%s'", princ, princ_unparsed);
173 free(princ_unparsed);
175 ret = krb5_unparse_name(context, p, &princ_unparsed);
176 if (ret)
177 krb5_err(context, 1, ret, "krb5_unparse_name_short");
179 if (strcmp(princ, princ_unparsed))
180 krb5_errx(context, 1, "'%s' != '%s'", princ, princ_unparsed);
181 free(princ_unparsed);
183 ret = krb5_parse_name_flags(context, princ,
184 KRB5_PRINCIPAL_PARSE_NO_REALM,
185 &p2);
186 if (!ret)
187 krb5_err(context, 1, ret, "Should have failed to parse %s a "
188 "short name", princ);
190 ret = krb5_parse_name_flags(context, princ_short,
191 KRB5_PRINCIPAL_PARSE_NO_REALM,
192 &p2);
193 if (ret)
194 krb5_err(context, 1, ret, "krb5_parse_name");
196 ret = krb5_unparse_name_flags(context, p2,
197 KRB5_PRINCIPAL_UNPARSE_NO_REALM,
198 &princ_unparsed);
199 krb5_free_principal(context, p2);
200 if (ret)
201 krb5_err(context, 1, ret, "krb5_unparse_name_norealm");
203 if (strcmp(princ_short, princ_unparsed))
204 krb5_errx(context, 1, "'%s' != '%s'", princ_short, princ_unparsed);
205 free(princ_unparsed);
207 ret = krb5_parse_name_flags(context, princ_short,
208 KRB5_PRINCIPAL_PARSE_REQUIRE_REALM,
209 &p2);
210 if (!ret)
211 krb5_err(context, 1, ret, "Should have failed to parse %s "
212 "because it lacked a realm", princ_short);
214 ret = krb5_parse_name_flags(context, princ,
215 KRB5_PRINCIPAL_PARSE_REQUIRE_REALM,
216 &p2);
217 if (ret)
218 krb5_err(context, 1, ret, "krb5_parse_name");
220 if (!krb5_principal_compare(context, p, p2))
221 krb5_errx(context, 1, "p != p2");
223 ret = krb5_unparse_name_flags(context, p2,
224 KRB5_PRINCIPAL_UNPARSE_NO_REALM,
225 &princ_unparsed);
226 krb5_free_principal(context, p2);
227 if (ret)
228 krb5_err(context, 1, ret, "krb5_unparse_name_norealm");
230 if (strcmp(princ_short, princ_unparsed))
231 krb5_errx(context, 1, "'%s' != '%s'", princ_short, princ_unparsed);
232 free(princ_unparsed);
234 krb5_free_principal(context, p);
236 /* test quoting */
238 princ = "test\\ principal@SU.SE";
239 noquote = "test principal@SU.SE";
241 ret = krb5_parse_name_flags(context, princ, 0, &p);
242 if (ret)
243 krb5_err(context, 1, ret, "krb5_parse_name");
245 ret = krb5_unparse_name_flags(context, p, 0, &princ_unparsed);
246 if (ret)
247 krb5_err(context, 1, ret, "krb5_unparse_name_flags");
249 if (strcmp(princ, princ_unparsed))
250 krb5_errx(context, 1, "q '%s' != '%s'", princ, princ_unparsed);
251 free(princ_unparsed);
253 ret = krb5_unparse_name_flags(context, p, KRB5_PRINCIPAL_UNPARSE_DISPLAY,
254 &princ_unparsed);
255 if (ret)
256 krb5_err(context, 1, ret, "krb5_unparse_name_flags");
258 if (strcmp(noquote, princ_unparsed))
259 krb5_errx(context, 1, "nq '%s' != '%s'", noquote, princ_unparsed);
260 free(princ_unparsed);
262 krb5_free_principal(context, p);
265 static void
266 test_enterprise(krb5_context context)
268 krb5_error_code ret;
269 char *unparsed;
270 krb5_principal p;
272 ret = krb5_set_default_realm(context, "SAMBA.ORG");
273 if (ret)
274 krb5_err(context, 1, ret, "krb5_parse_name");
276 ret = krb5_parse_name_flags(context, "lha@su.se@WIN.SU.SE",
277 KRB5_PRINCIPAL_PARSE_ENTERPRISE, &p);
278 if (ret)
279 krb5_err(context, 1, ret, "krb5_parse_name_flags");
281 ret = krb5_unparse_name(context, p, &unparsed);
282 if (ret)
283 krb5_err(context, 1, ret, "krb5_unparse_name");
285 krb5_free_principal(context, p);
287 if (strcmp(unparsed, "lha\\@su.se@WIN.SU.SE") != 0)
288 krb5_errx(context, 1, "enterprise name failed 1");
289 free(unparsed);
295 ret = krb5_parse_name_flags(context, "lha\\@su.se@WIN.SU.SE",
296 KRB5_PRINCIPAL_PARSE_ENTERPRISE, &p);
297 if (ret)
298 krb5_err(context, 1, ret, "krb5_parse_name_flags");
300 ret = krb5_unparse_name(context, p, &unparsed);
301 if (ret)
302 krb5_err(context, 1, ret, "krb5_unparse_name");
304 krb5_free_principal(context, p);
305 if (strcmp(unparsed, "lha\\@su.se\\@WIN.SU.SE@SAMBA.ORG") != 0)
306 krb5_errx(context, 1, "enterprise name failed 2: %s", unparsed);
307 free(unparsed);
313 ret = krb5_parse_name_flags(context, "lha\\@su.se@WIN.SU.SE", 0, &p);
314 if (ret)
315 krb5_err(context, 1, ret, "krb5_parse_name_flags");
317 ret = krb5_unparse_name(context, p, &unparsed);
318 if (ret)
319 krb5_err(context, 1, ret, "krb5_unparse_name");
321 krb5_free_principal(context, p);
322 if (strcmp(unparsed, "lha\\@su.se@WIN.SU.SE") != 0)
323 krb5_errx(context, 1, "enterprise name failed 3");
324 free(unparsed);
330 ret = krb5_parse_name_flags(context, "lha@su.se",
331 KRB5_PRINCIPAL_PARSE_ENTERPRISE, &p);
332 if (ret)
333 krb5_err(context, 1, ret, "krb5_parse_name_flags");
335 ret = krb5_unparse_name(context, p, &unparsed);
336 if (ret)
337 krb5_err(context, 1, ret, "krb5_unparse_name");
339 krb5_free_principal(context, p);
340 if (strcmp(unparsed, "lha\\@su.se@SAMBA.ORG") != 0)
341 krb5_errx(context, 1, "enterprise name failed 2: %s", unparsed);
342 free(unparsed);
347 main(int argc, char **argv)
349 krb5_context context;
350 krb5_error_code ret;
352 setprogname(argv[0]);
354 ret = krb5_init_context(&context);
355 if (ret)
356 errx (1, "krb5_init_context failed: %d", ret);
358 test_princ(context);
360 test_enterprise(context);
362 krb5_free_context(context);
364 return 0;