blob: c3894563e628284578781ec62c7bef4972e0952b [file] [log] [blame]
The Android Open Source Project23580ca2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdlib.h>
18#include <stdio.h>
19#include <string.h>
20#undef NDEBUG
21#include <assert.h>
22#include "permissions.h"
23
24static int
25test_permission_list()
26{
27 PermissionRequestList list;
28 int ret;
29 int numRequests;
30
31 /* Bad parameter
32 */
33 ret = initPermissionRequestList(NULL);
34 assert(ret < 0);
35
36 /* Good parameter
37 */
38 ret = initPermissionRequestList(&list);
39 assert(ret == 0);
40
41 /* Bad parameters
42 */
43 ret = addPermissionRequestToList(NULL, NULL, false, 0);
44 assert(ret < 0);
45
46 ret = addPermissionRequestToList(&list, NULL, false, 0);
47 assert(ret < 0);
48
49 /* Good parameters
50 */
51 numRequests = 0;
52
53 ret = addPermissionRequestToList(&list, "one", false, 1);
54 assert(ret == 0);
55 numRequests++;
56
57 ret = addPermissionRequestToList(&list, "two", false, 2);
58 assert(ret == 0);
59 numRequests++;
60
61 ret = addPermissionRequestToList(&list, "three", false, 3);
62 assert(ret == 0);
63 numRequests++;
64
65 ret = addPermissionRequestToList(&list, "recursive", true, 55);
66 assert(ret == 0);
67 numRequests++;
68
69 /* Validate the list
70 */
71 assert(list.requests != NULL);
72 assert(list.numRequests == numRequests);
73 assert(list.numRequests <= list.requestsAllocated);
74 bool sawOne = false;
75 bool sawTwo = false;
76 bool sawThree = false;
77 bool sawRecursive = false;
78 int i;
79 for (i = 0; i < list.numRequests; i++) {
80 PermissionRequest *req = &list.requests[i];
81 assert(req->allowed == 0);
82
83 /* Order isn't guaranteed, so we have to switch every time.
84 */
85 if (strcmp(req->path, "one") == 0) {
86 assert(!sawOne);
87 assert(req->requested == 1);
88 assert(!req->recursive);
89 sawOne = true;
90 } else if (strcmp(req->path, "two") == 0) {
91 assert(!sawTwo);
92 assert(req->requested == 2);
93 assert(!req->recursive);
94 sawTwo = true;
95 } else if (strcmp(req->path, "three") == 0) {
96 assert(!sawThree);
97 assert(req->requested == 3);
98 assert(!req->recursive);
99 sawThree = true;
100 } else if (strcmp(req->path, "recursive") == 0) {
101 assert(!sawRecursive);
102 assert(req->requested == 55);
103 assert(req->recursive);
104 sawRecursive = true;
105 } else {
106 assert(false);
107 }
108 }
109 assert(sawOne);
110 assert(sawTwo);
111 assert(sawThree);
112 assert(sawRecursive);
113
114 /* Smoke test the teardown
115 */
116 freePermissionRequestListElements(&list);
117
118 return 0;
119}
120
121static int
122test_permission_table()
123{
124 int ret;
125
126 /* Test the global permissions table.
127 * Try calling functions without initializing first.
128 */
129 ret = registerPermissionSet(0, NULL);
130 assert(ret < 0);
131
132 ret = countPermissionConflicts((PermissionRequestList *)16, false);
133 assert(ret < 0);
134
135 ret = getPermissionCount();
136 assert(ret < 0);
137
138 const Permission *p;
139 p = getPermissionAt(0);
140 assert(p == NULL);
141
142 /* Initialize.
143 */
144 ret = permissionInit();
145 assert(ret == 0);
146
147 /* Make sure we can't initialize twice.
148 */
149 ret = permissionInit();
150 assert(ret < 0);
151
152 /* Test the inspection functions.
153 */
154 ret = getPermissionCount();
155 assert(ret == 0);
156
157 p = getPermissionAt(-1);
158 assert(p == NULL);
159
160 p = getPermissionAt(0);
161 assert(p == NULL);
162
163 p = getPermissionAt(1);
164 assert(p == NULL);
165
166 /* Test registerPermissionSet().
167 * Try some bad parameter values.
168 */
169 ret = registerPermissionSet(-1, NULL);
170 assert(ret < 0);
171
172 ret = registerPermissionSet(1, NULL);
173 assert(ret < 0);
174
175 /* Register some permissions.
176 */
177 Permission p1;
178 p1.path = "one";
179 p1.allowed = 1;
180 ret = registerPermissionSet(1, &p1);
181 assert(ret == 0);
182 ret = getPermissionCount();
183 assert(ret == 1);
184
185 Permission p2[2];
186 p2[0].path = "two";
187 p2[0].allowed = 2;
188 p2[1].path = "three";
189 p2[1].allowed = 3;
190 ret = registerPermissionSet(2, p2);
191 assert(ret == 0);
192 ret = getPermissionCount();
193 assert(ret == 3);
194
195 ret = registerPermissionSet(0, NULL);
196 assert(ret == 0);
197 ret = getPermissionCount();
198 assert(ret == 3);
199
200 p1.path = "four";
201 p1.allowed = 4;
202 ret = registerPermissionSet(1, &p1);
203 assert(ret == 0);
204
205 /* Make sure the table looks correct.
206 * Order is important; more-recent additions
207 * should appear at higher indices.
208 */
209 ret = getPermissionCount();
210 assert(ret == 4);
211
212 int i;
213 for (i = 0; i < ret; i++) {
214 const Permission *p;
215 p = getPermissionAt(i);
216 assert(p != NULL);
217 assert(p->allowed == (unsigned int)(i + 1));
218 switch (i) {
219 case 0:
220 assert(strcmp(p->path, "one") == 0);
221 break;
222 case 1:
223 assert(strcmp(p->path, "two") == 0);
224 break;
225 case 2:
226 assert(strcmp(p->path, "three") == 0);
227 break;
228 case 3:
229 assert(strcmp(p->path, "four") == 0);
230 break;
231 default:
232 assert(!"internal error");
233 break;
234 }
235 }
236 p = getPermissionAt(ret);
237 assert(p == NULL);
238
239 /* Smoke test the teardown
240 */
241 permissionCleanup();
242
243 return 0;
244}
245
246static int
247test_allowed_permissions()
248{
249 int ret;
250 int numPerms;
251
252 /* Make sure these fail before initialization.
253 */
254 ret = countPermissionConflicts((PermissionRequestList *)1, false);
255 assert(ret < 0);
256
257 ret = getAllowedPermissions((const char *)1, false, (unsigned int *)1);
258 assert(ret < 0);
259
260 /* Initialize.
261 */
262 ret = permissionInit();
263 assert(ret == 0);
264
265 /* Make sure countPermissionConflicts() fails with bad parameters.
266 */
267 ret = countPermissionConflicts(NULL, false);
268 assert(ret < 0);
269
270 /* Register a set of permissions.
271 */
272 Permission perms[] = {
273 { "/", PERM_NONE },
274 { "/stat", PERM_STAT },
275 { "/read", PERMSET_READ },
276 { "/write", PERMSET_WRITE },
277 { "/.stat", PERM_STAT },
278 { "/.stat/.read", PERMSET_READ },
279 { "/.stat/.read/.write", PERMSET_WRITE },
280 { "/.stat/.write", PERMSET_WRITE },
281 };
282 numPerms = sizeof(perms) / sizeof(perms[0]);
283 ret = registerPermissionSet(numPerms, perms);
284 assert(ret == 0);
285
286 /* Build a permission request list.
287 */
288 PermissionRequestList list;
289 ret = initPermissionRequestList(&list);
290 assert(ret == 0);
291
292 ret = addPermissionRequestToList(&list, "/stat", false, PERM_STAT);
293 assert(ret == 0);
294
295 ret = addPermissionRequestToList(&list, "/read", false, PERM_READ);
296 assert(ret == 0);
297
298 ret = addPermissionRequestToList(&list, "/write", false, PERM_WRITE);
299 assert(ret == 0);
300
301 //TODO: cover more cases once the permission stuff has been implemented
302
303 /* All of the requests in the list should be allowed.
304 */
305 ret = countPermissionConflicts(&list, false);
306 assert(ret == 0);
307
308 /* Add a request that will be denied.
309 */
310 ret = addPermissionRequestToList(&list, "/stat", false, 1<<31 | PERM_STAT);
311 assert(ret == 0);
312
313 ret = countPermissionConflicts(&list, false);
314 assert(ret == 1);
315
316 //TODO: more tests
317
318 permissionCleanup();
319
320 return 0;
321}
322
323int
324test_permissions()
325{
326 int ret;
327
328 ret = test_permission_list();
329 if (ret != 0) {
330 fprintf(stderr, "test_permission_list() failed: %d\n", ret);
331 return ret;
332 }
333
334 ret = test_permission_table();
335 if (ret != 0) {
336 fprintf(stderr, "test_permission_table() failed: %d\n", ret);
337 return ret;
338 }
339
340 ret = test_allowed_permissions();
341 if (ret != 0) {
342 fprintf(stderr, "test_permission_table() failed: %d\n", ret);
343 return ret;
344 }
345
346 return 0;
347}