]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - lib/libarchive/test/test_acl_basic.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / lib / libarchive / test / test_acl_basic.c
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #include "test.h"
26 __FBSDID("$FreeBSD$");
27
28 /*
29  * Exercise the system-independent portion of the ACL support.
30  * Check that archive_entry objects can save and restore ACL data
31  * and that pax archive can save and restore ACL data.
32  *
33  * This should work on all systems, regardless of whether local
34  * filesystems support ACLs or not.
35  */
36
37 struct acl_t {
38         int type;  /* Type of ACL: "access" or "default" */
39         int permset; /* Permissions for this class of users. */
40         int tag; /* Owner, User, Owning group, group, other, etc. */
41         int qual; /* GID or UID of user/group, depending on tag. */
42         const char *name; /* Name of user/group, depending on tag. */
43 };
44
45 struct acl_t acls0[] = {
46         { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_EXECUTE,
47           ARCHIVE_ENTRY_ACL_USER_OBJ, 0, "" },
48         { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_READ,
49           ARCHIVE_ENTRY_ACL_GROUP_OBJ, 0, "" },
50         { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_WRITE,
51           ARCHIVE_ENTRY_ACL_OTHER, 0, "" },
52 };
53
54 struct acl_t acls1[] = {
55         { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_EXECUTE,
56           ARCHIVE_ENTRY_ACL_USER_OBJ, -1, "" },
57         { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_READ,
58           ARCHIVE_ENTRY_ACL_USER, 77, "user77" },
59         { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_READ,
60           ARCHIVE_ENTRY_ACL_GROUP_OBJ, -1, "" },
61         { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_WRITE,
62           ARCHIVE_ENTRY_ACL_OTHER, -1, "" },
63 };
64
65 struct acl_t acls2[] = {
66         { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_EXECUTE | ARCHIVE_ENTRY_ACL_READ,
67           ARCHIVE_ENTRY_ACL_USER_OBJ, -1, "" },
68         { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_READ,
69           ARCHIVE_ENTRY_ACL_USER, 77, "user77" },
70         { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, 0,
71           ARCHIVE_ENTRY_ACL_USER, 78, "user78" },
72         { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_READ,
73           ARCHIVE_ENTRY_ACL_GROUP_OBJ, -1, "" },
74         { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, 0007,
75           ARCHIVE_ENTRY_ACL_GROUP, 78, "group78" },
76         { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_WRITE | ARCHIVE_ENTRY_ACL_EXECUTE,
77           ARCHIVE_ENTRY_ACL_OTHER, -1, "" },
78 };
79
80 static void
81 set_acls(struct archive_entry *ae, struct acl_t *acls, int n)
82 {
83         int i;
84
85         archive_entry_acl_clear(ae);
86         for (i = 0; i < n; i++) {
87                 archive_entry_acl_add_entry(ae,
88                     acls[i].type, acls[i].permset, acls[i].tag, acls[i].qual,
89                     acls[i].name);
90         }
91 }
92
93 static int
94 acl_match(struct acl_t *acl, int type, int permset, int tag, int qual, const char *name)
95 {
96         if (type != acl->type)
97                 return (0);
98         if (permset != acl->permset)
99                 return (0);
100         if (tag != acl->tag)
101                 return (0);
102         if (tag == ARCHIVE_ENTRY_ACL_USER_OBJ)
103                 return (1);
104         if (tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ)
105                 return (1);
106         if (tag == ARCHIVE_ENTRY_ACL_OTHER)
107                 return (1);
108         if (qual != acl->qual)
109                 return (0);
110         if (name == NULL) {
111                 if (acl->name == NULL || acl->name[0] == '\0')
112                         return (1);
113         }
114         if (acl->name == NULL) {
115                 if (name[0] == '\0')
116                         return (1);
117         }
118         return (0 == strcmp(name, acl->name));
119 }
120
121 static void
122 compare_acls(struct archive_entry *ae, struct acl_t *acls, int n, int mode)
123 {
124         int *marker = malloc(sizeof(marker[0]) * n);
125         int i;
126         int r;
127         int type, permset, tag, qual;
128         int matched;
129         const char *name;
130
131         for (i = 0; i < n; i++)
132                 marker[i] = i;
133
134         while (0 == (r = archive_entry_acl_next(ae,
135                          ARCHIVE_ENTRY_ACL_TYPE_ACCESS,
136                          &type, &permset, &tag, &qual, &name))) {
137                 for (i = 0, matched = 0; i < n && !matched; i++) {
138                         if (acl_match(&acls[marker[i]], type, permset,
139                                 tag, qual, name)) {
140                                 /* We found a match; remove it. */
141                                 marker[i] = marker[n - 1];
142                                 n--;
143                                 matched = 1;
144                         }
145                 }
146                 if (tag == ARCHIVE_ENTRY_ACL_USER_OBJ) {
147                         if (!matched) printf("No match for user_obj perm\n");
148                         failure("USER_OBJ permset (%02o) != user mode (%02o)",
149                             permset, 07 & (mode >> 6));
150                         assert((permset << 6) == (mode & 0700));
151                 } else if (tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ) {
152                         if (!matched) printf("No match for group_obj perm\n");
153                         failure("GROUP_OBJ permset %02o != group mode %02o",
154                             permset, 07 & (mode >> 3));
155                         assert((permset << 3) == (mode & 0070));
156                 } else if (tag == ARCHIVE_ENTRY_ACL_OTHER) {
157                         if (!matched) printf("No match for other perm\n");
158                         failure("OTHER permset (%02o) != other mode (%02o)",
159                             permset, mode & 07);
160                         assert((permset << 0) == (mode & 0007));
161                 } else {
162                         failure("Could not find match for ACL "
163                             "(type=%d,permset=%d,tag=%d,qual=%d,name=``%s'')",
164                             type, permset, tag, qual, name);
165                         assert(matched == 1);
166                 }
167         }
168 #if ARCHIVE_VERSION_STAMP < 1009000
169         /* Known broken before 1.9.0. */
170         skipping("archive_entry_acl_next() exits with ARCHIVE_EOF");
171 #else
172         assertEqualInt(ARCHIVE_EOF, r);
173 #endif
174         assert((mode & 0777) == (archive_entry_mode(ae) & 0777));
175         failure("Could not find match for ACL "
176             "(type=%d,permset=%d,tag=%d,qual=%d,name=``%s'')",
177             acls[marker[0]].type, acls[marker[0]].permset,
178             acls[marker[0]].tag, acls[marker[0]].qual, acls[marker[0]].name);
179         assert(n == 0); /* Number of ACLs not matched should == 0 */
180         free(marker);
181 }
182
183 DEFINE_TEST(test_acl_basic)
184 {
185         struct archive_entry *ae;
186
187         /* Create a simple archive_entry. */
188         assert((ae = archive_entry_new()) != NULL);
189         archive_entry_set_pathname(ae, "file");
190         archive_entry_set_mode(ae, S_IFREG | 0777);
191
192         /* Basic owner/owning group should just update mode bits. */
193         set_acls(ae, acls0, sizeof(acls0)/sizeof(acls0[0]));
194         failure("Basic ACLs shouldn't be stored as extended ACLs");
195         assert(0 == archive_entry_acl_reset(ae, ARCHIVE_ENTRY_ACL_TYPE_ACCESS));
196         failure("Basic ACLs should set mode to 0142, not %04o",
197             archive_entry_mode(ae)&0777);
198         assert((archive_entry_mode(ae) & 0777) == 0142);
199
200
201         /* With any extended ACL entry, we should read back a full set. */
202         set_acls(ae, acls1, sizeof(acls1)/sizeof(acls1[0]));
203         failure("One extended ACL should flag all ACLs to be returned.");
204         assert(4 == archive_entry_acl_reset(ae, ARCHIVE_ENTRY_ACL_TYPE_ACCESS));
205         compare_acls(ae, acls1, sizeof(acls1)/sizeof(acls1[0]), 0142);
206         failure("Basic ACLs should set mode to 0142, not %04o",
207             archive_entry_mode(ae)&0777);
208         assert((archive_entry_mode(ae) & 0777) == 0142);
209
210
211         /* A more extensive set of ACLs. */
212         set_acls(ae, acls2, sizeof(acls2)/sizeof(acls2[0]));
213         assertEqualInt(6, archive_entry_acl_reset(ae, ARCHIVE_ENTRY_ACL_TYPE_ACCESS));
214         compare_acls(ae, acls2, sizeof(acls2)/sizeof(acls2[0]), 0543);
215         failure("Basic ACLs should set mode to 0543, not %04o",
216             archive_entry_mode(ae)&0777);
217         assert((archive_entry_mode(ae) & 0777) == 0543);
218
219         /*
220          * Check that clearing ACLs gets rid of them all by repeating
221          * the first test.
222          */
223         set_acls(ae, acls0, sizeof(acls0)/sizeof(acls0[0]));
224         failure("Basic ACLs shouldn't be stored as extended ACLs");
225         assert(0 == archive_entry_acl_reset(ae, ARCHIVE_ENTRY_ACL_TYPE_ACCESS));
226         failure("Basic ACLs should set mode to 0142, not %04o",
227             archive_entry_mode(ae)&0777);
228         assert((archive_entry_mode(ae) & 0777) == 0142);
229         archive_entry_free(ae);
230 }