2 * Copyright (c) 2003-2008 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: head/lib/libarchive/test/test_acl_freebsd.c 189427 2009-03-06 04:21:23Z kientzle $");
28 #if defined(__FreeBSD__) && __FreeBSD__ > 4
32 int type; /* Type of ACL: "access" or "default" */
33 int permset; /* Permissions for this class of users. */
34 int tag; /* Owner, User, Owning group, group, other, etc. */
35 int qual; /* GID or UID of user/group, depending on tag. */
36 const char *name; /* Name of user/group, depending on tag. */
39 static struct myacl_t acls2[] = {
40 { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_EXECUTE | ARCHIVE_ENTRY_ACL_READ,
41 ARCHIVE_ENTRY_ACL_USER_OBJ, -1, "" },
42 { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_READ,
43 ARCHIVE_ENTRY_ACL_USER, 77, "user77" },
44 { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, 0,
45 ARCHIVE_ENTRY_ACL_USER, 78, "user78" },
46 { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_READ,
47 ARCHIVE_ENTRY_ACL_GROUP_OBJ, -1, "" },
48 { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, 0007,
49 ARCHIVE_ENTRY_ACL_GROUP, 78, "group78" },
50 { ARCHIVE_ENTRY_ACL_TYPE_ACCESS,
51 ARCHIVE_ENTRY_ACL_WRITE | ARCHIVE_ENTRY_ACL_EXECUTE,
52 ARCHIVE_ENTRY_ACL_OTHER, -1, "" },
53 { ARCHIVE_ENTRY_ACL_TYPE_ACCESS,
54 ARCHIVE_ENTRY_ACL_WRITE | ARCHIVE_ENTRY_ACL_READ | ARCHIVE_ENTRY_ACL_EXECUTE,
55 ARCHIVE_ENTRY_ACL_MASK, -1, "" },
60 set_acls(struct archive_entry *ae, struct myacl_t *acls)
64 archive_entry_acl_clear(ae);
65 for (i = 0; acls[i].name != NULL; i++) {
66 archive_entry_acl_add_entry(ae,
67 acls[i].type, acls[i].permset, acls[i].tag, acls[i].qual,
73 acl_match(acl_entry_t aclent, struct myacl_t *myacl)
78 acl_permset_t opaque_ps;
81 acl_get_tag_type(aclent, &tag_type);
83 /* translate the silly opaque permset to a bitmap */
84 acl_get_permset(aclent, &opaque_ps);
85 if (acl_get_perm_np(opaque_ps, ACL_EXECUTE))
86 permset |= ARCHIVE_ENTRY_ACL_EXECUTE;
87 if (acl_get_perm_np(opaque_ps, ACL_WRITE))
88 permset |= ARCHIVE_ENTRY_ACL_WRITE;
89 if (acl_get_perm_np(opaque_ps, ACL_READ))
90 permset |= ARCHIVE_ENTRY_ACL_READ;
92 if (permset != myacl->permset)
97 if (myacl->tag != ARCHIVE_ENTRY_ACL_USER_OBJ) return (0);
100 if (myacl->tag != ARCHIVE_ENTRY_ACL_USER)
102 up = acl_get_qualifier(aclent);
105 if ((uid_t)myacl->qual != u)
109 if (myacl->tag != ARCHIVE_ENTRY_ACL_GROUP_OBJ) return (0);
112 if (myacl->tag != ARCHIVE_ENTRY_ACL_GROUP)
114 gp = acl_get_qualifier(aclent);
117 if ((gid_t)myacl->qual != g)
121 if (myacl->tag != ARCHIVE_ENTRY_ACL_MASK) return (0);
124 if (myacl->tag != ARCHIVE_ENTRY_ACL_OTHER) return (0);
131 compare_acls(acl_t acl, struct myacl_t *myacls)
134 int entry_id = ACL_FIRST_ENTRY;
137 acl_entry_t acl_entry;
139 /* Count ACL entries in myacls array and allocate an indirect array. */
140 for (n = 0; myacls[n].name != NULL; ++n)
142 marker = malloc(sizeof(marker[0]) * n);
143 for (i = 0; i < n; i++)
147 * Iterate over acls in system acl object, try to match each
148 * one with an item in the myacls array.
150 while (1 == acl_get_entry(acl, entry_id, &acl_entry)) {
151 /* After the first time... */
152 entry_id = ACL_NEXT_ENTRY;
154 /* Search for a matching entry (tag and qualifier) */
155 for (i = 0, matched = 0; i < n && !matched; i++) {
156 if (acl_match(acl_entry, &myacls[marker[i]])) {
157 /* We found a match; remove it. */
158 marker[i] = marker[n - 1];
164 /* TODO: Print out more details in this case. */
165 failure("ACL entry on file that shouldn't be there");
166 assert(matched == 1);
169 /* Dump entries in the myacls array that weren't in the system acl. */
170 for (i = 0; i < n; ++i) {
171 failure(" ACL entry missing from file: "
172 "type=%d,permset=%d,tag=%d,qual=%d,name=``%s''\n",
173 myacls[marker[i]].type, myacls[marker[i]].permset,
174 myacls[marker[i]].tag, myacls[marker[i]].qual,
175 myacls[marker[i]].name);
176 assert(0); /* Record this as a failure. */
185 * Verify ACL restore-to-disk. This test is FreeBSD-specific.
188 DEFINE_TEST(test_acl_freebsd_posix1e)
190 #if !defined(__FreeBSD__)
191 skipping("FreeBSD-specific ACL restore test");
192 #elif __FreeBSD__ < 5
193 skipping("ACL restore supported only on FreeBSD 5.0 and later");
197 struct archive_entry *ae;
202 * First, do a quick manual set/read of ACL data to
203 * verify that the local filesystem does support ACLs.
204 * If it doesn't, we'll simply skip the remaining tests.
206 acl = acl_from_text("u::rwx,u:1:rw,g::rwx,g:15:rx,o::rwx,m::rwx");
207 assert((void *)acl != NULL);
208 /* Create a test file and try to set an ACL on it. */
209 fd = open("pretest", O_WRONLY | O_CREAT | O_EXCL, 0777);
210 failure("Could not create test file?!");
211 if (!assert(fd >= 0)) {
216 n = acl_set_fd(fd, acl);
218 if (n != 0 && errno == EOPNOTSUPP) {
220 skipping("ACL tests require that ACL support be enabled on the filesystem");
223 if (n != 0 && errno == EINVAL) {
225 skipping("This filesystem does not support POSIX.1e ACLs");
228 failure("acl_set_fd(): errno = %d (%s)",
229 errno, strerror(errno));
230 assertEqualInt(0, n);
233 /* Create a write-to-disk object. */
234 assert(NULL != (a = archive_write_disk_new()));
235 archive_write_disk_set_options(a,
236 ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_ACL);
238 /* Populate an archive entry with some metadata, including ACL info */
239 ae = archive_entry_new();
241 archive_entry_set_pathname(ae, "test0");
242 archive_entry_set_mtime(ae, 123456, 7890);
243 archive_entry_set_size(ae, 0);
245 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
246 archive_entry_free(ae);
248 /* Close the archive. */
249 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
250 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
252 /* Verify the data on disk. */
253 assertEqualInt(0, stat("test0", &st));
254 assertEqualInt(st.st_mtime, 123456);
255 acl = acl_get_file("test0", ACL_TYPE_ACCESS);
256 assert(acl != (acl_t)NULL);
257 compare_acls(acl, acls2);
262 * Copyright (c) 2003-2008 Tim Kientzle
263 * All rights reserved.
265 * Redistribution and use in source and binary forms, with or without
266 * modification, are permitted provided that the following conditions
268 * 1. Redistributions of source code must retain the above copyright
269 * notice, this list of conditions and the following disclaimer.
270 * 2. Redistributions in binary form must reproduce the above copyright
271 * notice, this list of conditions and the following disclaimer in the
272 * documentation and/or other materials provided with the distribution.
274 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
275 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
276 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
277 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
278 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
279 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
280 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
281 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
282 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
283 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
286 __FBSDID("$FreeBSD: head/lib/libarchive/test/test_acl_freebsd.c 189427 2009-03-06 04:21:23Z kientzle $");
288 #if defined(__FreeBSD__) && __FreeBSD__ > 4
292 int type; /* Type of ACL: "access" or "default" */
293 int permset; /* Permissions for this class of users. */
294 int tag; /* Owner, User, Owning group, group, other, etc. */
295 int qual; /* GID or UID of user/group, depending on tag. */
296 const char *name; /* Name of user/group, depending on tag. */
299 static struct myacl_t acls2[] = {
300 { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_EXECUTE | ARCHIVE_ENTRY_ACL_READ,
301 ARCHIVE_ENTRY_ACL_USER_OBJ, -1, "" },
302 { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_READ,
303 ARCHIVE_ENTRY_ACL_USER, 77, "user77" },
304 { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, 0,
305 ARCHIVE_ENTRY_ACL_USER, 78, "user78" },
306 { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_READ,
307 ARCHIVE_ENTRY_ACL_GROUP_OBJ, -1, "" },
308 { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, 0007,
309 ARCHIVE_ENTRY_ACL_GROUP, 78, "group78" },
310 { ARCHIVE_ENTRY_ACL_TYPE_ACCESS,
311 ARCHIVE_ENTRY_ACL_WRITE | ARCHIVE_ENTRY_ACL_EXECUTE,
312 ARCHIVE_ENTRY_ACL_OTHER, -1, "" },
313 { ARCHIVE_ENTRY_ACL_TYPE_ACCESS,
314 ARCHIVE_ENTRY_ACL_WRITE | ARCHIVE_ENTRY_ACL_READ | ARCHIVE_ENTRY_ACL_EXECUTE,
315 ARCHIVE_ENTRY_ACL_MASK, -1, "" },
320 set_acls(struct archive_entry *ae, struct myacl_t *acls)
324 archive_entry_acl_clear(ae);
325 for (i = 0; acls[i].name != NULL; i++) {
326 archive_entry_acl_add_entry(ae,
327 acls[i].type, acls[i].permset, acls[i].tag, acls[i].qual,
333 acl_match(acl_entry_t aclent, struct myacl_t *myacl)
338 acl_permset_t opaque_ps;
341 acl_get_tag_type(aclent, &tag_type);
343 /* translate the silly opaque permset to a bitmap */
344 acl_get_permset(aclent, &opaque_ps);
345 if (acl_get_perm_np(opaque_ps, ACL_EXECUTE))
346 permset |= ARCHIVE_ENTRY_ACL_EXECUTE;
347 if (acl_get_perm_np(opaque_ps, ACL_WRITE))
348 permset |= ARCHIVE_ENTRY_ACL_WRITE;
349 if (acl_get_perm_np(opaque_ps, ACL_READ))
350 permset |= ARCHIVE_ENTRY_ACL_READ;
352 if (permset != myacl->permset)
357 if (myacl->tag != ARCHIVE_ENTRY_ACL_USER_OBJ) return (0);
360 if (myacl->tag != ARCHIVE_ENTRY_ACL_USER)
362 up = acl_get_qualifier(aclent);
365 if ((uid_t)myacl->qual != u)
369 if (myacl->tag != ARCHIVE_ENTRY_ACL_GROUP_OBJ) return (0);
372 if (myacl->tag != ARCHIVE_ENTRY_ACL_GROUP)
374 gp = acl_get_qualifier(aclent);
377 if ((gid_t)myacl->qual != g)
381 if (myacl->tag != ARCHIVE_ENTRY_ACL_MASK) return (0);
384 if (myacl->tag != ARCHIVE_ENTRY_ACL_OTHER) return (0);
391 compare_acls(acl_t acl, struct myacl_t *myacls)
394 int entry_id = ACL_FIRST_ENTRY;
397 acl_entry_t acl_entry;
399 /* Count ACL entries in myacls array and allocate an indirect array. */
400 for (n = 0; myacls[n].name != NULL; ++n)
402 marker = malloc(sizeof(marker[0]) * n);
403 for (i = 0; i < n; i++)
407 * Iterate over acls in system acl object, try to match each
408 * one with an item in the myacls array.
410 while (1 == acl_get_entry(acl, entry_id, &acl_entry)) {
411 /* After the first time... */
412 entry_id = ACL_NEXT_ENTRY;
414 /* Search for a matching entry (tag and qualifier) */
415 for (i = 0, matched = 0; i < n && !matched; i++) {
416 if (acl_match(acl_entry, &myacls[marker[i]])) {
417 /* We found a match; remove it. */
418 marker[i] = marker[n - 1];
424 /* TODO: Print out more details in this case. */
425 failure("ACL entry on file that shouldn't be there");
426 assert(matched == 1);
429 /* Dump entries in the myacls array that weren't in the system acl. */
430 for (i = 0; i < n; ++i) {
431 failure(" ACL entry missing from file: "
432 "type=%d,permset=%d,tag=%d,qual=%d,name=``%s''\n",
433 myacls[marker[i]].type, myacls[marker[i]].permset,
434 myacls[marker[i]].tag, myacls[marker[i]].qual,
435 myacls[marker[i]].name);
436 assert(0); /* Record this as a failure. */
445 * Verify ACL restore-to-disk. This test is FreeBSD-specific.
448 DEFINE_TEST(test_acl_freebsd_posix1e)
450 #if !defined(__FreeBSD__)
451 skipping("FreeBSD-specific ACL restore test");
452 #elif __FreeBSD__ < 5
453 skipping("ACL restore supported only on FreeBSD 5.0 and later");
457 struct archive_entry *ae;
462 * First, do a quick manual set/read of ACL data to
463 * verify that the local filesystem does support ACLs.
464 * If it doesn't, we'll simply skip the remaining tests.
466 acl = acl_from_text("u::rwx,u:1:rw,g::rwx,g:15:rx,o::rwx,m::rwx");
467 assert((void *)acl != NULL);
468 /* Create a test file and try to set an ACL on it. */
469 fd = open("pretest", O_WRONLY | O_CREAT | O_EXCL, 0777);
470 failure("Could not create test file?!");
471 if (!assert(fd >= 0)) {
476 n = acl_set_fd(fd, acl);
478 if (n != 0 && errno == EOPNOTSUPP) {
480 skipping("ACL tests require that ACL support be enabled on the filesystem");
483 if (n != 0 && errno == EINVAL) {
485 skipping("This filesystem does not support POSIX.1e ACLs");
488 failure("acl_set_fd(): errno = %d (%s)",
489 errno, strerror(errno));
490 assertEqualInt(0, n);
493 /* Create a write-to-disk object. */
494 assert(NULL != (a = archive_write_disk_new()));
495 archive_write_disk_set_options(a,
496 ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_ACL);
498 /* Populate an archive entry with some metadata, including ACL info */
499 ae = archive_entry_new();
501 archive_entry_set_pathname(ae, "test0");
502 archive_entry_set_mtime(ae, 123456, 7890);
503 archive_entry_set_size(ae, 0);
505 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
506 archive_entry_free(ae);
508 /* Close the archive. */
509 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
510 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
512 /* Verify the data on disk. */
513 assertEqualInt(0, stat("test0", &st));
514 assertEqualInt(st.st_mtime, 123456);
515 acl = acl_get_file("test0", ACL_TYPE_ACCESS);
516 assert(acl != (acl_t)NULL);
517 compare_acls(acl, acls2);