[tests] more refactoring

This commit is contained in:
Vincent Hanquez 2015-05-12 14:36:20 +01:00
parent 5d2e2ce999
commit ffe42392ca
3 changed files with 12 additions and 12 deletions

View File

@ -33,8 +33,7 @@ instance Show AFISParams where
show (AFISParams dat expand _ _) = "data: " ++ show dat ++ " expanded: " ++ show expand
instance Arbitrary AFISParams where
arbitrary = AFISParams <$> arbitraryBS <*> choose (2,2) <*> elements [SHA1] <*> arbitrary
where arbitraryBS = choose (3,46) >>= \sz -> B.pack <$> replicateM sz arbitrary
arbitrary = AFISParams <$> arbitraryBSof 3 46 <*> choose (2,2) <*> elements [SHA1] <*> arbitrary
instance Arbitrary ChaChaDRG where
arbitrary = drgNewTest <$> arbitrary

View File

@ -6,7 +6,6 @@ import Crypto.Hash (MD5(..), SHA1(..), SHA256(..)
, Kekkak_224(..), Kekkak_256(..), Kekkak_384(..), Kekkak_512(..)
, SHA3_224(..), SHA3_256(..), SHA3_384(..), SHA3_512(..)
, HashAlgorithm, digestFromByteString)
--import Data.Foldable (foldl')
import qualified Data.ByteString as B
import Imports
@ -116,12 +115,10 @@ macTests =
data MacIncremental a = MacIncremental ByteString ByteString (HMAC.HMAC a)
deriving (Show,Eq)
arbitraryBS = B.pack <$> (choose (1,299) >>= \i -> replicateM i arbitrary)
instance HashAlgorithm a => Arbitrary (MacIncremental a) where
arbitrary = do
key <- B.pack <$> replicateM 65 (choose (0x30,0x30)) -- B.pack arbitraryBS
msg <- B.pack <$> replicateM 2 (choose (0x40,0x40)) -- B.pack arbitraryBS
key <- arbitraryBSof 1 89
msg <- arbitraryBSof 1 99
return $ MacIncremental key msg (HMAC.hmac key msg)
data MacIncrementalList a = MacIncrementalList ByteString [ByteString] (HMAC.HMAC a)
@ -129,11 +126,9 @@ data MacIncrementalList a = MacIncrementalList ByteString [ByteString] (HMAC.HMA
instance HashAlgorithm a => Arbitrary (MacIncrementalList a) where
arbitrary = do
--key <- arbitraryBS
--msgs <- choose (1,20) >>= \i -> replicateM i arbitraryBS
key <- B.pack <$> replicateM 128 (choose (0x30,0x30)) -- B.pack arbitraryBS
msgs <- B.pack <$> replicateM 2 (choose (0x40,0x40)) -- B.pack arbitraryBS
return $ MacIncrementalList key [msgs] (HMAC.hmac key (B.concat [msgs]))
key <- arbitraryBSof 1 89
msgs <- choose (1,20) >>= \n -> replicateM n (arbitraryBSof 1 99)
return $ MacIncrementalList key msgs (HMAC.hmac key (B.concat msgs))
macIncrementalTests :: [TestTree]
macIncrementalTests =

View File

@ -14,6 +14,12 @@ newtype ChunkingLen = ChunkingLen [Int]
instance Arbitrary ChunkingLen where
arbitrary = ChunkingLen `fmap` replicateM 16 (choose (0,14))
arbitraryBS :: Int -> Gen ByteString
arbitraryBS n = B.pack `fmap` replicateM n arbitrary
arbitraryBSof :: Int -> Int -> Gen ByteString
arbitraryBSof minSize maxSize = choose (minSize, maxSize) >>= \n -> (B.pack `fmap` replicateM n arbitrary)
chunkS :: ChunkingLen -> ByteString -> [ByteString]
chunkS (ChunkingLen originalChunks) = loop originalChunks
where loop l bs