2 * Copyright (c) 2003 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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"
37 static int debug_flag
= 0;
38 static int version_flag
= 0;
39 static int help_flag
= 0;
42 test_default_name(krb5_context context
)
45 const char *p
, *test_cc_name
= "/tmp/krb5-cc-test-foo";
48 p
= krb5_cc_default_name(context
);
50 krb5_errx (context
, 1, "krb5_cc_default_name 1 failed");
53 ret
= krb5_cc_set_default_name(context
, NULL
);
55 krb5_errx (context
, 1, "krb5_cc_set_default_name failed");
57 p
= krb5_cc_default_name(context
);
59 krb5_errx (context
, 1, "krb5_cc_default_name 2 failed");
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
);
67 krb5_errx (context
, 1, "krb5_cc_set_default_name 1 failed");
69 p
= krb5_cc_default_name(context
);
71 krb5_errx (context
, 1, "krb5_cc_default_name 2 failed");
74 if (strcmp(p3
, test_cc_name
) != 0)
75 krb5_errx (context
, 1, "krb5_cc_set_default_name 1 failed");
83 * Check that a closed cc still keeps it data and that it's no longer
84 * there when it's destroyed.
88 test_mcache(krb5_context context
)
96 ret
= krb5_parse_name(context
, "lha@SU.SE", &p
);
98 krb5_err(context
, 1, ret
, "krb5_parse_name");
100 ret
= krb5_cc_new_unique(context
, krb5_cc_type_memory
, NULL
, &id
);
102 krb5_err(context
, 1, ret
, "krb5_cc_new_unique");
104 ret
= krb5_cc_initialize(context
, id
, p
);
106 krb5_err(context
, 1, ret
, "krb5_cc_initialize");
108 nc
= krb5_cc_get_name(context
, id
);
110 krb5_errx(context
, 1, "krb5_cc_get_name");
112 tc
= krb5_cc_get_type(context
, id
);
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
);
122 krb5_err(context
, 1, ret
, "krb5_cc_resolve");
124 ret
= krb5_cc_get_principal(context
, id2
, &p2
);
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
);
137 krb5_err(context
, 1, ret
, "krb5_cc_resolve");
139 ret
= krb5_cc_get_principal(context
, id2
, &p2
);
141 krb5_errx(context
, 1, "krb5_cc_get_principal");
143 krb5_cc_destroy(context
, id2
);
148 * Test that init works on a destroyed cc.
152 test_init_vs_destroy(krb5_context context
, const char *type
)
156 krb5_principal p
, p2
;
159 ret
= krb5_parse_name(context
, "lha@SU.SE", &p
);
161 krb5_err(context
, 1, ret
, "krb5_parse_name");
163 ret
= krb5_cc_new_unique(context
, type
, NULL
, &id
);
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
);
174 krb5_err(context
, 1, ret
, "krb5_cc_resolve");
176 krb5_cc_destroy(context
, id
);
178 ret
= krb5_cc_initialize(context
, id2
, p
);
180 krb5_err(context
, 1, ret
, "krb5_cc_initialize");
182 ret
= krb5_cc_get_principal(context
, id2
, &p2
);
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
);
192 test_cache_remove(krb5_context context
, const char *type
)
199 ret
= krb5_parse_name(context
, "lha@SU.SE", &p
);
201 krb5_err(context
, 1, ret
, "krb5_parse_name");
203 ret
= krb5_cc_new_unique(context
, type
, NULL
, &id
);
205 krb5_err(context
, 1, ret
, "krb5_cc_gen_new");
207 ret
= krb5_cc_initialize(context
, id
, p
);
209 krb5_err(context
, 1, ret
, "krb5_cc_initialize");
212 memset(&cred
, 0, sizeof(cred
));
213 ret
= krb5_parse_name(context
, "krbtgt/SU.SE@SU.SE", &cred
.server
);
215 krb5_err(context
, 1, ret
, "krb5_parse_name");
216 ret
= krb5_parse_name(context
, "lha@SU.SE", &cred
.client
);
218 krb5_err(context
, 1, ret
, "krb5_parse_name");
220 ret
= krb5_cc_store_cred(context
, id
, &cred
);
222 krb5_err(context
, 1, ret
, "krb5_cc_store_cred");
224 ret
= krb5_cc_remove_cred(context
, id
, 0, &cred
);
226 krb5_err(context
, 1, ret
, "krb5_cc_remove_cred");
228 ret
= krb5_cc_destroy(context
, id
);
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
);
238 test_mcc_default(void)
240 krb5_context context
;
245 for (i
= 0; i
< 10; i
++) {
247 ret
= krb5_init_context(&context
);
249 krb5_err(context
, 1, ret
, "krb5_init_context");
251 ret
= krb5_cc_set_default_name(context
, "MEMORY:foo");
253 krb5_err(context
, 1, ret
, "krb5_cc_set_default_name");
255 ret
= krb5_cc_default(context
, &id
);
257 krb5_err(context
, 1, ret
, "krb5_cc_default");
259 ret
= krb5_cc_default(context
, &id2
);
261 krb5_err(context
, 1, ret
, "krb5_cc_default");
263 ret
= krb5_cc_close(context
, id
);
265 krb5_err(context
, 1, ret
, "krb5_cc_close");
267 ret
= krb5_cc_close(context
, id2
);
269 krb5_err(context
, 1, ret
, "krb5_cc_close");
271 krb5_free_context(context
);
282 { "foo%{null}", 0, "foo" },
283 { "foo%{null}bar", 0, "foobar" },
290 test_def_cc_name(krb5_context context
)
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
);
299 if (cc_names
[i
].fail
== 0)
300 krb5_errx(context
, 1, "test %d \"%s\" failed",
303 if (cc_names
[i
].fail
)
304 krb5_errx(context
, 1, "test %d \"%s\" was successful",
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
);
310 printf("%s => %s\n", cc_names
[i
].str
, str
);
317 test_cache_find(krb5_context context
, const char *principal
, int find
)
319 krb5_principal client
;
321 krb5_ccache id
= NULL
;
323 ret
= krb5_parse_name(context
, principal
, &client
);
325 krb5_err(context
, 1, ret
, "parse_name for %s failed", principal
);
327 ret
= krb5_cc_cache_match(context
, client
, &id
);
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
);
334 krb5_cc_close(context
, id
);
335 krb5_free_principal(context
, client
);
340 test_cache_iter(krb5_context context
, const char *type
, int destroy
)
342 krb5_cc_cache_cursor cursor
;
346 ret
= krb5_cc_cache_get_first (context
, type
, &cursor
);
347 if (ret
== KRB5_CC_NOSUPP
)
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
;
358 printf("name: %s\n", krb5_cc_get_name(context
, id
));
359 ret
= krb5_cc_get_principal(context
, id
, &principal
);
361 ret
= krb5_unparse_name(context
, principal
, &name
);
364 printf("\tprincipal: %s\n", name
);
367 krb5_free_principal(context
, principal
);
370 krb5_cc_destroy(context
, id
);
372 krb5_cc_close(context
, id
);
375 krb5_cc_cache_end_seq_get(context
, cursor
);
379 test_cache_iter_all(krb5_context context
)
381 krb5_cccol_cursor cursor
;
385 ret
= krb5_cccol_cursor_new (context
, &cursor
);
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
;
395 printf("name: %s\n", krb5_cc_get_name(context
, id
));
396 ret
= krb5_cc_get_principal(context
, id
, &principal
);
398 ret
= krb5_unparse_name(context
, principal
, &name
);
401 printf("\tprincipal: %s\n", name
);
404 krb5_free_principal(context
, principal
);
406 krb5_cc_close(context
, id
);
409 krb5_cccol_cursor_free(context
, &cursor
);
414 test_copy(krb5_context context
, const char *from
, const char *to
)
416 krb5_ccache fromid
, toid
;
418 krb5_principal p
, p2
;
420 ret
= krb5_parse_name(context
, "lha@SU.SE", &p
);
422 krb5_err(context
, 1, ret
, "krb5_parse_name");
424 ret
= krb5_cc_new_unique(context
, from
, NULL
, &fromid
);
426 krb5_err(context
, 1, ret
, "krb5_cc_new_unique");
428 ret
= krb5_cc_initialize(context
, fromid
, p
);
430 krb5_err(context
, 1, ret
, "krb5_cc_initialize");
432 ret
= krb5_cc_new_unique(context
, to
, NULL
, &toid
);
434 krb5_err(context
, 1, ret
, "krb5_cc_gen_new");
436 ret
= krb5_cc_copy_cache(context
, fromid
, toid
);
438 krb5_err(context
, 1, ret
, "krb5_cc_copy_cache");
440 ret
= krb5_cc_get_principal(context
, toid
, &p2
);
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
);
455 test_move(krb5_context context
, const char *type
)
457 const krb5_cc_ops
*ops
;
458 krb5_ccache fromid
, toid
;
460 krb5_principal p
, p2
;
462 ops
= krb5_cc_get_prefix_ops(context
, type
);
466 ret
= krb5_cc_new_unique(context
, type
, NULL
, &fromid
);
467 if (ret
== KRB5_CC_NOSUPP
)
470 krb5_err(context
, 1, ret
, "krb5_cc_new_unique");
472 ret
= krb5_parse_name(context
, "lha@SU.SE", &p
);
474 krb5_err(context
, 1, ret
, "krb5_parse_name");
476 ret
= krb5_cc_initialize(context
, fromid
, p
);
478 krb5_err(context
, 1, ret
, "krb5_cc_initialize");
480 ret
= krb5_cc_new_unique(context
, type
, NULL
, &toid
);
482 krb5_err(context
, 1, ret
, "krb5_cc_new_unique");
484 ret
= krb5_cc_initialize(context
, toid
, p
);
486 krb5_err(context
, 1, ret
, "krb5_cc_initialize");
488 ret
= krb5_cc_get_principal(context
, toid
, &p2
);
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
);
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
);
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
);
517 test_cc_config(krb5_context context
)
524 ret
= krb5_cc_new_unique(context
, "MEMORY", "bar", &id
);
526 krb5_err(context
, 1, ret
, "krb5_cc_new_unique");
528 ret
= krb5_parse_name(context
, "lha@SU.SE", &p
);
530 krb5_err(context
, 1, ret
, "krb5_parse_name");
532 ret
= krb5_cc_initialize(context
, id
, p
);
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
;
544 data
.data
= rk_UNCONST(name
);
545 data
.length
= strlen(name
);
547 ret
= krb5_cc_set_config(context
, id
, p1
, "FriendlyName", &data
);
549 krb5_errx(context
, 1, "krb5_cc_set_config: add");
551 ret
= krb5_cc_get_config(context
, id
, p1
, "FriendlyName", &data2
);
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
);
558 krb5_errx(context
, 1, "krb5_cc_set_config: add -second");
560 ret
= krb5_cc_get_config(context
, id
, p1
, "FriendlyName", &data2
);
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
);
567 krb5_errx(context
, 1, "krb5_cc_set_config: delete");
569 ret
= krb5_cc_get_config(context
, id
, p1
, "FriendlyName", &data2
);
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
,
591 arg_printusage (args
, sizeof(args
)/sizeof(*args
), NULL
, "hostname ...");
596 main(int argc
, char **argv
)
598 krb5_context context
;
601 krb5_ccache id1
, id2
;
603 setprogname(argv
[0]);
605 if(getarg(args
, sizeof(args
) / sizeof(args
[0]), argc
, argv
, &optidx
))
619 ret
= krb5_init_context(&context
);
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
);
626 test_cache_remove(context
, krb5_cc_type_scc
);
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
);
634 test_init_vs_destroy(context
, krb5_cc_type_api
);
636 test_init_vs_destroy(context
, krb5_cc_type_scc
);
638 test_def_cc_name(context
);
640 test_cache_iter_all(context
);
642 test_cache_iter(context
, krb5_cc_type_memory
, 0);
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
);