001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.geronimo.web25.deployment;
021:
022: import java.net.URL;
023: import java.util.Collection;
024: import java.util.Set;
025: import java.util.HashSet;
026: import java.util.Map;
027: import java.util.HashMap;
028: import java.util.Collections;
029: import java.util.jar.JarFile;
030: import java.security.PermissionCollection;
031:
032: import javax.security.jacc.WebResourcePermission;
033:
034: import junit.framework.TestCase;
035: import org.apache.geronimo.common.DeploymentException;
036: import org.apache.geronimo.deployment.ModuleIDBuilder;
037: import org.apache.geronimo.gbean.AbstractName;
038: import org.apache.geronimo.j2ee.deployment.EARContext;
039: import org.apache.geronimo.j2ee.deployment.Module;
040: import org.apache.geronimo.kernel.Naming;
041: import org.apache.geronimo.xbeans.javaee.WebAppType;
042: import org.apache.geronimo.xbeans.javaee.WebAppDocument;
043: import org.apache.geronimo.security.jacc.ComponentPermissions;
044: import org.apache.xmlbeans.XmlOptions;
045:
046: /**
047: * @version $Rev: 538086 $ $Date: 2007-05-15 01:11:36 -0700 (Tue, 15 May 2007) $
048: */
049: public class SpecSecurityParsingTest extends TestCase {
050:
051: private ClassLoader classLoader = this .getClass().getClassLoader();
052: private XmlOptions options = new XmlOptions();
053: private TestWebModuleBuilder builder = new TestWebModuleBuilder();
054: private Set<String> roleSet = new HashSet<String>();
055: private Map<String, PermissionCollection> rolePermissionMap = new HashMap<String, PermissionCollection>();
056:
057: public void testParsing() throws Exception {
058: roleSet.add("Admin");
059: URL srcXml = classLoader.getResource("security/web1.xml");
060: WebAppDocument webAppDoc = WebAppDocument.Factory.parse(srcXml,
061: options);
062: WebAppType webAppType = webAppDoc.getWebApp();
063: ComponentPermissions permissions = builder
064: .buildSpecSecurityConfig(webAppType, roleSet,
065: rolePermissionMap);
066: PermissionCollection unchecked = permissions
067: .getUncheckedPermissions();
068: assertTrue(unchecked.implies(new WebResourcePermission(
069: "/login.do", "!")));
070: assertTrue(unchecked.implies(new WebResourcePermission("/foo",
071: "!")));
072: assertFalse(unchecked.implies(new WebResourcePermission(
073: "/foo.do", "!")));
074: PermissionCollection adminPermissions = permissions
075: .getRolePermissions().get("Admin");
076: assertTrue(adminPermissions.implies(new WebResourcePermission(
077: "foo.do", "GET,POST")));
078: }
079:
080: /**
081: * make sure a resource permission with a role doesn't turn into an unchecked permission due to mistakes in
082: * HTTPMethod "all" handling
083: * @throws Exception
084: */
085: public void testAllMethodsConstraint() throws Exception {
086: roleSet.add("Admin");
087: URL srcXml = classLoader.getResource("security/web2.xml");
088: WebAppDocument webAppDoc = WebAppDocument.Factory.parse(srcXml,
089: options);
090: WebAppType webAppType = webAppDoc.getWebApp();
091: ComponentPermissions permissions = builder
092: .buildSpecSecurityConfig(webAppType, roleSet,
093: rolePermissionMap);
094: PermissionCollection unchecked = permissions
095: .getUncheckedPermissions();
096: assertFalse(unchecked.implies(new WebResourcePermission(
097: "/Test", "!")));
098: PermissionCollection adminPermissions = permissions
099: .getRolePermissions().get("Admin");
100: assertTrue(adminPermissions.implies(new WebResourcePermission(
101: "/Test", "GET,POST")));
102: }
103:
104: public static class TestWebModuleBuilder extends
105: AbstractWebModuleBuilder {
106:
107: protected TestWebModuleBuilder() {
108: super (null, null, null, null, null, Collections.EMPTY_SET,
109: null);
110: }
111:
112: protected Module createModule(Object plan, JarFile moduleFile,
113: String targetPath, URL specDDUrl, boolean standAlone,
114: String contextRoot, AbstractName earName,
115: Naming naming, ModuleIDBuilder idBuilder)
116: throws DeploymentException {
117: return null;
118: }
119:
120: public void initContext(EARContext earContext, Module module,
121: ClassLoader cl) throws DeploymentException {
122: }
123:
124: public void addGBeans(EARContext earContext, Module module,
125: ClassLoader cl, Collection repository)
126: throws DeploymentException {
127: }
128:
129: public String getSchemaNamespace() {
130: return null;
131: }
132: }
133: }
|