~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_gpg.py

  • Committer: Jonathan Riddell
  • Date: 2011-09-16 11:13:47 UTC
  • mto: This revision was merged to the branch mainline in revision 6144.
  • Revision ID: jriddell@canonical.com-20110916111347-fyjk426bkl0jrbfu
gettext() show_warning usage

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
# import system imports here
21
21
import sys
22
22
 
23
 
from bzrlib import errors, ui
24
 
import bzrlib.gpg as gpg
25
 
from bzrlib.tests import TestCase
26
 
from bzrlib.tests import features
 
23
from bzrlib import (
 
24
    errors,
 
25
    gpg,
 
26
    trace,
 
27
    ui,
 
28
    )
 
29
from bzrlib.tests import (
 
30
    TestCase,
 
31
    features,
 
32
    )
27
33
 
28
34
class FakeConfig(object):
29
35
 
91
97
        self.assertRaises(errors.BzrBadParameterUnicode,
92
98
                          self.assertProduces, u'foo')
93
99
 
 
100
class TestVerify(TestCase):
 
101
 
94
102
    def import_keys(self):
95
103
        from StringIO import StringIO
96
104
        import gpgme
187
195
-----END PGP PRIVATE KEY BLOCK-----
188
196
""")
189
197
 
 
198
        revoked_key = StringIO("""-----BEGIN PGP PUBLIC KEY BLOCK-----
 
199
Version: GnuPG v1.4.11 (GNU/Linux)
 
200
 
 
201
mI0ETjlW5gEEAOb/6P+TVM59E897wRtatxys2BhsHCXM4T7xjIiANfDwejDdifqh
 
202
tluTfSJLLxPembtrrEjux1C0AJgc+f0MIfsc3Pr3eFJzKB2ot/1IVG1/1KnA0zt3
 
203
W2xPT3lRib27WJ9Fag+dMtQaIzgJ7/n2DFxsFZ33FD2kxrEXB2exGg6FABEBAAGI
 
204
pgQgAQIAEAUCTjlXkAkdAHJldm9rZWQACgkQjs6dvEpb0cQPHAP/Wi9rbx0e+1Sf
 
205
ziGgyVdr3m3A6uvze5oXKVgFRbGRUYSH4/I8GW0W9x4TcRg9h+YaQ8NUdADr9kNE
 
206
tKAljLqYA5qdqSfYuaij1M++Xj+KUZ359R74sHuQqwnRy1XXQNfRs/QpXA7vLdds
 
207
rjg+pbWuXO92TZJUdnqtWW+VEyZBsPy0G3Rlc3Qga2V5IDx0ZXN0QGV4YW1wbGUu
 
208
Y29tPoi4BBMBAgAiBQJOOVbmAhsDBgsJCAcDAgYVCAIJCgsEFgIDAQIeAQIXgAAK
 
209
CRCOzp28SlvRxNWzA/42WVmI0b+6mF/imEOlY1TiyvrcpK250rkSDsCtL4lOwy7G
 
210
antZhpgNfnXRd/ySfsS3EB6dpOWgOSxGRvWQhA+vxBT9BYNk49qd3JIrSaSWpR12
 
211
rET8qO1rEQQFWsw03CxTGujxGlmEO+a1yguRXp2UWaY7FngcQmD+8q7BUIVm7riN
 
212
BE45VuYBBADTEH2jHTjNCc5CMOhea6EJTrkx3upcEqB2oyhWeSWJiBGOxlcddsjo
 
213
3J3/EmBB8kK1hM9TidD3SG64x1N287lg8ELJBlKv+pQVyxohGJ1u/THgpTDMMQcL
 
214
luG5rAHQGSfyzKTiOnaTyBYg3M/nzgUOU9dKEFB0EA3tjUXFOT+r3wARAQABiJ8E
 
215
GAECAAkFAk45VuYCGwwACgkQjs6dvEpb0cRSLQP/fzCWX2lXwlwWiVF8BOPF7o9z
 
216
icHErc7/X17RGb4qj1kVf+UkRdUWJrbEVh4h6MncBIuA70WsYogiw+Kz/0LCtQAR
 
217
YUJsPy/EL++OKPH1aFasOdTxwkTka85+RdYqhP1+z/aYLFMWq6mRFI+o6x2k5mGi
 
218
7dMv2kKTJPoXUpiXJbg=
 
219
=hLYO
 
220
-----END PGP PUBLIC KEY BLOCK-----
 
221
""")
 
222
 
 
223
        expired_key = StringIO("""-----BEGIN PGP PUBLIC KEY BLOCK-----
 
224
Version: GnuPG v1.4.11 (GNU/Linux)
 
