Пример #1
0
        public void Test_LoadCustomAddinAssembly()
        {
            MockRepository mocks = new MockRepository();

            // Set new
            EsapiConfig.Instance.Validator.Type = typeof(SurrogateValidator).AssemblyQualifiedName;

            // Set assemblies to load
            AddinAssemblyElement addinAssembly = new AddinAssemblyElement();

            addinAssembly.Name = typeof(Esapi).Assembly.FullName;
            EsapiConfig.Instance.Validator.Rules.Assemblies.Add(addinAssembly);

            // Set mock expectations
            IValidator mockValidator = mocks.StrictMock <IValidator>();

            // Load default
            Expect.Call(delegate { mockValidator.AddRule(BuiltinValidationRules.CreditCard, null); }).Constraints(Is.Equal(BuiltinValidationRules.CreditCard), Is.Anything());
            Expect.Call(delegate { mockValidator.AddRule(BuiltinValidationRules.Date, null); }).Constraints(Is.Equal(BuiltinValidationRules.Date), Is.Anything());
            Expect.Call(delegate { mockValidator.AddRule(BuiltinValidationRules.Double, null); }).Constraints(Is.Equal(BuiltinValidationRules.Double), Is.Anything());
            Expect.Call(delegate { mockValidator.AddRule(BuiltinValidationRules.Integer, null); }).Constraints(Is.Equal(BuiltinValidationRules.Integer), Is.Anything());
            Expect.Call(delegate { mockValidator.AddRule(BuiltinValidationRules.Printable, null); }).Constraints(Is.Equal(BuiltinValidationRules.Printable), Is.Anything());
            mocks.ReplayAll();

            // Create and test
            SurrogateValidator.DefaultValidator = mockValidator;
            IValidator validator = Esapi.Validator;

            Assert.IsTrue(validator.GetType().Equals(typeof(SurrogateValidator)));
            mocks.VerifyAll();
        }
Пример #2
0
        /// <summary>
        /// Load validation rule
        /// </summary>
        /// <param name="validator"></param>
        /// <param name="ruleType"></param>
        /// <returns></returns>
        private static bool LoadValidationRule(IValidator validator, Type ruleType)
        {
            if (ruleType == null)
            {
                throw new ArgumentNullException("ruleType");
            }
            if (validator == null)
            {
                throw new ArgumentNullException("validator");
            }

            bool loaded = false;

            object[] attrs = ruleType.GetCustomAttributes(typeof(ValidationRuleAttribute), false);
            if (attrs != null && attrs.Length > 0)
            {
                ValidationRuleAttribute ruleAttr = (ValidationRuleAttribute)attrs[0];

                if (ruleAttr.AutoLoad)
                {
                    validator.AddRule(ruleAttr.Name, ObjectBuilder.Build <IValidationRule>(ruleType));
                    loaded = true;
                }
            }

            return(loaded);
        }
Пример #3
0
        public void Test_LoadCustomCodecs()
        {
            MockRepository mocks = new MockRepository();

            // Set new
            EsapiConfig.Instance.Validator.Type = typeof(SurrogateValidator).AssemblyQualifiedName;

            // Set rules to load
            string[] ruleNames = new[] { Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString() };
            foreach (string ruleName in ruleNames)
            {
                ValidationRuleElement ruleElement = new ValidationRuleElement();
                ruleElement.Name = ruleName;
                ruleElement.Type = typeof(SurrogateValidationRule).AssemblyQualifiedName;

                EsapiConfig.Instance.Validator.Rules.Add(ruleElement);
            }

            // Set mock expectations
            IValidator mockValidator = mocks.StrictMock <IValidator>();

            // Custom rules are loaded and are of proper type
            foreach (string ruleName in ruleNames)
            {
                Expect.Call(delegate { mockValidator.AddRule(ruleName, null); }).Constraints(Is.Equal(ruleName), Is.TypeOf <SurrogateValidationRule>());
            }
            mocks.ReplayAll();

            // Create and test
            SurrogateValidator.DefaultValidator = mockValidator;
            IValidator validator = Esapi.Validator;

            Assert.IsTrue(validator.GetType().Equals(typeof(SurrogateValidator)));
            mocks.VerifyAll();
        }
