Search in sources :

Example 11 with OperationPolicyExpression

use of org.nhindirect.policy.OperationPolicyExpression in project nhin-d by DirectProject.

the class SimpleTextV1LexiconPolicyParser_buildExpressionTest method testBuildExpression_tbsFieldName_rdnAttribute_validatePolicyExpression.

public void testBuildExpression_tbsFieldName_rdnAttribute_validatePolicyExpression() throws Exception {
    final SimpleTextV1LexiconPolicyParser parser = new SimpleTextV1LexiconPolicyParser();
    final InputStream stream = FileUtils.openInputStream(new File("./src/test/resources/policies/literalWithSpaces.txt"));
    final Vector<SimpleTextV1LexiconPolicyParser.TokenTypeAssociation> tokens = parser.parseToTokens(stream);
    // now build expressions
    PolicyExpression expression = parser.buildExpression(tokens.iterator());
    // check that the expression is an equals
    assertNotNull(expression);
    assertEquals(PolicyExpressionType.OPERATION, expression.getExpressionType());
    OperationPolicyExpression operationExpression = (OperationPolicyExpression) expression;
    assertEquals(PolicyOperator.EQUALS, operationExpression.getPolicyOperator());
    // break down the sub operation parameters... should be a cert reference and a literal
    expression = operationExpression.getOperands().get(0);
    assertEquals(PolicyExpressionType.REFERENCE, expression.getExpressionType());
    assertTrue(expression instanceof SubjectAttributeField);
    expression = operationExpression.getOperands().get(1);
    assertEquals(PolicyExpressionType.LITERAL, expression.getExpressionType());
    assertEquals("United States", ((LiteralPolicyExpression<?>) expression).getPolicyValue().getPolicyValue());
}
Also used : LiteralPolicyExpression(org.nhindirect.policy.LiteralPolicyExpression) InputStream(java.io.InputStream) SubjectAttributeField(org.nhindirect.policy.x509.SubjectAttributeField) LiteralPolicyExpression(org.nhindirect.policy.LiteralPolicyExpression) PolicyExpression(org.nhindirect.policy.PolicyExpression) OperationPolicyExpression(org.nhindirect.policy.OperationPolicyExpression) File(java.io.File) OperationPolicyExpression(org.nhindirect.policy.OperationPolicyExpression)

Example 12 with OperationPolicyExpression

use of org.nhindirect.policy.OperationPolicyExpression in project nhin-d by DirectProject.

the class SimpleTextV1LexiconPolicyParser_buildExpressionTest method testBuildExpression_simpleExpression_validatePolicyExpression.