225
 
 
226
mI0ETjZ6PAEEALkR4GcFQidCCxV7pgQwQd5MZua0YO2l92fVqHX+PhnZ6egCLKdD
 
227
2bWlMUd6MLPF3FlRL7BBAxvW/DazkBOp7ljsnpMpptEzY49Uem1irYLYiVb9zK96
 
228
0sQZzFxFkfEYetQEXC68mIck8tbySOX5NAOw++3jFm3J7dsU1R3XtYzRABEBAAG0
 
229
G3Rlc3Qga2V5IDx0ZXN0QGV4YW1wbGUuY29tPoi+BBMBAgAoBQJONno8AhsDBQkA
 
230
AVGABgsJCAcDAgYVCAIJCgsEFgIDAQIeAQIXgAAKCRAc4m97T40VEz+DA/9PBphG
 
231
Yp9cHVaHSfTUKGTGgIbvRe60sFNpDCYZeAGDrygOMuI8MNzbVpwefRBFHVPx7jWd
 
232
rrYMsLkcsNUS9D0baU+0D/qp7JVg7ZSQtG0O6IG4eTZhibteY1fu0+unlXmg9NHx
 
233
5VvhwzBiJDYji00M2p/CZEMiYFUuy76CsxUpN7iNBE42ejwBBACkv2/mX7IPQg0C
 
234
A3KSrJsJv+sdvKm4b4xuI4OwagwTIVz4KlTqV4IBrVjSBfwyMXucXz0bTW85qjgA
 
235
+n67td8vyjYYZUEz1uY9lSquQQDnAN0txL3cLHZXWiWOkmzZVddQtlflK2a/J9o0
 
236
QkHPVUm+hc4l64dIzStrNl2S66fAvQARAQABiKUEGAECAA8FAk42ejwCGwwFCQAB
 
237
UYAACgkQHOJve0+NFROEYQP/epg+o8iBs31hkSERyZjrRR66LpywezWj30Rn/3mX
 
238
Fzi9HkF4xLemWOzdNt9C5PYrOep85PQg8haEjknxVjZFS0ikT1h3OWk/TF1ZrLVm
 
239
WzyX8DaHQEjKpLJJjXcAbTiZBNMk0QaVC9RvIeHpCf3n3DC49DdjsPJRMKOn8KDi
 
240
kRk=
 
241
=p0gt
 
242
-----END PGP PUBLIC KEY BLOCK-----
 
243
""")
190
244
        context.import_(key)
191
245
        context.import_(secret_key)
 
246
        context.import_(revoked_key)
 
247
        context.import_(expired_key)
192
248
 
193
 
    def test_verify_valid(self):
 
249
    def test_verify_untrusted_but_accepted(self):
 
250
        #untrusted by gpg but listed as acceptable_keys by user
194
251
        self.requireFeature(features.gpgme)
195
252
        self.import_keys()
196
253
            
221
278
        self.assertEqual((gpg.SIGNATURE_VALID, None), my_gpg.verify(content,
222
279
                            plain))
223
280
 
 
281
    def test_verify_unacceptable_key(self):
 
282
        self.requireFeature(features.gpgme)
 
283
        self.import_keys()
 
284
            
 
285
        content = """-----BEGIN PGP SIGNED MESSAGE-----
 
286
Hash: SHA1
 
287
 
 
288
bazaar-ng testament short form 1
 
289
revision-id: amy@example.com-20110527185938-hluafawphszb8dl1
 
290
sha1: 6411f9bdf6571200357140c9ce7c0f50106ac9a4
 
291
-----BEGIN PGP SIGNATURE-----
 
292
Version: GnuPG v1.4.11 (GNU/Linux)
 
293
 
 
294
iQEcBAEBAgAGBQJN+ekFAAoJEIdoGx7jCA5FGtEH/i+XxJRvqU6wdBtLVrGBMAGk
 
295
FZ5VP+KyXYtymSbgSstj/vM12NeMIeFs3xGnNnYuX1MIcY6We5TKtCH0epY6ym5+
 
296
6g2Q2QpQ5/sT2d0mWzR0K4uVngmxVQaXTdk5PdZ40O7ULeDLW6CxzxMHyUL1rsIx
 
297
7UBUTBh1O/1n3ZfD99hUkm3hVcnsN90uTKH59zV9NWwArU0cug60+5eDKJhSJDbG
 
298
rIwlqbFAjDZ7L/48e+IaYIJwBZFzMBpJKdCxzALLtauMf+KK8hGiL2hrRbWm7ty6
 
299
NgxfkMYOB4rDPdSstT35N+5uBG3n/UzjxHssi0svMfVETYYX40y57dm2eZQXFp8=
 
300
=iwsn
 
301
-----END PGP SIGNATURE-----
 
