KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > file > TestChacc


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/file/TestChacc.java,v $
3  * Date : $Date: 2006/09/21 09:34:47 $
4  * Version: $Revision: 1.16 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31  
32 package org.opencms.file;
33
34 import org.opencms.main.CmsException;
35 import org.opencms.security.CmsAccessControlEntry;
36 import org.opencms.security.CmsPermissionSet;
37 import org.opencms.security.I_CmsPrincipal;
38 import org.opencms.test.OpenCmsTestCase;
39 import org.opencms.test.OpenCmsTestProperties;
40 import org.opencms.test.OpenCmsTestResourceFilter;
41
42 import java.util.Iterator JavaDoc;
43 import java.util.List JavaDoc;
44
45 import junit.extensions.TestSetup;
46 import junit.framework.Test;
47 import junit.framework.TestSuite;
48
49 /**
50  * Unit test for the "chacc" method of the CmsObject.<p>
51  *
52  * @author Michael Emmerich
53  * @version $Revision: 1.16 $
54  */

55 public class TestChacc extends OpenCmsTestCase {
56   
57     /**
58      * Default JUnit constructor.<p>
59      *
60      * @param arg0 JUnit parameters
61      */

62     public TestChacc(String JavaDoc arg0) {
63         super(arg0);
64     }
65     
66     /**
67      * Test suite for this test class.<p>
68      *
69      * @return the test suite
70      */

71     public static Test suite() {
72         OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH);
73         
74         TestSuite suite = new TestSuite();
75         suite.setName(TestChacc.class.getName());
76                 
77         suite.addTest(new TestChacc("testChaccFileGroup"));
78         suite.addTest(new TestChacc("testChaccFileUser"));
79         suite.addTest(new TestChacc("testChaccAddRemove"));
80                
81         TestSetup wrapper = new TestSetup(suite) {
82             
83             protected void setUp() {
84                 setupOpenCms("simpletest", "/sites/default/");
85             }
86             
87             protected void tearDown() {
88                 removeOpenCms();
89             }
90         };
91         
92         return wrapper;
93     }
94     
95     /**
96      * Test the chacc method on a file and a group.<p>
97      *
98      * @param tc the OpenCmsTestCase
99      * @param cms the CmsObject
100      * @param resource1 the resource to change permissions
101      * @param group the group to change the permissions from
102      * @param permissions the new permission set for this group
103      * @param flags the flags for modifying the permission set
104      * @throws Throwable if something goes wrong
105      */

106     public static void chaccFileGroup(OpenCmsTestCase tc, CmsObject cms, String JavaDoc resource1, CmsGroup group, CmsPermissionSet permissions, int flags) throws Throwable JavaDoc {
107        
108         tc.storeResources(cms, resource1);
109                 
110         cms.lockResource(resource1);
111         cms.chacc(resource1, I_CmsPrincipal.PRINCIPAL_GROUP, group.getName(), permissions.getAllowedPermissions(), permissions.getDeniedPermissions(), flags);
112         cms.unlockResource(resource1);
113         
114         // now evaluate the result
115
tc.assertFilter(cms, resource1, OpenCmsTestResourceFilter.FILTER_CHACC);
116         // test the ace of the new permission
117
// add the group flag to the acl
118
CmsResource res = cms.readResource(resource1, CmsResourceFilter.ALL);
119  
120         CmsAccessControlEntry ace =
121                new CmsAccessControlEntry(res.getResourceId(), group.getId(),
122                    permissions.getAllowedPermissions(), permissions.getDeniedPermissions(),
123                    flags + CmsAccessControlEntry.ACCESS_FLAGS_GROUP);
124         tc.assertAce(cms, resource1, ace);
125          // test the acl with the permission set
126
int denied = permissions.getDeniedPermissions();
127          if (flags == CmsAccessControlEntry.ACCESS_FLAGS_OVERWRITE) {
128             denied = 0;
129          }
130         CmsPermissionSet permission = new CmsPermissionSet(permissions.getAllowedPermissions(), denied);
131         tc.assertAcl(cms, resource1, group.getId(), permission);
132     }
133     
134     /**
135      * Test the chacc method on a file and a group.<p>
136      *
137      * @param tc the OpenCmsTestCase
138      * @param cms the CmsObject
139      * @param resource1 the resource to change permissions
140      * @param group the group to change the permissions from
141      * @param permissions the new permission set for this group
142      * @param flags the flags for modifying the permission set
143      * @throws Throwable if something goes wrong
144      */

