/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.test.jbossnet.security;

import junit.framework.Test;
import junit.framework.TestSuite;

import org.jboss.test.jbossnet.JBossNetTestBase;

import javax.xml.namespace.QName;
import javax.xml.rpc.Stub;
import java.net.URL;

/**
 * Tests security issues of web services
 *
 * @author <a href="mailto:Christoph.Jung@infor.de">Christoph G. Jung</a>
 * @author <a href="mailto:thomas.diesler@jboss.org">Thomas Diesler</a>
 * @auhtor <a href="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
 * @version $Revision: 1.1.1.1.4.8 $
 */
public class SecurityUnitTestCase extends JBossNetTestBase
{
   private QName AUTHENTICATION_SERVICE = new QName("http://" + getServerHost() + ":8080/jboss-net/services/Authentication", "AuthenticationService");
   private QName AUTHORIZATION_SERVICE = new QName("http://" + getServerHost() + ":8080/jboss-net/services/Authorization", "AuthorizationService");

   // 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;

   /** setup the bean */
   public void setUp() throws Exception
   {
      super.setUp();
      URL wsdlAuthentication = new URL(SERVICES_LOCATION + "/Authentication?wsdl");
      URL wsdlAuthorization = new URL(SERVICES_LOCATION + "/Authorization?wsdl");

      jduke_authentication = (Authentication)createService(wsdlAuthentication, AUTHENTICATION_SERVICE).getPort(Authentication.class);
      ((Stub)jduke_authentication)._setProperty(Stub.USERNAME_PROPERTY, "jduke");
      ((Stub)jduke_authentication)._setProperty(Stub.PASSWORD_PROPERTY, "theduke");

      jduke_authorization = (Authorization)createService(wsdlAuthorization, AUTHORIZATION_SERVICE).getPort(Authorization.class);
      ((Stub)jduke_authorization)._setProperty(Stub.USERNAME_PROPERTY, "jduke");
      ((Stub)jduke_authorization)._setProperty(Stub.PASSWORD_PROPERTY, "theduke");

      jdukeman_authentication = (Authentication)createService(wsdlAuthentication, AUTHENTICATION_SERVICE).getPort(Authentication.class);
      ((Stub)jdukeman_authentication)._setProperty(Stub.USERNAME_PROPERTY, "jdukeman");
      ((Stub)jdukeman_authentication)._setProperty(Stub.PASSWORD_PROPERTY, "anotherduke");

      jdukeman_authorization = (Authorization)createService(wsdlAuthorization, AUTHORIZATION_SERVICE).getPort(Authorization.class);
      ((Stub)jdukeman_authorization)._setProperty(Stub.USERNAME_PROPERTY, "jdukeman");
      ((Stub)jdukeman_authorization)._setProperty(Stub.PASSWORD_PROPERTY, "anotherduke");

      not_authenticated = (Authentication)(Authentication)createService(wsdlAuthentication, AUTHENTICATION_SERVICE).getPort(Authentication.class);
      not_authorized = (Authorization)createService(wsdlAuthorization, AUTHORIZATION_SERVICE).getPort(Authorization.class);
   }

   /** 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)
      {
      }
   }

   public static Test suite() throws Exception
   {
      if (checkJBossSecurityHandlersPresent() == true)
      {
         return getDeploySetup(SecurityUnitTestCase.class, "jbossnet-security.ear");
      }
      else
      {
         return new TestSuite();
      }
   }

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

   public static boolean checkJBossSecurityHandlersPresent()
   {
      ClassLoader cl = Thread.currentThread().getContextClassLoader();
      try
      {
         cl.loadClass("org.jboss.net.axis.server.JBossAuthenticationHandler");
         cl.loadClass("org.jboss.net.axis.server.JBossAuthorizationHandler");
         return true;
      }
      catch (ClassNotFoundException e)
      {
         return false;
      }
   }
}