test_objects.py 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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. """Tests for git base objects."""
  20. # TODO: Round-trip parse-serialize-parse and serialize-parse-serialize tests.
  21. from io import BytesIO
  22. import datetime
  23. from itertools import (
  24. permutations,
  25. )
  26. import os
  27. import stat
  28. import warnings
  29. from dulwich.errors import (
  30. ObjectFormatException,
  31. )
  32. from dulwich.objects import (
  33. Blob,
  34. Tree,
  35. Commit,
  36. ShaFile,
  37. Tag,
  38. TreeEntry,
  39. format_timezone,
  40. hex_to_sha,
  41. sha_to_hex,
  42. hex_to_filename,
  43. check_hexsha,
  44. check_identity,
  45. parse_timezone,
  46. object_class,
  47. parse_tree,
  48. _parse_tree_py,
  49. sorted_tree_items,
  50. _sorted_tree_items_py,
  51. )
  52. from dulwich.tests import (
  53. TestCase,
  54. )
  55. from dulwich.tests.utils import (
  56. make_commit,
  57. make_object,
  58. functest_builder,
  59. ext_functest_builder,
  60. )
  61. a_sha = b'6f670c0fb53f9463760b7295fbb814e965fb20c8'
  62. b_sha = b'2969be3e8ee1c0222396a5611407e4769f14e54b'
  63. c_sha = b'954a536f7819d40e6f637f849ee187dd10066349'
  64. tree_sha = b'70c190eb48fa8bbb50ddc692a17b44cb781af7f6'
  65. tag_sha = b'71033db03a03c6a36721efcf1968dd8f8e0cf023'
  66. class TestHexToSha(TestCase):
  67. def test_simple(self):
  68. self.assertEqual(b'\xab\xcd' * 10, hex_to_sha(b'abcd' * 10))
  69. def test_reverse(self):
  70. self.assertEqual(b'abcd' * 10, sha_to_hex(b'\xab\xcd' * 10))
  71. class BlobReadTests(TestCase):
  72. """Test decompression of blobs"""
  73. def get_sha_file(self, cls, base, sha):
  74. dir = os.path.join(os.path.dirname(__file__), 'data', base)
  75. return cls.from_path(hex_to_filename(dir, sha))
  76. def get_blob(self, sha):
  77. """Return the blob named sha from the test data dir"""
  78. return self.get_sha_file(Blob, 'blobs', sha)
  79. def get_tree(self, sha):
  80. return self.get_sha_file(Tree, 'trees', sha)
  81. def get_tag(self, sha):
  82. return self.get_sha_file(Tag, 'tags', sha)
  83. def commit(self, sha):
  84. return self.get_sha_file(Commit, 'commits', sha)
  85. def test_decompress_simple_blob(self):
  86. b = self.get_blob(a_sha)
  87. self.assertEqual(b.data, b'test 1\n')
  88. self.assertEqual(b.sha().hexdigest().encode('ascii'), a_sha)
  89. def test_hash(self):
  90. b = self.get_blob(a_sha)
  91. self.assertEqual(hash(b.id), hash(b))
  92. def test_parse_empty_blob_object(self):
  93. sha = b'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391'
  94. b = self.get_blob(sha)
  95. self.assertEqual(b.data, b'')
  96. self.assertEqual(b.id, sha)
  97. self.assertEqual(b.sha().hexdigest().encode('ascii'), sha)
  98. def test_create_blob_from_string(self):
  99. string = b'test 2\n'
  100. b = Blob.from_string(string)
  101. self.assertEqual(b.data, string)
  102. self.assertEqual(b.sha().hexdigest().encode('ascii'), b_sha)
  103. def test_legacy_from_file(self):
  104. b1 = Blob.from_string(b'foo')
  105. b_raw = b1.as_legacy_object()
  106. b2 = b1.from_file(BytesIO(b_raw))
  107. self.assertEqual(b1, b2)
  108. def test_chunks(self):
  109. string = b'test 5\n'
  110. b = Blob.from_string(string)
  111. self.assertEqual([string], b.chunked)
  112. def test_set_chunks(self):
  113. b = Blob()
  114. b.chunked = [b'te', b'st', b' 5\n']
  115. self.assertEqual(b'test 5\n', b.data)
  116. b.chunked = [b'te', b'st', b' 6\n']
  117. self.assertEqual(b'test 6\n', b.as_raw_string())
  118. def test_parse_legacy_blob(self):
  119. string = b'test 3\n'
  120. b = self.get_blob(c_sha)
  121. self.assertEqual(b.data, string)
  122. self.assertEqual(b.sha().hexdigest().encode('ascii'), c_sha)
  123. def test_eq(self):
  124. blob1 = self.get_blob(a_sha)
  125. blob2 = self.get_blob(a_sha)
  126. self.assertEqual(blob1, blob2)
  127. def test_read_tree_from_file(self):
  128. t = self.get_tree(tree_sha)
  129. self.assertEqual(t.items()[0], (b'a', 33188, a_sha))
  130. self.assertEqual(t.items()[1], (b'b', 33188, b_sha))
  131. def test_read_tag_from_file(self):
  132. t = self.get_tag(tag_sha)
  133. self.assertEqual(t.object,
  134. (Commit, b'51b668fd5bf7061b7d6fa525f88803e6cfadaa51'))
  135. self.assertEqual(t.name, b'signed')
  136. self.assertEqual(t.tagger, b'Ali Sabil <ali.sabil@gmail.com>')
  137. self.assertEqual(t.tag_time, 1231203091)
  138. self.assertEqual(t.message, b'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')
  139. def test_read_commit_from_file(self):
  140. sha = b'60dacdc733de308bb77bb76ce0fb0f9b44c9769e'
  141. c = self.commit(sha)
  142. self.assertEqual(c.tree, tree_sha)
  143. self.assertEqual(c.parents,
  144. [b'0d89f20333fbb1d2f3a94da77f4981373d8f4310'])
  145. self.assertEqual(c.author,
  146. b'James Westby <jw+debian@jameswestby.net>')
  147. self.assertEqual(c.committer,
  148. b'James Westby <jw+debian@jameswestby.net>')
  149. self.assertEqual(c.commit_time, 1174759230)
  150. self.assertEqual(c.commit_timezone, 0)
  151. self.assertEqual(c.author_timezone, 0)
  152. self.assertEqual(c.message, b'Test commit\n')
  153. def test_read_commit_no_parents(self):
  154. sha = b'0d89f20333fbb1d2f3a94da77f4981373d8f4310'
  155. c = self.commit(sha)
  156. self.assertEqual(c.tree, b'90182552c4a85a45ec2a835cadc3451bebdfe870')
  157. self.assertEqual(c.parents, [])
  158. self.assertEqual(c.author,
  159. b'James Westby <jw+debian@jameswestby.net>')
  160. self.assertEqual(c.committer,
  161. b'James Westby <jw+debian@jameswestby.net>')
  162. self.assertEqual(c.commit_time, 1174758034)
  163. self.assertEqual(c.commit_timezone, 0)
  164. self.assertEqual(c.author_timezone, 0)
  165. self.assertEqual(c.message, b'Test commit\n')
  166. def test_read_commit_two_parents(self):
  167. sha = b'5dac377bdded4c9aeb8dff595f0faeebcc8498cc'
  168. c = self.commit(sha)
  169. self.assertEqual(c.tree, b'd80c186a03f423a81b39df39dc87fd269736ca86')
  170. self.assertEqual(c.parents,
  171. [b'ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd',
  172. b'4cffe90e0a41ad3f5190079d7c8f036bde29cbe6'])
  173. self.assertEqual(c.author,
  174. b'James Westby <jw+debian@jameswestby.net>')
  175. self.assertEqual(c.committer,
  176. b'James Westby <jw+debian@jameswestby.net>')
  177. self.assertEqual(c.commit_time, 1174773719)
  178. self.assertEqual(c.commit_timezone, 0)
  179. self.assertEqual(c.author_timezone, 0)
  180. self.assertEqual(c.message, b'Merge ../b\n')
  181. def test_stub_sha(self):
  182. sha = b'5' * 40
  183. c = make_commit(id=sha, message=b'foo')
  184. self.assertTrue(isinstance(c, Commit))
  185. self.assertEqual(sha, c.id)
  186. self.assertNotEqual(sha, c._make_sha())
  187. class ShaFileCheckTests(TestCase):
  188. def assertCheckFails(self, cls, data):
  189. obj = cls()
  190. def do_check():
  191. obj.set_raw_string(data)
  192. obj.check()
  193. self.assertRaises(ObjectFormatException, do_check)
  194. def assertCheckSucceeds(self, cls, data):
  195. obj = cls()
  196. obj.set_raw_string(data)
  197. self.assertEqual(None, obj.check())
  198. small_buffer_zlib_object = (
  199. b'\x48\x89\x15\xcc\x31\x0e\xc2\x30\x0c\x40\x51\xe6'
  200. b'\x9c\xc2\x3b\xaa\x64\x37\xc4\xc1\x12\x42\x5c\xc5'
  201. b'\x49\xac\x52\xd4\x92\xaa\x78\xe1\xf6\x94\xed\xeb'
  202. b'\x0d\xdf\x75\x02\xa2\x7c\xea\xe5\x65\xd5\x81\x8b'
  203. b'\x9a\x61\xba\xa0\xa9\x08\x36\xc9\x4c\x1a\xad\x88'
  204. b'\x16\xba\x46\xc4\xa8\x99\x6a\x64\xe1\xe0\xdf\xcd'
  205. b'\xa0\xf6\x75\x9d\x3d\xf8\xf1\xd0\x77\xdb\xfb\xdc'
  206. b'\x86\xa3\x87\xf1\x2f\x93\xed\x00\xb7\xc7\xd2\xab'
  207. b'\x2e\xcf\xfe\xf1\x3b\x50\xa4\x91\x53\x12\x24\x38'
  208. b'\x23\x21\x86\xf0\x03\x2f\x91\x24\x52'
  209. )
  210. class ShaFileTests(TestCase):
  211. def test_deflated_smaller_window_buffer(self):
  212. # zlib on some systems uses smaller buffers,
  213. # resulting in a different header.
  214. # See https://github.com/libgit2/libgit2/pull/464
  215. sf = ShaFile.from_file(BytesIO(small_buffer_zlib_object))
  216. self.assertEqual(sf.type_name, b'tag')
  217. self.assertEqual(sf.tagger, b' <@localhost>')
  218. class CommitSerializationTests(TestCase):
  219. def make_commit(self, **kwargs):
  220. attrs = {'tree': b'd80c186a03f423a81b39df39dc87fd269736ca86',
  221. 'parents': [b'ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd',
  222. b'4cffe90e0a41ad3f5190079d7c8f036bde29cbe6'],
  223. 'author': b'James Westby <jw+debian@jameswestby.net>',
  224. 'committer': b'James Westby <jw+debian@jameswestby.net>',
  225. 'commit_time': 1174773719,
  226. 'author_time': 1174773719,
  227. 'commit_timezone': 0,
  228. 'author_timezone': 0,
  229. 'message': b'Merge ../b\n'}
  230. attrs.update(kwargs)
  231. return make_commit(**attrs)
  232. def test_encoding(self):
  233. c = self.make_commit(encoding=b'iso8859-1')
  234. self.assertTrue(b'encoding iso8859-1\n' in c.as_raw_string())
  235. def test_short_timestamp(self):
  236. c = self.make_commit(commit_time=30)
  237. c1 = Commit()
  238. c1.set_raw_string(c.as_raw_string())
  239. self.assertEqual(30, c1.commit_time)
  240. def test_raw_length(self):
  241. c = self.make_commit()
  242. self.assertEqual(len(c.as_raw_string()), c.raw_length())
  243. def test_simple(self):
  244. c = self.make_commit()
  245. self.assertEqual(c.id, b'5dac377bdded4c9aeb8dff595f0faeebcc8498cc')
  246. self.assertEqual(
  247. b'tree d80c186a03f423a81b39df39dc87fd269736ca86\n'
  248. b'parent ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd\n'
  249. b'parent 4cffe90e0a41ad3f5190079d7c8f036bde29cbe6\n'
  250. b'author James Westby <jw+debian@jameswestby.net> '
  251. b'1174773719 +0000\n'
  252. b'committer James Westby <jw+debian@jameswestby.net> '
  253. b'1174773719 +0000\n'
  254. b'\n'
  255. b'Merge ../b\n', c.as_raw_string())
  256. def test_timezone(self):
  257. c = self.make_commit(commit_timezone=(5 * 60))
  258. self.assertTrue(b' +0005\n' in c.as_raw_string())
  259. def test_neg_timezone(self):
  260. c = self.make_commit(commit_timezone=(-1 * 3600))
  261. self.assertTrue(b' -0100\n' in c.as_raw_string())
  262. def test_deserialize(self):
  263. c = self.make_commit()
  264. d = Commit()
  265. d._deserialize(c.as_raw_chunks())
  266. self.assertEqual(c, d)
  267. def test_serialize_gpgsig(self):
  268. commit = self.make_commit(gpgsig=b"""-----BEGIN PGP SIGNATURE-----
  269. Version: GnuPG v1
  270. iQIcBAABCgAGBQJULCdfAAoJEACAbyvXKaRXuKwP/RyP9PA49uAvu8tQVCC/uBa8
  271. vi975+xvO14R8Pp8k2nps7lSxCdtCd+xVT1VRHs0wNhOZo2YCVoU1HATkPejqSeV
  272. NScTHcxnk4/+bxyfk14xvJkNp7FlQ3npmBkA+lbV0Ubr33rvtIE5jiJPyz+SgWAg
  273. xdBG2TojV0squj00GoH/euK6aX7GgZtwdtpTv44haCQdSuPGDcI4TORqR6YSqvy3
  274. GPE+3ZqXPFFb+KILtimkxitdwB7CpwmNse2vE3rONSwTvi8nq3ZoQYNY73CQGkUy
  275. qoFU0pDtw87U3niFin1ZccDgH0bB6624sLViqrjcbYJeg815Htsu4rmzVaZADEVC
  276. XhIO4MThebusdk0AcNGjgpf3HRHk0DPMDDlIjm+Oao0cqovvF6VyYmcb0C+RmhJj
  277. dodLXMNmbqErwTk3zEkW0yZvNIYXH7m9SokPCZa4eeIM7be62X6h1mbt0/IU6Th+
  278. v18fS0iTMP/Viug5und+05C/v04kgDo0CPphAbXwWMnkE4B6Tl9sdyUYXtvQsL7x
  279. 0+WP1gL27ANqNZiI07Kz/BhbBAQI/+2TFT7oGr0AnFPQ5jHp+3GpUf6OKuT1wT3H
  280. ND189UFuRuubxb42vZhpcXRbqJVWnbECTKVUPsGZqat3enQUB63uM4i6/RdONDZA
  281. fDeF1m4qYs+cUXKNUZ03
  282. =X6RT
  283. -----END PGP SIGNATURE-----""")
  284. self.maxDiff = None
  285. self.assertEqual(b"""\
  286. tree d80c186a03f423a81b39df39dc87fd269736ca86
  287. parent ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd
  288. parent 4cffe90e0a41ad3f5190079d7c8f036bde29cbe6
  289. author James Westby <jw+debian@jameswestby.net> 1174773719 +0000
  290. committer James Westby <jw+debian@jameswestby.net> 1174773719 +0000
  291. gpgsig -----BEGIN PGP SIGNATURE-----
  292. Version: GnuPG v1
  293. iQIcBAABCgAGBQJULCdfAAoJEACAbyvXKaRXuKwP/RyP9PA49uAvu8tQVCC/uBa8
  294. vi975+xvO14R8Pp8k2nps7lSxCdtCd+xVT1VRHs0wNhOZo2YCVoU1HATkPejqSeV
  295. NScTHcxnk4/+bxyfk14xvJkNp7FlQ3npmBkA+lbV0Ubr33rvtIE5jiJPyz+SgWAg
  296. xdBG2TojV0squj00GoH/euK6aX7GgZtwdtpTv44haCQdSuPGDcI4TORqR6YSqvy3
  297. GPE+3ZqXPFFb+KILtimkxitdwB7CpwmNse2vE3rONSwTvi8nq3ZoQYNY73CQGkUy
  298. qoFU0pDtw87U3niFin1ZccDgH0bB6624sLViqrjcbYJeg815Htsu4rmzVaZADEVC
  299. XhIO4MThebusdk0AcNGjgpf3HRHk0DPMDDlIjm+Oao0cqovvF6VyYmcb0C+RmhJj
  300. dodLXMNmbqErwTk3zEkW0yZvNIYXH7m9SokPCZa4eeIM7be62X6h1mbt0/IU6Th+
  301. v18fS0iTMP/Viug5und+05C/v04kgDo0CPphAbXwWMnkE4B6Tl9sdyUYXtvQsL7x
  302. 0+WP1gL27ANqNZiI07Kz/BhbBAQI/+2TFT7oGr0AnFPQ5jHp+3GpUf6OKuT1wT3H
  303. ND189UFuRuubxb42vZhpcXRbqJVWnbECTKVUPsGZqat3enQUB63uM4i6/RdONDZA
  304. fDeF1m4qYs+cUXKNUZ03
  305. =X6RT
  306. -----END PGP SIGNATURE-----
  307. Merge ../b
  308. """, commit.as_raw_string())
  309. def test_serialize_mergetag(self):
  310. tag = make_object(
  311. Tag, object=(Commit, b'a38d6181ff27824c79fc7df825164a212eff6a3f'),
  312. object_type_name=b'commit',
  313. name=b'v2.6.22-rc7',
  314. tag_time=1183319674,
  315. tag_timezone=0,
  316. tagger=b'Linus Torvalds <torvalds@woody.linux-foundation.org>',
  317. message=default_message)
  318. commit = self.make_commit(mergetag=[tag])
  319. self.assertEqual(b"""tree d80c186a03f423a81b39df39dc87fd269736ca86
  320. parent ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd
  321. parent 4cffe90e0a41ad3f5190079d7c8f036bde29cbe6
  322. author James Westby <jw+debian@jameswestby.net> 1174773719 +0000
  323. committer James Westby <jw+debian@jameswestby.net> 1174773719 +0000
  324. mergetag object a38d6181ff27824c79fc7df825164a212eff6a3f
  325. type commit
  326. tag v2.6.22-rc7
  327. tagger Linus Torvalds <torvalds@woody.linux-foundation.org> 1183319674 +0000
  328. Linux 2.6.22-rc7
  329. -----BEGIN PGP SIGNATURE-----
  330. Version: GnuPG v1.4.7 (GNU/Linux)
  331. iD8DBQBGiAaAF3YsRnbiHLsRAitMAKCiLboJkQECM/jpYsY3WPfvUgLXkACgg3ql
  332. OK2XeQOiEeXtT76rV4t2WR4=
  333. =ivrA
  334. -----END PGP SIGNATURE-----
  335. Merge ../b
  336. """, commit.as_raw_string())
  337. def test_serialize_mergetags(self):
  338. tag = make_object(
  339. Tag, object=(Commit, b'a38d6181ff27824c79fc7df825164a212eff6a3f'),
  340. object_type_name=b'commit',
  341. name=b'v2.6.22-rc7',
  342. tag_time=1183319674,
  343. tag_timezone=0,
  344. tagger=b'Linus Torvalds <torvalds@woody.linux-foundation.org>',
  345. message=default_message)
  346. commit = self.make_commit(mergetag=[tag, tag])
  347. self.assertEqual(b"""tree d80c186a03f423a81b39df39dc87fd269736ca86
  348. parent ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd
  349. parent 4cffe90e0a41ad3f5190079d7c8f036bde29cbe6
  350. author James Westby <jw+debian@jameswestby.net> 1174773719 +0000
  351. committer James Westby <jw+debian@jameswestby.net> 1174773719 +0000
  352. mergetag object a38d6181ff27824c79fc7df825164a212eff6a3f
  353. type commit
  354. tag v2.6.22-rc7
  355. tagger Linus Torvalds <torvalds@woody.linux-foundation.org> 1183319674 +0000
  356. Linux 2.6.22-rc7
  357. -----BEGIN PGP SIGNATURE-----
  358. Version: GnuPG v1.4.7 (GNU/Linux)
  359. iD8DBQBGiAaAF3YsRnbiHLsRAitMAKCiLboJkQECM/jpYsY3WPfvUgLXkACgg3ql
  360. OK2XeQOiEeXtT76rV4t2WR4=
  361. =ivrA
  362. -----END PGP SIGNATURE-----
  363. mergetag object a38d6181ff27824c79fc7df825164a212eff6a3f
  364. type commit
  365. tag v2.6.22-rc7
  366. tagger Linus Torvalds <torvalds@woody.linux-foundation.org> 1183319674 +0000
  367. Linux 2.6.22-rc7
  368. -----BEGIN PGP SIGNATURE-----
  369. Version: GnuPG v1.4.7 (GNU/Linux)
  370. iD8DBQBGiAaAF3YsRnbiHLsRAitMAKCiLboJkQECM/jpYsY3WPfvUgLXkACgg3ql
  371. OK2XeQOiEeXtT76rV4t2WR4=
  372. =ivrA
  373. -----END PGP SIGNATURE-----
  374. Merge ../b
  375. """, commit.as_raw_string())
  376. def test_deserialize_mergetag(self):
  377. tag = make_object(
  378. Tag, object=(Commit, b'a38d6181ff27824c79fc7df825164a212eff6a3f'),
  379. object_type_name=b'commit',
  380. name=b'v2.6.22-rc7',
  381. tag_time=1183319674,
  382. tag_timezone=0,
  383. tagger=b'Linus Torvalds <torvalds@woody.linux-foundation.org>',
  384. message=default_message)
  385. commit = self.make_commit(mergetag=[tag])
  386. d = Commit()
  387. d._deserialize(commit.as_raw_chunks())
  388. self.assertEqual(commit, d)
  389. def test_deserialize_mergetags(self):
  390. tag = make_object(
  391. Tag, object=(Commit, b'a38d6181ff27824c79fc7df825164a212eff6a3f'),
  392. object_type_name=b'commit',
  393. name=b'v2.6.22-rc7',
  394. tag_time=1183319674,
  395. tag_timezone=0,
  396. tagger=b'Linus Torvalds <torvalds@woody.linux-foundation.org>',
  397. message=default_message)
  398. commit = self.make_commit(mergetag=[tag, tag])
  399. d = Commit()
  400. d._deserialize(commit.as_raw_chunks())
  401. self.assertEqual(commit, d)
  402. default_committer = b'James Westby <jw+debian@jameswestby.net> 1174773719 +0000'
  403. class CommitParseTests(ShaFileCheckTests):
  404. def make_commit_lines(self,
  405. tree=b'd80c186a03f423a81b39df39dc87fd269736ca86',
  406. parents=[b'ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd',
  407. b'4cffe90e0a41ad3f5190079d7c8f036bde29cbe6'],
  408. author=default_committer,
  409. committer=default_committer,
  410. encoding=None,
  411. message=b'Merge ../b\n',
  412. extra=None):
  413. lines = []
  414. if tree is not None:
  415. lines.append(b'tree ' + tree)
  416. if parents is not None:
  417. lines.extend(b'parent ' + p for p in parents)
  418. if author is not None:
  419. lines.append(b'author ' + author)
  420. if committer is not None:
  421. lines.append(b'committer ' + committer)
  422. if encoding is not None:
  423. lines.append(b'encoding ' + encoding)
  424. if extra is not None:
  425. for name, value in sorted(extra.items()):
  426. lines.append(name + b' ' + value)
  427. lines.append(b'')
  428. if message is not None:
  429. lines.append(message)
  430. return lines
  431. def make_commit_text(self, **kwargs):
  432. return b'\n'.join(self.make_commit_lines(**kwargs))
  433. def test_simple(self):
  434. c = Commit.from_string(self.make_commit_text())
  435. self.assertEqual(b'Merge ../b\n', c.message)
  436. self.assertEqual(b'James Westby <jw+debian@jameswestby.net>', c.author)
  437. self.assertEqual(b'James Westby <jw+debian@jameswestby.net>',
  438. c.committer)
  439. self.assertEqual(b'd80c186a03f423a81b39df39dc87fd269736ca86', c.tree)
  440. self.assertEqual([b'ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd',
  441. b'4cffe90e0a41ad3f5190079d7c8f036bde29cbe6'],
  442. c.parents)
  443. expected_time = datetime.datetime(2007, 3, 24, 22, 1, 59)
  444. self.assertEqual(expected_time,
  445. datetime.datetime.utcfromtimestamp(c.commit_time))
  446. self.assertEqual(0, c.commit_timezone)
  447. self.assertEqual(expected_time,
  448. datetime.datetime.utcfromtimestamp(c.author_time))
  449. self.assertEqual(0, c.author_timezone)
  450. self.assertEqual(None, c.encoding)
  451. def test_custom(self):
  452. c = Commit.from_string(self.make_commit_text(
  453. extra={b'extra-field': b'data'}))
  454. self.assertEqual([(b'extra-field', b'data')], c.extra)
  455. def test_encoding(self):
  456. c = Commit.from_string(self.make_commit_text(encoding=b'UTF-8'))
  457. self.assertEqual(b'UTF-8', c.encoding)
  458. def test_check(self):
  459. self.assertCheckSucceeds(Commit, self.make_commit_text())
  460. self.assertCheckSucceeds(Commit, self.make_commit_text(parents=None))
  461. self.assertCheckSucceeds(Commit,
  462. self.make_commit_text(encoding=b'UTF-8'))
  463. self.assertCheckFails(Commit, self.make_commit_text(tree=b'xxx'))
  464. self.assertCheckFails(Commit, self.make_commit_text(
  465. parents=[a_sha, b'xxx']))
  466. bad_committer = b'some guy without an email address 1174773719 +0000'
  467. self.assertCheckFails(Commit,
  468. self.make_commit_text(committer=bad_committer))
  469. self.assertCheckFails(Commit,
  470. self.make_commit_text(author=bad_committer))
  471. self.assertCheckFails(Commit, self.make_commit_text(author=None))
  472. self.assertCheckFails(Commit, self.make_commit_text(committer=None))
  473. self.assertCheckFails(Commit, self.make_commit_text(
  474. author=None, committer=None))
  475. def test_check_duplicates(self):
  476. # duplicate each of the header fields
  477. for i in range(5):
  478. lines = self.make_commit_lines(parents=[a_sha], encoding=b'UTF-8')
  479. lines.insert(i, lines[i])
  480. text = b'\n'.join(lines)
  481. if lines[i].startswith(b'parent'):
  482. # duplicate parents are ok for now
  483. self.assertCheckSucceeds(Commit, text)
  484. else:
  485. self.assertCheckFails(Commit, text)
  486. def test_check_order(self):
  487. lines = self.make_commit_lines(parents=[a_sha], encoding=b'UTF-8')
  488. headers = lines[:5]
  489. rest = lines[5:]
  490. # of all possible permutations, ensure only the original succeeds
  491. for perm in permutations(headers):
  492. perm = list(perm)
  493. text = b'\n'.join(perm + rest)
  494. if perm == headers:
  495. self.assertCheckSucceeds(Commit, text)
  496. else:
  497. self.assertCheckFails(Commit, text)
  498. def test_parse_gpgsig(self):
  499. c = Commit.from_string(b"""tree aaff74984cccd156a469afa7d9ab10e4777beb24
  500. author Jelmer Vernooij <jelmer@samba.org> 1412179807 +0200
  501. committer Jelmer Vernooij <jelmer@samba.org> 1412179807 +0200
  502. gpgsig -----BEGIN PGP SIGNATURE-----
  503. Version: GnuPG v1
  504. iQIcBAABCgAGBQJULCdfAAoJEACAbyvXKaRXuKwP/RyP9PA49uAvu8tQVCC/uBa8
  505. vi975+xvO14R8Pp8k2nps7lSxCdtCd+xVT1VRHs0wNhOZo2YCVoU1HATkPejqSeV
  506. NScTHcxnk4/+bxyfk14xvJkNp7FlQ3npmBkA+lbV0Ubr33rvtIE5jiJPyz+SgWAg
  507. xdBG2TojV0squj00GoH/euK6aX7GgZtwdtpTv44haCQdSuPGDcI4TORqR6YSqvy3
  508. GPE+3ZqXPFFb+KILtimkxitdwB7CpwmNse2vE3rONSwTvi8nq3ZoQYNY73CQGkUy
  509. qoFU0pDtw87U3niFin1ZccDgH0bB6624sLViqrjcbYJeg815Htsu4rmzVaZADEVC
  510. XhIO4MThebusdk0AcNGjgpf3HRHk0DPMDDlIjm+Oao0cqovvF6VyYmcb0C+RmhJj
  511. dodLXMNmbqErwTk3zEkW0yZvNIYXH7m9SokPCZa4eeIM7be62X6h1mbt0/IU6Th+
  512. v18fS0iTMP/Viug5und+05C/v04kgDo0CPphAbXwWMnkE4B6Tl9sdyUYXtvQsL7x
  513. 0+WP1gL27ANqNZiI07Kz/BhbBAQI/+2TFT7oGr0AnFPQ5jHp+3GpUf6OKuT1wT3H
  514. ND189UFuRuubxb42vZhpcXRbqJVWnbECTKVUPsGZqat3enQUB63uM4i6/RdONDZA
  515. fDeF1m4qYs+cUXKNUZ03
  516. =X6RT
  517. -----END PGP SIGNATURE-----
  518. foo
  519. """)
  520. self.assertEqual(b'foo\n', c.message)
  521. self.assertEqual([], c.extra)
  522. self.assertEqual(b"""-----BEGIN PGP SIGNATURE-----
  523. Version: GnuPG v1
  524. iQIcBAABCgAGBQJULCdfAAoJEACAbyvXKaRXuKwP/RyP9PA49uAvu8tQVCC/uBa8
  525. vi975+xvO14R8Pp8k2nps7lSxCdtCd+xVT1VRHs0wNhOZo2YCVoU1HATkPejqSeV
  526. NScTHcxnk4/+bxyfk14xvJkNp7FlQ3npmBkA+lbV0Ubr33rvtIE5jiJPyz+SgWAg
  527. xdBG2TojV0squj00GoH/euK6aX7GgZtwdtpTv44haCQdSuPGDcI4TORqR6YSqvy3
  528. GPE+3ZqXPFFb+KILtimkxitdwB7CpwmNse2vE3rONSwTvi8nq3ZoQYNY73CQGkUy
  529. qoFU0pDtw87U3niFin1ZccDgH0bB6624sLViqrjcbYJeg815Htsu4rmzVaZADEVC
  530. XhIO4MThebusdk0AcNGjgpf3HRHk0DPMDDlIjm+Oao0cqovvF6VyYmcb0C+RmhJj
  531. dodLXMNmbqErwTk3zEkW0yZvNIYXH7m9SokPCZa4eeIM7be62X6h1mbt0/IU6Th+
  532. v18fS0iTMP/Viug5und+05C/v04kgDo0CPphAbXwWMnkE4B6Tl9sdyUYXtvQsL7x
  533. 0+WP1gL27ANqNZiI07Kz/BhbBAQI/+2TFT7oGr0AnFPQ5jHp+3GpUf6OKuT1wT3H
  534. ND189UFuRuubxb42vZhpcXRbqJVWnbECTKVUPsGZqat3enQUB63uM4i6/RdONDZA
  535. fDeF1m4qYs+cUXKNUZ03
  536. =X6RT
  537. -----END PGP SIGNATURE-----""", c.gpgsig)
  538. _TREE_ITEMS = {
  539. b'a.c': (0o100755, b'd80c186a03f423a81b39df39dc87fd269736ca86'),
  540. b'a': (stat.S_IFDIR, b'd80c186a03f423a81b39df39dc87fd269736ca86'),
  541. b'a/c': (stat.S_IFDIR, b'd80c186a03f423a81b39df39dc87fd269736ca86'),
  542. }
  543. _SORTED_TREE_ITEMS = [
  544. TreeEntry(b'a.c', 0o100755, b'd80c186a03f423a81b39df39dc87fd269736ca86'),
  545. TreeEntry(b'a', stat.S_IFDIR, b'd80c186a03f423a81b39df39dc87fd269736ca86'),
  546. TreeEntry(b'a/c', stat.S_IFDIR, b'd80c186a03f423a81b39df39dc87fd269736ca86'),
  547. ]
  548. class TreeTests(ShaFileCheckTests):
  549. def test_add(self):
  550. myhexsha = b'd80c186a03f423a81b39df39dc87fd269736ca86'
  551. x = Tree()
  552. x.add(b'myname', 0o100755, myhexsha)
  553. self.assertEqual(x[b'myname'], (0o100755, myhexsha))
  554. self.assertEqual(b'100755 myname\0' + hex_to_sha(myhexsha),
  555. x.as_raw_string())
  556. def test_add_old_order(self):
  557. myhexsha = b'd80c186a03f423a81b39df39dc87fd269736ca86'
  558. x = Tree()
  559. warnings.simplefilter("ignore", DeprecationWarning)
  560. try:
  561. x.add(0o100755, b'myname', myhexsha)
  562. finally:
  563. warnings.resetwarnings()
  564. self.assertEqual(x[b'myname'], (0o100755, myhexsha))
  565. self.assertEqual(b'100755 myname\0' + hex_to_sha(myhexsha),
  566. x.as_raw_string())
  567. def test_simple(self):
  568. myhexsha = b'd80c186a03f423a81b39df39dc87fd269736ca86'
  569. x = Tree()
  570. x[b'myname'] = (0o100755, myhexsha)
  571. self.assertEqual(b'100755 myname\0' + hex_to_sha(myhexsha),
  572. x.as_raw_string())
  573. def test_tree_update_id(self):
  574. x = Tree()
  575. x[b'a.c'] = (0o100755, b'd80c186a03f423a81b39df39dc87fd269736ca86')
  576. self.assertEqual(b'0c5c6bc2c081accfbc250331b19e43b904ab9cdd', x.id)
  577. x[b'a.b'] = (stat.S_IFDIR, b'd80c186a03f423a81b39df39dc87fd269736ca86')
  578. self.assertEqual(b'07bfcb5f3ada15bbebdfa3bbb8fd858a363925c8', x.id)
  579. def test_tree_iteritems_dir_sort(self):
  580. x = Tree()
  581. for name, item in _TREE_ITEMS.items():
  582. x[name] = item
  583. self.assertEqual(_SORTED_TREE_ITEMS, x.items())
  584. def test_tree_items_dir_sort(self):
  585. x = Tree()
  586. for name, item in _TREE_ITEMS.items():
  587. x[name] = item
  588. self.assertEqual(_SORTED_TREE_ITEMS, x.items())
  589. def _do_test_parse_tree(self, parse_tree):
  590. dir = os.path.join(os.path.dirname(__file__), 'data', 'trees')
  591. o = Tree.from_path(hex_to_filename(dir, tree_sha))
  592. self.assertEqual([(b'a', 0o100644, a_sha), (b'b', 0o100644, b_sha)],
  593. list(parse_tree(o.as_raw_string())))
  594. # test a broken tree that has a leading 0 on the file mode
  595. broken_tree = b'0100644 foo\0' + hex_to_sha(a_sha)
  596. def eval_parse_tree(*args, **kwargs):
  597. return list(parse_tree(*args, **kwargs))
  598. self.assertEqual([(b'foo', 0o100644, a_sha)],
  599. eval_parse_tree(broken_tree))
  600. self.assertRaises(ObjectFormatException,
  601. eval_parse_tree, broken_tree, strict=True)
  602. test_parse_tree = functest_builder(_do_test_parse_tree, _parse_tree_py)
  603. test_parse_tree_extension = ext_functest_builder(_do_test_parse_tree,
  604. parse_tree)
  605. def _do_test_sorted_tree_items(self, sorted_tree_items):
  606. def do_sort(entries):
  607. return list(sorted_tree_items(entries, False))
  608. actual = do_sort(_TREE_ITEMS)
  609. self.assertEqual(_SORTED_TREE_ITEMS, actual)
  610. self.assertTrue(isinstance(actual[0], TreeEntry))
  611. # C/Python implementations may differ in specific error types, but
  612. # should all error on invalid inputs.
  613. # For example, the C implementation has stricter type checks, so may
  614. # raise TypeError where the Python implementation raises AttributeError.
  615. errors = (TypeError, ValueError, AttributeError)
  616. self.assertRaises(errors, do_sort, b'foo')
  617. self.assertRaises(errors, do_sort, {b'foo': (1, 2, 3)})
  618. myhexsha = b'd80c186a03f423a81b39df39dc87fd269736ca86'
  619. self.assertRaises(errors, do_sort, {b'foo': (b'xxx', myhexsha)})
  620. self.assertRaises(errors, do_sort, {b'foo': (0o100755, 12345)})
  621. test_sorted_tree_items = functest_builder(_do_test_sorted_tree_items,
  622. _sorted_tree_items_py)
  623. test_sorted_tree_items_extension = ext_functest_builder(
  624. _do_test_sorted_tree_items, sorted_tree_items)
  625. def _do_test_sorted_tree_items_name_order(self, sorted_tree_items):
  626. self.assertEqual([
  627. TreeEntry(b'a', stat.S_IFDIR,
  628. b'd80c186a03f423a81b39df39dc87fd269736ca86'),
  629. TreeEntry(b'a.c', 0o100755,
  630. b'd80c186a03f423a81b39df39dc87fd269736ca86'),
  631. TreeEntry(b'a/c', stat.S_IFDIR,
  632. b'd80c186a03f423a81b39df39dc87fd269736ca86'),
  633. ], list(sorted_tree_items(_TREE_ITEMS, True)))
  634. test_sorted_tree_items_name_order = functest_builder(
  635. _do_test_sorted_tree_items_name_order, _sorted_tree_items_py)
  636. test_sorted_tree_items_name_order_extension = ext_functest_builder(
  637. _do_test_sorted_tree_items_name_order, sorted_tree_items)
  638. def test_check(self):
  639. t = Tree
  640. sha = hex_to_sha(a_sha)
  641. # filenames
  642. self.assertCheckSucceeds(t, b'100644 .a\0' + sha)
  643. self.assertCheckFails(t, b'100644 \0' + sha)
  644. self.assertCheckFails(t, b'100644 .\0' + sha)
  645. self.assertCheckFails(t, b'100644 a/a\0' + sha)
  646. self.assertCheckFails(t, b'100644 ..\0' + sha)
  647. # modes
  648. self.assertCheckSucceeds(t, b'100644 a\0' + sha)
  649. self.assertCheckSucceeds(t, b'100755 a\0' + sha)
  650. self.assertCheckSucceeds(t, b'160000 a\0' + sha)
  651. # TODO more whitelisted modes
  652. self.assertCheckFails(t, b'123456 a\0' + sha)
  653. self.assertCheckFails(t, b'123abc a\0' + sha)
  654. # should fail check, but parses ok
  655. self.assertCheckFails(t, b'0100644 foo\0' + sha)
  656. # shas
  657. self.assertCheckFails(t, b'100644 a\0' + (b'x' * 5))
  658. self.assertCheckFails(t, b'100644 a\0' + (b'x' * 18) + b'\0')
  659. self.assertCheckFails(t, b'100644 a\0' + (b'x' * 21) + b'\n100644 b\0' + sha)
  660. # ordering
  661. sha2 = hex_to_sha(b_sha)
  662. self.assertCheckSucceeds(t, b'100644 a\0' + sha + b'\n100644 b\0' + sha)
  663. self.assertCheckSucceeds(t, b'100644 a\0' + sha + b'\n100644 b\0' + sha2)
  664. self.assertCheckFails(t, b'100644 a\0' + sha + b'\n100755 a\0' + sha2)
  665. self.assertCheckFails(t, b'100644 b\0' + sha2 + b'\n100644 a\0' + sha)
  666. def test_iter(self):
  667. t = Tree()
  668. t[b'foo'] = (0o100644, a_sha)
  669. self.assertEqual(set([b'foo']), set(t))
  670. class TagSerializeTests(TestCase):
  671. def test_serialize_simple(self):
  672. x = make_object(Tag,
  673. tagger=b'Jelmer Vernooij <jelmer@samba.org>',
  674. name=b'0.1',
  675. message=b'Tag 0.1',
  676. object=(Blob, b'd80c186a03f423a81b39df39dc87fd269736ca86'),
  677. tag_time=423423423,
  678. tag_timezone=0)
  679. self.assertEqual((b'object d80c186a03f423a81b39df39dc87fd269736ca86\n'
  680. b'type blob\n'
  681. b'tag 0.1\n'
  682. b'tagger Jelmer Vernooij <jelmer@samba.org> '
  683. b'423423423 +0000\n'
  684. b'\n'
  685. b'Tag 0.1'), x.as_raw_string())
  686. default_tagger = (b'Linus Torvalds <torvalds@woody.linux-foundation.org> '
  687. b'1183319674 -0700')
  688. default_message = b"""Linux 2.6.22-rc7
  689. -----BEGIN PGP SIGNATURE-----
  690. Version: GnuPG v1.4.7 (GNU/Linux)
  691. iD8DBQBGiAaAF3YsRnbiHLsRAitMAKCiLboJkQECM/jpYsY3WPfvUgLXkACgg3ql
  692. OK2XeQOiEeXtT76rV4t2WR4=
  693. =ivrA
  694. -----END PGP SIGNATURE-----
  695. """
  696. class TagParseTests(ShaFileCheckTests):
  697. def make_tag_lines(self,
  698. object_sha=b'a38d6181ff27824c79fc7df825164a212eff6a3f',
  699. object_type_name=b'commit',
  700. name=b'v2.6.22-rc7',
  701. tagger=default_tagger,
  702. message=default_message):
  703. lines = []
  704. if object_sha is not None:
  705. lines.append(b'object ' + object_sha)
  706. if object_type_name is not None:
  707. lines.append(b'type ' + object_type_name)
  708. if name is not None:
  709. lines.append(b'tag ' + name)
  710. if tagger is not None:
  711. lines.append(b'tagger ' + tagger)
  712. lines.append(b'')
  713. if message is not None:
  714. lines.append(message)
  715. return lines
  716. def make_tag_text(self, **kwargs):
  717. return b'\n'.join(self.make_tag_lines(**kwargs))
  718. def test_parse(self):
  719. x = Tag()
  720. x.set_raw_string(self.make_tag_text())
  721. self.assertEqual(
  722. b'Linus Torvalds <torvalds@woody.linux-foundation.org>', x.tagger)
  723. self.assertEqual(b'v2.6.22-rc7', x.name)
  724. object_type, object_sha = x.object
  725. self.assertEqual(b'a38d6181ff27824c79fc7df825164a212eff6a3f',
  726. object_sha)
  727. self.assertEqual(Commit, object_type)
  728. self.assertEqual(datetime.datetime.utcfromtimestamp(x.tag_time),
  729. datetime.datetime(2007, 7, 1, 19, 54, 34))
  730. self.assertEqual(-25200, x.tag_timezone)
  731. def test_parse_no_tagger(self):
  732. x = Tag()
  733. x.set_raw_string(self.make_tag_text(tagger=None))
  734. self.assertEqual(None, x.tagger)
  735. self.assertEqual(b'v2.6.22-rc7', x.name)
  736. def test_check(self):
  737. self.assertCheckSucceeds(Tag, self.make_tag_text())
  738. self.assertCheckFails(Tag, self.make_tag_text(object_sha=None))
  739. self.assertCheckFails(Tag, self.make_tag_text(object_type_name=None))
  740. self.assertCheckFails(Tag, self.make_tag_text(name=None))
  741. self.assertCheckFails(Tag, self.make_tag_text(name=b''))
  742. self.assertCheckFails(Tag, self.make_tag_text(
  743. object_type_name=b'foobar'))
  744. self.assertCheckFails(Tag, self.make_tag_text(
  745. tagger=b'some guy without an email address 1183319674 -0700'))
  746. self.assertCheckFails(Tag, self.make_tag_text(
  747. tagger=(b'Linus Torvalds <torvalds@woody.linux-foundation.org> '
  748. b'Sun 7 Jul 2007 12:54:34 +0700')))
  749. self.assertCheckFails(Tag, self.make_tag_text(object_sha=b'xxx'))
  750. def test_check_duplicates(self):
  751. # duplicate each of the header fields
  752. for i in range(4):
  753. lines = self.make_tag_lines()
  754. lines.insert(i, lines[i])
  755. self.assertCheckFails(Tag, b'\n'.join(lines))
  756. def test_check_order(self):
  757. lines = self.make_tag_lines()
  758. headers = lines[:4]
  759. rest = lines[4:]
  760. # of all possible permutations, ensure only the original succeeds
  761. for perm in permutations(headers):
  762. perm = list(perm)
  763. text = b'\n'.join(perm + rest)
  764. if perm == headers:
  765. self.assertCheckSucceeds(Tag, text)
  766. else:
  767. self.assertCheckFails(Tag, text)
  768. class CheckTests(TestCase):
  769. def test_check_hexsha(self):
  770. check_hexsha(a_sha, "failed to check good sha")
  771. self.assertRaises(ObjectFormatException, check_hexsha, b'1' * 39,
  772. 'sha too short')
  773. self.assertRaises(ObjectFormatException, check_hexsha, b'1' * 41,
  774. 'sha too long')
  775. self.assertRaises(ObjectFormatException, check_hexsha, b'x' * 40,
  776. 'invalid characters')
  777. def test_check_identity(self):
  778. check_identity(b'Dave Borowitz <dborowitz@google.com>',
  779. "failed to check good identity")
  780. check_identity(b'<dborowitz@google.com>',
  781. "failed to check good identity")
  782. self.assertRaises(ObjectFormatException, check_identity,
  783. b'Dave Borowitz', "no email")
  784. self.assertRaises(ObjectFormatException, check_identity,
  785. b'Dave Borowitz <dborowitz', "incomplete email")
  786. self.assertRaises(ObjectFormatException, check_identity,
  787. b'dborowitz@google.com>', "incomplete email")
  788. self.assertRaises(ObjectFormatException, check_identity,
  789. b'Dave Borowitz <<dborowitz@google.com>', "typo")
  790. self.assertRaises(ObjectFormatException, check_identity,
  791. b'Dave Borowitz <dborowitz@google.com>>', "typo")
  792. self.assertRaises(ObjectFormatException, check_identity,
  793. b'Dave Borowitz <dborowitz@google.com>xxx',
  794. "trailing characters")
  795. class TimezoneTests(TestCase):
  796. def test_parse_timezone_utc(self):
  797. self.assertEqual((0, False), parse_timezone(b'+0000'))
  798. def test_parse_timezone_utc_negative(self):
  799. self.assertEqual((0, True), parse_timezone(b'-0000'))
  800. def test_generate_timezone_utc(self):
  801. self.assertEqual(b'+0000', format_timezone(0))
  802. def test_generate_timezone_utc_negative(self):
  803. self.assertEqual(b'-0000', format_timezone(0, True))
  804. def test_parse_timezone_cet(self):
  805. self.assertEqual((60 * 60, False), parse_timezone(b'+0100'))
  806. def test_format_timezone_cet(self):
  807. self.assertEqual(b'+0100', format_timezone(60 * 60))
  808. def test_format_timezone_pdt(self):
  809. self.assertEqual(b'-0400', format_timezone(-4 * 60 * 60))
  810. def test_parse_timezone_pdt(self):
  811. self.assertEqual((-4 * 60 * 60, False), parse_timezone(b'-0400'))
  812. def test_format_timezone_pdt_half(self):
  813. self.assertEqual(b'-0440',
  814. format_timezone(int(((-4 * 60) - 40) * 60)))
  815. def test_format_timezone_double_negative(self):
  816. self.assertEqual(b'--700',
  817. format_timezone(int(((7 * 60)) * 60), True))
  818. def test_parse_timezone_pdt_half(self):
  819. self.assertEqual((((-4 * 60) - 40) * 60, False),
  820. parse_timezone(b'-0440'))
  821. def test_parse_timezone_double_negative(self):
  822. self.assertEqual(
  823. (int(((7 * 60)) * 60), False), parse_timezone(b'+700'))
  824. self.assertEqual(
  825. (int(((7 * 60)) * 60), True), parse_timezone(b'--700'))
  826. class ShaFileCopyTests(TestCase):
  827. def assert_copy(self, orig):
  828. oclass = object_class(orig.type_num)
  829. copy = orig.copy()
  830. self.assertTrue(isinstance(copy, oclass))
  831. self.assertEqual(copy, orig)
  832. self.assertTrue(copy is not orig)
  833. def test_commit_copy(self):
  834. attrs = {'tree': b'd80c186a03f423a81b39df39dc87fd269736ca86',
  835. 'parents': [b'ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd',
  836. b'4cffe90e0a41ad3f5190079d7c8f036bde29cbe6'],
  837. 'author': b'James Westby <jw+debian@jameswestby.net>',
  838. 'committer': b'James Westby <jw+debian@jameswestby.net>',
  839. 'commit_time': 1174773719,
  840. 'author_time': 1174773719,
  841. 'commit_timezone': 0,
  842. 'author_timezone': 0,
  843. 'message': b'Merge ../b\n'}
  844. commit = make_commit(**attrs)
  845. self.assert_copy(commit)
  846. def test_blob_copy(self):
  847. blob = make_object(Blob, data=b'i am a blob')
  848. self.assert_copy(blob)
  849. def test_tree_copy(self):
  850. blob = make_object(Blob, data=b'i am a blob')
  851. tree = Tree()
  852. tree[b'blob'] = (stat.S_IFREG, blob.id)
  853. self.assert_copy(tree)
  854. def test_tag_copy(self):
  855. tag = make_object(
  856. Tag, name=b'tag', message=b'',
  857. tagger=b'Tagger <test@example.com>',
  858. tag_time=12345, tag_timezone=0,
  859. object=(Commit, b'0' * 40))
  860. self.assert_copy(tag)