Commit 9fb43fad authored by Romain Ferrari's avatar Romain Ferrari

Adding unitary tests for StringFunctions:

urn:oasis:names:tc:xacml:2.0:function:string-concatenate and
urn:oasis:names:tc:xacml:3.0:function:boolean-from-string
and adding them into the MainTest test suite
enhancing log for unitary tests of Match, AnyOf and AllOf elements
parent 77d84f42
......@@ -163,9 +163,9 @@ public class StringFunction extends FunctionBase
} else if(str.equalsIgnoreCase("false")) {
result = new EvaluationResult(BooleanAttribute.getFalseInstance());
} else {
Status status = new Status(Arrays.asList(Status.STATUS_PROCESSING_ERROR));
Status status = new Status(Arrays.asList(Status.STATUS_SYNTAX_ERROR));
StatusCode code = new StatusCode();
code.setValue(Status.STATUS_PROCESSING_ERROR);
code.setValue(Status.STATUS_SYNTAX_ERROR);
status.setStatusCode(code);
result = new EvaluationResult(status);
}
......
package com.sun.xacml.xacmlv3;
import java.io.File;
import java.net.URL;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOf;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.Policy;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicySet;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.Request;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.Rule;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sun.xacml.EvaluationCtx;
import com.sun.xacml.MatchResult;
import com.sun.xacml.ParsingException;
import com.sun.xacml.PolicyMetaData;
import com.thalesgroup.authzforce.pdp.core.test.impl.MainTest;
import com.thalesgroup.authzforce.pdp.core.test.utils.TestUtils;
/**
* @author Romain Ferrari
*
*/
public class TestMatchAlg {
private static EvaluationCtx context;
private static Request request = null;
private static PolicySet policySet = null;
private static Rule ruleNoMatch;
private static Rule ruleMatch;
private final static int ONE_ALL_OF = 0;
private final static int MULTI_MATCH = 1;
private final static int MULTI_ALL_OF = 2;
private final static int RULE_NO_MATCH = 0;
private final static int RULE_MATCH = 1;
/*
* LOGGER used for all class
*/
private static final Logger LOGGER = LoggerFactory
.getLogger(TestMatchAlg.class);
@BeforeClass
public static void setUp() {
LOGGER.info("Starting match algorithm test of AllOf, AnyOf and Match elements");
try {
URL requestFile = Thread.currentThread().getContextClassLoader().getResource("custom"+ File.separator +"requestAllOfTest.xml");
URL policyFile = Thread.currentThread().getContextClassLoader().getResource("custom"+ File.separator +"PolicySetCustomTest.xml");
request = (Request)JAXBContext.newInstance(Request.class).createUnmarshaller().unmarshal(requestFile);
policySet = (PolicySet)JAXBContext.newInstance(PolicySet.class).createUnmarshaller().unmarshal(policyFile);
} catch (JAXBException e) {
e.printStackTrace();
System.err.println(e.getLocalizedMessage());
}
context = TestUtils.createContext(request);
ruleNoMatch = (Rule) ((Policy)policySet.getPolicySetsAndPoliciesAndPolicySetIdReferences().get(0)).getCombinerParametersAndRuleCombinerParametersAndVariableDefinitions().get(RULE_NO_MATCH);
ruleMatch = (Rule) ((Policy)policySet.getPolicySetsAndPoliciesAndPolicySetIdReferences().get(0)).getCombinerParametersAndRuleCombinerParametersAndVariableDefinitions().get(RULE_MATCH);
}
/**
* Testing the AllOf algorithm
* @throws ParsingException
*/
@Test
public final void testMatchAllOf() throws ParsingException {
LOGGER.info("Testing AllOf algorithm");
AnyOf anyOf = ruleNoMatch.getTarget().getAnyOves().get(ONE_ALL_OF);
MatchResult result = null;
com.sun.xacml.xacmlv3.AnyOf myAnyOf = com.sun.xacml.xacmlv3.AnyOf.getInstance(anyOf, new PolicyMetaData());
result = myAnyOf.match(context);
Assert.assertEquals("AllOf algorithm failed when looking for MATCH", MatchResult.NO_MATCH, result.getResult());
anyOf = ruleMatch.getTarget().getAnyOves().get(ONE_ALL_OF);
myAnyOf = com.sun.xacml.xacmlv3.AnyOf.getInstance(anyOf, new PolicyMetaData());
result = myAnyOf.match(context);
Assert.assertEquals("AllOf algorithm failed when looking for MATCH", MatchResult.MATCH, result.getResult());
LOGGER.info("AllOf algorithm: OK");
}
/**
* Testing the Match algorithm
* @throws ParsingException
*/
@Test
public final void testMatchMatch() throws ParsingException {
LOGGER.info("Testing Match algorithm");
AnyOf anyOf = ruleNoMatch.getTarget().getAnyOves().get(MULTI_MATCH);
MatchResult result = null;
com.sun.xacml.xacmlv3.AnyOf myAnyOf = com.sun.xacml.xacmlv3.AnyOf.getInstance(anyOf, new PolicyMetaData());
result = myAnyOf.match(context);
Assert.assertEquals("Match algorithm failed when looking for NO_MATCH", MatchResult.NO_MATCH, result.getResult());
anyOf = ruleMatch.getTarget().getAnyOves().get(MULTI_MATCH);
myAnyOf = com.sun.xacml.xacmlv3.AnyOf.getInstance(anyOf, new PolicyMetaData());
result = myAnyOf.match(context);
Assert.assertEquals("Match algorithm failed when looking for MATCH", MatchResult.MATCH, result.getResult());
LOGGER.info("Match algorithm: OK");
}
/**
* Testing the AnyOf algorithm
* @throws ParsingException
*/
@Test
public final void testMatchAnyOf() throws ParsingException {
LOGGER.info("Testing AnyOf algorithm");
AnyOf anyOf = ruleNoMatch.getTarget().getAnyOves().get(MULTI_ALL_OF);
MatchResult result = null;
com.sun.xacml.xacmlv3.AnyOf myAnyOf = com.sun.xacml.xacmlv3.AnyOf.getInstance(anyOf, new PolicyMetaData());
result = myAnyOf.match(context);
Assert.assertEquals("AnyOf algorithm failed when looking for MATCH", MatchResult.NO_MATCH, result.getResult());
anyOf = ruleMatch.getTarget().getAnyOves().get(MULTI_ALL_OF);
myAnyOf = com.sun.xacml.xacmlv3.AnyOf.getInstance(anyOf, new PolicyMetaData());
result = myAnyOf.match(context);
Assert.assertEquals("AnyOf algorithm failed when looking for MATCH", MatchResult.MATCH, result.getResult());
LOGGER.info("AnyOf algorithm: OK");
}
}
\ No newline at end of file
......@@ -20,6 +20,11 @@ import com.sun.xacml.attr.StringAttribute;
import com.sun.xacml.cond.MatchFunction;
import com.thalesgroup.authzforce.pdp.core.test.utils.TestUtils;
/**
*
* @author romain.ferrari[AT]thalesgroup.com
*
*/
public class TestMatchFunction {
private static final Logger LOGGER = LoggerFactory.getLogger(TestMatchFunction.class);
......@@ -123,19 +128,12 @@ public class TestMatchFunction {
testFunctions.add(NAME_STRING_ENDS_WITH);
testFunctions.add(NAME_STRING_CONTAINS);
LOGGER.debug("Function to be tested");
LOGGER.debug(NAME_REGEXP_STRING_MATCH);
LOGGER.debug(NAME_X500NAME_MATCH);
LOGGER.debug(NAME_RFC822NAME_MATCH);
LOGGER.debug(NAME_STRING_REGEXP_MATCH);
LOGGER.debug(NAME_ANYURI_REGEXP_MATCH);
LOGGER.debug(NAME_IPADDRESS_REGEXP_MATCH);
LOGGER.debug(NAME_DNSNAME_REGEXP_MATCH);
LOGGER.debug(NAME_RFC822NAME_REGEXP_MATCH);
LOGGER.debug(NAME_X500NAME_REGEXP_MATCH);
LOGGER.debug(NAME_STRING_STARTS_WITH);
LOGGER.debug(NAME_STRING_ENDS_WITH);
LOGGER.debug(NAME_STRING_CONTAINS);
if(LOGGER.isDebugEnabled()) {
LOGGER.debug("Function to be tested");
for (String functionToBeTested : testFunctions) {
LOGGER.debug(functionToBeTested);
}
}
}
@Test
......
/**
*
*/
package com.sun.xacml.xacmlv3.function;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.Request;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sun.xacml.EvaluationCtx;
import com.sun.xacml.attr.BooleanAttribute;
import com.sun.xacml.attr.StringAttribute;
import com.sun.xacml.attr.xacmlv3.AttributeValue;
import com.sun.xacml.cond.StringFunction;
import com.sun.xacml.cond.xacmlv3.EvaluationResult;
import com.sun.xacml.ctx.Status;
import com.thalesgroup.authzforce.pdp.core.test.utils.TestUtils;
/**
* @author romain.ferrari[AT]thalesgroup.com
*
*/
public class TestStringFunction {
private static final Logger LOGGER = LoggerFactory
.getLogger(TestStringFunction.class);
private static final String FUNCTION_NS_2 = "urn:oasis:names:tc:xacml:2.0:function:";
private static final String FUNCTION_NS_3 = "urn:oasis:names:tc:xacml:3.0:function:";
/**
* Standard identifier for the string-concatenate function.
*/
public static final String NAME_STRING_CONCATENATE =
FUNCTION_NS_2 + "string-concatenate";
/**
* Standard identifier for the string-concatenate function.
*/
public static final String NAME_BOOLEAN_FROM_STRING =
FUNCTION_NS_3 + "boolean-from-string";
private static final EvaluationCtx globalContext = TestUtils
.createContext(new Request());
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
LOGGER.info("Begining testing for String Functions");
Set<String> testFunctions = new HashSet<String>();
testFunctions.add(NAME_STRING_CONCATENATE);
testFunctions.add(NAME_BOOLEAN_FROM_STRING);
if(LOGGER.isDebugEnabled()) {
LOGGER.debug("Function to be tested");
for (String functionToBeTested : testFunctions) {
LOGGER.debug(functionToBeTested);
}
}
}
@Test
public final void testNameStringConcatenate() {
LOGGER.info("Testing function: " + NAME_STRING_CONCATENATE);
StringFunction testNameStringConcatenate = new StringFunction(NAME_STRING_CONCATENATE);
List<AttributeValue> inputs = new ArrayList<AttributeValue>(Arrays.asList(
StringAttribute.getInstance("This "),
StringAttribute.getInstance("is "),
StringAttribute.getInstance("my "),
StringAttribute.getInstance("test !")));
StringAttribute expected = new StringAttribute("This is my test !");
Assert.assertEquals(expected, testNameStringConcatenate.evaluate(inputs, globalContext).getAttributeValue());
LOGGER.info("Function: " + NAME_STRING_CONCATENATE + ": OK");
}
@Test
public final void testNameBooleanFromString() {
LOGGER.info("Testing function: " + NAME_BOOLEAN_FROM_STRING);
StringFunction testNameBooleanFromString = new StringFunction(NAME_BOOLEAN_FROM_STRING);
List<AttributeValue> inputTrue = new ArrayList<AttributeValue>(Arrays.asList(StringAttribute.getInstance("true")));
List<AttributeValue> inputFalse = new ArrayList<AttributeValue>(Arrays.asList(StringAttribute.getInstance("false")));
List<AttributeValue> inputError = new ArrayList<AttributeValue>(Arrays.asList(StringAttribute.getInstance("error")));
Assert.assertTrue(Boolean.parseBoolean(((BooleanAttribute)testNameBooleanFromString.evaluate(inputTrue, globalContext).getAttributeValue()).encode()));
Assert.assertFalse(Boolean.parseBoolean(((BooleanAttribute)testNameBooleanFromString.evaluate(inputFalse, globalContext).getAttributeValue()).encode()));
EvaluationResult errorResult = testNameBooleanFromString.evaluate(inputError, globalContext);
Assert.assertEquals(Status.STATUS_SYNTAX_ERROR, errorResult.getStatus().getCode().get(0));
LOGGER.info("Function: " + NAME_BOOLEAN_FROM_STRING + ": OK");
}
}
......@@ -8,8 +8,9 @@ import org.junit.runners.Suite.SuiteClasses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sun.xacml.xacmlv3.MatchTest;
import com.sun.xacml.xacmlv3.TestMatchAlg;
import com.sun.xacml.xacmlv3.function.TestMatchFunction;
import com.sun.xacml.xacmlv3.function.TestStringFunction;
/**
* @author Romain Ferrari
......@@ -26,8 +27,9 @@ import com.sun.xacml.xacmlv3.function.TestMatchFunction;
*/
@RunWith(Suite.class)
@SuiteClasses(value={
MatchTest.class,
TestMatchAlg.class,
TestMatchFunction.class,
TestStringFunction.class,
ConformanceV3.class,
BasicV3_1.class,
BasicV3_2.class,
......
......@@ -39,42 +39,13 @@
<!-- /AUDIT LOGGER -->
<!-- Unitary Test Loggers -->
<logger name="com.sun.xacml.Test_PDP" additivity="false">
<level value="info" />
<appender-ref ref="stdout" />
</logger>
<logger name="com.thalesgroup.authzforce.pdp.core.test.impl.BasicFunctionV3" additivity="false">
<level value="info" />
<appender-ref ref="stdout" />
</logger>
<logger
name="com.thalesgroup.authzforce.pdp.core.test.impl.BasicMultipleRequestV3" additivity="false">
<level value="info" />
<appender-ref ref="stdout" />
</logger>
<logger name="com.thalesgroup.authzforce.pdp.core.test.impl.BasicV3" additivity="false">
<level value="info" />
<appender-ref ref="stdout" />
</logger>
<logger name="com.thalesgroup.authzforce.pdp.core.test.impl.ConformanceV3" additivity="false">
<level value="info" />
<appender-ref ref="stdout" />
</logger>
<logger name="com.thalesgroup.authzforce.pdp.core.test.utils.TestUtils" additivity="false">
<level value="info" />
<logger name="com.thalesgroup.authzforce.pdp.core.test" additivity="false">
<level value="DEBUG" />
<appender-ref ref="stdout" />
</logger>
<logger name="com.thalesgroup.authzforce.pdp.core.test.impl.MainTest" additivity="false">
<level value="info" />
<appender-ref ref="stdout" />
</logger>
</logger>
<!-- Unitary Test Loggers -->
<logger name="com.sun.xacml.cond.BaseFunctionFactory" additivity="false">
<level value="info" />
<appender-ref ref="stdout" />
</logger>
<logger name="com.sun.xacml.cond.StandardFunctionFactory" additivity="false">
<logger name="com.sun.xacml.cond" additivity="false">
<level value="info" />
<appender-ref ref="stdout" />
</logger>
......
......@@ -91,30 +91,12 @@
</logger>
<!-- /AUDIT LOGGER -->
<logger name="com.sun.xacml.cond.BaseFunctionFactory" additivity="false" level="INFO">
<logger name="com.sun.xacml.cond" additivity="false" level="DEBUG">
<appender-ref ref="stdout" />
</logger>
<logger name="com.sun.xacml.cond.StandardFunctionFactory" additivity="false" level="INFO">
<logger name="com.thalesgroup.authzforce.pdp.core.test" additivity="false" level="DEBUG">
<appender-ref ref="stdout" />
</logger>
<logger name="com.thalesgroup.authzforce.pdp.core.test.impl.BasicFunctionV3" additivity="false" level="INFO">
<appender-ref ref="stdout" />
</logger>
<logger name="com.thalesgroup.authzforce.pdp.core.test.impl.BasicMultipleRequestV3" additivity="false" level="INFO">
<appender-ref ref="stdout" />
</logger>
<logger name="com.thalesgroup.authzforce.pdp.core.test.impl.BasicV3" additivity="false" level="INFO">
<appender-ref ref="stdout" />
</logger>
<logger name="com.thalesgroup.authzforce.pdp.core.test.impl.ConformanceV3" additivity="false" level="INFO">
<appender-ref ref="stdout" />
</logger>
<logger name="com.thalesgroup.authzforce.pdp.core.test.utils.TestUtils" additivity="false" level="INFO">
<appender-ref ref="stdout" />
</logger>
<logger name="com.thalesgroup.authzforce.pdp.core.test.impl.MainTest" additivity="false" level="INFO">
<appender-ref ref="stdout" />
</logger>
</logger>
<logger name="net.sf.ehcache" additivity="false" level="WARN">
<appender-ref ref="stdout" />
</logger>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment