fsx: Add fallocate collapse range operation
[xfstests-dev.git] / src / acl_test.c
1 /*
2  * Copyright (c) 2001 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 /*
20  * Test our various libacl functions.
21  * Use IRIX semantics or Linux semantics if pertinent.
22  */
23  
24 #include <errno.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <getopt.h>
28 #include <sys/acl.h>
29
30 char *prog;
31 int irixsemantics = 0;
32
33 void usage(void)
34 {
35     fprintf(stderr, "usage: %s\n"
36            "    -i - use irix semantics\n" 
37            ,prog);
38            
39 }
40
41 void
42 print_err(char *msg)
43 {
44     printf("%s: %s: %s\n", prog, msg, strerror(errno));
45 }
46
47 void
48 dump_ace(acl_entry_t ace)
49 {
50     printf("<tag:%d,id:%d,perm:%u>", 
51             ace->ae_tag, ace->ae_id, ace->ae_perm);
52 }
53
54 void
55 dump_acl(acl_t acl)
56 {
57     int i;
58     printf("ACL[n=%d]: ", acl->acl_cnt);
59     for (i=0;i<acl->acl_cnt;i++) {
60         acl_entry_t ace = &acl->acl_entry[i];
61         printf("%d: ", i);
62         dump_ace(ace);
63         printf(" ");
64     }
65     printf("\n");
66
67 }
68
69 void
70 dump_acl_by_entry(acl_t acl)
71 {
72     int sts, i;
73     acl_entry_t ace; 
74
75     printf("Get 1st entry on filled ACL\n");
76     sts = acl_get_entry(acl, ACL_FIRST_ENTRY, &ace);
77     printf("acl_get_entry -> %d\n", sts);
78     if (sts > 0) {
79         printf("1: "); dump_ace(ace); printf("\n");
80     }
81
82     for(i=2;i<=acl->acl_cnt+2;i++) {
83         printf("Get %dth entry on filled ACL\n", i);
84         sts = acl_get_entry(acl, ACL_NEXT_ENTRY, &ace);
85         printf("acl_get_entry -> %d\n", sts);
86         if (sts > 0) {
87             printf("%d: ",i); dump_ace(ace); printf("\n");
88         }
89     }
90 }
91
92 /*
93  * create a full acl with entries with known bogus values
94  */
95 acl_t
96 create_filled_acl(void)
97 {
98     acl_t acl;
99     int i;
100
101     acl = acl_init(ACL_MAX_ENTRIES);    
102
103     for(i=0;i<ACL_MAX_ENTRIES;i++) {
104         acl_entry_t ace = &acl->acl_entry[i];
105         ace->ae_tag = i;
106         ace->ae_id = i+1;
107         ace->ae_perm = i+2;
108         acl->acl_cnt++;
109     }
110     return acl;
111 }
112
113 void
114 test_acl_get_qualifier(void)
115 {
116     struct acl_entry ace;
117     uid_t *uidp;
118
119     printf("*** test out acl_get_qualifier ***\n");
120
121     /* simple ace */
122     ace.ae_tag = ACL_USER;
123     ace.ae_id = 1;
124     ace.ae_perm = 1;
125
126     /* make sure we can get uid and free it */
127     uidp = acl_get_qualifier(&ace); 
128     printf("uid = %d\n", *uidp);
129     acl_free(uidp);
130
131     /* change to another valid tag with a qualifier */
132     ace.ae_tag = ACL_GROUP;
133     uidp = acl_get_qualifier(&ace); 
134     printf("uid = %d\n", *uidp);
135     acl_free(uidp);
136
137     /* let's get some errors */
138
139     ace.ae_tag = ACL_USER_OBJ;
140     uidp = acl_get_qualifier(&ace); 
141     if (uidp == NULL)
142         printf("uidp is NULL: %s\n", strerror(errno));
143     else
144         printf("Error: uidp is NOT NULL\n");
145
146     uidp = acl_get_qualifier(NULL); 
147     if (uidp == NULL)
148         printf("uidp is NULL: %s\n", strerror(errno));
149     else
150         printf("Error: uidp is NOT NULL\n");
151 }
152
153 int
154 main(int argc, char **argv)
155 {
156         int c, i;
157         acl_t acl1, acl2, acl3;
158         acl_entry_t ace1;
159         char *p;
160
161         prog = argv[0];
162         for (p = prog; *p; p++) {
163                 if (*p == '/') {
164                         prog = p + 1;
165                 }
166         }
167
168         while ((c = getopt(argc, argv, "i")) != -1) {
169                 switch (c) {
170                 case 'i':
171                         irixsemantics = 1;
172                         break;
173                 case '?':
174                         usage();
175                         return 1;
176                 }
177         }
178
179         if (irixsemantics) {
180             acl_set_compat(ACL_COMPAT_IRIXGET);
181         }
182
183         /* ---------------------------------------------- */
184         printf("*** test out creating an ACL ***\n");
185
186         printf("Test acl_init(ACL_MAX_ENTRIES+1)\n");
187         acl1 = acl_init(ACL_MAX_ENTRIES+1);
188         if (acl1 == NULL) {
189             print_err("acl_init(max+1)");
190         }
191         printf("Test acl_init(-1)\n");
192         acl1 = acl_init(-1);
193         if (acl1 == NULL) {
194             print_err("acl_init(-1)");
195         }
196         printf("Test acl_init(0)\n");
197         acl1 = acl_init(0);
198         if (acl1 == NULL) {
199             print_err("acl_init(0)");
200         }
201
202         printf("Test acl_create_entry(NULL, ...)\n");
203         if (acl_create_entry(NULL, &ace1) == -1) {
204             print_err("acl_create_entry(NULL,ace1)");
205         }
206         printf("Test acl_create_entry(..., NULL)\n");
207         if (acl_create_entry(&acl1, NULL) == -1) {
208             print_err("acl_create_entry(NULL,ace1)");
209         }
210         printf("Test acl_create_entry(acl1, ace1)\n");
211         acl1 = NULL;
212         if (acl_create_entry(&acl1, &ace1) == -1) {
213             print_err("acl_create_entry(*null,ace1)");
214         }
215
216         acl_free(acl1);
217         acl1 = acl_init(0);
218         for (i=0;i<=ACL_MAX_ENTRIES+1;i++) {
219             printf("%d: creating ace\n", i);
220             if (acl_create_entry(&acl1, &ace1) == -1) {
221                 print_err("acl_create_entry");
222             }
223             dump_acl(acl1);
224         }
225
226         /* ---------------------------------------------- */
227         printf("*** test out getting ACEs ***\n");
228
229         dump_acl_by_entry(acl1);
230
231         printf("dump empty ACL\n");
232         acl2 = acl_init(0);
233         if (acl2 == NULL) {
234             print_err("acl_init(0)");
235         }
236         dump_acl_by_entry(acl2);
237
238         printf("fill an ACL with known bogus values\n");
239         acl3 = create_filled_acl();     
240         dump_acl_by_entry(acl3);
241
242         /* ---------------------------------------------- */
243         printf("*** test out ACL to text for empty ACL***\n");
244         {
245             char *text;
246             ssize_t len;
247             acl_t empty_acl = acl_init(0);
248             text = acl_to_text(empty_acl, NULL); 
249             printf("acl_to_text(empty_acl,NULL) -> \"%s\"\n", text); 
250             text = acl_to_text(empty_acl, &len); 
251             printf("acl_to_text(empty_acl,NULL) -> \"%s\", len = %u\n", text, len); 
252             text = acl_to_text(NULL, NULL); 
253             printf("acl_to_text(NULL,NULL) -> \"%s\"\n", text==NULL?"NULL":text); 
254         }
255         /* NOTE: Other tests will test out the text for ACLs with ACEs.
256          *       So don't have to test it here.
257          *       It is simplest to choose ids not in /etc/passwd /etc/group
258          *       which is done already in a script. 
259          */
260
261         test_acl_get_qualifier();
262
263         return 0;
264 }