~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/per_repository/test_signatures.py

  • Committer: John Arbash Meinel
  • Author(s): Mark Hammond
  • Date: 2008-09-09 17:02:21 UTC
  • mto: This revision was merged to the branch mainline in revision 3697.
  • Revision ID: john@arbash-meinel.com-20080909170221-svim3jw2mrz0amp3
An updated transparent icon for bzr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2011, 2012, 2016 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
 
 
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
 
 
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'))
102
 
 
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.assertEqual(
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.assertEqual(
126
 
            (gpg.SIGNATURE_VALID, None, ),
127
 
            repo.verify_revision_signature('A', strategy))
128
 
 
129
 
    def test_verify_revision_signatures(self):
130
 
        wt = self.make_branch_and_tree('.')
131
 
        wt.commit("base", allow_pointless=True, rev_id='A')
132
 
        wt.commit("second", allow_pointless=True, rev_id='B')
133
 
        strategy = gpg.LoopbackGPGStrategy(None)
134
 
        repo = wt.branch.repository
135
 
        self.addCleanup(repo.lock_write().unlock)
136
 
        repo.start_write_group()
137
 
        repo.sign_revision('A', strategy)
138
 
        repo.commit_write_group()
139
 
        self.assertEqual('-----BEGIN PSEUDO-SIGNED CONTENT-----\n' +
140
 
                         Testament.from_revision(repo,
141
 
                         'A').as_short_text() +
142
 
                         '-----END PSEUDO-SIGNED CONTENT-----\n',
143
 
                         repo.get_signature_text('A'))
144
 
        self.assertEqual(
145
 
            [('A', gpg.SIGNATURE_VALID, None),
146
 
             ('B', gpg.SIGNATURE_NOT_SIGNED, None)],
147
 
            list(repo.verify_revision_signatures(['A', 'B'], strategy)))
148
 
 
149
 
 
150
 
class TestUnsupportedSignatures(per_repository.TestCaseWithRepository):
151
 
 
152
 
    def test_sign_revision(self):
153
 
        if self.repository_format.supports_revision_signatures:
154
 
            raise tests.TestNotApplicable(
155
 
                "repository supports signing revisions")
156
 
        wt = self.make_branch_and_tree('source')
157
 
        wt.commit('A', allow_pointless=True, rev_id='A')
158
 
        repo = wt.branch.repository
159
 
        repo.lock_write()
160
 
        repo.start_write_group()
161
 
        self.assertRaises(errors.UnsupportedOperation,
162
 
            repo.sign_revision, 'A', gpg.LoopbackGPGStrategy(None))
163
 
        repo.commit_write_group()