~bzr-pqm/bzr/bzr.dev

6583.4.4 by Reagan Sanders
Split the existing test cases for verify-signatures away from those for sign-my-commits and added test cases covering the --verbose option.
1
# Copyright (C) 2005 Canonical Ltd
2
# -*- coding: utf-8 -*-
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
18
"""Black-box tests for bzr verify-signatures."""
19
20
from bzrlib import (
21
    gpg,
22
    tests,
23
    )
24
from bzrlib.tests.matchers import ContainsNoVfsCalls
25
26
27
class TestVerifySignatures(tests.TestCaseWithTransport):
28
29
    def monkey_patch_gpg(self):
30
        """Monkey patch the gpg signing strategy to be a loopback.
31
32
        This also registers the cleanup, so that we will revert to
33
        the original gpg strategy when done.
34
        """
35
        # monkey patch gpg signing mechanism
36
        self.overrideAttr(gpg, 'GPGStrategy', gpg.LoopbackGPGStrategy)
37
38
    def setup_tree(self, location='.'):
39
        wt = self.make_branch_and_tree(location)
40
        wt.commit("base A", allow_pointless=True, rev_id='A')
41
        wt.commit("base B", allow_pointless=True, rev_id='B')
42
        wt.commit("base C", allow_pointless=True, rev_id='C')
43
        wt.commit("base D", allow_pointless=True, rev_id='D',
44
                committer='Alternate <alt@foo.com>')
45
        wt.add_parent_tree_id("aghost")
46
        wt.commit("base E", allow_pointless=True, rev_id='E')
47
        return wt
48
49
    def test_verify_signatures(self):
50
        wt = self.setup_tree()
51
        self.monkey_patch_gpg()
52
        self.run_bzr('sign-my-commits')
53
        out = self.run_bzr('verify-signatures', retcode=1)
54
        self.assertEquals(('4 commits with valid signatures\n'
55
                           '0 commits with key now expired\n'
56
                           '0 commits with unknown keys\n'
57
                           '0 commits not valid\n'
58
                           '1 commit not signed\n', ''), out)
59
60
    def test_verify_signatures_acceptable_key(self):
61
        wt = self.setup_tree()
62
        self.monkey_patch_gpg()
63
        self.run_bzr('sign-my-commits')
64
        out = self.run_bzr(['verify-signatures', '--acceptable-keys=foo,bar'],
65
                            retcode=1)
66
        self.assertEquals(('4 commits with valid signatures\n'
67
                           '0 commits with key now expired\n'
68
                           '0 commits with unknown keys\n'
69
                           '0 commits not valid\n'
70
                           '1 commit not signed\n', ''), out)
71
72
    def test_verify_signatures_verbose(self):
73
        wt = self.setup_tree()
74
        self.monkey_patch_gpg()
75
        self.run_bzr('sign-my-commits')
76
        out = self.run_bzr('verify-signatures --verbose', retcode=1)
77
        self.assertEquals(('4 commits with valid signatures\n'
78
                           '  None signed 4 commits\n'
79
                           '0 commits with key now expired\n'
80
                           '0 commits with unknown keys\n'
81
                           '0 commits not valid\n'
82
                           '1 commit not signed\n'
83
                           '  1 commit by author Alternate <alt@foo.com>\n', ''), out)
84
85
    def test_verify_signatures_verbose_all_valid(self):
86
        wt = self.setup_tree()
87
        self.monkey_patch_gpg()
88
        self.run_bzr('sign-my-commits')
89
        self.run_bzr(['sign-my-commits', '.', 'Alternate <alt@foo.com>'])
90
        out = self.run_bzr('verify-signatures --verbose')
91
        self.assertEquals(('All commits signed with verifiable keys\n'
92
                           '  None signed 5 commits\n', ''), out)
93
94
95
class TestSmartServerVerifySignatures(tests.TestCaseWithTransport):
96
97
    def monkey_patch_gpg(self):
98
        """Monkey patch the gpg signing strategy to be a loopback.
99
100
        This also registers the cleanup, so that we will revert to
101
        the original gpg strategy when done.
102
        """
103
        # monkey patch gpg signing mechanism
104
        self.overrideAttr(gpg, 'GPGStrategy', gpg.LoopbackGPGStrategy)
105
106
    def test_verify_signatures(self):
107
        self.setup_smart_server_with_call_log()
108
        t = self.make_branch_and_tree('branch')
109
        self.build_tree_contents([('branch/foo', 'thecontents')])
110
        t.add("foo")
111
        t.commit("message")
112
        self.monkey_patch_gpg()
113
        out, err = self.run_bzr(['sign-my-commits', self.get_url('branch')])
114
        self.reset_smart_call_log()
115
        self.run_bzr('sign-my-commits')
116
        out = self.run_bzr(['verify-signatures', self.get_url('branch')])
117
        # This figure represent the amount of work to perform this use case. It
118
        # is entirely ok to reduce this number if a test fails due to rpc_count
119
        # being too low. If rpc_count increases, more network roundtrips have
120
        # become necessary for this use case. Please do not adjust this number
121
        # upwards without agreement from bzr's network support maintainers.
122
        self.assertLength(10, self.hpss_calls)
123
        self.assertLength(1, self.hpss_connections)
124
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)