1 22 package org.jboss.test.jacc.test; 23 24 import javax.rmi.PortableRemoteObject ; 25 import javax.security.auth.login.Configuration ; 26 import javax.security.auth.login.LoginContext ; 27 28 import junit.extensions.TestSetup; 29 import junit.framework.Test; 30 import junit.framework.TestSuite; 31 32 import org.jboss.security.auth.login.XMLLoginConfigImpl; 33 import org.jboss.test.JBossTestCase; 34 import org.jboss.test.JBossTestSetup; 35 import org.jboss.test.security.interfaces.UsefulStatelessSession; 36 import org.jboss.test.security.interfaces.UsefulStatelessSessionHome; 37 import org.jboss.test.util.AppCallbackHandler; 38 39 41 47 public class CallerInRoleUnitTestCase extends JBossTestCase 48 { 49 LoginContext lc; 50 public CallerInRoleUnitTestCase(String name) 51 { 52 super(name); 53 } 54 55 public void testCallerInRoleForBean1() throws Exception 56 { 57 login("anil","opensource".toCharArray()); 58 Object obj = getInitialContext().lookup("bean1.UsefulStatelessSessionBean"); 59 obj = PortableRemoteObject.narrow(obj, UsefulStatelessSessionHome.class); 60 UsefulStatelessSessionHome home = (UsefulStatelessSessionHome) obj; 61 UsefulStatelessSession bean = home.create(); 62 assertEquals("NiceUser is true", "true", ""+bean.isCallerInRole("NiceUser") ); 63 assertEquals("BadRole is false", "false", ""+bean.isCallerInRole("BadRole") ); 64 bean.remove(); 65 logout(); 66 } 67 68 private void login(String username, char[] password) throws Exception 69 { 70 lc = null; 71 String confName = System.getProperty("conf.name", "spec-test"); 72 AppCallbackHandler handler = new AppCallbackHandler(username, password); 73 log.debug("Creating LoginContext("+confName+")"); 74 lc = new LoginContext (confName, handler); 75 lc.login(); 76 log.debug("Created LoginContext, subject="+lc.getSubject()); 77 } 78 79 private void logout() throws Exception 80 { 81 lc.logout(); 82 } 83 84 87 public static Test suite() throws Exception 88 { 89 TestSuite suite = new TestSuite(); 90 suite.addTest(new TestSuite(CallerInRoleUnitTestCase.class)); 91 92 TestSetup wrapper = new JBossTestSetup(suite) 94 { 95 protected void setUp() throws Exception 96 { 97 super.setUp(); 98 Configuration.setConfiguration(new XMLLoginConfigImpl()); 99 redeploy("security-jacc-callerinrole.jar"); 100 } 101 protected void tearDown() throws Exception 102 { 103 undeploy("security-jacc-callerinrole.jar"); 104 super.tearDown(); 105 } 106 }; 107 return wrapper; 108 } 109 } 110 | Popular Tags |