private bool IsPasswordSatisfiesDescriptor(string password, PasswordDescriptor descriptor)
        {
            if (password.Length != descriptor.PasswordLength)
            {
                return(false);
            }

            var tempPassword = password.ToCharArray();

            if (descriptor.LowerCase)
            {
                var tempPasswordLengthBefore = tempPassword.Length;
                tempPassword = tempPassword.Except(descriptor.Alphabet.LowerCase).ToArray();
                if (tempPassword.Length == tempPasswordLengthBefore)
                {
                    return(false);
                }
            }
            if (descriptor.UpperCase)
            {
                var tempPasswordLengthBefore = tempPassword.Length;
                tempPassword = tempPassword.Except(descriptor.Alphabet.UpperCase).ToArray();
                if (tempPassword.Length == tempPasswordLengthBefore)
                {
                    return(false);
                }
            }
            if (descriptor.Digits)
            {
                var tempPasswordLengthBefore = tempPassword.Length;
                tempPassword = tempPassword.Except(descriptor.Alphabet.Digits).ToArray();
                if (tempPassword.Length == tempPasswordLengthBefore)
                {
                    return(false);
                }
            }
            if (descriptor.SpecialSymbols)
            {
                var tempPasswordLengthBefore = tempPassword.Length;
                tempPassword = tempPassword.Except(descriptor.Alphabet.SpecialSymbols).ToArray();
                if (tempPassword.Length == tempPasswordLengthBefore)
                {
                    return(false);
                }
            }

            if (tempPassword.Length != 0)
            {
                return(false);
            }

            return(true);
        }
        public void PasswordDescriptor_ForPasswordLengthBelow4_SetPasswordLength4()
        {
            //arrange
            var generator = GetGenerator();

            var shortPasswordDescriptor = new PasswordDescriptor()
            {
                PasswordLength = -10,
            };

            //act
            generator.PasswordDescriptor = shortPasswordDescriptor;

            //assert
            Assert.AreEqual(generator.PasswordDescriptor.PasswordLength, 4);
        }
        public void Constructor_WithoutPasswordDescriptorParam_CreatePasswordDescriptorByDefault()
        {
            //arrange
            PasswordGenerator generator = new PasswordGenerator("key", new CompositePCLCryptographer());
            var defaultDescriptor       = new PasswordDescriptor
            {
                LowerCase      = true,
                UpperCase      = true,
                Digits         = true,
                SpecialSymbols = true,
                PasswordLength = 18
            };

            //assert
            Assert.True(PasswordDescriptorEquals(defaultDescriptor, generator.PasswordDescriptor));
        }
        public void PasswordDescriptor_ForPasswordLengthAbove40_SetPasswordLength40()
        {
            //arrange
            var generator = GetGenerator();

            var longPasswordDescriptor = new PasswordDescriptor()
            {
                PasswordLength = 41,
            };

            //act
            generator.PasswordDescriptor = longPasswordDescriptor;

            //assert
            Assert.AreEqual(generator.PasswordDescriptor.PasswordLength, 40);
        }
        public void PasswordDescriptor_ForNullValue_ReturnDefault()
        {
            //arrange
            var generator         = GetGenerator();
            var defaultDescriptor = new PasswordDescriptor
            {
                LowerCase      = true,
                UpperCase      = true,
                Digits         = true,
                SpecialSymbols = true,
                PasswordLength = 18
            };

            //act
            generator.PasswordDescriptor = null;

            //assert
            Assert.That(PasswordDescriptorEquals(defaultDescriptor, generator.PasswordDescriptor));
        }
        private bool PasswordDescriptorEquals(PasswordDescriptor lh, PasswordDescriptor rh)
        {
            if (lh.PasswordLength != rh.PasswordLength)
            {
                return(false);
            }
            if (lh.LowerCase != rh.LowerCase)
            {
                return(false);
            }
            if (lh.UpperCase != rh.UpperCase)
            {
                return(false);
            }
            if (lh.Digits != rh.Digits)
            {
                return(false);
            }
            if (lh.SpecialSymbols != rh.SpecialSymbols)
            {
                return(false);
            }
            if (!lh.Alphabet.LowerCase.SequenceEqual(rh.Alphabet.LowerCase))
            {
                return(false);
            }
            if (!lh.Alphabet.UpperCase.SequenceEqual(rh.Alphabet.UpperCase))
            {
                return(false);
            }
            if (!lh.Alphabet.Digits.SequenceEqual(rh.Alphabet.Digits))
            {
                return(false);
            }
            if (!lh.Alphabet.SpecialSymbols.SequenceEqual(rh.Alphabet.SpecialSymbols))
            {
                return(false);
            }

            return(true);
        }
        public void Generate_WithDescriptor_ReturnPasswordMatchesDescriptor(string input, bool lowerCase, bool upperCase,
                                                                            bool digits, bool specialSymbols, int passwordLength)
        {
            //arrange
            var descriptor = new PasswordDescriptor
            {
                LowerCase      = lowerCase,
                UpperCase      = upperCase,
                Digits         = digits,
                SpecialSymbols = specialSymbols,
                PasswordLength = passwordLength
            };

            var generator = GetGenerator();

            //act
            var password = generator.Generate(input, descriptor);

            //assert
            Assert.IsTrue(IsPasswordSatisfiesDescriptor(password, descriptor));
        }
        private PasswordDescriptor GetRandomDescriptor()
        {
            var res = new PasswordDescriptor();

            res.PasswordLength = rnd.Next(4, 32);

            res.LowerCase      = rnd.Next(0, 2) == 0 ? true : false;
            res.UpperCase      = rnd.Next(0, 2) == 0 ? true : false;
            res.Digits         = rnd.Next(0, 2) == 0 ? true : false;
            res.SpecialSymbols = rnd.Next(0, 2) == 0 ? true : false;

            if (!(res.LowerCase && res.UpperCase && res.Digits && res.SpecialSymbols))
            {
                var select = rnd.Next(0, 4);

                switch (select)
                {
                case 0:
                    res.LowerCase = true;
                    break;

                case 1:
                    res.UpperCase = true;
                    break;

                case 2:
                    res.Digits = true;
                    break;

                case 3:
                    res.SpecialSymbols = true;
                    break;
                }
            }
            return(res);
        }