summaryrefslogtreecommitdiffstats
path: root/testsuite/unittests/test_krb5_samba.c
blob: 8b7e843b85fcb8dca944d93f02e1cde50d0a39ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>

#include <krb5.h>

#include "includes.h"
#include "lib/krb5_wrap/krb5_samba.h"


static int setup_krb5_context(void **state)
{
	krb5_context context = NULL;
	krb5_error_code code;

	code = krb5_init_context(&context);
	assert_return_code(code, code);

	*state = context;

	return 0;
}

static int teardown_krb5_context(void **state)
{
	krb5_context context = *state;

	if (context != NULL) {
		krb5_free_context(context);
	}
	return 0;
}

static void test_smb_krb5_kt_open(void **state)
{
	krb5_context context = *state;
	krb5_keytab keytab = NULL;
	krb5_error_code code;
	char keytab_template[] = "/tmp/keytab.XXXXXX";
	int fd;

	fd = mkstemp(keytab_template);
	assert_return_code(fd, errno);
	unlink(keytab_template);

	code = smb_krb5_kt_open(context,
				keytab_template,
				false,
				&keytab);
	assert_int_equal(code, 0);

	krb5_kt_close(context, keytab);
	close(fd);
}

static void test_smb_krb5_kt_open_file(void **state)
{
	krb5_context context = *state;
	krb5_keytab keytab = NULL;
	krb5_error_code code;
	char keytab_template[] = "/tmp/keytab.XXXXXX";
	char keytab_file[6 + strlen(keytab_template)];
	int fd;

	fd = mkstemp(keytab_template);
	assert_return_code(fd, errno);
	unlink(keytab_template);

	snprintf(keytab_file, sizeof(keytab_file), "FILE:%s", keytab_template);

	code = smb_krb5_kt_open(context,
				keytab_file,
				false,
				&keytab);
	assert_int_equal(code, 0);

	krb5_kt_close(context, keytab);
	close(fd);
}

static void test_smb_krb5_kt_open_fail(void **state)
{
	krb5_context context = *state;
	krb5_keytab keytab = NULL;
	krb5_error_code code;

	code = smb_krb5_kt_open(context,
				NULL,
				false,
				&keytab);
	assert_int_equal(code, KRB5_KT_BADNAME);
	code = smb_krb5_kt_open(context,
				"wurst",
				false,
				&keytab);
	assert_int_equal(code, KRB5_KT_BADNAME);

	code = smb_krb5_kt_open(context,
				"FILE:wurst",
				false,
				&keytab);
	assert_int_equal(code, KRB5_KT_BADNAME);

	code = smb_krb5_kt_open(context,
				"WRFILE:wurst",
				false,
				&keytab);
	assert_int_equal(code, KRB5_KT_BADNAME);
}

static void test_smb_krb5_kt_open_relative_memory(void **state)
{
	krb5_context context = *state;
	krb5_keytab keytab = NULL;
	krb5_error_code code;

	code = smb_krb5_kt_open_relative(context,
					 NULL,
					 true,
					 &keytab);
	assert_int_equal(code, 0);

	krb5_kt_close(context, keytab);
}

int main(void) {
	const struct CMUnitTest tests[] = {
		cmocka_unit_test_setup_teardown(test_smb_krb5_kt_open,
						setup_krb5_context,
						teardown_krb5_context),
		cmocka_unit_test_setup_teardown(test_smb_krb5_kt_open_file,
						setup_krb5_context,
						teardown_krb5_context),
		cmocka_unit_test_setup_teardown(test_smb_krb5_kt_open_fail,
						setup_krb5_context,
						teardown_krb5_context),
		cmocka_unit_test_setup_teardown(test_smb_krb5_kt_open_relative_memory,
						setup_krb5_context,
						teardown_krb5_context),
	};

	cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
	return cmocka_run_group_tests(tests, NULL, NULL);
}