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_AllLogicalOperators() { AllowedLogicalOperators allLogicalOperators = 0; foreach (AllowedLogicalOperators allowedLogicalOperator in Enum.GetValues(typeof(AllowedLogicalOperators))) { if (allowedLogicalOperator != AllowedLogicalOperators.All) { allLogicalOperators = allLogicalOperators | allowedLogicalOperator; } } Assert.Equal(allLogicalOperators, AllowedLogicalOperators.All); }
private static AllowedLogicalOperators ToLogicalOperator(BinaryOperatorNode binaryNode) { AllowedLogicalOperators result = AllowedLogicalOperators.None; switch (binaryNode.OperatorKind) { case BinaryOperatorKind.Equal: result = AllowedLogicalOperators.Equal; break; case BinaryOperatorKind.NotEqual: result = AllowedLogicalOperators.NotEqual; break; case BinaryOperatorKind.And: result = AllowedLogicalOperators.And; break; case BinaryOperatorKind.GreaterThan: result = AllowedLogicalOperators.GreaterThan; break; case BinaryOperatorKind.GreaterThanOrEqual: result = AllowedLogicalOperators.GreaterThanOrEqual; break; case BinaryOperatorKind.LessThan: result = AllowedLogicalOperators.LessThan; break; case BinaryOperatorKind.LessThanOrEqual: result = AllowedLogicalOperators.LessThanOrEqual; break; case BinaryOperatorKind.Or: result = AllowedLogicalOperators.Or; break; case BinaryOperatorKind.Has: result = AllowedLogicalOperators.Has; break; default: // should never be here Contract.Assert(false, "ToLogicalOperator should never be here."); break; } return(result); }
/// <summary> /// Override this method to validate the LogicalOperators such as 'eq', 'ne', 'gt', 'ge', 'lt', 'le', 'and', 'or'. /// /// Please note that 'not' is not included here. Please override ValidateUnaryOperatorNode to customize 'not'. /// </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 ValidateLogicalOperator(BinaryOperatorNode binaryNode, ODataValidationSettings settings) { Contract.Assert(binaryNode != null); Contract.Assert(settings != null); AllowedLogicalOperators logicalOperator = ToLogicalOperator(binaryNode); if ((settings.AllowedLogicalOperators & logicalOperator) != logicalOperator) { // this means the given logical operator is not allowed throw new ODataException(Error.Format(SRResources.NotAllowedLogicalOperator, logicalOperator, "AllowedLogicalOperators")); } // recursion case goes here ValidateQueryNode(binaryNode.Left, settings); ValidateQueryNode(binaryNode.Right, settings); }
/// <summary> /// Override this method to validate the LogicalOperators such as 'eq', 'ne', 'gt', 'ge', 'lt', 'le', 'and', 'or'. /// /// Please note that 'not' is not included here. Please override ValidateUnaryOperatorNode to customize 'not'. /// </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 ValidateLogicalOperator(BinaryOperatorNode binaryNode, ODataValidationSettings settings) { if (binaryNode == null) { throw Error.ArgumentNull("binaryNode"); } if (settings == null) { throw Error.ArgumentNull("settings"); } AllowedLogicalOperators logicalOperator = ToLogicalOperator(binaryNode); if ((settings.AllowedLogicalOperators & logicalOperator) != logicalOperator) { // this means the given logical operator is not allowed throw new ODataException(Error.Format(SRResources.NotAllowedLogicalOperator, logicalOperator, "AllowedLogicalOperators")); } // recursion case goes here ValidateQueryNode(binaryNode.Left, settings); ValidateQueryNode(binaryNode.Right, settings); }
public void AllowedLogicalOperators_ThrowIfNoneAllowed(AllowedLogicalOperators unused, string query, string operatorName) { // Arrange var settings = new ODataValidationSettings { AllowedLogicalOperators = AllowedLogicalOperators.None, }; var expectedMessage = string.Format( "Logical operator '{0}' is not allowed. " + "To allow it, set the 'AllowedLogicalOperators' property on EnableQueryAttribute or QueryValidationSettings.", operatorName); var option = new FilterQueryOption(query, _productContext); // Act & Assert Assert.Throws<ODataException>(() => _validator.Validate(option, settings), expectedMessage); }
public void AllowedLogicalOperators_SucceedIfAllowed(AllowedLogicalOperators allow, string query, string unused) { // Arrange var settings = new ODataValidationSettings { AllowedLogicalOperators = 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();