| 1 22 package org.jboss.test.jmx.test; 23 24 import javax.management.ObjectName ; 25 import javax.management.MBeanServerConnection ; 26 import javax.security.auth.login.LoginContext ; 27 import javax.naming.InitialContext ; 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 40 public class SecureRMIAdaptorUnitTestCase 41 extends JBossTestCase 42 { 43 public SecureRMIAdaptorUnitTestCase(String name) 44 { 45 super(name); 46 } 47 48 public static Test suite() 49 throws Exception  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  56 { 57 deploymentException = null; 58 try 59 { 60 this.delegate.init(); 61 String serviceURL = this.getResourceURL("jmx/jmxadaptor/securejmx-invoker-service.xml"); 63 this.redeploy(serviceURL); 64 this.getLog().debug("deployed package: " + serviceURL); 65 } 66 catch (Exception ex) 67 { 68 deploymentException = ex; 70 } 71 } 72 73 protected void tearDown() throws Exception  74 { 75 String 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 89 public void testAuthenticatedAccess() throws Exception  90 { 91 LoginContext lc = login("admin", "admin".toCharArray()); 92 InitialContext ctx = getInitialContext(); 93 MBeanServerConnection conn = (MBeanServerConnection ) ctx.lookup("jmx/invoker/AuthenticatedRMIAdaptor"); 94 ObjectName server = new ObjectName ("jboss.system:type=Server"); 95 String version = (String ) conn.getAttribute(server, "Version"); 96 log.info("Obtained server version: "+version); 97 lc.logout(); 98 } 99 100 105 public void testUnauthenticatedAccess() throws Exception  106 { 107 InitialContext ctx = getInitialContext(); 108 MBeanServerConnection conn = (MBeanServerConnection ) ctx.lookup("jmx/invoker/AuthenticatedRMIAdaptor"); 109 ObjectName server = new ObjectName ("jboss.system:type=Server"); 110 try 111 { 112 String version = (String ) conn.getAttribute(server, "Version"); 113 log.info("Obtained server version: "+version); 114 fail("Was able to get server Version attribute"); 115 } 116 catch(Exception e) 117 { 118 log.info("Access failed as expected", e); 119 } 120 } 121 122 127 public void testAuthorizedAccess() throws Exception  128 { 129 LoginContext lc = login("admin", "admin".toCharArray()); 130 InitialContext ctx = getInitialContext(); 131 MBeanServerConnection conn = (MBeanServerConnection ) ctx.lookup("jmx/invoker/AuthorizedRMIAdaptor"); 132 ObjectName server = new ObjectName ("jboss.system:type=Server"); 133 String version = (String ) conn.getAttribute(server, "Version"); 134 log.info("Obtained server version: "+version); 135 lc.logout(); 136 } 137 138 143 public void testUnauthorizedAccess() throws Exception  144 { 145 InitialContext ctx = getInitialContext(); 146 MBeanServerConnection conn = (MBeanServerConnection ) ctx.lookup("jmx/invoker/AuthorizedRMIAdaptor"); 147 ObjectName server = new ObjectName ("jboss.system:type=Server"); 148 try 149 { 150 String version = (String ) conn.getAttribute(server, "Version"); 151 log.info("Obtained server version: "+version); 152 fail("Was able to get server Version attribute"); 153 } 154 catch(Exception e) 155 { 156 log.info("Access failed as expected", e); 157 } 158 } 159 160 private LoginContext login(String username, char[] password) throws Exception  161 { 162 String confName = System.getProperty("conf.name", "other"); 163 AppCallbackHandler handler = new AppCallbackHandler(username, password); 164 log.debug("Creating LoginContext("+confName+")"); 165 LoginContext lc = new LoginContext (confName, handler); 166 lc.login(); 167 log.debug("Created LoginContext, subject="+lc.getSubject()); 168 return lc; 169 } 170 171 } 172 | Popular Tags |