1 #!/usr/bin/env python2.4
2 from basetest
import BaseTest
3 import sys
, tempfile
, os
, shutil
6 sys
.path
.insert(0, '..')
7 from zeroinstall
.injector
import gpg
, model
, trust
8 from zeroinstall
.support
import basedir
10 err_sig
= """<?xml version='1.0'?>
11 This invalid document is signed with an unknown key.
13 iD8DBQBHcCKvP++6OUJbtdsRAnosAKDSRHUhZj/BFt6VbCNPH68e46Co7ACfZTkc8jpATVOoAlb6\nMuRsSmvz99k=
17 bad_xml_main
= """<?xml version='1.0'?>
21 ('last line is not end-of-comment',
22 """<!-- Base64 Signature
24 ('No signature block in XML',
26 iD8DBQBDtpK9rgeCgFmlPMERAg0gAKCaJhXFnk
29 ('extra data on comment line',
30 """<!-- Base64 Signature data
31 iD8DBQBDtpK9rgeCgFmlPMERAg0gAKCaJhXFnk
34 ('last line is not end-of-comment',
35 """<!-- Base64 Signature
36 iD8DBQBDtpK9rgeCgFmlPMERAg0gAKCaJhXFnk
37 WZRBLT0an56WYaBODukSsf4=
40 ('Invalid base 64 encoded signature:',
41 """<!-- Base64 Signature
42 iD8DBQBDtpK9rgeCgFmlPMERAg0gAKCaJhXFnk
43 WZRBLT0an56WYaBODukSsf4=
47 ('Invalid characters found',
48 """<!-- Base64 Signature
49 iD8DBQBDtpK9rge<CgFmlPMERAg0gAKCaJhXFnk
50 WZRBLT0an56WYaBODukSsf4=
54 good_xml_sig
= """<?xml version='1.0'?>
57 iD8DBQBDuChIrgeCgFmlPMERAnGEAJ0ZS1PeyWonx6xS/mgpYTKNgSXa5QCeMSYPHhNcvxu3f84y
62 bad_xml_sig
= """<?xml version='1.0'?>
65 iD8DBQBDuChIrgeCgFmlPMERAnGEAJ0ZS1PeyWonx6xS/mgpYTKNgSXa5QCeMSYPHhNcvxu3f84y
70 from data
import thomas_key
72 class TestGPG(BaseTest
):
76 stream
= tempfile
.TemporaryFile()
77 stream
.write(thomas_key
)
79 gpg
.import_key(stream
)
80 trust
.trust_db
.trust_key(
81 '92429807C9853C0744A68B9AAE07828059A53CC1')
83 def testImportBad(self
):
84 stream
= tempfile
.TemporaryFile()
85 stream
.write("Bad key")
88 gpg
.import_key(stream
)
90 except model
.SafeException
:
94 stream
= tempfile
.TemporaryFile()
97 data
, sigs
= gpg
.check_stream(stream
)
98 self
.assertEquals(err_sig
, data
.read())
100 assert isinstance(sigs
[0], gpg
.ErrSig
)
101 assert sigs
[0].need_key() == "3FEFBA39425BB5DB"
102 self
.assertEquals("17", sigs
[0].status
[gpg
.ErrSig
.ALG
])
103 assert sigs
[0].is_trusted() is False
104 assert str(sigs
[0]).startswith('ERROR')
106 def testBadXMLSig(self
):
107 self
.assertEquals(bad_xml_sig
, self
.check_bad(bad_xml_sig
))
109 def testInvalidXMLSig(self
):
110 for error
, sig
in invalid_xmls_sigs
:
112 self
.check_bad(bad_xml_main
+ '\n' + sig
)
113 except model
.SafeException
, ex
:
114 if error
not in str(ex
):
115 raise model
.SafeException(str(ex
) + '\nSig:\n' + sig
)
117 def check_bad(self
, sig
):
118 stream
= tempfile
.TemporaryFile()
121 data
, sigs
= gpg
.check_stream(stream
)
122 assert len(sigs
) == 1
123 assert isinstance(sigs
[0], gpg
.BadSig
)
124 self
.assertEquals("AE07828059A53CC1",
125 sigs
[0].status
[gpg
.BadSig
.KEYID
])
126 assert sigs
[0].is_trusted() is False
127 assert sigs
[0].need_key() is None
128 assert str(sigs
[0]).startswith('BAD')
131 def testGoodXMLSig(self
):
132 self
.assertEquals(good_xml_sig
, self
.check_good(good_xml_sig
))
134 def check_good(self
, sig
):
135 stream
= tempfile
.TemporaryFile()
138 data
, sigs
= gpg
.check_stream(stream
)
139 assert len(sigs
) == 1
140 assert isinstance(sigs
[0], gpg
.ValidSig
)
141 self
.assertEquals("92429807C9853C0744A68B9AAE07828059A53CC1",
143 assert sigs
[0].is_trusted() is True
144 assert sigs
[0].need_key() is None
145 assert str(sigs
[0]).startswith('Valid')
146 for item
in sigs
[0].get_details():
147 if item
[0] in ('pub', 'uid') and len(item
) > 9:
149 "Thomas Leonard <tal197@users.sourceforge.net>",
153 self
.fail("Missing name")
157 stream
= tempfile
.TemporaryFile()
158 stream
.write("Hello")
161 gpg
.check_stream(stream
)
163 except model
.SafeException
:
166 def testLoadKeys(self
):
167 self
.assertEquals({}, gpg
.load_keys([]))
169 suite
= unittest
.makeSuite(TestGPG
)
170 if __name__
== '__main__':
171 sys
.argv
.append('-v')