1 22 package org.jboss.test.security.test; 23 24 import java.net.URL ; 25 26 import javax.management.Attribute ; 27 import javax.management.MBeanServerConnection ; 28 import javax.management.ObjectName ; 29 30 import junit.framework.Test; 31 import junit.framework.TestSuite; 32 33 import org.jboss.system.ServiceContext; 34 import org.jboss.test.JBossTestCase; 35 import org.jboss.test.JBossTestSetup; 36 import org.jboss.test.util.ServiceControllerUtil; 37 38 40 46 public class DynamicLoginConfigServiceUnitTestCase extends JBossTestCase 47 { 48 private String login_config = "<policy>\n<application-policy name='test-dyn'>"+ 49 "<authentication>"+ 50 "<login-module code='org.jboss.security.auth.spi.UsersRolesLoginModule'" + 51 " flag = 'required' /> </authentication></application-policy></policy>"; 52 53 private ServiceControllerUtil sutil = null; 54 public DynamicLoginConfigServiceUnitTestCase(String name) 55 { 56 super(name); 57 try 58 { 59 sutil = new ServiceControllerUtil(this.getServer()); 60 } 61 catch (Exception e) 62 { 63 fail("Exception thrown in creating util class"+e.getLocalizedMessage()); 64 } 65 } 66 67 71 public void testAbsoluteLoginConfigURL() throws Exception 72 { 73 MBeanServerConnection server = getServer(); 74 ObjectName oname = new ObjectName ("jboss:service=TempFileCreator"); 75 URL confURL = (URL )server.invoke(oname,"createTempFile", 76 new Object []{"test-dyn",login_config}, 77 new String [] {"java.lang.String", "java.lang.String"}); 78 assertNotNull(" config url != null",confURL); 79 ObjectName serviceName = new ObjectName ("jboss:service=TestDynamicLoginConfig"); 80 if(server.isRegistered(serviceName)) 81 server.unregisterMBean(serviceName); 82 prepareTestDynamicLoginConfig(server,serviceName,confURL.toExternalForm()); 83 sutil.createAService(serviceName); 84 assertTrue("Created?", sutil.isCreated(serviceName)); 85 sutil.startAService(serviceName); 86 assertTrue("Started?", sutil.isStarted(serviceName)); 87 sutil.stopAService(serviceName); 89 assertTrue("Stopped?", sutil.isStopped(serviceName)); 90 sutil.startAService(serviceName); 91 assertTrue("Started?", sutil.isStarted(serviceName)); 92 93 String authConfig = (String )server.getAttribute(serviceName,"AuthConfig"); 94 assertEquals(confURL + "matches", confURL.toExternalForm(), authConfig); 95 sutil.stopAService(serviceName); 96 assertTrue("Stopped?", sutil.isStopped(serviceName)); 97 sutil.startAService(serviceName); 98 assertTrue("Started?", sutil.isStarted(serviceName)); 99 sutil.destroyAService(serviceName); 100 assertEquals("state is Destroyed", ServiceContext.getStateString(ServiceContext.DESTROYED), 101 sutil.getStateString(serviceName)); 102 if(server.isRegistered(serviceName)) 103 server.unregisterMBean(serviceName); 104 } 105 106 110 public void testAuthConf() throws Exception 111 { 112 MBeanServerConnection server = getServer(); 113 ObjectName serviceName = new ObjectName ("jboss:service=TestDynamicLoginConfig"); 114 sutil.removeAService(serviceName); 115 if(server.isRegistered(serviceName)) 116 { 117 server.unregisterMBean(serviceName); 118 } 119 prepareTestDynamicLoginConfig(server, 120 new ObjectName ("jboss:service=TestDynamicLoginConfig"), null); 121 try 122 { 123 sutil.createAService(serviceName); 124 assertEquals("state is Created", ServiceContext.getStateString(ServiceContext.CREATED), 125 sutil.getStateString(serviceName)); 126 sutil.startAService(serviceName); 127 assertFalse("Should not Start", sutil.isStarted(serviceName)); 128 } 129 catch(Exception t) 130 { 131 fail("Exception thrown:"+t.getLocalizedMessage()); 132 } 133 finally 134 { 135 if(sutil.isStarted(serviceName)) 136 { 137 sutil.stopAService(serviceName); 138 assertEquals("state is Stopped", ServiceContext.getStateString(ServiceContext.STOPPED), 139 sutil.getStateString(serviceName)); 140 } 141 } 142 143 try 144 { 145 server.setAttribute(serviceName, 146 new Attribute ("AuthConfig","login-config.xml")); 147 sutil.startAService(serviceName); 148 assertFalse("Should not Start?", sutil.isStarted(serviceName)); 149 } 150 catch(Exception t) 151 { 152 fail("Failute to set an attribute and start a service:"+t.getLocalizedMessage()); 153 } 154 finally 155 { 156 boolean isStarted = sutil.isStarted(serviceName); 157 log.info(serviceName+", isStarted: "+isStarted); 158 if(isStarted) 159 { 160 sutil.stopAService(serviceName); 161 assertEquals("state is Stopped", ServiceContext.getStateString(ServiceContext.STOPPED), 162 sutil.getStateString(serviceName)); 163 } 164 sutil.destroyAService(serviceName); 165 assertEquals("state is Destroyed", ServiceContext.getStateString(ServiceContext.DESTROYED), 166 sutil.getStateString(serviceName)); 167 sutil.removeAService(serviceName); 168 if(server.isRegistered(serviceName)) 169 server.unregisterMBean(serviceName); 170 } 171 } 172 173 private void prepareTestDynamicLoginConfig(MBeanServerConnection server, 174 ObjectName serviceOName, String confURL) throws Exception 175 { 176 server.createMBean("org.jboss.security.auth.login.DynamicLoginConfig", 177 serviceOName); 178 if(confURL != null) 179 { 180 Attribute attr = new Attribute ("AuthConfig", confURL); 181 server.setAttribute(serviceOName,attr); 182 } 183 184 ObjectName lcs = new ObjectName ("jboss.security:service=XMLLoginConfig"); 185 Attribute attrLCS = new Attribute ("LoginConfigService", lcs); 186 server.setAttribute(serviceOName,attrLCS); 187 } 188 189 public static Test suite() 190 throws Exception 191 { 192 TestSuite suite = new TestSuite(); 193 suite.addTest(new TestSuite(DynamicLoginConfigServiceUnitTestCase.class)); 194 JBossTestSetup wrapper = new JBossTestSetup(suite) 195 { 196 protected void setUp() throws Exception 197 { 198 deploymentException = null; 199 try 200 { 201 this.delegate.init(); 202 this.deploy("tempfilecreator.jar"); 203 this.redeploy(getResourceURL("jmx/tempFileCreator-service.xml")); 204 } 205 catch (Exception ex) 206 { 207 deploymentException = ex; 209 } 210 } 211 212 protected void tearDown() throws Exception 213 { 214 this.undeploy(getResourceURL("jmx/tempFileCreator-service.xml")); 215 } 216 }; 217 return wrapper; 218 } 219 } 220 | Popular Tags |