]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/libarchive/libarchive/test/test_acl_basic.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / libarchive / 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  *
32  * This should work on all systems, regardless of whether local
33  * filesystems support ACLs or not.
34  */
35
36 struct acl_t {
37         int type;  /* Type of ACL: "access" or "default" */
38         int permset; /* Permissions for this class of users. */
39         int tag; /* Owner, User, Owning group, group, other, etc. */
40         int qual; /* GID or UID of user/group, depending on tag. */
41         const char *name; /* Name of user/group, depending on tag. */
42 };
43
44 static struct acl_t acls0[] = {
45         { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_EXECUTE,
46           ARCHIVE_ENTRY_ACL_USER_OBJ, 0, "" },
47         { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_READ,
48           ARCHIVE_ENTRY_ACL_GROUP_OBJ, 0, "" },
49         { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_WRITE,
50           ARCHIVE_ENTRY_ACL_OTHER, 0, "" },
51 };
52
53 static struct acl_t acls1[] = {
54         { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_EXECUTE,
55           ARCHIVE_ENTRY_ACL_USER_OBJ, -1, "" },
56         { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_READ,
57           ARCHIVE_ENTRY_ACL_USER, 77, "user77" },
58         { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_READ,
59           ARCHIVE_ENTRY_ACL_GROUP_OBJ, -1, "" },
60         { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_WRITE,
61           ARCHIVE_ENTRY_ACL_OTHER, -1, "" },
62 };
63
64 static struct acl_t acls2[] = {
65         { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_EXECUTE | ARCHIVE_ENTRY_ACL_READ,
66           ARCHIVE_ENTRY_ACL_USER_OBJ, -1, "" },
67         { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_READ,
68           ARCHIVE_ENTRY_ACL_USER, 77, "user77" },
69         { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, 0,
70           ARCHIVE_ENTRY_ACL_USER, 78, "user78" },
71         { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_READ,
72           ARCHIVE_ENTRY_ACL_GROUP_OBJ, -1, "" },
73         { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, 0007,
74           ARCHIVE_ENTRY_ACL_GROUP, 78, "group78" },
75         { ARCHIVE_ENTRY_ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_WRITE | ARCHIVE_ENTRY_ACL_EXECUTE,
76           ARCHIVE_ENTRY_ACL_OTHER, -1, "" },
77 };
78
79 static void
80 set_acls(struct archive_entry *ae, struct acl_t *acls, int n)
81 {
82         int i;
83
84         archive_entry_acl_clear(ae);
85         for (i = 0; i < n; i++) {
86                 archive_entry_acl_add_entry(ae,
87                     acls[i].type, acls[i].permset, acls[i].tag, acls[i].qual,
88                     acls[i].name);
89         }
90 }
91
92 static int
93 acl_match(struct acl_t *acl, int type, int permset, int tag, int qual, const char *name)
94 {
95         if (type != acl->type)
96                 return (0);
97         if (permset != acl->permset)
98                 return (0);
99         if (tag != acl->tag)
100                 return (0);
101         if (tag == ARCHIVE_ENTRY_ACL_USER_OBJ)
102                 return (1);
103         if (tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ)
104                 return (1);
105         if (tag == ARCHIVE_ENTRY_ACL_OTHER)
106                 return (1);
107         if (qual != acl->qual)
108                 return (0);
109         if (name == NULL) {
110                 if (acl->name == NULL || acl->name[0] == '\0')
111                         return (1);
112         }
113         if (acl->name == NULL) {
114                 if (name[0] == '\0')
115                         return (1);
116         }
117         return (0 == strcmp(name, acl->name));
118 }
119
120 static void
121 compare_acls(struct archive_entry *ae, struct acl_t *acls, int n, int mode)
122 {
123         int *marker = malloc(sizeof(marker[0]) * n);
124         int i;
125         int r;
126         int type, permset, tag, qual;
127         int matched;
128         const char *name;
129
130         for (i = 0; i < n; i++)
131                 marker[i] = i;
132
133         while (0 == (r = archive_entry_acl_next(ae,
134                          ARCHIVE_ENTRY_ACL_TYPE_ACCESS,
135                          &type, &permset, &tag, &qual, &name))) {
136                 for (i = 0, matched = 0; i < n && !matched; i++) {
137                         if (acl_match(&acls[marker[i]], type, permset,
138                                 tag, qual, name)) {
139                                 /* We found a match; remove it. */
140                                 marker[i] = marker[n - 1];
141                                 n--;
142                                 matched = 1;
143                         }
144                 }
145                 if (tag == ARCHIVE_ENTRY_ACL_USER_OBJ) {
146                         if (!matched) printf("No match for user_obj perm\n");
147                         failure("USER_OBJ permset (%02o) != user mode (%02o)",
148                             permset, 07 & (mode >> 6));
149                         assert((permset << 6) == (mode & 0700));
150                 } else if (tag == ARCHIVE_ENTRY_ACL_GROUP_OBJ) {
151                         if (!matched) printf("No match for group_obj perm\n");
152                         failure("GROUP_OBJ permset %02o != group mode %02o",
153                             permset, 07 & (mode >> 3));
154                         assert((permset << 3) == (mode & 0070));
155                 } else if (tag == ARCHIVE_ENTRY_ACL_OTHER) {
156                         if (!matched) printf("No match for other perm\n");
157                         failure("OTHER permset (%02o) != other mode (%02o)",
158                             permset, mode & 07);
159                         assert((permset << 0) == (mode & 0007));
160                 } else {
161                         failure("Could not find match for ACL "
162                             "(type=%d,permset=%d,tag=%d,qual=%d,name=``%s'')",
163                             type, permset, tag, qual, name);
164                         assert(matched == 1);
165                 }
166         }
167 #if ARCHIVE_VERSION_NUMBER < 1009000
168         /* Known broken before 1.9.0. */
169         skipping("archive_entry_acl_next() exits with ARCHIVE_EOF");
170 #else
171         assertEqualInt(ARCHIVE_EOF, r);
172 #endif
173         assert((mode & 0777) == (archive_entry_mode(ae) & 0777));
174         failure("Could not find match for ACL "
175             "(type=%d,permset=%d,tag=%d,qual=%d,name=``%s'')",
176             acls[marker[0]].type, acls[marker[0]].permset,
177             acls[marker[0]].tag, acls[marker[0]].qual, acls[marker[0]].name);
178         assert(n == 0); /* Number of ACLs not matched should == 0 */
179         free(marker);
180 }
181
182 DEFINE_TEST(test_acl_basic)
183 {
184         struct archive_entry *ae;
185
186         /* Create a simple archive_entry. */
187         assert((ae = archive_entry_new()) != NULL);
188         archive_entry_set_pathname(ae, "file");
189         archive_entry_set_mode(ae, S_IFREG | 0777);
190
191         /* Basic owner/owning group should just update mode bits. */
192         set_acls(ae, acls0, sizeof(acls0)/sizeof(acls0[0]));
193         failure("Basic ACLs shouldn't be stored as extended ACLs");
194         assert(0 == archive_entry_acl_reset(ae, ARCHIVE_ENTRY_ACL_TYPE_ACCESS));
195         failure("Basic ACLs should set mode to 0142, not %04o",
196             archive_entry_mode(ae)&0777);
197         assert((archive_entry_mode(ae) & 0777) == 0142);
198
199
200         /* With any extended ACL entry, we should read back a full set. */
201         set_acls(ae, acls1, sizeof(acls1)/sizeof(acls1[0]));
202         failure("One extended ACL should flag all ACLs to be returned.");
203         assert(4 == archive_entry_acl_reset(ae, ARCHIVE_ENTRY_ACL_TYPE_ACCESS));
204         compare_acls(ae, acls1, sizeof(acls1)/sizeof(acls1[0]), 0142);
205         failure("Basic ACLs should set mode to 0142, not %04o",
206             archive_entry_mode(ae)&0777);
207         assert((archive_entry_mode(ae) & 0777) == 0142);
208
209
210         /* A more extensive set of ACLs. */
211         set_acls(ae, acls2, sizeof(acls2)/sizeof(acls2[0]));
212         assertEqualInt(6, archive_entry_acl_reset(ae, ARCHIVE_ENTRY_ACL_TYPE_ACCESS));
213         compare_acls(ae, acls2, sizeof(acls2)/sizeof(acls2[0]), 0543);
214         failure("Basic ACLs should set mode to 0543, not %04o",
215             archive_entry_mode(ae)&0777);
216         assert((archive_entry_mode(ae) & 0777) == 0543);
217
218         /*
219          * Check that clearing ACLs gets rid of them all by repeating
220          * the first test.
221          */
222         set_acls(ae, acls0, sizeof(acls0)/sizeof(acls0[0]));
223         failure("Basic ACLs shouldn't be stored as extended ACLs");
224         assert(0 == archive_entry_acl_reset(ae, ARCHIVE_ENTRY_ACL_TYPE_ACCESS));
225         failure("Basic ACLs should set mode to 0142, not %04o",
226             archive_entry_mode(ae)&0777);
227         assert((archive_entry_mode(ae) & 0777) == 0142);
228         archive_entry_free(ae);
229 }