public void testBuildExpression_simpleExpression_validatePolicyExpression() throws Exception {
    final SimpleTextV1LexiconPolicyParser parser = new SimpleTextV1LexiconPolicyParser();
    final InputStream stream = FileUtils.openInputStream(new File("./src/test/resources/policies/simpleLexiconSamp1.txt"));
    final Vector<SimpleTextV1LexiconPolicyParser.TokenTypeAssociation> tokens = parser.parseToTokens(stream);
    // now build expressions
    PolicyExpression expression = parser.buildExpression(tokens.iterator());
    // check that the expression is a logical and
    assertNotNull(expression);
    assertEquals(PolicyExpressionType.OPERATION, expression.getExpressionType());
    OperationPolicyExpression operationExpression = (OperationPolicyExpression) expression;
    assertEquals(PolicyOperator.LOGICAL_AND, operationExpression.getPolicyOperator());
    // now break down the operands which should each be a operator expressions
    // operator 1
    // should be an equals operator expressions
    expression = operationExpression.getOperands().get(0);
    assertEquals(PolicyExpressionType.OPERATION, expression.getExpressionType());
    OperationPolicyExpression param1operationExpression = (OperationPolicyExpression) expression;
    assertEquals(PolicyOperator.EQUALS, param1operationExpression.getPolicyOperator());
    // break down the sub operation parameters... should be two literals
    expression = param1operationExpression.getOperands().get(0);
    assertEquals(PolicyExpressionType.LITERAL, expression.getExpressionType());
    assertEquals("1", ((LiteralPolicyExpression<?>) expression).getPolicyValue().getPolicyValue());
    expression = param1operationExpression.getOperands().get(1);
    assertEquals(PolicyExpressionType.LITERAL, expression.getExpressionType());
    assertEquals("2", ((LiteralPolicyExpression<?>) expression).getPolicyValue().getPolicyValue());
    // operator 2
    // should be an equals operator expressions
    expression = operationExpression.getOperands().get(1);
    assertEquals(PolicyExpressionType.OPERATION, expression.getExpressionType());
    OperationPolicyExpression param2operationExpression = (OperationPolicyExpression) expression;
    assertEquals(PolicyOperator.NOT_EQUALS, param2operationExpression.getPolicyOperator());
    // break down the sub operation parameters... should be two literals
    expression = param2operationExpression.getOperands().get(0);
    assertEquals(PolicyExpressionType.LITERAL, expression.getExpressionType());
    assertEquals("2", ((LiteralPolicyExpression<?>) expression).getPolicyValue().getPolicyValue());
    expression = param2operationExpression.getOperands().get(1);
    assertEquals(PolicyExpressionType.LITERAL, expression.getExpressionType());
    assertEquals("1", ((LiteralPolicyExpression<?>) expression).getPolicyValue().getPolicyValue());
}
Also used : LiteralPolicyExpression(org.nhindirect.policy.LiteralPolicyExpression) InputStream(java.io.InputStream) LiteralPolicyExpression(org.nhindirect.policy.LiteralPolicyExpression) PolicyExpression(org.nhindirect.policy.PolicyExpression) OperationPolicyExpression(org.nhindirect.policy.OperationPolicyExpression) File(java.io.File) OperationPolicyExpression(org.nhindirect.policy.OperationPolicyExpression)

Example 13 with OperationPolicyExpression

use of org.nhindirect.policy.OperationPolicyExpression in project nhin-d by DirectProject.

the class XMLLexiconPolicyParser_serializeTest method testSerialize_complexExpression_validateExpression.

