MyAttributeValueFactory { public static boolean evaulateFunction(URI function, List<MyAttributeValue> arguments) { String returnType = returnTypeOfFunction(function); if(!checkTypes(arguments, returnType)) { throw new RuntimeException("Incorrect type of arguments, when evaluating function: '" + function + "'"); } OperationType op = operationTypeOfFunction(function); if (!isArgumentNumberCorrect(op, arguments.size())) { throw new RuntimeException("Incorrect number of arguments when evaluating function: '" + function + "'"); } switch (op) { case EQUAL: return arguments.get(0).isCompatibleAndEquals(arguments.get(1)); case NOTEQUAL: return arguments.get(0).isCompatibleAndNotEquals(arguments.get(1)); case EQUALONEOF: { MyAttributeValue first = arguments.get(0); for (int i = 1; i < arguments.size(); ++i) { if (first.isCompatibleAndEquals(arguments.get(i))) { return true; } } return false; } case LESS: return arguments.get(0).isCompatibleAndLess(arguments.get(1)); case LESSEQ: return arguments.get(0).isCompatibleAndLessOrEqual(arguments.get(1)); case GREATER: return arguments.get(1).isCompatibleAndLess(arguments.get(0)); case GREATEREQ: return arguments.get(1).isCompatibleAndLessOrEqual(arguments.get(0)); default: throw new RuntimeException("Problem with evaluating function: '" + function + "'"); } }  static MyAttributeValue parseValue(URI dataType, Object attributeValue, /*Nullable*/ EnumAllowedValues av); static OperationType operationTypeOfFunction(URI functionAsUri); static String returnTypeOfFunction(URI functionAsUri); static MyAttributeValue parseValueFromFunction(URI function, Object param); static boolean evaulateFunction(URI function, List<MyAttributeValue> arguments);  }
@Test public void testMismatchedTypes() throws Exception { List<MyAttributeValue> arguments = new ArrayList<MyAttributeValue>(); arguments.clear(); arguments.add(new MyAttributeValueString("hello", null)); arguments.add(new MyAttributeValueUri("hello", null)); try { evaulateFunction(stringEqualFunction, arguments); fail("Expected an exception"); } catch (Exception ex) { assertTrue(ex.getMessage().contains("Incorrect type of arguments")); } }
@Test public void testEncodings() throws Exception { final String HELLO = "hello"; List<MyAttributeValue> arguments = new ArrayList<MyAttributeValue>(); arguments.clear(); arguments.add(new MyEncodingStringSha256(HELLO, null)); arguments.add(new MyEncodingStringUtf8(HELLO, null)); assertFalse(evaulateFunction(stringEqualFunction, arguments)); arguments.clear(); arguments.add(new MyEncodingStringUtf8(HELLO, null)); arguments.add(new MyEncodingStringUtf8(HELLO, null)); assertTrue(evaulateFunction(stringEqualFunction, arguments)); arguments.clear(); arguments.add(new MyEncodingStringSha256(HELLO, null)); arguments.add(new MyEncodingStringSha256(HELLO, null)); assertTrue(evaulateFunction(stringEqualFunction, arguments)); arguments.clear(); arguments.add(new MyAttributeValueString(HELLO, null)); arguments.add(new MyEncodingStringSha256(HELLO, null)); assertTrue(evaulateFunction(stringEqualFunction, arguments)); }
@Test public void testIncorrectNumberOfArguments() throws Exception { List<MyAttributeValue> arguments = new ArrayList<MyAttributeValue>(); arguments.clear(); arguments.add(new MyAttributeValueString("hello", null)); arguments.add(new MyAttributeValueString("hello", null)); arguments.add(new MyAttributeValueString("hello", null)); try { evaulateFunction(stringEqualFunction, arguments); fail("Expected an exception"); } catch (Exception ex) { assertTrue(ex.getMessage().contains("Incorrect number of arguments")); } arguments.clear(); arguments.add(new MyAttributeValueString("hello", null)); try { evaulateFunction(stringEqualFunction, arguments); fail("Expected an exception"); } catch (Exception ex) { assertTrue(ex.getMessage().contains("Incorrect number of arguments")); } arguments.clear(); try { evaulateFunction(stringEqualFunction, arguments); fail("Expected an exception"); } catch (Exception ex) { assertTrue(ex.getMessage().contains("Incorrect number of arguments")); } }
@Test public void testEqualsOneOf() throws Exception { List<MyAttributeValue> arguments = new ArrayList<MyAttributeValue>(); arguments.clear(); arguments.add(new MyAttributeValueString("arg0", null)); arguments.add(new MyAttributeValueString("arg0", null)); arguments.add(new MyAttributeValueString("arg1", null)); arguments.add(new MyAttributeValueString("arg2", null)); arguments.add(new MyAttributeValueString("arg3", null)); assertTrue(evaulateFunction(stringEqualOneof, arguments)); arguments.set(0, new MyAttributeValueString("arg2", null)); assertTrue(evaulateFunction(stringEqualOneof, arguments)); arguments.set(0, new MyAttributeValueString("arg3", null)); assertTrue(evaulateFunction(stringEqualOneof, arguments)); arguments.set(0, new MyAttributeValueString("hello", null)); assertFalse(evaulateFunction(stringEqualOneof, arguments)); arguments.clear(); arguments.add(new MyAttributeValueString("arg0", null)); arguments.add(new MyAttributeValueString("arg1", null)); arguments.add(new MyAttributeValueString("arg1", null)); arguments.add(new MyAttributeValueString("arg1", null)); assertFalse(evaulateFunction(stringEqualOneof, arguments)); }
@Test public void testEqualsForAllTypes() throws Exception { List<MyAttributeValue> arguments = new ArrayList<MyAttributeValue>(); arguments.clear(); arguments.add(new MyAttributeValueString("hello", null)); arguments.add(new MyAttributeValueString("hello", null)); assertTrue(evaulateFunction(new URI("urn:oasis:names:tc:xacml:1.0:function:string-equal"), arguments)); assertFalse(evaulateFunction(new URI("urn:abc4trust:1.0:function:string-not-equal"), arguments)); arguments.clear(); arguments.add(new MyAttributeValueBoolean(Boolean.valueOf(true), null)); arguments.add(new MyAttributeValueBoolean(true, null)); assertTrue(evaulateFunction(new URI("urn:oasis:names:tc:xacml:1.0:function:boolean-equal"), arguments)); assertFalse(evaulateFunction(new URI("urn:abc4trust:1.0:function:boolean-not-equal"), arguments)); arguments.clear(); arguments.add(new MyAttributeValueInteger(Integer.valueOf(42), null)); arguments.add(new MyAttributeValueInteger(BigInteger.valueOf(42), null)); assertTrue(evaulateFunction(new URI("urn:oasis:names:tc:xacml:1.0:function:integer-equal"), arguments)); assertFalse(evaulateFunction(new URI("urn:abc4trust:1.0:function:integer-not-equal"), arguments)); arguments.clear(); arguments.add(new MyAttributeValueDate("2011-12-04Z", null)); arguments.add(new MyAttributeValueDate("2011-12-04Z", null)); assertTrue(evaulateFunction(new URI("urn:oasis:names:tc:xacml:1.0:function:date-equal"), arguments)); assertFalse(evaulateFunction(new URI("urn:abc4trust:1.0:function:date-not-equal"), arguments)); arguments.clear(); arguments.add(new MyAttributeValueDate("2011-12-04", null)); arguments.add(new MyAttributeValueDate("2011-12-04", null)); assertTrue(evaulateFunction(new URI("urn:oasis:names:tc:xacml:1.0:function:date-equal"), arguments)); arguments.clear(); arguments.add(new MyAttributeValueDate("2011-12-04Z", null)); arguments.add(new MyAttributeValueDate("2011-12-04", null)); assertTrue(evaulateFunction(new URI("urn:oasis:names:tc:xacml:1.0:function:date-equal"), arguments)); arguments.clear(); arguments.add(new MyAttributeValueTime("17:39:42", null)); arguments.add(new MyAttributeValueTime("17:39:42", null)); assertTrue(evaulateFunction(new URI("urn:oasis:names:tc:xacml:1.0:function:time-equal"), arguments)); arguments.clear(); arguments.add(new MyAttributeValueDateTime("2011-12-04T17:42:42Z", null)); arguments.add(new MyAttributeValueDateTime("2011-12-04T17:42:42Z", null)); assertTrue(evaulateFunction(new URI("urn:oasis:names:tc:xacml:1.0:function:dateTime-equal"), arguments)); arguments.clear(); arguments.add(new MyAttributeValueDateTime("2011-12-04T17:43:14", null)); arguments.add(new MyAttributeValueDateTime("2011-12-04T17:43:14", null)); assertTrue(evaulateFunction(new URI("urn:oasis:names:tc:xacml:1.0:function:dateTime-equal"), arguments)); assertFalse(evaulateFunction(new URI("urn:abc4trust:1.0:function:dateTime-not-equal"), arguments)); arguments.clear(); arguments.add(new MyAttributeValueDateTime("2011-12-04T17:43:14Z", null)); arguments.add(new MyAttributeValueDateTime("2011-12-04T17:43:14", null)); assertTrue(evaulateFunction(new URI("urn:oasis:names:tc:xacml:1.0:function:dateTime-equal"), arguments)); arguments.clear(); arguments.add(new MyAttributeValueDateTime("2011-12-04T19:44:15+02:00", null)); arguments.add(new MyAttributeValueDateTime("2011-12-04T11:44:15-06:00", null)); assertTrue(evaulateFunction(new URI("urn:oasis:names:tc:xacml:1.0:function:dateTime-equal"), arguments)); arguments.clear(); arguments.add(new MyAttributeValueUri(new URI("abc4trust.eu/blabla"), null)); arguments.add(new MyAttributeValueUri(new URI("abc4trust.eu/blabla"), null)); assertTrue(evaulateFunction(new URI("urn:oasis:names:tc:xacml:1.0:function:anyURI-equal"), arguments)); assertFalse(evaulateFunction(new URI("urn:abc4trust:1.0:function:anyURI-not-equal"), arguments)); }
@Test public void testUnequalsForAllTypes() throws Exception { List<MyAttributeValue> arguments = new ArrayList<MyAttributeValue>(); arguments.clear(); arguments.add(new MyAttributeValueString("hello", null)); arguments.add(new MyAttributeValueString("world", null)); assertFalse(evaulateFunction(new URI("urn:oasis:names:tc:xacml:1.0:function:string-equal"), arguments)); assertTrue(evaulateFunction(new URI("urn:abc4trust:1.0:function:string-not-equal"), arguments)); arguments.clear(); arguments.add(new MyAttributeValueBoolean(Boolean.valueOf(true), null)); arguments.add(new MyAttributeValueBoolean(false, null)); assertFalse(evaulateFunction(new URI("urn:oasis:names:tc:xacml:1.0:function:boolean-equal"), arguments)); assertTrue(evaulateFunction(new URI("urn:abc4trust:1.0:function:boolean-not-equal"), arguments)); arguments.clear(); arguments.add(new MyAttributeValueInteger(6 * 9, null)); arguments.add(new MyAttributeValueInteger(BigInteger.valueOf(42), null)); assertFalse(evaulateFunction(new URI("urn:oasis:names:tc:xacml:1.0:function:integer-equal"), arguments)); assertTrue(evaulateFunction(new URI("urn:abc4trust:1.0:function:integer-not-equal"), arguments)); arguments.clear(); arguments.add(new MyAttributeValueDate("2011-12-04Z", null)); arguments.add(new MyAttributeValueDate("2011-12-05Z", null)); assertFalse(evaulateFunction(new URI("urn:oasis:names:tc:xacml:1.0:function:date-equal"), arguments)); assertTrue(evaulateFunction(new URI("urn:abc4trust:1.0:function:date-not-equal"), arguments)); arguments.clear(); arguments.add(new MyAttributeValueDate("2011-12-04", null)); arguments.add(new MyAttributeValueDate("2011-12-05", null)); assertFalse(evaulateFunction(new URI("urn:oasis:names:tc:xacml:1.0:function:date-equal"), arguments)); arguments.clear(); arguments.add(new MyAttributeValueDate("2011-12-04Z", null)); arguments.add(new MyAttributeValueDate("2011-12-05", null)); assertFalse(evaulateFunction(new URI("urn:oasis:names:tc:xacml:1.0:function:date-equal"), arguments)); arguments.clear(); arguments.add(new MyAttributeValueTime("17:39:42", null)); arguments.add(new MyAttributeValueTime("17:31:42", null)); assertFalse(evaulateFunction(new URI("urn:oasis:names:tc:xacml:1.0:function:time-equal"), arguments)); arguments.clear(); arguments.add(new MyAttributeValueDateTime("2011-12-04T17:42:42Z", null)); arguments.add(new MyAttributeValueDateTime("2011-12-04T17:42:41Z", null)); assertFalse(evaulateFunction(new URI("urn:oasis:names:tc:xacml:1.0:function:dateTime-equal"), arguments)); arguments.clear(); arguments.add(new MyAttributeValueDateTime("2011-12-04T17:43:14", null)); arguments.add(new MyAttributeValueDateTime("2011-12-05T17:43:14", null)); assertFalse(evaulateFunction(new URI("urn:oasis:names:tc:xacml:1.0:function:dateTime-equal"), arguments)); assertTrue(evaulateFunction(new URI("urn:abc4trust:1.0:function:dateTime-not-equal"), arguments)); arguments.clear(); arguments.add(new MyAttributeValueDateTime("2011-12-04T17:43:14Z", null)); arguments.add(new MyAttributeValueDateTime("2011-12-04T17:43:15", null)); assertFalse(evaulateFunction(new URI("urn:oasis:names:tc:xacml:1.0:function:dateTime-equal"), arguments)); arguments.clear(); arguments.add(new MyAttributeValueDateTime("2011-12-04T19:44:15+02:00", null)); arguments.add(new MyAttributeValueDateTime("2011-12-04T11:44:15-07:00", null)); assertFalse(evaulateFunction(new URI("urn:oasis:names:tc:xacml:1.0:function:dateTime-equal"), arguments)); arguments.clear(); arguments.add(new MyAttributeValueUri(new URI("abc4trust.eu/hello"), null)); arguments.add(new MyAttributeValueUri(new URI("abc4trust.eu/world"), null)); assertFalse(evaulateFunction(new URI("urn:oasis:names:tc:xacml:1.0:function:anyURI-equal"), arguments)); assertTrue(evaulateFunction(new URI("urn:abc4trust:1.0:function:anyURI-not-equal"), arguments)); }
@Test public void testComparisons() throws Exception { List<MyAttributeValue> arguments = new ArrayList<MyAttributeValue>(); arguments.clear(); arguments.add(new MyAttributeValueInteger(4, null)); arguments.add(new MyAttributeValueInteger(7, null)); assertFalse(evaulateFunction(new URI( "urn:oasis:names:tc:xacml:1.0:function:integer-greater-than"), arguments)); assertFalse(evaulateFunction(new URI( "urn:oasis:names:tc:xacml:1.0:function:integer-greater-than-or-equal"), arguments)); assertTrue(evaulateFunction(new URI("urn:oasis:names:tc:xacml:1.0:function:integer-less-than"), arguments)); assertTrue(evaulateFunction(new URI( "urn:oasis:names:tc:xacml:1.0:function:integer-less-than-or-equal"), arguments)); arguments.clear(); arguments.add(new MyAttributeValueInteger(42, null)); arguments.add(new MyAttributeValueInteger(42, null)); assertFalse(evaulateFunction(new URI( "urn:oasis:names:tc:xacml:1.0:function:integer-greater-than"), arguments)); assertTrue(evaulateFunction(new URI( "urn:oasis:names:tc:xacml:1.0:function:integer-greater-than-or-equal"), arguments)); assertFalse(evaulateFunction( new URI("urn:oasis:names:tc:xacml:1.0:function:integer-less-than"), arguments)); assertTrue(evaulateFunction(new URI( "urn:oasis:names:tc:xacml:1.0:function:integer-less-than-or-equal"), arguments)); arguments.clear(); arguments.add(new MyAttributeValueInteger(142, null)); arguments.add(new MyAttributeValueInteger(42, null)); assertTrue(evaulateFunction(new URI( "urn:oasis:names:tc:xacml:1.0:function:integer-greater-than"), arguments)); assertTrue(evaulateFunction(new URI( "urn:oasis:names:tc:xacml:1.0:function:integer-greater-than-or-equal"), arguments)); assertFalse(evaulateFunction( new URI("urn:oasis:names:tc:xacml:1.0:function:integer-less-than"), arguments)); assertFalse(evaulateFunction(new URI( "urn:oasis:names:tc:xacml:1.0:function:integer-less-than-or-equal"), arguments)); arguments.clear(); arguments.add(new MyAttributeValueDate("2010-10-10", null)); arguments.add(new MyAttributeValueDate("2011-11-11", null)); assertFalse(evaulateFunction( new URI("urn:oasis:names:tc:xacml:1.0:function:date-greater-than"), arguments)); assertFalse(evaulateFunction(new URI( "urn:oasis:names:tc:xacml:1.0:function:date-greater-than-or-equal"), arguments)); assertTrue(evaulateFunction(new URI("urn:oasis:names:tc:xacml:1.0:function:date-less-than"), arguments)); assertTrue(evaulateFunction(new URI( "urn:oasis:names:tc:xacml:1.0:function:date-less-than-or-equal"), arguments)); arguments.clear(); arguments.add(new MyAttributeValueDate("2010-01-01", null)); arguments.add(new MyAttributeValueDate("2010-01-01", null)); assertFalse(evaulateFunction( new URI("urn:oasis:names:tc:xacml:1.0:function:date-greater-than"), arguments)); assertTrue(evaulateFunction(new URI( "urn:oasis:names:tc:xacml:1.0:function:date-greater-than-or-equal"), arguments)); assertFalse(evaulateFunction(new URI("urn:oasis:names:tc:xacml:1.0:function:date-less-than"), arguments)); assertTrue(evaulateFunction(new URI( "urn:oasis:names:tc:xacml:1.0:function:date-less-than-or-equal"), arguments)); arguments.clear(); arguments.add(new MyAttributeValueDate("2011-11-11", null)); arguments.add(new MyAttributeValueDate("2010-10-10", null)); assertTrue(evaulateFunction(new URI("urn:oasis:names:tc:xacml:1.0:function:date-greater-than"), arguments)); assertTrue(evaulateFunction(new URI( "urn:oasis:names:tc:xacml:1.0:function:date-greater-than-or-equal"), arguments)); assertFalse(evaulateFunction(new URI("urn:oasis:names:tc:xacml:1.0:function:date-less-than"), arguments)); assertFalse(evaulateFunction(new URI( "urn:oasis:names:tc:xacml:1.0:function:date-less-than-or-equal"), arguments)); arguments.clear(); arguments.add(new MyAttributeValueDateTime("2010-10-10T10:10:10Z", null)); arguments.add(new MyAttributeValueDateTime("2011-11-11T10:10:10Z", null)); assertFalse(evaulateFunction( new URI("urn:oasis:names:tc:xacml:1.0:function:dateTime-greater-than"), arguments)); assertFalse(evaulateFunction(new URI( "urn:oasis:names:tc:xacml:1.0:function:dateTime-greater-than-or-equal"), arguments)); assertTrue(evaulateFunction(new URI("urn:oasis:names:tc:xacml:1.0:function:dateTime-less-than"), arguments)); assertTrue(evaulateFunction(new URI( "urn:oasis:names:tc:xacml:1.0:function:dateTime-less-than-or-equal"), arguments)); arguments.clear(); arguments.add(new MyAttributeValueDateTime("2010-01-01T10:10:10Z", null)); arguments.add(new MyAttributeValueDateTime("2010-01-01T10:10:10Z", null)); assertFalse(evaulateFunction( new URI("urn:oasis:names:tc:xacml:1.0:function:dateTime-greater-than"), arguments)); assertTrue(evaulateFunction(new URI( "urn:oasis:names:tc:xacml:1.0:function:dateTime-greater-than-or-equal"), arguments)); assertFalse(evaulateFunction(new URI("urn:oasis:names:tc:xacml:1.0:function:dateTime-less-than"), arguments)); assertTrue(evaulateFunction(new URI( "urn:oasis:names:tc:xacml:1.0:function:dateTime-less-than-or-equal"), arguments)); arguments.clear(); arguments.add(new MyAttributeValueDateTime("2011-11-11T10:10:10Z", null)); arguments.add(new MyAttributeValueDateTime("2010-10-10T10:10:10Z", null)); assertTrue(evaulateFunction(new URI("urn:oasis:names:tc:xacml:1.0:function:dateTime-greater-than"), arguments)); assertTrue(evaulateFunction(new URI( "urn:oasis:names:tc:xacml:1.0:function:dateTime-greater-than-or-equal"), arguments)); assertFalse(evaulateFunction(new URI("urn:oasis:names:tc:xacml:1.0:function:dateTime-less-than"), arguments)); assertFalse(evaulateFunction(new URI( "urn:oasis:names:tc:xacml:1.0:function:dateTime-less-than-or-equal"), arguments)); }