2 * Copyright 2010, Sine Nomine Associates and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
17 #include <afs/cellconfig.h>
19 #include <tests/tap/basic.h>
22 extern int _afsconf_Touch(struct afsconf_dir *adir);
39 struct testcase testset0[] = {
40 {"jdoe", NULL, NULL, LOCAL},
41 {"jdoe", NULL, "example.org", LOCAL},
42 {"jdoe", NULL, "EXAMPLE.ORG", LOCAL},
43 {"jdoe", NULL, NULL, LOCAL},
44 {"jdoe", NULL, "my.realm.org", FOREIGN},
45 {"jdoe", NULL, "MY.REALM.ORG", FOREIGN},
46 {"jdoe", NULL, "MY.OTHER.REALM.ORG", FOREIGN},
47 {"jdoe", "admin", NULL, LOCAL},
48 {"jdoe", "admin", "my.realm.org", FOREIGN},
49 {"jdoe", "admin", "MY.REALM.ORG", FOREIGN},
50 {"jdoe", "admin", "your.realm.org", FOREIGN},
51 {"jdoe", "admin", "YOUR.REALM.ORG", FOREIGN},
52 {"admin", NULL, "example.org", LOCAL},
53 {"admin", NULL, "my.realm.org", FOREIGN},
54 {"admin", NULL, "MY.REALM.ORG", FOREIGN},
55 {"admin", NULL, "MY.OTHER.REALM.ORG", FOREIGN},
62 * local realms: MY.REALM.ORG, MY.OTHER.REALM.ORG
64 struct testcase testset1[] = {
65 {"jdoe", NULL, NULL, LOCAL},
66 {"jdoe", NULL, "example.org", LOCAL},
67 {"jdoe", NULL, "EXAMPLE.ORG", LOCAL},
68 {"jdoe", NULL, NULL, LOCAL},
69 {"jdoe", NULL, "my.realm.org", LOCAL},
70 {"jdoe", NULL, "MY.REALM.ORG", LOCAL},
71 {"jdoe", NULL, "MY.OTHER.REALM.ORG", LOCAL},
72 {"jdoe", NULL, "SOME.REALM.ORG", FOREIGN},
73 {"jdoe", "admin", NULL, LOCAL},
74 {"jdoe", "admin", "my.realm.org", LOCAL},
75 {"jdoe", "admin", "MY.REALM.ORG", LOCAL},
76 {"jdoe", "admin", "MY.OTHER.REALM.ORG", LOCAL},
77 {"jdoe", "admin", "your.realm.org", FOREIGN},
78 {"jdoe", "admin", "YOUR.REALM.ORG", FOREIGN},
79 {"admin", NULL, "example.org", LOCAL},
80 {"admin", NULL, "my.realm.org", LOCAL},
81 {"admin", NULL, "MY.REALM.ORG", LOCAL},
82 {"admin", NULL, "MY.OTHER.REALM.ORG", LOCAL},
89 * local realms: MY.REALM.ORG, MY.OTHER.REALM.ORG
90 * exclude: admin@MY.REALM.ORG
92 struct testcase testset2[] = {
93 {"jdoe", NULL, NULL, LOCAL},
94 {"jdoe", NULL, "example.org", LOCAL},
95 {"jdoe", NULL, "EXAMPLE.ORG", LOCAL},
96 {"jdoe", NULL, NULL, LOCAL},
97 {"jdoe", NULL, "my.realm.org", LOCAL},
98 {"jdoe", NULL, "MY.REALM.ORG", LOCAL},
99 {"jdoe", NULL, "MY.OTHER.REALM.ORG", LOCAL},
100 {"jdoe", "admin", NULL, LOCAL},
101 {"jdoe", "admin", "my.realm.org", LOCAL},
102 {"jdoe", "admin", "MY.REALM.ORG", LOCAL},
103 {"jdoe", "admin", "MY.OTHER.REALM.ORG", LOCAL},
104 {"jdoe", "admin", "your.realm.org", FOREIGN},
105 {"jdoe", "admin", "YOUR.REALM.ORG", FOREIGN},
106 {"admin", NULL, "example.org", LOCAL},
107 {"admin", NULL, "my.realm.org", LOCAL},
108 {"admin", NULL, "MY.REALM.ORG", FOREIGN},
109 {"admin", NULL, "MY.OTHER.REALM.ORG", LOCAL},
113 struct testcase* testset[] = { testset0, testset1, testset2 };
118 char *s = malloc(len + 1);
120 fprintf(stderr, "Failed to allocate string buffer.\n");
129 run_tests(struct afsconf_dir *dir, int setnum, char *setname)
134 for (t = testset[setnum]; t->name; t++) {
135 afs_int32 local = -1;
137 code = afsconf_IsLocalRealmMatch(dir, &local, t->name, t->inst, t->cell);
138 ok(code == 0, "%s: test case %s/%s/%s",
140 t->name ? t->name : "(null)",
141 t->inst ? t->inst : "(null)",
142 t->cell ? t->cell : "(null)");
144 ok(local == t->expectedLocal, "... expected %d, got %d", t->expectedLocal, local);
150 run_edge_tests(struct afsconf_dir *dir)
152 afs_int32 local = -1;
158 /* null argument checks */
159 code = afsconf_IsLocalRealmMatch(dir, &local, NULL, inst, cell);
160 ok(code == EINVAL, "null name: code=%d", code);
162 code = afsconf_IsLocalRealmMatch(dir, &local, name, NULL, cell);
163 ok(code == 0, "null inst: code=%d", code);
165 code = afsconf_IsLocalRealmMatch(dir, &local, name, inst, NULL);
166 ok(code == 0, "null cell: code=%d", code);
168 /* large ticket test */
169 name = make_string(64);
170 inst = make_string(64);
171 cell = make_string(64);
172 code = afsconf_IsLocalRealmMatch(dir, &local, name, inst, cell);
173 ok(code == 0, "name size 64: code=%d", code);
178 name = make_string(255);
180 cell = "my.realm.org";
181 code = afsconf_IsLocalRealmMatch(dir, &local, name, inst, cell);
182 ok(code == 0, "name size 255: code=%d", code);
187 write_krb_conf(char *dirname, char *data)
189 char *filename = NULL;
192 asnprintf(&filename, 256, "%s/%s", dirname, "krb.conf");
193 if ((fp = fopen(filename, "w")) == NULL) {
194 fprintf(stderr, "Unable to create test file %s\n", filename);
198 diag("writing to %s: %s", filename, data);
200 fprintf(fp, "%s\n", data);
206 write_krb_excl(char *dirname)
208 char *filename = NULL;
212 asnprintf(&filename, 256, "%s/%s", dirname, "krb.excl");
213 if ((fp = fopen(filename, "w")) == NULL) {
214 fprintf(stderr, "Unable to create test file %s\n", filename);
217 data = "admin@MY.REALM.ORG";
219 diag("writing to %s: %s", filename, data);
221 fprintf(fp, "%s\n", data);
222 data = "admin@EXAMPLE.ORG";
224 diag("writing to %s: %s", filename, data);
226 fprintf(fp, "%s\n", data);
232 update_csdb(char *dirname)
234 char *filename = NULL;
237 asnprintf(&filename, 256, "%s/%s", dirname, "CellServDB");
238 if ((fp = fopen(filename, "a")) == NULL) {
239 fprintf(stderr, "Unable to create test file %s\n", filename);
242 fprintf(fp, "10.0.0.1 #bogus.example.org\n");
250 struct afsconf_dir *dir;
253 /* run edge case tests */
254 dirname = afstest_BuildTestConfig();
255 dir = afsconf_Open(dirname);
257 fprintf(stderr, "Unable to configure directory.\n");
261 afstest_UnlinkTestConfig(dirname);
265 test_no_config_files(void)
267 struct afsconf_dir *dir;
270 /* run tests without config files */
271 dirname = afstest_BuildTestConfig();
272 dir = afsconf_Open(dirname);
274 fprintf(stderr, "Unable to configure directory.\n");
277 run_tests(dir, 0, "no config");
278 afstest_UnlinkTestConfig(dirname);
282 test_with_config_files(void)
284 struct afsconf_dir *dir;
287 /* run tests with config files */
288 dirname = afstest_BuildTestConfig();
289 write_krb_conf(dirname, "MY.REALM.ORG MY.OTHER.REALM.ORG");
290 write_krb_excl(dirname);
291 dir = afsconf_Open(dirname);
293 fprintf(stderr, "Unable to configure directory.\n");
296 run_tests(dir, 2, "config");
297 afstest_UnlinkTestConfig(dirname);
301 test_set_local_realms(void)
303 struct afsconf_dir *dir;
306 /* Simulate command line -realm option; overrides config file, if one.
307 * Multiple realms can be added. */
308 ok(afsconf_SetLocalRealm("MY.REALM.ORG") == 0, "set local realm MY.REALM.ORG");
309 ok(afsconf_SetLocalRealm("MY.OTHER.REALM.ORG") == 0, "set local realm MY.OTHER.REALM.ORG");
311 /* run tests without config files */
312 dirname = afstest_BuildTestConfig();
313 dir = afsconf_Open(dirname);
315 fprintf(stderr, "Unable to configure directory.\n");
318 write_krb_conf(dirname, "SOME.REALM.ORG");
319 run_tests(dir, 1, "set realm test");
320 afstest_UnlinkTestConfig(dirname);
324 test_update_config_files(void)
327 struct afsconf_dir *dir;
329 afs_int32 local = -1;
331 dirname = afstest_BuildTestConfig();
332 write_krb_conf(dirname, "SOME.REALM.ORG");
333 dir = afsconf_Open(dirname);
335 fprintf(stderr, "Unable to configure directory.\n");
339 code = afsconf_IsLocalRealmMatch(dir, &local, "jdoe", NULL, "SOME.REALM.ORG");
340 ok(code == 0 && local == 1, "before update: jdoe@SOME.REALM.ORG");
342 code = afsconf_IsLocalRealmMatch(dir, &local, "jdoe", NULL, "MY.REALM.ORG");
343 ok(code == 0 && local == 0, "before update: admin@MY.REALM.ORG");
345 write_krb_conf(dirname, "MY.REALM.ORG MY.OTHER.REALM.ORG");
346 write_krb_excl(dirname);
347 update_csdb(dirname);
348 _afsconf_Touch(dir); /* forces reopen */
350 code = afsconf_IsLocalRealmMatch(dir, &local, "jdoe", NULL, "MY.REALM.ORG");
351 ok(code == 0 && local == 1, "after update: jdoe@MY.REALM.ORG");
353 code = afsconf_IsLocalRealmMatch(dir, &local, "admin", NULL, "MY.REALM.ORG");
354 ok(code == 0 && local == 0, "after update: admin@MY.REALM.ORG");
356 afstest_UnlinkTestConfig(dirname);
360 main(int argc, char **argv)
362 afstest_SkipTestsIfBadHostname();
367 test_no_config_files();
368 test_with_config_files();
369 test_update_config_files();
370 test_set_local_realms(); /* must be the last test */