~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_gpg.py

  • Committer: Tarmac
  • Author(s): Vincent Ladeuil
  • Date: 2017-01-30 14:42:05 UTC
  • mfrom: (6620.1.1 trunk)
  • Revision ID: tarmac-20170130144205-r8fh2xpmiuxyozpv
Merge  2.7 into trunk including fix for bug #1657238 [r=vila]

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
import sys
22
22
 
23
23
from bzrlib import (
 
24
    config,
24
25
    errors,
25
26
    gpg,
 
27
    tests,
26
28
    trace,
27
29
    ui,
28
30
    )
31
33
    features,
32
34
    )
33
35
 
34
 
class FakeConfig(object):
35
 
 
36
 
    def gpg_signing_key(self):
37
 
        return "amy@example.com"
38
 
 
39
 
    def gpg_signing_command(self):
40
 
        return "false"
41
 
 
42
 
    def acceptable_keys(self):
43
 
        return None
44
 
 
45
 
 
46
 
class TestCommandLine(TestCase):
 
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())
47
52
 
48
53
    def test_signing_command_line(self):
49
 
        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'''))
50
71
        self.assertEqual(['false',  '--clearsign', '-u', 'amy@example.com'],
51
72
                         my_gpg._command_line())
52
73
 
53
74
    def test_checks_return_code(self):
54
75
        # This test needs a unix like platform - one with 'false' to run.
55
76
        # if you have one, please make this work :)
56
 
        my_gpg = gpg.GPGStrategy(FakeConfig())
57
 
        self.assertRaises(errors.SigningFailed, my_gpg.sign, 'content')
 
77
        self.assertRaises(errors.SigningFailed, self.my_gpg.sign, 'content')
58
78
 
59
79
    def assertProduces(self, content):
60
80
        # This needs a 'cat' command or similar to work.
61
 
        my_gpg = gpg.GPGStrategy(FakeConfig())
62
81
        if sys.platform == 'win32':
63
82
            # Windows doesn't come with cat, and we don't require it
64
83
            # so lets try using python instead.
65
84
            # But stupid windows and line-ending conversions.
66
85
            # It is too much work to make sys.stdout be in binary mode.
67
86
            # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65443
68
 
            my_gpg._command_line = lambda:[sys.executable, '-c',
 
87
            self.my_gpg._command_line = lambda:[sys.executable, '-c',
69
88
                    'import sys; sys.stdout.write(sys.stdin.read())']
70
89
            new_content = content.replace('\n', '\r\n')
71
90
 
72
 
            self.assertEqual(new_content, my_gpg.sign(content))
 
91
            self.assertEqual(new_content, self.my_gpg.sign(content))
73
92
        else:
74
 
            my_gpg._command_line = lambda:['cat', '-']
75
 
            self.assertEqual(content, my_gpg.sign(content))
 
93
            self.my_gpg._command_line = lambda:['cat', '-']
 
94
            self.assertEqual(content, self.my_gpg.sign(content))
76
95
 
77
96
    def test_returns_output(self):
78
97
        content = "some content\nwith newlines\n"
97
116
        self.assertRaises(errors.BzrBadParameterUnicode,
98
117
                          self.assertProduces, u'foo')
99
118
 
 
119
 
100
120
class TestVerify(TestCase):
101
121
 
102
122
    def import_keys(self):
250
270
        #untrusted by gpg but listed as acceptable_keys by user
251
271
        self.requireFeature(features.gpgme)
252
272
        self.import_keys()
253
 
            
 
273
 
254
274
        content = """-----BEGIN PGP SIGNED MESSAGE-----
255
275
Hash: SHA1
256
276
 
281
301
    def test_verify_unacceptable_key(self):
282
302
        self.requireFeature(features.gpgme)
283
303
        self.import_keys()
284
 
            
 
304
 
285
305
        content = """-----BEGIN PGP SIGNED MESSAGE-----
286
306
Hash: SHA1
287
307
 
312
332
    def test_verify_valid_but_untrusted(self):
313
333
        self.requireFeature(features.gpgme)
314
334
        self.import_keys()
315
 
            
 
335
 
316
336
        content = """-----BEGIN PGP SIGNED MESSAGE-----
317
337
Hash: SHA1
318
338
 
342
362
    def test_verify_bad_testament(self):
343
363
        self.requireFeature(features.gpgme)
344
364
        self.import_keys()
345
 
            
 
365
 
346
366
        content = """-----BEGIN PGP SIGNED MESSAGE-----
347
367
Hash: SHA1
348
368
 
374
394
    def test_verify_revoked_signature(self):
375
395
        self.requireFeature(features.gpgme)
376
396
        self.import_keys()
377
 
            
 
397
 
378
398
        content = """-----BEGIN PGP SIGNED MESSAGE-----
379
399
Hash: SHA1
380
400
 
479
499
        self.assertEqual(my_gpg.acceptable_keys,
480
500
                         [u'B5DEED5FCB15DAE6ECEF919587681B1EE3080E45'])
481
501
 
 
502
    def test_set_acceptable_keys_from_config(self):
 
503
        self.requireFeature(features.gpgme)
 
504
        self.import_keys()
 
505
        my_gpg = gpg.GPGStrategy(FakeConfig(
 
506
                'acceptable_keys=bazaar@example.com'))
 
507
        my_gpg.set_acceptable_keys(None)
 
508
        self.assertEqual(my_gpg.acceptable_keys,
 
509
                         [u'B5DEED5FCB15DAE6ECEF919587681B1EE3080E45'])
 
510
 
482
511
    def test_set_acceptable_keys_unknown(self):
483
512
        self.requireFeature(features.gpgme)
484
513
        my_gpg = gpg.GPGStrategy(FakeConfig())