1 22 package org.jboss.test.jca.test; 23 24 import java.sql.Connection ; 25 import java.sql.Statement ; 26 import java.sql.SQLException ; 27 import javax.naming.InitialContext ; 28 import javax.security.auth.login.LoginContext ; 29 import javax.sql.DataSource ; 30 31 import junit.framework.Test; 32 import org.jboss.test.JBossTestCase; 33 import org.jboss.test.util.AppCallbackHandler; 34 import org.jboss.test.jca.securedejb.CallerIdentity; 35 import org.jboss.test.jca.securedejb.CallerIdentityHome; 36 37 43 public class SecurityContextUnitTestCase extends JBossTestCase 44 { 45 46 public SecurityContextUnitTestCase(String name) 47 { 48 super(name); 49 } 50 51 public static Test suite() throws Exception 52 { 53 System.setProperty("jbosstest.secure", "false"); 55 Test t1 = getDeploySetup(SecurityContextUnitTestCase.class, "jca-securedejb.jar"); 56 return t1; 57 } 58 59 public void testCallerIdentityPropagation() throws Throwable 60 { 61 log.info("+++ testCallerIdentityPropagation"); 62 InitialContext ctx = super.getInitialContext(); 63 try 64 { 65 log.info("Lookup CallerIdentityDS"); 66 DataSource ds = (DataSource ) ctx.lookup("CallerIdentityDS"); 67 Connection conn = ds.getConnection("sa", ""); 68 Statement stmt = conn.createStatement(); 69 stmt.execute("CREATE USER ejbcaller PASSWORD ejbcallerpw"); 70 stmt.close(); 71 conn.close(); 72 } 73 catch(SQLException ignore) 74 { 75 log.debug("ejbcaller user setup failed", ignore); 76 } 77 78 LoginContext lc = login("ejbcaller", "ejbcallerpw".toCharArray()); 79 CallerIdentityHome home = (CallerIdentityHome) ctx.lookup("jca-test/CallerIdentity"); 80 CallerIdentity bean = home.create(); 81 bean.useCallerForAuth(); 82 lc.logout(); 83 } 84 85 public void testConfiguredIdentityPropagation() throws Throwable 86 { 87 InitialContext ctx = super.getInitialContext(); 88 89 LoginContext lc = login("ejbcaller", "ejbcallerpw".toCharArray()); 90 CallerIdentityHome home = (CallerIdentityHome) ctx.lookup("jca-test/CallerIdentity"); 91 CallerIdentity bean = home.create(); 92 bean.useConfiguredForAuth(); 93 lc.logout(); 94 } 95 96 public void testRunAsIdentityPropagationFS() throws Throwable 97 { 98 InitialContext ctx = super.getInitialContext(); 99 LoginContext lc = login("ejbcaller", "ejbcallerpw".toCharArray()); 100 CallerIdentityHome home = (CallerIdentityHome) ctx.lookup("jca-test/RunAsIdentityFS"); 101 CallerIdentity bean = home.create(); 102 bean.useRunAsForAuthFS(); 103 lc.logout(); 104 } 105 106 public void testRunAsIdentityPropagationDS() throws Throwable 107 { 108 InitialContext ctx = super.getInitialContext(); 109 LoginContext lc = login("ejbcaller", "ejbcallerpw".toCharArray()); 110 CallerIdentityHome home = (CallerIdentityHome) ctx.lookup("jca-test/RunAsIdentityDS"); 111 CallerIdentity bean = home.create(); 112 bean.useRunAsForAuthDS(); 113 lc.logout(); 114 } 115 116 private LoginContext login(String username, char[] password) throws Exception 117 { 118 String confName = System.getProperty("conf.name", "other"); 119 AppCallbackHandler handler = new AppCallbackHandler(username, password); 120 log.debug("Creating LoginContext("+confName+")"); 121 LoginContext lc = new LoginContext (confName, handler); 122 lc.login(); 123 log.debug("Created LoginContext, subject="+lc.getSubject()); 124 return lc; 125 } 126 } 127 | Popular Tags |