KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > security > container > auth > modules > DelegatingServerAuthModuleUnitTestCase


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.modules;
23
24 import java.io.IOException JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30
31 import javax.security.auth.Subject JavaDoc;
32 import javax.security.auth.callback.CallbackHandler JavaDoc;
33 import javax.security.auth.login.AppConfigurationEntry JavaDoc;
34 import javax.security.auth.login.Configuration JavaDoc;
35 import javax.security.auth.message.AuthException;
36
37 import org.jboss.security.SimplePrincipal;
38 import org.jboss.security.auth.container.modules.DelegatingServerAuthModule;
39 import org.jboss.security.auth.login.LoginModuleStackHolder;
40 import org.jboss.security.auth.login.XMLLoginConfigImpl;
41 import org.jboss.test.JBossTestCase;
42 import org.jboss.test.util.AppCallbackHandler;
43
44 //$Id: DelegatingServerAuthModuleUnitTestCase.java 45183 2006-05-23 20:23:05Z asaldhana $
45

46 /**
47  * Unit TestCase for DelegatingServerAuthModule
48  * @author <a HREF="mailto:Anil.Saldhana@jboss.org">Anil Saldhana</a>
49  * @since Dec 24, 2005
50  * @version $Revision: 45183 $
51  */

52 public class DelegatingServerAuthModuleUnitTestCase extends JBossTestCase
53 {
54    private DelegatingServerAuthModule authm = null;
55    
56    public DelegatingServerAuthModuleUnitTestCase(String JavaDoc name)
57    {
58       super(name);
59    }
60    
61    public void testCtrWithConfigName()
62    {
63       authm = new DelegatingServerAuthModule("other");
64       assertNotNull("DelegatingServerAuthModule != null", authm);
65    }
66    
67    public void testCtrWithConfigPassed() throws Exception JavaDoc
68    {
69       authm = new DelegatingServerAuthModule("other", getConfiguration());
70       assertNotNull("DelegatingServerAuthModule != null", authm);
71    }
72    
73    public void testCtrWithLoginModuleStackHolder() throws Exception JavaDoc
74    {
75       authm = new DelegatingServerAuthModule(getLoginModuleStackHolder());
76       assertNotNull("DelegatingServerAuthModule != null", authm);
77    }
78    
79    public void testValidateRequestWithConfigName() throws Exception JavaDoc
80    {
81       authm = new DelegatingServerAuthModule("other");
82       assertNotNull("DelegatingServerAuthModule != null", authm);
83       Map JavaDoc options = new HashMap JavaDoc();
84       options.put("securityConfigService","jboss.security:service=SecurityConfig");
85       options.put("testMode", "TRUE");
86       authm.initialize(null,null,getCallbackHandler("admin","secret"),options);
87       try
88       {
89          authm.validateRequest(null,null,null,null);
90       }catch(AuthException e)
91       {
92          fail(e.getLocalizedMessage());
93       }
94       
95       checkAuthenticatedSubject(authm.getAuthenticatedSubject());
96    }
97    
98    public void testValidateRequestWithConfiguration() throws Exception JavaDoc
99    {
100       authm = new DelegatingServerAuthModule("other", getConfiguration());
101       assertNotNull("DelegatingServerAuthModule != null", authm);
102       authm.initialize(null,null,getCallbackHandler("admin","secret"),null);
103       try
104       {
105          authm.validateRequest(null,null,null,null);
106       }catch(AuthException e)
107       {
108          fail(e.getLocalizedMessage());
109       }
110       checkAuthenticatedSubject(authm.getAuthenticatedSubject());
111    }
112    
113    public void testValidateRequestWithLoginModuleStackHolder() throws Exception JavaDoc
114    {
115       authm = new DelegatingServerAuthModule(getLoginModuleStackHolder());
116       assertNotNull("DelegatingServerAuthModule != null", authm);
117       authm.initialize(null,null,getCallbackHandler("admin","secret"),null);
118       try
119       {
120          authm.validateRequest(null,null,null,null);
121       }catch(AuthException e)
122       {
123          fail(e.getLocalizedMessage());
124       }
125       checkAuthenticatedSubject(authm.getAuthenticatedSubject());
126    }
127    
128    public void testInValidRequestWithLoginModuleStackHolder() throws Exception JavaDoc
129    {
130       authm = new DelegatingServerAuthModule( getLoginModuleStackHolder());
131       assertNotNull("DelegatingServerAuthModule != null", authm);
132       authm.initialize(null,null,getCallbackHandler("admin","bad"),null );
133       try
134       {
135          authm.validateRequest(null,null,null,null);
136          fail("Test Should have failed");
137       }catch(AuthException ae)
138       {
139          //pass
140
}
141    }
142    
143    public void testInValidRequestWithConfiguration() throws Exception JavaDoc
144    {
145       authm = new DelegatingServerAuthModule("other", getConfiguration());
146       assertNotNull("DelegatingServerAuthModule != null", authm);
147       authm.initialize(null,null,getCallbackHandler("admin","bad"),null );
148       try
149       {
150          authm.validateRequest(null,null,null,null);
151          fail("Test Should have failed");
152       }catch(AuthException ae)
153       {
154          //pass
155
}
156    }
157    
158    public void testInValidRequestWithConfigName() throws Exception JavaDoc
159    {
160       authm = new DelegatingServerAuthModule("other");
161       assertNotNull("DelegatingServerAuthModule != null", authm);
162       Map JavaDoc options = new HashMap JavaDoc();
163       options.put("securityConfigService","jboss.security:service=SecurityConfig");
164       options.put("testMode", "TRUE");
165       authm.initialize(null,null,getCallbackHandler("admin","bad"),options );
166       try
167       {
168          authm.validateRequest(null,null,null,null);
169          fail("Test Should have failed");
170       }catch(AuthException ae)
171       {
172          //pass
173
}
174    }
175    
176    //PRIVATE METHODS
177
private Configuration JavaDoc getConfiguration() throws IOException JavaDoc
178    {
179       // Install the custom JAAS configuration
180
XMLLoginConfigImpl config = new XMLLoginConfigImpl();
181       config.setConfigResource("security/login-config.xml");
182       config.loadConfig();
183       return config;
184    }
185    
186    private CallbackHandler JavaDoc getCallbackHandler(String JavaDoc username, String JavaDoc pwd)
187    {
188       return new AppCallbackHandler(username,pwd.toCharArray());
189    }
190    
191    private void checkAuthenticatedSubject(Subject JavaDoc authenticatedSubject)
192    {
193       assertNotNull("Authenticated Subject != null",authenticatedSubject );
194       Set JavaDoc principals = authenticatedSubject.getPrincipals(SimplePrincipal.class);
195       assertTrue("admin is contained in principal",
196             principals.contains(new SimplePrincipal("admin")) );
197    }
198    
199    private LoginModuleStackHolder getLoginModuleStackHolder() throws IOException JavaDoc
200    {
201       Configuration JavaDoc config = getConfiguration();
202       AppConfigurationEntry JavaDoc[] appEntries = config.getAppConfigurationEntry("other");
203       List JavaDoc aList = Arrays.asList(appEntries);
204       LoginModuleStackHolder lmsh = new LoginModuleStackHolder("other", aList);
205       return lmsh;
206    }
207 }
208
Popular Tags