ifdef away code to be able to build with --disable-krb4
[heimdal.git] / lib / krb5 / name-45-test.c
blobf6302c9610f111e3efc72e06d7cc04225ffef67e
1 /*
2 * Copyright (c) 2002 - 2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of KTH nor the names of its contributors may be
18 * used to endorse or promote products derived from this software without
19 * specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
33 #define KRB5_DEPRECATED
35 #include "krb5_locl.h"
36 #include <err.h>
38 #ifdef KRB4
40 enum { MAX_COMPONENTS = 3 };
42 static struct testcase {
43 const char *v4_name;
44 const char *v4_inst;
45 const char *v4_realm;
47 krb5_realm v5_realm;
48 unsigned ncomponents;
49 char *comp_val[MAX_COMPONENTS];
51 const char *config_file;
52 krb5_error_code ret; /* expected error code from 524 */
54 krb5_error_code ret2; /* expected error code from 425 */
55 } tests[] = {
56 {"", "", "", "", 1, {""}, NULL, 0, 0},
57 {"a", "", "", "", 1, {"a"}, NULL, 0, 0},
58 {"a", "b", "", "", 2, {"a", "b"}, NULL, 0, 0},
59 {"a", "b", "c", "c", 2, {"a", "b"}, NULL, 0, 0},
61 {"krbtgt", "FOO.SE", "FOO.SE", "FOO.SE", 2,
62 {"krbtgt", "FOO.SE"}, NULL, 0, 0},
64 {"foo", "bar2", "BAZ", "BAZ", 2,
65 {"foo", "bar2"}, NULL, 0, 0},
66 {"foo", "bar2", "BAZ", "BAZ", 2,
67 {"foo", "bar2"},
68 "[libdefaults]\n"
69 " v4_name_convert = {\n"
70 " host = {\n"
71 " foo = foo5\n"
72 " }\n"
73 "}\n",
74 HEIM_ERR_V4_PRINC_NO_CONV, 0},
75 {"foo", "bar2", "BAZ", "BAZ", 2,
76 {"foo5", "bar2.baz"},
77 "[realms]\n"
78 " BAZ = {\n"
79 " v4_name_convert = {\n"
80 " host = {\n"
81 " foo = foo5\n"
82 " }\n"
83 " }\n"
84 " v4_instance_convert = {\n"
85 " bar2 = bar2.baz\n"
86 " }\n"
87 " }\n",
88 0, 0},
90 {"rcmd", "foo", "realm", "realm", 2, {"host", "foo"}, NULL,
91 HEIM_ERR_V4_PRINC_NO_CONV, 0},
92 {"rcmd", "foo", "realm", "realm", 2, {"host", "foo.realm"},
93 "[realms]\n"
94 " realm = {\n"
95 " v4_instance_convert = {\n"
96 " foo = foo.realm\n"
97 " }\n"
98 " }\n",
99 0, 0},
101 {"pop", "mail0", "NADA.KTH.SE", "NADA.KTH.SE", 2,
102 {"pop", "mail0.nada.kth.se"}, "", HEIM_ERR_V4_PRINC_NO_CONV, 0},
103 {"pop", "mail0", "NADA.KTH.SE", "NADA.KTH.SE", 2,
104 {"pop", "mail0.nada.kth.se"},
105 "[realms]\n"
106 " NADA.KTH.SE = {\n"
107 " default_domain = nada.kth.se\n"
108 " }\n",
109 0, 0},
110 {"pop", "mail0", "NADA.KTH.SE", "NADA.KTH.SE", 2,
111 {"pop", "mail0.nada.kth.se"},
112 "[libdefaults]\n"
113 " v4_instance_resolve = true\n",
114 HEIM_ERR_V4_PRINC_NO_CONV, 0},
116 {"rcmd", "hokkigai", "NADA.KTH.SE", "NADA.KTH.SE", 2,
117 {"host", "hokkigai.pdc.kth.se"}, "", HEIM_ERR_V4_PRINC_NO_CONV, 0},
118 {"rcmd", "hokkigai", "NADA.KTH.SE", "NADA.KTH.SE", 2,
119 {"host", "hokkigai.pdc.kth.se"},
120 "[libdefaults]\n"
121 " v4_instance_resolve = true\n"
122 "[realms]\n"
123 " NADA.KTH.SE = {\n"
124 " v4_name_convert = {\n"
125 " host = {\n"
126 " rcmd = host\n"
127 " }\n"
128 " }\n"
129 " default_domain = pdc.kth.se\n"
130 " }\n",
131 0, 0},
133 {"0123456789012345678901234567890123456789",
134 "0123456789012345678901234567890123456789",
135 "0123456789012345678901234567890123456789",
136 "0123456789012345678901234567890123456789",
137 2, {"0123456789012345678901234567890123456789",
138 "0123456789012345678901234567890123456789"}, NULL,
139 0, KRB5_PARSE_MALFORMED},
141 {"012345678901234567890123456789012345678",
142 "012345678901234567890123456789012345678",
143 "012345678901234567890123456789012345678",
144 "012345678901234567890123456789012345678",
145 2, {"012345678901234567890123456789012345678",
146 "012345678901234567890123456789012345678"}, NULL,
147 0, 0},
149 {NULL, NULL, NULL, NULL, 0, {NULL}, NULL, 0}
153 main(int argc, char **argv)
155 struct testcase *t;
156 krb5_context context;
157 krb5_error_code ret;
158 char hostname[1024];
159 int val = 0;
161 setprogname(argv[0]);
163 gethostname(hostname, sizeof(hostname));
164 if (!(strstr(hostname, "kth.se") != NULL || strstr(hostname, "su.se") != NULL))
165 return 0;
167 for (t = tests; t->v4_name; ++t) {
168 krb5_principal princ;
169 int i;
170 char name[40], inst[40], realm[40];
171 char printable_princ[256];
173 ret = krb5_init_context (&context);
174 if (ret)
175 errx (1, "krb5_init_context failed: %d", ret);
177 if (t->config_file != NULL) {
178 char template[] = "/tmp/krb5-conf-XXXXXX";
179 int fd = mkstemp(template);
180 char *files[2];
182 if (fd < 0)
183 krb5_err (context, 1, errno, "mkstemp %s", template);
185 if (write (fd, t->config_file, strlen(t->config_file))
186 != strlen(t->config_file))
187 krb5_err (context, 1, errno, "write %s", template);
188 close (fd);
189 files[0] = template;
190 files[1] = NULL;
192 ret = krb5_set_config_files (context, files);
193 unlink (template);
194 if (ret)
195 krb5_err (context, 1, ret, "krb5_set_config_files");
198 ret = krb5_425_conv_principal (context,
199 t->v4_name,
200 t->v4_inst,
201 t->v4_realm,
202 &princ);
203 if (ret) {
204 if (ret != t->ret) {
205 krb5_warn (context, ret,
206 "krb5_425_conv_principal %s.%s@%s",
207 t->v4_name, t->v4_inst, t->v4_realm);
208 val = 1;
210 } else {
211 if (t->ret) {
212 char *s;
213 krb5_unparse_name(context, princ, &s);
214 krb5_warnx (context,
215 "krb5_425_conv_principal %s.%s@%s "
216 "passed unexpected: %s",
217 t->v4_name, t->v4_inst, t->v4_realm, s);
218 free(s);
219 val = 1;
220 krb5_free_context(context);
221 continue;
225 if (ret) {
226 krb5_free_context(context);
227 continue;
230 if (strcmp (t->v5_realm, princ->realm) != 0) {
231 printf ("wrong realm (\"%s\" should be \"%s\")"
232 " for \"%s.%s@%s\"\n",
233 princ->realm, t->v5_realm,
234 t->v4_name,
235 t->v4_inst,
236 t->v4_realm);
237 val = 1;
240 if (t->ncomponents != princ->name.name_string.len) {
241 printf ("wrong number of components (%u should be %u)"
242 " for \"%s.%s@%s\"\n",
243 princ->name.name_string.len, t->ncomponents,
244 t->v4_name,
245 t->v4_inst,
246 t->v4_realm);
247 val = 1;
248 } else {
249 for (i = 0; i < t->ncomponents; ++i) {
250 if (strcmp(t->comp_val[i],
251 princ->name.name_string.val[i]) != 0) {
252 printf ("bad component %d (\"%s\" should be \"%s\")"
253 " for \"%s.%s@%s\"\n",
255 princ->name.name_string.val[i],
256 t->comp_val[i],
257 t->v4_name,
258 t->v4_inst,
259 t->v4_realm);
260 val = 1;
264 ret = krb5_524_conv_principal (context, princ,
265 name, inst, realm);
266 if (krb5_unparse_name_fixed(context, princ,
267 printable_princ, sizeof(printable_princ)))
268 strlcpy(printable_princ, "unknown principal",
269 sizeof(printable_princ));
270 if (ret) {
271 if (ret != t->ret2) {
272 krb5_warn (context, ret,
273 "krb5_524_conv_principal %s", printable_princ);
274 val = 1;
276 } else {
277 if (t->ret2) {
278 krb5_warnx (context,
279 "krb5_524_conv_principal %s "
280 "passed unexpected", printable_princ);
281 val = 1;
282 krb5_free_context(context);
283 continue;
286 if (ret) {
287 krb5_free_principal (context, princ);
288 krb5_free_context(context);
289 continue;
292 krb5_free_principal (context, princ);
293 krb5_free_context(context);
295 return val;
298 #else /* stub for !KRB4 */
299 int main(int argc, char *argv[]) {
300 return 77;
302 #endif