1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/ccache/t_cc.c */
4 * Copyright 2000 by the Massachusetts Institute of Technology.
7 * Export of this software from the United States of America may
8 * require a specific license from the United States Government.
9 * It is the responsibility of any person or organization contemplating
10 * export to obtain such a license before exporting.
12 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13 * distribute this software and its documentation for any purpose and
14 * without fee is hereby granted, provided that the above copyright
15 * notice appear in all copies and that both that copyright notice and
16 * this permission notice appear in supporting documentation, and that
17 * the name of M.I.T. not be used in advertising or publicity pertaining
18 * to distribution of the software without specific, written prior
19 * permission. Furthermore if you modify this software you must label
20 * your software as modified software and not distribute it in such a
21 * fashion that it might be confused with the original M.I.T. software.
22 * M.I.T. makes no representations about the suitability of
23 * this software for any purpose. It is provided "as is" without express
24 * or implied warranty.
39 krb5_creds test_creds;
46 static int add=0x12345;
48 static krb5_address addr;
50 static krb5_address *addrs[] = {
55 addr.magic = KV5M_ADDRESS;
56 addr.addrtype = ADDRTYPE_INET;
58 addr.contents = (krb5_octet *) &add;
60 test_creds.magic = KV5M_CREDS;
61 test_creds.client = NULL;
62 test_creds.server = NULL;
64 test_creds.keyblock.magic = KV5M_KEYBLOCK;
65 test_creds.keyblock.contents = 0;
66 test_creds.keyblock.enctype = 1;
67 test_creds.keyblock.length = 1;
68 test_creds.keyblock.contents = (unsigned char *) "1";
69 test_creds.times.authtime = 1111;
70 test_creds.times.starttime = 2222;
71 test_creds.times.endtime = 3333;
72 test_creds.times.renew_till = 4444;
73 test_creds.is_skey = 1;
74 test_creds.ticket_flags = 5555;
75 test_creds.addresses = addrs;
77 #define SET_TICKET(ent, str) {ent.magic = KV5M_DATA; ent.length = sizeof(str); ent.data = str;}
78 SET_TICKET(test_creds.ticket, "This is ticket 1");
79 SET_TICKET(test_creds.second_ticket, "This is ticket 2");
80 test_creds.authdata = NULL;
83 static krb5_error_code
84 init_test_cred(krb5_context context)
90 kret = krb5_build_principal(context, &test_creds.client, sizeof(REALM), REALM,
91 "client-comp1", "client-comp2", NULL);
95 kret = krb5_build_principal(context, &test_creds.server, sizeof(REALM), REALM,
96 "server-comp1", "server-comp2", NULL);
98 krb5_free_principal(context, test_creds.client);
99 test_creds.client = 0;
103 test_creds.authdata = malloc (3 * sizeof(krb5_authdata *));
104 if (!test_creds.authdata) {
109 for (i = 0 ; i <= 2 ; i++) {
110 test_creds.authdata[i] = 0;
112 a = (krb5_authdata *) malloc(sizeof(krb5_authdata));
117 a->magic = KV5M_AUTHDATA;
118 a->ad_type = KRB5_AUTHDATA_IF_RELEVANT;
119 a->contents = (krb5_octet * ) malloc(1);
127 test_creds.authdata[0] = a;
129 a = (krb5_authdata *) malloc(sizeof(krb5_authdata));
134 a->magic = KV5M_AUTHDATA;
135 a->ad_type = KRB5_AUTHDATA_KDC_ISSUED;
136 a->contents = (krb5_octet * ) malloc(2);
145 test_creds.authdata[1] = a;
149 if (test_creds.client) {
150 krb5_free_principal(context, test_creds.client);
151 test_creds.client = 0;
153 if (test_creds.server) {
154 krb5_free_principal(context, test_creds.server);
155 test_creds.server = 0;
158 if (test_creds.authdata) {
159 krb5_free_authdata(context, test_creds.authdata);
160 test_creds.authdata = 0;
168 free_test_cred(krb5_context context)
170 krb5_free_principal(context, test_creds.client);
172 krb5_free_principal(context, test_creds.server);
174 if(test_creds.authdata) {
175 krb5_free_authdata(context, test_creds.authdata);
176 test_creds.authdata = 0;
180 #define CHECK(kret,msg) \
181 if (kret != KRB5_OK) { \
182 com_err(msg, kret, ""); \
185 } else if(debug) printf("%s went ok\n", msg);
187 #define CHECK_STR(str,msg) \
189 com_err(msg, kret, ""); \
191 } else if(debug) printf("%s went ok\n", msg);
193 #define CHECK_BOOL(expr,errstr,msg) \
195 fprintf(stderr, "%s %s\n", msg, errstr); \
197 } else if(debug) printf("%s went ok\n", msg);
199 #define CHECK_FAIL(experr, kret, msg) \
200 if (experr != kret) { CHECK(kret, msg);}
203 cc_test(krb5_context context, const char *name, krb5_flags flags)
207 krb5_error_code kret;
208 krb5_cc_cursor cursor;
215 kret = init_test_cred(context);
216 CHECK(kret, "init_creds");
218 kret = krb5_cc_resolve(context, name, &id);
219 CHECK(kret, "resolve");
220 kret = krb5_cc_initialize(context, id, test_creds.client);
221 CHECK(kret, "initialize");
223 c_name = krb5_cc_get_name(context, id);
224 CHECK_STR(c_name, "get_name");
226 c_name = krb5_cc_get_type(context, id);
227 CHECK_STR(c_name, "get_type");
228 save_type=strdup(c_name);
229 CHECK_STR(save_type, "copying type");
231 kret = krb5_cc_store_cred(context, id, &test_creds);
232 CHECK(kret, "store");
234 kret = krb5_cc_get_principal(context, id, &tmp);
235 CHECK(kret, "get_principal");
237 CHECK_BOOL(krb5_realm_compare(context, tmp, test_creds.client) != TRUE,
238 "realms do not match", "realm_compare");
241 CHECK_BOOL(krb5_principal_compare(context, tmp, test_creds.client) != TRUE,
242 "principals do not match", "principal_compare");
244 krb5_free_principal(context, tmp);
246 kret = krb5_cc_set_flags (context, id, flags);
247 CHECK(kret, "set_flags");
249 kret = krb5_cc_start_seq_get(context, id, &cursor);
250 CHECK(kret, "start_seq_get");
252 while (kret != KRB5_CC_END) {
253 if(debug) printf("Calling next_cred\n");
254 kret = krb5_cc_next_cred(context, id, &cursor, &creds);
255 if(kret == KRB5_CC_END) {
256 if(debug) printf("next_cred: ok at end\n");
259 CHECK(kret, "next_cred");
260 krb5_free_cred_contents(context, &creds);
264 kret = krb5_cc_end_seq_get(context, id, &cursor);
265 CHECK(kret, "end_seq_get");
267 kret = krb5_cc_close(context, id);
268 CHECK(kret, "close");
271 /* ------------------------------------------------- */
272 kret = krb5_cc_resolve(context, name, &id);
273 CHECK(kret, "resolve2");
276 /* Copy the cache test*/
277 snprintf(newcache, sizeof(newcache), "%s.new", name);
278 kret = krb5_cc_resolve(context, newcache, &id2);
279 CHECK(kret, "resolve of new cache");
281 /* This should fail as the new creds are not initialized */
282 kret = krb5_cc_copy_creds(context, id, id2);
283 CHECK_FAIL(KRB5_FCC_NOFILE, kret, "copy_creds");
285 kret = krb5_cc_initialize(context, id2, test_creds.client);
286 CHECK(kret, "initialize of id2");
288 kret = krb5_cc_copy_creds(context, id, id2);
289 CHECK(kret, "copy_creds");
291 kret = krb5_cc_destroy(context, id2);
292 CHECK(kret, "destroy new cache");
295 /* Destroy the first cache */
296 kret = krb5_cc_destroy(context, id);
297 CHECK(kret, "destroy");
299 /* ----------------------------------------------------- */
300 /* Tests the generate new code */
301 kret = krb5_cc_new_unique(context, save_type,
303 CHECK(kret, "new_unique");
305 kret = krb5_cc_initialize(context, id2, test_creds.client);
306 CHECK(kret, "initialize");
308 kret = krb5_cc_store_cred(context, id2, &test_creds);
309 CHECK(kret, "store");
311 kret = krb5_cc_destroy(context, id2);
312 CHECK(kret, "destroy id2");
315 free_test_cred(context);
320 * Checks if a credential type is registered with the library
323 check_registered(krb5_context context, const char *prefix)
326 krb5_error_code kret;
329 snprintf(name, sizeof(name), "%s/tmp/cctest.%ld", prefix, (long) getpid());
331 kret = krb5_cc_resolve(context, name, &id);
332 if(kret != KRB5_OK) {
333 if(kret == KRB5_CC_UNKNOWN_TYPE)
335 com_err("Checking on credential type", kret, "%s", prefix);
340 kret = krb5_cc_close(context, id);
341 if(kret != KRB5_OK) {
342 com_err("Checking on credential type - closing", kret, "%s", prefix);
351 do_test(krb5_context context, const char *prefix)
355 snprintf(name, sizeof(name), "%s/tmp/cctest.%ld", prefix, (long) getpid());
356 printf("Starting test on %s\n", name);
357 cc_test (context, name, 0);
358 cc_test (context, name, !0);
359 printf("Test on %s passed\n", name);
363 test_misc(krb5_context context)
365 /* Tests for certain error returns */
366 krb5_error_code kret;
368 const krb5_cc_ops *ops_save;
370 fprintf(stderr, "Testing miscellaneous error conditions\n");
372 kret = krb5_cc_resolve(context, "unknown_method_ep:/tmp/name", &id);
373 if (kret != KRB5_CC_UNKNOWN_TYPE) {
374 CHECK(kret, "resolve unknown type");
377 /* Test for not specifiying a cache type with no defaults */
378 ops_save = krb5_cc_dfl_ops;
381 kret = krb5_cc_resolve(context, "/tmp/e", &id);
382 if (kret != KRB5_CC_BADNAME) {
383 CHECK(kret, "resolve no builtin type");
386 krb5_cc_dfl_ops = ops_save;
389 extern const krb5_cc_ops krb5_mcc_ops;
390 extern const krb5_cc_ops krb5_fcc_ops;
395 krb5_context context;
396 krb5_error_code kret;
398 if ((kret = krb5_init_context(&context))) {
399 printf("Couldn't initialize krb5 library: %s\n",
400 error_message(kret));
404 kret = krb5_cc_register(context, &krb5_mcc_ops,0);
405 if(kret && kret != KRB5_CC_TYPE_EXISTS) {
406 CHECK(kret, "register_mem");
409 kret = krb5_cc_register(context, &krb5_fcc_ops,0);
410 if(kret && kret != KRB5_CC_TYPE_EXISTS) {
411 CHECK(kret, "register_mem");
414 /* Registering a second time tests for error return */
415 kret = krb5_cc_register(context, &krb5_fcc_ops,0);
416 if(kret != KRB5_CC_TYPE_EXISTS) {
417 CHECK(kret, "register_mem");
420 /* Registering with override should work */
421 kret = krb5_cc_register(context, &krb5_fcc_ops,1);
422 CHECK(kret, "register_mem override");
427 do_test(context, "");
429 if (check_registered(context, "KEYRING:process:"))
430 do_test(context, "KEYRING:process:");
432 printf("Skiping KEYRING: test - unregistered type\n");
434 do_test(context, "MEMORY:");
435 do_test(context, "FILE:");
437 krb5_free_context(context);