001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id:
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.clients.security;
027:
028: import org.objectweb.jonas.jtests.beans.secured.BaseS;
029: import org.objectweb.jonas.jtests.util.JTestCase;
030: import org.objectweb.security.context.SecurityContext;
031: import org.objectweb.security.context.SecurityCurrent;
032:
033: /**
034: * Security Management common tests for all type of beans (Entity/Session)
035: *
036: * @author Ph.Coq, Ph.Durieux
037: *
038: */
039:
040: public abstract class B_AccessControl extends JTestCase {
041:
042: protected static String PRINCIPAL1_NAME = "principal1";
043: protected static String PRINCIPAL3_NAME = "principal3";
044: protected static String ROLE1_NAME = "baserole1";
045: protected static String ROLE2_NAME = "baserole2";
046:
047: protected static SecurityCurrent current = null;
048: protected static SecurityContext principal1 = null;
049: protected static SecurityContext principal2 = null;
050: protected static SecurityContext principal3 = null;
051: protected static SecurityContext principal4 = null;
052:
053: public B_AccessControl(String name) {
054: super (name);
055: }
056:
057: /**
058: * init environment:
059: * - load beans
060: */
061: protected void setUp() {
062: super .setUp();
063: if (current == null) {
064: current = SecurityCurrent.getCurrent();
065: principal1 = new SecurityContext("principal1");
066: principal2 = new SecurityContext("principal2");
067: String[] roles3 = new String[] { "role1", "role3" };
068: principal3 = new SecurityContext(PRINCIPAL3_NAME, roles3);
069: String[] roles4 = new String[] { "role2" };
070: principal4 = new SecurityContext("principal4", roles4);
071: }
072: }
073:
074: public abstract BaseS getBaseS(String name) throws Exception;
075:
076: /**
077: * test getCallerPrincipal.
078: * The Principal must be propagated.
079: */
080: public void testGetCallerPrincipal() throws Exception {
081: current.setSecurityContext(principal1);
082: BaseS sl = getBaseS("un");
083: assertEquals(PRINCIPAL1_NAME, sl.getPrincipalName());
084: sl.remove();
085: }
086:
087: /**
088: * test isCallerInRole.
089: * principal1 = role1
090: * principal2 = role2
091: */
092: public void testIsCallerInRole() throws Exception {
093: current.setSecurityContext(principal1);
094: BaseS sl = getBaseS("deux");
095: assertTrue(sl.isCallerInRole(ROLE1_NAME) == true);
096: assertTrue(sl.isCallerInRole(ROLE2_NAME) == false);
097: sl.remove();
098: }
099:
100: /**
101: * test basic method accept
102: */
103: public void testBasicMethodAccept() throws Exception {
104: current.setSecurityContext(principal2);
105: BaseS sl = getBaseS("quatre");
106: sl.simpleMethod();
107: sl.remove();
108: }
109:
110: /**
111: * test principal propagation from bean to bean
112: */
113: public void testBeanToBeanPropagation() throws Exception {
114: current.setSecurityContext(principal1);
115: BaseS sl = getBaseS("sept");
116: assertEquals(PRINCIPAL1_NAME, sl
117: .getPrincipalNameOfAnotherBean());
118: sl.remove();
119: }
120:
121: }
|