145     public static void chaccFolderGroup(OpenCmsTestCase tc, CmsObject cms, String JavaDoc resource1, CmsGroup group, CmsPermissionSet permissions, int flags) throws Throwable JavaDoc {
146        
147         tc.storeResources(cms, resource1);
148                 
149         cms.lockResource(resource1);
150         cms.chacc(resource1, I_CmsPrincipal.PRINCIPAL_GROUP, group.getName(), permissions.getAllowedPermissions(), permissions.getDeniedPermissions(), flags);
151         cms.unlockResource(resource1);
152
153         // now evaluate the result
154
tc.assertFilter(cms, resource1, OpenCmsTestResourceFilter.FILTER_CHACC);
155         // test the ace of the new permission
156
// add the group flag to the acl
157
CmsResource res = cms.readResource(resource1, CmsResourceFilter.ALL);
158  
159         CmsAccessControlEntry ace =
160                new CmsAccessControlEntry(res.getResourceId(), group.getId(),
161                    permissions.getAllowedPermissions(), permissions.getDeniedPermissions(),
162                    flags + CmsAccessControlEntry.ACCESS_FLAGS_GROUP);
163         tc.assertAce(cms, resource1, ace);
164          // test the acl with the permission set
165
int denied = permissions.getDeniedPermissions();
166          if ((flags & CmsAccessControlEntry.ACCESS_FLAGS_OVERWRITE) > 0) {
167             denied = 0;
168          }
169         CmsPermissionSet permission = new CmsPermissionSet(permissions.getAllowedPermissions(), denied);
170         tc.assertAcl(cms, resource1, group.getId(), permission);
171         
172         // now check all the subresources in the folder, access must be modified as well
173
List JavaDoc subresources = cms.readResources(resource1, CmsResourceFilter.ALL);
174         Iterator JavaDoc j = subresources.iterator();
175         
176         while (j.hasNext()) {
177             CmsResource subRes = (CmsResource)j.next();
178             String JavaDoc subResName = cms.getSitePath(subRes);
179             // now evaluate the result
180
tc.assertFilter(cms, subResName, OpenCmsTestResourceFilter.FILTER_CHACC);
181             // test the ace of the new permission
182
// add the group and the inherited flag to the acl
183
ace = new CmsAccessControlEntry(res.getResourceId(), group.getId(),
184                       permissions.getAllowedPermissions(), permissions.getDeniedPermissions(),
185                       flags + CmsAccessControlEntry.ACCESS_FLAGS_GROUP + CmsAccessControlEntry.ACCESS_FLAGS_INHERITED);
186             tc.assertAce(cms, subResName, ace);
187             
188             // test the acl with the permission set
189
permission = new CmsPermissionSet(permissions.getAllowedPermissions(), denied);
190            tc.assertAcl(cms, resource1, subResName, group.getId(), permission);
191             
192         }
193     }
194
195     /**
196      * Test the chacc method on a file and a user.<p>
197      *
198      * @param tc the OpenCmsTestCase
199      * @param cms the CmsObject
200      * @param resource1 the resource to change permissions
201      * @param user the user to change the permissions from
202      * @param permissions the new permission set for this group
203      * @param flags the flags for modifying the permission set
204      * @throws Throwable if something goes wrong
205      */

206     public static void chaccFileUser(OpenCmsTestCase tc, CmsObject cms, String JavaDoc resource1, CmsUser user, CmsPermissionSet permissions, int flags) throws Throwable JavaDoc {
207        
208         tc.storeResources(cms, resource1);
209                 
210         cms.lockResource(resource1);
211         cms.chacc(resource1, I_CmsPrincipal.PRINCIPAL_USER, user.getName(), permissions.getAllowedPermissions(), permissions.getDeniedPermissions(), flags);
212         cms.unlockResource(resource1);
213
214         // now evaluate the result
215
tc.assertFilter(cms, resource1, OpenCmsTestResourceFilter.FILTER_CHACC);
216         // test the ace of the new permission
217
// add the user flag to the acl
218
CmsResource res = cms.readResource(resource1, CmsResourceFilter.ALL);
219  
220         CmsAccessControlEntry ace =
221                new CmsAccessControlEntry(res.getResourceId(), user.getId(),
222                    permissions.getAllowedPermissions(), permissions.getDeniedPermissions(),
223                    flags + CmsAccessControlEntry.ACCESS_FLAGS_USER);
224         tc.assertAce(cms, resource1, ace);
225          // test the acl with the permission set
226
int denied = permissions.getDeniedPermissions();
227          if (flags == CmsAccessControlEntry.ACCESS_FLAGS_OVERWRITE) {
228             denied = 0;
229          }
230         CmsPermissionSet permission = new CmsPermissionSet(permissions.getAllowedPermissions(), denied);
231         tc.assertAcl(cms, resource1, user.getId(), permission);
232     }
233     
234     /**
235      * Test the chacc method on a file and a group.<p>
236      *
237      * @throws Throwable if something goes wrong
238      */

