KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.HashSet JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Properties JavaDoc;
27 import javax.security.auth.login.Configuration JavaDoc;
28 import javax.security.auth.login.AppConfigurationEntry JavaDoc;
29 import javax.security.auth.Subject JavaDoc;
30
31 import junit.framework.TestCase;
32 import junit.textui.TestRunner;
33
34 import org.jboss.logging.Logger;
35 import org.jboss.security.plugins.JaasSecurityManager;
36 import org.jboss.security.SimplePrincipal;
37 import org.jboss.security.SecurityAssociation;
38 import org.jboss.security.auth.callback.SecurityAssociationHandler;
39 import org.jboss.util.TimedCachePolicy;
40
41 /** Stress testing of the JaasSecurityManager
42  *
43  * @author Scott.Stark@jboss.org
44  * @version $Revision: 58115 $
45  */

46 public class SecurityMgrStressTestCase extends TestCase
47 {
48    static final int Nusers = 10;
49    static final Logger log = Logger.getLogger(SecurityMgrStressTestCase.class);
50    
51    /**
52     * Constructor for the SimpleUnitTestCase object
53     *
54     * @param name Test name
55     */

56    public SecurityMgrStressTestCase(String JavaDoc name)
57    {
58       super(name);
59    }
60
61    /** Test concurrent access to the isValid and doesUserHaveRole security
62     * mgr methods.
63     *
64     * @exception Exception thrown on any failure
65     */

66    public void testMTAuthentication() throws Exception JavaDoc
67    {
68       SecurityAssociation.setServer();
69       int count = Integer.getInteger("jbosstest.threadcount", 10).intValue();
70       int iterations = Integer.getInteger("jbosstest.iterationcount", 5000).intValue();
71       log.info("Creating "+count+" threads doing "+iterations+" iterations");
72       JaasSecurityManager secMgr = new JaasSecurityManager("testIdentity", new SecurityAssociationHandler());
73       TimedCachePolicy cache = new TimedCachePolicy(3, false, 100);
74       cache.create();
75       cache.start();
76       secMgr.setCachePolicy(cache);
77       Thread JavaDoc[] testThreads = new Thread JavaDoc[count];
78       AuthTester[] testers = new AuthTester[count];
79       for(int t = 0; t < count; t ++)
80       {
81          int userID = t % Nusers;
82          AuthTester test = new AuthTester(secMgr, iterations, userID);
83          if( t == count - 2 )
84             test.failAuthentication();
85          if( t == count - 1 )
86             test.failAuthorization();
87          testers[t] = test;
88          Thread JavaDoc thr = new Thread JavaDoc(test, "Tester#"+t);
89          thr.start();
90          testThreads[t] = thr;
91       }
92
93       for(int t = 0; t < count; t ++)
94       {
95          Thread JavaDoc thr = testThreads[t];
96          thr.join();
97          AuthTester test = testers[t];
98          if( test.failAuthentication == true || test.failAuthorization == true )
99             assertTrue("Failure test has an error", test.error != null);
100          else if( test.error != null )
101             fail("Unexpected error seen by : "+test);
102       }
103    }
104
105    protected void setUp()
106    {
107       // Install the custom JAAS configuration
108
Configuration.setConfiguration(new TestConfig());
109    }
110
111    /** Used to run the testcase from the command line
112     *
113     * @param args The command line arguments
114     */

115    public static void main(String JavaDoc[] args)
116    {
117       TestRunner.run(SecurityMgrStressTestCase.class);
118    }
119
120    /** Hard coded login configurations for the test cases. The configuration
121     name corresponds to the unit test function that uses the configuration.
122     */

123    private static class TestConfig extends Configuration JavaDoc
124    {
125       private AppConfigurationEntry JavaDoc[] theEntry;
126
127       TestConfig()
128       {
129          String JavaDoc name = "org.jboss.security.auth.spi.MemoryUsersRolesLoginModule";
130          Properties JavaDoc users = new Properties JavaDoc();
131          Properties JavaDoc roles = new Properties JavaDoc();
132          for(int i = 0; i < Nusers; i ++)
133          {
134             String JavaDoc username = "jduke"+i;
135             users.setProperty(username, "theduke"+i);
136             StringBuffer JavaDoc roleNames = new StringBuffer JavaDoc();
137             for(int j = 0; j < 3; j ++)
138             {
139                if( j > 0 )
140                   roleNames.append(',');
141                roleNames.append(username+"-Role"+j);
142             }
143             roles.setProperty(username, roleNames.toString());
144          }
145
146          HashMap JavaDoc options = new HashMap JavaDoc();
147          options.put("users", users);
148          options.put("roles", roles);
149          AppConfigurationEntry JavaDoc ace = new AppConfigurationEntry JavaDoc(name,
150             AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, options);
151          theEntry = new AppConfigurationEntry JavaDoc[]{ace};
152       }
153       public AppConfigurationEntry JavaDoc[] getAppConfigurationEntry(String JavaDoc name)
154       {
155          return theEntry;
156       }
157       public void refresh()
158       {
159       }
160    }
161
162    private static class AuthTester implements Runnable JavaDoc
163    {
164       JaasSecurityManager secMgr;
165       int iterations;
166       String JavaDoc username;
167       String JavaDoc password;
168       Throwable JavaDoc error;
169       boolean failAuthentication;
170       boolean failAuthorization;
171
172       AuthTester(JaasSecurityManager secMgr, int iterations, int id)
173       {
174          this.iterations = iterations;
175          this.secMgr = secMgr;
176          this.username = "jduke"+id;
177          this.password = "theduke"+id;
178       }
179
180       void failAuthentication()
181       {
182          failAuthentication = true;
183       }
184       void failAuthorization()
185       {
186          failAuthorization = true;
187       }
188
189       public void run()
190       {
191          log.info("Begin run, t="+Thread.currentThread());
192          String JavaDoc thePassword = password;
193          if( failAuthentication == true )
194              thePassword += "-fail";
195          SimplePrincipal user = new SimplePrincipal(username);
196          HashSet JavaDoc roleSet = new HashSet JavaDoc();
197          for(int j = 0; j < 3; j ++)
198          {
199             String JavaDoc role = username+"-Role"+j;
200             if( failAuthorization == true )
201                role += "-fail";
202             roleSet.add(new SimplePrincipal(role));
203          }
204
205          try
206          {
207             for(int i = 0; i < iterations; i ++)
208             {
209                Subject JavaDoc subject = new Subject JavaDoc();
210                boolean authenticated = secMgr.isValid(user, thePassword, subject);
211                if( authenticated == false )
212                   throw new SecurityException JavaDoc("Failed to authenticate: "+user);
213                SecurityAssociation.pushSubjectContext(subject, user, "any");
214                boolean authorized = secMgr.doesUserHaveRole(user, roleSet);
215                SecurityAssociation.popSubjectContext();
216                if( authorized == false )
217                {
218                   Subject JavaDoc s = secMgr.getActiveSubject();
219                   throw new SecurityException JavaDoc("Failed to authorize, subject="+s);
220                }
221             }
222          }
223          catch(Throwable JavaDoc t)
224          {
225             error = t;
226             if( failAuthentication == false && failAuthorization == false )
227             log.error("Security failure", t);
228          }
229          log.info("End run, t="+Thread.currentThread());
230       }
231    }
232
233 }
234
Popular Tags