Test unpacking .zip archives.
[zeroinstall/zeroinstall-mseaborn.git] / tests / testgpg.py
blob8e762689c8b77a772f37e9b72f45dfeba9505c9c
1 #!/usr/bin/env python2.4
2 from basetest import BaseTest
3 import sys, tempfile, os, shutil
4 import unittest
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.
12 <!-- Base64 Signature
13 iD8DBQBHcCKvP++6OUJbtdsRAnosAKDSRHUhZj/BFt6VbCNPH68e46Co7ACfZTkc8jpATVOoAlb6\nMuRsSmvz99k=
14 -->
15 """
17 bad_xml_main = """<?xml version='1.0'?>
18 <root/>"""
20 invalid_xmls_sigs = [
21 ('last line is not end-of-comment',
22 """<!-- Base64 Signature
23 """),
24 ('No signature block in XML',
25 """<!-- Base64 Sig
26 iD8DBQBDtpK9rgeCgFmlPMERAg0gAKCaJhXFnk
27 -->
28 """),
29 ('extra data on comment line',
30 """<!-- Base64 Signature data
31 iD8DBQBDtpK9rgeCgFmlPMERAg0gAKCaJhXFnk
32 -->
33 """),
34 ('last line is not end-of-comment',
35 """<!-- Base64 Signature
36 iD8DBQBDtpK9rgeCgFmlPMERAg0gAKCaJhXFnk
37 WZRBLT0an56WYaBODukSsf4=
38 --> More
39 """),
40 ('Invalid base 64 encoded signature:',
41 """<!-- Base64 Signature
42 iD8DBQBDtpK9rgeCgFmlPMERAg0gAKCaJhXFnk
43 WZRBLT0an56WYaBODukSsf4=
44 =zMc+
45 -->
46 """),
47 ('Invalid characters found',
48 """<!-- Base64 Signature
49 iD8DBQBDtpK9rge<CgFmlPMERAg0gAKCaJhXFnk
50 WZRBLT0an56WYaBODukSsf4=
51 -->
52 """)]
54 good_xml_sig = """<?xml version='1.0'?>
55 <root/>
56 <!-- Base64 Signature
57 iD8DBQBDuChIrgeCgFmlPMERAnGEAJ0ZS1PeyWonx6xS/mgpYTKNgSXa5QCeMSYPHhNcvxu3f84y
58 Uk7hxHFeQPo=
59 -->
60 """
62 bad_xml_sig = """<?xml version='1.0'?>
63 <ro0t/>
64 <!-- Base64 Signature
65 iD8DBQBDuChIrgeCgFmlPMERAnGEAJ0ZS1PeyWonx6xS/mgpYTKNgSXa5QCeMSYPHhNcvxu3f84y
66 Uk7hxHFeQPo=
67 -->
68 """
70 from data import thomas_key
72 class TestGPG(BaseTest):
73 def setUp(self):
74 BaseTest.setUp(self)
76 stream = tempfile.TemporaryFile()
77 stream.write(thomas_key)
78 stream.seek(0)
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")
86 stream.seek(0)
87 try:
88 gpg.import_key(stream)
89 assert False
90 except model.SafeException:
91 pass # OK
93 def testErrSig(self):
94 stream = tempfile.TemporaryFile()
95 stream.write(err_sig)
96 stream.seek(0)
97 data, sigs = gpg.check_stream(stream)
98 self.assertEquals(err_sig, data.read())
99 assert len(sigs) == 1
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:
111 try:
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()
119 stream.write(sig)
120 stream.seek(0)
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')
129 return data.read()
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()
136 stream.write(sig)
137 stream.seek(0)
138 data, sigs = gpg.check_stream(stream)
139 assert len(sigs) == 1
140 assert isinstance(sigs[0], gpg.ValidSig)
141 self.assertEquals("92429807C9853C0744A68B9AAE07828059A53CC1",
142 sigs[0].fingerprint)
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:
148 self.assertEquals(
149 "Thomas Leonard <tal197@users.sourceforge.net>",
150 item[9])
151 break
152 else:
153 self.fail("Missing name")
154 return data.read()
156 def testNoSig(self):
157 stream = tempfile.TemporaryFile()
158 stream.write("Hello")
159 stream.seek(0)
160 try:
161 gpg.check_stream(stream)
162 assert False
163 except model.SafeException:
164 pass # OK
166 def testLoadKeys(self):
167 self.assertEquals({}, gpg.load_keys([]))
169 suite = unittest.makeSuite(TestGPG)
170 if __name__ == '__main__':
171 sys.argv.append('-v')
172 unittest.main()