[HEIMDAL-646] malloc(0) checks for AIX
[heimdal.git] / lib / krb5 / test_cc.c
blob40ff941a5ea32d635a19a55a2ed004e9d833c7a1
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 static int debug_flag = 0;
38 static int version_flag = 0;
39 static int help_flag = 0;
41 static void
42 test_default_name(krb5_context context)
44 krb5_error_code ret;
45 const char *p, *test_cc_name = "/tmp/krb5-cc-test-foo";
46 char *p1, *p2, *p3;
48 p = krb5_cc_default_name(context);
49 if (p == NULL)
50 krb5_errx (context, 1, "krb5_cc_default_name 1 failed");
51 p1 = estrdup(p);
53 ret = krb5_cc_set_default_name(context, NULL);
54 if (p == NULL)
55 krb5_errx (context, 1, "krb5_cc_set_default_name failed");
57 p = krb5_cc_default_name(context);
58 if (p == NULL)
59 krb5_errx (context, 1, "krb5_cc_default_name 2 failed");
60 p2 = estrdup(p);
62 if (strcmp(p1, p2) != 0)
63 krb5_errx (context, 1, "krb5_cc_default_name no longer same");
65 ret = krb5_cc_set_default_name(context, test_cc_name);
66 if (p == NULL)
67 krb5_errx (context, 1, "krb5_cc_set_default_name 1 failed");
69 p = krb5_cc_default_name(context);
70 if (p == NULL)
71 krb5_errx (context, 1, "krb5_cc_default_name 2 failed");
72 p3 = estrdup(p);
74 if (strcmp(p3, test_cc_name) != 0)
75 krb5_errx (context, 1, "krb5_cc_set_default_name 1 failed");
77 free(p1);
78 free(p2);
79 free(p3);
83 * Check that a closed cc still keeps it data and that it's no longer
84 * there when it's destroyed.
87 static void
88 test_mcache(krb5_context context)
90 krb5_error_code ret;
91 krb5_ccache id, id2;
92 const char *nc, *tc;
93 char *c;
94 krb5_principal p, p2;
96 ret = krb5_parse_name(context, "lha@SU.SE", &p);
97 if (ret)
98 krb5_err(context, 1, ret, "krb5_parse_name");
100 ret = krb5_cc_new_unique(context, krb5_cc_type_memory, NULL, &id);
101 if (ret)
102 krb5_err(context, 1, ret, "krb5_cc_new_unique");
104 ret = krb5_cc_initialize(context, id, p);
105 if (ret)
106 krb5_err(context, 1, ret, "krb5_cc_initialize");
108 nc = krb5_cc_get_name(context, id);
109 if (nc == NULL)
110 krb5_errx(context, 1, "krb5_cc_get_name");
112 tc = krb5_cc_get_type(context, id);
113 if (tc == NULL)
114 krb5_errx(context, 1, "krb5_cc_get_name");
116 asprintf(&c, "%s:%s", tc, nc);
118 krb5_cc_close(context, id);
120 ret = krb5_cc_resolve(context, c, &id2);
121 if (ret)
122 krb5_err(context, 1, ret, "krb5_cc_resolve");
124 ret = krb5_cc_get_principal(context, id2, &p2);
125 if (ret)
126 krb5_err(context, 1, ret, "krb5_cc_get_principal");
128 if (krb5_principal_compare(context, p, p2) == FALSE)
129 krb5_errx(context, 1, "p != p2");
131 krb5_cc_destroy(context, id2);
132 krb5_free_principal(context, p);
133 krb5_free_principal(context, p2);
135 ret = krb5_cc_resolve(context, c, &id2);
136 if (ret)
137 krb5_err(context, 1, ret, "krb5_cc_resolve");
139 ret = krb5_cc_get_principal(context, id2, &p2);
140 if (ret == 0)
141 krb5_errx(context, 1, "krb5_cc_get_principal");
143 krb5_cc_destroy(context, id2);
144 free(c);
148 * Test that init works on a destroyed cc.
151 static void
152 test_init_vs_destroy(krb5_context context, const char *type)
154 krb5_error_code ret;
155 krb5_ccache id, id2;
156 krb5_principal p, p2;
157 char *n;
159 ret = krb5_parse_name(context, "lha@SU.SE", &p);
160 if (ret)
161 krb5_err(context, 1, ret, "krb5_parse_name");
163 ret = krb5_cc_new_unique(context, type, NULL, &id);
164 if (ret)
165 krb5_err(context, 1, ret, "krb5_cc_new_unique");
167 asprintf(&n, "%s:%s",
168 krb5_cc_get_type(context, id),
169 krb5_cc_get_name(context, id));
171 ret = krb5_cc_resolve(context, n, &id2);
172 free(n);
173 if (ret)
174 krb5_err(context, 1, ret, "krb5_cc_resolve");
176 krb5_cc_destroy(context, id);
178 ret = krb5_cc_initialize(context, id2, p);
179 if (ret)
180 krb5_err(context, 1, ret, "krb5_cc_initialize");
182 ret = krb5_cc_get_principal(context, id2, &p2);
183 if (ret)
184 krb5_err(context, 1, ret, "krb5_cc_get_principal");
186 krb5_cc_destroy(context, id2);
187 krb5_free_principal(context, p);
188 krb5_free_principal(context, p2);
191 static void
192 test_cache_remove(krb5_context context, const char *type)
194 krb5_error_code ret;
195 krb5_ccache id;
196 krb5_principal p;
197 krb5_creds cred;
199 ret = krb5_parse_name(context, "lha@SU.SE", &p);
200 if (ret)
201 krb5_err(context, 1, ret, "krb5_parse_name");
203 ret = krb5_cc_new_unique(context, type, NULL, &id);
204 if (ret)
205 krb5_err(context, 1, ret, "krb5_cc_gen_new");
207 ret = krb5_cc_initialize(context, id, p);
208 if (ret)
209 krb5_err(context, 1, ret, "krb5_cc_initialize");
211 /* */
212 memset(&cred, 0, sizeof(cred));
213 ret = krb5_parse_name(context, "krbtgt/SU.SE@SU.SE", &cred.server);
214 if (ret)
215 krb5_err(context, 1, ret, "krb5_parse_name");
216 ret = krb5_parse_name(context, "lha@SU.SE", &cred.client);
217 if (ret)
218 krb5_err(context, 1, ret, "krb5_parse_name");
220 ret = krb5_cc_store_cred(context, id, &cred);
221 if (ret)
222 krb5_err(context, 1, ret, "krb5_cc_store_cred");
224 ret = krb5_cc_remove_cred(context, id, 0, &cred);
225 if (ret)
226 krb5_err(context, 1, ret, "krb5_cc_remove_cred");
228 ret = krb5_cc_destroy(context, id);
229 if (ret)
230 krb5_err(context, 1, ret, "krb5_cc_destroy");
232 krb5_free_principal(context, p);
233 krb5_free_principal(context, cred.server);
234 krb5_free_principal(context, cred.client);
237 static void
238 test_mcc_default(void)
240 krb5_context context;
241 krb5_error_code ret;
242 krb5_ccache id, id2;
243 int i;
245 for (i = 0; i < 10; i++) {
247 ret = krb5_init_context(&context);
248 if (ret)
249 krb5_err(context, 1, ret, "krb5_init_context");
251 ret = krb5_cc_set_default_name(context, "MEMORY:foo");
252 if (ret)
253 krb5_err(context, 1, ret, "krb5_cc_set_default_name");
255 ret = krb5_cc_default(context, &id);
256 if (ret)
257 krb5_err(context, 1, ret, "krb5_cc_default");
259 ret = krb5_cc_default(context, &id2);
260 if (ret)
261 krb5_err(context, 1, ret, "krb5_cc_default");
263 ret = krb5_cc_close(context, id);
264 if (ret)
265 krb5_err(context, 1, ret, "krb5_cc_close");
267 ret = krb5_cc_close(context, id2);
268 if (ret)
269 krb5_err(context, 1, ret, "krb5_cc_close");
271 krb5_free_context(context);
275 struct {
276 char *str;
277 int fail;
278 char *res;
279 } cc_names[] = {
280 { "foo", 0, "foo" },
281 { "%{uid}", 0 },
282 { "foo%{null}", 0, "foo" },
283 { "foo%{null}bar", 0, "foobar" },
284 { "%{", 1 },
285 { "%{foo %{", 1 },
286 { "%{{", 1 },
289 static void
290 test_def_cc_name(krb5_context context)
292 krb5_error_code ret;
293 char *str;
294 int i;
296 for (i = 0; i < sizeof(cc_names)/sizeof(cc_names[0]); i++) {
297 ret = _krb5_expand_default_cc_name(context, cc_names[i].str, &str);
298 if (ret) {
299 if (cc_names[i].fail == 0)
300 krb5_errx(context, 1, "test %d \"%s\" failed",
301 i, cc_names[i].str);
302 } else {
303 if (cc_names[i].fail)
304 krb5_errx(context, 1, "test %d \"%s\" was successful",
305 i, cc_names[i].str);
306 if (cc_names[i].res && strcmp(cc_names[i].res, str) != 0)
307 krb5_errx(context, 1, "test %d %s != %s",
308 i, cc_names[i].res, str);
309 if (debug_flag)
310 printf("%s => %s\n", cc_names[i].str, str);
311 free(str);
316 static void
317 test_cache_find(krb5_context context, const char *principal, int find)
319 krb5_principal client;
320 krb5_error_code ret;
321 krb5_ccache id = NULL;
323 ret = krb5_parse_name(context, principal, &client);
324 if (ret)
325 krb5_err(context, 1, ret, "parse_name for %s failed", principal);
327 ret = krb5_cc_cache_match(context, client, &id);
328 if (ret && find)
329 krb5_err(context, 1, ret, "cc_cache_match for %s failed", principal);
330 if (ret == 0 && !find)
331 krb5_err(context, 1, ret, "cc_cache_match for %s found", principal);
333 if (id)
334 krb5_cc_close(context, id);
335 krb5_free_principal(context, client);
339 static void
340 test_cache_iter(krb5_context context, const char *type, int destroy)
342 krb5_cc_cache_cursor cursor;
343 krb5_error_code ret;
344 krb5_ccache id;
346 ret = krb5_cc_cache_get_first (context, type, &cursor);
347 if (ret == KRB5_CC_NOSUPP)
348 return;
349 else if (ret)
350 krb5_err(context, 1, ret, "krb5_cc_cache_get_first(%s)", type);
353 while ((ret = krb5_cc_cache_next (context, cursor, &id)) == 0) {
354 krb5_principal principal;
355 char *name;
357 if (debug_flag)
358 printf("name: %s\n", krb5_cc_get_name(context, id));
359 ret = krb5_cc_get_principal(context, id, &principal);
360 if (ret == 0) {
361 ret = krb5_unparse_name(context, principal, &name);
362 if (ret == 0) {
363 if (debug_flag)
364 printf("\tprincipal: %s\n", name);
365 free(name);
367 krb5_free_principal(context, principal);
369 if (destroy)
370 krb5_cc_destroy(context, id);
371 else
372 krb5_cc_close(context, id);
375 krb5_cc_cache_end_seq_get(context, cursor);
378 static void
379 test_cache_iter_all(krb5_context context)
381 krb5_cccol_cursor cursor;
382 krb5_error_code ret;
383 krb5_ccache id;
385 ret = krb5_cccol_cursor_new (context, &cursor);
386 if (ret)
387 krb5_err(context, 1, ret, "krb5_cccol_cursor_new");
390 while ((ret = krb5_cccol_cursor_next (context, cursor, &id)) == 0 && id != NULL) {
391 krb5_principal principal;
392 char *name;
394 if (debug_flag)
395 printf("name: %s\n", krb5_cc_get_name(context, id));
396 ret = krb5_cc_get_principal(context, id, &principal);
397 if (ret == 0) {
398 ret = krb5_unparse_name(context, principal, &name);
399 if (ret == 0) {
400 if (debug_flag)
401 printf("\tprincipal: %s\n", name);
402 free(name);
404 krb5_free_principal(context, principal);
406 krb5_cc_close(context, id);
409 krb5_cccol_cursor_free(context, &cursor);
413 static void
414 test_copy(krb5_context context, const char *from, const char *to)
416 krb5_ccache fromid, toid;
417 krb5_error_code ret;
418 krb5_principal p, p2;
420 ret = krb5_parse_name(context, "lha@SU.SE", &p);
421 if (ret)
422 krb5_err(context, 1, ret, "krb5_parse_name");
424 ret = krb5_cc_new_unique(context, from, NULL, &fromid);
425 if (ret)
426 krb5_err(context, 1, ret, "krb5_cc_new_unique");
428 ret = krb5_cc_initialize(context, fromid, p);
429 if (ret)
430 krb5_err(context, 1, ret, "krb5_cc_initialize");
432 ret = krb5_cc_new_unique(context, to, NULL, &toid);
433 if (ret)
434 krb5_err(context, 1, ret, "krb5_cc_gen_new");
436 ret = krb5_cc_copy_cache(context, fromid, toid);
437 if (ret)
438 krb5_err(context, 1, ret, "krb5_cc_copy_cache");
440 ret = krb5_cc_get_principal(context, toid, &p2);
441 if (ret)
442 krb5_err(context, 1, ret, "krb5_cc_get_principal");
444 if (krb5_principal_compare(context, p, p2) == FALSE)
445 krb5_errx(context, 1, "p != p2");
447 krb5_free_principal(context, p);
448 krb5_free_principal(context, p2);
450 krb5_cc_destroy(context, fromid);
451 krb5_cc_destroy(context, toid);
454 static void
455 test_move(krb5_context context, const char *type)
457 const krb5_cc_ops *ops;
458 krb5_ccache fromid, toid;
459 krb5_error_code ret;
460 krb5_principal p, p2;
462 ops = krb5_cc_get_prefix_ops(context, type);
463 if (ops == NULL)
464 return;
466 ret = krb5_cc_new_unique(context, type, NULL, &fromid);
467 if (ret == KRB5_CC_NOSUPP)
468 return;
469 else if (ret)
470 krb5_err(context, 1, ret, "krb5_cc_new_unique");
472 ret = krb5_parse_name(context, "lha@SU.SE", &p);
473 if (ret)
474 krb5_err(context, 1, ret, "krb5_parse_name");
476 ret = krb5_cc_initialize(context, fromid, p);
477 if (ret)
478 krb5_err(context, 1, ret, "krb5_cc_initialize");
480 ret = krb5_cc_new_unique(context, type, NULL, &toid);
481 if (ret)
482 krb5_err(context, 1, ret, "krb5_cc_new_unique");
484 ret = krb5_cc_initialize(context, toid, p);
485 if (ret)
486 krb5_err(context, 1, ret, "krb5_cc_initialize");
488 ret = krb5_cc_get_principal(context, toid, &p2);
489 if (ret)
490 krb5_err(context, 1, ret, "krb5_cc_get_principal");
492 if (krb5_principal_compare(context, p, p2) == FALSE)
493 krb5_errx(context, 1, "p != p2");
495 krb5_free_principal(context, p);
496 krb5_free_principal(context, p2);
498 krb5_cc_destroy(context, toid);
499 krb5_cc_destroy(context, fromid);
503 static void
504 test_prefix_ops(krb5_context context, const char *name, const krb5_cc_ops *ops)
506 const krb5_cc_ops *o;
508 o = krb5_cc_get_prefix_ops(context, name);
509 if (o == NULL)
510 krb5_errx(context, 1, "found no match for prefix '%s'", name);
511 if (strcmp(o->prefix, ops->prefix) != 0)
512 krb5_errx(context, 1, "ops for prefix '%s' is not "
513 "the expected %s != %s", name, o->prefix, ops->prefix);
516 static void
517 test_cc_config(krb5_context context)
519 krb5_error_code ret;
520 krb5_principal p;
521 krb5_ccache id;
522 unsigned int i;
524 ret = krb5_cc_new_unique(context, "MEMORY", "bar", &id);
525 if (ret)
526 krb5_err(context, 1, ret, "krb5_cc_new_unique");
528 ret = krb5_parse_name(context, "lha@SU.SE", &p);
529 if (ret)
530 krb5_err(context, 1, ret, "krb5_parse_name");
532 ret = krb5_cc_initialize(context, id, p);
533 if (ret)
534 krb5_err(context, 1, ret, "krb5_cc_initialize");
536 for (i = 0; i < 1000; i++) {
537 krb5_data data, data2;
538 const char *name = "foo";
539 krb5_principal p1 = NULL;
541 if (i & 1)
542 p1 = p;
544 data.data = rk_UNCONST(name);
545 data.length = strlen(name);
547 ret = krb5_cc_set_config(context, id, p1, "FriendlyName", &data);
548 if (ret)
549 krb5_errx(context, 1, "krb5_cc_set_config: add");
551 ret = krb5_cc_get_config(context, id, p1, "FriendlyName", &data2);
552 if (ret)
553 krb5_errx(context, 1, "krb5_cc_get_config: first");
554 krb5_data_free(&data2);
556 ret = krb5_cc_set_config(context, id, p1, "FriendlyName", &data);
557 if (ret)
558 krb5_errx(context, 1, "krb5_cc_set_config: add -second");
560 ret = krb5_cc_get_config(context, id, p1, "FriendlyName", &data2);
561 if (ret)
562 krb5_errx(context, 1, "krb5_cc_get_config: second");
563 krb5_data_free(&data2);
565 ret = krb5_cc_set_config(context, id, p1, "FriendlyName", NULL);
566 if (ret)
567 krb5_errx(context, 1, "krb5_cc_set_config: delete");
569 ret = krb5_cc_get_config(context, id, p1, "FriendlyName", &data2);
570 if (ret == 0)
571 krb5_errx(context, 1, "krb5_cc_get_config: non-existant");
574 krb5_cc_destroy(context, id);
575 krb5_free_principal(context, p);
579 static struct getargs args[] = {
580 {"debug", 'd', arg_flag, &debug_flag,
581 "turn on debuggin", NULL },
582 {"version", 0, arg_flag, &version_flag,
583 "print version", NULL },
584 {"help", 0, arg_flag, &help_flag,
585 NULL, NULL }
588 static void
589 usage (int ret)
591 arg_printusage (args, sizeof(args)/sizeof(*args), NULL, "hostname ...");
592 exit (ret);
596 main(int argc, char **argv)
598 krb5_context context;
599 krb5_error_code ret;
600 int optidx = 0;
601 krb5_ccache id1, id2;
603 setprogname(argv[0]);
605 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
606 usage(1);
608 if (help_flag)
609 usage (0);
611 if(version_flag){
612 print_version(NULL);
613 exit(0);
616 argc -= optidx;
617 argv += optidx;
619 ret = krb5_init_context(&context);
620 if (ret)
621 errx (1, "krb5_init_context failed: %d", ret);
623 test_cache_remove(context, krb5_cc_type_file);
624 test_cache_remove(context, krb5_cc_type_memory);
625 #ifdef USE_SQLITE
626 test_cache_remove(context, krb5_cc_type_scc);
627 #endif
629 test_default_name(context);
630 test_mcache(context);
631 test_init_vs_destroy(context, krb5_cc_type_memory);
632 test_init_vs_destroy(context, krb5_cc_type_file);
633 #if 0
634 test_init_vs_destroy(context, krb5_cc_type_api);
635 #endif
636 test_init_vs_destroy(context, krb5_cc_type_scc);
637 test_mcc_default();
638 test_def_cc_name(context);
640 test_cache_iter_all(context);
642 test_cache_iter(context, krb5_cc_type_memory, 0);
644 krb5_principal p;
645 krb5_cc_new_unique(context, krb5_cc_type_memory, "bar", &id1);
646 krb5_cc_new_unique(context, krb5_cc_type_memory, "baz", &id2);
647 krb5_parse_name(context, "lha@SU.SE", &p);
648 krb5_cc_initialize(context, id1, p);
649 krb5_free_principal(context, p);
652 test_cache_find(context, "lha@SU.SE", 1);
653 test_cache_find(context, "hulabundulahotentot@SU.SE", 0);
655 test_cache_iter(context, krb5_cc_type_memory, 0);
656 test_cache_iter(context, krb5_cc_type_memory, 1);
657 test_cache_iter(context, krb5_cc_type_memory, 0);
658 test_cache_iter(context, krb5_cc_type_file, 0);
659 test_cache_iter(context, krb5_cc_type_api, 0);
660 test_cache_iter(context, krb5_cc_type_scc, 0);
661 test_cache_iter(context, krb5_cc_type_scc, 1);
663 test_copy(context, krb5_cc_type_file, krb5_cc_type_file);
664 test_copy(context, krb5_cc_type_memory, krb5_cc_type_memory);
665 test_copy(context, krb5_cc_type_file, krb5_cc_type_memory);
666 test_copy(context, krb5_cc_type_memory, krb5_cc_type_file);
667 test_copy(context, krb5_cc_type_scc, krb5_cc_type_file);
668 test_copy(context, krb5_cc_type_file, krb5_cc_type_scc);
669 test_copy(context, krb5_cc_type_scc, krb5_cc_type_memory);
670 test_copy(context, krb5_cc_type_memory, krb5_cc_type_scc);
672 test_move(context, krb5_cc_type_file);
673 test_move(context, krb5_cc_type_memory);
674 test_move(context, krb5_cc_type_kcm);
675 test_move(context, krb5_cc_type_scc);
677 test_prefix_ops(context, "FILE:/tmp/foo", &krb5_fcc_ops);
678 test_prefix_ops(context, "FILE", &krb5_fcc_ops);
679 test_prefix_ops(context, "MEMORY", &krb5_mcc_ops);
680 test_prefix_ops(context, "MEMORY:foo", &krb5_mcc_ops);
681 test_prefix_ops(context, "/tmp/kaka", &krb5_fcc_ops);
682 test_prefix_ops(context, "SCC:", &krb5_scc_ops);
683 test_prefix_ops(context, "SCC:foo", &krb5_scc_ops);
685 krb5_cc_destroy(context, id1);
686 krb5_cc_destroy(context, id2);
688 test_cc_config(context);
690 krb5_free_context(context);
692 sleep(60);
694 return 0;