private static AllowedArithmeticOperators ToArithmeticOperator(BinaryOperatorNode binaryNode) { AllowedArithmeticOperators result = AllowedArithmeticOperators.None; switch (binaryNode.OperatorKind) { case BinaryOperatorKind.Add: result = AllowedArithmeticOperators.Add; break; case BinaryOperatorKind.Divide: result = AllowedArithmeticOperators.Divide; break; case BinaryOperatorKind.Modulo: result = AllowedArithmeticOperators.Modulo; break; case BinaryOperatorKind.Multiply: result = AllowedArithmeticOperators.Multiply; break; case BinaryOperatorKind.Subtract: result = AllowedArithmeticOperators.Subtract; break; default: // should never be here Contract.Assert(false, "ToArithmeticOperator should never be here."); break; } return(result); }
/// <summary> /// Allows the specified arithmetic operators. /// </summary> /// <param name="arithmeticOperators">One or more <see cref="AllowedArithmeticOperators">allowed arithmetic operators</see>.</param> /// <returns>The original <see cref="ODataActionQueryOptionsConventionBuilder{T}"/>.</returns> public virtual ODataActionQueryOptionsConventionBuilder <T> Allow(AllowedArithmeticOperators arithmeticOperators) { Contract.Ensures(Contract.Result <ODataActionQueryOptionsConventionBuilder <T> >() != null); ValidationSettings.AllowedArithmeticOperators |= arithmeticOperators; return(this); }
public void describe_should_return_description_for_filter( int maxNodeCount, string[] properties, AllowedLogicalOperators logicalOperators, AllowedArithmeticOperators arithmeticOperators, AllowedFunctions functions, string expected) { // arrange var provider = new DefaultODataQueryOptionDescriptionProvider(); var context = new ODataQueryOptionDescriptionContext() { MaxNodeCount = maxNodeCount, AllowedArithmeticOperators = arithmeticOperators, AllowedLogicalOperators = logicalOperators, AllowedFunctions = functions }; for (var i = 0; i < properties.Length; i++) { context.AllowedFilterProperties.Add(properties[i]); } // act var description = provider.Describe(Filter, context); // assert description.Should().Be(expected); }
static IEnumerable <string> EnumerateArithmeticOperators(AllowedArithmeticOperators arithmeticOperators) { if (arithmeticOperators.HasFlag(Add)) { yield return("add"); } if (arithmeticOperators.HasFlag(Subtract)) { yield return("sub"); } if (arithmeticOperators.HasFlag(Multiply)) { yield return("mul"); } if (arithmeticOperators.HasFlag(Divide)) { yield return("div"); } if (arithmeticOperators.HasFlag(Modulo)) { yield return("mod"); } }
public ODataValidationSettings() { // default it to all the operators _allowedArithmeticOperators = AllowedArithmeticOperators.All; _allowedFunctionNames = AllowedFunctionNames.AllFunctionNames; _allowedLogicalOperators = AllowedLogicalOperators.All; _allowedQueryParameters = AllowedQueryOptions.All; _allowedOrderByProperties = new Collection<string>(); }
public ODataValidationSettings() { // default it to all the operators _allowedArithmeticOperators = AllowedArithmeticOperators.All; _allowedFunctionNames = AllowedFunctionNames.AllFunctionNames; _allowedLogicalOperators = AllowedLogicalOperators.All; _allowedQueryParameters = AllowedQueryOptions.All; _allowedOrderByProperties = new Collection <string>(); }
public void All_Contains_AllArithmeticOperators() { AllowedArithmeticOperators allArithmeticOperators = 0; foreach (AllowedArithmeticOperators allowedArithmeticOperator in Enum.GetValues(typeof(AllowedArithmeticOperators))) { if (allowedArithmeticOperator != AllowedArithmeticOperators.All) { allArithmeticOperators |= allowedArithmeticOperator; } } Assert.Equal(allArithmeticOperators, AllowedArithmeticOperators.All); }
/// <summary> /// Override this method for the Arithmetic operators, including add, sub, mul, div, mod. /// </summary> /// <remarks> /// This method is intended to be called from method overrides in subclasses. This method also supports unit-testing scenarios and is not intended to be called from user code. /// Call the Validate method to validate a <see cref="FilterQueryOption"/> instance. /// </remarks> /// <param name="binaryNode"></param> /// <param name="settings"></param> protected virtual void ValidateArithmeticOperator(BinaryOperatorNode binaryNode, ODataValidationSettings settings) { Contract.Assert(binaryNode != null); Contract.Assert(settings != null); AllowedArithmeticOperators arithmeticOperator = ToArithmeticOperator(binaryNode); if ((settings.AllowedArithmeticOperators & arithmeticOperator) != arithmeticOperator) { // this means the given logical operator is not allowed throw new ODataException(Error.Format(SRResources.NotAllowedArithmeticOperator, arithmeticOperator, "AllowedArithmeticOperators")); } // recursion case goes here ValidateQueryNode(binaryNode.Left, settings); ValidateQueryNode(binaryNode.Right, settings); }
/// <summary> /// Override this method for the Arithmetic operators, including add, sub, mul, div, mod. /// </summary> /// <remarks> /// This method is intended to be called from method overrides in subclasses. This method also supports unit-testing scenarios and is not intended to be called from user code. /// Call the Validate method to validate a <see cref="FilterQueryOption"/> instance. /// </remarks> /// <param name="binaryNode"></param> /// <param name="settings"></param> public virtual void ValidateArithmeticOperator(BinaryOperatorNode binaryNode, ODataValidationSettings settings) { if (binaryNode == null) { throw Error.ArgumentNull("binaryNode"); } if (settings == null) { throw Error.ArgumentNull("settings"); } AllowedArithmeticOperators arithmeticOperator = ToArithmeticOperator(binaryNode); if ((settings.AllowedArithmeticOperators & arithmeticOperator) != arithmeticOperator) { // this means the given logical operator is not allowed throw new ODataException(Error.Format(SRResources.NotAllowedArithmeticOperator, arithmeticOperator, "AllowedArithmeticOperators")); } // recursion case goes here ValidateQueryNode(binaryNode.Left, settings); ValidateQueryNode(binaryNode.Right, settings); }
public void AllowedArithmeticOperators_ThrowIfNoneAllowed(AllowedArithmeticOperators unused, string query, string operatorName) { // Arrange var settings = new ODataValidationSettings { AllowedArithmeticOperators = AllowedArithmeticOperators.None, }; var expectedMessage = string.Format( "Arithmetic operator '{0}' is not allowed. " + "To allow it, set the 'AllowedArithmeticOperators' property on EnableQueryAttribute or QueryValidationSettings.", operatorName); var option = new FilterQueryOption(query, _productContext); // Act & Assert Assert.Throws<ODataException>(() => _validator.Validate(option, settings), expectedMessage); }
public void AllowedArithmeticOperators_SucceedIfAllowed(AllowedArithmeticOperators allow, string query, string unused) { // Arrange var settings = new ODataValidationSettings { AllowedArithmeticOperators = allow, }; var option = new FilterQueryOption(query, _productContext); // Act & Assert Assert.DoesNotThrow(() => _validator.Validate(option, settings)); }
public void SqlQueryBuilderWithObjectHierarchy_Use_SupportedODataQueryOptions_from_parameter(AllowedArithmeticOperators allowedArithmeticOperators, AllowedFunctions allowedFunctions, AllowedLogicalOperators allowedLogicalOperators, AllowedQueryOptions allowedQueryOptions) { ODataValidationSettings dataValidationSettings = new ODataValidationSettings { AllowedArithmeticOperators = allowedArithmeticOperators, AllowedFunctions = allowedFunctions, AllowedLogicalOperators = allowedLogicalOperators, AllowedQueryOptions = allowedQueryOptions }; SqlQueryBuilderWithObjectHierarchy myClass = new SqlQueryBuilderWithObjectHierarchy('.', dataValidationSettings); var getSupportedODataQueryOptions = myClass.GetType().GetMethod("GetSupportedODataQueryOptions", BindingFlags.Static | BindingFlags.Public); var compareResult = new CompareLogic().Compare(getSupportedODataQueryOptions.Invoke(null, null), dataValidationSettings); Assert.IsTrue(compareResult.AreEqual); }
/// <inheritdoc/> public override int GetHashCode() => AllowedArithmeticOperators.GetHashCode() ^ AllowedFunctions.GetHashCode() ^ AllowedLogicalOperators.GetHashCode() ^ AllowedQueryOptions.GetHashCode() ^ MaxTop.GetHashCode();