[jboss-cvs] JBossAS SVN: r59104 - trunk/testsuite/src/main/org/jboss/test/jacc/test

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Dec 18 18:18:09 EST 2006


Author: anil.saldhana at jboss.com
Date: 2006-12-18 18:18:08 -0500 (Mon, 18 Dec 2006)
New Revision: 59104

Added:
   trunk/testsuite/src/main/org/jboss/test/jacc/test/TestJBossPolicyConfiguration.java
   trunk/testsuite/src/main/org/jboss/test/jacc/test/WebPermissionsValidationTestCase.java
Log:
Test to validate jacc permissions generation for the web layer

Added: trunk/testsuite/src/main/org/jboss/test/jacc/test/TestJBossPolicyConfiguration.java
===================================================================
--- trunk/testsuite/src/main/org/jboss/test/jacc/test/TestJBossPolicyConfiguration.java	2006-12-18 23:17:33 UTC (rev 59103)
+++ trunk/testsuite/src/main/org/jboss/test/jacc/test/TestJBossPolicyConfiguration.java	2006-12-18 23:18:08 UTC (rev 59104)
@@ -0,0 +1,154 @@
+/*
+  * 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.jacc.test;
+
+import java.security.Permission;
+import java.security.PermissionCollection;
+import java.security.Permissions;
+import java.util.Enumeration;
+import java.util.HashMap;
+
+import javax.security.jacc.PolicyConfiguration;
+import javax.security.jacc.PolicyContextException;
+
+//$Id$
+
+/**
+ *  Policy Configuration used for permissions validation
+ *  @author <a href="mailto:Anil.Saldhana at jboss.org">Anil Saldhana</a>
+ *  @since  Dec 18, 2006 
+ *  @version $Revision$
+ */
+public class TestJBossPolicyConfiguration implements PolicyConfiguration
+{
+   private String contextID;  
+   
+   private Permissions excludedPolicy = new Permissions();
+   private Permissions uncheckedPolicy = new Permissions();
+   
+   private HashMap<String,Permissions> rolePerms = new HashMap<String,Permissions>();
+   
+
+   public TestJBossPolicyConfiguration(String contextID)
+   { 
+      this.contextID = contextID;
+   }
+
+   public void addToExcludedPolicy(Permission permission) throws PolicyContextException
+   { 
+      this.excludedPolicy.add(permission);
+   }
+
+   public void addToExcludedPolicy(PermissionCollection permissions) throws PolicyContextException
+   { 
+      Enumeration<Permission> en = permissions.elements();
+      while(en.hasMoreElements())
+         addToExcludedPolicy(en.nextElement());
+   }
+
+   public void addToRole(String roleName, Permission permission) throws PolicyContextException
+   { 
+      Permissions p = rolePerms.get(roleName);
+      if(p == null)
+         p = new Permissions();
+      p.add(permission);
+      rolePerms.put(roleName, p);
+   }
+
+   public void addToRole(String roleName, PermissionCollection permissions) throws PolicyContextException
+   { 
+      Enumeration<Permission> en = permissions.elements();
+      while(en.hasMoreElements())
+         addToRole(roleName,en.nextElement());
+   }
+
+   public void addToUncheckedPolicy(Permission permission) throws PolicyContextException
+   { 
+      this.uncheckedPolicy.add(permission);
+   }
+
+   public void addToUncheckedPolicy(PermissionCollection permissions) throws PolicyContextException
+   { 
+      Enumeration<Permission> en = permissions.elements();
+      while(en.hasMoreElements())
+         addToUncheckedPolicy(en.nextElement());
+   }
+
+   public void commit() throws PolicyContextException
+   { 
+   }
+
+   public void delete() throws PolicyContextException
+   { 
+   }
+
+   public String getContextID() throws PolicyContextException
+   { 
+      return this.contextID;
+   }
+
+   public boolean inService() throws PolicyContextException
+   { 
+      return false;
+   }
+
+   public void linkConfiguration(PolicyConfiguration link) throws PolicyContextException
+   {  
+   }
+
+   public void removeExcludedPolicy() throws PolicyContextException
+   { 
+      this.excludedPolicy = null;
+   }
+
+   public void removeRole(String roleName) throws PolicyContextException
+   { 
+      Permissions p = this.rolePerms.get(roleName);
+      if(p != null)
+      {
+         p = null;
+         rolePerms.remove(roleName);
+      }   
+   }
+
+   public void removeUncheckedPolicy() throws PolicyContextException
+   { 
+      this.uncheckedPolicy = null;
+   }
+   
+   //Value added methods 
+
+   public Permissions getExcludedPolicy()
+   {
+      return excludedPolicy;
+   }
+
+   public Permissions getUncheckedPolicy()
+   {
+      return uncheckedPolicy;
+   } 
+   
+   public Permissions getPermissionsForRole(String roleName)
+   {
+      return this.rolePerms.get(roleName);
+   }
+}

