/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 Bull S.A.
 * Contact: jonas-team@objectweb.org
 * 
 * This library 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 any later version.
 * 
 * This library 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 library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.jtests.clients.security;


import javax.ejb.EJBException;
import java.rmi.RemoteException;
import java.util.Hashtable;
import java.util.Vector;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.transaction.TransactionRequiredException;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.objectweb.security.context.SecurityCurrent;
import org.objectweb.security.context.SecurityContext;
import org.objectweb.jonas.jtests.beans.secured.BaseS;
import org.objectweb.jonas.jtests.util.JTestCase;


/**
 * Security Management common tests for all type of beans (Entity/Session)
 *
 * @author Ph.Coq, Ph.Durieux
 *
 */

public abstract class A_AccessControl  extends JTestCase {

    protected static String PRINCIPAL1_NAME = "principal1";
    protected static String ROLE1_NAME = "baserole1";
    protected static String ROLE2_NAME = "baserole2";
    protected static SecurityCurrent current = null;
    protected static SecurityContext principal1 = null;
    protected static SecurityContext principal2 = null;


    public A_AccessControl(String name) {
	super(name);
    }

    /**
     * init environment:
     * - load beans
     */
    protected void setUp() {
	super.setUp();
	if (current == null) {
	    current = SecurityCurrent.getCurrent();
	    principal1 = new SecurityContext("principal1");
	    principal2 = new SecurityContext("principal2");
        }
    }

    public abstract BaseS getBaseS(String name) throws Exception;

    /**
     * test getCallerPrincipal. 
     * The Principal must be propagated.
     */
    public void testGetCallerPrincipal() throws Exception {
	current.setSecurityContext(principal1);
	BaseS sl = getBaseS("un");
	assertEquals(PRINCIPAL1_NAME, sl.getPrincipalName());
	sl.remove();
    }

    /**
     * test isCallerInRole.
     * principal1 = role1
     * principal2 = role2
     */
    public void testIsCallerInRole() throws Exception {
	current.setSecurityContext(principal1);
        BaseS sl = getBaseS("deux");
	assertTrue(sl.isCallerInRole(ROLE1_NAME) == true);
	assertTrue(sl.isCallerInRole(ROLE2_NAME) == false);
	sl.remove();
    }

    /**
     * test basic method reject
     */
    public void testBasicMethodReject() throws Exception {
	current.setSecurityContext(principal1);
        BaseS sl = getBaseS("trois");
	try {
	    sl.simpleMethod(); 
	    fail("should be rejected: not in the role");
	} catch (RemoteException e) {
	}
	sl.remove();
    }

    /**
     * test basic method accept
     */
    public void testBasicMethodAccept() throws Exception {
	current.setSecurityContext(principal2);
        BaseS sl = getBaseS("quatre");
	sl.simpleMethod();
	sl.remove();
    }

    /**
     * test complex method reject
     */
    public void testComplexMethodReject() throws Exception {
	current.setSecurityContext(principal1);
        BaseS sl = getBaseS("cinq");
	try {
	    Hashtable ht = new Hashtable();
	    ht.put("foo", new Vector(10));
	    ht.put("bar", new Hashtable());
	    Object[] o = {"bar", new Hashtable(), new Vector()};
	    sl.complexMethod(ht, o);
	    fail("should be rejected: not in the role");
	} catch (RemoteException e) {
	}
	sl.remove();
    }

    /**
     * test complex method accept
     */
    public void testComplexMethodAccept() throws Exception {
	current.setSecurityContext(principal2);
        BaseS sl = getBaseS("six");
	Hashtable ht = new Hashtable();
	ht.put("foo", new Vector(10));
	ht.put("bar", new Hashtable());
	Object[] o = {"bar", new Hashtable(), new Vector()};
	sl.complexMethod(ht, o);
	sl.remove();
    }

    /**
     * test security-role-ref in DD
     * baserole -> role1
     */
    public void testSecurityRoleRef() throws Exception {
	current.setSecurityContext(principal1);
        BaseS sl = getBaseS("sept");
	assertTrue(sl.isCallerInRole(ROLE1_NAME) == true);
	sl.remove();
    }

    /**
     * test principal propagation from bean to bean
     */
    public void testBeanToBeanPropagation() throws Exception {
	current.setSecurityContext(principal1);
        BaseS sl = getBaseS("sept");
	assertEquals(PRINCIPAL1_NAME, sl.getPrincipalNameOfAnotherBean());
	sl.remove();
    }

    /**
     * test principal propagation from bean to bean and access is denied
     */
    public void testRejectBeanToBeanAccess() throws Exception {
	current.setSecurityContext(principal2);
        BaseS sl = getBaseS("huit");
        try {
            sl.getPrincipalNameOfAnotherBean();
            fail("should be rejected: not in the role");
        } catch (RemoteException e) {
	} finally {
            sl.remove();
        }
    }

    /**
     * test accept access to a local method 
     * callAnotherMethod is called with role1 and call DerivedSF.anotheMethod with this role
     * return false if access to anotheMethod was denied
     * expected return is true
     */
    
    public void testLocalMethodAccept() throws Exception {
	current.setSecurityContext(principal1);
        BaseS sl = getBaseS("neuf");
	assertTrue(sl.callAnotherMethod() == true);
        sl.remove();
    }


    /**
     * test reject access to a local method 
     * callAnotherMethod is called with role1 and call DerivedSF.anotheMethod with this role
     * return false if access to anotheMethod was denied
     * expected return is true
     */
    
    public void testLocalMethodReject() throws Exception {       
        current.setSecurityContext(principal2);
        BaseS sl = getBaseS("dix");
 	assertTrue(sl.callAnotherMethod() == false);
        sl.remove();
    }
}
