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.security.Principal;
025: import java.util.HashMap;
026: import java.util.Arrays;
027: import java.lang.reflect.Method;
028:
029: import javax.security.auth.login.Configuration;
030: import javax.security.auth.login.AppConfigurationEntry;
031: import javax.security.auth.login.LoginContext;
032: import javax.security.auth.Subject;
033:
034: import junit.framework.TestCase;
035: import junit.framework.Test;
036: import junit.framework.TestSuite;
037: import org.jboss.security.SecurityAssociation;
038: import org.jboss.security.SimplePrincipal;
039: import org.jboss.security.auth.callback.UsernamePasswordHandler;
040:
041: /**
042: ClientLoginModuleUnitTestCase/SecurityAssociation interaction tests
043:
044: @author Scott.Stark@jboss.org
045: @version $Revision: 57211 $
046: */
047: public class ClientLoginModuleUnitTestCase extends TestCase {
048: static TestConfig jaasConfig = new TestConfig();
049:
050: static class TestConfig extends Configuration {
051: public void refresh() {
052: }
053:
054: public AppConfigurationEntry[] getAppConfigurationEntry(
055: String name) {
056: AppConfigurationEntry[] entry = null;
057: try {
058: Class[] parameterTypes = {};
059: Method m = getClass().getDeclaredMethod(name,
060: parameterTypes);
061: Object[] args = {};
062: entry = (AppConfigurationEntry[]) m.invoke(this , args);
063: } catch (Exception e) {
064: }
065: return entry;
066: }
067:
068: AppConfigurationEntry[] testSingleThreaded() {
069: String name = "org.jboss.security.ClientLoginModule";
070: HashMap options = new HashMap();
071: options.put("multi-threaded", "false");
072: AppConfigurationEntry ace = new AppConfigurationEntry(
073: name,
074: AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
075: options);
076: AppConfigurationEntry[] entry = { ace };
077: return entry;
078: }
079:
080: AppConfigurationEntry[] testSingleThreadedRestoreIdentity() {
081: String name = "org.jboss.security.ClientLoginModule";
082: HashMap options = new HashMap();
083: options.put("multi-threaded", "false");
084: options.put("restore-login-identity", "true");
085: AppConfigurationEntry ace = new AppConfigurationEntry(
086: name,
087: AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
088: options);
089: AppConfigurationEntry[] entry = { ace };
090: return entry;
091: }
092:
093: AppConfigurationEntry[] testSingleThreadedRestoreStack() {
094: String name = "org.jboss.security.ClientLoginModule";
095: HashMap options = new HashMap();
096: options.put("multi-threaded", "false");
097: options.put("restore-login-identity", "true");
098: AppConfigurationEntry ace = new AppConfigurationEntry(
099: name,
100: AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
101: options);
102: AppConfigurationEntry[] entry = { ace };
103: return entry;
104: }
105:
106: AppConfigurationEntry[] testMultiThreaded() {
107: String name = "org.jboss.security.ClientLoginModule";
108: HashMap options = new HashMap();
109: options.put("multi-threaded", "true");
110: AppConfigurationEntry ace = new AppConfigurationEntry(
111: name,
112: AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
113: options);
114: AppConfigurationEntry[] entry = { ace };
115: return entry;
116: }
117:
118: AppConfigurationEntry[] testMultiThreadedRestoreIdentity() {
119: String name = "org.jboss.security.ClientLoginModule";
120: HashMap options = new HashMap();
121: options.put("multi-threaded", "true");
122: options.put("restore-login-identity", "true");
123: AppConfigurationEntry ace = new AppConfigurationEntry(
124: name,
125: AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
126: options);
127: AppConfigurationEntry[] entry = { ace };
128: return entry;
129: }
130:
131: AppConfigurationEntry[] testMultiThreadedRestoreStack() {
132: String name = "org.jboss.security.ClientLoginModule";
133: HashMap options = new HashMap();
134: options.put("multi-threaded", "true");
135: options.put("restore-login-identity", "true");
136: AppConfigurationEntry ace = new AppConfigurationEntry(
137: name,
138: AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
139: options);
140: AppConfigurationEntry[] entry = { ace };
141: return entry;
142: }
143:
144: }
145:
146: public static Test suite() throws Exception {
147: TestSuite suite = new TestSuite();
148: suite.addTest(new ClientLoginModuleUnitTestCase(
149: "testSingleThreaded"));
150: suite.addTest(new ClientLoginModuleUnitTestCase(
151: "testSingleThreadedRestoreIdentity"));
152: suite.addTest(new ClientLoginModuleUnitTestCase(
153: "testSingleThreadedRestoreStack"));
154: suite.addTest(new ClientLoginModuleUnitTestCase(
155: "testMultiThreaded"));
156: suite.addTest(new ClientLoginModuleUnitTestCase(
157: "testMultiThreadedRestoreIdentity"));
158: suite.addTest(new ClientLoginModuleUnitTestCase(
159: "testMultiThreadedRestoreStack"));
160: return suite;
161: }
162:
163: public ClientLoginModuleUnitTestCase(String name) {
164: super (name);
165: }
166:
167: protected void setUp() throws Exception {
168: Configuration.setConfiguration(jaasConfig);
169: }
170:
171: protected void tearDown() {
172: }
173:
174: public void testSingleThreaded() throws Exception {
175: System.out.println("+++ testSingleThreaded");
176: UsernamePasswordHandler handler = new UsernamePasswordHandler(
177: "jduke", "theduke");
178: LoginContext lc = new LoginContext("testSingleThreaded",
179: handler);
180: lc.login();
181: Subject subject = lc.getSubject();
182: System.out.println("LC.Subject: " + subject);
183: Principal theduke = new SimplePrincipal("jduke");
184: assertTrue("Principals contains theduke", subject
185: .getPrincipals().contains(theduke));
186: Principal saPrincipal = SecurityAssociation.getPrincipal();
187: assertTrue("SecurityAssociation.getPrincipal == theduke",
188: saPrincipal.equals(theduke));
189: char[] password = (char[]) SecurityAssociation.getCredential();
190: assertTrue("password == theduke", Arrays.equals(password,
191: "theduke".toCharArray()));
192: }
193:
194: public void testSingleThreadedRestoreIdentity() throws Exception {
195: System.out.println("+++ testSingleThreadedRestoreIdentity");
196:
197: Principal jduke1 = new SimplePrincipal("jduke1");
198: SecurityAssociation.setPrincipal(jduke1);
199: SecurityAssociation.setCredential("theduke1");
200:
201: UsernamePasswordHandler handler = new UsernamePasswordHandler(
202: "jduke2", "theduke2");
203: LoginContext lc = new LoginContext(
204: "testSingleThreadedRestoreIdentity", handler);
205: lc.login();
206: Subject subject = lc.getSubject();
207: System.out.println("LC.Subject: " + subject);
208:
209: Principal jduke2 = new SimplePrincipal("jduke2");
210: assertTrue("Principals contains jduke2", subject
211: .getPrincipals().contains(jduke2));
212: Principal saPrincipal = SecurityAssociation.getPrincipal();
213: assertTrue("SecurityAssociation.getPrincipal == jduke2",
214: saPrincipal.equals(jduke2));
215: char[] password = (char[]) SecurityAssociation.getCredential();
216: assertTrue("password == theduke2", Arrays.equals(password,
217: "theduke2".toCharArray()));
218:
219: lc.logout();
220: // Validate restored state
221: saPrincipal = SecurityAssociation.getPrincipal();
222: assertTrue("SecurityAssociation.getPrincipal == jduke1",
223: saPrincipal.equals(jduke1));
224: String theduke1 = (String) SecurityAssociation.getCredential();
225: assertTrue("password == theduke1", theduke1.equals("theduke1"));
226:
227: }
228:
229: public void testSingleThreadedRestoreStack() throws Exception {
230: System.out.println("+++ testSingleThreadedRestoreStack");
231:
232: Principal jduke1 = new SimplePrincipal("jduke1");
233: Subject subject1 = new Subject();
234: SecurityAssociation.pushSubjectContext(subject1, jduke1,
235: "theduke1");
236:
237: Principal jduke2 = new SimplePrincipal("jduke2");
238: Subject subject2 = new Subject();
239: SecurityAssociation.pushSubjectContext(subject2, jduke2,
240: "theduke2");
241:
242: UsernamePasswordHandler handler = new UsernamePasswordHandler(
243: "jduke3", "theduke3");
244: LoginContext lc = new LoginContext(
245: "testSingleThreadedRestoreIdentity", handler);
246: lc.login();
247: Subject subject = lc.getSubject();
248: System.out.println("LC.Subject: " + subject);
249:
250: Principal jduke3 = new SimplePrincipal("jduke3");
251: assertTrue("Principals contains jduke3", subject
252: .getPrincipals().contains(jduke3));
253: Principal saPrincipal = SecurityAssociation.getPrincipal();
254: assertTrue("SecurityAssociation.getPrincipal == jduke3",
255: saPrincipal.equals(jduke3));
256: char[] password = (char[]) SecurityAssociation.getCredential();
257: assertTrue("password == theduke3", Arrays.equals(password,
258: "theduke3".toCharArray()));
259: SecurityAssociation.SubjectContext sc3 = SecurityAssociation
260: .peekSubjectContext();
261: System.out.println(sc3);
262: assertTrue("SecurityAssociation.peekSubjectContext == jduke3",
263: sc3.getPrincipal().equals(jduke3));
264: char[] theduke3 = (char[]) sc3.getCredential();
265: assertTrue("password == theduke3", Arrays.equals(theduke3,
266: "theduke3".toCharArray()));
267:
268: lc.logout();
269:
270: // Validate restored state
271: SecurityAssociation.SubjectContext sc2 = SecurityAssociation
272: .peekSubjectContext();
273: System.out.println(sc2);
274: assertTrue("SecurityAssociation.peekSubjectContext == jduke2",
275: sc2.getPrincipal().equals(jduke2));
276: String theduke2 = (String) sc2.getCredential();
277: assertTrue("password == theduke2", theduke2.equals("theduke2"));
278:
279: SecurityAssociation.popSubjectContext();
280: SecurityAssociation.SubjectContext sc1 = SecurityAssociation
281: .peekSubjectContext();
282: System.out.println(sc1);
283: assertTrue("SecurityAssociation.peekSubjectContext == jduke1",
284: sc1.getPrincipal().equals(jduke1));
285: String theduke1 = (String) sc1.getCredential();
286: assertTrue("password == theduke1", theduke1.equals("theduke1"));
287: }
288:
289: public void testMultiThreaded() throws Exception {
290: TestMultiThreaded r0 = new TestMultiThreaded();
291: Thread t0 = new Thread(r0, "testMultiThreaded#0");
292: t0.start();
293: TestMultiThreaded r1 = new TestMultiThreaded();
294: Thread t1 = new Thread(r1, "testMultiThreaded#1");
295: t1.start();
296:
297: t0.join();
298: assertTrue(r0.failure == null);
299: t1.join();
300: assertTrue(r1.failure == null);
301: }
302:
303: static class TestMultiThreaded implements Runnable {
304: Exception failure;
305:
306: public void run() {
307: try {
308: System.out.println("+++ testMultiThreadedRunnable");
309: UsernamePasswordHandler handler = new UsernamePasswordHandler(
310: "jduke", "theduke");
311: LoginContext lc = new LoginContext(
312: "testSingleThreaded", handler);
313: lc.login();
314: Subject subject = lc.getSubject();
315: System.out.println("LC.Subject: " + subject);
316: Principal theduke = new SimplePrincipal("jduke");
317: assertTrue("Principals contains theduke", subject
318: .getPrincipals().contains(theduke));
319: Principal saPrincipal = SecurityAssociation
320: .getPrincipal();
321: assertTrue(
322: "SecurityAssociation.getPrincipal == theduke",
323: saPrincipal.equals(theduke));
324: char[] password = (char[]) SecurityAssociation
325: .getCredential();
326: assertTrue("password == theduke", Arrays.equals(
327: password, "theduke".toCharArray()));
328: } catch (Exception e) {
329: failure = e;
330: }
331: }
332: }
333:
334: public void testMultiThreadedRestoreIdentity() throws Exception {
335: TestMultiThreadedRestoreIdentity r0 = new TestMultiThreadedRestoreIdentity();
336: Thread t0 = new Thread(r0, "testMultiThreadedRestoreIdentity#0");
337: t0.start();
338: TestMultiThreadedRestoreIdentity r1 = new TestMultiThreadedRestoreIdentity();
339: Thread t1 = new Thread(r1, "testMultiThreadedRestoreIdentity#1");
340: t1.start();
341:
342: t0.join();
343: assertTrue(r0.failure == null);
344: t1.join();
345: assertTrue(r1.failure == null);
346: }
347:
348: static class TestMultiThreadedRestoreIdentity implements Runnable {
349: Exception failure;
350:
351: public void run() {
352: try {
353: System.out
354: .println("+++ testMultiThreadedRestoreIdentity");
355:
356: Principal jduke1 = new SimplePrincipal("jduke1");
357: SecurityAssociation.setPrincipal(jduke1);
358: SecurityAssociation.setCredential("theduke1");
359:
360: UsernamePasswordHandler handler = new UsernamePasswordHandler(
361: "jduke2", "theduke2");
362: LoginContext lc = new LoginContext(
363: "testSingleThreadedRestoreIdentity", handler);
364: lc.login();
365: Subject subject = lc.getSubject();
366: System.out.println("LC.Subject: " + subject);
367:
368: Principal jduke2 = new SimplePrincipal("jduke2");
369: assertTrue("Principals contains jduke2", subject
370: .getPrincipals().contains(jduke2));
371: Principal saPrincipal = SecurityAssociation
372: .getPrincipal();
373: assertTrue(
374: "SecurityAssociation.getPrincipal == jduke2",
375: saPrincipal.equals(jduke2));
376: char[] password = (char[]) SecurityAssociation
377: .getCredential();
378: assertTrue("password == theduke2", Arrays.equals(
379: password, "theduke2".toCharArray()));
380:
381: lc.logout();
382: // Validate restored state
383: saPrincipal = SecurityAssociation.getPrincipal();
384: assertTrue(
385: "SecurityAssociation.getPrincipal == jduke1",
386: saPrincipal.equals(jduke1));
387: String theduke1 = (String) SecurityAssociation
388: .getCredential();
389: assertTrue("password == theduke1", theduke1
390: .equals("theduke1"));
391:
392: } catch (Exception e) {
393: failure = e;
394: }
395: }
396: }
397:
398: public void testMultiThreadedRestoreStack() throws Exception {
399: TestMultiThreadedRestoreStack r0 = new TestMultiThreadedRestoreStack();
400: Thread t0 = new Thread(r0, "testMultiThreadedRestoreIdentity#0");
401: t0.start();
402: TestMultiThreadedRestoreStack r1 = new TestMultiThreadedRestoreStack();
403: Thread t1 = new Thread(r1, "testMultiThreadedRestoreIdentity#1");
404: t1.start();
405:
406: t0.join();
407: assertTrue(r0.failure == null);
408: t1.join();
409: assertTrue(r1.failure == null);
410: }
411:
412: static class TestMultiThreadedRestoreStack implements Runnable {
413: Exception failure;
414:
415: public void run() {
416: try {
417: System.out.println("+++ testMultThreadedRestoreStack");
418:
419: Principal jduke1 = new SimplePrincipal("jduke1");
420: Subject subject1 = new Subject();
421: SecurityAssociation.pushSubjectContext(subject1,
422: jduke1, "theduke1");
423:
424: Principal jduke2 = new SimplePrincipal("jduke2");
425: Subject subject2 = new Subject();
426: SecurityAssociation.pushSubjectContext(subject2,
427: jduke2, "theduke2");
428:
429: UsernamePasswordHandler handler = new UsernamePasswordHandler(
430: "jduke3", "theduke3");
431: LoginContext lc = new LoginContext(
432: "testSingleThreadedRestoreIdentity", handler);
433: lc.login();
434: Subject subject = lc.getSubject();
435: System.out.println("LC.Subject: " + subject);
436:
437: Principal jduke3 = new SimplePrincipal("jduke3");
438: assertTrue("Principals contains jduke3", subject
439: .getPrincipals().contains(jduke3));
440: Principal saPrincipal = SecurityAssociation
441: .getPrincipal();
442: assertTrue(
443: "SecurityAssociation.getPrincipal == jduke3",
444: saPrincipal.equals(jduke3));
445: char[] password = (char[]) SecurityAssociation
446: .getCredential();
447: assertTrue("password == theduke3", Arrays.equals(
448: password, "theduke3".toCharArray()));
449: SecurityAssociation.SubjectContext sc3 = SecurityAssociation
450: .peekSubjectContext();
451: System.out.println(sc3);
452: assertTrue(
453: "SecurityAssociation.peekSubjectContext == jduke3",
454: sc3.getPrincipal().equals(jduke3));
455: char[] theduke3 = (char[]) sc3.getCredential();
456: assertTrue("password == theduke3", Arrays.equals(
457: theduke3, "theduke3".toCharArray()));
458:
459: lc.logout();
460:
461: // Validate restored state
462: SecurityAssociation.SubjectContext sc2 = SecurityAssociation
463: .peekSubjectContext();
464: System.out.println(sc2);
465: assertTrue(
466: "SecurityAssociation.peekSubjectContext == jduke2",
467: sc2.getPrincipal().equals(jduke2));
468: String theduke2 = (String) sc2.getCredential();
469: assertTrue("password == theduke2", theduke2
470: .equals("theduke2"));
471:
472: SecurityAssociation.popSubjectContext();
473: SecurityAssociation.SubjectContext sc1 = SecurityAssociation
474: .peekSubjectContext();
475: System.out.println(sc1);
476: assertTrue(
477: "SecurityAssociation.peekSubjectContext == jduke1",
478: sc1.getPrincipal().equals(jduke1));
479: String theduke1 = (String) sc1.getCredential();
480: assertTrue("password == theduke1", theduke1
481: .equals("theduke1"));
482: } catch (Exception e) {
483: failure = e;
484: }
485: }
486: }
487:
488: }
|