KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > security > test > authorization > XACMLEJBIntegrationUnitTest


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.authorization;
23
24 import java.rmi.RemoteException JavaDoc;
25
26 import javax.rmi.PortableRemoteObject JavaDoc;
27 import javax.security.auth.login.LoginContext JavaDoc;
28
29 import junit.extensions.TestSetup;
30 import junit.framework.Test;
31 import junit.framework.TestSuite;
32
33 import org.jboss.test.JBossTestCase;
34 import org.jboss.test.JBossTestSetup;
35 import org.jboss.test.security.interfaces.StatelessSession;
36 import org.jboss.test.security.interfaces.StatelessSessionHome;
37 import org.jboss.test.util.AppCallbackHandler;
38
39 //$Id: XACMLEJBIntegrationUnitTest.java 46132 2006-07-07 16:51:32Z asaldhana $
40

41 /**
42  * Unit tests for the XACML Integration of the EJB Layer
43  * @author <a HREF="mailto:Anil.Saldhana@jboss.org">Anil Saldhana</a>
44  * @since Jul 6, 2006
45  * @version $Revision: 46132 $
46  */

47 public class XACMLEJBIntegrationUnitTest extends JBossTestCase
48 {
49
50    static String JavaDoc username = "scott";
51    static char[] password = "echoman".toCharArray();
52
53    LoginContext JavaDoc lc;
54    boolean loggedIn;
55
56    private static String JavaDoc login_config = "security/authorization/xacml-ejb/app-policy-service.xml";
57
58    public XACMLEJBIntegrationUnitTest(String JavaDoc name)
59    {
60       super(name);
61    }
62
63    public static Test suite() throws Exception JavaDoc
64    {
65       TestSuite suite = new TestSuite();
66       suite.addTest(new TestSuite(XACMLEJBIntegrationUnitTest.class));
67       // Create an initializer for the test suite
68
TestSetup wrapper = new JBossTestSetup(suite)
69       {
70          protected void setUp() throws Exception JavaDoc
71          {
72             super.setUp();
73             deploy("xacml-ejb.jar");
74             deploy(getResourceURL(login_config));
75          }
76          protected void tearDown() throws Exception JavaDoc
77          {
78             undeploy(getResourceURL(login_config));
79             undeploy("xacml-ejb.jar");
80             super.tearDown();
81          }
82       };
83       return wrapper;
84    }
85
86
87    /** Test that the echo method is accessible by an Echo
88    role. Since the noop() method of the StatelessSession
89    bean was not assigned any permissions it should be unchecked.
90     */

91    public void testMethodAccess() throws Exception JavaDoc
92    {
93       log.debug("+++ testMethodAccess");
94       login();
95       Object JavaDoc obj = getInitialContext().lookup("spec.StatelessSession");
96       obj = PortableRemoteObject.narrow(obj, StatelessSessionHome.class);
97       StatelessSessionHome home = (StatelessSessionHome) obj;
98       log.debug("Found StatelessSessionHome");
99       StatelessSession bean = home.create();
100       log.debug("Created spec.StatelessSession");
101       log.debug("Bean.echo('Hello') -> "+bean.echo("Hello"));
102
103       try
104       {
105          // This should not be allowed
106
bean.noop();
107          fail("Was able to call StatelessSession.noop");
108       }
109       catch(RemoteException JavaDoc e)
110       {
111          log.debug("StatelessSession.noop failed as expected");
112       }
113       bean.remove();
114       logout();
115    }
116
117    /** Login as user scott using the conf.name login config or
118   'spec-test' if conf.name is not defined.
119     */

120    private void login() throws Exception JavaDoc
121    {
122       login(username, password);
123    }
124    private void login(String JavaDoc username, char[] password) throws Exception JavaDoc
125    {
126       if( loggedIn )
127          return;
128
129       lc = null;
130       String JavaDoc confName = System.getProperty("conf.name", "spec-test");
131       AppCallbackHandler handler = new AppCallbackHandler(username, password);
132       log.debug("Creating LoginContext("+confName+")");
133       lc = new LoginContext JavaDoc(confName, handler);
134       lc.login();
135       log.debug("Created LoginContext, subject="+lc.getSubject());
136       loggedIn = true;
137    }
138    
139    private void logout() throws Exception JavaDoc
140    {
141       if( loggedIn )
142       {
143          loggedIn = false;
144          lc.logout();
145       }
146    }
147 }
148
Popular Tags