diff options
author | Clint Adams <clint@debian.org> | 2012-04-25 22:26:57 -0400 |
---|---|---|
committer | Clint Adams <clint@debian.org> | 2012-04-25 22:26:57 -0400 |
commit | d17ea2bb11255f7e6ae9069d3b58e8f06d5946a5 (patch) | |
tree | 60c0cebd5ae6be14f293ca96e9756351581fa497 | |
parent | 83f494d1c3dbf75054284334bc30e8a1b9825146 (diff) |
Add multipartMerge
-rw-r--r-- | Codec/Encryption/OpenPGP/ASCIIArmor.hs | 2 | ||||
-rw-r--r-- | Codec/Encryption/OpenPGP/ASCIIArmor/Multipart.hs | 24 | ||||
-rw-r--r-- | openpgp-asciiarmor.cabal | 3 | ||||
-rw-r--r-- | tests/data/msg2.asc | 13 | ||||
-rw-r--r-- | tests/data/msg2.pgp | bin | 0 -> 126 bytes | |||
-rw-r--r-- | tests/suite.hs | 15 |
6 files changed, 55 insertions, 2 deletions
diff --git a/Codec/Encryption/OpenPGP/ASCIIArmor.hs b/Codec/Encryption/OpenPGP/ASCIIArmor.hs index 26d58dd..6d0c172 100644 --- a/Codec/Encryption/OpenPGP/ASCIIArmor.hs +++ b/Codec/Encryption/OpenPGP/ASCIIArmor.hs | |||
@@ -7,7 +7,9 @@ module Codec.Encryption.OpenPGP.ASCIIArmor ( | |||
7 | encode | 7 | encode |
8 | , decode | 8 | , decode |
9 | , parseArmor | 9 | , parseArmor |
10 | , multipartMerge | ||
10 | ) where | 11 | ) where |
11 | 12 | ||
12 | import Codec.Encryption.OpenPGP.ASCIIArmor.Encode (encode) | 13 | import Codec.Encryption.OpenPGP.ASCIIArmor.Encode (encode) |
13 | import Codec.Encryption.OpenPGP.ASCIIArmor.Decode (decode, parseArmor) | 14 | import Codec.Encryption.OpenPGP.ASCIIArmor.Decode (decode, parseArmor) |
15 | import Codec.Encryption.OpenPGP.ASCIIArmor.Multipart (multipartMerge) | ||
diff --git a/Codec/Encryption/OpenPGP/ASCIIArmor/Multipart.hs b/Codec/Encryption/OpenPGP/ASCIIArmor/Multipart.hs new file mode 100644 index 0000000..8719c7c --- /dev/null +++ b/Codec/Encryption/OpenPGP/ASCIIArmor/Multipart.hs | |||
@@ -0,0 +1,24 @@ | |||
1 | -- ASCIIArmor/Multipart.hs: OpenPGP (RFC4880) ASCII armor implementation | ||
2 | -- Copyright Ⓒ 2012 Clint Adams | ||
3 | -- This software is released under the terms of the ISC license. | ||
4 | -- (See the LICENSE file). | ||
5 | |||
6 | module Codec.Encryption.OpenPGP.ASCIIArmor.Multipart ( | ||
7 | multipartMerge | ||
8 | ) where | ||
9 | |||
10 | import Codec.Encryption.OpenPGP.ASCIIArmor.Types | ||
11 | |||
12 | import Data.ByteString (ByteString) | ||
13 | import qualified Data.ByteString as B | ||
14 | |||
15 | multipartMerge :: [Armor] -> Armor | ||
16 | multipartMerge as = go as (Armor ArmorMessage [] B.empty) | ||
17 | where | ||
18 | go :: [Armor] -> Armor -> Armor | ||
19 | go [] state = state | ||
20 | go ((Armor at hs bs):as) state = go as (go' at hs bs state) | ||
21 | go' :: ArmorType -> [(String,String)] -> ByteString -> Armor -> Armor | ||
22 | go' (ArmorSplitMessage _ _) hs bs (Armor _ ohs obs) = Armor ArmorMessage (ohs ++ hs) (obs `B.append` bs) | ||
23 | go' (ArmorSplitMessageIndefinite _) hs bs (Armor _ ohs obs) = Armor ArmorMessage (ohs ++ hs) (obs `B.append` bs) | ||
24 | go' _ _ _ state = state | ||
diff --git a/openpgp-asciiarmor.cabal b/openpgp-asciiarmor.cabal index e161d6f..9b141c2 100644 --- a/openpgp-asciiarmor.cabal +++ b/openpgp-asciiarmor.cabal | |||
@@ -15,7 +15,9 @@ Extra-source-files: tests/suite.hs | |||
15 | , tests/data/msg1a.asc | 15 | , tests/data/msg1a.asc |
16 | , tests/data/msg1b.asc | 16 | , tests/data/msg1b.asc |
17 | , tests/data/msg1c.asc | 17 | , tests/data/msg1c.asc |
18 | , tests/data/msg2.asc | ||
18 | , tests/data/msg1.gpg | 19 | , tests/data/msg1.gpg |
20 | , tests/data/msg2.pgp | ||
19 | 21 | ||
20 | Cabal-version: >= 1.10 | 22 | Cabal-version: >= 1.10 |
21 | 23 | ||
@@ -26,6 +28,7 @@ Library | |||
26 | , Codec.Encryption.OpenPGP.ASCIIArmor.Encode | 28 | , Codec.Encryption.OpenPGP.ASCIIArmor.Encode |
27 | , Codec.Encryption.OpenPGP.ASCIIArmor.Types | 29 | , Codec.Encryption.OpenPGP.ASCIIArmor.Types |
28 | Other-Modules: Data.Digest.CRC24 | 30 | Other-Modules: Data.Digest.CRC24 |
31 | , Codec.Encryption.OpenPGP.ASCIIArmor.Multipart | ||
29 | Build-depends: attoparsec | 32 | Build-depends: attoparsec |
30 | , base > 4 && < 5 | 33 | , base > 4 && < 5 |
31 | , base64-bytestring | 34 | , base64-bytestring |
diff --git a/tests/data/msg2.asc b/tests/data/msg2.asc new file mode 100644 index 0000000..7e5a287 --- /dev/null +++ b/tests/data/msg2.asc | |||
@@ -0,0 +1,13 @@ | |||
1 | -----BEGIN PGP MESSAGE, PART 01/02----- | ||
2 | Version: ClosedPrivacy 0.99 | ||
3 | |||
4 | pgAAAHnw/J+O5ibiYizWTVDTrl/FUe926aD7lhNS+qjFNASGRmSSvWqWyubVcW0Z | ||
5 | YnVtKfpv03Y+zIUQv+TATmWcwpkzhQ9QeTk70ZBFFmNXsuM12dTQGkY8IDRsmUT9 | ||
6 | =H7u4 | ||
7 | -----END PGP MESSAGE, PART 01/02----- | ||
8 | |||
9 | -----BEGIN PGP MESSAGE, PART 02/02----- | ||
10 | |||
11 | m+f4GTQ2FwJzO0GeazzBV4ywKLqSnCQVFBNKhDnw | ||
12 | =hLwC | ||
13 | -----END PGP MESSAGE, PART 02/02----- | ||
diff --git a/tests/data/msg2.pgp b/tests/data/msg2.pgp new file mode 100644 index 0000000..46c4db7 --- /dev/null +++ b/tests/data/msg2.pgp | |||
Binary files differ | |||
diff --git a/tests/suite.hs b/tests/suite.hs index a843664..6fdd816 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) | 6 | import Codec.Encryption.OpenPGP.ASCIIArmor (encode, decode, multipartMerge) |
7 | import Codec.Encryption.OpenPGP.ASCIIArmor.Types | 7 | import Codec.Encryption.OpenPGP.ASCIIArmor.Types |
8 | 8 | ||
9 | import Data.ByteString (ByteString) | 9 | import Data.ByteString (ByteString) |
@@ -21,7 +21,17 @@ testArmorDecode fp targets = do | |||
21 | tbss <- mapM (\target -> B.readFile $ "tests/data/" ++ target) targets | 21 | tbss <- mapM (\target -> B.readFile $ "tests/data/" ++ target) targets |
22 | case decode bs of | 22 | case decode bs of |
23 | Left e -> assertFailure $ "Decode failed (" ++ e ++ ") on " ++ fp | 23 | Left e -> assertFailure $ "Decode failed (" ++ e ++ ") on " ++ fp |
24 | Right as -> do assertEqual ("for " ++ fp) tbss (map getPayload as) | 24 | Right as -> assertEqual ("for " ++ fp) tbss (map getPayload as) |
25 | where | ||
26 | getPayload (Armor _ _ pl) = pl | ||
27 | |||
28 | testArmorMultipartDecode :: FilePath -> FilePath -> Assertion | ||
29 | testArmorMultipartDecode fp target = do | ||
30 | bs <- B.readFile $ "tests/data/" ++ fp | ||
31 | tbs <- B.readFile $ "tests/data/" ++ target | ||
32 | case decode bs of | ||
33 | Left e -> assertFailure $ "Decode failed (" ++ e ++ ") on " ++ fp | ||
34 | Right as -> assertEqual ("for " ++ fp) tbs (getPayload (multipartMerge as)) | ||
25 | where | 35 | where |
26 | getPayload (Armor _ _ pl) = pl | 36 | getPayload (Armor _ _ pl) = pl |
27 | 37 | ||
@@ -41,6 +51,7 @@ tests = [ | |||
41 | testCase "Decode sample armor" (testArmorDecode "msg1.asc" ["msg1.gpg"]) | 51 | testCase "Decode sample armor" (testArmorDecode "msg1.asc" ["msg1.gpg"]) |
42 | , testCase "Decode sample armor with cruft" (testArmorDecode "msg1a.asc" ["msg1.gpg"]) | 52 | , testCase "Decode sample armor with cruft" (testArmorDecode "msg1a.asc" ["msg1.gpg"]) |
43 | , testCase "Decode multiple sample armors" (testArmorDecode "msg1b.asc" ["msg1.gpg","msg1.gpg","msg1.gpg"]) | 53 | , testCase "Decode multiple sample armors" (testArmorDecode "msg1b.asc" ["msg1.gpg","msg1.gpg","msg1.gpg"]) |
54 | , testCase "Decode multi-part armor" (testArmorMultipartDecode "msg2.asc" "msg2.pgp") | ||
44 | , testCase "Encode sample armor" (testArmorEncode ["msg1.gpg"] "msg1.asc") | 55 | , testCase "Encode sample armor" (testArmorEncode ["msg1.gpg"] "msg1.asc") |
45 | , testCase "Encode multiple sample armors" (testArmorEncode ["msg1.gpg","msg1.gpg","msg1.gpg"] "msg1c.asc") | 56 | , testCase "Encode multiple sample armors" (testArmorEncode ["msg1.gpg","msg1.gpg","msg1.gpg"] "msg1c.asc") |
46 | ] | 57 | ] |