KickJava   Java API By Example, From Geeks To Geeks.

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


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.security.Principal JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28
29 import javax.security.auth.login.Configuration JavaDoc;
30 import javax.security.auth.login.AppConfigurationEntry JavaDoc;
31 import javax.security.auth.login.LoginContext JavaDoc;
32 import javax.security.auth.Subject JavaDoc;
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 /**
42  ClientLoginModuleUnitTestCase/SecurityAssociation interaction tests
43  
44  @author Scott.Stark@jboss.org
45  @version $Revision: 41290 $
46 */

47 public class ClientLoginModuleUnitTestCase
48    extends TestCase
49 {
50    static TestConfig jaasConfig = new TestConfig();
51
52    static class TestConfig extends Configuration JavaDoc
53    {
54       public void refresh()
55       {
56       }
57
58       public AppConfigurationEntry JavaDoc[] getAppConfigurationEntry(String JavaDoc name)
59       {
60          AppConfigurationEntry JavaDoc[] entry = null;
61          try
62          {
63             Class JavaDoc[] parameterTypes = {};
64             Method JavaDoc m = getClass().getDeclaredMethod(name, parameterTypes);
65             Object JavaDoc[] args = {};
66             entry = (AppConfigurationEntry JavaDoc[]) m.invoke(this, args);
67          }
68          catch(Exception JavaDoc e)
69          {
70          }
71          return entry;
72       }
73       AppConfigurationEntry JavaDoc[] testSingleThreaded()
74       {
75          String JavaDoc name = "org.jboss.security.ClientLoginModule";
76          HashMap JavaDoc options = new HashMap JavaDoc();
77          options.put("multi-threaded", "false");
78          AppConfigurationEntry JavaDoc ace = new AppConfigurationEntry JavaDoc(name,
79          AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, options);
80          AppConfigurationEntry JavaDoc[] entry = {ace};
81          return entry;
82       }
83       AppConfigurationEntry JavaDoc[] testSingleThreadedRestoreIdentity()
84       {
85          String JavaDoc name = "org.jboss.security.ClientLoginModule";
86          HashMap JavaDoc options = new HashMap JavaDoc();
87          options.put("multi-threaded", "false");
88          options.put("restore-login-identity", "true");
89          AppConfigurationEntry JavaDoc ace = new AppConfigurationEntry JavaDoc(name,
90          AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, options);
91          AppConfigurationEntry JavaDoc[] entry = {ace};
92          return entry;
93       }
94       AppConfigurationEntry JavaDoc[] testSingleThreadedRestoreStack()
95       {
96          String JavaDoc name = "org.jboss.security.ClientLoginModule";
97          HashMap JavaDoc options = new HashMap JavaDoc();
98          options.put("multi-threaded", "false");
99          options.put("restore-login-identity", "true");
100          AppConfigurationEntry JavaDoc ace = new AppConfigurationEntry JavaDoc(name,
101          AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, options);
102          AppConfigurationEntry JavaDoc[] entry = {ace};
103          return entry;
104       }
105       AppConfigurationEntry JavaDoc[] testMultiThreaded()
106       {
107          String JavaDoc name = "org.jboss.security.ClientLoginModule";
108          HashMap JavaDoc options = new HashMap JavaDoc();
109          options.put("multi-threaded", "true");
110          AppConfigurationEntry JavaDoc ace = new AppConfigurationEntry JavaDoc(name,
111          AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, options);
112          AppConfigurationEntry JavaDoc[] entry = {ace};
113          return entry;
114       }
115       AppConfigurationEntry JavaDoc[] testMultiThreadedRestoreIdentity()
116       {
117          String JavaDoc name = "org.jboss.security.ClientLoginModule";
118          HashMap JavaDoc options = new HashMap JavaDoc();
119          options.put("multi-threaded", "true");
120          options.put("restore-login-identity", "true");
121          AppConfigurationEntry JavaDoc ace = new AppConfigurationEntry JavaDoc(name,
122          AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, options);
123          AppConfigurationEntry JavaDoc[] entry = {ace};
124          return entry;
125       }
126       AppConfigurationEntry JavaDoc[] testMultiThreadedRestoreStack()
127       {
128          String JavaDoc name = "org.jboss.security.ClientLoginModule";
129          HashMap JavaDoc options = new HashMap JavaDoc();
130          options.put("multi-threaded", "true");
131          options.put("restore-login-identity", "true");
132          AppConfigurationEntry JavaDoc ace = new AppConfigurationEntry JavaDoc(name,
133          AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, options);
134          AppConfigurationEntry JavaDoc[] entry = {ace};
135          return entry;
136       }
137       
138    }
139
140    public static Test suite() throws Exception JavaDoc
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 JavaDoc name)
153    {
154       super(name);
155    }
156
157    protected void setUp() throws Exception JavaDoc
158    {
159       Configuration.setConfiguration(jaasConfig);
160    }
161    protected void tearDown()
162    {
163    }
164
165    public void testSingleThreaded() throws Exception JavaDoc
166    {
167       System.out.println("+++ testSingleThreaded");
168       UsernamePasswordHandler handler = new UsernamePasswordHandler("jduke",
169          "theduke");
170       LoginContext JavaDoc lc = new LoginContext JavaDoc("testSingleThreaded", handler);
171       lc.login();
172       Subject JavaDoc subject = lc.getSubject();
173       System.out.println("LC.Subject: "+subject);
174       Principal JavaDoc theduke = new SimplePrincipal("jduke");
175       assertTrue("Principals contains theduke", subject.getPrincipals().contains(theduke));
176       Principal JavaDoc 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 JavaDoc
184    {
185       System.out.println("+++ testSingleThreadedRestoreIdentity");
186
187       Principal JavaDoc jduke1 = new SimplePrincipal("jduke1");
188       SecurityAssociation.setPrincipal(jduke1);
189       SecurityAssociation.setCredential("theduke1");
190
191       UsernamePasswordHandler handler = new UsernamePasswordHandler("jduke2",
192          "theduke2");
193       LoginContext JavaDoc lc = new LoginContext JavaDoc("testSingleThreadedRestoreIdentity", handler);
194       lc.login();
195       Subject JavaDoc subject = lc.getSubject();
196       System.out.println("LC.Subject: "+subject);
197       
198       Principal JavaDoc jduke2 = new SimplePrincipal("jduke2");
199       assertTrue("Principals contains jduke2", subject.getPrincipals().contains(jduke2));
200       Principal JavaDoc 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       // Validate restored state
208
saPrincipal = SecurityAssociation.getPrincipal();
209       assertTrue("SecurityAssociation.getPrincipal == jduke1", saPrincipal.equals(jduke1));
210       String JavaDoc theduke1 = (String JavaDoc) SecurityAssociation.getCredential();
211       assertTrue("password == theduke1", theduke1.equals("theduke1"));
212       
213    }
214
215    public void testSingleThreadedRestoreStack() throws Exception JavaDoc
216    {
217       System.out.println("+++ testSingleThreadedRestoreStack");
218
219       Principal JavaDoc jduke1 = new SimplePrincipal("jduke1");
220       Subject JavaDoc subject1 = new Subject JavaDoc();
221       SecurityAssociation.pushSubjectContext(subject1, jduke1, "theduke1");
222
223       Principal JavaDoc jduke2 = new SimplePrincipal("jduke2");
224       Subject JavaDoc subject2 = new Subject JavaDoc();
225       SecurityAssociation.pushSubjectContext(subject2, jduke2, "theduke2");
226
227       UsernamePasswordHandler handler = new UsernamePasswordHandler("jduke3",
228          "theduke3");
229       LoginContext JavaDoc lc = new LoginContext JavaDoc("testSingleThreadedRestoreIdentity", handler);
230       lc.login();
231       Subject JavaDoc subject = lc.getSubject();
232       System.out.println("LC.Subject: "+subject);
233       
234       Principal JavaDoc jduke3 = new SimplePrincipal("jduke3");
235       assertTrue("Principals contains jduke3", subject.getPrincipals().contains(jduke3));
236       Principal JavaDoc 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       // Validate restored state
251
SecurityAssociation.SubjectContext sc2 = SecurityAssociation.peekSubjectContext();
252       System.out.println(sc2);
253       assertTrue("SecurityAssociation.peekSubjectContext == jduke2", sc2.getPrincipal().equals(jduke2));
254       String JavaDoc theduke2 = (String JavaDoc) 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 JavaDoc theduke1 = (String JavaDoc) sc1.getCredential();
262       assertTrue("password == theduke1", theduke1.equals("theduke1"));
263    }
264
265    public void testMultiThreaded() throws Exception JavaDoc
266    {
267       TestMultiThreaded r0 = new TestMultiThreaded();
268       Thread JavaDoc t0 = new Thread JavaDoc(r0, "testMultiThreaded#0");
269       t0.start();
270       TestMultiThreaded r1 = new TestMultiThreaded();
271       Thread JavaDoc t1 = new Thread JavaDoc(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 JavaDoc
280    {
281       Exception JavaDoc failure;
282       public void run()
283       {
284          try
285          {
286             System.out.println("+++ testMultiThreadedRunnable");
287             UsernamePasswordHandler handler = new UsernamePasswordHandler("jduke",
288                "theduke");
289             LoginContext JavaDoc lc = new LoginContext JavaDoc("testSingleThreaded", handler);
290             lc.login();
291             Subject JavaDoc subject = lc.getSubject();
292             System.out.println("LC.Subject: "+subject);
293             Principal JavaDoc theduke = new SimplePrincipal("jduke");
294             assertTrue("Principals contains theduke", subject.getPrincipals().contains(theduke));
295             Principal JavaDoc 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 JavaDoc e)
302          {
303             failure = e;
304          }
305       }
306    }
307
308    public void testMultiThreadedRestoreIdentity() throws Exception JavaDoc
309    {
310       TestMultiThreadedRestoreIdentity r0 = new TestMultiThreadedRestoreIdentity();
311       Thread JavaDoc t0 = new Thread JavaDoc(r0, "testMultiThreadedRestoreIdentity#0");
312       t0.start();
313       TestMultiThreadedRestoreIdentity r1 = new TestMultiThreadedRestoreIdentity();
314       Thread JavaDoc t1 = new Thread JavaDoc(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 JavaDoc
323    {
324       Exception JavaDoc failure;
325       public void run()
326       {
327          try
328          {
329             System.out.println("+++ testMultiThreadedRestoreIdentity");
330       
331             Principal JavaDoc jduke1 = new SimplePrincipal("jduke1");
332             SecurityAssociation.setPrincipal(jduke1);
333             SecurityAssociation.setCredential("theduke1");
334       
335             UsernamePasswordHandler handler = new UsernamePasswordHandler("jduke2",
336                "theduke2");
337             LoginContext JavaDoc lc = new LoginContext JavaDoc("testSingleThreadedRestoreIdentity", handler);
338             lc.login();
339             Subject JavaDoc subject = lc.getSubject();
340             System.out.println("LC.Subject: "+subject);
341             
342             Principal JavaDoc jduke2 = new SimplePrincipal("jduke2");
343             assertTrue("Principals contains jduke2", subject.getPrincipals().contains(jduke2));
344             Principal JavaDoc 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             // Validate restored state
352
saPrincipal = SecurityAssociation.getPrincipal();
353             assertTrue("SecurityAssociation.getPrincipal == jduke1", saPrincipal.equals(jduke1));
354             String JavaDoc theduke1 = (String JavaDoc) SecurityAssociation.getCredential();
355             assertTrue("password == theduke1", theduke1.equals("theduke1"));
356       
357          }
358          catch(Exception JavaDoc e)
359          {
360             failure = e;
361          }
362       }
363    }
364
365    public void testMultiThreadedRestoreStack() throws Exception JavaDoc
366    {
367       TestMultiThreadedRestoreStack r0 = new TestMultiThreadedRestoreStack();
368       Thread JavaDoc t0 = new Thread JavaDoc(r0, "testMultiThreadedRestoreIdentity#0");
369       t0.start();
370       TestMultiThreadedRestoreStack r1 = new TestMultiThreadedRestoreStack();
371       Thread JavaDoc t1 = new Thread JavaDoc(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 JavaDoc
380    {
381       Exception JavaDoc failure;
382       public void run()
383       {
384          try
385          {
386             System.out.println("+++ testMultThreadedRestoreStack");
387
388             Principal JavaDoc jduke1 = new SimplePrincipal("jduke1");
389             Subject JavaDoc subject1 = new Subject JavaDoc();
390             SecurityAssociation.pushSubjectContext(subject1, jduke1, "theduke1");
391
392             Principal JavaDoc jduke2 = new SimplePrincipal("jduke2");
393             Subject JavaDoc subject2 = new Subject JavaDoc();
394             SecurityAssociation.pushSubjectContext(subject2, jduke2, "theduke2");
395
396             UsernamePasswordHandler handler = new UsernamePasswordHandler("jduke3",
397                "theduke3");
398             LoginContext JavaDoc lc = new LoginContext JavaDoc("testSingleThreadedRestoreIdentity", handler);
399             lc.login();
400             Subject JavaDoc subject = lc.getSubject();
401             System.out.println("LC.Subject: "+subject);
402       
403             Principal JavaDoc jduke3 = new SimplePrincipal("jduke3");
404             assertTrue("Principals contains jduke3", subject.getPrincipals().contains(jduke3));
405             Principal JavaDoc 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             // Validate restored state
420
SecurityAssociation.SubjectContext sc2 = SecurityAssociation.peekSubjectContext();
421             System.out.println(sc2);
422             assertTrue("SecurityAssociation.peekSubjectContext == jduke2", sc2.getPrincipal().equals(jduke2));
423             String JavaDoc theduke2 = (String JavaDoc) 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 JavaDoc theduke1 = (String JavaDoc) sc1.getCredential();
431             assertTrue("password == theduke1", theduke1.equals("theduke1"));
432          }
433          catch(Exception JavaDoc e)
434          {
435             failure = e;
436          }
437       }
438    }
439
440 }
441
Popular Tags