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.security.test;
023:
024: import java.net.URL;
025: import javax.rmi.PortableRemoteObject;
026: import javax.security.auth.login.Configuration;
027: import javax.security.auth.login.LoginContext;
028:
029: import org.jboss.security.auth.login.XMLLoginConfigImpl;
030: import org.jboss.test.JBossTestCase;
031: import org.jboss.test.JBossTestSetup;
032: import org.jboss.test.security.interfaces.CustomPrincipalHome;
033: import org.jboss.test.security.interfaces.CustomPrincipal;
034: import org.jboss.test.security.ejb.CustomPrincipalImpl;
035: import org.jboss.test.util.AppCallbackHandler;
036: import org.jboss.test.util.web.HttpUtils;
037:
038: import junit.extensions.TestSetup;
039: import junit.framework.Test;
040: import junit.framework.TestSuite;
041:
042: /** JAAS specific tests.
043:
044: @author Scott.Stark@jboss.org
045: @version $Revision: 57211 $
046: */
047: public class JaasUnitTestCase extends JBossTestCase {
048: static String username = "jduke";
049: static char[] password = "theduke".toCharArray();
050:
051: LoginContext lc;
052: boolean loggedIn;
053:
054: public JaasUnitTestCase(String name) {
055: super (name);
056: }
057:
058: /** Test return of a custom principal from getCallerPrincipal.
059: */
060: public void testCustomEJBPrincipal() throws Exception {
061: login();
062: log.debug("+++ testCustomEJBPrincipal()");
063: Object obj = getInitialContext().lookup(
064: "jaas.CustomPrincipalHome");
065: obj = PortableRemoteObject.narrow(obj,
066: CustomPrincipalHome.class);
067: CustomPrincipalHome home = (CustomPrincipalHome) obj;
068: log.debug("Found CustomPrincipalHome");
069: CustomPrincipal bean = home.create();
070: log.debug("Created CustomPrincipal");
071:
072: boolean isCustomType = bean
073: .validateCallerPrincipal(CustomPrincipalImpl.class);
074: bean.remove();
075: logout();
076: assertTrue("CustomPrincipalImpl was seen", isCustomType);
077: }
078:
079: /** Test return of a custom principal from getCallerPrincipal coming from
080: * a custom login module.
081: */
082: public void testCustomEJBPrincipal2() throws Exception {
083: login();
084: log.debug("+++ testCustomEJBPrincipal()");
085: Object obj = getInitialContext().lookup(
086: "jaas.CustomPrincipal2Home");
087: obj = PortableRemoteObject.narrow(obj,
088: CustomPrincipalHome.class);
089: CustomPrincipalHome home = (CustomPrincipalHome) obj;
090: log.debug("Found CustomPrincipalHome");
091: CustomPrincipal bean = home.create();
092: log.debug("Created CustomPrincipal");
093:
094: boolean isCustomType = bean
095: .validateCallerPrincipal(CustomPrincipalImpl.class);
096: bean.remove();
097: logout();
098: assertTrue("CustomPrincipalImpl was seen", isCustomType);
099: }
100:
101: public void testCustomWebPrincipal() throws Exception {
102: log.debug("+++ testCustomWebPrincipal()");
103: String base = HttpUtils.getBaseURL();
104: URL testURL = new URL(base + "jaas/CustomPrincipalServlet"
105: + "?type=" + CustomPrincipalImpl.class.getName());
106: HttpUtils.accessURL(testURL);
107: }
108:
109: /** Login as user scott using the conf.name login config or
110: 'jaas-test' if conf.name is not defined.
111: */
112: private void login() throws Exception {
113: login(username, password);
114: }
115:
116: private void login(String username, char[] password)
117: throws Exception {
118: if (loggedIn)
119: return;
120:
121: lc = null;
122: String confName = System.getProperty("conf.name", "jaas-test");
123: AppCallbackHandler handler = new AppCallbackHandler(username,
124: 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:
132: private void logout() throws Exception {
133: if (loggedIn) {
134: loggedIn = false;
135: lc.logout();
136: }
137: }
138:
139: /**
140: * Setup the test suite.
141: */
142: public static Test suite() throws Exception {
143: TestSuite suite = new TestSuite();
144: suite.addTest(new TestSuite(JaasUnitTestCase.class));
145:
146: // Create an initializer for the test suite
147: TestSetup wrapper = new JBossTestSetup(suite) {
148: protected void setUp() throws Exception {
149: super .setUp();
150: Configuration
151: .setConfiguration(new XMLLoginConfigImpl());
152: deploy("security-jaas.ear");
153: flushAuthCache("jaas-test");
154: }
155:
156: protected void tearDown() throws Exception {
157: undeploy("security-jaas.ear");
158: super.tearDown();
159:
160: }
161: };
162: return wrapper;
163: }
164:
165: }
|