KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > security > test > JaasUnitTestCase


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.test;
23
24 import java.net.URL JavaDoc;
25 import javax.rmi.PortableRemoteObject JavaDoc;
26 import javax.security.auth.login.Configuration JavaDoc;
27 import javax.security.auth.login.LoginContext JavaDoc;
28
29 import org.jboss.security.auth.login.XMLLoginConfigImpl;
30 import org.jboss.test.JBossTestCase;
31 import org.jboss.test.JBossTestSetup;
32 import org.jboss.test.security.interfaces.CustomPrincipalHome;
33 import org.jboss.test.security.interfaces.CustomPrincipal;
34 import org.jboss.test.security.ejb.CustomPrincipalImpl;
35 import org.jboss.test.util.AppCallbackHandler;
36 import org.jboss.test.util.web.HttpUtils;
37
38 import junit.extensions.TestSetup;
39 import junit.framework.Test;
40 import junit.framework.TestSuite;
41
42
43 /** JAAS specific tests.
44  
45  @author Scott.Stark@jboss.org
46  @version $Revision: 37406 $
47  */

48 public class JaasUnitTestCase
49    extends JBossTestCase
50 {
51    static String JavaDoc username = "jduke";
52    static char[] password = "theduke".toCharArray();
53    
54    LoginContext JavaDoc lc;
55    boolean loggedIn;
56
57    public JaasUnitTestCase(String JavaDoc name)
58    {
59       super(name);
60    }
61
62    /** Test return of a custom principal from getCallerPrincipal.
63     */

64    public void testCustomEJBPrincipal() throws Exception JavaDoc
65    {
66       login();
67       log.debug("+++ testCustomEJBPrincipal()");
68       Object JavaDoc obj = getInitialContext().lookup("jaas.CustomPrincipalHome");
69       obj = PortableRemoteObject.narrow(obj, CustomPrincipalHome.class);
70       CustomPrincipalHome home = (CustomPrincipalHome) obj;
71       log.debug("Found CustomPrincipalHome");
72       CustomPrincipal bean = home.create();
73       log.debug("Created CustomPrincipal");
74       
75       boolean isCustomType = bean.validateCallerPrincipal(CustomPrincipalImpl.class);
76       bean.remove();
77       logout();
78       assertTrue("CustomPrincipalImpl was seen", isCustomType);
79    }
80
81    /** Test return of a custom principal from getCallerPrincipal coming from
82     * a custom login module.
83     */

84    public void testCustomEJBPrincipal2() throws Exception JavaDoc
85    {
86       login();
87       log.debug("+++ testCustomEJBPrincipal()");
88       Object JavaDoc obj = getInitialContext().lookup("jaas.CustomPrincipal2Home");
89       obj = PortableRemoteObject.narrow(obj, CustomPrincipalHome.class);
90       CustomPrincipalHome home = (CustomPrincipalHome) obj;
91       log.debug("Found CustomPrincipalHome");
92       CustomPrincipal bean = home.create();
93       log.debug("Created CustomPrincipal");
94       
95       boolean isCustomType = bean.validateCallerPrincipal(CustomPrincipalImpl.class);
96       bean.remove();
97       logout();
98       assertTrue("CustomPrincipalImpl was seen", isCustomType);
99    }
100
101    public void testCustomWebPrincipal() throws Exception JavaDoc
102    {
103       log.debug("+++ testCustomWebPrincipal()");
104       String JavaDoc base = HttpUtils.getBaseURL();
105       URL JavaDoc testURL = new URL JavaDoc(base + "jaas/CustomPrincipalServlet"
106          +"?type="+CustomPrincipalImpl.class.getName());
107       HttpUtils.accessURL(testURL);
108    }
109
110    /** Login as user scott using the conf.name login config or
111     'jaas-test' if conf.name is not defined.
112     */

113    private void login() throws Exception JavaDoc
114    {
115       login(username, password);
116    }
117    private void login(String JavaDoc username, char[] password) throws Exception JavaDoc
118    {
119       if( loggedIn )
120          return;
121       
122       lc = null;
123       String JavaDoc confName = System.getProperty("conf.name", "jaas-test");
124       AppCallbackHandler handler = new AppCallbackHandler(username, password);
125       log.debug("Creating LoginContext("+confName+")");
126       lc = new LoginContext JavaDoc(confName, handler);
127       lc.login();
128       log.debug("Created LoginContext, subject="+lc.getSubject());
129       loggedIn = true;
130    }
131    private void logout() throws Exception JavaDoc
132    {
133       if( loggedIn )
134       {
135          loggedIn = false;
136          lc.logout();
137       }
138    }
139
140
141    /**
142     * Setup the test suite.
143     */

144    public static Test suite() throws Exception JavaDoc
145    {
146       TestSuite suite = new TestSuite();
147       suite.addTest(new TestSuite(JaasUnitTestCase.class));
148
149       // Create an initializer for the test suite
150
TestSetup wrapper = new JBossTestSetup(suite)
151       {
152          protected void setUp() throws Exception JavaDoc
153          {
154             super.setUp();
155             Configuration.setConfiguration(new XMLLoginConfigImpl());
156             deploy("security-jaas.ear");
157             flushAuthCache("jaas-test");
158          }
159          protected void tearDown() throws Exception JavaDoc
160          {
161             undeploy("security-jaas.ear");
162             super.tearDown();
163          
164          }
165       };
166       return wrapper;
167    }
168
169 }
170
Popular Tags