| 1 |
{-# LANGUAGE OverloadedStrings #-} |
| 2 |
-- | |
| 3 |
-- Module : Test.Tasty.KAT.FileLoader |
| 4 |
-- License : MIT |
| 5 |
-- Maintainer : Vincent Hanquez <[email protected]> |
| 6 |
-- Stability : experimental |
| 7 |
-- Portability : Good |
| 8 |
-- |
| 9 |
-- extra loaders helpers |
| 10 |
-- |
| 11 |
module Test.Tasty.KAT.FileLoader |
| 12 |
( katLoader |
| 13 |
, katLoaderSimple |
| 14 |
-- * generic helpers on TestResource |
| 15 |
, mapTestUnitValues |
| 16 |
, mapTestUnits |
| 17 |
-- * common helpers on TestResource |
| 18 |
, mapTestUnitValuesBase64 |
| 19 |
, mapTestUnitValuesBase16 |
| 20 |
-- * common value decoding helpers |
| 21 |
, valueUnbase16 |
| 22 |
, valueUnbase64 |
| 23 |
, valueInteger |
| 24 |
, valueHexInteger |
| 25 |
-- * associated hierarchy of KAT types |
| 26 |
, TestResource |
| 27 |
, TestGroup |
| 28 |
, TestUnit |
| 29 |
) where |
| 30 |
|
| 31 |
import Control.Arrow (second) |
| 32 |
import Data.Bits |
| 33 |
import qualified Data.ByteString.Internal as B |
| 34 |
import qualified Data.ByteString.Unsafe as B |
| 35 |
import Data.ByteString (ByteString) |
| 36 |
import Data.ByteString.Char8 () -- for Bytestring OverloadedString instance on old ghc |
| 37 |
import Data.List |
| 38 |
import Data.Word |
| 39 |
import Foreign.Storable |
| 40 |
import Foreign.Ptr |
| 41 |
|
| 42 |
import Test.Tasty.KAT.Internal |
| 43 |
|
| 44 |
type TestResource a = [(String, TestGroup a)] |
| 45 |
type TestGroup a = [TestUnit a] |
| 46 |
type TestUnit a = [a] |
| 47 |
|
| 48 |
-- | From a simple KAT file, extract |
| 49 |
-- |
| 50 |
-- * lines starting by #, are assumed to be comment |
| 51 |
-- |
| 52 |
-- format should be the following: |
| 53 |
-- |
| 54 |
-- > skipped .. |
| 55 |
-- > skipped .. |
| 56 |
-- > [group1] |
| 57 |
-- > |
| 58 |
-- > f1= v1 |
| 59 |
-- > f2= v2 |
| 60 |
-- > ... |
| 61 |
-- > |
| 62 |
-- > f1= v3 |
| 63 |
-- > f2= v4 |
| 64 |
-- > ... |
| 65 |
-- > |
| 66 |
-- > [group2] |
| 67 |
-- > ... |
| 68 |
katLoaderSimple :: [String] -> TestResource (String, String) |
| 69 |
katLoaderSimple = katLoader '=' "#" |
| 70 |
|
| 71 |
katLoader :: Char -- ^ key value separator, e.g. '=' |
| 72 |
-> String -- ^ line comment, e.g. "--" "#" |
| 73 |
-> [String] -- ^ input lines |
| 74 |
-> TestResource (String, String) |
| 75 |
katLoader kvSep lineComment = |
| 76 |
map (second (map (map kv))) |
| 77 |
. removeEmpty |
| 78 |
. map (second (splitWhen null)) -- split a group of lines into a group of tests |
| 79 |
. groupify "" [] |
| 80 |
. map noTrailing |
| 81 |
. filter (not . isComment) |
| 82 |
where isComment = isPrefixOf lineComment |
| 83 |
|
| 84 |
removeEmpty = filter ((/= []) . snd) |
| 85 |
|
| 86 |
groupify :: String -> [String] -> [String] -> [(String, [String])] |
| 87 |
groupify gname acc [] = [(gname, reverse acc)] |
| 88 |
groupify gname acc (x:xs) = |
| 89 |
case getGroupHeader x of |
| 90 |
Just hdr -> (gname, reverse acc) : groupify hdr [] xs |
| 91 |
Nothing -> groupify gname (x:acc) xs |
| 92 |
|
| 93 |
kv :: String -> (String, String) |
| 94 |
kv s = case break (== kvSep) s of |
| 95 |
(k, c:v) |
| 96 |
| c == kvSep -> (stripSpaces k, stripSpaces v) |
| 97 |
| otherwise -> (stripSpaces k, stripSpaces v) |
| 98 |
(_, _) -> (s, "") -- no error handling .. |
| 99 |
|
| 100 |
getGroupHeader :: String -> Maybe String |
| 101 |
getGroupHeader s |
| 102 |
| isPrefixOf "[" s && isSuffixOf "]" s = Just . drop 1 . reverse . drop 1 . reverse $ s |
| 103 |
| otherwise = Nothing |
| 104 |
|
| 105 |
noTrailing = reverse . dropWhile (== ' ') . reverse |
| 106 |
|
| 107 |
splitWhen :: (a -> Bool) -> [a] -> [[a]] |
| 108 |
splitWhen p s = case dropWhile p s of |
| 109 |
[] -> [] |
| 110 |
s' -> w : splitWhen p s'' |
| 111 |
where (w, s'') = break p s' |
| 112 |
stripSpaces = dropWhile (== ' ') . reverse . dropWhile (== ' ') . reverse |
| 113 |
|
| 114 |
mapTestUnitValues :: (String -> a) -> TestResource (String, String) -> TestResource (String,a) |
| 115 |
mapTestUnitValues f = map (second (map (map (\(k,v) -> (k, f v))))) |
| 116 |
|
| 117 |
mapTestUnits :: (TestUnit (String,a) -> TestUnit b) |
| 118 |
-> TestResource (String,a) |
| 119 |
-> TestResource b |
| 120 |
mapTestUnits f = map (second (map f)) |
| 121 |
|
| 122 |
mapTestUnitValuesBase64 :: TestResource (String, String) -> TestResource (String, ByteString) |
| 123 |
mapTestUnitValuesBase64 = mapTestUnitValues valueUnbase64 |
| 124 |
|
| 125 |
mapTestUnitValuesBase16 :: TestResource (String, String) -> TestResource (String, ByteString) |
| 126 |
mapTestUnitValuesBase16 = mapTestUnitValues valueUnbase16 |
| 127 |
|
| 128 |
-- expect an ascii string. |
| 129 |
valueUnbase64 :: String -> ByteString |
| 130 |
valueUnbase64 s |
| 131 |
| (length s `mod` 4) /= 0 = error ("decodiong base64 not proper length: " ++ s) |
| 132 |
| otherwise = unsafeCreateUptoN maxSz $ \ptr -> do |
| 133 |
szRemove <- loop s ptr |
| 134 |
return (maxSz - szRemove) |
| 135 |
where maxSz = (length s `div` 4) * 3 |
| 136 |
loop [] _ = return 0 |
| 137 |
loop (w:x:'=':'=':[]) ptr = do |
| 138 |
let w' = rset w |
| 139 |
x' = rset x |
| 140 |
poke ptr ((w' `shiftL` 2) .|. (x' `shiftR` 4)) |
| 141 |
return 2 |
| 142 |
loop (w:x:y:'=':[]) ptr = do |
| 143 |
let w' = rset w |
| 144 |
x' = rset x |
| 145 |
y' = rset y |
| 146 |
poke ptr ((w' `shiftL` 2) .|. (x' `shiftR` 4)) |
| 147 |
poke (ptr `plusPtr` 1) ((x' `shiftL` 4) .|. (y' `shiftR` 2)) |
| 148 |
return 1 |
| 149 |
loop (w:x:y:z:r) ptr = do |
| 150 |
let w' = rset w |
| 151 |
x' = rset x |
| 152 |
y' = rset y |
| 153 |
z' = rset z |
| 154 |
poke ptr ((w' `shiftL` 2) .|. (x' `shiftR` 4)) |
| 155 |
poke (ptr `plusPtr` 1) ((x' `shiftL` 4) .|. (y' `shiftR` 2)) |
| 156 |
poke (ptr `plusPtr` 2) ((y' `shiftL` 6) .|. z') |
| 157 |
loop r (ptr `plusPtr` 3) |
| 158 |
loop _ _ = error ("internal error in base64 decoding") |
| 159 |
|
| 160 |
rset :: Char -> Word8 |
| 161 |
rset c |
| 162 |
| cval <= 0xff = B.unsafeIndex rsetTable cval |
| 163 |
| otherwise = 0xff |
| 164 |
where cval = fromEnum c |
| 165 |
-- dict = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" |
| 166 |
rsetTable = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\ |
| 167 |
\\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\ |
| 168 |
\\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x3e\xff\xff\xff\x3f\ |
| 169 |
\\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\xff\xff\xff\xff\xff\xff\ |
| 170 |
\\xff\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\ |
| 171 |
\\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\xff\xff\xff\xff\xff\ |
| 172 |
\\xff\x1a\x1b\x1c\x1d\x1e\x1f\x20\x21\x22\x23\x24\x25\x26\x27\x28\ |
| 173 |
\\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31\x32\x33\xff\xff\xff\xff\xff\ |
| 174 |
\\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\ |
| 175 |
\\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\ |
| 176 |
\\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\ |
| 177 |
\\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\ |
| 178 |
\\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\ |
| 179 |
\\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\ |
| 180 |
\\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\ |
| 181 |
\\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" |
| 182 |
|
| 183 |
-- expect an ascii string. |
| 184 |
valueUnbase16 :: String -> ByteString |
| 185 |
valueUnbase16 s |
| 186 |
| odd (length s) = error ("decoding base16 not proper length: " ++ s) |
| 187 |
| otherwise = B.unsafeCreate (length s `div` 2) (loop s) |
| 188 |
where loop [] _ = return () |
| 189 |
loop (x1:x2:xs) ptr = do |
| 190 |
poke ptr ((unhex x1 `shiftL` 4) .|. unhex x2) |
| 191 |
loop xs (ptr `plusPtr` 2) |
| 192 |
loop _ _ = error "internal error in base16 decoding" |
| 193 |
unhex :: Char -> Word8 |
| 194 |
unhex c |
| 195 |
| c >= '0' && c <= '9' = fromIntegral (fromEnum c - fromEnum '0') |
| 196 |
| c >= 'a' && c <= 'f' = 10 + fromIntegral (fromEnum c - fromEnum 'a') |
| 197 |
| c >= 'A' && c <= 'F' = 10 + fromIntegral (fromEnum c - fromEnum 'A') |
| 198 |
| otherwise = error ("invalid base16 character " ++ show c ++ " in " ++ show s) |
| 199 |
|
| 200 |
valueInteger :: String -> Integer |
| 201 |
valueInteger s = read s |
| 202 |
|
| 203 |
valueHexInteger :: String -> Integer |
| 204 |
valueHexInteger s = read ("0x" ++ s) |