Пример #4
0
        public void Test_StringRule()
        {
            IValidator validator = Esapi.Validator;

            string id = Guid.NewGuid().ToString();
            StringValidationRule rule = new StringValidationRule();

            validator.AddRule(id, rule);

            // Test valid
            Assert.IsTrue(validator.IsValid(id, Guid.NewGuid().ToString()));

            // Test allow null or empty
            Assert.IsFalse(validator.IsValid(id, string.Empty));
            Assert.IsFalse(validator.IsValid(id, null));

            rule.AllowNullOrEmpty = true;
            Assert.IsTrue(validator.IsValid(id, string.Empty));
            Assert.IsTrue(validator.IsValid(id, null));

            // Test whitelist
            Assert.IsTrue(validator.IsValid(id, "abc"));
            rule.AddWhitelistPattern("\\d+");
            Assert.IsFalse(validator.IsValid(id, "abc"));
            Assert.IsTrue(validator.IsValid(id, "123"));

            // Test blacklist
            rule.AddBlacklistPattern("1");
            Assert.IsFalse(validator.IsValid(id, "123"));
            Assert.IsTrue(validator.IsValid(id, "23"));
        }
        public IResponseMessage Validate(IRequest request)
        {
            IResponseMessage responseMessage = new ReponseMessage();

            try
            {
                validator.AddRule(new NotNullRule());
                validator.AddRule(new NotNullPlaylistID());

                if (validator.ValidateRequest(request))
                {
                    responseMessage.StatusCode = Status.SUCCESS;
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }

            return(responseMessage);
        }
Пример #6
0
        public void Test_DoubleRuleRange()
        {
            IValidator validator = Esapi.Validator;

            // Test range
            string id = Guid.NewGuid().ToString();
            DoubleValidationRule doubleRule = new DoubleValidationRule()
            {
                MinValue = 0, MaxValue = 10
            };

            validator.AddRule(id, doubleRule);

            Assert.IsTrue(validator.IsValid(id, "0"));
            Assert.IsTrue(validator.IsValid(id, "10"));
            Assert.IsTrue(validator.IsValid(id, "5"));
            Assert.IsFalse(validator.IsValid(id, "-1"));
            Assert.IsFalse(validator.IsValid(id, "11"));
        }
Пример #7
0
        public void Test_StringRuleRange()
        {
            IValidator validator = Esapi.Validator;

            // Test range
            string id = Guid.NewGuid().ToString();
            StringValidationRule rule = new StringValidationRule()
            {
                MinLength = 1, MaxLength = 10
            };

            validator.AddRule(id, rule);

            Assert.IsTrue(validator.IsValid(id, "a"));
            Assert.IsTrue(validator.IsValid(id, "1234567890"));
            Assert.IsTrue(validator.IsValid(id, "12345"));
            Assert.IsFalse(validator.IsValid(id, ""));
            Assert.IsFalse(validator.IsValid(id, "12345678901"));
        }
Пример #8
0
        public void Test_DateRuleRange()
        {
            IValidator validator = Esapi.Validator;

            // Test range
            DateTime now = DateTime.Now;
            string   id  = Guid.NewGuid().ToString();

            // NOTE : conversion to string looses precision so force a conversion otherwise
            // validating MinValue or MaxValue would fail (see the tests below)
            DateValidationRule rule = new DateValidationRule()
            {
                MinValue = DateTime.Parse(now.AddDays(1).ToString()),
                MaxValue = DateTime.Parse(now.AddDays(10).ToString())
            };

            validator.AddRule(id, rule);

            Assert.IsTrue(validator.IsValid(id, now.AddDays(1).ToString()));
            Assert.IsTrue(validator.IsValid(id, now.AddDays(10).ToString()));
            Assert.IsTrue(validator.IsValid(id, now.AddDays(5).ToString()));
            Assert.IsFalse(validator.IsValid(id, now.ToString()));
            Assert.IsFalse(validator.IsValid(id, now.AddDays(11).ToString()));
        }
Пример #9
0
        /// <summary>
        /// Load validation rule
        /// </summary>
        /// <param name="validator"></param>
        /// <param name="ruleType"></param>
        /// <returns></returns>
        private static bool LoadValidationRule(IValidator validator, Type ruleType)
        {
            if (ruleType == null){
                throw new ArgumentNullException("ruleType");
            }
            if (validator == null) {
                throw new ArgumentNullException("validator");
            }

            bool loaded = false;

            object[] attrs = ruleType.GetCustomAttributes(typeof(ValidationRuleAttribute), false);
            if (attrs != null && attrs.Length > 0) {
                ValidationRuleAttribute ruleAttr = (ValidationRuleAttribute)attrs[0];

                if (ruleAttr.AutoLoad) {
                    validator.AddRule(ruleAttr.Name, ObjectBuilder.Build<IValidationRule>(ruleType));
                    loaded = true;
                }
            }

            return loaded;
        }