public void testSerialize_complexExpression_validateExpression() throws Exception {
    final XMLLexiconPolicyParser parser = new XMLLexiconPolicyParser();
    // build the expression
    final Integer keyUsage = KeyUsage.keyEncipherment;
    final PolicyValue<Integer> op1 = PolicyValueFactory.getInstance(keyUsage);
    final LiteralPolicyExpression<Integer> expr1 = LiteralPolicyExpressionFactory.getInstance(op1);
    final KeyUsageExtensionField expr2 = new KeyUsageExtensionField(true);
    final Vector<PolicyExpression> operands1 = new Vector<PolicyExpression>();
    operands1.add(expr1);
    operands1.add(expr2);
    final OperationPolicyExpression oper1 = OperationPolicyExpressionFactory.getInstance(PolicyOperator.BITWISE_AND, operands1);
    // build outer expression embedding the first operation as a parameter
    final PolicyValue<Integer> op3 = PolicyValueFactory.getInstance(0);
    final LiteralPolicyExpression<Integer> expr3 = LiteralPolicyExpressionFactory.getInstance(op3);
    final Vector<PolicyExpression> operands2 = new Vector<PolicyExpression>();
    operands2.add(oper1);
    operands2.add(expr3);
    final OperationPolicyExpression oper2 = OperationPolicyExpressionFactory.getInstance(PolicyOperator.GREATER, operands2);
    // build a separate expression for extended key usage
    final ExtendedKeyUsageExtensionField expr4 = new ExtendedKeyUsageExtensionField(true);
    final PolicyValue<String> op5 = PolicyValueFactory.getInstance(ExtendedKeyUsageIdentifier.ID_KP_EMAIL_PROTECTION.getId());
    final LiteralPolicyExpression<String> expr5 = LiteralPolicyExpressionFactory.getInstance(op5);
    final Vector<PolicyExpression> operands3 = new Vector<PolicyExpression>();
    operands3.add(expr4);
    operands3.add(expr5);
    final OperationPolicyExpression oper3 = OperationPolicyExpressionFactory.getInstance(PolicyOperator.CONTAINS, operands3);
    // build an and operator and make sure the cert has all policies met
    final Vector<PolicyExpression> operands4 = new Vector<PolicyExpression>();
    operands4.add(oper2);
    operands4.add(oper3);
    final OperationPolicyExpression oper4 = OperationPolicyExpressionFactory.getInstance(PolicyOperator.LOGICAL_AND, operands4);
    // serialize
    final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    parser.serialize(oper4, outStream);
    assertTrue(outStream.size() > 0);
    String XML = new String(outStream.toByteArray());
    System.out.println(XML);
    // deserialize
    final ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());
    final PolicyExpression deserExpression = parser.parse(inStream);
    assertNotNull(deserExpression);
    assertEquals(PolicyExpressionType.OPERATION, deserExpression.getExpressionType());
}
Also used : ExtendedKeyUsageExtensionField(org.nhindirect.policy.x509.ExtendedKeyUsageExtensionField) LiteralPolicyExpression(org.nhindirect.policy.LiteralPolicyExpression) PolicyExpression(org.nhindirect.policy.PolicyExpression) OperationPolicyExpression(org.nhindirect.policy.OperationPolicyExpression) ByteArrayOutputStream(java.io.ByteArrayOutputStream) XMLLexiconPolicyParser(org.nhindirect.policy.impl.XMLLexiconPolicyParser) OperationPolicyExpression(org.nhindirect.policy.OperationPolicyExpression) ByteArrayInputStream(java.io.ByteArrayInputStream) KeyUsageExtensionField(org.nhindirect.policy.x509.KeyUsageExtensionField) ExtendedKeyUsageExtensionField(org.nhindirect.policy.x509.ExtendedKeyUsageExtensionField) Vector(java.util.Vector)

Example 14 with OperationPolicyExpression

use of org.nhindirect.policy.OperationPolicyExpression in project nhin-d by DirectProject.

the class XMLLexiconPolicyParser_serializeTest method testSerialize_simpleExpression_validateExpression.

public void testSerialize_simpleExpression_validateExpression() throws Exception {
    final XMLLexiconPolicyParser parser = new XMLLexiconPolicyParser();
    // build the expression
    //final PolicyValue<Boolean> op1 = PolicyValueFactory.getInstance(true);
    final LiteralPolicyExpression<Integer> expr = LiteralPolicyExpressionFactory.getInstance(KeyUsageBit.DATA_ENCIPHERMENT.getUsageBit());
    final KeyUsageExtensionField extensionField = new KeyUsageExtensionField(true);
    final Vector<PolicyExpression> operands = new Vector<PolicyExpression>();
    operands.add(expr);
    operands.add(extensionField);
    final OperationPolicyExpression oper = OperationPolicyExpressionFactory.getInstance(PolicyOperator.EQUALS, operands);
    // serialize
    final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    parser.serialize(oper, outStream);
    String XML = new String(outStream.toByteArray());
    System.out.println(XML);
/*
		
		// deserialize
		final ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());

		final PolicyExpression deserExpression = parser.parse(inStream);
		
		assertNotNull(deserExpression);
		
		assertEquals(PolicyExpressionType.OPERATION, deserExpression.getExpressionType());
		*/
}
Also used : LiteralPolicyExpression(org.nhindirect.policy.LiteralPolicyExpression) PolicyExpression(org.nhindirect.policy.PolicyExpression) OperationPolicyExpression(org.nhindirect.policy.OperationPolicyExpression) KeyUsageExtensionField(org.nhindirect.policy.x509.KeyUsageExtensionField) ExtendedKeyUsageExtensionField(org.nhindirect.policy.x509.ExtendedKeyUsageExtensionField) ByteArrayOutputStream(java.io.ByteArrayOutputStream) XMLLexiconPolicyParser(org.nhindirect.policy.impl.XMLLexiconPolicyParser) Vector(java.util.Vector) OperationPolicyExpression(org.nhindirect.policy.OperationPolicyExpression)

