1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
import Test.QuickCheck
import Test.Framework(defaultMain, testGroup)
import Test.Framework.Providers.QuickCheck2(testProperty)
import Text.Printf
import Control.Applicative
import Data.ASN1.Get (runGet, Result(..))
import Data.ASN1.BitArray
import Data.ASN1.Stream
import Data.ASN1.Prim
import Data.ASN1.Serialize
import Data.ASN1.BinaryEncoding.Parse
import Data.ASN1.BinaryEncoding.Writer
import Data.ASN1.BinaryEncoding
import Data.ASN1.Encoding
import Data.ASN1.Types
import Data.ASN1.Types.Lowlevel
import Data.ASN1.OID
import Data.Time.Clock
import Data.Time.Calendar
import Data.Time.LocalTime
import Data.Word
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as BC
import qualified Data.Text.Lazy as T
import Control.Monad
import Control.Monad.Identity
import System.IO
instance Arbitrary ASN1Class where
arbitrary = elements [ Universal, Application, Context, Private ]
instance Arbitrary ASN1Length where
arbitrary = do
c <- choose (0,2) :: Gen Int
case c of
0 -> liftM LenShort (choose (0,0x79))
1 -> do
nb <- choose (0x80,0x1000)
return $ mkSmallestLength nb
_ -> return LenIndefinite
where
nbBytes nb = if nb > 255 then 1 + nbBytes (nb `div` 256) else 1
arbitraryDefiniteLength :: Gen ASN1Length
arbitraryDefiniteLength = arbitrary `suchThat` (\l -> l /= LenIndefinite)
arbitraryTag :: Gen ASN1Tag
arbitraryTag = choose(1,10000)
instance Arbitrary ASN1Header where
arbitrary = liftM4 ASN1Header arbitrary arbitraryTag arbitrary arbitrary
arbitraryEvents :: Gen ASN1Events
arbitraryEvents = do
hdr@(ASN1Header _ _ _ len) <- liftM4 ASN1Header arbitrary arbitraryTag (return False) arbitraryDefiniteLength
let blen = case len of
LenLong _ x -> x
LenShort x -> x
_ -> 0
pr <- liftM Primitive (arbitraryBSsized blen)
return (ASN1Events [Header hdr, pr])
newtype ASN1Events = ASN1Events [ASN1Event]
instance Show ASN1Events where
show (ASN1Events x) = show x
instance Arbitrary ASN1Events where
arbitrary = arbitraryEvents
arbitraryOID :: Gen OID
arbitraryOID = do
i1 <- choose (0,2) :: Gen Integer
i2 <- choose (0,39) :: Gen Integer
ran <- choose (0,30) :: Gen Int
l <- replicateM ran (suchThat arbitrary (\i -> i > 0))
return $ (i1:i2:l)
arbitraryBSsized :: Int -> Gen B.ByteString
arbitraryBSsized len = do
ws <- replicateM len (choose (0, 255) :: Gen Int)
return $ B.pack $ map fromIntegral ws
instance Arbitrary B.ByteString where
arbitrary = do
len <- choose (0, 529) :: Gen Int
arbitraryBSsized len
instance Arbitrary T.Text where
arbitrary = do
len <- choose (0, 529) :: Gen Int
ws <- replicateM len arbitrary
return $ T.pack ws
instance Arbitrary BitArray where
arbitrary = do
bs <- arbitrary
w <- choose (0,7) :: Gen Int
return $ toBitArray bs w
instance Arbitrary Day where
arbitrary = do
y <- choose (1951, 2050)
m <- choose (0, 11)
d <- choose (0, 31)
return $ fromGregorian y m d
instance Arbitrary DiffTime where
arbitrary = do
h <- choose (0, 23)
mi <- choose (0, 59)
se <- choose (0, 59)
return $ secondsToDiffTime (h*3600+mi*60+se)
instance Arbitrary UTCTime where
arbitrary = UTCTime <$> arbitrary <*> arbitrary
instance Arbitrary TimeZone where
arbitrary = return $ utc
instance Arbitrary ASN1TimeType where
arbitrary = elements [TimeUTC, TimeGeneralized]
instance Arbitrary ASN1StringEncoding where
arbitrary = elements [UTF8, Numeric, Printable, T61, VideoTex, IA5, Graphic, Visible, General, UTF32, BMP]
arbitraryPrintString encoding = do
let printableString = (['a'..'z'] ++ ['A'..'Z'] ++ ['0'..'9'] ++ " ()+,-./:=?")
asn1CharacterString encoding <$> replicateM 21 (elements printableString)
arbitraryBS encoding = ASN1CharacterString encoding . B.pack <$> replicateM 7 (choose (0,0xff))
arbitraryIA5String = asn1CharacterString IA5 <$> replicateM 21 (choose (toEnum 0,toEnum 127))
arbitraryUCS2 :: Gen ASN1CharacterString
arbitraryUCS2 = asn1CharacterString BMP <$> replicateM 12 (choose (toEnum 0,toEnum 0xffff))
arbitraryUnicode :: ASN1StringEncoding -> Gen ASN1CharacterString
arbitraryUnicode e = asn1CharacterString e <$> replicateM 35 (choose (toEnum 0,toEnum 0x10ffff))
instance Arbitrary ASN1CharacterString where
arbitrary = oneof
[ arbitraryUnicode UTF8
, arbitraryUnicode UTF32
, arbitraryUCS2
, arbitraryPrintString Numeric
, arbitraryPrintString Printable
, arbitraryBS T61
, arbitraryBS VideoTex
, arbitraryIA5String
, arbitraryPrintString Graphic
, arbitraryPrintString Visible
, arbitraryPrintString General
]
instance Arbitrary ASN1 where
arbitrary = oneof
[ liftM Boolean arbitrary
, liftM IntVal arbitrary
, liftM BitString arbitrary
, liftM OctetString arbitrary
, return Null
, liftM OID arbitraryOID
--, Real Double
-- , return Enumerated
, ASN1String <$> arbitrary
, ASN1Time <$> arbitrary <*> arbitrary <*> arbitrary
]
newtype ASN1s = ASN1s [ASN1]
instance Show ASN1s where
show (ASN1s x) = show x
instance Arbitrary ASN1s where
arbitrary = do
x <- choose (0,5) :: Gen Int
z <- case x of
4 -> makeList Sequence
3 -> makeList Set
_ -> resize 2 $ listOf1 arbitrary
return $ ASN1s z
where
makeList str = do
(ASN1s l) <- arbitrary
return ([Start str] ++ l ++ [End str])
prop_header_marshalling_id :: ASN1Header -> Bool
prop_header_marshalling_id v = (ofDone $ runGet getHeader $ putHeader v) == Right v
where ofDone (Done r _ _) = Right r
ofDone _ = Left "not done"
prop_event_marshalling_id :: ASN1Events -> Bool
prop_event_marshalling_id (ASN1Events e) = (parseLBS $ toLazyByteString e) == Right e
prop_asn1_der_marshalling_id v = (decodeASN1 DER . encodeASN1 DER) v `assertEq` Right v
where assertEq got expected
| got /= expected = error ("got: " ++ show got ++ " expected: " ++ show expected)
| otherwise = True
marshallingTests = testGroup "Marshalling"
[ testProperty "Header" prop_header_marshalling_id
, testProperty "Event" prop_event_marshalling_id
, testProperty "DER" prop_asn1_der_marshalling_id
]
main = defaultMain [marshallingTests]
|