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 bzrlib import osutils
24
from bzrlib.tests import SymlinkFeature, TestCaseWithTransport
25
from bzrlib.testament import (
30
from bzrlib.transform import TreeTransform
33
class TestamentSetup(TestCaseWithTransport):
36
super(TestamentSetup, self).setUp()
37
self.wt = self.make_branch_and_tree('.', format='dirstate-with-subtree')
38
self.wt.set_root_id('TREE_ROT')
39
b = self.b = self.wt.branch
40
b.nick = "test branch"
41
self.wt.commit(message='initial null commit',
42
committer='test@user',
43
timestamp=1129025423, # 'Tue Oct 11 20:10:23 2005'
46
self.build_tree_contents([('hello', 'contents of hello file'),
48
('src/foo.c', 'int main()\n{\n}\n')])
49
self.wt.add(['hello', 'src', 'src/foo.c'],
50
['hello-id', 'src-id', 'foo.c-id'])
51
tt = TreeTransform(self.wt)
52
trans_id = tt.trans_id_tree_path('hello')
53
tt.set_executability(True, trans_id)
55
self.wt.commit(message='add files and directories',
59
committer='test@user')
62
class TestamentTests(TestamentSetup):
64
def testament_class(self):
67
def expected(self, key):
68
return texts[self.testament_class()][key]
70
def from_revision(self, repository, revision_id):
71
return self.testament_class().from_revision(repository, revision_id)
73
def test_null_testament(self):
74
"""Testament for a revision with no contents."""
75
t = self.from_revision(self.b.repository, 'test@user-1')
78
ass(isinstance(t, Testament))
79
eq(t.revision_id, 'test@user-1')
80
eq(t.committer, 'test@user')
81
eq(t.timestamp, 1129025423)
84
def test_testment_text_form(self):
85
"""Conversion of testament to canonical text form."""
86
t = self.from_revision(self.b.repository, 'test@user-1')
87
text_form = t.as_text()
88
self.log('testament text form:\n' + text_form)
89
self.assertEqualDiff(text_form, self.expected('rev_1'))
90
short_text_form = t.as_short_text()
91
self.assertEqualDiff(short_text_form, self.expected('rev_1_short'))
93
def test_testament_with_contents(self):
94
"""Testament containing a file and a directory."""
95
t = self.from_revision(self.b.repository, 'test@user-2')
96
text_form = t.as_text()
97
self.log('testament text form:\n' + text_form)
98
self.assertEqualDiff(text_form, self.expected('rev_2'))
99
actual_short = t.as_short_text()
100
self.assertEqualDiff(actual_short, self.expected('rev_2_short'))
102
def test_testament_symlinks(self):
103
"""Testament containing symlink (where possible)"""
104
self.requireFeature(SymlinkFeature)
105
os.symlink('wibble/linktarget', 'link')
106
self.wt.add(['link'], ['link-id'])
107
self.wt.commit(message='add symlink',
108
timestamp=1129025493,
110
rev_id='test@user-3',
111
committer='test@user')
112
t = self.from_revision(self.b.repository, 'test@user-3')
113
self.assertEqualDiff(t.as_text(), self.expected('rev_3'))
115
def test_testament_revprops(self):
116
"""Testament to revision with extra properties"""
117
props = dict(flavor='sour cherry\ncream cheese',
121
self.wt.commit(message='revision with properties',
122
timestamp=1129025493,
124
rev_id='test@user-3',
125
committer='test@user',
127
t = self.from_revision(self.b.repository, 'test@user-3')
128
self.assertEqualDiff(t.as_text(), self.expected('rev_props'))
130
def test_testament_unicode_commit_message(self):
132
message=u'non-ascii commit \N{COPYRIGHT SIGN} me',
133
timestamp=1129025493,
135
rev_id='test@user-3',
136
committer=u'Erik B\xe5gfors <test@user>',
137
revprops={'uni':u'\xb5'}
139
t = self.from_revision(self.b.repository, 'test@user-3')
140
self.assertEqualDiff(
141
self.expected('sample_unicode').encode('utf-8'), t.as_text())
143
def test_from_tree(self):
144
tree = self.b.repository.revision_tree('test@user-2')
145
testament = self.testament_class().from_revision_tree(tree)
146
text_1 = testament.as_short_text()
147
text_2 = self.from_revision(self.b.repository,
148
'test@user-2').as_short_text()
149
self.assertEqual(text_1, text_2)
151
def test___init__(self):
152
revision = self.b.repository.get_revision('test@user-2')
153
tree = self.b.repository.revision_tree('test@user-2')
154
testament_1 = self.testament_class()(revision, tree)
155
text_1 = testament_1.as_short_text()
156
text_2 = self.from_revision(self.b.repository,
157
'test@user-2').as_short_text()
158
self.assertEqual(text_1, text_2)
161
class TestamentTestsStrict(TestamentTests):
163
def testament_class(self):
164
return StrictTestament
167
class TestamentTestsStrict2(TestamentTests):
169
def testament_class(self):
170
return StrictTestament3
173
REV_1_TESTAMENT = """\
174
bazaar-ng testament version 1
175
revision-id: test@user-1
177
timestamp: 1129025423
189
REV_1_STRICT_TESTAMENT = """\
190
bazaar-ng testament version 2.1
191
revision-id: test@user-1
193
timestamp: 1129025423
205
REV_1_STRICT_TESTAMENT3 = """\
206
bazaar testament version 3 strict
207
revision-id: test@user-1
209
timestamp: 1129025423
215
directory . TREE_ROT test@user-1 no
223
bazaar-ng testament short form 1
224
revision-id: test@user-1
226
""" % osutils.sha(REV_1_TESTAMENT).hexdigest()
229
REV_1_SHORT_STRICT = """\
230
bazaar-ng testament short form 2.1
231
revision-id: test@user-1
233
""" % osutils.sha(REV_1_STRICT_TESTAMENT).hexdigest()
236
REV_1_SHORT_STRICT3 = """\
237
bazaar testament short form 3 strict
238
revision-id: test@user-1
240
""" % osutils.sha(REV_1_STRICT_TESTAMENT3).hexdigest()
243
REV_2_TESTAMENT = """\
244
bazaar-ng testament version 1
245
revision-id: test@user-2
247
timestamp: 1129025483
252
add files and directories
254
file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73
256
file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24
263
REV_2_STRICT_TESTAMENT = """\
264
bazaar-ng testament version 2.1
265
revision-id: test@user-2
267
timestamp: 1129025483
272
add files and directories
274
file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73 test@user-2 yes
275
directory src src-id test@user-2 no
276
file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24 test@user-2 no
283
REV_2_STRICT_TESTAMENT3 = """\
284
bazaar testament version 3 strict
285
revision-id: test@user-2
287
timestamp: 1129025483
292
add files and directories
294
directory . TREE_ROT test@user-1 no
295
file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73 test@user-2 yes
296
directory src src-id test@user-2 no
297
file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24 test@user-2 no
305
bazaar-ng testament short form 1
306
revision-id: test@user-2
308
""" % osutils.sha(REV_2_TESTAMENT).hexdigest()
311
REV_2_SHORT_STRICT = """\
312
bazaar-ng testament short form 2.1
313
revision-id: test@user-2
315
""" % osutils.sha(REV_2_STRICT_TESTAMENT).hexdigest()
318
REV_2_SHORT_STRICT3 = """\
319
bazaar testament short form 3 strict
320
revision-id: test@user-2
322
""" % osutils.sha(REV_2_STRICT_TESTAMENT3).hexdigest()
325
REV_PROPS_TESTAMENT = """\
326
bazaar-ng testament version 1
327
revision-id: test@user-3
329
timestamp: 1129025493
334
revision with properties
336
file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73
338
file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24
351
REV_PROPS_TESTAMENT_STRICT = """\
352
bazaar-ng testament version 2.1
353
revision-id: test@user-3
355
timestamp: 1129025493
360
revision with properties
362
file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73 test@user-2 yes
363
directory src src-id test@user-2 no
364
file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24 test@user-2 no
377
REV_PROPS_TESTAMENT_STRICT3 = """\
378
bazaar testament version 3 strict
379
revision-id: test@user-3
381
timestamp: 1129025493
386
revision with properties
388
directory . TREE_ROT test@user-1 no
389
file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73 test@user-2 yes
390
directory src src-id test@user-2 no
391
file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24 test@user-2 no
404
REV_3_TESTAMENT = """\
405
bazaar-ng testament version 1
406
revision-id: test@user-3
408
timestamp: 1129025493
415
file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73
416
symlink link link-id wibble/linktarget
418
file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24
425
REV_3_TESTAMENT_STRICT = """\
426
bazaar-ng testament version 2.1
427
revision-id: test@user-3
429
timestamp: 1129025493
436
file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73 test@user-2 yes
437
symlink link link-id wibble/linktarget test@user-3 no
438
directory src src-id test@user-2 no
439
file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24 test@user-2 no
446
REV_3_TESTAMENT_STRICT3 = """\
447
bazaar testament version 3 strict
448
revision-id: test@user-3
450
timestamp: 1129025493
457
directory . TREE_ROT test@user-1 no
458
file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73 test@user-2 yes
459
symlink link link-id wibble/linktarget test@user-3 no
460
directory src src-id test@user-2 no
461
file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24 test@user-2 no
468
SAMPLE_UNICODE_TESTAMENT = u"""\
469
bazaar-ng testament version 1
470
revision-id: test@user-3
471
committer: Erik B\xe5gfors <test@user>
472
timestamp: 1129025493
477
non-ascii commit \N{COPYRIGHT SIGN} me
479
file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73
481
file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24
490
SAMPLE_UNICODE_TESTAMENT_STRICT = u"""\
491
bazaar-ng testament version 2.1
492
revision-id: test@user-3
493
committer: Erik B\xe5gfors <test@user>
494
timestamp: 1129025493
499
non-ascii commit \N{COPYRIGHT SIGN} me
501
file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73 test@user-2 yes
502
directory src src-id test@user-2 no
503
file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24 test@user-2 no
512
SAMPLE_UNICODE_TESTAMENT_STRICT3 = u"""\
513
bazaar testament version 3 strict
514
revision-id: test@user-3
515
committer: Erik B\xe5gfors <test@user>
516
timestamp: 1129025493
521
non-ascii commit \N{COPYRIGHT SIGN} me
523
directory . TREE_ROT test@user-1 no
524
file hello hello-id 34dd0ac19a24bf80c4d33b5c8960196e8d8d1f73 test@user-2 yes
525
directory src src-id test@user-2 no
526
file src/foo.c foo.c-id a2a049c20f908ae31b231d98779eb63c66448f24 test@user-2 no
536
Testament: { 'rev_1': REV_1_TESTAMENT,
537
'rev_1_short': REV_1_SHORT,
538
'rev_2': REV_2_TESTAMENT,
539
'rev_2_short': REV_2_SHORT,
540
'rev_3': REV_3_TESTAMENT,
541
'rev_props': REV_PROPS_TESTAMENT,
542
'sample_unicode': SAMPLE_UNICODE_TESTAMENT,
544
StrictTestament: {'rev_1': REV_1_STRICT_TESTAMENT,
545
'rev_1_short': REV_1_SHORT_STRICT,
546
'rev_2': REV_2_STRICT_TESTAMENT,
547
'rev_2_short': REV_2_SHORT_STRICT,
548
'rev_3': REV_3_TESTAMENT_STRICT,
549
'rev_props': REV_PROPS_TESTAMENT_STRICT,
550
'sample_unicode': SAMPLE_UNICODE_TESTAMENT_STRICT,
552
StrictTestament3: {'rev_1': REV_1_STRICT_TESTAMENT3,
553
'rev_1_short': REV_1_SHORT_STRICT3,
554
'rev_2': REV_2_STRICT_TESTAMENT3,
555
'rev_2_short': REV_2_SHORT_STRICT3,
556
'rev_3': REV_3_TESTAMENT_STRICT3,
557
'rev_props': REV_PROPS_TESTAMENT_STRICT3,
558
'sample_unicode': SAMPLE_UNICODE_TESTAMENT_STRICT3,