1 # test_objects.py -- tests for objects.py
2 # Copyright (C) 2007 James Westby <jw+debian@jameswestby.net>
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
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.
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,
21 """Tests for git base objects."""
23 # TODO: Round-trip parse-serialize-parse and serialize-parse-serialize tests.
31 from dulwich.errors import (
33 ObjectFormatException,
35 from dulwich.objects import (
50 from dulwich.tests import (
54 a_sha = '6f670c0fb53f9463760b7295fbb814e965fb20c8'
55 b_sha = '2969be3e8ee1c0222396a5611407e4769f14e54b'
56 c_sha = '954a536f7819d40e6f637f849ee187dd10066349'
57 tree_sha = '70c190eb48fa8bbb50ddc692a17b44cb781af7f6'
58 tag_sha = '71033db03a03c6a36721efcf1968dd8f8e0cf023'
62 from itertools import permutations
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)
72 r = n if r is None else r
76 cycles = range(n, n-r, -1)
77 yield tuple(pool[i] for i in indices[:r])
79 for i in reversed(range(r)):
82 indices[i:] = indices[i+1:] + indices[i:i+1]
86 indices[i], indices[-j] = indices[-j], indices[i]
87 yield tuple(pool[i] for i in indices[:r])
93 class TestHexToSha(unittest.TestCase):
95 def test_simple(self):
96 self.assertEquals("\xab\xcd" * 10, hex_to_sha("abcd" * 10))
98 def test_reverse(self):
99 self.assertEquals("abcd" * 10, sha_to_hex("\xab\xcd" * 10))
102 class BlobReadTests(unittest.TestCase):
103 """Test decompression of blobs"""
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))
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)
113 def get_tree(self, sha):
114 return self.get_sha_file(Tree, 'trees', sha)
116 def get_tag(self, sha):
117 return self.get_sha_file(Tag, 'tags', sha)
119 def commit(self, sha):
120 return self.get_sha_file(Commit, 'commits', sha)
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)
128 b = self.get_blob(a_sha)
129 self.assertEqual(hash(b.id), hash(b))
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)
138 def test_create_blob_from_string(self):
140 b = Blob.from_string(string)
141 self.assertEqual(b.data, string)
142 self.assertEqual(b.sha().hexdigest(), b_sha)
144 def test_chunks(self):
146 b = Blob.from_string(string)
147 self.assertEqual([string], b.chunked)
149 def test_set_chunks(self):
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())
156 def test_parse_legacy_blob(self):
158 b = self.get_blob(c_sha)
159 self.assertEqual(b.data, string)
160 self.assertEqual(b.sha().hexdigest(), c_sha)
163 blob1 = self.get_blob(a_sha)
164 blob2 = self.get_blob(a_sha)
165 self.assertEqual(blob1, blob2)
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))
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')
180 def test_read_commit_from_file(self):
181 sha = '60dacdc733de308bb77bb76ce0fb0f9b44c9769e'
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')
194 def test_read_commit_no_parents(self):
195 sha = '0d89f20333fbb1d2f3a94da77f4981373d8f4310'
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')
208 def test_read_commit_two_parents(self):
209 sha = '5dac377bdded4c9aeb8dff595f0faeebcc8498cc'
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')
223 def test_check_id(self):
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)
231 class ShaFileCheckTests(unittest.TestCase):
233 def assertCheckFails(self, cls, data):
236 obj.set_raw_string(data)
238 self.assertRaises(ObjectFormatException, do_check)
240 def assertCheckSucceeds(self, cls, data):
242 obj.set_raw_string(data)
243 self.assertEqual(None, obj.check())
246 class CommitSerializationTests(unittest.TestCase):
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'
261 def test_encoding(self):
263 c.encoding = "iso8859-1"
264 self.assertTrue("encoding iso8859-1\n" in c.as_raw_string())
266 def test_short_timestamp(self):
270 c1.set_raw_string(c.as_raw_string())
271 self.assertEquals(30, c1.commit_time)
273 def test_raw_length(self):
275 self.assertEquals(len(c.as_raw_string()), c.raw_length())
277 def test_simple(self):
279 self.assertEquals(c.id, '5dac377bdded4c9aeb8dff595f0faeebcc8498cc')
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'
287 'Merge ../b\n', c.as_raw_string())
289 def test_timezone(self):
291 c.commit_timezone = 5 * 60
292 self.assertTrue(" +0005\n" in c.as_raw_string())
294 def test_neg_timezone(self):
296 c.commit_timezone = -1 * 3600
297 self.assertTrue(" -0100\n" in c.as_raw_string())
300 default_committer = 'James Westby <jw+debian@jameswestby.net> 1174773719 +0000'
302 class CommitParseTests(ShaFileCheckTests):
304 def make_commit_lines(self,
305 tree='d80c186a03f423a81b39df39dc87fd269736ca86',
306 parents=['ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd',
307 '4cffe90e0a41ad3f5190079d7c8f036bde29cbe6'],
308 author=default_committer,
309 committer=default_committer,
311 message='Merge ../b\n',
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))
328 if message is not None:
329 lines.append(message)
332 def make_commit_text(self, **kwargs):
333 return '\n'.join(self.make_commit_lines(**kwargs))
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>',
341 self.assertEquals('d80c186a03f423a81b39df39dc87fd269736ca86', c.tree)
342 self.assertEquals(['ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd',
343 '4cffe90e0a41ad3f5190079d7c8f036bde29cbe6'],
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)
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)
359 def test_encoding(self):
360 c = Commit.from_string(self.make_commit_text(encoding='UTF-8'))
361 self.assertEquals('UTF-8', c.encoding)
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'))
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))
382 def test_check_duplicates(self):
383 # duplicate each of the header fields
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)
392 self.assertCheckFails(Commit, text)
394 def test_check_order(self):
395 lines = self.make_commit_lines(parents=[a_sha], encoding='UTF-8')
398 # of all possible permutations, ensure only the original succeeds
399 for perm in permutations(headers):
401 text = '\n'.join(perm + rest)
403 self.assertCheckSucceeds(Commit, text)
405 self.assertCheckFails(Commit, text)
408 class TreeTests(ShaFileCheckTests):
410 def test_simple(self):
411 myhexsha = "d80c186a03f423a81b39df39dc87fd269736ca86"
413 x["myname"] = (0100755, myhexsha)
414 self.assertEquals('100755 myname\0' + hex_to_sha(myhexsha),
417 def test_tree_dir_sort(self):
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()])
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())))
430 def test_parse_tree(self):
431 self._do_test_parse_tree(_parse_tree_py)
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)
438 def test_check(self):
440 sha = hex_to_sha(a_sha)
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)
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)
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))
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))
471 t["foo"] = (0100644, a_sha)
472 self.assertEquals(set(["foo"]), set(t))
475 class TagSerializeTests(unittest.TestCase):
477 def test_serialize_simple(self):
479 x.tagger = "Jelmer Vernooij <jelmer@samba.org>"
481 x.message = "Tag 0.1"
482 x.object = (Blob, "d80c186a03f423a81b39df39dc87fd269736ca86")
483 x.tag_time = 423423423
485 self.assertEquals("""object d80c186a03f423a81b39df39dc87fd269736ca86
488 tagger Jelmer Vernooij <jelmer@samba.org> 423423423 +0000
490 Tag 0.1""", x.as_raw_string())
493 default_tagger = ('Linus Torvalds <torvalds@woody.linux-foundation.org> '
495 default_message = """Linux 2.6.22-rc7
496 -----BEGIN PGP SIGNATURE-----
497 Version: GnuPG v1.4.7 (GNU/Linux)
499 iD8DBQBGiAaAF3YsRnbiHLsRAitMAKCiLboJkQECM/jpYsY3WPfvUgLXkACgg3ql
500 OK2XeQOiEeXtT76rV4t2WR4=
502 -----END PGP SIGNATURE-----
506 class TagParseTests(ShaFileCheckTests):
507 def make_tag_lines(self,
508 object_sha="a38d6181ff27824c79fc7df825164a212eff6a3f",
509 object_type_name="commit",
511 tagger=default_tagger,
512 message=default_message):
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)
519 lines.append("tag %s" % name)
520 if tagger is not None:
521 lines.append("tagger %s" % tagger)
523 if message is not None:
524 lines.append(message)
527 def make_tag_text(self, **kwargs):
528 return "\n".join(self.make_tag_lines(**kwargs))
530 def test_parse(self):
532 x.set_raw_string(self.make_tag_text())
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",
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)
544 def test_parse_no_tagger(self):
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)
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"))
565 def test_check_duplicates(self):
566 # duplicate each of the header fields
568 lines = self.make_tag_lines()
569 lines.insert(i, lines[i])
570 self.assertCheckFails(Tag, '\n'.join(lines))
572 def test_check_order(self):
573 lines = self.make_tag_lines()
576 # of all possible permutations, ensure only the original succeeds
577 for perm in permutations(headers):
579 text = '\n'.join(perm + rest)
581 self.assertCheckSucceeds(Tag, text)
583 self.assertCheckFails(Tag, text)
586 class CheckTests(unittest.TestCase):
588 def test_check_hexsha(self):
589 check_hexsha(a_sha, "failed to check good sha")
590 self.assertRaises(ObjectFormatException, check_hexsha, '1' * 39,
592 self.assertRaises(ObjectFormatException, check_hexsha, '1' * 41,
594 self.assertRaises(ObjectFormatException, check_hexsha, 'x' * 40,
595 'invalid characters')
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")
617 class TimezoneTests(unittest.TestCase):
619 def test_parse_timezone_utc(self):
620 self.assertEquals((0, False), parse_timezone("+0000"))
622 def test_parse_timezone_utc_negative(self):
623 self.assertEquals((0, True), parse_timezone("-0000"))
625 def test_generate_timezone_utc(self):
626 self.assertEquals("+0000", format_timezone(0))
628 def test_generate_timezone_utc_negative(self):
629 self.assertEquals("-0000", format_timezone(0, True))
631 def test_parse_timezone_cet(self):
632 self.assertEquals((60 * 60, False), parse_timezone("+0100"))
634 def test_format_timezone_cet(self):
635 self.assertEquals("+0100", format_timezone(60 * 60))
637 def test_format_timezone_pdt(self):
638 self.assertEquals("-0400", format_timezone(-4 * 60 * 60))
640 def test_parse_timezone_pdt(self):
641 self.assertEquals((-4 * 60 * 60, False), parse_timezone("-0400"))
643 def test_format_timezone_pdt_half(self):
644 self.assertEquals("-0440",
645 format_timezone(int(((-4 * 60) - 40) * 60)))
647 def test_parse_timezone_pdt_half(self):
648 self.assertEquals((((-4 * 60) - 40) * 60, False),
649 parse_timezone("-0440"))