KickJava   Java API By Example, From Geeks To Geeks.

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


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.ObjectName JavaDoc;
25 import javax.management.MBeanServerConnection JavaDoc;
26 import javax.security.auth.login.LoginContext JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28
29 import junit.framework.Test;
30 import junit.framework.TestSuite;
31 import org.jboss.test.JBossTestCase;
32 import org.jboss.test.JBossTestSetup;
33 import org.jboss.test.util.AppCallbackHandler;
34
35 /** Tests for a secured deployment of the jmx invoker adaptor
36  *
37  * @author Scott.Stark@jboss.org
38  * @version $Revision: 37406 $
39  */

40 public class SecureRMIAdaptorUnitTestCase
41    extends JBossTestCase
42 {
43    public SecureRMIAdaptorUnitTestCase(String JavaDoc name)
44    {
45       super(name);
46    }
47
48    public static Test suite()
49       throws Exception JavaDoc
50    {
51       TestSuite suite = new TestSuite();
52       suite.addTest(new TestSuite(SecureRMIAdaptorUnitTestCase.class));
53       JBossTestSetup wrapper = new JBossTestSetup(suite)
54       {
55          protected void setUp() throws Exception JavaDoc
56          {
57             deploymentException = null;
58             try
59             {
60                this.delegate.init();
61                // deploy the comma seperated list of jars
62
String JavaDoc serviceURL = this.getResourceURL("jmx/jmxadaptor/securejmx-invoker-service.xml");
63                this.redeploy(serviceURL);
64                this.getLog().debug("deployed package: " + serviceURL);
65             }
66             catch (Exception JavaDoc ex)
67             {
68                // Throw this in testServerFound() instead.
69
deploymentException = ex;
70             }
71          }
72
73          protected void tearDown() throws Exception JavaDoc
74          {
75             String JavaDoc serviceURL = this.getResourceURL("jmx/jmxadaptor/securejmx-invoker-service.xml");
76             this.undeploy(serviceURL);
77             this.getLog().debug("undeployed package: " + serviceURL);
78          }
79       };
80       return wrapper;
81
82    }
83
84    /**
85     * Test that a valid jmx-console domain user can invoke operations
86     * through the jmx/invoker/AuthenticatedRMIAdaptor
87     * @throws Exception
88     */

89    public void testAuthenticatedAccess() throws Exception JavaDoc
90    {
91       LoginContext JavaDoc lc = login("admin", "admin".toCharArray());
92       InitialContext JavaDoc ctx = getInitialContext();
93       MBeanServerConnection JavaDoc conn = (MBeanServerConnection JavaDoc) ctx.lookup("jmx/invoker/AuthenticatedRMIAdaptor");
94       ObjectName JavaDoc server = new ObjectName JavaDoc("jboss.system:type=Server");
95       String JavaDoc version = (String JavaDoc) conn.getAttribute(server, "Version");
96       log.info("Obtained server version: "+version);
97       lc.logout();
98    }
99
100    /**
101     * Test that a valid jmx-console domain user can NOT invoke operations
102     * through the jmx/invoker/AuthenticatedRMIAdaptor
103     * @throws Exception
104     */

105    public void testUnauthenticatedAccess() throws Exception JavaDoc
106    {
107       InitialContext JavaDoc ctx = getInitialContext();
108       MBeanServerConnection JavaDoc conn = (MBeanServerConnection JavaDoc) ctx.lookup("jmx/invoker/AuthenticatedRMIAdaptor");
109       ObjectName JavaDoc server = new ObjectName JavaDoc("jboss.system:type=Server");
110       try
111       {
112          String JavaDoc version = (String JavaDoc) conn.getAttribute(server, "Version");
113          log.info("Obtained server version: "+version);
114          fail("Was able to get server Version attribute");
115       }
116       catch(Exception JavaDoc e)
117       {
118          log.info("Access failed as expected", e);
119       }
120    }
121
122    /**
123     * Test that a valid jmx-console domain user can invoke operations
124     * through the jmx/invoker/AuthenticatedRMIAdaptor
125     * @throws Exception
126     */

127    public void testAuthorizedAccess() throws Exception JavaDoc
128    {
129       LoginContext JavaDoc lc = login("admin", "admin".toCharArray());
130       InitialContext JavaDoc ctx = getInitialContext();
131       MBeanServerConnection JavaDoc conn = (MBeanServerConnection JavaDoc) ctx.lookup("jmx/invoker/AuthorizedRMIAdaptor");
132       ObjectName JavaDoc server = new ObjectName JavaDoc("jboss.system:type=Server");
133       String JavaDoc version = (String JavaDoc) conn.getAttribute(server, "Version");
134       log.info("Obtained server version: "+version);
135       lc.logout();
136    }
137
138    /**
139     * Test that a valid jmx-console domain user can NOT invoke operations
140     * through the jmx/invoker/AuthenticatedRMIAdaptor
141     * @throws Exception
142     */

143    public void testUnauthorizedAccess() throws Exception JavaDoc
144    {
145       InitialContext JavaDoc ctx = getInitialContext();
146       MBeanServerConnection JavaDoc conn = (MBeanServerConnection JavaDoc) ctx.lookup("jmx/invoker/AuthorizedRMIAdaptor");
147       ObjectName JavaDoc server = new ObjectName JavaDoc("jboss.system:type=Server");
148       try
149       {
150          String JavaDoc version = (String JavaDoc) conn.getAttribute(server, "Version");
151          log.info("Obtained server version: "+version);
152          fail("Was able to get server Version attribute");
153       }
154       catch(Exception JavaDoc e)
155       {
156          log.info("Access failed as expected", e);
157       }
158    }
159
160    private LoginContext JavaDoc login(String JavaDoc username, char[] password) throws Exception JavaDoc
161    {
162       String JavaDoc confName = System.getProperty("conf.name", "other");
163       AppCallbackHandler handler = new AppCallbackHandler(username, password);
164       log.debug("Creating LoginContext("+confName+")");
165       LoginContext JavaDoc lc = new LoginContext JavaDoc(confName, handler);
166       lc.login();
167       log.debug("Created LoginContext, subject="+lc.getSubject());
168       return lc;
169    }
170
171 }
172
Popular Tags