|
Packit |
141393 |
{-# LANGUAGE ExistentialQuantification #-}
|
|
Packit |
141393 |
module Utils where
|
|
Packit |
141393 |
|
|
Packit |
141393 |
import Control.Applicative
|
|
Packit |
141393 |
import Control.Monad (replicateM)
|
|
Packit |
141393 |
import Data.Char
|
|
Packit |
141393 |
import Data.Word
|
|
Packit |
141393 |
import Data.List
|
|
Packit |
141393 |
import Data.ByteString (ByteString)
|
|
Packit |
141393 |
import qualified Data.ByteString as B
|
|
Packit |
141393 |
import qualified Data.ByteString.Lazy as L
|
|
Packit |
141393 |
import Crypto.Random
|
|
Packit |
141393 |
import Crypto.Number.Serialize (os2ip)
|
|
Packit |
141393 |
import Prelude
|
|
Packit |
141393 |
|
|
Packit |
141393 |
import Test.Tasty.QuickCheck
|
|
Packit |
141393 |
import Test.Tasty.HUnit ((@=?))
|
|
Packit |
141393 |
|
|
Packit |
141393 |
newtype TestDRG = TestDRG (Word64, Word64, Word64, Word64, Word64)
|
|
Packit |
141393 |
deriving (Show,Eq)
|
|
Packit |
141393 |
|
|
Packit |
141393 |
instance Arbitrary TestDRG where
|
|
Packit |
141393 |
arbitrary = TestDRG `fmap` arbitrary
|
|
Packit |
141393 |
|
|
Packit |
141393 |
withTestDRG (TestDRG l) f = fst $ withDRG (drgNewTest l) f
|
|
Packit |
141393 |
|
|
Packit |
141393 |
newtype ChunkingLen = ChunkingLen [Int]
|
|
Packit |
141393 |
deriving (Show,Eq)
|
|
Packit |
141393 |
|
|
Packit |
141393 |
instance Arbitrary ChunkingLen where
|
|
Packit |
141393 |
arbitrary = ChunkingLen `fmap` replicateM 16 (choose (0,14))
|
|
Packit |
141393 |
|
|
Packit |
141393 |
newtype ChunkingLen0_127 = ChunkingLen0_127 [Int]
|
|
Packit |
141393 |
deriving (Show,Eq)
|
|
Packit |
141393 |
|
|
Packit |
141393 |
instance Arbitrary ChunkingLen0_127 where
|
|
Packit |
141393 |
arbitrary = ChunkingLen0_127 `fmap` replicateM 16 (choose (0,127))
|
|
Packit |
141393 |
|
|
Packit |
141393 |
|
|
Packit |
141393 |
newtype ArbitraryBS0_2901 = ArbitraryBS0_2901 ByteString
|
|
Packit |
141393 |
deriving (Show,Eq,Ord)
|
|
Packit |
141393 |
|
|
Packit |
141393 |
instance Arbitrary ArbitraryBS0_2901 where
|
|
Packit |
141393 |
arbitrary = ArbitraryBS0_2901 `fmap` arbitraryBSof 0 2901
|
|
Packit |
141393 |
|
|
Packit |
141393 |
newtype Int0_2901 = Int0_2901 Int
|
|
Packit |
141393 |
deriving (Show,Eq,Ord)
|
|
Packit |
141393 |
|
|
Packit |
141393 |
newtype Int1_2901 = Int1_2901 Int
|
|
Packit |
141393 |
deriving (Show,Eq,Ord)
|
|
Packit |
141393 |
|
|
Packit |
141393 |
instance Arbitrary Int0_2901 where
|
|
Packit |
141393 |
arbitrary = Int0_2901 `fmap` choose (0,2901)
|
|
Packit |
141393 |
|
|
Packit |
141393 |
instance Arbitrary Int1_2901 where
|
|
Packit |
141393 |
arbitrary = Int1_2901 `fmap` choose (1,2901)
|
|
Packit |
141393 |
|
|
Packit |
141393 |
-- | a integer wrapper with a better range property
|
|
Packit |
141393 |
newtype QAInteger = QAInteger { getQAInteger :: Integer }
|
|
Packit |
141393 |
deriving (Show,Eq)
|
|
Packit |
141393 |
|
|
Packit |
141393 |
instance Arbitrary QAInteger where
|
|
Packit |
141393 |
arbitrary = oneof
|
|
Packit |
141393 |
[ QAInteger . fromIntegral <$> (choose (0, 65536) :: Gen Int) -- small integer
|
|
Packit |
141393 |
, larger <$> choose (0,4096) <*> choose (0, 65536) -- medium integer
|
|
Packit |
141393 |
, QAInteger . os2ip . B.pack <$> (choose (0,32) >>= \n -> replicateM n arbitrary) -- [ 0 .. 2^32 ] sized integer
|
|
Packit |
141393 |
]
|
|
Packit |
141393 |
where
|
|
Packit |
141393 |
larger :: Int -> Int -> QAInteger
|
|
Packit |
141393 |
larger p b = QAInteger (fromIntegral p * somePrime + fromIntegral b)
|
|
Packit |
141393 |
|
|
Packit |
141393 |
somePrime :: Integer
|
|
Packit |
141393 |
somePrime = 18446744073709551557
|
|
Packit |
141393 |
|
|
Packit |
141393 |
arbitraryBS :: Int -> Gen ByteString
|
|
Packit |
141393 |
arbitraryBS n = B.pack `fmap` replicateM n arbitrary
|
|
Packit |
141393 |
|
|
Packit |
141393 |
arbitraryBSof :: Int -> Int -> Gen ByteString
|
|
Packit |
141393 |
arbitraryBSof minSize maxSize = choose (minSize, maxSize) >>= \n -> (B.pack `fmap` replicateM n arbitrary)
|
|
Packit |
141393 |
|
|
Packit |
141393 |
chunkS :: ChunkingLen -> ByteString -> [ByteString]
|
|
Packit |
141393 |
chunkS (ChunkingLen originalChunks) = loop originalChunks
|
|
Packit |
141393 |
where loop l bs
|
|
Packit |
141393 |
| B.null bs = []
|
|
Packit |
141393 |
| otherwise =
|
|
Packit |
141393 |
case l of
|
|
Packit |
141393 |
(x:xs) -> let (b1, b2) = B.splitAt x bs in b1 : loop xs b2
|
|
Packit |
141393 |
[] -> loop originalChunks bs
|
|
Packit |
141393 |
|
|
Packit |
141393 |
chunksL :: ChunkingLen -> L.ByteString -> L.ByteString
|
|
Packit |
141393 |
chunksL (ChunkingLen originalChunks) = L.fromChunks . loop originalChunks . L.toChunks
|
|
Packit |
141393 |
where loop _ [] = []
|
|
Packit |
141393 |
loop l (b:bs)
|
|
Packit |
141393 |
| B.null b = loop l bs
|
|
Packit |
141393 |
| otherwise =
|
|
Packit |
141393 |
case l of
|
|
Packit |
141393 |
(x:xs) -> let (b1, b2) = B.splitAt x b in b1 : loop xs (b2:bs)
|
|
Packit |
141393 |
[] -> loop originalChunks (b:bs)
|
|
Packit |
141393 |
|
|
Packit |
141393 |
katZero :: Int
|
|
Packit |
141393 |
katZero = 0
|
|
Packit |
141393 |
|
|
Packit |
141393 |
--hexalise :: String -> [Word8]
|
|
Packit |
141393 |
hexalise s = concatMap (\c -> [ hex $ c `div` 16, hex $ c `mod` 16 ]) s
|
|
Packit |
141393 |
where hex i
|
|
Packit |
141393 |
| i >= 0 && i <= 9 = fromIntegral (ord '0') + i
|
|
Packit |
141393 |
| i >= 10 && i <= 15 = fromIntegral (ord 'a') + i - 10
|
|
Packit |
141393 |
| otherwise = 0
|
|
Packit |
141393 |
|
|
Packit |
141393 |
splitB :: Int -> ByteString -> [ByteString]
|
|
Packit |
141393 |
splitB l b =
|
|
Packit |
141393 |
if B.length b > l
|
|
Packit |
141393 |
then
|
|
Packit |
141393 |
let (b1, b2) = B.splitAt l b in
|
|
Packit |
141393 |
b1 : splitB l b2
|
|
Packit |
141393 |
else
|
|
Packit |
141393 |
[ b ]
|
|
Packit |
141393 |
|
|
Packit |
141393 |
assertBytesEq :: ByteString -> ByteString -> Bool
|
|
Packit |
141393 |
assertBytesEq b1 b2 | b1 /= b2 = error ("expected: " ++ show b1 ++ " got: " ++ show b2)
|
|
Packit |
141393 |
| otherwise = True
|
|
Packit |
141393 |
|
|
Packit |
141393 |
assertEq :: (Show a, Eq a) => a -> a -> Bool
|
|
Packit |
141393 |
assertEq b1 b2 | b1 /= b2 = error ("expected: " ++ show b1 ++ " got: " ++ show b2)
|
|
Packit |
141393 |
| otherwise = True
|
|
Packit |
141393 |
|
|
Packit |
141393 |
propertyEq :: (Show a, Eq a) => a -> a -> Bool
|
|
Packit |
141393 |
propertyEq = assertEq
|
|
Packit |
141393 |
|
|
Packit |
141393 |
data PropertyTest =
|
|
Packit |
141393 |
forall a . (Show a, Eq a) => EqTest String a a
|
|
Packit |
141393 |
|
|
Packit |
141393 |
type PropertyName = String
|
|
Packit |
141393 |
|
|
Packit |
141393 |
eqTest :: (Show a, Eq a)
|
|
Packit |
141393 |
=> PropertyName
|
|
Packit |
141393 |
-> a -- ^ expected value
|
|
Packit |
141393 |
-> a -- ^ got
|
|
Packit |
141393 |
-> PropertyTest
|
|
Packit |
141393 |
eqTest name a b = EqTest name a b
|
|
Packit |
141393 |
|
|
Packit |
141393 |
propertyHold :: [PropertyTest] -> Bool
|
|
Packit |
141393 |
propertyHold l =
|
|
Packit |
141393 |
case foldl runProperty [] l of
|
|
Packit |
141393 |
[] -> True
|
|
Packit |
141393 |
failed -> error (intercalate "\n" failed)
|
|
Packit |
141393 |
where
|
|
Packit |
141393 |
runProperty acc (EqTest name a b)
|
|
Packit |
141393 |
| a == b = acc
|
|
Packit |
141393 |
| otherwise =
|
|
Packit |
141393 |
(name ++ ": expected " ++ show a ++ " but got: " ++ show b) : acc
|
|
Packit |
141393 |
|
|
Packit |
141393 |
propertyHoldCase :: [PropertyTest] -> IO ()
|
|
Packit |
141393 |
propertyHoldCase l = True @=? propertyHold l
|