use of org.nhindirect.policy.impl.machine.StackMachine in project nhin-d by DirectProject.
the class StackMachine_evaluateTest method testEvaluate_sizeCollection_empty_assertSize0.
public void testEvaluate_sizeCollection_empty_assertSize0() throws Exception {
final Vector<Opcode> stuffToProcess = new Vector<Opcode>();
stuffToProcess.add(new StackMachineEntry(PolicyValueFactory.getInstance(Collections.emptyList())));
stuffToProcess.add(new StackMachineEntry(PolicyOperator.SIZE));
stuffToProcess.add(new StackMachineEntry(PolicyValueFactory.getInstance(0)));
stuffToProcess.add(new StackMachineEntry(PolicyOperator.EQUALS));
final StackMachine stMachine = new StackMachine();
assertTrue(stMachine.evaluate(stuffToProcess));
}
use of org.nhindirect.policy.impl.machine.StackMachine in project nhin-d by DirectProject.
the class StackMachine_evaluateTest method testEvaluate_greater_topOfStackIsGreater_assertFalse.
public void testEvaluate_greater_topOfStackIsGreater_assertFalse() throws Exception {
final Vector<Opcode> stuffToProcess = new Vector<Opcode>();
stuffToProcess.add(new StackMachineEntry(PolicyValueFactory.getInstance(0)));
stuffToProcess.add(new StackMachineEntry(PolicyValueFactory.getInstance(1)));
stuffToProcess.add(new StackMachineEntry(PolicyOperator.GREATER));
final StackMachine stMachine = new StackMachine();
assertFalse(stMachine.evaluate(stuffToProcess));
}
use of org.nhindirect.policy.impl.machine.StackMachine 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);
}
use of org.nhindirect.policy.impl.machine.StackMachine 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);
}
use of org.nhindirect.policy.impl.machine.StackMachine 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);
}
Aggregations