1/*
2 * Copyright (c) 2008-2010 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Portions Copyright (c) 2008-2010 Apple Inc. 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 the Institute nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include <err.h>
37#include <string.h>
38#include <Kerberos/krb5.h>
39#include "test_collection.h"
40
41int main(int argc, char **argv)
42{
43	char *realm = "ADS.APPLE.COM";
44	const char *ptr = NULL;
45	char *nn = NULL;
46	krb5_error_code ret = 0;
47
48	krb5_context ctx = NULL;
49	krb5_principal princ = NULL, xprinc = NULL;
50	krb5_address **addy = NULL;
51	krb5_authdata **auth = NULL;
52
53	krb5_ccache ccache = NULL;
54	krb5_creds creds;
55	krb5_get_init_creds_opt options;
56	krb5_cc_cursor cursor;
57
58	test_collection_t *tc = NULL;
59	tc = tests_init_and_start("test-krb5");
60	tests_set_flags(tc, TC_FLAG_EXIT_ON_FAILURE);
61	tests_set_total_count_hint(tc, 16);
62
63	krb5_get_init_creds_opt_init(&options);
64
65	ret = krb5_init_context(&ctx);
66	test_evaluate(tc, "krb5_init_context", ret);
67	krb5_free_context(ctx);
68
69	ret = krb5_init_secure_context(&ctx);
70	test_evaluate(tc, "krb5_init_secure_context", ret);
71	ret = krb5_parse_name(ctx, realm, &xprinc);
72	test_evaluate(tc, "krb5_parse_name", ret);
73	ret = krb5_unparse_name(ctx, xprinc, &nn);
74	test_evaluate(tc, "krb5_unparse_name", ret);
75
76	ptr = krb5_cc_default_name(ctx);
77	test_evaluate(tc, "krb5_cc_default_name", ptr == NULL);
78	ret = krb5_build_principal(ctx, &princ, sizeof(ptr), ptr,
79			"client-comp1", "client-comp2", NULL);
80	test_evaluate(tc, "krb5_build_principal", ret);
81
82	ret = krb5_cc_resolve(ctx, realm, &ccache);
83	test_evaluate(tc, "krb5_cc_resolve", ret);
84	ret = krb5_cc_close(ctx, ccache);
85	test_evaluate(tc, "krb5_cc_close", ret);
86
87	ret = krb5_cc_resolve(ctx, realm, &ccache);
88	test_evaluate(tc, "krb5_cc_resolve", ret);
89	ret = krb5_cc_initialize(ctx, ccache, princ);
90	test_evaluate(tc, "krb5_cc_initialize", ret);
91
92	ptr = krb5_cc_get_name(ctx, ccache);
93	test_evaluate(tc, "krb5_cc_get_name", ret);
94	ret = krb5_cc_get_principal(ctx, ccache, &princ);
95	test_evaluate(tc, "krb5_cc_get_principal1", ret);
96	ret = krb5_cc_get_principal(ctx, ccache, &xprinc);
97	test_evaluate(tc, "krb5_cc_get_principal2", ret);
98
99	ret = krb5_cc_start_seq_get(ctx, ccache, &cursor);
100	test_evaluate(tc, "krb5_cc_start_seq_get", ret);
101	while((ret = krb5_cc_next_cred(ctx, ccache, &cursor, &creds)) == 0)
102	{
103		ret = krb5_cc_store_cred(ctx, ccache, &creds);
104		test_evaluate(tc, "krb5_cc_store_cred", ret);
105		krb5_free_cred_contents(ctx, &creds);
106	}
107	ret = krb5_cc_end_seq_get(ctx, ccache, &cursor);
108	test_evaluate(tc, "krb5_cc_end_seq_get", ret);
109
110	ret = krb5_cc_destroy(ctx, ccache);
111	test_evaluate(tc, "krb5_cc_destroy", ret);
112
113	addy = malloc(sizeof(krb5_address*)*2);
114	addy[0] = malloc(sizeof(krb5_address));
115	addy[0]->contents = malloc(sizeof(krb5_octet));
116	addy[1] = NULL;
117	//test_evaluate(tc, "krb5_os_localaddr", ret);
118	krb5_free_addresses(ctx, addy);
119
120	auth = calloc(1, sizeof(krb5_authdata));
121	krb5_free_authdata(ctx, auth);
122	krb5_free_principal(ctx, xprinc);
123
124	krb5_free_context(ctx);
125
126	// test error_message
127	test_evaluate(tc, "error_message-asn1", strcmp(error_message(1859794432L), "ASN.1 failed call to system time library"));
128	test_evaluate(tc, "error_message-gk5", strcmp(error_message(35224064), "No @ in SERVICE-NAME name string"));
129	test_evaluate(tc, "error_message-wind", strcmp(error_message(-969269760), "No error"));
130	test_evaluate(tc, "error_message-krb5", strcmp(error_message(-1765328384L), "No error"));
131	test_evaluate(tc, "error_message-krb", strcmp(error_message(39525376), "Kerberos 4 successful"));
132	test_evaluate(tc, "error_message-k524", strcmp(error_message(-1750206208), "wrong keytype in ticket"));
133	test_evaluate(tc, "error_message-heim", strcmp(error_message(-1980176640), "Error parsing log destination"));
134	test_evaluate(tc, "error_message-hx", strcmp(error_message(569856), "ASN.1 failed call to system time library"));
135
136	return tests_stop_and_free(tc);
137}
138