1
# Copyright (C) 2005-2011 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Tests for repository revision signatures."""
26
from bzrlib.testament import Testament
27
from bzrlib.tests import per_repository
29
class TestSignatures(per_repository.TestCaseWithRepository):
32
super(TestSignatures, self).setUp()
33
if not self.repository_format.supports_revision_signatures:
34
raise tests.TestNotApplicable(
35
"repository does not support signing revisions")
38
# compare the gpg-to-sign info for a commit with a ghost and
39
# an identical tree without a ghost
40
# fetch missing should rewrite the TOC of weaves to list newly available parents.
42
def test_sign_existing_revision(self):
43
wt = self.make_branch_and_tree('.')
44
wt.commit("base", allow_pointless=True, rev_id='A')
45
strategy = gpg.LoopbackGPGStrategy(None)
46
repo = wt.branch.repository
47
self.addCleanup(repo.lock_write().unlock)
48
repo.start_write_group()
49
repo.sign_revision('A', strategy)
50
repo.commit_write_group()
51
self.assertEqual('-----BEGIN PSEUDO-SIGNED CONTENT-----\n' +
52
Testament.from_revision(repo,
53
'A').as_short_text() +
54
'-----END PSEUDO-SIGNED CONTENT-----\n',
55
repo.get_signature_text('A'))
57
def test_store_signature(self):
58
wt = self.make_branch_and_tree('.')
62
branch.repository.start_write_group()
64
branch.repository.store_revision_signature(
65
gpg.LoopbackGPGStrategy(None), 'FOO', 'A')
66
except errors.NoSuchRevision:
67
branch.repository.abort_write_group()
68
raise tests.TestNotApplicable(
69
"repository does not support signing non-present"
72
branch.repository.abort_write_group()
75
branch.repository.commit_write_group()
78
# A signature without a revision should not be accessible.
79
self.assertRaises(errors.NoSuchRevision,
80
branch.repository.has_signature_for_revision_id,
82
wt.commit("base", allow_pointless=True, rev_id='A')
83
self.assertEqual('-----BEGIN PSEUDO-SIGNED CONTENT-----\n'
84
'FOO-----END PSEUDO-SIGNED CONTENT-----\n',
85
branch.repository.get_signature_text('A'))
87
def test_clone_preserves_signatures(self):
88
wt = self.make_branch_and_tree('source')
89
wt.commit('A', allow_pointless=True, rev_id='A')
90
repo = wt.branch.repository
92
repo.start_write_group()
93
repo.sign_revision('A', gpg.LoopbackGPGStrategy(None))
94
repo.commit_write_group()
96
#FIXME: clone should work to urls,
97
# wt.clone should work to disks.
98
self.build_tree(['target/'])
99
d2 = repo.bzrdir.clone(urlutils.local_path_to_url('target'))
100
self.assertEqual(repo.get_signature_text('A'),
101
d2.open_repository().get_signature_text('A'))
103
def test_verify_revision_signature_not_signed(self):
104
wt = self.make_branch_and_tree('.')
105
wt.commit("base", allow_pointless=True, rev_id='A')
106
strategy = gpg.LoopbackGPGStrategy(None)
108
(gpg.SIGNATURE_NOT_SIGNED, None),
109
wt.branch.repository.verify_revision_signature('A', strategy))
111
def test_verify_revision_signature(self):
112
wt = self.make_branch_and_tree('.')
113
wt.commit("base", allow_pointless=True, rev_id='A')
114
strategy = gpg.LoopbackGPGStrategy(None)
115
repo = wt.branch.repository
116
self.addCleanup(repo.lock_write().unlock)
117
repo.start_write_group()
118
repo.sign_revision('A', strategy)
119
repo.commit_write_group()
120
self.assertEqual('-----BEGIN PSEUDO-SIGNED CONTENT-----\n' +
121
Testament.from_revision(repo,
122
'A').as_short_text() +
123
'-----END PSEUDO-SIGNED CONTENT-----\n',
124
repo.get_signature_text('A'))
126
(gpg.SIGNATURE_VALID, None, ),
127
repo.verify_revision_signature('A', strategy))
130
class TestUnsupportedSignatures(per_repository.TestCaseWithRepository):
132
def test_sign_revision(self):
133
if self.repository_format.supports_revision_signatures:
134
raise tests.TestNotApplicable(
135
"repository supports signing revisions")
136
wt = self.make_branch_and_tree('source')
137
wt.commit('A', allow_pointless=True, rev_id='A')
138
repo = wt.branch.repository
140
repo.start_write_group()
141
self.assertRaises(errors.UnsupportedOperation,
142
repo.sign_revision, 'A', gpg.LoopbackGPGStrategy(None))
143
repo.commit_write_group()