diff options
Diffstat (limited to 'tests/suite.hs')
-rw-r--r-- | tests/suite.hs | 57 |
1 files changed, 5 insertions, 52 deletions
diff --git a/tests/suite.hs b/tests/suite.hs index 7ea5e57..ad7a8f2 100644 --- a/tests/suite.hs +++ b/tests/suite.hs | |||
@@ -7,9 +7,7 @@ import Test.HUnit hiding (Test) | |||
7 | import Data.Word | 7 | import Data.Word |
8 | import Data.Binary | 8 | import Data.Binary |
9 | import qualified Data.OpenPGP as OpenPGP | 9 | import qualified Data.OpenPGP as OpenPGP |
10 | import qualified Data.OpenPGP.Crypto as OpenPGP | ||
11 | import qualified Data.ByteString.Lazy as LZ | 10 | import qualified Data.ByteString.Lazy as LZ |
12 | import qualified Data.ByteString.Lazy.UTF8 as LZ (fromString) | ||
13 | 11 | ||
14 | instance Arbitrary OpenPGP.HashAlgorithm where | 12 | instance Arbitrary OpenPGP.HashAlgorithm where |
15 | arbitrary = elements [OpenPGP.MD5, OpenPGP.SHA1, OpenPGP.SHA256, OpenPGP.SHA384, OpenPGP.SHA512] | 13 | arbitrary = elements [OpenPGP.MD5, OpenPGP.SHA1, OpenPGP.SHA256, OpenPGP.SHA384, OpenPGP.SHA512] |
@@ -18,8 +16,8 @@ testSerialization :: FilePath -> Assertion | |||
18 | testSerialization fp = do | 16 | testSerialization fp = do |
19 | bs <- LZ.readFile $ "tests/data/" ++ fp | 17 | bs <- LZ.readFile $ "tests/data/" ++ fp |
20 | nullShield "First" (decode bs) (\firstpass -> | 18 | nullShield "First" (decode bs) (\firstpass -> |
21 | nullShield "Second" (decode $ encode firstpass) (\secondpass -> | 19 | nullShield "Second" (decode $ encode firstpass) ( |
22 | assertEqual ("for " ++ fp) firstpass secondpass | 20 | assertEqual ("for " ++ fp) firstpass |
23 | ) | 21 | ) |
24 | ) | 22 | ) |
25 | where | 23 | where |
@@ -27,38 +25,12 @@ testSerialization fp = do | |||
27 | assertFailure $ pass ++ " pass of " ++ fp ++ " decoded to nothing." | 25 | assertFailure $ pass ++ " pass of " ++ fp ++ " decoded to nothing." |
28 | nullShield _ m f = f m | 26 | nullShield _ m f = f m |
29 | 27 | ||
30 | testFingerprint :: FilePath -> String -> Assertion | ||
31 | testFingerprint fp kf = do | ||
32 | bs <- LZ.readFile $ "tests/data/" ++ fp | ||
33 | let (OpenPGP.Message [packet]) = decode bs | ||
34 | assertEqual ("for " ++ fp) kf (OpenPGP.fingerprint packet) | ||
35 | |||
36 | testVerifyMessage :: FilePath -> FilePath -> Assertion | ||
37 | testVerifyMessage keyring message = do | ||
38 | keys <- fmap decode $ LZ.readFile $ "tests/data/" ++ keyring | ||
39 | m <- fmap decode $ LZ.readFile $ "tests/data/" ++ message | ||
40 | let verification = OpenPGP.verify keys m 0 | ||
41 | assertEqual (keyring ++ " for " ++ message) True verification | ||
42 | |||
43 | prop_sign_and_verify :: OpenPGP.Message -> String -> OpenPGP.HashAlgorithm -> String -> String -> Bool | ||
44 | prop_sign_and_verify secring kid halgo filename msg = | ||
45 | let | ||
46 | m = OpenPGP.LiteralDataPacket { | ||
47 | OpenPGP.format = 'u', | ||
48 | OpenPGP.filename = filename, | ||
49 | OpenPGP.timestamp = 12341234, | ||
50 | OpenPGP.content = LZ.fromString msg | ||
51 | } | ||
52 | sig = OpenPGP.sign secring (OpenPGP.Message [m]) halgo kid 12341234 | ||
53 | in | ||
54 | OpenPGP.verify secring (OpenPGP.Message [m,sig]) 0 | ||
55 | |||
56 | prop_s2k_count :: Word8 -> Bool | 28 | prop_s2k_count :: Word8 -> Bool |
57 | prop_s2k_count c = | 29 | prop_s2k_count c = |
58 | c == OpenPGP.encode_s2k_count (OpenPGP.decode_s2k_count c) | 30 | c == OpenPGP.encode_s2k_count (OpenPGP.decode_s2k_count c) |
59 | 31 | ||
60 | tests :: OpenPGP.Message -> [Test] | 32 | tests :: [Test] |
61 | tests secring = | 33 | tests = |
62 | [ | 34 | [ |
63 | testGroup "Serialization" [ | 35 | testGroup "Serialization" [ |
64 | testCase "000001-006.public_key" (testSerialization "000001-006.public_key"), | 36 | testCase "000001-006.public_key" (testSerialization "000001-006.public_key"), |
@@ -148,29 +120,10 @@ tests secring = | |||
148 | testCase "uncompressed-ops-dsa.gpg" (testSerialization "uncompressed-ops-dsa.gpg"), | 120 | testCase "uncompressed-ops-dsa.gpg" (testSerialization "uncompressed-ops-dsa.gpg"), |
149 | testCase "uncompressed-ops-rsa.gpg" (testSerialization "uncompressed-ops-rsa.gpg") | 121 | testCase "uncompressed-ops-rsa.gpg" (testSerialization "uncompressed-ops-rsa.gpg") |
150 | ], | 122 | ], |
151 | testGroup "Fingerprint" [ | ||
152 | testCase "000001-006.public_key" (testFingerprint "000001-006.public_key" "421F28FEAAD222F856C8FFD5D4D54EA16F87040E"), | ||
153 | testCase "000016-006.public_key" (testFingerprint "000016-006.public_key" "AF95E4D7BAC521EE9740BED75E9F1523413262DC"), | ||
154 | testCase "000027-006.public_key" (testFingerprint "000027-006.public_key" "1EB20B2F5A5CC3BEAFD6E5CB7732CF988A63EA86"), | ||
155 | testCase "000035-006.public_key" (testFingerprint "000035-006.public_key" "CB7933459F59C70DF1C3FBEEDEDC3ECF689AF56D") | ||
156 | ], | ||
157 | testGroup "Message verification" [ | ||
158 | --testCase "uncompressed-ops-dsa" (testVerifyMessage "pubring.gpg" "uncompressed-ops-dsa.gpg"), | ||
159 | --testCase "uncompressed-ops-dsa-sha384" (testVerifyMessage "pubring.gpg" "uncompressed-ops-dsa-sha384.txt.gpg"), | ||
160 | testCase "uncompressed-ops-rsa" (testVerifyMessage "pubring.gpg" "uncompressed-ops-rsa.gpg"), | ||
161 | testCase "compressedsig" (testVerifyMessage "pubring.gpg" "compressedsig.gpg"), | ||
162 | testCase "compressedsig-zlib" (testVerifyMessage "pubring.gpg" "compressedsig-zlib.gpg"), | ||
163 | testCase "compressedsig-bzip2" (testVerifyMessage "pubring.gpg" "compressedsig-bzip2.gpg") | ||
164 | ], | ||
165 | testGroup "Signing" [ | ||
166 | testProperty "Crypto signatures verify" (prop_sign_and_verify secring "FEF8AFA0F661C3EE") | ||
167 | ], | ||
168 | testGroup "S2K count" [ | 123 | testGroup "S2K count" [ |
169 | testProperty "S2K count encode reverses decode" prop_s2k_count | 124 | testProperty "S2K count encode reverses decode" prop_s2k_count |
170 | ] | 125 | ] |
171 | ] | 126 | ] |
172 | 127 | ||
173 | main :: IO () | 128 | main :: IO () |
174 | main = do | 129 | main = defaultMain tests |
175 | secring <- fmap decode $ LZ.readFile "tests/data/secring.gpg" | ||
176 | defaultMain (tests secring) | ||