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 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 45 public class PolicyUnitTestCase extends JBossTestCase 46 { 47 48 public PolicyUnitTestCase(String name) 49 { 50 super(name); 51 } 52 53 55 public void testSecurityAssociation() throws Exception 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 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 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 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 e) 97 { 98 log.debug("Bad.setCredential failed as expected", e); 99 } 100 } 101 102 104 public void testFileIO() throws Exception 105 { 106 log.debug("+++ testFileIO()"); 107 IOStatelessSessionBean bean = getIOSession(); 108 109 try 110 { 111 bean.read("nofile.txt"); 113 doFail("Was able to call IOSession.read"); 114 } 115 catch(Exception e) 116 { 117 log.debug("IOSession.read failed as expected", e); 118 } 119 120 try 121 { 122 bean.write("nofile.txt"); 124 doFail("Was able to call IOSession.write"); 125 } 126 catch(Exception e) 127 { 128 log.debug("IOSession.write failed as expected", e); 129 } 130 } 131 132 public void testSockets() throws Exception 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 e) 142 { 143 log.debug("IOSession.listen failed as expected", e); 144 } 145 146 final ServerSocket tmp = new ServerSocket (0); 147 log.debug("Created ServerSocket: "+tmp); 148 final Logger theLog = log; 149 Thread t = new Thread ("Acceptor") 150 { 151 public void run() 152 { 153 try 154 { 155 Socket s = tmp.accept(); 156 theLog.debug("Accepted Socket: "+s); 157 s.close(); 158 theLog.debug("ServerSocket thread exiting"); 159 } 160 catch(IOException 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 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 e) 181 { 182 log.debug("IOSession.createClassLoader failed as expected", e); 183 } 184 185 try 186 { 187 bean.getContextClassLoader(); 188 log.debug("Was able to call IOSession.getContextClassLoader"); 190 } 191 catch(Exception 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 e) 202 { 203 log.debug("IOSession.setContextClassLoader failed as expected", e); 204 } 205 } 206 207 public void testReflection() throws Exception 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 e) 217 { 218 log.debug("IOSession.useReflection failed as expected", e); 219 } 220 } 221 222 public void testThreadAccess() throws Exception 223 { 224 log.debug("+++ testThreadAccess()"); 225 IOStatelessSessionBean bean = getIOSession(); 226 try 227 { 228 bean.renameThread(); 230 doFail("Was able to call IOSession.renameThread"); 231 } 232 catch(Exception e) 233 { 234 log.debug("IOSession.renameThread failed as expected", e); 235 } 236 } 237 238 public void testSystemAccess() throws Exception 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 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 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 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 e) 278 { 279 log.debug("IOSession.loadLibrary failed as expected", e); 280 } 281 282 296 } 297 298 300 public void testServerFound() throws Exception 301 { 302 log.debug("+++ testServerFound()"); 303 } 304 305 307 public static Test suite() throws Exception 308 { 309 TestSuite suite = new TestSuite(PolicyUnitTestCase.class); 310 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 msg) 325 { 326 log.error(msg); 327 fail(msg); 328 } 329 private void doFail(String msg, Throwable t) 330 { 331 log.error(msg, t); 332 fail(msg); 333 } 334 } 335 | Popular Tags |