Started updating main GUI for Python 3 and GTK 3
[zeroinstall/solver.git] / tests / testgpg.py
blob49d60660273916ac16065a796f46af4077fbe25f
1 #!/usr/bin/env python
2 from basetest import BaseTest
3 import sys, tempfile
4 import unittest
5 import warnings
7 sys.path.insert(0, '..')
8 from zeroinstall.injector import gpg, model, trust
10 err_sig = b"""<?xml version="1.0" ?>
11 <?xml-stylesheet type='text/xsl' href='interface.xsl'?>
12 <interface xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
13 <name>test</name>
14 <summary>test</summary>
15 </interface>
16 <!-- Base64 Signature
17 iJwEAAECAAYFAk1NVyAACgkQerial32qo5eVCgP/RYEzT43M2Dj3winnkX2HQDO2Fx5dq83pmidd
18 LDEID3FxbuIpMUP/2rvPmNM3itRo/J4R2xkM65TEol/55uxDC1bbuarKf3wbgwEF60srFEDeeiYM
19 FmTQtWYPtrzAGtNRTgKfD75xk9lcM2GHmKNlgSQ7G8ZsfL6KaraF4Wa6nqU=
21 -->
22 """
24 bad_xml_main = b"""<?xml version='1.0'?>
25 <root/>"""
27 invalid_xmls_sigs = [
28 ('last line is not end-of-comment',
29 b"""<!-- Base64 Signature
30 """),
31 ('No signature block in XML',
32 b"""<!-- Base64 Sig
33 iD8DBQBDtpK9rgeCgFmlPMERAg0gAKCaJhXFnk
34 -->
35 """),
36 ('extra data on comment line',
37 b"""<!-- Base64 Signature data
38 iD8DBQBDtpK9rgeCgFmlPMERAg0gAKCaJhXFnk
39 -->
40 """),
41 ('last line is not end-of-comment',
42 b"""<!-- Base64 Signature
43 iD8DBQBDtpK9rgeCgFmlPMERAg0gAKCaJhXFnk
44 WZRBLT0an56WYaBODukSsf4=
45 --> More
46 """),
47 ('Invalid base 64 encoded signature:',
48 b"""<!-- Base64 Signature
49 iD8DBQBDtpK9rgeCgFmlPMERAg0gAKCaJhXFnk
50 WZRBLT0an56WYaBODukSsf4=
51 =zMc+
52 -->
53 """),
54 ('Invalid characters found',
55 b"""<!-- Base64 Signature
56 iD8DBQBDtpK9rge<CgFmlPMERAg0gAKCaJhXFnk
57 WZRBLT0an56WYaBODukSsf4=
58 -->
59 """)]
61 good_xml_sig = b"""<?xml version='1.0'?>
62 <root/>
63 <!-- Base64 Signature
64 iD8DBQBDuChIrgeCgFmlPMERAnGEAJ0ZS1PeyWonx6xS/mgpYTKNgSXa5QCeMSYPHhNcvxu3f84y
65 Uk7hxHFeQPo=
66 -->
67 """
69 bad_xml_sig = b"""<?xml version='1.0'?>
70 <ro0t/>
71 <!-- Base64 Signature
72 iD8DBQBDuChIrgeCgFmlPMERAnGEAJ0ZS1PeyWonx6xS/mgpYTKNgSXa5QCeMSYPHhNcvxu3f84y
73 Uk7hxHFeQPo=
74 -->
75 """
77 from data import thomas_key
79 THOMAS_FINGERPRINT = '92429807C9853C0744A68B9AAE07828059A53CC1'
81 class TestGPG(BaseTest):
82 def setUp(self):
83 BaseTest.setUp(self)
85 with tempfile.TemporaryFile(mode = 'w+b') as stream:
86 stream.write(thomas_key)
87 stream.seek(0)
88 gpg.import_key(stream)
89 trust.trust_db.trust_key(THOMAS_FINGERPRINT)
90 warnings.filterwarnings("ignore", category = DeprecationWarning)
92 def testImportBad(self):
93 with tempfile.TemporaryFile(mode = 'w+b') as stream:
94 stream.write(b"Bad key")
95 stream.seek(0)
96 try:
97 gpg.import_key(stream)
98 assert False
99 except model.SafeException:
100 pass # OK
102 def testErrSig(self):
103 with tempfile.TemporaryFile(mode = 'w+b') as stream:
104 stream.write(err_sig)
105 stream.seek(0)
106 data, sigs = gpg.check_stream(stream)
107 self.assertEqual(err_sig, data.read())
108 assert len(sigs) == 1
109 assert isinstance(sigs[0], gpg.ErrSig)
110 assert sigs[0].need_key() == "7AB89A977DAAA397"
111 self.assertEqual("1", sigs[0].status[gpg.ErrSig.ALG])
112 assert sigs[0].is_trusted() is False
113 assert str(sigs[0]).startswith('ERROR')
115 def testBadXMLSig(self):
116 self.assertEqual(bad_xml_sig, self.check_bad(bad_xml_sig))
118 def testInvalidXMLSig(self):
119 for error, sig in invalid_xmls_sigs:
120 try:
121 self.check_bad(bad_xml_main + b'\n' + sig)
122 except model.SafeException as ex:
123 if error not in str(ex):
124 raise model.SafeException(str(ex) + '\nSig:\n' + sig)
126 def check_bad(self, sig):
127 with tempfile.TemporaryFile(mode = 'w+b') as stream:
128 stream.write(sig)
129 stream.seek(0)
130 data, sigs = gpg.check_stream(stream)
131 assert len(sigs) == 1
132 assert isinstance(sigs[0], gpg.BadSig)
133 self.assertEqual("AE07828059A53CC1",
134 sigs[0].status[gpg.BadSig.KEYID])
135 assert sigs[0].is_trusted() is False
136 assert sigs[0].need_key() is None
137 assert str(sigs[0]).startswith('BAD')
138 return data.read()
140 def testGoodXMLSig(self):
141 self.assertEqual(good_xml_sig, self.check_good(good_xml_sig))
143 def check_good(self, sig):
144 with tempfile.TemporaryFile(mode = 'w+b') as stream:
145 stream.write(sig)
146 stream.seek(0)
147 data, sigs = gpg.check_stream(stream)
149 assert len(sigs) == 1
150 assert isinstance(sigs[0], gpg.ValidSig)
151 self.assertEqual("92429807C9853C0744A68B9AAE07828059A53CC1",
152 sigs[0].fingerprint)
153 assert sigs[0].is_trusted() is True
154 assert sigs[0].need_key() is None
155 assert str(sigs[0]).startswith('Valid')
156 for item in sigs[0].get_details():
157 if item[0] == 'uid' and len(item) > 9:
158 assert item[9] in ["Thomas Leonard <tal197@users.sourceforge.net>"], str(item)
159 break
160 else:
161 self.fail("Missing name")
162 return data.read()
164 def testNoSig(self):
165 with tempfile.TemporaryFile(mode = 'w+b') as stream:
166 stream.write(b"Hello")
167 stream.seek(0)
168 try:
169 gpg.check_stream(stream)
170 assert False
171 except model.SafeException:
172 pass # OK
174 def testLoadKeys(self):
175 self.assertEqual({}, gpg.load_keys([]))
176 keys = gpg.load_keys([THOMAS_FINGERPRINT])
177 self.assertEqual(1, len(keys))
178 key = keys[THOMAS_FINGERPRINT]
179 self.assertEqual(THOMAS_FINGERPRINT, key.fingerprint)
180 self.assertEqual('Thomas Leonard <tal197@users.sourceforge.net>',
181 key.name)
183 if __name__ == '__main__':
184 unittest.main()