1 22 package org.jboss.test.securitymgr.test; 23 24 import java.io.IOException ; 25 import java.net.ServerSocket ; 26 import java.net.Socket ; 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 46 public class EJBSpecUnitTestCase 47 extends JBossTestCase 48 { 49 50 public EJBSpecUnitTestCase(String name) 51 { 52 super(name); 53 } 54 55 57 public void testFileExists() throws Exception 58 { 59 log.debug("+++ testFileExists()"); 60 IOSession bean = getIOSession(); 61 62 try 63 { 64 bean.read("nofile.txt"); 66 doFail("Was able to call IOSession.read"); 67 } 68 catch(Exception e) 69 { 70 log.debug("IOSession.read failed as expected", e); 71 } 72 } 73 75 public void testFileWrite() throws Exception 76 { 77 log.debug("+++ testFileWrite()"); 78 IOSession bean = getIOSession(); 79 try 80 { 81 bean.write("nofile.txt"); 83 doFail("Was able to call IOSession.write"); 84 } 85 catch(Exception e) 86 { 87 log.debug("IOSession.write failed as expected", e); 88 } 89 bean.remove(); 90 } 91 92 public void testSocketListen() throws Exception 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 e) 102 { 103 log.debug("IOSession.listen failed as expected", e); 104 } 105 } 106 107 public void testSocketConnect() throws Exception 108 { 109 log.debug("+++ testSocketConnect()"); 110 IOSession bean = getIOSession(); 111 final ServerSocket tmp = new ServerSocket (0); 112 log.debug("Created ServerSocket: "+tmp); 113 Thread t = new Thread ("Acceptor") 114 { 115 public void run() 116 { 117 try 118 { 119 Socket s = tmp.accept(); 120 log.debug("Accepted Socket: "+s); 121 s.close(); 122 log.debug("ServerSocket thread exiting"); 123 } 124 catch(IOException 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 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 e) 146 { 147 log.debug("IOSession.createClassLoader failed as expected", e); 148 } 149 } 150 151 public void testGetContextClassLoader() throws Exception 152 { 153 log.debug("+++ testGetContextClassLoader()"); 154 IOSession bean = getIOSession(); 155 try 156 { 157 bean.getContextClassLoader(); 158 log.debug("Was able to call IOSession.getContextClassLoader"); 160 } 161 catch(Exception e) 162 { 163 log.debug("IOSession.getContextClassLoader failed as expected", e); 164 } 165 bean.remove(); 166 } 167 168 public void testSetContextClassLoader() throws Exception 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 e) 178 { 179 log.debug("IOSession.setContextClassLoader failed as expected", e); 180 } 181 bean.remove(); 182 } 183 184 public void testReflection() throws Exception 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 e) 194 { 195 log.debug("IOSession.useReflection failed as expected", e); 196 } 197 bean.remove(); 198 } 199 200 public void testThreadAccess() throws Exception 201 { 202 log.debug("+++ testThreadAccess()"); 203 IOSession bean = getIOSession(); 204 try 205 { 206 208 bean.renameThread(); 209 log.warn("Was able to call IOSession.renameThread"); 210 } 211 catch(Exception e) 212 { 213 log.debug("IOSession.renameThread failed as expected", e); 214 } 215 bean.remove(); 216 } 217 218 public void testCreateThread() throws Exception 219 { 220 log.debug("+++ testCreateThread()"); 221 IOSession bean = getIOSession(); 222 try 223 { 224 226 bean.createThread(); 227 log.warn("Was able to call IOSession.createThread"); 228 } 229 catch(Exception e) 230 { 231 log.debug("IOSession.createThread failed as expected", e); 232 } 233 bean.remove(); 234 } 235 236 public void testCreateSecurityMgr() throws Exception 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 e) 246 { 247 log.debug("IOSession.createSecurityMgr failed as expected", e); 248 } 249 bean.remove(); 250 } 251 252 public void testChangeSystemErr() throws Exception 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 e) 262 { 263 log.debug("IOSession.changeSystemErr failed as expected", e); 264 } 265 bean.remove(); 266 } 267 268 public void testLoadLibrary() throws Exception 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 e) 278 { 279 log.debug("IOSession.loadLibrary failed as expected", e); 280 } 281 bean.remove(); 282 } 283 284 public void testSystemExit() throws Exception 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 e) 294 { 295 log.debug("IOSession.systemExit failed as expected", e); 296 } 297 bean.remove(); 298 } 299 300 303 public static Test suite() throws Exception 304 { 305 return getDeploySetup(EJBSpecUnitTestCase.class, "securitymgr-ejb.jar"); 306 } 307 308 private IOSession getIOSession() throws Exception 309 { 310 Object 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 msg) 319 { 320 log.error(msg); 321 fail(msg); 322 } 323 } 324 | Popular Tags |