1 #!/usr/bin/env python2.4
2 from basetest
import BaseTest
6 sys
.path
.insert(0, '..')
7 from zeroinstall
.injector
import gpg
, model
, trust
9 err_sig
= """-----BEGIN PGP MESSAGE-----
10 Version: GnuPG v1.4.0 (GNU/Linux)
12 owGbwMvMwCTYk9R5Infvsj7G01xJDE513j1OiSlcHfbMrCDOBJisINP6XQwLGjzn
13 tMxedXc3y75I7r1hQZFTb/ewMcx3yefZ8zb/vZd10I7LEYdDj4fnKsYAAA==
15 -----END PGP MESSAGE-----
18 bad_sig
= """-----BEGIN PGP SIGNED MESSAGE-----
22 -----BEGIN PGP SIGNATURE-----
23 Version: GnuPG v1.4.0 (GNU/Linux)
25 iD8DBQFCfk3grgeCgFmlPMERAhl8AKC0aktrLzz646zTY0TRzdnxPdbLBgCeJWbk
26 GRVbJusevCKvtoSn7RAW2mg=
28 -----END PGP SIGNATURE-----
31 good_sig
= """-----BEGIN PGP SIGNED MESSAGE-----
35 -----BEGIN PGP SIGNATURE-----
36 Version: GnuPG v1.4.0 (GNU/Linux)
38 iD8DBQFCfk3grgeCgFmlPMERAhl8AKC0aktrLzz646zTY0TRzdnxPdbLBgCeJWbk
39 GRVbJusevCKvtoSn7RAW2mg=
41 -----END PGP SIGNATURE-----
44 bad_xml_main
= """<?xml version='1.0'?>
48 ('last line is not end-of-comment',
49 """<!-- Base64 Signature
51 ('No signature block in XML',
53 iD8DBQBDtpK9rgeCgFmlPMERAg0gAKCaJhXFnk
56 ('extra data on comment line',
57 """<!-- Base64 Signature data
58 iD8DBQBDtpK9rgeCgFmlPMERAg0gAKCaJhXFnk
61 ('last line is not end-of-comment',
62 """<!-- Base64 Signature
63 iD8DBQBDtpK9rgeCgFmlPMERAg0gAKCaJhXFnk
64 WZRBLT0an56WYaBODukSsf4=
67 ('Invalid base 64 encoded signature:',
68 """<!-- Base64 Signature
69 iD8DBQBDtpK9rgeCgFmlPMERAg0gAKCaJhXFnk
70 WZRBLT0an56WYaBODukSsf4=
74 ('Invalid characters found',
75 """<!-- Base64 Signature
76 iD8DBQBDtpK9rge<CgFmlPMERAg0gAKCaJhXFnk
77 WZRBLT0an56WYaBODukSsf4=
81 good_xml_sig
= """<?xml version='1.0'?>
84 iD8DBQBDuChIrgeCgFmlPMERAnGEAJ0ZS1PeyWonx6xS/mgpYTKNgSXa5QCeMSYPHhNcvxu3f84y
89 bad_xml_sig
= """<?xml version='1.0'?>
92 iD8DBQBDuChIrgeCgFmlPMERAnGEAJ0ZS1PeyWonx6xS/mgpYTKNgSXa5QCeMSYPHhNcvxu3f84y
97 from data
import thomas_key
99 class TestGPG(BaseTest
):
103 stream
= tempfile
.TemporaryFile()
104 stream
.write(thomas_key
)
106 gpg
.import_key(stream
)
107 trust
.trust_db
.trust_key(
108 '92429807C9853C0744A68B9AAE07828059A53CC1')
110 def testImportBad(self
):
111 stream
= tempfile
.TemporaryFile()
112 stream
.write("Bad key")
115 gpg
.import_key(stream
)
117 except model
.SafeException
:
120 def testErrSig(self
):
121 stream
= tempfile
.TemporaryFile()
122 stream
.write(err_sig
)
124 data
, sigs
= gpg
.check_stream(stream
)
125 self
.assertEquals("Bad\n", data
.read())
126 assert len(sigs
) == 1
127 assert isinstance(sigs
[0], gpg
.ErrSig
)
128 assert sigs
[0].need_key() == "8C6289C86DBDA68E"
129 self
.assertEquals("17", sigs
[0].status
[gpg
.ErrSig
.ALG
])
130 assert sigs
[0].is_trusted() is False
131 assert str(sigs
[0]).startswith('ERROR')
133 def testBadSig(self
):
134 self
.assertEquals("Hell0\n", self
.check_bad(bad_sig
))
136 def testBadXMLSig(self
):
137 self
.assertEquals(bad_xml_sig
, self
.check_bad(bad_xml_sig
))
139 def testInvalidXMLSig(self
):
140 for error
, sig
in invalid_xmls_sigs
:
142 self
.check_bad(bad_xml_main
+ '\n' + sig
)
143 except model
.SafeException
, ex
:
144 if error
not in str(ex
):
145 raise model
.SafeException(str(ex
) + '\nSig:\n' + sig
)
147 def check_bad(self
, sig
):
148 stream
= tempfile
.TemporaryFile()
151 data
, sigs
= gpg
.check_stream(stream
)
152 assert len(sigs
) == 1
153 assert isinstance(sigs
[0], gpg
.BadSig
)
154 self
.assertEquals("AE07828059A53CC1",
155 sigs
[0].status
[gpg
.BadSig
.KEYID
])
156 assert sigs
[0].is_trusted() is False
157 assert sigs
[0].need_key() is None
158 assert str(sigs
[0]).startswith('BAD')
161 def testGoodSig(self
):
162 self
.assertEquals("Hello\n", self
.check_good(good_sig
))
164 def testGoodXMLSig(self
):
165 self
.assertEquals(good_xml_sig
, self
.check_good(good_xml_sig
))
167 def check_good(self
, sig
):
168 stream
= tempfile
.TemporaryFile()
171 data
, sigs
= gpg
.check_stream(stream
)
172 assert len(sigs
) == 1
173 assert isinstance(sigs
[0], gpg
.ValidSig
)
174 self
.assertEquals("92429807C9853C0744A68B9AAE07828059A53CC1",
176 assert sigs
[0].is_trusted() is True
177 assert sigs
[0].need_key() is None
178 assert str(sigs
[0]).startswith('Valid')
179 for item
in sigs
[0].get_details():
180 if item
[0] in ('pub', 'uid') and len(item
) > 9:
182 "Thomas Leonard <tal197@users.sourceforge.net>",
186 self
.fail("Missing name")
190 stream
= tempfile
.TemporaryFile()
191 stream
.write("Hello")
194 gpg
.check_stream(stream
)
196 except model
.SafeException
:
199 def testLoadKeys(self
):
200 self
.assertEquals({}, gpg
.load_keys([]))
202 suite
= unittest
.makeSuite(TestGPG
)
203 if __name__
== '__main__':
204 sys
.argv
.append('-v')