KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > security > managers > JASPISecurityManagerUnitTestCase


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.managers;
23
24 import java.io.IOException JavaDoc;
25 import java.lang.reflect.UndeclaredThrowableException JavaDoc;
26 import java.security.Principal JavaDoc;
27  
28 import javax.security.auth.Subject JavaDoc;
29 import javax.security.auth.login.Configuration JavaDoc;
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 //$Id: JASPISecurityManagerUnitTestCase.java 45187 2006-05-23 20:32:39Z asaldhana $
42

43 /**
44  * Unit TestCase for the Default JASPISecurityManager implementation
45  * @author <a HREF="mailto:Anil.Saldhana@jboss.org">Anil Saldhana</a>
46  * @since Dec 30, 2005
47  * @version $Revision: 45187 $
48  */

49 public class JASPISecurityManagerUnitTestCase extends JBossTestCase
50 {
51    protected Principal JavaDoc id = null;
52    
53    public JASPISecurityManagerUnitTestCase(String JavaDoc name)
54    {
55       super(name);
56    }
57
58    public void testConstruction() throws Exception JavaDoc
59    {
60       Configuration JavaDoc 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       //Check for bad CallbackHandler passage that has no setSecurityInfo method
68
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 JavaDoc ute)
74       {
75          Throwable JavaDoc t = ute.getUndeclaredThrowable();
76          if(t instanceof NoSuchMethodException JavaDoc == false)
77          {
78             fail("Test failed while expecting NoMethodException");
79          }
80       }
81    }
82    
83    public void testSuccessfulValidation() throws Exception JavaDoc
84    {
85       Configuration JavaDoc cfg = getConfiguration();
86       Configuration.setConfiguration(cfg);
87       JASPISecurityManager jsm = new JASPISecurityManager("java:/jaas/other",
88             new SecurityAssociationHandler());
89       Subject JavaDoc subject = new Subject JavaDoc();
90       Object JavaDoc 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 JavaDoc
100    {
101       Configuration JavaDoc 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 methods
110
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 JavaDoc getConfiguration() throws IOException JavaDoc
118    {
119       // Install the custom JAAS configuration
120
XMLLoginConfigImpl config = new XMLLoginConfigImpl();
121       config.setConfigResource("security/login-config.xml");
122       config.loadConfig();
123       return config;
124    }
125 }
126
Popular Tags