Beispiel #1
0
        public void LoanTypeIdTest_SelfReportedLoanModel_check_for_string_length()
        {
            SelfReportedLoanModel passingValueLowerBound = new SelfReportedLoanModel()
            {
                LoanTypeId = "1"
            };
            SelfReportedLoanModel passingValueUpperBound = new SelfReportedLoanModel()
            {
                LoanTypeId = "50CharactersCharactersCharactersCharactersCharacte"
            };
            SelfReportedLoanModel failingValueUnderMin = new SelfReportedLoanModel()
            {
                LoanTypeId = ""
            };
            SelfReportedLoanModel failingValueAboveMax = new SelfReportedLoanModel()
            {
                LoanTypeId = "51CharactersCharactersCharactersCharactersCharacter"
            };


            StringLengthAttribute stringLengthCheck = new StringLengthAttribute(50);

            stringLengthCheck.MinimumLength = 1;


            Assert.IsTrue(stringLengthCheck.IsValid(passingValueLowerBound.LoanTypeId), "Assertion of positive case (lower bound) being true failed");
            Assert.IsTrue(stringLengthCheck.IsValid(passingValueUpperBound.LoanTypeId), "Assertion of positive case (upper bound) being true failed");
            Assert.IsFalse(stringLengthCheck.IsValid(failingValueUnderMin.LoanTypeId), "Assertion of negative case (under min) case being false failed");
            Assert.IsFalse(stringLengthCheck.IsValid(failingValueAboveMax.LoanTypeId), "Assertion of negative case (above max) case being false failed");
        }
Beispiel #2
0
        public override bool IsValid(object value)
        {
            // A key is valid if it is *exactly* 128-bit or *exactly* 256-bit.
            // We do no padding ourselves, we want the user to explicitly set his key to 128-bit or 256-bit.
            // There are multiple reasons for this:
            //   1. We could possibly confuse the user by adding zero-padding to the wrong side he expected.
            //   2. The user would maybe even not expect that a key not exactly 128-bit or 256-bit does work.
            //   3. We would still need to enforce a strict size of 256-bit for keys larger than 128-bit because
            //      we cannot expand a key larger than 128-bit to 256-bit without cropping. Again, we could crop
            //      the key in ways the user may not expect.
            byte[] inputKey = value as byte[];
            string hexKey   = string.Join("", inputKey.Select(b => b.ToString("X2")));

            // Because one byte consists of 2 hexadecimal letters, we multiply by 2.
            var check128Bit = new StringLengthAttribute(16 * 2)
            {
                MinimumLength = 16 * 2
            };
            var check256Bit = new StringLengthAttribute(32 * 2)
            {
                MinimumLength = 32 * 2
            };

            return(check128Bit.IsValid(hexKey) || check256Bit.IsValid(hexKey));
        }
        public void StringLengthAttribute_Fail_Negative_Max()
        {
            StringLengthAttribute attr = new StringLengthAttribute(-10);

            ExceptionHelper.ExpectException <InvalidOperationException>(delegate() {
                attr.IsValid("");
            }, Resources.DataAnnotationsResources.StringLengthAttribute_InvalidMaxLength);
        }
 public void StringLengthAttribute_Fail_Min_Exceed_Max() {
     StringLengthAttribute attr = new StringLengthAttribute(5);
     attr.MinimumLength = 10;
     ExceptionHelper.ExpectException<InvalidOperationException>(delegate() {
         attr.IsValid("");
     },
     String.Format(CultureInfo.CurrentCulture, Resources.DataAnnotationsResources.RangeAttribute_MinGreaterThanMax, 5, 10));
 }
        public void String_Is_Included_In_The_Interval()
        {
            var validationService = new StringLengthAttribute(3, 7, "test");
            var actual            = validationService.IsValid("dsjhjh");

            Assert.AreEqual(actual.ValidationResult, ValidationResult.Success);
            Assert.AreEqual(actual.ErrorMessage, null);
        }
        public void Strind_Length_More_Than_3()
        {
            var validationService = new StringLengthAttribute(3, "test");
            var actual            = validationService.IsValid("dsjhjh");

            Assert.AreEqual(actual.ValidationResult, ValidationResult.StringLengthError);
            Assert.AreEqual(actual.ErrorMessage, "The length of the test property is more than 3");
        }
        public void Strind_Length_Less_Than_3()
        {
            var validationService = new StringLengthAttribute(3, "test");
            var actual            = validationService.IsValid("ds");

            Assert.AreEqual(actual.ValidationResult, ValidationResult.Success);
            Assert.AreEqual(actual.ErrorMessage, null);
        }