Added: trunk/testsuite/src/main/org/jboss/test/jacc/test/WebPermissionsValidationTestCase.java
===================================================================
--- trunk/testsuite/src/main/org/jboss/test/jacc/test/WebPermissionsValidationTestCase.java	2006-12-18 23:17:33 UTC (rev 59103)
+++ trunk/testsuite/src/main/org/jboss/test/jacc/test/WebPermissionsValidationTestCase.java	2006-12-18 23:18:08 UTC (rev 59104)
@@ -0,0 +1,147 @@
+/*
+  * 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.jacc.test;
+ 
+import java.io.InputStream;
+import java.net.URL;
+import java.security.Permissions;
+
+import javax.security.jacc.WebResourcePermission;
+import javax.security.jacc.WebRoleRefPermission;
+import javax.security.jacc.WebUserDataPermission;
+
+import org.jboss.metadata.WebMetaData;
+import org.jboss.metadata.web.WebMetaDataObjectFactory;
+import org.jboss.test.JBossTestCase;
+import org.jboss.util.xml.JBossEntityResolver; 
+import org.jboss.web.WebPermissionMapping;
+import org.jboss.xb.binding.JBossXBException;
+import org.jboss.xb.binding.ObjectModelFactory;
+import org.jboss.xb.binding.Unmarshaller;
+import org.jboss.xb.binding.UnmarshallerFactory;
+
+//$Id$
+
+/**
+ *  Validate the parsing of web.xml and the creation of JACC Permissions
+ *  @author <a href="mailto:Anil.Saldhana at jboss.org">Anil Saldhana</a>
+ *  @since  Dec 18, 2006 
+ *  @version $Revision$
+ */
+public class WebPermissionsValidationTestCase extends JBossTestCase
+{ 
+   public WebPermissionsValidationTestCase(String name)
+   {
+      super(name); 
+   }
+   
+   public WebMetaData getWebMetaData(ObjectModelFactory factory, InputStream webxml) 
+   throws JBossXBException
+   { 
+      Unmarshaller unmarshaller = null; 
+      UnmarshallerFactory unmarshallerFactory = UnmarshallerFactory
+              .newInstance();
+      //        unmarshallerFactory.setFeature(Unmarshaller.SCHEMA_VALIDATION, Boolean.TRUE);
+      unmarshaller = unmarshallerFactory.newUnmarshaller();
+      JBossEntityResolver entityResolver = new JBossEntityResolver();
+      unmarshaller.setEntityResolver(entityResolver);
+
+      return (WebMetaData) unmarshaller.unmarshal(webxml, factory, null); 
+   } 
+   
+   public void testWebPermissions() throws Exception 
+   {
+      ClassLoader cl = Thread.currentThread().getContextClassLoader();
+      URL webxml = cl.getResource("security/jacc/webperm/web.xml");
+      assertNotNull("web.xml exists?", webxml);
+      WebMetaData wmd = getWebMetaData(new WebMetaDataObjectFactory(),
+            webxml.openStream());
+      TestJBossPolicyConfiguration tpc = new TestJBossPolicyConfiguration("dummy");
+      WebPermissionMapping.createPermissions(wmd, tpc); 
+      checkUncheckedPermissions(tpc.getUncheckedPolicy());
+      checkExcludedPermissions(tpc.getExcludedPolicy());
+      checkAddToRoleForAdministrator(tpc.getPermissionsForRole("Administrator")); 
+      checkAddToRoleForManager(tpc.getPermissionsForRole("Manager")); 
+      checkAddToRoleForEmployee(tpc.getPermissionsForRole("Employee"));
+   }  
+    
+   
+   private void checkUncheckedPermissions(Permissions p)
+   {
+      assertTrue(p.implies(new WebResourcePermission("/sslprotected.jsp", "!GET,POST")));
+      assertTrue(p.implies(new WebResourcePermission("/:/secured.jsp:/unchecked.jsp:/excluded.jsp:/sslprotected.jsp",
+            (String) null)));
+      assertTrue(p.implies(new WebResourcePermission("/excluded.jsp", "!GET,POST")));
+      assertTrue(p.implies(new WebResourcePermission("/secured.jsp", "!GET,POST")));
+      assertTrue(p.implies(new WebResourcePermission("/unchecked.jsp", (String) null)));
+      
+      assertTrue(p.implies(new WebUserDataPermission("/sslprotected.jsp", "GET,POST:CONFIDENTIAL")));
+      assertTrue(p.implies(new WebUserDataPermission("/excluded.jsp", "!GET,POST")));
+      assertTrue(p.implies(new WebUserDataPermission("/sslprotected.jsp", "!GET,POST")));
+      assertTrue(p.implies(new WebUserDataPermission("/secured.jsp", (String) null)));
+      assertTrue(p.implies(new WebUserDataPermission("/:/unchecked.jsp:/secured.jsp:/sslprotected.jsp:/excluded.jsp",
+            (String) null)));
+      assertTrue(p.implies(new WebUserDataPermission("/unchecked.jsp", (String) null)));
+   }
+   
+   private void checkExcludedPermissions(Permissions p)
+   {
+      assertTrue(p.implies(new WebResourcePermission("/excluded.jsp", "GET,POST"))); 
+      assertTrue(p.implies(new WebUserDataPermission("/excluded.jsp", "GET,POST")));
+   }
+   
+   private void checkAddToRoleForManager(Permissions p)
+   { 
+      assertTrue(p.implies(new WebRoleRefPermission("secured", "Manager")));
+      assertTrue(p.implies(new WebRoleRefPermission("sslprotected", "MGR")));
+      assertTrue(p.implies(new WebRoleRefPermission("sslprotected", "Manager")));
+      assertTrue(p.implies(new WebRoleRefPermission("unchecked", "Manager")));
+      assertTrue(p.implies(new WebRoleRefPermission("excluded", "Manager")));
+      //Jacc1.1 
+      assertTrue(p.implies(new WebRoleRefPermission("", "Manager"))); 
+   } 
+   
+   private void checkAddToRoleForAdministrator(Permissions p)
+   {
+      assertTrue(p.implies(new WebResourcePermission("/secured.jsp", "GET,POST")));
+      assertTrue(p.implies(new WebResourcePermission("/sslprotected.jsp", "GET,POST")));
+      
+      assertTrue(p.implies(new WebRoleRefPermission("secured", "ADM")));
+      assertTrue(p.implies(new WebRoleRefPermission("secured", "Administrator")));
+      assertTrue(p.implies(new WebRoleRefPermission("sslprotected", "ADM")));
+      assertTrue(p.implies(new WebRoleRefPermission("sslprotected", "Administrator")));
+      assertTrue(p.implies(new WebRoleRefPermission("unchecked", "Administrator")));
+      assertTrue(p.implies(new WebRoleRefPermission("excluded", "Administrator")));
+      //Jacc1.1
+      assertTrue(p.implies(new WebRoleRefPermission("", "Administrator"))); 
+   }
+   
+   private void checkAddToRoleForEmployee(Permissions p)
+   {
+      assertTrue(p.implies(new WebRoleRefPermission("secured", "Employee")));
+      assertTrue(p.implies(new WebRoleRefPermission("sslprotected", "Employee")));
+      assertTrue(p.implies(new WebRoleRefPermission("unchecked", "Employee")));
+      assertTrue(p.implies(new WebRoleRefPermission("excluded", "Employee"))); 
+      //  Jacc1.1
+      assertTrue(p.implies(new WebRoleRefPermission("", "Employee"))); 
+   }
+}




More information about the jboss-cvs-commits mailing list