Minor style improvement to policy menu.
[zeroinstall.git] / tests / testgpg.py
blob482ff7aa5505f5be2b36350a79a3aeddc27cfa94
1 #!/usr/bin/env python2.3
2 import sys, tempfile, os, shutil
3 import unittest
5 sys.path.insert(0, '..')
6 from zeroinstall.injector import gpg, model, basedir, trust
8 err_sig = """-----BEGIN PGP MESSAGE-----
9 Version: GnuPG v1.4.0 (GNU/Linux)
11 owGbwMvMwCTYk9R5Infvsj7G01xJDE513j1OiSlcHfbMrCDOBJisINP6XQwLGjzn
12 tMxedXc3y75I7r1hQZFTb/ewMcx3yefZ8zb/vZd10I7LEYdDj4fnKsYAAA==
13 =kMeU
14 -----END PGP MESSAGE-----
15 """
17 bad_sig = """-----BEGIN PGP SIGNED MESSAGE-----
18 Hash: SHA1
20 Hell0
21 -----BEGIN PGP SIGNATURE-----
22 Version: GnuPG v1.4.0 (GNU/Linux)
24 iD8DBQFCfk3grgeCgFmlPMERAhl8AKC0aktrLzz646zTY0TRzdnxPdbLBgCeJWbk
25 GRVbJusevCKvtoSn7RAW2mg=
26 =xQJ5
27 -----END PGP SIGNATURE-----
28 """
30 good_sig = """-----BEGIN PGP SIGNED MESSAGE-----
31 Hash: SHA1
33 Hello
34 -----BEGIN PGP SIGNATURE-----
35 Version: GnuPG v1.4.0 (GNU/Linux)
37 iD8DBQFCfk3grgeCgFmlPMERAhl8AKC0aktrLzz646zTY0TRzdnxPdbLBgCeJWbk
38 GRVbJusevCKvtoSn7RAW2mg=
39 =xQJ5
40 -----END PGP SIGNATURE-----
41 """
43 bad_xml_main = """<?xml version='1.0'?>
44 <root/>"""
46 invalid_xmls_sigs = [
47 ('last line is not end-of-comment',
48 """<!-- Base64 Signature
49 """),
50 ('No signature block in XML',
51 """<!-- Base64 Sig
52 iD8DBQBDtpK9rgeCgFmlPMERAg0gAKCaJhXFnk
53 -->
54 """),
55 ('extra data on comment line',
56 """<!-- Base64 Signature data
57 iD8DBQBDtpK9rgeCgFmlPMERAg0gAKCaJhXFnk
58 -->
59 """),
60 ('last line is not end-of-comment',
61 """<!-- Base64 Signature
62 iD8DBQBDtpK9rgeCgFmlPMERAg0gAKCaJhXFnk
63 WZRBLT0an56WYaBODukSsf4=
64 --> More
65 """),
66 ('Invalid base 64 encoded signature:',
67 """<!-- Base64 Signature
68 iD8DBQBDtpK9rgeCgFmlPMERAg0gAKCaJhXFnk
69 WZRBLT0an56WYaBODukSsf4=
70 =zMc+
71 -->
72 """),
73 ('Invalid characters found',
74 """<!-- Base64 Signature
75 iD8DBQBDtpK9rge<CgFmlPMERAg0gAKCaJhXFnk
76 WZRBLT0an56WYaBODukSsf4=
77 -->
78 """)]
80 good_xml_sig = """<?xml version='1.0'?>
81 <root/>
82 <!-- Base64 Signature
83 iD8DBQBDuChIrgeCgFmlPMERAnGEAJ0ZS1PeyWonx6xS/mgpYTKNgSXa5QCeMSYPHhNcvxu3f84y
84 Uk7hxHFeQPo=
85 -->
86 """
88 bad_xml_sig = """<?xml version='1.0'?>
89 <ro0t/>
90 <!-- Base64 Signature
91 iD8DBQBDuChIrgeCgFmlPMERAnGEAJ0ZS1PeyWonx6xS/mgpYTKNgSXa5QCeMSYPHhNcvxu3f84y
92 Uk7hxHFeQPo=
93 -->
94 """
96 from data import thomas_key
98 class TestGPG(unittest.TestCase):
99 def setUp(self):
100 self.gnupg_home = tempfile.mktemp()
101 self.config_home = tempfile.mktemp()
102 os.mkdir(self.config_home, 0700)
103 os.environ['XDG_CONFIG_HOME'] = self.config_home
104 os.environ['GNUPGHOME'] = self.gnupg_home
105 reload(basedir)
106 os.mkdir(self.gnupg_home, 0700)
107 stream = tempfile.TemporaryFile()
108 stream.write(thomas_key)
109 stream.seek(0)
110 gpg.import_key(stream)
111 trust.trust_db.trust_key(
112 '92429807C9853C0744A68B9AAE07828059A53CC1')
114 def tearDown(self):
115 shutil.rmtree(self.gnupg_home)
116 shutil.rmtree(self.config_home)
118 def testImportBad(self):
119 stream = tempfile.TemporaryFile()
120 stream.write("Bad key")
121 stream.seek(0)
122 try:
123 gpg.import_key(stream)
124 assert False
125 except model.SafeException:
126 pass # OK
128 def testErrSig(self):
129 stream = tempfile.TemporaryFile()
130 stream.write(err_sig)
131 stream.seek(0)
132 data, sigs = gpg.check_stream(stream)
133 self.assertEquals("Bad\n", data.read())
134 assert len(sigs) == 1
135 assert isinstance(sigs[0], gpg.ErrSig)
136 assert sigs[0].need_key() == "8C6289C86DBDA68E"
137 self.assertEquals("17", sigs[0].status[gpg.ErrSig.ALG])
138 assert sigs[0].is_trusted() is False
139 assert str(sigs[0]).startswith('ERROR')
141 def testBadSig(self):
142 self.assertEquals("Hell0\n", self.check_bad(bad_sig))
144 def testBadXMLSig(self):
145 self.assertEquals(bad_xml_sig, self.check_bad(bad_xml_sig))
147 def testInvalidXMLSig(self):
148 for error, sig in invalid_xmls_sigs:
149 try:
150 self.check_bad(bad_xml_main + '\n' + sig)
151 except model.SafeException, ex:
152 if error not in str(ex):
153 raise model.SafeException(str(ex) + '\nSig:\n' + sig)
155 def check_bad(self, sig):
156 stream = tempfile.TemporaryFile()
157 stream.write(sig)
158 stream.seek(0)
159 data, sigs = gpg.check_stream(stream)
160 assert len(sigs) == 1
161 assert isinstance(sigs[0], gpg.BadSig)
162 self.assertEquals("AE07828059A53CC1",
163 sigs[0].status[gpg.BadSig.KEYID])
164 assert sigs[0].is_trusted() is False
165 assert sigs[0].need_key() is None
166 assert str(sigs[0]).startswith('BAD')
167 return data.read()
169 def testGoodSig(self):
170 self.assertEquals("Hello\n", self.check_good(good_sig))
172 def testGoodXMLSig(self):
173 self.assertEquals(good_xml_sig, self.check_good(good_xml_sig))
175 def check_good(self, sig):
176 stream = tempfile.TemporaryFile()
177 stream.write(sig)
178 stream.seek(0)
179 data, sigs = gpg.check_stream(stream)
180 assert len(sigs) == 1
181 assert isinstance(sigs[0], gpg.ValidSig)
182 self.assertEquals("92429807C9853C0744A68B9AAE07828059A53CC1",
183 sigs[0].fingerprint)
184 assert sigs[0].is_trusted() is True
185 assert sigs[0].need_key() is None
186 assert str(sigs[0]).startswith('Valid')
187 for item in sigs[0].get_details():
188 if item[0] in ('pub', 'uid') and len(item) > 9:
189 self.assertEquals(
190 "Thomas Leonard <tal197@users.sourceforge.net>",
191 item[9])
192 break
193 else:
194 self.fail("Missing name")
195 return data.read()
197 def testNoSig(self):
198 stream = tempfile.TemporaryFile()
199 stream.write("Hello")
200 stream.seek(0)
201 try:
202 gpg.check_stream(stream)
203 assert False
204 except model.SafeException:
205 pass # OK
207 suite = unittest.makeSuite(TestGPG)
208 if __name__ == '__main__':
209 sys.argv.append('-v')
210 unittest.main()