KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.HttpURLConnection JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import javax.management.ObjectName JavaDoc;
29
30 import junit.framework.Test;
31 import junit.framework.TestSuite;
32
33 import org.jboss.security.NestableGroup;
34 import org.jboss.security.NestablePrincipal;
35 import org.jboss.security.RunAsIdentity;
36 import org.jboss.security.SimpleGroup;
37 import org.jboss.security.SimplePrincipal;
38 import org.jboss.test.JBossTestCase;
39 import org.jboss.test.JBossTestSetup;
40 import org.jboss.test.util.web.HttpUtils;
41
42 //$Id: DeepCopySubjectUnitTestCase.java 46076 2006-07-05 18:59:22Z asaldhana $
43

44 /**
45  * JBAS-2657: Add option to deep copy the authenticated subject sets
46  *
47  * Testcase that unit tests the cloneability of various JBossSX
48  * Principal/Groups
49  * Also does a test of the serverside Subject deep copy via a mutable
50  * Principal
51  * @author <a HREF="mailto:Anil.Saldhana@jboss.org">Anil Saldhana</a>
52  * @since Apr 4, 2006
53  * @version $Revision: 46076 $
54  */

55 public class DeepCopySubjectUnitTestCase extends JBossTestCase
56 {
57    public static String JavaDoc REALM = "JBossTest Servlets";
58    
59    public DeepCopySubjectUnitTestCase(String JavaDoc name)
60    {
61       super(name);
62    }
63    
64    /**
65     * Test the cloneability of Nestable Principal
66     *
67     * @throws Exception
68     */

69    public void testCloneNestablePrincipal() throws Exception JavaDoc
70    {
71       SimplePrincipal sp1 = new SimplePrincipal("sp1");
72       SimplePrincipal sp2 = new SimplePrincipal("sp2");
73       NestablePrincipal np = new NestablePrincipal("TestStack");
74       //Add principals to the NestablePrincipal
75
np.addMember(sp1);
76       np.addMember(sp2);
77       assertTrue("np.isMember(sp2)", np.isMember(sp2));
78       
79       //Clone the NestablePrincipal
80
NestablePrincipal clonedNP = (NestablePrincipal)np.clone();
81       
82       //Remove a principal from the orig NestablePrincipal
83
np.removeMember(sp2);
84       //Only the active principal is valid
85
assertFalse("np.isMember(sp2) == false", np.isMember(sp2));
86       assertTrue("np.isMember(sp1)", np.isMember(sp1));
87       //Check that the cloned NestablePrincipal is not affected
88
assertTrue("clonedNP.isMember(sp2)", clonedNP.isMember(sp2));
89    }
90    
91    /**
92     * Test the Cloneability of NestableGroup
93     *
94     * @throws Exception
95     */

96    public void testCloneNestableGroup() throws Exception JavaDoc
97    {
98       SimplePrincipal sp1 = new SimplePrincipal("sp1");
99       SimplePrincipal sp2 = new SimplePrincipal("sp2");
100       
101       SimpleGroup sg1 = new SimpleGroup("sg1");
102       SimpleGroup sg2 = new SimpleGroup("sg1");
103       sg1.addMember(sp1);
104       sg2.addMember(sp2);
105       NestableGroup ng = new NestableGroup("TestGroup");
106       //Add principals to the NestablePrincipal
107
ng.addMember(sg1);
108       ng.addMember(sg2);
109       assertTrue("ng.isMember(sp2)", ng.isMember(sp2));
110       
111       //Clone the NestableGroup
112
NestableGroup clonedNP = (NestableGroup)ng.clone();
113       
114       //Remove a group from the orig NestableGroup
115
ng.removeMember(sg2);
116       //Only the active principal is valid
117
assertFalse("ng.isMember(sp2) == false", ng.isMember(sp2));
118       assertTrue("ng.isMember(sp1)", ng.isMember(sp1));
119       //Check that the cloned NestablePrincipal is not affected
120
assertTrue("clonedNP.isMember(sp2)", clonedNP.isMember(sp2));
121    }
122    
123    /**
124     * Test the cloneability of Simple Group
125     *
126     * @throws Exception
127     */

128    public void testCloneSimpleGroup() throws Exception JavaDoc
129    {
130       SimplePrincipal sp1 = new SimplePrincipal("sp1");
131       SimplePrincipal sp2 = new SimplePrincipal("sp2");
132       
133       SimpleGroup sg = new SimpleGroup("sg1");
134       sg.addMember(sp1);
135       sg.addMember(sp2);
136       assertTrue("sg.isMember(sp1)", sg.isMember(sp1));
137       assertTrue("sg.isMember(sp2)", sg.isMember(sp2));
138       
139       //Clone
140
SimpleGroup clonedSP = (SimpleGroup)sg.clone();
141       sg.removeMember(sp2);
142       
143       //Only the active principal is valid
144
assertFalse("sg.isMember(sp2) == false", sg.isMember(sp2));
145       assertTrue("sg.isMember(sp1)", sg.isMember(sp1));
146       //Check that the cloned SimpleGroup is not affected
147
assertTrue("clonedSP.isMember(sp2)", clonedSP.isMember(sp2));
148    }
149    
150    /**
151     * Test the cloneability of RunAsIdentity
152     *
153     * @throws Exception
154     */

155    public void testCloneRunAsIdentity() throws Exception JavaDoc
156    {
157       SimplePrincipal sp1 = new SimplePrincipal("sp1");
158       SimplePrincipal sp2 = new SimplePrincipal("sp2");
159       RunAsIdentity ras = new RunAsIdentity("testRole", "testUser");
160       //There is no need to test the set of run-as roles
161
//as each time, a new HashSet is returned
162
Set JavaDoc principalSet = ras.getPrincipalsSet();
163       principalSet.add(sp1);
164       principalSet.add(sp2);
165       //Clone
166
RunAsIdentity rasClone = (RunAsIdentity)ras.clone();
167       principalSet.remove(sp1);
168       assertFalse("principalSet.contains(sp1)==false",
169             principalSet.contains(sp1));
170
171       Set JavaDoc clonedPrincipalSet = rasClone.getPrincipalsSet();
172       assertTrue("clonedPrincipalSet.contains(sp1)",
173                      clonedPrincipalSet.contains(sp1));
174       assertTrue("clonedPrincipalSet.contains(sp2)",
175                      clonedPrincipalSet.contains(sp2));
176    }
177    
178    /**
179     * Test the Deep Copy of Subjects by the JaasSecurityManager
180     * via a test servlet deployed
181     *
182     * @throws Exception
183     */

184    public void testSubjectCloning() throws Exception JavaDoc
185    {
186       flagDeepCopy(Boolean.FALSE);
187       accessWeb(true);
188       flagDeepCopy(Boolean.TRUE);
189       this.redeploy("deepcopy.ear");
190       accessWeb(false);
191       flagDeepCopy(Boolean.FALSE);
192       this.redeploy("deepcopy.ear");
193       accessWeb(true);
194    }
195    
196    /**
197     * Turn the deep copy of subjects on the JaasSecurityManagerService
198     * ON or OFF based on the flag
199     *
200     * @param flag Boolean.TRUE or Boolean.FALSE
201     * @throws Exception
202     */

203    private void flagDeepCopy(Boolean JavaDoc flag) throws Exception JavaDoc
204    {
205       this.getServer().invoke(new ObjectName JavaDoc("jboss.security:service=JaasSecurityManager"),
206             "setDeepCopySubjectMode",new Object JavaDoc[]{flag}, new String JavaDoc[]{Boolean.TYPE.getName()});
207    }
208    
209    /**
210     * Utility method that accesses the secured servlet
211     * @param shouldMatch Parameter to be passed to the web app
212     * @throws Exception
213     */

214    private void accessWeb(boolean shouldMatch) throws Exception JavaDoc
215    {
216       //Access the SecureServletSecureEJB servlet
217
String JavaDoc baseURL = HttpUtils.getBaseURL("scott", "echoman");
218       //Test the Restricted servlet
219
URL JavaDoc url = new URL JavaDoc(baseURL+"deepcopy/DeepCopyServlet?shouldMatch="+shouldMatch);
220       HttpUtils.accessURL(url, REALM, HttpURLConnection.HTTP_OK);
221    }
222     
223    public static Test suite() throws Exception JavaDoc
224    {
225       TestSuite suite = new TestSuite();
226       suite.addTest(new TestSuite(DeepCopySubjectUnitTestCase.class));
227
228       // Create an initializer for the test suite
229
Test wrapper = new JBossTestSetup(suite)
230       {
231          protected void setUp() throws Exception JavaDoc
232          {
233             super.setUp();
234             deploy("deepcopy.ear");
235             // Make sure the security cache is clear
236
flushAuthCache();
237          }
238          protected void tearDown() throws Exception JavaDoc
239          {
240             undeploy("deepcopy.ear");
241             super.tearDown();
242          }
243       };
244       return wrapper;
245    }
246 }
247
Popular Tags