302
"""
 
303
        plain = """bazaar-ng testament short form 1
 
304
revision-id: amy@example.com-20110527185938-hluafawphszb8dl1
 
305
sha1: 6411f9bdf6571200357140c9ce7c0f50106ac9a4
 
306
"""
 
307
        my_gpg = gpg.GPGStrategy(FakeConfig())
 
308
        my_gpg.set_acceptable_keys("foo@example.com")
 
309
        self.assertEqual((gpg.SIGNATURE_KEY_MISSING, u'E3080E45'),
 
310
                         my_gpg.verify(content, plain))
 
311
 
 
312
    def test_verify_valid_but_untrusted(self):
 
313
        self.requireFeature(features.gpgme)
 
314
        self.import_keys()
 
315
            
 
316
        content = """-----BEGIN PGP SIGNED MESSAGE-----
 
317
Hash: SHA1
 
318
 
 
319
bazaar-ng testament short form 1
 
320
revision-id: amy@example.com-20110527185938-hluafawphszb8dl1
 
321
sha1: 6411f9bdf6571200357140c9ce7c0f50106ac9a4
 
322
-----BEGIN PGP SIGNATURE-----
 
323
Version: GnuPG v1.4.11 (GNU/Linux)
 
324
 
 
325
iQEcBAEBAgAGBQJN+ekFAAoJEIdoGx7jCA5FGtEH/i+XxJRvqU6wdBtLVrGBMAGk
 
326
FZ5VP+KyXYtymSbgSstj/vM12NeMIeFs3xGnNnYuX1MIcY6We5TKtCH0epY6ym5+
 
327
6g2Q2QpQ5/sT2d0mWzR0K4uVngmxVQaXTdk5PdZ40O7ULeDLW6CxzxMHyUL1rsIx
 
328
7UBUTBh1O/1n3ZfD99hUkm3hVcnsN90uTKH59zV9NWwArU0cug60+5eDKJhSJDbG
 
329
rIwlqbFAjDZ7L/48e+IaYIJwBZFzMBpJKdCxzALLtauMf+KK8hGiL2hrRbWm7ty6
 
330
NgxfkMYOB4rDPdSstT35N+5uBG3n/UzjxHssi0svMfVETYYX40y57dm2eZQXFp8=
 
331
=iwsn
 
332
-----END PGP SIGNATURE-----
 
333
"""
 
334
        plain = """bazaar-ng testament short form 1
 
335
revision-id: amy@example.com-20110527185938-hluafawphszb8dl1
 
336
sha1: 6411f9bdf6571200357140c9ce7c0f50106ac9a4
 
337
"""
 
338
        my_gpg = gpg.GPGStrategy(FakeConfig())
 
339
        self.assertEqual((gpg.SIGNATURE_NOT_VALID, None), my_gpg.verify(content,
 
340
                            plain))
 
341
 
224
342
    def test_verify_bad_testament(self):
225
343
        self.requireFeature(features.gpgme)
226
344
        self.import_keys()
252
370
        self.assertEqual((gpg.SIGNATURE_NOT_VALID, None), my_gpg.verify(content,
253
371
                            plain))
254
372
 
 
373
 
 
374
    def test_verify_revoked_signature(self):
 
375
        self.requireFeature(features.gpgme)
 
376
        self.import_keys()
 
377
            
 
378
        content = """-----BEGIN PGP SIGNED MESSAGE-----
 
379
Hash: SHA1
 
380
 
 
381
asdf
 
382
-----BEGIN PGP SIGNATURE-----
 
383
Version: GnuPG v1.4.11 (GNU/Linux)
 
384
 
 
385
iJwEAQECAAYFAk45V18ACgkQjs6dvEpb0cSIZQP/eOGTXGPlrNwvDkcX2d8O///I
 
386
ecB4sUIUEpv1XAk1MkNu58lsjjK72lRaLusEGqd7HwrFmpxVeVs0oWLg23PNPCFs
 
387
yJBID9ma+VxFVPtkEFnrc1R72sBJLfBcTxMkwVTC8eeznjdtn+cg+aLkxbPdrGnr
 
388
JFA6kUIJU2w9LU/b88Y=
 
389
=UuRX
 
390
-----END PGP SIGNATURE-----
 
391
"""
 
392
        plain = """asdf\n"""
 
393
        my_gpg = gpg.GPGStrategy(FakeConfig())
 
394
        my_gpg.set_acceptable_keys("test@example.com")
 
395
        self.assertEqual((gpg.SIGNATURE_NOT_VALID, None), my_gpg.verify(content,
 
396
                            plain))
 
397
 
255
398
    def test_verify_invalid(self):
256
399
        self.requireFeature(features.gpgme)
 
400
        self.import_keys()
257
401
        content = """-----BEGIN PGP SIGNED MESSAGE-----
