From 7f282297228fd8353346fb2c78cba813c5abdd99 Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Tue, 18 Dec 2018 17:56:10 -0500 Subject: [PATCH] implemented validatePWData --- src/Password.hs | 16 +++++++++--- test/Spec.hs | 2 ++ test/Spec/ValidatePWData.hs | 49 +++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 test/Spec/ValidatePWData.hs diff --git a/src/Password.hs b/src/Password.hs index 2781c7b..55ba9a5 100644 --- a/src/Password.hs +++ b/src/Password.hs @@ -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 diff --git a/test/Spec.hs b/test/Spec.hs index a6d8b78..53db99d 100644 --- a/test/Spec.hs +++ b/test/Spec.hs @@ -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 diff --git a/test/Spec/ValidatePWData.hs b/test/Spec/ValidatePWData.hs new file mode 100644 index 0000000..a505396 --- /dev/null +++ b/test/Spec/ValidatePWData.hs @@ -0,0 +1,49 @@ +{- + +passman +Copyright (C) 2018 Jonathan Lamothe + + +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 +. + +-} + +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