pyldb: avoid segfault when adding an element with no name
[sfrench/samba-autobuild/.git] / testsuite / unittests / test_krb5_samba.c
1 #include <stdarg.h>
2 #include <stddef.h>
3 #include <setjmp.h>
4 #include <cmocka.h>
5
6 #include <krb5.h>
7
8 #include "includes.h"
9 #include "lib/krb5_wrap/krb5_samba.h"
10
11
12 static int setup_krb5_context(void **state)
13 {
14         krb5_context context = NULL;
15         krb5_error_code code;
16
17         code = krb5_init_context(&context);
18         assert_return_code(code, code);
19
20         *state = context;
21
22         return 0;
23 }
24
25 static int teardown_krb5_context(void **state)
26 {
27         krb5_context context = *state;
28
29         if (context != NULL) {
30                 krb5_free_context(context);
31         }
32         return 0;
33 }
34
35 static void test_smb_krb5_kt_open(void **state)
36 {
37         krb5_context context = *state;
38         krb5_keytab keytab = NULL;
39         krb5_error_code code;
40         char keytab_template[] = "/tmp/keytab.XXXXXX";
41         int fd;
42
43         fd = mkstemp(keytab_template);
44         assert_return_code(fd, errno);
45         unlink(keytab_template);
46
47         code = smb_krb5_kt_open(context,
48                                 keytab_template,
49                                 false,
50                                 &keytab);
51         assert_int_equal(code, 0);
52
53         krb5_kt_close(context, keytab);
54         close(fd);
55 }
56
57 static void test_smb_krb5_kt_open_file(void **state)
58 {
59         krb5_context context = *state;
60         krb5_keytab keytab = NULL;
61         krb5_error_code code;
62         char keytab_template[] = "/tmp/keytab.XXXXXX";
63         char keytab_file[6 + strlen(keytab_template)];
64         int fd;
65
66         fd = mkstemp(keytab_template);
67         assert_return_code(fd, errno);
68         unlink(keytab_template);
69
70         snprintf(keytab_file, sizeof(keytab_file), "FILE:%s", keytab_template);
71
72         code = smb_krb5_kt_open(context,
73                                 keytab_file,
74                                 false,
75                                 &keytab);
76         assert_int_equal(code, 0);
77
78         krb5_kt_close(context, keytab);
79         close(fd);
80 }
81
82 static void test_smb_krb5_kt_open_fail(void **state)
83 {
84         krb5_context context = *state;
85         krb5_keytab keytab = NULL;
86         krb5_error_code code;
87
88         code = smb_krb5_kt_open(context,
89                                 NULL,
90                                 false,
91                                 &keytab);
92         assert_int_equal(code, KRB5_KT_BADNAME);
93         code = smb_krb5_kt_open(context,
94                                 "wurst",
95                                 false,
96                                 &keytab);
97         assert_int_equal(code, KRB5_KT_BADNAME);
98
99         code = smb_krb5_kt_open(context,
100                                 "FILE:wurst",
101                                 false,
102                                 &keytab);
103         assert_int_equal(code, KRB5_KT_BADNAME);
104
105         code = smb_krb5_kt_open(context,
106                                 "WRFILE:wurst",
107                                 false,
108                                 &keytab);
109         assert_int_equal(code, KRB5_KT_BADNAME);
110 }
111
112 static void test_smb_krb5_kt_open_relative_memory(void **state)
113 {
114         krb5_context context = *state;
115         krb5_keytab keytab = NULL;
116         krb5_error_code code;
117
118         code = smb_krb5_kt_open_relative(context,
119                                          NULL,
120                                          true,
121                                          &keytab);
122         assert_int_equal(code, 0);
123
124         krb5_kt_close(context, keytab);
125 }
126
127 int main(void) {
128         const struct CMUnitTest tests[] = {
129                 cmocka_unit_test_setup_teardown(test_smb_krb5_kt_open,
130                                                 setup_krb5_context,
131                                                 teardown_krb5_context),
132                 cmocka_unit_test_setup_teardown(test_smb_krb5_kt_open_file,
133                                                 setup_krb5_context,
134                                                 teardown_krb5_context),
135                 cmocka_unit_test_setup_teardown(test_smb_krb5_kt_open_fail,
136                                                 setup_krb5_context,
137                                                 teardown_krb5_context),
138                 cmocka_unit_test_setup_teardown(test_smb_krb5_kt_open_relative_memory,
139                                                 setup_krb5_context,
140                                                 teardown_krb5_context),
141         };
142
143         cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
144         return cmocka_run_group_tests(tests, NULL, NULL);
145 }