Author: sohil.shah(a)jboss.com
Date: 2009-02-14 13:35:41 -0500 (Sat, 14 Feb 2009)
New Revision: 12822
Added:
modules/authorization/trunk/http-profile/src/test/java/org/jboss/security/authz/http/components/TestRoles.java
Modified:
modules/authorization/trunk/http-profile/pom.xml
Log:
testing the User Roles related Rule for the Http Profile
Modified: modules/authorization/trunk/http-profile/pom.xml
===================================================================
--- modules/authorization/trunk/http-profile/pom.xml 2009-02-14 18:00:24 UTC (rev 12821)
+++ modules/authorization/trunk/http-profile/pom.xml 2009-02-14 18:35:41 UTC (rev 12822)
@@ -64,7 +64,7 @@
<version>2.3.1</version>
<configuration>
<includes>
- <include>**/TestParameterMatching.java</include>
+ <include>**/TestRoles.java</include>
</includes>
</configuration>
</plugin>
Added:
modules/authorization/trunk/http-profile/src/test/java/org/jboss/security/authz/http/components/TestRoles.java
===================================================================
---
modules/authorization/trunk/http-profile/src/test/java/org/jboss/security/authz/http/components/TestRoles.java
(rev 0)
+++
modules/authorization/trunk/http-profile/src/test/java/org/jboss/security/authz/http/components/TestRoles.java 2009-02-14
18:35:41 UTC (rev 12822)
@@ -0,0 +1,150 @@
+/*
+* JBoss, a division of Red Hat
+* Copyright 2006, Red Hat Middleware, LLC, 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.security.authz.http.components;
+
+import java.net.URI;
+
+import org.apache.log4j.Logger;
+import org.jboss.security.authz.components.action.Read;
+import org.jboss.security.authz.components.resource.HttpResource;
+import org.jboss.security.authz.components.subject.Roles;
+import org.jboss.security.authz.enforcement.Request;
+import org.jboss.security.authz.enforcement.Response;
+
+import org.jboss.security.authz.model.Policy;
+import org.jboss.security.authz.policy.server.PolicyServer;
+import org.jboss.security.authz.policy.server.Server;
+
+import junit.framework.TestCase;
+
+/**
+ * @author <a href="mailto:sshah@redhat.com">Sohil Shah</a>
+ */
+public class TestRoles extends TestCase
+{
+ private static Logger log = Logger.getLogger(TestRoles.class);
+
+ private PolicyServer policyServer;
+
+ public void setUp() throws Exception
+ {
+ Server.bootstrap();
+ this.policyServer =
(PolicyServer)Server.lookup("/policy-server/PolicyServer");
+ }
+
+ public void test() throws Exception
+ {
+ HttpResource policyResource = new HttpResource();
+ policyResource.setUri(new URI("/private/devspace/*"));
+ policyResource.addAllowed("admin");
+ policyResource.addAllowed("pm");
+ policyResource.addAllowed("lead");
+
+ this.policyServer.newPolicy(policyResource.getPolicyMetaData());
+
+ //Assert Policy State of the Server
+ Policy[] policies = this.policyServer.readAllPolicies();
+
+ assertTrue("Policy Store must not be empty!!", (policies != null &&
policies.length == 1));
+ log.info("------------------------------------------------------------------------------");
+ log.info(policies[0].generateXACMLPolicy());
+
+ HttpResource incoming = new HttpResource();
+ incoming.setUri(new URI("/private/devspace/wiki.html"));
+
+ //Access Grant
+ this.enforce(this.createRequest(incoming, new String[]{"hacker",
"coder", "bigshot", "lead"}), true);
+
+
+ //Access Deny
+ this.enforce(this.createRequest(incoming, new String[]{"hacker",
"coder", "bigshot"}), false);
+ }
+
+ public void testCaseAgnosticity() throws Exception
+ {
+ HttpResource policyResource = new HttpResource();
+ policyResource.setUri(new URI("/private/devspace/*"));
+ policyResource.addAllowed("AdMin");
+ policyResource.addAllowed("Pm");
+ policyResource.addAllowed("LeaD");
+
+ this.policyServer.newPolicy(policyResource.getPolicyMetaData());
+
+ //Assert Policy State of the Server
+ Policy[] policies = this.policyServer.readAllPolicies();
+
+ assertTrue("Policy Store must not be empty!!", (policies != null &&
policies.length == 1));
+ log.info("------------------------------------------------------------------------------");
+ log.info(policies[0].generateXACMLPolicy());
+
+ HttpResource incoming = new HttpResource();
+ incoming.setUri(new URI("/private/devspace/wiki.html"));
+
+ //Access Grant
+ this.enforce(this.createRequest(incoming, new String[]{"hAcKer",
"cOder", "BiGSHot", "lEAd"}), true);
+
+
+ //Access Deny
+ this.enforce(this.createRequest(incoming, new String[]{"hAcKer",
"cOder", "BiGSHot"}), false);
+ }
+ //-----------------------------------------------------------------------------------------------------------------------------------------------------
+ private void enforce(Request request, boolean mustBePermitted) throws Exception
+ {
+
+ Response response = this.policyServer.evaluate(request);
+
+ assertNotNull(response);
+ log.info("-----------------------------------");
+ log.info("Decision="+response.getMessage());
+
+ if(mustBePermitted)
+ {
+ assertTrue("Access must be granted!!!", response.isAccessGranted());
+ }
+ else
+ {
+ assertFalse("Access must be denied!!!", response.isAccessGranted());
+ }
+ }
+
+ private Request createRequest(HttpResource contextResource, String[] userRoles) throws
Exception
+ {
+ //Create a RequestType
+ Request request = new Request();
+
+ //Create Subjects
+ Roles roles = new Roles();
+ for(int i=0; i<userRoles.length; i++)
+ {
+ roles.addName(userRoles[i]);
+ }
+ request.addSubject(roles.getSubject());
+
+ //Create Resource
+ request.addResource(contextResource.getResource());
+
+ //Create Action
+ request.setAction(new Read().getAction());
+
+ return request;
+ }
+}