KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
25 import java.io.FileWriter JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import java.security.Principal JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 import javax.security.auth.Subject JavaDoc;
33 import javax.security.auth.callback.Callback JavaDoc;
34 import javax.security.auth.callback.CallbackHandler JavaDoc;
35 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
36 import javax.security.auth.login.AppConfigurationEntry JavaDoc;
37 import javax.security.auth.login.Configuration JavaDoc;
38 import javax.security.auth.login.LoginContext JavaDoc;
39 import javax.security.auth.login.LoginException JavaDoc;
40
41 import junit.framework.TestCase;
42
43 import org.jboss.logging.Logger;
44 import org.jboss.logging.XLevel;
45 import org.jboss.security.SimpleGroup;
46 import org.jboss.security.SimplePrincipal;
47
48 //$Id: RoleMappingModuleUnitTestCase.java 58115 2006-11-04 08:42:14Z scott.stark@jboss.org $
49

50 /**
51  * JBAS-3323: Role Mapping Login Module that maps application role to
52  * declarative role
53  * @author <a HREF="mailto:Anil.Saldhana@jboss.org">Anil Saldhana</a>
54  * @since Jun 22, 2006
55  * @version $Revision: 58115 $
56  */

57 public class RoleMappingModuleUnitTestCase extends TestCase
58 {
59    private static String JavaDoc tmpDir = System.getProperty("java.io.tmpdir");
60    private static String JavaDoc rolesFile = "file:" + tmpDir + "/rolesmapping-roles.properties";
61    
62    static class TestConfig extends Configuration JavaDoc
63    {
64       public void refresh()
65       {
66       }
67       
68       public AppConfigurationEntry JavaDoc[] getAppConfigurationEntry(String JavaDoc name)
69       {
70          AppConfigurationEntry JavaDoc[] entry = null;
71          try
72          {
73             Class JavaDoc[] parameterTypes = {};
74             Method JavaDoc m = getClass().getDeclaredMethod(name, parameterTypes);
75             Object JavaDoc[] args = {};
76             entry = (AppConfigurationEntry JavaDoc[]) m.invoke(this, args);
77          }
78          catch(Exception JavaDoc e)
79          {
80          }
81          return entry;
82       }
83       
84       AppConfigurationEntry JavaDoc[] testRoleMapping()
85       {
86          AppConfigurationEntry JavaDoc ace = getIdentityLoginModuleEntry();
87          
88          String JavaDoc name2 = "org.jboss.security.auth.spi.RoleMappingLoginModule";
89          HashMap JavaDoc options2 = new HashMap JavaDoc();
90          options2.put("rolesProperties", rolesFile);
91          AppConfigurationEntry JavaDoc ace2 = new AppConfigurationEntry JavaDoc(name2,
92                AppConfigurationEntry.LoginModuleControlFlag.OPTIONAL, options2);
93          
94          AppConfigurationEntry JavaDoc[] entry = {ace,ace2};
95          return entry;
96       }
97       
98       AppConfigurationEntry JavaDoc[] testRoleMappingWithReplace()
99       {
100          AppConfigurationEntry JavaDoc ace = getIdentityLoginModuleEntry();
101          
102          String JavaDoc name2 = "org.jboss.security.auth.spi.RoleMappingLoginModule";
103          HashMap JavaDoc options2 = new HashMap JavaDoc();
104          options2.put("rolesProperties", rolesFile);
105          options2.put("replaceRole", "true");
106          AppConfigurationEntry JavaDoc ace2 = new AppConfigurationEntry JavaDoc(name2,
107                AppConfigurationEntry.LoginModuleControlFlag.OPTIONAL, options2);
108          
109          AppConfigurationEntry JavaDoc[] entry = {ace,ace2};
110          return entry;
111       }
112
113       private AppConfigurationEntry JavaDoc getIdentityLoginModuleEntry()
114       {
115          String JavaDoc name = "org.jboss.security.auth.spi.IdentityLoginModule";
116          HashMap JavaDoc options = new HashMap JavaDoc();
117          options.put("principal", "stark");
118          options.put("roles", "Role3,Role4");
119          AppConfigurationEntry JavaDoc ace = new AppConfigurationEntry JavaDoc(name,
120                AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, options);
121          return ace;
122       }
123    }
124    
125    public RoleMappingModuleUnitTestCase(String JavaDoc name)
126    {
127       super(name);
128    }
129    
130    protected void setUp() throws Exception JavaDoc
131    {
132       // Install the custom JAAS configuration
133
Configuration.setConfiguration(new TestConfig());
134    }
135    
136    /**
137     * Test the RoleMappingLoginModule with no option to replace the role
138     * @throws Exception
139     */

140    public void testRoleMappingModule() throws Exception JavaDoc
141    {
142       File JavaDoc file = createRolesFile();
143       assertTrue("File exists",file.exists());
144       processLogin("testRoleMapping", false);
145       clearRolesFile(file);
146    }
147    
148    /**
149     * Test the RoleMappingLoginModule with an option to replace the role
150     * @throws Exception
151     */

152    public void testRoleMappingModuleWithReplace() throws Exception JavaDoc
153    {
154       File JavaDoc file = createRolesFile();
155       assertTrue("File exists",file.exists());
156       processLogin("testRoleMappingWithReplace",true);
157       clearRolesFile(file);
158    }
159
160    /**
161     * Do the JAAS Login that includes the RoleMappingLoginModule
162     * @param config Jaas Configuration Name
163     * @param replaceRole flag whether the role has been replaced in the subject
164     * @throws LoginException
165     */

166    private void processLogin(String JavaDoc config, boolean replaceRole) throws LoginException JavaDoc
167    {
168       Subject JavaDoc subject = new Subject JavaDoc();
169       LoginContext JavaDoc lc = new LoginContext JavaDoc(config,subject, new TestCallbackHandler());
170       lc.login();
171       subject = lc.getSubject();
172       Iterator JavaDoc iter = subject.getPrincipals().iterator();
173       boolean ranAsserts = false;
174       while(iter.hasNext())
175       {
176          Principal JavaDoc p = (Principal JavaDoc)iter.next();
177          if(p instanceof SimpleGroup)
178          {
179             SimpleGroup sg = (SimpleGroup)p;
180             ranAsserts = true;
181             assertTrue("testRole exists?", sg.isMember(new SimplePrincipal("testRole")));
182             assertTrue("testRole2 exists?", sg.isMember(new SimplePrincipal("testRole2")));
183             assertTrue("Role4 exists?", sg.isMember(new SimplePrincipal("Role4")));
184             if(replaceRole)
185               assertFalse("Role3 does not exist?", sg.isMember(new SimplePrincipal("Role3")));
186             else
187                assertTrue("Role3 exists?", sg.isMember(new SimplePrincipal("Role3")));
188          }
189       }
190       assertTrue("Ran Asserts?",ranAsserts);
191    }
192
193    /**
194     * Delete the properties file created for the test
195     * @param file
196     */

197    private void clearRolesFile(File JavaDoc file)
198    {
199       if(file.exists())
200          file.delete();
201       assertFalse("File does not exist",file.exists());
202    }
203
204    /**
205     * Create a properties file for the test
206     * @return
207     * @throws IOException
208     */

209    private File JavaDoc createRolesFile() throws IOException JavaDoc
210    {
211       File JavaDoc file = new File JavaDoc(tmpDir + "/rolesmapping-roles.properties");
212       clearRolesFile(file); //Delete residual files (if any)
213
FileWriter JavaDoc fw = new FileWriter JavaDoc(file);
214       fw.write("Role3=testRole,testRole2");
215       fw.close();
216       return file;
217    }
218    
219    /**
220     *
221     * A TestCallbackHandler.
222     * Does not do anything.
223     * @author <a HREF="anil.saldhana@jboss.com">Anil Saldhana</a>
224     * @version $Revision: 58115 $
225     */

226    private class TestCallbackHandler implements CallbackHandler JavaDoc
227    {
228       public void handle(Callback JavaDoc[] arg0) throws IOException JavaDoc,
229       UnsupportedCallbackException JavaDoc
230       {
231       }
232    }
233 }
234
Popular Tags