// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information. using System.Linq; using System.Linq.Expressions; using System.Runtime.Serialization; using System.Xml.Serialization; using Moq; using Xunit; using Assert = Microsoft.TestCommon.AssertEx; namespace System.Web.Http.Query { public class QueryValidatorTest { QueryValidator _queryValidator = QueryValidator.Instance; [Fact] public void XmlIgnoreAttributeCausesInvalidOperation() { IQueryable query = new QueryValidatorSampleClass[0].AsQueryable(); Assert.Throws( () => _queryValidator.Validate(query.Where((sample) => sample.XmlIgnoreProperty == 0)), "The property or field 'XmlIgnoreProperty' in type 'QueryValidatorSampleClass' is not accessible."); } [Fact] public void IgnoreDataMemberAttributeCausesInvalidOperation() { IQueryable query = new QueryValidatorSampleClass[0].AsQueryable(); Assert.Throws( () => _queryValidator.Validate(query.Where((sample) => sample.IgnoreDataMemberProperty == 0)), "The property or field 'IgnoreDataMemberProperty' in type 'QueryValidatorSampleClass' is not accessible."); } [Fact] public void NonSerializedAttributeCausesInvalidOperation() { IQueryable query = new QueryValidatorSampleClass[0].AsQueryable(); Assert.Throws( () => _queryValidator.Validate(query.Where((sample) => sample.NonSerializedAttributeField == 0)), "The property or field 'NonSerializedAttributeField' in type 'QueryValidatorSampleClass' is not accessible."); } [Fact] public void NormalPropertyAccessDoesnotThrow() { IQueryable query = new QueryValidatorSampleClass[0].AsQueryable(); _queryValidator.Validate(query.Where((sample) => sample.NormalProperty == 0)); } [Fact] public void NormalFieldAccessDoesnotThrow() { IQueryable query = new QueryValidatorSampleClass[0].AsQueryable(); _queryValidator.Validate(query.Where((sample) => sample.PublicField == 0)); } [Fact] public void DataContractDataMemberPropertyAccessDoesnotThrow() { IQueryable query = new QueryValidatorSampleDataContractClass[0].AsQueryable(); _queryValidator.Validate(query.Where((sample) => sample.DataMemberProperty == 0)); } [Fact] public void DataContractNonDataMemberPropertyAccessCausesInvalidOperation() { IQueryable query = new QueryValidatorSampleDataContractClass[0].AsQueryable(); Assert.Throws( () => _queryValidator.Validate(query.Where((sample) => sample.NonDataMemberProperty == 0)), "The property or field 'NonDataMemberProperty' in type 'QueryValidatorSampleDataContractClass' is not accessible."); } [Fact] public void DataContractNonDataMemberFieldAccessCausesInvalidOperation() { IQueryable query = new QueryValidatorSampleDataContractClass[0].AsQueryable(); Assert.Throws( () => _queryValidator.Validate(query.Where((sample) => sample.NonDataMemberField == 0)), "The property or field 'NonDataMemberField' in type 'QueryValidatorSampleDataContractClass' is not accessible."); } [Fact] public void SetOnlyPropertyAccessCausesInvalidOperation() { Mock query = new Mock(); query .Setup(q => q.Expression) .Returns( Expression.PropertyOrField( Expression.Constant(new QueryValidatorSampleClass()), "SetOnlyProperty")); Assert.Throws( () => _queryValidator.Validate(query.Object), "The property or field 'SetOnlyProperty' in type 'QueryValidatorSampleClass' is not accessible."); } [Fact] public void NonPublicGetPropertyAccessCausesInvalidOperation() { Mock query = new Mock(); query .Setup(q => q.Expression) .Returns( Expression.PropertyOrField( Expression.Constant(new QueryValidatorSampleClass()), "NonPublicGetProperty")); Assert.Throws( () => _queryValidator.Validate(query.Object), "The property or field 'NonPublicGetProperty' in type 'QueryValidatorSampleClass' is not accessible."); } public class QueryValidatorSampleClass { [XmlIgnore] public int XmlIgnoreProperty { get; set; } [IgnoreDataMember] public int IgnoreDataMemberProperty { get; set; } [NonSerialized] public int NonSerializedAttributeField = 0; public int NormalProperty { get; set; } public int SetOnlyProperty { set { } } public int NonPublicGetProperty { set; private get; } public int PublicField; } [DataContract] public class QueryValidatorSampleDataContractClass { [DataMember] public int DataMemberProperty { get; set; } public int NonDataMemberProperty { get; set; } public int NonDataMemberField; } } }