/*
 * JBoss, the OpenSource J2EE webOS
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

// $Id: SecurityUnitTestCase.java,v 1.1.1.1.4.2 2004/10/08 18:34:06 starksm Exp $

package org.jboss.test.jbossnet.security;

import junit.framework.Test;
import org.jboss.test.jbossnet.WSTests;

/**
 * Tests security issues of web services
 * @created 5. Oktober 2001, 12:11
 * @author <a href="mailto:Christoph.Jung@infor.de">Christoph G. Jung</a>
 * @version $Revision: 1.1.1.1.4.2 $
 */
public class SecurityUnitTestCase extends WSTests
{

   /** has an associated end point that may be configured once */
   protected String JDUKE_END_POINT =
      PROTOCOL + "jduke:theduke@" + SERVICE_PORT;
   protected String JDUKEMAN_END_POINT =
      PROTOCOL + "jdukeman:anotherduke@" + SERVICE_PORT;
   protected String SCOTT_END_POINT =
      PROTOCOL + "scott:echoman@" + SERVICE_PORT;

   public final static String SECURITY_HACK_NAME =
      "jboss.test:name=SecurityHack";

   protected String AUTHENTICATION_SOAP_ACTION = "Authentication";
   protected String AUTHORIZATION_SOAP_ACTION = "Authorization";

   // Constructors --------------------------------------------------
   public SecurityUnitTestCase(String name)
   {
      super(name);
   }

   /** the session bean with which we interact */
   Authentication jduke_authentication;
   Authentication jdukeman_authentication;
   Authorization jduke_authorization;
   Authorization jdukeman_authorization;
   Authorization not_authorized;
   Authentication not_authenticated;

   /** where the config is stored */
   protected String getAxisConfiguration()
   {
      return "jbossnet/ejbsimple/client/" + super.getAxisConfiguration();
   }

   /** setup the bean */
   public void setUp() throws Exception
   {
      super.setUp();
      jduke_authentication =
         (Authentication) createAxisService(Authentication.class,
            JDUKE_END_POINT,
            AUTHENTICATION_SOAP_ACTION);
      jduke_authorization =
         (Authorization) createAxisService(Authorization.class,
            JDUKE_END_POINT,
            AUTHORIZATION_SOAP_ACTION);
      jdukeman_authentication =
         (Authentication) createAxisService(Authentication.class,
            JDUKEMAN_END_POINT,
            AUTHENTICATION_SOAP_ACTION);
      jdukeman_authorization =
         (Authorization) createAxisService(Authorization.class,
            JDUKEMAN_END_POINT,
            AUTHORIZATION_SOAP_ACTION);
      not_authorized =
         (Authorization) createAxisService(Authorization.class,
            AUTHORIZATION_SOAP_ACTION);
      not_authenticated =
         (Authentication) createAxisService(Authentication.class,
            AUTHENTICATION_SOAP_ACTION);
   }

   /** test authentication effects */
   public void testAuthentication() throws Exception
   {
      assertTrue(
         "Correctly authenticated as role1, role2",
         jduke_authentication.workedOut());
      assertTrue(
         "Correctly authenticated as role3, role2",
         !jdukeman_authentication.workedOut());
      try
      {
         not_authenticated.workedOut();
         fail("Not authenticated user came through.");
      }
      catch (Exception e)
      {
      }
   }

   /** test authorization effects */
   public void testAuthorization() throws Exception
   {
      assertTrue("Correctly authorized.", jduke_authorization.workedOut());
      try
      {
         jdukeman_authorization.workedOut();
         fail("Wrong authorization came through");
      }
      catch (Exception e)
      {
      }
   }

   /** this is to deploy the whole ear under a new login config */
   public static Test suite() throws Exception
   {
      return WSTests.getDeploySetup(SecurityUnitTestCase.class, "wssecurity.ear");
   }

   public static void main(String[] args)
   {
      junit.textui.TestRunner.run(SecurityUnitTestCase.class);
   }

}