2
0

test_objects.py 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450
  1. # test_objects.py -- tests for objects.py
  2. # Copyright (C) 2007 James Westby <jw+debian@jameswestby.net>
  3. #
  4. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  5. # General Public License as public by the Free Software Foundation; version 2.0
  6. # or (at your option) any later version. You can redistribute it and/or
  7. # modify it under the terms of either of these two licenses.
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # You should have received a copy of the licenses; if not, see
  16. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  17. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  18. # License, Version 2.0.
  19. #
  20. """Tests for git base objects."""
  21. # TODO: Round-trip parse-serialize-parse and serialize-parse-serialize tests.
  22. import datetime
  23. import os
  24. import stat
  25. from contextlib import contextmanager
  26. from io import BytesIO
  27. from itertools import permutations
  28. from dulwich.errors import ObjectFormatException
  29. from dulwich.objects import (
  30. MAX_TIME,
  31. Blob,
  32. Commit,
  33. ShaFile,
  34. Tag,
  35. Tree,
  36. TreeEntry,
  37. _parse_tree_py,
  38. _sorted_tree_items_py,
  39. check_hexsha,
  40. check_identity,
  41. format_timezone,
  42. hex_to_filename,
  43. hex_to_sha,
  44. object_class,
  45. parse_timezone,
  46. parse_tree,
  47. pretty_format_tree_entry,
  48. sha_to_hex,
  49. sorted_tree_items,
  50. )
  51. from dulwich.tests.utils import (
  52. ext_functest_builder,
  53. functest_builder,
  54. make_commit,
  55. make_object,
  56. )
  57. from . import TestCase
  58. a_sha = b"6f670c0fb53f9463760b7295fbb814e965fb20c8"
  59. b_sha = b"2969be3e8ee1c0222396a5611407e4769f14e54b"
  60. c_sha = b"954a536f7819d40e6f637f849ee187dd10066349"
  61. tree_sha = b"70c190eb48fa8bbb50ddc692a17b44cb781af7f6"
  62. tag_sha = b"71033db03a03c6a36721efcf1968dd8f8e0cf023"
  63. class TestHexToSha(TestCase):
  64. def test_simple(self):
  65. self.assertEqual(b"\xab\xcd" * 10, hex_to_sha(b"abcd" * 10))
  66. def test_reverse(self):
  67. self.assertEqual(b"abcd" * 10, sha_to_hex(b"\xab\xcd" * 10))
  68. class BlobReadTests(TestCase):
  69. """Test decompression of blobs."""
  70. def get_sha_file(self, cls, base, sha):
  71. dir = os.path.join(os.path.dirname(__file__), "..", "testdata", base)
  72. return cls.from_path(hex_to_filename(dir, sha))
  73. def get_blob(self, sha):
  74. """Return the blob named sha from the test data dir."""
  75. return self.get_sha_file(Blob, "blobs", sha)
  76. def get_tree(self, sha):
  77. return self.get_sha_file(Tree, "trees", sha)
  78. def get_tag(self, sha):
  79. return self.get_sha_file(Tag, "tags", sha)
  80. def commit(self, sha):
  81. return self.get_sha_file(Commit, "commits", sha)
  82. def test_decompress_simple_blob(self):
  83. b = self.get_blob(a_sha)
  84. self.assertEqual(b.data, b"test 1\n")
  85. self.assertEqual(b.sha().hexdigest().encode("ascii"), a_sha)
  86. def test_hash(self):
  87. b = self.get_blob(a_sha)
  88. self.assertEqual(hash(b.id), hash(b))
  89. def test_parse_empty_blob_object(self):
  90. sha = b"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"
  91. b = self.get_blob(sha)
  92. self.assertEqual(b.data, b"")
  93. self.assertEqual(b.id, sha)
  94. self.assertEqual(b.sha().hexdigest().encode("ascii"), sha)
  95. def test_create_blob_from_string(self):
  96. string = b"test 2\n"
  97. b = Blob.from_string(string)
  98. self.assertEqual(b.data, string)
  99. self.assertEqual(b.sha().hexdigest().encode("ascii"), b_sha)
  100. def test_legacy_from_file(self):
  101. b1 = Blob.from_string(b"foo")
  102. b_raw = b1.as_legacy_object()
  103. b2 = b1.from_file(BytesIO(b_raw))
  104. self.assertEqual(b1, b2)
  105. def test_legacy_from_file_compression_level(self):
  106. b1 = Blob.from_string(b"foo")
  107. b_raw = b1.as_legacy_object(compression_level=6)
  108. b2 = b1.from_file(BytesIO(b_raw))
  109. self.assertEqual(b1, b2)
  110. def test_chunks(self):
  111. string = b"test 5\n"
  112. b = Blob.from_string(string)
  113. self.assertEqual([string], b.chunked)
  114. def test_splitlines(self):
  115. for case in [
  116. [],
  117. [b"foo\nbar\n"],
  118. [b"bl\na", b"blie"],
  119. [b"bl\na", b"blie", b"bloe\n"],
  120. [b"", b"bl\na", b"blie", b"bloe\n"],
  121. [b"", b"", b"", b"bla\n"],
  122. [b"", b"", b"", b"bla\n", b""],
  123. [b"bl", b"", b"a\naaa"],
  124. [b"a\naaa", b"a"],
  125. ]:
  126. b = Blob()
  127. b.chunked = case
  128. self.assertEqual(b.data.splitlines(True), b.splitlines())
  129. def test_set_chunks(self):
  130. b = Blob()
  131. b.chunked = [b"te", b"st", b" 5\n"]
  132. self.assertEqual(b"test 5\n", b.data)
  133. b.chunked = [b"te", b"st", b" 6\n"]
  134. self.assertEqual(b"test 6\n", b.as_raw_string())
  135. self.assertEqual(b"test 6\n", bytes(b))
  136. def test_parse_legacy_blob(self):
  137. string = b"test 3\n"
  138. b = self.get_blob(c_sha)
  139. self.assertEqual(b.data, string)
  140. self.assertEqual(b.sha().hexdigest().encode("ascii"), c_sha)
  141. def test_eq(self):
  142. blob1 = self.get_blob(a_sha)
  143. blob2 = self.get_blob(a_sha)
  144. self.assertEqual(blob1, blob2)
  145. def test_read_tree_from_file(self):
  146. t = self.get_tree(tree_sha)
  147. self.assertEqual(t.items()[0], (b"a", 33188, a_sha))
  148. self.assertEqual(t.items()[1], (b"b", 33188, b_sha))
  149. def test_read_tree_from_file_parse_count(self):
  150. old_deserialize = Tree._deserialize
  151. def reset_deserialize():
  152. Tree._deserialize = old_deserialize
  153. self.addCleanup(reset_deserialize)
  154. self.deserialize_count = 0
  155. def counting_deserialize(*args, **kwargs):
  156. self.deserialize_count += 1
  157. return old_deserialize(*args, **kwargs)
  158. Tree._deserialize = counting_deserialize
  159. t = self.get_tree(tree_sha)
  160. self.assertEqual(t.items()[0], (b"a", 33188, a_sha))
  161. self.assertEqual(t.items()[1], (b"b", 33188, b_sha))
  162. self.assertEqual(self.deserialize_count, 1)
  163. def test_read_tag_from_file(self):
  164. t = self.get_tag(tag_sha)
  165. self.assertEqual(
  166. t.object, (Commit, b"51b668fd5bf7061b7d6fa525f88803e6cfadaa51")
  167. )
  168. self.assertEqual(t.name, b"signed")
  169. self.assertEqual(t.tagger, b"Ali Sabil <ali.sabil@gmail.com>")
  170. self.assertEqual(t.tag_time, 1231203091)
  171. self.assertEqual(t.message, b"This is a signed tag\n")
  172. self.assertEqual(
  173. t.signature,
  174. b"-----BEGIN PGP SIGNATURE-----\n"
  175. b"Version: GnuPG v1.4.9 (GNU/Linux)\n"
  176. b"\n"
  177. b"iEYEABECAAYFAkliqx8ACgkQqSMmLy9u/"
  178. b"kcx5ACfakZ9NnPl02tOyYP6pkBoEkU1\n"
  179. b"5EcAn0UFgokaSvS371Ym/4W9iJj6vh3h\n"
  180. b"=ql7y\n"
  181. b"-----END PGP SIGNATURE-----\n",
  182. )
  183. def test_read_commit_from_file(self):
  184. sha = b"60dacdc733de308bb77bb76ce0fb0f9b44c9769e"
  185. c = self.commit(sha)
  186. self.assertEqual(c.tree, tree_sha)
  187. self.assertEqual(c.parents, [b"0d89f20333fbb1d2f3a94da77f4981373d8f4310"])
  188. self.assertEqual(c.author, b"James Westby <jw+debian@jameswestby.net>")
  189. self.assertEqual(c.committer, b"James Westby <jw+debian@jameswestby.net>")
  190. self.assertEqual(c.commit_time, 1174759230)
  191. self.assertEqual(c.commit_timezone, 0)
  192. self.assertEqual(c.author_timezone, 0)
  193. self.assertEqual(c.message, b"Test commit\n")
  194. def test_read_commit_no_parents(self):
  195. sha = b"0d89f20333fbb1d2f3a94da77f4981373d8f4310"
  196. c = self.commit(sha)
  197. self.assertEqual(c.tree, b"90182552c4a85a45ec2a835cadc3451bebdfe870")
  198. self.assertEqual(c.parents, [])
  199. self.assertEqual(c.author, b"James Westby <jw+debian@jameswestby.net>")
  200. self.assertEqual(c.committer, b"James Westby <jw+debian@jameswestby.net>")
  201. self.assertEqual(c.commit_time, 1174758034)
  202. self.assertEqual(c.commit_timezone, 0)
  203. self.assertEqual(c.author_timezone, 0)
  204. self.assertEqual(c.message, b"Test commit\n")
  205. def test_read_commit_two_parents(self):
  206. sha = b"5dac377bdded4c9aeb8dff595f0faeebcc8498cc"
  207. c = self.commit(sha)
  208. self.assertEqual(c.tree, b"d80c186a03f423a81b39df39dc87fd269736ca86")
  209. self.assertEqual(
  210. c.parents,
  211. [
  212. b"ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd",
  213. b"4cffe90e0a41ad3f5190079d7c8f036bde29cbe6",
  214. ],
  215. )
  216. self.assertEqual(c.author, b"James Westby <jw+debian@jameswestby.net>")
  217. self.assertEqual(c.committer, b"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, b"Merge ../b\n")
  222. def test_stub_sha(self):
  223. sha = b"5" * 40
  224. c = make_commit(id=sha, message=b"foo")
  225. self.assertIsInstance(c, Commit)
  226. self.assertEqual(sha, c.id)
  227. self.assertNotEqual(sha, c.sha())
  228. class ShaFileCheckTests(TestCase):
  229. def assertCheckFails(self, cls, data):
  230. obj = cls()
  231. def do_check():
  232. obj.set_raw_string(data)
  233. obj.check()
  234. self.assertRaises(ObjectFormatException, do_check)
  235. def assertCheckSucceeds(self, cls, data):
  236. obj = cls()
  237. obj.set_raw_string(data)
  238. self.assertEqual(None, obj.check())
  239. small_buffer_zlib_object = (
  240. b"\x48\x89\x15\xcc\x31\x0e\xc2\x30\x0c\x40\x51\xe6"
  241. b"\x9c\xc2\x3b\xaa\x64\x37\xc4\xc1\x12\x42\x5c\xc5"
  242. b"\x49\xac\x52\xd4\x92\xaa\x78\xe1\xf6\x94\xed\xeb"
  243. b"\x0d\xdf\x75\x02\xa2\x7c\xea\xe5\x65\xd5\x81\x8b"
  244. b"\x9a\x61\xba\xa0\xa9\x08\x36\xc9\x4c\x1a\xad\x88"
  245. b"\x16\xba\x46\xc4\xa8\x99\x6a\x64\xe1\xe0\xdf\xcd"
  246. b"\xa0\xf6\x75\x9d\x3d\xf8\xf1\xd0\x77\xdb\xfb\xdc"
  247. b"\x86\xa3\x87\xf1\x2f\x93\xed\x00\xb7\xc7\xd2\xab"
  248. b"\x2e\xcf\xfe\xf1\x3b\x50\xa4\x91\x53\x12\x24\x38"
  249. b"\x23\x21\x86\xf0\x03\x2f\x91\x24\x52"
  250. )
  251. class ShaFileTests(TestCase):
  252. def test_deflated_smaller_window_buffer(self):
  253. # zlib on some systems uses smaller buffers,
  254. # resulting in a different header.
  255. # See https://github.com/libgit2/libgit2/pull/464
  256. sf = ShaFile.from_file(BytesIO(small_buffer_zlib_object))
  257. self.assertEqual(sf.type_name, b"tag")
  258. self.assertEqual(sf.tagger, b" <@localhost>")
  259. class CommitSerializationTests(TestCase):
  260. def make_commit(self, **kwargs):
  261. attrs = {
  262. "tree": b"d80c186a03f423a81b39df39dc87fd269736ca86",
  263. "parents": [
  264. b"ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd",
  265. b"4cffe90e0a41ad3f5190079d7c8f036bde29cbe6",
  266. ],
  267. "author": b"James Westby <jw+debian@jameswestby.net>",
  268. "committer": b"James Westby <jw+debian@jameswestby.net>",
  269. "commit_time": 1174773719,
  270. "author_time": 1174773719,
  271. "commit_timezone": 0,
  272. "author_timezone": 0,
  273. "message": b"Merge ../b\n",
  274. }
  275. attrs.update(kwargs)
  276. return make_commit(**attrs)
  277. def test_encoding(self):
  278. c = self.make_commit(encoding=b"iso8859-1")
  279. self.assertIn(b"encoding iso8859-1\n", c.as_raw_string())
  280. def test_short_timestamp(self):
  281. c = self.make_commit(commit_time=30)
  282. c1 = Commit()
  283. c1.set_raw_string(c.as_raw_string())
  284. self.assertEqual(30, c1.commit_time)
  285. def test_full_tree(self):
  286. c = self.make_commit(commit_time=30)
  287. t = Tree()
  288. t.add(b"data-x", 0o644, Blob().id)
  289. c.tree = t
  290. c1 = Commit()
  291. c1.set_raw_string(c.as_raw_string())
  292. self.assertEqual(t.id, c1.tree)
  293. self.assertEqual(c.as_raw_string(), c1.as_raw_string())
  294. def test_raw_length(self):
  295. c = self.make_commit()
  296. self.assertEqual(len(c.as_raw_string()), c.raw_length())
  297. def test_simple(self):
  298. c = self.make_commit()
  299. self.assertEqual(c.id, b"5dac377bdded4c9aeb8dff595f0faeebcc8498cc")
  300. self.assertEqual(
  301. b"tree d80c186a03f423a81b39df39dc87fd269736ca86\n"
  302. b"parent ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd\n"
  303. b"parent 4cffe90e0a41ad3f5190079d7c8f036bde29cbe6\n"
  304. b"author James Westby <jw+debian@jameswestby.net> "
  305. b"1174773719 +0000\n"
  306. b"committer James Westby <jw+debian@jameswestby.net> "
  307. b"1174773719 +0000\n"
  308. b"\n"
  309. b"Merge ../b\n",
  310. c.as_raw_string(),
  311. )
  312. def test_timezone(self):
  313. c = self.make_commit(commit_timezone=(5 * 60))
  314. self.assertIn(b" +0005\n", c.as_raw_string())
  315. def test_neg_timezone(self):
  316. c = self.make_commit(commit_timezone=(-1 * 3600))
  317. self.assertIn(b" -0100\n", c.as_raw_string())
  318. def test_deserialize(self):
  319. c = self.make_commit()
  320. d = Commit()
  321. d._deserialize(c.as_raw_chunks())
  322. self.assertEqual(c, d)
  323. def test_serialize_gpgsig(self):
  324. commit = self.make_commit(
  325. gpgsig=b"""-----BEGIN PGP SIGNATURE-----
  326. Version: GnuPG v1
  327. iQIcBAABCgAGBQJULCdfAAoJEACAbyvXKaRXuKwP/RyP9PA49uAvu8tQVCC/uBa8
  328. vi975+xvO14R8Pp8k2nps7lSxCdtCd+xVT1VRHs0wNhOZo2YCVoU1HATkPejqSeV
  329. NScTHcxnk4/+bxyfk14xvJkNp7FlQ3npmBkA+lbV0Ubr33rvtIE5jiJPyz+SgWAg
  330. xdBG2TojV0squj00GoH/euK6aX7GgZtwdtpTv44haCQdSuPGDcI4TORqR6YSqvy3
  331. GPE+3ZqXPFFb+KILtimkxitdwB7CpwmNse2vE3rONSwTvi8nq3ZoQYNY73CQGkUy
  332. qoFU0pDtw87U3niFin1ZccDgH0bB6624sLViqrjcbYJeg815Htsu4rmzVaZADEVC
  333. XhIO4MThebusdk0AcNGjgpf3HRHk0DPMDDlIjm+Oao0cqovvF6VyYmcb0C+RmhJj
  334. dodLXMNmbqErwTk3zEkW0yZvNIYXH7m9SokPCZa4eeIM7be62X6h1mbt0/IU6Th+
  335. v18fS0iTMP/Viug5und+05C/v04kgDo0CPphAbXwWMnkE4B6Tl9sdyUYXtvQsL7x
  336. 0+WP1gL27ANqNZiI07Kz/BhbBAQI/+2TFT7oGr0AnFPQ5jHp+3GpUf6OKuT1wT3H
  337. ND189UFuRuubxb42vZhpcXRbqJVWnbECTKVUPsGZqat3enQUB63uM4i6/RdONDZA
  338. fDeF1m4qYs+cUXKNUZ03
  339. =X6RT
  340. -----END PGP SIGNATURE-----"""
  341. )
  342. self.maxDiff = None
  343. self.assertEqual(
  344. b"""\
  345. tree d80c186a03f423a81b39df39dc87fd269736ca86
  346. parent ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd
  347. parent 4cffe90e0a41ad3f5190079d7c8f036bde29cbe6
  348. author James Westby <jw+debian@jameswestby.net> 1174773719 +0000
  349. committer James Westby <jw+debian@jameswestby.net> 1174773719 +0000
  350. gpgsig -----BEGIN PGP SIGNATURE-----
  351. Version: GnuPG v1
  352. iQIcBAABCgAGBQJULCdfAAoJEACAbyvXKaRXuKwP/RyP9PA49uAvu8tQVCC/uBa8
  353. vi975+xvO14R8Pp8k2nps7lSxCdtCd+xVT1VRHs0wNhOZo2YCVoU1HATkPejqSeV
  354. NScTHcxnk4/+bxyfk14xvJkNp7FlQ3npmBkA+lbV0Ubr33rvtIE5jiJPyz+SgWAg
  355. xdBG2TojV0squj00GoH/euK6aX7GgZtwdtpTv44haCQdSuPGDcI4TORqR6YSqvy3
  356. GPE+3ZqXPFFb+KILtimkxitdwB7CpwmNse2vE3rONSwTvi8nq3ZoQYNY73CQGkUy
  357. qoFU0pDtw87U3niFin1ZccDgH0bB6624sLViqrjcbYJeg815Htsu4rmzVaZADEVC
  358. XhIO4MThebusdk0AcNGjgpf3HRHk0DPMDDlIjm+Oao0cqovvF6VyYmcb0C+RmhJj
  359. dodLXMNmbqErwTk3zEkW0yZvNIYXH7m9SokPCZa4eeIM7be62X6h1mbt0/IU6Th+
  360. v18fS0iTMP/Viug5und+05C/v04kgDo0CPphAbXwWMnkE4B6Tl9sdyUYXtvQsL7x
  361. 0+WP1gL27ANqNZiI07Kz/BhbBAQI/+2TFT7oGr0AnFPQ5jHp+3GpUf6OKuT1wT3H
  362. ND189UFuRuubxb42vZhpcXRbqJVWnbECTKVUPsGZqat3enQUB63uM4i6/RdONDZA
  363. fDeF1m4qYs+cUXKNUZ03
  364. =X6RT
  365. -----END PGP SIGNATURE-----
  366. Merge ../b
  367. """,
  368. commit.as_raw_string(),
  369. )
  370. def test_serialize_mergetag(self):
  371. tag = make_object(
  372. Tag,
  373. object=(Commit, b"a38d6181ff27824c79fc7df825164a212eff6a3f"),
  374. object_type_name=b"commit",
  375. name=b"v2.6.22-rc7",
  376. tag_time=1183319674,
  377. tag_timezone=0,
  378. tagger=b"Linus Torvalds <torvalds@woody.linux-foundation.org>",
  379. message=default_message,
  380. )
  381. commit = self.make_commit(mergetag=[tag])
  382. self.assertEqual(
  383. b"""tree d80c186a03f423a81b39df39dc87fd269736ca86
  384. parent ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd
  385. parent 4cffe90e0a41ad3f5190079d7c8f036bde29cbe6
  386. author James Westby <jw+debian@jameswestby.net> 1174773719 +0000
  387. committer James Westby <jw+debian@jameswestby.net> 1174773719 +0000
  388. mergetag object a38d6181ff27824c79fc7df825164a212eff6a3f
  389. type commit
  390. tag v2.6.22-rc7
  391. tagger Linus Torvalds <torvalds@woody.linux-foundation.org> 1183319674 +0000
  392. Linux 2.6.22-rc7
  393. -----BEGIN PGP SIGNATURE-----
  394. Version: GnuPG v1.4.7 (GNU/Linux)
  395. iD8DBQBGiAaAF3YsRnbiHLsRAitMAKCiLboJkQECM/jpYsY3WPfvUgLXkACgg3ql
  396. OK2XeQOiEeXtT76rV4t2WR4=
  397. =ivrA
  398. -----END PGP SIGNATURE-----
  399. Merge ../b
  400. """,
  401. commit.as_raw_string(),
  402. )
  403. def test_serialize_mergetags(self):
  404. tag = make_object(
  405. Tag,
  406. object=(Commit, b"a38d6181ff27824c79fc7df825164a212eff6a3f"),
  407. object_type_name=b"commit",
  408. name=b"v2.6.22-rc7",
  409. tag_time=1183319674,
  410. tag_timezone=0,
  411. tagger=b"Linus Torvalds <torvalds@woody.linux-foundation.org>",
  412. message=default_message,
  413. )
  414. commit = self.make_commit(mergetag=[tag, tag])
  415. self.assertEqual(
  416. b"""tree d80c186a03f423a81b39df39dc87fd269736ca86
  417. parent ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd
  418. parent 4cffe90e0a41ad3f5190079d7c8f036bde29cbe6
  419. author James Westby <jw+debian@jameswestby.net> 1174773719 +0000
  420. committer James Westby <jw+debian@jameswestby.net> 1174773719 +0000
  421. mergetag object a38d6181ff27824c79fc7df825164a212eff6a3f
  422. type commit
  423. tag v2.6.22-rc7
  424. tagger Linus Torvalds <torvalds@woody.linux-foundation.org> 1183319674 +0000
  425. Linux 2.6.22-rc7
  426. -----BEGIN PGP SIGNATURE-----
  427. Version: GnuPG v1.4.7 (GNU/Linux)
  428. iD8DBQBGiAaAF3YsRnbiHLsRAitMAKCiLboJkQECM/jpYsY3WPfvUgLXkACgg3ql
  429. OK2XeQOiEeXtT76rV4t2WR4=
  430. =ivrA
  431. -----END PGP SIGNATURE-----
  432. mergetag object a38d6181ff27824c79fc7df825164a212eff6a3f
  433. type commit
  434. tag v2.6.22-rc7
  435. tagger Linus Torvalds <torvalds@woody.linux-foundation.org> 1183319674 +0000
  436. Linux 2.6.22-rc7
  437. -----BEGIN PGP SIGNATURE-----
  438. Version: GnuPG v1.4.7 (GNU/Linux)
  439. iD8DBQBGiAaAF3YsRnbiHLsRAitMAKCiLboJkQECM/jpYsY3WPfvUgLXkACgg3ql
  440. OK2XeQOiEeXtT76rV4t2WR4=
  441. =ivrA
  442. -----END PGP SIGNATURE-----
  443. Merge ../b
  444. """,
  445. commit.as_raw_string(),
  446. )
  447. def test_deserialize_mergetag(self):
  448. tag = make_object(
  449. Tag,
  450. object=(Commit, b"a38d6181ff27824c79fc7df825164a212eff6a3f"),
  451. object_type_name=b"commit",
  452. name=b"v2.6.22-rc7",
  453. tag_time=1183319674,
  454. tag_timezone=0,
  455. tagger=b"Linus Torvalds <torvalds@woody.linux-foundation.org>",
  456. message=default_message,
  457. )
  458. commit = self.make_commit(mergetag=[tag])
  459. d = Commit()
  460. d._deserialize(commit.as_raw_chunks())
  461. self.assertEqual(commit, d)
  462. def test_deserialize_mergetags(self):
  463. tag = make_object(
  464. Tag,
  465. object=(Commit, b"a38d6181ff27824c79fc7df825164a212eff6a3f"),
  466. object_type_name=b"commit",
  467. name=b"v2.6.22-rc7",
  468. tag_time=1183319674,
  469. tag_timezone=0,
  470. tagger=b"Linus Torvalds <torvalds@woody.linux-foundation.org>",
  471. message=default_message,
  472. )
  473. commit = self.make_commit(mergetag=[tag, tag])
  474. d = Commit()
  475. d._deserialize(commit.as_raw_chunks())
  476. self.assertEqual(commit, d)
  477. default_committer = b"James Westby <jw+debian@jameswestby.net> 1174773719 +0000"
  478. class CommitParseTests(ShaFileCheckTests):
  479. def make_commit_lines(
  480. self,
  481. tree=b"d80c186a03f423a81b39df39dc87fd269736ca86",
  482. parents=[
  483. b"ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd",
  484. b"4cffe90e0a41ad3f5190079d7c8f036bde29cbe6",
  485. ],
  486. author=default_committer,
  487. committer=default_committer,
  488. encoding=None,
  489. message=b"Merge ../b\n",
  490. extra=None,
  491. ):
  492. lines = []
  493. if tree is not None:
  494. lines.append(b"tree " + tree)
  495. if parents is not None:
  496. lines.extend(b"parent " + p for p in parents)
  497. if author is not None:
  498. lines.append(b"author " + author)
  499. if committer is not None:
  500. lines.append(b"committer " + committer)
  501. if encoding is not None:
  502. lines.append(b"encoding " + encoding)
  503. if extra is not None:
  504. for name, value in sorted(extra.items()):
  505. lines.append(name + b" " + value)
  506. lines.append(b"")
  507. if message is not None:
  508. lines.append(message)
  509. return lines
  510. def make_commit_text(self, **kwargs):
  511. return b"\n".join(self.make_commit_lines(**kwargs))
  512. def test_simple(self):
  513. c = Commit.from_string(self.make_commit_text())
  514. self.assertEqual(b"Merge ../b\n", c.message)
  515. self.assertEqual(b"James Westby <jw+debian@jameswestby.net>", c.author)
  516. self.assertEqual(b"James Westby <jw+debian@jameswestby.net>", c.committer)
  517. self.assertEqual(b"d80c186a03f423a81b39df39dc87fd269736ca86", c.tree)
  518. self.assertEqual(
  519. [
  520. b"ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd",
  521. b"4cffe90e0a41ad3f5190079d7c8f036bde29cbe6",
  522. ],
  523. c.parents,
  524. )
  525. expected_time = datetime.datetime(2007, 3, 24, 22, 1, 59)
  526. self.assertEqual(
  527. expected_time, datetime.datetime.utcfromtimestamp(c.commit_time)
  528. )
  529. self.assertEqual(0, c.commit_timezone)
  530. self.assertEqual(
  531. expected_time, datetime.datetime.utcfromtimestamp(c.author_time)
  532. )
  533. self.assertEqual(0, c.author_timezone)
  534. self.assertEqual(None, c.encoding)
  535. def test_custom(self):
  536. c = Commit.from_string(self.make_commit_text(extra={b"extra-field": b"data"}))
  537. self.assertEqual([(b"extra-field", b"data")], c._extra)
  538. def test_encoding(self):
  539. c = Commit.from_string(self.make_commit_text(encoding=b"UTF-8"))
  540. self.assertEqual(b"UTF-8", c.encoding)
  541. def test_check(self):
  542. self.assertCheckSucceeds(Commit, self.make_commit_text())
  543. self.assertCheckSucceeds(Commit, self.make_commit_text(parents=None))
  544. self.assertCheckSucceeds(Commit, self.make_commit_text(encoding=b"UTF-8"))
  545. self.assertCheckFails(Commit, self.make_commit_text(tree=b"xxx"))
  546. self.assertCheckFails(Commit, self.make_commit_text(parents=[a_sha, b"xxx"]))
  547. bad_committer = b"some guy without an email address 1174773719 +0000"
  548. self.assertCheckFails(Commit, self.make_commit_text(committer=bad_committer))
  549. self.assertCheckFails(Commit, self.make_commit_text(author=bad_committer))
  550. self.assertCheckFails(Commit, self.make_commit_text(author=None))
  551. self.assertCheckFails(Commit, self.make_commit_text(committer=None))
  552. self.assertCheckFails(
  553. Commit, self.make_commit_text(author=None, committer=None)
  554. )
  555. def test_check_duplicates(self):
  556. # duplicate each of the header fields
  557. for i in range(5):
  558. lines = self.make_commit_lines(parents=[a_sha], encoding=b"UTF-8")
  559. lines.insert(i, lines[i])
  560. text = b"\n".join(lines)
  561. if lines[i].startswith(b"parent"):
  562. # duplicate parents are ok for now
  563. self.assertCheckSucceeds(Commit, text)
  564. else:
  565. self.assertCheckFails(Commit, text)
  566. def test_check_order(self):
  567. lines = self.make_commit_lines(parents=[a_sha], encoding=b"UTF-8")
  568. headers = lines[:5]
  569. rest = lines[5:]
  570. # of all possible permutations, ensure only the original succeeds
  571. for perm in permutations(headers):
  572. perm = list(perm)
  573. text = b"\n".join(perm + rest)
  574. if perm == headers:
  575. self.assertCheckSucceeds(Commit, text)
  576. else:
  577. self.assertCheckFails(Commit, text)
  578. def test_check_commit_with_unparseable_time(self):
  579. identity_with_wrong_time = (
  580. b"Igor Sysoev <igor@sysoev.ru> 18446743887488505614+42707004"
  581. )
  582. # Those fail at reading time
  583. self.assertCheckFails(
  584. Commit,
  585. self.make_commit_text(
  586. author=default_committer, committer=identity_with_wrong_time
  587. ),
  588. )
  589. self.assertCheckFails(
  590. Commit,
  591. self.make_commit_text(
  592. author=identity_with_wrong_time, committer=default_committer
  593. ),
  594. )
  595. def test_check_commit_with_overflow_date(self):
  596. """Date with overflow should raise an ObjectFormatException when checked."""
  597. identity_with_wrong_time = (
  598. b"Igor Sysoev <igor@sysoev.ru> 18446743887488505614 +42707004"
  599. )
  600. commit0 = Commit.from_string(
  601. self.make_commit_text(
  602. author=identity_with_wrong_time, committer=default_committer
  603. )
  604. )
  605. commit1 = Commit.from_string(
  606. self.make_commit_text(
  607. author=default_committer, committer=identity_with_wrong_time
  608. )
  609. )
  610. # Those fails when triggering the check() method
  611. for commit in [commit0, commit1]:
  612. with self.assertRaises(ObjectFormatException):
  613. commit.check()
  614. def test_mangled_author_line(self):
  615. """Mangled author line should successfully parse."""
  616. author_line = (
  617. b'Karl MacMillan <kmacmill@redhat.com> <"Karl MacMillan '
  618. b'<kmacmill@redhat.com>"> 1197475547 -0500'
  619. )
  620. expected_identity = (
  621. b'Karl MacMillan <kmacmill@redhat.com> <"Karl MacMillan '
  622. b'<kmacmill@redhat.com>">'
  623. )
  624. commit = Commit.from_string(self.make_commit_text(author=author_line))
  625. # The commit parses properly
  626. self.assertEqual(commit.author, expected_identity)
  627. # But the check fails because the author identity is bogus
  628. with self.assertRaises(ObjectFormatException):
  629. commit.check()
  630. def test_parse_gpgsig(self):
  631. c = Commit.from_string(
  632. b"""tree aaff74984cccd156a469afa7d9ab10e4777beb24
  633. author Jelmer Vernooij <jelmer@samba.org> 1412179807 +0200
  634. committer Jelmer Vernooij <jelmer@samba.org> 1412179807 +0200
  635. gpgsig -----BEGIN PGP SIGNATURE-----
  636. Version: GnuPG v1
  637. iQIcBAABCgAGBQJULCdfAAoJEACAbyvXKaRXuKwP/RyP9PA49uAvu8tQVCC/uBa8
  638. vi975+xvO14R8Pp8k2nps7lSxCdtCd+xVT1VRHs0wNhOZo2YCVoU1HATkPejqSeV
  639. NScTHcxnk4/+bxyfk14xvJkNp7FlQ3npmBkA+lbV0Ubr33rvtIE5jiJPyz+SgWAg
  640. xdBG2TojV0squj00GoH/euK6aX7GgZtwdtpTv44haCQdSuPGDcI4TORqR6YSqvy3
  641. GPE+3ZqXPFFb+KILtimkxitdwB7CpwmNse2vE3rONSwTvi8nq3ZoQYNY73CQGkUy
  642. qoFU0pDtw87U3niFin1ZccDgH0bB6624sLViqrjcbYJeg815Htsu4rmzVaZADEVC
  643. XhIO4MThebusdk0AcNGjgpf3HRHk0DPMDDlIjm+Oao0cqovvF6VyYmcb0C+RmhJj
  644. dodLXMNmbqErwTk3zEkW0yZvNIYXH7m9SokPCZa4eeIM7be62X6h1mbt0/IU6Th+
  645. v18fS0iTMP/Viug5und+05C/v04kgDo0CPphAbXwWMnkE4B6Tl9sdyUYXtvQsL7x
  646. 0+WP1gL27ANqNZiI07Kz/BhbBAQI/+2TFT7oGr0AnFPQ5jHp+3GpUf6OKuT1wT3H
  647. ND189UFuRuubxb42vZhpcXRbqJVWnbECTKVUPsGZqat3enQUB63uM4i6/RdONDZA
  648. fDeF1m4qYs+cUXKNUZ03
  649. =X6RT
  650. -----END PGP SIGNATURE-----
  651. foo
  652. """
  653. )
  654. self.assertEqual(b"foo\n", c.message)
  655. self.assertEqual([], c._extra)
  656. self.assertEqual(
  657. b"""-----BEGIN PGP SIGNATURE-----
  658. Version: GnuPG v1
  659. iQIcBAABCgAGBQJULCdfAAoJEACAbyvXKaRXuKwP/RyP9PA49uAvu8tQVCC/uBa8
  660. vi975+xvO14R8Pp8k2nps7lSxCdtCd+xVT1VRHs0wNhOZo2YCVoU1HATkPejqSeV
  661. NScTHcxnk4/+bxyfk14xvJkNp7FlQ3npmBkA+lbV0Ubr33rvtIE5jiJPyz+SgWAg
  662. xdBG2TojV0squj00GoH/euK6aX7GgZtwdtpTv44haCQdSuPGDcI4TORqR6YSqvy3
  663. GPE+3ZqXPFFb+KILtimkxitdwB7CpwmNse2vE3rONSwTvi8nq3ZoQYNY73CQGkUy
  664. qoFU0pDtw87U3niFin1ZccDgH0bB6624sLViqrjcbYJeg815Htsu4rmzVaZADEVC
  665. XhIO4MThebusdk0AcNGjgpf3HRHk0DPMDDlIjm+Oao0cqovvF6VyYmcb0C+RmhJj
  666. dodLXMNmbqErwTk3zEkW0yZvNIYXH7m9SokPCZa4eeIM7be62X6h1mbt0/IU6Th+
  667. v18fS0iTMP/Viug5und+05C/v04kgDo0CPphAbXwWMnkE4B6Tl9sdyUYXtvQsL7x
  668. 0+WP1gL27ANqNZiI07Kz/BhbBAQI/+2TFT7oGr0AnFPQ5jHp+3GpUf6OKuT1wT3H
  669. ND189UFuRuubxb42vZhpcXRbqJVWnbECTKVUPsGZqat3enQUB63uM4i6/RdONDZA
  670. fDeF1m4qYs+cUXKNUZ03
  671. =X6RT
  672. -----END PGP SIGNATURE-----""",
  673. c.gpgsig,
  674. )
  675. def test_parse_header_trailing_newline(self):
  676. c = Commit.from_string(
  677. b"""\
  678. tree a7d6277f78d3ecd0230a1a5df6db00b1d9c521ac
  679. parent c09b6dec7a73760fbdb478383a3c926b18db8bbe
  680. author Neil Matatall <oreoshake@github.com> 1461964057 -1000
  681. committer Neil Matatall <oreoshake@github.com> 1461964057 -1000
  682. gpgsig -----BEGIN PGP SIGNATURE-----
  683. wsBcBAABCAAQBQJXI80ZCRA6pcNDcVZ70gAAarcIABs72xRX3FWeox349nh6ucJK
  684. CtwmBTusez2Zwmq895fQEbZK7jpaGO5TRO4OvjFxlRo0E08UFx3pxZHSpj6bsFeL
  685. hHsDXnCaotphLkbgKKRdGZo7tDqM84wuEDlh4MwNe7qlFC7bYLDyysc81ZX5lpMm
  686. 2MFF1TvjLAzSvkT7H1LPkuR3hSvfCYhikbPOUNnKOo0sYjeJeAJ/JdAVQ4mdJIM0
  687. gl3REp9+A+qBEpNQI7z94Pg5Bc5xenwuDh3SJgHvJV6zBWupWcdB3fAkVd4TPnEZ
  688. nHxksHfeNln9RKseIDcy4b2ATjhDNIJZARHNfr6oy4u3XPW4svRqtBsLoMiIeuI=
  689. =ms6q
  690. -----END PGP SIGNATURE-----
  691. 3.3.0 version bump and docs
  692. """
  693. )
  694. self.assertEqual([], c._extra)
  695. self.assertEqual(
  696. b"""\
  697. -----BEGIN PGP SIGNATURE-----
  698. wsBcBAABCAAQBQJXI80ZCRA6pcNDcVZ70gAAarcIABs72xRX3FWeox349nh6ucJK
  699. CtwmBTusez2Zwmq895fQEbZK7jpaGO5TRO4OvjFxlRo0E08UFx3pxZHSpj6bsFeL
  700. hHsDXnCaotphLkbgKKRdGZo7tDqM84wuEDlh4MwNe7qlFC7bYLDyysc81ZX5lpMm
  701. 2MFF1TvjLAzSvkT7H1LPkuR3hSvfCYhikbPOUNnKOo0sYjeJeAJ/JdAVQ4mdJIM0
  702. gl3REp9+A+qBEpNQI7z94Pg5Bc5xenwuDh3SJgHvJV6zBWupWcdB3fAkVd4TPnEZ
  703. nHxksHfeNln9RKseIDcy4b2ATjhDNIJZARHNfr6oy4u3XPW4svRqtBsLoMiIeuI=
  704. =ms6q
  705. -----END PGP SIGNATURE-----\n""",
  706. c.gpgsig,
  707. )
  708. _TREE_ITEMS = {
  709. b"a.c": (0o100755, b"d80c186a03f423a81b39df39dc87fd269736ca86"),
  710. b"a": (stat.S_IFDIR, b"d80c186a03f423a81b39df39dc87fd269736ca86"),
  711. b"a/c": (stat.S_IFDIR, b"d80c186a03f423a81b39df39dc87fd269736ca86"),
  712. }
  713. _SORTED_TREE_ITEMS = [
  714. TreeEntry(b"a.c", 0o100755, b"d80c186a03f423a81b39df39dc87fd269736ca86"),
  715. TreeEntry(b"a", stat.S_IFDIR, b"d80c186a03f423a81b39df39dc87fd269736ca86"),
  716. TreeEntry(b"a/c", stat.S_IFDIR, b"d80c186a03f423a81b39df39dc87fd269736ca86"),
  717. ]
  718. class TreeTests(ShaFileCheckTests):
  719. def test_add(self):
  720. myhexsha = b"d80c186a03f423a81b39df39dc87fd269736ca86"
  721. x = Tree()
  722. x.add(b"myname", 0o100755, myhexsha)
  723. self.assertEqual(x[b"myname"], (0o100755, myhexsha))
  724. self.assertEqual(b"100755 myname\0" + hex_to_sha(myhexsha), x.as_raw_string())
  725. def test_simple(self):
  726. myhexsha = b"d80c186a03f423a81b39df39dc87fd269736ca86"
  727. x = Tree()
  728. x[b"myname"] = (0o100755, myhexsha)
  729. self.assertEqual(b"100755 myname\0" + hex_to_sha(myhexsha), x.as_raw_string())
  730. self.assertEqual(b"100755 myname\0" + hex_to_sha(myhexsha), bytes(x))
  731. def test_tree_update_id(self):
  732. x = Tree()
  733. x[b"a.c"] = (0o100755, b"d80c186a03f423a81b39df39dc87fd269736ca86")
  734. self.assertEqual(b"0c5c6bc2c081accfbc250331b19e43b904ab9cdd", x.id)
  735. x[b"a.b"] = (stat.S_IFDIR, b"d80c186a03f423a81b39df39dc87fd269736ca86")
  736. self.assertEqual(b"07bfcb5f3ada15bbebdfa3bbb8fd858a363925c8", x.id)
  737. def test_tree_iteritems_dir_sort(self):
  738. x = Tree()
  739. for name, item in _TREE_ITEMS.items():
  740. x[name] = item
  741. self.assertEqual(_SORTED_TREE_ITEMS, x.items())
  742. def test_tree_items_dir_sort(self):
  743. x = Tree()
  744. for name, item in _TREE_ITEMS.items():
  745. x[name] = item
  746. self.assertEqual(_SORTED_TREE_ITEMS, x.items())
  747. def _do_test_parse_tree(self, parse_tree):
  748. dir = os.path.join(os.path.dirname(__file__), "..", "testdata", "trees")
  749. o = Tree.from_path(hex_to_filename(dir, tree_sha))
  750. self.assertEqual(
  751. [(b"a", 0o100644, a_sha), (b"b", 0o100644, b_sha)],
  752. list(parse_tree(o.as_raw_string())),
  753. )
  754. # test a broken tree that has a leading 0 on the file mode
  755. broken_tree = b"0100644 foo\0" + hex_to_sha(a_sha)
  756. def eval_parse_tree(*args, **kwargs):
  757. return list(parse_tree(*args, **kwargs))
  758. self.assertEqual([(b"foo", 0o100644, a_sha)], eval_parse_tree(broken_tree))
  759. self.assertRaises(
  760. ObjectFormatException, eval_parse_tree, broken_tree, strict=True
  761. )
  762. test_parse_tree = functest_builder(_do_test_parse_tree, _parse_tree_py)
  763. test_parse_tree_extension = ext_functest_builder(_do_test_parse_tree, parse_tree)
  764. def _do_test_sorted_tree_items(self, sorted_tree_items):
  765. def do_sort(entries):
  766. return list(sorted_tree_items(entries, False))
  767. actual = do_sort(_TREE_ITEMS)
  768. self.assertEqual(_SORTED_TREE_ITEMS, actual)
  769. self.assertIsInstance(actual[0], TreeEntry)
  770. # C/Python implementations may differ in specific error types, but
  771. # should all error on invalid inputs.
  772. # For example, the Rust implementation has stricter type checks, so may
  773. # raise TypeError where the Python implementation raises
  774. # AttributeError.
  775. errors = (TypeError, ValueError, AttributeError)
  776. self.assertRaises(errors, do_sort, b"foo")
  777. self.assertRaises(errors, do_sort, {b"foo": (1, 2, 3)})
  778. myhexsha = b"d80c186a03f423a81b39df39dc87fd269736ca86"
  779. self.assertRaises(errors, do_sort, {b"foo": (b"xxx", myhexsha)})
  780. self.assertRaises(errors, do_sort, {b"foo": (0o100755, 12345)})
  781. test_sorted_tree_items = functest_builder(
  782. _do_test_sorted_tree_items, _sorted_tree_items_py
  783. )
  784. test_sorted_tree_items_extension = ext_functest_builder(
  785. _do_test_sorted_tree_items, sorted_tree_items
  786. )
  787. def _do_test_sorted_tree_items_name_order(self, sorted_tree_items):
  788. self.assertEqual(
  789. [
  790. TreeEntry(
  791. b"a",
  792. stat.S_IFDIR,
  793. b"d80c186a03f423a81b39df39dc87fd269736ca86",
  794. ),
  795. TreeEntry(
  796. b"a.c",
  797. 0o100755,
  798. b"d80c186a03f423a81b39df39dc87fd269736ca86",
  799. ),
  800. TreeEntry(
  801. b"a/c",
  802. stat.S_IFDIR,
  803. b"d80c186a03f423a81b39df39dc87fd269736ca86",
  804. ),
  805. ],
  806. list(sorted_tree_items(_TREE_ITEMS, True)),
  807. )
  808. test_sorted_tree_items_name_order = functest_builder(
  809. _do_test_sorted_tree_items_name_order, _sorted_tree_items_py
  810. )
  811. test_sorted_tree_items_name_order_extension = ext_functest_builder(
  812. _do_test_sorted_tree_items_name_order, sorted_tree_items
  813. )
  814. def test_check(self):
  815. t = Tree
  816. sha = hex_to_sha(a_sha)
  817. # filenames
  818. self.assertCheckSucceeds(t, b"100644 .a\0" + sha)
  819. self.assertCheckFails(t, b"100644 \0" + sha)
  820. self.assertCheckFails(t, b"100644 .\0" + sha)
  821. self.assertCheckFails(t, b"100644 a/a\0" + sha)
  822. self.assertCheckFails(t, b"100644 ..\0" + sha)
  823. self.assertCheckFails(t, b"100644 .git\0" + sha)
  824. # modes
  825. self.assertCheckSucceeds(t, b"100644 a\0" + sha)
  826. self.assertCheckSucceeds(t, b"100755 a\0" + sha)
  827. self.assertCheckSucceeds(t, b"160000 a\0" + sha)
  828. # TODO more whitelisted modes
  829. self.assertCheckFails(t, b"123456 a\0" + sha)
  830. self.assertCheckFails(t, b"123abc a\0" + sha)
  831. # should fail check, but parses ok
  832. self.assertCheckFails(t, b"0100644 foo\0" + sha)
  833. # shas
  834. self.assertCheckFails(t, b"100644 a\0" + (b"x" * 5))
  835. self.assertCheckFails(t, b"100644 a\0" + (b"x" * 18) + b"\0")
  836. self.assertCheckFails(t, b"100644 a\0" + (b"x" * 21) + b"\n100644 b\0" + sha)
  837. # ordering
  838. sha2 = hex_to_sha(b_sha)
  839. self.assertCheckSucceeds(t, b"100644 a\0" + sha + b"100644 b\0" + sha)
  840. self.assertCheckSucceeds(t, b"100644 a\0" + sha + b"100644 b\0" + sha2)
  841. self.assertCheckFails(t, b"100644 a\0" + sha + b"100755 a\0" + sha2)
  842. self.assertCheckFails(t, b"100644 b\0" + sha2 + b"100644 a\0" + sha)
  843. def test_iter(self):
  844. t = Tree()
  845. t[b"foo"] = (0o100644, a_sha)
  846. self.assertEqual({b"foo"}, set(t))
  847. class TagSerializeTests(TestCase):
  848. def test_serialize_simple(self):
  849. x = make_object(
  850. Tag,
  851. tagger=b"Jelmer Vernooij <jelmer@samba.org>",
  852. name=b"0.1",
  853. message=b"Tag 0.1",
  854. object=(Blob, b"d80c186a03f423a81b39df39dc87fd269736ca86"),
  855. tag_time=423423423,
  856. tag_timezone=0,
  857. )
  858. self.assertEqual(
  859. (
  860. b"object d80c186a03f423a81b39df39dc87fd269736ca86\n"
  861. b"type blob\n"
  862. b"tag 0.1\n"
  863. b"tagger Jelmer Vernooij <jelmer@samba.org> "
  864. b"423423423 +0000\n"
  865. b"\n"
  866. b"Tag 0.1"
  867. ),
  868. x.as_raw_string(),
  869. )
  870. def test_serialize_none_message(self):
  871. x = make_object(
  872. Tag,
  873. tagger=b"Jelmer Vernooij <jelmer@samba.org>",
  874. name=b"0.1",
  875. message=None,
  876. object=(Blob, b"d80c186a03f423a81b39df39dc87fd269736ca86"),
  877. tag_time=423423423,
  878. tag_timezone=0,
  879. )
  880. self.assertEqual(
  881. (
  882. b"object d80c186a03f423a81b39df39dc87fd269736ca86\n"
  883. b"type blob\n"
  884. b"tag 0.1\n"
  885. b"tagger Jelmer Vernooij <jelmer@samba.org> "
  886. b"423423423 +0000\n"
  887. ),
  888. x.as_raw_string(),
  889. )
  890. default_tagger = (
  891. b"Linus Torvalds <torvalds@woody.linux-foundation.org> " b"1183319674 -0700"
  892. )
  893. default_message = b"""Linux 2.6.22-rc7
  894. -----BEGIN PGP SIGNATURE-----
  895. Version: GnuPG v1.4.7 (GNU/Linux)
  896. iD8DBQBGiAaAF3YsRnbiHLsRAitMAKCiLboJkQECM/jpYsY3WPfvUgLXkACgg3ql
  897. OK2XeQOiEeXtT76rV4t2WR4=
  898. =ivrA
  899. -----END PGP SIGNATURE-----
  900. """
  901. class TagParseTests(ShaFileCheckTests):
  902. def make_tag_lines(
  903. self,
  904. object_sha=b"a38d6181ff27824c79fc7df825164a212eff6a3f",
  905. object_type_name=b"commit",
  906. name=b"v2.6.22-rc7",
  907. tagger=default_tagger,
  908. message=default_message,
  909. ):
  910. lines = []
  911. if object_sha is not None:
  912. lines.append(b"object " + object_sha)
  913. if object_type_name is not None:
  914. lines.append(b"type " + object_type_name)
  915. if name is not None:
  916. lines.append(b"tag " + name)
  917. if tagger is not None:
  918. lines.append(b"tagger " + tagger)
  919. if message is not None:
  920. lines.append(b"")
  921. lines.append(message)
  922. return lines
  923. def make_tag_text(self, **kwargs):
  924. return b"\n".join(self.make_tag_lines(**kwargs))
  925. def test_parse(self):
  926. x = Tag()
  927. x.set_raw_string(self.make_tag_text())
  928. self.assertEqual(
  929. b"Linus Torvalds <torvalds@woody.linux-foundation.org>", x.tagger
  930. )
  931. self.assertEqual(b"v2.6.22-rc7", x.name)
  932. object_type, object_sha = x.object
  933. self.assertEqual(b"a38d6181ff27824c79fc7df825164a212eff6a3f", object_sha)
  934. self.assertEqual(Commit, object_type)
  935. self.assertEqual(
  936. datetime.datetime.utcfromtimestamp(x.tag_time),
  937. datetime.datetime(2007, 7, 1, 19, 54, 34),
  938. )
  939. self.assertEqual(-25200, x.tag_timezone)
  940. def test_parse_no_tagger(self):
  941. x = Tag()
  942. x.set_raw_string(self.make_tag_text(tagger=None))
  943. self.assertEqual(None, x.tagger)
  944. self.assertEqual(b"v2.6.22-rc7", x.name)
  945. self.assertEqual(None, x.tag_time)
  946. def test_parse_no_message(self):
  947. x = Tag()
  948. x.set_raw_string(self.make_tag_text(message=None))
  949. self.assertEqual(None, x.message)
  950. self.assertEqual(
  951. b"Linus Torvalds <torvalds@woody.linux-foundation.org>", x.tagger
  952. )
  953. self.assertEqual(
  954. datetime.datetime.utcfromtimestamp(x.tag_time),
  955. datetime.datetime(2007, 7, 1, 19, 54, 34),
  956. )
  957. self.assertEqual(-25200, x.tag_timezone)
  958. self.assertEqual(b"v2.6.22-rc7", x.name)
  959. def test_check(self):
  960. self.assertCheckSucceeds(Tag, self.make_tag_text())
  961. self.assertCheckFails(Tag, self.make_tag_text(object_sha=None))
  962. self.assertCheckFails(Tag, self.make_tag_text(object_type_name=None))
  963. self.assertCheckFails(Tag, self.make_tag_text(name=None))
  964. self.assertCheckFails(Tag, self.make_tag_text(name=b""))
  965. self.assertCheckFails(Tag, self.make_tag_text(object_type_name=b"foobar"))
  966. self.assertCheckFails(
  967. Tag,
  968. self.make_tag_text(
  969. tagger=b"some guy without an email address 1183319674 -0700"
  970. ),
  971. )
  972. self.assertCheckFails(
  973. Tag,
  974. self.make_tag_text(
  975. tagger=(
  976. b"Linus Torvalds <torvalds@woody.linux-foundation.org> "
  977. b"Sun 7 Jul 2007 12:54:34 +0700"
  978. )
  979. ),
  980. )
  981. self.assertCheckFails(Tag, self.make_tag_text(object_sha=b"xxx"))
  982. def test_check_tag_with_unparseable_field(self):
  983. self.assertCheckFails(
  984. Tag,
  985. self.make_tag_text(
  986. tagger=(
  987. b"Linus Torvalds <torvalds@woody.linux-foundation.org> "
  988. b"423423+0000"
  989. )
  990. ),
  991. )
  992. def test_check_tag_with_overflow_time(self):
  993. """Date with overflow should raise an ObjectFormatException when checked."""
  994. author = f"Some Dude <some@dude.org> {MAX_TIME + 1} +0000"
  995. tag = Tag.from_string(self.make_tag_text(tagger=(author.encode())))
  996. with self.assertRaises(ObjectFormatException):
  997. tag.check()
  998. def test_check_duplicates(self):
  999. # duplicate each of the header fields
  1000. for i in range(4):
  1001. lines = self.make_tag_lines()
  1002. lines.insert(i, lines[i])
  1003. self.assertCheckFails(Tag, b"\n".join(lines))
  1004. def test_check_order(self):
  1005. lines = self.make_tag_lines()
  1006. headers = lines[:4]
  1007. rest = lines[4:]
  1008. # of all possible permutations, ensure only the original succeeds
  1009. for perm in permutations(headers):
  1010. perm = list(perm)
  1011. text = b"\n".join(perm + rest)
  1012. if perm == headers:
  1013. self.assertCheckSucceeds(Tag, text)
  1014. else:
  1015. self.assertCheckFails(Tag, text)
  1016. def test_tree_copy_after_update(self):
  1017. """Check Tree.id is correctly updated when the tree is copied after updated."""
  1018. shas = []
  1019. tree = Tree()
  1020. shas.append(tree.id)
  1021. tree.add(b"data", 0o644, Blob().id)
  1022. copied = tree.copy()
  1023. shas.append(tree.id)
  1024. shas.append(copied.id)
  1025. self.assertNotIn(shas[0], shas[1:])
  1026. self.assertEqual(shas[1], shas[2])
  1027. class CheckTests(TestCase):
  1028. def test_check_hexsha(self):
  1029. check_hexsha(a_sha, "failed to check good sha")
  1030. self.assertRaises(
  1031. ObjectFormatException, check_hexsha, b"1" * 39, "sha too short"
  1032. )
  1033. self.assertRaises(
  1034. ObjectFormatException, check_hexsha, b"1" * 41, "sha too long"
  1035. )
  1036. self.assertRaises(
  1037. ObjectFormatException,
  1038. check_hexsha,
  1039. b"x" * 40,
  1040. "invalid characters",
  1041. )
  1042. def test_check_identity(self):
  1043. check_identity(
  1044. b"Dave Borowitz <dborowitz@google.com>",
  1045. "failed to check good identity",
  1046. )
  1047. check_identity(b" <dborowitz@google.com>", "failed to check good identity")
  1048. self.assertRaises(
  1049. ObjectFormatException,
  1050. check_identity,
  1051. b"<dborowitz@google.com>",
  1052. "no space before email",
  1053. )
  1054. self.assertRaises(
  1055. ObjectFormatException, check_identity, b"Dave Borowitz", "no email"
  1056. )
  1057. self.assertRaises(
  1058. ObjectFormatException,
  1059. check_identity,
  1060. b"Dave Borowitz <dborowitz",
  1061. "incomplete email",
  1062. )
  1063. self.assertRaises(
  1064. ObjectFormatException,
  1065. check_identity,
  1066. b"dborowitz@google.com>",
  1067. "incomplete email",
  1068. )
  1069. self.assertRaises(
  1070. ObjectFormatException,
  1071. check_identity,
  1072. b"Dave Borowitz <<dborowitz@google.com>",
  1073. "typo",
  1074. )
  1075. self.assertRaises(
  1076. ObjectFormatException,
  1077. check_identity,
  1078. b"Dave Borowitz <dborowitz@google.com>>",
  1079. "typo",
  1080. )
  1081. self.assertRaises(
  1082. ObjectFormatException,
  1083. check_identity,
  1084. b"Dave Borowitz <dborowitz@google.com>xxx",
  1085. "trailing characters",
  1086. )
  1087. self.assertRaises(
  1088. ObjectFormatException,
  1089. check_identity,
  1090. b"Dave Borowitz <dborowitz@google.com>xxx",
  1091. "trailing characters",
  1092. )
  1093. self.assertRaises(
  1094. ObjectFormatException,
  1095. check_identity,
  1096. b"Dave<Borowitz <dborowitz@google.com>",
  1097. "reserved byte in name",
  1098. )
  1099. self.assertRaises(
  1100. ObjectFormatException,
  1101. check_identity,
  1102. b"Dave>Borowitz <dborowitz@google.com>",
  1103. "reserved byte in name",
  1104. )
  1105. self.assertRaises(
  1106. ObjectFormatException,
  1107. check_identity,
  1108. b"Dave\0Borowitz <dborowitz@google.com>",
  1109. "null byte",
  1110. )
  1111. self.assertRaises(
  1112. ObjectFormatException,
  1113. check_identity,
  1114. b"Dave\nBorowitz <dborowitz@google.com>",
  1115. "newline byte",
  1116. )
  1117. class TimezoneTests(TestCase):
  1118. def test_parse_timezone_utc(self):
  1119. self.assertEqual((0, False), parse_timezone(b"+0000"))
  1120. def test_parse_timezone_utc_negative(self):
  1121. self.assertEqual((0, True), parse_timezone(b"-0000"))
  1122. def test_generate_timezone_utc(self):
  1123. self.assertEqual(b"+0000", format_timezone(0))
  1124. def test_generate_timezone_utc_negative(self):
  1125. self.assertEqual(b"-0000", format_timezone(0, True))
  1126. def test_parse_timezone_cet(self):
  1127. self.assertEqual((60 * 60, False), parse_timezone(b"+0100"))
  1128. def test_format_timezone_cet(self):
  1129. self.assertEqual(b"+0100", format_timezone(60 * 60))
  1130. def test_format_timezone_pdt(self):
  1131. self.assertEqual(b"-0400", format_timezone(-4 * 60 * 60))
  1132. def test_parse_timezone_pdt(self):
  1133. self.assertEqual((-4 * 60 * 60, False), parse_timezone(b"-0400"))
  1134. def test_format_timezone_pdt_half(self):
  1135. self.assertEqual(b"-0440", format_timezone(int(((-4 * 60) - 40) * 60)))
  1136. def test_format_timezone_double_negative(self):
  1137. self.assertEqual(b"--700", format_timezone(int((7 * 60) * 60), True))
  1138. def test_parse_timezone_pdt_half(self):
  1139. self.assertEqual((((-4 * 60) - 40) * 60, False), parse_timezone(b"-0440"))
  1140. def test_parse_timezone_double_negative(self):
  1141. self.assertEqual((int((7 * 60) * 60), False), parse_timezone(b"+700"))
  1142. self.assertEqual((int((7 * 60) * 60), True), parse_timezone(b"--700"))
  1143. class ShaFileCopyTests(TestCase):
  1144. def assert_copy(self, orig):
  1145. oclass = object_class(orig.type_num)
  1146. copy = orig.copy()
  1147. self.assertIsInstance(copy, oclass)
  1148. self.assertEqual(copy, orig)
  1149. self.assertIsNot(copy, orig)
  1150. def test_commit_copy(self):
  1151. attrs = {
  1152. "tree": b"d80c186a03f423a81b39df39dc87fd269736ca86",
  1153. "parents": [
  1154. b"ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd",
  1155. b"4cffe90e0a41ad3f5190079d7c8f036bde29cbe6",
  1156. ],
  1157. "author": b"James Westby <jw+debian@jameswestby.net>",
  1158. "committer": b"James Westby <jw+debian@jameswestby.net>",
  1159. "commit_time": 1174773719,
  1160. "author_time": 1174773719,
  1161. "commit_timezone": 0,
  1162. "author_timezone": 0,
  1163. "message": b"Merge ../b\n",
  1164. }
  1165. commit = make_commit(**attrs)
  1166. self.assert_copy(commit)
  1167. def test_blob_copy(self):
  1168. blob = make_object(Blob, data=b"i am a blob")
  1169. self.assert_copy(blob)
  1170. def test_tree_copy(self):
  1171. blob = make_object(Blob, data=b"i am a blob")
  1172. tree = Tree()
  1173. tree[b"blob"] = (stat.S_IFREG, blob.id)
  1174. self.assert_copy(tree)
  1175. def test_tag_copy(self):
  1176. tag = make_object(
  1177. Tag,
  1178. name=b"tag",
  1179. message=b"",
  1180. tagger=b"Tagger <test@example.com>",
  1181. tag_time=12345,
  1182. tag_timezone=0,
  1183. object=(Commit, b"0" * 40),
  1184. )
  1185. self.assert_copy(tag)
  1186. class ShaFileSerializeTests(TestCase):
  1187. """`ShaFile` objects only gets serialized once if they haven't changed."""
  1188. @contextmanager
  1189. def assert_serialization_on_change(
  1190. self, obj, needs_serialization_after_change=True
  1191. ):
  1192. old_id = obj.id
  1193. self.assertFalse(obj._needs_serialization)
  1194. yield obj
  1195. if needs_serialization_after_change:
  1196. self.assertTrue(obj._needs_serialization)
  1197. else:
  1198. self.assertFalse(obj._needs_serialization)
  1199. new_id = obj.id
  1200. self.assertFalse(obj._needs_serialization)
  1201. self.assertNotEqual(old_id, new_id)
  1202. def test_commit_serialize(self):
  1203. attrs = {
  1204. "tree": b"d80c186a03f423a81b39df39dc87fd269736ca86",
  1205. "parents": [
  1206. b"ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd",
  1207. b"4cffe90e0a41ad3f5190079d7c8f036bde29cbe6",
  1208. ],
  1209. "author": b"James Westby <jw+debian@jameswestby.net>",
  1210. "committer": b"James Westby <jw+debian@jameswestby.net>",
  1211. "commit_time": 1174773719,
  1212. "author_time": 1174773719,
  1213. "commit_timezone": 0,
  1214. "author_timezone": 0,
  1215. "message": b"Merge ../b\n",
  1216. }
  1217. commit = make_commit(**attrs)
  1218. with self.assert_serialization_on_change(commit):
  1219. commit.parents = [b"ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd"]
  1220. def test_blob_serialize(self):
  1221. blob = make_object(Blob, data=b"i am a blob")
  1222. with self.assert_serialization_on_change(
  1223. blob, needs_serialization_after_change=False
  1224. ):
  1225. blob.data = b"i am another blob"
  1226. def test_tree_serialize(self):
  1227. blob = make_object(Blob, data=b"i am a blob")
  1228. tree = Tree()
  1229. tree[b"blob"] = (stat.S_IFREG, blob.id)
  1230. with self.assert_serialization_on_change(tree):
  1231. tree[b"blob2"] = (stat.S_IFREG, blob.id)
  1232. def test_tag_serialize(self):
  1233. tag = make_object(
  1234. Tag,
  1235. name=b"tag",
  1236. message=b"",
  1237. tagger=b"Tagger <test@example.com>",
  1238. tag_time=12345,
  1239. tag_timezone=0,
  1240. object=(Commit, b"0" * 40),
  1241. )
  1242. with self.assert_serialization_on_change(tag):
  1243. tag.message = b"new message"
  1244. def test_tag_serialize_time_error(self):
  1245. with self.assertRaises(ObjectFormatException):
  1246. tag = make_object(
  1247. Tag,
  1248. name=b"tag",
  1249. message=b"some message",
  1250. tagger=b"Tagger <test@example.com> 1174773719+0000",
  1251. object=(Commit, b"0" * 40),
  1252. )
  1253. tag._deserialize(tag._serialize())
  1254. class PrettyFormatTreeEntryTests(TestCase):
  1255. def test_format(self):
  1256. self.assertEqual(
  1257. "40000 tree 40820c38cfb182ce6c8b261555410d8382a5918b\tfoo\n",
  1258. pretty_format_tree_entry(
  1259. b"foo", 0o40000, b"40820c38cfb182ce6c8b261555410d8382a5918b"
  1260. ),
  1261. )