001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.test.security;
023:
024: import junit.framework.TestCase;
025: import org.jboss.portal.security.RoleSecurityBinding;
026: import org.jboss.portal.security.SecurityConstants;
027: import org.jboss.portal.security.impl.JBossAuthorizationDomainRegistryImpl;
028: import org.jboss.portal.security.impl.jacc.JACCPortalAuthorizationManagerFactory;
029: import org.jboss.portal.security.spi.auth.PortalAuthorizationManager;
030: import org.jboss.portal.security.spi.auth.PortalAuthorizationManagerFactory;
031:
032: import java.util.Collections;
033:
034: /**
035: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
036: * @version $Revision: 8784 $
037: */
038: public class ComplexTestCase extends TestCase {
039:
040: /** . */
041: private PortalAuthorizationManagerFactory factory;
042:
043: /** . */
044: private Server server;
045:
046: protected void setUp() throws Exception {
047: server = new Server();
048: server.start();
049:
050: //
051: FlatAuthorizationDomain domain = new FlatAuthorizationDomain();
052: domain.getConfigurator().setSecurityBindings(
053: "_admin",
054: Collections.singleton(new RoleSecurityBinding("",
055: "admin")));
056: domain.getConfigurator().setSecurityBindings(
057: "_user",
058: Collections.singleton(new RoleSecurityBinding("",
059: "user")));
060: domain.getConfigurator().setSecurityBindings(
061: "_unchecked",
062: Collections.singleton(new RoleSecurityBinding("",
063: SecurityConstants.UNCHECKED_ROLE_NAME)));
064:
065: //
066: JBossAuthorizationDomainRegistryImpl registry = new JBossAuthorizationDomainRegistryImpl();
067: registry.addDomain(domain);
068:
069: //
070: JACCPortalAuthorizationManagerFactory factory = new JACCPortalAuthorizationManagerFactory();
071: factory.setAuthorizationDomainRegistry(registry);
072: this .factory = factory;
073: }
074:
075: public void testUnauthenticated() throws Exception {
076: server.execute(new Server.Task() {
077: public void execute() throws Exception {
078: PortalAuthorizationManager manager = factory
079: .getManager();
080:
081: //
082: assertFalse(manager.checkPermission(new FlatPermission(
083: "_admin")));
084: assertFalse(manager.checkPermission(new FlatPermission(
085: "_user")));
086: }
087: });
088: }
089:
090: public void testAdmin() throws Exception {
091: server.execute(new Server.Task() {
092: public void execute() throws Exception {
093: PortalAuthorizationManager manager = factory
094: .getManager();
095:
096: //
097: server.associateRoles(new String[] { "admin" });
098: assertTrue(manager.checkPermission(new FlatPermission(
099: "_unchecked")));
100: assertTrue(manager.checkPermission(new FlatPermission(
101: "_admin")));
102: assertFalse(manager.checkPermission(new FlatPermission(
103: "_user")));
104: }
105: });
106: }
107:
108: public void testUser() throws Exception {
109: server.execute(new Server.Task() {
110: public void execute() throws Exception {
111: PortalAuthorizationManager manager = factory
112: .getManager();
113:
114: //
115: server.associateRoles(new String[] { "user" });
116: assertFalse(manager.checkPermission(new FlatPermission(
117: "_admin")));
118: assertTrue(manager.checkPermission(new FlatPermission(
119: "_user")));
120: }
121: });
122: }
123:
124: public void testUserAndAdmin() throws Exception {
125: server.execute(new Server.Task() {
126: public void execute() throws Exception {
127: PortalAuthorizationManager manager = factory
128: .getManager();
129:
130: //
131: server.associateRoles(new String[] { "user", "admin" });
132: assertTrue(manager.checkPermission(new FlatPermission(
133: "_admin")));
134: assertTrue(manager.checkPermission(new FlatPermission(
135: "_user")));
136: }
137: });
138: }
139: }
|