239     public void testChaccFileGroup() throws Throwable JavaDoc {
240
241         CmsObject cms = getCmsObject();
242         echo("Testing chacc on a file and a group");
243         chaccFileGroup(this, cms, "/index.html", cms.readGroup("Users"), CmsPermissionSet.ACCESS_READ, CmsAccessControlEntry.ACCESS_FLAGS_OVERWRITE);
244     }
245     
246     /**
247      * Test the chacc method on a file and a user.<p>
248      *
249      * @throws Throwable if something goes wrong
250      */

251     public void testChaccFileUser() throws Throwable JavaDoc {
252
253         CmsObject cms = getCmsObject();
254         echo("Testing chacc on a file and a user");
255         chaccFileUser(this, cms, "/folder1/index.html", cms.readUser("Guest"), CmsPermissionSet.ACCESS_WRITE, 0);
256     }
257     
258     /**
259      * Test the chacc method on a folder and a group.<p>
260      *
261      * @throws Throwable if something goes wrong
262      */

263     public void testChaccFolderGroup() throws Throwable JavaDoc {
264         //TODO: This test is not working correctly so far!
265
CmsObject cms = getCmsObject();
266         echo("Testing chacc on a folder and a group");
267         chaccFolderGroup(this, cms, "/folder2/", cms.readGroup("Guests"), CmsPermissionSet.ACCESS_READ, CmsAccessControlEntry.ACCESS_FLAGS_OVERWRITE + CmsAccessControlEntry.ACCESS_FLAGS_INHERIT);
268     }
269     
270     /**
271      * Test the creation and deletion of access control entries and checks permissions of a test user.<p>
272      *
273      * @throws Throwable if something goes wrong
274      */

275     public void testChaccAddRemove() throws Throwable JavaDoc {
276         
277         echo("Testing adding and removing ACEs on files and folders");
278         
279         CmsObject cms = getCmsObject();
280         cms.createGroup("Testgroup", "A test group", 0, null);
281         CmsGroup testGroup = cms.readGroup("Testgroup");
282         cms.createUser("testuser", "test", "A test user", null);
283         cms.addUserToGroup("testuser", "Testgroup");
284         CmsUser testUser = cms.readUser("testuser");
285         
286         CmsProject offline = cms.readProject("Offline");
287         
288         String JavaDoc resName = "/folder2/";
289         
290         cms.lockResource(resName);
291         cms.chacc(resName, I_CmsPrincipal.PRINCIPAL_USER, testUser.getName(), "+r+w+v+i");
292         cms.chacc(resName, I_CmsPrincipal.PRINCIPAL_GROUP, testGroup.getName(), "+r+v+i");
293         cms.unlockResource(resName);
294         cms.publishProject();
295         
296         CmsPermissionSet permissions = new CmsPermissionSet(CmsPermissionSet.PERMISSION_READ
297             | CmsPermissionSet.PERMISSION_VIEW
298             | CmsPermissionSet.PERMISSION_WRITE, 0);
299         
300         
301         // check set permissions for the test user
302
cms.loginUser("testuser", "test");
303         cms.getRequestContext().setCurrentProject(offline);
304         cms.lockResource(resName);
305         assertTrue(cms.hasPermissions(cms.readResource(resName), permissions));
306         assertTrue(cms.hasPermissions(cms.readResource("/folder2/index.html"), permissions));
307         boolean success = false;
308         try {
309             assertFalse(cms.hasPermissions(cms.readResource("/folder1/"), permissions));
310         } catch (CmsException e) {
311             success = true;
312         }
313         if (! success) {
314             throw new Exception JavaDoc("Test user has permissions +r+v+w set on /folder1/");
315         }
316         cms.unlockResource(resName);
317         
318         // switch back to Admin user and remove ACE
319
cms.loginUser("Admin", "admin");
320         cms.getRequestContext().setCurrentProject(offline);
321         cms.lockResource(resName);
322         cms.rmacc(resName, I_CmsPrincipal.PRINCIPAL_USER, testUser.getName());
323         cms.unlockResource(resName);
324         cms.publishProject();
325         
326         cms.loginUser("testuser", "test");
327         cms.getRequestContext().setCurrentProject(offline);
328         assertFalse(cms.hasPermissions(cms.readResource(resName), CmsPermissionSet.ACCESS_WRITE));
329         
330         cms.loginUser("Admin", "admin");
331         cms.getRequestContext().setCurrentProject(offline);
332         cms.lockResource(resName);
333         cms.rmacc(resName, I_CmsPrincipal.PRINCIPAL_GROUP, testGroup.getName());
334         cms.unlockResource(resName);
335         cms.publishProject();
336         
337         // re-check permissions of test user after removing ACE
338
cms.loginUser("testuser", "test");
339         success = false;
340         try {
341             assertFalse(cms.hasPermissions(cms.readResource(resName), permissions));
342         } catch (CmsException e) {
343             success = true;
344         }
345         if (! success) {
346             throw new Exception JavaDoc("Test user has permissions +r+v set on /folder2/ after removal of ACE");
347         }
348     }
349 }
Popular Tags