~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_gpg.py

  • Committer: Martin Packman
  • Date: 2012-01-05 09:50:04 UTC
  • mfrom: (6424 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6426.
  • Revision ID: martin.packman@canonical.com-20120105095004-mia9xb7y0efmto0v
Merge bzr.dev to resolve conflicts in bzrlib.builtins

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
27
 
 
28
 
class FakeConfig(object):
29
 
 
30
 
    def gpg_signing_key(self):
31
 
        return "amy@example.com"
32
 
 
33
 
    def gpg_signing_command(self):
34
 
        return "false"
35
 
 
36
 
    def acceptable_keys(self):
37
 
        return None
38
 
 
39
 
 
40
 
class TestCommandLine(TestCase):
 
23
from bzrlib import (
 
24
    config,
 
25
    errors,
 
26
    gpg,
 
27
    tests,
 
28
    trace,
 
29
    ui,
 
30
    )
 
31
from bzrlib.tests import (
 
32
    TestCase,
 
33
    features,
 
34
    )
 
35
 
 
36
 
 
37
class FakeConfig(config.MemoryStack):
 
38
 
 
39
    def __init__(self, content=None):
 
40
        if content is None:
 
41
            content = '''
 
42
gpg_signing_key=amy@example.com
 
43
gpg_signing_command=false'''
 
44
        super(FakeConfig, self).__init__(content)
 
45
 
 
46
 
 
47
class TestCommandLine(tests.TestCase):
 
48
 
 
49
    def setUp(self):
 
50
        super(TestCommandLine, self).setUp()
 
51
        self.my_gpg = gpg.GPGStrategy(FakeConfig())
41
52
 
42
53
    def test_signing_command_line(self):
43
 
        my_gpg = gpg.GPGStrategy(FakeConfig())
 
54
        self.assertEqual(['false',  '--clearsign', '-u', 'amy@example.com'],
 
55
                         self.my_gpg._command_line())
 
56
 
 
57
    def test_signing_command_line_from_default(self):
 
58
        # Using 'default' for gpg_signing_key will use the mail part of 'email'
 
59
        my_gpg = gpg.GPGStrategy(FakeConfig('''
 
60
email=Amy <amy@example.com>
 
61
gpg_signing_key=default
 
62
gpg_signing_command=false'''))
 
63
        self.assertEqual(['false',  '--clearsign', '-u', 'amy@example.com'],
 
64
                         my_gpg._command_line())
 
65
 
 
66
    def test_signing_command_line_from_email(self):
 
67
        # Not setting gpg_signing_key will use the mail part of 'email'
 
68
        my_gpg = gpg.GPGStrategy(FakeConfig('''
 
69
email=Amy <amy@example.com>
 
70
gpg_signing_command=false'''))
44
71
        self.assertEqual(['false',  '--clearsign', '-u', 'amy@example.com'],
45
72
                         my_gpg._command_line())
46
73
 
47
74
    def test_checks_return_code(self):
48
75
        # This test needs a unix like platform - one with 'false' to run.
49
76
        # if you have one, please make this work :)
50
 
        my_gpg = gpg.GPGStrategy(FakeConfig())
51
 
        self.assertRaises(errors.SigningFailed, my_gpg.sign, 'content')
 
77
        self.assertRaises(errors.SigningFailed, self.my_gpg.sign, 'content')
52
78
 
53
79
    def assertProduces(self, content):
54
80
        # This needs a 'cat' command or similar to work.
55
 
        my_gpg = gpg.GPGStrategy(FakeConfig())
56
81
        if sys.platform == 'win32':
57
82
            # Windows doesn't come with cat, and we don't require it
58
83
            # so lets try using python instead.
59
84
            # But stupid windows and line-ending conversions.
60
85
            # It is too much work to make sys.stdout be in binary mode.
61
86
            # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65443
62
 
            my_gpg._command_line = lambda:[sys.executable, '-c',
 
87
            self.my_gpg._command_line = lambda:[sys.executable, '-c',
63
88
                    'import sys; sys.stdout.write(sys.stdin.read())']
64
89
            new_content = content.replace('\n', '\r\n')
65
90
 
66
 
            self.assertEqual(new_content, my_gpg.sign(content))
 
91
            self.assertEqual(new_content, self.my_gpg.sign(content))
67
92
        else:
68
 
            my_gpg._command_line = lambda:['cat', '-']
69
 
            self.assertEqual(content, my_gpg.sign(content))
 
93
            self.my_gpg._command_line = lambda:['cat', '-']
 
94
            self.assertEqual(content, self.my_gpg.sign(content))
70
95
 
71
96
    def test_returns_output(self):
72
97
        content = "some content\nwith newlines\n"
91
116
        self.assertRaises(errors.BzrBadParameterUnicode,
92
117
                          self.assertProduces, u'foo')
93
118
 
 
119
 
94
120
class TestVerify(TestCase):
95
121
 
96
122
    def import_keys(self):
244
270
        #untrusted by gpg but listed as acceptable_keys by user
245
271
        self.requireFeature(features.gpgme)
246
272
        self.import_keys()
247
 
            
 
273
 
248
274
        content = """-----BEGIN PGP SIGNED MESSAGE-----
249
275
Hash: SHA1
250
276
 
275
301
    def test_verify_unacceptable_key(self):
276
302
        self.requireFeature(features.gpgme)
277
303
        self.import_keys()
278
 
            
 
304
 
279
305
        content = """-----BEGIN PGP SIGNED MESSAGE-----
280
306
Hash: SHA1
281
307
 
306
332
    def test_verify_valid_but_untrusted(self):
307
333
        self.requireFeature(features.gpgme)
308
334
        self.import_keys()
309
 
            
 
335
 
310
336
        content = """-----BEGIN PGP SIGNED MESSAGE-----
311
337
Hash: SHA1
312
338
 
336
362
    def test_verify_bad_testament(self):
337
363
        self.requireFeature(features.gpgme)
338
364
        self.import_keys()
339
 
            
 
365
 
340
366
        content = """-----BEGIN PGP SIGNED MESSAGE-----
341
367
Hash: SHA1
342
368
 
368
394
    def test_verify_revoked_signature(self):
369
395
        self.requireFeature(features.gpgme)
370
396
        self.import_keys()
371
 
            
 
397
 
372
398
        content = """-----BEGIN PGP SIGNED MESSAGE-----
373
399
Hash: SHA1
374
400
 
391
417
 
392
418
    def test_verify_invalid(self):
393
419
        self.requireFeature(features.gpgme)
 
420
        self.import_keys()
394
421
        content = """-----BEGIN PGP SIGNED MESSAGE-----
395
422
Hash: SHA1
396
423
 
415
442
 
416
443
    def test_verify_expired_but_valid(self):
417
444
        self.requireFeature(features.gpgme)
 
445
        self.import_keys()
418
446
        content = """-----BEGIN PGP SIGNED MESSAGE-----
419
447
Hash: SHA1
420
448
 
441
469
 
442
470
    def test_verify_unknown_key(self):
443
471
        self.requireFeature(features.gpgme)
 
472
        self.import_keys()
444
473
        content = """-----BEGIN PGP SIGNED MESSAGE-----
445
474
Hash: SHA1
446
475
 
473
502
    def test_set_acceptable_keys_unknown(self):
474
503
        self.requireFeature(features.gpgme)
475
504
        my_gpg = gpg.GPGStrategy(FakeConfig())
 
505
        self.notes = []
 
506
        def note(*args):
 
507
            self.notes.append(args[0] % args[1:])
 
508
        self.overrideAttr(trace, 'note', note)
476
509
        my_gpg.set_acceptable_keys("unknown")
477
510
        self.assertEqual(my_gpg.acceptable_keys, [])
 
511
        self.assertEqual(self.notes,
 
512
            ['No GnuPG key results for pattern: unknown'])
478
513
 
479
514
 
480
515
class TestDisabled(TestCase):