1 22 package org.jboss.test.security.managers; 23 24 import java.io.IOException ; 25 import java.lang.reflect.UndeclaredThrowableException ; 26 import java.security.Principal ; 27 28 import javax.security.auth.Subject ; 29 import javax.security.auth.login.Configuration ; 30 import javax.security.auth.message.config.ServerAuthContext; 31 32 import org.jboss.security.AuthenticationManager; 33 import org.jboss.security.SecurityAssociation; 34 import org.jboss.security.SimplePrincipal; 35 import org.jboss.security.auth.callback.SecurityAssociationHandler; 36 import org.jboss.security.auth.login.XMLLoginConfigImpl; 37 import org.jboss.security.plugins.JASPISecurityManager; 38 import org.jboss.test.JBossTestCase; 39 import org.jboss.test.util.AppCallbackHandler; 40 41 43 49 public class JASPISecurityManagerUnitTestCase extends JBossTestCase 50 { 51 protected Principal id = null; 52 53 public JASPISecurityManagerUnitTestCase(String name) 54 { 55 super(name); 56 } 57 58 public void testConstruction() throws Exception 59 { 60 Configuration cfg = getConfiguration(); 61 Configuration.setConfiguration(cfg); 62 JASPISecurityManager jsm = new JASPISecurityManager(); 63 checkJASPISecurityManagerObject(jsm); 64 65 jsm = new JASPISecurityManager("jaspi", new SecurityAssociationHandler()); 66 checkJASPISecurityManagerObject(jsm); 67 try 69 { 70 jsm = new JASPISecurityManager("jaspi", new AppCallbackHandler("test","pwd".toCharArray())); 71 fail("JASPISecurityManager ctr should throw " + 72 "an exception as handler has no setSecurityInfo method"); 73 }catch(UndeclaredThrowableException ute) 74 { 75 Throwable t = ute.getUndeclaredThrowable(); 76 if(t instanceof NoSuchMethodException == false) 77 { 78 fail("Test failed while expecting NoMethodException"); 79 } 80 } 81 } 82 83 public void testSuccessfulValidation() throws Exception 84 { 85 Configuration cfg = getConfiguration(); 86 Configuration.setConfiguration(cfg); 87 JASPISecurityManager jsm = new JASPISecurityManager("java:/jaas/other", 88 new SecurityAssociationHandler()); 89 Subject subject = new Subject (); 90 Object cred = "secret"; 91 id = new SimplePrincipal("admin"); 92 SecurityAssociation.setPrincipal(id); 93 SecurityAssociation.setCredential(cred); 94 boolean isValid = jsm.isValid( id,cred,subject); 95 assertTrue("Successful validation?", isValid); 96 SecurityAssociation.pushSubjectContext(subject,id, cred); 97 } 98 99 public void testUnSuccessfulValidation() throws Exception 100 { 101 Configuration cfg = getConfiguration(); 102 Configuration.setConfiguration(cfg); 103 JASPISecurityManager jsm = new JASPISecurityManager("java:/jaas/other", 104 new SecurityAssociationHandler()); 105 boolean isValid = jsm.isValid(new SimplePrincipal("admin"),"bad"); 106 assertFalse("Should be invalid?", isValid); 107 } 108 109 private void checkJASPISecurityManagerObject(JASPISecurityManager jsm) 111 { 112 assertNotNull("JASPISecurityManager != null?", jsm); 113 assertTrue("is AuthenticationManager?", jsm instanceof AuthenticationManager); 114 assertTrue("is ServerAuthContext?", jsm instanceof ServerAuthContext); 115 } 116 117 private Configuration getConfiguration() throws IOException 118 { 119 XMLLoginConfigImpl config = new XMLLoginConfigImpl(); 121 config.setConfigResource("security/login-config.xml"); 122 config.loadConfig(); 123 return config; 124 } 125 } 126 | Popular Tags |