Search in sources :

Example 21 with PolicyExpression

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

the class StackMachineCompiler_compileTest method testCompile_simpleUnaryLiteralOperation_assertEntriesAndEvaluation.

public void testCompile_simpleUnaryLiteralOperation_assertEntriesAndEvaluation() throws Exception {
    // build the expression
    final PolicyValue<Boolean> op1 = PolicyValueFactory.getInstance(true);
    final LiteralPolicyExpression<Boolean> expr = LiteralPolicyExpressionFactory.getInstance(op1);
    final Vector<PolicyExpression> operands = new Vector<PolicyExpression>();
    operands.add(expr);
    final OperationPolicyExpression oper = OperationPolicyExpressionFactory.getInstance(PolicyOperator.LOGICAL_NOT, operands);
    final StackMachineCompiler compiler = new StackMachineCompiler();
    final Vector<Opcode> entries = compiler.compile(null, oper);
    assertEquals(2, entries.size());
    assertEquals(op1, ((StackMachineEntry) entries.get(0)).getValue());
    assertEquals(PolicyOperator.LOGICAL_NOT, ((StackMachineEntry) entries.get(1)).getOperator());
    // execute the compiled expression in the stack machine
    final StackMachine machine = new StackMachine();
    Boolean evalVal = machine.evaluate((Vector<Opcode>) entries);
    assertFalse(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)

Example 22 with PolicyExpression

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

the class StackMachineCompiler_compileTest method testCompile_simpleBinaryLiteralAndExpressionOperation_assertEntriesAndEvaluation.

public void testCompile_simpleBinaryLiteralAndExpressionOperation_assertEntriesAndEvaluation() throws Exception {
    final X509Certificate cert = TestUtils.loadCertificate("AlAnderson@hospitalA.direct.visionshareinc.com.der");
    // build the expression
    final Integer keyUsage = KeyUsage.digitalSignature | KeyUsage.keyEncipherment | KeyUsage.nonRepudiation;
    final PolicyValue<Integer> op1 = PolicyValueFactory.getInstance(keyUsage);
    final LiteralPolicyExpression<Integer> expr1 = LiteralPolicyExpressionFactory.getInstance(op1);
    final KeyUsageExtensionField expr2 = new KeyUsageExtensionField(true);
    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(cert, oper);
    assertEquals(3, entries.size());
    assertEquals(op1, ((StackMachineEntry) entries.get(0)).getValue());
    assertEquals(keyUsage, ((StackMachineEntry) entries.get(1)).getValue().getPolicyValue());
    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 : 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) X509Certificate(java.security.cert.X509Certificate) OperationPolicyExpression(org.nhindirect.policy.OperationPolicyExpression) StackMachine(org.nhindirect.policy.impl.machine.StackMachine) StackMachineEntry(org.nhindirect.policy.impl.machine.StackMachineEntry) KeyUsageExtensionField(org.nhindirect.policy.x509.KeyUsageExtensionField) ExtendedKeyUsageExtensionField(org.nhindirect.policy.x509.ExtendedKeyUsageExtensionField) Vector(java.util.Vector)

Example 23 with PolicyExpression

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

the class StackMachineCompiler_compileTest method testCompile_multipleEmbeddedOperations_keyUsage_assertEntriesAndEvaluation.

public void testCompile_multipleEmbeddedOperations_keyUsage_assertEntriesAndEvaluation() throws Exception {
    final X509Certificate cert = TestUtils.loadCertificate("AlAnderson@hospitalA.direct.visionshareinc.com.der");
    // build the expression
    final Integer keyUsage = KeyUsage.nonRepudiation;
    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);
    final StackMachineCompiler compiler = new StackMachineCompiler();
    final Vector<Opcode> entries = compiler.compile(cert, oper2);
    assertEquals(5, entries.size());
    assertEquals(op1, ((StackMachineEntry) entries.get(0)).getValue());
    assertEquals(expr2.getPolicyValue().getPolicyValue(), ((StackMachineEntry) entries.get(1)).getValue().getPolicyValue());
    assertEquals(PolicyOperator.BITWISE_AND, ((StackMachineEntry) entries.get(2)).getOperator());
    assertEquals(op3, ((StackMachineEntry) entries.get(3)).getValue());
    assertEquals(PolicyOperator.GREATER, ((StackMachineEntry) entries.get(4)).getOperator());
    // execute the compiled expression in the stack machine
    final StackMachine machine = new StackMachine();
    final Boolean evalVal = machine.evaluate(entries);
    assertTrue(evalVal);
}
Also used : 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) X509Certificate(java.security.cert.X509Certificate) OperationPolicyExpression(org.nhindirect.policy.OperationPolicyExpression) StackMachine(org.nhindirect.policy.impl.machine.StackMachine) StackMachineEntry(org.nhindirect.policy.impl.machine.StackMachineEntry) KeyUsageExtensionField(org.nhindirect.policy.x509.KeyUsageExtensionField) ExtendedKeyUsageExtensionField(org.nhindirect.policy.x509.ExtendedKeyUsageExtensionField) Vector(java.util.Vector)

