KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > test > RMIAdaptorAuthorizationUnitTestCase


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.jmx.test;
23
24 import javax.management.MBeanInfo JavaDoc;
25 import javax.management.MBeanServerConnection JavaDoc;
26 import javax.management.ObjectName JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28 import javax.security.auth.login.LoginContext JavaDoc;
29
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.util.AppCallbackHandler;
36
37 //$Id$
38

39 /**
40  * Authorization of the RMI Adaptor
41  * Especially tests the usage of the authorization delegate
42  * called as org.jboss.jmx.connector.invoker.ExternalizableRolesAuthorization
43  * @author <a HREF="mailto:Anil.Saldhana@jboss.org">Anil Saldhana</a>
44  * @since May 10, 2006
45  * @version $Revision: 55394 $
46  */

47 public class RMIAdaptorAuthorizationUnitTestCase extends JBossTestCase
48 {
49    public RMIAdaptorAuthorizationUnitTestCase(String JavaDoc name)
50    {
51       super(name);
52    }
53    
54    /**
55     * Test that a valid jmx-console domain user can invoke operations
56     * through the jmx/invoker/AuthenticatedRMIAdaptor
57     * @throws Exception
58     */

59    public void testConfigurableRolesAuthorizedAccess() throws Exception JavaDoc
60    {
61       LoginContext JavaDoc lc = login("admin", "admin".toCharArray());
62       InitialContext JavaDoc ctx = getInitialContext();
63       MBeanServerConnection JavaDoc conn = (MBeanServerConnection JavaDoc) ctx.lookup("jmx/invoker/ConfigurableAuthorizedRMIAdaptor");
64       ObjectName JavaDoc server = new ObjectName JavaDoc("jboss.system:type=Server");
65       String JavaDoc version = (String JavaDoc) conn.getAttribute(server, "Version");
66       log.info("Obtained server version: "+version);
67       MBeanInfo JavaDoc info = conn.getMBeanInfo(server);
68       assertNotNull("MBeanInfo != null", info);
69       Integer JavaDoc mbeanCount = conn.getMBeanCount();
70       assertNotNull("mbeanCount != null", mbeanCount);
71       lc.logout();
72    }
73    
74    /**
75     * Test invalid access
76     * @throws Exception
77     */

78    public void testUnAuthorizedAccess() throws Exception JavaDoc
79    {
80       InitialContext JavaDoc ctx = getInitialContext();
81       MBeanServerConnection JavaDoc conn = (MBeanServerConnection JavaDoc) ctx.lookup("jmx/invoker/ConfigurableAuthorizedRMIAdaptor");
82       ObjectName JavaDoc server = new ObjectName JavaDoc("jboss.system:type=Server");
83       try
84       {
85          String JavaDoc version = (String JavaDoc) conn.getAttribute(server, "Version");
86          log.info("Obtained server version: "+version);
87          fail("Was able to get server Version attribute");
88       }
89       catch(Exception JavaDoc e)
90       {
91          log.info("Access failed as expected", e);
92       }
93    }
94    
95    public static Test suite()
96    throws Exception JavaDoc
97    {
98       TestSuite suite = new TestSuite();
99       suite.addTest(new TestSuite(RMIAdaptorAuthorizationUnitTestCase.class));
100       
101       JBossTestSetup wrapper = new JBossTestSetup(suite)
102       {
103          protected void setUp() throws Exception JavaDoc
104          {
105             deploymentException = null;
106             try
107             {
108                this.delegate.init();
109                redeploy("jmxinvoker-authorization-test.jar");
110                // deploy the comma seperated list of jars
111
redeploy(getResourceURL("jmx/jmxadaptor/authorization-jmx-invoker-service.xml"));
112                redeploy(getResourceURL("jmx/jmxadaptor/jaas-service.xml"));
113             }
114             catch (Exception JavaDoc ex)
115             {
116                // Throw this in testServerFound() instead.
117
deploymentException = ex;
118             }
119          }
120          
121          protected void tearDown() throws Exception JavaDoc
122          {
123             undeploy(getResourceURL("jmx/jmxadaptor/authorization-jmx-invoker-service.xml"));
124             undeploy("jmxinvoker-authorization-test.jar");
125             undeploy(getResourceURL("jmx/jmxadaptor/jaas-service.xml"));
126          }
127       };
128       return wrapper;
129    }
130
131    private LoginContext JavaDoc login(String JavaDoc username, char[] password) throws Exception JavaDoc
132    {
133       String JavaDoc confName = System.getProperty("conf.name", "other");
134       AppCallbackHandler handler = new AppCallbackHandler(username, password);
135       log.debug("Creating LoginContext("+confName+")");
136       LoginContext JavaDoc lc = new LoginContext JavaDoc(confName, handler);
137       lc.login();
138       log.debug("Created LoginContext, subject="+lc.getSubject());
139       return lc;
140    }
141 }
142
Popular Tags