summaryrefslogtreecommitdiff
path: root/tests/suite.hs
blob: 9480c0b4f892d495724ac72d4e4ccf58f6acd667 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import Test.Framework (defaultMain, testGroup, Test)
import Test.Framework.Providers.HUnit
import Test.Framework.Providers.QuickCheck2
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
	bs <- LZ.readFile $ "tests/data/" ++ fp
	nullShield "First" (decode bs) (\firstpass ->
			nullShield "Second" (decode $ encode firstpass) (\secondpass ->
				assertEqual ("for " ++ fp) firstpass secondpass
			)
		)
	where
	nullShield pass (OpenPGP.Message []) _ =
		assertFailure $ pass ++ " pass of " ++ fp ++ " decoded to nothing."
	nullShield _ m f = f m

testFingerprint :: FilePath -> String -> Assertion
testFingerprint fp kf = do
	bs <- LZ.readFile $ "tests/data/" ++ fp
	let (OpenPGP.Message [packet]) = decode bs
	assertEqual ("for " ++ fp) kf (OpenPGP.fingerprint packet)

testVerifyMessage :: FilePath -> FilePath -> Assertion
testVerifyMessage keyring message = do
	keys <- fmap decode $ LZ.readFile $ "tests/data/" ++ keyring
	m <- fmap decode $ LZ.readFile $ "tests/data/" ++ message
	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 :: OpenPGP.Message -> [Test]
