No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / krb5 / name-45-test.c
blob0c4ffab05c747a9223ecc49cfe98540a57b493f3
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 #include "krb5_locl.h"
34 #include <err.h>
36 __RCSID("$Heimdal: name-45-test.c 19763 2007-01-08 13:35:49Z lha $"
37 "$NetBSD$");
39 enum { MAX_COMPONENTS = 3 };
41 static struct testcase {
42 const char *v4_name;
43 const char *v4_inst;
44 const char *v4_realm;
46 krb5_realm v5_realm;
47 unsigned ncomponents;
48 char *comp_val[MAX_COMPONENTS];
50 const char *config_file;
51 krb5_error_code ret; /* expected error code from 524 */
53 krb5_error_code ret2; /* expected error code from 425 */
54 } tests[] = {
55 {"", "", "", "", 1, {""}, NULL, 0, 0},
56 {"a", "", "", "", 1, {"a"}, NULL, 0, 0},
57 {"a", "b", "", "", 2, {"a", "b"}, NULL, 0, 0},
58 {"a", "b", "c", "c", 2, {"a", "b"}, NULL, 0, 0},
60 {"krbtgt", "FOO.SE", "FOO.SE", "FOO.SE", 2,
61 {"krbtgt", "FOO.SE"}, NULL, 0, 0},
63 {"foo", "bar2", "BAZ", "BAZ", 2,
64 {"foo", "bar2"}, NULL, 0, 0},
65 {"foo", "bar2", "BAZ", "BAZ", 2,
66 {"foo", "bar2"},
67 "[libdefaults]\n"
68 " v4_name_convert = {\n"
69 " host = {\n"
70 " foo = foo5\n"
71 " }\n"
72 "}\n",
73 HEIM_ERR_V4_PRINC_NO_CONV, 0},
74 {"foo", "bar2", "BAZ", "BAZ", 2,
75 {"foo5", "bar2.baz"},
76 "[realms]\n"
77 " BAZ = {\n"
78 " v4_name_convert = {\n"
79 " host = {\n"
80 " foo = foo5\n"
81 " }\n"
82 " }\n"
83 " v4_instance_convert = {\n"
84 " bar2 = bar2.baz\n"
85 " }\n"
86 " }\n",
87 0, 0},
89 {"rcmd", "foo", "realm", "realm", 2, {"host", "foo"}, NULL,
90 HEIM_ERR_V4_PRINC_NO_CONV, 0},
91 {"rcmd", "foo", "realm", "realm", 2, {"host", "foo.realm"},
92 "[realms]\n"
93 " realm = {\n"
94 " v4_instance_convert = {\n"
95 " foo = foo.realm\n"
96 " }\n"
97 " }\n",
98 0, 0},
100 {"pop", "mail0", "NADA.KTH.SE", "NADA.KTH.SE", 2,
101 {"pop", "mail0.nada.kth.se"}, "", HEIM_ERR_V4_PRINC_NO_CONV, 0},
102 {"pop", "mail0", "NADA.KTH.SE", "NADA.KTH.SE", 2,
103 {"pop", "mail0.nada.kth.se"},
104 "[realms]\n"
105 " NADA.KTH.SE = {\n"
106 " default_domain = nada.kth.se\n"
107 " }\n",
108 0, 0},
109 {"pop", "mail0", "NADA.KTH.SE", "NADA.KTH.SE", 2,
110 {"pop", "mail0.nada.kth.se"},
111 "[libdefaults]\n"
112 " v4_instance_resolve = true\n",
113 HEIM_ERR_V4_PRINC_NO_CONV, 0},
115 {"rcmd", "hokkigai", "NADA.KTH.SE", "NADA.KTH.SE", 2,
116 {"host", "hokkigai.pdc.kth.se"}, "", HEIM_ERR_V4_PRINC_NO_CONV, 0},
117 {"rcmd", "hokkigai", "NADA.KTH.SE", "NADA.KTH.SE", 2,
118 {"host", "hokkigai.pdc.kth.se"},
119 "[libdefaults]\n"
120 " v4_instance_resolve = true\n"
121 "[realms]\n"
122 " NADA.KTH.SE = {\n"
123 " v4_name_convert = {\n"
124 " host = {\n"
125 " rcmd = host\n"
126 " }\n"
127 " }\n"
128 " default_domain = pdc.kth.se\n"
129 " }\n",
130 0, 0},
132 {"0123456789012345678901234567890123456789",
133 "0123456789012345678901234567890123456789",
134 "0123456789012345678901234567890123456789",
135 "0123456789012345678901234567890123456789",
136 2, {"0123456789012345678901234567890123456789",
137 "0123456789012345678901234567890123456789"}, NULL,
138 0, KRB5_PARSE_MALFORMED},
140 {"012345678901234567890123456789012345678",
141 "012345678901234567890123456789012345678",
142 "012345678901234567890123456789012345678",
143 "012345678901234567890123456789012345678",
144 2, {"012345678901234567890123456789012345678",
145 "012345678901234567890123456789012345678"}, NULL,
146 0, 0},
148 {NULL, NULL, NULL, NULL, 0, {NULL}, NULL, 0}
152 main(int argc, char **argv)
154 struct testcase *t;
155 krb5_context context;
156 krb5_error_code ret;
157 char hostname[1024];
158 int val = 0;
160 setprogname(argv[0]);
162 gethostname(hostname, sizeof(hostname));
163 if (!(strstr(hostname, "kth.se") != NULL || strstr(hostname, "su.se") != NULL))
164 return 0;
166 for (t = tests; t->v4_name; ++t) {
167 krb5_principal princ;
168 int i;
169 char name[40], inst[40], realm[40];
170 char printable_princ[256];
172 ret = krb5_init_context (&context);
173 if (ret)
174 errx (1, "krb5_init_context failed: %d", ret);
176 if (t->config_file != NULL) {
177 char template[] = "/tmp/krb5-conf-XXXXXX";
178 int fd = mkstemp(template);
179 char *files[2];
181 if (fd < 0)
182 krb5_err (context, 1, errno, "mkstemp %s", template);
184 if (write (fd, t->config_file, strlen(t->config_file))
185 != strlen(t->config_file))
186 krb5_err (context, 1, errno, "write %s", template);
187 close (fd);
188 files[0] = template;
189 files[1] = NULL;
191 ret = krb5_set_config_files (context, files);
192 unlink (template);
193 if (ret)
194 krb5_err (context, 1, ret, "krb5_set_config_files");
197 ret = krb5_425_conv_principal (context,
198 t->v4_name,
199 t->v4_inst,
200 t->v4_realm,
201 &princ);
202 if (ret) {
203 if (ret != t->ret) {
204 krb5_warn (context, ret,
205 "krb5_425_conv_principal %s.%s@%s",
206 t->v4_name, t->v4_inst, t->v4_realm);
207 val = 1;
209 } else {
210 if (t->ret) {
211 char *s;
212 krb5_unparse_name(context, princ, &s);
213 krb5_warnx (context,
214 "krb5_425_conv_principal %s.%s@%s "
215 "passed unexpected: %s",
216 t->v4_name, t->v4_inst, t->v4_realm, s);
217 free(s);
218 val = 1;
219 krb5_free_context(context);
220 continue;
224 if (ret) {
225 krb5_free_context(context);
226 continue;
229 if (strcmp (t->v5_realm, princ->realm) != 0) {
230 printf ("wrong realm (\"%s\" should be \"%s\")"
231 " for \"%s.%s@%s\"\n",
232 princ->realm, t->v5_realm,
233 t->v4_name,
234 t->v4_inst,
235 t->v4_realm);
236 val = 1;
239 if (t->ncomponents != princ->name.name_string.len) {
240 printf ("wrong number of components (%u should be %u)"
241 " for \"%s.%s@%s\"\n",
242 princ->name.name_string.len, t->ncomponents,
243 t->v4_name,
244 t->v4_inst,
245 t->v4_realm);
246 val = 1;
247 } else {
248 for (i = 0; i < t->ncomponents; ++i) {
249 if (strcmp(t->comp_val[i],
250 princ->name.name_string.val[i]) != 0) {
251 printf ("bad component %d (\"%s\" should be \"%s\")"
252 " for \"%s.%s@%s\"\n",
254 princ->name.name_string.val[i],
255 t->comp_val[i],
256 t->v4_name,
257 t->v4_inst,
258 t->v4_realm);
259 val = 1;
263 ret = krb5_524_conv_principal (context, princ,
264 name, inst, realm);
265 if (krb5_unparse_name_fixed(context, princ,
266 printable_princ, sizeof(printable_princ)))
267 strlcpy(printable_princ, "unknown principal",
268 sizeof(printable_princ));
269 if (ret) {
270 if (ret != t->ret2) {
271 krb5_warn (context, ret,
272 "krb5_524_conv_principal %s", printable_princ);
273 val = 1;
275 } else {
276 if (t->ret2) {
277 krb5_warnx (context,
278 "krb5_524_conv_principal %s "
279 "passed unexpected", printable_princ);
280 val = 1;
281 krb5_free_context(context);
282 continue;
285 if (ret) {
286 krb5_free_principal (context, princ);
287 krb5_free_context(context);
288 continue;
291 krb5_free_principal (context, princ);
292 krb5_free_context(context);
294 return val;