xref: /minix3/crypto/external/bsd/heimdal/dist/lib/krb5/test_acl.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: test_acl.c,v 1.1.1.2 2014/04/24 12:45:51 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 2004 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc  * All rights reserved.
7ebfedea0SLionel Sambuc  *
8ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc  * are met:
11ebfedea0SLionel Sambuc  *
12ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc  *
15ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc  *
19ebfedea0SLionel Sambuc  * 3. Neither the name of KTH nor the names of its contributors may be
20ebfedea0SLionel Sambuc  *    used to endorse or promote products derived from this software without
21ebfedea0SLionel Sambuc  *    specific prior written permission.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
24ebfedea0SLionel Sambuc  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26ebfedea0SLionel Sambuc  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
27ebfedea0SLionel Sambuc  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28ebfedea0SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29ebfedea0SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30ebfedea0SLionel Sambuc  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31ebfedea0SLionel Sambuc  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32ebfedea0SLionel Sambuc  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33ebfedea0SLionel Sambuc  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
34ebfedea0SLionel Sambuc 
35ebfedea0SLionel Sambuc #include "krb5_locl.h"
36ebfedea0SLionel Sambuc #include <err.h>
37ebfedea0SLionel Sambuc 
38ebfedea0SLionel Sambuc #define RETVAL(c, r, e, s) \
39ebfedea0SLionel Sambuc 	do { if (r != e) krb5_errx(c, 1, "%s", s); } while (0)
40ebfedea0SLionel Sambuc #define STRINGMATCH(c, s, _s1, _s2) \
41ebfedea0SLionel Sambuc 	do {							\
42ebfedea0SLionel Sambuc 		if (_s1 == NULL || _s2 == NULL) 		\
43ebfedea0SLionel Sambuc 			krb5_errx(c, 1, "s1 or s2 is NULL");	\
44ebfedea0SLionel Sambuc 		if (strcmp(_s1,_s2) != 0) 			\
45ebfedea0SLionel Sambuc 			krb5_errx(c, 1, "%s", s);		\
46ebfedea0SLionel Sambuc 	} while (0)
47ebfedea0SLionel Sambuc 
48ebfedea0SLionel Sambuc static void
test_match_string(krb5_context context)49ebfedea0SLionel Sambuc test_match_string(krb5_context context)
50ebfedea0SLionel Sambuc {
51ebfedea0SLionel Sambuc     krb5_error_code ret;
52ebfedea0SLionel Sambuc     char *s1, *s2;
53ebfedea0SLionel Sambuc 
54ebfedea0SLionel Sambuc     ret = krb5_acl_match_string(context, "foo", "s", "foo");
55ebfedea0SLionel Sambuc     RETVAL(context, ret, 0, "single s");
56ebfedea0SLionel Sambuc     ret = krb5_acl_match_string(context, "foo foo", "s", "foo");
57ebfedea0SLionel Sambuc     RETVAL(context, ret, EACCES, "too many strings");
58ebfedea0SLionel Sambuc     ret = krb5_acl_match_string(context, "foo bar", "ss", "foo", "bar");
59ebfedea0SLionel Sambuc     RETVAL(context, ret, 0, "two strings");
60ebfedea0SLionel Sambuc     ret = krb5_acl_match_string(context, "foo  bar", "ss", "foo", "bar");
61ebfedea0SLionel Sambuc     RETVAL(context, ret, 0, "two strings double space");
62ebfedea0SLionel Sambuc     ret = krb5_acl_match_string(context, "foo \tbar", "ss", "foo", "bar");
63ebfedea0SLionel Sambuc     RETVAL(context, ret, 0, "two strings space + tab");
64ebfedea0SLionel Sambuc     ret = krb5_acl_match_string(context, "foo", "ss", "foo", "bar");
65ebfedea0SLionel Sambuc     RETVAL(context, ret, EACCES, "one string, two format strings");
66ebfedea0SLionel Sambuc     ret = krb5_acl_match_string(context, "foo", "ss", "foo", "foo");
67ebfedea0SLionel Sambuc     RETVAL(context, ret, EACCES, "one string, two format strings (same)");
68ebfedea0SLionel Sambuc     ret = krb5_acl_match_string(context, "foo  \t", "s", "foo");
69ebfedea0SLionel Sambuc     RETVAL(context, ret, 0, "ending space");
70ebfedea0SLionel Sambuc 
71ebfedea0SLionel Sambuc     ret = krb5_acl_match_string(context, "foo/bar", "f", "foo/bar");
72ebfedea0SLionel Sambuc     RETVAL(context, ret, 0, "liternal fnmatch");
73ebfedea0SLionel Sambuc     ret = krb5_acl_match_string(context, "foo/bar", "f", "foo/*");
74ebfedea0SLionel Sambuc     RETVAL(context, ret, 0, "foo/*");
75ebfedea0SLionel Sambuc     ret = krb5_acl_match_string(context, "foo/bar.example.org", "f",
76ebfedea0SLionel Sambuc 				"foo/*.example.org");
77ebfedea0SLionel Sambuc     RETVAL(context, ret, 0, "foo/*.example.org");
78ebfedea0SLionel Sambuc     ret = krb5_acl_match_string(context, "foo/bar.example.com", "f",
79ebfedea0SLionel Sambuc 				"foo/*.example.org");
80ebfedea0SLionel Sambuc     RETVAL(context, ret, EACCES, "foo/*.example.com");
81ebfedea0SLionel Sambuc 
82ebfedea0SLionel Sambuc     ret = krb5_acl_match_string(context, "foo/bar/baz", "f", "foo/*/baz");
83ebfedea0SLionel Sambuc     RETVAL(context, ret, 0, "foo/*/baz");
84ebfedea0SLionel Sambuc 
85ebfedea0SLionel Sambuc     ret = krb5_acl_match_string(context, "foo", "r", &s1);
86ebfedea0SLionel Sambuc     RETVAL(context, ret, 0, "ret 1");
87ebfedea0SLionel Sambuc     STRINGMATCH(context, "ret 1 match", s1, "foo"); free(s1);
88ebfedea0SLionel Sambuc 
89ebfedea0SLionel Sambuc     ret = krb5_acl_match_string(context, "foo bar", "rr", &s1, &s2);
90ebfedea0SLionel Sambuc     RETVAL(context, ret, 0, "ret 2");
91ebfedea0SLionel Sambuc     STRINGMATCH(context, "ret 2 match 1", s1, "foo"); free(s1);
92ebfedea0SLionel Sambuc     STRINGMATCH(context, "ret 2 match 2", s2, "bar"); free(s2);
93ebfedea0SLionel Sambuc 
94ebfedea0SLionel Sambuc     ret = krb5_acl_match_string(context, "foo bar", "sr", "bar", &s1);
95ebfedea0SLionel Sambuc     RETVAL(context, ret, EACCES, "ret mismatch");
96ebfedea0SLionel Sambuc     if (s1 != NULL) krb5_errx(context, 1, "s1 not NULL");
97ebfedea0SLionel Sambuc 
98ebfedea0SLionel Sambuc     ret = krb5_acl_match_string(context, "foo", "l", "foo");
99ebfedea0SLionel Sambuc     RETVAL(context, ret, EINVAL, "unknown letter");
100ebfedea0SLionel Sambuc }
101ebfedea0SLionel Sambuc 
102ebfedea0SLionel Sambuc 
103ebfedea0SLionel Sambuc int
main(int argc,char ** argv)104ebfedea0SLionel Sambuc main(int argc, char **argv)
105ebfedea0SLionel Sambuc {
106ebfedea0SLionel Sambuc     krb5_context context;
107ebfedea0SLionel Sambuc     krb5_error_code ret;
108ebfedea0SLionel Sambuc 
109ebfedea0SLionel Sambuc     setprogname(argv[0]);
110ebfedea0SLionel Sambuc 
111ebfedea0SLionel Sambuc     ret = krb5_init_context(&context);
112ebfedea0SLionel Sambuc     if (ret)
113ebfedea0SLionel Sambuc 	errx (1, "krb5_init_context failed: %d", ret);
114ebfedea0SLionel Sambuc 
115ebfedea0SLionel Sambuc     test_match_string(context);
116ebfedea0SLionel Sambuc 
117ebfedea0SLionel Sambuc     krb5_free_context(context);
118ebfedea0SLionel Sambuc 
119ebfedea0SLionel Sambuc     return 0;
120ebfedea0SLionel Sambuc }
121