Skip to content

Commit

Permalink
Print passwords when length >= 79
Browse files Browse the repository at this point in the history
  • Loading branch information
sgillespie committed Jun 12, 2015
1 parent 03ec508 commit 72087bb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
10 changes: 9 additions & 1 deletion cli-test/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ instance Show CliArgs where

instance Arbitrary CliArgs where
arbitrary = do
len <- arbitrary `suchThat` (>2) `suchThat` (<=40) :: Gen Int
len <- arbitrary `suchThat` (>0) `suchThat` (<=40) :: Gen Int
num <- arbitrary `suchThat` (>2) `suchThat` (<=20) :: Gen Int
args <- sublistOf ["-n %d" `printf` num,
show len]
Expand Down Expand Up @@ -93,6 +93,14 @@ prop_shouldNotPrintZeroPasswords (CliArgs args)
response <- readHandle out'
return (response == "")

prop_shouldPrintLongPasswords :: GreaterThan79 Int -> Property
prop_shouldPrintLongPasswords (GT79 a)
= ioProperty $ do
(in', out', err', p) <- run' "dist/build/elocrypt/elocrypt" [show a]
response <- readHandle out'
return . all (==1) . map length . map words . lines $ response


-- Utility functions
run' :: FilePath -> [String] -> IO (Handle, Handle, Handle, ProcessHandle)
run' exe args = do
Expand Down
6 changes: 4 additions & 2 deletions src/cli/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ passwords :: RandomGen g => Options -> g -> String
passwords opts gen = format . group' cols $ ps
where ps = take num . newPasswords (optLength opts) $ gen
format = concat . intersperse "\n" . map (concat . intersperse " ")
cols = termLen `div` (optLength opts + 2)
num = fromMaybe (cols * termHeight) (optNumber opts)
cols = if optLength opts <= termLen - 2
then termLen `div` (optLength opts + 2)
else 1
num = fromMaybe ((if cols == 0 then 1 else cols) * termHeight) (optNumber opts)

group' :: Int -> [a] -> [[a]]
group' _ [] = []
Expand Down
6 changes: 6 additions & 0 deletions test/Test/Elocrypt/Instances.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ newtype GreaterThan2 a = GT2 { getGT2 :: a }

instance (Num a, Ord a, Arbitrary a) => Arbitrary (GreaterThan2 a) where
arbitrary = GT2 `fmap` (arbitrary `suchThat` (>2))

newtype GreaterThan79 a = GT79 { getGT79 :: a }
deriving (Eq, Ord, Show)

instance (Integral a, Random a) => Arbitrary (GreaterThan79 a) where
arbitrary = GT79 `fmap` choose (80, 500)

0 comments on commit 72087bb

Please sign in to comment.