~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_gpg.py

  • Committer: John Arbash Meinel
  • Date: 2008-08-28 20:13:31 UTC
  • mfrom: (3658 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3688.
  • Revision ID: john@arbash-meinel.com-20080828201331-dqffxf54l2heokll
Merge bzr.dev 3658

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007, 2009, 2011 Canonical Ltd
 
1
# Copyright (C) 2005 Canonical Ltd
2
2
#   Authors: Robert Collins <robert.collins@canonical.com>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
13
13
#
14
14
# You should have received a copy of the GNU General Public License
15
15
# along with this program; if not, write to the Free Software
16
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
17
 
18
18
"""Tests for signing and verifying blobs of data via gpg."""
19
19
 
20
20
# import system imports here
 
21
import os
21
22
import sys
22
23
 
23
24
from bzrlib import errors, ui
24
25
import bzrlib.gpg as gpg
25
 
from bzrlib.tests import TestCase
26
 
from bzrlib.tests import features
 
26
from bzrlib.tests import TestCase, TestCaseInTempDir
27
27
 
28
28
class FakeConfig(object):
29
29
 
30
 
    def gpg_signing_key(self):
31
 
        return "amy@example.com"
32
 
 
33
30
    def gpg_signing_command(self):
34
31
        return "false"
35
 
 
36
 
    def acceptable_keys(self):
37
 
        return None
38
 
 
 
32
        
39
33
 
40
34
class TestCommandLine(TestCase):
41
35
 
42
36
    def test_signing_command_line(self):
43
37
        my_gpg = gpg.GPGStrategy(FakeConfig())
44
 
        self.assertEqual(['false',  '--clearsign', '-u', 'amy@example.com'],
 
38
        self.assertEqual(['false',  '--clearsign'],
45
39
                         my_gpg._command_line())
46
40
 
47
41
    def test_checks_return_code(self):
56
50
        if sys.platform == 'win32':
57
51
            # Windows doesn't come with cat, and we don't require it
58
52
            # so lets try using python instead.
59
 
            # But stupid windows and line-ending conversions.
 
53
            # But stupid windows and line-ending conversions. 
60
54
            # It is too much work to make sys.stdout be in binary mode.
61
55
            # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65443
62
56
            my_gpg._command_line = lambda:[sys.executable, '-c',
75
69
    def test_clears_progress(self):
76
70
        content = "some content\nwith newlines\n"
77
71
        old_clear_term = ui.ui_factory.clear_term
78
 
        clear_term_called = []
 
72
        clear_term_called = [] 
79
73
        def clear_term():
80
74
            old_clear_term()
81
75
            clear_term_called.append(True)
91
85
        self.assertRaises(errors.BzrBadParameterUnicode,
92
86
                          self.assertProduces, u'foo')
93
87
 
94
 
    def import_keys(self):
95
 
        from StringIO import StringIO
96
 
        import gpgme
97
 
        context = gpgme.Context()
98
 
 
99
 
        key = StringIO("""-----BEGIN PGP PUBLIC KEY BLOCK-----
100
 
Version: GnuPG v1.4.11 (GNU/Linux)
101
 
 
102
 
mQENBE343IgBCADwzPW7kmKb2bjB+UU+1ER/ABMZspvtoZMPusUw7bk6coXHF/0W
103
 
u1K/hSYeX9xaGOfOQw41r/g13MoR9dsL6L84RLiisf38rRoBZt+d5bCbZA5Xo801
104
 
2PeoBoGo6u5oOYKAFLMvrUitPiiE0IT/oQTfC4YUrLN4A+9W0QZruPGIpIXwmZXr
105
 
L0zsqYfNqIN0ompeJenVpKpvm3loJ/zfK7R3EJ3hsv6nkUmWCFsP1Pw3UV1YuCmw
106
 
Mkdn1U7DaOql1WjXgj9ABQDJrun2TGsqrSRzBODtHKA/uOX0K3VfKBU8VZo3dXUm
107
 
1Q4ZeZC39L9qJGTH8TQYlwBLe1yAOp+vx7QJABEBAAG0JEJhemFhciBUZXN0IEtl
108
 
eSA8YmF6YWFyQGV4YW1wbGUuY29tPokBOAQTAQIAIgUCTfjciAIbAwYLCQgHAwIG
109
 
FQgCCQoLBBYCAwECHgECF4AACgkQh2gbHuMIDkWJUggAwj537fH6WW+GGLA5onys
110
 
2hZmXUq/tU+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nIgmwVzdkOIkaRJUG2tSQFvkfhvtE
126
 
LhnkL5l4MO0wrUds0UWRwa3d7j/P2ExrqXdlLmEzrifWyEQ=
127
 
=hUJn
128
 
-----END PGP PUBLIC KEY BLOCK-----
129
 
""")
130
 
 
131
 
        secret_key = StringIO("""-----BEGIN PGP PRIVATE KEY BLOCK-----
132
 
Version: GnuPG v1.4.11 (GNU/Linux)
133
 
 
134
 
lQOYBE343IgBCADwzPW7kmKb2bjB+UU+1ER/ABMZspvtoZMPusUw7bk6coXHF/0W
135
 
u1K/hSYeX9xaGOfOQw41r/g13MoR9dsL6L84RLiisf38rRoBZt+d5bCbZA5Xo801
136
 
2PeoBoGo6u5oOYKAFLMvrUitPiiE0IT/oQTfC4YUrLN4A+9W0QZruPGIpIXwmZXr
137
 
L0zsqYfNqIN0ompeJenVpKpvm3loJ/zfK7R3EJ3hsv6nkUmWCFsP1Pw3UV1YuCmw
138
 
Mkdn1U7DaOql1WjXgj9ABQDJrun2TGsqrSRzBODtHKA/uOX0K3VfKBU8VZo3dXUm
139
 
1Q4ZeZC39L9qJGTH8TQYlwBLe1yAOp+vx7QJABEBAAEAB/0RJTbV991SOtVfPQVu
140
 
LM+tD0SiOXJwIBIINlngsFHWVIiBSDb6uF8dneMR70IRnuEFHFyAUXA7PZDxvcSu
141
 
phAqIdKCWxQPkAULAS0o4U2K3ZFGh4uOqvfZ8eSnh1rETFv7Yf3u23K89cZiy99n
142
 
EtWgSqzC/2z5PaZ7/alsYCBqhHuyd4Phaud7qv7FTz8mFrCf+CCY+D08wbnZBu4g
143
 
N9tBwoxT/UKRfv3nghIh9v+3qWfBEFGhrYbt92XKFbHOQeATZz8AGIv1eqN/+ZQY
144
 
oYmvVfO3GkrWaRoPeJNLqSDEn/45O1Uh9MJ4mQclXqB0QzMShle8uusHxIeJSQsR
145
 
z//VBAD11WS7qSgCeiHR+4jDzrrlb2snnA2bfDToEomDxd/n8xm7nJWdkNfJ2BCw
146
 
KvnxYVxjFNAwkKJGRaj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lJMagZFPdhRJz2bttz01s/B8aca6CrDpFRjT
178
 
03zRFUZjwDYqZDWBC181dCE9yla4OkWd5QyRKSS2EE02KEYqRzT0RngQn7s4AW2r
179
 
326up3Jhleln3hgD4Kk3V3KHmyK8zqZA0qWzry4Vl2jjkbnAPB2JAR8EGAECAAkF
180
 
Ak343IgCGwwACgkQh2gbHuMIDkWtHwgA2c4Xc9SPVNtFzBXpGhaYewVAes+YzyiB
181
 
eJrUuy+MTWIRagL6a68qHo7J1/3STpmyVLut0OenarNhjxb9OIOTE/5++xniYyLV
182
 
jQX7e3LWPNaFyycHZWslKJq/EReTS0o+s2/gRQZYbrAewL1MGPfsh/6eszKKb3hU
183
 
e8jR4eZfzIyU3g7cd+aAP0Jsl1TG54/d7fU4hi7CH/oXsYgjxM0RUdT/y+H+ZBwL
184
 
Gmk1tz5uh9/6Qiyhr9MAwvC0mhKtfWdebQre9l49EuciCbBXN2Q4iRpElQba1JAW
185
 
+R+G+0QuGeQvmXgw7TCtR2zRRZHBrd3uP8/YTGupd2UuYTOuJ9bIRA==
186
 
=LXn0
187
 
-----END PGP PRIVATE KEY BLOCK-----
188
 
""")
189
 
 
190
 
        context.import_(key)
191
 
        context.import_(secret_key)
192
 
 
193
 
    def test_verify_valid(self):
194
 
        self.requireFeature(features.gpgme)
195
 
        self.import_keys()
196
 
            
197
 
        content = """-----BEGIN PGP SIGNED MESSAGE-----
198
 
Hash: SHA1
199
 
 
200
 
bazaar-ng testament short form 1
201
 
revision-id: amy@example.com-20110527185938-hluafawphszb8dl1
202
 
sha1: 6411f9bdf6571200357140c9ce7c0f50106ac9a4
203
 
-----BEGIN PGP SIGNATURE-----
204
 
Version: GnuPG v1.4.11 (GNU/Linux)
205
 
 
206
 
iQEcBAEBAgAGBQJN+ekFAAoJEIdoGx7jCA5FGtEH/i+XxJRvqU6wdBtLVrGBMAGk
207
 
FZ5VP+KyXYtymSbgSstj/vM12NeMIeFs3xGnNnYuX1MIcY6We5TKtCH0epY6ym5+
208
 
6g2Q2QpQ5/sT2d0mWzR0K4uVngmxVQaXTdk5PdZ40O7ULeDLW6CxzxMHyUL1rsIx
209
 
7UBUTBh1O/1n3ZfD99hUkm3hVcnsN90uTKH59zV9NWwArU0cug60+5eDKJhSJDbG
210
 
rIwlqbFAjDZ7L/48e+IaYIJwBZFzMBpJKdCxzALLtauMf+KK8hGiL2hrRbWm7ty6
211
 
NgxfkMYOB4rDPdSstT35N+5uBG3n/UzjxHssi0svMfVETYYX40y57dm2eZQXFp8=
212
 
=iwsn
213
 
-----END PGP SIGNATURE-----
214
 
"""
215
 
        plain = """bazaar-ng testament short form 1
216
 
revision-id: amy@example.com-20110527185938-hluafawphszb8dl1
217
 
sha1: 6411f9bdf6571200357140c9ce7c0f50106ac9a4
218
 
"""
219
 
        my_gpg = gpg.GPGStrategy(FakeConfig())
220
 
        my_gpg.set_acceptable_keys("bazaar@example.com")
221
 
        self.assertEqual((gpg.SIGNATURE_VALID, None), my_gpg.verify(content,
222
 
                            plain))
223
 
 
224
 
    def test_verify_bad_testament(self):
225
 
        self.requireFeature(features.gpgme)
226
 
        self.import_keys()
227
 
            
228
 
        content = """-----BEGIN PGP SIGNED MESSAGE-----
229
 
Hash: SHA1
230
 
 
231
 
bazaar-ng testament short form 1
232
 
revision-id: amy@example.com-20110527185938-hluafawphszb8dl1
233
 
sha1: 6411f9bdf6571200357140c9ce7c0f50106ac9a4
234
 
-----BEGIN PGP SIGNATURE-----
235
 
Version: GnuPG v1.4.11 (GNU/Linux)
236
 
 
237
 
iQEcBAEBAgAGBQJN+ekFAAoJEIdoGx7jCA5FGtEH/i+XxJRvqU6wdBtLVrGBMAGk
238
 
FZ5VP+KyXYtymSbgSstj/vM12NeMIeFs3xGnNnYuX1MIcY6We5TKtCH0epY6ym5+
239
 
6g2Q2QpQ5/sT2d0mWzR0K4uVngmxVQaXTdk5PdZ40O7ULeDLW6CxzxMHyUL1rsIx
240
 
7UBUTBh1O/1n3ZfD99hUkm3hVcnsN90uTKH59zV9NWwArU0cug60+5eDKJhSJDbG
241
 
rIwlqbFAjDZ7L/48e+IaYIJwBZFzMBpJKdCxzALLtauMf+KK8hGiL2hrRbWm7ty6
242
 
NgxfkMYOB4rDPdSstT35N+5uBG3n/UzjxHssi0svMfVETYYX40y57dm2eZQXFp8=
243
 
=iwsn
244
 
-----END PGP SIGNATURE-----
245
 
"""
246
 
        plain = """bazaar-ng testament short form 1
247
 
revision-id: doctor@example.com-20110527185938-hluafawphszb8dl1
248
 
sha1: 6411f9bdf6571200357140c9ce7c0f50106ac9a4
249
 
"""
250
 
        my_gpg = gpg.GPGStrategy(FakeConfig())
251
 
        my_gpg.set_acceptable_keys("bazaar@example.com")
252
 
        self.assertEqual((gpg.SIGNATURE_NOT_VALID, None), my_gpg.verify(content,
253
 
                            plain))
254
 
 
255
 
    def test_verify_invalid(self):
256
 
        self.requireFeature(features.gpgme)
257
 
        content = """-----BEGIN PGP SIGNED MESSAGE-----
258
 
Hash: SHA1
259
 
 
260
 
bazaar-ng testament short form 1
261
 
revision-id: amy@example.com-20110527185938-hluafawphszb8dl1
262
 
sha1: 6411f9bdf6571200357140c9ce7c0f50106ac9a4
263
 
-----BEGIN PGP SIGNATURE-----
264
 
Version: GnuPG v1.4.11 (GNU/Linux)
265
 
 
266
 
iEYEARECAAYFAk33gYsACgkQpQbm1N1NUIhiDACglOuQDlnSF4NxfHSkN/zrmFy8
267
 
nswAoNGXAVuR9ONasAKIGBNUE0b+lols
268
 
=SOuC
269
 
-----END PGP SIGNATURE-----
270
 
"""
271
 
        plain = """bazaar-ng testament short form 1
272
 
revision-id: amy@example.com-20110527185938-hluafawphszb8dl1
273
 
sha1: 6411f9bdf6571200357140c9ce7c0f50106ac9a4
274
 
"""
275
 
        my_gpg = gpg.GPGStrategy(FakeConfig())
276
 
        self.assertEqual((gpg.SIGNATURE_NOT_VALID, None),
277
 
                            my_gpg.verify(content, plain))
278
 
 
279
 
    def test_set_acceptable_keys(self):
280
 
        self.requireFeature(features.gpgme)
281
 
        self.import_keys()
282
 
        my_gpg = gpg.GPGStrategy(FakeConfig())
283
 
        my_gpg.set_acceptable_keys("bazaar@example.com")
284
 
        self.assertEqual(my_gpg.acceptable_keys,
285
 
                         [u'B5DEED5FCB15DAE6ECEF919587681B1EE3080E45'])
286
 
 
287
 
    def test_set_acceptable_keys_unknown(self):
288
 
        self.requireFeature(features.gpgme)
289
 
        my_gpg = gpg.GPGStrategy(FakeConfig())
290
 
        my_gpg.set_acceptable_keys("unknown")
291
 
        self.assertEqual(my_gpg.acceptable_keys, [])
292
 
 
293
88
 
294
89
class TestDisabled(TestCase):
295
90
 
296
91
    def test_sign(self):
297
92
        self.assertRaises(errors.SigningFailed,
298
93
                          gpg.DisabledGPGStrategy(None).sign, 'content')
299
 
 
300
 
    def test_verify(self):
301
 
        self.assertRaises(errors.SignatureVerificationFailed,
302
 
                          gpg.DisabledGPGStrategy(None).verify, 'content',
303
 
                          'testament')