Commit db46abbf authored by cdanger's avatar cdanger

Added unit tests for marshalling/unmarshalling AttributeValue from/to

XACML
parent f250aa16
......@@ -20,14 +20,14 @@ package org.ow2.authzforce.core.pdp.impl.test.value;
import java.util.Arrays;
import java.util.Collection;
import net.sf.saxon.lib.StandardURIChecker;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import net.sf.saxon.lib.StandardURIChecker;
/**
*
* XACML anyURI validation test. This test is no longer used since we refer to the definition of anyURI datatype given in XSD 1.1, which has the same value space as the string datatype. This is
......@@ -44,11 +44,11 @@ public class AnyURIAttributeTest
@Parameters
public static Collection<Object[]> data()
{
Object[][] data = new Object[][] { { "http://datypic.com", "absolute URI (also a URL)", true }, { "mailto:info@datypic.com", "absolute URI", true },
{ "../%C3%A9dition.html", "relative URI containing escaped non-ASCII character", true }, { "../édition.html", "relative URI containing escaped non-ASCII character", true },
{ "http://datypic.com/prod.html#shirt", "URI with fragment identifier", true }, { "../prod.html#shirt", "relative URI with fragment identifier", true },
{ "", "an empty value is allowed", true }, { "http://datypic.com#frag1#frag2", "too many # characters", false },
{ "http://datypic.com#f% rag", "% character followed by something other than two hexadecimal digits", false } };
final Object[][] data = new Object[][] { { "http://datypic.com", "absolute URI (also a URL)", true }, { "mailto:info@datypic.com", "absolute URI", true },
{ "../%C3%A9dition.html", "relative URI containing escaped non-ASCII character", true }, { "../édition.html", "relative URI containing escaped non-ASCII character", true },
{ "http://datypic.com/prod.html#shirt", "URI with fragment identifier", true }, { "../prod.html#shirt", "relative URI with fragment identifier", true },
{ "", "an empty value is allowed", true }, { "http://datypic.com#frag1#frag2", "too many # characters", false },
{ "http://datypic.com#f% rag", "% character followed by something other than two hexadecimal digits", false } };
return Arrays.asList(data);
}
......
......@@ -18,6 +18,8 @@
package org.ow2.authzforce.core.pdp.impl.test.value;
import java.io.Serializable;
import java.io.StringReader;
import java.io.StringWriter;
import java.math.BigInteger;
import java.net.URI;
import java.time.Instant;
......@@ -27,13 +29,19 @@ import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import javax.security.auth.x500.X500Principal;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.junit.Assert;
import org.junit.Test;
......@@ -42,9 +50,16 @@ import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.ow2.authzforce.core.pdp.api.value.AttributeBag;
import org.ow2.authzforce.core.pdp.api.value.AttributeValue;
import org.ow2.authzforce.core.pdp.api.value.AttributeValueFactory;
import org.ow2.authzforce.core.pdp.api.value.AttributeValueFactoryRegistry;
import org.ow2.authzforce.core.pdp.api.value.StandardDatatypes;
import org.ow2.authzforce.core.pdp.impl.value.StandardAttributeValueFactories;
import org.ow2.authzforce.xacml.Xacml3JaxbHelper;
import com.google.common.collect.HashMultiset;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.Attribute;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
/**
*
......@@ -72,6 +87,8 @@ public class StandardJavaTypeToXacmlAttributeDatatypeConversionTest
private static final AttributeValueFactoryRegistry ATT_VALUE_FACTORIES_WITH_LONG_INT_SUPPORT = StandardAttributeValueFactories.getRegistry(false, Optional.of(BigInteger.valueOf(Long.MAX_VALUE)));
private static final AttributeValueFactoryRegistry ATT_VALUE_FACTORIES_WITH_BIG_INT_SUPPORT = StandardAttributeValueFactories.getRegistry(false, Optional.of(INTEGER_GREATER_THAN_MAX_LONG));
private static volatile AtomicInteger testId = new AtomicInteger(0);
@Parameters(name = "{index}: {1} -> {2}")
public static Collection<Object[]> data()
{
......@@ -283,6 +300,31 @@ public class StandardJavaTypeToXacmlAttributeDatatypeConversionTest
{
final AttributeBag<?> attBag = attValFactories.newAttributeBag(rawValues);
Assert.assertEquals("Invalid datatype for created attribute values", attBag.getElementDatatype().getId(), expectedAttributeDatatypeId);
/*
* Marshall to XACML and try to unmarshall to original Java value to make sure marshalling is OK
*/
final Attribute xacmlAtt = new Attribute(new ArrayList<>(attBag.elements()), testId.toString(), null, false);
final Marshaller marshaller = Xacml3JaxbHelper.createXacml3Marshaller();
final StringWriter strWriter = new StringWriter();
marshaller.marshal(xacmlAtt, strWriter);
final String marshalledStr = strWriter.toString();
final Unmarshaller unmarshaller = Xacml3JaxbHelper.createXacml3Unmarshaller();
final Attribute unmarshalledXacmlAtt = (Attribute) unmarshaller.unmarshal(new StringReader(marshalledStr));
final List<AttributeValueType> xacmlAttVals = unmarshalledXacmlAtt.getAttributeValues();
if (xacmlAttVals.isEmpty())
{
Assert.fail("Marshalling/unmarshalling failed: no AttributeValue after unmarshalling: " + marshalledStr);
return;
}
final AttributeValueType xacmlAttVal0 = xacmlAttVals.get(0);
final AttributeValueFactory<?> attValFactory = this.attValFactories.getExtension(xacmlAttVal0.getDataType());
final List<AttributeValue> attVals = unmarshalledXacmlAtt.getAttributeValues().stream()
.map(inputXacmlAttValue -> attValFactory.getInstance(inputXacmlAttValue.getContent(), inputXacmlAttValue.getOtherAttributes(), null)).collect(Collectors.toList());
Assert.assertEquals("AttributeValues after unmarshalling do not match original AttributeValues before marshalling: " + marshalledStr, attBag.elements(), HashMultiset.create(attVals));
} catch (final Exception e)
{
Assert.assertTrue("Unexpected error: " + e, expectedExceptionClass != null && expectedExceptionClass.isInstance(e));
......
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