1 22 package org.jboss.test.security.container.auth; 23 24 import java.util.HashMap ; 25 26 import javax.security.auth.message.config.AuthConfigFactory; 27 import javax.security.auth.message.config.AuthConfigProvider; 28 import javax.security.auth.message.config.AuthConfigFactory.RegistrationListener; 29 30 import org.apache.commons.httpclient.HttpClient; 31 import org.apache.commons.httpclient.methods.GetMethod; 32 import org.jboss.security.SecurityConstants; 33 import org.jboss.security.auth.message.config.JBossAuthConfigFactory; 34 import org.jboss.test.JBossTestCase; 35 36 42 public class AuthConfigFactoryTestCase extends JBossTestCase 43 { 44 private String baseURLNoAuth = "http://" + getServerHost() + ":" + Integer.getInteger("web.port", 8080) + "/"; 45 private HttpClient httpConn = new HttpClient(); 46 String allLayerProvider = "org.jboss.test.security.container.auth.AllLayerAuthConfigProvider"; 47 String allACProvider = "org.jboss.test.security.container.auth.AllAppContextAuthConfigProvider"; 48 49 public AuthConfigFactoryTestCase(String name) 50 { 51 super(name); 52 } 53 54 protected AuthConfigFactory factory = null; 55 56 protected void setUp() throws Exception 57 { 58 factory = AuthConfigFactory.getFactory(); 59 assertNotNull("AuthContextFactory is null?", factory); 60 } 61 62 protected void tearDown() throws Exception 63 { 64 factory = null; 65 } 66 67 public void testGetFactory() 68 { 69 assertTrue("AuthConfigFactory instance of JBossAuthConfigFactory", 70 factory instanceof JBossAuthConfigFactory); 71 } 72 73 public void testSetFactory() 74 { 75 AuthConfigFactory.setFactory(new TestAuthConfigFactory()); 76 factory = AuthConfigFactory.getFactory(); 77 assertNotNull("AuthConfigFactory is null?", factory); 78 assertTrue("AuthConfigFactory instance of TestAuthConfigFactory", 79 factory instanceof TestAuthConfigFactory); 80 AuthConfigFactory.setFactory(null); 82 assertTrue("AuthConfigFactory instance of JBossAuthConfigFactory", 83 AuthConfigFactory.getFactory() instanceof JBossAuthConfigFactory); 84 } 85 86 87 public void testServerAuthContext() throws Exception 88 { 89 this.deploy("jbosssx-jaspi-web.war"); 91 try 92 { 93 String str = "jbosssx-jaspi-web/DebugServlet?user=jduke&pass=theduke"; 95 GetMethod indexGet = new GetMethod(baseURLNoAuth+str); 96 int responseCode = httpConn.executeMethod(indexGet); 97 assertTrue("Response code == 200?", responseCode == 200); 98 str = "jbosssx-jaspi-web/DebugServlet?user=jduke&pass=bad"; 100 indexGet = new GetMethod(baseURLNoAuth+str); 101 responseCode = httpConn.executeMethod(indexGet); 102 assertTrue("Response code == 500?", responseCode == 500); 103 } 104 catch(Throwable t) 105 { 106 throw new Exception (t.getMessage()); 107 } 108 finally 109 { 110 this.undeploy("jbosssx-jaspi-web.war"); 111 } 112 } 113 114 public void testConfigProviderRegistration() throws Exception 115 { 116 String registrationID = null; 117 String layer = null; 118 String appContext = "testAppContext"; 119 factory = AuthConfigFactory.getFactory(); 120 registrationID = factory.registerConfigProvider(allLayerProvider, 122 new HashMap (),layer,appContext, "This is a test provider"); 123 AuthConfigProvider acp = factory.getConfigProvider("TestLayer",appContext, 124 null); 125 assertTrue("ACP instanceof AllLayerAuthConfigProvider", 126 acp instanceof AllLayerAuthConfigProvider); 127 acp = factory.getConfigProvider(layer,appContext, null); 128 assertTrue("ACP instanceof AllLayerAuthConfigProvider", 129 acp instanceof AllLayerAuthConfigProvider); 130 131 assertTrue("Registration removed", factory.removeRegistration(registrationID)); 132 layer = SecurityConstants.SERVLET_LAYER; 133 registrationID = factory.registerConfigProvider(allACProvider, new HashMap (), 135 layer, null, "This is a test provider"); 136 acp = factory.getConfigProvider(layer,"testAppContext", null); 137 assertTrue("ACP instanceof AllAppContextAuthConfigProvider", 138 acp instanceof AllAppContextAuthConfigProvider); 139 acp = factory.getConfigProvider(layer,"testAppContext", null); 140 assertTrue("ACP instanceof AllAppContextAuthConfigProvider", 141 acp instanceof AllAppContextAuthConfigProvider); 142 acp = factory.getConfigProvider(layer,"testOtherAppContext", null); 143 assertTrue("ACP instanceof AllAppContextAuthConfigProvider", 144 acp instanceof AllAppContextAuthConfigProvider); 145 } 146 147 public void testRegistrationListener() throws Exception 148 { 149 RegistrationListener rl = new RegistrationListener() 150 { 151 public void notify(String layer, String appContext) 152 { 153 } 154 }; 155 156 String layer = SecurityConstants.SERVLET_LAYER; 157 factory.registerConfigProvider( allACProvider, new HashMap (), 159 layer, null, "This is a test provider"); 160 AuthConfigProvider acp = factory.getConfigProvider(layer,"testAppContext", rl); 161 String [] ids = factory.getRegistrationIDs(acp); 162 String [] detachedIds = factory.detachListener(rl, layer, "testAppContext"); 163 checkStringArrayEquals(ids, detachedIds); 164 } 165 166 private void checkStringArrayEquals(String [] a, String [] b) 167 { 168 if(a == null && b == null) 169 return; 170 assertEquals("Length should be equal",a.length,b.length); 171 int len = a.length; 172 for(int i=0; i < len; i++) 173 assertEquals(a[i],b[i]); 174 } 175 } 176 | Popular Tags |