41afd9945465d28dc2d8ac05baf933ba09c87275
[xfstests-dev.git] / src / acl_test.c
1 /*
2  * Copyright (c) 2001 Silicon Graphics, Inc.  All Rights Reserved.
3  * 
4  * This prog is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  * 
8  * This prog is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * 
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this prog with
17  * other software, or any other product whatsoever.
18  * 
19  * You should have received a copy of the GNU General Public License along
20  * with this prog; if not, write the Free Software Foundation, Inc., 59
21  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22  * 
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  * 
26  * http://www.sgi.com 
27  * 
28  * For further information regarding this notice, see: 
29  * 
30  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31  */
32
33 /*
34  * Test our various libacl functions.
35  * Use IRIX semantics or Linux semantics if pertinent.
36  */
37  
38 #include "global.h"
39
40 #include <errno.h>
41 #include <acl.h>
42
43 char *prog;
44 int irixsemantics = 0;
45
46 void usage(void)
47 {
48     fprintf(stderr, "usage: %s\n"
49            "    -i - use irix semantics\n" 
50            ,prog);
51            
52 }
53
54 void
55 print_err(char *msg)
56 {
57     printf("%s: %s: %s\n", prog, msg, strerror(errno));
58 }
59
60 void
61 dump_ace(acl_entry_t ace)
62 {
63     printf("<tag:%d,id:%d,perm:%u>", 
64             ace->ae_tag, ace->ae_id, ace->ae_perm);
65 }
66
67 void
68 dump_acl(acl_t acl)
69 {
70     int i;
71     printf("ACL[n=%d]: ", acl->acl_cnt);
72     for (i=0;i<acl->acl_cnt;i++) {
73         acl_entry_t ace = &acl->acl_entry[i];
74         printf("%d: ", i);
75         dump_ace(ace);
76         printf(" ");
77     }
78     printf("\n");
79
80 }
81
82 void
83 dump_acl_by_entry(acl_t acl)
84 {
85     int sts, i;
86     acl_entry_t ace; 
87
88     printf("Get 1st entry on filled ACL\n");
89     sts = acl_get_entry(acl, ACL_FIRST_ENTRY, &ace);
90     printf("acl_get_entry -> %d\n", sts);
91     if (sts > 0) {
92         printf("1: "); dump_ace(ace); printf("\n");
93     }
94
95     for(i=2;i<=acl->acl_cnt+2;i++) {
96         printf("Get %dth entry on filled ACL\n", i);
97         sts = acl_get_entry(acl, ACL_NEXT_ENTRY, &ace);
98         printf("acl_get_entry -> %d\n", sts);
99         if (sts > 0) {
100             printf("%d: ",i); dump_ace(ace); printf("\n");
101         }
102     }
103 }
104
105 /*
106  * create a full acl with entries with known bogus values
107  */
108 acl_t
109 create_filled_acl(void)
110 {
111     acl_t acl;
112     int i;
113
114     acl = acl_init(ACL_MAX_ENTRIES);    
115
116     for(i=0;i<ACL_MAX_ENTRIES;i++) {
117         acl_entry_t ace = &acl->acl_entry[i];
118         ace->ae_tag = i;
119         ace->ae_id = i+1;
120         ace->ae_perm = i+2;
121         acl->acl_cnt++;
122     }
123     return acl;
124 }
125
126 int
127 main(int argc, char **argv)
128 {
129         int c, i;
130         acl_t acl1, acl2, acl3;
131         acl_entry_t ace1;
132
133         prog = basename(argv[0]);
134
135         while ((c = getopt(argc, argv, "i")) != -1) {
136                 switch (c) {
137                 case 'i':
138                         irixsemantics = 1;
139                         break;
140                 case '?':
141                         usage();
142                         return 1;
143                 }
144         }
145
146         if (irixsemantics) {
147             acl_set_compat(ACL_COMPAT_IRIXGET);
148         }
149
150         /* ---------------------------------------------- */
151         printf("*** test out creating an ACL ***\n");
152
153         printf("Test acl_init(ACL_MAX_ENTRIES+1)\n");
154         acl1 = acl_init(ACL_MAX_ENTRIES+1);
155         if (acl1 == NULL) {
156             print_err("acl_init(max+1)");
157         }
158         printf("Test acl_init(-1)\n");
159         acl1 = acl_init(-1);
160         if (acl1 == NULL) {
161             print_err("acl_init(-1)");
162         }
163         printf("Test acl_init(0)\n");
164         acl1 = acl_init(0);
165         if (acl1 == NULL) {
166             print_err("acl_init(0)");
167         }
168
169         printf("Test acl_create_entry(NULL, ...)\n");
170         if (acl_create_entry(NULL, &ace1) == -1) {
171             print_err("acl_create_entry(NULL,ace1)");
172         }
173         printf("Test acl_create_entry(..., NULL)\n");
174         if (acl_create_entry(&acl1, NULL) == -1) {
175             print_err("acl_create_entry(NULL,ace1)");
176         }
177         printf("Test acl_create_entry(acl1, ace1)\n");
178         acl1 = NULL;
179         if (acl_create_entry(&acl1, &ace1) == -1) {
180             print_err("acl_create_entry(*null,ace1)");
181         }
182
183         acl_free(acl1);
184         acl1 = acl_init(0);
185         for (i=0;i<=ACL_MAX_ENTRIES+1;i++) {
186             printf("%d: creating ace\n", i);
187             if (acl_create_entry(&acl1, &ace1) == -1) {
188                 print_err("acl_create_entry");
189             }
190             dump_acl(acl1);
191         }
192
193         /* ---------------------------------------------- */
194         printf("*** test out getting ACEs ***\n");
195
196         dump_acl_by_entry(acl1);
197
198         printf("dump empty ACL\n");
199         acl2 = acl_init(0);
200         if (acl2 == NULL) {
201             print_err("acl_init(0)");
202         }
203         dump_acl_by_entry(acl2);
204
205         printf("fill an ACL with known bogus values\n");
206         acl3 = create_filled_acl();     
207         dump_acl_by_entry(acl3);
208
209         /* ---------------------------------------------- */
210         printf("*** test out ACL to text for empty ACL***\n");
211         {
212             char *text;
213             ssize_t len;
214             acl_t empty_acl = acl_init(0);
215             text = acl_to_text(empty_acl, NULL); 
216             printf("acl_to_text(empty_acl,NULL) -> \"%s\"\n", text); 
217             text = acl_to_text(empty_acl, &len); 
218             printf("acl_to_text(empty_acl,NULL) -> \"%s\", len = %u\n", text, len); 
219             text = acl_to_text(NULL, NULL); 
220             printf("acl_to_text(NULL,NULL) -> \"%s\"\n", text==NULL?"NULL":text); 
221         }
222         /* NOTE: Other tests will test out the text for ACLs with ACEs.
223          *       So don't have to test it here.
224          *       It is simplest to choose ids not in /etc/passwd /etc/group
225          *       which is done already in a script. 
226          */
227
228         return 0;
229 }