258
402
Hash: SHA1
259
403
 
276
420
        self.assertEqual((gpg.SIGNATURE_NOT_VALID, None),
277
421
                            my_gpg.verify(content, plain))
278
422
 
 
423
    def test_verify_expired_but_valid(self):
 
424
        self.requireFeature(features.gpgme)
 
425
        self.import_keys()
 
426
        content = """-----BEGIN PGP SIGNED MESSAGE-----
 
427
Hash: SHA1
 
428
 
 
429
bazaar-ng testament short form 1
 
430
revision-id: test@example.com-20110801100657-f1dr1nompeex723z
 
431
sha1: 59ab434be4c2d5d646dee84f514aa09e1b72feeb
 
432
-----BEGIN PGP SIGNATURE-----
 
433
Version: GnuPG v1.4.10 (GNU/Linux)
 
434
 
 
435
iJwEAQECAAYFAk42esUACgkQHOJve0+NFRPc5wP7BoZkzBU8JaHMLv/LmqLr0sUz
 
436
zuE51ofZZ19L7KVtQWsOi4jFy0fi4A5TFwO8u9SOfoREGvkw292Uty9subSouK5/
 
437
mFmDOYPQ+O83zWgYZsBmMJWYDZ+X9I6XXZSbPtV/7XyTjaxtl5uRnDVJjg+AzKvD
 
438
dTp8VatVVrwuvzOPDVc=
 
439
=uHen
 
440
-----END PGP SIGNATURE-----
 
441
"""
 
442
        plain = """bazaar-ng testament short form 1
 
443
revision-id: test@example.com-20110801100657-f1dr1nompeex723z
 
444
sha1: 59ab434be4c2d5d646dee84f514aa09e1b72feeb
 
445
"""
 
446
        my_gpg = gpg.GPGStrategy(FakeConfig())
 
447
        self.assertEqual((gpg.SIGNATURE_EXPIRED, u'4F8D1513'),
 
448
                            my_gpg.verify(content, plain))
 
449
 
 
450
    def test_verify_unknown_key(self):
 
451
        self.requireFeature(features.gpgme)
 
452
        self.import_keys()
 
453
        content = """-----BEGIN PGP SIGNED MESSAGE-----
 
454
Hash: SHA1
 
455
 
 
456
asdf
 
457
-----BEGIN PGP SIGNATURE-----
 
458
Version: GnuPG v1.4.11 (GNU/Linux)
 
459
 
 
460
iQEcBAEBAgAGBQJOORKwAAoJENf6AkFdUeVvJDYH/1Cz+AJn1Jvy5n64o+0fZ5Ow
 
461
Y7UQb4QQTIOV7jI7n4hv/yBzuHrtImFzYvQl/o2Ezzi8B8L5gZtQy+xCUF+Q8iWs
 
462
gytZ5JUtSze7hDZo1NUl4etjoRGYqRfrUcvE2LkVH2dFbDGyyQfVmoeSHa5akuuP
 
463
QZmyg2F983rACVIpGvsqTH6RcBdvE9vx68lugeKQA8ArDn39/74FBFipFzrXSPij
 
464
eKFpl+yZmIb3g6HkPIC8o4j/tMvc37xF1OG5sBu8FT0+FC+VgY7vAblneDftAbyP
 
465
sIODx4WcfJtjLG/qkRYqJ4gDHo0eMpTJSk2CWebajdm4b+JBrM1F9mgKuZFLruE=
 
466
=RNR5
 
467
-----END PGP SIGNATURE-----
 
468
"""
 
469
        plain = "asdf\n"
 
470
        my_gpg = gpg.GPGStrategy(FakeConfig())
 
471
        self.assertEqual((gpg.SIGNATURE_KEY_MISSING, u'5D51E56F'),
 
472
                            my_gpg.verify(content, plain))
 
473
 
279
474
    def test_set_acceptable_keys(self):
280
475
        self.requireFeature(features.gpgme)
281
476
        self.import_keys()
287
482
    def test_set_acceptable_keys_unknown(self):
288
483
        self.requireFeature(features.gpgme)
289
484
        my_gpg = gpg.GPGStrategy(FakeConfig())
 
485
        self.notes = []
 
486
        def note(*args):
 
487
            self.notes.append(args[0] % args[1:])
 
488
        self.overrideAttr(trace, 'note', note)
290
489
        my_gpg.set_acceptable_keys("unknown")
291
490
        self.assertEqual(my_gpg.acceptable_keys, [])
 
491
        self.assertEqual(self.notes,
 
492
            ['No GnuPG key results for pattern: unknown'])
292
493
 
293
494
 
294
495
class TestDisabled(TestCase):