Example 24 with PolicyExpression

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

the class DefaultPolicyFilter_isCompliantTest method testIsCompliant_engineReturnsCompliant_assertTrue.

@SuppressWarnings("unchecked")
public void testIsCompliant_engineReturnsCompliant_assertTrue() throws Exception {
    final Compiler compiler = mock(Compiler.class);
    final ExecutionEngine engine = mock(ExecutionEngine.class);
    final PolicyExpression expression = mock(PolicyExpression.class);
    final X509Certificate cert = mock(X509Certificate.class);
    when(engine.evaluate((Vector<Opcode>) any())).thenReturn(true);
    final DefaultPolicyFilter filter = new DefaultPolicyFilter();
    filter.setCompiler(compiler);
    filter.setExecutionEngine(engine);
    assertTrue(filter.isCompliant(cert, expression));
}
Also used : Compiler(org.nhindirect.policy.Compiler) ExecutionEngine(org.nhindirect.policy.ExecutionEngine) Opcode(org.nhindirect.policy.Opcode) PolicyExpression(org.nhindirect.policy.PolicyExpression) X509Certificate(java.security.cert.X509Certificate)

Example 25 with PolicyExpression

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

the class DefaultPolicyFilter_isCompliantTest method testIsCompliant_missingComplier_assertException.

public void testIsCompliant_missingComplier_assertException() throws Exception {
    final PolicyExpression expression = mock(PolicyExpression.class);
    final X509Certificate cert = mock(X509Certificate.class);
    final DefaultPolicyFilter filter = new DefaultPolicyFilter();
    filter.setCompiler(null);
    boolean exceptionOccured = false;
    try {
        assertFalse(filter.isCompliant(cert, expression));
    } catch (IllegalStateException e) {
        exceptionOccured = true;
    }
    assertTrue(exceptionOccured);
}
Also used : PolicyExpression(org.nhindirect.policy.PolicyExpression) X509Certificate(java.security.cert.X509Certificate)

Aggregations

PolicyExpression (org.nhindirect.policy.PolicyExpression)54 X509Certificate (java.security.cert.X509Certificate)21 InternetAddress (javax.mail.internet.InternetAddress)18 LiteralPolicyExpression (org.nhindirect.policy.LiteralPolicyExpression)16 OperationPolicyExpression (org.nhindirect.policy.OperationPolicyExpression)16 PolicyResolver (org.nhindirect.stagent.policy.PolicyResolver)12 InputStream (java.io.InputStream)11 PolicyFilter (org.nhindirect.policy.PolicyFilter)10 Vector (java.util.Vector)9 Collection (java.util.Collection)8 HashMap (java.util.HashMap)8 Opcode (org.nhindirect.policy.Opcode)7 KeyUsageExtensionField (org.nhindirect.policy.x509.KeyUsageExtensionField)7 CertificateResolver (org.nhindirect.stagent.cert.CertificateResolver)7 TrustAnchorResolver (org.nhindirect.stagent.trust.TrustAnchorResolver)7 File (java.io.File)6 StackMachineCompiler (org.nhindirect.policy.impl.machine.StackMachineCompiler)6 ExtendedKeyUsageExtensionField (org.nhindirect.policy.x509.ExtendedKeyUsageExtensionField)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5