1 25 package org.objectweb.easybeans.tests.common.ejbs.stateful.containermanaged.security; 26 27 import static org.testng.Assert.assertFalse; 28 import static org.testng.Assert.assertTrue; 29 import static org.testng.Assert.fail; 30 31 import javax.annotation.Resource; 32 import javax.annotation.security.RunAs; 33 import javax.ejb.EJBAccessException ; 34 import javax.ejb.SessionContext ; 35 36 import org.objectweb.easybeans.log.JLog; 37 import org.objectweb.easybeans.log.JLogFactory; 38 import org.objectweb.easybeans.tests.common.ejbs.base.security.ItfSecurityRoles; 39 40 46 @RunAs("mainrole") 47 public abstract class SecurityRolesTester implements ItfSecurityRolesTester { 48 49 52 private static JLog logger = JLogFactory.getLog(SecurityRolesTester.class); 53 54 58 public abstract ItfSecurityRoles getBean(); 59 60 63 @Resource 64 private SessionContext sessionContext; 65 66 69 public void testGetCallerPrincipalSameCaller() { 70 assertTrue(getBean().testCallerPrincipal(), "The method getCallerPrincipal is not working properly. Two bean" 71 + " with the same caller returned different values."); 72 } 73 74 78 public void testGetCallerPrincipalDifferentCaller() { 79 assertFalse(getBean().getCallerPrincipal().equals(sessionContext.getCallerPrincipal()), 80 "The method getCallerPrincipal is not working properly. The bean has a RunAs " 81 + "definition in the class, but this is valid only for the callee.Consequently, " 82 + "the getCallerPrincipal in this method and in the callee must be different."); 83 } 84 85 89 public void testIsCallerInRoleIncorrect() { 90 assertFalse(getBean().isCallerinRole("secondaryrole"), 91 "The caller has the runAs = secondaryrole and the method isCallerInRole in the callee returns true"); 92 } 93 94 98 public void testIsCallerInRoleCorrect() { 99 assertTrue(getBean().isCallerinRole("mainrole"), 100 "The caller has the runAs = mainrole and the method isCallerInRole in the callee returns false"); 101 } 102 103 107 @SuppressWarnings ("deprecation") 108 public void testGetCallerIdentity() { 109 try { 110 sessionContext.getCallerIdentity(); 111 fail("The container did not throw an exception when the method getCallerIdentity was called."); 112 } catch (RuntimeException e) { 113 logger.debug("The bean threw an expected exception {0}", e); 114 } 115 } 116 117 121 @SuppressWarnings ("deprecation") 122 public void testIsCallerInRoleDeprecated() { 123 try { 124 sessionContext.isCallerInRole(new DummyIdentity()); 125 fail("The container did not throw an exception when the method isCallerInRole(Identity arg) was called."); 126 } catch (RuntimeException e) { 127 logger.debug("The bean threw an expected exception {0}", e); 128 } 129 } 130 131 134 public void testPermitAll() { 135 getBean().permitAllAttribute(); 136 } 137 138 141 public void testDenyAll() { 142 try{ 143 getBean().denyAllAttribute(); 144 fail("The method has a denyAll annotation, so the bean cannot call this method."); 145 }catch(EJBAccessException e){ 146 logger.debug("The bean threw an expected exception {0}", e); 147 } 148 149 } 150 151 155 public void testAllowedRolesWithTwoRoles() { 156 getBean().permitTwoRoles(); 157 } 158 159 163 public void testAllowedRolesWithOneRole() { 164 getBean().permitOneRole(); 165 } 166 167 } 168 | Popular Tags |