use of org.nhindirect.policy.Opcode 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));
}
use of org.nhindirect.policy.Opcode in project nhin-d by DirectProject.
the class DefaultPolicyFilter_isCompliantTest method testIsCompliant_parse_engineReturnsCompliant_assertTrue.
@SuppressWarnings("unchecked")
public void testIsCompliant_parse_engineReturnsCompliant_assertTrue() throws Exception {
final InputStream inStream = FileUtils.openInputStream(new File("./src/test/resources/policies/dataEnciphermentOnlyRequired.xml"));
final Compiler compiler = mock(Compiler.class);
final ExecutionEngine engine = mock(ExecutionEngine.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, inStream, PolicyLexicon.XML));
}
use of org.nhindirect.policy.Opcode in project nhin-d by DirectProject.
the class DefaultPolicyFilter_isCompliantTest method testIsCompliant_engineReturnsNotCompliant_assertFalse.
@SuppressWarnings("unchecked")
public void testIsCompliant_engineReturnsNotCompliant_assertFalse() 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(false);
final DefaultPolicyFilter filter = new DefaultPolicyFilter();
filter.setCompiler(compiler);
filter.setExecutionEngine(engine);
assertFalse(filter.isCompliant(cert, expression));
}
use of org.nhindirect.policy.Opcode in project nhin-d by DirectProject.
the class StackMachineCompiler_compileTest method testCompile_multipleEmbeddedOperations_extendedKeyUsage_keyUsage_assertEntriesAndEvaluation.
public void testCompile_multipleEmbeddedOperations_extendedKeyUsage_keyUsage_assertEntriesAndEvaluation() throws Exception {
final X509Certificate cert = TestUtils.loadCertificate("mshost.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();
// 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);
final Vector<Opcode> entries = compiler.compile(cert, oper4);
assertEquals(9, 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());
assertEquals(expr4.getPolicyValue(), ((StackMachineEntry) entries.get(5)).getValue());
assertEquals(op5, ((StackMachineEntry) entries.get(6)).getValue());
assertEquals(PolicyOperator.CONTAINS, ((StackMachineEntry) entries.get(7)).getOperator());
assertEquals(PolicyOperator.LOGICAL_AND, ((StackMachineEntry) entries.get(8)).getOperator());
// execute the compiled expression in the stack machine
final StackMachine machine = new StackMachine();
final Boolean evalVal = machine.evaluate(entries);
assertTrue(evalVal);
}
use of org.nhindirect.policy.Opcode in project nhin-d by DirectProject.
the class StackMachine_evaluateTest method testEvaluate_logicalNotFalseValue_assertTrue.
public void testEvaluate_logicalNotFalseValue_assertTrue() throws Exception {
final Vector<Opcode> stuffToProcess = new Vector<Opcode>();
stuffToProcess.add(new StackMachineEntry(PolicyValueFactory.getInstance(false)));
stuffToProcess.add(new StackMachineEntry(PolicyOperator.LOGICAL_NOT));
final StackMachine stMachine = new StackMachine();
assertTrue(stMachine.evaluate(stuffToProcess));
}
Aggregations