2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 __FBSDID("$FreeBSD$");
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.
33 * This should work on all systems, regardless of whether local
34 * filesystems support ACLs or not.
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. */
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, "" },
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, "" },
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, "" },
81 set_acls(struct archive_entry *ae, struct acl_t *acls, int n)
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,
94 acl_match(struct acl_t *acl, int type, int permset, int tag, int qual, const char *name)
96 if (type != acl->type)
98 if (permset != acl->permset)
102 if (tag == ARCHIVE_ENTRY_ACL_USER_OBJ)
104 if (tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ)
106 if (tag == ARCHIVE_ENTRY_ACL_OTHER)
108 if (qual != acl->qual)
111 if (acl->name == NULL || acl->name[0] == '\0')
114 if (acl->name == NULL) {
118 return (0 == strcmp(name, acl->name));
122 compare_acls(struct archive_entry *ae, struct acl_t *acls, int n, int mode)
124 int *marker = malloc(sizeof(marker[0]) * n);
127 int type, permset, tag, qual;
131 for (i = 0; i < n; i++)
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,
140 /* We found a match; remove it. */
141 marker[i] = marker[n - 1];
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)",
160 assert((permset << 0) == (mode & 0007));
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);
168 #if ARCHIVE_VERSION_STAMP < 1009000
169 /* Known broken before 1.9.0. */
170 skipping("archive_entry_acl_next() exits with ARCHIVE_EOF");
172 assertEqualInt(ARCHIVE_EOF, r);
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 */
183 DEFINE_TEST(test_acl_basic)
185 struct archive_entry *ae;
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);
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);
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);
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);
220 * Check that clearing ACLs gets rid of them all by repeating
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);