Allow non-os file-like objects passed to ShaFile.from_file.
[jelmer/dulwich-libgit2.git] / dulwich / tests / test_objects.py
1 # test_objects.py -- tests for objects.py
2 # Copyright (C) 2007 James Westby <jw+debian@jameswestby.net>
3
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; version 2
7 # of the License or (at your option) any later version of 
8 # the License.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 # MA  02110-1301, USA.
19
20
21 """Tests for git base objects."""
22
23 # TODO: Round-trip parse-serialize-parse and serialize-parse-serialize tests.
24
25
26 import datetime
27 import os
28 import stat
29 import unittest
30
31 from dulwich.errors import (
32     ChecksumMismatch,
33     ObjectFormatException,
34     )
35 from dulwich.objects import (
36     Blob,
37     Tree,
38     Commit,
39     Tag,
40     format_timezone,
41     hex_to_sha,
42     sha_to_hex,
43     hex_to_filename,
44     check_hexsha,
45     check_identity,
46     parse_timezone,
47     parse_tree,
48     _parse_tree_py,
49     )
50 from dulwich.tests import (
51     TestSkipped,
52     )
53
54 a_sha = '6f670c0fb53f9463760b7295fbb814e965fb20c8'
55 b_sha = '2969be3e8ee1c0222396a5611407e4769f14e54b'
56 c_sha = '954a536f7819d40e6f637f849ee187dd10066349'
57 tree_sha = '70c190eb48fa8bbb50ddc692a17b44cb781af7f6'
58 tag_sha = '71033db03a03c6a36721efcf1968dd8f8e0cf023'
59
60
61 try:
62     from itertools import permutations
63 except ImportError:
64     # Implementation of permutations from Python 2.6 documentation:
65     # http://docs.python.org/2.6/library/itertools.html#itertools.permutations
66     # Copyright (c) 2001-2010 Python Software Foundation; All Rights Reserved
67     def permutations(iterable, r=None):
68         # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
69         # permutations(range(3)) --> 012 021 102 120 201 210
70         pool = tuple(iterable)
71         n = len(pool)
72         r = n if r is None else r
73         if r > n:
74             return
75         indices = range(n)
76         cycles = range(n, n-r, -1)
77         yield tuple(pool[i] for i in indices[:r])
78         while n:
79             for i in reversed(range(r)):
80                 cycles[i] -= 1
81                 if cycles[i] == 0:
82                     indices[i:] = indices[i+1:] + indices[i:i+1]
83                     cycles[i] = n - i
84                 else:
85                     j = cycles[i]
86                     indices[i], indices[-j] = indices[-j], indices[i]
87                     yield tuple(pool[i] for i in indices[:r])
88                     break
89             else:
90                 return
91
92
93 class TestHexToSha(unittest.TestCase):
94
95     def test_simple(self):
96         self.assertEquals("\xab\xcd" * 10, hex_to_sha("abcd" * 10))
97
98     def test_reverse(self):
99         self.assertEquals("abcd" * 10, sha_to_hex("\xab\xcd" * 10))
100
101
102 class BlobReadTests(unittest.TestCase):
103     """Test decompression of blobs"""
104
105     def get_sha_file(self, cls, base, sha):
106         dir = os.path.join(os.path.dirname(__file__), 'data', base)
107         return cls.from_path(hex_to_filename(dir, sha))
108
109     def get_blob(self, sha):
110         """Return the blob named sha from the test data dir"""
111         return self.get_sha_file(Blob, 'blobs', sha)
112   
113     def get_tree(self, sha):
114         return self.get_sha_file(Tree, 'trees', sha)
115   
116     def get_tag(self, sha):
117         return self.get_sha_file(Tag, 'tags', sha)
118   
119     def commit(self, sha):
120         return self.get_sha_file(Commit, 'commits', sha)
121   
122     def test_decompress_simple_blob(self):
123         b = self.get_blob(a_sha)
124         self.assertEqual(b.data, 'test 1\n')
125         self.assertEqual(b.sha().hexdigest(), a_sha)
126   
127     def test_hash(self):
128         b = self.get_blob(a_sha)
129         self.assertEqual(hash(b.id), hash(b))
130
131     def test_parse_empty_blob_object(self):
132         sha = 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391'
133         b = self.get_blob(sha)
134         self.assertEqual(b.data, '')
135         self.assertEqual(b.id, sha)
136         self.assertEqual(b.sha().hexdigest(), sha)
137   
138     def test_create_blob_from_string(self):
139         string = 'test 2\n'
140         b = Blob.from_string(string)
141         self.assertEqual(b.data, string)
142         self.assertEqual(b.sha().hexdigest(), b_sha)
143
144     def test_chunks(self):
145         string = 'test 5\n'
146         b = Blob.from_string(string)
147         self.assertEqual([string], b.chunked)
148
149     def test_set_chunks(self):
150         b = Blob()
151         b.chunked = ['te', 'st', ' 5\n']
152         self.assertEqual('test 5\n', b.data)
153         b.chunked = ['te', 'st', ' 6\n']
154         self.assertEqual('test 6\n', b.as_raw_string())
155   
156     def test_parse_legacy_blob(self):
157         string = 'test 3\n'
158         b = self.get_blob(c_sha)
159         self.assertEqual(b.data, string)
160         self.assertEqual(b.sha().hexdigest(), c_sha)
161   
162     def test_eq(self):
163         blob1 = self.get_blob(a_sha)
164         blob2 = self.get_blob(a_sha)
165         self.assertEqual(blob1, blob2)
166   
167     def test_read_tree_from_file(self):
168         t = self.get_tree(tree_sha)
169         self.assertEqual(t.entries()[0], (33188, 'a', a_sha))
170         self.assertEqual(t.entries()[1], (33188, 'b', b_sha))
171   
172     def test_read_tag_from_file(self):
173         t = self.get_tag(tag_sha)
174         self.assertEqual(t.object, (Commit, '51b668fd5bf7061b7d6fa525f88803e6cfadaa51'))
175         self.assertEqual(t.name,'signed')
176         self.assertEqual(t.tagger,'Ali Sabil <ali.sabil@gmail.com>')
177         self.assertEqual(t.tag_time, 1231203091)
178         self.assertEqual(t.message, 'This is a signed tag\n-----BEGIN PGP SIGNATURE-----\nVersion: GnuPG v1.4.9 (GNU/Linux)\n\niEYEABECAAYFAkliqx8ACgkQqSMmLy9u/kcx5ACfakZ9NnPl02tOyYP6pkBoEkU1\n5EcAn0UFgokaSvS371Ym/4W9iJj6vh3h\n=ql7y\n-----END PGP SIGNATURE-----\n')
179   
180     def test_read_commit_from_file(self):
181         sha = '60dacdc733de308bb77bb76ce0fb0f9b44c9769e'
182         c = self.commit(sha)
183         self.assertEqual(c.tree, tree_sha)
184         self.assertEqual(c.parents, ['0d89f20333fbb1d2f3a94da77f4981373d8f4310'])
185         self.assertEqual(c.author,
186             'James Westby <jw+debian@jameswestby.net>')
187         self.assertEqual(c.committer,
188             'James Westby <jw+debian@jameswestby.net>')
189         self.assertEqual(c.commit_time, 1174759230)
190         self.assertEqual(c.commit_timezone, 0)
191         self.assertEqual(c.author_timezone, 0)
192         self.assertEqual(c.message, 'Test commit\n')
193   
194     def test_read_commit_no_parents(self):
195         sha = '0d89f20333fbb1d2f3a94da77f4981373d8f4310'
196         c = self.commit(sha)
197         self.assertEqual(c.tree, '90182552c4a85a45ec2a835cadc3451bebdfe870')
198         self.assertEqual(c.parents, [])
199         self.assertEqual(c.author,
200             'James Westby <jw+debian@jameswestby.net>')
201         self.assertEqual(c.committer,
202             'James Westby <jw+debian@jameswestby.net>')
203         self.assertEqual(c.commit_time, 1174758034)
204         self.assertEqual(c.commit_timezone, 0)
205         self.assertEqual(c.author_timezone, 0)
206         self.assertEqual(c.message, 'Test commit\n')
207   
208     def test_read_commit_two_parents(self):
209         sha = '5dac377bdded4c9aeb8dff595f0faeebcc8498cc'
210         c = self.commit(sha)
211         self.assertEqual(c.tree, 'd80c186a03f423a81b39df39dc87fd269736ca86')
212         self.assertEqual(c.parents, ['ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd',
213                                        '4cffe90e0a41ad3f5190079d7c8f036bde29cbe6'])
214         self.assertEqual(c.author,
215             'James Westby <jw+debian@jameswestby.net>')
216         self.assertEqual(c.committer,
217             'James Westby <jw+debian@jameswestby.net>')
218         self.assertEqual(c.commit_time, 1174773719)
219         self.assertEqual(c.commit_timezone, 0)
220         self.assertEqual(c.author_timezone, 0)
221         self.assertEqual(c.message, 'Merge ../b\n')
222
223     def test_check_id(self):
224         wrong_sha = '1' * 40
225         b = self.get_blob(wrong_sha)
226         self.assertEqual(wrong_sha, b.id)
227         self.assertRaises(ChecksumMismatch, b.check)
228         self.assertEqual('742b386350576589175e374a5706505cbd17680c', b.id)
229
230
231 class ShaFileCheckTests(unittest.TestCase):
232
233     def assertCheckFails(self, cls, data):
234         obj = cls()
235         def do_check():
236             obj.set_raw_string(data)
237             obj.check()
238         self.assertRaises(ObjectFormatException, do_check)
239
240     def assertCheckSucceeds(self, cls, data):
241         obj = cls()
242         obj.set_raw_string(data)
243         self.assertEqual(None, obj.check())
244
245
246 class CommitSerializationTests(unittest.TestCase):
247
248     def make_base(self):
249         c = Commit()
250         c.tree = 'd80c186a03f423a81b39df39dc87fd269736ca86'
251         c.parents = ['ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd', '4cffe90e0a41ad3f5190079d7c8f036bde29cbe6']
252         c.author = 'James Westby <jw+debian@jameswestby.net>'
253         c.committer = 'James Westby <jw+debian@jameswestby.net>'
254         c.commit_time = 1174773719
255         c.author_time = 1174773719
256         c.commit_timezone = 0
257         c.author_timezone = 0
258         c.message =  'Merge ../b\n'
259         return c
260
261     def test_encoding(self):
262         c = self.make_base()
263         c.encoding = "iso8859-1"
264         self.assertTrue("encoding iso8859-1\n" in c.as_raw_string())        
265
266     def test_short_timestamp(self):
267         c = self.make_base()
268         c.commit_time = 30
269         c1 = Commit()
270         c1.set_raw_string(c.as_raw_string())
271         self.assertEquals(30, c1.commit_time)
272
273     def test_raw_length(self):
274         c = self.make_base()
275         self.assertEquals(len(c.as_raw_string()), c.raw_length())
276
277     def test_simple(self):
278         c = self.make_base()
279         self.assertEquals(c.id, '5dac377bdded4c9aeb8dff595f0faeebcc8498cc')
280         self.assertEquals(
281                 'tree d80c186a03f423a81b39df39dc87fd269736ca86\n'
282                 'parent ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd\n'
283                 'parent 4cffe90e0a41ad3f5190079d7c8f036bde29cbe6\n'
284                 'author James Westby <jw+debian@jameswestby.net> 1174773719 +0000\n'
285                 'committer James Westby <jw+debian@jameswestby.net> 1174773719 +0000\n'
286                 '\n'
287                 'Merge ../b\n', c.as_raw_string())
288
289     def test_timezone(self):
290         c = self.make_base()
291         c.commit_timezone = 5 * 60
292         self.assertTrue(" +0005\n" in c.as_raw_string())
293
294     def test_neg_timezone(self):
295         c = self.make_base()
296         c.commit_timezone = -1 * 3600
297         self.assertTrue(" -0100\n" in c.as_raw_string())
298
299
300 default_committer = 'James Westby <jw+debian@jameswestby.net> 1174773719 +0000'
301
302 class CommitParseTests(ShaFileCheckTests):
303
304     def make_commit_lines(self,
305                           tree='d80c186a03f423a81b39df39dc87fd269736ca86',
306                           parents=['ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd',
307                                    '4cffe90e0a41ad3f5190079d7c8f036bde29cbe6'],
308                           author=default_committer,
309                           committer=default_committer,
310                           encoding=None,
311                           message='Merge ../b\n',
312                           extra=None):
313         lines = []
314         if tree is not None:
315             lines.append('tree %s' % tree)
316         if parents is not None:
317             lines.extend('parent %s' % p for p in parents)
318         if author is not None:
319             lines.append('author %s' % author)
320         if committer is not None:
321             lines.append('committer %s' % committer)
322         if encoding is not None:
323             lines.append('encoding %s' % encoding)
324         if extra is not None:
325             for name, value in sorted(extra.iteritems()):
326                 lines.append('%s %s' % (name, value))
327         lines.append('')
328         if message is not None:
329             lines.append(message)
330         return lines
331
332     def make_commit_text(self, **kwargs):
333         return '\n'.join(self.make_commit_lines(**kwargs))
334
335     def test_simple(self):
336         c = Commit.from_string(self.make_commit_text())
337         self.assertEquals('Merge ../b\n', c.message)
338         self.assertEquals('James Westby <jw+debian@jameswestby.net>', c.author)
339         self.assertEquals('James Westby <jw+debian@jameswestby.net>',
340                           c.committer)
341         self.assertEquals('d80c186a03f423a81b39df39dc87fd269736ca86', c.tree)
342         self.assertEquals(['ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd',
343                            '4cffe90e0a41ad3f5190079d7c8f036bde29cbe6'],
344                           c.parents)
345         expected_time = datetime.datetime(2007, 3, 24, 22, 1, 59)
346         self.assertEquals(expected_time,
347                           datetime.datetime.utcfromtimestamp(c.commit_time))
348         self.assertEquals(0, c.commit_timezone)
349         self.assertEquals(expected_time,
350                           datetime.datetime.utcfromtimestamp(c.author_time))
351         self.assertEquals(0, c.author_timezone)
352         self.assertEquals(None, c.encoding)
353
354     def test_custom(self):
355         c = Commit.from_string(self.make_commit_text(
356           extra={'extra-field': 'data'}))
357         self.assertEquals([('extra-field', 'data')], c.extra)
358
359     def test_encoding(self):
360         c = Commit.from_string(self.make_commit_text(encoding='UTF-8'))
361         self.assertEquals('UTF-8', c.encoding)
362
363     def test_check(self):
364         self.assertCheckSucceeds(Commit, self.make_commit_text())
365         self.assertCheckSucceeds(Commit, self.make_commit_text(parents=None))
366         self.assertCheckSucceeds(Commit,
367                                  self.make_commit_text(encoding='UTF-8'))
368
369         self.assertCheckFails(Commit, self.make_commit_text(tree='xxx'))
370         self.assertCheckFails(Commit, self.make_commit_text(
371           parents=[a_sha, 'xxx']))
372         bad_committer = "some guy without an email address 1174773719 +0000"
373         self.assertCheckFails(Commit,
374                               self.make_commit_text(committer=bad_committer))
375         self.assertCheckFails(Commit,
376                               self.make_commit_text(author=bad_committer))
377         self.assertCheckFails(Commit, self.make_commit_text(author=None))
378         self.assertCheckFails(Commit, self.make_commit_text(committer=None))
379         self.assertCheckFails(Commit, self.make_commit_text(
380           author=None, committer=None))
381
382     def test_check_duplicates(self):
383         # duplicate each of the header fields
384         for i in xrange(5):
385             lines = self.make_commit_lines(parents=[a_sha], encoding='UTF-8')
386             lines.insert(i, lines[i])
387             text = '\n'.join(lines)
388             if lines[i].startswith('parent'):
389                 # duplicate parents are ok for now
390                 self.assertCheckSucceeds(Commit, text)
391             else:
392                 self.assertCheckFails(Commit, text)
393
394     def test_check_order(self):
395         lines = self.make_commit_lines(parents=[a_sha], encoding='UTF-8')
396         headers = lines[:5]
397         rest = lines[5:]
398         # of all possible permutations, ensure only the original succeeds
399         for perm in permutations(headers):
400             perm = list(perm)
401             text = '\n'.join(perm + rest)
402             if perm == headers:
403                 self.assertCheckSucceeds(Commit, text)
404             else:
405                 self.assertCheckFails(Commit, text)
406
407
408 class TreeTests(ShaFileCheckTests):
409
410     def test_simple(self):
411         myhexsha = "d80c186a03f423a81b39df39dc87fd269736ca86"
412         x = Tree()
413         x["myname"] = (0100755, myhexsha)
414         self.assertEquals('100755 myname\0' + hex_to_sha(myhexsha),
415                 x.as_raw_string())
416
417     def test_tree_dir_sort(self):
418         x = Tree()
419         x["a.c"] = (0100755, "d80c186a03f423a81b39df39dc87fd269736ca86")
420         x["a"] = (stat.S_IFDIR, "d80c186a03f423a81b39df39dc87fd269736ca86")
421         x["a/c"] = (stat.S_IFDIR, "d80c186a03f423a81b39df39dc87fd269736ca86")
422         self.assertEquals(["a.c", "a", "a/c"], [p[0] for p in x.iteritems()])
423
424     def _do_test_parse_tree(self, parse_tree):
425         dir = os.path.join(os.path.dirname(__file__), 'data', 'trees')
426         o = Tree.from_path(hex_to_filename(dir, tree_sha))
427         self.assertEquals([('a', 0100644, a_sha), ('b', 0100644, b_sha)],
428                           list(parse_tree(o.as_raw_string())))
429
430     def test_parse_tree(self):
431         self._do_test_parse_tree(_parse_tree_py)
432
433     def test_parse_tree_extension(self):
434         if parse_tree is _parse_tree_py:
435             raise TestSkipped('parse_tree extension not found')
436         self._do_test_parse_tree(parse_tree)
437
438     def test_check(self):
439         t = Tree
440         sha = hex_to_sha(a_sha)
441
442         # filenames
443         self.assertCheckSucceeds(t, '100644 .a\0%s' % sha)
444         self.assertCheckFails(t, '100644 \0%s' % sha)
445         self.assertCheckFails(t, '100644 .\0%s' % sha)
446         self.assertCheckFails(t, '100644 a/a\0%s' % sha)
447         self.assertCheckFails(t, '100644 ..\0%s' % sha)
448
449         # modes
450         self.assertCheckSucceeds(t, '100644 a\0%s' % sha)
451         self.assertCheckSucceeds(t, '100755 a\0%s' % sha)
452         self.assertCheckSucceeds(t, '160000 a\0%s' % sha)
453         # TODO more whitelisted modes
454         self.assertCheckFails(t, '123456 a\0%s' % sha)
455         self.assertCheckFails(t, '123abc a\0%s' % sha)
456
457         # shas
458         self.assertCheckFails(t, '100644 a\0%s' % ('x' * 5))
459         self.assertCheckFails(t, '100644 a\0%s' % ('x' * 18 + '\0'))
460         self.assertCheckFails(t, '100644 a\0%s\n100644 b\0%s' % ('x' * 21, sha))
461
462         # ordering
463         sha2 = hex_to_sha(b_sha)
464         self.assertCheckSucceeds(t, '100644 a\0%s\n100644 b\0%s' % (sha, sha))
465         self.assertCheckSucceeds(t, '100644 a\0%s\n100644 b\0%s' % (sha, sha2))
466         self.assertCheckFails(t, '100644 a\0%s\n100755 a\0%s' % (sha, sha2))
467         self.assertCheckFails(t, '100644 b\0%s\n100644 a\0%s' % (sha2, sha))
468
469     def test_iter(self):
470         t = Tree()
471         t["foo"] = (0100644, a_sha)
472         self.assertEquals(set(["foo"]), set(t))
473
474
475 class TagSerializeTests(unittest.TestCase):
476
477     def test_serialize_simple(self):
478         x = Tag()
479         x.tagger = "Jelmer Vernooij <jelmer@samba.org>"
480         x.name = "0.1"
481         x.message = "Tag 0.1"
482         x.object = (Blob, "d80c186a03f423a81b39df39dc87fd269736ca86")
483         x.tag_time = 423423423
484         x.tag_timezone = 0
485         self.assertEquals("""object d80c186a03f423a81b39df39dc87fd269736ca86
486 type blob
487 tag 0.1
488 tagger Jelmer Vernooij <jelmer@samba.org> 423423423 +0000
489
490 Tag 0.1""", x.as_raw_string())
491
492
493 default_tagger = ('Linus Torvalds <torvalds@woody.linux-foundation.org> '
494                   '1183319674 -0700')
495 default_message = """Linux 2.6.22-rc7
496 -----BEGIN PGP SIGNATURE-----
497 Version: GnuPG v1.4.7 (GNU/Linux)
498
499 iD8DBQBGiAaAF3YsRnbiHLsRAitMAKCiLboJkQECM/jpYsY3WPfvUgLXkACgg3ql
500 OK2XeQOiEeXtT76rV4t2WR4=
501 =ivrA
502 -----END PGP SIGNATURE-----
503 """
504
505
506 class TagParseTests(ShaFileCheckTests):
507     def make_tag_lines(self,
508                        object_sha="a38d6181ff27824c79fc7df825164a212eff6a3f",
509                        object_type_name="commit",
510                        name="v2.6.22-rc7",
511                        tagger=default_tagger,
512                        message=default_message):
513         lines = []
514         if object_sha is not None:
515             lines.append("object %s" % object_sha)
516         if object_type_name is not None:
517             lines.append("type %s" % object_type_name)
518         if name is not None:
519             lines.append("tag %s" % name)
520         if tagger is not None:
521             lines.append("tagger %s" % tagger)
522         lines.append("")
523         if message is not None:
524             lines.append(message)
525         return lines
526
527     def make_tag_text(self, **kwargs):
528         return "\n".join(self.make_tag_lines(**kwargs))
529
530     def test_parse(self):
531         x = Tag()
532         x.set_raw_string(self.make_tag_text())
533         self.assertEquals(
534             "Linus Torvalds <torvalds@woody.linux-foundation.org>", x.tagger)
535         self.assertEquals("v2.6.22-rc7", x.name)
536         object_type, object_sha = x.object
537         self.assertEquals("a38d6181ff27824c79fc7df825164a212eff6a3f",
538                           object_sha)
539         self.assertEquals(Commit, object_type)
540         self.assertEquals(datetime.datetime.utcfromtimestamp(x.tag_time),
541                           datetime.datetime(2007, 7, 1, 19, 54, 34))
542         self.assertEquals(-25200, x.tag_timezone)
543
544     def test_parse_no_tagger(self):
545         x = Tag()
546         x.set_raw_string(self.make_tag_text(tagger=None))
547         self.assertEquals(None, x.tagger)
548         self.assertEquals("v2.6.22-rc7", x.name)
549
550     def test_check(self):
551         self.assertCheckSucceeds(Tag, self.make_tag_text())
552         self.assertCheckFails(Tag, self.make_tag_text(object_sha=None))
553         self.assertCheckFails(Tag, self.make_tag_text(object_type_name=None))
554         self.assertCheckFails(Tag, self.make_tag_text(name=None))
555         self.assertCheckFails(Tag, self.make_tag_text(name=''))
556         self.assertCheckFails(Tag, self.make_tag_text(
557           object_type_name="foobar"))
558         self.assertCheckFails(Tag, self.make_tag_text(
559           tagger="some guy without an email address 1183319674 -0700"))
560         self.assertCheckFails(Tag, self.make_tag_text(
561           tagger=("Linus Torvalds <torvalds@woody.linux-foundation.org> "
562                   "Sun 7 Jul 2007 12:54:34 +0700")))
563         self.assertCheckFails(Tag, self.make_tag_text(object_sha="xxx"))
564
565     def test_check_duplicates(self):
566         # duplicate each of the header fields
567         for i in xrange(4):
568             lines = self.make_tag_lines()
569             lines.insert(i, lines[i])
570             self.assertCheckFails(Tag, '\n'.join(lines))
571
572     def test_check_order(self):
573         lines = self.make_tag_lines()
574         headers = lines[:4]
575         rest = lines[4:]
576         # of all possible permutations, ensure only the original succeeds
577         for perm in permutations(headers):
578             perm = list(perm)
579             text = '\n'.join(perm + rest)
580             if perm == headers:
581                 self.assertCheckSucceeds(Tag, text)
582             else:
583                 self.assertCheckFails(Tag, text)
584
585
586 class CheckTests(unittest.TestCase):
587
588     def test_check_hexsha(self):
589         check_hexsha(a_sha, "failed to check good sha")
590         self.assertRaises(ObjectFormatException, check_hexsha, '1' * 39,
591                           'sha too short')
592         self.assertRaises(ObjectFormatException, check_hexsha, '1' * 41,
593                           'sha too long')
594         self.assertRaises(ObjectFormatException, check_hexsha, 'x' * 40,
595                           'invalid characters')
596
597     def test_check_identity(self):
598         check_identity("Dave Borowitz <dborowitz@google.com>",
599                        "failed to check good identity")
600         check_identity("<dborowitz@google.com>",
601                        "failed to check good identity")
602         self.assertRaises(ObjectFormatException, check_identity,
603                           "Dave Borowitz", "no email")
604         self.assertRaises(ObjectFormatException, check_identity,
605                           "Dave Borowitz <dborowitz", "incomplete email")
606         self.assertRaises(ObjectFormatException, check_identity,
607                           "dborowitz@google.com>", "incomplete email")
608         self.assertRaises(ObjectFormatException, check_identity,
609                           "Dave Borowitz <<dborowitz@google.com>", "typo")
610         self.assertRaises(ObjectFormatException, check_identity,
611                           "Dave Borowitz <dborowitz@google.com>>", "typo")
612         self.assertRaises(ObjectFormatException, check_identity,
613                           "Dave Borowitz <dborowitz@google.com>xxx",
614                           "trailing characters")
615
616
617 class TimezoneTests(unittest.TestCase):
618
619     def test_parse_timezone_utc(self):
620         self.assertEquals((0, False), parse_timezone("+0000"))
621
622     def test_parse_timezone_utc_negative(self):
623         self.assertEquals((0, True), parse_timezone("-0000"))
624
625     def test_generate_timezone_utc(self):
626         self.assertEquals("+0000", format_timezone(0))
627
628     def test_generate_timezone_utc_negative(self):
629         self.assertEquals("-0000", format_timezone(0, True))
630
631     def test_parse_timezone_cet(self):
632         self.assertEquals((60 * 60, False), parse_timezone("+0100"))
633
634     def test_format_timezone_cet(self):
635         self.assertEquals("+0100", format_timezone(60 * 60))
636
637     def test_format_timezone_pdt(self):
638         self.assertEquals("-0400", format_timezone(-4 * 60 * 60))
639
640     def test_parse_timezone_pdt(self):
641         self.assertEquals((-4 * 60 * 60, False), parse_timezone("-0400"))
642
643     def test_format_timezone_pdt_half(self):
644         self.assertEquals("-0440",
645             format_timezone(int(((-4 * 60) - 40) * 60)))
646
647     def test_parse_timezone_pdt_half(self):
648         self.assertEquals((((-4 * 60) - 40) * 60, False),
649             parse_timezone("-0440"))