KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > securitymgr > test > PolicyUnitTestCase


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.securitymgr.test;
23
24 import java.io.IOException JavaDoc;
25 import java.net.ServerSocket JavaDoc;
26 import java.net.Socket JavaDoc;
27
28 import org.jboss.test.securitymgr.ejb.BadBean;
29 import org.jboss.test.securitymgr.ejb.IOStatelessSessionBean;
30
31 import junit.framework.Test;
32 import junit.framework.TestSuite;
33
34 import org.jboss.logging.Logger;
35
36 import org.jboss.test.JBossTestCase;
37
38 /** Tests of the security permission enforcement that creates and directly
39  invokes the ejb methods to test the security policy permissions
40  without the noise of the ejb container.
41
42 @author Scott.Stark@jboss.org
43 @version $Revision: 58115 $
44  */

45 public class PolicyUnitTestCase extends JBossTestCase
46 {
47
48    public PolicyUnitTestCase(String JavaDoc name)
49    {
50       super(name);
51    }
52
53    /** Test that a bean cannot access the SecurityAssociation class
54     */

55    public void testSecurityAssociation() throws Exception JavaDoc
56    {
57       log.debug("+++ testSecurityAssociation()");
58       BadBean bean = getBadSession();
59
60       try
61       {
62          bean.getPrincipal();
63          doFail("Was able to call Bad.getPrincipal");
64       }
65       catch(Exception JavaDoc e)
66       {
67          log.debug("Bad.getPrincipal failed as expected", e);
68       }
69
70       try
71       {
72          bean.getCredential();
73          doFail("Was able to call Bad.getCredential");
74       }
75       catch(Exception JavaDoc e)
76       {
77          log.debug("Bad.getCredential failed as expected", e);
78       }
79
80       try
81       {
82          bean.setPrincipal(null);
83          doFail("Was able to call Bad.setPrincipal");
84       }
85       catch(Exception JavaDoc e)
86       {
87          log.debug("Bad.setPrincipal failed as expected", e);
88       }
89
90       try
91       {
92          char[] password = "secret".toCharArray();
93          bean.setCredential(password);
94          doFail("Was able to call Bad.setCredential");
95       }
96       catch(Exception JavaDoc e)
97       {
98          log.debug("Bad.setCredential failed as expected", e);
99       }
100    }
101
102    /** Test that a bean cannot access the filesystem using java.io.File
103     */

104    public void testFileIO() throws Exception JavaDoc
105    {
106       log.debug("+++ testFileIO()");
107       IOStatelessSessionBean bean = getIOSession();
108
109       try
110       {
111          // This should fail because the bean calls File.exists()
112
bean.read("nofile.txt");
113          doFail("Was able to call IOSession.read");
114       }
115       catch(Exception JavaDoc e)
116       {
117          log.debug("IOSession.read failed as expected", e);
118       }
119
120       try
121       {
122          // This should fail because the bean calls File.exists()
123
bean.write("nofile.txt");
124          doFail("Was able to call IOSession.write");
125       }
126       catch(Exception JavaDoc e)
127       {
128          log.debug("IOSession.write failed as expected", e);
129       }
130    }
131
132    public void testSockets() throws Exception JavaDoc
133    {
134       log.debug("+++ testSockets()");
135       IOStatelessSessionBean bean = getIOSession();
136       try
137       {
138          bean.listen(0);
139          doFail("Was able to call IOSession.listen");
140       }
141       catch(Exception JavaDoc e)
142       {
143          log.debug("IOSession.listen failed as expected", e);
144       }
145
146       final ServerSocket JavaDoc tmp = new ServerSocket JavaDoc(0);
147       log.debug("Created ServerSocket: "+tmp);
148       final Logger theLog = log;
149       Thread JavaDoc t = new Thread JavaDoc("Acceptor")
150       {
151          public void run()
152          {
153             try
154             {
155                Socket JavaDoc s = tmp.accept();
156                theLog.debug("Accepted Socket: "+s);
157                s.close();
158                theLog.debug("ServerSocket thread exiting");
159             }
160             catch(IOException JavaDoc e)
161             {
162             }
163          }
164       };
165       int port = tmp.getLocalPort();
166       t.start();
167       bean.connect("localhost", port);
168       tmp.close();
169    }
170
171    public void testClassLoaders() throws Exception JavaDoc
172    {
173       log.debug("+++ testClassLoaders()");
174       IOStatelessSessionBean bean = getIOSession();
175       try
176       {
177          bean.createClassLoader();
178          doFail("Was able to call IOSession.createClassLoader");
179       }
180       catch(Exception JavaDoc e)
181       {
182          log.debug("IOSession.createClassLoader failed as expected", e);
183       }
184
185       try
186       {
187          bean.getContextClassLoader();
188          //doFail("Was able to call IOSession.getContextClassLoader");
189
log.debug("Was able to call IOSession.getContextClassLoader");
190       }
191       catch(Exception JavaDoc e)
192       {
193          log.debug("IOSession.getContextClassLoader failed as expected", e);
194       }
195
196       try
197       {
198          bean.setContextClassLoader();
199          doFail("Was able to call IOSession.setContextClassLoader");
200       }
201       catch(Exception JavaDoc e)
202       {
203          log.debug("IOSession.setContextClassLoader failed as expected", e);
204       }
205    }
206
207    public void testReflection() throws Exception JavaDoc
208    {
209       log.debug("+++ testReflection()");
210       IOStatelessSessionBean bean = getIOSession();
211       try
212       {
213          bean.useReflection();
214          doFail("Was able to call IOSession.useReflection");
215       }
216       catch(Exception JavaDoc e)
217       {
218          log.debug("IOSession.useReflection failed as expected", e);
219       }
220    }
221
222    public void testThreadAccess() throws Exception JavaDoc
223    {
224       log.debug("+++ testThreadAccess()");
225       IOStatelessSessionBean bean = getIOSession();
226       try
227       {
228          // This test will fail because the calling thread it not in the root thread group
229
bean.renameThread();
230          doFail("Was able to call IOSession.renameThread");
231       }
232       catch(Exception JavaDoc e)
233       {
234          log.debug("IOSession.renameThread failed as expected", e);
235       }
236    }
237
238    public void testSystemAccess() throws Exception JavaDoc
239    {
240       log.debug("+++ testSystemAccess()");
241       IOStatelessSessionBean bean = getIOSession();
242       try
243       {
244          bean.createSecurityMgr();
245          doFail("Was able to call IOSession.createSecurityMgr");
246       }
247       catch(Exception JavaDoc e)
248       {
249          log.debug("IOSession.createSecurityMgr failed as expected", e);
250       }
251
252       try
253       {
254          bean.createSecurityMgr();
255          doFail("Was able to call IOSession.changeSystemOut");
256       }
257       catch(Exception JavaDoc e)
258       {
259          log.debug("IOSession.changeSystemOut failed as expected", e);
260       }
261
262       try
263       {
264          bean.changeSystemErr();
265          doFail("Was able to call IOSession.changeSystemErr");
266       }
267       catch(Exception JavaDoc e)
268       {
269          log.debug("IOSession.changeSystemErr failed as expected", e);
270       }
271
272       try
273       {
274          bean.loadLibrary();
275          doFail("Was able to call IOSession.loadLibrary");
276       }
277       catch(Exception JavaDoc e)
278       {
279          log.debug("IOSession.loadLibrary failed as expected", e);
280       }
281
282       /* This test can't be enforced when running in a forked VM. The
283        Runtime.exec adss the (java.lang.RuntimePermission exitVM) to all jars
284        in the caller classpath it seems. We can't let it fail as this
285        kills the junit vm and no results are generated.
286       try
287       {
288          bean.systemExit(1);
289          doFail("Was able to call IOSession.systemExit");
290       }
291       catch(Exception e)
292       {
293          log.debug("IOSession.systemExit failed as expected", e);
294       }
295       */

296    }
297
298    /** We don't use the JBoss server so nullify this test
299     */

300    public void testServerFound() throws Exception JavaDoc
301    {
302      log.debug("+++ testServerFound()");
303    }
304
305    /** We don't deploy anything.
306     */

307    public static Test suite() throws Exception JavaDoc
308    {
309       TestSuite suite = new TestSuite(PolicyUnitTestCase.class);
310       //suite.addTest(new PolicyUnitTestCase("testReflection"));
311
return suite;
312    }
313
314    private BadBean getBadSession()
315    {
316       BadBean bean = new BadBean();
317       return bean;
318    }
319    private IOStatelessSessionBean getIOSession()
320    {
321       IOStatelessSessionBean bean = new IOStatelessSessionBean();
322       return bean;
323    }
324    private void doFail(String JavaDoc msg)
325    {
326       log.error(msg);
327       fail(msg);
328    }
329    private void doFail(String JavaDoc msg, Throwable JavaDoc t)
330    {
331       log.error(msg, t);
332       fail(msg);
333    }
334 }
335
Popular Tags