Fixed rating menu not showing localized strings
[zeroinstall.git] / tests / testgpg.py
blob24aedfe344dda26f787d79629ec44cd0cf8170d8
1 #!/usr/bin/env python2.5
2 from basetest import BaseTest
3 import sys, tempfile
4 import unittest
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==
14 =kMeU
15 -----END PGP MESSAGE-----
16 """
18 bad_sig = """-----BEGIN PGP SIGNED MESSAGE-----
19 Hash: SHA1
21 Hell0
22 -----BEGIN PGP SIGNATURE-----
23 Version: GnuPG v1.4.0 (GNU/Linux)
25 iD8DBQFCfk3grgeCgFmlPMERAhl8AKC0aktrLzz646zTY0TRzdnxPdbLBgCeJWbk
26 GRVbJusevCKvtoSn7RAW2mg=
27 =xQJ5
28 -----END PGP SIGNATURE-----
29 """
31 good_sig = """-----BEGIN PGP SIGNED MESSAGE-----
32 Hash: SHA1
34 Hello
35 -----BEGIN PGP SIGNATURE-----
36 Version: GnuPG v1.4.0 (GNU/Linux)
38 iD8DBQFCfk3grgeCgFmlPMERAhl8AKC0aktrLzz646zTY0TRzdnxPdbLBgCeJWbk
39 GRVbJusevCKvtoSn7RAW2mg=
40 =xQJ5
41 -----END PGP SIGNATURE-----
42 """
44 bad_xml_main = """<?xml version='1.0'?>
45 <root/>"""
47 invalid_xmls_sigs = [
48 ('last line is not end-of-comment',
49 """<!-- Base64 Signature
50 """),
51 ('No signature block in XML',
52 """<!-- Base64 Sig
53 iD8DBQBDtpK9rgeCgFmlPMERAg0gAKCaJhXFnk
54 -->
55 """),
56 ('extra data on comment line',
57 """<!-- Base64 Signature data
58 iD8DBQBDtpK9rgeCgFmlPMERAg0gAKCaJhXFnk
59 -->
60 """),
61 ('last line is not end-of-comment',
62 """<!-- Base64 Signature
63 iD8DBQBDtpK9rgeCgFmlPMERAg0gAKCaJhXFnk
64 WZRBLT0an56WYaBODukSsf4=
65 --> More
66 """),
67 ('Invalid base 64 encoded signature:',
68 """<!-- Base64 Signature
69 iD8DBQBDtpK9rgeCgFmlPMERAg0gAKCaJhXFnk
70 WZRBLT0an56WYaBODukSsf4=
71 =zMc+
72 -->
73 """),
74 ('Invalid characters found',
75 """<!-- Base64 Signature
76 iD8DBQBDtpK9rge<CgFmlPMERAg0gAKCaJhXFnk
77 WZRBLT0an56WYaBODukSsf4=
78 -->
79 """)]
81 good_xml_sig = """<?xml version='1.0'?>
82 <root/>
83 <!-- Base64 Signature
84 iD8DBQBDuChIrgeCgFmlPMERAnGEAJ0ZS1PeyWonx6xS/mgpYTKNgSXa5QCeMSYPHhNcvxu3f84y
85 Uk7hxHFeQPo=
86 -->
87 """
89 bad_xml_sig = """<?xml version='1.0'?>
90 <ro0t/>
91 <!-- Base64 Signature
92 iD8DBQBDuChIrgeCgFmlPMERAnGEAJ0ZS1PeyWonx6xS/mgpYTKNgSXa5QCeMSYPHhNcvxu3f84y
93 Uk7hxHFeQPo=
94 -->
95 """
97 from data import thomas_key
99 THOMAS_FINGERPRINT = '92429807C9853C0744A68B9AAE07828059A53CC1'
101 class TestGPG(BaseTest):
102 def setUp(self):
103 BaseTest.setUp(self)
105 stream = tempfile.TemporaryFile()
106 stream.write(thomas_key)
107 stream.seek(0)
108 gpg.import_key(stream)
109 trust.trust_db.trust_key(THOMAS_FINGERPRINT)
111 def testImportBad(self):
112 stream = tempfile.TemporaryFile()
113 stream.write("Bad key")
114 stream.seek(0)
115 try:
116 gpg.import_key(stream)
117 assert False
118 except model.SafeException:
119 pass # OK
121 def testErrSig(self):
122 stream = tempfile.TemporaryFile()
123 stream.write(err_sig)
124 stream.seek(0)
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:
142 try:
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()
150 stream.write(sig)
151 stream.seek(0)
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')
160 return data.read()
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()
170 stream.write(sig)
171 stream.seek(0)
172 data, sigs = gpg.check_stream(stream)
173 assert len(sigs) == 1
174 assert isinstance(sigs[0], gpg.ValidSig)
175 self.assertEquals("92429807C9853C0744A68B9AAE07828059A53CC1",
176 sigs[0].fingerprint)
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:
182 self.assertEquals(
183 "Thomas Leonard <tal197@users.sourceforge.net>",
184 item[9])
185 break
186 else:
187 self.fail("Missing name")
188 return data.read()
190 def testNoSig(self):
191 stream = tempfile.TemporaryFile()
192 stream.write("Hello")
193 stream.seek(0)
194 try:
195 gpg.check_stream(stream)
196 assert False
197 except model.SafeException:
198 pass # OK
200 def testLoadKeys(self):
202 self.assertEquals({}, gpg.load_keys([]))
203 keys = gpg.load_keys([THOMAS_FINGERPRINT])
204 self.assertEquals(1, len(keys))
205 key = keys[THOMAS_FINGERPRINT]
206 self.assertEquals(THOMAS_FINGERPRINT, key.fingerprint)
207 self.assertEquals('Thomas Leonard <tal197@users.sourceforge.net>',
208 key.name)
210 suite = unittest.makeSuite(TestGPG)
211 if __name__ == '__main__':
212 sys.argv.append('-v')
213 unittest.main()