1 22 package org.jboss.test.security.test; 23 24 import java.net.URL ; 25 import javax.rmi.PortableRemoteObject ; 26 import javax.security.auth.login.Configuration ; 27 import javax.security.auth.login.LoginContext ; 28 29 import org.jboss.security.auth.login.XMLLoginConfigImpl; 30 import org.jboss.test.JBossTestCase; 31 import org.jboss.test.JBossTestSetup; 32 import org.jboss.test.security.interfaces.CustomPrincipalHome; 33 import org.jboss.test.security.interfaces.CustomPrincipal; 34 import org.jboss.test.security.ejb.CustomPrincipalImpl; 35 import org.jboss.test.util.AppCallbackHandler; 36 import org.jboss.test.util.web.HttpUtils; 37 38 import junit.extensions.TestSetup; 39 import junit.framework.Test; 40 import junit.framework.TestSuite; 41 42 43 48 public class JaasUnitTestCase 49 extends JBossTestCase 50 { 51 static String username = "jduke"; 52 static char[] password = "theduke".toCharArray(); 53 54 LoginContext lc; 55 boolean loggedIn; 56 57 public JaasUnitTestCase(String name) 58 { 59 super(name); 60 } 61 62 64 public void testCustomEJBPrincipal() throws Exception 65 { 66 login(); 67 log.debug("+++ testCustomEJBPrincipal()"); 68 Object obj = getInitialContext().lookup("jaas.CustomPrincipalHome"); 69 obj = PortableRemoteObject.narrow(obj, CustomPrincipalHome.class); 70 CustomPrincipalHome home = (CustomPrincipalHome) obj; 71 log.debug("Found CustomPrincipalHome"); 72 CustomPrincipal bean = home.create(); 73 log.debug("Created CustomPrincipal"); 74 75 boolean isCustomType = bean.validateCallerPrincipal(CustomPrincipalImpl.class); 76 bean.remove(); 77 logout(); 78 assertTrue("CustomPrincipalImpl was seen", isCustomType); 79 } 80 81 84 public void testCustomEJBPrincipal2() throws Exception 85 { 86 login(); 87 log.debug("+++ testCustomEJBPrincipal()"); 88 Object obj = getInitialContext().lookup("jaas.CustomPrincipal2Home"); 89 obj = PortableRemoteObject.narrow(obj, CustomPrincipalHome.class); 90 CustomPrincipalHome home = (CustomPrincipalHome) obj; 91 log.debug("Found CustomPrincipalHome"); 92 CustomPrincipal bean = home.create(); 93 log.debug("Created CustomPrincipal"); 94 95 boolean isCustomType = bean.validateCallerPrincipal(CustomPrincipalImpl.class); 96 bean.remove(); 97 logout(); 98 assertTrue("CustomPrincipalImpl was seen", isCustomType); 99 } 100 101 public void testCustomWebPrincipal() throws Exception 102 { 103 log.debug("+++ testCustomWebPrincipal()"); 104 String base = HttpUtils.getBaseURL(); 105 URL testURL = new URL (base + "jaas/CustomPrincipalServlet" 106 +"?type="+CustomPrincipalImpl.class.getName()); 107 HttpUtils.accessURL(testURL); 108 } 109 110 113 private void login() throws Exception 114 { 115 login(username, password); 116 } 117 private void login(String username, char[] password) throws Exception 118 { 119 if( loggedIn ) 120 return; 121 122 lc = null; 123 String confName = System.getProperty("conf.name", "jaas-test"); 124 AppCallbackHandler handler = new AppCallbackHandler(username, password); 125 log.debug("Creating LoginContext("+confName+")"); 126 lc = new LoginContext (confName, handler); 127 lc.login(); 128 log.debug("Created LoginContext, subject="+lc.getSubject()); 129 loggedIn = true; 130 } 131 private void logout() throws Exception 132 { 133 if( loggedIn ) 134 { 135 loggedIn = false; 136 lc.logout(); 137 } 138 } 139 140 141 144 public static Test suite() throws Exception 145 { 146 TestSuite suite = new TestSuite(); 147 suite.addTest(new TestSuite(JaasUnitTestCase.class)); 148 149 TestSetup wrapper = new JBossTestSetup(suite) 151 { 152 protected void setUp() throws Exception 153 { 154 super.setUp(); 155 Configuration.setConfiguration(new XMLLoginConfigImpl()); 156 deploy("security-jaas.ear"); 157 flushAuthCache("jaas-test"); 158 } 159 protected void tearDown() throws Exception 160 { 161 undeploy("security-jaas.ear"); 162 super.tearDown(); 163 164 } 165 }; 166 return wrapper; 167 } 168 169 } 170 | Popular Tags |