1 22 package org.jboss.test.security.test; 23 24 import java.rmi.RemoteException ; 25 import javax.naming.InitialContext ; 26 import javax.rmi.PortableRemoteObject ; 27 import javax.security.auth.login.Configuration ; 28 import javax.security.auth.login.LoginContext ; 29 30 import org.jboss.security.auth.login.XMLLoginConfigImpl; 31 import org.jboss.test.JBossTestCase; 32 import org.jboss.test.JBossTestSetup; 33 import org.jboss.test.security.interfaces.IOSession; 34 import org.jboss.test.security.interfaces.IOSessionHome; 35 import org.jboss.test.security.interfaces.ReadAccessException; 36 import org.jboss.test.util.AppCallbackHandler; 37 38 import junit.extensions.TestSetup; 39 import junit.framework.Test; 40 import junit.framework.TestSuite; 41 42 43 48 public class SecurityProxyUnitTestCase 49 extends JBossTestCase 50 { 51 LoginContext lc; 52 boolean loggedIn; 53 54 public SecurityProxyUnitTestCase(String name) 55 { 56 super(name); 57 } 58 59 64 public void testMethodAccess() throws Exception 65 { 66 log.debug("+++ testMethodAccess"); 67 login(); 68 Object obj = getInitialContext().lookup("security-proxy/ProxiedStatelessBean"); 69 obj = PortableRemoteObject.narrow(obj, IOSessionHome.class); 70 IOSessionHome home = (IOSessionHome) obj; 71 log.debug("Found IOSessionHome"); 72 IOSession bean = home.create(); 73 log.debug("Created IOSession"); 74 75 try 76 { 77 bean.read("/restricted/pgp.keys"); 79 fail("Was able to call read(/restricted/pgp.keys)"); 80 } 81 catch(RemoteException e) 82 { 83 log.debug("IOSession.read failed as expected"); 84 } 85 bean.read("/public/pgp.keys"); 86 87 try 88 { 89 bean.retryableRead("/restricted/pgp.keys"); 91 fail("Was able to call read(/restricted/pgp.keys)"); 92 } 93 catch(ReadAccessException e) 94 { 95 log.debug("IOSession.read failed as expected with ReadAccessException"); 96 bean.read("/public/pgp.keys"); 97 } 98 99 try 100 { 101 bean.write("/restricted/pgp.keys"); 103 fail("Was able to call write(/restricted/pgp.keys)"); 104 } 105 catch(RemoteException e) 106 { 107 log.debug("IOSession.write failed as expected"); 108 } 109 bean.write("/public/pgp.keys"); 110 111 bean.remove(); 112 } 113 114 117 private void login() throws Exception 118 { 119 login("jduke", "theduke".toCharArray()); 120 } 121 private void login(String username, char[] password) throws Exception 122 { 123 if( loggedIn ) 124 return; 125 126 lc = null; 127 String confName = System.getProperty("conf.name", "spec-test"); 128 AppCallbackHandler handler = new AppCallbackHandler(username, password); 129 log.debug("Creating LoginContext("+confName+")"); 130 lc = new LoginContext (confName, handler); 131 lc.login(); 132 log.debug("Created LoginContext, subject="+lc.getSubject()); 133 loggedIn = true; 134 } 135 private void logout() throws Exception 136 { 137 if( loggedIn ) 138 { 139 loggedIn = false; 140 lc.logout(); 141 } 142 } 143 144 145 148 public static Test suite() throws Exception 149 { 150 TestSuite suite = new TestSuite(); 151 suite.addTest(new TestSuite(SecurityProxyUnitTestCase.class)); 152 153 TestSetup wrapper = new JBossTestSetup(suite) 155 { 156 protected void setUp() throws Exception 157 { 158 super.setUp(); 159 Configuration.setConfiguration(new XMLLoginConfigImpl()); 160 redeploy("security-proxy.jar"); 161 flushAuthCache(); 162 } 163 protected void tearDown() throws Exception 164 { 165 undeploy("security-proxy.jar"); 166 super.tearDown(); 167 168 } 169 }; 170 return wrapper; 171 } 172 173 } 174 | Popular Tags |