KickJava   Java API By Example, From Geeks To Geeks.

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


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 junit.framework.Test;
29 import org.jboss.test.JBossTestCase;
30 import org.jboss.test.securitymgr.interfaces.IOSession;
31 import org.jboss.test.securitymgr.interfaces.IOSessionHome;
32
33 /** Tests of the programming restrictions defined by the EJB spec. The JBoss
34 server must be running under a security manager. The securitymgr-ejb.jar
35 should be granted only the following permission:
36
37 grant securitymgr-ejb.jar {
38    permission java.util.PropertyPermission "*", "read";
39    permission java.lang.RuntimePermission "queuePrintJob";
40    permission java.net.SocketPermission "*", "connect";
41  };
42
43 @author Scott.Stark@jboss.org
44 @version $Revision: 37406 $
45  */

46 public class EJBSpecUnitTestCase
47    extends JBossTestCase
48 {
49
50    public EJBSpecUnitTestCase(String JavaDoc name)
51    {
52       super(name);
53    }
54
55    /** Test that a bean cannot access the filesystem using java.io.File
56     */

57    public void testFileExists() throws Exception JavaDoc
58    {
59       log.debug("+++ testFileExists()");
60       IOSession bean = getIOSession();
61
62       try
63       {
64          // This should fail because the bean calls File.exists()
65
bean.read("nofile.txt");
66          doFail("Was able to call IOSession.read");
67       }
68       catch(Exception JavaDoc e)
69       {
70          log.debug("IOSession.read failed as expected", e);
71       }
72    }
73    /** Test that a bean cannot access the filesystem using java.io.File
74     */

75    public void testFileWrite() throws Exception JavaDoc
76    {
77       log.debug("+++ testFileWrite()");
78       IOSession bean = getIOSession();
79       try
80       {
81          // This should fail because the bean calls File.exists()
82
bean.write("nofile.txt");
83          doFail("Was able to call IOSession.write");
84       }
85       catch(Exception JavaDoc e)
86       {
87          log.debug("IOSession.write failed as expected", e);
88       }
89       bean.remove();
90    }
91
92    public void testSocketListen() throws Exception JavaDoc
93    {
94       log.debug("+++ testSocketListen()");
95       IOSession bean = getIOSession();
96       try
97       {
98          bean.listen(0);
99          doFail("Was able to call IOSession.listen");
100       }
101       catch(Exception JavaDoc e)
102       {
103          log.debug("IOSession.listen failed as expected", e);
104       }
105    }
106
107    public void testSocketConnect() throws Exception JavaDoc
108    {
109       log.debug("+++ testSocketConnect()");
110       IOSession bean = getIOSession();
111       final ServerSocket JavaDoc tmp = new ServerSocket JavaDoc(0);
112       log.debug("Created ServerSocket: "+tmp);
113       Thread JavaDoc t = new Thread JavaDoc("Acceptor")
114       {
115          public void run()
116          {
117             try
118             {
119                Socket JavaDoc s = tmp.accept();
120                log.debug("Accepted Socket: "+s);
121                s.close();
122                log.debug("ServerSocket thread exiting");
123             }
124             catch(IOException JavaDoc e)
125             {
126             }
127          }
128       };
129       int port = tmp.getLocalPort();
130       t.start();
131       bean.connect("localhost", port);
132       tmp.close();
133       bean.remove();
134    }
135
136    public void testCreateClassLoader() throws Exception JavaDoc
137    {
138       log.debug("+++ testCreateClassLoader()");
139       IOSession bean = getIOSession();
140       try
141       {
142          bean.createClassLoader();
143          doFail("Was able to call IOSession.createClassLoader");
144       }
145       catch(Exception JavaDoc e)
146       {
147          log.debug("IOSession.createClassLoader failed as expected", e);
148       }
149    }
150    
151    public void testGetContextClassLoader() throws Exception JavaDoc
152    {
153       log.debug("+++ testGetContextClassLoader()");
154       IOSession bean = getIOSession();
155       try
156       {
157          bean.getContextClassLoader();
158          //doFail("Was able to call IOSession.getContextClassLoader");
159
log.debug("Was able to call IOSession.getContextClassLoader");
160       }
161       catch(Exception JavaDoc e)
162       {
163          log.debug("IOSession.getContextClassLoader failed as expected", e);
164       }
165       bean.remove();
166    }
167
168    public void testSetContextClassLoader() throws Exception JavaDoc
169    {
170       log.debug("+++ testSetContextClassLoader()");
171       IOSession bean = getIOSession();
172       try
173       {
174          bean.setContextClassLoader();
175          doFail("Was able to call IOSession.setContextClassLoader");
176       }
177       catch(Exception JavaDoc e)
178       {
179          log.debug("IOSession.setContextClassLoader failed as expected", e);
180       }
181       bean.remove();
182    }
183
184    public void testReflection() throws Exception JavaDoc
185    {
186       log.debug("+++ testReflection()");
187       IOSession bean = getIOSession();
188       try
189       {
190          bean.useReflection();
191          doFail("Was able to call IOSession.useReflection");
192       }
193       catch(Exception JavaDoc e)
194       {
195          log.debug("IOSession.useReflection failed as expected", e);
196       }
197       bean.remove();
198    }
199
200    public void testThreadAccess() throws Exception JavaDoc
201    {
202       log.debug("+++ testThreadAccess()");
203       IOSession bean = getIOSession();
204       try
205       {
206          /* This test will fail because the calling thread it not in the root
207             thread group so we just log a warning */

208          bean.renameThread();
209          log.warn("Was able to call IOSession.renameThread");
210       }
211       catch(Exception JavaDoc e)
212       {
213          log.debug("IOSession.renameThread failed as expected", e);
214       }
215       bean.remove();
216    }
217
218    public void testCreateThread() throws Exception JavaDoc
219    {
220       log.debug("+++ testCreateThread()");
221       IOSession bean = getIOSession();
222       try
223       {
224          /* This test will fail because the calling thread it not in the root
225             thread group so we just log a warning */

226          bean.createThread();
227          log.warn("Was able to call IOSession.createThread");
228       }
229       catch(Exception JavaDoc e)
230       {
231          log.debug("IOSession.createThread failed as expected", e);
232       }
233       bean.remove();
234    }
235
236    public void testCreateSecurityMgr() throws Exception JavaDoc
237    {
238       log.debug("+++ testCreateSecurityMgr()");
239       IOSession bean = getIOSession();
240       try
241       {
242          bean.createSecurityMgr();
243          doFail("Was able to call IOSession.createSecurityMgr");
244       }
245       catch(Exception JavaDoc e)
246       {
247          log.debug("IOSession.createSecurityMgr failed as expected", e);
248       }
249       bean.remove();
250    }
251
252    public void testChangeSystemErr() throws Exception JavaDoc
253    {
254       log.debug("+++ testChangeSystemErr()");
255       IOSession bean = getIOSession();
256       try
257       {
258          bean.changeSystemErr();
259          doFail("Was able to call IOSession.changeSystemErr");
260       }
261       catch(Exception JavaDoc e)
262       {
263          log.debug("IOSession.changeSystemErr failed as expected", e);
264       }
265       bean.remove();
266    }
267
268    public void testLoadLibrary() throws Exception JavaDoc
269    {
270       log.debug("+++ testLoadLibrary()");
271       IOSession bean = getIOSession();
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       bean.remove();
282    }
283
284    public void testSystemExit() throws Exception JavaDoc
285    {
286       log.debug("+++ testSystemExit()");
287       IOSession bean = getIOSession();
288       try
289       {
290          bean.systemExit(1);
291          doFail("Was able to call IOSession.systemExit");
292       }
293       catch(Exception JavaDoc e)
294       {
295          log.debug("IOSession.systemExit failed as expected", e);
296       }
297       bean.remove();
298    }
299
300    /**
301     * Setup the test suite.
302     */

303    public static Test suite() throws Exception JavaDoc
304    {
305       return getDeploySetup(EJBSpecUnitTestCase.class, "securitymgr-ejb.jar");
306    }
307
308    private IOSession getIOSession() throws Exception JavaDoc
309    {
310       Object JavaDoc obj = getInitialContext().lookup("secmgr.IOSessionHome");
311       IOSessionHome home = (IOSessionHome) obj;
312       log.debug("Found secmgr.IOSessionHome");
313       IOSession bean = home.create();
314       log.debug("Created IOSession");
315       return bean;
316    }
317
318    private void doFail(String JavaDoc msg)
319    {
320       log.error(msg);
321       fail(msg);
322    }
323 }
324
Popular Tags