KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > clients > security > A_AccessControl


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: A_AccessControl.java,v 1.9 2004/10/20 14:42:29 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.clients.security;
27
28
29 import java.rmi.RemoteException JavaDoc;
30 import java.util.Hashtable JavaDoc;
31 import java.util.Vector JavaDoc;
32
33 import org.objectweb.jonas.jtests.beans.secured.BaseS;
34 import org.objectweb.jonas.jtests.util.JTestCase;
35 import org.objectweb.security.context.SecurityContext;
36 import org.objectweb.security.context.SecurityCurrent;
37
38
39 /**
40  * Security Management common tests for all type of beans (Entity/Session)
41  *
42  * @author Ph.Coq, Ph.Durieux
43  *
44  */

45
46 public abstract class A_AccessControl extends JTestCase {
47
48     protected static String JavaDoc PRINCIPAL1_NAME = "principal1";
49     protected static String JavaDoc PRINCIPAL3_NAME = "principal3";
50     protected static String JavaDoc ROLE1_NAME = "baserole1";
51     protected static String JavaDoc ROLE2_NAME = "baserole2";
52
53     protected static SecurityCurrent current = null;
54     protected static SecurityContext principal1 = null;
55     protected static SecurityContext principal2 = null;
56     protected static SecurityContext principal3 = null;
57     protected static SecurityContext principal4 = null;
58
59
60     public A_AccessControl(String JavaDoc name) {
61         super(name);
62     }
63
64     /**
65      * init environment:
66      * - load beans
67      */

68     protected void setUp() {
69         super.setUp();
70         if (current == null) {
71             current = SecurityCurrent.getCurrent();
72             principal1 = new SecurityContext("principal1", new String JavaDoc[] {"role1"});
73             principal2 = new SecurityContext("principal2", new String JavaDoc[] {"role2"});
74             String JavaDoc[] roles3 = new String JavaDoc[]{"role1", "role3"};
75             principal3 = new SecurityContext(PRINCIPAL3_NAME, roles3);
76             String JavaDoc[] roles4 = new String JavaDoc[]{"role2"};
77             principal4 = new SecurityContext("principal4", roles4);
78         }
79     }
80
81     public abstract BaseS getBaseS(String JavaDoc name) throws Exception JavaDoc;
82     public abstract void removeBaseS(String JavaDoc name) throws Exception JavaDoc;
83
84     /**
85      * test getCallerPrincipal.
86      * The Principal must be propagated.
87      */

88     public void testGetCallerPrincipal() throws Exception JavaDoc {
89         current.setSecurityContext(principal1);
90         BaseS sl = getBaseS("un");
91         assertEquals(PRINCIPAL1_NAME, sl.getPrincipalName());
92         sl.remove();
93     }
94
95     /**
96      * test isCallerInRole.
97      * principal1 = role1
98      * principal2 = role2
99      */

100     public void testIsCallerInRole() throws Exception JavaDoc {
101         current.setSecurityContext(principal1);
102         BaseS sl = getBaseS("deux");
103         assertTrue(sl.isCallerInRole(ROLE1_NAME) == true);
104         assertTrue(sl.isCallerInRole(ROLE2_NAME) == false);
105         sl.remove();
106     }
107
108     /**
109      * test testIsCallerInRoleRolesInContext.
110      * principal1 = role1 (jonas-users.properties)
111      * principal2 = role2 (jonas-users.properties)
112      * principal3 = role1, role3 (role with security context)
113      * principal4 = role2 (role with security context)
114      */

115     public void testIsCallerInRoleRolesInContext() throws Exception JavaDoc {
116         current.setSecurityContext(principal3);
117         BaseS sl = getBaseS("deuxbis");
118         assertTrue(sl.isCallerInRole(ROLE1_NAME) == true);
119         assertTrue(sl.isCallerInRole(ROLE2_NAME) == false);
120         sl.remove();
121     }
122
123     /**
124      * test basic method reject
125      */

126     public void testBasicMethodReject() throws Exception JavaDoc {
127         current.setSecurityContext(principal1);
128         BaseS sl = getBaseS("trois");
129         try {
130             sl.simpleMethod();
131             fail("should be rejected: not in the role");
132         } catch (RemoteException JavaDoc e) {
133         }
134         sl.remove();
135     }
136
137     /**
138      * test basic method reject
139      */

140     public void testBasicMethodRejectRolesInContext() throws Exception JavaDoc {
141         current.setSecurityContext(principal3);
142         BaseS sl = getBaseS("troisbis");
143         try {
144             sl.simpleMethod();
145             fail("should be rejected: not in the role");
146         } catch (RemoteException JavaDoc e) {
147         }
148         sl.remove();
149     }
150
151
152     /**
153      * test basic method accept
154      */

155     public void testBasicMethodAccept() throws Exception JavaDoc {
156         current.setSecurityContext(principal2);
157         BaseS sl = getBaseS("quatre");
158         sl.simpleMethod();
159         sl.remove();
160     }
161
162     /**
163      * test basic method accept
164      */

165     public void testBasicMethodAcceptRolesInContext() throws Exception JavaDoc {
166         current.setSecurityContext(principal4);
167         BaseS sl = getBaseS("quatrebis");
168         sl.simpleMethod();
169         sl.remove();
170     }
171
172     /**
173      * test complex method reject
174      */

175     public void testComplexMethodReject() throws Exception JavaDoc {
176         current.setSecurityContext(principal1);
177         BaseS sl = getBaseS("cinq");
178         try {
179             Hashtable JavaDoc ht = new Hashtable JavaDoc();
180             ht.put("foo", new Vector JavaDoc(10));
181             ht.put("bar", new Hashtable JavaDoc());
182             Object JavaDoc[] o = {"bar", new Hashtable JavaDoc(), new Vector JavaDoc()};
183             sl.complexMethod(ht, o);
184             fail("should be rejected: not in the role");
185         } catch (RemoteException JavaDoc e) {
186         }
187         sl.remove();
188     }
189
190     /**
191      * test complex method reject
192      */

193     public void testComplexMethodRejectRolesInContext() throws Exception JavaDoc {
194         current.setSecurityContext(principal3);
195         BaseS sl = getBaseS("cinqbis");
196         try {
197             Hashtable JavaDoc ht = new Hashtable JavaDoc();
198             ht.put("foo", new Vector JavaDoc(10));
199             ht.put("bar", new Hashtable JavaDoc());
200             Object JavaDoc[] o = {"bar", new Hashtable JavaDoc(), new Vector JavaDoc()};
201             sl.complexMethod(ht, o);
202             fail("should be rejected: not in the role");
203         } catch (RemoteException JavaDoc e) {
204         }
205         sl.remove();
206     }
207
208     /**
209      * test complex method accept
210      */

211     public void testComplexMethodAccept() throws Exception JavaDoc {
212         current.setSecurityContext(principal2);
213         BaseS sl = getBaseS("six");
214         Hashtable JavaDoc ht = new Hashtable JavaDoc();
215         ht.put("foo", new Vector JavaDoc(10));
216         ht.put("bar", new Hashtable JavaDoc());
217         Object JavaDoc[] o = {"bar", new Hashtable JavaDoc(), new Vector JavaDoc()};
218         sl.complexMethod(ht, o);
219         sl.remove();
220     }
221
222     /**
223      * test complex method accept
224      */

225     public void testComplexMethodAcceptRolesInContext() throws Exception JavaDoc {
226         current.setSecurityContext(principal4);
227         BaseS sl = getBaseS("sixbis");
228         Hashtable JavaDoc ht = new Hashtable JavaDoc();
229         ht.put("foo", new Vector JavaDoc(10));
230         ht.put("bar", new Hashtable JavaDoc());
231         Object JavaDoc[] o = {"bar", new Hashtable JavaDoc(), new Vector JavaDoc()};
232         sl.complexMethod(ht, o);
233         sl.remove();
234     }
235
236     /**
237      * test security-role-ref in DD
238      * baserole -> role1
239      */

240     public void testSecurityRoleRef() throws Exception JavaDoc {
241         current.setSecurityContext(principal1);
242         BaseS sl = getBaseS("sept");
243         assertTrue(sl.isCallerInRole(ROLE1_NAME) == true);
244         sl.remove();
245     }
246
247     /**
248      * test security-role-ref in DD
249      * baserole -> role1
250      */

251     public void testSecurityRoleRefRolesInContext() throws Exception JavaDoc {
252         current.setSecurityContext(principal3);
253         BaseS sl = getBaseS("septbis");
254         assertTrue(sl.isCallerInRole(ROLE1_NAME) == true);
255         sl.remove();
256     }
257
258     /**
259      * test principal propagation from bean to bean
260      */

261     public void testBeanToBeanPropagation() throws Exception JavaDoc {
262         current.setSecurityContext(principal1);
263         BaseS sl = getBaseS("sept");
264         assertEquals(PRINCIPAL1_NAME, sl.getPrincipalNameOfAnotherBean());
265         sl.remove();
266     }
267
268     /**
269      * test principal propagation from bean to bean
270      */

271     public void testBeanToBeanPropagationRolesInContext() throws Exception JavaDoc {
272         current.setSecurityContext(principal3);
273         BaseS sl = getBaseS("sept");
274         assertEquals(PRINCIPAL3_NAME, sl.getPrincipalNameOfAnotherBean());
275         sl.remove();
276     }
277
278     /**
279      * test principal propagation from bean to bean and access is denied
280      */

281     public void testRejectBeanToBeanAccess() throws Exception JavaDoc {
282         current.setSecurityContext(principal2);
283         BaseS sl = getBaseS("huit");
284         try {
285             sl.getPrincipalNameOfAnotherBean();
286             fail("should be rejected: not in the role");
287         } catch (RemoteException JavaDoc e) {
288         } finally {
289             // sl should have been discarded.
290
removeBaseS("huit");
291         }
292     }
293
294     /**
295      * test principal propagation from bean to bean and access is denied
296      */

297     public void testRejectBeanToBeanAccessRolesInContext() throws Exception JavaDoc {
298         current.setSecurityContext(principal4);
299         BaseS sl = getBaseS("huitbis");
300         try {
301             sl.getPrincipalNameOfAnotherBean();
302             fail("should be rejected: not in the role");
303         } catch (RemoteException JavaDoc e) {
304         } finally {
305             // sl should have been discarded.
306
removeBaseS("huitbis");
307         }
308     }
309
310     /**
311      * test accept access to a local method
312      * callAnotherMethod is called with role1 and call DerivedSF.anotheMethod with this role
313      * return false if access to anotheMethod was denied
314      * expected return is true
315      */

316     public void testLocalMethodAccept() throws Exception JavaDoc {
317         current.setSecurityContext(principal1);
318         BaseS sl = getBaseS("neuf");
319         assertTrue(sl.callAnotherMethod() == true);
320         sl.remove();
321     }
322
323     /**
324      * test accept access to a local method
325      * callAnotherMethod is called with role1 and call DerivedSF.anotheMethod with this role
326      * return false if access to anotheMethod was denied
327      * expected return is true
328      */

329     public void testLocalMethodAcceptRolesInContext() throws Exception JavaDoc {
330         current.setSecurityContext(principal3);
331         BaseS sl = getBaseS("neufbis");
332         assertTrue(sl.callAnotherMethod() == true);
333         sl.remove();
334     }
335
336     /**
337      * test reject access to a local method
338      * callAnotherMethod is called with role1 and call DerivedSF.anotheMethod with this role
339      * return false if access to anotheMethod was denied
340      * expected return is true
341      */

342     public void testLocalMethodReject() throws Exception JavaDoc {
343         current.setSecurityContext(principal2);
344         BaseS sl = getBaseS("dix");
345         assertTrue(sl.callAnotherMethod() == false);
346         sl.remove();
347     }
348
349     /**
350      * test reject access to a local method
351      * callAnotherMethod is called with role1 and call DerivedSF.anotheMethod with this role
352      * return false if access to anotheMethod was denied
353      * expected return is true
354      */

355     public void testLocalMethodRejectRolesInContext() throws Exception JavaDoc {
356         current.setSecurityContext(principal4);
357         BaseS sl = getBaseS("dixbis");
358         assertTrue(sl.callAnotherMethod() == false);
359         sl.remove();
360     }
361     
362     /**
363      * test on an exluded method (excluded list)
364      */

365     public void testExcludedMethod() throws Exception JavaDoc {
366         current.setSecurityContext(principal2);
367         BaseS sl = getBaseS("excluded");
368         try {
369             sl.excludedMethod();
370             fail("should be excluded");
371         } catch (RemoteException JavaDoc e) {
372         }
373         sl.remove();
374     }
375     
376     /**
377      * test timeout
378      */

379     public void testTimeout() throws Exception JavaDoc {
380         current.setSecurityContext(principal2);
381         int duration = 5;
382         BaseS sl = getBaseS("timed");
383         try {
384             int oldval = sl.getTimerCount();
385             sl.setTimer(duration, 0, 0);
386             sleep(2000);
387             assertEquals("timer expired too quickly", oldval, sl.getTimerCount());
388             sleep(4000);
389             assertEquals("timer did not expired", oldval + 1, sl.getTimerCount());
390         } finally {
391             sl.remove();
392         }
393     }
394 }
395
Popular Tags