use DES_set_key_unchecked().
[heimdal.git] / lib / krb5 / test_cc.c
blob9c0617878744b8395e4dec22d6ad0d42041c8750
1 /*
2 * Copyright (c) 2003 - 2007 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 <getarg.h>
35 #include <err.h>
37 RCSID("$Id$");
39 static int debug_flag = 0;
40 static int version_flag = 0;
41 static int help_flag = 0;
43 static void
44 test_default_name(krb5_context context)
46 krb5_error_code ret;
47 const char *p, *test_cc_name = "/tmp/krb5-cc-test-foo";
48 char *p1, *p2, *p3;
50 p = krb5_cc_default_name(context);
51 if (p == NULL)
52 krb5_errx (context, 1, "krb5_cc_default_name 1 failed");
53 p1 = estrdup(p);
55 ret = krb5_cc_set_default_name(context, NULL);
56 if (p == NULL)
57 krb5_errx (context, 1, "krb5_cc_set_default_name failed");
59 p = krb5_cc_default_name(context);
60 if (p == NULL)
61 krb5_errx (context, 1, "krb5_cc_default_name 2 failed");
62 p2 = estrdup(p);
64 if (strcmp(p1, p2) != 0)
65 krb5_errx (context, 1, "krb5_cc_default_name no longer same");
67 ret = krb5_cc_set_default_name(context, test_cc_name);
68 if (p == NULL)
69 krb5_errx (context, 1, "krb5_cc_set_default_name 1 failed");
71 p = krb5_cc_default_name(context);
72 if (p == NULL)
73 krb5_errx (context, 1, "krb5_cc_default_name 2 failed");
74 p3 = estrdup(p);
76 if (strcmp(p3, test_cc_name) != 0)
77 krb5_errx (context, 1, "krb5_cc_set_default_name 1 failed");
79 free(p1);
80 free(p2);
81 free(p3);
85 * Check that a closed cc still keeps it data and that it's no longer
86 * there when it's destroyed.
89 static void
90 test_mcache(krb5_context context)
92 krb5_error_code ret;
93 krb5_ccache id, id2;
94 const char *nc, *tc;
95 char *c;
96 krb5_principal p, p2;
98 ret = krb5_parse_name(context, "lha@SU.SE", &p);
99 if (ret)
100 krb5_err(context, 1, ret, "krb5_parse_name");
102 ret = krb5_cc_gen_new(context, &krb5_mcc_ops, &id);
103 if (ret)
104 krb5_err(context, 1, ret, "krb5_cc_gen_new");
106 ret = krb5_cc_initialize(context, id, p);
107 if (ret)
108 krb5_err(context, 1, ret, "krb5_cc_initialize");
110 nc = krb5_cc_get_name(context, id);
111 if (nc == NULL)
112 krb5_errx(context, 1, "krb5_cc_get_name");
114 tc = krb5_cc_get_type(context, id);
115 if (tc == NULL)
116 krb5_errx(context, 1, "krb5_cc_get_name");
118 asprintf(&c, "%s:%s", tc, nc);
120 krb5_cc_close(context, id);
122 ret = krb5_cc_resolve(context, c, &id2);
123 if (ret)
124 krb5_err(context, 1, ret, "krb5_cc_resolve");
126 ret = krb5_cc_get_principal(context, id2, &p2);
127 if (ret)
128 krb5_err(context, 1, ret, "krb5_cc_get_principal");
130 if (krb5_principal_compare(context, p, p2) == FALSE)
131 krb5_errx(context, 1, "p != p2");
133 krb5_cc_destroy(context, id2);
134 krb5_free_principal(context, p);
135 krb5_free_principal(context, p2);
137 ret = krb5_cc_resolve(context, c, &id2);
138 if (ret)
139 krb5_err(context, 1, ret, "krb5_cc_resolve");
141 ret = krb5_cc_get_principal(context, id2, &p2);
142 if (ret == 0)
143 krb5_errx(context, 1, "krb5_cc_get_principal");
145 krb5_cc_destroy(context, id2);
146 free(c);
150 * Test that init works on a destroyed cc.
153 static void
154 test_init_vs_destroy(krb5_context context, const krb5_cc_ops *ops)
156 krb5_error_code ret;
157 krb5_ccache id, id2;
158 krb5_principal p, p2;
159 char *n;
161 ret = krb5_parse_name(context, "lha@SU.SE", &p);
162 if (ret)
163 krb5_err(context, 1, ret, "krb5_parse_name");
165 ret = krb5_cc_gen_new(context, ops, &id);
166 if (ret)
167 krb5_err(context, 1, ret, "krb5_cc_gen_new");
169 asprintf(&n, "%s:%s",
170 krb5_cc_get_type(context, id),
171 krb5_cc_get_name(context, id));
173 ret = krb5_cc_resolve(context, n, &id2);
174 free(n);
175 if (ret)
176 krb5_err(context, 1, ret, "krb5_cc_resolve");
178 krb5_cc_destroy(context, id);
180 ret = krb5_cc_initialize(context, id2, p);
181 if (ret)
182 krb5_err(context, 1, ret, "krb5_cc_initialize");
184 ret = krb5_cc_get_principal(context, id2, &p2);
185 if (ret)
186 krb5_err(context, 1, ret, "krb5_cc_get_principal");
188 krb5_cc_destroy(context, id2);
189 krb5_free_principal(context, p);
190 krb5_free_principal(context, p2);
193 static void
194 test_cache_remove(krb5_context context, const krb5_cc_ops *ops)
196 krb5_error_code ret;
197 krb5_ccache id;
198 krb5_principal p;
199 krb5_creds cred;
201 ret = krb5_parse_name(context, "lha@SU.SE", &p);
202 if (ret)
203 krb5_err(context, 1, ret, "krb5_parse_name");
205 ret = krb5_cc_gen_new(context, ops, &id);
206 if (ret)
207 krb5_err(context, 1, ret, "krb5_cc_gen_new");
209 ret = krb5_cc_initialize(context, id, p);
210 if (ret)
211 krb5_err(context, 1, ret, "krb5_cc_initialize");
213 /* */
214 memset(&cred, 0, sizeof(cred));
215 ret = krb5_parse_name(context, "krbtgt/SU.SE@SU.SE", &cred.server);
216 if (ret)
217 krb5_err(context, 1, ret, "krb5_parse_name");
218 ret = krb5_parse_name(context, "lha@SU.SE", &cred.client);
219 if (ret)
220 krb5_err(context, 1, ret, "krb5_parse_name");
222 ret = krb5_cc_store_cred(context, id, &cred);
223 if (ret)
224 krb5_err(context, 1, ret, "krb5_cc_store_cred");
226 ret = krb5_cc_remove_cred(context, id, 0, &cred);
227 if (ret)
228 krb5_err(context, 1, ret, "krb5_cc_remove_cred");
230 ret = krb5_cc_destroy(context, id);
231 if (ret)
232 krb5_err(context, 1, ret, "krb5_cc_destroy");
234 krb5_free_principal(context, p);
235 krb5_free_principal(context, cred.server);
236 krb5_free_principal(context, cred.client);
239 static void
240 test_mcc_default(void)
242 krb5_context context;
243 krb5_error_code ret;
244 krb5_ccache id, id2;
245 int i;
247 for (i = 0; i < 10; i++) {
249 ret = krb5_init_context(&context);
250 if (ret)
251 krb5_err(context, 1, ret, "krb5_init_context");
253 ret = krb5_cc_set_default_name(context, "MEMORY:foo");
254 if (ret)
255 krb5_err(context, 1, ret, "krb5_cc_set_default_name");
257 ret = krb5_cc_default(context, &id);
258 if (ret)
259 krb5_err(context, 1, ret, "krb5_cc_default");
261 ret = krb5_cc_default(context, &id2);
262 if (ret)
263 krb5_err(context, 1, ret, "krb5_cc_default");
265 ret = krb5_cc_close(context, id);
266 if (ret)
267 krb5_err(context, 1, ret, "krb5_cc_close");
269 ret = krb5_cc_close(context, id2);
270 if (ret)
271 krb5_err(context, 1, ret, "krb5_cc_close");
273 krb5_free_context(context);
277 struct {
278 char *str;
279 int fail;
280 char *res;
281 } cc_names[] = {
282 { "foo", 0, "foo" },
283 { "%{uid}", 0 },
284 { "foo%{null}", 0, "foo" },
285 { "foo%{null}bar", 0, "foobar" },
286 { "%{", 1 },
287 { "%{foo %{", 1 },
288 { "%{{", 1 },
291 static void
292 test_def_cc_name(krb5_context context)
294 krb5_error_code ret;
295 char *str;
296 int i;
298 for (i = 0; i < sizeof(cc_names)/sizeof(cc_names[0]); i++) {
299 ret = _krb5_expand_default_cc_name(context, cc_names[i].str, &str);
300 if (ret) {
301 if (cc_names[i].fail == 0)
302 krb5_errx(context, 1, "test %d \"%s\" failed",
303 i, cc_names[i].str);
304 } else {
305 if (cc_names[i].fail)
306 krb5_errx(context, 1, "test %d \"%s\" was successful",
307 i, cc_names[i].str);
308 if (cc_names[i].res && strcmp(cc_names[i].res, str) != 0)
309 krb5_errx(context, 1, "test %d %s != %s",
310 i, cc_names[i].res, str);
311 if (debug_flag)
312 printf("%s => %s\n", cc_names[i].str, str);
313 free(str);
318 static void
319 test_cache_find(krb5_context context, const char *type, const char *principal,
320 int find)
322 krb5_principal client;
323 krb5_error_code ret;
324 krb5_ccache id = NULL;
326 ret = krb5_parse_name(context, principal, &client);
327 if (ret)
328 krb5_err(context, 1, ret, "parse_name for %s failed", principal);
330 ret = krb5_cc_cache_match(context, client, type, &id);
331 if (ret && find)
332 krb5_err(context, 1, ret, "cc_cache_match for %s failed", principal);
333 if (ret == 0 && !find)
334 krb5_err(context, 1, ret, "cc_cache_match for %s found", principal);
336 if (id)
337 krb5_cc_close(context, id);
338 krb5_free_principal(context, client);
342 static void
343 test_cache_iter(krb5_context context, const char *type, int destroy)
345 krb5_cc_cache_cursor cursor;
346 krb5_error_code ret;
347 krb5_ccache id;
349 ret = krb5_cc_cache_get_first (context, type, &cursor);
350 if (ret == KRB5_CC_NOSUPP)
351 return;
352 else if (ret)
353 krb5_err(context, 1, ret, "krb5_cc_cache_get_first(%s)", type);
356 while ((ret = krb5_cc_cache_next (context, cursor, &id)) == 0) {
357 krb5_principal principal;
358 char *name;
360 if (debug_flag)
361 printf("name: %s\n", krb5_cc_get_name(context, id));
362 ret = krb5_cc_get_principal(context, id, &principal);
363 if (ret == 0) {
364 ret = krb5_unparse_name(context, principal, &name);
365 if (ret == 0) {
366 if (debug_flag)
367 printf("\tprincipal: %s\n", name);
368 free(name);
370 krb5_free_principal(context, principal);
372 if (destroy)
373 krb5_cc_destroy(context, id);
374 else
375 krb5_cc_close(context, id);
378 krb5_cc_cache_end_seq_get(context, cursor);
381 static void
382 test_copy(krb5_context context, const char *fromtype, const char *totype)
384 const krb5_cc_ops *from, *to;
385 krb5_ccache fromid, toid;
386 krb5_error_code ret;
387 krb5_principal p, p2;
389 from = krb5_cc_get_prefix_ops(context, fromtype);
390 if (from == NULL)
391 krb5_errx(context, 1, "%s isn't a type", fromtype);
393 to = krb5_cc_get_prefix_ops(context, totype);
394 if (to == NULL)
395 krb5_errx(context, 1, "%s isn't a type", totype);
397 ret = krb5_parse_name(context, "lha@SU.SE", &p);
398 if (ret)
399 krb5_err(context, 1, ret, "krb5_parse_name");
401 ret = krb5_cc_gen_new(context, from, &fromid);
402 if (ret)
403 krb5_err(context, 1, ret, "krb5_cc_gen_new");
405 ret = krb5_cc_initialize(context, fromid, p);
406 if (ret)
407 krb5_err(context, 1, ret, "krb5_cc_initialize");
409 ret = krb5_cc_gen_new(context, to, &toid);
410 if (ret)
411 krb5_err(context, 1, ret, "krb5_cc_gen_new");
413 ret = krb5_cc_copy_cache(context, fromid, toid);
414 if (ret)
415 krb5_err(context, 1, ret, "krb5_cc_copy_cache");
417 ret = krb5_cc_get_principal(context, toid, &p2);
418 if (ret)
419 krb5_err(context, 1, ret, "krb5_cc_get_principal");
421 if (krb5_principal_compare(context, p, p2) == FALSE)
422 krb5_errx(context, 1, "p != p2");
424 krb5_free_principal(context, p);
425 krb5_free_principal(context, p2);
427 krb5_cc_destroy(context, fromid);
428 krb5_cc_destroy(context, toid);
431 static void
432 test_move(krb5_context context, const char *type)
434 const krb5_cc_ops *ops;
435 krb5_ccache fromid, toid;
436 krb5_error_code ret;
437 krb5_principal p, p2;
439 ops = krb5_cc_get_prefix_ops(context, type);
440 if (ops == NULL)
441 krb5_errx(context, 1, "%s isn't a type", type);
443 ret = krb5_cc_gen_new(context, ops, &fromid);
444 if (ret == KRB5_CC_NOSUPP)
445 return;
446 else if (ret)
447 krb5_err(context, 1, ret, "krb5_cc_gen_new");
449 ret = krb5_parse_name(context, "lha@SU.SE", &p);
450 if (ret)
451 krb5_err(context, 1, ret, "krb5_parse_name");
453 ret = krb5_cc_initialize(context, fromid, p);
454 if (ret)
455 krb5_err(context, 1, ret, "krb5_cc_initialize");
457 ret = krb5_cc_gen_new(context, ops, &toid);
458 if (ret)
459 krb5_err(context, 1, ret, "krb5_cc_gen_new");
461 ret = krb5_cc_initialize(context, toid, p);
462 if (ret)
463 krb5_err(context, 1, ret, "krb5_cc_initialize");
465 ret = krb5_cc_get_principal(context, toid, &p2);
466 if (ret)
467 krb5_err(context, 1, ret, "krb5_cc_get_principal");
469 if (krb5_principal_compare(context, p, p2) == FALSE)
470 krb5_errx(context, 1, "p != p2");
472 krb5_free_principal(context, p);
473 krb5_free_principal(context, p2);
475 krb5_cc_destroy(context, toid);
479 static void
480 test_prefix_ops(krb5_context context, const char *name, const krb5_cc_ops *ops)
482 const krb5_cc_ops *o;
484 o = krb5_cc_get_prefix_ops(context, name);
485 if (o == NULL)
486 krb5_errx(context, 1, "found no match for prefix '%s'", name);
487 if (strcmp(o->prefix, ops->prefix) != 0)
488 krb5_errx(context, 1, "ops for prefix '%s' is not "
489 "the expected %s != %s", name, o->prefix, ops->prefix);
493 static struct getargs args[] = {
494 {"debug", 'd', arg_flag, &debug_flag,
495 "turn on debuggin", NULL },
496 {"version", 0, arg_flag, &version_flag,
497 "print version", NULL },
498 {"help", 0, arg_flag, &help_flag,
499 NULL, NULL }
502 static void
503 usage (int ret)
505 arg_printusage (args, sizeof(args)/sizeof(*args), NULL, "hostname ...");
506 exit (ret);
510 main(int argc, char **argv)
512 krb5_context context;
513 krb5_error_code ret;
514 int optidx = 0;
515 krb5_ccache id1, id2;
517 setprogname(argv[0]);
519 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
520 usage(1);
522 if (help_flag)
523 usage (0);
525 if(version_flag){
526 print_version(NULL);
527 exit(0);
530 argc -= optidx;
531 argv += optidx;
533 ret = krb5_init_context(&context);
534 if (ret)
535 errx (1, "krb5_init_context failed: %d", ret);
537 test_cache_remove(context, &krb5_fcc_ops);
538 test_cache_remove(context, &krb5_mcc_ops);
539 test_cache_remove(context, &krb5_scc_ops);
541 test_default_name(context);
542 test_mcache(context);
543 test_init_vs_destroy(context, &krb5_mcc_ops);
544 test_init_vs_destroy(context, &krb5_fcc_ops);
545 #if 0
546 test_init_vs_destroy(context, &krb5_acc_ops);
547 #endif
548 test_init_vs_destroy(context, &krb5_scc_ops);
549 test_mcc_default();
550 test_def_cc_name(context);
551 test_cache_iter(context, "MEMORY", 0);
553 krb5_principal p;
554 krb5_cc_new_unique(context, "MEMORY", "bar", &id1);
555 krb5_cc_new_unique(context, "MEMORY", "baz", &id2);
556 krb5_parse_name(context, "lha@SU.SE", &p);
557 krb5_cc_initialize(context, id1, p);
558 krb5_free_principal(context, p);
561 test_cache_find(context, "MEMORY", "lha@SU.SE", 1);
562 test_cache_find(context, "MEMORY", "hulabundulahotentot@SU.SE", 0);
564 test_cache_iter(context, "MEMORY", 0);
565 test_cache_iter(context, "MEMORY", 1);
566 test_cache_iter(context, "MEMORY", 0);
567 test_cache_iter(context, "FILE", 0);
568 test_cache_iter(context, "API", 0);
569 test_cache_iter(context, "SDB", 0);
570 test_cache_iter(context, "SDB", 1);
572 test_copy(context, "FILE", "FILE");
573 test_copy(context, "MEMORY", "MEMORY");
574 test_copy(context, "FILE", "MEMORY");
575 test_copy(context, "MEMORY", "FILE");
576 test_copy(context, "SDB", "FILE");
577 test_copy(context, "FILE", "SDB");
578 test_copy(context, "SDB", "MEMORY");
579 test_copy(context, "MEMORY", "SDB");
581 test_move(context, "FILE");
582 test_move(context, "MEMORY");
583 test_move(context, "KCM");
584 test_move(context, "SDB");
586 test_prefix_ops(context, "FILE:/tmp/foo", &krb5_fcc_ops);
587 test_prefix_ops(context, "FILE", &krb5_fcc_ops);
588 test_prefix_ops(context, "MEMORY", &krb5_mcc_ops);
589 test_prefix_ops(context, "MEMORY:foo", &krb5_mcc_ops);
590 test_prefix_ops(context, "/tmp/kaka", &krb5_fcc_ops);
591 test_prefix_ops(context, "SDB:", &krb5_scc_ops);
592 test_prefix_ops(context, "SDB:foo", &krb5_scc_ops);
594 krb5_cc_destroy(context, id1);
595 krb5_cc_destroy(context, id2);
597 krb5_free_context(context);
599 return 0;