~jelmer/brz/gpg

« back to all changes in this revision

Viewing changes to breezy/tests/test_gpg.py

  • Committer: Jelmer Vernooij
  • Date: 2017-07-04 20:03:11 UTC
  • Revision ID: jelmer@jelmer.uk-20170704200311-7aj6oger5labqyoy
Sign using python-gpg rather than command-line gpg.

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
        if content is None:
44
44
            content = '''
45
45
gpg_signing_key=amy@example.com
46
 
gpg_signing_command=false'''
 
46
'''
47
47
        super(FakeConfig, self).__init__(content)
48
48
 
49
49
 
50
 
class TestCommandLine(tests.TestCase):
51
 
 
52
 
    def setUp(self):
53
 
        super(TestCommandLine, self).setUp()
54
 
        self.my_gpg = gpg.GPGStrategy(FakeConfig())
55
 
 
56
 
    def test_signing_command_line(self):
57
 
        self.assertEqual(['false',  '--clearsign', '-u', 'amy@example.com'],
58
 
                         self.my_gpg._command_line())
59
 
 
60
 
    def test_signing_command_line_from_default(self):
61
 
        # Using 'default' for gpg_signing_key will use the mail part of 'email'
62
 
        my_gpg = gpg.GPGStrategy(FakeConfig('''
63
 
email=Amy <amy@example.com>
64
 
gpg_signing_key=default
65
 
gpg_signing_command=false'''))
66
 
        self.assertEqual(['false',  '--clearsign', '-u', 'amy@example.com'],
67
 
                         my_gpg._command_line())
68
 
 
69
 
    def test_signing_command_line_from_email(self):
70
 
        # Not setting gpg_signing_key will use the mail part of 'email'
71
 
        my_gpg = gpg.GPGStrategy(FakeConfig('''
72
 
email=Amy <amy@example.com>
73
 
gpg_signing_command=false'''))
74
 
        self.assertEqual(['false',  '--clearsign', '-u', 'amy@example.com'],
75
 
                         my_gpg._command_line())
76
 
 
77
 
    def test_checks_return_code(self):
78
 
        # This test needs a unix like platform - one with 'false' to run.
79
 
        # if you have one, please make this work :)
80
 
        self.assertRaises(errors.SigningFailed, self.my_gpg.sign, 'content')
81
 
 
82
 
    def assertProduces(self, content):
83
 
        # This needs a 'cat' command or similar to work.
84
 
        if sys.platform == 'win32':
85
 
            # Windows doesn't come with cat, and we don't require it
86
 
            # so lets try using python instead.
87
 
            # But stupid windows and line-ending conversions.
88
 
            # It is too much work to make sys.stdout be in binary mode.
89
 
            # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65443
90
 
            self.my_gpg._command_line = lambda:[sys.executable, '-c',
91
 
                    'import sys; sys.stdout.write(sys.stdin.read())']
92
 
            new_content = content.replace('\n', '\r\n')
93
 
 
94
 
            self.assertEqual(new_content, self.my_gpg.sign(content))
95
 
        else:
96
 
            self.my_gpg._command_line = lambda:['cat', '-']
97
 
            self.assertEqual(content, self.my_gpg.sign(content))
98
 
 
99
 
    def test_returns_output(self):
100
 
        content = "some content\nwith newlines\n"
101
 
        self.assertProduces(content)
102
 
 
103
 
    def test_clears_progress(self):
104
 
        content = "some content\nwith newlines\n"
105
 
        old_clear_term = ui.ui_factory.clear_term
106
 
        clear_term_called = []
107
 
        def clear_term():
108
 
            old_clear_term()
109
 
            clear_term_called.append(True)
110
 
        ui.ui_factory.clear_term = clear_term
111
 
        try:
112
 
            self.assertProduces(content)
113
 
        finally:
114
 
            ui.ui_factory.clear_term = old_clear_term
115
 
        self.assertEqual([True], clear_term_called)
116
 
 
117
 
    def test_aborts_on_unicode(self):
118
 
        """You can't sign Unicode text; it must be encoded first."""
119
 
        self.assertRaises(errors.BzrBadParameterUnicode,
120
 
                          self.assertProduces, u'foo')
121
 
 
122
 
 
123
50
class TestVerify(TestCase):
124
51
 
125
52
    def import_keys(self):
526
453
class TestDisabled(TestCase):
527
454
 
528
455
    def test_sign(self):
529
 
        self.assertRaises(errors.SigningFailed,
 
456
        self.assertRaises(gpg.SigningFailed,
530
457
                          gpg.DisabledGPGStrategy(None).sign, 'content')
531
458
 
532
459
    def test_verify(self):
533
 
        self.assertRaises(errors.SignatureVerificationFailed,
 
460
        self.assertRaises(gpg.SignatureVerificationFailed,
534
461
                          gpg.DisabledGPGStrategy(None).verify, 'content',
535
462
                          'testament')