KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > security > container > auth > config > JASPIConfigurationTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.security.container.auth.config;
23  
24 //$Id: JASPIConfigurationTestCase.java 45693 2006-06-20 05:09:52Z asaldhana $
25
import java.io.InputStream JavaDoc;
26 import java.io.InputStreamReader JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import javax.security.auth.login.AppConfigurationEntry JavaDoc;
30
31 import org.jboss.security.auth.container.config.AuthModuleEntry;
32 import org.jboss.security.auth.login.AuthenticationInfo;
33 import org.jboss.security.auth.login.BaseAuthenticationInfo;
34 import org.jboss.security.auth.login.JASPIAuthenticationInfo;
35 import org.jboss.security.auth.login.LoginConfigObjectModelFactory;
36 import org.jboss.security.auth.login.LoginModuleStackHolder;
37 import org.jboss.security.auth.spi.UsersObjectModelFactory;
38 import org.jboss.security.config.ApplicationPolicy;
39 import org.jboss.security.config.PolicyConfig;
40 import org.jboss.test.JBossTestCase;
41 import org.jboss.xb.binding.Unmarshaller;
42 import org.jboss.xb.binding.UnmarshallerFactory;
43
44 /**
45  * Tests the new login-config.xml based on JSR-196
46  * @author <a HREF="mailto:Anil.Saldhana@jboss.org">Anil Saldhana</a>
47  * @since Dec 19, 2005
48  */

49 public class JASPIConfigurationTestCase extends JBossTestCase
50 {
51    public JASPIConfigurationTestCase(String JavaDoc name)
52    {
53       super(name);
54    }
55    
56    public void testJaasAuthenticationInfo() throws Exception JavaDoc
57    {
58       PolicyConfig config = getPolicyConfig("security/jaspi/login-config-jaspi.xml",
59             new LoginConfigObjectModelFactory());
60       assertNotNull("Returned PolicyConfig is != null ?", config);
61       ApplicationPolicy aPolicy = config.get("JBossWS");
62       BaseAuthenticationInfo infoBase = aPolicy.getAuthenticationInfo();
63       assertTrue("infoBase==AuthenticationInfo", infoBase instanceof AuthenticationInfo);
64       AuthenticationInfo info = (AuthenticationInfo)infoBase;
65       assertTrue("JBossWS != null", info != null);
66       AppConfigurationEntry JavaDoc[] entries = info.getAppConfigurationEntry();
67       assertTrue("entries.length == 1", entries.length == 1);
68       AppConfigurationEntry JavaDoc ace = entries[0];
69       assertTrue("org.jboss.security.auth.spi.UsersRolesLoginModule",
70          ace.getLoginModuleName().equals("org.jboss.security.auth.spi.UsersRolesLoginModule"));
71       Map JavaDoc options = ace.getOptions();
72       assertTrue("Options.size == 3", options.size() == 3);
73       String JavaDoc guest = (String JavaDoc) options.get("unauthenticatedIdentity");
74       assertTrue("anonymous", guest.equals("anonymous"));
75       String JavaDoc users = (String JavaDoc) options.get("usersProperties");
76       assertTrue("anonymous", users.equals("props/jbossws-users.properties"));
77       String JavaDoc roles = (String JavaDoc) options.get("rolesProperties");
78       assertTrue("anonymous", roles.equals("props/jbossws-roles.properties"));
79    }
80    
81    public void testJaspiAuthenticationInfo() throws Exception JavaDoc
82    {
83       LoginConfigObjectModelFactory lcomf = new LoginConfigObjectModelFactory();
84       PolicyConfig config = getPolicyConfig("security/jaspi/login-config-jaspi.xml",lcomf);
85       assertNotNull("Returned PolicyConfig is != null ?", config);
86       ApplicationPolicy aPolicy = config.get("jaspi");
87       BaseAuthenticationInfo infoBase = aPolicy.getAuthenticationInfo();
88       assertTrue("infoBase==AuthenticationJaspiInfo", infoBase instanceof JASPIAuthenticationInfo);
89       JASPIAuthenticationInfo info = (JASPIAuthenticationInfo)infoBase;
90       assertTrue("jaspi != null", info != null);
91       AuthModuleEntry[] authEntry = info.getAuthModuleEntry();
92       //Get the first AuthModule
93
AuthModuleEntry aEntry1 = authEntry[0];
94       validateAuthModule1(aEntry1);
95       //Get the second AuthModule
96
AuthModuleEntry aEntry2 = authEntry[1];
97       validateAuthModule2(aEntry2);
98    }
99    
100    protected void validateAuthModule1(AuthModuleEntry aEntry1)
101    {
102       assertEquals("auth.module1.class.name", aEntry1.getAuthModuleName());
103       Map JavaDoc aEntry1Options = aEntry1.getOptions();
104       assertNotNull("Options in the first AuthModule != null", aEntry1Options);
105       assertTrue( "Length of options == 3", aEntry1Options.size() == 3);
106       String JavaDoc usersProperties = (String JavaDoc) aEntry1Options.get("usersProperties");
107       assertNotNull("options.usersProperties exists", usersProperties);
108       assertTrue("options.usersProperties == props/jbossws-users.properties",
109             usersProperties.equals("props/jbossws-users.properties"));
110       String JavaDoc rolesProperties = (String JavaDoc) aEntry1Options.get("rolesProperties");
111       assertNotNull("options.rolesProperties exists", rolesProperties);
112       assertTrue("options.rolesProperties == props/jbossws-roles.properties",
113             rolesProperties.equals("props/jbossws-roles.properties"));
114    }
115    
116    protected void validateAuthModule2(AuthModuleEntry aEntry2)
117    {
118       assertEquals("auth.module2.class.name", aEntry2.getAuthModuleName());
119       LoginModuleStackHolder lmsh = aEntry2.getLoginModuleStackHolder();
120       assertNotNull("LoginModuleStackHolder != null", lmsh);
121       assertEquals("lm-stack", lmsh.getName());
122    }
123    
124    protected PolicyConfig getPolicyConfig(String JavaDoc config,
125          LoginConfigObjectModelFactory lcomf ) throws Exception JavaDoc
126    {
127       UsersObjectModelFactory uomf = new UsersObjectModelFactory();
128       InputStream JavaDoc is = Thread.currentThread().getContextClassLoader().getResourceAsStream(config);
129       InputStreamReader JavaDoc xmlReader = new InputStreamReader JavaDoc(is);
130       Unmarshaller unmarshaller = UnmarshallerFactory.newInstance().newUnmarshaller();
131       unmarshaller.setNamespaceAware(true);
132       unmarshaller.setValidation(true);
133       unmarshaller.mapFactoryToNamespace(uomf, "http://www.jboss.org/j2ee/schemas/XMLLoginModule");
134       return (PolicyConfig) unmarshaller.unmarshal(xmlReader, lcomf, null);
135    }
136 }
137
Popular Tags