1 25 26 package org.objectweb.jonas.jtests.clients.security; 27 28 29 import java.rmi.RemoteException ; 30 import java.util.Hashtable ; 31 import java.util.Vector ; 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 45 46 public abstract class A_AccessControl extends JTestCase { 47 48 protected static String PRINCIPAL1_NAME = "principal1"; 49 protected static String PRINCIPAL3_NAME = "principal3"; 50 protected static String ROLE1_NAME = "baserole1"; 51 protected static String 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 name) { 61 super(name); 62 } 63 64 68 protected void setUp() { 69 super.setUp(); 70 if (current == null) { 71 current = SecurityCurrent.getCurrent(); 72 principal1 = new SecurityContext("principal1", new String [] {"role1"}); 73 principal2 = new SecurityContext("principal2", new String [] {"role2"}); 74 String [] roles3 = new String []{"role1", "role3"}; 75 principal3 = new SecurityContext(PRINCIPAL3_NAME, roles3); 76 String [] roles4 = new String []{"role2"}; 77 principal4 = new SecurityContext("principal4", roles4); 78 } 79 } 80 81 public abstract BaseS getBaseS(String name) throws Exception ; 82 public abstract void removeBaseS(String name) throws Exception ; 83 84 88 public void testGetCallerPrincipal() throws Exception { 89 current.setSecurityContext(principal1); 90 BaseS sl = getBaseS("un"); 91 assertEquals(PRINCIPAL1_NAME, sl.getPrincipalName()); 92 sl.remove(); 93 } 94 95 100 public void testIsCallerInRole() throws Exception { 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 115 public void testIsCallerInRoleRolesInContext() throws Exception { 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 126 public void testBasicMethodReject() throws Exception { 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 e) { 133 } 134 sl.remove(); 135 } 136 137 140 public void testBasicMethodRejectRolesInContext() throws Exception { 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 e) { 147 } 148 sl.remove(); 149 } 150 151 152 155 public void testBasicMethodAccept() throws Exception { 156 current.setSecurityContext(principal2); 157 BaseS sl = getBaseS("quatre"); 158 sl.simpleMethod(); 159 sl.remove(); 160 } 161 162 165 public void testBasicMethodAcceptRolesInContext() throws Exception { 166 current.setSecurityContext(principal4); 167 BaseS sl = getBaseS("quatrebis"); 168 sl.simpleMethod(); 169 sl.remove(); 170 } 171 172 175 public void testComplexMethodReject() throws Exception { 176 current.setSecurityContext(principal1); 177 BaseS sl = getBaseS("cinq"); 178 try { 179 Hashtable ht = new Hashtable (); 180 ht.put("foo", new Vector (10)); 181 ht.put("bar", new Hashtable ()); 182 Object [] o = {"bar", new Hashtable (), new Vector ()}; 183 sl.complexMethod(ht, o); 184 fail("should be rejected: not in the role"); 185 } catch (RemoteException e) { 186 } 187 sl.remove(); 188 } 189 190 193 public void testComplexMethodRejectRolesInContext() throws Exception { 194 current.setSecurityContext(principal3); 195 BaseS sl = getBaseS("cinqbis"); 196 try { 197 Hashtable ht = new Hashtable (); 198 ht.put("foo", new Vector (10)); 199 ht.put("bar", new Hashtable ()); 200 Object [] o = {"bar", new Hashtable (), new Vector ()}; 201 sl.complexMethod(ht, o); 202 fail("should be rejected: not in the role"); 203 } catch (RemoteException e) { 204 } 205 sl.remove(); 206 } 207 208 211 public void testComplexMethodAccept() throws Exception { 212 current.setSecurityContext(principal2); 213 BaseS sl = getBaseS("six"); 214 Hashtable ht = new Hashtable (); 215 ht.put("foo", new Vector (10)); 216 ht.put("bar", new Hashtable ()); 217 Object [] o = {"bar", new Hashtable (), new Vector ()}; 218 sl.complexMethod(ht, o); 219 sl.remove(); 220 } 221 222 225 public void testComplexMethodAcceptRolesInContext() throws Exception { 226 current.setSecurityContext(principal4); 227 BaseS sl = getBaseS("sixbis"); 228 Hashtable ht = new Hashtable (); 229 ht.put("foo", new Vector (10)); 230 ht.put("bar", new Hashtable ()); 231 Object [] o = {"bar", new Hashtable (), new Vector ()}; 232 sl.complexMethod(ht, o); 233 sl.remove(); 234 } 235 236 240 public void testSecurityRoleRef() throws Exception { 241 current.setSecurityContext(principal1); 242 BaseS sl = getBaseS("sept"); 243 assertTrue(sl.isCallerInRole(ROLE1_NAME) == true); 244 sl.remove(); 245 } 246 247 251 public void testSecurityRoleRefRolesInContext() throws Exception { 252 current.setSecurityContext(principal3); 253 BaseS sl = getBaseS("septbis"); 254 assertTrue(sl.isCallerInRole(ROLE1_NAME) == true); 255 sl.remove(); 256 } 257 258 261 public void testBeanToBeanPropagation() throws Exception { 262 current.setSecurityContext(principal1); 263 BaseS sl = getBaseS("sept"); 264 assertEquals(PRINCIPAL1_NAME, sl.getPrincipalNameOfAnotherBean()); 265 sl.remove(); 266 } 267 268 271 public void testBeanToBeanPropagationRolesInContext() throws Exception { 272 current.setSecurityContext(principal3); 273 BaseS sl = getBaseS("sept"); 274 assertEquals(PRINCIPAL3_NAME, sl.getPrincipalNameOfAnotherBean()); 275 sl.remove(); 276 } 277 278 281 public void testRejectBeanToBeanAccess() throws Exception { 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 e) { 288 } finally { 289 removeBaseS("huit"); 291 } 292 } 293 294 297 public void testRejectBeanToBeanAccessRolesInContext() throws Exception { 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 e) { 304 } finally { 305 removeBaseS("huitbis"); 307 } 308 } 309 310 316 public void testLocalMethodAccept() throws Exception { 317 current.setSecurityContext(principal1); 318 BaseS sl = getBaseS("neuf"); 319 assertTrue(sl.callAnotherMethod() == true); 320 sl.remove(); 321 } 322 323 329 public void testLocalMethodAcceptRolesInContext() throws Exception { 330 current.setSecurityContext(principal3); 331 BaseS sl = getBaseS("neufbis"); 332 assertTrue(sl.callAnotherMethod() == true); 333 sl.remove(); 334 } 335 336 342 public void testLocalMethodReject() throws Exception { 343 current.setSecurityContext(principal2); 344 BaseS sl = getBaseS("dix"); 345 assertTrue(sl.callAnotherMethod() == false); 346 sl.remove(); 347 } 348 349 355 public void testLocalMethodRejectRolesInContext() throws Exception { 356 current.setSecurityContext(principal4); 357 BaseS sl = getBaseS("dixbis"); 358 assertTrue(sl.callAnotherMethod() == false); 359 sl.remove(); 360 } 361 362 365 public void testExcludedMethod() throws Exception { 366 current.setSecurityContext(principal2); 367 BaseS sl = getBaseS("excluded"); 368 try { 369 sl.excludedMethod(); 370 fail("should be excluded"); 371 } catch (RemoteException e) { 372 } 373 sl.remove(); 374 } 375 376 379 public void testTimeout() throws Exception { 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 |