diff options
author | Clint Adams <clint@debian.org> | 2012-04-26 20:28:40 -0400 |
---|---|---|
committer | Clint Adams <clint@debian.org> | 2012-04-26 20:28:40 -0400 |
commit | 8c34729c62ba64c810fbfa73719ae7f7110c0fbe (patch) | |
tree | c510a323f4c4a49cfb19ca3ba3132e8a80dc48d1 | |
parent | 1ddca48c27cece9352e85ce6188f697eb9124750 (diff) |
More laziness.
-rw-r--r-- | Codec/Encryption/OpenPGP/ASCIIArmor.hs | 10 | ||||
-rw-r--r-- | Codec/Encryption/OpenPGP/ASCIIArmor/Decode.hs | 23 | ||||
-rw-r--r-- | Codec/Encryption/OpenPGP/ASCIIArmor/Encode.hs | 55 | ||||
-rw-r--r-- | Data/Digest/CRC24.hs | 13 | ||||
-rw-r--r-- | tests/suite.hs | 39 |
5 files changed, 87 insertions, 53 deletions
diff --git a/Codec/Encryption/OpenPGP/ASCIIArmor.hs b/Codec/Encryption/OpenPGP/ASCIIArmor.hs index 6d0c172..fcb7337 100644 --- a/Codec/Encryption/OpenPGP/ASCIIArmor.hs +++ b/Codec/Encryption/OpenPGP/ASCIIArmor.hs | |||
@@ -4,12 +4,14 @@ | |||
4 | -- (See the LICENSE file). | 4 | -- (See the LICENSE file). |
5 | 5 | ||
6 | module Codec.Encryption.OpenPGP.ASCIIArmor ( | 6 | module Codec.Encryption.OpenPGP.ASCIIArmor ( |
7 | encode | 7 | decode |
8 | , decode | 8 | , decodeLazy |
9 | , encode | ||
10 | , encodeLazy | ||
9 | , parseArmor | 11 | , parseArmor |
10 | , multipartMerge | 12 | , multipartMerge |
11 | ) where | 13 | ) where |
12 | 14 | ||
13 | import Codec.Encryption.OpenPGP.ASCIIArmor.Encode (encode) | 15 | import Codec.Encryption.OpenPGP.ASCIIArmor.Decode (decode, decodeLazy, parseArmor) |
14 | import Codec.Encryption.OpenPGP.ASCIIArmor.Decode (decode, parseArmor) | 16 | import Codec.Encryption.OpenPGP.ASCIIArmor.Encode (encode, encodeLazy) |
15 | import Codec.Encryption.OpenPGP.ASCIIArmor.Multipart (multipartMerge) | 17 | import Codec.Encryption.OpenPGP.ASCIIArmor.Multipart (multipartMerge) |
diff --git a/Codec/Encryption/OpenPGP/ASCIIArmor/Decode.hs b/Codec/Encryption/OpenPGP/ASCIIArmor/Decode.hs index b89fbfa..bfaef39 100644 --- a/Codec/Encryption/OpenPGP/ASCIIArmor/Decode.hs +++ b/Codec/Encryption/OpenPGP/ASCIIArmor/Decode.hs | |||
@@ -7,12 +7,15 @@ | |||
7 | module Codec.Encryption.OpenPGP.ASCIIArmor.Decode ( | 7 | module Codec.Encryption.OpenPGP.ASCIIArmor.Decode ( |
8 | parseArmor | 8 | parseArmor |
9 | , decode | 9 | , decode |
10 | , decodeLazy | ||
10 | ) where | 11 | ) where |
11 | 12 | ||
12 | import Codec.Encryption.OpenPGP.ASCIIArmor.Types | 13 | import Codec.Encryption.OpenPGP.ASCIIArmor.Types |
13 | import Codec.Encryption.OpenPGP.ASCIIArmor.Utils | 14 | import Codec.Encryption.OpenPGP.ASCIIArmor.Utils |
14 | import Control.Applicative (many, (<|>), (<$>), Alternative, (<*), (<*>), (*>), optional) | 15 | import Control.Applicative (many, (<|>), (<$>), Alternative, (<*), (<*>), (*>), optional) |
15 | import Data.Attoparsec.ByteString (Parser, many1, string, inClass, notInClass, satisfy, word8, (<?>), parse, IResult(..)) | 16 | import Data.Attoparsec.ByteString (Parser, many1, string, inClass, notInClass, satisfy, word8, (<?>)) |
17 | import qualified Data.Attoparsec.ByteString as AS | ||
18 | import qualified Data.Attoparsec.ByteString.Lazy as AL | ||
16 | import Data.Attoparsec.ByteString.Char8 (isDigit_w8, anyChar) | 19 | import Data.Attoparsec.ByteString.Char8 (isDigit_w8, anyChar) |
17 | import Data.Attoparsec.Combinator (manyTill) | 20 | import Data.Attoparsec.Combinator (manyTill) |
18 | import Data.Bits (shiftL) | 21 | import Data.Bits (shiftL) |
@@ -29,11 +32,17 @@ import Data.String (IsString, fromString) | |||
29 | import Data.Word (Word32) | 32 | import Data.Word (Word32) |
30 | 33 | ||
31 | decode :: IsString e => ByteString -> Either e [Armor] | 34 | decode :: IsString e => ByteString -> Either e [Armor] |
32 | decode bs = go (parse parseArmors bs) | 35 | decode bs = go (AS.parse parseArmors bs) |
33 | where | 36 | where |
34 | go (Fail t c e) = Left (fromString e) | 37 | go (AS.Fail t c e) = Left (fromString e) |
35 | go (Partial cont) = go (cont B.empty) | 38 | go (AS.Partial cont) = go (cont B.empty) |
36 | go (Done _ r) = Right r | 39 | go (AS.Done _ r) = Right r |
40 | |||
41 | decodeLazy :: IsString e => BL.ByteString -> Either e [Armor] | ||
42 | decodeLazy bs = go (AL.parse parseArmors bs) | ||
43 | where | ||
44 | go (AL.Fail t c e) = Left (fromString e) | ||
45 | go (AL.Done _ r) = Right r | ||
37 | 46 | ||
38 | parseArmors :: Parser [Armor] | 47 | parseArmors :: Parser [Armor] |
39 | parseArmors = many parseArmor | 48 | parseArmors = many parseArmor |
@@ -110,8 +119,8 @@ aType :: ArmorType -> ByteString | |||
110 | aType (ArmorMessage) = BC8.pack "MESSAGE" | 119 | aType (ArmorMessage) = BC8.pack "MESSAGE" |
111 | aType (ArmorPublicKeyBlock) = BC8.pack "PUBLIC KEY BLOCK" | 120 | aType (ArmorPublicKeyBlock) = BC8.pack "PUBLIC KEY BLOCK" |
112 | aType (ArmorPrivateKeyBlock) = BC8.pack "PRIVATE KEY BLOCK" | 121 | aType (ArmorPrivateKeyBlock) = BC8.pack "PRIVATE KEY BLOCK" |
113 | aType (ArmorSplitMessage x y) = BC8.pack "MESSAGE, PART " `B.append` (l2s x) `B.append` BC8.singleton '/' `B.append` (l2s y) | 122 | aType (ArmorSplitMessage x y) = BC8.pack "MESSAGE, PART " `B.append` l2s x `B.append` BC8.singleton '/' `B.append` l2s y |
114 | aType (ArmorSplitMessageIndefinite x) = BC8.pack "MESSAGE, PART " `B.append` (l2s x) | 123 | aType (ArmorSplitMessageIndefinite x) = BC8.pack "MESSAGE, PART " `B.append` l2s x |
115 | aType (ArmorSignature) = BC8.pack "SIGNATURE" | 124 | aType (ArmorSignature) = BC8.pack "SIGNATURE" |
116 | 125 | ||
117 | l2s :: BL.ByteString -> ByteString | 126 | l2s :: BL.ByteString -> ByteString |
diff --git a/Codec/Encryption/OpenPGP/ASCIIArmor/Encode.hs b/Codec/Encryption/OpenPGP/ASCIIArmor/Encode.hs index 06d7f1a..cb7eb3d 100644 --- a/Codec/Encryption/OpenPGP/ASCIIArmor/Encode.hs +++ b/Codec/Encryption/OpenPGP/ASCIIArmor/Encode.hs | |||
@@ -5,66 +5,71 @@ | |||
5 | 5 | ||
6 | module Codec.Encryption.OpenPGP.ASCIIArmor.Encode ( | 6 | module Codec.Encryption.OpenPGP.ASCIIArmor.Encode ( |
7 | encode | 7 | encode |
8 | , encodeLazy | ||
8 | ) where | 9 | ) where |
9 | 10 | ||
10 | import Codec.Encryption.OpenPGP.ASCIIArmor.Types | 11 | import Codec.Encryption.OpenPGP.ASCIIArmor.Types |
11 | import Data.ByteString (ByteString) | 12 | import Data.ByteString.Lazy (ByteString) |
12 | import qualified Data.ByteString as B | 13 | import qualified Data.ByteString as B |
13 | import qualified Data.ByteString.Lazy as BL | 14 | import qualified Data.ByteString.Lazy as BL |
14 | import qualified Data.ByteString.Char8 as BC8 | 15 | import qualified Data.ByteString.Char8 as BC8 |
16 | import qualified Data.ByteString.Lazy.Char8 as BLC8 | ||
15 | import qualified Data.ByteString.Base64 as Base64 | 17 | import qualified Data.ByteString.Base64 as Base64 |
16 | import Data.Digest.CRC24 (crc24) | 18 | import Data.Digest.CRC24 (crc24Lazy) |
17 | import Data.Serialize (put) | 19 | import Data.Serialize (put) |
18 | import Data.Serialize.Put (runPut, putWord32be) | 20 | import Data.Serialize.Put (runPutLazy, putWord32be) |
19 | import Data.String (IsString, fromString) | 21 | import Data.String (IsString, fromString) |
20 | 22 | ||
21 | encode :: [Armor] -> ByteString | 23 | encode :: [Armor] -> B.ByteString |
22 | encode = B.concat . map armor | 24 | encode = B.concat . BL.toChunks . encodeLazy |
25 | |||
26 | encodeLazy :: [Armor] -> ByteString | ||
27 | encodeLazy = BL.concat . map armor | ||
23 | 28 | ||
24 | armor :: Armor -> ByteString | 29 | armor :: Armor -> ByteString |
25 | armor (Armor atype ahs bs) = beginLine atype `B.append` armorHeaders ahs `B.append` blankLine `B.append` armorData (B.concat . BL.toChunks $ bs) `B.append` armorChecksum (B.concat . BL.toChunks $ bs) `B.append` endLine atype | 30 | armor (Armor atype ahs bs) = beginLine atype `BL.append` armorHeaders ahs `BL.append` blankLine `BL.append` armorData bs `BL.append` armorChecksum bs `BL.append` endLine atype |
26 | armor (ClearSigned chs ctxt csig) = BC8.pack "-----BEGIN PGP SIGNED MESSAGE-----\n" `B.append` armorHeaders chs `B.append` blankLine `B.append` dashEscape (B.concat . BL.toChunks $ ctxt) `B.append` armor csig | 31 | armor (ClearSigned chs ctxt csig) = BLC8.pack "-----BEGIN PGP SIGNED MESSAGE-----\n" `BL.append` armorHeaders chs `BL.append` blankLine `BL.append` dashEscape ctxt `BL.append` armor csig |
27 | 32 | ||
28 | blankLine :: ByteString | 33 | blankLine :: ByteString |
29 | blankLine = BC8.singleton '\n' | 34 | blankLine = BLC8.singleton '\n' |
30 | 35 | ||
31 | beginLine :: ArmorType -> ByteString | 36 | beginLine :: ArmorType -> ByteString |
32 | beginLine atype = BC8.pack "-----BEGIN PGP " `B.append` aType atype `B.append` BC8.pack "-----\n" | 37 | beginLine atype = BLC8.pack "-----BEGIN PGP " `BL.append` aType atype `BL.append` BLC8.pack "-----\n" |
33 | 38 | ||
34 | endLine :: ArmorType -> ByteString | 39 | endLine :: ArmorType -> ByteString |
35 | endLine atype = BC8.pack "-----END PGP " `B.append` aType atype `B.append` BC8.pack "-----\n" | 40 | endLine atype = BLC8.pack "-----END PGP " `BL.append` aType atype `BL.append` BLC8.pack "-----\n" |
36 | 41 | ||
37 | aType :: ArmorType -> ByteString | 42 | aType :: ArmorType -> ByteString |
38 | aType (ArmorMessage) = BC8.pack "MESSAGE" | 43 | aType (ArmorMessage) = BLC8.pack "MESSAGE" |
39 | aType (ArmorPublicKeyBlock) = BC8.pack "PUBLIC KEY BLOCK" | 44 | aType (ArmorPublicKeyBlock) = BLC8.pack "PUBLIC KEY BLOCK" |
40 | aType (ArmorPrivateKeyBlock) = BC8.pack "PRIVATE KEY BLOCK" | 45 | aType (ArmorPrivateKeyBlock) = BLC8.pack "PRIVATE KEY BLOCK" |
41 | aType (ArmorSplitMessage x y) = BC8.pack $ "MESSAGE, PART " ++ show x ++ "/" ++ show y | 46 | aType (ArmorSplitMessage x y) = BLC8.pack $ "MESSAGE, PART " ++ show x ++ "/" ++ show y |
42 | aType (ArmorSplitMessageIndefinite x) = BC8.pack $ "MESSAGE, PART " ++ show x | 47 | aType (ArmorSplitMessageIndefinite x) = BLC8.pack $ "MESSAGE, PART " ++ show x |
43 | aType (ArmorSignature) = BC8.pack "SIGNATURE" | 48 | aType (ArmorSignature) = BLC8.pack "SIGNATURE" |
44 | 49 | ||
45 | armorHeaders :: [(String, String)] -> ByteString | 50 | armorHeaders :: [(String, String)] -> ByteString |
46 | armorHeaders ahs = BC8.unlines . map armorHeader $ ahs | 51 | armorHeaders ahs = BLC8.unlines . map armorHeader $ ahs |
47 | where | 52 | where |
48 | armorHeader :: (String, String) -> ByteString | 53 | armorHeader :: (String, String) -> ByteString |
49 | armorHeader (k, v) = BC8.pack k `B.append` BC8.pack ": " `B.append` BC8.pack v | 54 | armorHeader (k, v) = BLC8.pack k `BL.append` BLC8.pack ": " `BL.append` BLC8.pack v |
50 | 55 | ||
51 | armorData :: ByteString -> ByteString | 56 | armorData :: ByteString -> ByteString |
52 | armorData = BC8.unlines . wordWrap 64 . Base64.encode | 57 | armorData = BLC8.unlines . wordWrap 64 . BL.fromChunks . return . Base64.encode . B.concat . BL.toChunks |
53 | 58 | ||
54 | wordWrap :: Int -> ByteString -> [ByteString] | 59 | wordWrap :: Int -> ByteString -> [ByteString] |
55 | wordWrap lw bs | 60 | wordWrap lw bs |
56 | | B.null bs = [] | 61 | | BL.null bs = [] |
57 | | lw < 1 || lw > 76 = wordWrap 76 bs | 62 | | lw < 1 || lw > 76 = wordWrap 76 bs |
58 | | otherwise = B.take lw bs : wordWrap lw (B.drop lw bs) | 63 | | otherwise = BL.take (fromIntegral lw) bs : wordWrap lw (BL.drop (fromIntegral lw) bs) |
59 | 64 | ||
60 | armorChecksum :: ByteString -> ByteString | 65 | armorChecksum :: ByteString -> ByteString |
61 | armorChecksum = BC8.cons '=' . armorData . B.tail . runPut . putWord32be . crc24 | 66 | armorChecksum = BLC8.cons '=' . armorData . BL.tail . runPutLazy . putWord32be . crc24Lazy |
62 | 67 | ||
63 | dashEscape :: ByteString -> ByteString | 68 | dashEscape :: ByteString -> ByteString |
64 | dashEscape = BC8.unlines . map escapeLine . BC8.lines | 69 | dashEscape = BLC8.unlines . map escapeLine . BLC8.lines |
65 | where | 70 | where |
66 | escapeLine :: ByteString -> ByteString | 71 | escapeLine :: ByteString -> ByteString |
67 | escapeLine l | 72 | escapeLine l |
68 | | BC8.singleton '-' `B.isPrefixOf` l = BC8.pack "- " `B.append` l | 73 | | BLC8.singleton '-' `BL.isPrefixOf` l = BLC8.pack "- " `BL.append` l |
69 | | BC8.pack "From " `B.isPrefixOf` l = BC8.pack "- " `B.append` l | 74 | | BLC8.pack "From " `BL.isPrefixOf` l = BLC8.pack "- " `BL.append` l |
70 | | otherwise = l | 75 | | otherwise = l |
diff --git a/Data/Digest/CRC24.hs b/Data/Digest/CRC24.hs index 4637834..4586177 100644 --- a/Data/Digest/CRC24.hs +++ b/Data/Digest/CRC24.hs | |||
@@ -4,12 +4,14 @@ | |||
4 | -- (See the LICENSE file). | 4 | -- (See the LICENSE file). |
5 | 5 | ||
6 | module Data.Digest.CRC24 ( | 6 | module Data.Digest.CRC24 ( |
7 | crc24 | 7 | crc24 |
8 | , crc24Lazy | ||
8 | ) where | 9 | ) where |
9 | 10 | ||
10 | import Data.Bits (shiftL, (.&.), xor) | 11 | import Data.Bits (shiftL, (.&.), xor) |
11 | import Data.ByteString (ByteString) | 12 | import Data.ByteString.Lazy (ByteString) |
12 | import qualified Data.ByteString as B | 13 | import qualified Data.ByteString as B |
14 | import qualified Data.ByteString.Lazy as BL | ||
13 | import Data.Word (Word8, Word32) | 15 | import Data.Word (Word8, Word32) |
14 | 16 | ||
15 | crc24_init :: Word32 | 17 | crc24_init :: Word32 |
@@ -21,5 +23,8 @@ crc24_poly = 0x1864CFB | |||
21 | crc24_update :: Word32 -> Word8 -> Word32 | 23 | crc24_update :: Word32 -> Word8 -> Word32 |
22 | crc24_update c b = (last . take 9 $ iterate (\x -> if (shiftL x 1) .&. 0x1000000 == 0x1000000 then shiftL x 1 `xor` crc24_poly else shiftL x 1) (c `xor` shiftL (fromIntegral b) 16)) .&. 0xFFFFFF | 24 | crc24_update c b = (last . take 9 $ iterate (\x -> if (shiftL x 1) .&. 0x1000000 == 0x1000000 then shiftL x 1 `xor` crc24_poly else shiftL x 1) (c `xor` shiftL (fromIntegral b) 16)) .&. 0xFFFFFF |
23 | 25 | ||
24 | crc24 :: ByteString -> Word32 | 26 | crc24 :: B.ByteString -> Word32 |
25 | crc24 bs = B.foldl crc24_update crc24_init bs | 27 | crc24 bs = crc24Lazy . BL.fromChunks $ [bs] |
28 | |||
29 | crc24Lazy :: ByteString -> Word32 | ||
30 | crc24Lazy bs = BL.foldl crc24_update crc24_init bs | ||
diff --git a/tests/suite.hs b/tests/suite.hs index ffe8b50..ffcbc17 100644 --- a/tests/suite.hs +++ b/tests/suite.hs | |||
@@ -3,7 +3,7 @@ import Test.Framework.Providers.HUnit | |||
3 | 3 | ||
4 | import Test.HUnit | 4 | import Test.HUnit |
5 | 5 | ||
6 | import Codec.Encryption.OpenPGP.ASCIIArmor (encode, decode, multipartMerge) | 6 | import Codec.Encryption.OpenPGP.ASCIIArmor (decode, decodeLazy, encode, encodeLazy, multipartMerge) |
7 | import Codec.Encryption.OpenPGP.ASCIIArmor.Types | 7 | import Codec.Encryption.OpenPGP.ASCIIArmor.Types |
8 | import Codec.Encryption.OpenPGP.ASCIIArmor.Utils | 8 | import Codec.Encryption.OpenPGP.ASCIIArmor.Utils |
9 | 9 | ||
@@ -20,9 +20,9 @@ testCRC24 bs crc = assertEqual "crc24" crc (crc24 bs) | |||
20 | 20 | ||
21 | testArmorDecode :: FilePath -> [FilePath] -> Assertion | 21 | testArmorDecode :: FilePath -> [FilePath] -> Assertion |
22 | testArmorDecode fp targets = do | 22 | testArmorDecode fp targets = do |
23 | bs <- B.readFile $ "tests/data/" ++ fp | 23 | bs <- BL.readFile $ "tests/data/" ++ fp |
24 | tbss <- mapM (\target -> BL.readFile $ "tests/data/" ++ target) targets | 24 | tbss <- mapM (\target -> BL.readFile $ "tests/data/" ++ target) targets |
25 | case decode bs of | 25 | case decodeLazy bs of |
26 | Left e -> assertFailure $ "Decode failed (" ++ e ++ ") on " ++ fp | 26 | Left e -> assertFailure $ "Decode failed (" ++ e ++ ") on " ++ fp |
27 | Right as -> assertEqual ("for " ++ fp) tbss (map getPayload as) | 27 | Right as -> assertEqual ("for " ++ fp) tbss (map getPayload as) |
28 | where | 28 | where |
@@ -30,9 +30,9 @@ testArmorDecode fp targets = do | |||
30 | 30 | ||
31 | testArmorMultipartDecode :: FilePath -> FilePath -> Assertion | 31 | testArmorMultipartDecode :: FilePath -> FilePath -> Assertion |
32 | testArmorMultipartDecode fp target = do | 32 | testArmorMultipartDecode fp target = do |
33 | bs <- B.readFile $ "tests/data/" ++ fp | 33 | bs <- BL.readFile $ "tests/data/" ++ fp |
34 | tbs <- BL.readFile $ "tests/data/" ++ target | 34 | tbs <- BL.readFile $ "tests/data/" ++ target |
35 | case decode bs of | 35 | case decodeLazy bs of |
36 | Left e -> assertFailure $ "Decode failed (" ++ e ++ ") on " ++ fp | 36 | Left e -> assertFailure $ "Decode failed (" ++ e ++ ") on " ++ fp |
37 | Right as -> assertEqual ("for " ++ fp) tbs (getPayload (multipartMerge as)) | 37 | Right as -> assertEqual ("for " ++ fp) tbs (getPayload (multipartMerge as)) |
38 | where | 38 | where |
@@ -40,9 +40,9 @@ testArmorMultipartDecode fp target = do | |||
40 | 40 | ||
41 | testClearsignedDecodeBody :: FilePath -> FilePath -> Assertion | 41 | testClearsignedDecodeBody :: FilePath -> FilePath -> Assertion |
42 | testClearsignedDecodeBody fp target = do | 42 | testClearsignedDecodeBody fp target = do |
43 | bs <- B.readFile $ "tests/data/" ++ fp | 43 | bs <- BL.readFile $ "tests/data/" ++ fp |
44 | tbs <- BL.readFile $ "tests/data/" ++ target | 44 | tbs <- BL.readFile $ "tests/data/" ++ target |
45 | case decode bs of | 45 | case decodeLazy bs of |
46 | Left e -> assertFailure $ "Decode failed (" ++ e ++ ") on " ++ fp | 46 | Left e -> assertFailure $ "Decode failed (" ++ e ++ ") on " ++ fp |
47 | Right [a] -> assertEqual ("for " ++ fp) (convertEndings tbs) (getBody a) | 47 | Right [a] -> assertEqual ("for " ++ fp) (convertEndings tbs) (getBody a) |
48 | where | 48 | where |
@@ -51,9 +51,9 @@ testClearsignedDecodeBody fp target = do | |||
51 | 51 | ||
52 | testClearsignedDecodeSig :: FilePath -> FilePath -> Assertion | 52 | testClearsignedDecodeSig :: FilePath -> FilePath -> Assertion |
53 | testClearsignedDecodeSig fp target = do | 53 | testClearsignedDecodeSig fp target = do |
54 | bs <- B.readFile $ "tests/data/" ++ fp | 54 | bs <- BL.readFile $ "tests/data/" ++ fp |
55 | tbs <- BL.readFile $ "tests/data/" ++ target | 55 | tbs <- BL.readFile $ "tests/data/" ++ target |
56 | case decode bs of | 56 | case decodeLazy bs of |
57 | Left e -> assertFailure $ "Decode failed (" ++ e ++ ") on " ++ fp | 57 | Left e -> assertFailure $ "Decode failed (" ++ e ++ ") on " ++ fp |
58 | Right [a] -> assertEqual ("for " ++ fp) tbs (getSig a) | 58 | Right [a] -> assertEqual ("for " ++ fp) tbs (getSig a) |
59 | where | 59 | where |
@@ -62,15 +62,26 @@ testClearsignedDecodeSig fp target = do | |||
62 | testArmorEncode :: [FilePath] -> FilePath -> Assertion | 62 | testArmorEncode :: [FilePath] -> FilePath -> Assertion |
63 | testArmorEncode fps target = do | 63 | testArmorEncode fps target = do |
64 | bss <- mapM (\fp -> BL.readFile $ "tests/data/" ++ fp) fps | 64 | bss <- mapM (\fp -> BL.readFile $ "tests/data/" ++ fp) fps |
65 | tbs <- B.readFile $ "tests/data/" ++ target | 65 | tbs <- BL.readFile $ "tests/data/" ++ target |
66 | assertEqual ("literaldata") tbs (encode (map (\bs -> Armor ArmorMessage [("Version","OpenPrivacy 0.99")] bs) bss)) | 66 | assertEqual ("literaldata") tbs (encodeLazy (map (\bs -> Armor ArmorMessage [("Version","OpenPrivacy 0.99")] bs) bss)) |
67 | 67 | ||
68 | testClearsignedEncode :: FilePath -> FilePath -> FilePath -> Assertion | 68 | testClearsignedEncode :: FilePath -> FilePath -> FilePath -> Assertion |
69 | testClearsignedEncode ftxt fsig ftarget = do | 69 | testClearsignedEncode ftxt fsig ftarget = do |
70 | txt <- BL.readFile $ "tests/data/" ++ ftxt | 70 | txt <- BL.readFile $ "tests/data/" ++ ftxt |
71 | sig <- BL.readFile $ "tests/data/" ++ fsig | 71 | sig <- BL.readFile $ "tests/data/" ++ fsig |
72 | target <- B.readFile $ "tests/data/" ++ ftarget | 72 | target <- BL.readFile $ "tests/data/" ++ ftarget |
73 | assertEqual ("clearsigned encode") target (encode [ClearSigned [("Hash","SHA1")] txt (Armor ArmorSignature [("Version","OpenPrivacy 0.99")] sig)]) | 73 | assertEqual ("clearsigned encode") target (encodeLazy [ClearSigned [("Hash","SHA1")] txt (Armor ArmorSignature [("Version","OpenPrivacy 0.99")] sig)]) |
74 | |||
75 | testStrictDecode :: FilePath -> Assertion | ||
76 | testStrictDecode fp = do | ||
77 | bs <- BL.readFile $ "tests/data/" ++ fp | ||
78 | assertEqual ("strict decode") (decodeLazy bs :: Either String [Armor]) (decode (B.concat . BL.toChunks $ bs) :: Either String [Armor]) | ||
79 | |||
80 | testStrictEncode :: FilePath -> Assertion | ||
81 | testStrictEncode fp = do | ||
82 | bs <- BL.readFile $ "tests/data/" ++ fp | ||
83 | let fakearmors = [Armor ArmorMessage [("Version","OpenPrivacy 0.99")] bs, ClearSigned [("Hash","SHA1")] bs (Armor ArmorSignature [("Version","OpenPrivacy 0.99")] bs)] | ||
84 | assertEqual ("strict encode") (encodeLazy fakearmors) (BL.fromChunks [(encode fakearmors)]) | ||
74 | 85 | ||
75 | tests = [ | 86 | tests = [ |
76 | testGroup "CRC24" [ | 87 | testGroup "CRC24" [ |
@@ -89,6 +100,8 @@ tests = [ | |||
89 | , testCase "Encode sample armor" (testArmorEncode ["msg1.gpg"] "msg1.asc") | 100 | , testCase "Encode sample armor" (testArmorEncode ["msg1.gpg"] "msg1.asc") |
90 | , testCase "Encode multiple sample armors" (testArmorEncode ["msg1.gpg","msg1.gpg","msg1.gpg"] "msg1c.asc") | 101 | , testCase "Encode multiple sample armors" (testArmorEncode ["msg1.gpg","msg1.gpg","msg1.gpg"] "msg1c.asc") |
91 | , testCase "Encode clear-signed sig" (testClearsignedEncode "msg3" "msg3.sig" "msg3.asc") | 102 | , testCase "Encode clear-signed sig" (testClearsignedEncode "msg3" "msg3.sig" "msg3.asc") |
103 | , testCase "Decode from strict ByteString" (testStrictDecode "msg1.asc") | ||
104 | , testCase "Encode to strict ByteString" (testStrictEncode "msg1.gpg") | ||
92 | ] | 105 | ] |
93 | ] | 106 | ] |
94 | 107 | ||