1 22 package org.jboss.test.security.test; 23 24 import java.lang.reflect.UndeclaredThrowableException ; 25 import java.rmi.RemoteException ; 26 import javax.rmi.PortableRemoteObject ; 27 import javax.security.auth.login.LoginContext ; 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 47 public class SRPUnitTestCase extends JBossTestCase 48 { 49 static final String JAR = "security-srp.jar"; 50 static String username = "scott"; 51 static char[] password = "echoman".toCharArray(); 52 53 LoginContext lc; 54 boolean loggedIn; 55 56 public SRPUnitTestCase(String name) 57 { 58 super(name); 59 } 60 61 63 public void testEchoArgs() throws Exception 64 { 65 log.debug("+++ testEchoArgs"); 66 login("srp-test", username, password); 67 Object 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 e) 78 { 79 Throwable t = e; 80 if( e instanceof UndeclaredThrowableException ) 81 { 82 UndeclaredThrowableException ex = (UndeclaredThrowableException ) e; 83 t = ex.getUndeclaredThrowable(); 84 } 85 else if( e instanceof RemoteException ) 86 { 87 RemoteException ex = (RemoteException ) e; 88 t = ex.detail; 89 } 90 91 log.error("echo failed", t); 92 boolean failure = true; 93 if( t instanceof SecurityException ) 94 { 95 String msg = t.getMessage(); 96 if( msg.startsWith("Unsupported keysize") ) 97 { 98 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 117 public void testMultiUserEchoArgs() throws Exception 118 { 119 log.debug("+++ testMultiUserEchoArgs"); 120 UserThread ut0 = new UserThread(log); 121 UserThread ut1 = new UserThread(log); 122 123 Thread t0 = new Thread (ut0, "UserThread#0"); 124 t0.setDaemon(true); 125 t0.start(); 126 Thread t1 = new Thread (ut1, "UserThread#1"); 127 t1.setDaemon(true); 128 t1.start(); 129 130 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 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 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 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 182 private void login(String confName, String username, char[] password) 183 throws Exception 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 (confName, handler); 192 lc.login(); 193 log.debug("Created LoginContext, subject="+lc.getSubject()); 194 loggedIn = true; 195 } 196 private void logout() throws Exception 197 { 198 if( loggedIn ) 199 { 200 loggedIn = false; 201 lc.logout(); 202 } 203 } 204 205 208 public static Test suite() throws Exception 209 { 210 TestSuite suite = new TestSuite(); 211 suite.addTest(new TestSuite(SRPUnitTestCase.class)); 212 213 TestSetup wrapper = new JBossTestSetup(suite) 215 { 216 protected void setUp() throws Exception 217 { 218 super.setUp(); 219 deploy(JAR); 220 String authConfPath = super.getResourceURL("security-srp/auth.conf"); 222 System.setProperty("java.security.auth.login.config", authConfPath); 223 } 224 protected void tearDown() throws Exception 225 { 226 undeploy(JAR); 227 super.tearDown(); 228 } 229 }; 230 return wrapper; 231 } 232 233 class UserThread implements Runnable 234 { 235 boolean semaphore; 236 Throwable 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 t) 256 { 257 this.ex = t; 258 t.printStackTrace(); 259 } 260 } 261 private synchronized void internalTestEchoArgs() 262 throws Exception 263 { 264 log.debug("+++ internalTestEchoArgs"); 265 AppCallbackHandler handler = new AppCallbackHandler(username, password); 266 log.debug("Creating LoginContext(srp-test-multi)"); 267 LoginContext lc = new LoginContext ("srp-test-multi", handler); 268 lc.login(); 269 log.debug("Created LoginContext, subject="+lc.getSubject()); 270 271 Object obj = getInitialContext().lookup("srp.StatelessSession"); 272 obj = PortableRemoteObject.narrow(obj, StatelessSessionHome.class); 273 StatelessSessionHome home = (StatelessSessionHome) obj; 274 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 |