package org.jboss.test.webservice.samples;
import junit.framework.Test;
import org.jboss.security.SecurityAssociation;
import org.jboss.security.SimplePrincipal;
import org.jboss.test.webservice.WebserviceTestBase;
import javax.naming.InitialContext;
import javax.xml.namespace.QName;
import javax.xml.rpc.Service;
import javax.xml.rpc.Stub;
import java.rmi.RemoteException;
public class ServerSideEJBSecTestCase extends WebserviceTestBase
{
public static final String USERNAME = "kermit";
public static final String PASSWORD = "thefrog";
public ServerSideEJBSecTestCase(String name)
{
super(name);
}
public static Test suite() throws Exception
{
return getDeploySetup(ServerSideEJBSecTestCase.class, "ws4ee-samples-server-ejb-sec.jar, ws4ee-samples-server-ejb-sec-client.jar");
}
protected void tearDown() throws Exception
{
SecurityAssociation.setPrincipal(null);
SecurityAssociation.setCredential(null);
}
public void testRoleSecuredSLSB() throws Exception
{
InitialContext iniCtx = getClientContext();
OrganizationHome home = (OrganizationHome)iniCtx.lookup("ejb/RoleSecuredSLSB");
OrganizationRemote bean = null;
try
{
bean = home.create();
fail("Security exception expected");
}
catch (Exception e)
{
SecurityAssociation.setPrincipal(new SimplePrincipal(USERNAME));
SecurityAssociation.setCredential(PASSWORD);
bean = home.create();
}
String info = bean.getContactInfo("mafia");
assertEquals("The 'mafia' boss is currently out of office, please call again.", info);
}
public void testBasicSecuredSLSB() throws Exception
{
InitialContext iniCtx = getClientContext();
OrganizationHome home = (OrganizationHome)iniCtx.lookup("ejb/BasicSecuredSLSB");
OrganizationRemote bean = home.create();
String info = bean.getContactInfo("mafia");
assertEquals("The 'mafia' boss is currently out of office, please call again.", info);
}
public void testBasicSecuredServiceAccess() throws Exception
{
InitialContext iniCtx = getClientContext();
Service service = (Service)iniCtx.lookup("java:comp/env/service/BasicSecured");
Organization endpoint = (Organization)service.getPort(new QName("BasicSecuredPort"), Organization.class);
try
{
endpoint.getContactInfo("mafia");
fail("Security exception expected");
}
catch (RemoteException ignore)
{
}
Stub stub = (Stub)endpoint;
stub._setProperty(Stub.USERNAME_PROPERTY, USERNAME);
stub._setProperty(Stub.PASSWORD_PROPERTY, PASSWORD);
String info = endpoint.getContactInfo("mafia");
assertEquals("The 'mafia' boss is currently out of office, please call again.", info);
}
public void testRoleSecuredServiceAccess() throws Exception
{
InitialContext iniCtx = getClientContext();
Service service = (Service)iniCtx.lookup("java:comp/env/service/RoleSecured");
Organization endpoint = (Organization)service.getPort(new QName("RoleSecuredPort"), Organization.class);
try
{
endpoint.getContactInfo("mafia");
fail("Security exception expected");
}
catch (RemoteException ignore)
{
}
Stub stub = (Stub)endpoint;
stub._setProperty(Stub.USERNAME_PROPERTY, USERNAME);
stub._setProperty(Stub.PASSWORD_PROPERTY, PASSWORD);
String info = endpoint.getContactInfo("mafia");
assertEquals("The 'mafia' boss is currently out of office, please call again.", info);
}
}