2 from basetest
import BaseTest
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">
14 <summary>test</summary>
17 iJwEAAECAAYFAk1NVyAACgkQerial32qo5eVCgP/RYEzT43M2Dj3winnkX2HQDO2Fx5dq83pmidd
18 LDEID3FxbuIpMUP/2rvPmNM3itRo/J4R2xkM65TEol/55uxDC1bbuarKf3wbgwEF60srFEDeeiYM
19 FmTQtWYPtrzAGtNRTgKfD75xk9lcM2GHmKNlgSQ7G8ZsfL6KaraF4Wa6nqU=
24 bad_xml_main
= b
"""<?xml version='1.0'?>
28 ('last line is not end-of-comment',
29 b
"""<!-- Base64 Signature
31 ('No signature block in XML',
33 iD8DBQBDtpK9rgeCgFmlPMERAg0gAKCaJhXFnk
36 ('extra data on comment line',
37 b
"""<!-- Base64 Signature data
38 iD8DBQBDtpK9rgeCgFmlPMERAg0gAKCaJhXFnk
41 ('last line is not end-of-comment',
42 b
"""<!-- Base64 Signature
43 iD8DBQBDtpK9rgeCgFmlPMERAg0gAKCaJhXFnk
44 WZRBLT0an56WYaBODukSsf4=
47 ('Invalid base 64 encoded signature:',
48 b
"""<!-- Base64 Signature
49 iD8DBQBDtpK9rgeCgFmlPMERAg0gAKCaJhXFnk
50 WZRBLT0an56WYaBODukSsf4=
54 ('Invalid characters found',
55 b
"""<!-- Base64 Signature
56 iD8DBQBDtpK9rge<CgFmlPMERAg0gAKCaJhXFnk
57 WZRBLT0an56WYaBODukSsf4=
61 good_xml_sig
= b
"""<?xml version='1.0'?>
64 iD8DBQBDuChIrgeCgFmlPMERAnGEAJ0ZS1PeyWonx6xS/mgpYTKNgSXa5QCeMSYPHhNcvxu3f84y
69 bad_xml_sig
= b
"""<?xml version='1.0'?>
72 iD8DBQBDuChIrgeCgFmlPMERAnGEAJ0ZS1PeyWonx6xS/mgpYTKNgSXa5QCeMSYPHhNcvxu3f84y
77 from data
import thomas_key
79 THOMAS_FINGERPRINT
= '92429807C9853C0744A68B9AAE07828059A53CC1'
81 class TestGPG(BaseTest
):
85 with tempfile
.TemporaryFile(mode
= 'w+b') as stream
:
86 stream
.write(thomas_key
)
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")
97 gpg
.import_key(stream
)
99 except model
.SafeException
:
102 def testErrSig(self
):
103 with tempfile
.TemporaryFile(mode
= 'w+b') as stream
:
104 stream
.write(err_sig
)
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
:
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
:
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')
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
:
147 data
, sigs
= gpg
.check_stream(stream
)
149 assert len(sigs
) == 1
150 assert isinstance(sigs
[0], gpg
.ValidSig
)
151 self
.assertEqual("92429807C9853C0744A68B9AAE07828059A53CC1",
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
)
161 self
.fail("Missing name")
165 with tempfile
.TemporaryFile(mode
= 'w+b') as stream
:
166 stream
.write(b
"Hello")
169 gpg
.check_stream(stream
)
171 except model
.SafeException
:
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>',
183 if __name__
== '__main__':