~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_gpg.py

  • Committer: Vincent Ladeuil
  • Date: 2011-12-08 09:24:06 UTC
  • mto: This revision was merged to the branch mainline in revision 6351.
  • Revision ID: v.ladeuil+lp@free.fr-20111208092406-ueqyyoftzwk22bq4
Open 2.5b5 for bug fixes

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