~bzr-pqm/bzr/bzr.dev

6205.2.1 by Jelmer Vernooij
Move signature tests to bt.per_repository.
1
# Copyright (C) 2005-2011 Canonical Ltd
2
#
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.
7
#
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.
12
#
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
16
17
"""Tests for repository revision signatures."""
18
19
from bzrlib import (
20
    errors,
21
    gpg,
22
    tests,
23
    urlutils,
24
    )
25
26
from bzrlib.testament import Testament
27
from bzrlib.tests import per_repository
28
29
class TestSignatures(per_repository.TestCaseWithRepository):
30
6257.3.2 by Jelmer Vernooij
Merge bzr.dev.
31
    def setUp(self):
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")
36
6205.2.1 by Jelmer Vernooij
Move signature tests to bt.per_repository.
37
# TODO 20051003 RBC:
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.
41
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'))
56
57
    def test_store_signature(self):
58
        wt = self.make_branch_and_tree('.')
59
        branch = wt.branch
60
        branch.lock_write()
61
        try:
62
            branch.repository.start_write_group()
63
            try:
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"
70
                    "revisions")
71
            except:
72
                branch.repository.abort_write_group()
73
                raise
74
            else:
75
                branch.repository.commit_write_group()
76
        finally:
77
            branch.unlock()
78
        # A signature without a revision should not be accessible.
79
        self.assertRaises(errors.NoSuchRevision,
80
                          branch.repository.has_signature_for_revision_id,
81
                          'A')
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'))
86
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
91
        repo.lock_write()
92
        repo.start_write_group()
93
        repo.sign_revision('A', gpg.LoopbackGPGStrategy(None))
94
        repo.commit_write_group()
95
        repo.unlock()
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'))
6257.2.1 by Jelmer Vernooij
Cope with repositories that don't support revision signatures.
102
6257.3.1 by Jelmer Vernooij
Support verifying remote signatures.
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)
107
        self.assertEquals(
108
            (gpg.SIGNATURE_NOT_SIGNED, None),
109
            wt.branch.repository.verify_revision_signature('A', strategy))
110
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'))
125
        self.assertEquals(
126
            (gpg.SIGNATURE_VALID, None, ),
127
            repo.verify_revision_signature('A', strategy))
6257.3.2 by Jelmer Vernooij
Merge bzr.dev.
128
6257.2.1 by Jelmer Vernooij
Cope with repositories that don't support revision signatures.
129
130
class TestUnsupportedSignatures(per_repository.TestCaseWithRepository):
131
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
139
        repo.lock_write()
140
        repo.start_write_group()
141
        self.assertRaises(errors.UnsupportedOperation,
142
            repo.sign_revision, 'A', gpg.LoopbackGPGStrategy(None))
143
        repo.commit_write_group()