public void TypeDefValidator_Should_Validate_Method_Using_Denied_New_Type()
        {
            const string source = @"
using System; 
public class Test 
{
    public void A()
    { 
        var b = new DateTime();
    }
}";

            var typeDefinition = CompileToTypeDef(source);

            var policy = new WhitelistPolicy()
                         .Namespace("System", AccessPolicy.Denied, t =>
                                    t.Type("Object", AccessPolicy.Allowed)
                                    .Type("Void", AccessPolicy.Allowed)
                                    .Type("String", AccessPolicy.Denied));

            var validationPolicy = new ValidationPolicy()
                                   .WhitelistValidator(policy);

            var validator = new TypePolicyValidator(validationPolicy);

            var result = validator.Validate(typeDefinition).ToList();

            Assert.Single(result);
        }
        public void TypeDefValidator_Should_Allow_References_To_Own_Methods()
        {
            const string source = @"
using System; 

public class Test 
{
    public void A() {
    }

    public void B() {
        A();
    }
}";

            var typeDefinition = CompileToTypeDef(source);

            var policy = new WhitelistPolicy()
                         .Namespace("System", AccessPolicy.Denied, t =>
                                    t.Type("Object", AccessPolicy.Allowed)
                                    .Type("Void", AccessPolicy.Allowed)
                                    .Type("String", AccessPolicy.Denied));

            var validationPolicy = new ValidationPolicy()
                                   .WhitelistValidator(policy);

            var validator = new TypePolicyValidator(validationPolicy);

            var result = validator.Validate(typeDefinition).ToList();

            Assert.Empty(result);
        }
        public void TypeDefValidator_Should_Validate_Method_Using_Denied_Nested_Array_Element()
        {
            const string source = @"
using System; 
using System.Collections.Generic;

public class Test 
{
    public void A()
    { 
        var b = new [] { new [] { ""a"" } };
    }
}";

            var typeDefinition = CompileToTypeDef(source);

            var policy = new WhitelistPolicy()
                         .Namespace("System", AccessPolicy.Denied, t =>
                                    t.Type(nameof(Boolean), AccessPolicy.Allowed)
                                    .Type("Object", AccessPolicy.Allowed)
                                    .Type("Void", AccessPolicy.Allowed)
                                    .Type("String", AccessPolicy.Denied))
                         .Namespace("System.Collections.Generic", AccessPolicy.Allowed);

            var validationPolicy = new ValidationPolicy()
                                   .WhitelistValidator(policy);

            var validator = new TypePolicyValidator(validationPolicy);

            var result = validator.Validate(typeDefinition).ToList();

            Assert.True(result.All(r => r is WhitelistValidator.DeniedTypeValidationResult));
        }
        public void TypeDefValidator_Should_Validate_Method_Using_Denied_Field()
        {
            const string source = @"
using System; 

public class Test 
{
    public void A()
    { 
        var b = BitConverter.IsLittleEndian;
    }
}";

            var typeDefinition = CompileToTypeDef(source);

            var policy = new WhitelistPolicy()
                         .Namespace("System", AccessPolicy.Denied, t =>
                                    t.Type(nameof(Boolean), AccessPolicy.Allowed)
                                    .Type(nameof(BitConverter), AccessPolicy.Allowed,
                                          m => m.Member(nameof(BitConverter.IsLittleEndian), AccessPolicy.Denied))
                                    .Type("Object", AccessPolicy.Allowed)
                                    .Type("Void", AccessPolicy.Allowed)
                                    .Type("String", AccessPolicy.Denied));

            var validationPolicy = new ValidationPolicy()
                                   .WhitelistValidator(policy);

            var validator = new TypePolicyValidator(validationPolicy);

            var result = validator.Validate(typeDefinition).ToList();

            Assert.Single(result);
        }
示例#5
0
        public void TypePolicyValidator_Should_Validate_Own_Methods()
        {
            const string source = @"
using System; 

public class Test 
{
    static extern uint A();

    public void B() {
        var dt = DateTime.Now;
    }
}";

            var typeDefinition = CompileToTypeDef(source);

            var policy = new WhitelistPolicy()
                         .Namespace("System", AccessPolicy.Denied, t =>
                                    t.Type("Object", AccessPolicy.Allowed)
                                    .Type("Void", AccessPolicy.Allowed)
                                    .Type("String", AccessPolicy.Denied));

            var validationPolicy = new ValidationPolicy()
                                   .WhitelistValidator(policy);

            var validator = new TypePolicyValidator(validationPolicy);

            var result = validator.Validate(typeDefinition).ToList();

            Assert.True(result.Any());
            Assert.True(result.All(r => r is WhitelistValidator.WhitelistValidationResult));
        }
        public void TypeDefValidator_Should_Validate_Method_Allowed_Return_Type()
        {
            const string source = @"public class Test {public string A(){return ""a"";}}";

            var typeDefinition = CompileToTypeDef(source);

            var policy = new WhitelistPolicy()
                         .Namespace("System", AccessPolicy.Denied, t =>
                                    t.Type("Object", AccessPolicy.Allowed)
                                    .Type("Void", AccessPolicy.Allowed)
                                    .Type("String", AccessPolicy.Allowed));

            var validationPolicy = new ValidationPolicy()
                                   .WhitelistValidator(policy);

            var validator = new TypePolicyValidator(validationPolicy);

            var result = validator.Validate(typeDefinition).ToList();

            Assert.Empty(result);
        }