public void TestGetInstance_AssertExpression() { IList<IPolicyExpression> operands = new List<IPolicyExpression>(); operands.Add(new LiteralPolicyExpression<bool>(true)); operands.Add(new LiteralPolicyExpression<bool>(false)); OperationPolicyExpression expression =new OperationPolicyExpression(PolicyOperator.BitwiseAnd<bool, bool>(), operands); expression.Should().NotBeNull(); expression.GetPolicyOperator().Should().Be(PolicyOperator.BitwiseAnd<bool, bool>()); expression.GetExpressionType().Should().Be(PolicyExpressionType.OPERATION); expression.GetOperands().ShouldBeEquivalentTo(operands); }
public void TestGetInstance_AssertExpression() { IList <IPolicyExpression> operands = new List <IPolicyExpression>(); operands.Add(new LiteralPolicyExpression <bool>(true)); operands.Add(new LiteralPolicyExpression <bool>(false)); OperationPolicyExpression expression = new OperationPolicyExpression(PolicyOperator.BitwiseAnd <bool, bool>(), operands); expression.Should().NotBeNull(); expression.GetPolicyOperator().Should().Be(PolicyOperator.BitwiseAnd <bool, bool>()); expression.GetExpressionType().Should().Be(PolicyExpressionType.OPERATION); expression.GetOperands().ShouldBeEquivalentTo(operands); }
public void TestSerialize_SimpleExpression_ValidateExpression() { LiteralPolicyExpression<int> expr = new LiteralPolicyExpression<int>(new PolicyValue<int>((int) KeyUsageBit.DataEncipherment)); XMLLexiconPolicyParser parser = new XMLLexiconPolicyParser(expr); KeyUsageExtensionField extensionField = new KeyUsageExtensionField(true); List<IPolicyExpression> operands = new List<IPolicyExpression>(); operands.Add(expr); operands.Add(extensionField); IOperationPolicyExpression oper = new OperationPolicyExpression( PolicyOperator<int, int, bool>.EQUALS, operands); string xml = parser.Serialize(oper); Console.WriteLine(xml); }
public void TestSerialize_SimpleExpression_ValidateExpression() { LiteralPolicyExpression <int> expr = new LiteralPolicyExpression <int>(new PolicyValue <int>((int)KeyUsageBit.DataEncipherment)); XMLLexiconPolicyParser parser = new XMLLexiconPolicyParser(expr); KeyUsageExtensionField extensionField = new KeyUsageExtensionField(true); List <IPolicyExpression> operands = new List <IPolicyExpression>(); operands.Add(expr); operands.Add(extensionField); IOperationPolicyExpression oper = new OperationPolicyExpression( PolicyOperator <int, int, bool> .EQUALS, operands); string xml = parser.Serialize(oper); Console.WriteLine(xml); }
protected StackMachineEntry <OperationPolicyExpression> Compile(IList <IOpCode> entries, X509Certificate2 cert, IPolicyExpression expression) { switch (expression.GetExpressionType()) { //case PolicyExpressionType.LITERAL: // return new StackMachineEntry(((LiteralPolicyExpression < ? >) // expression). // GetPolicyValue()) // ; //case PolicyExpressionType.REFERENCE: //{ // object refExpression = GetRefExpressionType(expression); // //IReferencePolicyExpression<X509Certificate2, object> refExpression = null; // EvaluateReferenceExpression(cert, refExpression); // return new ReferenceMachineEntry<IReferencePolicyExpression<X509Certificate2,T>>(expression); //} case PolicyExpressionType.OPERATION: { OperationPolicyExpression opExpression = (OperationPolicyExpression)expression; foreach (IPolicyExpression policyExpression in opExpression.GetOperands()) { if (policyExpression is IReferencePolicyExpression <X509Certificate2, String> ) { var refExpression = policyExpression as IReferencePolicyExpression <X509Certificate2, String>; EvaluateReferenceExpression(cert, refExpression); entries.Add(Compile(refExpression)); } else if (policyExpression is IReferencePolicyExpression <X509Certificate2, Int32> ) { var refExpression = policyExpression as IReferencePolicyExpression <X509Certificate2, Int32>; EvaluateReferenceExpression(cert, refExpression); entries.Add(Compile(refExpression)); } else if (policyExpression is IReferencePolicyExpression <X509Certificate2, Int64> ) { var refExpression = policyExpression as IReferencePolicyExpression <X509Certificate2, Int64>; EvaluateReferenceExpression(cert, refExpression); entries.Add(Compile(refExpression)); } else if (policyExpression is IReferencePolicyExpression <X509Certificate2, IList <String> > ) { var refExpression = policyExpression as IReferencePolicyExpression <X509Certificate2, IList <String> >; EvaluateReferenceExpression(cert, refExpression); entries.Add(Compile(refExpression)); } else if (policyExpression is IReferencePolicyExpression <X509Certificate2, Boolean> ) { var refExpression = policyExpression as IReferencePolicyExpression <X509Certificate2, Boolean>; EvaluateReferenceExpression(cert, refExpression); entries.Add(Compile(refExpression)); } else if (policyExpression is ILiteralPolicyExpression <String> ) { entries.Add(Compile(policyExpression as ILiteralPolicyExpression <String>)); } else { entries.Add(Compile(entries, cert, policyExpression)); } } return(new StackMachineEntry <OperationPolicyExpression>(opExpression.GetPolicyOperator())); } default: return(null); } }
/** * Builds an aggregated {@link PolicyExpression} from a parsed list of tokens. * @param tokens Parsed list of tokens used to build the {@link PolicyExpression}. * @param level Used for keeping track of depth of operations. * @return A {@link PolicyExpression} built from the parsed list of tokens. * @throws PolicyParseException */ protected IPolicyExpression BuildExpression(IEnumerator <TokenTypeAssociation> tokens, bool operandFrame) { if (!tokens.MoveNext()) { return(null); } IList <IPolicyExpression> builtOperandExpressions = new List <IPolicyExpression>(); do { TokenTypeAssociation assos = tokens.Current; switch (assos.GetTokenType()) { case TokenType.START_LEVEL: { IncrementLevel(); IPolicyExpression expression = BuildExpression(tokens); if (operandFrame) { return(expression); } builtOperandExpressions.Add(expression); break; } case TokenType.END_LEVEL: if (GetLevel() == 0) { throw new PolicyGrammarException("To many \")\" tokens. Delete this token"); } if (builtOperandExpressions.Count == 0) { throw new PolicyGrammarException("Group must contain at least one expression."); } DecrementLevel(); return(builtOperandExpressions[0]); case TokenType.OPERATOR_BINARY_EXPRESSION: case TokenType.OPERATOR_UNARY_EXPRESSION: { // regardless if this is a unary or binary expression, then next set of tokens should consist // of a parameter to this operator IPolicyExpression subExpression = BuildExpression(tokens, true); int tokenHashCode = 0; if (subExpression != null) { //TODO Refactor if (assos.GetTokenType() == TokenType.OPERATOR_UNARY_EXPRESSION) { tokenHashCode = (assos.GetToken() + "_" + GetOperandType(subExpression)).GetHashCode(); } if (assos.GetTokenType() == TokenType.OPERATOR_BINARY_EXPRESSION) { string leftOperandType = GetOperandType(builtOperandExpressions.First()); string rightOperandType = GetOperandType(subExpression); tokenHashCode = (assos.GetToken() + "_" + leftOperandType + "_" + rightOperandType).GetHashCode(); } } else //(subExpression == null) { throw new PolicyGrammarException("Missing parameter. Operator must be followed by an expression."); } builtOperandExpressions.Add(subExpression); // get the operator for this token OperatorBase operatorBase = PolicyOperator.FromToken(tokenHashCode); // now add the parameters to the operator if (builtOperandExpressions.Count == 1 && operatorBase is BinaryOperator) { throw new PolicyGrammarException("Missing parameter. Binary operators require two parameters."); } IPolicyExpression operatorExpression = new OperationPolicyExpression(operatorBase, builtOperandExpressions); if (operandFrame) { return(operatorExpression); } builtOperandExpressions = new List <IPolicyExpression>(); builtOperandExpressions.Add(operatorExpression); break; } case TokenType.LITERAL_EXPRESSION: { IPolicyExpression expression = new LiteralPolicyExpression <string>(new PolicyValue <string>(assos.GetToken())); if (operandFrame) { return(expression); // exit this operand frame } builtOperandExpressions.Add(expression); break; } case TokenType.CERTIFICATE_REFERENCE_EXPRESSION: { IPolicyExpression expression = BuildCertificateReferenceField(assos.GetToken()); if (operandFrame) { return(expression); // exit this operand frame } builtOperandExpressions.Add(expression); break; } } } while(tokens.MoveNext()); if (builtOperandExpressions.Count > 1) { throw new PolicyGrammarException("Erroneous expression."); } return(builtOperandExpressions[0]); }
/** * Builds an aggregated {@link PolicyExpression} from a parsed list of tokens. * @param tokens Parsed list of tokens used to build the {@link PolicyExpression}. * @param level Used for keeping track of depth of operations. * @return A {@link PolicyExpression} built from the parsed list of tokens. * @throws PolicyParseException */ protected IPolicyExpression BuildExpression(IEnumerator<TokenTypeAssociation> tokens, bool operandFrame) { if (!tokens.MoveNext()) { return null; } IList<IPolicyExpression> builtOperandExpressions = new List<IPolicyExpression>(); do { TokenTypeAssociation assos = tokens.Current; switch (assos.GetTokenType()) { case TokenType.START_LEVEL: { IncrementLevel(); IPolicyExpression expression = BuildExpression(tokens); if (operandFrame) return expression; builtOperandExpressions.Add(expression); break; } case TokenType.END_LEVEL: if (GetLevel() == 0) throw new PolicyGrammarException("To many \")\" tokens. Delete this token"); if (builtOperandExpressions.Count == 0) throw new PolicyGrammarException("Group must contain at least one expression."); DecrementLevel(); return builtOperandExpressions[0]; case TokenType.OPERATOR_BINARY_EXPRESSION: case TokenType.OPERATOR_UNARY_EXPRESSION: { // regardless if this is a unary or binary expression, then next set of tokens should consist // of a parameter to this operator IPolicyExpression subExpression = BuildExpression(tokens, true); int tokenHashCode = 0; if (subExpression != null) { //TODO Refactor if (assos.GetTokenType() == TokenType.OPERATOR_UNARY_EXPRESSION) { tokenHashCode = (assos.GetToken() + "_" + GetOperandType(subExpression)).GetHashCode(); } if (assos.GetTokenType() == TokenType.OPERATOR_BINARY_EXPRESSION) { string leftOperandType = GetOperandType(builtOperandExpressions.First()); string rightOperandType = GetOperandType(subExpression); tokenHashCode = (assos.GetToken() + "_" + leftOperandType + "_" + rightOperandType).GetHashCode(); } } else //(subExpression == null) { throw new PolicyGrammarException("Missing parameter. Operator must be followed by an expression."); } builtOperandExpressions.Add(subExpression); // get the operator for this token OperatorBase operatorBase = PolicyOperator.FromToken(tokenHashCode); // now add the parameters to the operator if (builtOperandExpressions.Count == 1 && operatorBase is BinaryOperator) throw new PolicyGrammarException("Missing parameter. Binary operators require two parameters."); IPolicyExpression operatorExpression = new OperationPolicyExpression(operatorBase, builtOperandExpressions); if (operandFrame) return operatorExpression; builtOperandExpressions = new List<IPolicyExpression>(); builtOperandExpressions.Add(operatorExpression); break; } case TokenType.LITERAL_EXPRESSION: { IPolicyExpression expression = new LiteralPolicyExpression<string>(new PolicyValue<string>(assos.GetToken())); if (operandFrame) return expression; // exit this operand frame builtOperandExpressions.Add(expression); break; } case TokenType.CERTIFICATE_REFERENCE_EXPRESSION: { IPolicyExpression expression = BuildCertificateReferenceField(assos.GetToken()); if (operandFrame) return expression; // exit this operand frame builtOperandExpressions.Add(expression); break; } } } while(tokens.MoveNext()); if (builtOperandExpressions.Count > 1) throw new PolicyGrammarException("Erroneous expression."); return builtOperandExpressions[0]; }