~bzr-pqm/bzr/bzr.dev

2052.3.2 by John Arbash Meinel
Change Copyright .. by Canonical to Copyright ... Canonical
1
# Copyright (C) 2005 Canonical Ltd
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
2
# -*- coding: utf-8 -*-
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
3
#
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
8
#
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
13
#
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
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
4183.7.1 by Sabin Iacob
update FSF mailing address
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
17
1185.79.3 by John Arbash Meinel
cleanups from Robert
18
"""Black-box tests for bzr sign-my-commits."""
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
19
4985.1.5 by Vincent Ladeuil
Deploying the new overrideAttr facility further reduces the complexity
20
from bzrlib import (
21
    gpg,
22
    tests,
23
    )
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
24
25
4985.1.5 by Vincent Ladeuil
Deploying the new overrideAttr facility further reduces the complexity
26
class SignMyCommits(tests.TestCaseWithTransport):
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
27
28
    def monkey_patch_gpg(self):
29
        """Monkey patch the gpg signing strategy to be a loopback.
30
31
        This also registers the cleanup, so that we will revert to
32
        the original gpg strategy when done.
33
        """
34
        # monkey patch gpg signing mechanism
4985.1.5 by Vincent Ladeuil
Deploying the new overrideAttr facility further reduces the complexity
35
        self.overrideAttr(gpg, 'GPGStrategy', gpg.LoopbackGPGStrategy)
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
36
1185.79.3 by John Arbash Meinel
cleanups from Robert
37
    def setup_tree(self, location='.'):
38
        wt = self.make_branch_and_tree(location)
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
39
        wt.commit("base A", allow_pointless=True, rev_id='A')
40
        wt.commit("base B", allow_pointless=True, rev_id='B')
41
        wt.commit("base C", allow_pointless=True, rev_id='C')
42
        wt.commit("base D", allow_pointless=True, rev_id='D',
43
                committer='Alternate <alt@foo.com>')
5972.3.23 by Jelmer Vernooij
Fix handling of ghosts in sign_my_commits.
44
        wt.add_parent_tree_id("aghost")
45
        wt.commit("base E", allow_pointless=True, rev_id='E')
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
46
        return wt
47
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
48
    def assertUnsigned(self, repo, revision_id):
49
        """Assert that revision_id is not signed in repo."""
50
        self.assertFalse(repo.has_signature_for_revision_id(revision_id))
51
52
    def assertSigned(self, repo, revision_id):
53
        """Assert that revision_id is signed in repo."""
54
        self.assertTrue(repo.has_signature_for_revision_id(revision_id))
55
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
56
    def test_sign_my_commits(self):
57
        #Test re signing of data.
58
        wt = self.setup_tree()
59
        repo = wt.branch.repository
60
61
        self.monkey_patch_gpg()
62
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
63
        self.assertUnsigned(repo, 'A')
64
        self.assertUnsigned(repo, 'B')
65
        self.assertUnsigned(repo, 'C')
66
        self.assertUnsigned(repo, 'D')
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
67
68
        self.run_bzr('sign-my-commits')
69
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
70
        self.assertSigned(repo, 'A')
71
        self.assertSigned(repo, 'B')
72
        self.assertSigned(repo, 'C')
73
        self.assertUnsigned(repo, 'D')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
74
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
75
    def test_sign_my_commits_location(self):
1185.79.3 by John Arbash Meinel
cleanups from Robert
76
        wt = self.setup_tree('other')
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
77
        repo = wt.branch.repository
78
79
        self.monkey_patch_gpg()
80
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
81
        self.run_bzr('sign-my-commits other')
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
82
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
83
        self.assertSigned(repo, 'A')
84
        self.assertSigned(repo, 'B')
85
        self.assertSigned(repo, 'C')
86
        self.assertUnsigned(repo, 'D')
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
87
88
    def test_sign_diff_committer(self):
89
        wt = self.setup_tree()
90
        repo = wt.branch.repository
91
92
        self.monkey_patch_gpg()
93
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
94
        self.run_bzr(['sign-my-commits', '.', 'Alternate <alt@foo.com>'])
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
95
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
96
        self.assertUnsigned(repo, 'A')
97
        self.assertUnsigned(repo, 'B')
98
        self.assertUnsigned(repo, 'C')
99
        self.assertSigned(repo, 'D')
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
100
101
    def test_sign_dry_run(self):
102
        wt = self.setup_tree()
103
        repo = wt.branch.repository
104
105
        self.monkey_patch_gpg()
106
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
107
        out = self.run_bzr('sign-my-commits --dry-run')[0]
1185.78.6 by John Arbash Meinel
Adding sign-my-commits as a builtin, along with some simple tests.
108
5972.3.6 by Jelmer Vernooij
Fix sign_my_commits test.
109
        outlines = out.splitlines()
5972.3.23 by Jelmer Vernooij
Fix handling of ghosts in sign_my_commits.
110
        self.assertEquals(5, len(outlines))
111
        self.assertEquals('Signed 4 revisions', outlines[-1])
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
112
        self.assertUnsigned(repo, 'A')
113
        self.assertUnsigned(repo, 'B')
114
        self.assertUnsigned(repo, 'C')
115
        self.assertUnsigned(repo, 'D')
5972.3.23 by Jelmer Vernooij
Fix handling of ghosts in sign_my_commits.
116
        self.assertUnsigned(repo, 'E')
5971.1.9 by Jonathan Riddell
add some tests
117
118
    def test_verify_commits(self):
119
        wt = self.setup_tree()
120
        self.monkey_patch_gpg()
121
        self.run_bzr('sign-my-commits')
5971.1.68 by Jonathan Riddell
rename command in tests
122
        out = self.run_bzr('verify-signatures', retcode=1)
5971.1.63 by Jonathan Riddell
use self.outf.write() rather than note()
123
        self.assertEquals(('4 commits with valid signatures\n'
6043.2.11 by Jonathan Riddell
fix test_verify_commits
124
                           '0 commits with key now expired\n'
5971.1.63 by Jonathan Riddell
use self.outf.write() rather than note()
125
                           '0 commits with unknown keys\n'
126
                           '0 commits not valid\n'
127
                           '1 commit not signed\n', ''), out)
5971.1.14 by Jonathan Riddell
add test for set_acceptable_keys, accept non-trusted keys if specified as acceptable, import dummy key in tests so it works outside my machine
128
129
    def test_verify_commits_acceptable_key(self):
130
        wt = self.setup_tree()
131
        self.monkey_patch_gpg()
132
        self.run_bzr('sign-my-commits')
5971.1.68 by Jonathan Riddell
rename command in tests
133
        out = self.run_bzr(['verify-signatures', '--acceptable-keys=foo,bar'],
134
                            retcode=1)
5971.1.63 by Jonathan Riddell
use self.outf.write() rather than note()
135
        self.assertEquals(('4 commits with valid signatures\n'
6043.2.10 by Jonathan Riddell
fix test test_verify_commits_acceptable_key
136
                           '0 commits with key now expired\n'
5971.1.63 by Jonathan Riddell
use self.outf.write() rather than note()
137
                           '0 commits with unknown keys\n'
138
                           '0 commits not valid\n'
139
                           '1 commit not signed\n', ''), out)