1 22 package org.jboss.test.security.test; 23 24 import java.security.Principal ; 25 import java.util.HashMap ; 26 import java.util.Arrays ; 27 import java.lang.reflect.Method ; 28 29 import javax.security.auth.login.Configuration ; 30 import javax.security.auth.login.AppConfigurationEntry ; 31 import javax.security.auth.login.LoginContext ; 32 import javax.security.auth.Subject ; 33 34 import junit.framework.TestCase; 35 import junit.framework.Test; 36 import junit.framework.TestSuite; 37 import org.jboss.security.SecurityAssociation; 38 import org.jboss.security.SimplePrincipal; 39 import org.jboss.security.auth.callback.UsernamePasswordHandler; 40 41 47 public class ClientLoginModuleUnitTestCase 48 extends TestCase 49 { 50 static TestConfig jaasConfig = new TestConfig(); 51 52 static class TestConfig extends Configuration 53 { 54 public void refresh() 55 { 56 } 57 58 public AppConfigurationEntry [] getAppConfigurationEntry(String name) 59 { 60 AppConfigurationEntry [] entry = null; 61 try 62 { 63 Class [] parameterTypes = {}; 64 Method m = getClass().getDeclaredMethod(name, parameterTypes); 65 Object [] args = {}; 66 entry = (AppConfigurationEntry []) m.invoke(this, args); 67 } 68 catch(Exception e) 69 { 70 } 71 return entry; 72 } 73 AppConfigurationEntry [] testSingleThreaded() 74 { 75 String name = "org.jboss.security.ClientLoginModule"; 76 HashMap options = new HashMap (); 77 options.put("multi-threaded", "false"); 78 AppConfigurationEntry ace = new AppConfigurationEntry (name, 79 AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, options); 80 AppConfigurationEntry [] entry = {ace}; 81 return entry; 82 } 83 AppConfigurationEntry [] testSingleThreadedRestoreIdentity() 84 { 85 String name = "org.jboss.security.ClientLoginModule"; 86 HashMap options = new HashMap (); 87 options.put("multi-threaded", "false"); 88 options.put("restore-login-identity", "true"); 89 AppConfigurationEntry ace = new AppConfigurationEntry (name, 90 AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, options); 91 AppConfigurationEntry [] entry = {ace}; 92 return entry; 93 } 94 AppConfigurationEntry [] testSingleThreadedRestoreStack() 95 { 96 String name = "org.jboss.security.ClientLoginModule"; 97 HashMap options = new HashMap (); 98 options.put("multi-threaded", "false"); 99 options.put("restore-login-identity", "true"); 100 AppConfigurationEntry ace = new AppConfigurationEntry (name, 101 AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, options); 102 AppConfigurationEntry [] entry = {ace}; 103 return entry; 104 } 105 AppConfigurationEntry [] testMultiThreaded() 106 { 107 String name = "org.jboss.security.ClientLoginModule"; 108 HashMap options = new HashMap (); 109 options.put("multi-threaded", "true"); 110 AppConfigurationEntry ace = new AppConfigurationEntry (name, 111 AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, options); 112 AppConfigurationEntry [] entry = {ace}; 113 return entry; 114 } 115 AppConfigurationEntry [] testMultiThreadedRestoreIdentity() 116 { 117 String name = "org.jboss.security.ClientLoginModule"; 118 HashMap options = new HashMap (); 119 options.put("multi-threaded", "true"); 120 options.put("restore-login-identity", "true"); 121 AppConfigurationEntry ace = new AppConfigurationEntry (name, 122 AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, options); 123 AppConfigurationEntry [] entry = {ace}; 124 return entry; 125 } 126 AppConfigurationEntry [] testMultiThreadedRestoreStack() 127 { 128 String name = "org.jboss.security.ClientLoginModule"; 129 HashMap options = new HashMap (); 130 options.put("multi-threaded", "true"); 131 options.put("restore-login-identity", "true"); 132 AppConfigurationEntry ace = new AppConfigurationEntry (name, 133 AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, options); 134 AppConfigurationEntry [] entry = {ace}; 135 return entry; 136 } 137 138 } 139 140 public static Test suite() throws Exception 141 { 142 TestSuite suite = new TestSuite(); 143 suite.addTest(new ClientLoginModuleUnitTestCase("testSingleThreaded")); 144 suite.addTest(new ClientLoginModuleUnitTestCase("testSingleThreadedRestoreIdentity")); 145 suite.addTest(new ClientLoginModuleUnitTestCase("testSingleThreadedRestoreStack")); 146 suite.addTest(new ClientLoginModuleUnitTestCase("testMultiThreaded")); 147 suite.addTest(new ClientLoginModuleUnitTestCase("testMultiThreadedRestoreIdentity")); 148 suite.addTest(new ClientLoginModuleUnitTestCase("testMultiThreadedRestoreStack")); 149 return suite; 150 } 151 152 public ClientLoginModuleUnitTestCase(String name) 153 { 154 super(name); 155 } 156 157 protected void setUp() throws Exception 158 { 159 Configuration.setConfiguration(jaasConfig); 160 } 161 protected void tearDown() 162 { 163 } 164 165 public void testSingleThreaded() throws Exception 166 { 167 System.out.println("+++ testSingleThreaded"); 168 UsernamePasswordHandler handler = new UsernamePasswordHandler("jduke", 169 "theduke"); 170 LoginContext lc = new LoginContext ("testSingleThreaded", handler); 171 lc.login(); 172 Subject subject = lc.getSubject(); 173 System.out.println("LC.Subject: "+subject); 174 Principal theduke = new SimplePrincipal("jduke"); 175 assertTrue("Principals contains theduke", subject.getPrincipals().contains(theduke)); 176 Principal saPrincipal = SecurityAssociation.getPrincipal(); 177 assertTrue("SecurityAssociation.getPrincipal == theduke", saPrincipal.equals(theduke)); 178 char[] password = (char[]) SecurityAssociation.getCredential(); 179 assertTrue("password == theduke", 180 Arrays.equals(password, "theduke".toCharArray())); 181 } 182 183 public void testSingleThreadedRestoreIdentity() throws Exception 184 { 185 System.out.println("+++ testSingleThreadedRestoreIdentity"); 186 187 Principal jduke1 = new SimplePrincipal("jduke1"); 188 SecurityAssociation.setPrincipal(jduke1); 189 SecurityAssociation.setCredential("theduke1"); 190 191 UsernamePasswordHandler handler = new UsernamePasswordHandler("jduke2", 192 "theduke2"); 193 LoginContext lc = new LoginContext ("testSingleThreadedRestoreIdentity", handler); 194 lc.login(); 195 Subject subject = lc.getSubject(); 196 System.out.println("LC.Subject: "+subject); 197 198 Principal jduke2 = new SimplePrincipal("jduke2"); 199 assertTrue("Principals contains jduke2", subject.getPrincipals().contains(jduke2)); 200 Principal saPrincipal = SecurityAssociation.getPrincipal(); 201 assertTrue("SecurityAssociation.getPrincipal == jduke2", saPrincipal.equals(jduke2)); 202 char[] password = (char[]) SecurityAssociation.getCredential(); 203 assertTrue("password == theduke2", 204 Arrays.equals(password, "theduke2".toCharArray())); 205 206 lc.logout(); 207 saPrincipal = SecurityAssociation.getPrincipal(); 209 assertTrue("SecurityAssociation.getPrincipal == jduke1", saPrincipal.equals(jduke1)); 210 String theduke1 = (String ) SecurityAssociation.getCredential(); 211 assertTrue("password == theduke1", theduke1.equals("theduke1")); 212 213 } 214 215 public void testSingleThreadedRestoreStack() throws Exception 216 { 217 System.out.println("+++ testSingleThreadedRestoreStack"); 218 219 Principal jduke1 = new SimplePrincipal("jduke1"); 220 Subject subject1 = new Subject (); 221 SecurityAssociation.pushSubjectContext(subject1, jduke1, "theduke1"); 222 223 Principal jduke2 = new SimplePrincipal("jduke2"); 224 Subject subject2 = new Subject (); 225 SecurityAssociation.pushSubjectContext(subject2, jduke2, "theduke2"); 226 227 UsernamePasswordHandler handler = new UsernamePasswordHandler("jduke3", 228 "theduke3"); 229 LoginContext lc = new LoginContext ("testSingleThreadedRestoreIdentity", handler); 230 lc.login(); 231 Subject subject = lc.getSubject(); 232 System.out.println("LC.Subject: "+subject); 233 234 Principal jduke3 = new SimplePrincipal("jduke3"); 235 assertTrue("Principals contains jduke3", subject.getPrincipals().contains(jduke3)); 236 Principal saPrincipal = SecurityAssociation.getPrincipal(); 237 assertTrue("SecurityAssociation.getPrincipal == jduke3", saPrincipal.equals(jduke3)); 238 char[] password = (char[]) SecurityAssociation.getCredential(); 239 assertTrue("password == theduke3", 240 Arrays.equals(password, "theduke3".toCharArray())); 241 SecurityAssociation.SubjectContext sc3 = SecurityAssociation.peekSubjectContext(); 242 System.out.println(sc3); 243 assertTrue("SecurityAssociation.peekSubjectContext == jduke3", sc3.getPrincipal().equals(jduke3)); 244 char[] theduke3 = (char[]) sc3.getCredential(); 245 assertTrue("password == theduke3", 246 Arrays.equals(theduke3, "theduke3".toCharArray())); 247 248 lc.logout(); 249 250 SecurityAssociation.SubjectContext sc2 = SecurityAssociation.peekSubjectContext(); 252 System.out.println(sc2); 253 assertTrue("SecurityAssociation.peekSubjectContext == jduke2", sc2.getPrincipal().equals(jduke2)); 254 String theduke2 = (String ) sc2.getCredential(); 255 assertTrue("password == theduke2", theduke2.equals("theduke2")); 256 257 SecurityAssociation.popSubjectContext(); 258 SecurityAssociation.SubjectContext sc1 = SecurityAssociation.peekSubjectContext(); 259 System.out.println(sc1); 260 assertTrue("SecurityAssociation.peekSubjectContext == jduke1", sc1.getPrincipal().equals(jduke1)); 261 String theduke1 = (String ) sc1.getCredential(); 262 assertTrue("password == theduke1", theduke1.equals("theduke1")); 263 } 264 265 public void testMultiThreaded() throws Exception 266 { 267 TestMultiThreaded r0 = new TestMultiThreaded(); 268 Thread t0 = new Thread (r0, "testMultiThreaded#0"); 269 t0.start(); 270 TestMultiThreaded r1 = new TestMultiThreaded(); 271 Thread t1 = new Thread (r1, "testMultiThreaded#1"); 272 t1.start(); 273 274 t0.join(); 275 assertTrue(r0.failure == null); 276 t1.join(); 277 assertTrue(r1.failure == null); 278 } 279 static class TestMultiThreaded implements Runnable 280 { 281 Exception failure; 282 public void run() 283 { 284 try 285 { 286 System.out.println("+++ testMultiThreadedRunnable"); 287 UsernamePasswordHandler handler = new UsernamePasswordHandler("jduke", 288 "theduke"); 289 LoginContext lc = new LoginContext ("testSingleThreaded", handler); 290 lc.login(); 291 Subject subject = lc.getSubject(); 292 System.out.println("LC.Subject: "+subject); 293 Principal theduke = new SimplePrincipal("jduke"); 294 assertTrue("Principals contains theduke", subject.getPrincipals().contains(theduke)); 295 Principal saPrincipal = SecurityAssociation.getPrincipal(); 296 assertTrue("SecurityAssociation.getPrincipal == theduke", saPrincipal.equals(theduke)); 297 char[] password = (char[]) SecurityAssociation.getCredential(); 298 assertTrue("password == theduke", 299 Arrays.equals(password, "theduke".toCharArray())); 300 } 301 catch(Exception e) 302 { 303 failure = e; 304 } 305 } 306 } 307 308 public void testMultiThreadedRestoreIdentity() throws Exception 309 { 310 TestMultiThreadedRestoreIdentity r0 = new TestMultiThreadedRestoreIdentity(); 311 Thread t0 = new Thread (r0, "testMultiThreadedRestoreIdentity#0"); 312 t0.start(); 313 TestMultiThreadedRestoreIdentity r1 = new TestMultiThreadedRestoreIdentity(); 314 Thread t1 = new Thread (r1, "testMultiThreadedRestoreIdentity#1"); 315 t1.start(); 316 317 t0.join(); 318 assertTrue(r0.failure == null); 319 t1.join(); 320 assertTrue(r1.failure == null); 321 } 322 static class TestMultiThreadedRestoreIdentity implements Runnable 323 { 324 Exception failure; 325 public void run() 326 { 327 try 328 { 329 System.out.println("+++ testMultiThreadedRestoreIdentity"); 330 331 Principal jduke1 = new SimplePrincipal("jduke1"); 332 SecurityAssociation.setPrincipal(jduke1); 333 SecurityAssociation.setCredential("theduke1"); 334 335 UsernamePasswordHandler handler = new UsernamePasswordHandler("jduke2", 336 "theduke2"); 337 LoginContext lc = new LoginContext ("testSingleThreadedRestoreIdentity", handler); 338 lc.login(); 339 Subject subject = lc.getSubject(); 340 System.out.println("LC.Subject: "+subject); 341 342 Principal jduke2 = new SimplePrincipal("jduke2"); 343 assertTrue("Principals contains jduke2", subject.getPrincipals().contains(jduke2)); 344 Principal saPrincipal = SecurityAssociation.getPrincipal(); 345 assertTrue("SecurityAssociation.getPrincipal == jduke2", saPrincipal.equals(jduke2)); 346 char[] password = (char[]) SecurityAssociation.getCredential(); 347 assertTrue("password == theduke2", 348 Arrays.equals(password, "theduke2".toCharArray())); 349 350 lc.logout(); 351 saPrincipal = SecurityAssociation.getPrincipal(); 353 assertTrue("SecurityAssociation.getPrincipal == jduke1", saPrincipal.equals(jduke1)); 354 String theduke1 = (String ) SecurityAssociation.getCredential(); 355 assertTrue("password == theduke1", theduke1.equals("theduke1")); 356 357 } 358 catch(Exception e) 359 { 360 failure = e; 361 } 362 } 363 } 364 365 public void testMultiThreadedRestoreStack() throws Exception 366 { 367 TestMultiThreadedRestoreStack r0 = new TestMultiThreadedRestoreStack(); 368 Thread t0 = new Thread (r0, "testMultiThreadedRestoreIdentity#0"); 369 t0.start(); 370 TestMultiThreadedRestoreStack r1 = new TestMultiThreadedRestoreStack(); 371 Thread t1 = new Thread (r1, "testMultiThreadedRestoreIdentity#1"); 372 t1.start(); 373 374 t0.join(); 375 assertTrue(r0.failure == null); 376 t1.join(); 377 assertTrue(r1.failure == null); 378 } 379 static class TestMultiThreadedRestoreStack implements Runnable 380 { 381 Exception failure; 382 public void run() 383 { 384 try 385 { 386 System.out.println("+++ testMultThreadedRestoreStack"); 387 388 Principal jduke1 = new SimplePrincipal("jduke1"); 389 Subject subject1 = new Subject (); 390 SecurityAssociation.pushSubjectContext(subject1, jduke1, "theduke1"); 391 392 Principal jduke2 = new SimplePrincipal("jduke2"); 393 Subject subject2 = new Subject (); 394 SecurityAssociation.pushSubjectContext(subject2, jduke2, "theduke2"); 395 396 UsernamePasswordHandler handler = new UsernamePasswordHandler("jduke3", 397 "theduke3"); 398 LoginContext lc = new LoginContext ("testSingleThreadedRestoreIdentity", handler); 399 lc.login(); 400 Subject subject = lc.getSubject(); 401 System.out.println("LC.Subject: "+subject); 402 403 Principal jduke3 = new SimplePrincipal("jduke3"); 404 assertTrue("Principals contains jduke3", subject.getPrincipals().contains(jduke3)); 405 Principal saPrincipal = SecurityAssociation.getPrincipal(); 406 assertTrue("SecurityAssociation.getPrincipal == jduke3", saPrincipal.equals(jduke3)); 407 char[] password = (char[]) SecurityAssociation.getCredential(); 408 assertTrue("password == theduke3", 409 Arrays.equals(password, "theduke3".toCharArray())); 410 SecurityAssociation.SubjectContext sc3 = SecurityAssociation.peekSubjectContext(); 411 System.out.println(sc3); 412 assertTrue("SecurityAssociation.peekSubjectContext == jduke3", sc3.getPrincipal().equals(jduke3)); 413 char[] theduke3 = (char[]) sc3.getCredential(); 414 assertTrue("password == theduke3", 415 Arrays.equals(theduke3, "theduke3".toCharArray())); 416 417 lc.logout(); 418 419 SecurityAssociation.SubjectContext sc2 = SecurityAssociation.peekSubjectContext(); 421 System.out.println(sc2); 422 assertTrue("SecurityAssociation.peekSubjectContext == jduke2", sc2.getPrincipal().equals(jduke2)); 423 String theduke2 = (String ) sc2.getCredential(); 424 assertTrue("password == theduke2", theduke2.equals("theduke2")); 425 426 SecurityAssociation.popSubjectContext(); 427 SecurityAssociation.SubjectContext sc1 = SecurityAssociation.peekSubjectContext(); 428 System.out.println(sc1); 429 assertTrue("SecurityAssociation.peekSubjectContext == jduke1", sc1.getPrincipal().equals(jduke1)); 430 String theduke1 = (String ) sc1.getCredential(); 431 assertTrue("password == theduke1", theduke1.equals("theduke1")); 432 } 433 catch(Exception e) 434 { 435 failure = e; 436 } 437 } 438 } 439 440 } 441 | Popular Tags |