001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: F_securityTest.java 4406 2004-03-19 11:57:20Z benoitf $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.clients.j2eeca;
027:
028: import javax.naming.Context;
029: import javax.naming.InitialContext;
030: import javax.rmi.PortableRemoteObject;
031: import javax.transaction.UserTransaction;
032:
033: import junit.framework.Test;
034: import junit.framework.TestSuite;
035:
036: import org.objectweb.jonas.jtests.beans.j2eeca.securedCA;
037: import org.objectweb.jonas.jtests.beans.j2eeca.securedCAHome;
038: import org.objectweb.jonas.jtests.util.JTestCase;
039:
040: /**
041: * Tests Jonas Connector Architecture container-managed sign-on.
042: */
043:
044: public class F_securityTest extends JTestCase {
045:
046: private static UserTransaction utx = null;
047:
048: static Context ctx = null;
049:
050: // Lookup bean home
051:
052: protected static String BEAN_HOME = "securedCAHome";
053: protected static securedCAHome home = null;
054: private static final String RAR_JNDI_NAME = "FictionalSecurity";
055: static securedCA bean = null;
056: final public int CLOSE_HANDLE = 0;
057: final public int CLOSE_PHYSICAL = 1;
058: public int CloseType = 0;
059: public String UseBeans = "j2eeca";
060:
061: public F_securityTest(String name) {
062: super (name);
063: }
064:
065: protected void setUp() {
066: super .setUp();
067: try {
068: // get JNDI initial context
069:
070: if (ctx == null) {
071: ctx = new InitialContext();
072: }
073:
074: // We want to start transactions from client: get UserTransaction
075:
076: if (utx == null) {
077: utx = (UserTransaction) ctx
078: .lookup("javax.transaction.UserTransaction");
079: }
080:
081: if (home == null) {
082: useBeans(UseBeans, false);
083: }
084: getBean();
085: assertTrue(5 == 5);
086:
087: } catch (Exception e) {
088: fail("Cannot lookup UserTransaction in setUp: " + e);
089: }
090: }
091:
092: private void getBean() throws Exception {
093: // Connecting to securedCAHome thru JNDI
094:
095: if (home == null) {
096: home = (securedCAHome) PortableRemoteObject.narrow(ctx
097: .lookup(BEAN_HOME), securedCAHome.class);
098: bean = home.create();
099: }
100: }
101:
102: protected void tearDown() throws Exception {
103: bean.closeUp(CloseType);
104: }
105:
106: protected void startUp(String testName) {
107: try {
108: bean.method1(RAR_JNDI_NAME, testName);
109: } catch (Exception ee) {
110: ee.printStackTrace();
111: System.exit(2);
112: }
113: }
114:
115: // test list ****************************************************
116: /**
117: * Container managed sign-on.
118: *
119: * The application server provides the required security information for
120: * the resource principal through its configured security policies and mechanisms.
121: *
122: * When this class is deployed, the <code>secured.xml</code> file contains:
123: * <p>
124: * <code><res-auth>Container</res-auth></code>
125: *
126: */
127: public void testSecurityContextContainer() throws Exception {
128: CloseType = CLOSE_PHYSICAL;
129: bean.setResAuth("Container");
130: startUp("testSecurityContextContainer");
131: //bean.setMatchNull(true);
132: String pw = bean.getSecurityPassword();
133: String u = bean.getSecurityUserName();
134: String realResAuth = bean.getResAuth();
135: if (realResAuth.equals("Container")) {
136: assertEquals("defaultPassword", pw);
137: assertEquals("defaultUserName", u);
138: } else {
139: assertFalse(pw.equals("defaultPassword"));
140: }
141: }
142:
143: // end test list ****************************************************
144:
145: public static Test suite() {
146: return new TestSuite(F_securityTest.class);
147: }
148:
149: public static void main(String args[]) {
150:
151: String testtorun = null;
152:
153: // Get args
154:
155: for (int argn = 0; argn < args.length; argn++) {
156:
157: String s_arg = args[argn];
158: Integer i_arg;
159:
160: if (s_arg.equals("-n")) {
161: testtorun = args[++argn];
162: }
163: }
164:
165: if (testtorun == null) {
166: junit.textui.TestRunner.run(suite());
167: } else {
168: junit.textui.TestRunner.run(new F_securityTest(testtorun));
169: }
170: }
171: }
|