Example 15 with OperationPolicyExpression

use of org.nhindirect.policy.OperationPolicyExpression in project nhin-d by DirectProject.

the class StackMachineCompiler_compileTest method testCompile_simpleBinaryLiteralOperation_assertEntriesAndEvaluation.

public void testCompile_simpleBinaryLiteralOperation_assertEntriesAndEvaluation() throws Exception {
    // build the expression
    final PolicyValue<Integer> op1 = PolicyValueFactory.getInstance(1);
    final PolicyValue<Integer> op2 = PolicyValueFactory.getInstance(1);
    final LiteralPolicyExpression<Integer> expr1 = LiteralPolicyExpressionFactory.getInstance(op1);
    final LiteralPolicyExpression<Integer> expr2 = LiteralPolicyExpressionFactory.getInstance(op2);
    final Vector<PolicyExpression> operands = new Vector<PolicyExpression>();
    operands.add(expr1);
    operands.add(expr2);
    final OperationPolicyExpression oper = OperationPolicyExpressionFactory.getInstance(PolicyOperator.EQUALS, operands);
    final StackMachineCompiler compiler = new StackMachineCompiler();
    final Vector<Opcode> entries = compiler.compile(null, oper);
    assertEquals(3, entries.size());
    assertEquals(op1, ((StackMachineEntry) entries.get(0)).getValue());
    assertEquals(op2, ((StackMachineEntry) entries.get(1)).getValue());
    assertEquals(PolicyOperator.EQUALS, ((StackMachineEntry) entries.get(2)).getOperator());
    // execute the compiled expression in the stack machine
    final StackMachine machine = new StackMachine();
    final Boolean evalVal = machine.evaluate(entries);
    assertTrue(evalVal);
}
Also used : StackMachine(org.nhindirect.policy.impl.machine.StackMachine) Opcode(org.nhindirect.policy.Opcode) LiteralPolicyExpression(org.nhindirect.policy.LiteralPolicyExpression) PolicyExpression(org.nhindirect.policy.PolicyExpression) OperationPolicyExpression(org.nhindirect.policy.OperationPolicyExpression) StackMachineCompiler(org.nhindirect.policy.impl.machine.StackMachineCompiler) Vector(java.util.Vector) OperationPolicyExpression(org.nhindirect.policy.OperationPolicyExpression)

Aggregations

LiteralPolicyExpression (org.nhindirect.policy.LiteralPolicyExpression)16 OperationPolicyExpression (org.nhindirect.policy.OperationPolicyExpression)16 PolicyExpression (org.nhindirect.policy.PolicyExpression)16 Vector (java.util.Vector)9 InputStream (java.io.InputStream)7 KeyUsageExtensionField (org.nhindirect.policy.x509.KeyUsageExtensionField)7 ExtendedKeyUsageExtensionField (org.nhindirect.policy.x509.ExtendedKeyUsageExtensionField)6 Opcode (org.nhindirect.policy.Opcode)5 StackMachine (org.nhindirect.policy.impl.machine.StackMachine)5 StackMachineCompiler (org.nhindirect.policy.impl.machine.StackMachineCompiler)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 File (java.io.File)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 X509Certificate (java.security.cert.X509Certificate)3 StackMachineEntry (org.nhindirect.policy.impl.machine.StackMachineEntry)3 XMLLexiconPolicyParser (org.nhindirect.policy.impl.XMLLexiconPolicyParser)2 SubjectAttributeField (org.nhindirect.policy.x509.SubjectAttributeField)1 SubjectKeyIdentifierExtensionField (org.nhindirect.policy.x509.SubjectKeyIdentifierExtensionField)1 X509Field (org.nhindirect.policy.x509.X509Field)1