KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > security > test > SRPUnitTestCase


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.security.test;
23
24 import java.lang.reflect.UndeclaredThrowableException JavaDoc;
25 import java.rmi.RemoteException JavaDoc;
26 import javax.rmi.PortableRemoteObject JavaDoc;
27 import javax.security.auth.login.LoginContext JavaDoc;
28
29 import junit.extensions.TestSetup;
30 import junit.framework.Test;
31 import junit.framework.TestSuite;
32
33 import org.jboss.test.util.AppCallbackHandler;
34 import org.jboss.test.JBossTestCase;
35 import org.jboss.test.JBossTestSetup;
36 import org.jboss.test.security.interfaces.StatelessSession;
37 import org.jboss.test.security.interfaces.StatelessSessionHome;
38 import org.jboss.logging.Logger;
39
40 /** Test of the secure remote password(SRP) session key to perform crypto
41 operations.
42  
43  
44  @author Scott.Stark@jboss.org
45  @version $Revision: 58472 $
46  */

47 public class SRPUnitTestCase extends JBossTestCase
48 {
49    static final String JavaDoc JAR = "security-srp.jar";
50    static String JavaDoc username = "scott";
51    static char[] password = "echoman".toCharArray();
52
53    LoginContext JavaDoc lc;
54    boolean loggedIn;
55
56    public SRPUnitTestCase(String JavaDoc name)
57    {
58       super(name);
59    }
60
61    /** Test that the echo method is secured by the SRPCacheLogin module
62     */

63    public void testEchoArgs() throws Exception JavaDoc
64    {
65       log.debug("+++ testEchoArgs");
66       login("srp-test", username, password);
67       Object JavaDoc obj = getInitialContext().lookup("srp-jce.StatelessSession");
68       obj = PortableRemoteObject.narrow(obj, StatelessSessionHome.class);
69       StatelessSessionHome home = (StatelessSessionHome) obj;
70       log.debug("Found StatelessSessionHome");
71       StatelessSession bean = home.create();
72       log.debug("Created srp-jce.StatelessSession");
73       try
74       {
75          log.debug("Bean.echo('Hello') -> "+bean.echo("Hello"));
76       }
77       catch(Exception JavaDoc e)
78       {
79          Throwable JavaDoc t = e;
80          if( e instanceof UndeclaredThrowableException JavaDoc )
81          {
82             UndeclaredThrowableException JavaDoc ex = (UndeclaredThrowableException JavaDoc) e;
83             t = ex.getUndeclaredThrowable();
84          }
85          else if( e instanceof RemoteException JavaDoc )
86          {
87             RemoteException JavaDoc ex = (RemoteException JavaDoc) e;
88             t = ex.detail;
89          }
90
91          log.error("echo failed", t);
92          boolean failure = true;
93          if( t instanceof SecurityException JavaDoc )
94          {
95             String JavaDoc msg = t.getMessage();
96             if( msg.startsWith("Unsupported keysize") )
97             {
98                /* The size of the srp session key is bigger than the JCE version
99                in use supports. Most likely the unlimited strength policy is
100                not installed so don't fail the test.
101                */

102                failure = false;
103                log.info("Not failing test due to key size issue");
104             }
105          }
106
107          if( failure )
108             fail("Call to echo failed: "+t.getMessage());
109       }
110
111       logout();
112    }
113
114    /** Test that the echo method is secured by the SRPCacheLogin module when
115     * using multi-session srp with two threads
116     */

117    public void testMultiUserEchoArgs() throws Exception JavaDoc
118    {
119       log.debug("+++ testMultiUserEchoArgs");
120       UserThread ut0 = new UserThread(log);
121       UserThread ut1 = new UserThread(log);
122
123       Thread JavaDoc t0 = new Thread JavaDoc(ut0, "UserThread#0");
124       t0.setDaemon(true);
125       t0.start();
126       Thread JavaDoc t1 = new Thread JavaDoc(ut1, "UserThread#1");
127       t1.setDaemon(true);
128       t1.start();
129
130       // Release the ut0 thread and wait for it to finish the first ejb call
131
synchronized( ut0 )
132       {
133          ut0.semaphore = true;
134          ut0.notify();
135          log.info("waiting on ut0 #1");
136          ut0.wait(5000);
137       }
138       log.info("released ut0 #1");
139       // Release the ut1 thread and wait for it to finish the first ejb call
140
synchronized( ut1 )
141       {
142          ut1.semaphore = true;
143          ut1.notify();
144          log.info("waiting on ut1 #1");
145          ut1.wait(5000);
146       }
147       log.info("released ut1 #1");
148       assertTrue("UserThread0.ex == null", ut0.ex == null);
149          
150       // Release the ut1 thread and wait for it to finish the second ejb call
151
synchronized( ut1 )
152       {
153          ut1.semaphore = true;
154          ut1.notify();
155          log.info("waiting on ut1 #2");
156          ut1.wait(5000);
157       }
158       log.info("released ut1 #2");
159       assertTrue("UserThread1.ex == null", ut1.ex == null);
160
161       // Release the ut0 thread and wait for it to finish the second ejb call
162
synchronized( ut0 )
163       {
164          ut0.semaphore = true;
165          ut0.notify();
166          log.info("waiting on ut0 #2");
167          ut0.wait(5000);
168       }
169       log.info("released ut0 #2");
170
171       t0.join();
172       log.debug("UserThread0.ex", ut0.ex);
173       t1.join();
174       log.debug("UserThread1.ex", ut1.ex);
175       assertTrue("UserThread0.ex == null", ut0.ex == null);
176       assertTrue("UserThread1.ex == null", ut1.ex == null);
177    }
178
179    /** Login using the given confName login configuration with the provided
180     username and password credential.
181     */

182    private void login(String JavaDoc confName, String JavaDoc username, char[] password)
183       throws Exception JavaDoc
184    {
185       if( loggedIn )
186          return;
187
188       lc = null;
189       AppCallbackHandler handler = new AppCallbackHandler(username, password);
190       log.debug("Creating LoginContext("+confName+")");
191       lc = new LoginContext JavaDoc(confName, handler);
192       lc.login();
193       log.debug("Created LoginContext, subject="+lc.getSubject());
194       loggedIn = true;
195    }
196    private void logout() throws Exception JavaDoc
197    {
198       if( loggedIn )
199       {
200          loggedIn = false;
201          lc.logout();
202       }
203    }
204
205    /**
206     * Setup the test suite.
207     */

208    public static Test suite() throws Exception JavaDoc
209    {
210       TestSuite suite = new TestSuite();
211       suite.addTest(new TestSuite(SRPUnitTestCase.class));
212
213       // Create an initializer for the test suite
214
TestSetup wrapper = new JBossTestSetup(suite)
215       {
216          protected void setUp() throws Exception JavaDoc
217          {
218             super.setUp();
219             deploy(JAR);
220             // Establish the JAAS login config
221
String JavaDoc authConfPath = super.getResourceURL("security-srp/auth.conf");
222             System.setProperty("java.security.auth.login.config", authConfPath);
223          }
224          protected void tearDown() throws Exception JavaDoc
225          {
226             undeploy(JAR);
227             super.tearDown();
228          }
229       };
230       return wrapper;
231    }
232
233    class UserThread implements Runnable JavaDoc
234    {
235       boolean semaphore;
236       Throwable JavaDoc ex;
237       Logger log;
238       UserThread(Logger log)
239       {
240          this.log = log;
241       }
242
243       public synchronized boolean semaphore()
244       {
245          semaphore = true;
246          return semaphore;
247       }
248
249       public void run()
250       {
251          try
252          {
253             internalTestEchoArgs();
254          }
255          catch(Throwable JavaDoc t)
256          {
257             this.ex = t;
258             t.printStackTrace();
259          }
260       }
261       private synchronized void internalTestEchoArgs()
262          throws Exception JavaDoc
263       {
264          log.debug("+++ internalTestEchoArgs");
265          AppCallbackHandler handler = new AppCallbackHandler(username, password);
266          log.debug("Creating LoginContext(srp-test-multi)");
267          LoginContext JavaDoc lc = new LoginContext JavaDoc("srp-test-multi", handler);
268          lc.login();
269          log.debug("Created LoginContext, subject="+lc.getSubject());
270
271          Object JavaDoc obj = getInitialContext().lookup("srp.StatelessSession");
272          obj = PortableRemoteObject.narrow(obj, StatelessSessionHome.class);
273          StatelessSessionHome home = (StatelessSessionHome) obj;
274          // Wait for the test thread to tell use to continue
275
log.debug("Enter wait");
276          while( semaphore == false )
277          {
278             log.info("waiting for notification");
279             wait(1000);
280          }
281          semaphore = false;
282          log.debug("Notified, Found StatelessSessionHome");
283          StatelessSession bean = home.create();
284          log.debug("Created srp.StatelessSession");
285          log.debug("Bean.echo('Hello') -> "+bean.echo("Hello"));
286          notifyAll();
287          log.debug("Notified all, enter wait#2");
288          while( semaphore == false )
289          {
290             log.info("waiting for notification");
291             wait(1000);
292          }
293          log.debug("Notified, Bean.echo('Hello#2') -> "+bean.echo("Hello#2"));
294          notifyAll();
295          log.debug("Notified all, logging out");
296          lc.logout();
297          log.debug("Logout");
298       }
299    }
300 }
301
Popular Tags