1 #!/usr/bin/env python2.3
2 import sys
, tempfile
, os
, shutil
5 sys
.path
.insert(0, '..')
6 from zeroinstall
.injector
import gpg
, model
8 err_sig
= """-----BEGIN PGP MESSAGE-----
9 Version: GnuPG v1.4.0 (GNU/Linux)
11 owGbwMvMwCTYk9R5Infvsj7G01xJDE513j1OiSlcHfbMrCDOBJisINP6XQwLGjzn
12 tMxedXc3y75I7r1hQZFTb/ewMcx3yefZ8zb/vZd10I7LEYdDj4fnKsYAAA==
14 -----END PGP MESSAGE-----
17 bad_sig
= """-----BEGIN PGP SIGNED MESSAGE-----
21 -----BEGIN PGP SIGNATURE-----
22 Version: GnuPG v1.4.0 (GNU/Linux)
24 iD8DBQFCfk3grgeCgFmlPMERAhl8AKC0aktrLzz646zTY0TRzdnxPdbLBgCeJWbk
25 GRVbJusevCKvtoSn7RAW2mg=
27 -----END PGP SIGNATURE-----
30 good_sig
= """-----BEGIN PGP SIGNED MESSAGE-----
34 -----BEGIN PGP SIGNATURE-----
35 Version: GnuPG v1.4.0 (GNU/Linux)
37 iD8DBQFCfk3grgeCgFmlPMERAhl8AKC0aktrLzz646zTY0TRzdnxPdbLBgCeJWbk
38 GRVbJusevCKvtoSn7RAW2mg=
40 -----END PGP SIGNATURE-----
43 bad_xml_main
= """<?xml version='1.0'?>
47 ('last line is not end-of-comment',
48 """<!-- Base64 Signature
50 ('No signature block in XML',
52 iD8DBQBDtpK9rgeCgFmlPMERAg0gAKCaJhXFnk
55 ('extra data on comment line',
56 """<!-- Base64 Signature data
57 iD8DBQBDtpK9rgeCgFmlPMERAg0gAKCaJhXFnk
60 ('last line is not end-of-comment',
61 """<!-- Base64 Signature
62 iD8DBQBDtpK9rgeCgFmlPMERAg0gAKCaJhXFnk
63 WZRBLT0an56WYaBODukSsf4=
66 ('Invalid base 64 encoded signature:',
67 """<!-- Base64 Signature
68 iD8DBQBDtpK9rgeCgFmlPMERAg0gAKCaJhXFnk
69 WZRBLT0an56WYaBODukSsf4=
73 ('Invalid characters found',
74 """<!-- Base64 Signature
75 iD8DBQBDtpK9rge<CgFmlPMERAg0gAKCaJhXFnk
76 WZRBLT0an56WYaBODukSsf4=
80 good_xml_sig
= """<?xml version='1.0'?>
83 iD8DBQBDuChIrgeCgFmlPMERAnGEAJ0ZS1PeyWonx6xS/mgpYTKNgSXa5QCeMSYPHhNcvxu3f84y
88 bad_xml_sig
= """<?xml version='1.0'?>
91 iD8DBQBDuChIrgeCgFmlPMERAnGEAJ0ZS1PeyWonx6xS/mgpYTKNgSXa5QCeMSYPHhNcvxu3f84y
96 from data
import thomas_key
98 class TestGPG(unittest
.TestCase
):
100 self
.gnupg_home
= tempfile
.mktemp()
101 os
.environ
['GNUPGHOME'] = self
.gnupg_home
102 os
.mkdir(self
.gnupg_home
, 0700)
103 stream
= tempfile
.TemporaryFile()
104 stream
.write(thomas_key
)
106 gpg
.import_key(stream
)
109 shutil
.rmtree(self
.gnupg_home
)
111 def testImportBad(self
):
112 stream
= tempfile
.TemporaryFile()
113 stream
.write("Bad key")
116 gpg
.import_key(stream
)
118 except model
.SafeException
:
121 def testErrSig(self
):
122 stream
= tempfile
.TemporaryFile()
123 stream
.write(err_sig
)
125 data
, sigs
= gpg
.check_stream(stream
)
126 self
.assertEquals("Bad\n", data
.read())
127 assert len(sigs
) == 1
128 assert isinstance(sigs
[0], gpg
.ErrSig
)
129 assert sigs
[0].need_key() == "8C6289C86DBDA68E"
130 self
.assertEquals("17", sigs
[0].status
[gpg
.ErrSig
.ALG
])
131 assert sigs
[0].is_trusted() is False
132 assert str(sigs
[0]).startswith('ERROR')
134 def testBadSig(self
):
135 self
.assertEquals("Hell0\n", self
.check_bad(bad_sig
))
137 def testBadXMLSig(self
):
138 self
.assertEquals(bad_xml_sig
, self
.check_bad(bad_xml_sig
))
140 def testInvalidXMLSig(self
):
141 for error
, sig
in invalid_xmls_sigs
:
143 self
.check_bad(bad_xml_main
+ '\n' + sig
)
144 except model
.SafeException
, ex
:
145 if error
not in str(ex
):
146 raise model
.SafeException(str(ex
) + '\nSig:\n' + sig
)
148 def check_bad(self
, sig
):
149 stream
= tempfile
.TemporaryFile()
152 data
, sigs
= gpg
.check_stream(stream
)
153 assert len(sigs
) == 1
154 assert isinstance(sigs
[0], gpg
.BadSig
)
155 self
.assertEquals("AE07828059A53CC1",
156 sigs
[0].status
[gpg
.BadSig
.KEYID
])
157 assert sigs
[0].is_trusted() is False
158 assert sigs
[0].need_key() is None
159 assert str(sigs
[0]).startswith('BAD')
162 def testGoodSig(self
):
163 self
.assertEquals("Hello\n", self
.check_good(good_sig
))
165 def testGoodXMLSig(self
):
166 self
.assertEquals(good_xml_sig
, self
.check_good(good_xml_sig
))
168 def check_good(self
, sig
):
169 stream
= tempfile
.TemporaryFile()
172 data
, sigs
= gpg
.check_stream(stream
)
173 assert len(sigs
) == 1
174 assert isinstance(sigs
[0], gpg
.ValidSig
)
175 self
.assertEquals("92429807C9853C0744A68B9AAE07828059A53CC1",
177 assert sigs
[0].is_trusted() is True
178 assert sigs
[0].need_key() is None
179 assert str(sigs
[0]).startswith('Valid')
180 for item
in sigs
[0].get_details():
181 if item
[0] in ('pub', 'uid') and len(item
) > 9:
183 "Thomas Leonard <tal197@users.sourceforge.net>",
187 self
.fail("Missing name")
191 stream
= tempfile
.TemporaryFile()
192 stream
.write("Hello")
195 gpg
.check_stream(stream
)
197 except model
.SafeException
:
200 suite
= unittest
.makeSuite(TestGPG
)
201 if __name__
== '__main__':
202 sys
.argv
.append('-v')