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 __RCSID("$Heimdal: test_cc.c 22115 2007-12-03 21:21:42Z lha $"
40 static int debug_flag
= 0;
41 static int version_flag
= 0;
42 static int help_flag
= 0;
45 test_default_name(krb5_context context
)
48 const char *p
, *test_cc_name
= "/tmp/krb5-cc-test-foo";
51 p
= krb5_cc_default_name(context
);
53 krb5_errx (context
, 1, "krb5_cc_default_name 1 failed");
56 ret
= krb5_cc_set_default_name(context
, NULL
);
58 krb5_errx (context
, 1, "krb5_cc_set_default_name failed");
60 p
= krb5_cc_default_name(context
);
62 krb5_errx (context
, 1, "krb5_cc_default_name 2 failed");
65 if (strcmp(p1
, p2
) != 0)
66 krb5_errx (context
, 1, "krb5_cc_default_name no longer same");
68 ret
= krb5_cc_set_default_name(context
, test_cc_name
);
70 krb5_errx (context
, 1, "krb5_cc_set_default_name 1 failed");
72 p
= krb5_cc_default_name(context
);
74 krb5_errx (context
, 1, "krb5_cc_default_name 2 failed");
77 if (strcmp(p3
, test_cc_name
) != 0)
78 krb5_errx (context
, 1, "krb5_cc_set_default_name 1 failed");
86 * Check that a closed cc still keeps it data and that it's no longer
87 * there when it's destroyed.
91 test_mcache(krb5_context context
)
99 ret
= krb5_parse_name(context
, "lha@SU.SE", &p
);
101 krb5_err(context
, 1, ret
, "krb5_parse_name");
103 ret
= krb5_cc_gen_new(context
, &krb5_mcc_ops
, &id
);
105 krb5_err(context
, 1, ret
, "krb5_cc_gen_new");
107 ret
= krb5_cc_initialize(context
, id
, p
);
109 krb5_err(context
, 1, ret
, "krb5_cc_initialize");
111 nc
= krb5_cc_get_name(context
, id
);
113 krb5_errx(context
, 1, "krb5_cc_get_name");
115 tc
= krb5_cc_get_type(context
, id
);
117 krb5_errx(context
, 1, "krb5_cc_get_name");
119 asprintf(&c
, "%s:%s", tc
, nc
);
121 krb5_cc_close(context
, id
);
123 ret
= krb5_cc_resolve(context
, c
, &id2
);
125 krb5_err(context
, 1, ret
, "krb5_cc_resolve");
127 ret
= krb5_cc_get_principal(context
, id2
, &p2
);
129 krb5_err(context
, 1, ret
, "krb5_cc_get_principal");
131 if (krb5_principal_compare(context
, p
, p2
) == FALSE
)
132 krb5_errx(context
, 1, "p != p2");
134 krb5_cc_destroy(context
, id2
);
135 krb5_free_principal(context
, p
);
136 krb5_free_principal(context
, p2
);
138 ret
= krb5_cc_resolve(context
, c
, &id2
);
140 krb5_err(context
, 1, ret
, "krb5_cc_resolve");
142 ret
= krb5_cc_get_principal(context
, id2
, &p2
);
144 krb5_errx(context
, 1, "krb5_cc_get_principal");
146 krb5_cc_destroy(context
, id2
);
151 * Test that init works on a destroyed cc.
155 test_init_vs_destroy(krb5_context context
, const krb5_cc_ops
*ops
)
159 krb5_principal p
, p2
;
162 ret
= krb5_parse_name(context
, "lha@SU.SE", &p
);
164 krb5_err(context
, 1, ret
, "krb5_parse_name");
166 ret
= krb5_cc_gen_new(context
, ops
, &id
);
168 krb5_err(context
, 1, ret
, "krb5_cc_gen_new");
170 asprintf(&n
, "%s:%s",
171 krb5_cc_get_type(context
, id
),
172 krb5_cc_get_name(context
, id
));
174 ret
= krb5_cc_resolve(context
, n
, &id2
);
177 krb5_err(context
, 1, ret
, "krb5_cc_resolve");
179 krb5_cc_destroy(context
, id
);
181 ret
= krb5_cc_initialize(context
, id2
, p
);
183 krb5_err(context
, 1, ret
, "krb5_cc_initialize");
185 ret
= krb5_cc_get_principal(context
, id2
, &p2
);
187 krb5_err(context
, 1, ret
, "krb5_cc_get_principal");
189 krb5_cc_destroy(context
, id2
);
190 krb5_free_principal(context
, p
);
191 krb5_free_principal(context
, p2
);
195 test_fcache_remove(krb5_context context
)
202 ret
= krb5_parse_name(context
, "lha@SU.SE", &p
);
204 krb5_err(context
, 1, ret
, "krb5_parse_name");
206 ret
= krb5_cc_gen_new(context
, &krb5_fcc_ops
, &id
);
208 krb5_err(context
, 1, ret
, "krb5_cc_gen_new");
210 ret
= krb5_cc_initialize(context
, id
, p
);
212 krb5_err(context
, 1, ret
, "krb5_cc_initialize");
215 memset(&cred
, 0, sizeof(cred
));
216 ret
= krb5_parse_name(context
, "krbtgt/SU.SE@SU.SE", &cred
.server
);
218 krb5_err(context
, 1, ret
, "krb5_parse_name");
219 ret
= krb5_parse_name(context
, "lha@SU.SE", &cred
.client
);
221 krb5_err(context
, 1, ret
, "krb5_parse_name");
223 ret
= krb5_cc_store_cred(context
, id
, &cred
);
225 krb5_err(context
, 1, ret
, "krb5_cc_store_cred");
227 ret
= krb5_cc_remove_cred(context
, id
, 0, &cred
);
229 krb5_err(context
, 1, ret
, "krb5_cc_remove_cred");
231 ret
= krb5_cc_destroy(context
, id
);
233 krb5_err(context
, 1, ret
, "krb5_cc_destroy");
235 krb5_free_principal(context
, p
);
236 krb5_free_principal(context
, cred
.server
);
237 krb5_free_principal(context
, cred
.client
);
241 test_mcc_default(void)
243 krb5_context context
;
248 for (i
= 0; i
< 10; i
++) {
250 ret
= krb5_init_context(&context
);
252 krb5_err(context
, 1, ret
, "krb5_init_context");
254 ret
= krb5_cc_set_default_name(context
, "MEMORY:foo");
256 krb5_err(context
, 1, ret
, "krb5_cc_set_default_name");
258 ret
= krb5_cc_default(context
, &id
);
260 krb5_err(context
, 1, ret
, "krb5_cc_default");
262 ret
= krb5_cc_default(context
, &id2
);
264 krb5_err(context
, 1, ret
, "krb5_cc_default");
266 ret
= krb5_cc_close(context
, id
);
268 krb5_err(context
, 1, ret
, "krb5_cc_close");
270 ret
= krb5_cc_close(context
, id2
);
272 krb5_err(context
, 1, ret
, "krb5_cc_close");
274 krb5_free_context(context
);
285 { "foo%{null}", 0, "foo" },
286 { "foo%{null}bar", 0, "foobar" },
293 test_def_cc_name(krb5_context context
)
299 for (i
= 0; i
< sizeof(cc_names
)/sizeof(cc_names
[0]); i
++) {
300 ret
= _krb5_expand_default_cc_name(context
, cc_names
[i
].str
, &str
);
302 if (cc_names
[i
].fail
== 0)
303 krb5_errx(context
, 1, "test %d \"%s\" failed",
306 if (cc_names
[i
].fail
)
307 krb5_errx(context
, 1, "test %d \"%s\" was successful",
309 if (cc_names
[i
].res
&& strcmp(cc_names
[i
].res
, str
) != 0)
310 krb5_errx(context
, 1, "test %d %s != %s",
311 i
, cc_names
[i
].res
, str
);
313 printf("%s => %s\n", cc_names
[i
].str
, str
);
320 test_cache_find(krb5_context context
, const char *type
, const char *principal
,
323 krb5_principal client
;
325 krb5_ccache id
= NULL
;
327 ret
= krb5_parse_name(context
, principal
, &client
);
329 krb5_err(context
, 1, ret
, "parse_name for %s failed", principal
);
331 ret
= krb5_cc_cache_match(context
, client
, type
, &id
);
333 krb5_err(context
, 1, ret
, "cc_cache_match for %s failed", principal
);
334 if (ret
== 0 && !find
)
335 krb5_err(context
, 1, ret
, "cc_cache_match for %s found", principal
);
338 krb5_cc_close(context
, id
);
339 krb5_free_principal(context
, client
);
344 test_cache_iter(krb5_context context
, const char *type
, int destroy
)
346 krb5_cc_cache_cursor cursor
;
350 ret
= krb5_cc_cache_get_first (context
, type
, &cursor
);
351 if (ret
== KRB5_CC_NOSUPP
)
354 krb5_err(context
, 1, ret
, "krb5_cc_cache_get_first(%s)", type
);
357 while ((ret
= krb5_cc_cache_next (context
, cursor
, &id
)) == 0) {
358 krb5_principal principal
;
362 printf("name: %s\n", krb5_cc_get_name(context
, id
));
363 ret
= krb5_cc_get_principal(context
, id
, &principal
);
365 ret
= krb5_unparse_name(context
, principal
, &name
);
368 printf("\tprincipal: %s\n", name
);
371 krb5_free_principal(context
, principal
);
374 krb5_cc_destroy(context
, id
);
376 krb5_cc_close(context
, id
);
379 krb5_cc_cache_end_seq_get(context
, cursor
);
383 test_copy(krb5_context context
, const char *fromtype
, const char *totype
)
385 const krb5_cc_ops
*from
, *to
;
386 krb5_ccache fromid
, toid
;
388 krb5_principal p
, p2
;
390 from
= krb5_cc_get_prefix_ops(context
, fromtype
);
392 krb5_errx(context
, 1, "%s isn't a type", fromtype
);
394 to
= krb5_cc_get_prefix_ops(context
, totype
);
396 krb5_errx(context
, 1, "%s isn't a type", totype
);
398 ret
= krb5_parse_name(context
, "lha@SU.SE", &p
);
400 krb5_err(context
, 1, ret
, "krb5_parse_name");
402 ret
= krb5_cc_gen_new(context
, from
, &fromid
);
404 krb5_err(context
, 1, ret
, "krb5_cc_gen_new");
406 ret
= krb5_cc_initialize(context
, fromid
, p
);
408 krb5_err(context
, 1, ret
, "krb5_cc_initialize");
410 ret
= krb5_cc_gen_new(context
, to
, &toid
);
412 krb5_err(context
, 1, ret
, "krb5_cc_gen_new");
414 ret
= krb5_cc_copy_cache(context
, fromid
, toid
);
416 krb5_err(context
, 1, ret
, "krb5_cc_copy_cache");
418 ret
= krb5_cc_get_principal(context
, toid
, &p2
);
420 krb5_err(context
, 1, ret
, "krb5_cc_get_principal");
422 if (krb5_principal_compare(context
, p
, p2
) == FALSE
)
423 krb5_errx(context
, 1, "p != p2");
425 krb5_free_principal(context
, p
);
426 krb5_free_principal(context
, p2
);
428 krb5_cc_destroy(context
, fromid
);
429 krb5_cc_destroy(context
, toid
);
433 test_prefix_ops(krb5_context context
, const char *name
, const krb5_cc_ops
*ops
)
435 const krb5_cc_ops
*o
;
437 o
= krb5_cc_get_prefix_ops(context
, name
);
439 krb5_errx(context
, 1, "found no match for prefix '%s'", name
);
440 if (strcmp(o
->prefix
, ops
->prefix
) != 0)
441 krb5_errx(context
, 1, "ops for prefix '%s' is not "
442 "the expected %s != %s", name
, o
->prefix
, ops
->prefix
);
446 static struct getargs args
[] = {
447 {"debug", 'd', arg_flag
, &debug_flag
,
448 "turn on debuggin", NULL
},
449 {"version", 0, arg_flag
, &version_flag
,
450 "print version", NULL
},
451 {"help", 0, arg_flag
, &help_flag
,
458 arg_printusage (args
, sizeof(args
)/sizeof(*args
), NULL
, "hostname ...");
463 main(int argc
, char **argv
)
465 krb5_context context
;
468 krb5_ccache id1
, id2
;
470 setprogname(argv
[0]);
472 if(getarg(args
, sizeof(args
) / sizeof(args
[0]), argc
, argv
, &optidx
))
486 ret
= krb5_init_context(&context
);
488 errx (1, "krb5_init_context failed: %d", ret
);
490 test_fcache_remove(context
);
491 test_default_name(context
);
492 test_mcache(context
);
493 test_init_vs_destroy(context
, &krb5_mcc_ops
);
494 test_init_vs_destroy(context
, &krb5_fcc_ops
);
496 test_def_cc_name(context
);
497 test_cache_iter(context
, "MEMORY", 0);
500 krb5_cc_new_unique(context
, "MEMORY", "bar", &id1
);
501 krb5_cc_new_unique(context
, "MEMORY", "baz", &id2
);
502 krb5_parse_name(context
, "lha@SU.SE", &p
);
503 krb5_cc_initialize(context
, id1
, p
);
504 krb5_free_principal(context
, p
);
507 test_cache_find(context
, "MEMORY", "lha@SU.SE", 1);
508 test_cache_find(context
, "MEMORY", "hulabundulahotentot@SU.SE", 0);
510 test_cache_iter(context
, "MEMORY", 0);
511 test_cache_iter(context
, "MEMORY", 1);
512 test_cache_iter(context
, "MEMORY", 0);
513 test_cache_iter(context
, "FILE", 0);
514 test_cache_iter(context
, "API", 0);
516 test_copy(context
, "FILE", "FILE");
517 test_copy(context
, "MEMORY", "MEMORY");
518 test_copy(context
, "FILE", "MEMORY");
519 test_copy(context
, "MEMORY", "FILE");
521 test_prefix_ops(context
, "FILE:/tmp/foo", &krb5_fcc_ops
);
522 test_prefix_ops(context
, "FILE", &krb5_fcc_ops
);
523 test_prefix_ops(context
, "MEMORY", &krb5_mcc_ops
);
524 test_prefix_ops(context
, "MEMORY:foo", &krb5_mcc_ops
);
525 test_prefix_ops(context
, "/tmp/kaka", &krb5_fcc_ops
);
527 krb5_cc_destroy(context
, id1
);
528 krb5_cc_destroy(context
, id2
);
530 krb5_free_context(context
);