001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.jacc.test;
023:
024: import java.net.URL;
025:
026: import javax.rmi.PortableRemoteObject;
027: import javax.security.auth.login.Configuration;
028: import javax.security.auth.login.LoginContext;
029:
030: import junit.extensions.TestSetup;
031: import junit.framework.Test;
032: import junit.framework.TestSuite;
033:
034: import org.jboss.security.auth.login.XMLLoginConfigImpl;
035: import org.jboss.test.JBossTestCase;
036: import org.jboss.test.JBossTestSetup;
037: import org.jboss.test.security.interfaces.UsefulStatelessSession;
038: import org.jboss.test.security.interfaces.UsefulStatelessSessionHome;
039: import org.jboss.test.util.AppCallbackHandler;
040:
041: //$Id: CallerInRoleUnitTestCase.java 60043 2007-01-27 02:08:36Z scott.stark@jboss.org $
042:
043: /**
044: * JBAS-2661:EJB context isCallerInRole not delegating to JACC when installed
045: * @author <a href="mailto:Anil.Saldhana@jboss.org">Anil Saldhana</a>
046: * @since Apr 20, 2006
047: * @version $Revision: 60043 $
048: */
049: public class CallerInRoleUnitTestCase extends JBossTestCase {
050: LoginContext lc;
051:
052: public CallerInRoleUnitTestCase(String name) {
053: super (name);
054: }
055:
056: public void testCallerInRoleForBean1() throws Exception {
057: login("anil", "opensource".toCharArray());
058: Object obj = getInitialContext().lookup(
059: "bean1.UsefulStatelessSessionBean");
060: obj = PortableRemoteObject.narrow(obj,
061: UsefulStatelessSessionHome.class);
062: UsefulStatelessSessionHome home = (UsefulStatelessSessionHome) obj;
063: UsefulStatelessSession bean = home.create();
064: assertEquals("NiceUser is true", "true", ""
065: + bean.isCallerInRole("NiceUser"));
066: assertEquals("BadRole is false", "false", ""
067: + bean.isCallerInRole("BadRole"));
068: bean.remove();
069: logout();
070: }
071:
072: private void login(String username, char[] password)
073: throws Exception {
074: lc = null;
075: /* Validate the java.security.auth.login.config setting, and if not
076: found, set it to the security/auth.conf classpath resource value.
077: */
078: initDefaultLoginConfig();
079: String confName = System.getProperty("conf.name", "spec-test");
080: AppCallbackHandler handler = new AppCallbackHandler(username,
081: password);
082: log.debug("Creating LoginContext(" + confName + ")");
083: lc = new LoginContext(confName, handler);
084: lc.login();
085: log.debug("Created LoginContext, subject=" + lc.getSubject());
086: }
087:
088: private void logout() throws Exception {
089: lc.logout();
090: }
091:
092: /**
093: * Setup the test suite.
094: */
095: public static Test suite() throws Exception {
096: TestSuite suite = new TestSuite();
097: suite.addTest(new TestSuite(CallerInRoleUnitTestCase.class));
098:
099: // Create an initializer for the test suite
100: TestSetup wrapper = new JBossTestSetup(suite) {
101: protected void setUp() throws Exception {
102: super .setUp();
103: Configuration
104: .setConfiguration(new XMLLoginConfigImpl());
105: redeploy("security-jacc-callerinrole.jar");
106: }
107:
108: protected void tearDown() throws Exception {
109: undeploy("security-jacc-callerinrole.jar");
110: super.tearDown();
111: }
112: };
113: return wrapper;
114: }
115: }
|