passman/test/Spec/PWGenerate.hs

111 lines
3.2 KiB
Haskell
Raw Permalink Normal View History

2018-12-19 16:45:16 -05:00
{-
passman
Copyright (C) Jonathan Lamothe
2020-12-14 22:41:24 -05:00
<jonathan@jlamothe.net>
2018-12-19 16:45:16 -05:00
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.PWGenerate (tests) where
import Data.Maybe (fromJust)
import Lens.Micro (set, (^.))
import System.Random (mkStdGen, StdGen)
2018-12-19 16:45:16 -05:00
import Test.HUnit
(Test (..)
, assertBool
, assertEqual
, assertFailure
, (~?=)
)
import Password
tests :: Test
2018-12-19 16:45:16 -05:00
tests = TestLabel "pwGenerate" $ TestList
[ defaultData
, invalidPolicy
, constraints
, noSpecial
, differentMaster
]
defaultData :: Test
2018-12-19 16:45:16 -05:00
defaultData = TestLabel "default data" $ TestCase $
case pwGenerate "foo" validData of
Nothing -> assertFailure "no password generated"
Just x -> assertEqual "incorrect password length"
(validData^.pwPolicy.pwLength) (length x)
invalidPolicy :: Test
2018-12-19 16:45:16 -05:00
invalidPolicy = TestLabel "invalid policy" $
pwGenerate "foo" invalidPolicy' ~?= Nothing
constraints :: Test
2018-12-19 16:45:16 -05:00
constraints = TestLabel "strict constraints" $ TestCase $
case pwGenerate "foo" constraints' of
Nothing -> assertFailure "no password generated"
Just x -> do
assertEqual "incorrect password length"
(constraints'^.pwPolicy.pwLength) (length x)
assertEqual "incorrect number of upper case"
(constraints'^.pwPolicy.pwUpper) (pwCountUpper x)
assertEqual "incorrect number of lower case"
(constraints'^.pwPolicy.pwLower) (pwCountLower x)
assertEqual "incorrect number of digita"
(constraints'^.pwPolicy.pwDigits) (pwCountDigits x)
assertEqual "incorrect number of special characters"
(fromJust $ constraints'^.pwPolicy.pwSpecial) (pwCountSpecial x)
noSpecial :: Test
2018-12-19 16:45:16 -05:00
noSpecial = TestLabel "no special chars" $ TestCase $
case pwGenerate "foo" noSpecial' of
Nothing -> assertFailure "no password generated"
Just x -> do
assertEqual "incorrect password length"
(noSpecial'^.pwPolicy.pwLength) (length x)
assertEqual "special characters found" 0 $ pwCountSpecial x
differentMaster :: Test
2018-12-19 16:45:16 -05:00
differentMaster = TestLabel "different master passwords" $ TestCase $
assertBool "passwords match" $
fromJust (pwGenerate "foo" validData) /=
fromJust (pwGenerate "bar" validData)
validData :: PWData
2018-12-19 16:45:16 -05:00
(validData, _) = newPWData g
invalidPolicy' :: PWData
2018-12-19 16:45:16 -05:00
invalidPolicy' = set (pwPolicy.pwLength) (-1) validData
constraints' :: PWData
2018-12-19 16:45:16 -05:00
constraints' = set (pwPolicy.pwUpper) 4 $
set (pwPolicy.pwLower) 4 $
set (pwPolicy.pwDigits) 4 $
set (pwPolicy.pwSpecial) (Just 4)
validData
noSpecial' :: PWData
2018-12-19 16:45:16 -05:00
noSpecial' = set (pwPolicy.pwLength) 256 $
set (pwPolicy.pwSpecial) Nothing
validData
g :: StdGen
2018-12-19 16:45:16 -05:00
g = mkStdGen 1
--jl