1 22 package org.jboss.test.xml; 23 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.net.URL ; 27 import java.util.Arrays ; 28 import java.util.HashSet ; 29 import java.util.Map ; 30 import javax.security.auth.login.AppConfigurationEntry ; 31 32 import junit.framework.TestCase; 33 import org.jboss.security.auth.spi.Users; 34 import org.jboss.security.auth.container.config.AuthModuleEntry; 35 import org.jboss.security.auth.login.JASPIAuthenticationInfo; 36 import org.jboss.security.auth.login.LoginModuleStackHolder; 37 import org.jboss.security.config.PolicyConfig; 38 import org.jboss.security.auth.login.AuthenticationInfo; 39 import org.jboss.security.config.ApplicationPolicy; 40 import org.jboss.xb.binding.JBossXBException; 41 import org.jboss.xb.binding.Unmarshaller; 42 import org.jboss.xb.binding.UnmarshallerFactory; 43 import org.jboss.xb.binding.sunday.unmarshalling.SchemaBindingResolver; 44 import org.jboss.xb.binding.sunday.unmarshalling.SchemaBinding; 45 import org.jboss.xb.binding.sunday.unmarshalling.XsdBinder; 46 import org.w3c.dom.ls.LSInput ; 47 import org.xml.sax.EntityResolver ; 48 import org.xml.sax.InputSource ; 49 50 52 60 public class DynamicLoginConfigUnitTestCase 61 extends TestCase 62 { 63 69 public void testConfig() throws Exception 70 { 71 System.setProperty("java.protocol.handler.pkgs", "org.jboss.net.protocol"); 73 InputStream is = getResource("xml/loginconfig/config.xml"); 74 75 Object root = getParsedRoot(is); 76 77 PolicyConfig config = (PolicyConfig) root; 78 is.close(); 79 80 ApplicationPolicy aPolicy = (ApplicationPolicy)config.get("conf1"); 82 AuthenticationInfo info = (AuthenticationInfo)aPolicy.getAuthenticationInfo(); 83 validateJaasBindings(info); 84 } 85 86 92 public void testJASPIConfig() throws Exception 93 { 94 System.setProperty("java.protocol.handler.pkgs", "org.jboss.net.protocol"); 96 InputStream is = getResource("xml/loginconfig/jaspi-config.xml"); 97 98 Object root = getParsedRoot(is); 99 100 PolicyConfig config = (PolicyConfig) root; 101 is.close(); 102 103 ApplicationPolicy aPolicy = (ApplicationPolicy)config.get("conf1"); 105 AuthenticationInfo info = (AuthenticationInfo)aPolicy.getAuthenticationInfo(); 106 validateJaasBindings(info); 107 108 aPolicy = (ApplicationPolicy)config.get("conf-jaspi"); 110 JASPIAuthenticationInfo jaspiInfo = (JASPIAuthenticationInfo)aPolicy.getAuthenticationInfo(); 111 validateJASPIBindings( jaspiInfo ); 112 } 113 114 private Object getParsedRoot(InputStream is) throws JBossXBException 116 { 117 124 final URL url = Thread.currentThread().getContextClassLoader().getResource("xml/loginconfig/"); 125 Unmarshaller unmarshaller = UnmarshallerFactory.newInstance().newUnmarshaller(); 126 unmarshaller.setEntityResolver(new EntityResolver (){ 127 public InputSource resolveEntity(String publicId, String systemId) 128 { 129 return null; 130 } 131 }); 132 Object root = unmarshaller.unmarshal(is, new SchemaBindingResolver(){ 133 public String getBaseURI() 134 { 135 throw new UnsupportedOperationException ("getBaseURI is not implemented."); 136 } 137 138 public void setBaseURI(String baseURI) 139 { 140 throw new UnsupportedOperationException ("setBaseURI is not implemented."); 141 } 142 143 public SchemaBinding resolve(String nsUri, String baseURI, String schemaLocation) 144 { 145 return XsdBinder.bind(url.toExternalForm() + schemaLocation, this); 146 } 147 148 public LSInput resolveAsLSInput(String nsUri, String baseUri, String schemaLocation) 149 { 150 throw new UnsupportedOperationException ("resolveAsLSInput is not implemented."); 151 } 152 }); 153 return root; 154 } 155 156 157 private void validateJaasBindings( AuthenticationInfo info ) 158 { 159 assertNotNull("conf1", info); 160 AppConfigurationEntry [] entry = info.getAppConfigurationEntry(); 161 assertTrue("entry.length == 1", entry.length == 1); 162 assertTrue("entry[0].getLoginModuleName() == XMLLoginModule", 163 entry[0].getLoginModuleName().equals("org.jboss.security.auth.spi.XMLLoginModule")); 164 Map options = entry[0].getOptions(); 165 assertTrue("There are two options", options.size() == 2); 166 String unauthenticatedIdentity = (String ) options.get("unauthenticatedIdentity"); 167 assertNotNull("options.unauthenticatedIdentity exists", unauthenticatedIdentity); 168 assertTrue("options.unauthenticatedIdentity == guest", 169 unauthenticatedIdentity.equals("guest")); 170 171 Users users = (Users) options.get("userInfo"); 172 assertNotNull("options.userInfo is a Users", users); 173 assertTrue("Users.size("+users.size()+") is 6", users.size() == 6); 174 Users.User jduke = users.getUser("jduke"); 175 assertNotNull("jduke is a user", jduke); 176 assertTrue("jduke.password == theduke", jduke.getPassword().equals("theduke")); 177 String [] roleNames = jduke.getRoleNames("Roles"); 178 HashSet roles = new HashSet (Arrays.asList(roleNames)); 179 assertTrue("jduke has roles", roles.size() == 3); 180 assertTrue("Role1 is a role", roles.contains("Role1")); 181 assertTrue("Role2 is a role", roles.contains("Role2")); 182 assertTrue("Echo is a role", roles.contains("Echo")); 183 } 184 185 private void validateJASPIBindings( JASPIAuthenticationInfo jaspiInfo ) 186 { 187 assertNotNull("conf-jaspi", jaspiInfo); 188 AuthModuleEntry[] authEntry = jaspiInfo.getAuthModuleEntry(); 189 assertTrue("entry.length == 2", authEntry.length == 2); 190 AuthModuleEntry aEntry1 = authEntry[0]; 192 validateAuthModule1(aEntry1); 193 AuthModuleEntry aEntry2 = authEntry[1]; 195 validateAuthModule2(aEntry2); 196 } 197 198 private void validateAuthModule1(AuthModuleEntry aEntry1) 199 { 200 assertEquals("auth.module1.class.name", aEntry1.getAuthModuleName()); 201 Map aEntry1Options = aEntry1.getOptions(); 202 assertNotNull("Options in the first AuthModule != null", aEntry1Options); 203 assertTrue( "Length of options == 3", aEntry1Options.size() == 3); 204 String usersProperties = (String ) aEntry1Options.get("usersProperties"); 205 assertNotNull("options.usersProperties exists", usersProperties); 206 assertTrue("options.usersProperties == props/jbossws-users.properties", 207 usersProperties.equals("props/jbossws-users.properties")); 208 String rolesProperties = (String ) aEntry1Options.get("rolesProperties"); 209 assertNotNull("options.rolesProperties exists", rolesProperties); 210 assertTrue("options.rolesProperties == props/jbossws-roles.properties", 211 rolesProperties.equals("props/jbossws-roles.properties")); 212 } 213 214 private void validateAuthModule2(AuthModuleEntry aEntry2) 215 { 216 assertEquals("auth.module2.class.name", aEntry2.getAuthModuleName()); 217 LoginModuleStackHolder lmsh = aEntry2.getLoginModuleStackHolder(); 218 assertNotNull("LoginModuleStackHolder != null", lmsh); 219 assertEquals("lm-stack", lmsh.getName()); 220 } 221 222 private InputStream getResource(String path) 223 throws IOException 224 { 225 URL url = Thread.currentThread().getContextClassLoader().getResource(path); 226 if(url == null) 227 { 228 fail("URL not found: " + path); 229 } 230 return url.openStream(); 231 } 232 } 233 | Popular Tags |