Beispiel #8
0
 /// <summary>
 /// 验证将要给的值是否合格
 /// </summary>
 /// <param name="slAttribute"></param>
 /// <param name="paraVal"></param>
 /// <returns></returns>
 private static bool ValidValue(StringLengthAttribute slAttribute, string paraVal)
 {
     if (slAttribute.IsValid(paraVal))
     {
         return(true);
     }
     return(false);
 }
        public void String_Is_Not_Included_In_The_Interval()
        {
            var validationService = new StringLengthAttribute(3, 7, "test");
            var actual            = validationService.IsValid("dsjhdlfgfdkjjdfjh");

            Assert.AreEqual(actual.ValidationResult, ValidationResult.StringLengthError);
            Assert.AreEqual(actual.ErrorMessage, "The length of the test property is not included between 3 and 7");
        }
        public void StringLengthAttribute_Fail_Min_Exceed_Max()
        {
            StringLengthAttribute attr = new StringLengthAttribute(5);

            attr.MinimumLength = 10;
            ExceptionHelper.ExpectException <InvalidOperationException>(delegate() {
                attr.IsValid("");
            },
                                                                        String.Format(CultureInfo.CurrentCulture, Resources.DataAnnotationsResources.RangeAttribute_MinGreaterThanMax, 5, 10));
        }
 public void StringLengthAttribute_IsValid_Min_And_Max() {
     StringLengthAttribute attr = new StringLengthAttribute(4);
     attr.MinimumLength = 2;
     Assert.IsFalse(attr.IsValid(""));
     Assert.IsFalse(attr.IsValid("a"));
     Assert.IsTrue(attr.IsValid("ab"));
     Assert.IsTrue(attr.IsValid("abc"));
     Assert.IsTrue(attr.IsValid("abcd"));
     Assert.IsFalse(attr.IsValid("abcde"));
 }
        public void StringLengthAttribute_IsValid_Min_And_Max()
        {
            StringLengthAttribute attr = new StringLengthAttribute(4);

            attr.MinimumLength = 2;
            Assert.IsFalse(attr.IsValid(""));
            Assert.IsFalse(attr.IsValid("a"));
            Assert.IsTrue(attr.IsValid("ab"));
            Assert.IsTrue(attr.IsValid("abc"));
            Assert.IsTrue(attr.IsValid("abcd"));
            Assert.IsFalse(attr.IsValid("abcde"));
        }
        /// <summary>
        /// Checks the length of the string to verify whether or not it falls within the permissible limits.
        /// </summary>
        /// <param name="variable">The variable in context.</param>
        /// <param name="getVariableName">The name of the variable that should be populated in the error context.</param>
        /// <param name="minimumLength">The minimum length of the variable.</param>
        /// <param name="maximumLength">The maximum length of the variable.</param>
        /// <param name="actionContext">Current action context.</param>
        /// <param name="modelState">Current model object.</param>
        /// <returns>The status of the operation.</returns>
        public static bool StringLength(string variable, Expression <Func <string, string> > getVariableName, int minimumLength, int maximumLength, HttpActionContext actionContext, ModelStateDictionary modelState)
        {
            var variableName = ((MemberExpression)getVariableName.Body).Member.Name;
            var attr         = new StringLengthAttribute(maximumLength)
            {
                MinimumLength = minimumLength
            };

            if (!attr.IsValid(variable))
            {
                modelState.AddModelError(attr.ToString(), attr.FormatErrorMessage(variableName));

                AddErrorToResponse(actionContext, modelState);
                return(false);
            }

            return(true);
        }
        public void Login_TryLoginUserWithShortPassword_ErrorMessageReturned()
        {
            var user = new UserForLogInDto {
                Email = " ", Password = "******"
            };

            var requiredAttribute     = new RequiredAttribute();
            var stringLengthAttribute = new StringLengthAttribute(maximumLength: 20)
            {
                MinimumLength = 5,
                ErrorMessage  = "Password must be between 5 and 20 characters."
            };

            var requiredEmailResult    = requiredAttribute.IsValid(user.Email);
            var requiredPasswordResult = requiredAttribute.IsValid(user.Password);
            var minLengthResult        = stringLengthAttribute.IsValid(user.Password);

            Assert.IsTrue(requiredEmailResult == false);
            Assert.IsTrue(minLengthResult == false);
            Assert.IsTrue(requiredPasswordResult == true);
        }
Beispiel #15
0
        protected override ValidationResult IsValid(object value, ValidationContext context)
        {
            // The initialization vector must be of the size specified by the version.
            // DJB version:     64-bit
            // IETF version:    96-bit
            // For the same reasons as with the key, we will do no padding,
            // so the IV must be already in the correct size.
            ChaCha  chacha         = context.ObjectInstance as ChaCha;
            Version currentVersion = ((ChaChaSettings)chacha.Settings).Version;

            byte[] inputIV  = value as byte[];
            string hexIV    = string.Join("", inputIV.Select(b => b.ToString("X2")));
            int    maxBits  = (int)currentVersion.IVBits;
            int    maxBytes = maxBits / 8;

            var required = new StringLengthAttribute(maxBytes * 2)
            {
                MinimumLength = maxBytes * 2
            };

            return(required.IsValid(hexIV) ?
                   ValidationResult.Success :
                   new ValidationResult(ErrorMessage));
        }
 public void StringLengthAttribute_Fail_Negative_Max() {
     StringLengthAttribute attr = new StringLengthAttribute(-10);
     ExceptionHelper.ExpectException<InvalidOperationException>(delegate() {
         attr.IsValid("");
     }, Resources.DataAnnotationsResources.StringLengthAttribute_InvalidMaxLength);
 }
 public void IsValid_ValidString()
 {
     Assert.AreEqual(true, attribute.IsValid(String.Empty));
     Assert.AreEqual(true, attribute.IsValid("abc"));
 }
        public void Param_Is_Not_String()
        {
            var validationService = new StringLengthAttribute(3, "test");

            Assert.Throws <ArgumentException>(() => validationService.IsValid(234));
        }
        public void LongStringShouldFailValidation()
        {
            StringLengthAttribute attribute = new StringLengthAttribute(ExampleModel.MaxNameLength);

            Assert.False(attribute.IsValid(ExampleModel.GenerateInvalidName()));
        }