1 22 23 package org.jboss.ejb3.test.jacc.unit; 24 25 import javax.ejb.EJBException ; 26 import org.jboss.ejb3.test.jacc.AllEntity; 27 import org.jboss.ejb3.test.jacc.SomeEntity; 28 import org.jboss.ejb3.test.jacc.StarEntity; 29 import org.jboss.ejb3.test.jacc.Stateful; 30 import org.jboss.ejb3.test.jacc.Stateless; 31 import org.jboss.security.SecurityAssociation; 32 import org.jboss.security.SimplePrincipal; 33 import org.jboss.test.JBossTestCase; 34 import junit.framework.Test; 35 36 41 public class JaccTestCase extends JBossTestCase 42 { 43 org.jboss.logging.Logger log = getLog(); 44 45 static boolean deployed = false; 46 static int test = 0; 47 48 public JaccTestCase(String name) 49 { 50 super(name); 51 } 52 53 public void testUnchecked() throws Exception 54 { 55 Stateful stateful = (Stateful)getInitialContext().lookup("StatefulBean/remote"); 56 Stateless stateless = (Stateless)getInitialContext().lookup("StatelessBean/remote"); 57 58 SecurityAssociation.setPrincipal(new SimplePrincipal("somebody")); 59 SecurityAssociation.setCredential("password".toCharArray()); 60 61 int result = stateful.unchecked(1); 62 assertEquals(1,result); 63 64 result = stateless.unchecked(10); 65 assertEquals(10,result); 66 67 SecurityAssociation.setPrincipal(new SimplePrincipal("rolefail")); 68 SecurityAssociation.setCredential("password".toCharArray()); 69 70 result = stateful.unchecked(100); 71 assertEquals(100,result); 72 73 result = stateless.unchecked(99); 74 assertEquals(99,result); 75 76 } 77 78 79 public void testChecked() throws Exception 80 { 81 Stateful stateful = (Stateful)getInitialContext().lookup("StatefulBean/remote"); 82 Stateless stateless = (Stateless)getInitialContext().lookup("StatelessBean/remote"); 83 84 SecurityAssociation.setPrincipal(new SimplePrincipal("somebody")); 85 SecurityAssociation.setCredential("password".toCharArray()); 86 87 int result = stateful.checked(5); 88 assertEquals(5,result); 89 90 result = stateless.checked(50); 91 assertEquals(50,result); 92 93 SecurityAssociation.setPrincipal(new SimplePrincipal("rolefail")); 94 SecurityAssociation.setCredential("password".toCharArray()); 95 96 boolean securityException = false; 97 try 98 { 99 stateful.checked(500); 100 } 101 catch (SecurityException e){ 102 securityException = true; 103 } 104 105 assertTrue(securityException); 106 107 try 108 { 109 stateless.checked(501); 110 } 111 catch (SecurityException e){ 112 securityException = true; 113 } 114 115 assertTrue(securityException); 116 } 117 118 public void testAllEntity()throws Exception 119 { 120 Stateless stateless = (Stateless)getInitialContext().lookup("StatelessBean/remote"); 121 SecurityAssociation.setPrincipal(new SimplePrincipal("somebody")); 122 SecurityAssociation.setCredential("password".toCharArray()); 123 124 System.out.println("Good role"); 125 System.out.println("Inserting..."); 126 AllEntity e = stateless.insertAllEntity(); 127 System.out.println("Reading..."); 128 e = stateless.readAllEntity(e.id); 129 e.val += "y"; 130 System.out.println("Updating..."); 131 stateless.updateAllEntity(e); 132 System.out.println("Deleting..."); 133 stateless.deleteAllEntity(e); 134 System.out.println("Inserting..."); 135 e = stateless.insertAllEntity(); 136 137 System.out.println("Bad role"); 138 SecurityAssociation.setPrincipal(new SimplePrincipal("rolefail")); 139 SecurityAssociation.setCredential("password".toCharArray()); 140 141 AllEntity ae2 = null; 142 try 143 { 144 System.out.println("Inserting..."); 145 ae2 = stateless.insertAllEntity(); 146 throw new FailedException("Insert check not done for AllEntity"); 147 } 148 catch(FailedException ex) 149 { 150 throw ex; 151 } 152 catch (Exception ex) 153 { 154 hasSecurityOrEJBException(ex); 155 } 156 157 try 158 { 159 System.out.println("Reading..."); 160 ae2 = stateless.readAllEntity(e.id); 161 throw new FailedException("Read check not done for AllEntity"); 162 } 163 catch(FailedException ex) 164 { 165 throw ex; 166 } 167 catch (Exception ex) 168 { 169 hasSecurityOrEJBException(ex); 170 } 171 172 try 173 { 174 e.val += "y"; 175 stateless.updateAllEntity(e); 176 throw new FailedException("Update check not done for AllEntity"); 177 } 178 catch(FailedException ex) 179 { 180 throw ex; 181 } 182 catch (Exception ex) 183 { 184 hasSecurityOrEJBException(ex); 185 } 186 187 try 188 { 189 stateless.deleteAllEntity(e); 190 throw new FailedException("Delete check not done for AllEntity"); 191 } 192 catch(FailedException ex) 193 { 194 throw ex; 195 } 196 catch (Exception ex) 197 { 198 hasSecurityOrEJBException(ex); 199 } 200 201 try 202 { 203 e = stateless.insertAllEntity(); 204 throw new FailedException("Insert check not done for AllEntity"); 205 } 206 catch(FailedException ex) 207 { 208 throw ex; 209 } 210 catch (Exception ex) 211 { 212 hasSecurityOrEJBException(ex); 213 } 214 } 215 216 217 public void testStarEntity()throws Exception 218 { 219 Stateless stateless = (Stateless)getInitialContext().lookup("StatelessBean/remote"); 220 SecurityAssociation.setPrincipal(new SimplePrincipal("somebody")); 221 SecurityAssociation.setCredential("password".toCharArray()); 222 223 System.out.println("Good role"); 224 System.out.println("Inserting..."); 225 StarEntity e = stateless.insertStarEntity(); 226 System.out.println("Reading..."); 227 e = stateless.readStarEntity(e.id); 228 e.val += "y"; 229 System.out.println("Updating..."); 230 stateless.updateStarEntity(e); 231 System.out.println("Deleting..."); 232 stateless.deleteStarEntity(e); 233 System.out.println("Inserting..."); 234 e = stateless.insertStarEntity(); 235 236 System.out.println("Bad role"); 237 SecurityAssociation.setPrincipal(new SimplePrincipal("rolefail")); 238 SecurityAssociation.setCredential("password".toCharArray()); 239 240 StarEntity ae2 = null; 241 try 242 { 243 System.out.println("Inserting..."); 244 ae2 = stateless.insertStarEntity(); 245 throw new FailedException("Insert check not done for StarEntity"); 246 } 247 catch(FailedException ex) 248 { 249 throw ex; 250 } 251 catch (Exception ex) 252 { 253 hasSecurityOrEJBException(ex); 254 } 255 256 try 257 { 258 System.out.println("Reading..."); 259 ae2 = stateless.readStarEntity(e.id); 260 throw new FailedException("Read check not done for StarEntity"); 261 } 262 catch(FailedException ex) 263 { 264 throw ex; 265 } 266 catch (Exception ex) 267 { 268 hasSecurityOrEJBException(ex); 269 } 270 271 try 272 { 273 e.val += "y"; 274 stateless.updateStarEntity(e); 275 throw new FailedException("Update check not done for StarEntity"); 276 } 277 catch(FailedException ex) 278 { 279 throw ex; 280 } 281 catch (Exception ex) 282 { 283 hasSecurityOrEJBException(ex); 284 } 285 286 try 287 { 288 stateless.deleteStarEntity(e); 289 throw new FailedException("Delete check not done for StarEntity"); 290 } 291 catch(FailedException ex) 292 { 293 throw ex; 294 } 295 catch (Exception ex) 296 { 297 hasSecurityOrEJBException(ex); 298 } 299 300 try 301 { 302 e = stateless.insertStarEntity(); 303 throw new FailedException("Insert check not done for StarEntity"); 304 } 305 catch(FailedException ex) 306 { 307 throw ex; 308 } 309 catch (Exception ex) 310 { 311 hasSecurityOrEJBException(ex); 312 } 313 } 314 315 public void testSomeEntity()throws Exception 316 { 317 Stateless stateless = (Stateless)getInitialContext().lookup("StatelessBean/remote"); 318 SecurityAssociation.setPrincipal(new SimplePrincipal("somebody")); 319 SecurityAssociation.setCredential("password".toCharArray()); 320 321 System.out.println("Good role"); 322 System.out.println("Inserting..."); 323 SomeEntity e = stateless.insertSomeEntity(); 324 325 try 326 { 327 System.out.println("Reading..."); 328 e = stateless.readSomeEntity(e.id); 329 throw new FailedException("Read check not done for SomeEntity"); 330 } 331 catch(FailedException ex) 332 { 333 throw ex; 334 } 335 catch (Exception ex) 336 { 337 hasSecurityOrEJBException(ex); 338 } 339 340 try 341 { 342 e.val += "y"; 343 System.out.println("Updating..."); 344 stateless.updateSomeEntity(e); 345 throw new FailedException("Update check not done for SomeEntity"); 346 } 347 catch(FailedException ex) 348 { 349 throw ex; 350 } 351 catch (Exception ex) 352 { 353 hasSecurityOrEJBException(ex); 354 } 355 356 357 System.out.println("Deleting..."); 358 stateless.deleteSomeEntity(e); 359 System.out.println("Inserting..."); 360 e = stateless.insertSomeEntity(); 361 362 System.out.println("Bad role"); 363 SecurityAssociation.setPrincipal(new SimplePrincipal("rolefail")); 364 SecurityAssociation.setCredential("password".toCharArray()); 365 366 SomeEntity ae2 = null; 367 try 368 { 369 System.out.println("Inserting..."); 370 ae2 = stateless.insertSomeEntity(); 371 throw new FailedException("Insert check not done for SomeEntity"); 372 } 373 catch(FailedException ex) 374 { 375 throw ex; 376 } 377 catch (Exception ex) 378 { 379 hasSecurityOrEJBException(ex); 380 } 381 382 try 383 { 384 stateless.deleteSomeEntity(e); 385 throw new FailedException("Delete check not done for SomeEntity"); 386 } 387 catch(FailedException ex) 388 { 389 throw ex; 390 } 391 catch (Exception ex) 392 { 393 hasSecurityOrEJBException(ex); 394 } 395 396 try 397 { 398 e = stateless.insertSomeEntity(); 399 throw new FailedException("Insert check not done for SomeEntity"); 400 } 401 catch(FailedException ex) 402 { 403 throw ex; 404 } 405 catch (Exception ex) 406 { 407 hasSecurityOrEJBException(ex); 408 } 409 } 410 411 412 private void hasSecurityOrEJBException(Exception e)throws FailedException 413 { 414 Throwable t = e; 415 416 while (t != null) 417 { 418 String classname = t.getClass().getName(); 420 if (classname.equals(SecurityException .class.getName()) || 421 classname.equals(EJBException .class.getName()) ) 422 { 423 return; 424 } 425 t = t.getCause(); 426 } 427 428 throw new FailedException("SecurityException not thrown"); 429 } 430 431 432 433 434 public static Test suite() throws Exception 435 { 436 return getDeploySetup(JaccTestCase.class, "jacc-test.jar"); 437 } 438 439 private class FailedException extends Exception 440 { 441 public FailedException(String msg) 442 { 443 super(msg); 444 } 445 } 446 } | Popular Tags |