implemented pwSearch

This commit is contained in:
Jonathan Lamothe 2018-12-25 15:47:39 -05:00
parent 00311092d0
commit e2a7fa6fc2
3 changed files with 76 additions and 2 deletions

View File

@ -49,7 +49,7 @@ module Password (
import Control.Lens (makeLenses, over, set, (^.))
import qualified Data.ByteString.Lazy as B
import qualified Data.ByteString.Base64.Lazy as B64
import Data.Char (isUpper, isLower, isDigit, isAlphaNum)
import Data.Char (isUpper, isLower, isDigit, isAlphaNum, toLower)
import Data.Digest.Pure.SHA
import qualified Data.Map as M
import Data.Maybe (fromMaybe)
@ -246,7 +246,8 @@ pwSearch
-- ^ the database to search
-> [String]
-- ^ the matching service names
pwSearch = undefined
pwSearch x db = filter (\y -> l y `contains` l x) $ M.keys db where
l = map toLower
isSpecial :: Char -> Bool
isSpecial x = not $ isUpper x || isLower x || isDigit x
@ -293,4 +294,18 @@ toUTF8 = encodeUtf8 . T.pack
toB64 :: B.ByteString -> String
toB64 = T.unpack . decodeUtf8 . B64.encode
contains :: String -> String -> Bool
_ `contains` "" = True
"" `contains` _ = False
xs@(x:xs') `contains` ys
| xs `startsWith` ys = True
| otherwise = xs' `contains` ys
startsWith :: String -> String -> Bool
_ `startsWith` "" = True
"" `startsWith` _ = False
(x:xs) `startsWith` (y:ys)
| x == y = xs `startsWith` ys
| otherwise = False
--jl

View File

@ -33,6 +33,7 @@ import qualified Spec.NewPWSalt as NewPWSalt
import qualified Spec.PWGenerate as PWGenerate
import qualified Spec.PWGetService as PWGetService
import qualified Spec.PWHasService as PWHasService
import qualified Spec.PWSearch as PWSearch
import qualified Spec.PWSetService as PWSetService
import qualified Spec.ValidatePWData as ValidatePWData
import qualified Spec.ValidatePWDatabase as ValidatePWDatabase
@ -55,6 +56,7 @@ tests = TestList
, PWHasService.tests
, PWSetService.tests
, PWGetService.tests
, PWSearch.tests
]
--jl

57
test/Spec/PWSearch.hs Normal file
View File

@ -0,0 +1,57 @@
{-
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.PWSearch (tests) where
import qualified Data.Map as M
import System.Random (mkStdGen)
import Test.HUnit (Test (..), assertBool, (~?=))
import Password
tests = TestLabel "pwSearch" $ TestList $ map test'
[ ( "no results", "quux", [] )
, ( "some results", "A", ["bar", "baz"] )
, ( "all results", "", ["foo", "bar", "baz"] )
]
test' (label, str, expect) = TestLabel label $ TestList $
TestLabel "length" (length result ~?= length expect) :
map (\x -> TestLabel ("has " ++ x) $ TestCase $
assertBool "not found" $ elem x expect) result
where result = pwSearch str db
db = M.fromList
[ ( "foo", foo )
, ( "bar", bar )
, ( "baz", baz )
]
(foo, g') = newPWData g
(bar, g'') = newPWData g'
(baz, _) = newPWData g''
g = mkStdGen 1
--jl