1 22 package org.jboss.test.security.test.authorization; 23 24 import java.rmi.RemoteException ; 25 26 import javax.rmi.PortableRemoteObject ; 27 import javax.security.auth.login.LoginContext ; 28 29 import junit.extensions.TestSetup; 30 import junit.framework.Test; 31 import junit.framework.TestSuite; 32 33 import org.jboss.test.JBossTestCase; 34 import org.jboss.test.JBossTestSetup; 35 import org.jboss.test.security.interfaces.StatelessSession; 36 import org.jboss.test.security.interfaces.StatelessSessionHome; 37 import org.jboss.test.util.AppCallbackHandler; 38 39 41 47 public class XACMLEJBIntegrationUnitTest extends JBossTestCase 48 { 49 50 static String username = "scott"; 51 static char[] password = "echoman".toCharArray(); 52 53 LoginContext lc; 54 boolean loggedIn; 55 56 private static String login_config = "security/authorization/xacml-ejb/app-policy-service.xml"; 57 58 public XACMLEJBIntegrationUnitTest(String name) 59 { 60 super(name); 61 } 62 63 public static Test suite() throws Exception 64 { 65 TestSuite suite = new TestSuite(); 66 suite.addTest(new TestSuite(XACMLEJBIntegrationUnitTest.class)); 67 TestSetup wrapper = new JBossTestSetup(suite) 69 { 70 protected void setUp() throws Exception 71 { 72 super.setUp(); 73 deploy("xacml-ejb.jar"); 74 deploy(getResourceURL(login_config)); 75 } 76 protected void tearDown() throws Exception 77 { 78 undeploy(getResourceURL(login_config)); 79 undeploy("xacml-ejb.jar"); 80 super.tearDown(); 81 } 82 }; 83 return wrapper; 84 } 85 86 87 91 public void testMethodAccess() throws Exception 92 { 93 log.debug("+++ testMethodAccess"); 94 login(); 95 Object obj = getInitialContext().lookup("spec.StatelessSession"); 96 obj = PortableRemoteObject.narrow(obj, StatelessSessionHome.class); 97 StatelessSessionHome home = (StatelessSessionHome) obj; 98 log.debug("Found StatelessSessionHome"); 99 StatelessSession bean = home.create(); 100 log.debug("Created spec.StatelessSession"); 101 log.debug("Bean.echo('Hello') -> "+bean.echo("Hello")); 102 103 try 104 { 105 bean.noop(); 107 fail("Was able to call StatelessSession.noop"); 108 } 109 catch(RemoteException e) 110 { 111 log.debug("StatelessSession.noop failed as expected"); 112 } 113 bean.remove(); 114 logout(); 115 } 116 117 120 private void login() throws Exception 121 { 122 login(username, password); 123 } 124 private void login(String username, char[] password) throws Exception 125 { 126 if( loggedIn ) 127 return; 128 129 lc = null; 130 String confName = System.getProperty("conf.name", "spec-test"); 131 AppCallbackHandler handler = new AppCallbackHandler(username, password); 132 log.debug("Creating LoginContext("+confName+")"); 133 lc = new LoginContext (confName, handler); 134 lc.login(); 135 log.debug("Created LoginContext, subject="+lc.getSubject()); 136 loggedIn = true; 137 } 138 139 private void logout() throws Exception 140 { 141 if( loggedIn ) 142 { 143 loggedIn = false; 144 lc.logout(); 145 } 146 } 147 } 148 | Popular Tags |