implemented validatePWData

This commit is contained in:
Jonathan Lamothe 2018-12-18 17:56:10 -05:00
parent 6bd791533f
commit 7f28229722
3 changed files with 64 additions and 3 deletions

View File

@ -35,8 +35,8 @@ module Password (
pwLength, pwUpper, pwLower, pwDigits, pwSpecial,
-- ** Default Instances
newPWDatabase, newPWData, newPWPolicy, newPWSalt,
-- * Functions
validatePWPolicy
-- ** Validations
validatePWData, validatePWPolicy
) where
import Control.Lens (makeLenses, (^.))
@ -53,7 +53,7 @@ type PWDatabase = M.Map String PWData
data PWData = PWData
{ _pwPolicy :: PWPolicy
-- ^ the password policy
, _pwSalt :: B.ByteString
, _pwSalt :: PWSalt
-- ^ random data used to generate the password
} deriving (Eq, Show)
@ -113,6 +113,16 @@ newPWSalt g = (result, g2) where
result = B.pack $ take 256 $ randoms g1
(g1, g2) = split g
-- | validates password data
validatePWData
:: PWData
-- ^ the data to be validated
-> Bool
-- ^ @"True"@ if valid; @"False"@ otherwise
validatePWData x =
validatePWPolicy (x^.pwPolicy) &&
B.length (x^.pwSalt) > 0
-- | validates a password policy
validatePWPolicy
:: PWPolicy

View File

@ -30,6 +30,7 @@ import qualified Spec.NewPWData as NewPWData
import qualified Spec.NewPWDatabase as NewPWDatabase
import qualified Spec.NewPWPolicy as NewPWPolicy
import qualified Spec.NewPWSalt as NewPWSalt
import qualified Spec.ValidatePWData as ValidatePWData
import qualified Spec.ValidatePWPolicy as ValidatePWPolicy
main = do
@ -43,6 +44,7 @@ tests = TestList
, NewPWPolicy.tests
, NewPWSalt.tests
, ValidatePWPolicy.tests
, ValidatePWData.tests
]
--jl

View File

@ -0,0 +1,49 @@
{-
passman
Copyright (C) 2018 Jonathan Lamothe
<jlamothe1980@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this program. If not, see
<https://www.gnu.org/licenses/>.
-}
module Spec.ValidatePWData (tests) where
import Control.Lens (set)
import qualified Data.ByteString as B
import System.Random (mkStdGen)
import Test.HUnit (Test (..), (~?=))
import Password
tests = TestLabel "validatePWData" $ TestList $ map test'
[ ( "valid", new, True )
, ( "invalid policy", invalidPolicy, False )
, ( "invalid salt", invalidSalt, False )
]
test' (label, x, expect) = TestLabel label $
validatePWData x ~?= expect
(new, _) = newPWData g
invalidPolicy = set (pwPolicy.pwLength) (-1) new
invalidSalt = set pwSalt B.empty new
g = mkStdGen 1
--jl