Skip to content

Commit

Permalink
QuickCheck that we can verify our own signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
singpolyma committed Apr 24, 2012
1 parent 8ae603c commit b206cb7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
Binary file modified tests/data/secring.gpg
Binary file not shown.
36 changes: 29 additions & 7 deletions tests/suite.hs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import Test.Framework (defaultMain, testGroup, Test)
import Test.Framework.Providers.HUnit
import Test.Framework.Providers.QuickCheck2
import Test.QuickCheck ()
import Test.QuickCheck
import Test.HUnit hiding (Test)

import Data.Word
import Data.Binary
import qualified Data.OpenPGP as OpenPGP
import qualified Data.OpenPGP.Crypto as OpenPGP
import qualified Data.ByteString.Lazy as LZ
import qualified Data.ByteString.Lazy.UTF8 as LZ (fromString)

instance Arbitrary OpenPGP.HashAlgorithm where
arbitrary = elements [OpenPGP.MD5, OpenPGP.SHA1, OpenPGP.SHA256, OpenPGP.SHA384, OpenPGP.SHA512]

testSerialization :: FilePath -> Assertion
testSerialization fp = do
Expand Down Expand Up @@ -36,14 +40,27 @@ testVerifyMessage keyring message = do
let verification = OpenPGP.verify keys m 0
assertEqual (keyring ++ " for " ++ message) True verification

prop_sign_and_verify :: OpenPGP.Message -> String -> OpenPGP.HashAlgorithm -> String -> String -> Bool
prop_sign_and_verify secring kid halgo filename msg =
let
m = OpenPGP.LiteralDataPacket {
OpenPGP.format = 'u',
OpenPGP.filename = filename,
OpenPGP.timestamp = 12341234,
OpenPGP.content = LZ.fromString msg
}
sig = OpenPGP.sign secring (OpenPGP.Message [m]) halgo kid 12341234
in
OpenPGP.verify secring (OpenPGP.Message [m,sig]) 0

prop_s2k_count :: Word8 -> Bool
prop_s2k_count c =
c == OpenPGP.encode_s2k_count (OpenPGP.decode_s2k_count c)

tests :: [Test]
tests =
tests :: OpenPGP.Message -> [Test]
tests secring =
[
testGroup "Serialization group" [
testGroup "Serialization" [
testCase "000001-006.public_key" (testSerialization "000001-006.public_key"),
testCase "000002-013.user_id" (testSerialization "000002-013.user_id"),
-- Issue #11 -- testCase "000003-002.sig" (testSerialization "000003-002.sig"),
Expand Down Expand Up @@ -131,24 +148,29 @@ tests =
-- Issue #11 -- testCase "uncompressed-ops-dsa.gpg" (testSerialization "uncompressed-ops-dsa.gpg"),
-- Issue #11 -- testCase "uncompressed-ops-rsa.gpg" (testSerialization "uncompressed-ops-rsa.gpg"),
],
testGroup "Fingerprint group" [
testGroup "Fingerprint" [
testCase "000001-006.public_key" (testFingerprint "000001-006.public_key" "421F28FEAAD222F856C8FFD5D4D54EA16F87040E"),
testCase "000016-006.public_key" (testFingerprint "000016-006.public_key" "AF95E4D7BAC521EE9740BED75E9F1523413262DC"),
testCase "000027-006.public_key" (testFingerprint "000027-006.public_key" "1EB20B2F5A5CC3BEAFD6E5CB7732CF988A63EA86"),
testCase "000035-006.public_key" (testFingerprint "000035-006.public_key" "CB7933459F59C70DF1C3FBEEDEDC3ECF689AF56D")
],
testGroup "Message verification group" [
testGroup "Message verification" [
--testCase "uncompressed-ops-dsa" (testVerifyMessage "pubring.gpg" "uncompressed-ops-dsa.gpg"),
--testCase "uncompressed-ops-dsa-sha384" (testVerifyMessage "pubring.gpg" "uncompressed-ops-dsa-sha384.txt.gpg"),
testCase "uncompressed-ops-rsa" (testVerifyMessage "pubring.gpg" "uncompressed-ops-rsa.gpg"),
testCase "compressedsig" (testVerifyMessage "pubring.gpg" "compressedsig.gpg"),
testCase "compressedsig-zlib" (testVerifyMessage "pubring.gpg" "compressedsig-zlib.gpg"),
testCase "compressedsig-bzip2" (testVerifyMessage "pubring.gpg" "compressedsig-bzip2.gpg")
],
testGroup "Signing" [
testProperty "Crypto signatures verify" (prop_sign_and_verify secring "FEF8AFA0F661C3EE")
],
testGroup "S2K count" [
testProperty "S2K count encode reverses decode" prop_s2k_count
]
]

main :: IO ()
main = defaultMain tests
main = do
secring <- fmap decode $ LZ.readFile "tests/data/secring.gpg"
defaultMain (tests secring)

0 comments on commit b206cb7

Please sign in to comment.