xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/krb5/test_cc.c (revision d3273b5b76f5afaafe308cead5511dbb8df8c5e9)
1 /*	$NetBSD: test_cc.c,v 1.2 2017/01/28 21:31:49 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 2003 - 2007 Kungliga Tekniska Högskolan
5  * (Royal Institute of Technology, Stockholm, Sweden).
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of KTH nor the names of its contributors may be
20  *    used to endorse or promote products derived from this software without
21  *    specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
24  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
34 
35 #include "krb5_locl.h"
36 #include <krb5/getarg.h>
37 #include <err.h>
38 
39 static int debug_flag	= 0;
40 static int version_flag = 0;
41 static int help_flag	= 0;
42 
43 #ifdef KRB5_USE_PATH_TOKENS
44 #define TEST_CC_NAME "%{TEMP}/krb5-cc-test-foo"
45 #else
46 #define TEST_CC_NAME "/tmp/krb5-cc-test-foo"
47 #endif
48 
49 static void
test_default_name(krb5_context context)50 test_default_name(krb5_context context)
51 {
52     krb5_error_code ret;
53     const char *p, *test_cc_name = TEST_CC_NAME;
54     char *p1, *p2, *p3;
55 
56     p = krb5_cc_default_name(context);
57     if (p == NULL)
58 	krb5_errx (context, 1, "krb5_cc_default_name 1 failed");
59     p1 = estrdup(p);
60 
61     ret = krb5_cc_set_default_name(context, NULL);
62     if (ret)
63 	krb5_errx (context, 1, "krb5_cc_set_default_name failed");
64 
65     p = krb5_cc_default_name(context);
66     if (p == NULL)
67 	krb5_errx (context, 1, "krb5_cc_default_name 2 failed");
68     p2 = estrdup(p);
69 
70     if (strcmp(p1, p2) != 0)
71 	krb5_errx (context, 1, "krb5_cc_default_name no longer same");
72 
73     ret = krb5_cc_set_default_name(context, test_cc_name);
74     if (ret)
75 	krb5_errx (context, 1, "krb5_cc_set_default_name 1 failed");
76 
77     p = krb5_cc_default_name(context);
78     if (p == NULL)
79 	krb5_errx (context, 1, "krb5_cc_default_name 2 failed");
80     p3 = estrdup(p);
81 
82 #ifndef KRB5_USE_PATH_TOKENS
83     /* If we are using path tokens, we don't expect the p3 and
84        test_cc_name to match since p3 is going to have expanded
85        tokens. */
86     if (strcmp(p3, test_cc_name) != 0)
87 	krb5_errx (context, 1, "krb5_cc_set_default_name 1 failed");
88 #endif
89 
90     free(p1);
91     free(p2);
92     free(p3);
93 }
94 
95 /*
96  * Check that a closed cc still keeps it data and that it's no longer
97  * there when it's destroyed.
98  */
99 
100 static void
test_mcache(krb5_context context)101 test_mcache(krb5_context context)
102 {
103     krb5_error_code ret;
104     krb5_ccache id, id2;
105     const char *nc, *tc;
106     char *c;
107     krb5_principal p, p2;
108 
109     ret = krb5_parse_name(context, "lha@SU.SE", &p);
110     if (ret)
111 	krb5_err(context, 1, ret, "krb5_parse_name");
112 
113     ret = krb5_cc_new_unique(context, krb5_cc_type_memory, NULL, &id);
114     if (ret)
115 	krb5_err(context, 1, ret, "krb5_cc_new_unique");
116 
117     ret = krb5_cc_initialize(context, id, p);
118     if (ret)
119 	krb5_err(context, 1, ret, "krb5_cc_initialize");
120 
121     nc = krb5_cc_get_name(context, id);
122     if (nc == NULL)
123 	krb5_errx(context, 1, "krb5_cc_get_name");
124 
125     tc = krb5_cc_get_type(context, id);
126     if (tc == NULL)
127 	krb5_errx(context, 1, "krb5_cc_get_name");
128 
129     if (asprintf(&c, "%s:%s", tc, nc) < 0 || c == NULL)
130 	errx(1, "malloc");
131 
132     krb5_cc_close(context, id);
133 
134     ret = krb5_cc_resolve(context, c, &id2);
135     if (ret)
136 	krb5_err(context, 1, ret, "krb5_cc_resolve");
137 
138     ret = krb5_cc_get_principal(context, id2, &p2);
139     if (ret)
140 	krb5_err(context, 1, ret, "krb5_cc_get_principal");
141 
142     if (krb5_principal_compare(context, p, p2) == FALSE)
143 	krb5_errx(context, 1, "p != p2");
144 
145     krb5_cc_destroy(context, id2);
146     krb5_free_principal(context, p);
147     krb5_free_principal(context, p2);
148 
149     ret = krb5_cc_resolve(context, c, &id2);
150     if (ret)
151 	krb5_err(context, 1, ret, "krb5_cc_resolve");
152 
153     ret = krb5_cc_get_principal(context, id2, &p2);
154     if (ret == 0)
155 	krb5_errx(context, 1, "krb5_cc_get_principal");
156 
157     krb5_cc_destroy(context, id2);
158     free(c);
159 }
160 
161 /*
162  * Test that init works on a destroyed cc.
163  */
164 
165 static void
test_init_vs_destroy(krb5_context context,const char * type)166 test_init_vs_destroy(krb5_context context, const char *type)
167 {
168     krb5_error_code ret;
169     krb5_ccache id, id2;
170     krb5_principal p, p2;
171     char *n = NULL;
172 
173     ret = krb5_parse_name(context, "lha@SU.SE", &p);
174     if (ret)
175 	krb5_err(context, 1, ret, "krb5_parse_name");
176 
177     ret = krb5_cc_new_unique(context, type, NULL, &id);
178     if (ret)
179 	krb5_err(context, 1, ret, "krb5_cc_new_unique: %s", type);
180 
181     if (asprintf(&n, "%s:%s",
182 		 krb5_cc_get_type(context, id),
183 		 krb5_cc_get_name(context, id)) < 0 || n == NULL)
184 	errx(1, "malloc");
185 
186 
187     ret = krb5_cc_resolve(context, n, &id2);
188     free(n);
189     if (ret)
190 	krb5_err(context, 1, ret, "krb5_cc_resolve");
191 
192     krb5_cc_destroy(context, id);
193 
194     ret = krb5_cc_initialize(context, id2, p);
195     if (ret)
196 	krb5_err(context, 1, ret, "krb5_cc_initialize");
197 
198     ret = krb5_cc_get_principal(context, id2, &p2);
199     if (ret)
200 	krb5_err(context, 1, ret, "krb5_cc_get_principal");
201 
202     krb5_cc_destroy(context, id2);
203     krb5_free_principal(context, p);
204     krb5_free_principal(context, p2);
205 }
206 
207 static void
test_cache_remove(krb5_context context,const char * type)208 test_cache_remove(krb5_context context, const char *type)
209 {
210     krb5_error_code ret;
211     krb5_ccache id;
212     krb5_principal p;
213     krb5_creds cred;
214 
215     ret = krb5_parse_name(context, "lha@SU.SE", &p);
216     if (ret)
217 	krb5_err(context, 1, ret, "krb5_parse_name");
218 
219     ret = krb5_cc_new_unique(context, type, NULL, &id);
220     if (ret)
221 	krb5_err(context, 1, ret, "krb5_cc_gen_new: %s", type);
222 
223     ret = krb5_cc_initialize(context, id, p);
224     if (ret)
225 	krb5_err(context, 1, ret, "krb5_cc_initialize");
226 
227     /* */
228     memset(&cred, 0, sizeof(cred));
229     ret = krb5_parse_name(context, "krbtgt/SU.SE@SU.SE", &cred.server);
230     if (ret)
231 	krb5_err(context, 1, ret, "krb5_parse_name");
232     ret = krb5_parse_name(context, "lha@SU.SE", &cred.client);
233     if (ret)
234 	krb5_err(context, 1, ret, "krb5_parse_name");
235 
236     ret = krb5_cc_store_cred(context, id, &cred);
237     if (ret)
238 	krb5_err(context, 1, ret, "krb5_cc_store_cred");
239 
240     ret = krb5_cc_remove_cred(context, id, 0, &cred);
241     if (ret)
242 	krb5_err(context, 1, ret, "krb5_cc_remove_cred");
243 
244     ret = krb5_cc_destroy(context, id);
245     if (ret)
246 	krb5_err(context, 1, ret, "krb5_cc_destroy");
247 
248     krb5_free_principal(context, p);
249     krb5_free_principal(context, cred.server);
250     krb5_free_principal(context, cred.client);
251 }
252 
253 static void
test_mcc_default(void)254 test_mcc_default(void)
255 {
256     krb5_context context;
257     krb5_error_code ret;
258     krb5_ccache id, id2;
259     int i;
260 
261     for (i = 0; i < 10; i++) {
262 
263 	ret = krb5_init_context(&context);
264 	if (ret)
265 	    krb5_err(context, 1, ret, "krb5_init_context");
266 
267 	ret = krb5_cc_set_default_name(context, "MEMORY:foo");
268 	if (ret)
269 	    krb5_err(context, 1, ret, "krb5_cc_set_default_name");
270 
271 	ret = krb5_cc_default(context, &id);
272 	if (ret)
273 	    krb5_err(context, 1, ret, "krb5_cc_default");
274 
275 	ret = krb5_cc_default(context, &id2);
276 	if (ret)
277 	    krb5_err(context, 1, ret, "krb5_cc_default");
278 
279 	ret = krb5_cc_close(context, id);
280 	if (ret)
281 	    krb5_err(context, 1, ret, "krb5_cc_close");
282 
283 	ret = krb5_cc_close(context, id2);
284 	if (ret)
285 	    krb5_err(context, 1, ret, "krb5_cc_close");
286 
287 	krb5_free_context(context);
288     }
289 }
290 
291 struct {
292     char *str;
293     int fail;
294     char *res;
295 } cc_names[] = {
296     { "foo", 0, "foo" },
297     { "foo%}", 0, "foo%}" },
298     { "%{uid}", 0, NULL },
299     { "foo%{null}", 0, "foo" },
300     { "foo%{null}bar", 0, "foobar" },
301     { "%{", 1, NULL },
302     { "%{foo %{", 1, NULL },
303     { "%{{", 1, NULL },
304     { "%{{}", 1, NULL },
305     { "%{nulll}", 1, NULL },
306     { "%{does not exist}", 1, NULL },
307     { "%{}", 1, NULL },
308 #ifdef KRB5_USE_PATH_TOKENS
309     { "%{APPDATA}", 0, NULL },
310     { "%{COMMON_APPDATA}", 0, NULL},
311     { "%{LOCAL_APPDATA}", 0, NULL},
312     { "%{SYSTEM}", 0, NULL},
313     { "%{WINDOWS}", 0, NULL},
314     { "%{TEMP}", 0, NULL},
315     { "%{USERID}", 0, NULL},
316     { "%{uid}", 0, NULL},
317     { "%{USERCONFIG}", 0, NULL},
318     { "%{COMMONCONFIG}", 0, NULL},
319     { "%{LIBDIR}", 0, NULL},
320     { "%{BINDIR}", 0, NULL},
321     { "%{LIBEXEC}", 0, NULL},
322     { "%{SBINDIR}", 0, NULL},
323 #endif
324 };
325 
326 static void
test_def_cc_name(krb5_context context)327 test_def_cc_name(krb5_context context)
328 {
329     krb5_error_code ret;
330     char *str;
331     int i;
332 
333     for (i = 0; i < sizeof(cc_names)/sizeof(cc_names[0]); i++) {
334 	ret = _krb5_expand_default_cc_name(context, cc_names[i].str, &str);
335 	if (ret) {
336 	    if (cc_names[i].fail == 0)
337 		krb5_errx(context, 1, "test %d \"%s\" failed",
338 			  i, cc_names[i].str);
339 	} else {
340 	    if (cc_names[i].fail)
341 		krb5_errx(context, 1, "test %d \"%s\" was successful",
342 			  i, cc_names[i].str);
343 	    if (cc_names[i].res && strcmp(cc_names[i].res, str) != 0)
344 		krb5_errx(context, 1, "test %d %s != %s",
345 			  i, cc_names[i].res, str);
346 	    if (debug_flag)
347 		printf("%s => %s\n", cc_names[i].str, str);
348 	    free(str);
349 	}
350     }
351 }
352 
353 static void
test_cache_find(krb5_context context,const char * principal,int find)354 test_cache_find(krb5_context context, const char *principal, int find)
355 {
356     krb5_principal client;
357     krb5_error_code ret;
358     krb5_ccache id = NULL;
359 
360     ret = krb5_parse_name(context, principal, &client);
361     if (ret)
362 	krb5_err(context, 1, ret, "parse_name for %s failed", principal);
363 
364     ret = krb5_cc_cache_match(context, client, &id);
365     if (ret && find)
366 	krb5_err(context, 1, ret, "cc_cache_match for %s failed", principal);
367     if (ret == 0 && !find)
368 	krb5_err(context, 1, ret, "cc_cache_match for %s found", principal);
369 
370     if (id)
371 	krb5_cc_close(context, id);
372     krb5_free_principal(context, client);
373 }
374 
375 
376 static void
test_cache_iter(krb5_context context,const char * type,int destroy)377 test_cache_iter(krb5_context context, const char *type, int destroy)
378 {
379     krb5_cc_cache_cursor cursor;
380     krb5_error_code ret;
381     krb5_ccache id;
382 
383     ret = krb5_cc_cache_get_first (context, type, &cursor);
384     if (ret == KRB5_CC_NOSUPP)
385 	return;
386     else if (ret)
387 	krb5_err(context, 1, ret, "krb5_cc_cache_get_first(%s)", type);
388 
389 
390     while ((ret = krb5_cc_cache_next (context, cursor, &id)) == 0) {
391 	krb5_principal principal;
392 	char *name;
393 
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);
403 	    }
404 	    krb5_free_principal(context, principal);
405 	}
406 	if (destroy)
407 	    krb5_cc_destroy(context, id);
408 	else
409 	    krb5_cc_close(context, id);
410     }
411 
412     krb5_cc_cache_end_seq_get(context, cursor);
413 }
414 
415 static void
test_cache_iter_all(krb5_context context)416 test_cache_iter_all(krb5_context context)
417 {
418     krb5_cccol_cursor cursor;
419     krb5_error_code ret;
420     krb5_ccache id;
421 
422     ret = krb5_cccol_cursor_new (context, &cursor);
423     if (ret)
424 	krb5_err(context, 1, ret, "krb5_cccol_cursor_new");
425 
426 
427     while ((ret = krb5_cccol_cursor_next (context, cursor, &id)) == 0 && id != NULL) {
428 	krb5_principal principal;
429 	char *name;
430 
431 	if (debug_flag)
432 	    printf("name: %s\n", krb5_cc_get_name(context, id));
433 	ret = krb5_cc_get_principal(context, id, &principal);
434 	if (ret == 0) {
435 	    ret = krb5_unparse_name(context, principal, &name);
436 	    if (ret == 0) {
437 		if (debug_flag)
438 		    printf("\tprincipal: %s\n", name);
439 		free(name);
440 	    }
441 	    krb5_free_principal(context, principal);
442 	}
443 	krb5_cc_close(context, id);
444     }
445 
446     krb5_cccol_cursor_free(context, &cursor);
447 }
448 
449 
450 static void
test_copy(krb5_context context,const char * from,const char * to)451 test_copy(krb5_context context, const char *from, const char *to)
452 {
453     krb5_ccache fromid, toid;
454     krb5_error_code ret;
455     krb5_principal p, p2;
456 
457     ret = krb5_parse_name(context, "lha@SU.SE", &p);
458     if (ret)
459 	krb5_err(context, 1, ret, "krb5_parse_name");
460 
461     ret = krb5_cc_new_unique(context, from, NULL, &fromid);
462     if (ret)
463 	krb5_err(context, 1, ret, "krb5_cc_new_unique: %s", from);
464 
465     ret = krb5_cc_initialize(context, fromid, p);
466     if (ret)
467 	krb5_err(context, 1, ret, "krb5_cc_initialize");
468 
469     ret = krb5_cc_new_unique(context, to, NULL, &toid);
470     if (ret)
471 	krb5_err(context, 1, ret, "krb5_cc_gen_new: %s", to);
472 
473     ret = krb5_cc_copy_cache(context, fromid, toid);
474     if (ret)
475 	krb5_err(context, 1, ret, "krb5_cc_copy_cache");
476 
477     ret = krb5_cc_get_principal(context, toid, &p2);
478     if (ret)
479 	krb5_err(context, 1, ret, "krb5_cc_get_principal");
480 
481     if (krb5_principal_compare(context, p, p2) == FALSE)
482 	krb5_errx(context, 1, "p != p2");
483 
484     krb5_free_principal(context, p);
485     krb5_free_principal(context, p2);
486 
487     krb5_cc_destroy(context, fromid);
488     krb5_cc_destroy(context, toid);
489 }
490 
491 static void
test_move(krb5_context context,const char * type)492 test_move(krb5_context context, const char *type)
493 {
494     const krb5_cc_ops *ops;
495     krb5_ccache fromid, toid;
496     krb5_error_code ret;
497     krb5_principal p, p2;
498 
499     ops = krb5_cc_get_prefix_ops(context, type);
500     if (ops == NULL)
501 	return;
502 
503     ret = krb5_cc_new_unique(context, type, NULL, &fromid);
504     if (ret == KRB5_CC_NOSUPP)
505 	return;
506     else if (ret)
507 	krb5_err(context, 1, ret, "krb5_cc_new_unique: %s", type);
508 
509     ret = krb5_parse_name(context, "lha@SU.SE", &p);
510     if (ret)
511 	krb5_err(context, 1, ret, "krb5_parse_name");
512 
513     ret = krb5_cc_initialize(context, fromid, p);
514     if (ret)
515 	krb5_err(context, 1, ret, "krb5_cc_initialize");
516 
517     ret = krb5_cc_new_unique(context, type, NULL, &toid);
518     if (ret)
519 	krb5_err(context, 1, ret, "krb5_cc_new_unique");
520 
521     ret = krb5_cc_initialize(context, toid, p);
522     if (ret)
523 	krb5_err(context, 1, ret, "krb5_cc_initialize");
524 
525     ret = krb5_cc_get_principal(context, toid, &p2);
526     if (ret)
527 	krb5_err(context, 1, ret, "krb5_cc_get_principal");
528 
529     if (krb5_principal_compare(context, p, p2) == FALSE)
530 	krb5_errx(context, 1, "p != p2");
531 
532     krb5_free_principal(context, p);
533     krb5_free_principal(context, p2);
534 
535     krb5_cc_destroy(context, toid);
536     krb5_cc_destroy(context, fromid);
537 }
538 
539 
540 static void
test_prefix_ops(krb5_context context,const char * name,const krb5_cc_ops * ops)541 test_prefix_ops(krb5_context context, const char *name, const krb5_cc_ops *ops)
542 {
543     const krb5_cc_ops *o;
544 
545     o = krb5_cc_get_prefix_ops(context, name);
546     if (o == NULL)
547 	krb5_errx(context, 1, "found no match for prefix '%s'", name);
548     if (strcmp(o->prefix, ops->prefix) != 0)
549 	krb5_errx(context, 1, "ops for prefix '%s' is not "
550 		  "the expected %s != %s", name, o->prefix, ops->prefix);
551 }
552 
553 static void
test_cc_config(krb5_context context,const char * cc_type,const char * cc_name,size_t count)554 test_cc_config(krb5_context context, const char *cc_type,
555 	       const char *cc_name, size_t count)
556 {
557     krb5_error_code ret;
558     krb5_principal p;
559     krb5_ccache id;
560     unsigned int i;
561 
562     ret = krb5_cc_new_unique(context, cc_type, cc_name, &id);
563     if (ret)
564 	krb5_err(context, 1, ret, "krb5_cc_new_unique");
565 
566     ret = krb5_parse_name(context, "lha@SU.SE", &p);
567     if (ret)
568 	krb5_err(context, 1, ret, "krb5_parse_name");
569 
570     ret = krb5_cc_initialize(context, id, p);
571     if (ret)
572 	krb5_err(context, 1, ret, "krb5_cc_initialize");
573 
574     for (i = 0; i < count; i++) {
575 	krb5_data data, data2;
576 	const char *name = "foo";
577 	krb5_principal p1 = NULL;
578 
579 	if (i & 1)
580 	    p1 = p;
581 
582 	data.data = rk_UNCONST(name);
583 	data.length = strlen(name);
584 
585 	/*
586 	 * Because of how krb5_cc_set_config() this will also test
587 	 * krb5_cc_remove_cred().
588 	 */
589 	ret = krb5_cc_set_config(context, id, p1, "FriendlyName", &data);
590 	if (ret)
591 	    krb5_errx(context, 1, "krb5_cc_set_config: add");
592 
593 	ret = krb5_cc_get_config(context, id, p1, "FriendlyName", &data2);
594 	if (ret)
595 	    krb5_errx(context, 1, "krb5_cc_get_config: first");
596 
597 	if (data.length != data2.length ||
598 	    memcmp(data.data, data2.data, data.length) != 0)
599 	    krb5_errx(context, 1, "krb5_cc_get_config: did not fetch what was set");
600 
601 	krb5_data_free(&data2);
602 
603 	data.data = rk_UNCONST("bar");
604 	data.length = strlen("bar");
605 
606 	ret = krb5_cc_set_config(context, id, p1, "FriendlyName", &data);
607 	if (ret)
608 	    krb5_errx(context, 1, "krb5_cc_set_config: add -second");
609 
610 	ret = krb5_cc_get_config(context, id, p1, "FriendlyName", &data2);
611 	if (ret)
612 	    krb5_errx(context, 1, "krb5_cc_get_config: second");
613 
614 	if (data.length != data2.length ||
615 	    memcmp(data.data, data2.data, data.length) != 0)
616 	    krb5_errx(context, 1, "krb5_cc_get_config: replace failed");
617 
618 	krb5_data_free(&data2);
619 
620 	ret = krb5_cc_set_config(context, id, p1, "FriendlyName", NULL);
621 	if (ret)
622 	    krb5_errx(context, 1, "krb5_cc_set_config: delete");
623 
624 	ret = krb5_cc_get_config(context, id, p1, "FriendlyName", &data2);
625 	if (ret == 0)
626 	    krb5_errx(context, 1, "krb5_cc_get_config: non-existant");
627 
628 	if (data2.length)
629 	    krb5_errx(context, 1, "krb5_cc_get_config: delete failed");
630     }
631 
632     krb5_cc_destroy(context, id);
633     krb5_free_principal(context, p);
634 }
635 
636 
637 static struct getargs args[] = {
638     {"debug",	'd',	arg_flag,	&debug_flag,
639      "turn on debuggin", NULL },
640     {"version",	0,	arg_flag,	&version_flag,
641      "print version", NULL },
642     {"help",	0,	arg_flag,	&help_flag,
643      NULL, NULL }
644 };
645 
646 static void
usage(int ret)647 usage (int ret)
648 {
649     arg_printusage (args, sizeof(args)/sizeof(*args), NULL, "hostname ...");
650     exit (ret);
651 }
652 
653 int
main(int argc,char ** argv)654 main(int argc, char **argv)
655 {
656     krb5_context context;
657     krb5_error_code ret;
658     int optidx = 0;
659     krb5_ccache id1, id2;
660 
661     setprogname(argv[0]);
662 
663     if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
664 	usage(1);
665 
666     if (help_flag)
667 	usage (0);
668 
669     if(version_flag){
670 	print_version(NULL);
671 	exit(0);
672     }
673 
674     argc -= optidx;
675     argv += optidx;
676 
677     ret = krb5_init_context(&context);
678     if (ret)
679 	errx (1, "krb5_init_context failed: %d", ret);
680 
681     test_cache_remove(context, krb5_cc_type_file);
682     test_cache_remove(context, krb5_cc_type_memory);
683 #ifdef USE_SQLITE
684     test_cache_remove(context, krb5_cc_type_scc);
685 #endif
686 
687     test_default_name(context);
688     test_mcache(context);
689     test_init_vs_destroy(context, krb5_cc_type_memory);
690     test_init_vs_destroy(context, krb5_cc_type_file);
691 #if 0
692     test_init_vs_destroy(context, krb5_cc_type_api);
693 #endif
694     test_init_vs_destroy(context, krb5_cc_type_scc);
695     test_init_vs_destroy(context, krb5_cc_type_dcc);
696     test_mcc_default();
697     test_def_cc_name(context);
698 
699     test_cache_iter_all(context);
700 
701     test_cache_iter(context, krb5_cc_type_memory, 0);
702     {
703 	krb5_principal p;
704 	krb5_cc_new_unique(context, krb5_cc_type_memory, "bar", &id1);
705 	krb5_cc_new_unique(context, krb5_cc_type_memory, "baz", &id2);
706 	krb5_parse_name(context, "lha@SU.SE", &p);
707 	krb5_cc_initialize(context, id1, p);
708 	krb5_free_principal(context, p);
709     }
710 
711     test_cache_find(context, "lha@SU.SE", 1);
712     test_cache_find(context, "hulabundulahotentot@SU.SE", 0);
713 
714     test_cache_iter(context, krb5_cc_type_memory, 0);
715     test_cache_iter(context, krb5_cc_type_memory, 1);
716     test_cache_iter(context, krb5_cc_type_memory, 0);
717     test_cache_iter(context, krb5_cc_type_file, 0);
718     test_cache_iter(context, krb5_cc_type_api, 0);
719     test_cache_iter(context, krb5_cc_type_scc, 0);
720     test_cache_iter(context, krb5_cc_type_scc, 1);
721 #if 0
722     test_cache_iter(context, krb5_cc_type_dcc, 0);
723     test_cache_iter(context, krb5_cc_type_dcc, 1);
724 #endif
725 
726     test_copy(context, krb5_cc_type_file, krb5_cc_type_file);
727     test_copy(context, krb5_cc_type_memory, krb5_cc_type_memory);
728     test_copy(context, krb5_cc_type_file, krb5_cc_type_memory);
729     test_copy(context, krb5_cc_type_memory, krb5_cc_type_file);
730     test_copy(context, krb5_cc_type_scc, krb5_cc_type_file);
731     test_copy(context, krb5_cc_type_file, krb5_cc_type_scc);
732     test_copy(context, krb5_cc_type_scc, krb5_cc_type_memory);
733     test_copy(context, krb5_cc_type_memory, krb5_cc_type_scc);
734 #if 0
735     test_copy(context, krb5_cc_type_dcc, krb5_cc_type_memory);
736     test_copy(context, krb5_cc_type_dcc, krb5_cc_type_file);
737     test_copy(context, krb5_cc_type_dcc, krb5_cc_type_scc);
738 #endif
739 
740     test_move(context, krb5_cc_type_file);
741     test_move(context, krb5_cc_type_memory);
742 #ifdef HAVE_KCM
743     test_move(context, krb5_cc_type_kcm);
744 #endif
745     test_move(context, krb5_cc_type_scc);
746 #if 0
747     test_move(context, krb5_cc_type_dcc);
748 #endif
749 
750     test_prefix_ops(context, "FILE:/tmp/foo", &krb5_fcc_ops);
751     test_prefix_ops(context, "FILE", &krb5_fcc_ops);
752     test_prefix_ops(context, "MEMORY", &krb5_mcc_ops);
753     test_prefix_ops(context, "MEMORY:foo", &krb5_mcc_ops);
754     test_prefix_ops(context, "/tmp/kaka", &krb5_fcc_ops);
755 #ifdef HAVE_SCC
756     test_prefix_ops(context, "SCC:", &krb5_scc_ops);
757     test_prefix_ops(context, "SCC:foo", &krb5_scc_ops);
758 #endif
759 #if 0
760     test_prefix_ops(context, "DIR:", &krb5_dcc_ops);
761     test_prefix_ops(context, "DIR:tkt1", &krb5_dcc_ops);
762 #endif
763 
764     krb5_cc_destroy(context, id1);
765     krb5_cc_destroy(context, id2);
766 
767     test_cc_config(context, "MEMORY", "bar", 1000);  /* 1000 because fast */
768     test_cc_config(context, "FILE", "/tmp/foocc", 30); /* 30 because slower */
769 
770     krb5_free_context(context);
771 
772 #if 0
773     sleep(60);
774 #endif
775 
776     return 0;
777 }
778