1 22 package org.jboss.test.naming.test; 23 24 import java.lang.reflect.UndeclaredThrowableException ; 25 import java.security.Principal ; 26 import java.util.Properties ; 27 28 import javax.naming.Context ; 29 import javax.naming.InitialContext ; 30 import javax.naming.NamingEnumeration ; 31 import javax.naming.NamingException ; 32 import javax.rmi.PortableRemoteObject ; 33 import javax.security.auth.login.LoginContext ; 34 35 import junit.framework.Test; 36 import junit.framework.TestSuite; 37 38 import org.jboss.security.SecurityAssociation; 39 import org.jboss.test.JBossTestCase; 40 import org.jboss.test.naming.interfaces.TestENC; 41 import org.jboss.test.naming.interfaces.TestENCHome; 42 import org.jboss.test.util.AppCallbackHandler; 43 44 50 public class SecurityUnitTestCase extends JBossTestCase 51 { 52 public static Test suite() throws Exception 53 { 54 TestSuite suite = new TestSuite(); 57 suite.addTest(new SecurityUnitTestCase("testSecureHttpInvokerFailure")); 58 suite.addTest(new SecurityUnitTestCase("testSecureHttpInvoker")); 59 suite.addTest(new SecurityUnitTestCase("testHttpReadonlyLookup")); 60 suite.addTest(new SecurityUnitTestCase("testHttpReadonlyContextLookup")); 61 suite.addTest(new SecurityUnitTestCase("testLoginInitialContext")); 62 suite.addTest(new SecurityUnitTestCase("testSecureEJBViaLoginInitialContextFactory")); 63 suite.addTest(new SecurityUnitTestCase("testSecureEJBViaJndiLoginInitialContextFactory")); 64 65 return suite; 66 } 67 68 73 public SecurityUnitTestCase(String name) 74 { 75 super(name); 76 } 77 78 82 public void testSecureHttpInvokerFailure() throws Exception 83 { 84 getLog().debug("+++ testSecureHttpInvokerFailure"); 85 Properties env = new Properties (); 86 env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.HttpNamingContextFactory"); 87 88 env.setProperty(Context.PROVIDER_URL, "http://localhost:8080/invoker/restricted/JNDIFactory"); 90 getLog().debug("Creating InitialContext with env="+env); 91 92 try 94 { 95 getLog().debug("Testing without valid login"); 96 InitialContext ctx1 = new InitialContext (env); 97 getLog().debug("Created InitialContext"); 98 Object obj1 = ctx1.lookup("jmx"); 99 getLog().debug("lookup(jmx) : "+obj1); 100 fail("Should not have been able to lookup(jmx)"); 101 } 102 catch(Exception e) 103 { 104 getLog().debug("Lookup failed as expected", e); 105 } 106 107 } 108 109 111 public void testSecureHttpInvoker() throws Exception 112 { 113 getLog().debug("+++ testSecureHttpInvoker"); 114 Properties env = new Properties (); 115 env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.HttpNamingContextFactory"); 116 117 String authConf = super.getResourceURL("security/auth.conf"); 119 getLog().debug("Using auth.conf: "+authConf); 120 System.setProperty("java.security.auth.login.config", authConf); 121 AppCallbackHandler handler = new AppCallbackHandler("admin", "admin".toCharArray()); 122 LoginContext lc = new LoginContext ("testSecureHttpInvoker", handler); 123 lc.login(); 124 125 env.setProperty(Context.PROVIDER_URL, "http://localhost:8080/invoker/restricted/JNDIFactory"); 127 getLog().debug("Creating InitialContext with env="+env); 128 InitialContext ctx = new InitialContext (env); 129 getLog().debug("Created InitialContext"); 130 Object obj = ctx.lookup("jmx"); 131 getLog().debug("lookup(jmx) : "+obj); 132 Context jmxCtx = (Context ) obj; 133 NamingEnumeration list = jmxCtx.list(""); 134 while( list.hasMore() ) 135 { 136 Object entry = list.next(); 137 getLog().debug(" + "+entry); 138 } 139 ctx.close(); 140 lc.logout(); 141 142 Principal p = SecurityAssociation.getPrincipal(); 143 assertTrue("SecurityAssociation.getPrincipal is null", p == null); 144 145 151 } 152 153 157 public void testHttpReadonlyLookup() throws Exception 158 { 159 getLog().debug("+++ testHttpReadonlyLookup"); 160 163 InitialContext bootCtx = new InitialContext (); 164 try 165 { 166 bootCtx.unbind("readonly"); 167 } 168 catch(NamingException ignore) 169 { 170 } 171 Context readonly = bootCtx.createSubcontext("readonly"); 172 readonly.bind("data", "somedata"); 173 174 Properties env = new Properties (); 175 env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.HttpNamingContextFactory"); 176 env.setProperty(Context.PROVIDER_URL, "http://localhost:8080/invoker/ReadOnlyJNDIFactory"); 177 getLog().debug("Creating InitialContext with env="+env); 178 InitialContext ctx = new InitialContext (env); 179 Object data = ctx.lookup("readonly/data"); 180 getLog().debug("lookup(readonly/data) : "+data); 181 try 182 { 183 ctx.bind("readonly/mydata", "otherdata"); 185 fail("Was able to bind into the readonly context"); 186 } 187 catch(UndeclaredThrowableException e) 188 { 189 getLog().debug("Invalid exception", e); 190 fail("UndeclaredThrowableException thrown"); 191 } 192 catch(Exception e) 193 { 194 getLog().debug("Bind failed as expected", e); 195 } 196 197 try 198 { 199 ctx.lookup("invokers"); 201 fail("Was able to lookup(invokers)"); 202 } 203 catch(UndeclaredThrowableException e) 204 { 205 getLog().debug("Invalid exception", e); 206 fail("UndeclaredThrowableException thrown"); 207 } 208 catch(Exception e) 209 { 210 getLog().debug("lookup(invokers) failed as expected", e); 211 } 212 } 213 214 218 public void testHttpReadonlyContextLookup() throws Exception 219 { 220 getLog().debug("+++ testHttpReadonlyContextLookup"); 221 224 deploy("naming-readonly.sar"); 225 226 229 Properties env = new Properties (); 230 env.setProperty(Context.INITIAL_CONTEXT_FACTORY, 231 "org.jboss.test.naming.test.BootstrapNamingContextFactory"); 232 env.setProperty(Context.PROVIDER_URL, "jnp://localhost:1099"); 233 env.setProperty("bootstrap-binding", "naming/Naming"); 234 getLog().debug("Creating bootstrap InitialContext with env="+env); 235 InitialContext bootCtx = new InitialContext (env); 236 try 237 { 238 bootCtx.unbind("readonly"); 239 } 240 catch(NamingException ignore) 241 { 242 } 243 getLog().debug("Creating readonly context"); 244 bootCtx.createSubcontext("readonly"); 245 bootCtx.bind("readonly/data", "somedata"); 246 247 env.setProperty("bootstrap-binding", "naming/ReadOnlyNaming"); 249 getLog().debug("Creating InitialContext with env="+env); 250 InitialContext ctx = new InitialContext (env); 251 Object data = ctx.lookup("readonly/data"); 252 getLog().debug("lookup(readonly/data) : "+data); 253 Object robinding = ctx.lookup("readonly"); 255 getLog().debug("Looked up readonly: "+robinding); 256 Context roctx = (Context ) robinding; 257 data = roctx.lookup("data"); 258 getLog().debug("Looked up data: "+data); 259 assertTrue("lookup(data) == somedata: "+data, "somedata".equals(data)); 260 try 261 { 262 roctx.bind("mydata", "otherdata"); 264 fail("Was able to bind into the readonly context"); 265 } 266 catch(UndeclaredThrowableException e) 267 { 268 getLog().debug("Invalid exception", e); 269 fail("UndeclaredThrowableException thrown"); 270 } 271 catch(NamingException e) 272 { 273 getLog().debug("Bind failed as expected", e); 274 } 275 276 try 277 { 278 ctx.lookup("invokers"); 280 fail("Was able to lookup(invokers)"); 281 } 282 catch(UndeclaredThrowableException e) 283 { 284 getLog().debug("Invalid exception", e); 285 fail("UndeclaredThrowableException thrown"); 286 } 287 catch(Exception e) 288 { 289 getLog().debug("lookup(invokers) failed as expected", e); 290 } 291 undeploy("naming-readonly.sar"); 292 } 293 294 297 public void testLoginInitialContext() throws Exception 298 { 299 getLog().debug("+++ testLoginInitialContext"); 300 Properties env = new Properties (); 301 env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.security.jndi.LoginInitialContextFactory"); 303 env.setProperty(Context.PROVIDER_URL, "jnp://localhost:1099/"); 304 env.setProperty(Context.SECURITY_CREDENTIALS, "theduke"); 305 env.setProperty(Context.SECURITY_PRINCIPAL, "jduke"); 306 env.setProperty(Context.SECURITY_PROTOCOL, "testLoginInitialContext"); 307 308 String authConf = super.getResourceURL("security/auth.conf"); 310 System.setProperty("java.security.auth.login.config", authConf); 311 312 getLog().debug("Creating InitialContext with env="+env); 313 InitialContext ctx = new InitialContext (env); 314 getLog().debug("Created InitialContext"); 315 Object obj = ctx.lookup("jmx"); 316 getLog().debug("lookup(jmx) : "+obj); 317 Context jmxCtx = (Context ) obj; 318 NamingEnumeration list = jmxCtx.list(""); 319 while( list.hasMore() ) 320 { 321 Object entry = list.next(); 322 getLog().debug(" + "+entry); 323 } 324 ctx.close(); 325 326 env.setProperty(Context.SECURITY_CREDENTIALS, "badpass"); 328 try 329 { 330 getLog().debug("Creating InitialContext with env="+env); 331 ctx = new InitialContext (env); 332 fail("Was able to create InitialContext with badpass"); 333 } 334 catch(NamingException e) 335 { 336 getLog().debug("InitialContext failed as expected with exception", e); 337 } 338 } 339 340 344 public void testSecureEJBViaLoginInitialContextFactory() throws Exception 345 { 346 getLog().debug("+++ testSecureEJBViaLoginInitialContextFactory"); 347 Properties env = new Properties (); 348 env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.security.jndi.LoginInitialContextFactory"); 350 env.setProperty(Context.PROVIDER_URL, "jnp://localhost:1099/"); 351 env.setProperty(Context.SECURITY_CREDENTIALS, "theduke"); 352 env.setProperty(Context.SECURITY_PRINCIPAL, "jduke"); 353 env.setProperty(Context.SECURITY_PROTOCOL, "testLoginInitialContext"); 354 355 String authConf = super.getResourceURL("security/auth.conf"); 357 log.info("auth.conf: "+authConf); 358 System.setProperty("java.security.auth.login.config", authConf); 359 360 getLog().debug("Creating InitialContext with env="+env); 361 InitialContext ctx = new InitialContext (env); 362 getLog().debug("Created InitialContext, ctx="+ctx); 363 super.deploy("naming.jar"); 364 Object obj = getInitialContext().lookup("ENCTests/ejbs/SecuredENCBean"); 365 obj = PortableRemoteObject.narrow(obj, TestENCHome.class); 366 TestENCHome home = (TestENCHome)obj; 367 368 try 369 { 370 TestENC bean = home.create(); 371 getLog().debug("Created SecuredENCBean"); 372 bean.accessENC(); 373 bean.remove(); 374 System.setProperty("java.security.auth.login.config", "invalid"); 375 } 376 finally 377 { 378 super.undeploy("naming.jar"); 379 } 380 } 381 382 386 public void testSecureEJBViaJndiLoginInitialContextFactory() throws Exception 387 { 388 getLog().debug("+++ testSecureEJBViaJndiLoginInitialContextFactory"); 389 Properties env = new Properties (); 390 env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.security.jndi.JndiLoginInitialContextFactory"); 392 env.setProperty(Context.PROVIDER_URL, "jnp://localhost:1099/"); 393 env.setProperty(Context.SECURITY_CREDENTIALS, "theduke"); 394 env.setProperty(Context.SECURITY_PRINCIPAL, "jduke"); 395 396 getLog().debug("Creating InitialContext with env="+env); 397 InitialContext ctx = new InitialContext (env); 398 getLog().debug("Created InitialContext, ctx="+ctx); 399 super.deploy("naming.jar"); 400 Object obj = getInitialContext().lookup("ENCTests/ejbs/SecuredENCBean"); 401 obj = PortableRemoteObject.narrow(obj, TestENCHome.class); 402 TestENCHome home = (TestENCHome)obj; 403 getLog().debug("Found SecuredENCBean"); 404 405 try 406 { 407 TestENC bean = home.create(); 408 getLog().debug("Created SecuredENCBean"); 409 bean.accessENC(); 410 bean.remove(); 411 } 412 finally 413 { 414 super.undeploy("naming.jar"); 415 } 416 } 417 } 418 | Popular Tags |