KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jca > test > SecurityContextUnitTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.jca.test;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.Statement JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28 import javax.security.auth.login.LoginContext JavaDoc;
29 import javax.sql.DataSource JavaDoc;
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 /**
38  * Tests of how security context interact with the JCA layer.
39  *
40  * @author Scott.Stark@jboss.org
41  * @version $Revision: 37406 $
42  */

43 public class SecurityContextUnitTestCase extends JBossTestCase
44 {
45
46    public SecurityContextUnitTestCase(String JavaDoc name)
47    {
48       super(name);
49    }
50    
51    public static Test suite() throws Exception JavaDoc
52    {
53       // Clear any default login behavior
54
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 JavaDoc
60    {
61       log.info("+++ testCallerIdentityPropagation");
62       InitialContext JavaDoc ctx = super.getInitialContext();
63       try
64       {
65          log.info("Lookup CallerIdentityDS");
66          DataSource JavaDoc ds = (DataSource JavaDoc) ctx.lookup("CallerIdentityDS");
67          Connection JavaDoc conn = ds.getConnection("sa", "");
68          Statement JavaDoc stmt = conn.createStatement();
69          stmt.execute("CREATE USER ejbcaller PASSWORD ejbcallerpw");
70          stmt.close();
71          conn.close();
72       }
73       catch(SQLException JavaDoc ignore)
74       {
75          log.debug("ejbcaller user setup failed", ignore);
76       }
77
78       LoginContext JavaDoc 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 JavaDoc
86    {
87       InitialContext JavaDoc ctx = super.getInitialContext();
88
89       LoginContext JavaDoc 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 JavaDoc
97    {
98       InitialContext JavaDoc ctx = super.getInitialContext();
99       LoginContext JavaDoc 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 JavaDoc
107    {
108       InitialContext JavaDoc ctx = super.getInitialContext();
109       LoginContext JavaDoc 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 JavaDoc login(String JavaDoc username, char[] password) throws Exception JavaDoc
117    {
118       String JavaDoc confName = System.getProperty("conf.name", "other");
119       AppCallbackHandler handler = new AppCallbackHandler(username, password);
120       log.debug("Creating LoginContext("+confName+")");
121       LoginContext JavaDoc lc = new LoginContext JavaDoc(confName, handler);
122       lc.login();
123       log.debug("Created LoginContext, subject="+lc.getSubject());
124       return lc;
125    }
126 }
127
Popular Tags