From 4a0bb47803be6be43b8bb5c62f302019a25f9fd1 Mon Sep 17 00:00:00 2001 From: Clint Adams Date: Wed, 25 Apr 2012 19:26:58 -0400 Subject: Encode multiple Armors into one ByteString. --- Codec/Encryption/OpenPGP/ASCIIArmor.hs | 4 ++-- Codec/Encryption/OpenPGP/ASCIIArmor/Encode.hs | 5 ++++- openpgp-asciiarmor.cabal | 1 + tests/data/msg1c.asc | 21 +++++++++++++++++++++ tests/suite.hs | 13 +++++++------ 5 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 tests/data/msg1c.asc diff --git a/Codec/Encryption/OpenPGP/ASCIIArmor.hs b/Codec/Encryption/OpenPGP/ASCIIArmor.hs index 075a481..26d58dd 100644 --- a/Codec/Encryption/OpenPGP/ASCIIArmor.hs +++ b/Codec/Encryption/OpenPGP/ASCIIArmor.hs @@ -4,10 +4,10 @@ -- (See the LICENSE file). module Codec.Encryption.OpenPGP.ASCIIArmor ( - armor + encode , decode , parseArmor ) where -import Codec.Encryption.OpenPGP.ASCIIArmor.Encode (armor) +import Codec.Encryption.OpenPGP.ASCIIArmor.Encode (encode) import Codec.Encryption.OpenPGP.ASCIIArmor.Decode (decode, parseArmor) diff --git a/Codec/Encryption/OpenPGP/ASCIIArmor/Encode.hs b/Codec/Encryption/OpenPGP/ASCIIArmor/Encode.hs index 8853be3..99d4d54 100644 --- a/Codec/Encryption/OpenPGP/ASCIIArmor/Encode.hs +++ b/Codec/Encryption/OpenPGP/ASCIIArmor/Encode.hs @@ -4,7 +4,7 @@ -- (See the LICENSE file). module Codec.Encryption.OpenPGP.ASCIIArmor.Encode ( - armor + encode ) where import Codec.Encryption.OpenPGP.ASCIIArmor.Types @@ -17,6 +17,9 @@ import Data.Serialize (put) import Data.Serialize.Put (runPut, putWord32be) import Data.String (IsString, fromString) +encode :: (Integral a, Show a) => [Armor a] -> ByteString +encode = B.concat . map armor + armor :: (Integral a, Show a) => Armor a -> ByteString armor (Armor atype ahs bs) = beginLine atype `B.append` armorHeaders ahs `B.append` blankLine `B.append` armorData bs `B.append` armorChecksum bs `B.append` endLine atype diff --git a/openpgp-asciiarmor.cabal b/openpgp-asciiarmor.cabal index ff2289a..e161d6f 100644 --- a/openpgp-asciiarmor.cabal +++ b/openpgp-asciiarmor.cabal @@ -14,6 +14,7 @@ Extra-source-files: tests/suite.hs , tests/data/msg1.asc , tests/data/msg1a.asc , tests/data/msg1b.asc + , tests/data/msg1c.asc , tests/data/msg1.gpg Cabal-version: >= 1.10 diff --git a/tests/data/msg1c.asc b/tests/data/msg1c.asc new file mode 100644 index 0000000..c7ac785 --- /dev/null +++ b/tests/data/msg1c.asc @@ -0,0 +1,21 @@ +-----BEGIN PGP MESSAGE----- +Version: OpenPrivacy 0.99 + +yDgBO22WxBHv7O8X7O/jygAEzol56iUKiXmV+XmpCtmpqQUKiQrFqclFqUDBovzS +vBSFjNSiVHsuAA== +=njUN +-----END PGP MESSAGE----- +-----BEGIN PGP MESSAGE----- +Version: OpenPrivacy 0.99 + +yDgBO22WxBHv7O8X7O/jygAEzol56iUKiXmV+XmpCtmpqQUKiQrFqclFqUDBovzS +vBSFjNSiVHsuAA== +=njUN +-----END PGP MESSAGE----- +-----BEGIN PGP MESSAGE----- +Version: OpenPrivacy 0.99 + +yDgBO22WxBHv7O8X7O/jygAEzol56iUKiXmV+XmpCtmpqQUKiQrFqclFqUDBovzS +vBSFjNSiVHsuAA== +=njUN +-----END PGP MESSAGE----- diff --git a/tests/suite.hs b/tests/suite.hs index 45a2fb2..a843664 100644 --- a/tests/suite.hs +++ b/tests/suite.hs @@ -3,7 +3,7 @@ import Test.Framework.Providers.HUnit import Test.HUnit -import Codec.Encryption.OpenPGP.ASCIIArmor (armor, decode) +import Codec.Encryption.OpenPGP.ASCIIArmor (encode, decode) import Codec.Encryption.OpenPGP.ASCIIArmor.Types import Data.ByteString (ByteString) @@ -25,11 +25,11 @@ testArmorDecode fp targets = do where getPayload (Armor _ _ pl) = pl -testArmorEncode :: FilePath -> FilePath -> Assertion -testArmorEncode fp target = do - bs <- B.readFile $ "tests/data/" ++ fp +testArmorEncode :: [FilePath] -> FilePath -> Assertion +testArmorEncode fps target = do + bss <- mapM (\fp -> B.readFile $ "tests/data/" ++ fp) fps tbs <- B.readFile $ "tests/data/" ++ target - assertEqual ("literaldata") (armor (Armor ArmorMessage [("Version","OpenPrivacy 0.99")] bs)) tbs + assertEqual ("literaldata") (encode (map (\bs -> Armor ArmorMessage [("Version","OpenPrivacy 0.99")] bs) bss)) tbs tests = [ testGroup "CRC24" [ @@ -41,7 +41,8 @@ tests = [ testCase "Decode sample armor" (testArmorDecode "msg1.asc" ["msg1.gpg"]) , testCase "Decode sample armor with cruft" (testArmorDecode "msg1a.asc" ["msg1.gpg"]) , testCase "Decode multiple sample armors" (testArmorDecode "msg1b.asc" ["msg1.gpg","msg1.gpg","msg1.gpg"]) - , testCase "Encode sample armor" (testArmorEncode "msg1.gpg" "msg1.asc") + , testCase "Encode sample armor" (testArmorEncode ["msg1.gpg"] "msg1.asc") + , testCase "Encode multiple sample armors" (testArmorEncode ["msg1.gpg","msg1.gpg","msg1.gpg"] "msg1c.asc") ] ] -- cgit v1.2.3