1
# Copyright (C) 2005-2010 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
"""Test testaments for gpg signing."""
19
# TODO: Testaments with x-bits
23
from breezy import osutils
24
from breezy.tests import TestCaseWithTransport
25
from breezy.bzr.testament import (
30
from breezy.tests.features import (
35
class TestamentSetup(TestCaseWithTransport):
38
super(TestamentSetup, self).setUp()
39
self.wt = self.make_branch_and_tree('.', format='development-subtree')
40
self.wt.set_root_id(b'TREE_ROT')
41
b = self.b = self.wt.branch
42
b.nick = "test branch"
43
self.wt.commit(message='initial null commit',
44
committer='test@user',
45
timestamp=1129025423, # 'Tue Oct 11 20:10:23 2005'
47
rev_id=b'test@user-1')
48
self.build_tree_contents([('hello', b'contents of hello file'),
50
('src/foo.c', b'int main()\n{\n}\n')])
51
self.wt.add(['hello', 'src', 'src/foo.c'],
52
[b'hello-id', b'src-id', b'foo.c-id'])
53
tt = self.wt.get_transform()
54
trans_id = tt.trans_id_tree_path('hello')
55
tt.set_executability(True, trans_id)
57
self.wt.commit(message='add files and directories',
60
rev_id=b'test@user-2',
61
committer='test@user')
64
class TestamentTests(TestamentSetup):
66
def testament_class(self):
69
def expected(self, key):
70
return texts[self.testament_class()][key]
72
def from_revision(self, repository, revision_id):
73
return self.testament_class().from_revision(repository, revision_id)
75
def test_null_testament(self):
76
"""Testament for a revision with no contents."""
77
t = self.from_revision(self.b.repository, b'test@user-1')
80
ass(isinstance(t, Testament))
81
eq(t.revision_id, b'test@user-1')
82
eq(t.committer, 'test@user')
83
eq(t.timestamp, 1129025423)
86
def test_testment_text_form(self):
87
"""Conversion of testament to canonical text form."""
88
t = self.from_revision(self.b.repository, b'test@user-1')
89
text_form = t.as_text()
90
self.log('testament text form:\n%s' % text_form)
91
self.assertEqualDiff(text_form, self.expected('rev_1'))
92
short_text_form = t.as_short_text()
93
self.assertEqualDiff(short_text_form, self.expected('rev_1_short'))
95
def test_testament_with_contents(self):
96
"""Testament containing a file and a directory."""
97
t = self.from_revision(self.b.repository, b'test@user-2')
98
text_form = t.as_text()
99
self.log('testament text form:\n%s' % text_form)
100
self.assertEqualDiff(text_form, self.expected('rev_2'))
101
actual_short = t.as_short_text()
102
self.assertEqualDiff(actual_short, self.expected('rev_2_short'))
104
def test_testament_symlinks(self):
105
"""Testament containing symlink (where possible)"""
106
self.requireFeature(SymlinkFeature)
107
os.symlink('wibble/linktarget', 'link')
108
self.wt.add(['link'], [b'link-id'])
109
self.wt.commit(message='add symlink',
110
timestamp=1129025493,
112
rev_id=b'test@user-3',
113
committer='test@user')
114
t = self.from_revision(self.b.repository, b'test@user-3')
115
self.assertEqualDiff(t.as_text(), self.expected('rev_3'))
117
def test_testament_revprops(self):
118
"""Testament to revision with extra properties"""
119
props = {u'flavor': 'sour cherry\ncream cheese',
123
self.wt.commit(message='revision with properties',
124
timestamp=1129025493,
126
rev_id=b'test@user-3',
127
committer='test@user',
129
t = self.from_revision(self.b.repository, b'test@user-3')
130
self.assertEqualDiff(t.as_text(), self.expected('rev_props'))
132
def test_testament_unicode_commit_message(self):
134
message=u'non-ascii commit \N{COPYRIGHT SIGN} me',
135
timestamp=1129025493,
137
rev_id=b'test@user-3',
138
committer=u'Erik B\xe5gfors <test@user>',
139
revprops={u'uni': u'\xb5'}
141
t = self.from_revision(self.b.repository, b'test@user-3')
142
self.assertEqualDiff(
143
self.expected('sample_unicode').encode('utf-8'), t.as_text())
145
def test_from_tree(self):
146
tree = self.b.repository.revision_tree(b'test@user-2')
147
testament = self.testament_class().from_revision_tree(tree)
148
text_1 = testament.as_short_text()
149
text_2 = self.from_revision(self.b.repository,
150
b'test@user-2').as_short_text()
151
self.assertEqual(text_1, text_2)
153
def test___init__(self):
154
revision = self.b.repository.get_revision(b'test@user-2')
155
tree = self.b.repository.revision_tree(b'test@user-2')
156
testament_1 = self.testament_class()(revision, tree)
157
text_1 = testament_1.as_short_text()
158
text_2 = self.from_revision(self.b.repository,
159
b'test@user-2').as_short_text()
160
self.assertEqual(text_1, text_2)
163
class TestamentTestsStrict(TestamentTests):
165
def testament_class(self):
166
return StrictTestament
169
class TestamentTestsStrict2(TestamentTests):
171
def testament_class(self):
172
return StrictTestament3
175
REV_1_TESTAMENT = b"""\
176
bazaar-ng testament version 1
177
revision-id: test@user-1
179
timestamp: 1129025423
191
REV_1_STRICT_TESTAMENT = b"""\
192
bazaar-ng testament version 2.1
193
revision-id: test@user-1
195
timestamp: 1129025423
207
REV_1_STRICT_TESTAMENT3 = b"""\
208
bazaar testament version 3 strict
209
revision-id: test@user-1
211
timestamp: 1129025423
217
directory . TREE_ROT test@user-1 no
225
bazaar-ng testament short form 1
226
revision-id: test@user-1
228
""" % osutils.sha_string(REV_1_TESTAMENT)
231
REV_1_SHORT_STRICT = b"""\
232
bazaar-ng testament short form 2.1
233
revision-id: test@user-1
235
""" % osutils.sha_string(REV_1_STRICT_TESTAMENT)
238
REV_1_SHORT_STRICT3 = b"""\
239
bazaar testament short form 3 strict
240
revision-id: test@user-1
242
""" % osutils.sha_string(REV_1_STRICT_TESTAMENT3)
245
REV_2_TESTAMENT = b"""\
246
bazaar-ng testament version 1
247
revision-id: test@user-2
249
timestamp: 1129025483
254
add files and directories
256
file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73
258
file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24
265
REV_2_STRICT_TESTAMENT = b"""\
266
bazaar-ng testament version 2.1
267
revision-id: test@user-2
269
timestamp: 1129025483
274
add files and directories
276
file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73 test@user-2 yes
277
directory src src-id test@user-2 no
278
file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24 test@user-2 no
285
REV_2_STRICT_TESTAMENT3 = b"""\
286
bazaar testament version 3 strict
287
revision-id: test@user-2
289
timestamp: 1129025483
294
add files and directories
296
directory . TREE_ROT test@user-1 no
297
file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73 test@user-2 yes
298
directory src src-id test@user-2 no
299
file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24 test@user-2 no
307
bazaar-ng testament short form 1
308
revision-id: test@user-2
310
""" % osutils.sha_string(REV_2_TESTAMENT)
313
REV_2_SHORT_STRICT = b"""\
314
bazaar-ng testament short form 2.1
315
revision-id: test@user-2
317
""" % osutils.sha_string(REV_2_STRICT_TESTAMENT)
320
REV_2_SHORT_STRICT3 = b"""\
321
bazaar testament short form 3 strict
322
revision-id: test@user-2
324
""" % osutils.sha_string(REV_2_STRICT_TESTAMENT3)
327
REV_PROPS_TESTAMENT = b"""\
328
bazaar-ng testament version 1
329
revision-id: test@user-3
331
timestamp: 1129025493
336
revision with properties
338
file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73
340
file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24
353
REV_PROPS_TESTAMENT_STRICT = b"""\
354
bazaar-ng testament version 2.1
355
revision-id: test@user-3
357
timestamp: 1129025493
362
revision with properties
364
file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73 test@user-2 yes
365
directory src src-id test@user-2 no
366
file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24 test@user-2 no
379
REV_PROPS_TESTAMENT_STRICT3 = b"""\
380
bazaar testament version 3 strict
381
revision-id: test@user-3
383
timestamp: 1129025493
388
revision with properties
390
directory . TREE_ROT test@user-1 no
391
file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73 test@user-2 yes
392
directory src src-id test@user-2 no
393
file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24 test@user-2 no
406
REV_3_TESTAMENT = b"""\
407
bazaar-ng testament version 1
408
revision-id: test@user-3
410
timestamp: 1129025493
417
file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73
418
symlink link link-id wibble/linktarget
420
file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24
427
REV_3_TESTAMENT_STRICT = b"""\
428
bazaar-ng testament version 2.1
429
revision-id: test@user-3
431
timestamp: 1129025493
438
file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73 test@user-2 yes
439
symlink link link-id wibble/linktarget test@user-3 no
440
directory src src-id test@user-2 no
441
file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24 test@user-2 no
448
REV_3_TESTAMENT_STRICT3 = b"""\
449
bazaar testament version 3 strict
450
revision-id: test@user-3
452
timestamp: 1129025493
459
directory . TREE_ROT test@user-1 no
460
file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73 test@user-2 yes
461
symlink link link-id wibble/linktarget test@user-3 no
462
directory src src-id test@user-2 no
463
file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24 test@user-2 no
470
SAMPLE_UNICODE_TESTAMENT = u"""\
471
bazaar-ng testament version 1
472
revision-id: test@user-3
473
committer: Erik B\xe5gfors <test@user>
474
timestamp: 1129025493
479
non-ascii commit \N{COPYRIGHT SIGN} me
481
file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73
483
file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24
492
SAMPLE_UNICODE_TESTAMENT_STRICT = u"""\
493
bazaar-ng testament version 2.1
494
revision-id: test@user-3
495
committer: Erik B\xe5gfors <test@user>
496
timestamp: 1129025493
501
non-ascii commit \N{COPYRIGHT SIGN} me
503
file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73 test@user-2 yes
504
directory src src-id test@user-2 no
505
file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24 test@user-2 no
514
SAMPLE_UNICODE_TESTAMENT_STRICT3 = u"""\
515
bazaar testament version 3 strict
516
revision-id: test@user-3
517
committer: Erik B\xe5gfors <test@user>
518
timestamp: 1129025493
523
non-ascii commit \N{COPYRIGHT SIGN} me
525
directory . TREE_ROT test@user-1 no
526
file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73 test@user-2 yes
527
directory src src-id test@user-2 no
528
file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24 test@user-2 no
538
Testament: {'rev_1': REV_1_TESTAMENT,
539
'rev_1_short': REV_1_SHORT,
540
'rev_2': REV_2_TESTAMENT,
541
'rev_2_short': REV_2_SHORT,
542
'rev_3': REV_3_TESTAMENT,
543
'rev_props': REV_PROPS_TESTAMENT,
544
'sample_unicode': SAMPLE_UNICODE_TESTAMENT,
546
StrictTestament: {'rev_1': REV_1_STRICT_TESTAMENT,
547
'rev_1_short': REV_1_SHORT_STRICT,
548
'rev_2': REV_2_STRICT_TESTAMENT,
549
'rev_2_short': REV_2_SHORT_STRICT,
550
'rev_3': REV_3_TESTAMENT_STRICT,
551
'rev_props': REV_PROPS_TESTAMENT_STRICT,
552
'sample_unicode': SAMPLE_UNICODE_TESTAMENT_STRICT,
554
StrictTestament3: {'rev_1': REV_1_STRICT_TESTAMENT3,
555
'rev_1_short': REV_1_SHORT_STRICT3,
556
'rev_2': REV_2_STRICT_TESTAMENT3,
557
'rev_2_short': REV_2_SHORT_STRICT3,
558
'rev_3': REV_3_TESTAMENT_STRICT3,
559
'rev_props': REV_PROPS_TESTAMENT_STRICT3,
560
'sample_unicode': SAMPLE_UNICODE_TESTAMENT_STRICT3,