tests secring =
	[
		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"),
			testCase "000004-012.ring_trust" (testSerialization "000004-012.ring_trust"),
			-- Issue #11 -- testCase "000005-002.sig" (testSerialization "000005-002.sig"),
			testCase "000006-012.ring_trust" (testSerialization "000006-012.ring_trust"),
			-- Issue #11 -- testCase "000007-002.sig" (testSerialization "000007-002.sig"),
			testCase "000008-012.ring_trust" (testSerialization "000008-012.ring_trust"),
			-- Issue #11 -- testCase "000009-002.sig" (testSerialization "000009-002.sig"),
			testCase "000010-012.ring_trust" (testSerialization "000010-012.ring_trust"),
			-- Issue #11 -- testCase "000011-002.sig" (testSerialization "000011-002.sig"),
			testCase "000012-012.ring_trust" (testSerialization "000012-012.ring_trust"),
			testCase "000013-014.public_subkey" (testSerialization "000013-014.public_subkey"),
			-- Issue #11 -- testCase "000014-002.sig" (testSerialization "000014-002.sig"),
			testCase "000015-012.ring_trust" (testSerialization "000015-012.ring_trust"),
			testCase "000016-006.public_key" (testSerialization "000016-006.public_key"),
			-- Issue #11 -- testCase "000017-002.sig" (testSerialization "000017-002.sig"),
			testCase "000018-012.ring_trust" (testSerialization "000018-012.ring_trust"),
			testCase "000019-013.user_id" (testSerialization "000019-013.user_id"),
			-- Issue #11 -- testCase "000020-002.sig" (testSerialization "000020-002.sig"),
			testCase "000021-012.ring_trust" (testSerialization "000021-012.ring_trust"),
			-- Issue #11 -- testCase "000022-002.sig" (testSerialization "000022-002.sig"),
			testCase "000023-012.ring_trust" (testSerialization "000023-012.ring_trust"),
			testCase "000024-014.public_subkey" (testSerialization "000024-014.public_subkey"),
			-- Issue #11 -- testCase "000025-002.sig" (testSerialization "000025-002.sig"),
			testCase "000026-012.ring_trust" (testSerialization "000026-012.ring_trust"),
			testCase "000027-006.public_key" (testSerialization "000027-006.public_key"),
			-- Issue #11 -- testCase "000028-002.sig" (testSerialization "000028-002.sig"),
			testCase "000029-012.ring_trust" (testSerialization "000029-012.ring_trust"),
			testCase "000030-013.user_id" (testSerialization "000030-013.user_id"),
			-- Issue #11 -- testCase "000031-002.sig" (testSerialization "000031-002.sig"),
			testCase "000032-012.ring_trust" (testSerialization "000032-012.ring_trust"),
			-- Issue #11 -- testCase "000033-002.sig" (testSerialization "000033-002.sig"),
			testCase "000034-012.ring_trust" (testSerialization "000034-012.ring_trust"),
			testCase "000035-006.public_key" (testSerialization "000035-006.public_key"),
			testCase "000036-013.user_id" (testSerialization "000036-013.user_id"),
			-- Issue #11 -- testCase "000037-002.sig" (testSerialization "000037-002.sig"),
			testCase "000038-012.ring_trust" (testSerialization "000038-012.ring_trust"),
			-- Issue #11 -- testCase "000039-002.sig" (testSerialization "000039-002.sig"),
			testCase "000040-012.ring_trust" (testSerialization "000040-012.ring_trust"),
			testCase "000041-017.attribute" (testSerialization "000041-017.attribute"),
			-- Issue #11 -- testCase "000042-002.sig" (testSerialization "000042-002.sig"),
			testCase "000043-012.ring_trust" (testSerialization "000043-012.ring_trust"),
			testCase "000044-014.public_subkey" (testSerialization "000044-014.public_subkey"),
			-- Issue #11 -- testCase "000045-002.sig" (testSerialization "000045-002.sig"),
			testCase "000046-012.ring_trust" (testSerialization "000046-012.ring_trust"),
			testCase "000047-005.secret_key" (testSerialization "000047-005.secret_key"),
			testCase "000048-013.user_id" (testSerialization "000048-013.user_id"),
			-- Issue #11 -- testCase "000049-002.sig" (testSerialization "000049-002.sig"),
			testCase "000050-012.ring_trust" (testSerialization "000050-012.ring_trust"),
			testCase "000051-007.secret_subkey" (testSerialization "000051-007.secret_subkey"),
			-- Issue #11 -- testCase "000052-002.sig" (testSerialization "000052-002.sig"),
			testCase "000053-012.ring_trust" (testSerialization "000053-012.ring_trust"),
			testCase "000054-005.secret_key" (testSerialization "000054-005.secret_key"),
			-- Issue #11 -- testCase "000055-002.sig" (testSerialization "000055-002.sig"),
			testCase "000056-012.ring_trust" (testSerialization "000056-012.ring_trust"),
			testCase "000057-013.user_id" (testSerialization "000057-013.user_id"),
			-- Issue #11 -- testCase "000058-002.sig" (testSerialization "000058-002.sig"),
			testCase "000059-012.ring_trust" (testSerialization "000059-012.ring_trust"),
			testCase "000060-007.secret_subkey" (testSerialization "000060-007.secret_subkey"),
			-- Issue #11 -- testCase "000061-002.sig" (testSerialization "000061-002.sig"),
			testCase "000062-012.ring_trust" (testSerialization "000062-012.ring_trust"),
			testCase "000063-005.secret_key" (testSerialization "000063-005.secret_key"),
			-- Issue #11 -- testCase "000064-002.sig" (testSerialization "000064-002.sig"),
			testCase "000065-012.ring_trust" (testSerialization "000065-012.ring_trust"),
			testCase "000066-013.user_id" (testSerialization "000066-013.user_id"),
			-- Issue #11 -- testCase "000067-002.sig" (testSerialization "000067-002.sig"),
			testCase "000068-012.ring_trust" (testSerialization "000068-012.ring_trust"),
			testCase "000069-005.secret_key" (testSerialization "000069-005.secret_key"),
			testCase "000070-013.user_id" (testSerialization "000070-013.user_id"),
			-- Issue #11 -- testCase "000071-002.sig" (testSerialization "000071-002.sig"),
			testCase "000072-012.ring_trust" (testSerialization "000072-012.ring_trust"),
			testCase "000073-017.attribute" (testSerialization "000073-017.attribute"),
			-- Issue #11 -- testCase "000074-002.sig" (testSerialization "000074-002.sig"),
			testCase "000075-012.ring_trust" (testSerialization "000075-012.ring_trust"),
			testCase "000076-007.secret_subkey" (testSerialization "000076-007.secret_subkey"),
			-- Issue #11 -- testCase "000077-002.sig" (testSerialization "000077-002.sig"),
			testCase "000078-012.ring_trust" (testSerialization "000078-012.ring_trust"),
			-- Issue #11 -- testCase "pubring.gpg" (testSerialization "pubring.gpg"),
			-- Issue #11 -- testCase "secring.gpg" (testSerialization "secring.gpg"),
			-- Issue #11 -- testCase "compressedsig.gpg" (testSerialization "compressedsig.gpg"),
			-- Issue #11 -- testCase "compressedsig-zlib.gpg" (testSerialization "compressedsig-zlib.gpg"),
			-- Issue #11 -- testCase "compressedsig-bzip2.gpg" (testSerialization "compressedsig-bzip2.gpg"),
			testCase "onepass_sig" (testSerialization "onepass_sig")
			-- 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" [
			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" [
			--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 = do
	secring <- fmap decode $ LZ.readFile "tests/data/secring.gpg"
	defaultMain (tests secring)